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.
34 #include <sys/types.h>
40 #if HAVE_SYS_UTSNAME_H
41 # include <sys/utsname.h>
44 #if defined(__GNU_LIBRARY__)
45 # include <gnu/libc-version.h>
57 #include "prefs_common.h"
60 * NOTE: the crash dialog is called when sylpheed is not
61 * initialized, so do not assume settings are available.
62 * for example, loading / creating pixmaps seems not
68 static GtkWidget *crash_dialog_show (const gchar *text,
69 const gchar *debug_output);
70 static void crash_create_debugger_file (void);
71 static void crash_save_crash_log (GtkButton *, const gchar *);
72 static void crash_create_bug_report (GtkButton *, const gchar *);
73 static void crash_debug (unsigned long crash_pid,
75 GString *debug_output);
76 static const gchar *get_compiled_in_features (void);
77 static const gchar *get_lib_version (void);
78 static const gchar *get_operating_system (void);
79 static gboolean is_crash_dialog_allowed (void);
80 static void crash_handler (int sig);
81 static void crash_cleanup_exit (void);
85 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
90 *\brief install crash handlers
92 void crash_install_handlers(void)
97 if (!is_crash_dialog_allowed()) return;
102 signal(SIGSEGV, crash_handler);
103 sigaddset(&mask, SIGSEGV);
107 signal(SIGFPE, crash_handler);
108 sigaddset(&mask, SIGFPE);
112 signal(SIGILL, crash_handler);
113 sigaddset(&mask, SIGILL);
117 signal(SIGABRT, crash_handler);
118 sigaddset(&mask, SIGABRT);
121 sigprocmask(SIG_UNBLOCK, &mask, 0);
122 #endif /* CRASH_DIALOG */
128 *\brief crash dialog entry point
130 void crash_main(const char *arg)
138 crash_create_debugger_file();
139 tokens = g_strsplit(arg, ",", 0);
141 pid = atol(tokens[0]);
142 text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
143 pid, atol(tokens[1]));
145 output = g_string_new("");
146 crash_debug(pid, tokens[2], output);
149 * try to get the settings
152 prefs_common_read_config();
154 crash_dialog_show(text, output->str);
155 g_string_free(output, TRUE);
158 #endif /* CRASH_DIALOG */
162 *\brief show crash dialog
164 *\param text Description
165 *\param debug_output Output text by gdb
167 *\return GtkWidget * Dialog widget
169 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
176 GtkWidget *scrolledwindow1;
178 GtkWidget *hbuttonbox3;
179 GtkWidget *hbuttonbox4;
185 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
186 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
187 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
188 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
189 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
190 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
193 vbox1 = gtk_vbox_new(FALSE, 2);
194 gtk_widget_show(vbox1);
195 gtk_container_add(GTK_CONTAINER(window1), vbox1);
197 hbox1 = gtk_hbox_new(FALSE, 4);
198 gtk_widget_show(hbox1);
199 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
200 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
202 label1 = gtk_label_new
203 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
204 gtk_widget_show(label1);
205 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
206 gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
208 frame1 = gtk_frame_new(_("Debug log"));
209 gtk_widget_show(frame1);
210 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
212 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
213 gtk_widget_show(scrolledwindow1);
214 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
215 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
216 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
217 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
219 text1 = gtk_text_new(NULL, NULL);
220 gtk_text_set_editable(GTK_TEXT(text1), FALSE);
221 gtk_widget_show(text1);
222 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
224 crash_report = g_strdup_printf(
225 "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
227 gtk_major_version, gtk_minor_version, gtk_micro_version,
228 get_compiled_in_features(),
229 get_operating_system(),
233 gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
235 hbuttonbox3 = gtk_hbutton_box_new();
236 gtk_widget_show(hbuttonbox3);
237 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
239 hbuttonbox4 = gtk_hbutton_box_new();
240 gtk_widget_show(hbuttonbox4);
241 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
243 button3 = gtk_button_new_with_label(_("Close"));
244 gtk_widget_show(button3);
245 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
246 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
248 button4 = gtk_button_new_with_label(_("Save..."));
249 gtk_widget_show(button4);
250 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
251 GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
253 button5 = gtk_button_new_with_label(_("Create bug report"));
254 gtk_widget_show(button5);
255 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
256 GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
258 gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
259 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
260 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
261 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
262 gtk_signal_connect(GTK_OBJECT(button4), "clicked",
263 GTK_SIGNAL_FUNC(crash_save_crash_log),
265 gtk_signal_connect(GTK_OBJECT(button5), "clicked",
266 GTK_SIGNAL_FUNC(crash_create_bug_report),
269 gtk_widget_show(window1);
277 *\brief create debugger script file in sylpheed directory.
278 * all the other options (creating temp files) looked too
281 static void crash_create_debugger_file(void)
283 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
285 str_write_to_file(DEBUG_SCRIPT, filespec);
290 *\brief saves crash log to a file
292 static void crash_save_crash_log(GtkButton *button, const gchar *text)
300 lt = localtime(&timer);
301 strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
302 if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
304 str_write_to_file(text, filename);
308 *\brief create bug report (goes to Sylpheed Claws bug tracker)
310 static void crash_create_bug_report(GtkButton *button, const gchar *data)
312 open_uri(CLAWS_BUGZILLA_URI, prefs_common.uri_cmd);
316 *\brief launches debugger and attaches it to crashed sylpheed
318 static void crash_debug(unsigned long crash_pid,
320 GString *debug_output)
327 if (0 == (pid = fork())) {
329 char **argptr = argp;
330 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
336 * setup debugger to attach to crashed sylpheed
341 *argptr++ = "--quiet";
342 *argptr++ = "--batch";
344 *argptr++ = filespec;
345 *argptr++ = exe_image;
346 *argptr++ = g_strdup_printf("%ld", crash_pid);
350 * redirect output to write end of pipe
355 if (-1 == execvp("gdb", argp))
356 puts("error execvp\n");
361 waitpid(pid, NULL, 0);
364 * make it non blocking
366 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
367 puts("set to non blocking failed\n");
373 r = read(choutput[0], buf, sizeof buf - 1);
376 g_string_append(debug_output, buf);
384 * kill the process we attached to
386 kill(crash_pid, SIGCONT);
395 static const gchar *get_compiled_in_features(void)
397 return g_strdup_printf("%s",
434 *\brief library version
436 static const gchar *get_lib_version(void)
438 #if defined(__GNU_LIBRARY__)
439 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
441 return g_strdup(_("Unknown"));
448 *\brief operating system
450 static const gchar *get_operating_system(void)
452 #if HAVE_SYS_UTSNAME_H
453 struct utsname utsbuf;
455 return g_strdup_printf("%s %s (%s)",
460 return g_strdup(_("Unknown"));
468 *\brief see if the crash dialog is allowed (because some
469 * developers may prefer to run sylpheed under gdb...)
471 static gboolean is_crash_dialog_allowed(void)
473 return !getenv("SYLPHEED_NO_CRASH");
477 *\brief this handler will probably evolve into
480 static void crash_handler(int sig)
483 static volatile unsigned long crashed_ = 0;
486 * let's hope argv0 aren't trashed.
487 * both are defined in main.c.
493 * besides guarding entrancy it's probably also better
494 * to mask off signals
496 if (crashed_) return;
506 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
508 gdk_pointer_ungrab(GDK_CURRENT_TIME);
509 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
512 if (0 == (pid = fork())) {
517 * probably also some other parameters (like GTK+ ones).
518 * also we pass the full startup dir and the real command
519 * line typed in (argv0)
524 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
528 chdir(sylpheed_get_startup_dir());
533 waitpid(pid, NULL, 0);
534 crash_cleanup_exit();
542 *\brief put all the things here we can do before
543 * letting the program die
545 static void crash_cleanup_exit(void)
547 extern gchar *get_socket_name(void);
548 const char *filename = get_socket_name();