~xubuntu-dev/libxfce4util/maverick

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
/*
 * Copyright (c) 2007 Brian Tarricone <bjt23@cornell.edu>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif

#include <glib.h>

#include <libxfce4util/libxfce4util.h>
#include <libxfce4util/libxfce4util-alias.h>

#define SIGNAL_PIPE_READ   __signal_pipe[0]
#define SIGNAL_PIPE_WRITE  __signal_pipe[1]

typedef struct
{
    gint signal;
    XfcePosixSignalHandler handler;
    gpointer user_data;
    struct sigaction old_sa;
} XfcePosixSignalHandlerData;

static gboolean __inited = FALSE;
static int __signal_pipe[2] = { -1, -1 };
static GHashTable *__handlers = NULL;
static GIOChannel *__signal_io = NULL;
static guint __io_watch_id = 0;


static void
xfce_posix_signal_handler_data_free(XfcePosixSignalHandlerData *hdata)
{
    if(!hdata)
        return;
    
    sigaction(hdata->signal, &hdata->old_sa, NULL);
    g_free(hdata);
}

static gboolean
xfce_posix_signal_handler_pipe_io(GIOChannel *source,
                                  GIOCondition condition,
                                  gpointer data)
{
    int signal = 0;
    GError *error = NULL;
    gsize bin = 0;
    XfcePosixSignalHandlerData *hdata;
    
    if(G_IO_STATUS_NORMAL == g_io_channel_read_chars(source, (gchar *)&signal,
                                                     sizeof(signal), &bin,
                                                     &error)
       && bin == sizeof(signal))
    {
        hdata = g_hash_table_lookup(__handlers, GINT_TO_POINTER(signal));
        if(hdata)
            hdata->handler(signal, hdata->user_data);
    } else {
        if(error) {
            g_critical("Signal pipe read failed: %s\n", error->message);
            g_error_free(error);
        } else {
            g_critical("Short read from signal pipe (expected %d, got %d)\n",
                       (int)sizeof(signal), (int)bin);
        }
    }
    
    return TRUE;
}

static void
xfce_posix_signal_handler(int signal)
{
    write(SIGNAL_PIPE_WRITE, &signal, sizeof(signal));
}


/**
 * xfce_posix_signal_handler_init:
 * @error: Location of a #GError to store any possible errors.
 *
 * Initializes the POSIX signal handler system.  Must be called
 * before setting any POSIX signal handlers.
 *
 * Returns: %TRUE on success, %FALSE on failure, in which case
 *          @error will be set.
 **/
gboolean
xfce_posix_signal_handler_init(GError **error)
{
    if(G_UNLIKELY(__inited))
        return TRUE;
    
    if(pipe(__signal_pipe)) {
        if(error) {
            g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
                        _("pipe() failed: %s"), strerror(errno));
        }
        return FALSE;
    }
    
    __handlers = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
                                       (GDestroyNotify)xfce_posix_signal_handler_data_free);
    
    __signal_io = g_io_channel_unix_new(SIGNAL_PIPE_READ);
    g_io_channel_set_close_on_unref(__signal_io, FALSE);
    g_io_channel_set_encoding(__signal_io, NULL, NULL);
    g_io_channel_set_buffered(__signal_io, FALSE);
    __io_watch_id = g_io_add_watch(__signal_io, G_IO_IN,
                                   xfce_posix_signal_handler_pipe_io, NULL);
    
    __inited = TRUE;
    return TRUE;
}

/**
 * xfce_posix_signal_handler_shutdown:
 *
 * Frees all memory associated with the POSIX signal handling system
 * and restores all default signal handlers.
 **/
void
xfce_posix_signal_handler_shutdown()
{
    if(G_UNLIKELY(!__inited))
        return;
    
    g_source_remove(__io_watch_id);
    __io_watch_id = 0;
    g_io_channel_unref(__signal_io);
    __signal_io = NULL;
    
    g_hash_table_destroy(__handlers);
    __handlers = NULL;
    
    close(SIGNAL_PIPE_READ);
    SIGNAL_PIPE_READ = -1;
    close(SIGNAL_PIPE_WRITE);
    SIGNAL_PIPE_WRITE = -1;
    
    __inited = FALSE;
}

/**
 * xfce_posix_signal_handler_set_handler:
 * @signal: A POSIX signal id number.
 * @handler: A callback function.
 * @user_data: Arbitrary data that will be passed to @handler.
 * @error: Location of a #GError to store any possible errors.
 *
 * Sets @handler to be called whenever @signal is caught by the
 * application.  The @user_data parameter will be passed as an argument
 * to @handler.
 *
 * Returns: %TRUE on success, %FALSE otherwise, in which case
 *          @error will be set.
 **/
gboolean
xfce_posix_signal_handler_set_handler(gint signal,
                                      XfcePosixSignalHandler handler,
                                      gpointer user_data,
                                      GError **error)
{
    XfcePosixSignalHandlerData *hdata;
    struct sigaction sa;
    
    if(G_UNLIKELY(!__inited)) {
        if(error) {
            g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                        _("xfce_posix_signal_handler_init() must be called first"));
        }
        return FALSE;
    }
    
    if(!handler) {
        g_warning("NULL signal handler supplied; removing existing handler");
        xfce_posix_signal_handler_restore_handler(signal);
        return TRUE;
    }
    
    if(g_hash_table_lookup(__handlers, GINT_TO_POINTER(signal)))
        xfce_posix_signal_handler_restore_handler(signal);
    
    hdata = g_new0(XfcePosixSignalHandlerData, 1);
    hdata->signal = signal;
    hdata->handler = handler;
    hdata->user_data = user_data;
    
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = xfce_posix_signal_handler;
    sa.sa_flags = SA_RESTART;
    
    if(sigaction(signal, &sa, &hdata->old_sa)) {
        if(error) {
            g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
                        _("sigaction() failed: %s\n"), strerror(errno));
        }
        g_free(hdata);
        return FALSE;
    }
    
    g_hash_table_insert(__handlers, GINT_TO_POINTER(signal), hdata);
    
    return TRUE;
}

/**
 * xfce_posix_signal_handler_restore_handler:
 * @signal: A POSIX signal id number.
 *
 * Restores the default handler for @signal.
 **/
void
xfce_posix_signal_handler_restore_handler(gint signal)
{
    if(G_UNLIKELY(!__inited))
        return;
    
    g_hash_table_remove(__handlers, GINT_TO_POINTER(signal));
}



#define __XFCE_POSIX_SIGNAL_HANDLER_C__
#include <libxfce4util/libxfce4util-aliasdef.c>