source: branches/samba-3.5.x/source3/lib/fault.c@ 736

Last change on this file since 736 was 454, checked in by Silvan Scherrer, 15 years ago

Samba Server 3.5: merged changes from 3.3

File size: 8.2 KB
Line 
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
23#ifdef HAVE_SYS_SYSCTL_H
24#include <sys/sysctl.h>
25#endif
26
27
28#ifdef HAVE_SYS_PRCTL_H
29#include <sys/prctl.h>
30#endif
31
32static void (*cont_fn)(void *);
33static char *corepath;
34
35/*******************************************************************
36report a fault
37********************************************************************/
38static void fault_report(int sig)
39{
40 static int counter;
41
42 if (counter) _exit(1);
43
44 counter++;
45
46 DEBUGSEP(0);
47 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),samba_version_string()));
48 DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n"));
49 DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n"));
50 DEBUGSEP(0);
51
52 smb_panic("internal error");
53
54 if (cont_fn) {
55 cont_fn(NULL);
56#ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */
57#ifdef SIGSEGV
58 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
59#endif
60#ifdef SIGBUS
61 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
62#endif
63#ifdef SIGABRT
64 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
65#endif
66#endif
67 return; /* this should cause a core dump */
68 }
69 exit(1);
70}
71
72/****************************************************************************
73catch serious errors
74****************************************************************************/
75static void sig_fault(int sig)
76{
77 fault_report(sig);
78}
79
80/*******************************************************************
81setup our fault handlers
82********************************************************************/
83void fault_setup(void (*fn)(void *))
84{
85 cont_fn = fn;
86
87#ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */
88#ifdef SIGSEGV
89 CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
90#endif
91#ifdef SIGBUS
92 CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
93#endif
94#ifdef SIGABRT
95 CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
96#endif
97#endif
98}
99
100/**
101 * Build up the default corepath as "<logbase>/cores/<progname>"
102 */
103static char *get_default_corepath(const char *logbase, const char *progname)
104{
105 char *tmp_corepath;
106
107 /* Setup core dir in logbase. */
108 tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase);
109 if (!tmp_corepath)
110 return NULL;
111
112 if ((mkdir(tmp_corepath, 0700) == -1) && errno != EEXIST)
113 goto err_out;
114
115 if (chmod(tmp_corepath, 0700) == -1)
116 goto err_out;
117
118 talloc_free(tmp_corepath);
119
120 /* Setup progname-specific core subdir */
121 tmp_corepath = talloc_asprintf(NULL, "%s/cores/%s", logbase, progname);
122 if (!tmp_corepath)
123 return NULL;
124
125 if (mkdir(tmp_corepath, 0700) == -1 && errno != EEXIST)
126 goto err_out;
127
128 if (chown(tmp_corepath, getuid(), getgid()) == -1)
129 goto err_out;
130
131 if (chmod(tmp_corepath, 0700) == -1)
132 goto err_out;
133
134 return tmp_corepath;
135
136 err_out:
137 talloc_free(tmp_corepath);
138 return NULL;
139}
140
141/**
142 * Get the FreeBSD corepath.
143 *
144 * On FreeBSD the current working directory is ignored when creating a core
145 * file. Instead the core directory is controlled via sysctl. This consults
146 * the value of "kern.corefile" so the correct corepath can be printed out
147 * before dump_core() calls abort.
148 */
149#if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
150static char *get_freebsd_corepath(void)
151{
152 char *tmp_corepath = NULL;
153 char *end = NULL;
154 size_t len = 128;
155 int ret;
156
157 /* Loop with increasing sizes so we don't allocate too much. */
158 do {
159 if (len > 1024) {
160 goto err_out;
161 }
162
163 tmp_corepath = (char *)talloc_realloc(NULL, tmp_corepath,
164 char, len);
165 if (!tmp_corepath) {
166 return NULL;
167 }
168
169 ret = sysctlbyname("kern.corefile", tmp_corepath, &len, NULL,
170 0);
171 if (ret == -1) {
172 if (errno != ENOMEM) {
173 DEBUG(0, ("sysctlbyname failed getting "
174 "kern.corefile %s\n",
175 strerror(errno)));
176 goto err_out;
177 }
178
179 /* Not a large enough array, try a bigger one. */
180 len = len << 1;
181 }
182 } while (ret == -1);
183
184 /* Strip off the common filename expansion */
185 if ((end = strrchr_m(tmp_corepath, '/'))) {
186 *end = '\0';
187 }
188
189 return tmp_corepath;
190
191 err_out:
192 if (tmp_corepath) {
193 talloc_free(tmp_corepath);
194 }
195 return NULL;
196}
197#endif
198
199/**
200 * Try getting system-specific corepath if one exists.
201 *
202 * If the system doesn't define a corepath, then the default is used.
203 */
204static char *get_corepath(const char *logbase, const char *progname)
205{
206#if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
207
208 /* @todo: Add support for the linux corepath. */
209
210 char *tmp_corepath = NULL;
211 tmp_corepath = get_freebsd_corepath();
212
213 /* If this has been set correctly, we're done. */
214 if (tmp_corepath) {
215 return tmp_corepath;
216 }
217#endif
218
219 /* Fall back to the default. */
220 return get_default_corepath(logbase, progname);
221}
222
223/*******************************************************************
224make all the preparations to safely dump a core file
225********************************************************************/
226
227void dump_core_setup(const char *progname)
228{
229 char *logbase = NULL;
230 char *end = NULL;
231
232 if (lp_logfile() && *lp_logfile()) {
233 if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
234 return;
235 }
236 if ((end = strrchr_m(logbase, '/'))) {
237 *end = '\0';
238 }
239 } else {
240 /* We will end up here if the log file is given on the command
241 * line by the -l option but the "log file" option is not set
242 * in smb.conf.
243 */
244 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
245 return;
246 }
247 }
248
249 SMB_ASSERT(progname != NULL);
250
251 corepath = get_corepath(logbase, progname);
252 if (!corepath) {
253 DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname,
254 strerror(errno)));
255 goto out;
256 }
257
258
259#ifdef HAVE_GETRLIMIT
260#ifdef RLIMIT_CORE
261 {
262 struct rlimit rlp;
263 getrlimit(RLIMIT_CORE, &rlp);
264 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
265 setrlimit(RLIMIT_CORE, &rlp);
266 getrlimit(RLIMIT_CORE, &rlp);
267 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
268 (int)rlp.rlim_cur,(int)rlp.rlim_max));
269 }
270#endif
271#endif
272
273#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
274 /* On Linux we lose the ability to dump core when we change our user
275 * ID. We know how to dump core safely, so let's make sure we have our
276 * dumpable flag set.
277 */
278 prctl(PR_SET_DUMPABLE, 1);
279#endif
280
281 /* FIXME: if we have a core-plus-pid facility, configurably set
282 * this up here.
283 */
284 out:
285 SAFE_FREE(logbase);
286}
287
288 void dump_core(void)
289{
290 static bool called;
291
292 if (called) {
293 DEBUG(0, ("dump_core() called recursive\n"));
294 exit(1);
295 }
296 called = true;
297
298 /* Note that even if core dumping has been disabled, we still set up
299 * the core path. This is to handle the case where core dumping is
300 * turned on in smb.conf and the relevant daemon is not restarted.
301 */
302 if (!lp_enable_core_files()) {
303 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
304 exit(1);
305 }
306
307#if DUMP_CORE
308 /* If we're running as non root we might not be able to dump the core
309 * file to the corepath. There must not be an unbecome_root() before
310 * we call abort(). */
311 if (geteuid() != 0) {
312 become_root();
313 }
314
315 if (corepath == NULL) {
316 DEBUG(0, ("Can not dump core: corepath not set up\n"));
317 exit(1);
318 }
319
320 if (*corepath != '\0') {
321 /* The chdir might fail if we dump core before we finish
322 * processing the config file.
323 */
324 if (chdir(corepath) != 0) {
325 DEBUG(0, ("unable to change to %s\n", corepath));
326 DEBUGADD(0, ("refusing to dump core\n"));
327 exit(1);
328 }
329
330 DEBUG(0,("dumping core in %s\n", corepath));
331 }
332
333 umask(~(0700));
334 dbgflush();
335
336 /* Ensure we don't have a signal handler for abort. */
337#ifdef SIGABRT
338 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
339#endif
340
341 abort();
342
343#else /* DUMP_CORE */
344 exit(1);
345#endif /* DUMP_CORE */
346}
347
Note: See TracBrowser for help on using the repository browser.