summaryrefslogtreecommitdiff
path: root/common/gdm-signal-handler.c
blob: d389771a06ca74054ed8bf4a04b08e14c6fb3715 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006 Red Hat, Inc.
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 *
 * 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
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 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.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#if HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#include <syslog.h>
#include <sys/wait.h>
#include <sys/stat.h>

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>

#include "gdm-signal-handler.h"

#define GDM_SIGNAL_HANDLER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GDM_TYPE_SIGNAL_HANDLER, GdmSignalHandlerPrivate))

typedef struct {
        int                  signal_number;
        GdmSignalHandlerFunc func;
        gpointer             data;
        guint                id;
} CallbackData;

struct GdmSignalHandlerPrivate
{
        GHashTable    *lookup;
        GHashTable    *id_lookup;
        GHashTable    *action_lookup;
        guint          next_id;
        GDestroyNotify fatal_func;
        gpointer       fatal_data;
};

static void     gdm_signal_handler_class_init   (GdmSignalHandlerClass *klass);
static void     gdm_signal_handler_init         (GdmSignalHandler      *signal_handler);
static void     gdm_signal_handler_finalize     (GObject               *object);

static gpointer signal_handler_object = NULL;
static int      signal_pipes[2];
static int      signals_blocked = 0;
static sigset_t signals_block_mask;
static sigset_t signals_oldmask;

G_DEFINE_TYPE (GdmSignalHandler, gdm_signal_handler, G_TYPE_OBJECT)

static void
block_signals_push (void)
{
        signals_blocked++;

        if (signals_blocked == 1) {
                /* Set signal mask */
                sigemptyset (&signals_block_mask);
                sigfillset (&signals_block_mask);
                sigprocmask (SIG_BLOCK, &signals_block_mask, &signals_oldmask);
        }
}

static void
block_signals_pop (void)
{
        signals_blocked--;

        if (signals_blocked == 0) {
                /* Set signal mask */
                sigprocmask (SIG_SETMASK, &signals_oldmask, NULL);
        }
}

static gboolean
signal_io_watch (GIOChannel       *ioc,
                 GIOCondition      condition,
                 GdmSignalHandler *handler)
{
        char     buf[256];
        gboolean is_fatal;
        gsize    bytes_read;
        int      i;

        block_signals_push ();

        g_io_channel_read_chars (ioc, buf, sizeof (buf), &bytes_read, NULL);

        is_fatal = FALSE;

        for (i = 0; i < bytes_read; i++) {
                int     signum;
                GSList *handlers;
                GSList *l;

                signum = (gint32)buf[i];

                g_debug ("GdmSignalHandler: handling signal %d", signum);
                handlers = g_hash_table_lookup (handler->priv->lookup, GINT_TO_POINTER (signum));

                g_debug ("GdmSignalHandler: Found %u callbacks", g_slist_length (handlers));
                for (l = handlers; l != NULL; l = l->next) {
                        gboolean      res;
                        CallbackData *data;

                        data = g_hash_table_lookup (handler->priv->id_lookup, l->data);
                        if (data != NULL) {
                                if (data->func != NULL) {
                                        g_debug ("GdmSignalHandler: running %d handler: %p", signum, data->func);
                                        res = data->func (signum, data->data);
                                        if (! res) {
                                                is_fatal = TRUE;
                                        }
                                }
                        }
                }
        }

        block_signals_pop ();

        if (is_fatal) {
                if (handler->priv->fatal_func != NULL) {
                        g_debug ("GdmSignalHandler: Caught termination signal - calling fatal func");
                        handler->priv->fatal_func (handler->priv->fatal_data);
                } else {
                        g_debug ("GdmSignalHandler: Caught termination signal - exiting");
                        exit (1);
                }

                return FALSE;
        }

        g_debug ("GdmSignalHandler: Done handling signals");

        return TRUE;
}

static void
fallback_get_backtrace (void)
{
#ifdef HAVE_EXECINFO_H
        void *  frames[64];
        size_t  size;
        char ** strings;
        size_t  i;

        size = backtrace (frames, G_N_ELEMENTS (frames));
        if ((strings = backtrace_symbols (frames, size))) {
                syslog (LOG_CRIT, "******************* START ********************************");
                for (i = 0; i < size; i++) {
                        syslog (LOG_CRIT, "Frame %zd: %s", i, strings[i]);
                }
                free (strings);
                syslog (LOG_CRIT, "******************* END **********************************");
                return;
        }
#endif
        g_warning ("GDM crashed, but symbols couldn't be retrieved.");
}


static gboolean
crashlogger_get_backtrace (void)
{
        gboolean success = FALSE;
        int      pid;

        pid = fork ();
        if (pid > 0) {
                /* Wait for the child to finish */
                int estatus;
                if (waitpid (pid, &estatus, 0) != -1) {
                        /* Only succeed if the crashlogger succeeded */
                        if (WIFEXITED (estatus) && (WEXITSTATUS (estatus) == 0)) {
                                success = TRUE;
                        }
                }
        } else if (pid == 0) {
                /* Child process */
                execl (LIBEXECDIR "/gdm-crash-logger",
                       LIBEXECDIR "/gdm-crash-logger", NULL);
        }

        return success;
}


