Mailing List archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[vdr] Re: caught signal 2



hm@seneca.muc.de wrote:

> I mean that for mainstream vdr... If (Interrupted) we should know "why", not
> just "that". 

OK, I asked fer it, so - here's a patch against 1.3.15, changing all
the signal() stuff to sigaction() in vdr.c. It appears to work as shown
(1455 is the master vdr thread):

 1455 pts/0    S      0:02 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1456 ?        SW     0:00 [kdvb-fe-0:0]
 1457 pts/0    S      0:00 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1458 pts/0    S      0:00 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1459 pts/0    S      0:02 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1460 pts/0    S      0:00 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1461 pts/0    S      0:00 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1462 pts/0    S      0:00 ./vdr -w 60 -l 3 -t /dev/tty8 -p 2001 -s /usr/local/b
 1463 ?        S      0:00 sshd: root@pts/6
 1464 ?        S      0:00 sshd: root@pts/6
 1466 pts/6    S      0:00 -bash
 1484 pts/6    R      0:00 ps ax

# Now we SIGINT it from another shell: 

linvdr:~ # kill -2 1455
linvdr:~ # echo $$
1466

and vdr says: 

Dec 17 23:56:58 linvdr vdr[1455]: caught signal 2 from 1466

(By the way it would be very nice if all the thread showed their names in
ps.)

Anyway, here's the patch. Feel free to comment, send virtual beer,
etc. 

--- vdr.c.O	2004-12-17 18:34:36.000000000 +0100
+++ vdr.c	2004-12-17 23:55:33.000000000 +0100
@@ -67,17 +67,22 @@
 #define EXIT(v) { ExitCode = (v); goto Exit; }
 
 static int Interrupted = 0;
+static pid_t Int_From = 0;
 
-static void SignalHandler(int signum)
+static void SignalHandler(int signum, siginfo_t *si, void *uc)
 {
+  struct sigaction sa;
   if (signum != SIGPIPE) {
      Interrupted = signum;
+     Int_From = si->si_pid;
      Interface->Interrupt();
      }
-  signal(signum, SignalHandler);
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_SIGINFO;
+  sigaction(signum, &sa, NULL);
 }
 
-static void Watchdog(int signum)
+static void Watchdog(int signum, siginfo_t *si, void *uc)
 {
   // Something terrible must have happened that prevented the 'alarm()' from
   // being called in time, so let's get out of here:
@@ -87,6 +92,7 @@
 
 int main(int argc, char *argv[])
 {
+  struct sigaction sa, oldsigact;
   // Check for UTF-8 and exit if present - asprintf() will fail if it encounters 8 bit ASCII codes
   char *LangEnv;
   if ((LangEnv = getenv("LANG"))    != NULL && strcasestr(LangEnv, "utf") ||
@@ -488,13 +494,64 @@
   Recordings.Load();
 
   // Signal handlers:
-
+/*
   if (signal(SIGHUP,  SignalHandler) == SIG_IGN) signal(SIGHUP,  SIG_IGN);
+*/
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_SIGINFO;
+  sigaction (SIGHUP, &sa, &oldsigact);
+  if (oldsigact.sa_handler == SIG_IGN) {
+    sa.sa_handler = SIG_IGN;
+    sa.sa_flags = 0;
+    sigaction (SIGHUP, &sa, NULL);
+  }
+/*
   if (signal(SIGINT,  SignalHandler) == SIG_IGN) signal(SIGINT,  SIG_IGN);
+*/
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_SIGINFO;
+  sigaction (SIGINT, &sa, &oldsigact);
+  if (oldsigact.sa_handler == SIG_IGN) {
+    sa.sa_handler = SIG_IGN;
+    sa.sa_flags = 0;
+    sigaction (SIGINT, &sa, NULL);
+  }
+/*
   if (signal(SIGTERM, SignalHandler) == SIG_IGN) signal(SIGTERM, SIG_IGN);
+*/
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_SIGINFO;
+  sigaction (SIGTERM, &sa, &oldsigact);
+  if (oldsigact.sa_handler == SIG_IGN) {
+    sa.sa_handler = SIG_IGN;
+    sa.sa_flags = 0;
+    sigaction (SIGTERM, &sa, NULL);
+  }
+/*
   if (signal(SIGPIPE, SignalHandler) == SIG_IGN) signal(SIGPIPE, SIG_IGN);
+*/
+  sa.sa_sigaction = SignalHandler;
+  sa.sa_flags = SA_SIGINFO;
+  sigaction (SIGPIPE, &sa, &oldsigact);
+  if (oldsigact.sa_handler == SIG_IGN) {
+    sa.sa_handler = SIG_IGN;
+    sa.sa_flags = 0;
+    sigaction (SIGPIPE, &sa, NULL);
+  }
   if (WatchdogTimeout > 0)
+/*
      if (signal(SIGALRM, Watchdog)   == SIG_IGN) signal(SIGALRM, SIG_IGN);
+*/
+  {
+    sa.sa_sigaction = Watchdog;
+    sa.sa_flags = SA_SIGINFO;
+    sigaction (SIGALRM, &sa, &oldsigact);
+    if (oldsigact.sa_handler == SIG_IGN) {
+      sa.sa_handler = SIG_IGN;
+      sa.sa_flags = 0;
+      sigaction (SIGALRM, &sa, NULL);
+    }
+  }
 
   // Watchdog:
 
@@ -857,8 +914,11 @@
                     ForceShutdown = false;
                     if (timer)
                        dsyslog("next timer event at %s", ctime(&Next));
-                    if (WatchdogTimeout > 0)
-                       signal(SIGALRM, SIG_IGN);
+                    if (WatchdogTimeout > 0) {
+                       sa.sa_handler = SIG_IGN;
+                       sa.sa_flags = 0;
+                       sigaction(SIGALRM, &sa, NULL);
+                    }
                     if (Interface->Confirm(tr("Press any key to cancel shutdown"), UserShutdown ? 5 : SHUTDOWNWAIT, true)) {
                        int Channel = timer ? timer->Channel()->Number() : 0;
                        const char *File = timer ? timer->File() : "";
@@ -868,12 +928,23 @@
                        isyslog("executing '%s'", cmd);
                        SystemExec(cmd);
                        free(cmd);
-                       }
+                    }
                     else if (WatchdogTimeout > 0) {
                        alarm(WatchdogTimeout);
+/*
                        if (signal(SIGALRM, Watchdog) == SIG_IGN)
                           signal(SIGALRM, SIG_IGN);
+*/
+                       sa.sa_sigaction = Watchdog;
+                       sa.sa_flags = SA_SIGINFO;
+                       sigaction (SIGALRM, &sa, &oldsigact);
+                       if (oldsigact.sa_handler == SIG_IGN) {
+                         sa.sa_handler = SIG_IGN;
+                         sa.sa_flags = 0;
+                         sigaction (SIGALRM, &sa, NULL);
                        }
+                    }
+                       
                     LastActivity = time(NULL); // don't try again too soon
                     UserShutdown = false;
                     continue; // skip the rest of the housekeeping for now
@@ -888,7 +959,7 @@
            }
         }
   if (Interrupted)
-     isyslog("caught signal %d", Interrupted);
+     isyslog("caught signal %d from %d", Interrupted, Int_From);
 
 Exit:
 


-- 
You need no longer worry about the future.  This time tomorrow you'll
be dead.




Home | Main Index | Thread Index