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>
56 #include "prefs_common.h"
59 * NOTE: the crash dialog is called when sylpheed is not
60 * initialized, so do not assume settings are available.
61 * for example, loading / creating pixmaps seems not
67 static GtkWidget *crash_dialog_show (const gchar *text,
68 const gchar *debug_output);
69 static void crash_create_debugger_file (void);
70 static void crash_save_crash_log (GtkButton *, const gchar *);
71 static void crash_create_bug_report (GtkButton *, const gchar *);
72 static void crash_debug (unsigned long crash_pid,
74 GString *debug_output);
75 static const gchar *get_compiled_in_features (void);
76 static const gchar *get_lib_version (void);
77 static const gchar *get_operating_system (void);
78 static gboolean is_crash_dialog_allowed (void);
79 static void crash_handler (int sig);
80 static void crash_cleanup_exit (void);
84 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
89 *\brief install crash handlers
91 void crash_install_handlers(void)
96 if (!is_crash_dialog_allowed()) return;
101 signal(SIGSEGV, crash_handler);
102 sigaddset(&mask, SIGSEGV);
106 signal(SIGFPE, crash_handler);
107 sigaddset(&mask, SIGFPE);
111 signal(SIGILL, crash_handler);
112 sigaddset(&mask, SIGILL);
116 signal(SIGABRT, crash_handler);
117 sigaddset(&mask, SIGABRT);
120 sigprocmask(SIG_UNBLOCK, &mask, 0);
121 #endif /* CRASH_DIALOG */
127 *\brief crash dialog entry point
129 void crash_main(const char *arg)
137 crash_create_debugger_file();
138 tokens = g_strsplit(arg, ",", 0);
140 pid = atol(tokens[0]);
141 text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
142 pid, atol(tokens[1]));
144 output = g_string_new("");
145 crash_debug(pid, tokens[2], output);
148 * try to get the settings
151 prefs_common_read_config();
153 crash_dialog_show(text, output->str);
154 g_string_free(output, TRUE);
157 #endif /* CRASH_DIALOG */
161 *\brief show crash dialog
163 *\param text Description
164 *\param debug_output Output text by gdb
166 *\return GtkWidget * Dialog widget
168 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
175 GtkWidget *scrolledwindow1;
177 GtkWidget *hbuttonbox3;
178 GtkWidget *hbuttonbox4;
184 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
185 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
186 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
187 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
188 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
189 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
192 vbox1 = gtk_vbox_new(FALSE, 2);
193 gtk_widget_show(vbox1);
194 gtk_container_add(GTK_CONTAINER(window1), vbox1);
196 hbox1 = gtk_hbox_new(FALSE, 4);
197 gtk_widget_show(hbox1);
198 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
199 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
201 label1 = gtk_label_new
202 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
203 gtk_widget_show(label1);
204 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
205 gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
207 frame1 = gtk_frame_new(_("Debug log"));
208 gtk_widget_show(frame1);
209 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
211 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
212 gtk_widget_show(scrolledwindow1);
213 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
214 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
215 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
216 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
218 text1 = gtk_text_new(NULL, NULL);
219 gtk_text_set_editable((text1), FALSE);
220 gtk_widget_show(text1);
221 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
223 crash_report = g_strdup_printf(
224 "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
226 gtk_major_version, gtk_minor_version, gtk_micro_version,
227 get_compiled_in_features(),
228 get_operating_system(),
232 gtk_text_insert((text1), NULL, NULL, NULL, crash_report, -1);
234 hbuttonbox3 = gtk_hbutton_box_new();
235 gtk_widget_show(hbuttonbox3);
236 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
238 hbuttonbox4 = gtk_hbutton_box_new();
239 gtk_widget_show(hbuttonbox4);
240 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
242 button3 = gtk_button_new_with_label(_("Close"));
243 gtk_widget_show(button3);
244 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
245 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
247 button4 = gtk_button_new_with_label(_("Save..."));
248 gtk_widget_show(button4);
249 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
250 GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
252 button5 = gtk_button_new_with_label(_("Create bug report"));
253 gtk_widget_show(button5);
254 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
255 GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
257 g_signal_connect(G_OBJECT(window1), "delete_event",
258 G_CALLBACK(gtk_main_quit), NULL);
259 g_signal_connect(G_OBJECT(button3), "clicked",
260 G_CALLBACK(gtk_main_quit), NULL);
261 g_signal_connect(G_OBJECT(button4), "clicked",
262 G_CALLBACK(crash_save_crash_log), crash_report);
263 g_signal_connect(G_OBJECT(button5), "clicked",
264 G_CALLBACK(crash_create_bug_report), NULL);
266 gtk_widget_show(window1);
274 *\brief create debugger script file in sylpheed directory.
275 * all the other options (creating temp files) looked too
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);
287 *\brief saves crash log to a file
289 static void crash_save_crash_log(GtkButton *button, const gchar *text)
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(_("Save crash information"), buf))
301 str_write_to_file(text, 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(CLAWS_BUGZILLA_URI, prefs_common.uri_cmd);
314 *\brief launches debugger and attaches it to crashed sylpheed
316 static void crash_debug(unsigned long crash_pid,
318 GString *debug_output)
325 if (0 == (pid = fork())) {
327 char **argptr = argp;
328 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
334 * setup debugger to attach to crashed sylpheed
339 *argptr++ = "--quiet";
340 *argptr++ = "--batch";
342 *argptr++ = filespec;
343 *argptr++ = exe_image;
344 *argptr++ = g_strdup_printf("%ld", crash_pid);
348 * redirect output to write end of pipe
353 if (-1 == execvp("gdb", argp))
354 puts("error execvp\n");
359 waitpid(pid, NULL, 0);
362 * make it non blocking
364 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
365 puts("set to non blocking failed\n");
371 r = read(choutput[0], buf, sizeof buf - 1);
374 g_string_append(debug_output, buf);
382 * kill the process we attached to
384 kill(crash_pid, SIGCONT);
393 static const gchar *get_compiled_in_features(void)
395 return g_strdup_printf("%s",
432 *\brief library version
434 static const gchar *get_lib_version(void)
436 #if defined(__GNU_LIBRARY__)
437 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
439 return g_strdup(_("Unknown"));
446 *\brief operating system
448 static const gchar *get_operating_system(void)
450 #if HAVE_SYS_UTSNAME_H
451 struct utsname utsbuf;
453 return g_strdup_printf("%s %s (%s)",
458 return g_strdup(_("Unknown"));
466 *\brief see if the crash dialog is allowed (because some
467 * developers may prefer to run sylpheed under gdb...)
469 static gboolean is_crash_dialog_allowed(void)
471 return !getenv("SYLPHEED_NO_CRASH");
475 *\brief this handler will probably evolve into
478 static void crash_handler(int sig)
481 static volatile unsigned long crashed_ = 0;
484 * let's hope argv0 aren't trashed.
485 * both are defined in main.c.
491 * besides guarding entrancy it's probably also better
492 * to mask off signals
494 if (crashed_) return;
504 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
506 gdk_pointer_ungrab(GDK_CURRENT_TIME);
507 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
510 if (0 == (pid = fork())) {
515 * probably also some other parameters (like GTK+ ones).
516 * also we pass the full startup dir and the real command
517 * line typed in (argv0)
522 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
526 chdir(sylpheed_get_startup_dir());
531 waitpid(pid, NULL, 0);
532 crash_cleanup_exit();
540 *\brief put all the things here we can do before
541 * letting the program die
543 static void crash_cleanup_exit(void)
545 extern gchar *get_socket_name(void);
546 const char *filename = get_socket_name();