* src/crash.c
[claws.git] / src / crash.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 by the Sylpheed Claws Team and Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #       include <config.h>
22 #endif
23
24 #ifdef CRASH_DIALOG
25
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>
34
35 #include <errno.h>
36 #include <fcntl.h>
37
38 #if HAVE_SYS_UTSNAME_H
39 #       include <sys/utsname.h>
40 #endif
41
42 #if defined(__GNU_LIBRARY__)
43 #       include <gnu/libc-version.h>
44 #endif
45
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"
55
56 /*
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.
61  */
62
63 /***/
64
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);
79
80 /***/
81
82 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
83
84 /***/
85
86 /*!
87  *\brief        install crash handlers
88  */
89 void crash_install_handlers(void)
90 {
91 #if CRASH_DIALOG 
92         sigset_t mask;
93
94         if (!is_crash_dialog_allowed()) return;
95
96         sigemptyset(&mask);
97
98 #ifdef SIGSEGV
99         signal(SIGSEGV, crash_handler);
100         sigaddset(&mask, SIGSEGV);
101 #endif
102         
103 #ifdef SIGFPE
104         signal(SIGFPE, crash_handler);
105         sigaddset(&mask, SIGFPE);
106 #endif
107
108 #ifdef SIGILL
109         signal(SIGILL, crash_handler);
110         sigaddset(&mask, SIGILL);
111 #endif
112
113 #ifdef SIGABRT
114         signal(SIGABRT, crash_handler);
115         sigaddset(&mask, SIGABRT);
116 #endif
117
118 #ifdef SIGTERM
119         signal(SIGTERM, crash_handler);
120         sigaddset(&mask, SIGTERM);
121 #endif
122
123         sigprocmask(SIG_UNBLOCK, &mask, 0);
124 #endif /* CRASH_DIALOG */       
125 }
126
127 /***/
128
129 /*!
130  *\brief        crash dialog entry point 
131  */
132 void crash_main(const char *arg) 
133 {
134 #if CRASH_DIALOG 
135         gchar *text;
136         gchar **tokens;
137         unsigned long pid;
138         GString *output;
139
140         crash_create_debugger_file();
141         tokens = g_strsplit(arg, ",", 0);
142
143         pid = atol(tokens[0]);
144         text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
145                                pid, atol(tokens[1]));
146
147         output = g_string_new("");     
148         crash_debug(pid, tokens[2], output);
149
150         /*
151          * try to get the settings
152          */
153         prefs_common_init();
154         prefs_common_read_config();
155
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 */       
161 }
162
163 /*!
164  *\brief        show crash dialog
165  *
166  *\param        text Description
167  *\param        debug_output Output text by gdb
168  *
169  *\return       GtkWidget * Dialog widget
170  */
171 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
172 {
173         GtkWidget *window1;
174         GtkWidget *vbox1;
175         GtkWidget *hbox1;
176         GtkWidget *label1;
177         GtkWidget *frame1;
178         GtkWidget *scrolledwindow1;
179         GtkWidget *text1;
180         GtkWidget *hbuttonbox3;
181         GtkWidget *hbuttonbox4;
182         GtkWidget *button3;
183         GtkWidget *button4;
184         GtkWidget *button5;
185         gchar     *crash_report;
186
187         window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
188         gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
189         gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
190         gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
191         gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
192         gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
193
194
195         vbox1 = gtk_vbox_new(FALSE, 2);
196         gtk_widget_show(vbox1);
197         gtk_container_add(GTK_CONTAINER(window1), vbox1);
198
199         hbox1 = gtk_hbox_new(FALSE, 4);
200         gtk_widget_show(hbox1);
201         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
202         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
203
204         label1 = gtk_label_new
205             (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
206         gtk_widget_show(label1);
207         gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
208         gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
209
210         frame1 = gtk_frame_new(_("Debug log"));
211         gtk_widget_show(frame1);
212         gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
213
214         scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
215         gtk_widget_show(scrolledwindow1);
216         gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
217         gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
218         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
219                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
220
221         text1 = gtk_text_new(NULL, NULL);
222         gtk_text_set_editable(GTK_TEXT(text1), FALSE);
223         gtk_widget_show(text1);
224         gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
225         
226         crash_report = g_strdup_printf(
227                 "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
228                 VERSION,
229                 gtk_major_version, gtk_minor_version, gtk_micro_version,
230                 get_compiled_in_features(),
231                 get_operating_system(),
232                 get_lib_version(),
233                 debug_output);
234
235         gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
236
237         hbuttonbox3 = gtk_hbutton_box_new();
238         gtk_widget_show(hbuttonbox3);
239         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
240
241         hbuttonbox4 = gtk_hbutton_box_new();
242         gtk_widget_show(hbuttonbox4);
243         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
244
245         button3 = gtk_button_new_with_label(_("Close"));
246         gtk_widget_show(button3);
247         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
248         GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
249
250         button4 = gtk_button_new_with_label(_("Save..."));
251         gtk_widget_show(button4);
252         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
253         GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
254
255         button5 = gtk_button_new_with_label(_("Create bug report"));
256         gtk_widget_show(button5);
257         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
258         GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
259         
260         gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
261                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
262         gtk_signal_connect(GTK_OBJECT(button3),   "clicked",
263                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
264         gtk_signal_connect(GTK_OBJECT(button4), "clicked",
265                            GTK_SIGNAL_FUNC(crash_save_crash_log),
266                            crash_report);
267         gtk_signal_connect(GTK_OBJECT(button5), "clicked",
268                            GTK_SIGNAL_FUNC(crash_create_bug_report),
269                            NULL);
270
271         gtk_widget_show(window1);
272
273         gtk_main();
274         return window1;
275 }
276
277
278 /*!
279  *\brief        create debugger script file in sylpheed directory.
280  *              all the other options (creating temp files) looked too 
281  *              convoluted.
282  */
283 static void crash_create_debugger_file(void)
284 {
285         gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
286         
287         str_write_to_file(DEBUG_SCRIPT, filespec);
288         g_free(filespec);
289 }
290
291 /*!
292  *\brief        saves crash log to a file
293  */
294 static void crash_save_crash_log(GtkButton *button, const gchar *text)
295 {
296         time_t timer;
297         struct tm *lt;
298         char buf[100];
299         gchar *filename;
300
301         timer = time(NULL);
302         lt = localtime(&timer);
303         strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
304         if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
305         &&  *filename)
306                 str_write_to_file(text, filename);
307         g_free(filename);       
308 }
309
310 /*!
311  *\brief        create bug report (goes to Sylpheed Claws bug tracker)  
312  */
313 static void crash_create_bug_report(GtkButton *button, const gchar *data)
314 {
315         open_uri("http://sylpheed-claws.sourceforge.net/cgi-bin/bugzilla/enter_bug.cgi",
316                  prefs_common.uri_cmd);
317 }
318
319 /*!
320  *\brief        launches debugger and attaches it to crashed sylpheed
321  */
322 static void crash_debug(unsigned long crash_pid, 
323                         gchar *exe_image,
324                         GString *debug_output)
325 {
326         int choutput[2];
327         pid_t pid;
328
329         pipe(choutput);
330
331         if (0 == (pid = fork())) {
332                 char *argp[10];
333                 char **argptr = argp;
334                 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
335
336                 setgid(getgid());
337                 setuid(getuid());
338
339                 /*
340                  * setup debugger to attach to crashed sylpheed
341                  */
342                 *argptr++ = "gdb"; 
343                 *argptr++ = "--nw";
344                 *argptr++ = "--nx";
345                 *argptr++ = "--quiet";
346                 *argptr++ = "--batch";
347                 *argptr++ = "-x";
348                 *argptr++ = filespec;
349                 *argptr++ = exe_image;
350                 *argptr++ = g_strdup_printf("%ld", crash_pid);
351                 *argptr   = NULL;
352
353                 /*
354                  * redirect output to write end of pipe
355                  */
356                 close(1);
357                 dup(choutput[1]);
358                 close(choutput[0]);
359                 if (-1 == execvp("gdb", argp)) 
360                         puts("error execvp\n");
361         } else {
362                 char buf[100];
363                 int r;
364         
365                 waitpid(pid, NULL, 0);
366
367                 /*
368                  * make it non blocking
369                  */
370                 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
371                         puts("set to non blocking failed\n");
372
373                 /*
374                  * get the output
375                  */
376                 do {
377                         r = read(choutput[0], buf, sizeof buf - 1);
378                         if (r > 0) {
379                                 buf[r] = 0;
380                                 g_string_append(debug_output, buf);
381                         }
382                 } while (r > 0);
383                 
384                 close(choutput[0]);
385                 close(choutput[1]);
386                 
387                 /*
388                  * kill the process we attached to
389                  */
390                 kill(crash_pid, SIGCONT); 
391         }
392 }
393
394 /***/
395
396 /*!
397  *\brief        features
398  */
399 static const gchar *get_compiled_in_features(void)
400 {
401         return g_strdup_printf("%s",
402 #if HAVE_GDK_IMLIB
403                    " gdk_imlib"
404 #endif
405 #if HAVE_GDK_PIXBUF
406                    " gdk-pixbuf"
407 #endif
408 #if USE_THREADS
409                    " gthread"
410 #endif
411 #if INET6
412                    " IPv6"
413 #endif
414 #if HAVE_LIBCOMPFACE
415                    " libcompface"
416 #endif
417 #if HAVE_LIBJCONV
418                    " libjconv"
419 #endif
420 #if USE_GPGME
421                    " GPGME"
422 #endif
423 #if USE_OPENSSL
424                    " OpenSSL"
425 #endif
426 #if USE_LDAP
427                    " LDAP"
428 #endif
429 #if USE_JPILOT
430                    " JPilot"
431 #endif
432 #if USE_ASPELL
433                    " GNU/aspell"
434 #endif
435         "");
436 }
437
438 /***/
439
440 /*!
441  *\brief        library version
442  */
443 static const gchar *get_lib_version(void)
444 {
445 #if defined(__GNU_LIBRARY__)
446         return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
447 #else
448         return g_strdup(_("Unknown"));
449 #endif
450 }
451
452 /***/
453
454 /*!
455  *\brief        operating system
456  */
457 static const gchar *get_operating_system(void)
458 {
459 #if HAVE_SYS_UTSNAME_H
460         struct utsname utsbuf;
461         uname(&utsbuf);
462         return g_strdup_printf("%s %s (%s)",
463                                utsbuf.sysname,
464                                utsbuf.release,
465                                utsbuf.machine);
466 #else
467         return g_strdup(_("Unknown"));
468         
469 #endif
470 }
471
472 /***/
473
474 /*!
475  *\brief        see if the crash dialog is allowed (because some
476  *              developers may prefer to run sylpheed under gdb...)
477  */
478 static gboolean is_crash_dialog_allowed(void)
479 {
480         return !getenv("SYLPHEED_NO_CRASH");
481 }
482
483 /*!
484  *\brief        this handler will probably evolve into 
485  *              something better.
486  */
487 static void crash_handler(int sig)
488 {
489         pid_t pid;
490         static volatile unsigned long crashed_ = 0;
491
492         /*
493          * let's hope startup_dir and argv0 aren't trashed.
494          * both are defined in main.c.
495          */
496         extern gchar *startup_dir;
497         extern gchar *argv0;
498
499
500         /*
501          * besides guarding entrancy it's probably also better 
502          * to mask off signals
503          */
504         if (crashed_) return;
505
506         crashed_++;
507
508 #ifdef SIGTERM
509         if (sig == SIGTERM) 
510                 clean_quit();
511 #endif
512
513         /*
514          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
515          */
516         gdk_pointer_ungrab(GDK_CURRENT_TIME);
517         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
518         gdk_flush();
519
520         if (0 == (pid = fork())) {
521                 char buf[50];
522                 char *args[5];
523         
524                 /*
525                  * probably also some other parameters (like GTK+ ones).
526                  * also we pass the full startup dir and the real command
527                  * line typed in (argv0)
528                  */
529                 args[0] = argv0; 
530                 args[1] = "--debug";
531                 args[2] = "--crash";
532                 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
533                 args[3] = buf;
534                 args[4] = NULL;
535
536                 chdir(startup_dir);
537                 setgid(getgid());
538                 setuid(getuid());
539                 execvp(argv0, args);
540         } else {
541                 waitpid(pid, NULL, 0);
542                 crash_cleanup_exit();
543                 _exit(253);
544         }
545
546         _exit(253);
547 }
548
549 /*!
550  *\brief        put all the things here we can do before
551  *              letting the program die
552  */
553 static void crash_cleanup_exit(void)
554 {
555         extern gchar *get_socket_name(void);
556         const char *filename = get_socket_name();
557         unlink(filename);
558 }
559
560 #endif