untwist file selection logic - be explicit
[claws.git] / src / crash.c
index c1f51e37bd6448adf3491e1d8168c3f676818796..ec91e910fb80637a745787b56431d42b00204366 100644 (file)
 #      include <config.h>
 #endif
 
+#ifdef CRASH_DIALOG
+
+#include "defs.h"
+
 #include <glib.h>
 #include <gtk/gtk.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
 #include <time.h>
+#include <sys/types.h>
+#include <sys/wait.h>
 
 #include <errno.h>
 #include <fcntl.h>
 #      include <gnu/libc-version.h>
 #endif
 
+#ifdef SIGTERM
+#include "main.h"
+#endif
 #include "intl.h"
 #include "crash.h"
 #include "utils.h"
-#include "prefs.h"
 #include "filesel.h"
 #include "version.h"
-
-#if 0
-#include "gtkutils.h"
-#include "pixmaps/notice_error.xpm"
-#endif
+#include "prefs_common.h"
 
 /*
- * NOTE 1: the crash dialog is called when sylpheed is not 
+ * NOTE: the crash dialog is called when sylpheed is not 
  * initialized, so do not assume settings are available.
  * for example, loading / creating pixmaps seems not 
  * to be possible.
  */
 
-static void             crash_handler                  (int sig);
-static gboolean                 is_crash_dialog_allowed        (void);
-static void             crash_debug                    (unsigned long crash_pid, GString *string);
-static gboolean                 crash_create_debugger_file     (void);
-static void             crash_save_crash_log           (GtkButton *, const gchar *);
+/***/
 
+static GtkWidget       *crash_dialog_show              (const gchar *text, 
+                                                        const gchar *debug_output);
+static void             crash_create_debugger_file     (void);
+static void             crash_save_crash_log           (GtkButton *, const gchar *);
+static void             crash_create_bug_report        (GtkButton *, const gchar *);
+static void             crash_debug                    (unsigned long crash_pid, 
+                                                        gchar   *exe_image,
+                                                        GString *debug_output);
 static const gchar     *get_compiled_in_features       (void);
 static const gchar     *get_lib_version                (void);
 static const gchar     *get_operating_system           (void);
+static gboolean                 is_crash_dialog_allowed        (void);
+static void             crash_handler                  (int sig);
+static void             crash_cleanup_exit             (void);
+
+/***/
 
+static const gchar *DEBUG_SCRIPT = "bt\nkill\nq";
 
 /***/
 
-static const gchar *DEBUG_SCRIPT = "bt full\nq";
+/*!
+ *\brief       install crash handlers
+ */
+void crash_install_handlers(void)
+{
+#if CRASH_DIALOG 
+       sigset_t mask;
+
+       if (!is_crash_dialog_allowed()) return;
+
+       sigemptyset(&mask);
+
+#ifdef SIGSEGV
+       signal(SIGSEGV, crash_handler);
+       sigaddset(&mask, SIGSEGV);
+#endif
+       
+#ifdef SIGFPE
+       signal(SIGFPE, crash_handler);
+       sigaddset(&mask, SIGFPE);
+#endif
+
+#ifdef SIGILL
+       signal(SIGILL, crash_handler);
+       sigaddset(&mask, SIGILL);
+#endif
+
+#ifdef SIGABRT
+       signal(SIGABRT, crash_handler);
+       sigaddset(&mask, SIGABRT);
+#endif
+
+       sigprocmask(SIG_UNBLOCK, &mask, 0);
+#endif /* CRASH_DIALOG */      
+}
 
 /***/
 
-/*
- *\brief       (can't get pixmap working, so discarding it)
+/*!
+ *\brief       crash dialog entry point 
  */
