source: vendor/3.6.23/source3/lib/fault.c

Last change on this file was 740, checked in by Silvan Scherrer, 13 years ago

Samba Server: update vendor to 3.6.0

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