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

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