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;
64 struct sigaction sigdefault;
71 Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
73 Emsg2(M_FATAL, -1, "Bacula interrupted by signal %d: %s\n", sig, sig_names[sig]);
79 static char pid_buf[20];
80 static char btpath[400];
83 fprintf(stderr, "Kaboom! %s, %s got signal %d. Attempting traceback.\n",
84 exename, my_name, sig);
86 if (strlen(exepath) + 12 > (int)sizeof(btpath)) {
87 strcpy(btpath, "btraceback");
89 strcpy(btpath, exepath);
90 strcat(btpath, "/btraceback");
93 strcat(exepath, exename);
94 if (chdir(working_directory) !=0) { /* dump in working directory */
95 Pmsg2(000, "chdir to %s failed. ERR=%s\n", working_directory, strerror(errno));
97 unlink("./core"); /* get rid of any old core file */
98 sprintf(pid_buf, "%d", (int)main_pid);
99 Dmsg1(300, "Working=%s\n", working_directory);
100 Dmsg1(300, "btpath=%s\n", btpath);
101 Dmsg1(300, "exepath=%s\n", exepath);
102 switch (pid = fork()) {
104 fprintf(stderr, "Fork error: ERR=%s\n", strerror(errno));
107 argv[0] = btpath; /* path to btraceback */
108 argv[1] = exepath; /* path to exe */
110 argv[3] = (char *)NULL;
111 if (execv(btpath, argv) != 0) {
112 printf("execv: %s failed: ERR=%s\n", btpath, strerror(errno));
115 default: /* parent */
118 sigdefault.sa_flags = 0;
119 sigdefault.sa_handler = SIG_DFL;
120 sigfillset(&sigdefault.sa_mask);
122 sigaction(sig, &sigdefault, NULL);
124 Dmsg0(500, "Doing waitpid\n");
125 waitpid(pid, NULL, 0); /* wait for child to produce dump */
126 fprintf(stderr, "Traceback complete, attempting cleanup ...\n");
127 Dmsg0(500, "Done waitpid\n");
128 exit_handler(1); /* clean up if possible */
129 Dmsg0(500, "Done exit_handler\n");
131 Dmsg0(500, "Doing sleep\n");
134 fprintf(stderr, "It looks like the traceback worked ...\n");
142 * Init stack dump by saving main process id --
143 * needed by debugger to attach to this program.
145 void init_stack_dump(void)
147 main_pid = getpid(); /* save main thread's pid */
153 void init_signals(void terminate(int sig))
155 struct sigaction sighandle;
156 struct sigaction sigignore;
157 struct sigaction sigdefault;
161 exit_handler = terminate;
162 if (BA_NSIG < _sys_nsig)
163 Emsg2(M_ABORT, 0, "BA_NSIG too small (%d) should be (%d)\n", BA_NSIG, _sys_nsig);
165 for (i=0; i<_sys_nsig; i++)
166 sig_names[i] = _sys_siglist[i];
168 exit_handler = terminate;
169 sig_names[0] = "UNKNOWN SIGNAL";
170 sig_names[SIGHUP] = "Hangup";
171 sig_names[SIGINT] = "Interrupt";
172 sig_names[SIGQUIT] = "Quit";
173 sig_names[SIGILL] = "Illegal instruction";;
174 sig_names[SIGTRAP] = "Trace/Breakpoint trap";
175 sig_names[SIGABRT] = "Abort";
177 sig_names[SIGEMT] = "EMT instruction (Emulation Trap)";
180 sig_names[SIGIOT] = "IOT trap";
182 sig_names[SIGBUS] = "BUS error";
183 sig_names[SIGFPE] = "Floating-point exception";
184 sig_names[SIGKILL] = "Kill, unblockable";
185 sig_names[SIGUSR1] = "User-defined signal 1";
186 sig_names[SIGSEGV] = "Segmentation violation";
187 sig_names[SIGUSR2] = "User-defined signal 2";
188 sig_names[SIGPIPE] = "Broken pipe";
189 sig_names[SIGALRM] = "Alarm clock";
190 sig_names[SIGTERM] = "Termination";
192 sig_names[SIGSTKFLT] = "Stack fault";
194 sig_names[SIGCHLD] = "Child status has changed";
195 sig_names[SIGCONT] = "Continue";
196 sig_names[SIGSTOP] = "Stop, unblockable";
197 sig_names[SIGTSTP] = "Keyboard stop";
198 sig_names[SIGTTIN] = "Background read from tty";
199 sig_names[SIGTTOU] = "Background write to tty";
200 sig_names[SIGURG] = "Urgent condition on socket";
201 sig_names[SIGXCPU] = "CPU limit exceeded";
202 sig_names[SIGXFSZ] = "File size limit exceeded";
203 sig_names[SIGVTALRM] = "Virtual alarm clock";
204 sig_names[SIGPROF] = "Profiling alarm clock";
205 sig_names[SIGWINCH] = "Window size change";
206 sig_names[SIGIO] = "I/O now possible";
208 sig_names[SIGPWR] = "Power failure restart";
211 sig_names[SIGWAITING] = "No runnable lwp";
214 sig_name[SIGLWP] = "SIGLWP special signal used by thread library";
217 sig_names[SIGFREEZE] = "Checkpoint Freeze";
220 sig_names[SIGTHAW] = "Checkpoint Thaw";
223 sig_names[SIGCANCEL] = "Thread Cancellation";
226 sig_names[SIGLOST] = "Resource Lost (e.g. record-lock lost)";
231 /* Now setup signal handlers */
232 sighandle.sa_flags = 0;
233 sighandle.sa_handler = signal_handler;
234 sigfillset(&sighandle.sa_mask);
235 sigignore.sa_flags = 0;
236 sigignore.sa_handler = SIG_IGN;
237 sigfillset(&sigignore.sa_mask);
238 sigdefault.sa_flags = 0;
239 sigdefault.sa_handler = SIG_DFL;
240 sigfillset(&sigdefault.sa_mask);
243 sigaction(SIGPIPE, &sigignore, NULL);
244 sigaction(SIGCHLD, &sigignore, NULL);
245 sigaction(SIGCONT, &sigignore, NULL);
246 sigaction(SIGPROF, &sigignore, NULL);
247 sigaction(SIGWINCH, &sigignore, NULL);
248 sigaction(SIGIO, &sighandle, NULL);
250 sigaction(SIGINT, &sigdefault, NULL);
251 sigaction(SIGXCPU, &sigdefault, NULL);
252 sigaction(SIGXFSZ, &sigdefault, NULL);
254 sigaction(SIGHUP, &sigignore, NULL);
255 sigaction(SIGQUIT, &sighandle, NULL);
256 sigaction(SIGILL, &sighandle, NULL);
257 sigaction(SIGTRAP, &sighandle, NULL);
258 /* sigaction(SIGABRT, &sighandle, NULL); */
260 sigaction(SIGEMT, &sighandle, NULL);
263 /* sigaction(SIGIOT, &sighandle, NULL); used by debugger */
265 sigaction(SIGBUS, &sighandle, NULL);
266 sigaction(SIGFPE, &sighandle, NULL);
267 sigaction(SIGKILL, &sighandle, NULL);
268 sigaction(SIGUSR1, &sighandle, NULL);
269 sigaction(SIGSEGV, &sighandle, NULL);
270 sigaction(SIGUSR2, &sighandle, NULL);
271 sigaction(SIGALRM, &sighandle, NULL);
272 sigaction(SIGTERM, &sighandle, NULL);
274 sigaction(SIGSTKFLT, &sighandle, NULL);
276 sigaction(SIGSTOP, &sighandle, NULL);
277 sigaction(SIGTSTP, &sighandle, NULL);
278 sigaction(SIGTTIN, &sighandle, NULL);
279 sigaction(SIGTTOU, &sighandle, NULL);
280 sigaction(SIGURG, &sighandle, NULL);
281 sigaction(SIGVTALRM, &sighandle, NULL);
283 sigaction(SIGPWR, &sighandle, NULL);
286 sigaction(SIGWAITING,&sighandle, NULL);
289 sigaction(SIGLWP, &sighandle, NULL);
292 sigaction(SIGFREEZE, &sighandle, NULL);
295 sigaction(SIGTHAW, &sighandle, NULL);
298 sigaction(SIGCANCEL, &sighandle, NULL);
301 sigaction(SIGLOST, &sighandle, NULL);