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