This commit was manufactured by cvs2svn to create tag 'LAST_STABLE'.
[claws.git] / src / crash.c
blob9ecd6ac362c4bc71ba2d80ad551ac90b808af490
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002 by the Sylpheed Claws Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #ifdef CRASH_DIALOG
26 #include <glib.h>
27 #include <gtk/gtk.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <signal.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/wait.h>
35 #include <errno.h>
36 #include <fcntl.h>
38 #if HAVE_SYS_UTSNAME_H
39 # include <sys/utsname.h>
40 #endif
42 #if defined(__GNU_LIBRARY__)
43 # include <gnu/libc-version.h>
44 #endif
46 #ifdef SIGTERM
47 #include "main.h"
48 #endif
49 #include "intl.h"
50 #include "crash.h"
51 #include "utils.h"
52 #include "filesel.h"
53 #include "version.h"
54 #include "prefs_common.h"
57 * NOTE: the crash dialog is called when sylpheed is not
58 * initialized, so do not assume settings are available.
59 * for example, loading / creating pixmaps seems not
60 * to be possible.
63 /***/
65 static GtkWidget *crash_dialog_show (const gchar *text,
66 const gchar *debug_output);
67 static void crash_create_debugger_file (void);
68 static void crash_save_crash_log (GtkButton *, const gchar *);
69 static void crash_create_bug_report (GtkButton *, const gchar *);
70 static void crash_debug (unsigned long crash_pid,
71 gchar *exe_image,
72 GString *debug_output);
73 static const gchar *get_compiled_in_features (void);
74 static const gchar *get_lib_version (void);
75 static const gchar *get_operating_system (void);
76 static gboolean is_crash_dialog_allowed (void);
77 static void crash_handler (int sig);
78 static void crash_cleanup_exit (void);
80 /***/
82 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
84 /***/
86 /*!
87 *\brief install crash handlers
89 void crash_install_handlers(void)
91 #if CRASH_DIALOG
92 sigset_t mask;
94 if (!is_crash_dialog_allowed()) return;
96 sigemptyset(&mask);
98 #ifdef SIGSEGV
99 signal(SIGSEGV, crash_handler);
100 sigaddset(&mask, SIGSEGV);
101 #endif
103 #ifdef SIGFPE
104 signal(SIGFPE, crash_handler);
105 sigaddset(&mask, SIGFPE);
106 #endif
108 #ifdef SIGILL
109 signal(SIGILL, crash_handler);
110 sigaddset(&mask, SIGILL);
111 #endif
113 #ifdef SIGABRT
114 signal(SIGABRT, crash_handler);
115 sigaddset(&mask, SIGABRT);
116 #endif
118 #ifdef SIGTERM
119 signal(SIGTERM, crash_handler);
120 sigaddset(&mask, SIGTERM);
121 #endif
123 sigprocmask(SIG_UNBLOCK, &mask, 0);
124 #endif /* CRASH_DIALOG */
127 /***/
130 *\brief crash dialog entry point
132 void crash_main(const char *arg)
134 #if CRASH_DIALOG
135 gchar *text;
136 gchar **tokens;
137 unsigned long pid;
138 GString *output;
140 crash_create_debugger_file();
141 tokens = g_strsplit(arg, ",", 0);
143 pid = atol(tokens[0]);
144 text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
145 pid, atol(tokens[1]));
147 output = g_string_new("");
148 crash_debug(pid, tokens[2], output);
151 * try to get the settings
153 prefs_common_init();
154 prefs_common_read_config();
156 crash_dialog_show(text, output->str);
157 g_string_free(output, TRUE);
158 g_free(text);
159 g_strfreev(tokens);
160 #endif /* CRASH_DIALOG */
164 *\brief (can't get pixmap working, so discarding it)
166 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
168 GtkWidget *window1;
169 GtkWidget *vbox1;
170 GtkWidget *hbox1;
171 GtkWidget *label1;
172 GtkWidget *frame1;
173 GtkWidget *scrolledwindow1;
174 GtkWidget *text1;
175 GtkWidget *hbuttonbox3;
176 GtkWidget *hbuttonbox4;
177 GtkWidget *button3;
178 GtkWidget *button4;
179 GtkWidget *button5;
180 gchar *crash_report;
182 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
183 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
184 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
185 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
186 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
187 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
190 vbox1 = gtk_vbox_new(FALSE, 2);
191 gtk_widget_show(vbox1);
192 gtk_container_add(GTK_CONTAINER(window1), vbox1);
194 hbox1 = gtk_hbox_new(FALSE, 4);
195 gtk_widget_show(hbox1);
196 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
197 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
199 label1 = gtk_label_new
200 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
201 gtk_widget_show(label1);
202 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
203 gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
205 frame1 = gtk_frame_new(_("Debug log"));
206 gtk_widget_show(frame1);
207 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
209 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
210 gtk_widget_show(scrolledwindow1);
211 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
212 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
213 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
214 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
216 text1 = gtk_text_new(NULL, NULL);
217 gtk_text_set_editable(GTK_TEXT(text1), FALSE);
218 gtk_widget_show(text1);
219 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
221 crash_report = g_strdup_printf(
222 "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
223 VERSION,
224 gtk_major_version, gtk_minor_version, gtk_micro_version,
225 get_compiled_in_features(),
226 get_operating_system(),
227 get_lib_version(),
228 debug_output);
230 gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
232 hbuttonbox3 = gtk_hbutton_box_new();
233 gtk_widget_show(hbuttonbox3);
234 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
236 hbuttonbox4 = gtk_hbutton_box_new();
237 gtk_widget_show(hbuttonbox4);
238 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
240 button3 = gtk_button_new_with_label(_("Close"));
241 gtk_widget_show(button3);
242 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
243 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
245 button4 = gtk_button_new_with_label(_("Save..."));
246 gtk_widget_show(button4);
247 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
248 GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
250 button5 = gtk_button_new_with_label(_("Create bug report"));
251 gtk_widget_show(button5);
252 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
253 GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
255 gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
256 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
257 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
258 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
259 gtk_signal_connect(GTK_OBJECT(button4), "clicked",
260 GTK_SIGNAL_FUNC(crash_save_crash_log),
261 crash_report);
262 gtk_signal_connect(GTK_OBJECT(button5), "clicked",
263 GTK_SIGNAL_FUNC(crash_create_bug_report),
264 NULL);
266 gtk_widget_show(window1);
268 gtk_main();
269 return window1;
274 *\brief create debugger script file in sylpheed directory.
275 * all the other options (creating temp files) looked too
276 * convoluted.
278 static void crash_create_debugger_file(void)
280 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
282 str_write_to_file(DEBUG_SCRIPT, filespec);
283 g_free(filespec);
287 *\brief saves crash log to a file
289 static void crash_save_crash_log(GtkButton *button, const gchar *text)
291 time_t timer;
292 struct tm *lt;
293 char buf[100];
294 gchar *filename;
296 timer = time(NULL);
297 lt = localtime(&timer);
298 strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
299 if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
300 && *filename)
301 str_write_to_file(text, filename);
302 g_free(filename);
306 *\brief create bug report (goes to Sylpheed Claws bug tracker)
308 static void crash_create_bug_report(GtkButton *button, const gchar *data)
310 open_uri("http://sourceforge.net/tracker/?func=add&group_id=25528&atid=384598",
311 prefs_common.uri_cmd);
315 *\brief launches debugger and attaches it to crashed sylpheed
317 static void crash_debug(unsigned long crash_pid,
318 gchar *exe_image,
319 GString *debug_output)
321 int choutput[2];
322 pid_t pid;
324 pipe(choutput);
326 if (0 == (pid = fork())) {
327 char *argp[10];
328 char **argptr = argp;
329 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
331 setgid(getgid());
332 setuid(getuid());
335 * setup debugger to attach to crashed sylpheed
337 *argptr++ = "gdb";
338 *argptr++ = "--nw";
339 *argptr++ = "--nx";
340 *argptr++ = "--quiet";
341 *argptr++ = "--batch";
342 *argptr++ = "-x";
343 *argptr++ = filespec;
344 *argptr++ = exe_image;
345 *argptr++ = g_strdup_printf("%ld", crash_pid);
346 *argptr = NULL;
349 * redirect output to write end of pipe
351 close(1);
352 dup(choutput[1]);
353 close(choutput[0]);
354 if (-1 == execvp("gdb", argp))
355 puts("error execvp\n");
356 } else {
357 char buf[100];
358 int r;
360 waitpid(pid, NULL, 0);
363 * make it non blocking
365 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
366 puts("set to non blocking failed\n");
369 * get the output
371 do {
372 r = read(choutput[0], buf, sizeof buf - 1);
373 if (r > 0) {
374 buf[r] = 0;
375 g_string_append(debug_output, buf);
377 } while (r > 0);
379 close(choutput[0]);
380 close(choutput[1]);
383 * kill the process we attached to
385 kill(crash_pid, SIGCONT);
389 /***/
392 *\brief features
394 static const gchar *get_compiled_in_features(void)
396 return g_strdup_printf("%s",
397 #if HAVE_GDK_IMLIB
398 " gdk_imlib"
399 #endif
400 #if HAVE_GDK_PIXBUF
401 " gdk-pixbuf"
402 #endif
403 #if USE_THREADS
404 " gthread"
405 #endif
406 #if INET6
407 " IPv6"
408 #endif
409 #if HAVE_LIBCOMPFACE
410 " libcompface"
411 #endif
412 #if HAVE_LIBJCONV
413 " libjconv"
414 #endif
415 #if USE_GPGME
416 " GPGME"
417 #endif
418 #if USE_OPENSSL
419 " OpenSSL"
420 #endif
421 #if USE_LDAP
422 " LDAP"
423 #endif
424 #if USE_JPILOT
425 " JPilot"
426 #endif
427 #if USE_ASPELL
428 " GNU/aspell"
429 #endif
430 "");
433 /***/
436 *\brief library version
438 static const gchar *get_lib_version(void)
440 #if defined(__GNU_LIBRARY__)
441 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
442 #else
443 return g_strdup(_("Unknown"));
444 #endif
447 /***/
450 *\brief operating system
452 static const gchar *get_operating_system(void)
454 #if HAVE_SYS_UTSNAME_H
455 struct utsname utsbuf;
456 uname(&utsbuf);
457 return g_strdup_printf("%s %s (%s)",
458 utsbuf.sysname,
459 utsbuf.release,
460 utsbuf.machine);
461 #else
462 return g_strdup(_("Unknown"));
464 #endif
467 /***/
470 *\brief see if the crash dialog is allowed (because some
471 * developers may prefer to run sylpheed under gdb...)
473 static gboolean is_crash_dialog_allowed(void)
475 return !getenv("SYLPHEED_NO_CRASH");
479 *\brief this handler will probably evolve into
480 * something better.
482 static void crash_handler(int sig)
484 pid_t pid;
485 static volatile unsigned long crashed_ = 0;
488 * let's hope startup_dir and argv0 aren't trashed.
489 * both are defined in main.c.
491 extern gchar *startup_dir;
492 extern gchar *argv0;
496 * besides guarding entrancy it's probably also better
497 * to mask off signals
499 if (crashed_) return;
501 crashed_++;
503 #ifdef SIGTERM
504 if (sig == SIGTERM)
505 clean_quit();
506 #endif
509 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
511 gdk_pointer_ungrab(GDK_CURRENT_TIME);
512 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
513 gdk_flush();
515 if (0 == (pid = fork())) {
516 char buf[50];
517 char *args[5];
520 * probably also some other parameters (like GTK+ ones).
521 * also we pass the full startup dir and the real command
522 * line typed in (argv0)
524 args[0] = argv0;
525 args[1] = "--debug";
526 args[2] = "--crash";
527 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
528 args[3] = buf;
529 args[4] = NULL;
531 chdir(startup_dir);
532 setgid(getgid());
533 setuid(getuid());
534 execvp(argv0, args);
535 } else {
536 waitpid(pid, NULL, 0);
537 crash_cleanup_exit();
538 _exit(253);
541 _exit(253);
545 *\brief put all the things here we can do before
546 * letting the program die
548 static void crash_cleanup_exit(void)
550 extern gchar *get_socket_name(void);
551 const char *filename = get_socket_name();
552 unlink(filename);
555 #endif