auth:tests: Only enable torture_gnutls_aes_128_cfb() on GnuTLS >= 3.6.11
[Samba.git] / lib / util / fault.c
blobc42bc51789ae56a9f67e116227cf7b57398b5bde
1 /*
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/>.
22 #include "replace.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "version.h"
27 #ifdef HAVE_SYS_SYSCTL_H
28 #include <sys/sysctl.h>
29 #endif
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
36 #include "debug.h"
37 #include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
38 #include "substitute.h"
39 #include "fault.h"
41 static struct {
42 bool disabled;
43 smb_panic_handler_t panic_handler;
44 } fault_state;
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;
56 /**
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
60 fault handling
61 **/
62 _PUBLIC_ void fault_setup_disable(void)
64 fault_state.disabled = true;
68 /*******************************************************************
69 report a fault
70 ********************************************************************/
71 static void fault_report(int sig)
73 static int counter;
75 if (counter) _exit(1);
77 counter++;
79 DEBUGSEP(0);
80 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),SAMBA_VERSION_STRING));
81 DEBUG(0,("\nIf you are running a recent Samba version, and "
82 "if you think this problem is not yet fixed in the "
83 "latest versions, please consider reporting this "
84 "bug, see "
85 "https://wiki.samba.org/index.php/Bug_Reporting\n"));
86 DEBUGSEP(0);
88 smb_panic("internal error");
90 /* smb_panic() never returns, so this is really redundant */
91 exit(1);
94 /****************************************************************************
95 catch serious errors
96 ****************************************************************************/
97 static void sig_fault(int sig)
99 fault_report(sig);
102 /*******************************************************************
103 setup our fault handlers
104 ********************************************************************/
105 void fault_setup(void)
107 if (fault_state.disabled) {
108 return;
110 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
111 #ifdef SIGSEGV
112 CatchSignal(SIGSEGV, sig_fault);
113 #endif
114 #ifdef SIGBUS
115 CatchSignal(SIGBUS, sig_fault);
116 #endif
117 #ifdef SIGABRT
118 CatchSignal(SIGABRT, sig_fault);
119 #endif
120 #endif
123 _PUBLIC_ const char *panic_action = NULL;
126 default smb_panic() implementation
128 static void smb_panic_default(const char *why) _NORETURN_;
129 static void smb_panic_default(const char *why)
131 DBG_ERR("PANIC (pid %llu): %s\n",
132 (unsigned long long)getpid(), why);
133 log_stack_trace();
135 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
137 * Make sure all children can attach a debugger.
139 prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
140 #endif
142 if (panic_action && *panic_action) {
143 char cmdstring[200];
144 if (strlcpy(cmdstring, panic_action, sizeof(cmdstring)) < sizeof(cmdstring)) {
145 int result;
146 char pidstr[20];
147 snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid());
148 all_string_sub(cmdstring, "%d", pidstr, sizeof(cmdstring));
149 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring));
150 result = system(cmdstring);
152 if (result == -1)
153 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
154 strerror(errno)));
155 else
156 DEBUG(0, ("smb_panic(): action returned status %d\n",
157 WEXITSTATUS(result)));
161 #ifdef SIGABRT
162 CatchSignal(SIGABRT, SIG_DFL);
163 #endif
164 abort();
169 Something really nasty happened - panic !
171 _PUBLIC_ void smb_panic(const char *why)
173 if (fault_state.panic_handler) {
174 fault_state.panic_handler(why);
175 _exit(1);
177 smb_panic_default(why);
180 /*******************************************************************
181 Print a backtrace of the stack to the debug log. This function
182 DELIBERATELY LEAKS MEMORY. The expectation is that you should
183 exit shortly after calling it.
184 ********************************************************************/
186 /* Buffer size to use when printing backtraces */
187 #define BACKTRACE_STACK_SIZE 64
190 #ifdef HAVE_LIBUNWIND_H
191 #include <libunwind.h>
192 #endif
194 #ifdef HAVE_EXECINFO_H
195 #include <execinfo.h>
196 #endif
198 void log_stack_trace(void)
200 #ifdef HAVE_LIBUNWIND
201 /* Try to use libunwind before any other technique since on ia64
202 * libunwind correctly walks the stack in more circumstances than
203 * backtrace.
205 unw_cursor_t cursor;
206 unw_context_t uc;
207 unsigned i = 0;
209 char procname[256];
210 unw_word_t ip, sp, off;
212 procname[sizeof(procname) - 1] = '\0';
214 if (unw_getcontext(&uc) != 0) {
215 goto libunwind_failed;
218 if (unw_init_local(&cursor, &uc) != 0) {
219 goto libunwind_failed;
222 DEBUG(0, ("BACKTRACE:\n"));
224 do {
225 ip = sp = 0;
226 unw_get_reg(&cursor, UNW_REG_IP, &ip);
227 unw_get_reg(&cursor, UNW_REG_SP, &sp);
229 switch (unw_get_proc_name(&cursor,
230 procname, sizeof(procname) - 1, &off) ) {
231 case 0:
232 /* Name found. */
233 case -UNW_ENOMEM:
234 /* Name truncated. */
235 DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
236 i, procname, (long long)off,
237 (long long)ip, (long long) sp));
238 break;
239 default:
240 /* case -UNW_ENOINFO: */
241 /* case -UNW_EUNSPEC: */
242 /* No symbol name found. */
243 DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
244 i, "<unknown symbol>",
245 (long long)ip, (long long) sp));
247 ++i;
248 } while (unw_step(&cursor) > 0);
250 return;
252 libunwind_failed:
253 DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
255 #elif defined(HAVE_BACKTRACE_SYMBOLS)
256 void *backtrace_stack[BACKTRACE_STACK_SIZE];
257 size_t backtrace_size;
258 char **backtrace_strings;
260 /* get the backtrace (stack frames) */
261 backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
262 backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
264 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
265 (unsigned long)backtrace_size));
267 if (backtrace_strings) {
268 size_t i;
270 for (i = 0; i < backtrace_size; i++)
271 DEBUGADD(0, (" #%zu %s\n", i, backtrace_strings[i]));
273 /* Leak the backtrace_strings, rather than risk what free() might do */
276 #else
277 DEBUG(0, ("unable to produce a stack trace on this platform\n"));
278 #endif