Coverity fixes
[claws.git] / src / crash.c
index b0a72fc4d3fe8d4e0a05314c29252302d95c1455..d3c5022af0558494781a01cd7c089d8c6182f720 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2002 by the Sylpheed Claws Team and Hiroyuki Yamamoto
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2002-2012 by the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #      include <config.h>
 #endif
 
+#ifdef CRASH_DIALOG
+
+#include "defs.h"
+
 #include <glib.h>
+#include <glib/gi18n.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 <sys/utsname.h>
 #endif
 
-#if defined(__GNU_LIBRARY__)
+#if defined(__GNU_LIBRARY__) && !defined(__UCLIBC__)
 #      include <gnu/libc-version.h>
 #endif
 
-#include "intl.h"
+#include "main.h"
+#include "claws.h"
 #include "crash.h"
 #include "utils.h"
 #include "filesel.h"
 #include "version.h"
-
-#if 0
-#include "gtkutils.h"
-#include "pixmaps/notice_error.xpm"
-#endif
+#include "prefs_common.h"
+#include "manage_window.h"
 
 /*
- * NOTE 1: the crash dialog is called when sylpheed is not 
+ * NOTE: the crash dialog is called when claws 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 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 gboolean                 crash_create_debugger_file     (void);
-static void             crash_save_crash_log           (GtkButton *, const gchar *);
-
 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 = "thread all apply\nbt full\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 
+ */
+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(_("Claws Mail 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_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_new(const gchar *text, const gchar *debug_output)
+static GtkWidget *crash_dialog_show(const gchar *text, const gchar *debug_output)
 {
        GtkWidget *window1;
        GtkWidget *vbox1;
@@ -93,14 +178,13 @@ 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;
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
 
-       window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       window1 = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "crash");
        gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
-       gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
+       gtk_window_set_title(GTK_WINDOW(window1), _("Claws Mail has crashed"));
        gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
        gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
        gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
@@ -115,13 +199,6 @@ 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.\nPlease file a bug report and include the information below."), text));
        gtk_widget_show(label1);
@@ -137,23 +214,32 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
        gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
-                                      GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
 
-       text1 = gtk_text_new(NULL, NULL);
-       gtk_text_set_editable(GTK_TEXT(text1), FALSE);
+       text1 = gtk_text_view_new();
+       gtk_text_view_set_editable(GTK_TEXT_VIEW(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",
+               "Claws Mail version %s\n"
+               "GTK+ version %d.%d.%d / GLib %d.%d.%d\n"
+               "Locale: %s (charset: %s)\n"
+               "Features:%s\n"
+               "Operating system: %s\n"
+               "C Library: %s\n--\n%s",
                VERSION,
                gtk_major_version, gtk_minor_version, gtk_micro_version,
+               glib_major_version, glib_minor_version, glib_micro_version,
+               conv_get_current_locale(), conv_get_locale_charset_str(),
                get_compiled_in_features(),
                get_operating_system(),
                get_lib_version(),
                debug_output);
 
-       gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text1));
+       gtk_text_buffer_get_start_iter(buffer, &iter);
+       gtk_text_buffer_insert(buffer, &iter, crash_report, -1);
 
        hbuttonbox3 = gtk_hbutton_box_new();
        gtk_widget_show(hbuttonbox3);
@@ -166,25 +252,28 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        button3 = gtk_button_new_with_label(_("Close"));
        gtk_widget_show(button3);
        gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
-       GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
+       gtkut_widget_set_can_default(button3, TRUE);
 
        button4 = gtk_button_new_with_label(_("Save..."));
        gtk_widget_show(button4);
        gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
-       GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
+       gtkut_widget_set_can_default(button4, TRUE);
 
        button5 = gtk_button_new_with_label(_("Create bug report"));
        gtk_widget_show(button5);
        gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
-       GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
+       gtkut_widget_set_can_default(button5, TRUE);
        
-       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);
+
+       MANAGE_WINDOW_SIGNALS_CONNECT(window1);
 
        gtk_widget_show(window1);
 
@@ -192,80 +281,13 @@ 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;
-       extern gchar *startup_dir;
-
-       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);
-       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.
+/*!
+ *\brief       create debugger script file in claws 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)
 {
        gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
        
@@ -273,7 +295,7 @@ static gboolean crash_create_debugger_file(void)
        g_free(filespec);
 }
 
-/*
+/*!
  *\brief       saves crash log to a file
  */
 static void crash_save_crash_log(GtkButton *button, const gchar *text)
@@ -281,19 +303,28 @@ static void crash_save_crash_log(GtkButton *button, const gchar *text)
        time_t timer;
        struct tm *lt;
        char buf[100];
+       struct tm buft;
        gchar *filename;
 
        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))
