correct the ChangeLog description
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #       include <config.h>
22 #endif
23
24 #ifdef CRASH_DIALOG
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include <time.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37
38 #include <errno.h>
39 #include <fcntl.h>
40
41 #if HAVE_SYS_UTSNAME_H
42 #       include <sys/utsname.h>
43 #endif
44
45 #if defined(__GNU_LIBRARY__)
46 #       include <gnu/libc-version.h>
47 #endif
48
49 #ifdef SIGTERM
50 #include "main.h"
51 #endif
52 #include "sylpheed.h"
53 #include "crash.h"
54 #include "utils.h"
55 #include "filesel.h"
56 #include "version.h"
57 #include "prefs_common.h"
58
59 /*
60  * NOTE: the crash dialog is called when sylpheed is not 
61  * initialized, so do not assume settings are available.
62  * for example, loading / creating pixmaps seems not 
63  * to be possible.
64  */
65
66 /***/
67
68 static GtkWidget        *crash_dialog_show              (const gchar *text, 
69                                                          const gchar *debug_output);
70 static void              crash_create_debugger_file     (void);
71 static void              crash_save_crash_log           (GtkButton *, const gchar *);
72 static void              crash_create_bug_report        (GtkButton *, const gchar *);
73 static void              crash_debug                    (unsigned long crash_pid, 
74                                                          gchar   *exe_image,
75                                                          GString *debug_output);
76 static const gchar      *get_compiled_in_features       (void);
77 static const gchar      *get_lib_version                (void);
78 static const gchar      *get_operating_system           (void);
79 static gboolean          is_crash_dialog_allowed        (void);
80 static void              crash_handler                  (int sig);
81 static void              crash_cleanup_exit             (void);
82
83 /***/
84
85 static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
86
87 /***/
88
89 /*!
90  *\brief        install crash handlers
91  */
92 void crash_install_handlers(void)
93 {
94 #if CRASH_DIALOG 
95         sigset_t mask;
96
97         if (!is_crash_dialog_allowed()) return;
98
99         sigemptyset(&mask);
100
101 #ifdef SIGSEGV
102         signal(SIGSEGV, crash_handler);
103         sigaddset(&mask, SIGSEGV);
104 #endif
105         
106 #ifdef SIGFPE
107         signal(SIGFPE, crash_handler);
108         sigaddset(&mask, SIGFPE);
109 #endif
110
111 #ifdef SIGILL
112         signal(SIGILL, crash_handler);
113         sigaddset(&mask, SIGILL);
114 #endif
115
116 #ifdef SIGABRT
117         signal(SIGABRT, crash_handler);
118         sigaddset(&mask, SIGABRT);
119 #endif
120
121         sigprocmask(SIG_UNBLOCK, &mask, 0);
122 #endif /* CRASH_DIALOG */       
123 }
124
125 /***/
126
127 /*!
128  *\brief        crash dialog entry point 
129  */
130 void crash_main(const char *arg) 
131 {
132 #if CRASH_DIALOG 
133         gchar *text;
134         gchar **tokens;
135         unsigned long pid;
136         GString *output;
137
138         crash_create_debugger_file();
139         tokens = g_strsplit(arg, ",", 0);
140
141         pid = atol(tokens[0]);
142         text = g_strdup_printf(_("Sylpheed-Claws process (%ld) received signal %ld"),
143                                pid, atol(tokens[1]));
144
145         output = g_string_new("");     
146         crash_debug(pid, tokens[2], output);
147
148         /*
149          * try to get the settings
150          */
151         prefs_common_read_config();
152
153         crash_dialog_show(text, output->str);
154         g_string_free(output, TRUE);
155         g_free(text);
156         g_strfreev(tokens);
157 #endif /* CRASH_DIALOG */       
158 }
159
160 /*!
161  *\brief        show crash dialog
162  *
163  *\param        text Description
164  *\param        debug_output Output text by gdb
165  *
166  *\return       GtkWidget * Dialog widget
167  */
168 static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
169 {
170         GtkWidget *window1;
171         GtkWidget *vbox1;
172         GtkWidget *hbox1;
173         GtkWidget *label1;
174         GtkWidget *frame1;
175         GtkWidget *scrolledwindow1;
176         GtkWidget *text1;
177         GtkWidget *hbuttonbox3;
178         GtkWidget *hbuttonbox4;
179         GtkWidget *button3;
180         GtkWidget *button4;
181         GtkWidget *button5;
182         gchar     *crash_report;
183         GtkTextBuffer *buffer;
184         GtkTextIter iter;
185
186         window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187         gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
188         gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed-Claws has crashed"));
189         gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
190         gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
191         gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
192
193
194         vbox1 = gtk_vbox_new(FALSE, 2);
195         gtk_widget_show(vbox1);
196         gtk_container_add(GTK_CONTAINER(window1), vbox1);
197
198         hbox1 = gtk_hbox_new(FALSE, 4);
199         gtk_widget_show(hbox1);
200         gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
201         gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
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_AUTOMATIC);
219
220         text1 = gtk_text_view_new();
221         gtk_text_view_set_editable(GTK_TEXT_VIEW(text1), FALSE);
222         gtk_widget_show(text1);
223         gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
224         
225         crash_report = g_strdup_printf(
226                 "Sylpheed-Claws version %s\n"
227                 "GTK+ version %d.%d.%d / GLib %d.%d.%d\n"
228                 "Locale: %s (charset: %s)\n"
229                 "Features:%s\n"
230                 "Operating system: %s\n"
231                 "C Library: %s\n--\n%s",
232                 VERSION,
233                 gtk_major_version, gtk_minor_version, gtk_micro_version,
234                 glib_major_version, glib_minor_version, glib_micro_version,
235                 conv_get_current_locale(), conv_get_locale_charset_str(),
236                 get_compiled_in_features(),
237                 get_operating_system(),
238                 get_lib_version(),
239                 debug_output);
240
241         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
242         gtk_text_buffer_get_start_iter(buffer, &iter);
243         gtk_text_buffer_insert(buffer, &iter, crash_report, -1);
244
245         hbuttonbox3 = gtk_hbutton_box_new();
246         gtk_widget_show(hbuttonbox3);
247         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
248
249         hbuttonbox4 = gtk_hbutton_box_new();
250         gtk_widget_show(hbuttonbox4);
251         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
252
253         button3 = gtk_button_new_with_label(_("Close"));
254         gtk_widget_show(button3);
255         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
256         GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
257
258         button4 = gtk_button_new_with_label(_("Save..."));
259         gtk_widget_show(button4);
260         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
261         GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
262
263         button5 = gtk_button_new_with_label(_("Create bug report"));
264         gtk_widget_show(button5);
265         gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
266         GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
267         
268         g_signal_connect(G_OBJECT(window1), "delete_event",
269                          G_CALLBACK(gtk_main_quit), NULL);
270         g_signal_connect(G_OBJECT(button3),   "clicked",
271                          G_CALLBACK(gtk_main_quit), NULL);
272         g_signal_connect(G_OBJECT(button4), "clicked",
273                          G_CALLBACK(crash_save_crash_log), crash_report);
274         g_signal_connect(G_OBJECT(button5), "clicked",
275                          G_CALLBACK(crash_create_bug_report), NULL);
276
277         gtk_widget_show(window1);
278
279         gtk_main();
280         return window1;
281 }
282
283
284 /*!
285  *\brief        create debugger script file in sylpheed directory.
286  *              all the other options (creating temp files) looked too 
287  *              convoluted.
288  */
289 static void crash_create_debugger_file(void)
290 {
291         gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
292         
293         str_write_to_file(DEBUG_SCRIPT, filespec);
294         g_free(filespec);
295 }
296
297 /*!
298  *\brief        saves crash log to a file
299  */
300 static void crash_save_crash_log(GtkButton *button, const gchar *text)
301 {
302         time_t timer;
303         struct tm *lt;
304         char buf[100];
305         gchar *filename;
306
307         timer = time(NULL);
308         lt = localtime(&timer);
309         strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
310         if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
311         &&  *filename)
312                 str_write_to_file(text, filename);
313         g_free(filename);       
314 }
315
316 /*!
317  *\brief        create bug report (goes to Sylpheed Claws bug tracker)  
318  */
319 static void crash_create_bug_report(GtkButton *button, const gchar *data)
320 {
321         open_uri(BUGZILLA_URI, prefs_common.uri_cmd);
322 }
323
324 /*!
325  *\brief        launches debugger and attaches it to crashed sylpheed
326  */
327 static void crash_debug(unsigned long crash_pid, 
328                         gchar *exe_image,
329                         GString *debug_output)
330 {
331         int choutput[2];
332         pid_t pid;
333
334         pipe(choutput);
335
336         if (0 == (pid = fork())) {
337                 char *argp[10];
338                 char **argptr = argp;
339                 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
340
341                 setgid(getgid());
342                 setuid(getuid());
343
344                 /*
345                  * setup debugger to attach to crashed sylpheed
346                  */
347                 *argptr++ = "gdb"; 
348                 *argptr++ = "--nw";
349                 *argptr++ = "--nx";
350                 *argptr++ = "--quiet";
351                 *argptr++ = "--batch";
352                 *argptr++ = "-x";
353                 *argptr++ = filespec;
354                 *argptr++ = exe_image;
355                 *argptr++ = g_strdup_printf("%ld", crash_pid);
356                 *argptr   = NULL;
357
358                 /*
359                  * redirect output to write end of pipe
360                  */
361                 close(1);
362                 dup(choutput[1]);
363                 close(choutput[0]);
364                 if (-1 == execvp("gdb", argp)) 
365                         puts("error execvp\n");
366         } else {
367                 char buf[100];
368                 int r;
369         
370                 waitpid(pid, NULL, 0);
371
372                 /*
373                  * make it non blocking
374                  */
375                 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
376                         puts("set to non blocking failed\n");
377
378                 /*
379                  * get the output
380                  */
381                 do {
382                         r = read(choutput[0], buf, sizeof buf - 1);
383                         if (r > 0) {
384                                 buf[r] = 0;
385                                 g_string_append(debug_output, buf);
386                         }
387                 } while (r > 0);
388                 
389                 close(choutput[0]);
390                 close(choutput[1]);
391                 
392                 /*
393                  * kill the process we attached to
394                  */
395                 kill(crash_pid, SIGCONT); 
396         }
397 }
398
399 /***/
400
401 /*!
402  *\brief        features
403  */
404 static const gchar *get_compiled_in_features(void)
405 {
406         return g_strdup_printf("%s",
407 #if USE_THREADS
408                    " gthread"
409 #endif
410 #if INET6
411                    " IPv6"
412 #endif
413 #if HAVE_ICONV
414                    " iconv"
415 #endif
416 #if HAVE_LIBCOMPFACE
417                    " compface"
418 #endif
419 #if USE_OPENSSL
420                    " OpenSSL"
421 #endif
422 #if USE_LDAP
423                    " LDAP"
424 #endif
425 #if USE_JPILOT
426                    " JPilot"
427 #endif
428 #if USE_ASPELL
429                    " GNU/aspell"
430 #endif
431 #if HAVE_LIBETPAN
432                    " libetpan"
433 #endif
434 #if USE_GNOMEPRINT
435                    " libgnomeprint"
436 #endif
437         "");
438 }
439
440 /***/
441
442 /*!
443  *\brief        library version
444  */
445 static const gchar *get_lib_version(void)
446 {
447 #if defined(__GNU_LIBRARY__)
448         return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
449 #else
450         return g_strdup(_("Unknown"));
451 #endif
452 }
453
454 /***/
455
456 /*!
457  *\brief        operating system
458  */
459 static const gchar *get_operating_system(void)
460 {
461 #if HAVE_SYS_UTSNAME_H
462         struct utsname utsbuf;
463         uname(&utsbuf);
464         return g_strdup_printf("%s %s (%s)",
465                                utsbuf.sysname,
466                                utsbuf.release,
467                                utsbuf.machine);
468 #else
469         return g_strdup(_("Unknown"));
470         
471 #endif
472 }
473
474 /***/
475
476 /*!
477  *\brief        see if the crash dialog is allowed (because some
478  *              developers may prefer to run sylpheed under gdb...)
479  */
480 static gboolean is_crash_dialog_allowed(void)
481 {
482         return !getenv("SYLPHEED_NO_CRASH");
483 }
484
485 /*!
486  *\brief        this handler will probably evolve into 
487  *              something better.
488  */
489 static void crash_handler(int sig)
490 {
491         pid_t pid;
492         static volatile unsigned long crashed_ = 0;
493
494         /*
495          * let's hope argv0 aren't trashed.
496          * both are defined in main.c.
497          */
498         extern gchar *argv0;
499
500
501         /*
502          * besides guarding entrancy it's probably also better 
503          * to mask off signals
504          */
505         if (crashed_) return;
506
507         crashed_++;
508
509 #ifdef SIGTERM
510         if (sig == SIGTERM) 
511                 clean_quit(NULL);
512 #endif
513
514         /*
515          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
516          */
517         gdk_pointer_ungrab(GDK_CURRENT_TIME);
518         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
519         gdk_flush();
520
521         if (0 == (pid = fork())) {
522                 char buf[50];
523                 char *args[5];
524         
525                 /*
526                  * probably also some other parameters (like GTK+ ones).
527                  * also we pass the full startup dir and the real command
528                  * line typed in (argv0)
529                  */
530                 args[0] = argv0; 
531                 args[1] = "--debug";
532                 args[2] = "--crash";
533                 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
534                 args[3] = buf;
535                 args[4] = NULL;
536
537                 chdir(sylpheed_get_startup_dir());
538                 setgid(getgid());
539                 setuid(getuid());
540                 execvp(argv0, args);
541         } else {
542                 waitpid(pid, NULL, 0);
543                 crash_cleanup_exit();
544                 _exit(253);
545         }
546
547         _exit(253);
548 }
549
550 /*!
551  *\brief        put all the things here we can do before
552  *              letting the program die
553  */
554 static void crash_cleanup_exit(void)
555 {
556         extern gchar *get_socket_name(void);
557         const char *filename = get_socket_name();
558         g_unlink(filename);
559 }
560
561 #endif