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