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