source: trunk/third/mozilla/xpfe/bootstrap/nsSigHandlers.cpp @ 18860

Revision 18860, 6.3 KB checked in by rbasch, 17 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18859, which included commits to RCS files with non-trunk default branches.
Line 
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"
69static NS_DEFINE_CID(kAppShellServiceCID,   NS_APPSHELL_SERVICE_CID);
70#else
71extern "C" char * strsignal(int);
72#endif
73
74#ifdef MOZ_WIDGET_PHOTON
75#include <photon/PhProto.h>
76#include <sys/mman.h>                   /* for munlockall() */
77#endif
78
79static char _progname[1024] = "huh?";
80
81#if defined(LINUX) && defined(DEBUG) && (defined(__i386) || defined(PPC))
82#define CRAWL_STACK_ON_SIGSEGV
83#endif
84 
85#ifdef MOZ_WIDGET_PHOTON
86void abnormal_exit_handler(int signum)
87{
88  /* Free any shared memory that has been allocated */
89  PgShmemCleanup();
90
91#if defined(DEBUG)
92  if (    (signum == SIGSEGV)
93       || (signum == SIGILL)
94       || (signum == SIGABRT)
95     )
96  {
97    PR_GetCurrentThread();
98    printf("prog = %s\npid = %d\nsignal = %s\n",
99           _progname, getpid(), strsignal(signum));
100
101    printf("Sleeping for 5 minutes.\n");
102    printf("Type 'gdb %s %d' to attatch your debugger to this thread.\n",
103           _progname, getpid());
104
105    sleep(300);
106
107    printf("Done sleeping...\n");
108  }
109#endif
110
111  _exit(1);
112}
113#elif defined(CRAWL_STACK_ON_SIGSEGV)
114
115#include <unistd.h>
116#include "nsISupportsUtils.h"
117
118void
119ah_crap_handler(int signum)
120{
121  PR_GetCurrentThread();
122
123  // I don't think strsignal is portable.  If it is, this can be changed.
124#ifdef LINUX
125  printf("\nProgram %s (pid = %d) received %s signal.\n",
126         _progname,
127         getpid(),
128         strsignal(signum));
129#else
130  printf("\nProgram %s (pid = %d) received signal %d.\n",
131         _progname,
132         getpid(),
133         signum);
134#endif
135 
136  printf("Stack:\n");
137  nsTraceRefcnt::WalkTheStack(stdout);
138
139  printf("Sleeping for 5 minutes.\n");
140  printf("Type 'gdb %s %d' to attatch your debugger to this thread.\n",
141         _progname,
142         getpid());
143
144  sleep(300);
145
146  printf("Done sleeping...\n");
147}
148#endif // CRAWL_STACK_ON_SIGSEGV
149
150#ifdef XP_BEOS
151void beos_signal_handler(int signum) {
152#ifdef DEBUG
153        fprintf(stderr, "beos_signal_handler: %d\n", signum);
154#endif
155        nsresult rv;
156        nsCOMPtr<nsIAppShellService> appShellService(do_GetService(kAppShellServiceCID,&rv));
157        if (NS_FAILED(rv)) {
158                // Failed to get the appshell service so shutdown the hard way
159#ifdef DEBUG
160                fprintf(stderr, "beos_signal_handler: appShell->do_GetService() failed\n");
161#endif
162                exit(13);
163        }
164
165        // Exit the appshell so that the app can shutdown normally
166        appShellService->Quit(nsIAppShellService::eAttemptQuit);
167}
168#endif
169
170void InstallUnixSignalHandlers(const char *ProgramName)
171{
172
173  PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) );
174
175#if defined(MOZ_WIDGET_PHOTON)
176 /* Neutrino need this to free shared memory in case of a crash */
177  signal(SIGTERM, abnormal_exit_handler);
178  signal(SIGQUIT, abnormal_exit_handler);
179  signal(SIGINT,  abnormal_exit_handler);
180  signal(SIGHUP,  abnormal_exit_handler);
181  signal(SIGSEGV, abnormal_exit_handler);
182  signal(SIGILL,  abnormal_exit_handler);
183  signal(SIGABRT, abnormal_exit_handler);
184
185/* Tell the OS it can page any part of this program to virtual memory */
186  munlockall();
187#elif defined(CRAWL_STACK_ON_SIGSEGV)
188  signal(SIGSEGV, ah_crap_handler);
189  signal(SIGILL, ah_crap_handler);
190  signal(SIGABRT, ah_crap_handler);
191#endif // CRAWL_STACK_ON_SIGSEGV
192
193#if defined(DEBUG) && defined(LINUX)
194  char *text = PR_GetEnv("MOZ_MEM_LIMIT");
195  if (text)
196  {
197    long m = atoi(text);
198    m *= (1024*1024);   
199    struct rlimit r;
200    r.rlim_cur = m;
201    r.rlim_max = m;
202    setrlimit(RLIMIT_AS, &r);
203  }
204#endif
205
206#if defined(SOLARIS)
207
208    #define NOFILES 512
209
210    // Boost Solaris file descriptors
211    {
212        struct rlimit rl;
213       
214        if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
215
216            if (rl.rlim_cur < NOFILES) {
217                rl.rlim_cur = NOFILES;
218
219                if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
220                    perror("setrlimit(RLIMIT_NOFILE)");
221                    fprintf(stderr, "Cannot exceed hard limit for open files");
222                }
223#if defined(DEBUG)
224                if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
225                    printf("File descriptors set to %d\n", rl.rlim_cur);
226#endif //DEBUG
227            }
228    }
229#endif //SOLARIS
230
231#ifdef XP_BEOS
232        signal(SIGTERM, beos_signal_handler);
233#endif
234}
Note: See TracBrowser for help on using the repository browser.