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