Author: gherzan Date: Thu Nov 12 15:27:02 2009 New Revision: 1671 Log: pisacd: signal handling, sigaction-style sigaction(2) allows for more flexible handling and for more signal data in the handlers. Modified: trunk/pisacd/cdmain.c Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Thu Nov 12 14:43:40 2009 (r1670) +++ trunk/pisacd/cdmain.c Thu Nov 12 15:27:02 2009 (r1671) @@ -10,11 +10,14 @@ * @date Jan. 2009 */ +/* required for the sa_sigaction member */ +#define _POSIX_C_SOURCE 200112 + #include <signal.h> #include <getopt.h> +#include <unistd.h> #include <sys/utsname.h> #include <sys/poll.h> -#include <unistd.h> #include "libpisa/tunnel.h" #include "libpisa/util.h" @@ -205,20 +208,26 @@ * * @param quitcode signal quit code */ -static void cd_quit(int quitcode) +static void cd_quit(int signum, UNUSED siginfo_t *siginfo, UNUSED void *ctx) { + struct sigaction sig_dfl_action = { + .sa_handler = SIG_DFL, + .sa_flags = 0 + }; + PISA_DEBUG(PL_SHUTDOWN, "Quitting PISA client daemon...\n"); pisa_client_disconnect_from_all_servers(); cd_ctx.flag_running = false; /* Reinstall default handler for that signal */ - signal(quitcode, SIG_DFL); + sigemptyset(&sig_dfl_action.sa_mask); + sigaction(signum, &sig_dfl_action, NULL); } /** * Reload configurations */ -static void cd_reload_confs(UNUSED int quitcode) +static void cd_reload_confs(UNUSED int signum, UNUSED siginfo_t *siginfo, UNUSED void *ctx) { PISA_DEBUG(PL_GENERIC, "Received a HUP signal, reloading basic configurations...\n"); @@ -255,13 +264,27 @@ */ static void cd_init(int argc, char *argv[]) { + struct sigaction sigact; + + /* use the "new" handling callback */ + sigact.sa_flags = SA_SIGINFO; + + sigemptyset(&sigact.sa_mask); + /* Set signal handler for each signal */ - signal(SIGBUS, cd_quit); - signal(SIGINT, cd_quit); - signal(SIGQUIT, cd_quit); - signal(SIGTERM, cd_quit); - signal(SIGHUP, cd_reload_confs); - signal(SIGPIPE, SIG_IGN); + + sigact.sa_sigaction = cd_quit; + sigaction(SIGBUS, &sigact, NULL); + sigaction(SIGINT, &sigact, NULL); + sigaction(SIGQUIT, &sigact, NULL); + sigaction(SIGTERM, &sigact, NULL); + + sigact.sa_sigaction = cd_reload_confs; + sigaction(SIGHUP, &sigact, NULL); + + sigact.sa_flags &= ~SA_SIGINFO; + sigact.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &sigact, NULL); /* Set default values in context and config */ cdctx_init();