2 Unix SMB/CIFS implementation.
3 Critical Fault handling
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Prouty 2009
6 Copyright (C) James Peach 2006
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "system/wait.h"
27 #ifdef HAVE_SYS_SYSCTL_H
28 #include <sys/sysctl.h>
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
37 #include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
38 #include "substitute.h"
43 smb_panic_handler_t panic_handler
;
47 /*******************************************************************
48 setup variables used for fault handling
49 ********************************************************************/
50 void fault_configure(smb_panic_handler_t panic_handler
)
52 fault_state
.panic_handler
= panic_handler
;
57 disable setting up fault handlers
58 This is used for the bind9 dlz module, as we
59 don't want a Samba module in bind9 to override the bind
62 _PUBLIC_
void fault_setup_disable(void)
64 fault_state
.disabled
= true;
68 /*******************************************************************
70 ********************************************************************/
71 static void fault_report(int sig
)
75 if (counter
) _exit(1);
80 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig
,(int)getpid(),SAMBA_VERSION_STRING
));
81 DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba HOWTO\n"));
84 smb_panic("internal error");
86 /* smb_panic() never returns, so this is really redundant */
90 /****************************************************************************
92 ****************************************************************************/
93 static void sig_fault(int sig
)
98 /*******************************************************************
99 setup our fault handlers
100 ********************************************************************/
101 void fault_setup(void)
103 if (fault_state
.disabled
) {
106 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
108 CatchSignal(SIGSEGV
, sig_fault
);
111 CatchSignal(SIGBUS
, sig_fault
);
114 CatchSignal(SIGABRT
, sig_fault
);
119 _PUBLIC_
const char *panic_action
= NULL
;
122 default smb_panic() implementation
124 static void smb_panic_default(const char *why
) _NORETURN_
;
125 static void smb_panic_default(const char *why
)
127 DBG_ERR("PANIC (pid %llu): %s\n",
128 (unsigned long long)getpid(), why
);
130 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
132 * Make sure all children can attach a debugger.
134 prctl(PR_SET_PTRACER
, getpid(), 0, 0, 0);
137 if (panic_action
&& *panic_action
) {
139 if (strlcpy(cmdstring
, panic_action
, sizeof(cmdstring
)) < sizeof(cmdstring
)) {
142 snprintf(pidstr
, sizeof(pidstr
), "%d", (int) getpid());
143 all_string_sub(cmdstring
, "%d", pidstr
, sizeof(cmdstring
));
144 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring
));
145 result
= system(cmdstring
);
148 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
151 DEBUG(0, ("smb_panic(): action returned status %d\n",
152 WEXITSTATUS(result
)));
157 CatchSignal(SIGABRT
, SIG_DFL
);
164 Something really nasty happened - panic !
166 _PUBLIC_
void smb_panic(const char *why
)
168 if (fault_state
.panic_handler
) {
169 fault_state
.panic_handler(why
);
172 smb_panic_default(why
);
177 /*******************************************************************
178 Print a backtrace of the stack to the debug log. This function
179 DELIBERATELY LEAKS MEMORY. The expectation is that you should
180 exit shortly after calling it.
181 ********************************************************************/
183 /* Buffer size to use when printing backtraces */
184 #define BACKTRACE_STACK_SIZE 64
187 #ifdef HAVE_LIBUNWIND_H
188 #include <libunwind.h>
191 #ifdef HAVE_EXECINFO_H
192 #include <execinfo.h>
195 void log_stack_trace(void)
197 #ifdef HAVE_LIBUNWIND
198 /* Try to use libunwind before any other technique since on ia64
199 * libunwind correctly walks the stack in more circumstances than
207 unw_word_t ip
, sp
, off
;
209 procname
[sizeof(procname
) - 1] = '\0';
211 if (unw_getcontext(&uc
) != 0) {
212 goto libunwind_failed
;
215 if (unw_init_local(&cursor
, &uc
) != 0) {
216 goto libunwind_failed
;
219 DEBUG(0, ("BACKTRACE:\n"));
223 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
224 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
226 switch (unw_get_proc_name(&cursor
,
227 procname
, sizeof(procname
) - 1, &off
) ) {
231 /* Name truncated. */
232 DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
233 i
, procname
, (long long)off
,
234 (long long)ip
, (long long) sp
));
237 /* case -UNW_ENOINFO: */
238 /* case -UNW_EUNSPEC: */
239 /* No symbol name found. */
240 DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
241 i
, "<unknown symbol>",
242 (long long)ip
, (long long) sp
));
245 } while (unw_step(&cursor
) > 0);
250 DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
252 #elif HAVE_BACKTRACE_SYMBOLS
253 void *backtrace_stack
[BACKTRACE_STACK_SIZE
];
254 size_t backtrace_size
;
255 char **backtrace_strings
;
257 /* get the backtrace (stack frames) */
258 backtrace_size
= backtrace(backtrace_stack
,BACKTRACE_STACK_SIZE
);
259 backtrace_strings
= backtrace_symbols(backtrace_stack
, backtrace_size
);
261 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
262 (unsigned long)backtrace_size
));
264 if (backtrace_strings
) {
267 for (i
= 0; i
< backtrace_size
; i
++)
268 DEBUGADD(0, (" #%u %s\n", i
, backtrace_strings
[i
]));
270 /* Leak the backtrace_strings, rather than risk what free() might do */
274 DEBUG(0, ("unable to produce a stack trace on this platform\n"));