| 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 2 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, write to the Free Software | 
|---|
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
|---|
| 19 | */ | 
|---|
| 20 |  | 
|---|
| 21 | #include "includes.h" | 
|---|
| 22 |  | 
|---|
| 23 | #ifdef HAVE_SYS_PRCTL_H | 
|---|
| 24 | #include <sys/prctl.h> | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | static void (*cont_fn)(void *); | 
|---|
| 28 | static pstring corepath; | 
|---|
| 29 |  | 
|---|
| 30 | /******************************************************************* | 
|---|
| 31 | report a fault | 
|---|
| 32 | ********************************************************************/ | 
|---|
| 33 | static void fault_report(int sig) | 
|---|
| 34 | { | 
|---|
| 35 | static int counter; | 
|---|
| 36 |  | 
|---|
| 37 | if (counter) _exit(1); | 
|---|
| 38 |  | 
|---|
| 39 | counter++; | 
|---|
| 40 |  | 
|---|
| 41 | DEBUGSEP(0); | 
|---|
| 42 | DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),SAMBA_VERSION_STRING)); | 
|---|
| 43 | DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n")); | 
|---|
| 44 | DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n")); | 
|---|
| 45 | DEBUGSEP(0); | 
|---|
| 46 |  | 
|---|
| 47 | smb_panic("internal error"); | 
|---|
| 48 |  | 
|---|
| 49 | if (cont_fn) { | 
|---|
| 50 | cont_fn(NULL); | 
|---|
| 51 | #ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */ | 
|---|
| 52 | #ifdef SIGSEGV | 
|---|
| 53 | CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL); | 
|---|
| 54 | #endif | 
|---|
| 55 | #ifdef SIGBUS | 
|---|
| 56 | CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL); | 
|---|
| 57 | #endif | 
|---|
| 58 | #ifdef SIGABRT | 
|---|
| 59 | CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL); | 
|---|
| 60 | #endif | 
|---|
| 61 | #endif /* __OS2__ */ | 
|---|
| 62 | return; /* this should cause a core dump */ | 
|---|
| 63 | } | 
|---|
| 64 | exit(1); | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | /**************************************************************************** | 
|---|
| 68 | catch serious errors | 
|---|
| 69 | ****************************************************************************/ | 
|---|
| 70 | static void sig_fault(int sig) | 
|---|
| 71 | { | 
|---|
| 72 | fault_report(sig); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | /******************************************************************* | 
|---|
| 76 | setup our fault handlers | 
|---|
| 77 | ********************************************************************/ | 
|---|
| 78 | void fault_setup(void (*fn)(void *)) | 
|---|
| 79 | { | 
|---|
| 80 | cont_fn = fn; | 
|---|
| 81 |  | 
|---|
| 82 | #ifndef __OS2__ /* don't use the built in signal capture stuff - prefer native handling of errors */ | 
|---|
| 83 | #ifdef SIGSEGV | 
|---|
| 84 | CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault); | 
|---|
| 85 | #endif | 
|---|
| 86 | #ifdef SIGBUS | 
|---|
| 87 | CatchSignal(SIGBUS,SIGNAL_CAST sig_fault); | 
|---|
| 88 | #endif | 
|---|
| 89 | #ifdef SIGABRT | 
|---|
| 90 | CatchSignal(SIGABRT,SIGNAL_CAST sig_fault); | 
|---|
| 91 | #endif | 
|---|
| 92 | #endif /* __OS2__ */ | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | /******************************************************************* | 
|---|
| 96 | make all the preparations to safely dump a core file | 
|---|
| 97 | ********************************************************************/ | 
|---|
| 98 |  | 
|---|
| 99 | void dump_core_setup(const char *progname) | 
|---|
| 100 | { | 
|---|
| 101 | pstring logbase; | 
|---|
| 102 | char * end; | 
|---|
| 103 |  | 
|---|
| 104 | if (lp_logfile() && *lp_logfile()) { | 
|---|
| 105 | snprintf(logbase, sizeof(logbase), "%s", lp_logfile()); | 
|---|
| 106 | if ((end = strrchr_m(logbase, '/'))) { | 
|---|
| 107 | *end = '\0'; | 
|---|
| 108 | } | 
|---|
| 109 | } else { | 
|---|
| 110 | /* We will end up here is the log file is given on the command | 
|---|
| 111 | * line by the -l option but the "log file" option is not set | 
|---|
| 112 | * in smb.conf. | 
|---|
| 113 | */ | 
|---|
| 114 | snprintf(logbase, sizeof(logbase), "%s", dyn_LOGFILEBASE); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | SMB_ASSERT(progname != NULL); | 
|---|
| 118 |  | 
|---|
| 119 | snprintf(corepath, sizeof(corepath), "%s/cores", logbase); | 
|---|
| 120 | mkdir(corepath,0700); | 
|---|
| 121 |  | 
|---|
| 122 | snprintf(corepath, sizeof(corepath), "%s/cores/%s", | 
|---|
| 123 | logbase, progname); | 
|---|
| 124 | mkdir(corepath,0700); | 
|---|
| 125 |  | 
|---|
| 126 | sys_chown(corepath,getuid(),getgid()); | 
|---|
| 127 | chmod(corepath,0700); | 
|---|
| 128 |  | 
|---|
| 129 | #ifdef HAVE_GETRLIMIT | 
|---|
| 130 | #ifdef RLIMIT_CORE | 
|---|
| 131 | { | 
|---|
| 132 | struct rlimit rlp; | 
|---|
| 133 | getrlimit(RLIMIT_CORE, &rlp); | 
|---|
| 134 | rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur); | 
|---|
| 135 | setrlimit(RLIMIT_CORE, &rlp); | 
|---|
| 136 | getrlimit(RLIMIT_CORE, &rlp); | 
|---|
| 137 | DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n", | 
|---|
| 138 | (int)rlp.rlim_cur,(int)rlp.rlim_max)); | 
|---|
| 139 | } | 
|---|
| 140 | #endif | 
|---|
| 141 | #endif | 
|---|
| 142 |  | 
|---|
| 143 | #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE) | 
|---|
| 144 | /* On Linux we lose the ability to dump core when we change our user | 
|---|
| 145 | * ID. We know how to dump core safely, so let's make sure we have our | 
|---|
| 146 | * dumpable flag set. | 
|---|
| 147 | */ | 
|---|
| 148 | prctl(PR_SET_DUMPABLE, 1); | 
|---|
| 149 | #endif | 
|---|
| 150 |  | 
|---|
| 151 | /* FIXME: if we have a core-plus-pid facility, configurably set | 
|---|
| 152 | * this up here. | 
|---|
| 153 | */ | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | void dump_core(void) | 
|---|
| 157 | { | 
|---|
| 158 | /* Note that even if core dumping has been disabled, we still set up | 
|---|
| 159 | * the core path. This is to handle the case where core dumping is | 
|---|
| 160 | * turned on in smb.conf and the relevant daemon is not restarted. | 
|---|
| 161 | */ | 
|---|
| 162 | if (!lp_enable_core_files()) { | 
|---|
| 163 | DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n")); | 
|---|
| 164 | exit(1); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | #if DUMP_CORE | 
|---|
| 168 | /* If we're running as non root we might not be able to dump the core | 
|---|
| 169 | * file to the corepath.  There must not be an unbecome_root() before | 
|---|
| 170 | * we call abort(). */ | 
|---|
| 171 | if (geteuid() != 0) { | 
|---|
| 172 | become_root(); | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | if (*corepath != '\0') { | 
|---|
| 176 | /* The chdir might fail if we dump core before we finish | 
|---|
| 177 | * processing the config file. | 
|---|
| 178 | */ | 
|---|
| 179 | if (chdir(corepath) != 0) { | 
|---|
| 180 | DEBUG(0, ("unable to change to %s\n", corepath)); | 
|---|
| 181 | DEBUGADD(0, ("refusing to dump core\n")); | 
|---|
| 182 | exit(1); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | DEBUG(0,("dumping core in %s\n", corepath)); | 
|---|
| 186 | } | 
|---|
| 187 |  | 
|---|
| 188 | umask(~(0700)); | 
|---|
| 189 | dbgflush(); | 
|---|
| 190 |  | 
|---|
| 191 | /* Ensure we don't have a signal handler for abort. */ | 
|---|
| 192 | #ifdef SIGABRT | 
|---|
| 193 | CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL); | 
|---|
| 194 | #endif | 
|---|
| 195 |  | 
|---|
| 196 | abort(); | 
|---|
| 197 |  | 
|---|
| 198 | #else /* DUMP_CORE */ | 
|---|
| 199 | exit(1); | 
|---|
| 200 | #endif /* DUMP_CORE */ | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|