+       lt = localtime_r(&timer, &buft);
+       strftime(buf, sizeof buf, "claws-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       launches debugger and attaches it to crashed sylpheed
+/*!
+ *\brief       create bug report (goes to Claws Mail bug tracker)      
+ */
+static void crash_create_bug_report(GtkButton *button, const gchar *data)
+{
+       open_uri(BUGZILLA_URI, prefs_common_get_uri_cmd());
+}
+
+/*!
+ *\brief       launches debugger and attaches it to crashed claws
  */
 static void crash_debug(unsigned long crash_pid, 
                        gchar *exe_image,
@@ -302,7 +333,10 @@ static void crash_debug(unsigned long crash_pid,
        int choutput[2];
        pid_t pid;
 
-       pipe(choutput);
+       if (pipe(choutput) == -1) {
+               g_print("can't pipe - error %s", errno);
+               return;
+       }
 
        if (0 == (pid = fork())) {
                char *argp[10];
@@ -313,7 +347,7 @@ static void crash_debug(unsigned long crash_pid,
                setuid(getuid());
 
                /*
-                * setup debugger to attach to crashed sylpheed
+                * setup debugger to attach to crashed claws
                 */
                *argptr++ = "gdb"; 
                *argptr++ = "--nw";
@@ -323,7 +357,7 @@ static void crash_debug(unsigned long crash_pid,
                *argptr++ = "-x";
                *argptr++ = filespec;
                *argptr++ = exe_image;
-               *argptr++ = g_strdup_printf("%d", crash_pid);
+               *argptr++ = g_strdup_printf("%ld", crash_pid);
                *argptr   = NULL;
 
                /*
@@ -333,7 +367,7 @@ static void crash_debug(unsigned long crash_pid,
                dup(choutput[1]);
                close(choutput[0]);
                if (-1 == execvp("gdb", argp)) 
-                       puts("error execvp\n");
+                       g_print("error execvp\n");
        } else {
                char buf[100];
                int r;
@@ -344,7 +378,7 @@ static void crash_debug(unsigned long crash_pid,
                 * make it non blocking
                 */
                if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
-                       puts("set to non blocking failed\n");
+                       g_print("set to non blocking failed\n");
 
                /*
                 * get the output
@@ -369,35 +403,23 @@ static void crash_debug(unsigned long crash_pid,
 
 /***/
 
-/*
+/*!
  *\brief       features
  */
 static const gchar *get_compiled_in_features(void)
 {
        return g_strdup_printf("%s",
-#if HAVE_GDK_IMLIB
-                  " gdk_imlib"
-#endif
-#if HAVE_GDK_PIXBUF
-                  " gdk-pixbuf"
-#endif
-#if USE_THREADS
-                  " gthread"
-#endif
 #if INET6
                   " IPv6"
 #endif
-#if HAVE_LIBCOMPFACE
-                  " libcompface"
-#endif
-#if HAVE_LIBJCONV
-                  " libjconv"
+#if HAVE_ICONV
+                  " iconv"
 #endif
-#if USE_GPGME
-                  " GPGME"
+#if HAVE_LIBCOMPFACE
+                  " compface"
 #endif
-#if USE_SSL
-                  " SSL"
+#if USE_GNUTLS
+                  " GnuTLS"
 #endif
 #if USE_LDAP
                   " LDAP"
@@ -405,20 +427,28 @@ static const gchar *get_compiled_in_features(void)
 #if USE_JPILOT
                   " JPilot"
 #endif
-#if USE_PSPELL
-                  " pspell"
+#if USE_ENCHANT
+                  " GNU/aspell"
+#endif
+#if HAVE_LIBETPAN
+                  " libetpan"
+#endif
+#if HAVE_LIBSM
+                  " libSM"
 #endif
        "");
 }
 
 /***/
 
-/*
+/*!
  *\brief       library version
  */
 static const gchar *get_lib_version(void)
 {
-#if defined(__GNU_LIBRARY__)
+#if defined(__UCLIBC__)
+       return g_strdup_printf("uClibc %i.%i.%i", __UCLIBC_MAJOR__, __UCLIBC_MINOR__, __UCLIBC_SUBLEVEL__);
+#elif defined(__GNU_LIBRARY__)
        return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
 #else
        return g_strdup(_("Unknown"));
@@ -427,7 +457,7 @@ static const gchar *get_lib_version(void)
 
 /***/
 
-/*
+/*!
  *\brief       operating system
  */
 static const gchar *get_operating_system(void)
@@ -447,16 +477,16 @@ static const gchar *get_operating_system(void)
 
 /***/
 
-/*
+/*!
  *\brief       see if the crash dialog is allowed (because some
- *             developers may prefer to run sylpheed under gdb...)
+ *             developers may prefer to run claws-mail under gdb...)
  */
 static gboolean is_crash_dialog_allowed(void)
 {
-       return !getenv("SYLPHEED_NO_CRASH");
+       return !getenv("CLAWS_NO_CRASH");
 }
 
-/*
+/*!
  *\brief       this handler will probably evolve into 
  *             something better.
  */
@@ -466,10 +496,9 @@ static void crash_handler(int sig)
        static volatile unsigned long crashed_ = 0;
 
        /*
-        * let's hope startup_dir and argv0 aren't trashed.
+        * let's hope argv0 aren't trashed.
         * both are defined in main.c.
         */
-       extern gchar *startup_dir;
        extern gchar *argv0;
 
 
@@ -481,6 +510,11 @@ static void crash_handler(int sig)
 
        crashed_++;
 
+#ifdef SIGTERM
+       if (sig == SIGTERM) 
+               clean_quit(NULL);
+#endif
+
        /*
         * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
         */
@@ -488,7 +522,6 @@ static void crash_handler(int sig)
        gdk_keyboard_ungrab(GDK_CURRENT_TIME);
        gdk_flush();
 
-
        if (0 == (pid = fork())) {
                char buf[50];
                char *args[5];
@@ -501,19 +534,31 @@ static void crash_handler(int sig)
                args[0] = argv0; 
                args[1] = "--debug";
                args[2] = "--crash";
-               sprintf(buf, "%ld,%d,%s", getppid(), sig, argv0);
+               sprintf(buf, "%d,%d,%s", getppid(), sig, argv0);
                args[3] = buf;
                args[4] = NULL;
 
-               chdir(startup_dir);
+               chdir(claws_get_startup_dir());
                setgid(getgid());
                setuid(getuid());
                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)
+{
+       const char *filename = claws_get_socket_name();
+       claws_unlink(filename);
+}
+
+#endif