static void
gdm_signal_handler_backtrace (void)
{
        struct stat s;
        gboolean    fallback = TRUE;

        /* Try to use gdb via gdm-crash-logger if it exists, since
         * we get much better information out of it.  Otherwise
         * fall back to execinfo.
         */
        if (g_stat (LIBEXECDIR "/gdm-crash-logger", &s) == 0) {
                fallback = crashlogger_get_backtrace () ? FALSE : TRUE;
        }

        if (fallback) {
                fallback_get_backtrace ();
        }
}

static void
signal_handler (int signo)
{
        static int in_fatal = 0;
        int        ignore;
        guchar     signo_byte = signo;

        /* avoid loops */
        if (in_fatal > 0) {
                return;
        }

        ++in_fatal;

        switch (signo) {
        case SIGSEGV:
        case SIGBUS:
        case SIGILL:
        case SIGABRT:
        case SIGTRAP:
                gdm_signal_handler_backtrace ();
                exit (1);
                break;
        case SIGFPE:
        case SIGPIPE:
                /* let the fatal signals interrupt us */
                --in_fatal;
                gdm_signal_handler_backtrace ();
                ignore = write (signal_pipes [1], &signo_byte, 1);
                break;
        default:
                --in_fatal;
                ignore = write (signal_pipes [1], &signo_byte, 1);
                break;
        }
}

static void
catch_signal (GdmSignalHandler *handler,
              int               signal_number)
{
        struct sigaction  action;
        struct sigaction *old_action;

        g_debug ("GdmSignalHandler: Registering for %d signals", signal_number);

        action.sa_handler = signal_handler;
        sigemptyset (&action.sa_mask);
        action.sa_flags = 0;

        old_action = g_new0 (struct sigaction, 1);

        sigaction (signal_number, &action, old_action);

        g_hash_table_insert (handler->priv->action_lookup,
                             GINT_TO_POINTER (signal_number),
                             old_action);
}

static void
uncatch_signal (GdmSignalHandler *handler,
                int               signal_number)
{
        struct sigaction *old_action;

        g_debug ("GdmSignalHandler: Unregistering for %d signals", signal_number);

        old_action = g_hash_table_lookup (handler->priv->action_lookup,
                                          GINT_TO_POINTER (signal_number));
        g_hash_table_remove (handler->priv->action_lookup,
                             GINT_TO_POINTER (signal_number));

        sigaction (signal_number, old_action, NULL);

        g_free (old_action);
}

guint
gdm_signal_handler_add (GdmSignalHandler    *handler,
                        int                  signal_number,
                        GdmSignalHandlerFunc callback,
                        gpointer             data)
{
        CallbackData *cdata;
        GSList       *list;

        g_return_val_if_fail (GDM_IS_SIGNAL_HANDLER (handler), 0);

        cdata = g_new0 (CallbackData, 1);
        cdata->signal_number = signal_number;
        cdata->func = callback;
        cdata->data = data;
        cdata->id = handler->priv->next_id++;

        g_debug ("GdmSignalHandler: Adding handler %u: signum=%d %p", cdata->id, cdata->signal_number, cdata->func);

        if (g_hash_table_lookup (handler->priv->action_lookup, GINT_TO_POINTER (signal_number)) == NULL) {
                catch_signal (handler, signal_number);
        }

        /* ID lookup owns the CallbackData */
        g_hash_table_insert (handler->priv->id_lookup, GUINT_TO_POINTER (cdata->id), cdata);

        list = g_hash_table_lookup (handler->priv->lookup, GINT_TO_POINTER (signal_number));
        list = g_slist_prepend (list, GUINT_TO_POINTER (cdata->id));

        g_hash_table_insert (handler->priv->lookup, GINT_TO_POINTER (signal_number), list);

        return cdata->id;
}

void
gdm_signal_handler_add_fatal (GdmSignalHandler *handler)
{
        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (handler));

        gdm_signal_handler_add (handler, SIGILL, NULL, NULL);
        gdm_signal_handler_add (handler, SIGBUS, NULL, NULL);
        gdm_signal_handler_add (handler, SIGSEGV, NULL, NULL);
        gdm_signal_handler_add (handler, SIGABRT, NULL, NULL);
        gdm_signal_handler_add (handler, SIGTRAP, NULL, NULL);
}

static void
callback_data_free (CallbackData *d)
{
        g_free (d);
}

static void
gdm_signal_handler_remove_and_free_data (GdmSignalHandler *handler,
                                         CallbackData     *cdata)
{
        GSList *list;

        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (handler));

        list = g_hash_table_lookup (handler->priv->lookup, GINT_TO_POINTER (cdata->signal_number));
        list = g_slist_remove_all (list, GUINT_TO_POINTER (cdata->id));
        if (list == NULL) {
                uncatch_signal (handler, cdata->signal_number);
        }

        g_debug ("GdmSignalHandler: Removing handler %u: signum=%d %p", cdata->signal_number, cdata->id, cdata->func);
        /* put changed list back in */
        g_hash_table_insert (handler->priv->lookup, GINT_TO_POINTER (cdata->signal_number), list);

        g_hash_table_remove (handler->priv->id_lookup, GUINT_TO_POINTER (cdata->id));
}

