From 3c7c1736b7d6b6d086e9c5b54b963f8e244e3418 Mon Sep 17 00:00:00 2001 From: Roy Marples Date: Mon, 9 Apr 2007 16:53:21 +0000 Subject: Use names instead of numbers for reporting signals and trap signals in rc --- src/Makefile | 2 +- src/rc.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- src/runscript.c | 11 ++++++++-- src/start-stop-daemon.c | 9 ++++++++- 4 files changed, 70 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 33509c30..91398f47 100644 --- a/src/Makefile +++ b/src/Makefile @@ -98,7 +98,7 @@ override CFLAGS += -DLIB=\"$(LIB)\" # However, this does save us using libtool when we're testing # NOTE: The toplevel Makefile for baselayout will automatically # disable then when doing `make dist` -##override LDFLAGS += -Wl,-rpath . +override LDFLAGS += -Wl,-rpath . all: $(TARGET) diff --git a/src/rc.c b/src/rc.c index 48fb9089..2b9629f7 100644 --- a/src/rc.c +++ b/src/rc.c @@ -55,6 +55,7 @@ extern char **environ; +static char *applet = NULL; static char **env = NULL; static char **newenv = NULL; static char **coldplugged_services; @@ -65,7 +66,6 @@ static char **types = NULL; static char *mycmd = NULL; static char *myarg = NULL; static char *tmp = NULL; -static char *applet = NULL; struct termios *termios_orig; @@ -103,6 +103,9 @@ static void cleanup (void) rc_rm_dir (RC_SVCDIR "softscripts.new", true); if (rc_is_dir (RC_SVCDIR "softscripts.old")) rc_rm_dir (RC_SVCDIR "softscripts.old", true); + + if (applet) + free (applet); } static int do_e (int argc, char **argv) @@ -453,6 +456,47 @@ static void wait_for_services () select (0, NULL, NULL, NULL, &tv); } +static void handle_signal (int sig) +{ + pid_t pid; + int status; + int serrno = errno; + char signame[10] = { '\0' }; + + switch (sig) + { + case SIGCHLD: + do + { + pid = waitpid (-1, &status, WNOHANG); + if (pid < 0) + { + if (errno && errno != ECHILD) + eerror ("waitpid: %s", strerror (errno)); + return; + } + } while (! WIFEXITED (status) && ! WIFSIGNALED (status)); + break; + + case SIGINT: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGINT"); + case SIGTERM: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGTERM"); + case SIGQUIT: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGQUIT"); + eerrorx ("%s: caught %s, aborting", applet, signame); + + default: + eerror ("%s: caught unknown signal %d", applet, sig); + } + + /* Restore errno */ + errno = serrno; +} + int main (int argc, char **argv) { char *RUNLEVEL = NULL; @@ -469,7 +513,7 @@ int main (int argc, char **argv) char ksoftbuffer [PATH_MAX]; if (argv[0]) - applet = basename (argv[0]); + applet = strdup (basename (argv[0])); if (! applet) eerrorx ("arguments required"); @@ -509,6 +553,12 @@ int main (int argc, char **argv) atexit (cleanup); newlevel = argv[0]; + /* Setup a signal handler */ + signal (SIGINT, handle_signal); + signal (SIGQUIT, handle_signal); + signal (SIGTERM, handle_signal); + signal (SIGCHLD, handle_signal); + /* Ensure our environment is pure Also, add our configuration to it */ env = rc_filter_env (); diff --git a/src/runscript.c b/src/runscript.c index d87d42f0..ad8a251c 100644 --- a/src/runscript.c +++ b/src/runscript.c @@ -94,6 +94,7 @@ static void handle_signal (int sig) pid_t pid; int status; int serrno = errno; + char signame[10] = { '\0' }; switch (sig) { @@ -113,11 +114,17 @@ static void handle_signal (int sig) } } while (! WIFEXITED (status) && ! WIFSIGNALED (status)); break; - + case SIGINT: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGINT"); case SIGTERM: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGTERM"); case SIGQUIT: - eerrorx ("%s: caught signal %d, aborting", applet, sig); + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGQUIT"); + eerrorx ("%s: caught %s, aborting", applet, signame); default: eerror ("%s: caught unknown signal %d", applet, sig); diff --git a/src/start-stop-daemon.c b/src/start-stop-daemon.c index fef416cb..b48b7966 100644 --- a/src/start-stop-daemon.c +++ b/src/start-stop-daemon.c @@ -459,13 +459,20 @@ static void handle_signal (int sig) int pid; int status; int serrno = errno; + char signame[10] = { '\0' }; switch (sig) { case SIGINT: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGINT"); case SIGTERM: + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGTERM"); case SIGQUIT: - eerrorx ("%s: caught signal %d, aborting", progname, sig); + if (! signame[0]) + snprintf (signame, sizeof (signame), "SIGQUIT"); + eerrorx ("%s: caught %s, aborting", progname, signame); case SIGCHLD: while (1) -- cgit v1.2.3