-void
-setup_signals(void)
-{
- int i;
-
- for(i=0;i<32;i++)
- signal(i,sighuh);
-
- if(signal(SIGTERM, SIG_IGN) != SIG_ERR)
- signal(SIGTERM, sigterm_handler);
- if(signal(SIGQUIT, SIG_IGN) != SIG_ERR)
- signal(SIGQUIT, sigquit_handler);
- if(signal(SIGSEGV, SIG_IGN) != SIG_ERR)
- signal(SIGSEGV, sigsegv_handler);
- if(signal(SIGHUP, SIG_IGN) != SIG_ERR)
- signal(SIGHUP, sighup_handler);
- signal(SIGPIPE, SIG_IGN);
- if(signal(SIGINT, SIG_IGN) != SIG_ERR)
- signal(SIGINT, sigint_handler);
- signal(SIGUSR1, sigusr1_handler);
- signal(SIGUSR2, sigusr2_handler);
- signal(SIGCHLD, SIG_IGN);
+static RETSIGTYPE unexpected_signal_handler(int a) {
+ logger(LOG_WARNING, "Got unexpected signal %d (%s)", a, strsignal(a));
+}
+
+static RETSIGTYPE ignore_signal_handler(int a) {
+ ifdebug(SCARY_THINGS) logger(LOG_DEBUG, "Ignored signal %d (%s)", a, strsignal(a));
+}
+
+static struct {
+ int signal;
+ void (*handler)(int);
+} sighandlers[] = {
+ {SIGHUP, sighup_handler},
+ {SIGTERM, sigterm_handler},
+ {SIGQUIT, sigquit_handler},
+ {SIGSEGV, fatal_signal_handler},
+ {SIGBUS, fatal_signal_handler},
+ {SIGILL, fatal_signal_handler},
+ {SIGPIPE, ignore_signal_handler},
+ {SIGINT, sigint_handler},
+ {SIGUSR1, sigusr1_handler},
+ {SIGUSR2, sigusr2_handler},
+ {SIGCHLD, ignore_signal_handler},
+ {SIGALRM, sigalrm_handler},
+ {SIGWINCH, sigwinch_handler},
+ {0, NULL}
+};
+#endif
+
+void setup_signals(void) {
+#ifndef HAVE_MINGW
+ int i;
+ struct sigaction act;
+
+ sigemptyset(&emptysigset);
+ act.sa_handler = NULL;
+ act.sa_mask = emptysigset;
+ act.sa_flags = 0;
+
+ /* Set a default signal handler for every signal, errors will be
+ ignored. */
+ for(i = 1; i < NSIG; i++) {
+ if(!do_detach)
+ act.sa_handler = SIG_DFL;
+ else
+ act.sa_handler = unexpected_signal_handler;
+ sigaction(i, &act, NULL);
+ }
+
+ /* If we didn't detach, allow coredumps */
+ if(!do_detach)
+ sighandlers[3].handler = SIG_DFL;
+
+ /* Then, for each known signal that we want to catch, assign a
+ handler to the signal, with error checking this time. */
+ for(i = 0; sighandlers[i].signal; i++) {
+ act.sa_handler = sighandlers[i].handler;
+ if(sigaction(sighandlers[i].signal, &act, NULL) < 0)
+ fprintf(stderr, "Installing signal handler for signal %d (%s) failed: %s\n",
+ sighandlers[i].signal, strsignal(sighandlers[i].signal),
+ strerror(errno));
+ }
+#endif