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