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