Skip to content

Commit

Permalink
Cleanup signal handling.
Browse files Browse the repository at this point in the history
SIGTSTP should not redirect you to the internal signal handler but just
use the normal signal handling e.g. stop the process.
  • Loading branch information
Marco van Wieringen committed Feb 17, 2015
1 parent ba80259 commit c1341bb
Showing 1 changed file with 103 additions and 89 deletions.
192 changes: 103 additions & 89 deletions src/lib/signal.c
Expand Up @@ -45,7 +45,7 @@ extern char *exepath;
extern char *exename;
extern bool prt_kaboom;

static const char *sig_names[BA_NSIG+1];
static const char *sig_names[BA_NSIG + 1];

typedef void (SIG_HANDLER)(int sig);
static SIG_HANDLER *exit_handler;
Expand Down Expand Up @@ -127,23 +127,31 @@ extern "C" void signal_handler(int sig)
static int already_dead = 0;
int chld_status = -1;

/* If we come back more than once, get out fast! */
/*
* If we come back more than once, get out fast!
*/
if (already_dead) {
exit(1);
}
Dmsg2(900, "sig=%d %s\n", sig, sig_names[sig]);
/* Ignore certain signals -- SIGUSR2 used to interrupt threads */

/*
* Ignore certain signals -- SIGUSR2 used to interrupt threads
*/
if (sig == SIGCHLD || sig == SIGUSR2) {
return;
}
already_dead++;
/* Don't use Emsg here as it may lock and thus block us */

/*
* Don't use Emsg here as it may lock and thus block us
*/
if (sig == SIGTERM) {
syslog(LOG_DAEMON|LOG_ERR, "Shutting down BAREOS service: %s ...\n", my_name);
syslog(LOG_DAEMON | LOG_ERR, "Shutting down BAREOS service: %s ...\n", my_name);
} else {
fprintf(stderr, _("BAREOS interrupted by signal %d: %s\n"), sig, get_signal_name(sig));
syslog(LOG_DAEMON|LOG_ERR,
_("BAREOS interrupted by signal %d: %s\n"), sig, get_signal_name(sig));
syslog(LOG_DAEMON | LOG_ERR,
_("BAREOS interrupted by signal %d: %s\n"), sig, get_signal_name(sig));
}

#ifdef TRACEBACK
Expand Down Expand Up @@ -188,7 +196,9 @@ extern "C" void signal_handler(int sig)
unlink("./core"); /* get rid of any old core file */

#ifdef DEVELOPER /* When DEVELOPER not set, this is done below */
/* print information about the current state into working/<file>.bactrace */
/*
* Print information about the current state into working/<file>.bactrace
*/
dbg_print_bareos();
#endif

Expand Down Expand Up @@ -217,12 +227,14 @@ extern "C" void signal_handler(int sig)
break;
}

/* Parent continue here, waiting for child */
/*
* Parent continue here, waiting for child
*/
sigdefault.sa_flags = 0;
sigdefault.sa_handler = SIG_DFL;
sigfillset(&sigdefault.sa_mask);

sigaction(sig, &sigdefault, NULL);
sigaction(sig, &sigdefault, NULL);
if (pid > 0) {
Dmsg0(500, "Doing waitpid\n");
waitpid(pid, &chld_status, 0); /* wait for child to produce dump */
Expand All @@ -235,13 +247,16 @@ extern "C" void signal_handler(int sig)
fprintf(stderr, _("It looks like the traceback worked...\n"));
} else {
fprintf(stderr, _("The btraceback call returned %d\n"),
WEXITSTATUS(chld_status));
WEXITSTATUS(chld_status));
}

