0274262298054ad5d080045ba8649d559a562768
[metze/samba/wip.git] / lib / util / fault.c
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "replace.h"
23 #include "system/filesys.h"
24 #include "system/wait.h"
25 #include "version.h"
26
27 #ifdef HAVE_SYS_SYSCTL_H
28 #include <sys/sysctl.h>
29 #endif
30
31
32 #ifdef HAVE_SYS_PRCTL_H
33 #include <sys/prctl.h>
34 #endif
35
36 #include "debug.h"
37 #include "lib/util/signal.h" /* Avoid /usr/include/signal.h */
38 #include "substitute.h"
39 #include "fault.h"
40
41 static struct {
42         bool disabled;
43         smb_panic_handler_t panic_handler;
44 } fault_state;
45
46
47 /*******************************************************************
48 setup variables used for fault handling
49 ********************************************************************/
50 void fault_configure(smb_panic_handler_t panic_handler)
51 {
52         fault_state.panic_handler = panic_handler;
53 }
54
55
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)
63 {
64         fault_state.disabled = true;
65 }
66
67
68 /*******************************************************************
69 report a fault
70 ********************************************************************/
71 static void fault_report(int sig)
72 {
73         static int counter;
74
75         if (counter) _exit(1);
76
77         counter++;
78
79         DEBUGSEP(0);
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"));
82         DEBUGSEP(0);
83
84         smb_panic("internal error");
85
86         /* smb_panic() never returns, so this is really redundant */
87         exit(1);
88 }
89
90 /****************************************************************************
91 catch serious errors
92 ****************************************************************************/
93 static void sig_fault(int sig)
94 {
95         fault_report(sig);
96 }
97
98 /*******************************************************************
99 setup our fault handlers
100 ********************************************************************/
101 void fault_setup(void)
102 {
103         static bool mxinit;
104
105         if (mxinit) {
106                 return;
107         }
108         mxinit = true;
109
110         if (fault_state.disabled) {
111                 return;
112         }
113 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
114 #ifdef SIGSEGV
115         CatchSignal(SIGSEGV, sig_fault);
116 #endif
117 #ifdef SIGBUS
118         CatchSignal(SIGBUS, sig_fault);
119 #endif
120 #ifdef SIGABRT
121         CatchSignal(SIGABRT, sig_fault);
122 #endif
123 #endif
124 }
125
126 _PUBLIC_ const char *panic_action = NULL;
127
128 /*
129    default smb_panic() implementation
130 */
131 static void smb_panic_default(const char *why) _NORETURN_;
132 static void smb_panic_default(const char *why)
133 {
134         DBG_ERR("PANIC (pid %llu): %s\n",
135                     (unsigned long long)getpid(), why);
136         log_stack_trace();
137
138 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
139         /*
140          * Make sure all children can attach a debugger.
141          */
142         prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
143 #endif
144
145         if (panic_action && *panic_action) {
146                 char cmdstring[200];
147                 if (strlcpy(cmdstring, panic_action, sizeof(cmdstring)) < sizeof(cmdstring)) {
148                         int result;
149                         char pidstr[20];
150                         snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid());
151                         all_string_sub(cmdstring, "%d", pidstr, sizeof(cmdstring));
152                         DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring));
153                         result = system(cmdstring);
154
155                         if (result == -1)
156                                 DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
157                                           strerror(errno)));
158                         else
159                                 DEBUG(0, ("smb_panic(): action returned status %d\n",
160                                           WEXITSTATUS(result)));
161                 }
162         }
163
164 #ifdef SIGABRT
165         CatchSignal(SIGABRT, SIG_DFL);
166 #endif
167         abort();
168 }
169
170
171 /**
172    Something really nasty happened - panic !
173 **/
174 _PUBLIC_ void smb_panic(const char *why)
175 {
176         if (fault_state.panic_handler) {
177                 fault_state.panic_handler(why);
178                 _exit(1);
179         }
180         smb_panic_default(why);
181 }
182
183 /*******************************************************************
184  Print a backtrace of the stack to the debug log. This function
185  DELIBERATELY LEAKS MEMORY. The expectation is that you should
186  exit shortly after calling it.
187 ********************************************************************/
188
189 /* Buffer size to use when printing backtraces */
190 #define BACKTRACE_STACK_SIZE 64
191
192
193 #ifdef HAVE_LIBUNWIND_H
194 #include <libunwind.h>
195 #endif
196
197 #ifdef HAVE_EXECINFO_H
198 #include <execinfo.h>
199 #endif
200
201 void log_stack_trace(void)
202 {
203 #ifdef HAVE_LIBUNWIND
204         /* Try to use libunwind before any other technique since on ia64
205          * libunwind correctly walks the stack in more circumstances than
206          * backtrace.
207          */
208         unw_cursor_t cursor;
209         unw_context_t uc;
210         unsigned i = 0;
211
212         char procname[256];
213         unw_word_t ip, sp, off;
214
215         procname[sizeof(procname) - 1] = '\0';
216
217         if (unw_getcontext(&uc) != 0) {
218                 goto libunwind_failed;
219         }
220
221         if (unw_init_local(&cursor, &uc) != 0) {
222                 goto libunwind_failed;
223         }
224
225         DEBUG(0, ("BACKTRACE:\n"));
226
227         do {
228             ip = sp = 0;
229             unw_get_reg(&cursor, UNW_REG_IP, &ip);
230             unw_get_reg(&cursor, UNW_REG_SP, &sp);
231
232             switch (unw_get_proc_name(&cursor,
233                         procname, sizeof(procname) - 1, &off) ) {
234             case 0:
235                     /* Name found. */
236             case -UNW_ENOMEM:
237                     /* Name truncated. */
238                     DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
239                             i, procname, (long long)off,
240                             (long long)ip, (long long) sp));
241                     break;
242             default:
243             /* case -UNW_ENOINFO: */
244             /* case -UNW_EUNSPEC: */
245                     /* No symbol name found. */
246                     DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
247                             i, "<unknown symbol>",
248                             (long long)ip, (long long) sp));
249             }
250             ++i;
251         } while (unw_step(&cursor) > 0);
252
253         return;
254
255 libunwind_failed:
256         DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
257
258 #elif defined(HAVE_BACKTRACE_SYMBOLS)
259         void *backtrace_stack[BACKTRACE_STACK_SIZE];
260         size_t backtrace_size;
261         char **backtrace_strings;
262
263         /* get the backtrace (stack frames) */
264         backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
265         backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
266
267         DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
268                   (unsigned long)backtrace_size));
269
270         if (backtrace_strings) {
271                 int i;
272
273                 for (i = 0; i < backtrace_size; i++)
274                         DEBUGADD(0, (" #%u %s\n", i, backtrace_strings[i]));
275
276                 /* Leak the backtrace_strings, rather than risk what free() might do */
277         }
278
279 #else
280         DEBUG(0, ("unable to produce a stack trace on this platform\n"));
281 #endif
282 }