void
gdm_signal_handler_remove (GdmSignalHandler    *handler,
                           guint                id)
{
        CallbackData *found;

        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (handler));

        found = g_hash_table_lookup (handler->priv->id_lookup, GUINT_TO_POINTER (id));
        if (found != NULL) {
                gdm_signal_handler_remove_and_free_data (handler, found);
                found = NULL;
        }
}

static CallbackData *
find_callback_data_by_func (GdmSignalHandler    *handler,
                            guint                signal_number,
                            GdmSignalHandlerFunc callback,
                            gpointer             data)
{
        GSList       *list;
        GSList       *l;
        CallbackData *found;

        found = NULL;

        list = g_hash_table_lookup (handler->priv->lookup, GINT_TO_POINTER (signal_number));

        for (l = list; l != NULL; l = l->next) {
                guint         id;
                CallbackData *d;

                id = GPOINTER_TO_UINT (l->data);

                d = g_hash_table_lookup (handler->priv->id_lookup, GUINT_TO_POINTER (id));
                if (d != NULL
                    && d->func == callback
                    && d->data == data) {
                        found = d;
                        break;
                }
        }

        return found;
}

void
gdm_signal_handler_remove_func (GdmSignalHandler    *handler,
                                guint                signal_number,
                                GdmSignalHandlerFunc callback,
                                gpointer             data)
{
        CallbackData *found;

        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (handler));

        found = find_callback_data_by_func (handler, signal_number, callback, data);

        if (found != NULL) {
                gdm_signal_handler_remove_and_free_data (handler, found);
                found = NULL;
        }

        /* FIXME: once all handlers are removed deregister signum handler */
}

static void
gdm_signal_handler_class_init (GdmSignalHandlerClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = gdm_signal_handler_finalize;

        g_type_class_add_private (klass, sizeof (GdmSignalHandlerPrivate));
}

static void
signal_list_free (GSList *list)
{
        g_slist_free (list);
}

void
gdm_signal_handler_set_fatal_func (GdmSignalHandler *handler,
                                   GDestroyNotify    func,
                                   gpointer          user_data)
{
        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (handler));

        handler->priv->fatal_func = func;
        handler->priv->fatal_data = user_data;
}

static void
gdm_signal_handler_init (GdmSignalHandler *handler)
{
        GIOChannel *ioc;

        handler->priv = GDM_SIGNAL_HANDLER_GET_PRIVATE (handler);

        handler->priv->next_id = 1;

        handler->priv->lookup = g_hash_table_new (NULL, NULL);
        handler->priv->id_lookup = g_hash_table_new (NULL, NULL);
        handler->priv->action_lookup = g_hash_table_new (NULL, NULL);

        if (pipe (signal_pipes) == -1) {
                g_error ("Could not create pipe() for signal handling");
        }
        fcntl(signal_pipes[0], F_SETFD, FD_CLOEXEC);
        fcntl(signal_pipes[1], F_SETFD, FD_CLOEXEC);

        ioc = g_io_channel_unix_new (signal_pipes[0]);
        g_io_channel_set_flags (ioc, G_IO_FLAG_NONBLOCK, NULL);
        g_io_add_watch (ioc, G_IO_IN, (GIOFunc)signal_io_watch, handler);
        g_io_channel_set_close_on_unref (ioc, TRUE);
        g_io_channel_unref (ioc);
}

static void
gdm_signal_handler_finalize (GObject *object)
{
        GdmSignalHandler *handler;
        GList            *l;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GDM_IS_SIGNAL_HANDLER (object));

        handler = GDM_SIGNAL_HANDLER (object);

        g_debug ("GdmSignalHandler: Finalizing signal handler");

        g_return_if_fail (handler->priv != NULL);
        for (l = g_hash_table_get_values (handler->priv->lookup);
             l != NULL; l = l->next) {
                signal_list_free ((GSList *) l->data);
        }
        g_hash_table_destroy (handler->priv->lookup);
        for (l = g_hash_table_get_values (handler->priv->id_lookup);
             l != NULL; l = l->next) {
                callback_data_free ((CallbackData *) l->data);
        }
        g_hash_table_destroy (handler->priv->id_lookup);
        for (l = g_hash_table_get_values (handler->priv->action_lookup);
             l != NULL; l = l->next) {
                g_free (l->data);
        }
        g_hash_table_destroy (handler->priv->action_lookup);

        close (signal_pipes [0]);
        close (signal_pipes [1]);

        G_OBJECT_CLASS (gdm_signal_handler_parent_class)->finalize (object);
}

GdmSignalHandler *
gdm_signal_handler_new (void)
{
        if (signal_handler_object != NULL) {
                g_object_ref (signal_handler_object);
        } else {
                signal_handler_object = g_object_new (GDM_TYPE_SIGNAL_HANDLER, NULL);
                g_object_add_weak_pointer (signal_handler_object,
                                           (gpointer *) &signal_handler_object);
        }

        return GDM_SIGNAL_HANDLER (signal_handler_object);
}