add #include "defs.h"
[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 "defs.h"
27
28 #include <glib.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36
37 #include <errno.h>
38 #include <fcntl.h>
39
40 #if HAVE_SYS_UTSNAME_H
41 #       include <sys/utsname.h>
42 #endif
43
44 #if defined(__GNU_LIBRARY__)
45 #       include <gnu/libc-version.h>
46 #endif
47
48 #ifdef SIGTERM
49 #include "main.h"
50 #endif
51 #include "intl.h"
52 #include "crash.h"
53 #include "utils.h"
54 #include "filesel.h"
55 #include "version.h"
56 #include "prefs_common.h"
57
58 /*
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 
62  * to be possible.
63  */
64
65 /***/
66
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, 
73                                                          gchar   *exe_image,
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);
81
82 /***/
83
84 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
85
86 /***/
87
88 /*!
89  *\brief        install crash handlers
90  */
91 void crash_install_handlers(void)
92 {
93 #if CRASH_DIALOG 
94         sigset_t mask;
95
96         if (!is_crash_dialog_allowed()) return;
97
98         sigemptyset(&mask);
99
100 #ifdef SIGSEGV
101         signal(SIGSEGV, crash_handler);
102         sigaddset(&mask, SIGSEGV);
103 #endif
104         
105 #ifdef SIGFPE
106         signal(SIGFPE, crash_handler);
107         sigaddset(&mask, SIGFPE);
108 #endif
109
110 #ifdef SIGILL
111         signal(SIGILL, crash_handler);
112         sigaddset(&mask, SIGILL);
113 #endif
114
115 #ifdef SIGABRT
116         signal(SIGABRT, crash_handler);
117         sigaddset(&mask, SIGABRT);
118 #endif
119
120         sigprocmask(SIG_UNBLOCK, &mask, 0);
121 #endif /* CRASH_DIALOG */       
122 }
123
124 /***/
125
126 /*!
127  *\brief        crash dialog entry point 
128  */
129 void crash_main(const char *arg) 
130 {
131 #if CRASH_DIALOG 
132         gchar *text;
133         gchar **tokens;
134         unsigned long pid;
135         GString *output;
136
137         crash_create_debugger_file();
138         tokens = g_strsplit(arg, ",", 0);
139
140         pid = atol(tokens[0]);
141         text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
142                                pid, atol(tokens[1]));
143
144         output = g_string_new("");     
145         crash_debug(pid, tokens[2], output);
146
147         /*
148          * try to get the settings
149          */
150         prefs_common_init();
151         prefs_common_read_config();
152
153         crash_dialog_show(text, output->str);
154         g_string_free(output, TRUE);
155         g_free(text);
156         g_strfreev(tokens);
157 #endif /* CRASH_DIALOG */       
158 }
159
160 /*!
161  *\brief        show crash dialog
162  *
163  *\param        text Description
164  *\param        debug_output Output text by gdb
165  *
166  *\return       GtkWidget * Dialog widget
167  */
168 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
169 {
170         GtkWidget *window1;
171         GtkWidget *vbox1;
172         GtkWidget *hbox1;
173         GtkWidget *label1;
174         GtkWidget *frame1;
175         GtkWidget *scrolledwindow1;
176         GtkWidget *text1;
177         GtkWidget *hbuttonbox3;
178         GtkWidget *hbuttonbox4;
179         GtkWidget *button3;
180         GtkWidget *button4;
181         GtkWidget *button5;
182         gchar     *crash_report;
183
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);
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_ALWAYS);
217
218         text1 = gtk_text_new(NULL, NULL);
219         gtk_text_set_editable(GTK_TEXT(text1), FALSE);
220         gtk_widget_show(text1);
221         gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
222         
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",
225                 VERSION,
226                 gtk_major_version, gtk_minor_version, gtk_micro_version,
227                 get_compiled_in_features(),
228                 get_operating_system(),
229                 get_lib_version(),
230                 debug_output);
231
232         gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
233
234         hbuttonbox3 = gtk_hbutton_box_new();
235         gtk_widget_show(hbuttonbox3);
236         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
237
238         hbuttonbox4 = gtk_hbutton_box_new();
239         gtk_widget_show(hbuttonbox4);
240         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
241
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);
246
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);
251
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);
256         
257         gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
258                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
259         gtk_signal_connect(GTK_OBJECT(button3),   "clicked",
260                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
261         gtk_signal_connect(GTK_OBJECT(button4), "clicked",
262                            GTK_SIGNAL_FUNC(crash_save_crash_log),
263                            crash_report);
264         gtk_signal_connect(GTK_OBJECT(button5), "clicked",
265                            GTK_SIGNAL_FUNC(crash_create_bug_report),
266                            NULL);
267
268         gtk_widget_show(window1);
269
270         gtk_main();
271         return window1;
272 }
273
274
275 /*!
276  *\brief        create debugger script file in sylpheed directory.
277  *              all the other options (creating temp files) looked too 
278  *              convoluted.
279  */
280 static void crash_create_debugger_file(void)
281 {
282         gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
283         
284         str_write_to_file(DEBUG_SCRIPT, filespec);
285         g_free(filespec);
286 }
287
288 /*!
289  *\brief        saves crash log to a file
290  */
291 static void crash_save_crash_log(GtkButton *button, const gchar *text)
292 {
293         time_t timer;
294         struct tm *lt;
295         char buf[100];
296         gchar *filename;
297
298         timer = time(NULL);
299         lt = localtime(&timer);
300         strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
301         if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
302         &&  *filename)
303                 str_write_to_file(text, filename);
304         g_free(filename);       
305 }
306
307 /*!
308  *\brief        create bug report (goes to Sylpheed Claws bug tracker)  
309  */
310 static void crash_create_bug_report(GtkButton *button, const gchar *data)
311 {
312         open_uri(CLAWS_BUGZILLA_URI, prefs_common.uri_cmd);
313 }
314
315 /*!
316  *\brief        launches debugger and attaches it to crashed sylpheed
317  */
318 static void crash_debug(unsigned long crash_pid, 
319                         gchar *exe_image,
320                         GString *debug_output)
321 {
322         int choutput[2];
323         pid_t pid;
324
325         pipe(choutput);
326
327         if (0 == (pid = fork())) {
328                 char *argp[10];
329                 char **argptr = argp;
330                 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
331
332                 setgid(getgid());
333                 setuid(getuid());
334
335                 /*
336                  * setup debugger to attach to crashed sylpheed
337                  */
338                 *argptr++ = "gdb"; 
339                 *argptr++ = "--nw";
340                 *argptr++ = "--nx";
341                 *argptr++ = "--quiet";
342                 *argptr++ = "--batch";
343                 *argptr++ = "-x";
344                 *argptr++ = filespec;
345                 *argptr++ = exe_image;
346                 *argptr++ = g_strdup_printf("%ld", crash_pid);
347                 *argptr   = NULL;
348
349                 /*
350                  * redirect output to write end of pipe
351                  */
352                 close(1);
353                 dup(choutput[1]);
354                 close(choutput[0]);
355                 if (-1 == execvp("gdb", argp)) 
356                         puts("error execvp\n");
357         } else {
358                 char buf[100];
359                 int r;
360         
361                 waitpid(pid, NULL, 0);
362
363                 /*
364                  * make it non blocking
365                  */
366                 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
367                         puts("set to non blocking failed\n");
368
369                 /*
370                  * get the output
371                  */
372                 do {
373                         r = read(choutput[0], buf, sizeof buf - 1);
374                         if (r > 0) {
375                                 buf[r] = 0;
376                                 g_string_append(debug_output, buf);
377                         }
378                 } while (r > 0);
379                 
380                 close(choutput[0]);
381                 close(choutput[1]);
382                 
383                 /*
384                  * kill the process we attached to
385                  */
386                 kill(crash_pid, SIGCONT); 
387         }
388 }
389
390 /***/
391
392 /*!
393  *\brief        features
394  */
395 static const gchar *get_compiled_in_features(void)
396 {
397         return g_strdup_printf("%s",
398 #if HAVE_GDK_IMLIB
399                    " gdk_imlib"
400 #endif
401 #if HAVE_GDK_PIXBUF
402                    " gdk-pixbuf"
403 #endif
404 #if USE_THREADS
405                    " gthread"
406 #endif
407 #if INET6
408                    " IPv6"
409 #endif
410 #if HAVE_LIBCOMPFACE
411                    " libcompface"
412 #endif
413 #if HAVE_LIBJCONV
414                    " libjconv"
415 #endif
416 #if USE_GPGME
417                    " GPGME"
418 #endif
419 #if USE_OPENSSL
420                    " OpenSSL"
421 #endif
422 #if USE_LDAP
423                    " LDAP"
424 #endif
425 #if USE_JPILOT
426                    " JPilot"
427 #endif
428 #if USE_ASPELL
429                    " GNU/aspell"
430 #endif
431         "");
432 }
433
434 /***/
435
436 /*!
437  *\brief        library version
438  */
439 static const gchar *get_lib_version(void)
440 {
441 #if defined(__GNU_LIBRARY__)
442         return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
443 #else
444         return g_strdup(_("Unknown"));
445 #endif
446 }
447
448 /***/
449
450 /*!
451  *\brief        operating system
452  */
453 static const gchar *get_operating_system(void)
454 {
455 #if HAVE_SYS_UTSNAME_H
456         struct utsname utsbuf;
457         uname(&utsbuf);
458         return g_strdup_printf("%s %s (%s)",
459                                utsbuf.sysname,
460                                utsbuf.release,
461                                utsbuf.machine);
462 #else
463         return g_strdup(_("Unknown"));
464         
465 #endif
466 }
467
468 /***/
469
470 /*!
471  *\brief        see if the crash dialog is allowed (because some
472  *              developers may prefer to run sylpheed under gdb...)
473  */
474 static gboolean is_crash_dialog_allowed(void)
475 {
476         return !getenv("SYLPHEED_NO_CRASH");
477 }
478
479 /*!
480  *\brief        this handler will probably evolve into 
481  *              something better.
482  */
483 static void crash_handler(int sig)
484 {
485         pid_t pid;
486         static volatile unsigned long crashed_ = 0;
487
488         /*
489          * let's hope argv0 aren't trashed.
490          * both are defined in main.c.
491          */
492         extern gchar *argv0;
493
494
495         /*
496          * besides guarding entrancy it's probably also better 
497          * to mask off signals
498          */
499         if (crashed_) return;
500
501         crashed_++;
502
503 #ifdef SIGTERM
504         if (sig == SIGTERM) 
505                 clean_quit();
506 #endif
507
508         /*
509          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
510          */
511         gdk_pointer_ungrab(GDK_CURRENT_TIME);
512         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
513         gdk_flush();
514
515         if (0 == (pid = fork())) {
516                 char buf[50];
517                 char *args[5];
518         
519                 /*
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)
523                  */
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;
530
531                 chdir(sylpheed_get_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);
539         }
540
541         _exit(253);
542 }
543
544 /*!
545  *\brief        put all the things here we can do before
546  *              letting the program die
547  */
548 static void crash_cleanup_exit(void)
549 {
550         extern gchar *get_socket_name(void);
551         const char *filename = get_socket_name();
552         unlink(filename);
553 }
554
555 #endif