Fix all warnings in source3 with gcc4.3.
[ira/wip.git] / source3 / lib / fault.c
index 20c75f7876c18e15add401c676f2a956c83cf649..c24fea1ca91267b7156699fbd38cbaf3a0eac13e 100644 (file)
@@ -1,12 +1,11 @@
 /* 
-   Unix SMB/Netbios implementation.
-   Version 1.9.
+   Unix SMB/CIFS implementation.
    Critical Fault handling
-   Copyright (C) Andrew Tridgell 1992-1995
+   Copyright (C) Andrew Tridgell 1992-1998
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#ifdef LINUX
-#define __KERNEL__
-#endif
-
 #include "includes.h"
-extern int DEBUGLEVEL;
-
 
-static void (*cont_fn)();
+#ifdef HAVE_SYS_PRCTL_H
+#include <sys/prctl.h>
+#endif
 
+static void (*cont_fn)(void *);
+static char *corepath;
 
 /*******************************************************************
 report a fault
 ********************************************************************/
 static void fault_report(int sig)
 {
-  DEBUG(0,("===============================================================\n"));
-  DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)getpid(),VERSION));
-  DEBUG(0,("\nPlease read the file BUGS.txt in the distribution\n"));
-  DEBUG(0,("===============================================================\n"));
+       static int counter;
+
+       if (counter) _exit(1);
+
+       counter++;
+
+       DEBUGSEP(0);
+       DEBUG(0,("INTERNAL ERROR: Signal %d in pid %d (%s)",sig,(int)sys_getpid(),SAMBA_VERSION_STRING));
+       DEBUG(0,("\nPlease read the Trouble-Shooting section of the Samba3-HOWTO\n"));
+       DEBUG(0,("\nFrom: http://www.samba.org/samba/docs/Samba3-HOWTO.pdf\n"));
+       DEBUGSEP(0);
   
-#if AJT
-  ajt_panic();
-#endif  
-
-  if (cont_fn)
-    {
-      fault_setup(cont_fn);
-      cont_fn(NULL);
+       smb_panic("internal error");
+
+       if (cont_fn) {
+               cont_fn(NULL);
 #ifdef SIGSEGV
-      signal(SIGSEGV,SIGNAL_CAST SIG_DFL);
+               CatchSignal(SIGSEGV,SIGNAL_CAST SIG_DFL);
 #endif
 #ifdef SIGBUS
-      signal(SIGBUS,SIGNAL_CAST SIG_DFL);
+               CatchSignal(SIGBUS,SIGNAL_CAST SIG_DFL);
 #endif
-      return; /* this should cause a core dump */
-    }
-  exit(1);
+#ifdef SIGABRT
+               CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
+#endif
+               return; /* this should cause a core dump */
+       }
+       exit(1);
 }
 
 /****************************************************************************
@@ -64,23 +66,184 @@ catch serious errors
 ****************************************************************************/
 static void sig_fault(int sig)
 {
-  fault_report(sig);
+       fault_report(sig);
 }
 
 /*******************************************************************
 setup our fault handlers
 ********************************************************************/
-void fault_setup(void (*fn)())
+void fault_setup(void (*fn)(void *))
 {
-  cont_fn = fn;
+       cont_fn = fn;
 
 #ifdef SIGSEGV
-  signal(SIGSEGV,SIGNAL_CAST sig_fault);
+       CatchSignal(SIGSEGV,SIGNAL_CAST sig_fault);
 #endif
 #ifdef SIGBUS
-  signal(SIGBUS,SIGNAL_CAST sig_fault);
+       CatchSignal(SIGBUS,SIGNAL_CAST sig_fault);
+#endif
+#ifdef SIGABRT
+       CatchSignal(SIGABRT,SIGNAL_CAST sig_fault);
+#endif
+}
+
+/*******************************************************************
+make all the preparations to safely dump a core file
+********************************************************************/
+
+void dump_core_setup(const char *progname)
+{
+       char *logbase = NULL;
+       char *end = NULL;
+
+       if (lp_logfile() && *lp_logfile()) {
+               if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
+                       return;
+               }
+               if ((end = strrchr_m(logbase, '/'))) {
+                       *end = '\0';
+               }
+       } else {
+               /* We will end up here is 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);
+
+       if (asprintf(&corepath, "%s/cores", logbase) < 0) {
+               SAFE_FREE(logbase);
+               return;
+       }
+       if (mkdir(corepath,0700) == -1) {
+               if (errno != EEXIST) {
+                       SAFE_FREE(corepath);
+                       SAFE_FREE(logbase);
+                       return;
+               }
+       }
+       if (chmod(corepath,0700) == -1) {
+               SAFE_FREE(corepath);
+               SAFE_FREE(logbase);
+               return;
+       }
+
+       SAFE_FREE(corepath);
+       if (asprintf(&corepath, "%s/cores/%s",
+                       logbase, progname) < 0) {
+               SAFE_FREE(logbase);
+               return;
+       }
+       if (mkdir(corepath,0700) == -1) {
+               if (errno != EEXIST) {
+                       SAFE_FREE(corepath);
+                       SAFE_FREE(logbase);
+                       return;
+               }
+       }
+
+       if (chown(corepath,getuid(),getgid()) == -1) {
+               SAFE_FREE(corepath);
+               SAFE_FREE(logbase);
+               return;
+       }
+       if (chmod(corepath,0700) == -1) {
+               SAFE_FREE(corepath);
+               SAFE_FREE(logbase);
+               return;
+       }
+
+       SAFE_FREE(corepath);
+       SAFE_FREE(logbase);
+
+#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
+
+#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
+
+       /* FIXME: if we have a core-plus-pid facility, configurably set
+        * this up here.
+        */
 }
 
+ 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() != 0) {
+               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();
+
+       /* Ensure we don't have a signal handler for abort. */
+#ifdef SIGABRT
+       CatchSignal(SIGABRT,SIGNAL_CAST SIG_DFL);
+#endif
+
+       abort();
+
+#else /* DUMP_CORE */
+       exit(1);
+#endif /* DUMP_CORE */
+}