diff options
author | Andrew Tridgell <tridge@samba.org> | 2011-03-22 16:17:39 +1100 |
---|---|---|
committer | Andrew Tridgell <tridge@samba.org> | 2011-03-23 11:03:57 +1100 |
commit | 580997ede06d587ecf00c6a3faff237806904cd3 (patch) | |
tree | 73e5ce8019ded546a2630703aa91bd0f41b567c5 /source3 | |
parent | 058c3bb923c51ef3f1b4b6d698bea2b1220bdd10 (diff) | |
download | samba-580997ede06d587ecf00c6a3faff237806904cd3.tar.gz samba-580997ede06d587ecf00c6a3faff237806904cd3.tar.bz2 samba-580997ede06d587ecf00c6a3faff237806904cd3.zip |
fault: get fault.c ready for use by s4
this moves the s3 specific dumpcore code into source3/lib/dumpcore.c,
and uses a function pointer to setup which smb_panic call to use
Diffstat (limited to 'source3')
-rw-r--r-- | source3/Makefile.in | 2 | ||||
-rw-r--r-- | source3/include/proto.h | 6 | ||||
-rw-r--r-- | source3/lib/dumpcore.c | 332 | ||||
-rw-r--r-- | source3/lib/util.c | 2 | ||||
-rw-r--r-- | source3/nmbd/nmbd.c | 2 | ||||
-rw-r--r-- | source3/param/loadparm.c | 2 | ||||
-rw-r--r-- | source3/smbd/server.c | 2 | ||||
-rw-r--r-- | source3/winbindd/winbindd.c | 2 | ||||
-rwxr-xr-x | source3/wscript_build | 3 |
9 files changed, 342 insertions, 11 deletions
diff --git a/source3/Makefile.in b/source3/Makefile.in index 7ba8332eb0..065f0cbad7 100644 --- a/source3/Makefile.in +++ b/source3/Makefile.in @@ -443,7 +443,7 @@ LIB_OBJ = $(LIBSAMBAUTIL_OBJ) $(UTIL_OBJ) $(CRYPTO_OBJ) \ lib/addrchange.o \ $(TDB_LIB_OBJ) \ $(VERSION_OBJ) lib/charcnv.o ../lib/util/debug.o ../lib/util/debug_s3.o ../lib/util/fault.o \ - lib/interface.o lib/pidfile.o \ + lib/interface.o lib/pidfile.o lib/dumpcore.o \ lib/system.o lib/sendfile.o lib/recvfile.o lib/time.o \ lib/username.o \ ../libds/common/flag_mapping.o \ diff --git a/source3/include/proto.h b/source3/include/proto.h index b5b35a4623..bb9241c893 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -515,10 +515,6 @@ void display_set_stderr(void); NTSTATUS map_nt_error_from_unix(int unix_error); int map_errno_from_nt_status(NTSTATUS status); -/* The following definitions come from lib/fault.c */ -void fault_setup(void); -void dump_core_setup(const char *progname); - /* The following definitions come from lib/file_id.c */ struct file_id vfs_file_id_from_sbuf(connection_struct *conn, const SMB_STRUCT_STAT *sbuf); @@ -1095,7 +1091,7 @@ const char *uidtoname(uid_t uid); char *gidtoname(gid_t gid); uid_t nametouid(const char *name); gid_t nametogid(const char *name); -void smb_panic(const char *const why); +void smb_panic_s3(const char *why); void log_stack_trace(void); const char *readdirname(SMB_STRUCT_DIR *p); bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensitive); diff --git a/source3/lib/dumpcore.c b/source3/lib/dumpcore.c new file mode 100644 index 0000000000..8a1c43a72f --- /dev/null +++ b/source3/lib/dumpcore.c @@ -0,0 +1,332 @@ +/* + Unix SMB/CIFS implementation. + Samba utility functions + + Copyright (C) Andrew Tridgell 1992-2011 + + based on old fault.c code, which had: + + Copyright (C) Jeremy Allison 2001-2007 + Copyright (C) Simo Sorce 2001 + Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003 + Copyright (C) James Peach 2006 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" + +static char *corepath; + +/** + * Build up the default corepath as "<logbase>/cores/<progname>" + */ +static char *get_default_corepath(const char *logbase, const char *progname) +{ + char *tmp_corepath; + + /* Setup core dir in logbase. */ + tmp_corepath = talloc_asprintf(NULL, "%s/cores", logbase); + if (!tmp_corepath) + return NULL; + + if ((mkdir(tmp_corepath, 0700) == -1) && errno != EEXIST) + goto err_out; + + if (chmod(tmp_corepath, 0700) == -1) + goto err_out; + + talloc_free(tmp_corepath); + + /* Setup progname-specific core subdir */ + tmp_corepath = talloc_asprintf(NULL, "%s/cores/%s", logbase, progname); + if (!tmp_corepath) + return NULL; + + if (mkdir(tmp_corepath, 0700) == -1 && errno != EEXIST) + goto err_out; + + if (chown(tmp_corepath, getuid(), getgid()) == -1) + goto err_out; + + if (chmod(tmp_corepath, 0700) == -1) + goto err_out; + + return tmp_corepath; + + err_out: + talloc_free(tmp_corepath); + return NULL; +} + + +/** + * Get the FreeBSD corepath. + * + * On FreeBSD the current working directory is ignored when creating a core + * file. Instead the core directory is controlled via sysctl. This consults + * the value of "kern.corefile" so the correct corepath can be printed out + * before dump_core() calls abort. + */ +#if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME)) +static char *get_freebsd_corepath(void) +{ + char *tmp_corepath = NULL; + char *end = NULL; + size_t len = 128; + int ret; + + /* Loop with increasing sizes so we don't allocate too much. */ + do { + if (len > 1024) { + goto err_out; + } + + tmp_corepath = (char *)talloc_realloc(NULL, tmp_corepath, + char, len); + if (!tmp_corepath) { + return NULL; + } + + ret = sysctlbyname("kern.corefile", tmp_corepath, &len, NULL, + 0); + if (ret == -1) { + if (errno != ENOMEM) { + DEBUG(0, ("sysctlbyname failed getting " + "kern.corefile %s\n", + strerror(errno))); + goto err_out; + } + + /* Not a large enough array, try a bigger one. */ + len = len << 1; + } + } while (ret == -1); + + /* Strip off the common filename expansion */ + if ((end = strrchr_m(tmp_corepath, '/'))) { + *end = '\0'; + } + + return tmp_corepath; + + err_out: + if (tmp_corepath) { + talloc_free(tmp_corepath); + } + return NULL; +} +#endif + +#if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN) + +/** + * Get the Linux corepath. + * + * On Linux the contents of /proc/sys/kernel/core_pattern indicates the + * location of the core path. + */ +static char *get_linux_corepath(void) +{ + char *end; + int fd; + char *result; + + fd = open("/proc/sys/kernel/core_pattern", O_RDONLY, 0); + if (fd == -1) { + return NULL; + } + + result = afdgets(fd, NULL, 0); + close(fd); + + if (result == NULL) { + return NULL; + } + + if (result[0] != '/') { + /* + * No absolute path, use the default (cwd) + */ + TALLOC_FREE(result); + return NULL; + } + /* Strip off the common filename expansion */ + + end = strrchr_m(result, '/'); + + if ((end != result) /* this would be the only / */ + && (end != NULL)) { + *end = '\0'; + } + return result; +} +#endif + + +/** + * Try getting system-specific corepath if one exists. + * + * If the system doesn't define a corepath, then the default is used. + */ +static char *get_corepath(const char *logbase, const char *progname) +{ +#if (defined(FREEBSD) && defined(HAVE_SYSCTLBYNAME)) + char *tmp_corepath = NULL; + tmp_corepath = get_freebsd_corepath(); + + /* If this has been set correctly, we're done. */ + if (tmp_corepath) { + return tmp_corepath; + } +#endif + +#if defined(HAVE_SYS_KERNEL_PROC_CORE_PATTERN) + char *tmp_corepath = NULL; + tmp_corepath = get_linux_corepath(); + + /* If this has been set correctly, we're done. */ + if (tmp_corepath) { + return tmp_corepath; + } +#endif + + /* Fall back to the default. */ + return get_default_corepath(logbase, progname); +} + +/******************************************************************* +make all the preparations to safely dump a core file +********************************************************************/ + +void dump_core_setup(const char *progname, const char *logfile) +{ + char *logbase = NULL; + char *end = NULL; + + if (logfile && *logfile) { + if (asprintf(&logbase, "%s", logfile) < 0) { + return; + } + if ((end = strrchr_m(logbase, '/'))) { + *end = '\0'; + } + } else { + /* We will end up here if the log file is given on the command + * line by the -l option but the "log file" option is not set + * in smb.conf. + */ + if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) { + return; + } + } + + SMB_ASSERT(progname != NULL); + + corepath = get_corepath(logbase, progname); + if (!corepath) { + DEBUG(0, ("Unable to setup corepath for %s: %s\n", progname, + strerror(errno))); + goto out; + } + + +#ifdef HAVE_GETRLIMIT +#ifdef RLIMIT_CORE + { + struct rlimit rlp; + getrlimit(RLIMIT_CORE, &rlp); + rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur); + setrlimit(RLIMIT_CORE, &rlp); + getrlimit(RLIMIT_CORE, &rlp); + DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n", + (int)rlp.rlim_cur,(int)rlp.rlim_max)); + } +#endif +#endif + + /* FIXME: if we have a core-plus-pid facility, configurably set + * this up here. + */ + out: + SAFE_FREE(logbase); +} + + void dump_core(void) +{ + static bool called; + + if (called) { + DEBUG(0, ("dump_core() called recursive\n")); + exit(1); + } + called = true; + + /* Note that even if core dumping has been disabled, we still set up + * the core path. This is to handle the case where core dumping is + * turned on in smb.conf and the relevant daemon is not restarted. + */ + if (!lp_enable_core_files()) { + DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n")); + exit(1); + } + +#if DUMP_CORE + /* If we're running as non root we might not be able to dump the core + * file to the corepath. There must not be an unbecome_root() before + * we call abort(). */ + if (geteuid() != sec_initial_uid()) { + become_root(); + } + + if (corepath == NULL) { + DEBUG(0, ("Can not dump core: corepath not set up\n")); + exit(1); + } + + if (*corepath != '\0') { + /* The chdir might fail if we dump core before we finish + * processing the config file. + */ + if (chdir(corepath) != 0) { + DEBUG(0, ("unable to change to %s\n", corepath)); + DEBUGADD(0, ("refusing to dump core\n")); + exit(1); + } + + DEBUG(0,("dumping core in %s\n", corepath)); + } + + umask(~(0700)); + dbgflush(); + +#if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) + /* On Linux we lose the ability to dump core when we change our user + * ID. We know how to dump core safely, so let's make sure we have our + * dumpable flag set. + */ + prctl(PR_SET_DUMPABLE, 1); +#endif + + /* Ensure we don't have a signal handler for abort. */ +#ifdef SIGABRT + CatchSignal(SIGABRT, SIG_DFL); +#endif + + abort(); + +#else /* DUMP_CORE */ + exit(1); +#endif /* DUMP_CORE */ +} diff --git a/source3/lib/util.c b/source3/lib/util.c index b6128feaf6..79b10fda5e 100644 --- a/source3/lib/util.c +++ b/source3/lib/util.c @@ -1344,7 +1344,7 @@ gid_t nametogid(const char *name) Something really nasty happened - panic ! ********************************************************************/ -void smb_panic(const char *const why) +void smb_panic_s3(const char *why) { char *cmd; int result; diff --git a/source3/nmbd/nmbd.c b/source3/nmbd/nmbd.c index 1735c90576..2aa896476f 100644 --- a/source3/nmbd/nmbd.c +++ b/source3/nmbd/nmbd.c @@ -812,7 +812,7 @@ static bool open_sockets(bool isdaemon, int port) } fault_setup(); - dump_core_setup("nmbd"); + dump_core_setup("nmbd", lp_logfile()); /* POSIX demands that signals are inherited. If the invoking process has * these signals masked, we will have problems, as we won't receive them. */ diff --git a/source3/param/loadparm.c b/source3/param/loadparm.c index a82d21cdb1..ba3cd3c6c5 100644 --- a/source3/param/loadparm.c +++ b/source3/param/loadparm.c @@ -9541,6 +9541,8 @@ static bool lp_load_ex(const char *pszFname, init_iconv(); + fault_configure(smb_panic_s3); + bAllowIncludeRegistry = true; return (bRetval); diff --git a/source3/smbd/server.c b/source3/smbd/server.c index 2c09dd26c5..37c97049b8 100644 --- a/source3/smbd/server.c +++ b/source3/smbd/server.c @@ -987,7 +987,7 @@ extern void build_options(bool screen); gain_root_group_privilege(); fault_setup(); - dump_core_setup("smbd"); + dump_core_setup("smbd", lp_logfile()); /* we are never interested in SIGPIPE */ BlockSignals(True,SIGPIPE); diff --git a/source3/winbindd/winbindd.c b/source3/winbindd/winbindd.c index 99e98ac2b0..47d8be6178 100644 --- a/source3/winbindd/winbindd.c +++ b/source3/winbindd/winbindd.c @@ -1230,7 +1230,7 @@ int main(int argc, char **argv, char **envp) CatchSignal(SIGUSR2, SIG_IGN); fault_setup(); - dump_core_setup("winbindd"); + dump_core_setup("winbindd", lp_logfile()); load_case_tables(); diff --git a/source3/wscript_build b/source3/wscript_build index fd9e4f488b..cb3bbc29be 100755 --- a/source3/wscript_build +++ b/source3/wscript_build @@ -69,7 +69,8 @@ LIB_SRC = ''' lib/util_transfer_file.c lib/addrchange.c ${TDB_LIB_SRC} - ../lib/util/debug_s3.c ../lib/util/fault.c + ../lib/util/debug_s3.c + lib/dumpcore.c lib/interface.c lib/pidfile.c lib/system.c lib/sendfile.c lib/recvfile.c lib/time.c lib/username.c |