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