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