2 Unix SMB/CIFS implementation.
3 Critical Fault handling
4 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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/>.
22 #ifdef HAVE_SYS_PRCTL_H
23 #include <sys/prctl.h>
26 static void (*cont_fn)(void *);
27 static pstring corepath;
29 /*******************************************************************
31 ********************************************************************/
32 static void fault_report(int sig)
36 if (counter) _exit(1);
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"));
46 smb_panic("internal error");
51 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
54 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
57 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
59 return; /* this should cause a core dump */
64 /****************************************************************************
66 ****************************************************************************/
67 static void sig_fault(int sig)
72 /*******************************************************************
73 setup our fault handlers
74 ********************************************************************/
75 void fault_setup(void (*fn)(void *))
80 CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
83 CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
86 CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
90 /*******************************************************************
91 make all the preparations to safely dump a core file
92 ********************************************************************/
94 void dump_core_setup(const char *progname)
99 if (lp_logfile() && *lp_logfile()) {
100 snprintf(logbase, sizeof(logbase), "%s", lp_logfile());
101 if ((end = strrchr_m(logbase, '/'))) {
105 /* We will end up here is the log file is given on the command
106 * line by the -l option but the "log file" option is not set
109 snprintf(logbase, sizeof(logbase), "%s", dyn_LOGFILEBASE);
112 SMB_ASSERT(progname != NULL);
114 snprintf(corepath, sizeof(corepath), "%s/cores", logbase);
115 mkdir(corepath,0700);
117 snprintf(corepath, sizeof(corepath), "%s/cores/%s",
119 mkdir(corepath,0700);
121 sys_chown(corepath,getuid(),getgid());
122 chmod(corepath,0700);
124 #ifdef HAVE_GETRLIMIT
128 getrlimit(RLIMIT_CORE, &rlp);
129 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
130 setrlimit(RLIMIT_CORE, &rlp);
131 getrlimit(RLIMIT_CORE, &rlp);
132 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
133 (int)rlp.rlim_cur,(int)rlp.rlim_max));
138 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
139 /* On Linux we lose the ability to dump core when we change our user
140 * ID. We know how to dump core safely, so let's make sure we have our
143 prctl(PR_SET_DUMPABLE, 1);
146 /* FIXME: if we have a core-plus-pid facility, configurably set
153 /* Note that even if core dumping has been disabled, we still set up
154 * the core path. This is to handle the case where core dumping is
155 * turned on in smb.conf and the relevant daemon is not restarted.
157 if (!lp_enable_core_files()) {
158 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
163 /* If we're running as non root we might not be able to dump the core
164 * file to the corepath. There must not be an unbecome_root() before
165 * we call abort(). */
166 if (geteuid() != 0) {
170 if (*corepath != '\0') {
171 /* The chdir might fail if we dump core before we finish
172 * processing the config file.
174 if (chdir(corepath) != 0) {
175 DEBUG(0, ("unable to change to %s\n", corepath));
176 DEBUGADD(0, ("refusing to dump core\n"));
180 DEBUG(0,("dumping core in %s\n", corepath));
186 /* Ensure we don't have a signal handler for abort. */
188 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
193 #else /* DUMP_CORE */
195 #endif /* DUMP_CORE */