From d9628b27abfb090d854f77073bc9a1870d804164 Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Thu, 19 Jul 2012 15:27:18 -0700 Subject: Hopefully make shutdown more clean. --- src/common/core.c | 49 ++++++++++++++++++++++++++++++++++--------------- src/common/core.h | 2 +- 2 files changed, 35 insertions(+), 16 deletions(-) (limited to 'src/common') diff --git a/src/common/core.c b/src/common/core.c index 2b1b67c..b08276c 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -15,19 +15,6 @@ extern int do_init (int, char **); extern void term_func (void); -static void chld_proc (int UNUSED) -{ - wait(NULL); -} -static void sig_proc (int UNUSED) -{ - term_func (); - for (int i = 0; i < fd_max; i++) - if (session[i]) - close (i); - exit (0); -} - // Added by Gabuzomeu // // This is an implementation of signal() using sigaction() for portability. @@ -40,7 +27,12 @@ sigfunc compat_signal (int signo, sigfunc func) struct sigaction sact, oact; sact.sa_handler = func; - sigemptyset (&sact.sa_mask); + sigfillset (&sact.sa_mask); + sigdelset(&sact.sa_mask, SIGSEGV); + sigdelset(&sact.sa_mask, SIGBUS); + sigdelset(&sact.sa_mask, SIGTRAP); + sigdelset(&sact.sa_mask, SIGILL); + sigdelset(&sact.sa_mask, SIGFPE); sact.sa_flags = 0; if (sigaction (signo, &sact, &oact) < 0) @@ -49,8 +41,29 @@ sigfunc compat_signal (int signo, sigfunc func) return oact.sa_handler; } +static void chld_proc (int UNUSED) +{ + wait(NULL); +} +static void sig_proc (int UNUSED) +{ + for (int i = 1; i < 31; ++i) + compat_signal(i, SIG_IGN); + term_func (); + _exit (0); +} + bool runflag = true; +/* + Note about fatal signals: + + Under certain circumstances, + the following signals MUST not be ignored: + SIGFPE, SIGSEGV, SIGILL + Unless you use SA_SIGINFO and *carefully* check the origin, + that means they must be SIG_DFL. + */ int main (int argc, char **argv) { /// Note that getpid() and getppid() may be very close @@ -58,6 +71,10 @@ int main (int argc, char **argv) do_socket (); + do_init (argc, argv); + // set up exit handlers *after* the initialization has happened. + // This is because term_func is likely to depend on successful init. + compat_signal (SIGPIPE, SIG_IGN); compat_signal (SIGTERM, sig_proc); compat_signal (SIGINT, sig_proc); @@ -68,8 +85,10 @@ int main (int argc, char **argv) compat_signal (SIGBUS, SIG_DFL); compat_signal (SIGTRAP, SIG_DFL); compat_signal (SIGILL, SIG_DFL); + compat_signal (SIGFPE, SIG_DFL); + + atexit (term_func); - do_init (argc, argv); while (runflag) { do_sendrecv (do_timer (gettick_nocache ())); diff --git a/src/common/core.h b/src/common/core.h index 6722ca2..44473e9 100644 --- a/src/common/core.h +++ b/src/common/core.h @@ -13,7 +13,7 @@ extern bool runflag; extern int do_init (int, char **); /// Cleanup function called whenever a signal kills us -/// NOT currently called when exit() is called +/// or when if we manage to exit() gracefully. extern void term_func (void); #endif // CORE_H -- cgit v1.2.3-70-g09d2