s3:quota: don't add the string '"' into the argument list
[kai/samba.git] / source3 / lib / dumpcore.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4
5    Copyright (C) Andrew Tridgell 1992-2011
6
7    based on old fault.c code, which had:
8
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
13
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.
18
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.
23
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/>.
26 */
27
28 #include "includes.h"
29 #include "system/filesys.h"
30
31 #ifdef HAVE_SYS_SYSCTL_H
32 #include <sys/sysctl.h>
33 #endif
34
35 #ifdef HAVE_SYS_PRCTL_H
36 #include <sys/prctl.h>
37 #endif
38
39 static char *corepath;
40
41 /**
42  * Build up the default corepath as "<logbase>/cores/<progname>"
43  */
44 static char *get_default_corepath(const char *logbase, const char *progname)
45 {
46         char *tmp_corepath;
47
48         /* Setup core dir in logbase. */
49         tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase);
50         if (!tmp_corepath)
51                 return NULL;
52
53         if ((mkdir(tmp_corepath, 0700) == -1) && errno != EEXIST)
54                 goto err_out;
55
56         if (chmod(tmp_corepath, 0700) == -1)
57                 goto err_out;
58
59         talloc_free(tmp_corepath);
60
61         /* Setup progname-specific core subdir */
62         tmp_corepath = talloc_asprintf(NULL, "%s/cores/%s", logbase, progname);
63         if (!tmp_corepath)
64                 return NULL;
65
66         if (mkdir(tmp_corepath, 0700) == -1 && errno != EEXIST)
67                 goto err_out;
68
69         if (chown(tmp_corepath, getuid(), getgid()) == -1)
70                 goto err_out;
71
72         if (chmod(tmp_corepath, 0700) == -1)
73                 goto err_out;
74
75         return tmp_corepath;
76
77  err_out:
78         talloc_free(tmp_corepath);
79         return NULL;
80 }
81
82
83 /**
84  * Get the FreeBSD corepath.
85  *
86  * On FreeBSD the current working directory is ignored when creating a core
87  * file.  Instead the core directory is controlled via sysctl.  This consults
88  * the value of "kern.corefile" so the correct corepath can be printed out
89  * before dump_core() calls abort.
90  */
91 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
92 static char *get_freebsd_corepath(void)
93 {
94         char *tmp_corepath = NULL;
95         char *end = NULL;
96         size_t len = 128;
97         int ret;
98
99         /* Loop with increasing sizes so we don't allocate too much. */
100         do {
101                 if (len > 1024)  {
102                         goto err_out;
103                 }
104
105                 tmp_corepath = (char *)talloc_realloc(NULL, tmp_corepath,
106                                                       char, len);
107                 if (!tmp_corepath) {
108                         return NULL;
109                 }
110
111                 ret = sysctlbyname("kern.corefile", tmp_corepath, &len, NULL,
112                                    0);
113                 if (ret == -1) {
114                         if (errno != ENOMEM) {
115                                 DEBUG(0, ("sysctlbyname failed getting "
116                                           "kern.corefile %s\n",
117                                           strerror(errno)));
118                                 goto err_out;
119                         }
120
121                         /* Not a large enough array, try a bigger one. */
122                         len = len << 1;
123                 }
124         } while (ret == -1);
125
126         /* Strip off the common filename expansion */
127         if ((end = strrchr_m(tmp_corepath, '/'))) {
128                 *end = '\0';
129         }
130
131         return tmp_corepath;
132
133  err_out:
134         if (tmp_corepath) {
135                 talloc_free(tmp_corepath);
136         }
137         return NULL;
138 }
139 #endif
140
141 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
142
143 /**
144  * Get the Linux corepath.
145  *
146  * On Linux the contents of /proc/sys/kernel/core_pattern indicates the
147  * location of the core path.
148  */
149 static char *get_linux_corepath(void)
150 {
151         char *end;
152         int fd;
153         char *result;
154
155         fd = open("/proc/sys/kernel/core_pattern", O_RDONLY, 0);
156         if (fd == -1) {
157                 return NULL;
158         }
159
160         result = afdgets(fd, NULL, 0);
161         close(fd);
162
163         if (result == NULL) {
164                 return NULL;
165         }
166
167         if (result[0] != '/') {
168                 /*
169                  * No absolute path, use the default (cwd)
170                  */
171                 TALLOC_FREE(result);
172                 return NULL;
173         }
174         /* Strip off the common filename expansion */
175
176         end = strrchr_m(result, '/');
177
178         if ((end != result) /* this would be the only / */
179             && (end != NULL)) {
180                 *end = '\0';
181         }
182         return result;
183 }
184 #endif
185
186
187 /**
188  * Try getting system-specific corepath if one exists.
189  *
190  * If the system doesn't define a corepath, then the default is used.
191  */
192 static char *get_corepath(const char *logbase, const char *progname)
193 {
194 #if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME))
195         char *tmp_corepath = NULL;
196         tmp_corepath = get_freebsd_corepath();
197
198         /* If this has been set correctly, we're done. */
199         if (tmp_corepath) {
200                 return tmp_corepath;
201         }
202 #endif
203
204 #if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN)
205         char *tmp_corepath = NULL;
206         tmp_corepath = get_linux_corepath();
207
208         /* If this has been set correctly, we're done. */
209         if (tmp_corepath) {
210                 return tmp_corepath;
211         }
212 #endif
213
214         /* Fall back to the default. */
215         return get_default_corepath(logbase, progname);
216 }
217
218 /*******************************************************************
219 make all the preparations to safely dump a core file
220 ********************************************************************/
221
222 void dump_core_setup(const char *progname, const char *log_file)
223 {
224         char *logbase = NULL;
225         char *end = NULL;
226
227         if (log_file && *log_file) {
228                 if (asprintf(&logbase, "%s", log_file) < 0) {
229                         return;
230                 }
231                 if ((end = strrchr_m(logbase, '/'))) {
232                         *end = '\0';
233                 }
234         } else {
235                 /* We will end up here if the log file is given on the command
236                  * line by the -l option but the "log file" option is not set
237                  * in smb.conf.
238                  */
239                 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
240                         return;
241                 }
242         }
243
244         SMB_ASSERT(progname != NULL);
245
246         corepath = get_corepath(logbase, progname);
247         if (!corepath) {
248                 DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname,
249                           strerror(errno)));
250                 goto out;
251         }
252
253
254 #ifdef HAVE_GETRLIMIT
255 #ifdef RLIMIT_CORE
256         {
257                 struct rlimit rlp;
258                 getrlimit(RLIMIT_CORE, &rlp);
259                 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
260                 setrlimit(RLIMIT_CORE, &rlp);
261                 getrlimit(RLIMIT_CORE, &rlp);
262                 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
263                          (int)rlp.rlim_cur,(int)rlp.rlim_max));
264         }
265 #endif
266 #endif
267
268         /* FIXME: if we have a core-plus-pid facility, configurably set
269          * this up here.
270          */
271  out:
272         SAFE_FREE(logbase);
273 }
274
275  void dump_core(void)
276 {
277         static bool called;
278
279         if (called) {
280                 DEBUG(0, ("dump_core() called recursive\n"));
281                 exit(1);
282         }
283         called = true;
284
285         /* Note that even if core dumping has been disabled, we still set up
286          * the core path. This is to handle the case where core dumping is
287          * turned on in smb.conf and the relevant daemon is not restarted.
288          */
289         if (!lp_enable_core_files()) {
290                 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
291                 exit(1);
292         }
293
294 #if DUMP_CORE
295         /* If we're running as non root we might not be able to dump the core
296          * file to the corepath.  There must not be an unbecome_root() before
297          * we call abort(). */
298         if (geteuid() != sec_initial_uid()) {
299                 become_root();
300         }
301
302         if (corepath == NULL) {
303                 DEBUG(0, ("Can not dump core: corepath not set up\n"));
304                 exit(1);
305         }
306
307         if (*corepath != '\0') {
308                 /* The chdir might fail if we dump core before we finish
309                  * processing the config file.
310                  */
311                 if (chdir(corepath) != 0) {
312                         DEBUG(0, ("unable to change to %s\n", corepath));
313                         DEBUGADD(0, ("refusing to dump core\n"));
314                         exit(1);
315                 }
316
317                 DEBUG(0,("dumping core in %s\n", corepath));
318         }
319
320         umask(~(0700));
321         dbgflush();
322
323 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
324         /* On Linux we lose the ability to dump core when we change our user
325          * ID. We know how to dump core safely, so let's make sure we have our
326          * dumpable flag set.
327          */
328         prctl(PR_SET_DUMPABLE, 1);
329 #endif
330
331         /* Ensure we don't have a signal handler for abort. */
332 #ifdef SIGABRT
333         CatchSignal(SIGABRT, SIG_DFL);
334 #endif
335
336         abort();
337
338 #else /* DUMP_CORE */
339         exit(1);
340 #endif /* DUMP_CORE */
341 }