2 Unix SMB/CIFS implementation.
3 Critical Fault handling
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Tim Prouty 2009
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.
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.
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/>.
23 #ifdef HAVE_SYS_PRCTL_H
24 #include <sys/prctl.h>
27 static void (*cont_fn
)(void *);
28 static char *corepath
;
30 /*******************************************************************
32 ********************************************************************/
33 static void fault_report(int sig
)
37 if (counter
) _exit(1);
42 DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig
,(int)sys_getpid(),samba_version_string()));
43 DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n"));
44 DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n"));
47 smb_panic("internal error");
52 CatchSignal(SIGSEGV
,SIGNAL_CAST SIG_DFL
);
55 CatchSignal(SIGBUS
,SIGNAL_CAST SIG_DFL
);
58 CatchSignal(SIGABRT
,SIGNAL_CAST SIG_DFL
);
60 return; /* this should cause a core dump */
65 /****************************************************************************
67 ****************************************************************************/
68 static void sig_fault(int sig
)
73 /*******************************************************************
74 setup our fault handlers
75 ********************************************************************/
76 void fault_setup(void (*fn
)(void *))
81 CatchSignal(SIGSEGV
,SIGNAL_CAST sig_fault
);
84 CatchSignal(SIGBUS
,SIGNAL_CAST sig_fault
);
87 CatchSignal(SIGABRT
,SIGNAL_CAST sig_fault
);
92 * Build up the default corepath as "<logbase>/cores/<progname>"
94 static char *get_default_corepath(const char *logbase
, const char *progname
)
98 /* Setup core dir in logbase. */
99 tmp_corepath
= talloc_asprintf(NULL
, "%s/cores", logbase
);
103 if ((mkdir(tmp_corepath
, 0700) == -1) && errno
!= EEXIST
)
106 if (chmod(tmp_corepath
, 0700) == -1)
109 talloc_free(tmp_corepath
);
111 /* Setup progname-specific core subdir */
112 tmp_corepath
= talloc_asprintf(NULL
, "%s/cores/%s", logbase
, progname
);
116 if (mkdir(tmp_corepath
, 0700) == -1 && errno
!= EEXIST
)
119 if (chown(tmp_corepath
, getuid(), getgid()) == -1)
122 if (chmod(tmp_corepath
, 0700) == -1)
128 talloc_free(tmp_corepath
);
133 * Get the FreeBSD corepath.
135 * On FreeBSD the current working directory is ignored when creating a core
136 * file. Instead the core directory is controlled via sysctl. This consults
137 * the value of "kern.corefile" so the correct corepath can be printed out
138 * before dump_core() calls abort.
140 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
141 static char *get_freebsd_corepath(void)
143 char *tmp_corepath
= NULL
;
148 /* Loop with increasing sizes so we don't allocate too much. */
154 tmp_corepath
= (char *)talloc_realloc(NULL
, tmp_corepath
,
160 ret
= sysctlbyname("kern.corefile", tmp_corepath
, &len
, NULL
,
163 if (errno
!= ENOMEM
) {
164 DEBUG(0, ("sysctlbyname failed getting "
165 "kern.corefile %s\n",
170 /* Not a large enough array, try a bigger one. */
175 /* Strip off the common filename expansion */
176 if ((end
= strrchr_m(tmp_corepath
, '/'))) {
184 talloc_free(tmp_corepath
);
191 * Try getting system-specific corepath if one exists.
193 * If the system doesn't define a corepath, then the default is used.
195 static char *get_corepath(const char *logbase
, const char *progname
)
197 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
199 /* @todo: Add support for the linux corepath. */
201 char *tmp_corepath
= NULL
;
202 tmp_corepath
= get_freebsd_corepath();
204 /* If this has been set correctly, we're done. */
210 /* Fall back to the default. */
211 return get_default_corepath(logbase
, progname
);
214 /*******************************************************************
215 make all the preparations to safely dump a core file
216 ********************************************************************/
218 void dump_core_setup(const char *progname
)
220 char *logbase
= NULL
;
223 if (lp_logfile() && *lp_logfile()) {
224 if (asprintf(&logbase
, "%s", lp_logfile()) < 0) {
227 if ((end
= strrchr_m(logbase
, '/'))) {
231 /* We will end up here if the log file is given on the command
232 * line by the -l option but the "log file" option is not set
235 if (asprintf(&logbase
, "%s", get_dyn_LOGFILEBASE()) < 0) {
240 SMB_ASSERT(progname
!= NULL
);
242 corepath
= get_corepath(logbase
, progname
);
244 DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname
,
250 #ifdef HAVE_GETRLIMIT
254 getrlimit(RLIMIT_CORE
, &rlp
);
255 rlp
.rlim_cur
= MAX(16*1024*1024,rlp
.rlim_cur
);
256 setrlimit(RLIMIT_CORE
, &rlp
);
257 getrlimit(RLIMIT_CORE
, &rlp
);
258 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
259 (int)rlp
.rlim_cur
,(int)rlp
.rlim_max
));
264 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
265 /* On Linux we lose the ability to dump core when we change our user
266 * ID. We know how to dump core safely, so let's make sure we have our
269 prctl(PR_SET_DUMPABLE
, 1);
272 /* FIXME: if we have a core-plus-pid facility, configurably set
284 DEBUG(0, ("dump_core() called recursive\n"));
289 /* Note that even if core dumping has been disabled, we still set up
290 * the core path. This is to handle the case where core dumping is
291 * turned on in smb.conf and the relevant daemon is not restarted.
293 if (!lp_enable_core_files()) {
294 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
299 /* If we're running as non root we might not be able to dump the core
300 * file to the corepath. There must not be an unbecome_root() before
301 * we call abort(). */
302 if (geteuid() != 0) {
306 if (corepath
== NULL
) {
307 DEBUG(0, ("Can not dump core: corepath not set up\n"));
311 if (*corepath
!= '\0') {
312 /* The chdir might fail if we dump core before we finish
313 * processing the config file.
315 if (chdir(corepath
) != 0) {
316 DEBUG(0, ("unable to change to %s\n", corepath
));
317 DEBUGADD(0, ("refusing to dump core\n"));
321 DEBUG(0,("dumping core in %s\n", corepath
));
327 /* Ensure we don't have a signal handler for abort. */
329 CatchSignal(SIGABRT
,SIGNAL_CAST SIG_DFL
);
334 #else /* DUMP_CORE */
336 #endif /* DUMP_CORE */