Bug 452317 - FeedConverter.js: QueryInterface should throw NS_ERROR_NO_INTERFACE...
[wine-gecko.git] / toolkit / xre / nsSigHandlers.cpp
blobd080ef09587952d093a7053a9d525f04271ecf2a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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.
15 * The Original Code is Mozilla Communicator client code.
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.
22 * Contributor(s):
23 * Jerry.Kirk@Nexwarecorp.com
24 * Chris Seawood <cls@seawood.org>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or 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 MPL, 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 MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * This module is supposed to abstract signal handling away from the other
42 * platforms that do not support it.
45 #include <signal.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include "prthread.h"
49 #include "plstr.h"
50 #include "prenv.h"
51 #include "nsDebug.h"
53 #if defined(LINUX)
54 #include <sys/time.h>
55 #include <sys/resource.h>
56 #include <unistd.h>
57 #include <stdlib.h> // atoi
58 #endif
60 #if defined(SOLARIS)
61 #include <sys/resource.h>
62 #endif
64 #ifdef XP_BEOS
65 #include <be/app/Application.h>
66 #include <string.h>
67 #include "nsCOMPtr.h"
68 #include "nsIServiceManager.h"
69 #include "nsIAppStartup.h"
70 #include "nsXPFEComponentsCID.h"
71 #endif
73 #ifdef MOZ_WIDGET_PHOTON
74 #include <photon/PhProto.h>
75 #endif
77 static char _progname[1024] = "huh?";
78 static unsigned int _gdb_sleep_duration = 300;
80 #if defined(LINUX) && defined(DEBUG) && \
81 (defined(__i386) || defined(__x86_64) || defined(PPC))
82 #define CRAWL_STACK_ON_SIGSEGV
83 #endif
85 #ifdef MOZ_WIDGET_PHOTON
86 void abnormal_exit_handler(int signum)
88 /* Free any shared memory that has been allocated */
89 PgShmemCleanup();
91 #if defined(DEBUG)
92 if ( (signum == SIGSEGV)
93 || (signum == SIGILL)
94 || (signum == SIGABRT)
95 || (signum == SIGFPE)
98 printf("prog = %s\npid = %d\nsignal = %s\n",
99 _progname, getpid(), strsignal(signum));
101 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
102 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
103 _progname, getpid());
105 sleep(_gdb_sleep_duration);
107 printf("Done sleeping...\n");
109 #endif
111 _exit(1);
113 #elif defined(CRAWL_STACK_ON_SIGSEGV)
115 #include <unistd.h>
116 #include "nsISupportsUtils.h"
117 #include "nsStackWalk.h"
119 extern "C" {
121 PR_STATIC_CALLBACK(void) PrintStackFrame(void *aPC, void *aClosure)
123 char buf[1024];
124 nsCodeAddressDetails details;
126 NS_DescribeCodeAddress(aPC, &details);
127 NS_FormatCodeAddressDetails(aPC, &details, buf, sizeof(buf));
128 fprintf(stdout, buf);
133 void
134 ah_crap_handler(int signum)
136 printf("\nProgram %s (pid = %d) received signal %d.\n",
137 _progname,
138 getpid(),
139 signum);
141 printf("Stack:\n");
142 NS_StackWalk(PrintStackFrame, 2, nsnull);
144 printf("Sleeping for %d seconds.\n",_gdb_sleep_duration);
145 printf("Type 'gdb %s %d' to attach your debugger to this thread.\n",
146 _progname,
147 getpid());
149 sleep(_gdb_sleep_duration);
151 printf("Done sleeping...\n");
153 #endif // CRAWL_STACK_ON_SIGSEGV
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<nsIAppStartup> appStartup(do_GetService(NS_APPSTARTUP_CONTRACTID, &rv));
162 if (NS_FAILED(rv)) {
163 // Failed to get the appstartup 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);
170 // Exit the appshell so that the app can shutdown normally
171 appStartup->Quit(nsIAppStartup::eAttemptQuit);
173 #endif
175 #ifdef MOZ_WIDGET_GTK2
176 // Need this include for version test below.
177 #include <glib.h>
178 #endif
180 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
182 static GLogFunc orig_log_func = NULL;
184 extern "C" {
185 static void
186 my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
187 const gchar *message, gpointer user_data);
190 /* static */ void
191 my_glib_log_func(const gchar *log_domain, GLogLevelFlags log_level,
192 const gchar *message, gpointer user_data)
194 if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION)) {
195 NS_DebugBreak(NS_DEBUG_ASSERTION, message, "glib assertion", __FILE__, __LINE__);
196 } else if (log_level & (G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)) {
197 NS_DebugBreak(NS_DEBUG_WARNING, message, "glib warning", __FILE__, __LINE__);
200 orig_log_func(log_domain, log_level, message, NULL);
203 #endif
205 void InstallUnixSignalHandlers(const char *ProgramName)
207 PL_strncpy(_progname,ProgramName, (sizeof(_progname)-1) );
209 const char *gdbSleep = PR_GetEnv("MOZ_GDB_SLEEP");
210 if (gdbSleep && *gdbSleep)
212 unsigned int s;
213 if (1 == sscanf(gdbSleep, "%u", &s)) {
214 _gdb_sleep_duration = s;
218 #if defined(MOZ_WIDGET_PHOTON)
219 /* Neutrino need this to free shared memory in case of a crash */
220 signal(SIGTERM, abnormal_exit_handler);
221 signal(SIGQUIT, abnormal_exit_handler);
222 signal(SIGINT, abnormal_exit_handler);
223 signal(SIGHUP, abnormal_exit_handler);
224 signal(SIGSEGV, abnormal_exit_handler);
225 signal(SIGILL, abnormal_exit_handler);
226 signal(SIGABRT, abnormal_exit_handler);
227 signal(SIGFPE, abnormal_exit_handler);
229 #elif defined(CRAWL_STACK_ON_SIGSEGV)
230 signal(SIGSEGV, ah_crap_handler);
231 signal(SIGILL, ah_crap_handler);
232 signal(SIGABRT, ah_crap_handler);
233 signal(SIGFPE, ah_crap_handler);
234 #endif // CRAWL_STACK_ON_SIGSEGV
236 #if defined(DEBUG) && defined(LINUX)
237 const char *memLimit = PR_GetEnv("MOZ_MEM_LIMIT");
238 if (memLimit && *memLimit)
240 long m = atoi(memLimit);
241 m *= (1024*1024);
242 struct rlimit r;
243 r.rlim_cur = m;
244 r.rlim_max = m;
245 setrlimit(RLIMIT_AS, &r);
247 #endif
249 #if defined(SOLARIS)
250 #define NOFILES 512
252 // Boost Solaris file descriptors
254 struct rlimit rl;
256 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
258 if (rl.rlim_cur < NOFILES) {
259 rl.rlim_cur = NOFILES;
261 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
262 perror("setrlimit(RLIMIT_NOFILE)");
263 fprintf(stderr, "Cannot exceed hard limit for open files");
265 #if defined(DEBUG)
266 if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
267 printf("File descriptors set to %d\n", rl.rlim_cur);
268 #endif //DEBUG
271 #endif //SOLARIS
273 #ifdef XP_BEOS
274 signal(SIGTERM, beos_signal_handler);
275 #endif
277 #if defined(MOZ_WIDGET_GTK2) && (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
278 const char *assertString = PR_GetEnv("XPCOM_DEBUG_BREAK");
279 if (assertString &&
280 (!strcmp(assertString, "suspend") ||
281 !strcmp(assertString, "stack") ||
282 !strcmp(assertString, "abort") ||
283 !strcmp(assertString, "trap") ||
284 !strcmp(assertString, "break"))) {
285 // Override the default glib logging function so we get stacks for it too.
286 orig_log_func = g_log_set_default_handler(my_glib_log_func, NULL);
288 #endif