summaryrefslogtreecommitdiff
path: root/signal-handler.c
blob: fb35c249e3abdb8c6f7e80325d72c35d619feb2b (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
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- */

/*  Sysprof -- Sampling, systemwide CPU profiler
 *  Copyright (C) 2005  Søren Sandmann (sandmann@daimi.au.dk)
 *
 *  This library 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 library 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 library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <signal.h>
#include <unistd.h>
#include <string.h>
#include "libnul.h"

typedef struct signal_watch_t signal_watch_t;
struct signal_watch_t
{
    int                 signo;
    signal_func_t          handler;
    gpointer            user_data;
    
    struct sigaction    old_action;
    
    signal_watch_t *       next;
};

static int          read_end = -1;
static int          write_end = -1;
static signal_watch_t *signal_watch_tes = NULL;

static signal_watch_t *
lookup_signal_watch_t (int signo)
{
    signal_watch_t *w;
    
    for (w = signal_watch_tes; w != NULL; w = w->next)
    {
	if (w->signo == signo)
	    return w;
    }
    
    return NULL;
}

static void
remove_signal_watch_t (signal_watch_t *watch)
{
    signal_watch_t *prev, *w;
    
    g_return_if_fail (watch != NULL);

    prev = NULL;
    for (w = signal_watch_tes; w != NULL; w = w->next)
    {
        if (w == watch)
        {
            if (prev)
                prev->next = w->next;
            else
                signal_watch_tes = w->next;

            break;
        }
        
        prev = w;
    }
}

static void
signal_handler (int        signo,
                siginfo_t *info,
                void      *data)
{
    /* FIXME: I suppose we should handle short
     * and non-successful writes ...
     *
     * And also, there is a deadlock if so many signals arrive that
     * write() blocks. Then we will be stuck right here, and the
     * main loop will never run. Kinda hard to fix without dropping
     * signals ...
     *
     */
    write (write_end, &signo, sizeof (int));
}

static void
on_read (gpointer data)
{
    signal_watch_t *watch;
    int signo;

    /* FIXME: handle short read I suppose */
    read (read_end, &signo, sizeof (int));

    watch = lookup_signal_watch_t (signo);

    if (watch)
        watch->handler (signo, watch->user_data);
}

static gboolean
create_pipe (int     *read_end,
	     int     *write_end,
             GError **err)
{
    int p[2];
    
    if (pipe (p) < 0)
    {
        /* FIXME - create an error */
        return FALSE;
    }

    /* FIXME: We should probably make the fd's non-blocking */
    if (read_end)
	*read_end = p[0];
    
    if (write_end)
	*write_end = p[1];

    return TRUE;
}

static gboolean
install_signal_handler (int                signo,
                        struct sigaction  *old_action,
                        GError           **err)
{
    struct sigaction action;
    
    memset (&action, 0, sizeof (action));
    
    action.sa_sigaction = signal_handler;
    sigemptyset (&action.sa_mask);
    action.sa_flags = SA_SIGINFO;
    
    if (sigaction (signo, &action, old_action) < 0)
    {
        /* FIXME - create an error */
        return TRUE;
    }

    return TRUE;
}

static void
signal_watch_free (signal_watch_t *watch)
{
    remove_signal_watch_t (watch);
    
    g_free (watch);
}

gboolean
nul_signal_set_handler (int          signo,
                        signal_func_t   handler,
                        gpointer     data,
                        GError     **err)
{
    signal_watch_t *watch;

    g_return_val_if_fail (handler == NULL ||
                          lookup_signal_watch_t (signo) == NULL, FALSE);

    if (handler == NULL)
    {
        watch = lookup_signal_watch_t (signo);

        if (watch)
        {
            /* FIXME: error */
            sigaction (signo, &watch->old_action, NULL);
            
            signal_watch_free (watch);
        }

        return TRUE;
    }
    else
    {
        if (read_end == -1)
        {
            if (!create_pipe (&read_end, &write_end, err))
                return FALSE;
            
            nul_fd_add_watch (read_end, NULL);
            nul_fd_set_read_callback (read_end, on_read);
        }
        
        watch = g_new0 (signal_watch_t, 1);
        
        watch->signo = signo;
        watch->handler = handler;
        watch->user_data = data;
        watch->next = signal_watch_tes;
        signal_watch_tes = watch;
        
        if (!install_signal_handler (signo, &watch->old_action, err))
        {
            signal_watch_free (watch);
            
            return FALSE;
        }
        
        return TRUE;
    }
}