util: Add --disable-fault-handling.
[sfrench/samba-autobuild/.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
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "version.h"
24
25 #ifdef HAVE_SYS_SYSCTL_H
26 #include <sys/sysctl.h>
27 #endif
28
29
30 #ifdef HAVE_SYS_PRCTL_H
31 #include <sys/prctl.h>
32 #endif
33
34 static struct {
35         bool disabled;
36         smb_panic_handler_t panic_handler;
37 } fault_state;
38
39
40 /*******************************************************************
41 setup variables used for fault handling
42 ********************************************************************/
43 void fault_configure(smb_panic_handler_t panic_handler)
44 {
45         fault_state.panic_handler = panic_handler;
46 }
47
48
49 /**
50    disable setting up fault handlers
51    This is used for the bind9 dlz module, as we
52    don't want a Samba module in bind9 to override the bind
53    fault handling
54 **/
55 _PUBLIC_ void fault_setup_disable(void)
56 {
57         fault_state.disabled = true;
58 }
59
60
61 /*******************************************************************
62 report a fault
63 ********************************************************************/
64 static void fault_report(int sig)
65 {
66         static int counter;
67
68         if (counter) _exit(1);
69
70         counter++;
71
72         DEBUGSEP(0);
73         DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),SAMBA_VERSION_STRING));
74         DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba HOWTO\n"));
75         DEBUGSEP(0);
76
77         smb_panic("internal error");
78
79         /* smb_panic() never returns, so this is really redundent */
80         exit(1);
81 }
82
83 /****************************************************************************
84 catch serious errors
85 ****************************************************************************/
86 static void sig_fault(int sig)
87 {
88         fault_report(sig);
89 }
90
91 /*******************************************************************
92 setup our fault handlers
93 ********************************************************************/
94 void fault_setup(void)
95 {
96         if (fault_state.disabled) {
97                 return;
98         }
99 #if !defined(HAVE_DISABLE_FAULT_HANDLING)
100 #ifdef SIGSEGV
101         CatchSignal(SIGSEGV, sig_fault);
102 #endif
103 #ifdef SIGBUS
104         CatchSignal(SIGBUS, sig_fault);
105 #endif
106 #ifdef SIGABRT
107         CatchSignal(SIGABRT, sig_fault);
108 #endif
109 #endif
110 }
111
112 _PUBLIC_ const char *panic_action = NULL;
113
114 /*
115    default smb_panic() implementation
116 */
117 static void smb_panic_default(const char *why)
118 {
119         int result;
120
121 #if defined(HAVE_PRCTL) && defined(PR_SET_PTRACER)
122         /*
123          * Make sure all children can attach a debugger.
124          */
125         prctl(PR_SET_PTRACER, getpid(), 0, 0, 0);
126 #endif
127
128         if (panic_action && *panic_action) {
129                 char pidstr[20];
130                 char cmdstring[200];
131                 strlcpy(cmdstring, panic_action, sizeof(cmdstring));
132                 snprintf(pidstr, sizeof(pidstr), "%d", (int) getpid());
133                 all_string_sub(cmdstring, "%d", pidstr, sizeof(cmdstring));
134                 DEBUG(0, ("smb_panic(): calling panic action [%s]\n", cmdstring));
135                 result = system(cmdstring);
136
137                 if (result == -1)
138                         DEBUG(0, ("smb_panic(): fork failed in panic action: %s\n",
139                                   strerror(errno)));
140                 else
141                         DEBUG(0, ("smb_panic(): action returned status %d\n",
142                                   WEXITSTATUS(result)));
143         }
144         DEBUG(0,("PANIC: %s\n", why));
145
146 #ifdef SIGABRT
147         CatchSignal(SIGABRT, SIG_DFL);
148 #endif
149         abort();
150 }
151
152
153 /**
154    Something really nasty happened - panic !
155 **/
156 _PUBLIC_ void smb_panic(const char *why)
157 {
158         if (fault_state.panic_handler) {
159                 fault_state.panic_handler(why);
160                 _exit(1);
161         }
162         smb_panic_default(why);
163 }