2 Unix SMB/CIFS implementation.
3 Critical Fault handling
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sys/types.h>
35 #ifdef HAVE_BACKTRACE_SYMBOLS
38 #include <atalk/logger.h>
41 #define SIGNAL_CAST (RETSIGTYPE (*)(int))
43 #ifndef SAFE_FREE /* Oh no this is also defined in tdb.h */
44 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
46 #define BACKTRACE_STACK_SIZE 64
48 static void (*cont_fn)(void *);
50 /*******************************************************************
51 Catch a signal. This should implement the following semantics:
53 1) The handler remains installed after being called.
54 2) The signal should be blocked during handler execution.
55 ********************************************************************/
57 static void (*CatchSignal(int signum,void (*handler)(int )))(int)
61 struct sigaction oldact;
65 act.sa_handler = handler;
68 * We *want* SIGALRM to interrupt a system call.
71 act.sa_flags = SA_RESTART;
73 sigemptyset(&act.sa_mask);
74 sigaddset(&act.sa_mask,signum);
75 sigaction(signum,&act,&oldact);
76 return oldact.sa_handler;
77 #else /* !HAVE_SIGACTION */
78 /* FIXME: need to handle sigvec and systems with broken signal() */
79 return signal(signum, handler);
83 /*******************************************************************
84 Something really nasty happened - panic !
85 ********************************************************************/
87 static void smb_panic(const char *why)
93 #ifdef HAVE_BACKTRACE_SYMBOLS
94 void *backtrace_stack[BACKTRACE_STACK_SIZE];
95 size_t backtrace_size;
96 char **backtrace_strings;
101 extern char *global_clobber_region_function;
102 extern unsigned int global_clobber_region_line;
104 if (global_clobber_region_function) {
105 DEBUG(0,("smb_panic: clobber_region() last called from [%s(%u)]",
106 global_clobber_region_function,
107 global_clobber_region_line));
113 cmd = lp_panic_action();
115 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
116 result = system(cmd);
119 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
122 DEBUG(0, ("smb_panic(): action returned status %d\n",
123 WEXITSTATUS(result)));
125 DEBUG(0,("PANIC: %s\n", why));
128 #ifdef HAVE_BACKTRACE_SYMBOLS
129 /* get the backtrace (stack frames) */
130 backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
131 backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
133 LOG(log_error, logtype_default, "BACKTRACE: %d stack frames:\n", backtrace_size);
135 if (backtrace_strings) {
138 for (i = 0; i < backtrace_size; i++)
139 LOG(log_error, logtype_default, " #%u %s", i, backtrace_strings[i]);
141 SAFE_FREE(backtrace_strings);
149 /*******************************************************************
151 ********************************************************************/
152 static void fault_report(int sig)
156 if (counter) _exit(1);
160 LOG(log_error, logtype_default, "===============================================================");
161 LOG(log_error, logtype_default, "INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),VERSION);
162 LOG(log_error, logtype_default, "===============================================================");
164 smb_panic("internal error");
169 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
172 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
174 return; /* this should cause a core dump */
179 /****************************************************************************
181 ****************************************************************************/
182 static void sig_fault(int sig)
187 /*******************************************************************
188 setup our fault handlers
189 ********************************************************************/
190 void fault_setup(void (*fn)(void *))
195 CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
198 CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);