WHATSNEW: Add release notes for Samba 4.5.4.
[Samba.git] / source3 / lib / dumpcore.c
blobc72aa882bd884213b6eb597f31054f02b59ac740
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"
29 #include "system/filesys.h"
31 #ifdef HAVE_SYS_SYSCTL_H
32 #include <sys/sysctl.h>
33 #endif
35 #ifdef HAVE_SYS_PRCTL_H
36 #include <sys/prctl.h>
37 #endif
39 static char *corepath;
41 /**
42 * Build up the default corepath as "<logbase>/cores/<progname>"
44 static char *get_default_corepath(const char *logbase, const char *progname)
46 const mode_t mode = 0700;
47 const uid_t uid = getuid();
48 char *tmp_corepath;
50 /* Setup core dir in logbase. */
51 tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase);
52 if (!tmp_corepath) {
53 DEBUG(0, ("Out of memory\n"));
54 return NULL;
57 if (!directory_create_or_exist_strict(tmp_corepath, uid, mode)) {
58 DEBUG(0, ("Failed to create %s for user %d with mode 0%o\n",
59 tmp_corepath, (int)uid, (int)mode));
60 goto err_out;
63 /* Setup progname-specific core subdir */
64 tmp_corepath = talloc_asprintf_append(tmp_corepath, "/%s", progname);
65 if (!tmp_corepath) {
66 DEBUG(0, ("Out of memory\n"));
67 goto err_out;
70 if (!directory_create_or_exist(tmp_corepath, mode)) {
71 DEBUG(0, ("Failed to create %s for user %d with mode 0%o\n",
72 tmp_corepath, (int)uid, (int)mode));
73 goto err_out;
76 return tmp_corepath;
78 err_out:
79 talloc_free(tmp_corepath);
80 return NULL;
84 /**
85 * Get the FreeBSD corepath.
87 * On FreeBSD the current working directory is ignored when creating a core
88 * file. Instead the core directory is controlled via sysctl. This consults
89 * the value of "kern.corefile" so the correct corepath can be printed out
90 * before dump_core() calls abort.
92 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
93 static char *get_freebsd_corepath(void)
95 char *tmp_corepath = NULL;
96 char *end = NULL;
97 size_t len = 128;
98 int ret;
100 /* Loop with increasing sizes so we don't allocate too much. */
101 do {
102 if (len > 1024) {
103 goto err_out;
106 tmp_corepath = (char *)talloc_realloc(NULL, tmp_corepath,
107 char, len);
108 if (!tmp_corepath) {
109 return NULL;
112 ret = sysctlbyname("kern.corefile", tmp_corepath, &len, NULL,
114 if (ret == -1) {
115 if (errno != ENOMEM) {
116 DEBUG(0, ("sysctlbyname failed getting "
117 "kern.corefile %s\n",
118 strerror(errno)));
119 goto err_out;
122 /* Not a large enough array, try a bigger one. */
123 len = len << 1;
125 } while (ret == -1);
127 /* Strip off the common filename expansion */
128 if ((end = strrchr_m(tmp_corepath, '/'))) {
129 *end = '\0';
132 return tmp_corepath;
134 err_out:
135 if (tmp_corepath) {
136 talloc_free(tmp_corepath);
138 return NULL;
140 #endif
142 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
145 * Get the Linux corepath.
147 * On Linux the contents of /proc/sys/kernel/core_pattern indicates the
148 * location of the core path.
150 static char *get_linux_corepath(void)
152 char *end;
153 int fd;
154 char *result;
156 fd = open("/proc/sys/kernel/core_pattern", O_RDONLY, 0);
157 if (fd == -1) {
158 return NULL;
161 result = afdgets(fd, NULL, 0);
162 close(fd);
164 if (result == NULL) {
165 return NULL;
168 if (result[0] != '/') {
170 * No absolute path, use the default (cwd)
172 TALLOC_FREE(result);
173 return NULL;
175 /* Strip off the common filename expansion */
177 end = strrchr_m(result, '/');
179 if ((end != result) /* this would be the only / */
180 && (end != NULL)) {
181 *end = '\0';
183 return result;
185 #endif
189 * Try getting system-specific corepath if one exists.
191 * If the system doesn't define a corepath, then the default is used.
193 static char *get_corepath(const char *logbase, const char *progname)
195 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
196 char *tmp_corepath = NULL;
197 tmp_corepath = get_freebsd_corepath();
199 /* If this has been set correctly, we're done. */
200 if (tmp_corepath) {
201 return tmp_corepath;
203 #endif
205 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
206 char *tmp_corepath = NULL;
207 tmp_corepath = get_linux_corepath();
209 /* If this has been set correctly, we're done. */
210 if (tmp_corepath) {
211 return tmp_corepath;
213 #endif
215 /* Fall back to the default. */
216 return get_default_corepath(logbase, progname);
219 /*******************************************************************
220 make all the preparations to safely dump a core file
221 ********************************************************************/
223 void dump_core_setup(const char *progname, const char *log_file)
225 char *logbase = NULL;
226 char *end = NULL;
228 if (log_file && *log_file) {
229 if (asprintf(&logbase, "%s", log_file) < 0) {
230 return;
232 if ((end = strrchr_m(logbase, '/'))) {
233 *end = '\0';
235 } else {
236 /* We will end up here if the log file is given on the command
237 * line by the -l option but the "log file" option is not set
238 * in smb.conf.
240 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
241 return;
245 SMB_ASSERT(progname != NULL);
247 corepath = get_corepath(logbase, progname);
248 if (!corepath) {
249 DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname,
250 strerror(errno)));
251 goto out;
254 /* FIXME: if we have a core-plus-pid facility, configurably set
255 * this up here.
257 out:
258 SAFE_FREE(logbase);
261 void dump_core(void)
263 static bool called;
265 if (called) {
266 DEBUG(0, ("dump_core() called recursive\n"));
267 exit(1);
269 called = true;
271 /* Note that even if core dumping has been disabled, we still set up
272 * the core path. This is to handle the case where core dumping is
273 * turned on in smb.conf and the relevant daemon is not restarted.
275 if (!lp_enable_core_files()) {
276 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
277 exit(1);
280 #if DUMP_CORE
281 /* If we're running as non root we might not be able to dump the core
282 * file to the corepath. There must not be an unbecome_root() before
283 * we call abort(). */
284 if (geteuid() != sec_initial_uid()) {
285 become_root();
288 if (corepath == NULL) {
289 DEBUG(0, ("Can not dump core: corepath not set up\n"));
290 exit(1);
293 if (*corepath != '\0') {
294 /* The chdir might fail if we dump core before we finish
295 * processing the config file.
297 if (chdir(corepath) != 0) {
298 DEBUG(0, ("unable to change to %s\n", corepath));
299 DEBUGADD(0, ("refusing to dump core\n"));
300 exit(1);
303 DEBUG(0,("dumping core in %s\n", corepath));
306 umask(~(0700));
307 dbgflush();
309 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
310 /* On Linux we lose the ability to dump core when we change our user
311 * ID. We know how to dump core safely, so let's make sure we have our
312 * dumpable flag set.
314 prctl(PR_SET_DUMPABLE, 1);
315 #endif
317 /* Ensure we don't have a signal handler for abort. */
318 #ifdef SIGABRT
319 CatchSignal(SIGABRT, SIG_DFL);
320 #endif
322 abort();
324 #else /* DUMP_CORE */
325 exit(1);
326 #endif /* DUMP_CORE */