]> code.delx.au - pulseaudio/blob - polyp/mainloop-signal.c
Merge Pierre's changes
[pulseaudio] / polyp / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38
39 #include "mainloop-signal.h"
40 #include "util.h"
41 #include "xmalloc.h"
42 #include "log.h"
43
44 struct pa_signal_event {
45 int sig;
46 #ifdef HAVE_SIGACTION
47 struct sigaction saved_sigaction;
48 #else
49 void (*saved_handler)(int sig);
50 #endif
51 void (*callback) (struct pa_mainloop_api*a, struct pa_signal_event *e, int signal, void *userdata);
52 void *userdata;
53 void (*destroy_callback) (struct pa_mainloop_api*a, struct pa_signal_event*e, void *userdata);
54 struct pa_signal_event *previous, *next;
55 };
56
57 static struct pa_mainloop_api *api = NULL;
58 static int signal_pipe[2] = { -1, -1 };
59 static struct pa_io_event* io_event = NULL;
60 static struct pa_defer_event *defer_event = NULL;
61 static struct pa_signal_event *signals = NULL;
62
63 #ifdef OS_IS_WIN32
64 static unsigned int waiting_signals = 0;
65 static CRITICAL_SECTION crit;
66 #endif
67
68 static void signal_handler(int sig) {
69 #ifndef HAVE_SIGACTION
70 signal(sig, signal_handler);
71 #endif
72 write(signal_pipe[1], &sig, sizeof(sig));
73
74 #ifdef OS_IS_WIN32
75 EnterCriticalSection(&crit);
76 waiting_signals++;
77 LeaveCriticalSection(&crit);
78 #endif
79 }
80
81 static void dispatch(struct pa_mainloop_api*a, int sig) {
82 struct pa_signal_event*s;
83
84 for (s = signals; s; s = s->next)
85 if (s->sig == sig) {
86 assert(s->callback);
87 s->callback(a, s, sig, s->userdata);
88 break;
89 }
90 }
91
92 static void defer(struct pa_mainloop_api*a, struct pa_defer_event*e, void *userdata) {
93 ssize_t r;
94 int sig;
95 unsigned int sigs;
96
97 #ifdef OS_IS_WIN32
98 EnterCriticalSection(&crit);
99 sigs = waiting_signals;
100 waiting_signals = 0;
101 LeaveCriticalSection(&crit);
102 #endif
103
104 while (sigs) {
105 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
106 pa_log(__FILE__": read(): %s\n", strerror(errno));
107 return;
108 }
109
110 if (r != sizeof(sig)) {
111 pa_log(__FILE__": short read()\n");
112 return;
113 }
114
115 dispatch(a, sig);
116
117 sigs--;
118 }
119 }
120
121 static void callback(struct pa_mainloop_api*a, struct pa_io_event*e, int fd, enum pa_io_event_flags f, void *userdata) {
122 ssize_t r;
123 int sig;
124 assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
125
126
127 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
128 if (errno == EAGAIN)
129 return;
130
131 pa_log(__FILE__": read(): %s\n", strerror(errno));
132 return;
133 }
134
135 if (r != sizeof(sig)) {
136 pa_log(__FILE__": short read()\n");
137 return;
138 }
139
140 dispatch(a, sig);
141 }
142
143 int pa_signal_init(struct pa_mainloop_api *a) {
144 assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event && !defer_event);
145
146 #ifdef OS_IS_WIN32
147 if (_pipe(signal_pipe, 200, _O_BINARY) < 0) {
148 #else
149 if (pipe(signal_pipe) < 0) {
150 #endif
151 pa_log(__FILE__": pipe() failed: %s\n", strerror(errno));
152 return -1;
153 }
154
155 pa_make_nonblock_fd(signal_pipe[0]);
156 pa_make_nonblock_fd(signal_pipe[1]);
157 pa_fd_set_cloexec(signal_pipe[0], 1);
158 pa_fd_set_cloexec(signal_pipe[1], 1);
159
160 api = a;
161
162 #ifndef OS_IS_WIN32
163 io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
164 assert(io_event);
165 #else
166 defer_event = api->defer_new(api, defer, NULL);
167 assert(defer_event);
168
169 InitializeCriticalSection(&crit);
170 #endif
171
172 return 0;
173 }
174
175 void pa_signal_done(void) {
176 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && (io_event || defer_event));
177
178 while (signals)
179 pa_signal_free(signals);
180
181
182 #ifndef OS_IS_WIN32
183 api->io_free(io_event);
184 io_event = NULL;
185 #else
186 api->defer_free(defer_event);
187 defer_event = NULL;
188
189 DeleteCriticalSection(&crit);
190 #endif
191
192 close(signal_pipe[0]);
193 close(signal_pipe[1]);
194 signal_pipe[0] = signal_pipe[1] = -1;
195
196 api = NULL;
197 }
198
199 struct pa_signal_event* pa_signal_new(int sig, void (*callback) (struct pa_mainloop_api *api, struct pa_signal_event*e, int sig, void *userdata), void *userdata) {
200 struct pa_signal_event *e = NULL;
201
202 #ifdef HAVE_SIGACTION
203 struct sigaction sa;
204 #endif
205
206 assert(sig > 0 && callback);
207
208 for (e = signals; e; e = e->next)
209 if (e->sig == sig)
210 goto fail;
211
212 e = pa_xmalloc(sizeof(struct pa_signal_event));
213 e->sig = sig;
214 e->callback = callback;
215 e->userdata = userdata;
216 e->destroy_callback = NULL;
217
218 #ifdef HAVE_SIGACTION
219 memset(&sa, 0, sizeof(sa));
220 sa.sa_handler = signal_handler;
221 sigemptyset(&sa.sa_mask);
222 sa.sa_flags = SA_RESTART;
223
224 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
225 #else
226 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
227 #endif
228 goto fail;
229
230 e->previous = NULL;
231 e->next = signals;
232 signals = e;
233
234 return e;
235 fail:
236 if (e)
237 pa_xfree(e);
238 return NULL;
239 }
240
241 void pa_signal_free(struct pa_signal_event *e) {
242 assert(e);
243
244 if (e->next)
245 e->next->previous = e->previous;
246 if (e->previous)
247 e->previous->next = e->next;
248 else
249 signals = e->next;
250
251 #ifdef HAVE_SIGACTION
252 sigaction(e->sig, &e->saved_sigaction, NULL);
253 #else
254 signal(e->sig, e->saved_handler);
255 #endif
256
257 if (e->destroy_callback)
258 e->destroy_callback(api, e, e->userdata);
259
260 pa_xfree(e);
261 }
262
263 void pa_signal_set_destroy(struct pa_signal_event *e, void (*callback) (struct pa_mainloop_api *api, struct pa_signal_event*e, void *userdata)) {
264 assert(e);
265 e->destroy_callback = callback;
266 }