Remove inclusion of sys/socket.h from nntp-thread.c
[claws.git] / src / crash.c
blob475f17928b59f193ee91b8b42c6e65380b444f41
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2019 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/>.
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #ifdef CRASH_DIALOG
25 #include "defs.h"
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
37 #include <errno.h>
38 #include <fcntl.h>
40 #if HAVE_SYS_UTSNAME_H
41 # include <sys/utsname.h>
42 #endif
44 #if defined(__GNU_LIBRARY__) && !defined(__UCLIBC__)
45 # include <gnu/libc-version.h>
46 #endif
48 #include "main.h"
49 #include "claws.h"
50 #include "crash.h"
51 #include "file-utils.h"
52 #include "filesel.h"
53 #include "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
61 * to be possible.
64 /***/
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,
72 gchar *exe_image,
73 GString *debug_output);
74 static gchar *get_compiled_in_features (void);
75 static gchar *get_lib_version (void);
76 static 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);
81 /***/
83 static const gchar *DEBUG_SCRIPT = "thread all apply\nbt full\nkill\nq";
85 /***/
87 /*!
88 *\brief install crash handlers
90 void crash_install_handlers(void)
92 #if CRASH_DIALOG
93 sigset_t mask;
95 if (!is_crash_dialog_allowed()) return;
97 sigemptyset(&mask);
99 #ifdef SIGSEGV
100 signal(SIGSEGV, crash_handler);
101 sigaddset(&mask, SIGSEGV);
102 #endif
104 #ifdef SIGFPE
105 signal(SIGFPE, crash_handler);
106 sigaddset(&mask, SIGFPE);
107 #endif
109 #ifdef SIGILL
110 signal(SIGILL, crash_handler);
111 sigaddset(&mask, SIGILL);
112 #endif
114 #ifdef SIGABRT
115 signal(SIGABRT, crash_handler);
116 sigaddset(&mask, SIGABRT);
117 #endif
119 sigprocmask(SIG_UNBLOCK, &mask, 0);
120 #endif /* CRASH_DIALOG */
123 /***/
126 *\brief crash dialog entry point
128 void crash_main(const char *arg)
130 #if CRASH_DIALOG
131 gchar *text;
132 gchar **tokens;
133 unsigned long pid;
134 GString *output;
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);
153 g_free(text);
154 g_strfreev(tokens);
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)
168 GtkWidget *window1;
169 GtkWidget *vbox1;
170 GtkWidget *hbox1;
171 GtkWidget *label1;
172 GtkWidget *frame1;
173 GtkWidget *scrolledwindow1;
174 GtkWidget *text1;
175 GtkWidget *hbuttonbox3;
176 GtkWidget *hbuttonbox4;
177 GtkWidget *button3;
178 GtkWidget *button4;
179 GtkWidget *button5;
180 gchar *crash_report;
181 GtkTextBuffer *buffer;
182 GtkTextIter iter;
183 gchar *features = get_compiled_in_features();
184 gchar *os = get_operating_system();
185 gchar *lversion = get_lib_version();
187 window1 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "crash");
188 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
189 gtk_window_set_title(GTK_WINDOW(window1), _("Claws Mail has crashed"));
190 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
191 gtk_window_set_type_hint(GTK_WINDOW(window1), GDK_WINDOW_TYPE_HINT_DIALOG);
192 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
193 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
196 vbox1 = gtk_box_new(GTK_ORIENTATION_VERTICAL, 2);
197 gtk_widget_show(vbox1);
198 gtk_container_add(GTK_CONTAINER(window1), vbox1);
200 hbox1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 4);
201 gtk_widget_show(hbox1);
202 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
203 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
205 label1 = gtk_label_new
206 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
207 gtk_widget_show(label1);
208 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
209 gtk_label_set_xalign(GTK_LABEL(label1), 0.0);
211 frame1 = gtk_frame_new(_("Debug log"));
212 gtk_widget_show(frame1);
213 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
215 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
216 gtk_widget_show(scrolledwindow1);
217 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
218 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
219 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
220 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
222 text1 = gtk_text_view_new();
223 gtk_text_view_set_editable(GTK_TEXT_VIEW(text1), FALSE);
224 gtk_widget_show(text1);
225 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
227 crash_report = g_strdup_printf(
228 "Claws Mail version %s\n"
229 "GTK version %d.%d.%d / GLib %d.%d.%d\n"
230 "Locale: %s (charset: %s)\n"
231 "Features:%s\n"
232 "Operating system: %s\n"
233 "C Library: %s\n--\n%s",
234 VERSION,
235 gtk_major_version, gtk_minor_version, gtk_micro_version,
236 glib_major_version, glib_minor_version, glib_micro_version,
237 conv_get_current_locale(), conv_get_locale_charset_str(),
238 features, os, lversion, debug_output);
239 g_free(features);
240 g_free(os);
241 g_free(lversion);
243 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
244 gtk_text_buffer_get_start_iter(buffer, &iter);
245 gtk_text_buffer_insert(buffer, &iter, crash_report, -1);
247 hbuttonbox3 = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
248 gtk_widget_show(hbuttonbox3);
249 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
251 hbuttonbox4 = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
252 gtk_widget_show(hbuttonbox4);
253 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
255 button3 = gtk_button_new_with_label(_("Close"));
256 gtk_widget_show(button3);
257 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
258 gtk_widget_set_can_default(button3, TRUE);
260 button4 = gtk_button_new_with_label(_("Save..."));
261 gtk_widget_show(button4);
262 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
263 gtk_widget_set_can_default(button4, TRUE);
265 button5 = gtk_button_new_with_label(_("Create bug report"));
266 gtk_widget_show(button5);
267 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
268 gtk_widget_set_can_default(button5, TRUE);
270 g_signal_connect(G_OBJECT(window1), "delete_event",
271 G_CALLBACK(gtk_main_quit), NULL);
272 g_signal_connect(G_OBJECT(button3), "clicked",
273 G_CALLBACK(gtk_main_quit), NULL);
274 g_signal_connect(G_OBJECT(button4), "clicked",
275 G_CALLBACK(crash_save_crash_log), crash_report);
276 g_signal_connect(G_OBJECT(button5), "clicked",
277 G_CALLBACK(crash_create_bug_report), NULL);
279 MANAGE_WINDOW_SIGNALS_CONNECT(window1);
281 gtk_widget_show(window1);
283 gtk_main();
284 return window1;
289 *\brief create debugger script file in claws directory.
290 * all the other options (creating temp files) looked too
291 * convoluted.
293 static void crash_create_debugger_file(void)
295 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
297 str_write_to_file(DEBUG_SCRIPT, filespec, TRUE);
298 g_free(filespec);
302 *\brief saves crash log to a file
304 static void crash_save_crash_log(GtkButton *button, const gchar *text)
306 time_t timer;
307 struct tm *lt;
308 char buf[100];
309 struct tm buft;
310 gchar *filename;
312 timer = time(NULL);
313 lt = localtime_r(&timer, &buft);
314 strftime(buf, sizeof buf, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
315 if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
316 && *filename)
317 str_write_to_file(text, filename, TRUE);
318 g_free(filename);
322 *\brief create bug report (goes to Claws Mail bug tracker)
324 static void crash_create_bug_report(GtkButton *button, const gchar *data)
326 open_uri(BUGZILLA_URI, prefs_common_get_uri_cmd());
330 *\brief launches debugger and attaches it to crashed claws
332 static void crash_debug(unsigned long crash_pid,
333 gchar *exe_image,
334 GString *debug_output)
336 int choutput[2];
337 pid_t pid;
339 if (pipe(choutput) == -1) {
340 g_print("can't pipe - error %s", g_strerror(errno));
341 return;
344 if (0 == (pid = fork())) {
345 char *argp[10];
346 char **argptr = argp;
347 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
349 if (setgid(getgid()) != 0)
350 perror("setgid");
351 if (setuid(getuid()) != 0)
352 perror("setuid");
355 * setup debugger to attach to crashed claws
357 *argptr++ = "gdb";
358 *argptr++ = "--nw";
359 *argptr++ = "--nx";
360 *argptr++ = "--quiet";
361 *argptr++ = "--batch";
362 *argptr++ = "-x";
363 *argptr++ = filespec;
364 *argptr++ = exe_image;
365 *argptr++ = g_strdup_printf("%ld", crash_pid);
366 *argptr = NULL;
369 * redirect output to write end of pipe
371 close(1);
372 if (dup(choutput[1]) < 0)
373 perror("dup");
374 close(choutput[0]);
375 if (-1 == execvp("gdb", argp))
376 perror("execvp");
377 } else {
378 char buf[100];
379 int r;
381 waitpid(pid, NULL, 0);
384 * make it non blocking
386 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
387 g_print("set to non blocking failed\n");
390 * get the output
392 do {
393 r = read(choutput[0], buf, sizeof buf - 1);
394 if (r > 0) {
395 buf[r] = 0;
396 g_string_append(debug_output, buf);
398 } while (r > 0);
400 close(choutput[0]);
401 close(choutput[1]);
404 * kill the process we attached to
406 kill(crash_pid, SIGCONT);
410 /***/
413 *\brief features
415 static gchar *get_compiled_in_features(void)
417 return g_strdup_printf("%s",
418 #if INET6
419 " IPv6"
420 #endif
421 #if HAVE_ICONV
422 " iconv"
423 #endif
424 #if HAVE_LIBCOMPFACE
425 " compface"
426 #endif
427 #if USE_GNUTLS
428 " GnuTLS"
429 #endif
430 #if USE_LDAP
431 " LDAP"
432 #endif
433 #if USE_JPILOT
434 " JPilot"
435 #endif
436 #if USE_ENCHANT
437 " GNU/aspell"
438 #endif
439 #if HAVE_LIBETPAN
440 " libetpan"
441 #endif
442 #if HAVE_LIBSM
443 " libSM"
444 #endif
445 "");
448 /***/
451 *\brief library version
453 static gchar *get_lib_version(void)
455 #if defined(__UCLIBC__)
456 return g_strdup_printf("uClibc %i.%i.%i", __UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__);
457 #elif defined(__GNU_LIBRARY__)
458 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
459 #else
460 return g_strdup(_("Unknown"));
461 #endif
464 /***/
467 *\brief operating system
469 static gchar *get_operating_system(void)
471 #if HAVE_SYS_UTSNAME_H
472 struct utsname utsbuf;
473 uname(&utsbuf);
474 return g_strdup_printf("%s %s (%s)",
475 utsbuf.sysname,
476 utsbuf.release,
477 utsbuf.machine);
478 #else
479 return g_strdup(_("Unknown"));
481 #endif
484 /***/
487 *\brief see if the crash dialog is allowed (because some
488 * developers may prefer to run Claws Mail under gdb...)
490 static gboolean is_crash_dialog_allowed(void)
492 return !g_getenv("CLAWS_NO_CRASH");
496 *\brief this handler will probably evolve into
497 * something better.
499 static void crash_handler(int sig)
501 pid_t pid;
502 static volatile unsigned long crashed_ = 0;
505 * let's hope argv0 aren't trashed.
506 * both are defined in main.c.
508 extern gchar *argv0;
512 * besides guarding entrancy it's probably also better
513 * to mask off signals
515 if (crashed_) return;
517 crashed_++;
519 #ifdef SIGTERM
520 if (sig == SIGTERM)
521 clean_quit(NULL);
522 #endif
525 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
527 gdk_pointer_ungrab(GDK_CURRENT_TIME);
528 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
529 gdk_flush();
531 if (0 == (pid = fork())) {
532 char buf[50];
533 char *args[5];
536 * probably also some other parameters (like GTK ones).
537 * also we pass the full startup dir and the real command
538 * line typed in (argv0)
540 args[0] = argv0;
541 args[1] = "--debug";
542 args[2] = "--crash";
543 sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
544 args[3] = buf;
545 args[4] = NULL;
547 if (chdir(claws_get_startup_dir()) != 0)
548 FILE_OP_ERROR(claws_get_startup_dir(), "chdir");
549 if (setgid(getgid()) != 0)
550 perror("setgid");
551 if (setuid(getuid()) != 0 )
552 perror("setuid");
553 execvp(argv0, args);
554 perror("execvp");
555 } else {
556 waitpid(pid, NULL, 0);
557 crash_cleanup_exit();
558 _exit(253);
561 _exit(253);
565 *\brief put all the things here we can do before
566 * letting the program die
568 static void crash_cleanup_exit(void)
570 const char *filename = claws_get_socket_name();
571 claws_unlink(filename);
574 #endif