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