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