2 * Signal handlers for Bacula daemons
4 * Kern Sibbald, April 2000
8 * Note, we probably should do a core dump for the serious
9 * signals such as SIGBUS, SIGPFE, ...
10 * Also, for SIGHUP and SIGUSR1, we should re-read the
11 * configuration file. However, since this is a "general"
12 * routine, we leave it to the individual daemons to
13 * tweek their signals after calling this routine.
17 Bacula® - The Network Backup Solution
19 Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
21 The main author of Bacula is Kern Sibbald, with contributions from
22 many others, a complete list can be found in the file AUTHORS.
23 This program is Free Software; you can redistribute it and/or
24 modify it under the terms of version two of the GNU General Public
25 License as published by the Free Software Foundation plus additions
26 that are listed in the file LICENSE.
28 This program is distributed in the hope that it will be useful, but
29 WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program; if not, write to the Free Software
35 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
38 Bacula® is a registered trademark of John Walker.
39 The licensor of Bacula is the Free Software Foundation Europe
40 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
41 Switzerland, email:ftf@fsfeurope.org.
53 extern char my_name[];
57 static const char *sig_names[BA_NSIG+1];
59 typedef void (SIG_HANDLER)(int sig);
60 static SIG_HANDLER *exit_handler;
63 static pid_t main_pid = 0;
65 const char *get_signal_name(int sig)
67 if (sig < 0 || sig > BA_NSIG || !sig_names[sig]) {
68 return _("Invalid signal number");
70 return sig_names[sig];
77 extern "C" void signal_handler(int sig)
79 static int already_dead = 0;
81 /* If we come back more than once, get out fast! */
85 Dmsg2(900, "sig=%d %s\n", sig, sig_names[sig]);
86 /* Ignore certain signals -- SIGUSR2 used to interrupt threads */
87 if (sig == SIGCHLD || sig == SIGUSR2) {
92 // Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
94 Emsg2(M_FATAL, -1, _("Bacula interrupted by signal %d: %s\n"), sig, sig_names[sig]);
99 struct sigaction sigdefault;
100 static char *argv[4];
101 static char pid_buf[20];
102 static char btpath[400];
105 int exelen = strlen(exepath);
107 fprintf(stderr, _("Kaboom! %s, %s got signal %d. Attempting traceback.\n"),
108 exename, my_name, sig);
109 fprintf(stderr, _("Kaboom! exepath=%s\n"), exepath);
111 if (exelen + 12 > (int)sizeof(btpath)) {
112 bstrncpy(btpath, "btraceback", sizeof(btpath));
114 bstrncpy(btpath, exepath, sizeof(btpath));
115 if (IsPathSeparator(btpath[exelen-1])) {
116 btpath[exelen-1] = 0;
118 bstrncat(btpath, "/btraceback", sizeof(btpath));
120 if (!IsPathSeparator(exepath[exelen - 1])) {
121 strcat(exepath, "/");
123 strcat(exepath, exename);
124 if (!working_directory) {
125 working_directory = buf;
128 if (*working_directory == 0) {
129 strcpy((char *)working_directory, "/tmp/");
131 if (chdir(working_directory) != 0) { /* dump in working directory */
133 Pmsg2(000, "chdir to %s failed. ERR=%s\n", working_directory, be.strerror());
134 strcpy((char *)working_directory, "/tmp/");
136 unlink("./core"); /* get rid of any old core file */
137 sprintf(pid_buf, "%d", (int)main_pid);
138 Dmsg1(300, "Working=%s\n", working_directory);
139 Dmsg1(300, "btpath=%s\n", btpath);
140 Dmsg1(300, "exepath=%s\n", exepath);
141 switch (pid = fork()) {
143 fprintf(stderr, _("Fork error: ERR=%s\n"), strerror(errno));
146 argv[0] = btpath; /* path to btraceback */
147 argv[1] = exepath; /* path to exe */
149 argv[3] = (char *)NULL;
150 fprintf(stderr, _("Calling: %s %s %s\n"), btpath, exepath, pid_buf);
151 if (execv(btpath, argv) != 0) {
152 printf(_("execv: %s failed: ERR=%s\n"), btpath, strerror(errno));
155 default: /* parent */
158 /* Parent continue here, waiting for child */
159 sigdefault.sa_flags = 0;
160 sigdefault.sa_handler = SIG_DFL;
161 sigfillset(&sigdefault.sa_mask);
163 sigaction(sig, &sigdefault, NULL);
165 Dmsg0(500, "Doing waitpid\n");
166 waitpid(pid, NULL, 0); /* wait for child to produce dump */
167 fprintf(stderr, _("Traceback complete, attempting cleanup ...\n"));
168 Dmsg0(500, "Done waitpid\n");
169 exit_handler(sig); /* clean up if possible */
170 Dmsg0(500, "Done exit_handler\n");
172 Dmsg0(500, "Doing sleep\n");
175 fprintf(stderr, _("It looks like the traceback worked ...\n"));
183 * Init stack dump by saving main process id --
184 * needed by debugger to attach to this program.
186 void init_stack_dump(void)
188 main_pid = getpid(); /* save main thread's pid */
194 void init_signals(void terminate(int sig))
196 struct sigaction sighandle;
197 struct sigaction sigignore;
198 struct sigaction sigdefault;
202 exit_handler = terminate;
203 if (BA_NSIG < _sys_nsig)
204 Emsg2(M_ABORT, 0, _("BA_NSIG too small (%d) should be (%d)\n"), BA_NSIG, _sys_nsig);
206 for (i=0; i<_sys_nsig; i++)
207 sig_names[i] = _sys_siglist[i];
209 exit_handler = terminate;
210 sig_names[0] = _("UNKNOWN SIGNAL");
211 sig_names[SIGHUP] = _("Hangup");
212 sig_names[SIGINT] = _("Interrupt");
213 sig_names[SIGQUIT] = _("Quit");
214 sig_names[SIGILL] = _("Illegal instruction");;
215 sig_names[SIGTRAP] = _("Trace/Breakpoint trap");
216 sig_names[SIGABRT] = _("Abort");
218 sig_names[SIGEMT] = _("EMT instruction (Emulation Trap)");
221 sig_names[SIGIOT] = _("IOT trap");
223 sig_names[SIGBUS] = _("BUS error");
224 sig_names[SIGFPE] = _("Floating-point exception");
225 sig_names[SIGKILL] = _("Kill, unblockable");
226 sig_names[SIGUSR1] = _("User-defined signal 1");
227 sig_names[SIGSEGV] = _("Segmentation violation");
228 sig_names[SIGUSR2] = _("User-defined signal 2");
229 sig_names[SIGPIPE] = _("Broken pipe");
230 sig_names[SIGALRM] = _("Alarm clock");
231 sig_names[SIGTERM] = _("Termination");
233 sig_names[SIGSTKFLT] = _("Stack fault");
235 sig_names[SIGCHLD] = _("Child status has changed");
236 sig_names[SIGCONT] = _("Continue");
237 sig_names[SIGSTOP] = _("Stop, unblockable");
238 sig_names[SIGTSTP] = _("Keyboard stop");
239 sig_names[SIGTTIN] = _("Background read from tty");
240 sig_names[SIGTTOU] = _("Background write to tty");
241 sig_names[SIGURG] = _("Urgent condition on socket");
242 sig_names[SIGXCPU] = _("CPU limit exceeded");
243 sig_names[SIGXFSZ] = _("File size limit exceeded");
244 sig_names[SIGVTALRM] = _("Virtual alarm clock");
245 sig_names[SIGPROF] = _("Profiling alarm clock");
246 sig_names[SIGWINCH] = _("Window size change");
247 sig_names[SIGIO] = _("I/O now possible");
249 sig_names[SIGPWR] = _("Power failure restart");
252 sig_names[SIGWAITING] = _("No runnable lwp");
255 sig_names[SIGLWP] = _("SIGLWP special signal used by thread library");
258 sig_names[SIGFREEZE] = _("Checkpoint Freeze");
261 sig_names[SIGTHAW] = _("Checkpoint Thaw");
264 sig_names[SIGCANCEL] = _("Thread Cancellation");
267 sig_names[SIGLOST] = _("Resource Lost (e.g. record-lock lost)");
272 /* Now setup signal handlers */
273 sighandle.sa_flags = 0;
274 sighandle.sa_handler = signal_handler;
275 sigfillset(&sighandle.sa_mask);
276 sigignore.sa_flags = 0;
277 sigignore.sa_handler = SIG_IGN;
278 sigfillset(&sigignore.sa_mask);
279 sigdefault.sa_flags = 0;
280 sigdefault.sa_handler = SIG_DFL;
281 sigfillset(&sigdefault.sa_mask);
284 sigaction(SIGPIPE, &sigignore, NULL);
285 sigaction(SIGCHLD, &sighandle, NULL);
286 sigaction(SIGCONT, &sigignore, NULL);
287 sigaction(SIGPROF, &sigignore, NULL);
288 sigaction(SIGWINCH, &sigignore, NULL);
289 sigaction(SIGIO, &sighandle, NULL);
291 sigaction(SIGINT, &sigdefault, NULL);
292 sigaction(SIGXCPU, &sigdefault, NULL);
293 sigaction(SIGXFSZ, &sigdefault, NULL);
295 sigaction(SIGHUP, &sigignore, NULL);
296 sigaction(SIGQUIT, &sighandle, NULL);
297 sigaction(SIGILL, &sighandle, NULL);
298 sigaction(SIGTRAP, &sighandle, NULL);
299 /* sigaction(SIGABRT, &sighandle, NULL); */
301 sigaction(SIGEMT, &sighandle, NULL);
304 /* sigaction(SIGIOT, &sighandle, NULL); used by debugger */
306 sigaction(SIGBUS, &sighandle, NULL);
307 sigaction(SIGFPE, &sighandle, NULL);
308 /* sigaction(SIGKILL, &sighandle, NULL); cannot be trapped */
309 sigaction(SIGUSR1, &sighandle, NULL);
310 sigaction(SIGSEGV, &sighandle, NULL);
311 sigaction(SIGUSR2, &sighandle, NULL);
312 sigaction(SIGALRM, &sighandle, NULL);
313 sigaction(SIGTERM, &sighandle, NULL);
315 sigaction(SIGSTKFLT, &sighandle, NULL);
317 /* sigaction(SIGSTOP, &sighandle, NULL); cannot be trapped */
318 sigaction(SIGTSTP, &sighandle, NULL);
319 sigaction(SIGTTIN, &sighandle, NULL);
320 sigaction(SIGTTOU, &sighandle, NULL);
321 sigaction(SIGURG, &sighandle, NULL);
322 sigaction(SIGVTALRM, &sighandle, NULL);
324 sigaction(SIGPWR, &sighandle, NULL);
327 sigaction(SIGWAITING,&sighandle, NULL);
330 sigaction(SIGLWP, &sighandle, NULL);
333 sigaction(SIGFREEZE, &sighandle, NULL);
336 sigaction(SIGTHAW, &sighandle, NULL);
339 sigaction(SIGCANCEL, &sighandle, NULL);
342 sigaction(SIGLOST, &sighandle, NULL);