[18859] | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
---|
| 2 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
| 3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
---|
| 4 | * |
---|
| 5 | * The contents of this file are subject to the Netscape Public License |
---|
| 6 | * Version 1.1 (the "License"); you may not use this file except in |
---|
| 7 | * compliance with the License. You may obtain a copy of the License at |
---|
| 8 | * http://www.mozilla.org/NPL/ |
---|
| 9 | * |
---|
| 10 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
| 11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
| 12 | * for the specific language governing rights and limitations under the |
---|
| 13 | * License. |
---|
| 14 | * |
---|
| 15 | * The Original Code is Mozilla Communicator client code. |
---|
| 16 | * |
---|
| 17 | * The Initial Developer of the Original Code is |
---|
| 18 | * Netscape Communications Corporation. |
---|
| 19 | * Portions created by the Initial Developer are Copyright (C) 1998 |
---|
| 20 | * the Initial Developer. All Rights Reserved. |
---|
| 21 | * |
---|
| 22 | * Contributor(s): |
---|
| 23 | * Jerry.Kirk@Nexwarecorp.com |
---|
| 24 | * Chris Seawood <cls@seawood.org> |
---|
| 25 | * |
---|
| 26 | * Alternatively, the contents of this file may be used under the terms of |
---|
| 27 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
---|
| 28 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
---|
| 29 | * in which case the provisions of the GPL or the LGPL are applicable instead |
---|
| 30 | * of those above. If you wish to allow use of your version of this file only |
---|
| 31 | * under the terms of either the GPL or the LGPL, and not to allow others to |
---|
| 32 | * use your version of this file under the terms of the NPL, indicate your |
---|
| 33 | * decision by deleting the provisions above and replace them with the notice |
---|
| 34 | * and other provisions required by the GPL or the LGPL. If you do not delete |
---|
| 35 | * the provisions above, a recipient may use your version of this file under |
---|
| 36 | * the terms of any one of the NPL, the GPL or the LGPL. |
---|
| 37 | * |
---|
| 38 | * ***** END LICENSE BLOCK ***** */ |
---|
| 39 | |
---|
| 40 | /* |
---|
| 41 | * This module is supposed to abstract signal handling away from the other |
---|
| 42 | * platforms that do not support it. |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | #include <signal.h> |
---|
| 46 | #include <stdio.h> |
---|
| 47 | #include "prthread.h" |
---|
| 48 | #include "plstr.h" |
---|
| 49 | #include "prenv.h" |
---|
| 50 | |
---|
| 51 | #if defined(LINUX) |
---|
| 52 | #include <sys/time.h> |
---|
| 53 | #include <sys/resource.h> |
---|
| 54 | #include <unistd.h> |
---|
| 55 | #include <stdlib.h> // atoi |
---|
| 56 | #endif |
---|
| 57 | |
---|
| 58 | #if defined(SOLARIS) |
---|
| 59 | #include <sys/resource.h> |
---|
| 60 | #endif |
---|
| 61 | |
---|
| 62 | #ifdef XP_BEOS |
---|
| 63 | #include <be/app/Application.h> |
---|
| 64 | #include <string.h> |
---|
| 65 | #include "nsCOMPtr.h" |
---|
| 66 | #include "nsIServiceManager.h" |
---|
| 67 | #include "nsIAppShellService.h" |
---|
| 68 | #include "nsAppShellCIDs.h" |
---|
| 69 | static NS_DEFINE_CID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID); |
---|
[19517] | 70 | #elif defined(LINUX) |
---|
| 71 | #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) |
---|
| 72 | #include <string.h> |
---|
[18859] | 73 | #else |
---|
| 74 | extern "C" char * strsignal(int); |
---|
| 75 | #endif |
---|
[19517] | 76 | #endif |
---|
[18859] | 77 | |
---|
| 78 | #ifdef MOZ_WIDGET_PHOTON |
---|
| 79 | #include <photon/PhProto.h> |
---|
| 80 | #include <sys/mman.h> /* for munlockall() */ |
---|
| 81 | #endif |
---|
| 82 | |
---|
| 83 | static char _progname[1024] = "huh?"; |
---|
| 84 | |
---|
| 85 | #if defined(LINUX) && defined(DEBUG) && (defined(__i386) || defined(PPC)) |
---|
| 86 | #define CRAWL_STACK_ON_SIGSEGV |
---|
| 87 | #endif |
---|
| 88 | |
---|
| 89 | #ifdef MOZ_WIDGET_PHOTON |
---|
| 90 | void abnormal_exit_handler(int signum) |
---|
| 91 | { |
---|
| 92 | /* Free any shared memory that has been allocated */ |
---|
| 93 | PgShmemCleanup(); |
---|
| 94 | |
---|
| 95 | #if defined(DEBUG) |
---|
| 96 | if ( (signum == SIGSEGV) |
---|
| 97 | || (signum == SIGILL) |
---|
| 98 | || (signum == SIGABRT) |
---|
| 99 | ) |
---|
| 100 | { |
---|
| 101 | PR_GetCurrentThread(); |
---|
| 102 | printf("prog = %s\npid = %d\nsignal = %s\n", |
---|
| 103 | _progname, getpid(), strsignal(signum)); |
---|
| 104 | |
---|
| 105 | printf("Sleeping for 5 minutes.\n"); |
---|
| 106 | printf("Type 'gdb %s %d' to attatch your debugger to this thread.\n", |
---|
| 107 | _progname, getpid()); |
---|
| 108 | |
---|
| 109 | sleep(300); |
---|
| 110 | |
---|
| 111 | printf("Done sleeping...\n"); |
---|
| 112 | } |
---|
| 113 | #endif |
---|
| 114 | |
---|
| 115 | _exit(1); |
---|
| 116 | } |
---|
| 117 | #elif defined(CRAWL_STACK_ON_SIGSEGV) |
---|
| 118 | |
---|
| 119 | #include <unistd.h> |
---|
| 120 | #include "nsISupportsUtils.h" |
---|
[19517] | 121 | #include "nsStackFrameUnix.h" |
---|
[18859] | 122 | |
---|
| 123 | void |
---|
| 124 | ah_crap_handler(int signum) |
---|
| 125 | { |
---|
| 126 | PR_GetCurrentThread(); |
---|
| 127 | |
---|
| 128 | // I don't think strsignal is portable. If it is, this can be changed. |
---|
| 129 | #ifdef LINUX |
---|
| 130 | printf("\nProgram %s (pid = %d) received %s signal.\n", |
---|
| 131 | _progname, |
---|
| 132 | getpid(), |
---|
| 133 | strsignal(signum)); |
---|
| 134 | #else |
---|
| 135 | printf("\nProgram %s (pid = %d) received signal %d.\n", |
---|
| 136 | _progname, |
---|
| 137 | getpid(), |
---|
| 138 | signum); |
---|
| 139 | #endif |
---|
| 140 | |
---|
| 141 | printf("Stack:\n"); |
---|
[19517] | 142 | DumpStackToFile(stdout); |
---|
[18859] | 143 | |
---|
| 144 | printf("Sleeping for 5 minutes.\n"); |
---|
| 145 | printf("Type 'gdb %s %d' to attatch your debugger to this thread.\n", |
---|
| 146 | _progname, |
---|
| 147 | getpid()); |
---|
| 148 | |
---|
| 149 | sleep(300); |
---|
| 150 | |
---|
| 151 | printf("Done sleeping...\n"); |
---|
| 152 | } |
---|
| 153 | #endif // CRAWL_STACK_ON_SIGSEGV |
---|
| 154 | |
---|
| 155 | #ifdef XP_BEOS |
---|
| 156 | void beos_signal_handler(int signum) { |
---|
| 157 | #ifdef DEBUG |
---|
| 158 | fprintf(stderr, "beos_signal_handler: %d\n", signum); |
---|
| 159 | #endif |
---|
| 160 | nsresult rv; |
---|
| 161 | nsCOMPtr<nsIAppShellService> appShellService(do_GetService(kAppShellServiceCID,&rv)); |
---|
| 162 | if (NS_FAILED(rv)) { |
---|
| 163 | // Failed to get the appshell service so shutdown the hard way |
---|
| 164 | #ifdef DEBUG |
---|
| 165 | fprintf(stderr, "beos_signal_handler: appShell->do_GetService() failed\n"); |
---|
| 166 | #endif |
---|
| 167 | exit(13); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | // Exit the appshell so that the app can shutdown normally |
---|
| 171 | appShellService->Quit(nsIAppShellService::eAttemptQuit); |
---|
| 172 | } |
---|
| 173 | #endif |
---|
| 174 | |
---|
| 175 | void InstallUnixSignalHandlers(const char *ProgramName) |
---|
| 176 | { |
---|
| 177 | |
---|
| 178 | PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) ); |
---|
| 179 | |
---|
| 180 | #if defined(MOZ_WIDGET_PHOTON) |
---|
| 181 | /* Neutrino need this to free shared memory in case of a crash */ |
---|
| 182 | signal(SIGTERM, abnormal_exit_handler); |
---|
| 183 | signal(SIGQUIT, abnormal_exit_handler); |
---|
| 184 | signal(SIGINT, abnormal_exit_handler); |
---|
| 185 | signal(SIGHUP, abnormal_exit_handler); |
---|
| 186 | signal(SIGSEGV, abnormal_exit_handler); |
---|
| 187 | signal(SIGILL, abnormal_exit_handler); |
---|
| 188 | signal(SIGABRT, abnormal_exit_handler); |
---|
| 189 | |
---|
| 190 | /* Tell the OS it can page any part of this program to virtual memory */ |
---|
| 191 | munlockall(); |
---|
| 192 | #elif defined(CRAWL_STACK_ON_SIGSEGV) |
---|
| 193 | signal(SIGSEGV, ah_crap_handler); |
---|
| 194 | signal(SIGILL, ah_crap_handler); |
---|
| 195 | signal(SIGABRT, ah_crap_handler); |
---|
| 196 | #endif // CRAWL_STACK_ON_SIGSEGV |
---|
| 197 | |
---|
| 198 | #if defined(DEBUG) && defined(LINUX) |
---|
| 199 | char *text = PR_GetEnv("MOZ_MEM_LIMIT"); |
---|
| 200 | if (text) |
---|
| 201 | { |
---|
| 202 | long m = atoi(text); |
---|
| 203 | m *= (1024*1024); |
---|
| 204 | struct rlimit r; |
---|
| 205 | r.rlim_cur = m; |
---|
| 206 | r.rlim_max = m; |
---|
| 207 | setrlimit(RLIMIT_AS, &r); |
---|
| 208 | } |
---|
| 209 | #endif |
---|
| 210 | |
---|
| 211 | #if defined(SOLARIS) |
---|
| 212 | |
---|
| 213 | #define NOFILES 512 |
---|
| 214 | |
---|
| 215 | // Boost Solaris file descriptors |
---|
| 216 | { |
---|
| 217 | struct rlimit rl; |
---|
| 218 | |
---|
| 219 | if (getrlimit(RLIMIT_NOFILE, &rl) == 0) |
---|
| 220 | |
---|
| 221 | if (rl.rlim_cur < NOFILES) { |
---|
| 222 | rl.rlim_cur = NOFILES; |
---|
| 223 | |
---|
| 224 | if (setrlimit(RLIMIT_NOFILE, &rl) < 0) { |
---|
| 225 | perror("setrlimit(RLIMIT_NOFILE)"); |
---|
| 226 | fprintf(stderr, "Cannot exceed hard limit for open files"); |
---|
| 227 | } |
---|
| 228 | #if defined(DEBUG) |
---|
| 229 | if (getrlimit(RLIMIT_NOFILE, &rl) == 0) |
---|
| 230 | printf("File descriptors set to %d\n", rl.rlim_cur); |
---|
| 231 | #endif //DEBUG |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | #endif //SOLARIS |
---|
| 235 | |
---|
| 236 | #ifdef XP_BEOS |
---|
| 237 | signal(SIGTERM, beos_signal_handler); |
---|
| 238 | #endif |
---|
| 239 | } |
---|