From f2ce40d90b92f6049745f6f06191bd43b6b931dd Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Fri, 1 Feb 2008 13:20:19 +0000 Subject: [PATCH] Use sigaction over signal. --- src/includes/rc-misc.h | 2 ++ src/rc/rc-misc.c | 13 +++++++++++++ src/rc/rc.c | 23 ++++++++++++++--------- src/rc/runscript.c | 13 +++++++------ src/rc/start-stop-daemon.c | 8 ++++---- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/includes/rc-misc.h b/src/includes/rc-misc.h index e034c1e5..946754ee 100644 --- a/src/includes/rc-misc.h +++ b/src/includes/rc-misc.h @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -133,6 +134,7 @@ bool rc_conf_yesno (const char *var); char **env_filter (void); char **env_config (void); bool service_plugable (const char *service); +void signal_setup (int sig, void (*handler)(int)); /* basename_c never modifies the argument. As such, if there is a trailing * slash then an empty string is returned. */ diff --git a/src/rc/rc-misc.c b/src/rc/rc-misc.c index 1b79d2c8..50101b43 100644 --- a/src/rc/rc-misc.c +++ b/src/rc/rc-misc.c @@ -39,10 +39,12 @@ #include #include #include +#include #include #include #include +#include "einfo.h" #include "rc.h" #include "rc-misc.h" #include "strlist.h" @@ -429,3 +431,14 @@ bool service_plugable (const char *service) free (list); return (allow); } + +void signal_setup (int sig, void (*handler)(int)) +{ + struct sigaction sa; + + memset (&sa, 0, sizeof (sa)); + sa.sa_handler = handler; + sigemptyset (&sa.sa_mask); + if (sigaction (sig, &sa, NULL) == -1) + eerrorx ("sigaction: %s", strerror (errno)); +} diff --git a/src/rc/rc.c b/src/rc/rc.c index 4205c4fb..954d6b59 100644 --- a/src/rc/rc.c +++ b/src/rc/rc.c @@ -441,6 +441,7 @@ static void handle_signal (int sig) pid_t pid; int status = 0; struct winsize ws; + sigset_t sset; switch (sig) { case SIGCHLD: @@ -480,9 +481,13 @@ static void handle_signal (int sig) /* NOTREACHED */ case SIGUSR1: eerror ("rc: Aborting!"); - /* Kill any running services we have started */ - signal (SIGCHLD, SIG_IGN); + /* Block child signals */ + sigemptyset (&sset); + sigaddset (&sset, SIGCHLD); + sigprocmask (SIG_BLOCK, &sset, NULL); + + /* Kill any running services we have started */ for (pl = service_pids; pl; pl = pl->next) kill (pl->pid, SIGTERM); @@ -761,12 +766,12 @@ int main (int argc, char **argv) rc_logger_open (newlevel ? newlevel : runlevel); /* Setup a signal handler */ - signal (SIGINT, handle_signal); - signal (SIGQUIT, handle_signal); - signal (SIGTERM, handle_signal); - signal (SIGUSR1, handle_signal); - signal (SIGWINCH, handle_signal); - + signal_setup (SIGINT, handle_signal); + signal_setup (SIGQUIT, handle_signal); + signal_setup (SIGTERM, handle_signal); + signal_setup (SIGUSR1, handle_signal); + signal_setup (SIGWINCH, handle_signal); + if (! rc_yesno (getenv ("EINFO_QUIET"))) interactive = exists (INTERACTIVE); rc_plugin_load (); @@ -871,7 +876,7 @@ int main (int argc, char **argv) } /* Now we start handling our children */ - signal (SIGCHLD, handle_signal); + signal_setup (SIGCHLD, handle_signal); /* We should only use ksoftlevel if we were in single user mode * If not, we need to erase ksoftlevel now. */ diff --git a/src/rc/runscript.c b/src/rc/runscript.c index c52ae157..458ecd2d 100644 --- a/src/rc/runscript.c +++ b/src/rc/runscript.c @@ -490,7 +490,8 @@ static bool svc_exec (const char *arg1, const char *arg2) signal_pipe[0] = signal_pipe[1] = -1; if (master_tty >= 0) { - signal (SIGWINCH, SIG_IGN); + /* Why did we do this? */ + /* signal (SIGWINCH, SIG_IGN); */ close (master_tty); master_tty = -1; } @@ -1199,11 +1200,11 @@ int runscript (int argc, char **argv) } /* Setup a signal handler */ - signal (SIGHUP, handle_signal); - signal (SIGINT, handle_signal); - signal (SIGQUIT, handle_signal); - signal (SIGTERM, handle_signal); - signal (SIGCHLD, handle_signal); + signal_setup (SIGHUP, handle_signal); + signal_setup (SIGINT, handle_signal); + signal_setup (SIGQUIT, handle_signal); + signal_setup (SIGTERM, handle_signal); + signal_setup (SIGCHLD, handle_signal); /* Load our plugins */ rc_plugin_load (); diff --git a/src/rc/start-stop-daemon.c b/src/rc/start-stop-daemon.c index cecf55cf..43d1d3d8 100644 --- a/src/rc/start-stop-daemon.c +++ b/src/rc/start-stop-daemon.c @@ -578,9 +578,9 @@ int start_stop_daemon (int argc, char **argv) applet = basename_c (argv[0]); atexit (cleanup); - signal (SIGINT, handle_signal); - signal (SIGQUIT, handle_signal); - signal (SIGTERM, handle_signal); + signal_setup (SIGINT, handle_signal); + signal_setup (SIGQUIT, handle_signal); + signal_setup (SIGTERM, handle_signal); if ((env = getenv ("SSD_NICELEVEL"))) if (sscanf (env, "%d", &nicelevel) != 1) @@ -823,7 +823,7 @@ int start_stop_daemon (int argc, char **argv) } if (background) - signal (SIGCHLD, handle_signal); + signal_setup (SIGCHLD, handle_signal); *--argv = exec; if ((pid = fork ()) == -1)