2 * Signal handlers for Bacula daemons
4 * Kern Sibbald, April 2000
6 * Note, we probably should do a core dump for the serious
7 * signals such as SIGBUS, SIGPFE, ...
8 * Also, for SIGHUP and SIGUSR1, we should re-read the
9 * configuration file. However, since this is a "general"
10 * routine, we leave it to the individual daemons to
11 * tweek their signals after calling this routine.
16 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation; either version 2 of
21 the License, or (at your option) any later version.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public
29 License along with this program; if not, write to the Free
30 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
44 extern char my_name[];
48 static const char *sig_names[BA_NSIG+1];
50 typedef void (SIG_HANDLER)(int sig);
51 static SIG_HANDLER *exit_handler;
54 static pid_t main_pid = 0;
59 static void signal_handler(int sig)
61 static int already_dead = FALSE;
62 struct sigaction sigdefault;
69 Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
71 Emsg2(M_FATAL, -1, "Interrupted by signal %d: %s\n", sig, sig_names[sig]);
77 static char pid_buf[20];
78 static char btpath[400];
81 fprintf(stderr, "Kaboom! Got signal %d. Attempting traceback.\n", sig);
82 if (strlen(exepath) + 12 > (int)sizeof(btpath)) {
83 strcpy(btpath, "btraceback");
85 strcpy(btpath, exepath);
86 strcat(btpath, "/btraceback");
89 strcat(exepath, exename);
90 if (chdir(working_directory) !=0) { /* dump in working directory */
91 Dmsg1(000, "chdir failed. ERR=%s\n", strerror(errno));
93 unlink("./core"); /* get rid of any old core file */
94 sprintf(pid_buf, "%d", main_pid);
95 Dmsg1(300, "Working=%s\n", working_directory);
96 Dmsg1(300, "btpath=%s\n", btpath);
97 Dmsg1(300, "exepath=%s\n", exepath);
98 switch (pid = fork()) {
100 fprintf(stderr, "Fork error: ERR=%s\n", strerror(errno));
103 argv[0] = btpath; /* path to btraceback */
104 argv[1] = exepath; /* path to exe */
106 argv[3] = (char *)NULL;
107 if (execv(btpath, argv) != 0) {
108 printf("execv: %s failed: ERR=%s\n", btpath, strerror(errno));
111 default: /* parent */
114 sigdefault.sa_flags = 0;
115 sigdefault.sa_handler = SIG_DFL;
116 sigfillset(&sigdefault.sa_mask);
118 sigaction(sig, &sigdefault, NULL);
120 Dmsg0(500, "Doing waitpid\n");
121 waitpid(pid, NULL, 0); /* wait for child to produce dump */
122 fprintf(stderr, "Traceback complete, attempting cleanup ...\n");
123 Dmsg0(500, "Done waitpid\n");
124 exit_handler(1); /* clean up if possible */
125 Dmsg0(500, "Done exit_handler\n");
127 Dmsg0(500, "Doing sleep\n");
130 fprintf(stderr, "It looks like the traceback worked ...\n");
138 * Init stack dump by saving main process id --
139 * needed by debugger to attach to this program.
141 void init_stack_dump(void)
143 main_pid = getpid(); /* save main thread's pid */
149 void init_signals(void terminate(int sig))
151 struct sigaction sighandle;
152 struct sigaction sigignore;
153 struct sigaction sigdefault;
157 exit_handler = terminate;
158 if (BA_NSIG < _sys_nsig)
159 Emsg2(M_ABORT, 0, "BA_NSIG too small (%d) should be (%d)\n", BA_NSIG, _sys_nsig);
161 for (i=0; i<_sys_nsig; i++)
162 sig_names[i] = _sys_siglist[i];
164 exit_handler = terminate;
165 sig_names[0] = "UNKNOWN SIGNAL";
166 sig_names[SIGHUP] = "Hangup";
167 sig_names[SIGINT] = "Interrupt";
168 sig_names[SIGQUIT] = "Quit";
169 sig_names[SIGILL] = "Illegal instruction";;
170 sig_names[SIGTRAP] = "Trace/Breakpoint trap";
171 sig_names[SIGABRT] = "Abort";
173 sig_names[SIGEMT] = "EMT instruction (Emulation Trap)";
176 sig_names[SIGIOT] = "IOT trap";
178 sig_names[SIGBUS] = "BUS error";
179 sig_names[SIGFPE] = "Floating-point exception";
180 sig_names[SIGKILL] = "Kill, unblockable";
181 sig_names[SIGUSR1] = "User-defined signal 1";
182 sig_names[SIGSEGV] = "Segmentation violation";
183 sig_names[SIGUSR2] = "User-defined signal 2";
184 sig_names[SIGPIPE] = "Broken pipe";
185 sig_names[SIGALRM] = "Alarm clock";
186 sig_names[SIGTERM] = "Termination";
188 sig_names[SIGSTKFLT] = "Stack fault";
190 sig_names[SIGCHLD] = "Child status has changed";
191 sig_names[SIGCONT] = "Continue";
192 sig_names[SIGSTOP] = "Stop, unblockable";
193 sig_names[SIGTSTP] = "Keyboard stop";
194 sig_names[SIGTTIN] = "Background read from tty";
195 sig_names[SIGTTOU] = "Background write to tty";
196 sig_names[SIGURG] = "Urgent condition on socket";
197 sig_names[SIGXCPU] = "CPU limit exceeded";
198 sig_names[SIGXFSZ] = "File size limit exceeded";
199 sig_names[SIGVTALRM] = "Virtual alarm clock";
200 sig_names[SIGPROF] = "Profiling alarm clock";
201 sig_names[SIGWINCH] = "Window size change";
202 sig_names[SIGIO] = "I/O now possible";
204 sig_names[SIGPWR] = "Power failure restart";
207 sig_names[SIGWAITING] = "No runnable lwp";
210 sig_name[SIGLWP] = "SIGLWP special signal used by thread library";
213 sig_names[SIGFREEZE] = "Checkpoint Freeze";
216 sig_names[SIGTHAW] = "Checkpoint Thaw";
219 sig_names[SIGCANCEL] = "Thread Cancellation";
222 sig_names[SIGLOST] = "Resource Lost (e.g. record-lock lost)";
227 /* Now setup signal handlers */
228 sighandle.sa_flags = 0;
229 sighandle.sa_handler = signal_handler;
230 sigfillset(&sighandle.sa_mask);
231 sigignore.sa_flags = 0;
232 sigignore.sa_handler = SIG_IGN;
233 sigfillset(&sigignore.sa_mask);
234 sigdefault.sa_flags = 0;
235 sigdefault.sa_handler = SIG_DFL;
236 sigfillset(&sigdefault.sa_mask);
239 sigaction(SIGPIPE, &sigignore, NULL);
240 sigaction(SIGCHLD, &sigignore, NULL);
241 sigaction(SIGCONT, &sigignore, NULL);
242 sigaction(SIGPROF, &sigignore, NULL);
243 sigaction(SIGWINCH, &sigignore, NULL);
244 sigaction(SIGIO, &sighandle, NULL);
246 sigaction(SIGINT, &sigdefault, NULL);
247 sigaction(SIGXCPU, &sigdefault, NULL);
248 sigaction(SIGXFSZ, &sigdefault, NULL);
250 sigaction(SIGHUP, &sighandle, NULL);
251 sigaction(SIGQUIT, &sighandle, NULL);
252 sigaction(SIGILL, &sighandle, NULL);
253 sigaction(SIGTRAP, &sighandle, NULL);
254 /* sigaction(SIGABRT, &sighandle, NULL); */
256 sigaction(SIGEMT, &sighandle, NULL);
259 /* sigaction(SIGIOT, &sighandle, NULL); used by debugger */
261 sigaction(SIGBUS, &sighandle, NULL);
262 sigaction(SIGFPE, &sighandle, NULL);
263 sigaction(SIGKILL, &sighandle, NULL);
264 sigaction(SIGUSR1, &sighandle, NULL);
265 sigaction(SIGSEGV, &sighandle, NULL);
266 sigaction(SIGUSR2, &sighandle, NULL);
267 sigaction(SIGALRM, &sighandle, NULL);
268 sigaction(SIGTERM, &sighandle, NULL);
270 sigaction(SIGSTKFLT, &sighandle, NULL);
272 sigaction(SIGSTOP, &sighandle, NULL);
273 sigaction(SIGTSTP, &sighandle, NULL);
274 sigaction(SIGTTIN, &sighandle, NULL);
275 sigaction(SIGTTOU, &sighandle, NULL);
276 sigaction(SIGURG, &sighandle, NULL);
277 sigaction(SIGVTALRM, &sighandle, NULL);
279 sigaction(SIGPWR, &sighandle, NULL);
282 sigaction(SIGWAITING,&sighandle, NULL);
285 sigaction(SIGLWP, &sighandle, NULL);
288 sigaction(SIGFREEZE, &sighandle, NULL);
291 sigaction(SIGTHAW, &sighandle, NULL);
294 sigaction(SIGCANCEL, &sighandle, NULL);
297 sigaction(SIGLOST, &sighandle, NULL);