plug memory leak reported by Kim Schultz & Martin Kluge
[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[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++ = "-x";
323                 *argptr++ = filespec;
324                 *argptr++ = exe_image;
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(debug_output, 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        see if the crash dialog is allowed (because some
451  *              developers may prefer to run sylpheed under gdb...)
452  */
453 static gboolean is_crash_dialog_allowed(void)
454 {
455         return !getenv("SYLPHEED_NO_CRASH");
456 }
457
458 /*
459  *\brief        this handler will probably evolve into 
460  *              something better.
461  */
462 static void crash_handler(int sig)
463 {
464         pid_t pid;
465         static volatile unsigned long crashed_ = 0;
466
467         /*
468          * let's hope startup_dir and argv0 aren't trashed.
469          * both are defined in main.c.
470          */
471         extern gchar *startup_dir;
472         extern gchar *argv0;
473
474
475         /*
476          * besides guarding entrancy it's probably also better 
477          * to mask off signals
478          */
479         if (crashed_) return;
480
481         crashed_++;
482
483         /*
484          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
485          */
486         gdk_pointer_ungrab(GDK_CURRENT_TIME);
487         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
488         gdk_flush();
489
490
491         if (0 == (pid = fork())) {
492                 char buf[50];
493                 char *args[4];
494         
495                 /*
496                  * probably also some other parameters (like GTK+ ones).
497                  * also we pass the full startup dir and the real command
498                  * line typed in (argv0)
499                  */
500                 args[0] = "--debug";
501                 args[1] = "--crash";
502                 sprintf(buf, "%ld,%d,%s", getppid(), sig, argv0);
503                 args[2] = buf;
504                 args[3] = NULL;
505
506                 chdir(startup_dir);
507                 setgid(getgid());
508                 setuid(getuid());
509                 execvp(argv0, args);
510         } else {
511                 waitpid(pid, NULL, 0);
512                 _exit(253);
513         }
514
515         _exit(253);
516 }
517