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 */
42 smb_panic_handler_t panic_handler
;
46 /*******************************************************************
47 setup variables used for fault handling
48 ********************************************************************/
49 void fault_configure(smb_panic_handler_t panic_handler
)
51 fault_state
.panic_handler
= panic_handler
;
56 disable setting up fault handlers
57 This is used for the bind9 dlz module, as we
58 don't want a Samba module in bind9 to override the bind
61 _PUBLIC_
void fault_setup_disable(void)
63 fault_state
.disabled
= true;
67 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
68 /*******************************************************************
70 ********************************************************************/
71 static void fault_report(int sig
)
74 char signal_string
[128];
76 if (counter
) _exit(1);
80 snprintf(signal_string
, sizeof(signal_string
),
81 "Signal %d: %s", sig
, strsignal(sig
));
82 smb_panic(signal_string
);
84 /* smb_panic() never returns, so this is really redundant */
88 /****************************************************************************
90 ****************************************************************************/
91 static void sig_fault(int sig
)
96 /*******************************************************************
97 setup our fault handlers
98 ********************************************************************/
99 void fault_setup(void)
101 if (fault_state
.disabled
) {
104 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
106 CatchSignal(SIGSEGV
, sig_fault
);
109 CatchSignal(SIGBUS
, sig_fault
);
112 CatchSignal(SIGABRT
, sig_fault
);
117 _PUBLIC_
const char *panic_action
= NULL
;
120 default smb_panic() implementation
122 static void smb_panic_default(const char *why
) _NORETURN_
;
123 static void smb_panic_default(const char *why
)
125 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
127 * Make sure all children can attach a debugger.
129 prctl(PR_SET_PTRACER
, getpid(), 0, 0, 0);
132 if (panic_action
&& *panic_action
) {
134 if (strlcpy(cmdstring
, panic_action
, sizeof(cmdstring
)) < sizeof(cmdstring
)) {
139 snprintf(pidstr
, sizeof(pidstr
), "%d", (int) getpid());
141 p
= strstr(cmdstring
, "%d");
150 strlcpy(cmdstring
, subst
, sizeof(cmdstring
));
153 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring
));
154 result
= system(cmdstring
);
157 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
160 DEBUG(0, ("smb_panic(): action returned status %d\n",
161 WEXITSTATUS(result
)));
166 CatchSignal(SIGABRT
, SIG_DFL
);
171 _PUBLIC_
void smb_panic_log(const char *why
)
174 DEBUG(0,("INTERNAL ERROR: %s in pid %lld (%s)\n",
176 (unsigned long long)getpid(),
177 SAMBA_VERSION_STRING
));
178 DEBUG(0,("If you are running a recent Samba version, and "
179 "if you think this problem is not yet fixed in the "
180 "latest versions, please consider reporting this "
182 "https://wiki.samba.org/index.php/Bug_Reporting\n"));
184 DEBUG(0,("PANIC (pid %llu): %s in " SAMBA_VERSION_STRING
"\n",
185 (unsigned long long)getpid(), why
));
191 Something really nasty happened - panic !
193 _PUBLIC_
void smb_panic(const char *why
)
197 if (fault_state
.panic_handler
) {
198 fault_state
.panic_handler(why
);
201 smb_panic_default(why
);
204 /*******************************************************************
205 Print a backtrace of the stack to the debug log. This function
206 DELIBERATELY LEAKS MEMORY. The expectation is that you should
207 exit shortly after calling it.
208 ********************************************************************/
210 /* Buffer size to use when printing backtraces */
211 #define BACKTRACE_STACK_SIZE 64
214 #ifdef HAVE_LIBUNWIND_H
215 #include <libunwind.h>
218 #ifdef HAVE_EXECINFO_H
219 #include <execinfo.h>
222 void log_stack_trace(void)
224 #ifdef HAVE_LIBUNWIND
226 * --with-libunwind is required to use libunwind, the
227 * backtrace_symbols() code below is the default.
229 * This code is available because a previous version of this
230 * comment asserted that on ia64 libunwind correctly walks the
231 * stack in more circumstances than backtrace.
238 unw_word_t ip
, sp
, off
;
240 procname
[sizeof(procname
) - 1] = '\0';
242 if (unw_getcontext(&uc
) != 0) {
243 goto libunwind_failed
;
246 if (unw_init_local(&cursor
, &uc
) != 0) {
247 goto libunwind_failed
;
250 DEBUG(0, ("BACKTRACE:\n"));
254 unw_get_reg(&cursor
, UNW_REG_IP
, &ip
);
255 unw_get_reg(&cursor
, UNW_REG_SP
, &sp
);
257 switch (unw_get_proc_name(&cursor
,
258 procname
, sizeof(procname
) - 1, &off
) ) {
262 /* Name truncated. */
263 DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
264 i
, procname
, (long long)off
,
265 (long long)ip
, (long long) sp
));
268 /* case -UNW_ENOINFO: */
269 /* case -UNW_EUNSPEC: */
270 /* No symbol name found. */
271 DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
272 i
, "<unknown symbol>",
273 (long long)ip
, (long long) sp
));
276 } while (unw_step(&cursor
) > 0);
281 DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
283 #elif defined(HAVE_BACKTRACE_SYMBOLS)
284 void *backtrace_stack
[BACKTRACE_STACK_SIZE
];
285 size_t backtrace_size
;
286 char **backtrace_strings
;
288 /* get the backtrace (stack frames) */
289 backtrace_size
= backtrace(backtrace_stack
,BACKTRACE_STACK_SIZE
);
290 backtrace_strings
= backtrace_symbols(backtrace_stack
, backtrace_size
);
292 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
293 (unsigned long)backtrace_size
));
295 if (backtrace_strings
) {
298 for (i
= 0; i
< backtrace_size
; i
++)
299 DEBUGADD(0, (" #%zu %s\n", i
, backtrace_strings
[i
]));
301 /* Leak the backtrace_strings, rather than risk what free() might do */
305 DEBUG(0, ("unable to produce a stack trace on this platform\n"));