Back to index

lightning-sunbird  0.9+nobinonly
nsSigHandlers.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jerry.Kirk@Nexwarecorp.com
00024  *   Chris Seawood <cls@seawood.org>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * This module is supposed to abstract signal handling away from the other
00042  * platforms that do not support it.
00043  */
00044 
00045 #include <signal.h>
00046 #include <stdio.h>
00047 #include "prthread.h"
00048 #include "plstr.h"
00049 #include "prenv.h"
00050 
00051 #if defined(LINUX)
00052 #include <sys/time.h>
00053 #include <sys/resource.h>
00054 #include <unistd.h>
00055 #include <stdlib.h> // atoi
00056 #endif
00057 
00058 #if defined(SOLARIS)
00059 #include <sys/resource.h>
00060 #endif
00061 
00062 #ifdef XP_BEOS
00063 #include <be/app/Application.h>
00064 #include <string.h>
00065 #include "nsCOMPtr.h"
00066 #include "nsIServiceManager.h"
00067 #include "nsIAppStartup.h"
00068 #include "nsXPFEComponentsCID.h"
00069 #endif
00070 
00071 #ifdef MOZ_WIDGET_PHOTON
00072 #include <photon/PhProto.h>
00073 #include <string.h>
00074 #endif
00075 
00076 static char _progname[1024] = "huh?";
00077 static unsigned int _gdb_sleep_duration = 300;
00078 
00079 #if defined(LINUX) && defined(DEBUG) && (defined(__i386) || defined(PPC))
00080 #define CRAWL_STACK_ON_SIGSEGV
00081 #endif
00082  
00083 #ifdef MOZ_WIDGET_PHOTON
00084 void abnormal_exit_handler(int signum)
00085 {
00086   /* Free any shared memory that has been allocated */
00087   PgShmemCleanup();
00088 
00089 #if defined(DEBUG)
00090   if (    (signum == SIGSEGV)
00091        || (signum == SIGILL)
00092        || (signum == SIGABRT)
00093      )
00094   {
00095     printf("prog = %s\npid = %d\nsignal = %s\n", 
00096            _progname, getpid(), strsignal(signum));
00097 
00098     printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
00099     printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
00100            _progname, getpid());
00101 
00102     sleep(_gdb_sleep_duration);
00103 
00104     printf("Done sleeping...\n");
00105   }
00106 #endif
00107 
00108   _exit(1);
00109 } 
00110 #elif defined(CRAWL_STACK_ON_SIGSEGV)
00111 
00112 #include <unistd.h>
00113 #include "nsISupportsUtils.h"
00114 #include "nsStackFrameUnix.h"
00115 
00116 void
00117 ah_crap_handler(int signum)
00118 {
00119 
00120   printf("\nProgram %s (pid = %d) received signal %d.\n",
00121          _progname,
00122          getpid(),
00123          signum);
00124   
00125   printf("Stack:\n");
00126   DumpStackToFile(stdout);
00127 
00128   printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
00129   printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
00130          _progname,
00131          getpid());
00132 
00133   sleep(_gdb_sleep_duration);
00134 
00135   printf("Done sleeping...\n");
00136 }
00137 #endif // CRAWL_STACK_ON_SIGSEGV
00138 
00139 #ifdef XP_BEOS
00140 void beos_signal_handler(int signum) {
00141 #ifdef DEBUG
00142        fprintf(stderr, "beos_signal_handler: %d\n", signum);
00143 #endif
00144        nsresult rv;
00145        nsCOMPtr<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
00146        if (NS_FAILED(rv)) {
00147               // Failed to get the appstartup service so shutdown the hard way
00148 #ifdef DEBUG
00149               fprintf(stderr, "beos_signal_handler: appShell->do_GetService() failed\n");
00150 #endif
00151               exit(13);
00152        }
00153 
00154        // Exit the appshell so that the app can shutdown normally
00155        appStartup->Quit(nsIAppStartup::eAttemptQuit);
00156 }
00157 #endif 
00158 
00159 void InstallUnixSignalHandlers(const char *ProgramName)
00160 {
00161 
00162   PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) );
00163 
00164   const char *gdbSleep = PR_GetEnv("MOZ_GDB_SLEEP");
00165   if (gdbSleep && *gdbSleep)
00166   {
00167     unsigned int s;
00168     if (1 == sscanf(gdbSleep, "%u", &s)) {
00169       _gdb_sleep_duration = s;
00170     }
00171   }
00172 
00173 #if defined(MOZ_WIDGET_PHOTON)
00174  /* Neutrino need this to free shared memory in case of a crash */
00175   signal(SIGTERM, abnormal_exit_handler);
00176   signal(SIGQUIT, abnormal_exit_handler);
00177   signal(SIGINT,  abnormal_exit_handler);
00178   signal(SIGHUP,  abnormal_exit_handler);
00179   signal(SIGSEGV, abnormal_exit_handler);
00180   signal(SIGILL,  abnormal_exit_handler);
00181   signal(SIGABRT, abnormal_exit_handler);
00182 
00183 #elif defined(CRAWL_STACK_ON_SIGSEGV)
00184   signal(SIGSEGV, ah_crap_handler);
00185   signal(SIGILL, ah_crap_handler);
00186   signal(SIGABRT, ah_crap_handler);
00187 #endif // CRAWL_STACK_ON_SIGSEGV
00188 
00189 #if defined(DEBUG) && defined(LINUX)
00190   char *text = PR_GetEnv("MOZ_MEM_LIMIT");
00191   if (text) 
00192   {
00193     long m = atoi(text);
00194     m *= (1024*1024);    
00195     struct rlimit r;
00196     r.rlim_cur = m;
00197     r.rlim_max = m;
00198     setrlimit(RLIMIT_AS, &r);
00199   }
00200 #endif
00201 
00202 #if defined(SOLARIS)
00203 
00204     #define NOFILES 512
00205 
00206     // Boost Solaris file descriptors
00207     {
00208        struct rlimit rl;
00209        
00210        if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
00211 
00212            if (rl.rlim_cur < NOFILES) {
00213               rl.rlim_cur = NOFILES;
00214 
00215               if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
00216                   perror("setrlimit(RLIMIT_NOFILE)");
00217                   fprintf(stderr, "Cannot exceed hard limit for open files");
00218               }
00219 #if defined(DEBUG)
00220               if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
00221                   printf("File descriptors set to %d\n", rl.rlim_cur);
00222 #endif //DEBUG
00223            }
00224     }
00225 #endif //SOLARIS
00226 
00227 #ifdef XP_BEOS
00228        signal(SIGTERM, beos_signal_handler);
00229 #endif
00230 }