Line data Source code
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 "fault.h"
39 : #include "util_process.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 50012 : void fault_configure(smb_panic_handler_t panic_handler)
51 : {
52 50012 : fault_state.panic_handler = panic_handler;
53 50012 : }
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 15 : _PUBLIC_ void fault_setup_disable(void)
63 : {
64 15 : fault_state.disabled = true;
65 15 : }
66 :
67 :
68 : #if !defined(HAVE_DISABLE_FAULT_HANDLING)
69 : /*******************************************************************
70 : report a fault
71 : ********************************************************************/
72 0 : static void fault_report(int sig)
73 : {
74 0 : static int counter;
75 0 : char signal_string[128];
76 :
77 0 : if (counter) _exit(1);
78 :
79 0 : counter++;
80 :
81 0 : snprintf(signal_string, sizeof(signal_string),
82 : "Signal %d: %s", sig, strsignal(sig));
83 0 : smb_panic(signal_string);
84 :
85 : /* smb_panic() never returns, so this is really redundant */
86 : exit(1);
87 : }
88 :
89 : /****************************************************************************
90 : catch serious errors
91 : ****************************************************************************/
92 0 : static void sig_fault(int sig)
93 : {
94 0 : fault_report(sig);
95 0 : }
96 : #endif
97 : /*******************************************************************
98 : setup our fault handlers
99 : ********************************************************************/
100 34488 : void fault_setup(void)
101 : {
102 34488 : if (fault_state.disabled) {
103 0 : return;
104 : }
105 : #if !defined(HAVE_DISABLE_FAULT_HANDLING)
106 : #ifdef SIGSEGV
107 34488 : CatchSignal(SIGSEGV, sig_fault);
108 : #endif
109 : #ifdef SIGBUS
110 34488 : CatchSignal(SIGBUS, sig_fault);
111 : #endif
112 : #ifdef SIGABRT
113 34488 : CatchSignal(SIGABRT, sig_fault);
114 : #endif
115 : #endif
116 : }
117 :
118 : _PUBLIC_ const char *panic_action = NULL;
119 :
120 : /*
121 : default smb_panic() implementation
122 : */
123 : static void smb_panic_default(const char *why) _NORETURN_;
124 0 : static void smb_panic_default(const char *why)
125 : {
126 : #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
127 : /*
128 : * Make sure all children can attach a debugger.
129 : */
130 0 : prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
131 : #endif
132 :
133 0 : if (panic_action && *panic_action) {
134 0 : char cmdstring[200];
135 0 : if (strlcpy(cmdstring, panic_action, sizeof(cmdstring)) < sizeof(cmdstring)) {
136 0 : int result;
137 0 : char pidstr[20];
138 0 : char subst[200];
139 0 : char *p = NULL;
140 0 : snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid());
141 :
142 0 : p = strstr(cmdstring, "%d");
143 0 : if (p != NULL) {
144 0 : snprintf(subst,
145 : sizeof(subst),
146 : "%.*s%s%s",
147 0 : (int)(p-cmdstring),
148 : cmdstring,
149 : pidstr,
150 : p+2);
151 0 : strlcpy(cmdstring, subst, sizeof(cmdstring));
152 : }
153 :
154 0 : DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring));
155 0 : result = system(cmdstring);
156 :
157 0 : if (result == -1)
158 0 : DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
159 : strerror(errno)));
160 : else
161 0 : DEBUG(0, ("smb_panic(): action returned status %d\n",
162 : WEXITSTATUS(result)));
163 : }
164 : }
165 :
166 : #ifdef SIGABRT
167 0 : CatchSignal(SIGABRT, SIG_DFL);
168 : #endif
169 0 : abort();
170 : }
171 :
172 0 : _PUBLIC_ void smb_panic_log(const char *why)
173 : {
174 0 : const char *binary_name = process_get_saved_binary_name();
175 0 : const char *short_title = process_get_short_title();
176 0 : const char *long_title = process_get_long_title();
177 :
178 0 : DEBUGSEP(0);
179 0 : DEBUG(0,("INTERNAL ERROR: %s in %s (%s) (%s) pid %lld (%s)\n",
180 : why,
181 : binary_name,
182 : short_title,
183 : long_title,
184 : (unsigned long long)getpid(),
185 : SAMBA_VERSION_STRING));
186 0 : DEBUG(0,("If you are running a recent Samba version, and "
187 : "if you think this problem is not yet fixed in the "
188 : "latest versions, please consider reporting this "
189 : "bug, see "
190 : "https://wiki.samba.org/index.php/Bug_Reporting\n"));
191 0 : DEBUGSEP(0);
192 0 : DEBUG(0,("PANIC (pid %llu): %s in " SAMBA_VERSION_STRING "\n",
193 : (unsigned long long)getpid(), why));
194 :
195 0 : log_stack_trace();
196 0 : }
197 :
198 : /**
199 : Something really nasty happened - panic !
200 :
201 : This function is in this file to allow sharing the last set process
202 : title into the logs before the backtrace
203 : **/
204 0 : _PUBLIC_ void smb_panic(const char *why)
205 : {
206 0 : smb_panic_log(why);
207 :
208 0 : if (fault_state.panic_handler) {
209 0 : fault_state.panic_handler(why);
210 0 : _exit(1);
211 : }
212 0 : smb_panic_default(why);
213 : }
214 :
215 : /*******************************************************************
216 : Print a backtrace of the stack to the debug log. This function
217 : DELIBERATELY LEAKS MEMORY. The expectation is that you should
218 : exit shortly after calling it.
219 : ********************************************************************/
220 :
221 : /* Buffer size to use when printing backtraces */
222 : #define BACKTRACE_STACK_SIZE 64
223 :
224 :
225 : #ifdef HAVE_LIBUNWIND_H
226 : #include <libunwind.h>
227 : #endif
228 :
229 : #ifdef HAVE_EXECINFO_H
230 : #include <execinfo.h>
231 : #endif
232 :
233 0 : void log_stack_trace(void)
234 : {
235 : #ifdef HAVE_LIBUNWIND
236 : /*
237 : * --with-libunwind is required to use libunwind, the
238 : * backtrace_symbols() code below is the default.
239 : *
240 : * This code is available because a previous version of this
241 : * comment asserted that on ia64 libunwind correctly walks the
242 : * stack in more circumstances than backtrace.
243 : */
244 : unw_cursor_t cursor;
245 : unw_context_t uc;
246 : unsigned i = 0;
247 :
248 : char procname[256];
249 : unw_word_t ip, sp, off;
250 :
251 : procname[sizeof(procname) - 1] = '\0';
252 :
253 : if (unw_getcontext(&uc) != 0) {
254 : goto libunwind_failed;
255 : }
256 :
257 : if (unw_init_local(&cursor, &uc) != 0) {
258 : goto libunwind_failed;
259 : }
260 :
261 : DEBUG(0, ("BACKTRACE:\n"));
262 :
263 : do {
264 : ip = sp = 0;
265 : unw_get_reg(&cursor, UNW_REG_IP, &ip);
266 : unw_get_reg(&cursor, UNW_REG_SP, &sp);
267 :
268 : switch (unw_get_proc_name(&cursor,
269 : procname, sizeof(procname) - 1, &off) ) {
270 : case 0:
271 : /* Name found. */
272 : case -UNW_ENOMEM:
273 : /* Name truncated. */
274 : DEBUGADD(0, (" #%u %s + %#llx [ip=%#llx] [sp=%#llx]\n",
275 : i, procname, (long long)off,
276 : (long long)ip, (long long) sp));
277 : break;
278 : default:
279 : /* case -UNW_ENOINFO: */
280 : /* case -UNW_EUNSPEC: */
281 : /* No symbol name found. */
282 : DEBUGADD(0, (" #%u %s [ip=%#llx] [sp=%#llx]\n",
283 : i, "<unknown symbol>",
284 : (long long)ip, (long long) sp));
285 : }
286 : ++i;
287 : } while (unw_step(&cursor) > 0);
288 :
289 : return;
290 :
291 : libunwind_failed:
292 : DEBUG(0, ("unable to produce a stack trace with libunwind\n"));
293 :
294 : #elif defined(HAVE_BACKTRACE_SYMBOLS)
295 0 : void *backtrace_stack[BACKTRACE_STACK_SIZE];
296 0 : size_t backtrace_size;
297 0 : char **backtrace_strings;
298 :
299 : /* get the backtrace (stack frames) */
300 0 : backtrace_size = backtrace(backtrace_stack,BACKTRACE_STACK_SIZE);
301 0 : backtrace_strings = backtrace_symbols(backtrace_stack, backtrace_size);
302 :
303 0 : DEBUG(0, ("BACKTRACE: %lu stack frames:\n",
304 : (unsigned long)backtrace_size));
305 :
306 0 : if (backtrace_strings) {
307 : size_t i;
308 :
309 0 : for (i = 0; i < backtrace_size; i++)
310 0 : DEBUGADD(0, (" #%zu %s\n", i, backtrace_strings[i]));
311 :
312 : /* Leak the backtrace_strings, rather than risk what free() might do */
313 : }
314 :
315 : #else
316 : DEBUG(0, ("unable to produce a stack trace on this platform\n"));
317 : #endif
318 0 : }
|