s3: Attempt to fix the build on FreeBSD
[Samba.git] / source3 / lib / dumpcore.c
blob4d093a2b163d3803fe70af6d7940591bc9e13351
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
5 Copyright (C) Andrew Tridgell 1992-2011
7 based on old fault.c code, which had:
9 Copyright (C) Jeremy Allison 2001-2007
10 Copyright (C) Simo Sorce 2001
11 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
12 Copyright (C) James Peach 2006
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "includes.h"
30 #ifdef HAVE_SYS_SYSCTL_H
31 #include <sys/sysctl.h>
32 #endif
34 static char *corepath;
36 /**
37 * Build up the default corepath as "<logbase>/cores/<progname>"
39 static char *get_default_corepath(const char *logbase, const char *progname)
41 char *tmp_corepath;
43 /* Setup core dir in logbase. */
44 tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase);
45 if (!tmp_corepath)
46 return NULL;
48 if ((mkdir(tmp_corepath, 0700) == -1) && errno != EEXIST)
49 goto err_out;
51 if (chmod(tmp_corepath, 0700) == -1)
52 goto err_out;
54 talloc_free(tmp_corepath);
56 /* Setup progname-specific core subdir */
57 tmp_corepath = talloc_asprintf(NULL, "%s/cores/%s", logbase, progname);
58 if (!tmp_corepath)
59 return NULL;
61 if (mkdir(tmp_corepath, 0700) == -1 && errno != EEXIST)
62 goto err_out;
64 if (chown(tmp_corepath, getuid(), getgid()) == -1)
65 goto err_out;
67 if (chmod(tmp_corepath, 0700) == -1)
68 goto err_out;
70 return tmp_corepath;
72 err_out:
73 talloc_free(tmp_corepath);
74 return NULL;
78 /**
79 * Get the FreeBSD corepath.
81 * On FreeBSD the current working directory is ignored when creating a core
82 * file. Instead the core directory is controlled via sysctl. This consults
83 * the value of "kern.corefile" so the correct corepath can be printed out
84 * before dump_core() calls abort.
86 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
87 static char *get_freebsd_corepath(void)
89 char *tmp_corepath = NULL;
90 char *end = NULL;
91 size_t len = 128;
92 int ret;
94 /* Loop with increasing sizes so we don't allocate too much. */
95 do {
96 if (len > 1024) {
97 goto err_out;
100 tmp_corepath = (char *)talloc_realloc(NULL, tmp_corepath,
101 char, len);
102 if (!tmp_corepath) {
103 return NULL;
106 ret = sysctlbyname("kern.corefile", tmp_corepath, &len, NULL,
108 if (ret == -1) {
109 if (errno != ENOMEM) {
110 DEBUG(0, ("sysctlbyname failed getting "
111 "kern.corefile %s\n",
112 strerror(errno)));
113 goto err_out;
116 /* Not a large enough array, try a bigger one. */
117 len = len << 1;
119 } while (ret == -1);
121 /* Strip off the common filename expansion */
122 if ((end = strrchr_m(tmp_corepath, '/'))) {
123 *end = '\0';
126 return tmp_corepath;
128 err_out:
129 if (tmp_corepath) {
130 talloc_free(tmp_corepath);
132 return NULL;
134 #endif
136 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
139 * Get the Linux corepath.
141 * On Linux the contents of /proc/sys/kernel/core_pattern indicates the
142 * location of the core path.
144 static char *get_linux_corepath(void)
146 char *end;
147 int fd;
148 char *result;
150 fd = open("/proc/sys/kernel/core_pattern", O_RDONLY, 0);
151 if (fd == -1) {
152 return NULL;
155 result = afdgets(fd, NULL, 0);
156 close(fd);
158 if (result == NULL) {
159 return NULL;
162 if (result[0] != '/') {
164 * No absolute path, use the default (cwd)
166 TALLOC_FREE(result);
167 return NULL;
169 /* Strip off the common filename expansion */
171 end = strrchr_m(result, '/');
173 if ((end != result) /* this would be the only / */
174 && (end != NULL)) {
175 *end = '\0';
177 return result;
179 #endif
183 * Try getting system-specific corepath if one exists.
185 * If the system doesn't define a corepath, then the default is used.
187 static char *get_corepath(const char *logbase, const char *progname)
189 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
190 char *tmp_corepath = NULL;
191 tmp_corepath = get_freebsd_corepath();
193 /* If this has been set correctly, we're done. */
194 if (tmp_corepath) {
195 return tmp_corepath;
197 #endif
199 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
200 char *tmp_corepath = NULL;
201 tmp_corepath = get_linux_corepath();
203 /* If this has been set correctly, we're done. */
204 if (tmp_corepath) {
205 return tmp_corepath;
207 #endif
209 /* Fall back to the default. */
210 return get_default_corepath(logbase, progname);
213 /*******************************************************************
214 make all the preparations to safely dump a core file
215 ********************************************************************/
217 void dump_core_setup(const char *progname, const char *logfile)
219 char *logbase = NULL;
220 char *end = NULL;
222 if (logfile && *logfile) {
223 if (asprintf(&logbase, "%s", logfile) < 0) {
224 return;
226 if ((end = strrchr_m(logbase, '/'))) {
227 *end = '\0';
229 } else {
230 /* We will end up here if the log file is given on the command
231 * line by the -l option but the "log file" option is not set
232 * in smb.conf.
234 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
235 return;
239 SMB_ASSERT(progname != NULL);
241 corepath = get_corepath(logbase, progname);
242 if (!corepath) {
243 DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname,
244 strerror(errno)));
245 goto out;
249 #ifdef HAVE_GETRLIMIT
250 #ifdef RLIMIT_CORE
252 struct rlimit rlp;
253 getrlimit(RLIMIT_CORE, &rlp);
254 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
255 setrlimit(RLIMIT_CORE, &rlp);
256 getrlimit(RLIMIT_CORE, &rlp);
257 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
258 (int)rlp.rlim_cur,(int)rlp.rlim_max));
260 #endif
261 #endif
263 /* FIXME: if we have a core-plus-pid facility, configurably set
264 * this up here.
266 out:
267 SAFE_FREE(logbase);
270 void dump_core(void)
272 static bool called;
274 if (called) {
275 DEBUG(0, ("dump_core() called recursive\n"));
276 exit(1);
278 called = true;
280 /* Note that even if core dumping has been disabled, we still set up
281 * the core path. This is to handle the case where core dumping is
282 * turned on in smb.conf and the relevant daemon is not restarted.
284 if (!lp_enable_core_files()) {
285 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
286 exit(1);
289 #if DUMP_CORE
290 /* If we're running as non root we might not be able to dump the core
291 * file to the corepath. There must not be an unbecome_root() before
292 * we call abort(). */
293 if (geteuid() != sec_initial_uid()) {
294 become_root();
297 if (corepath == NULL) {
298 DEBUG(0, ("Can not dump core: corepath not set up\n"));
299 exit(1);
302 if (*corepath != '\0') {
303 /* The chdir might fail if we dump core before we finish
304 * processing the config file.
306 if (chdir(corepath) != 0) {
307 DEBUG(0, ("unable to change to %s\n", corepath));
308 DEBUGADD(0, ("refusing to dump core\n"));
309 exit(1);
312 DEBUG(0,("dumping core in %s\n", corepath));
315 umask(~(0700));
316 dbgflush();
318 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
319 /* On Linux we lose the ability to dump core when we change our user
320 * ID. We know how to dump core safely, so let's make sure we have our
321 * dumpable flag set.
323 prctl(PR_SET_DUMPABLE, 1);
324 #endif
326 /* Ensure we don't have a signal handler for abort. */
327 #ifdef SIGABRT
328 CatchSignal(SIGABRT, SIG_DFL);
329 #endif
331 abort();
333 #else /* DUMP_CORE */
334 exit(1);
335 #endif /* DUMP_CORE */