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