source: branches/samba-3.2.x/source/lib/fault.c@ 791

Last change on this file since 791 was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 5.5 KB
Line 
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
26static void (*cont_fn)(void *);
27static char *corepath;
28
29/*******************************************************************
30report a fault
31********************************************************************/
32static 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#ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */
51#ifdef SIGSEGV
52 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
53#endif
54#ifdef SIGBUS
55 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
56#endif
57#ifdef SIGABRT
58 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
59#endif
60#endif /* __OS2__ */
61 return; /* this should cause a core dump */
62 }
63 exit(1);
64}
65
66/****************************************************************************
67catch serious errors
68****************************************************************************/
69static void sig_fault(int sig)
70{
71 fault_report(sig);
72}
73
74/*******************************************************************
75setup our fault handlers
76********************************************************************/
77void fault_setup(void (*fn)(void *))
78{
79 cont_fn = fn;
80
81#ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */
82#ifdef SIGSEGV
83 CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
84#endif
85#ifdef SIGBUS
86 CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
87#endif
88#ifdef SIGABRT
89 CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
90#endif
91#endif /* __OS2__ */
92}
93
94/*******************************************************************
95make all the preparations to safely dump a core file
96********************************************************************/
97
98void dump_core_setup(const char *progname)
99{
100 char *logbase = NULL;
101 char *end = NULL;
102
103 if (lp_logfile() && *lp_logfile()) {
104 if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
105 return;
106 }
107 if ((end = strrchr_m(logbase, '/'))) {
108 *end = '\0';
109 }
110 } else {
111 /* We will end up here is the log file is given on the command
112 * line by the -l option but the "log file" option is not set
113 * in smb.conf.
114 */
115 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
116 return;
117 }
118 }
119
120 SMB_ASSERT(progname != NULL);
121
122 if (asprintf(&corepath, "%s/cores", logbase) < 0) {
123 SAFE_FREE(logbase);
124 return;
125 }
126 mkdir(corepath,0700);
127
128 SAFE_FREE(corepath);
129 if (asprintf(&corepath, "%s/cores/%s",
130 logbase, progname) < 0) {
131 SAFE_FREE(logbase);
132 return;
133 }
134 mkdir(corepath,0700);
135
136 sys_chown(corepath,getuid(),getgid());
137 chmod(corepath,0700);
138
139 SAFE_FREE(logbase);
140
141#ifdef HAVE_GETRLIMIT
142#ifdef RLIMIT_CORE
143 {
144 struct rlimit rlp;
145 getrlimit(RLIMIT_CORE, &rlp);
146 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
147 setrlimit(RLIMIT_CORE, &rlp);
148 getrlimit(RLIMIT_CORE, &rlp);
149 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
150 (int)rlp.rlim_cur,(int)rlp.rlim_max));
151 }
152#endif
153#endif
154
155#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
156 /* On Linux we lose the ability to dump core when we change our user
157 * ID. We know how to dump core safely, so let's make sure we have our
158 * dumpable flag set.
159 */
160 prctl(PR_SET_DUMPABLE, 1);
161#endif
162
163 /* FIXME: if we have a core-plus-pid facility, configurably set
164 * this up here.
165 */
166}
167
168 void dump_core(void)
169{
170 static bool called;
171
172 if (called) {
173 DEBUG(0, ("dump_core() called recursive\n"));
174 exit(1);
175 }
176 called = true;
177
178 /* Note that even if core dumping has been disabled, we still set up
179 * the core path. This is to handle the case where core dumping is
180 * turned on in smb.conf and the relevant daemon is not restarted.
181 */
182 if (!lp_enable_core_files()) {
183 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
184 exit(1);
185 }
186
187#if DUMP_CORE
188 /* If we're running as non root we might not be able to dump the core
189 * file to the corepath. There must not be an unbecome_root() before
190 * we call abort(). */
191 if (geteuid() != 0) {
192 become_root();
193 }
194
195 if (*corepath != '\0') {
196 /* The chdir might fail if we dump core before we finish
197 * processing the config file.
198 */
199 if (chdir(corepath) != 0) {
200 DEBUG(0, ("unable to change to %s\n", corepath));
201 DEBUGADD(0, ("refusing to dump core\n"));
202 exit(1);
203 }
204
205 DEBUG(0,("dumping core in %s\n", corepath));
206 }
207
208 umask(~(0700));
209 dbgflush();
210
211 /* Ensure we don't have a signal handler for abort. */
212#ifdef SIGABRT
213 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
214#endif
215
216 abort();
217
218#else /* DUMP_CORE */
219 exit(1);
220#endif /* DUMP_CORE */
221}
222
Note: See TracBrowser for help on using the repository browser.