-static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
+void crash_main(const char *arg) 
+{
+#if CRASH_DIALOG 
+       gchar *text;
+       gchar **tokens;
+       unsigned long pid;
+       GString *output;
+
+       crash_create_debugger_file();
+       tokens = g_strsplit(arg, ",", 0);
+
+       pid = atol(tokens[0]);
+       text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
+                              pid, atol(tokens[1]));
+
+       output = g_string_new("");     
+       crash_debug(pid, tokens[2], output);
+
+       /*
+        * try to get the settings
+        */
+       prefs_common_init();
+       prefs_common_read_config();
+
+       crash_dialog_show(text, output->str);
+       g_string_free(output, TRUE);
+       g_free(text);
+       g_strfreev(tokens);
+#endif /* CRASH_DIALOG */      
+}
+
+/*!
+ *\brief       show crash dialog
+ *
+ *\param       text Description
+ *\param       debug_output Output text by gdb
+ *
+ *\return      GtkWidget * Dialog widget
+ */
+static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
 {
        GtkWidget *window1;
        GtkWidget *vbox1;
@@ -92,9 +179,6 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        GtkWidget *button3;
        GtkWidget *button4;
        GtkWidget *button5;
-       GtkWidget *pixwid;
-       GdkPixmap *pix;
-       GdkBitmap *msk;
        gchar     *crash_report;
 
        window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
@@ -114,15 +198,8 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
        gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
 
-#if 0
-       PIXMAP_CREATE(window1, pix, msk, notice_error_xpm);
-       pixwid = gtk_pixmap_new(pix, msk);
-       gtk_widget_show(pixwid);
-       gtk_box_pack_start(GTK_BOX(hbox1), pixwid, TRUE, TRUE, 0);
-#endif 
-
        label1 = gtk_label_new
-           (g_strdup_printf(_("%s.\n Please file a bug report and include the information below."), text));
+           (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
        gtk_widget_show(label1);
        gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
        gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
@@ -139,12 +216,12 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
                                       GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
 
        text1 = gtk_text_new(NULL, NULL);
-       gtk_text_set_editable(GTK_TEXT(text1), FALSE);
+       gtk_text_set_editable((text1), FALSE);
        gtk_widget_show(text1);
        gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
        
        crash_report = g_strdup_printf(
-               _("Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s"),
+               "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
                VERSION,
                gtk_major_version, gtk_minor_version, gtk_micro_version,
                get_compiled_in_features(),
@@ -152,7 +229,7 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
                get_lib_version(),
                debug_output);
 
-       gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
+       gtk_text_insert((text1), NULL, NULL, NULL, crash_report, -1);
 
        hbuttonbox3 = gtk_hbutton_box_new();
        gtk_widget_show(hbuttonbox3);
@@ -177,13 +254,14 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
        GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
        
-       gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
-                          GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
-       gtk_signal_connect(GTK_OBJECT(button3),   "clicked",
-                          GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
-       gtk_signal_connect(GTK_OBJECT(button4), "clicked",
-                          GTK_SIGNAL_FUNC(crash_save_crash_log),
-                          crash_report);
+       g_signal_connect(G_OBJECT(window1), "delete_event",
+                        G_CALLBACK(gtk_main_quit), NULL);
+       g_signal_connect(G_OBJECT(button3),   "clicked",
+                        G_CALLBACK(gtk_main_quit), NULL);
+       g_signal_connect(G_OBJECT(button4), "clicked",
+                        G_CALLBACK(crash_save_crash_log), crash_report);
+       g_signal_connect(G_OBJECT(button5), "clicked",
+                        G_CALLBACK(crash_create_bug_report), NULL);
 
        gtk_widget_show(window1);
 
@@ -191,91 +269,21 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        return window1;
 }
 
-/***/
 
-/*
- *\brief       install crash handlers
- */
-void crash_install_handlers(void)
-{
-#if HAVE_GDB
-       sigset_t mask;
-
-       if (!is_crash_dialog_allowed()) return;
-
-       sigemptyset(&mask);
-
-#ifdef SIGSEGV
-       signal(SIGSEGV, crash_handler);
-       sigaddset(&mask, SIGSEGV);
-#endif
-       
-#ifdef SIGFPE
-       signal(SIGFPE, crash_handler);
-       sigaddset(&mask, SIGFPE);
-#endif
-
-#ifdef SIGILL
-       signal(SIGILL, crash_handler);
-       sigaddset(&mask, SIGILL);
-#endif
-
-#ifdef SIGABRT
-       signal(SIGABRT, crash_handler);
-       sigaddset(&mask, SIGABRT);
-#endif
-
-       sigprocmask(SIG_UNBLOCK, &mask, 0);
-#endif /* HAVE_GDB */  
-}
-
-/***/
-
-/*
- *\brief       crash dialog entry point 
- */
-void crash_main(const char *arg) 
-{
-#if HAVE_GDB
-       gchar *text;
-       gchar **tokens;
-       unsigned long pid;
-       GString *output;
-
-       crash_create_debugger_file();
-       tokens = g_strsplit(arg, ",", 0);
-
-       pid = atol(tokens[0]);
-       text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
-                              pid, atol(tokens[1]));
-
-       output = g_string_new("");     
-       crash_debug(pid, output);
-       crash_dialog_new(text, output->str);
-       g_string_free(output, TRUE);
-       g_free(text);
-       g_strfreev(tokens);
-#endif /* HAVE_GDB */  
-}
-
-/*
+/*!
  *\brief       create debugger script file in sylpheed directory.
  *             all the other options (creating temp files) looked too 
  *             convoluted.
  */
-static gboolean crash_create_debugger_file(void)
+static void crash_create_debugger_file(void)
 {
-       PrefFile *pf;
        gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
-
-       pf = prefs_write_open(filespec);
+       
+       str_write_to_file(DEBUG_SCRIPT, filespec);
        g_free(filespec);
-       if (pf) 
-               fprintf(pf->fp, DEBUG_SCRIPT);
-       prefs_write_close(pf);  
 }
 
-/*
+/*!
  *\brief       saves crash log to a file
  */
 static void crash_save_crash_log(GtkButton *button, const gchar *text)
@@ -287,17 +295,27 @@ static void crash_save_crash_log(GtkButton *button, const gchar *text)
 
        timer = time(NULL);
        lt = localtime(&timer);
-       strftime(buf, sizeof buf, "sylpheed-crash-log-%y-%m-%d-%H-%M-%S.txt", lt);
-       if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
+       strftime(buf, sizeof buf, "sylpheed-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
+       if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
        &&  *filename)
                str_write_to_file(text, filename);
        g_free(filename);       
 }
 
