2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2015 by the Claws Mail Team
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.
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.
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/>.
28 #include <glib/gi18n.h>
34 #include <sys/types.h>
40 #if HAVE_SYS_UTSNAME_H
41 # include <sys/utsname.h>
44 #if defined(__GNU_LIBRARY__) && !defined(__UCLIBC__)
45 # include <gnu/libc-version.h>
54 #include "prefs_common.h"
55 #include "manage_window.h"
58 * NOTE: the crash dialog is called when claws is not
59 * initialized, so do not assume settings are available.
60 * for example, loading / creating pixmaps seems not
66 static GtkWidget
*crash_dialog_show (const gchar
*text
,
67 const gchar
*debug_output
);
68 static void crash_create_debugger_file (void);
69 static void crash_save_crash_log (GtkButton
*, const gchar
*);
70 static void crash_create_bug_report (GtkButton
*, const gchar
*);
71 static void crash_debug (unsigned long crash_pid
,
73 GString
*debug_output
);
74 static const gchar
*get_compiled_in_features (void);
75 static const gchar
*get_lib_version (void);
76 static const gchar
*get_operating_system (void);
77 static gboolean
is_crash_dialog_allowed (void);
78 static void crash_handler (int sig
);
79 static void crash_cleanup_exit (void);
83 static const gchar
*DEBUG_SCRIPT
= "thread all apply\nbt full\nkill\nq";
88 *\brief install crash handlers
90 void crash_install_handlers(void)
95 if (!is_crash_dialog_allowed()) return;
100 signal(SIGSEGV
, crash_handler
);
101 sigaddset(&mask
, SIGSEGV
);
105 signal(SIGFPE
, crash_handler
);
106 sigaddset(&mask
, SIGFPE
);
110 signal(SIGILL
, crash_handler
);
111 sigaddset(&mask
, SIGILL
);
115 signal(SIGABRT
, crash_handler
);
116 sigaddset(&mask
, SIGABRT
);
119 sigprocmask(SIG_UNBLOCK
, &mask
, 0);
120 #endif /* CRASH_DIALOG */
126 *\brief crash dialog entry point
128 void crash_main(const char *arg
)
136 crash_create_debugger_file();
137 tokens
= g_strsplit(arg
, ",", 0);
139 pid
= atol(tokens
[0]);
140 text
= g_strdup_printf(_("Claws Mail process (%ld) received signal %ld"),
141 pid
, atol(tokens
[1]));
143 output
= g_string_new("");
144 crash_debug(pid
, tokens
[2], output
);
147 * try to get the settings
149 prefs_common_read_config();
151 crash_dialog_show(text
, output
->str
);
152 g_string_free(output
, TRUE
);
155 #endif /* CRASH_DIALOG */
159 *\brief show crash dialog
161 *\param text Description
162 *\param debug_output Output text by gdb
164 *\return GtkWidget * Dialog widget
166 static GtkWidget
*crash_dialog_show(const gchar
*text
, const gchar
*debug_output
)
173 GtkWidget
*scrolledwindow1
;
175 GtkWidget
*hbuttonbox3
;
176 GtkWidget
*hbuttonbox4
;
181 GtkTextBuffer
*buffer
;
184 window1
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "crash");
185 gtk_container_set_border_width(GTK_CONTAINER(window1
), 5);
186 gtk_window_set_title(GTK_WINDOW(window1
), _("Claws Mail has crashed"));
187 gtk_window_set_position(GTK_WINDOW(window1
), GTK_WIN_POS_CENTER
);
188 gtk_window_set_modal(GTK_WINDOW(window1
), TRUE
);
189 gtk_window_set_default_size(GTK_WINDOW(window1
), 460, 272);
192 vbox1
= gtk_vbox_new(FALSE
, 2);
193 gtk_widget_show(vbox1
);
194 gtk_container_add(GTK_CONTAINER(window1
), vbox1
);
196 hbox1
= gtk_hbox_new(FALSE
, 4);
197 gtk_widget_show(hbox1
);
198 gtk_box_pack_start(GTK_BOX(vbox1
), hbox1
, FALSE
, TRUE
, 0);
199 gtk_container_set_border_width(GTK_CONTAINER(hbox1
), 4);
201 label1
= gtk_label_new
202 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text
));
203 gtk_widget_show(label1
);
204 gtk_box_pack_start(GTK_BOX(hbox1
), label1
, TRUE
, TRUE
, 0);
205 gtk_misc_set_alignment(GTK_MISC(label1
), 7.45058e-09, 0.5);
207 frame1
= gtk_frame_new(_("Debug log"));
208 gtk_widget_show(frame1
);
209 gtk_box_pack_start(GTK_BOX(vbox1
), frame1
, TRUE
, TRUE
, 0);
211 scrolledwindow1
= gtk_scrolled_window_new(NULL
, NULL
);
212 gtk_widget_show(scrolledwindow1
);
213 gtk_container_add(GTK_CONTAINER(frame1
), scrolledwindow1
);
214 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1
), 3);
215 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1
),
216 GTK_POLICY_NEVER
, GTK_POLICY_AUTOMATIC
);
218 text1
= gtk_text_view_new();
219 gtk_text_view_set_editable(GTK_TEXT_VIEW(text1
), FALSE
);
220 gtk_widget_show(text1
);
221 gtk_container_add(GTK_CONTAINER(scrolledwindow1
), text1
);
223 crash_report
= g_strdup_printf(
224 "Claws Mail version %s\n"
225 "GTK+ version %d.%d.%d / GLib %d.%d.%d\n"
226 "Locale: %s (charset: %s)\n"
228 "Operating system: %s\n"
229 "C Library: %s\n--\n%s",
231 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
232 glib_major_version
, glib_minor_version
, glib_micro_version
,
233 conv_get_current_locale(), conv_get_locale_charset_str(),
234 get_compiled_in_features(),
235 get_operating_system(),
239 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1
));
240 gtk_text_buffer_get_start_iter(buffer
, &iter
);
241 gtk_text_buffer_insert(buffer
, &iter
, crash_report
, -1);
243 hbuttonbox3
= gtk_hbutton_box_new();
244 gtk_widget_show(hbuttonbox3
);
245 gtk_box_pack_start(GTK_BOX(vbox1
), hbuttonbox3
, FALSE
, FALSE
, 0);
247 hbuttonbox4
= gtk_hbutton_box_new();
248 gtk_widget_show(hbuttonbox4
);
249 gtk_box_pack_start(GTK_BOX(vbox1
), hbuttonbox4
, FALSE
, FALSE
, 0);
251 button3
= gtk_button_new_with_label(_("Close"));
252 gtk_widget_show(button3
);
253 gtk_container_add(GTK_CONTAINER(hbuttonbox4
), button3
);
254 gtkut_widget_set_can_default(button3
, TRUE
);
256 button4
= gtk_button_new_with_label(_("Save..."));
257 gtk_widget_show(button4
);
258 gtk_container_add(GTK_CONTAINER(hbuttonbox4
), button4
);
259 gtkut_widget_set_can_default(button4
, TRUE
);
261 button5
= gtk_button_new_with_label(_("Create bug report"));
262 gtk_widget_show(button5
);
263 gtk_container_add(GTK_CONTAINER(hbuttonbox4
), button5
);
264 gtkut_widget_set_can_default(button5
, TRUE
);
266 g_signal_connect(G_OBJECT(window1
), "delete_event",
267 G_CALLBACK(gtk_main_quit
), NULL
);
268 g_signal_connect(G_OBJECT(button3
), "clicked",
269 G_CALLBACK(gtk_main_quit
), NULL
);
270 g_signal_connect(G_OBJECT(button4
), "clicked",
271 G_CALLBACK(crash_save_crash_log
), crash_report
);
272 g_signal_connect(G_OBJECT(button5
), "clicked",
273 G_CALLBACK(crash_create_bug_report
), NULL
);
275 MANAGE_WINDOW_SIGNALS_CONNECT(window1
);
277 gtk_widget_show(window1
);
285 *\brief create debugger script file in claws directory.
286 * all the other options (creating temp files) looked too
289 static void crash_create_debugger_file(void)
291 gchar
*filespec
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, DEBUGGERRC
, NULL
);
293 str_write_to_file(DEBUG_SCRIPT
, filespec
);
298 *\brief saves crash log to a file
300 static void crash_save_crash_log(GtkButton
*button
, const gchar
*text
)
309 lt
= localtime_r(&timer
, &buft
);
310 strftime(buf
, sizeof buf
, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt
);
311 if (NULL
!= (filename
= filesel_select_file_save(_("Save crash information"), buf
))
313 str_write_to_file(text
, filename
);
318 *\brief create bug report (goes to Claws Mail bug tracker)
320 static void crash_create_bug_report(GtkButton
*button
, const gchar
*data
)
322 open_uri(BUGZILLA_URI
, prefs_common_get_uri_cmd());
326 *\brief launches debugger and attaches it to crashed claws
328 static void crash_debug(unsigned long crash_pid
,
330 GString
*debug_output
)
335 if (pipe(choutput
) == -1) {
336 g_print("can't pipe - error %s", g_strerror(errno
));
340 if (0 == (pid
= fork())) {
342 char **argptr
= argp
;
343 gchar
*filespec
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, DEBUGGERRC
, NULL
);
345 if (setgid(getgid()) != 0)
347 if (setuid(getuid()) != 0)
351 * setup debugger to attach to crashed claws
356 *argptr
++ = "--quiet";
357 *argptr
++ = "--batch";
359 *argptr
++ = filespec
;
360 *argptr
++ = exe_image
;
361 *argptr
++ = g_strdup_printf("%ld", crash_pid
);
365 * redirect output to write end of pipe
368 if (dup(choutput
[1]) < 0)
371 if (-1 == execvp("gdb", argp
))
372 g_print("error execvp\n");
377 waitpid(pid
, NULL
, 0);
380 * make it non blocking
382 if (-1 == fcntl(choutput
[0], F_SETFL
, O_NONBLOCK
))
383 g_print("set to non blocking failed\n");
389 r
= read(choutput
[0], buf
, sizeof buf
- 1);
392 g_string_append(debug_output
, buf
);
400 * kill the process we attached to
402 kill(crash_pid
, SIGCONT
);
411 static const gchar
*get_compiled_in_features(void)
413 return g_strdup_printf("%s",
447 *\brief library version
449 static const gchar
*get_lib_version(void)
451 #if defined(__UCLIBC__)
452 return g_strdup_printf("uClibc %i.%i.%i", __UCLIBC_MAJOR__
, __UCLIBC_MINOR__
, __UCLIBC_SUBLEVEL__
);
453 #elif defined(__GNU_LIBRARY__)
454 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
456 return g_strdup(_("Unknown"));
463 *\brief operating system
465 static const gchar
*get_operating_system(void)
467 #if HAVE_SYS_UTSNAME_H
468 struct utsname utsbuf
;
470 return g_strdup_printf("%s %s (%s)",
475 return g_strdup(_("Unknown"));
483 *\brief see if the crash dialog is allowed (because some
484 * developers may prefer to run claws-mail under gdb...)
486 static gboolean
is_crash_dialog_allowed(void)
488 return !g_getenv("CLAWS_NO_CRASH");
492 *\brief this handler will probably evolve into
495 static void crash_handler(int sig
)
498 static volatile unsigned long crashed_
= 0;
501 * let's hope argv0 aren't trashed.
502 * both are defined in main.c.
508 * besides guarding entrancy it's probably also better
509 * to mask off signals
511 if (crashed_
) return;
521 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
523 gdk_pointer_ungrab(GDK_CURRENT_TIME
);
524 gdk_keyboard_ungrab(GDK_CURRENT_TIME
);
527 if (0 == (pid
= fork())) {
532 * probably also some other parameters (like GTK+ ones).
533 * also we pass the full startup dir and the real command
534 * line typed in (argv0)
539 sprintf(buf
, "%d,%d,%s", getppid(), sig
, argv0
);
543 if (chdir(claws_get_startup_dir()) != 0)
545 if (setgid(getgid()) != 0)
547 if (setuid(getuid()) != 0 )
551 waitpid(pid
, NULL
, 0);
552 crash_cleanup_exit();
560 *\brief put all the things here we can do before
561 * letting the program die
563 static void crash_cleanup_exit(void)
565 const char *filename
= claws_get_socket_name();
566 claws_unlink(filename
);