s3: make better use of ccache by not including version.h in every C-file.
[samba.git] / source3 / lib / fault.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Critical Fault handling
4    Copyright (C) Andrew Tridgell 1992-1998
5    
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.
10    
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.
15    
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/>.
18 */
19
20 #include "includes.h"
21
22 #ifdef HAVE_SYS_PRCTL_H
23 #include <sys/prctl.h>
24 #endif
25
26 static void (*cont_fn)(void *);
27 static char *corepath;
28
29 /*******************************************************************
30 report a fault
31 ********************************************************************/
32 static void fault_report(int sig)
33 {
34         static int counter;
35
36         if (counter) _exit(1);
37
38         counter++;
39
40         DEBUGSEP(0);
41         DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),samba_version_string()));
42         DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n"));
43         DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n"));
44         DEBUGSEP(0);
45   
46         smb_panic("internal error");
47
48         if (cont_fn) {
49                 cont_fn(NULL);
50 #ifdef SIGSEGV
51                 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
52 #endif
53 #ifdef SIGBUS
54                 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
55 #endif
56 #ifdef SIGABRT
57                 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
58 #endif
59                 return; /* this should cause a core dump */
60         }
61         exit(1);
62 }
63
64 /****************************************************************************
65 catch serious errors
66 ****************************************************************************/
67 static void sig_fault(int sig)
68 {
69         fault_report(sig);
70 }
71
72 /*******************************************************************
73 setup our fault handlers
74 ********************************************************************/
75 void fault_setup(void (*fn)(void *))
76 {
77         cont_fn = fn;
78
79 #ifdef SIGSEGV
80         CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
81 #endif
82 #ifdef SIGBUS
83         CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
84 #endif
85 #ifdef SIGABRT
86         CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
87 #endif
88 }
89
90 /*******************************************************************
91 make all the preparations to safely dump a core file
92 ********************************************************************/
93
94 void dump_core_setup(const char *progname)
95 {
96         char *logbase = NULL;
97         char *end = NULL;
98
99         if (lp_logfile() && *lp_logfile()) {
100                 if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
101                         return;
102                 }
103                 if ((end = strrchr_m(logbase, '/'))) {
104                         *end = '\0';
105                 }
106         } else {
107                 /* We will end up here is the log file is given on the command
108                  * line by the -l option but the "log file" option is not set
109                  * in smb.conf.
110                  */
111                 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
112                         return;
113                 }
114         }
115
116         SMB_ASSERT(progname != NULL);
117
118         if (asprintf(&corepath, "%s/cores", logbase) < 0) {
119                 SAFE_FREE(logbase);
120                 return;
121         }
122         if (mkdir(corepath,0700) == -1) {
123                 if (errno != EEXIST) {
124                         SAFE_FREE(corepath);
125                         SAFE_FREE(logbase);
126                         return;
127                 }
128         }
129         if (chmod(corepath,0700) == -1) {
130                 SAFE_FREE(corepath);
131                 SAFE_FREE(logbase);
132                 return;
133         }
134
135         SAFE_FREE(corepath);
136         if (asprintf(&corepath, "%s/cores/%s",
137                         logbase, progname) < 0) {
138                 SAFE_FREE(logbase);
139                 return;
140         }
141         if (mkdir(corepath,0700) == -1) {
142                 if (errno != EEXIST) {
143                         SAFE_FREE(corepath);
144                         SAFE_FREE(logbase);
145                         return;
146                 }
147         }
148
149         if (chown(corepath,getuid(),getgid()) == -1) {
150                 SAFE_FREE(corepath);
151                 SAFE_FREE(logbase);
152                 return;
153         }
154         if (chmod(corepath,0700) == -1) {
155                 SAFE_FREE(corepath);
156                 SAFE_FREE(logbase);
157                 return;
158         }
159
160         SAFE_FREE(corepath);
161         SAFE_FREE(logbase);
162
163 #ifdef HAVE_GETRLIMIT
164 #ifdef RLIMIT_CORE
165         {
166                 struct rlimit rlp;
167                 getrlimit(RLIMIT_CORE, &rlp);
168                 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
169                 setrlimit(RLIMIT_CORE, &rlp);
170                 getrlimit(RLIMIT_CORE, &rlp);
171                 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
172                          (int)rlp.rlim_cur,(int)rlp.rlim_max));
173         }
174 #endif
175 #endif
176
177 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
178         /* On Linux we lose the ability to dump core when we change our user
179          * ID. We know how to dump core safely, so let's make sure we have our
180          * dumpable flag set.
181          */
182         prctl(PR_SET_DUMPABLE, 1);
183 #endif
184
185         /* FIXME: if we have a core-plus-pid facility, configurably set
186          * this up here.
187          */
188 }
189
190  void dump_core(void)
191 {
192         static bool called;
193
194         if (called) {
195                 DEBUG(0, ("dump_core() called recursive\n"));
196                 exit(1);
197         }
198         called = true;
199
200         /* Note that even if core dumping has been disabled, we still set up
201          * the core path. This is to handle the case where core dumping is
202          * turned on in smb.conf and the relevant daemon is not restarted.
203          */
204         if (!lp_enable_core_files()) {
205                 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
206                 exit(1);
207         }
208
209 #if DUMP_CORE
210         /* If we're running as non root we might not be able to dump the core
211          * file to the corepath.  There must not be an unbecome_root() before
212          * we call abort(). */
213         if (geteuid() != 0) {
214                 become_root();
215         }
216
217         if (corepath == NULL) {
218                 DEBUG(0, ("Can not dump core: corepath not set up\n"));
219                 exit(1);
220         }
221
222         if (*corepath != '\0') {
223                 /* The chdir might fail if we dump core before we finish
224                  * processing the config file.
225                  */
226                 if (chdir(corepath) != 0) {
227                         DEBUG(0, ("unable to change to %s\n", corepath));
228                         DEBUGADD(0, ("refusing to dump core\n"));
229                         exit(1);
230                 }
231
232                 DEBUG(0,("dumping core in %s\n", corepath));
233         }
234
235         umask(~(0700));
236         dbgflush();
237
238         /* Ensure we don't have a signal handler for abort. */
239 #ifdef SIGABRT
240         CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
241 #endif
242
243         abort();
244
245 #else /* DUMP_CORE */
246         exit(1);
247 #endif /* DUMP_CORE */
248 }
249