r7831: use cn=TEST as base of test DNs so we don't interfere with potentially real...
[Samba/ekacnet.git] / source4 / lib / fault.c
blobf3a9e78f39895a1d2726a94c890a0e36d4e8f2d7
1 /*
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.
21 #include "includes.h"
22 #include "version.h"
23 #include "system/wait.h"
24 #include "system/filesys.h"
26 static void (*cont_fn)(void *);
28 /* the registered fault handler */
29 static struct {
30 const char *name;
31 void (*fault_handler)(int sig);
32 } fault_handlers;
35 #ifdef HAVE_BACKTRACE
36 #include <execinfo.h>
37 #define BACKTRACE_STACK_SIZE 64
38 #elif HAVE_LIBEXC_H
39 #include <libexc.h>
40 #endif
42 void call_backtrace(void)
44 #ifdef HAVE_BACKTRACE
45 #define BACKTRACE_STACK_SIZE 64
46 void *backtrace_stack[BACKTRACE_STACK_SIZE];
47 size_t backtrace_size;
48 char **backtrace_strings;
50 /* get the backtrace (stack frames) */
51 backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
52 backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
54 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
55 (unsigned long)backtrace_size));
57 if (backtrace_strings) {
58 int i;
60 for (i = 0; i < backtrace_size; i++)
61 DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
63 /* Leak the backtrace_strings, rather than risk what free() might do */
66 #elif HAVE_LIBEXC
68 #define NAMESIZE 32 /* Arbitrary */
70 /* The IRIX libexc library provides an API for unwinding the stack. See
71 * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
72 * since we are about to abort anyway, it hardly matters.
74 * Note that if we paniced due to a SIGSEGV or SIGBUS (or similar) this
75 * will fail with a nasty message upon failing to open the /proc entry.
78 uint64_t addrs[BACKTRACE_STACK_SIZE];
79 char * names[BACKTRACE_STACK_SIZE];
80 char namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
82 int i;
83 int levels;
85 ZERO_ARRAY(addrs);
86 ZERO_ARRAY(names);
87 ZERO_ARRAY(namebuf);
89 for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
90 names[i] = namebuf + (i * NAMESIZE);
93 levels = trace_back_stack(0, addrs, names,
94 BACKTRACE_STACK_SIZE, NAMESIZE);
96 DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
97 for (i = 0; i < levels; i++) {
98 DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
101 #undef NAMESIZE
102 #endif
105 /*******************************************************************
106 Something really nasty happened - panic !
107 ********************************************************************/
108 void smb_panic(const char *why)
110 const char *cmd = lp_panic_action();
111 int result;
113 if (cmd && *cmd) {
114 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmd));
115 result = system(cmd);
117 if (result == -1)
118 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
119 strerror(errno)));
120 else
121 DEBUG(0, ("smb_panic(): action returned status %d\n",
122 WEXITSTATUS(result)));
124 DEBUG(0,("PANIC: %s\n", why));
126 call_backtrace();
128 #ifdef SIGABRT
129 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
130 #endif
131 abort();
134 /*******************************************************************
135 report a fault
136 ********************************************************************/
137 static void fault_report(int sig)
139 static int counter;
141 if (counter) _exit(1);
143 DEBUG(0,("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"));
144 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),SAMBA_VERSION_STRING));
145 DEBUG(0,("\nPlease read the file BUGS.txt in the distribution\n"));
146 DEBUG(0,("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"));
148 smb_panic("internal error");
150 if (cont_fn) {
151 cont_fn(NULL);
152 #ifdef SIGSEGV
153 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
154 #endif
155 #ifdef SIGBUS
156 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
157 #endif
158 #ifdef SIGABRT
159 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
160 #endif
161 return; /* this should cause a core dump */
163 exit(1);
166 /****************************************************************************
167 catch serious errors
168 ****************************************************************************/
169 static void sig_fault(int sig)
171 if (fault_handlers.fault_handler) {
172 /* we have a fault handler, call it. It may not return. */
173 fault_handlers.fault_handler(sig);
175 /* If it returns or doean't exist, use regular reporter */
176 fault_report(sig);
179 /*******************************************************************
180 setup our fault handlers
181 ********************************************************************/
182 void fault_setup(void (*fn)(void *))
184 cont_fn = fn;
186 #ifdef SIGSEGV
187 CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
188 #endif
189 #ifdef SIGBUS
190 CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
191 #endif
192 #ifdef SIGABRT
193 CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
194 #endif
198 register a fault handler.
199 Should only be called once in the execution of smbd.
201 BOOL register_fault_handler(const char *name, void (*fault_handler)(int sig))
203 if (fault_handlers.name != NULL) {
204 /* it's already registered! */
205 DEBUG(2,("fault handler '%s' already registered - failed '%s'\n",
206 fault_handlers.name, name));
207 return False;
210 fault_handlers.name = name;
211 fault_handlers.fault_handler = fault_handler;
213 DEBUG(2,("fault handler '%s' registered\n", name));
214 return True;