lib/fault.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Critical Fault handling
00004    Copyright (C) Andrew Tridgell 1992-1998
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "includes.h"
00022 
00023 #ifdef HAVE_SYS_PRCTL_H
00024 #include <sys/prctl.h>
00025 #endif
00026 
00027 static void (*cont_fn)(void *);
00028 static pstring corepath;
00029 
00030 /*******************************************************************
00031 report a fault
00032 ********************************************************************/
00033 static void fault_report(int sig)
00034 {
00035         static int counter;
00036 
00037         if (counter) _exit(1);
00038 
00039         counter++;
00040 
00041         DEBUGSEP(0);
00042         DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),SAMBA_VERSION_STRING));
00043         DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n"));
00044         DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n"));
00045         DEBUGSEP(0);
00046   
00047         smb_panic("internal error");
00048 
00049         if (cont_fn) {
00050                 cont_fn(NULL);
00051 #ifdef SIGSEGV
00052                 CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
00053 #endif
00054 #ifdef SIGBUS
00055                 CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
00056 #endif
00057 #ifdef SIGABRT
00058                 CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
00059 #endif
00060                 return; /* this should cause a core dump */
00061         }
00062         exit(1);
00063 }
00064 
00065 /****************************************************************************
00066 catch serious errors
00067 ****************************************************************************/
00068 static void sig_fault(int sig)
00069 {
00070         fault_report(sig);
00071 }
00072 
00073 /*******************************************************************
00074 setup our fault handlers
00075 ********************************************************************/
00076 void fault_setup(void (*fn)(void *))
00077 {
00078         cont_fn = fn;
00079 
00080 #ifdef SIGSEGV
00081         CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
00082 #endif
00083 #ifdef SIGBUS
00084         CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
00085 #endif
00086 #ifdef SIGABRT
00087         CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
00088 #endif
00089 }
00090 
00091 /*******************************************************************
00092 make all the preparations to safely dump a core file
00093 ********************************************************************/
00094 
00095 void dump_core_setup(const char *progname)
00096 {
00097         pstring logbase;
00098         char * end;
00099 
00100         if (lp_logfile() && *lp_logfile()) {
00101                 snprintf(logbase, sizeof(logbase), "%s", lp_logfile());
00102                 if ((end = strrchr_m(logbase, '/'))) {
00103                         *end = '\0';
00104                 }
00105         } else {
00106                 /* We will end up here is the log file is given on the command
00107                  * line by the -l option but the "log file" option is not set
00108                  * in smb.conf.
00109                  */
00110                 snprintf(logbase, sizeof(logbase), "%s", dyn_LOGFILEBASE);
00111         }
00112 
00113         SMB_ASSERT(progname != NULL);
00114 
00115         snprintf(corepath, sizeof(corepath), "%s/cores", logbase);
00116         mkdir(corepath,0700);
00117 
00118         snprintf(corepath, sizeof(corepath), "%s/cores/%s",
00119                 logbase, progname);
00120         mkdir(corepath,0700);
00121 
00122         sys_chown(corepath,getuid(),getgid());
00123         chmod(corepath,0700);
00124 
00125 #ifdef HAVE_GETRLIMIT
00126 #ifdef RLIMIT_CORE
00127         {
00128                 struct rlimit rlp;
00129                 getrlimit(RLIMIT_CORE, &rlp);
00130                 rlp.rlim_cur = MAX(16*1024*1024,rlp.rlim_cur);
00131                 setrlimit(RLIMIT_CORE, &rlp);
00132                 getrlimit(RLIMIT_CORE, &rlp);
00133                 DEBUG(3,("Maximum core file size limits now %d(soft) %d(hard)\n",
00134                          (int)rlp.rlim_cur,(int)rlp.rlim_max));
00135         }
00136 #endif
00137 #endif
00138 
00139 #if defined(HAVE_PRCTL) && defined(PR_SET_DUMPABLE)
00140         /* On Linux we lose the ability to dump core when we change our user
00141          * ID. We know how to dump core safely, so let's make sure we have our
00142          * dumpable flag set.
00143          */
00144         prctl(PR_SET_DUMPABLE, 1);
00145 #endif
00146 
00147         /* FIXME: if we have a core-plus-pid facility, configurably set
00148          * this up here.
00149          */
00150 }
00151 
00152  void dump_core(void)
00153 {
00154         /* Note that even if core dumping has been disabled, we still set up
00155          * the core path. This is to handle the case where core dumping is
00156          * turned on in smb.conf and the relevant daemon is not restarted.
00157          */
00158         if (!lp_enable_core_files()) {
00159                 DEBUG(0, ("Exiting on internal error (core file administratively disabled)\n"));
00160                 exit(1);
00161         }
00162 
00163 #if DUMP_CORE
00164         /* If we're running as non root we might not be able to dump the core
00165          * file to the corepath.  There must not be an unbecome_root() before
00166          * we call abort(). */
00167         if (geteuid() != 0) {
00168                 become_root();
00169         }
00170 
00171         if (*corepath != '\0') {
00172                 /* The chdir might fail if we dump core before we finish
00173                  * processing the config file.
00174                  */
00175                 if (chdir(corepath) != 0) {
00176                         DEBUG(0, ("unable to change to %s\n", corepath));
00177                         DEBUGADD(0, ("refusing to dump core\n"));
00178                         exit(1);
00179                 }
00180 
00181                 DEBUG(0,("dumping core in %s\n", corepath));
00182         }
00183 
00184         umask(~(0700));
00185         dbgflush();
00186 
00187         /* Ensure we don't have a signal handler for abort. */
00188 #ifdef SIGABRT
00189         CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
00190 #endif
00191 
00192         abort();
00193 
00194 #else /* DUMP_CORE */
00195         exit(1);
00196 #endif /* DUMP_CORE */
00197 }
00198 

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7