-/*
+/*!
+ *\brief       create bug report (goes to Sylpheed Claws bug tracker)  
+ */
+static void crash_create_bug_report(GtkButton *button, const gchar *data)
+{
+       open_uri(CLAWS_BUGZILLA_URI, prefs_common.uri_cmd);
+}
+
+/*!
  *\brief       launches debugger and attaches it to crashed sylpheed
  */
-static void crash_debug(unsigned long crash_pid, GString *string)
+static void crash_debug(unsigned long crash_pid, 
+                       gchar *exe_image,
+                       GString *debug_output)
 {
        int choutput[2];
        pid_t pid;
@@ -305,7 +323,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
        pipe(choutput);
 
        if (0 == (pid = fork())) {
-               char *argp[9];
+               char *argp[10];
                char **argptr = argp;
                gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
 
@@ -315,14 +333,15 @@ static void crash_debug(unsigned long crash_pid, GString *string)
                /*
                 * setup debugger to attach to crashed sylpheed
                 */
+               *argptr++ = "gdb"; 
                *argptr++ = "--nw";
                *argptr++ = "--nx";
                *argptr++ = "--quiet";
                *argptr++ = "--batch";
-               *argptr++ = "--command";
-               *argptr++ = g_strdup_printf("%s", filespec);
-               *argptr++ = "sylpheed"; /* program file name */
-               *argptr++ = g_strdup_printf("%d", crash_pid);
+               *argptr++ = "-x";
+               *argptr++ = filespec;
+               *argptr++ = exe_image;
+               *argptr++ = g_strdup_printf("%ld", crash_pid);
                *argptr   = NULL;
 
                /*
@@ -352,7 +371,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
                        r = read(choutput[0], buf, sizeof buf - 1);
                        if (r > 0) {
                                buf[r] = 0;
-                               g_string_append(string, buf);
+                               g_string_append(debug_output, buf);
                        }
                } while (r > 0);
                
@@ -368,7 +387,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
 
 /***/
 
-/*
+/*!
  *\brief       features
  */
 static const gchar *get_compiled_in_features(void)
@@ -395,8 +414,8 @@ static const gchar *get_compiled_in_features(void)
 #if USE_GPGME
                   " GPGME"
 #endif
-#if USE_SSL
-                  " SSL"
+#if USE_OPENSSL
+                  " OpenSSL"
 #endif
 #if USE_LDAP
                   " LDAP"
@@ -404,15 +423,15 @@ static const gchar *get_compiled_in_features(void)
 #if USE_JPILOT
                   " JPilot"
 #endif
-#if USE_PSPELL
-                  " pspell"
+#if USE_ASPELL
+                  " GNU/aspell"
 #endif
        "");
 }
 
 /***/
 