/* If we want it printed, do so */
/*
* If we want it printed, do so
*/
#ifdef direct_print
if (prt_kaboom) {
FILE *fd;

snprintf(buf, sizeof(buf), "%s/bareos.%s.traceback", working_directory, pid_buf);
fd = fopen(buf, "r");
if (fd != NULL) {
Expand All @@ -263,7 +278,9 @@ extern "C" void signal_handler(int sig)
#endif

#ifndef DEVELOPER /* When DEVELOPER set, this is done above */
/* print information about the current state into working/<file>.bactrace */
/*
* Print information about the current state into working/<file>.bactrace
*/
dbg_print_bareos();
#endif

Expand All @@ -274,8 +291,7 @@ extern "C" void signal_handler(int sig)
}

/*
* Init stack dump by saving main process id --
* needed by debugger to attach to this program.
* Init stack dump by saving main process id -- needed by debugger to attach to this program.
*/
void init_stack_dump(void)
{
Expand All @@ -294,76 +310,79 @@ void init_signals(void terminate(int sig))
int i;

exit_handler = terminate;
if (BA_NSIG < _sys_nsig)
if (BA_NSIG < _sys_nsig) {
Emsg2(M_ABORT, 0, _("BA_NSIG too small (%d) should be (%d)\n"), BA_NSIG, _sys_nsig);
}

for (i=0; i<_sys_nsig; i++)
for (i = 0; i < _sys_nsig; i++) {
sig_names[i] = _sys_siglist[i];
}
#else
exit_handler = terminate;
sig_names[0] = _("UNKNOWN SIGNAL");
sig_names[SIGHUP] = _("Hangup");
sig_names[SIGINT] = _("Interrupt");
sig_names[SIGQUIT] = _("Quit");
sig_names[SIGILL] = _("Illegal instruction");
sig_names[SIGTRAP] = _("Trace/Breakpoint trap");
sig_names[SIGABRT] = _("Abort");
sig_names[0] = _("UNKNOWN SIGNAL");
sig_names[SIGHUP] = _("Hangup");
sig_names[SIGINT] = _("Interrupt");
sig_names[SIGQUIT] = _("Quit");
sig_names[SIGILL] = _("Illegal instruction");
sig_names[SIGTRAP] = _("Trace/Breakpoint trap");
sig_names[SIGABRT] = _("Abort");
#ifdef SIGEMT
sig_names[SIGEMT] = _("EMT instruction (Emulation Trap)");
sig_names[SIGEMT] = _("EMT instruction (Emulation Trap)");
#endif
#ifdef SIGIOT
sig_names[SIGIOT] = _("IOT trap");
sig_names[SIGIOT] = _("IOT trap");
#endif
sig_names[SIGBUS] = _("BUS error");
sig_names[SIGFPE] = _("Floating-point exception");
sig_names[SIGKILL] = _("Kill, unblockable");
sig_names[SIGUSR1] = _("User-defined signal 1");
sig_names[SIGSEGV] = _("Segmentation violation");
sig_names[SIGUSR2] = _("User-defined signal 2");
sig_names[SIGPIPE] = _("Broken pipe");
sig_names[SIGALRM] = _("Alarm clock");
sig_names[SIGTERM] = _("Termination");
sig_names[SIGBUS] = _("BUS error");
sig_names[SIGFPE] = _("Floating-point exception");
sig_names[SIGKILL] = _("Kill, unblockable");
sig_names[SIGUSR1] = _("User-defined signal 1");
sig_names[SIGSEGV] = _("Segmentation violation");
sig_names[SIGUSR2] = _("User-defined signal 2");
sig_names[SIGPIPE] = _("Broken pipe");
sig_names[SIGALRM] = _("Alarm clock");
sig_names[SIGTERM] = _("Termination");
#ifdef SIGSTKFLT
sig_names[SIGSTKFLT] = _("Stack fault");
#endif
sig_names[SIGCHLD] = _("Child status has changed");
sig_names[SIGCONT] = _("Continue");
sig_names[SIGSTOP] = _("Stop, unblockable");
sig_names[SIGTSTP] = _("Keyboard stop");
sig_names[SIGTTIN] = _("Background read from tty");
sig_names[SIGTTOU] = _("Background write to tty");
sig_names[SIGURG] = _("Urgent condition on socket");
sig_names[SIGXCPU] = _("CPU limit exceeded");
sig_names[SIGXFSZ] = _("File size limit exceeded");
sig_names[SIGCHLD] = _("Child status has changed");
sig_names[SIGCONT] = _("Continue");
sig_names[SIGSTOP] = _("Stop, unblockable");
sig_names[SIGTSTP] = _("Keyboard stop");
sig_names[SIGTTIN] = _("Background read from tty");
sig_names[SIGTTOU] = _("Background write to tty");
sig_names[SIGURG] = _("Urgent condition on socket");
sig_names[SIGXCPU] = _("CPU limit exceeded");
sig_names[SIGXFSZ] = _("File size limit exceeded");
sig_names[SIGVTALRM] = _("Virtual alarm clock");
sig_names[SIGPROF] = _("Profiling alarm clock");
sig_names[SIGWINCH] = _("Window size change");
sig_names[SIGIO] = _("I/O now possible");
sig_names[SIGPROF] = _("Profiling alarm clock");
sig_names[SIGWINCH] = _("Window size change");
sig_names[SIGIO] = _("I/O now possible");
#ifdef SIGPWR
sig_names[SIGPWR] = _("Power failure restart");
sig_names[SIGPWR] = _("Power failure restart");
#endif
#ifdef SIGWAITING
sig_names[SIGWAITING] = _("No runnable lwp");
#endif
#ifdef SIGLWP
sig_names[SIGLWP] = _("SIGLWP special signal used by thread library");
sig_names[SIGLWP] = _("SIGLWP special signal used by thread library");
#endif
#ifdef SIGFREEZE
sig_names[SIGFREEZE] = _("Checkpoint Freeze");
#endif
#ifdef SIGTHAW
sig_names[SIGTHAW] = _("Checkpoint Thaw");
sig_names[SIGTHAW] = _("Checkpoint Thaw");
#endif
#ifdef SIGCANCEL
sig_names[SIGCANCEL] = _("Thread Cancellation");
#endif
#ifdef SIGLOST
sig_names[SIGLOST] = _("Resource Lost (e.g. record-lock lost)");
sig_names[SIGLOST] = _("Resource Lost (e.g. record-lock lost)");
#endif
#endif


/* Now setup signal handlers */
/*
* Now setup signal handlers
*/
sighandle.sa_flags = 0;
sighandle.sa_handler = signal_handler;
sigfillset(&sighandle.sa_mask);
Expand All @@ -374,66 +393,61 @@ void init_signals(void terminate(int sig))
sigdefault.sa_handler = SIG_DFL;
sigfillset(&sigdefault.sa_mask);


sigaction(SIGPIPE, &sigignore, NULL);
sigaction(SIGCHLD, &sighandle, NULL);
sigaction(SIGCONT, &sigignore, NULL);
sigaction(SIGPROF, &sigignore, NULL);
sigaction(SIGWINCH, &sigignore, NULL);
sigaction(SIGIO, &sighandle, NULL);

sigaction(SIGINT, &sigdefault, NULL);
sigaction(SIGXCPU, &sigdefault, NULL);
sigaction(SIGXFSZ, &sigdefault, NULL);

sigaction(SIGHUP, &sigignore, NULL);
sigaction(SIGQUIT, &sighandle, NULL);
sigaction(SIGILL, &sighandle, NULL);
sigaction(SIGTRAP, &sighandle, NULL);
sigaction(SIGABRT, &sighandle, NULL);
sigaction(SIGPIPE, &sigignore, NULL);
sigaction(SIGCHLD, &sighandle, NULL);
sigaction(SIGCONT, &sigignore, NULL);
sigaction(SIGPROF, &sigignore, NULL);
sigaction(SIGWINCH, &sigignore, NULL);
sigaction(SIGIO, &sighandle, NULL);
sigaction(SIGINT, &sigdefault, NULL);
sigaction(SIGXCPU, &sigdefault, NULL);
sigaction(SIGXFSZ, &sigdefault, NULL);
sigaction(SIGHUP, &sigignore, NULL);
sigaction(SIGQUIT, &sighandle, NULL);
sigaction(SIGILL, &sighandle, NULL);
sigaction(SIGTRAP, &sighandle, NULL);
sigaction(SIGABRT, &sighandle, NULL);
#ifdef SIGEMT
sigaction(SIGEMT, &sighandle, NULL);
sigaction(SIGEMT, &sighandle, NULL);
#endif
#ifdef SIGIOT
sigaction(SIGIOT, &sighandle, NULL);
sigaction(SIGIOT, &sighandle, NULL);
#endif
sigaction(SIGBUS, &sighandle, NULL);
sigaction(SIGFPE, &sighandle, NULL);
/* sigaction(SIGKILL, &sighandle, NULL); cannot be trapped */
sigaction(SIGUSR1, &sighandle, NULL);
sigaction(SIGSEGV, &sighandle, NULL);
sigaction(SIGUSR2, &sighandle, NULL);
sigaction(SIGALRM, &sighandle, NULL);
sigaction(SIGTERM, &sighandle, NULL);
sigaction(SIGBUS, &sighandle, NULL);
sigaction(SIGFPE, &sighandle, NULL);
sigaction(SIGUSR1, &sighandle, NULL);
sigaction(SIGSEGV, &sighandle, NULL);
sigaction(SIGUSR2, &sighandle, NULL);
sigaction(SIGALRM, &sighandle, NULL);
sigaction(SIGTERM, &sighandle, NULL);
#ifdef SIGSTKFLT
sigaction(SIGSTKFLT, &sighandle, NULL);
#endif
/* sigaction(SIGSTOP, &sighandle, NULL); cannot be trapped */
sigaction(SIGTSTP, &sighandle, NULL);
sigaction(SIGTTIN, &sighandle, NULL);
sigaction(SIGTTOU, &sighandle, NULL);
sigaction(SIGURG, &sighandle, NULL);
sigaction(SIGTSTP, &sigdefault, NULL);
sigaction(SIGTTIN, &sighandle, NULL);
sigaction(SIGTTOU, &sighandle, NULL);
sigaction(SIGURG, &sighandle, NULL);
sigaction(SIGVTALRM, &sighandle, NULL);
#ifdef SIGPWR
sigaction(SIGPWR, &sighandle, NULL);
sigaction(SIGPWR, &sighandle, NULL);
#endif
#ifdef SIGWAITING
sigaction(SIGWAITING,&sighandle, NULL);
#endif
#ifdef SIGLWP
sigaction(SIGLWP, &sighandle, NULL);
sigaction(SIGLWP, &sighandle, NULL);
#endif
#ifdef SIGFREEZE
sigaction(SIGFREEZE, &sighandle, NULL);
#endif
#ifdef SIGTHAW
sigaction(SIGTHAW, &sighandle, NULL);
sigaction(SIGTHAW, &sighandle, NULL);
#endif
#ifdef SIGCANCEL
sigaction(SIGCANCEL, &sighandle, NULL);
#endif
#ifdef SIGLOST
sigaction(SIGLOST, &sighandle, NULL);
sigaction(SIGLOST, &sighandle, NULL);
#endif
}
#endif

0 comments on commit c1341bb

Please sign in to comment.