]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/signal.c
5cb3c147be5c1daeafe44fb40fd42195cdad57cb
[bacula/bacula] / bacula / src / lib / signal.c
1 /*
2  *  Signal handlers for Bacula daemons
3  *
4  *   Kern Sibbald, April 2000
5  * 
6  * Note, we probably should do a core dump for the serious
7  * signals such as SIGBUS, SIGPFE, ... 
8  * Also, for SIGHUP and SIGUSR1, we should re-read the 
9  * configuration file.  However, since this is a "general"  
10  * routine, we leave it to the individual daemons to
11  * tweek their signals after calling this routine.
12  *
13  */
14
15 /*
16    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35
36 #include "bacula.h"
37
38 #ifndef _NSIG
39 #define BA_NSIG 100
40 #else
41 #define BA_NSIG _NSIG
42 #endif
43
44 extern char my_name[];
45 extern char *exepath;
46 extern char *exename;
47
48 static const char *sig_names[BA_NSIG+1];
49
50 typedef void (SIG_HANDLER)(int sig);
51 static SIG_HANDLER *exit_handler;
52
53 /* main process id */
54 static pid_t main_pid = 0;
55
56 /* 
57  * Handle signals here
58  */
59 static void signal_handler(int sig)
60 {
61    static int already_dead = FALSE;
62    struct sigaction sigdefault;
63
64    if (already_dead) {
65       _exit(1);
66    }
67    already_dead = sig;
68    if (sig == SIGTERM) {
69       Emsg1(M_TERM, -1, "Shutting down Bacula service: %s ...\n", my_name);
70    } else {
71       Emsg2(M_FATAL, -1, "Interrupted by signal %d: %s\n", sig, sig_names[sig]);
72    }
73
74 #ifdef TRACEBACK
75    if (sig != SIGTERM) {
76       static char *argv[4];
77       static char pid_buf[20];
78       static char btpath[400];
79       pid_t pid;
80
81       fprintf(stderr, "Kaboom! Got signal %d. Attempting traceback.\n", sig);
82       if (strlen(exepath) + 12 > (int)sizeof(btpath)) {
83          strcpy(btpath, "btraceback");
84       } else {
85          strcpy(btpath, exepath);
86          strcat(btpath, "/btraceback");
87       }
88       strcat(exepath, "/");
89       strcat(exepath, exename);
90       if (chdir(working_directory) !=0) {  /* dump in working directory */
91          Dmsg1(000, "chdir failed. ERR=%s\n", strerror(errno));
92       }
93       unlink("./core");               /* get rid of any old core file */
94       sprintf(pid_buf, "%d", main_pid);
95       Dmsg1(300, "Working=%s\n", working_directory);
96       Dmsg1(300, "btpath=%s\n", btpath);
97       Dmsg1(300, "exepath=%s\n", exepath);
98       switch (pid = fork()) {
99       case -1:                        /* error */
100          fprintf(stderr, "Fork error: ERR=%s\n", strerror(errno));
101          break;
102       case 0:                         /* child */
103          argv[0] = btpath;            /* path to btraceback */
104          argv[1] = exepath;           /* path to exe */
105          argv[2] = pid_buf;
106          argv[3] = (char *)NULL;
107          if (execv(btpath, argv) != 0) {
108             printf("execv: %s failed: ERR=%s\n", btpath, strerror(errno));
109          }
110          exit(-1);
111       default:                        /* parent */
112          break;
113       }
114       sigdefault.sa_flags = 0;
115       sigdefault.sa_handler = SIG_DFL;
116       sigfillset(&sigdefault.sa_mask);
117
118       sigaction(sig,  &sigdefault, NULL);
119       if (pid > 0) {
120          Dmsg0(500, "Doing waitpid\n");
121          waitpid(pid, NULL, 0);       /* wait for child to produce dump */
122          fprintf(stderr, "Traceback complete, attempting cleanup ...\n");
123          Dmsg0(500, "Done waitpid\n");
124          exit_handler(1);             /* clean up if possible */
125          Dmsg0(500, "Done exit_handler\n");
126       } else {
127          Dmsg0(500, "Doing sleep\n");
128          sleep(30);
129       }
130       fprintf(stderr, "It looks like the traceback worked ...\n");
131    }
132 #endif
133
134    exit_handler(1);
135 }
136
137 /*
138  * Init stack dump by saving main process id --
139  *   needed by debugger to attach to this program.
140  */
141 void init_stack_dump(void)
142 {
143    main_pid = getpid();               /* save main thread's pid */
144 }
145
146 /*
147  * Initialize signals
148  */
149 void init_signals(void terminate(int sig))
150 {
151    struct sigaction sighandle;
152    struct sigaction sigignore;
153    struct sigaction sigdefault;
154 #ifdef _sys_nsig
155    int i;
156
157    exit_handler = terminate;
158    if (BA_NSIG < _sys_nsig)
159       Emsg2(M_ABORT, 0, "BA_NSIG too small (%d) should be (%d)\n", BA_NSIG, _sys_nsig);
160
161    for (i=0; i<_sys_nsig; i++)
162       sig_names[i] = _sys_siglist[i];
163 #else
164    exit_handler = terminate;
165    sig_names[0]         = "UNKNOWN SIGNAL";
166    sig_names[SIGHUP]    = "Hangup";
167    sig_names[SIGINT]    = "Interrupt";
168    sig_names[SIGQUIT]   = "Quit";
169    sig_names[SIGILL]    = "Illegal instruction";;
170    sig_names[SIGTRAP]   = "Trace/Breakpoint trap";
171    sig_names[SIGABRT]   = "Abort";
172 #ifdef SIGEMT
173    sig_names[SIGEMT]    = "EMT instruction (Emulation Trap)";
174 #endif
175 #ifdef SIGIOT
176    sig_names[SIGIOT]    = "IOT trap";
177 #endif
178    sig_names[SIGBUS]    = "BUS error";
179    sig_names[SIGFPE]    = "Floating-point exception";
180    sig_names[SIGKILL]   = "Kill, unblockable";
181    sig_names[SIGUSR1]   = "User-defined signal 1";
182    sig_names[SIGSEGV]   = "Segmentation violation";
183    sig_names[SIGUSR2]   = "User-defined signal 2";
184    sig_names[SIGPIPE]   = "Broken pipe";
185    sig_names[SIGALRM]   = "Alarm clock";
186    sig_names[SIGTERM]   = "Termination";
187 #ifdef SIGSTKFLT
188    sig_names[SIGSTKFLT] = "Stack fault";
189 #endif
190    sig_names[SIGCHLD]   = "Child status has changed";
191    sig_names[SIGCONT]   = "Continue";
192    sig_names[SIGSTOP]   = "Stop, unblockable";
193    sig_names[SIGTSTP]   = "Keyboard stop";
194    sig_names[SIGTTIN]   = "Background read from tty";
195    sig_names[SIGTTOU]   = "Background write to tty";
196    sig_names[SIGURG]    = "Urgent condition on socket";
197    sig_names[SIGXCPU]   = "CPU limit exceeded";
198    sig_names[SIGXFSZ]   = "File size limit exceeded";
199    sig_names[SIGVTALRM] = "Virtual alarm clock";
200    sig_names[SIGPROF]   = "Profiling alarm clock";
201    sig_names[SIGWINCH]  = "Window size change";
202    sig_names[SIGIO]     = "I/O now possible";
203 #ifdef SIGPWR
204    sig_names[SIGPWR]    = "Power failure restart";
205 #endif
206 #ifdef SIGWAITING
207    sig_names[SIGWAITING] = "No runnable lwp";
208 #endif
209 #ifdef SIGLWP
210    sig_name[SIGLWP]     = "SIGLWP special signal used by thread library";
211 #endif
212 #ifdef SIGFREEZE
213    sig_names[SIGFREEZE] = "Checkpoint Freeze";
214 #endif
215 #ifdef SIGTHAW
216    sig_names[SIGTHAW]   = "Checkpoint Thaw";
217 #endif
218 #ifdef SIGCANCEL
219    sig_names[SIGCANCEL] = "Thread Cancellation";
220 #endif
221 #ifdef SIGLOST
222    sig_names[SIGLOST]   = "Resource Lost (e.g. record-lock lost)";
223 #endif
224 #endif
225
226
227 /* Now setup signal handlers */
228    sighandle.sa_flags = 0;
229    sighandle.sa_handler = signal_handler;
230    sigfillset(&sighandle.sa_mask);
231    sigignore.sa_flags = 0;
232    sigignore.sa_handler = SIG_IGN;       
233    sigfillset(&sigignore.sa_mask);
234    sigdefault.sa_flags = 0;
235    sigdefault.sa_handler = SIG_DFL;
236    sigfillset(&sigdefault.sa_mask);
237
238
239    sigaction(SIGPIPE,   &sigignore, NULL);
240    sigaction(SIGCHLD,   &sigignore, NULL);
241    sigaction(SIGCONT,   &sigignore, NULL);
242    sigaction(SIGPROF,   &sigignore, NULL);
243    sigaction(SIGWINCH,  &sigignore, NULL);
244    sigaction(SIGIO,     &sighandle, NULL);     
245
246    sigaction(SIGINT,    &sigdefault, NULL);    
247    sigaction(SIGXCPU,   &sigdefault, NULL);
248    sigaction(SIGXFSZ,   &sigdefault, NULL);
249
250    sigaction(SIGHUP,    &sighandle, NULL);
251    sigaction(SIGQUIT,   &sighandle, NULL);   
252    sigaction(SIGILL,    &sighandle, NULL);    
253    sigaction(SIGTRAP,   &sighandle, NULL);   
254 /* sigaction(SIGABRT,   &sighandle, NULL);   */
255 #ifdef SIGEMT
256    sigaction(SIGEMT,    &sighandle, NULL);
257 #endif
258 #ifdef SIGIOT
259 /* sigaction(SIGIOT,    &sighandle, NULL);  used by debugger */
260 #endif
261    sigaction(SIGBUS,    &sighandle, NULL);    
262    sigaction(SIGFPE,    &sighandle, NULL);    
263    sigaction(SIGKILL,   &sighandle, NULL);   
264    sigaction(SIGUSR1,   &sighandle, NULL);   
265    sigaction(SIGSEGV,   &sighandle, NULL);   
266    sigaction(SIGUSR2,   &sighandle, NULL);
267    sigaction(SIGALRM,   &sighandle, NULL);   
268    sigaction(SIGTERM,   &sighandle, NULL);   
269 #ifdef SIGSTKFLT
270    sigaction(SIGSTKFLT, &sighandle, NULL); 
271 #endif
272    sigaction(SIGSTOP,   &sighandle, NULL);   
273    sigaction(SIGTSTP,   &sighandle, NULL);   
274    sigaction(SIGTTIN,   &sighandle, NULL);   
275    sigaction(SIGTTOU,   &sighandle, NULL);   
276    sigaction(SIGURG,    &sighandle, NULL);    
277    sigaction(SIGVTALRM, &sighandle, NULL); 
278 #ifdef SIGPWR
279    sigaction(SIGPWR,    &sighandle, NULL);    
280 #endif
281 #ifdef SIGWAITING
282    sigaction(SIGWAITING,&sighandle, NULL);
283 #endif
284 #ifdef SIGLWP
285    sigaction(SIGLWP,    &sighandle, NULL);
286 #endif
287 #ifdef SIGFREEZE
288    sigaction(SIGFREEZE, &sighandle, NULL);
289 #endif
290 #ifdef SIGTHAW
291    sigaction(SIGTHAW,   &sighandle, NULL);
292 #endif
293 #ifdef SIGCANCEL
294    sigaction(SIGCANCEL, &sighandle, NULL);
295 #endif
296 #ifdef SIGLOST
297    sigaction(SIGLOST,   &sighandle, NULL);
298 #endif
299 }