-/*
+/*!
  *\brief       library version
  */
 static const gchar *get_lib_version(void)
@@ -426,7 +445,7 @@ static const gchar *get_lib_version(void)
 
 /***/
 
-/*
+/*!
  *\brief       operating system
  */
 static const gchar *get_operating_system(void)
@@ -446,9 +465,8 @@ static const gchar *get_operating_system(void)
 
 /***/
 
-/*
- *\brief       checks KDE, GNOME and Sylpheed specific variables
- *             to see if the crash dialog is allowed (because some
+/*!
+ *\brief       see if the crash dialog is allowed (because some
  *             developers may prefer to run sylpheed under gdb...)
  */
 static gboolean is_crash_dialog_allowed(void)
@@ -456,7 +474,7 @@ static gboolean is_crash_dialog_allowed(void)
        return !getenv("SYLPHEED_NO_CRASH");
 }
 
-/*
+/*!
  *\brief       this handler will probably evolve into 
  *             something better.
  */
@@ -465,47 +483,71 @@ static void crash_handler(int sig)
        pid_t pid;
        static volatile unsigned long crashed_ = 0;
 
+       /*
+        * let's hope argv0 aren't trashed.
+        * both are defined in main.c.
+        */
+       extern gchar *argv0;
+
+
        /*
         * besides guarding entrancy it's probably also better 
         * to mask off signals
         */
-       if (crashed_) 
-               return;
+       if (crashed_) return;
 
        crashed_++;
 
+#ifdef SIGTERM
+       if (sig == SIGTERM) 
+               clean_quit(NULL);
+#endif
+
        /*
         * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
         */
        gdk_pointer_ungrab(GDK_CURRENT_TIME);
        gdk_keyboard_ungrab(GDK_CURRENT_TIME);
        gdk_flush();
-        
+
        if (0 == (pid = fork())) {
                char buf[50];
-               char *args[4];
+               char *args[5];
        
                /*
                 * probably also some other parameters (like GTK+ ones).
+                * also we pass the full startup dir and the real command
+                * line typed in (argv0)
                 */
-               args[0] = "--debug";
-               args[1] = "--crash";
-               sprintf(buf, "%ld,%d", getppid(), sig);
-               args[2] = buf;
-               args[3] = NULL;
-
+               args[0] = argv0; 
+               args[1] = "--debug";
+               args[2] = "--crash";
+               sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
+               args[3] = buf;
+               args[4] = NULL;
+
+               chdir(sylpheed_get_startup_dir());
                setgid(getgid());
                setuid(getuid());
-#if 0 
-               execvp("/alfons/Projects/sylpheed-claws/src/sylpheed", args);
-#else
-               execvp("sylpheed", args);
-#endif
+               execvp(argv0, args);
        } else {
                waitpid(pid, NULL, 0);
+               crash_cleanup_exit();
                _exit(253);
        }
 
        _exit(253);
 }
 
+/*!
+ *\brief       put all the things here we can do before
+ *             letting the program die
+ */
+static void crash_cleanup_exit(void)
+{
+       extern gchar *get_socket_name(void);
+       const char *filename = get_socket_name();
+       unlink(filename);
+}
+
+#endif