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