2010-02-03 [colin] 3.7.5cvs4
[claws.git] / src / crash.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 by the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #       include <config.h>
22 #endif
23
24 #ifdef CRASH_DIALOG
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37
38 #include <errno.h>
39 #include <fcntl.h>
40
41 #if HAVE_SYS_UTSNAME_H
42 #       include <sys/utsname.h>
43 #endif
44
45 #if defined(__GNU_LIBRARY__)
46 #       include <gnu/libc-version.h>
47 #endif
48
49 #include "main.h"
50 #include "claws.h"
51 #include "crash.h"
52 #include "utils.h"
53 #include "filesel.h"
54 #include "version.h"
55 #include "prefs_common.h"
56
57 /*
58  * NOTE: the crash dialog is called when claws is not 
59  * initialized, so do not assume settings are available.
60  * for example, loading / creating pixmaps seems not 
61  * to be possible.
62  */
63
64 /***/
65
66 static GtkWidget        *crash_dialog_show              (const gchar *text, 
67                                                          const gchar *debug_output);
68 static void              crash_create_debugger_file     (void);
69 static void              crash_save_crash_log           (GtkButton *, const gchar *);
70 static void              crash_create_bug_report        (GtkButton *, const gchar *);
71 static void              crash_debug                    (unsigned long crash_pid, 
72                                                          gchar   *exe_image,
73                                                          GString *debug_output);
74 static const gchar      *get_compiled_in_features       (void);
75 static const gchar      *get_lib_version                (void);
76 static const gchar      *get_operating_system           (void);
77 static gboolean          is_crash_dialog_allowed        (void);
78 static void              crash_handler                  (int sig);
79 static void              crash_cleanup_exit             (void);
80
81 /***/
82
83 static const gchar *DEBUG_SCRIPT = "thread all apply\nbt full\nkill\nq";
84
85 /***/
86
87 /*!
88  *\brief        install crash handlers
89  */
90 void crash_install_handlers(void)
91 {
92 #if CRASH_DIALOG 
93         sigset_t mask;
94
95         if (!is_crash_dialog_allowed()) return;
96
97         sigemptyset(&mask);
98
99 #ifdef SIGSEGV
100         signal(SIGSEGV, crash_handler);
101         sigaddset(&mask, SIGSEGV);
102 #endif
103         
104 #ifdef SIGFPE
105         signal(SIGFPE, crash_handler);
106         sigaddset(&mask, SIGFPE);
107 #endif
108
109 #ifdef SIGILL
110         signal(SIGILL, crash_handler);
111         sigaddset(&mask, SIGILL);
112 #endif
113
114 #ifdef SIGABRT
115         signal(SIGABRT, crash_handler);
116         sigaddset(&mask, SIGABRT);
117 #endif
118
119         sigprocmask(SIG_UNBLOCK, &mask, 0);
120 #endif /* CRASH_DIALOG */       
121 }
122
123 /***/
124
125 /*!
126  *\brief        crash dialog entry point 
127  */
128 void crash_main(const char *arg) 
129 {
130 #if CRASH_DIALOG 
131         gchar *text;
132         gchar **tokens;
133         unsigned long pid;
134         GString *output;
135
136         crash_create_debugger_file();
137         tokens = g_strsplit(arg, ",", 0);
138
139         pid = atol(tokens[0]);
140         text = g_strdup_printf(_("Claws Mail process (%ld) received signal %ld"),
141                                pid, atol(tokens[1]));
142
143         output = g_string_new("");     
144         crash_debug(pid, tokens[2], output);
145
146         /*
147          * try to get the settings
148          */
149         prefs_common_read_config();
150
151         crash_dialog_show(text, output->str);
152         g_string_free(output, TRUE);
153         g_free(text);
154         g_strfreev(tokens);
155 #endif /* CRASH_DIALOG */       
156 }
157
158 /*!
159  *\brief        show crash dialog
160  *
161  *\param        text Description
162  *\param        debug_output Output text by gdb
163  *
164  *\return       GtkWidget * Dialog widget
165  */
166 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
167 {
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;
181         GtkTextBuffer *buffer;
182         GtkTextIter iter;
183
184         window1 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "crash");
185         gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
186         gtk_window_set_title(GTK_WINDOW(window1), _("Claws Mail 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);
190
191
192         vbox1 = gtk_vbox_new(FALSE, 2);
193         gtk_widget_show(vbox1);
194         gtk_container_add(GTK_CONTAINER(window1), vbox1);
195
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);
200
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);
206
207         frame1 = gtk_frame_new(_("Debug log"));
208         gtk_widget_show(frame1);
209         gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
210
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);
217
218         text1 = gtk_text_view_new();
219         gtk_text_view_set_editable(GTK_TEXT_VIEW(text1), FALSE);
220         gtk_widget_show(text1);
221         gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
222         
223         crash_report = g_strdup_printf(
224                 "Claws Mail version %s\n"
225                 "GTK+ version %d.%d.%d / GLib %d.%d.%d\n"
226                 "Locale: %s (charset: %s)\n"
227                 "Features:%s\n"
228                 "Operating system: %s\n"
229                 "C Library: %s\n--\n%s",
230                 VERSION,
231                 gtk_major_version, gtk_minor_version, gtk_micro_version,
232                 glib_major_version, glib_minor_version, glib_micro_version,
233                 conv_get_current_locale(), conv_get_locale_charset_str(),
234                 get_compiled_in_features(),
235                 get_operating_system(),
236                 get_lib_version(),
237                 debug_output);
238
239         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
240         gtk_text_buffer_get_start_iter(buffer, &iter);
241         gtk_text_buffer_insert(buffer, &iter, crash_report, -1);
242
243         hbuttonbox3 = gtk_hbutton_box_new();
244         gtk_widget_show(hbuttonbox3);
245         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
246
247         hbuttonbox4 = gtk_hbutton_box_new();
248         gtk_widget_show(hbuttonbox4);
249         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
250
251         button3 = gtk_button_new_with_label(_("Close"));
252         gtk_widget_show(button3);
253         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
254         GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
255
256         button4 = gtk_button_new_with_label(_("Save..."));
257         gtk_widget_show(button4);
258         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
259         GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
260
261         button5 = gtk_button_new_with_label(_("Create bug report"));
262         gtk_widget_show(button5);
263         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
264         GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
265         
266         g_signal_connect(G_OBJECT(window1), "delete_event",
267                          G_CALLBACK(gtk_main_quit), NULL);
268         g_signal_connect(G_OBJECT(button3),   "clicked",
269                          G_CALLBACK(gtk_main_quit), NULL);
270         g_signal_connect(G_OBJECT(button4), "clicked",
271                          G_CALLBACK(crash_save_crash_log), crash_report);
272         g_signal_connect(G_OBJECT(button5), "clicked",
273                          G_CALLBACK(crash_create_bug_report), NULL);
274
275         gtk_widget_show(window1);
276
277         gtk_main();
278         return window1;
279 }
280
281
282 /*!
283  *\brief        create debugger script file in claws directory.
284  *              all the other options (creating temp files) looked too 
285  *              convoluted.
286  */
287 static void crash_create_debugger_file(void)
288 {
289         gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
290         
291         str_write_to_file(DEBUG_SCRIPT, filespec);
292         g_free(filespec);
293 }
294
295 /*!
296  *\brief        saves crash log to a file
297  */
298 static void crash_save_crash_log(GtkButton *button, const gchar *text)
299 {
300         time_t timer;
301         struct tm *lt;
302         char buf[100];
303         struct tm buft;
304         gchar *filename;
305
306         timer = time(NULL);
307         lt = localtime_r(&timer, &buft);
308         strftime(buf, sizeof buf, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
309         if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
310         &&  *filename)
311                 str_write_to_file(text, filename);
312         g_free(filename);       
313 }
314
315 /*!
316  *\brief        create bug report (goes to Claws Mail bug tracker)      
317  */
318 static void crash_create_bug_report(GtkButton *button, const gchar *data)
319 {
320         open_uri(BUGZILLA_URI, prefs_common_get_uri_cmd());
321 }
322
323 /*!
324  *\brief        launches debugger and attaches it to crashed claws
325  */
326 static void crash_debug(unsigned long crash_pid, 
327                         gchar *exe_image,
328                         GString *debug_output)
329 {
330         int choutput[2];
331         pid_t pid;
332
333         pipe(choutput);
334
335         if (0 == (pid = fork())) {
336                 char *argp[10];
337                 char **argptr = argp;
338                 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
339
340                 setgid(getgid());
341                 setuid(getuid());
342
343                 /*
344                  * setup debugger to attach to crashed claws
345                  */
346                 *argptr++ = "gdb"; 
347                 *argptr++ = "--nw";
348                 *argptr++ = "--nx";
349                 *argptr++ = "--quiet";
350                 *argptr++ = "--batch";
351                 *argptr++ = "-x";
352                 *argptr++ = filespec;
353                 *argptr++ = exe_image;
354                 *argptr++ = g_strdup_printf("%ld", crash_pid);
355                 *argptr   = NULL;
356
357                 /*
358                  * redirect output to write end of pipe
359                  */
360                 close(1);
361                 dup(choutput[1]);
362                 close(choutput[0]);
363                 if (-1 == execvp("gdb", argp)) 
364                         g_print("error execvp\n");
365         } else {
366                 char buf[100];
367                 int r;
368         
369                 waitpid(pid, NULL, 0);
370
371                 /*
372                  * make it non blocking
373                  */
374                 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
375                         g_print("set to non blocking failed\n");
376
377                 /*
378                  * get the output
379                  */
380                 do {
381                         r = read(choutput[0], buf, sizeof buf - 1);
382                         if (r > 0) {
383                                 buf[r] = 0;
384                                 g_string_append(debug_output, buf);
385                         }
386                 } while (r > 0);
387                 
388                 close(choutput[0]);
389                 close(choutput[1]);
390                 
391                 /*
392                  * kill the process we attached to
393                  */
394                 kill(crash_pid, SIGCONT); 
395         }
396 }
397
398 /***/
399
400 /*!
401  *\brief        features
402  */
403 static const gchar *get_compiled_in_features(void)
404 {
405         return g_strdup_printf("%s",
406 #if USE_THREADS
407                    " gthread"
408 #endif
409 #if INET6
410                    " IPv6"
411 #endif
412 #if HAVE_ICONV
413                    " iconv"
414 #endif
415 #if HAVE_LIBCOMPFACE
416                    " compface"
417 #endif
418 #if USE_GNUTLS
419                    " GnuTLS"
420 #endif
421 #if USE_LDAP
422                    " LDAP"
423 #endif
424 #if USE_JPILOT
425                    " JPilot"
426 #endif
427 #if USE_ENCHANT
428                    " GNU/aspell"
429 #endif
430 #if HAVE_LIBETPAN
431                    " libetpan"
432 #endif
433 #if USE_GNOMEPRINT
434                    " libgnomeprint"
435 #endif
436 #if HAVE_LIBSM
437                    " libSM"
438 #endif
439         "");
440 }
441
442 /***/
443
444 /*!
445  *\brief        library version
446  */
447 static const gchar *get_lib_version(void)
448 {
449 #if defined(__GNU_LIBRARY__)
450         return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
451 #else
452         return g_strdup(_("Unknown"));
453 #endif
454 }
455
456 /***/
457
458 /*!
459  *\brief        operating system
460  */
461 static const gchar *get_operating_system(void)
462 {
463 #if HAVE_SYS_UTSNAME_H
464         struct utsname utsbuf;
465         uname(&utsbuf);
466         return g_strdup_printf("%s %s (%s)",
467                                utsbuf.sysname,
468                                utsbuf.release,
469                                utsbuf.machine);
470 #else
471         return g_strdup(_("Unknown"));
472         
473 #endif
474 }
475
476 /***/
477
478 /*!
479  *\brief        see if the crash dialog is allowed (because some
480  *              developers may prefer to run claws-mail under gdb...)
481  */
482 static gboolean is_crash_dialog_allowed(void)
483 {
484         return !getenv("CLAWS_NO_CRASH");
485 }
486
487 /*!
488  *\brief        this handler will probably evolve into 
489  *              something better.
490  */
491 static void crash_handler(int sig)
492 {
493         pid_t pid;
494         static volatile unsigned long crashed_ = 0;
495
496         /*
497          * let's hope argv0 aren't trashed.
498          * both are defined in main.c.
499          */
500         extern gchar *argv0;
501
502
503         /*
504          * besides guarding entrancy it's probably also better 
505          * to mask off signals
506          */
507         if (crashed_) return;
508
509         crashed_++;
510
511 #ifdef SIGTERM
512         if (sig == SIGTERM) 
513                 clean_quit(NULL);
514 #endif
515
516         /*
517          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
518          */
519         gdk_pointer_ungrab(GDK_CURRENT_TIME);
520         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
521         gdk_flush();
522
523         if (0 == (pid = fork())) {
524                 char buf[50];
525                 char *args[5];
526         
527                 /*
528                  * probably also some other parameters (like GTK+ ones).
529                  * also we pass the full startup dir and the real command
530                  * line typed in (argv0)
531                  */
532                 args[0] = argv0; 
533                 args[1] = "--debug";
534                 args[2] = "--crash";
535                 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
536                 args[3] = buf;
537                 args[4] = NULL;
538
539                 chdir(claws_get_startup_dir());
540                 setgid(getgid());
541                 setuid(getuid());
542                 execvp(argv0, args);
543         } else {
544                 waitpid(pid, NULL, 0);
545                 crash_cleanup_exit();
546                 _exit(253);
547         }
548
549         _exit(253);
550 }
551
552 /*!
553  *\brief        put all the things here we can do before
554  *              letting the program die
555  */
556 static void crash_cleanup_exit(void)
557 {
558         const char *filename = claws_get_socket_name();
559         claws_unlink(filename);
560 }
561
562 #endif