s3-winbind: Added a common rpc_query_user function.
[Samba.git] / lib / util / fault.c
blob11b31d751e8f46ab7f52b8d2a1dd082de2a382ff
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "version.h"
22 #include "system/wait.h"
23 #include "system/filesys.h"
25 /**
26 * @file
27 * @brief Fault handling
30 /* the registered fault handler */
31 static struct {
32 const char *name;
33 void (*fault_handler)(int sig);
34 } fault_handlers;
36 static const char *progname;
38 #ifdef HAVE_BACKTRACE
39 #include <execinfo.h>
40 #elif HAVE_LIBEXC_H
41 #include <libexc.h>
42 #endif
44 /**
45 * Write backtrace to debug log
47 _PUBLIC_ void call_backtrace(void)
49 #ifdef HAVE_BACKTRACE
50 #ifndef BACKTRACE_STACK_SIZE
51 #define BACKTRACE_STACK_SIZE 64
52 #endif
53 void *backtrace_stack[BACKTRACE_STACK_SIZE];
54 size_t backtrace_size;
55 char **backtrace_strings;
57 /* get the backtrace (stack frames) */
58 backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
59 backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
61 DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
62 (unsigned long)backtrace_size));
64 if (backtrace_strings) {
65 int i;
67 for (i = 0; i < backtrace_size; i++)
68 DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
70 /* Leak the backtrace_strings, rather than risk what free() might do */
73 #elif HAVE_LIBEXC
75 #define NAMESIZE 32 /* Arbitrary */
76 #ifndef BACKTRACE_STACK_SIZE
77 #define BACKTRACE_STACK_SIZE 64
78 #endif
80 /* The IRIX libexc library provides an API for unwinding the stack. See
81 * libexc(3) for details. Apparantly trace_back_stack leaks memory, but
82 * since we are about to abort anyway, it hardly matters.
84 * Note that if we paniced due to a SIGSEGV or SIGBUS (or similar) this
85 * will fail with a nasty message upon failing to open the /proc entry.
88 uint64_t addrs[BACKTRACE_STACK_SIZE];
89 char * names[BACKTRACE_STACK_SIZE];
90 char namebuf[BACKTRACE_STACK_SIZE * NAMESIZE];
92 int i;
93 int levels;
95 ZERO_ARRAY(addrs);
96 ZERO_ARRAY(names);
97 ZERO_ARRAY(namebuf);
99 for (i = 0; i < BACKTRACE_STACK_SIZE; i++) {
100 names[i] = namebuf + (i * NAMESIZE);
103 levels = trace_back_stack(0, addrs, names,
104 BACKTRACE_STACK_SIZE, NAMESIZE);
106 DEBUG(0, ("BACKTRACE: %d stack frames:\n", levels));
107 for (i = 0; i < levels; i++) {
108 DEBUGADD(0, (" #%d 0x%llx %s\n", i, addrs[i], names[i]));
111 #undef NAMESIZE
112 #endif
115 _PUBLIC_ const char *panic_action = NULL;
118 Something really nasty happened - panic !
120 _PUBLIC_ _NORETURN_ void smb_panic(const char *why)
122 int result;
124 if (panic_action && *panic_action) {
125 char pidstr[20];
126 char cmdstring[200];
127 safe_strcpy(cmdstring, panic_action, sizeof(cmdstring));
128 snprintf(pidstr, sizeof(pidstr), "%u", getpid());
129 all_string_sub(cmdstring, "%PID%", pidstr, sizeof(cmdstring));
130 if (progname) {
131 all_string_sub(cmdstring, "%PROG%", progname, sizeof(cmdstring));
133 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring));
134 result = system(cmdstring);
136 if (result == -1)
137 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
138 strerror(errno)));
139 else
140 DEBUG(0, ("smb_panic(): action returned status %d\n",
141 WEXITSTATUS(result)));
143 DEBUG(0,("PANIC: %s\n", why));
145 call_backtrace();
147 #ifdef SIGABRT
148 CatchSignal(SIGABRT, SIG_DFL);
149 #endif
150 abort();
154 report a fault
156 _NORETURN_ static void fault_report(int sig)
158 static int counter;
160 if (counter) _exit(1);
162 DEBUG(0,("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"));
163 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),SAMBA_VERSION_STRING));
164 DEBUG(0,("\nPlease read the file BUGS.txt in the distribution\n"));
165 DEBUG(0,("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n"));
167 smb_panic("internal error");
169 exit(1);
173 catch serious errors
175 _NORETURN_ static void sig_fault(int sig)
177 if (fault_handlers.fault_handler) {
178 /* we have a fault handler, call it. It may not return. */
179 fault_handlers.fault_handler(sig);
181 /* If it returns or doesn't exist, use regular reporter */
182 fault_report(sig);
186 setup our fault handlers
188 _PUBLIC_ void fault_setup(const char *pname)
190 if (progname == NULL) {
191 progname = pname;
193 #ifdef SIGSEGV
194 CatchSignal(SIGSEGV, sig_fault);
195 #endif
196 #ifdef SIGBUS
197 CatchSignal(SIGBUS, sig_fault);
198 #endif
199 #ifdef SIGABRT
200 CatchSignal(SIGABRT, sig_fault);
201 #endif
202 #ifdef SIGFPE
203 CatchSignal(SIGFPE, sig_fault);
204 #endif
208 register a fault handler.
209 Should only be called once in the execution of smbd.
211 _PUBLIC_ bool register_fault_handler(const char *name,
212 void (*fault_handler)(int sig))
214 if (fault_handlers.name != NULL) {
215 /* it's already registered! */
216 DEBUG(2,("fault handler '%s' already registered - failed '%s'\n",
217 fault_handlers.name, name));
218 return false;
221 fault_handlers.name = name;
222 fault_handlers.fault_handler = fault_handler;
224 DEBUG(2,("fault handler '%s' registered\n", name));
225 return true;