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.
18 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
20 This program is free software; you can redistribute it and/or
21 modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation; either version 2 of
23 the License, or (at your option) any later version.
25 This program is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received a copy of the GNU General Public
31 License along with this program; if not, write to the Free
32 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
46 extern char my_name[];
50 static const char *sig_names[BA_NSIG+1];
52 typedef void (SIG_HANDLER)(int sig);
53 static SIG_HANDLER *exit_handler;
56 static pid_t main_pid = 0;
61 static void signal_handler(int sig)
63 static int already_dead = FALSE;
68 /* Ignore certain signals */
69 if (sig == SIGCHLD || sig == SIGUSR2) {
74 Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
76 Emsg2(M_FATAL, -1, "Bacula interrupted by signal %d: %s\n", sig, sig_names[sig]);
81 struct sigaction sigdefault;
83 static char pid_buf[20];
84 static char btpath[400];
86 int exelen = strlen(exepath);
88 fprintf(stderr, "Kaboom! %s, %s got signal %d. Attempting traceback.\n",
89 exename, my_name, sig);
91 if (exelen + 12 > (int)sizeof(btpath)) {
92 strcpy(btpath, "btraceback");
94 strcpy(btpath, exepath);
95 if (btpath[exelen-1] != '/') {
96 strcat(btpath, "/btraceback");
98 strcat(btpath, "btraceback");
101 if (btpath[exelen-1] != '/') {
102 strcat(exepath, "/");
104 strcat(exepath, exename);
105 if (chdir(working_directory) !=0) { /* dump in working directory */
106 Pmsg2(000, "chdir to %s failed. ERR=%s\n", working_directory, strerror(errno));
108 unlink("./core"); /* get rid of any old core file */
109 sprintf(pid_buf, "%d", (int)main_pid);
110 Dmsg1(300, "Working=%s\n", working_directory);
111 Dmsg1(300, "btpath=%s\n", btpath);
112 Dmsg1(300, "exepath=%s\n", exepath);
113 switch (pid = fork()) {
115 fprintf(stderr, "Fork error: ERR=%s\n", strerror(errno));
118 argv[0] = btpath; /* path to btraceback */
119 argv[1] = exepath; /* path to exe */
121 argv[3] = (char *)NULL;
122 fprintf(stderr, "Calling: %s %s %s\n", btpath, exepath, pid_buf);
123 if (execv(btpath, argv) != 0) {
124 printf("execv: %s failed: ERR=%s\n", btpath, strerror(errno));
127 default: /* parent */
130 /* Parent continue here, waiting for child */
131 sigdefault.sa_flags = 0;
132 sigdefault.sa_handler = SIG_DFL;
133 sigfillset(&sigdefault.sa_mask);
135 sigaction(sig, &sigdefault, NULL);
137 Dmsg0(500, "Doing waitpid\n");
138 waitpid(pid, NULL, 0); /* wait for child to produce dump */
139 fprintf(stderr, "Traceback complete, attempting cleanup ...\n");
140 Dmsg0(500, "Done waitpid\n");
141 exit_handler(1); /* clean up if possible */
142 Dmsg0(500, "Done exit_handler\n");
144 Dmsg0(500, "Doing sleep\n");
147 fprintf(stderr, "It looks like the traceback worked ...\n");
155 * Init stack dump by saving main process id --
156 * needed by debugger to attach to this program.
158 void init_stack_dump(void)
160 main_pid = getpid(); /* save main thread's pid */
166 void init_signals(void terminate(int sig))
168 struct sigaction sighandle;
169 struct sigaction sigignore;
170 struct sigaction sigdefault;
174 exit_handler = terminate;
175 if (BA_NSIG < _sys_nsig)
176 Emsg2(M_ABORT, 0, "BA_NSIG too small (%d) should be (%d)\n", BA_NSIG, _sys_nsig);
178 for (i=0; i<_sys_nsig; i++)
179 sig_names[i] = _sys_siglist[i];
181 exit_handler = terminate;
182 sig_names[0] = "UNKNOWN SIGNAL";
183 sig_names[SIGHUP] = "Hangup";
184 sig_names[SIGINT] = "Interrupt";
185 sig_names[SIGQUIT] = "Quit";
186 sig_names[SIGILL] = "Illegal instruction";;
187 sig_names[SIGTRAP] = "Trace/Breakpoint trap";
188 sig_names[SIGABRT] = "Abort";
190 sig_names[SIGEMT] = "EMT instruction (Emulation Trap)";
193 sig_names[SIGIOT] = "IOT trap";
195 sig_names[SIGBUS] = "BUS error";
196 sig_names[SIGFPE] = "Floating-point exception";
197 sig_names[SIGKILL] = "Kill, unblockable";
198 sig_names[SIGUSR1] = "User-defined signal 1";
199 sig_names[SIGSEGV] = "Segmentation violation";
200 sig_names[SIGUSR2] = "User-defined signal 2";
201 sig_names[SIGPIPE] = "Broken pipe";
202 sig_names[SIGALRM] = "Alarm clock";
203 sig_names[SIGTERM] = "Termination";
205 sig_names[SIGSTKFLT] = "Stack fault";
207 sig_names[SIGCHLD] = "Child status has changed";
208 sig_names[SIGCONT] = "Continue";
209 sig_names[SIGSTOP] = "Stop, unblockable";
210 sig_names[SIGTSTP] = "Keyboard stop";
211 sig_names[SIGTTIN] = "Background read from tty";
212 sig_names[SIGTTOU] = "Background write to tty";
213 sig_names[SIGURG] = "Urgent condition on socket";
214 sig_names[SIGXCPU] = "CPU limit exceeded";
215 sig_names[SIGXFSZ] = "File size limit exceeded";
216 sig_names[SIGVTALRM] = "Virtual alarm clock";
217 sig_names[SIGPROF] = "Profiling alarm clock";
218 sig_names[SIGWINCH] = "Window size change";
219 sig_names[SIGIO] = "I/O now possible";
221 sig_names[SIGPWR] = "Power failure restart";
224 sig_names[SIGWAITING] = "No runnable lwp";
227 sig_name[SIGLWP] = "SIGLWP special signal used by thread library";
230 sig_names[SIGFREEZE] = "Checkpoint Freeze";
233 sig_names[SIGTHAW] = "Checkpoint Thaw";
236 sig_names[SIGCANCEL] = "Thread Cancellation";
239 sig_names[SIGLOST] = "Resource Lost (e.g. record-lock lost)";
244 /* Now setup signal handlers */
245 sighandle.sa_flags = 0;
246 sighandle.sa_handler = signal_handler;
247 sigfillset(&sighandle.sa_mask);
248 sigignore.sa_flags = 0;
249 sigignore.sa_handler = SIG_IGN;
250 sigfillset(&sigignore.sa_mask);
251 sigdefault.sa_flags = 0;
252 sigdefault.sa_handler = SIG_DFL;
253 sigfillset(&sigdefault.sa_mask);
256 sigaction(SIGPIPE, &sigignore, NULL);
257 sigaction(SIGCHLD, &sighandle, NULL);
258 sigaction(SIGCONT, &sigignore, NULL);
259 sigaction(SIGPROF, &sigignore, NULL);
260 sigaction(SIGWINCH, &sigignore, NULL);
261 sigaction(SIGIO, &sighandle, NULL);
263 sigaction(SIGINT, &sigdefault, NULL);
264 sigaction(SIGXCPU, &sigdefault, NULL);
265 sigaction(SIGXFSZ, &sigdefault, NULL);
267 sigaction(SIGHUP, &sigignore, NULL);
268 sigaction(SIGQUIT, &sighandle, NULL);
269 sigaction(SIGILL, &sighandle, NULL);
270 sigaction(SIGTRAP, &sighandle, NULL);
271 /* sigaction(SIGABRT, &sighandle, NULL); */
273 sigaction(SIGEMT, &sighandle, NULL);
276 /* sigaction(SIGIOT, &sighandle, NULL); used by debugger */
278 sigaction(SIGBUS, &sighandle, NULL);
279 sigaction(SIGFPE, &sighandle, NULL);
280 sigaction(SIGKILL, &sighandle, NULL);
281 sigaction(SIGUSR1, &sighandle, NULL);
282 sigaction(SIGSEGV, &sighandle, NULL);
283 sigaction(SIGUSR2, &sighandle, NULL);
284 sigaction(SIGALRM, &sighandle, NULL);
285 sigaction(SIGTERM, &sighandle, NULL);
287 sigaction(SIGSTKFLT, &sighandle, NULL);
289 sigaction(SIGSTOP, &sighandle, NULL);
290 sigaction(SIGTSTP, &sighandle, NULL);
291 sigaction(SIGTTIN, &sighandle, NULL);
292 sigaction(SIGTTOU, &sighandle, NULL);
293 sigaction(SIGURG, &sighandle, NULL);
294 sigaction(SIGVTALRM, &sighandle, NULL);
296 sigaction(SIGPWR, &sighandle, NULL);
299 sigaction(SIGWAITING,&sighandle, NULL);
302 sigaction(SIGLWP, &sighandle, NULL);
305 sigaction(SIGFREEZE, &sighandle, NULL);
308 sigaction(SIGTHAW, &sighandle, NULL);
311 sigaction(SIGCANCEL, &sighandle, NULL);
314 sigaction(SIGLOST, &sighandle, NULL);