Patch #1: Kill process before opening Devices

Message ID 200509261436.37129.lists@magoa.net
State New
Headers

Commit Message

Sascha Volkenandt Sept. 26, 2005, 12:36 p.m. UTC
  Hi list,

I'll be releasing this and a few other patches for VDR 1.3.33, which have 
evolved during the last months while working on XeatreOS (a distribution 
based on LinVDR 0.7, which is used for the Xeatre boxes, 
http://www.xeatre.tv/). Some of their functionality may already be included 
in plugins or other patches. Nevertheless, I'd like to release them now, and 
let everybody decide whether they are useful or not.


This first one adds a commandline parameter '-k' to VDR. Using this, you can 
specify an arbitrary PID which will be killed just before VDR opens the DVB 
devices. This way the gap between a possible boot-up screen/movie and Live-TV 
gets shorter.

Greetings,
Sascha Volkenandt
  

Patch

diff -Nru vdr-1.3.33.origt/thread.c vdr-1.3.33/thread.c
--- vdr-1.3.33.origt/thread.c	Sun Aug 14 13:15:42 2005
+++ vdr-1.3.33/thread.c	Mon Sep 26 14:19:23 2005
@@ -14,6 +14,7 @@ 
 #include <sys/resource.h>
 #include <sys/time.h>
 #include <sys/wait.h>
+#include <signal.h>
 #include <unistd.h>
 #include "tools.h"
 
@@ -499,5 +500,20 @@ 
         }
      _exit(0);
      }
+}
+
+// --- KillProcess -----------------------------------------------------------
+
+void KillProcess(int Pid, int TimeoutMs)
+{
+  kill(Pid, SIGTERM);
+  while (TimeoutMs > 0) {
+        if (waitpid(Pid, NULL, WNOHANG) == Pid)
+           return;
+        cCondWait::SleepMs(10);
+        TimeoutMs -= 10;
+        }
+  kill(Pid, SIGKILL);
+  waitpid(Pid, NULL, 0);
 }
 
diff -Nru vdr-1.3.33.origt/thread.h vdr-1.3.33/thread.h
--- vdr-1.3.33.origt/thread.h	Sun Aug 14 13:21:48 2005
+++ vdr-1.3.33/thread.h	Mon Sep 26 14:19:23 2005
@@ -167,5 +167,6 @@ 
 // descriptors in the child process.
 
 int SystemExec(const char *Command);
+void KillProcess(int Pid, int TimeoutMs = 0);
 
 #endif //__THREAD_H
diff -Nru vdr-1.3.33.origt/vdr.c vdr-1.3.33/vdr.c
--- vdr-1.3.33.origt/vdr.c	Sat Sep 24 15:27:26 2005
+++ vdr-1.3.33/vdr.c	Mon Sep 26 14:20:02 2005
@@ -137,7 +137,8 @@ 
 
   cPluginManager PluginManager(DEFAULTPLUGINDIR);
   int ExitCode = 0;
-
+  int PidToKill = 0;
+  
   static struct option long_options[] = {
       { "audio",    required_argument, NULL, 'a' },
       { "config",   required_argument, NULL, 'c' },
@@ -145,6 +146,7 @@ 
       { "device",   required_argument, NULL, 'D' },
       { "epgfile",  required_argument, NULL, 'E' },
       { "help",     no_argument,       NULL, 'h' },
+      { "kill",     required_argument, NULL, 'k' },
       { "lib",      required_argument, NULL, 'L' },
       { "lirc",     optional_argument, NULL, 'l' | 0x100 },
       { "log",      required_argument, NULL, 'l' },
@@ -164,7 +166,7 @@ 
     };
 
   int c;
-  while ((c = getopt_long(argc, argv, "a:c:dD:E:hl:L:mp:P:r:s:t:v:Vw:", long_options, NULL)) != -1) {
+  while ((c = getopt_long(argc, argv, "a:c:dD:E:hk:l:L:mp:P:r:s:t:v:Vw:", long_options, NULL)) != -1) {
         switch (c) {
           case 'a': AudioCommand = optarg;
                     break;
@@ -185,6 +187,8 @@ 
                     break;
           case 'h': DisplayHelp = true;
                     break;
+          case 'k': PidToKill = atoi(optarg);
+                    break;
           case 'l': {
                       char *p = strchr(optarg, '.');
                       if (p)
@@ -291,6 +295,7 @@ 
                "                           if FILE is a directory, the default EPG file will be\n"
                "                           created in that directory\n"
                "  -h,       --help         print this help and exit\n"
+               "  -k PID,   --kill=PID     kill a process before initializing plugins and DVB\n"
                "  -l LEVEL, --log=LEVEL    set log level (default: 3)\n"
                "                           0 = no logging, 1 = errors only,\n"
                "                           2 = errors and info, 3 = errors, info and debug\n"
@@ -458,6 +463,11 @@ 
      cSchedules::Read();
      }
 
+  // Kill a process:
+
+  if (PidToKill != 0)
+     KillProcess(PidToKill, 1000);
+  
   // DVB interfaces:
 
   cDvbDevice::Initialize();