Cleanup signal handling code
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 26 Jan 2014 12:00:27 +0000 (12:00 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sun, 26 Jan 2014 17:04:21 +0000 (17:04 +0000)
src/include/libradius.h
src/lib/misc.c
src/main/exec.c
src/main/radiusd.c
src/main/util.c

index 4c3d59a..35e02cd 100644 (file)
@@ -87,6 +87,7 @@ RCSIDH(libradius_h, "$Id$")
 #include <stdlib.h>
 #include <stdarg.h>
 #include <stdbool.h>
+#include <signal.h>
 
 #include <freeradius-devel/threads.h>
 #include <freeradius-devel/radius.h>
index 69e329e..c3ba8ed 100644 (file)
@@ -62,8 +62,11 @@ int fr_set_signal(int sig, sig_t func)
 {
 #ifdef HAVE_SIGACTION
        struct sigaction act;
-       act.sa_handler = func;
+
        memset(&act, 0, sizeof(act));
+       act.sa_flags = 0;
+       sigemptyset(&act.sa_mask);
+       act.sa_handler = func;
 
        if (sigaction(sig, &act, NULL) < 0) {
                fr_strerror_printf("Failed setting signal %i handler via sigaction(): %s", sig, fr_syserror(errno));
index c7fb430..2ad93d3 100644 (file)
@@ -32,7 +32,6 @@ RCSID("$Id$")
 
 #include <fcntl.h>
 #include <ctype.h>
-#include <signal.h>
 
 #ifdef HAVE_SYS_WAIT_H
 #      include <sys/wait.h>
index a564b34..e90e29e 100644 (file)
@@ -36,8 +36,6 @@ RCSID("$Id$")
 #include <fcntl.h>
 #include <ctype.h>
 
-#include <signal.h>
-
 #ifdef HAVE_GETOPT_H
 #      include <getopt.h>
 #endif
@@ -109,9 +107,6 @@ int main(int argc, char *argv[])
        int flag = 0;
        int from_child[2] = {-1, -1};
 
-#ifdef HAVE_SIGACTION
-       struct sigaction act;
-#endif
 
 #ifdef OSFC2
        set_auth_parameters(argc,argv);
@@ -144,12 +139,6 @@ int main(int argc, char *argv[])
        mainconfig.port = -1;
        mainconfig.name = "radiusd";
 
-#ifdef HAVE_SIGACTION
-       memset(&act, 0, sizeof(act));
-       act.sa_flags = 0 ;
-       sigemptyset( &act.sa_mask ) ;
-#endif
-
        /*
         *      Don't put output anywhere until we get told a little
         *      more.
@@ -446,33 +435,27 @@ int main(int argc, char *argv[])
 #ifdef SIGPIPE
        signal(SIGPIPE, SIG_IGN);
 #endif
-#ifdef HAVE_SIGACTION
-       act.sa_handler = sig_hup;
-       sigaction(SIGHUP, &act, NULL);
-       act.sa_handler = sig_fatal;
-       sigaction(SIGTERM, &act, NULL);
-#else
-#ifdef SIGHUP
-       signal(SIGHUP, sig_hup);
-#endif
-       signal(SIGTERM, sig_fatal);
-#endif
+
+       if ((fr_set_signal(SIGHUP, sig_hup) < 0) ||
+           (fr_set_signal(SIGTERM, sig_fatal) < 0)) {
+               ERROR("%s", fr_strerror());
+               exit(EXIT_FAILURE);
+       }
+
        /*
         *      If we're debugging, then a CTRL-C will cause the
         *      server to die immediately.  Use SIGTERM to shut down
         *      the server cleanly in that case.
         */
        if ((mainconfig.debug_memory == 1) || (debug_flag == 0)) {
-#ifdef HAVE_SIGACTION
-               act.sa_handler = sig_fatal;
-               sigaction(SIGINT, &act, NULL);
-               sigaction(SIGQUIT, &act, NULL);
-#else
-               signal(SIGINT, sig_fatal);
+               if ((fr_set_signal(SIGINT, sig_fatal) < 0)
 #ifdef SIGQUIT
-               signal(SIGQUIT, sig_fatal);
-#endif
+               || (fr_set_signal(SIGQUIT, sig_fatal) < 0)
 #endif
+               ) {
+                       ERROR("%s", fr_strerror());
+                       exit(EXIT_FAILURE);
+               }
        }
 
        /*
@@ -648,22 +631,22 @@ static void sig_fatal(int sig)
        if (getpid() != radius_pid) _exit(sig);
 
        switch(sig) {
-               case SIGTERM:
-                       radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
-                       break;
+       case SIGTERM:
+               radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
+               break;
 
-               case SIGINT:
+       case SIGINT:
 #ifdef SIGQUIT
-               case SIGQUIT:
+       case SIGQUIT:
 #endif
-                       if (mainconfig.debug_memory || memory_report) {
-                               radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
-                               break;
-                       }
-                       /* FALL-THROUGH */
+               if (mainconfig.debug_memory || memory_report) {
+                       radius_signal_self(RADIUS_SIGNAL_SELF_TERM);
+                       break;
+               }
+               /* FALL-THROUGH */
 
-               default:
-                       _exit(sig);
+       default:
+               _exit(sig);
        }
 }
 
index 3931ad2..98e1128 100644 (file)
@@ -26,7 +26,6 @@ RCSID("$Id$")
 #include <freeradius-devel/rad_assert.h>
 
 #include <ctype.h>
-#include <signal.h>
 
 #include <sys/stat.h>
 #include <fcntl.h>