]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/signal.c
- Incremented the release number because this version requires
[bacula/bacula] / bacula / src / lib / signal.c
1 /*
2  *  Signal handlers for Bacula daemons
3  *
4  *   Kern Sibbald, April 2000
5  *
6  *   Version $Id$
7  * 
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.
14  *
15  */
16
17 /*
18    Copyright (C) 2000-2004 Kern Sibbald and John Walker
19
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.
24
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.
29
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,
33    MA 02111-1307, USA.
34
35  */
36
37 #ifndef HAVE_WIN32
38 #include "bacula.h"
39
40 #ifndef _NSIG
41 #define BA_NSIG 100
42 #else
43 #define BA_NSIG _NSIG
44 #endif
45
46 extern char my_name[];
47 extern char *exepath;
48 extern char *exename;
49
50 static const char *sig_names[BA_NSIG+1];
51
52 typedef void (SIG_HANDLER)(int sig);
53 static SIG_HANDLER *exit_handler;
54
55 /* main process id */
56 static pid_t main_pid = 0;
57
58 const char *get_signal_name(int sig)
59 {
60    if (sig < 0 || sig > BA_NSIG || !sig_names[sig]) {
61       return "Invalid signal number";
62    } else {
63       return sig_names[sig];
64    }
65 }
66
67 /* 
68  * Handle signals here
69  */
70 extern "C" void signal_handler(int sig)
71 {
72    static int already_dead = 0;
73
74    /* If we come back more than once, get out fast! */
75    if (already_dead) {
76       exit(1);
77    }
78    Dmsg2(200, "sig=%d %s\n", sig, sig_names[sig]);
79    /* Ignore certain signals -- SIGUSR2 used to interrupt threads */
80    if (sig == SIGCHLD || sig == SIGUSR2) {
81       return;
82    }
83    already_dead++;
84    if (sig == SIGTERM) {
85 //    Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
86    } else {
87       Emsg2(M_FATAL, -1, "Bacula interrupted by signal %d: %s\n", sig, sig_names[sig]);
88    }
89
90 #ifdef TRACEBACK
91    if (sig != SIGTERM) {
92       struct sigaction sigdefault;
93       static char *argv[4];
94       static char pid_buf[20];
95       static char btpath[400];
96       char buf[100];
97       pid_t pid;
98       int exelen = strlen(exepath);
99
100       fprintf(stderr, "Kaboom! %s, %s got signal %d. Attempting traceback.\n", 
101               exename, my_name, sig);
102
103       if (exelen + 12 > (int)sizeof(btpath)) {
104          bstrncpy(btpath, "btraceback", sizeof(btpath));
105       } else {
106          bstrncpy(btpath, exepath, sizeof(btpath));
107          if (btpath[exelen-1] == '/') {
108             btpath[exelen-1] = 0;
109          }
110          bstrncat(btpath, "/btraceback", sizeof(btpath));
111       }
112       if (exepath[exelen-1] != '/') {
113          strcat(exepath, "/");
114       }
115       strcat(exepath, exename);
116       if (!working_directory) {
117          working_directory = buf;
118          *buf = 0;
119       }
120       if (*working_directory == 0) {
121          strcpy((char *)working_directory, "/tmp/");
122       }
123       if (chdir(working_directory) != 0) {  /* dump in working directory */
124          berrno be;
125          Pmsg2(000, "chdir to %s failed. ERR=%s\n", working_directory,  be.strerror());
126          strcpy((char *)working_directory, "/tmp/");
127       }
128       unlink("./core");               /* get rid of any old core file */
129       sprintf(pid_buf, "%d", (int)main_pid);
130       Dmsg1(300, "Working=%s\n", working_directory);
131       Dmsg1(300, "btpath=%s\n", btpath);
132       Dmsg1(300, "exepath=%s\n", exepath);
133       switch (pid = fork()) {
134       case -1:                        /* error */
135          fprintf(stderr, "Fork error: ERR=%s\n", strerror(errno));
136          break;
137       case 0:                         /* child */
138          argv[0] = btpath;            /* path to btraceback */
139          argv[1] = exepath;           /* path to exe */
140          argv[2] = pid_buf;
141          argv[3] = (char *)NULL;
142          fprintf(stderr, "Calling: %s %s %s\n", btpath, exepath, pid_buf);
143          if (execv(btpath, argv) != 0) {
144             printf("execv: %s failed: ERR=%s\n", btpath, strerror(errno));
145          }
146          exit(-1);
147       default:                        /* parent */
148          break;
149       }
150       /* Parent continue here, waiting for child */
151       sigdefault.sa_flags = 0;
152       sigdefault.sa_handler = SIG_DFL;
153       sigfillset(&sigdefault.sa_mask);
154
155       sigaction(sig,  &sigdefault, NULL);
156       if (pid > 0) {
157          Dmsg0(500, "Doing waitpid\n");
158          waitpid(pid, NULL, 0);       /* wait for child to produce dump */
159          fprintf(stderr, "Traceback complete, attempting cleanup ...\n");
160          Dmsg0(500, "Done waitpid\n");
161          exit_handler(sig);           /* clean up if possible */
162          Dmsg0(500, "Done exit_handler\n");
163       } else {
164          Dmsg0(500, "Doing sleep\n");
165          bmicrosleep(30, 0);
166       }
167       fprintf(stderr, "It looks like the traceback worked ...\n");
168    }
169 #endif
170
171    exit_handler(sig);
172 }
173
174 /*
175  * Init stack dump by saving main process id --
176  *   needed by debugger to attach to this program.
177  */
178 void init_stack_dump(void)
179 {
180    main_pid = getpid();               /* save main thread's pid */
181 }
182
183 /*
184  * Initialize signals
185  */
186 void init_signals(void terminate(int sig))
187 {
188    struct sigaction sighandle;
189    struct sigaction sigignore;
190    struct sigaction sigdefault;
191 #ifdef _sys_nsig
192    int i;
193
194    exit_handler = terminate;
195    if (BA_NSIG < _sys_nsig)
196       Emsg2(M_ABORT, 0, "BA_NSIG too small (%d) should be (%d)\n", BA_NSIG, _sys_nsig);
197
198    for (i=0; i<_sys_nsig; i++)
199       sig_names[i] = _sys_siglist[i];
200 #else
201    exit_handler = terminate;
202    sig_names[0]         = "UNKNOWN SIGNAL";
203    sig_names[SIGHUP]    = "Hangup";
204    sig_names[SIGINT]    = "Interrupt";
205    sig_names[SIGQUIT]   = "Quit";
206    sig_names[SIGILL]    = "Illegal instruction";;
207    sig_names[SIGTRAP]   = "Trace/Breakpoint trap";
208    sig_names[SIGABRT]   = "Abort";
209 #ifdef SIGEMT
210    sig_names[SIGEMT]    = "EMT instruction (Emulation Trap)";
211 #endif
212 #ifdef SIGIOT
213    sig_names[SIGIOT]    = "IOT trap";
214 #endif
215    sig_names[SIGBUS]    = "BUS error";
216    sig_names[SIGFPE]    = "Floating-point exception";
217    sig_names[SIGKILL]   = "Kill, unblockable";
218    sig_names[SIGUSR1]   = "User-defined signal 1";
219    sig_names[SIGSEGV]   = "Segmentation violation";
220    sig_names[SIGUSR2]   = "User-defined signal 2";
221    sig_names[SIGPIPE]   = "Broken pipe";
222    sig_names[SIGALRM]   = "Alarm clock";
223    sig_names[SIGTERM]   = "Termination";
224 #ifdef SIGSTKFLT
225    sig_names[SIGSTKFLT] = "Stack fault";
226 #endif
227    sig_names[SIGCHLD]   = "Child status has changed";
228    sig_names[SIGCONT]   = "Continue";
229    sig_names[SIGSTOP]   = "Stop, unblockable";
230    sig_names[SIGTSTP]   = "Keyboard stop";
231    sig_names[SIGTTIN]   = "Background read from tty";
232    sig_names[SIGTTOU]   = "Background write to tty";
233    sig_names[SIGURG]    = "Urgent condition on socket";
234    sig_names[SIGXCPU]   = "CPU limit exceeded";
235    sig_names[SIGXFSZ]   = "File size limit exceeded";
236    sig_names[SIGVTALRM] = "Virtual alarm clock";
237    sig_names[SIGPROF]   = "Profiling alarm clock";
238    sig_names[SIGWINCH]  = "Window size change";
239    sig_names[SIGIO]     = "I/O now possible";
240 #ifdef SIGPWR
241    sig_names[SIGPWR]    = "Power failure restart";
242 #endif
243 #ifdef SIGWAITING
244    sig_names[SIGWAITING] = "No runnable lwp";
245 #endif
246 #ifdef SIGLWP
247    sig_name[SIGLWP]     = "SIGLWP special signal used by thread library";
248 #endif
249 #ifdef SIGFREEZE
250    sig_names[SIGFREEZE] = "Checkpoint Freeze";
251 #endif
252 #ifdef SIGTHAW
253    sig_names[SIGTHAW]   = "Checkpoint Thaw";
254 #endif
255 #ifdef SIGCANCEL
256    sig_names[SIGCANCEL] = "Thread Cancellation";
257 #endif
258 #ifdef SIGLOST
259    sig_names[SIGLOST]   = "Resource Lost (e.g. record-lock lost)";
260 #endif
261 #endif
262
263
264 /* Now setup signal handlers */
265    sighandle.sa_flags = 0;
266    sighandle.sa_handler = signal_handler;
267    sigfillset(&sighandle.sa_mask);
268    sigignore.sa_flags = 0;
269    sigignore.sa_handler = SIG_IGN;       
270    sigfillset(&sigignore.sa_mask);
271    sigdefault.sa_flags = 0;
272    sigdefault.sa_handler = SIG_DFL;
273    sigfillset(&sigdefault.sa_mask);
274
275
276    sigaction(SIGPIPE,   &sigignore, NULL);
277    sigaction(SIGCHLD,   &sighandle, NULL);
278    sigaction(SIGCONT,   &sigignore, NULL);
279    sigaction(SIGPROF,   &sigignore, NULL);
280    sigaction(SIGWINCH,  &sigignore, NULL);
281    sigaction(SIGIO,     &sighandle, NULL);     
282
283    sigaction(SIGINT,    &sigdefault, NULL);    
284    sigaction(SIGXCPU,   &sigdefault, NULL);
285    sigaction(SIGXFSZ,   &sigdefault, NULL);
286
287    sigaction(SIGHUP,    &sigignore, NULL);
288    sigaction(SIGQUIT,   &sighandle, NULL);   
289    sigaction(SIGILL,    &sighandle, NULL);    
290    sigaction(SIGTRAP,   &sighandle, NULL);   
291 /* sigaction(SIGABRT,   &sighandle, NULL);   */
292 #ifdef SIGEMT
293    sigaction(SIGEMT,    &sighandle, NULL);
294 #endif
295 #ifdef SIGIOT
296 /* sigaction(SIGIOT,    &sighandle, NULL);  used by debugger */
297 #endif
298    sigaction(SIGBUS,    &sighandle, NULL);    
299    sigaction(SIGFPE,    &sighandle, NULL);    
300    sigaction(SIGKILL,   &sighandle, NULL);   
301    sigaction(SIGUSR1,   &sighandle, NULL);   
302    sigaction(SIGSEGV,   &sighandle, NULL);   
303    sigaction(SIGUSR2,   &sighandle, NULL);
304    sigaction(SIGALRM,   &sighandle, NULL);   
305    sigaction(SIGTERM,   &sighandle, NULL);   
306 #ifdef SIGSTKFLT
307    sigaction(SIGSTKFLT, &sighandle, NULL); 
308 #endif
309    sigaction(SIGSTOP,   &sighandle, NULL);   
310    sigaction(SIGTSTP,   &sighandle, NULL);   
311    sigaction(SIGTTIN,   &sighandle, NULL);   
312    sigaction(SIGTTOU,   &sighandle, NULL);   
313    sigaction(SIGURG,    &sighandle, NULL);    
314    sigaction(SIGVTALRM, &sighandle, NULL); 
315 #ifdef SIGPWR
316    sigaction(SIGPWR,    &sighandle, NULL);    
317 #endif
318 #ifdef SIGWAITING
319    sigaction(SIGWAITING,&sighandle, NULL);
320 #endif
321 #ifdef SIGLWP
322    sigaction(SIGLWP,    &sighandle, NULL);
323 #endif
324 #ifdef SIGFREEZE
325    sigaction(SIGFREEZE, &sighandle, NULL);
326 #endif
327 #ifdef SIGTHAW
328    sigaction(SIGTHAW,   &sighandle, NULL);
329 #endif
330 #ifdef SIGCANCEL
331    sigaction(SIGCANCEL, &sighandle, NULL);
332 #endif
333 #ifdef SIGLOST
334    sigaction(SIGLOST,   &sighandle, NULL);
335 #endif
336 }
337 #endif