2f6638baf48fd4f533569c2bd51b4398725bc3fb
[claws.git] / src / crash.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002 by the Sylpheed Claws Team and  Hiroyuki Yamamoto
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <glib.h>
25 #include <gtk/gtk.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <signal.h>
29
30 #include <errno.h>
31 #include <fcntl.h>
32
33 #include "intl.h"
34 #include "crash.h"
35 #include "utils.h"
36 #include "prefs.h"
37
38 static void crash_handler                       (int sig);
39 static gboolean is_crash_dialog_allowed         (void);
40 static void crash_debug                         (unsigned long crash_pid, GString *string);
41 static gboolean crash_create_debugger_file      (void);
42
43 static const gchar *DEBUG_SCRIPT = "bt full\nq";
44
45 /***/
46
47 GtkWidget *crash_dialog_new (const gchar *text, const gchar *debug_output)
48 {
49         GtkWidget *window1;
50         GtkWidget *vbox1;
51         GtkWidget *label1;
52         GtkWidget *scrolledwindow1;
53         GtkWidget *text1;
54         GtkWidget *hbuttonbox1;
55         GtkWidget *close;
56         GtkWidget *button3;
57
58         window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
59         gtk_object_set_data(GTK_OBJECT(window1), "window1", window1);
60         gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed Claws - It Bites!"));
61
62         vbox1 = gtk_vbox_new(FALSE, 0);
63         gtk_widget_show(vbox1);
64         gtk_container_add(GTK_CONTAINER(window1), vbox1);
65         gtk_container_set_border_width(GTK_CONTAINER (vbox1), 1);
66
67         label1 = gtk_label_new(text);
68         gtk_widget_show(label1);
69         gtk_box_pack_start(GTK_BOX(vbox1), label1, FALSE, FALSE, 0);
70
71         scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
72         gtk_widget_show(scrolledwindow1);
73         gtk_box_pack_start(GTK_BOX(vbox1), scrolledwindow1, TRUE, TRUE, 0);
74         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1), 
75                                        GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
76
77         text1 = gtk_text_new(NULL, NULL);
78         gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, 
79                         debug_output, strlen(debug_output));
80         gtk_widget_show(text1);
81         gtk_container_add(GTK_CONTAINER (scrolledwindow1), text1);
82         
83         hbuttonbox1 = gtk_hbutton_box_new();
84         gtk_widget_show(hbuttonbox1);
85         gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, TRUE, 0);
86         gtk_button_box_set_child_ipadding(GTK_BUTTON_BOX (hbuttonbox1), 5, -1);
87
88         close = gtk_button_new_with_label(_("Close"));
89         gtk_widget_show(close);
90         gtk_container_add(GTK_CONTAINER(hbuttonbox1), close);
91         GTK_WIDGET_SET_FLAGS(close, GTK_CAN_DEFAULT);
92
93         button3 = gtk_button_new_with_label(_("Save..."));
94         gtk_widget_show(button3);
95         gtk_container_add(GTK_CONTAINER(hbuttonbox1), button3);
96         GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
97
98         gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
99                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
100         gtk_signal_connect(GTK_OBJECT(close),   "clicked",
101                            GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
102
103         gtk_widget_show(window1);
104         gtk_main();
105         return window1;
106 }
107
108 /***/
109
110 void crash_install_handlers(void)
111 {
112 #if HAVE_GDB
113         sigset_t mask;
114
115         if (!is_crash_dialog_allowed()) return;
116
117         sigemptyset(&mask);
118
119 #ifdef SIGSEGV
120         signal(SIGSEGV, crash_handler);
121         sigaddset(&mask, SIGSEGV);
122 #endif
123         
124 #ifdef SIGFPE
125         signal(SIGFPE, crash_handler);
126         sigaddset(&mask, SIGFPE);
127 #endif
128
129 #ifdef SIGILL
130         signal(SIGILL, crash_handler);
131         sigaddset(&mask, SIGILL);
132 #endif
133
134 #ifdef SIGABRT
135         signal(SIGABRT, crash_handler);
136         sigaddset(&mask, SIGABRT);
137 #endif
138
139         sigprocmask(SIG_UNBLOCK, &mask, 0);
140 #endif /* HAVE_GDB */   
141 }
142
143 /***/
144
145 /*
146  *\brief        crash dialog entry point 
147  */
148 void crash_main(const char *arg) 
149 {
150 #if HAVE_GDB
151         gchar *text;
152         gchar **tokens;
153         unsigned long pid;
154         GString *output;
155
156         crash_create_debugger_file();
157         tokens = g_strsplit(arg, ",", 0);
158
159         pid = atol(tokens[0]);
160         text = g_strdup_printf("Sylpheed process (%lx) received signal %ld",
161                                pid, atol(tokens[1]));
162
163         output = g_string_new("DEBUG LOG\n");     
164         crash_debug(pid, output);
165         crash_dialog_new(text, output->str);
166         g_string_free(output, TRUE);
167         g_free(text);
168         g_strfreev(tokens);
169 #endif /* HAVE_GDB */   
170 }
171
172 /*
173  *\brief        create debugger script file in sylpheed directory.
174  *              all the other options (creating temp files) looked too 
175  *              convoluted.
176  */
177 static gboolean crash_create_debugger_file(void)
178 {
179         PrefFile *pf;
180         gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
181
182         pf = prefs_write_open(filespec);
183         g_free(filespec);
184         if (pf) 
185                 fprintf(pf->fp, DEBUG_SCRIPT);
186         prefs_write_close(pf);  
187 }
188
189 /*
190  *\brief        launches debugger and attaches it to crashed sylpheed
191  */
192 static void crash_debug(unsigned long crash_pid, GString *string)
193 {
194         int choutput[2];
195         pid_t pid;
196
197         pipe(choutput);
198
199         if (0 == (pid = fork())) {
200                 char *argp[9];
201                 char **argptr = argp;
202                 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
203
204                 setgid(getgid());
205                 setuid(getuid());
206
207                 /*
208                  * setup debugger to attach to crashed sylpheed
209                  */
210                 *argptr++ = "--nw";
211                 *argptr++ = "--nx";
212                 *argptr++ = "--quiet";
213                 *argptr++ = "--batch";
214                 *argptr++ = "--command";
215                 *argptr++ = g_strdup_printf("%s", filespec);
216                 *argptr++ = "sylpheed"; /* program file name */
217                 *argptr++ = g_strdup_printf("%d", crash_pid);
218                 *argptr   = NULL;
219
220                 /*
221                  * redirect output to write end of pipe
222                  */
223                 close(1);
224                 dup(choutput[1]);
225                 close(choutput[0]);
226                 if (-1 == execvp("gdb", argp)) 
227                         puts("error execvp\n");
228         } else {
229                 char buf[100];
230                 int r;
231         
232                 waitpid(pid, NULL, 0);
233
234                 /*
235                  * make it non blocking
236                  */
237                 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
238                         puts("set to non blocking failed\n");
239
240                 /*
241                  * get the output
242                  */
243                 do {
244                         r = read(choutput[0], buf, sizeof buf - 1);
245                         if (r > 0) {
246                                 buf[r] = 0;
247                                 g_string_append(string, buf);
248                         }
249                 } while (r > 0);
250                 
251                 close(choutput[0]);
252                 close(choutput[1]);
253                 
254                 /*
255                  * kill the process we attached to
256                  */
257                 kill(crash_pid, SIGCONT); 
258         }
259 }
260
261 /*
262  *\brief        checks KDE, GNOME and Sylpheed specific variables
263  *              to see if the crash dialog is allowed (because some
264  *              developers may prefer to run sylpheed under gdb...)
265  */
266 static gboolean is_crash_dialog_allowed(void)
267 {
268         return getenv("KDE_DEBUG") || 
269                !getenv("GNOME_DISABLE_CRASH_DIALOG") ||
270                !getenv("SYLPHEED_NO_CRASH");
271 }
272
273 /*
274  *\brief        this handler will probably evolve into 
275  *              something better.
276  */
277 static void crash_handler(int sig)
278 {
279         pid_t pid;
280         static volatile unsigned long crashed_ = 0;
281
282         /*
283          * besides guarding entrancy it's probably also better 
284          * to mask off signals
285          */
286         if (crashed_) 
287                 return;
288
289         crashed_++;
290
291         /*
292          * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
293          */
294         gdk_pointer_ungrab(GDK_CURRENT_TIME);
295         gdk_keyboard_ungrab(GDK_CURRENT_TIME);
296         gdk_flush();
297          
298         if (0 == (pid = fork())) {
299                 char buf[50];
300                 char *args[4];
301         
302                 /*
303                  * probably also some other parameters (like GTK+ ones).
304                  */
305                 args[0] = "--debug";
306                 args[1] = "--crash";
307                 sprintf(buf, "%ld,%d", getppid(), sig);
308                 args[2] = buf;
309                 args[3] = NULL;
310
311                 setgid(getgid());
312                 setuid(getuid());
313 #if 0           
314                 execvp("/alfons/Projects/sylpheed-claws/src/sylpheed", args);
315 #else
316                 execvp("sylpheed", args);
317 #endif
318         } else {
319                 waitpid(pid, NULL, 0);
320                 _exit(253);
321         }
322
323         _exit(253);
324 }
325