summaryrefslogtreecommitdiffstats
path: root/src/pulse/mainloop-signal.c
blob: 91c6bf6db48ada8f756254a38341bac91e2dbfc1 (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
/* $Id$ */

/***
  This file is part of PulseAudio.

  Copyright 2004-2008 Lennart Poettering
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.

  PulseAudio 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 Lesser General Public License
  along with PulseAudio; 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>
#include <signal.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif

#include <pulse/xmalloc.h>
#include <pulse/gccmacro.h>

#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/macro.h>

#include "mainloop-signal.h"

struct pa_signal_event {
    int sig;
#ifdef HAVE_SIGACTION
    struct sigaction saved_sigaction;
#else
    void (*saved_handler)(int sig);
#endif
    void *userdata;
    pa_signal_cb_t callback;
    pa_signal_destroy_cb_t destroy_callback;
    pa_signal_event *previous, *next;
};

static pa_mainloop_api *api = NULL;
static int signal_pipe[2] = { -1, -1 };
static pa_io_event* io_event = NULL;
static pa_signal_event *signals = NULL;

static void signal_handler(int sig) {
    int saved_errno;

    saved_errno = errno;

#ifndef HAVE_SIGACTION
    signal(sig, signal_handler);
#endif

    pa_write(signal_pipe[1], &sig, sizeof(sig), NULL);

    errno = saved_errno;
}

static void dispatch(pa_mainloop_api*a, int sig) {
    pa_signal_event *s;

    for (s = signals; s; s = s->next)
        if (s->sig == sig) {
            pa_assert(s->callback);
            s->callback(a, s, sig, s->userdata);
            break;
        }
}

static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
    ssize_t r;
    int sig;

    pa_assert(a);
    pa_assert(e);
    pa_assert(f == PA_IO_EVENT_INPUT);
    pa_assert(e == io_event);
    pa_assert(fd == signal_pipe[0]);

    if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) {
        if (errno == EAGAIN)
            return;

        pa_log("read(): %s", pa_cstrerror(errno));
        return;
    }

    if (r != sizeof(sig)) {
        pa_log("short read()");
        return;
    }

    dispatch(a, sig);
}

int pa_signal_init(pa_mainloop_api *a) {

    pa_assert(a);
    pa_assert(!api);
    pa_assert(signal_pipe[0] == -1);
    pa_assert(signal_pipe[1] == -1);
    pa_assert(!io_event);

    if (pipe(signal_pipe) < 0) {
        pa_log("pipe(): %s", pa_cstrerror(errno));
        return -1;
    }

    pa_make_fd_nonblock(signal_pipe[0]);
    pa_make_fd_nonblock(signal_pipe[1]);
    pa_make_fd_cloexec(signal_pipe[0]);
    pa_make_fd_cloexec(signal_pipe[1]);

    api = a;

    pa_assert_se(io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL));

    return 0;
}

void pa_signal_done(void) {
    while (signals)
        pa_signal_free(signals);

    if (io_event) {
        pa_assert(api);
        api->io_free(io_event);
        io_event = NULL;
    }

    pa_close_pipe(signal_pipe);

    api = NULL;
}

pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t _callback, void *userdata) {
    pa_signal_event *e = NULL;

#ifdef HAVE_SIGACTION
    struct sigaction sa;
#endif

    pa_assert(sig > 0);
    pa_assert(_callback);

    for (e = signals; e; e = e->next)
        if (e->sig == sig)
            goto fail;

    e = pa_xnew(pa_signal_event, 1);
    e->sig = sig;
    e->callback = _callback;
    e->userdata = userdata;
    e->destroy_callback = NULL;

#ifdef HAVE_SIGACTION
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = signal_handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;

    if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
#else
    if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
#endif
        goto fail;

    e->previous = NULL;
    e->next = signals;
    signals = e;

    return e;
fail:
    if (e)
        pa_xfree(e);
    return NULL;
}

void pa_signal_free(pa_signal_event *e) {
    pa_assert(e);

    if (e->next)
        e->next->previous = e->previous;
    if (e->previous)
        e->previous->next = e->next;
    else
        signals = e->next;

#ifdef HAVE_SIGACTION
    pa_assert_se(sigaction(e->sig, &e->saved_sigaction, NULL) == 0);
#else
    pa_assert_se(signal(e->sig, e->saved_handler) == signal_handler);
#endif

    if (e->destroy_callback)
        e->destroy_callback(api, e, e->userdata);

    pa_xfree(e);
}

void pa_signal_set_destroy(pa_signal_event *e, pa_signal_destroy_cb_t _callback) {
    pa_assert(e);

    e->destroy_callback = _callback;
}