Use sigaction over signal.

This commit is contained in:
Roy Marples 2008-02-01 13:20:19 +00:00
parent 01e148d29d
commit f2ce40d90b
5 changed files with 40 additions and 19 deletions

View File

@ -34,6 +34,7 @@
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <string.h>
@ -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. */

View File

@ -39,10 +39,12 @@
#include <sys/utsname.h>
#include <ctype.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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));
}

View File

@ -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. */

View File

@ -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 ();

View File

@ -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)