]> code.delx.au - pulseaudio/blob - src/pulse/mainloop-signal.c
merge glitch-free branch back into trunk
[pulseaudio] / src / pulse / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2004-2008 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2 of the License,
12 or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <signal.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36
37 #ifdef HAVE_WINDOWS_H
38 #include <windows.h>
39 #endif
40
41 #include <pulse/xmalloc.h>
42 #include <pulse/gccmacro.h>
43
44 #include <pulsecore/core-error.h>
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/macro.h>
48
49 #include "mainloop-signal.h"
50
51 struct pa_signal_event {
52 int sig;
53 #ifdef HAVE_SIGACTION
54 struct sigaction saved_sigaction;
55 #else
56 void (*saved_handler)(int sig);
57 #endif
58 void *userdata;
59 pa_signal_cb_t callback;
60 pa_signal_destroy_cb_t destroy_callback;
61 pa_signal_event *previous, *next;
62 };
63
64 static pa_mainloop_api *api = NULL;
65 static int signal_pipe[2] = { -1, -1 };
66 static pa_io_event* io_event = NULL;
67 static pa_signal_event *signals = NULL;
68
69 static void signal_handler(int sig) {
70 int saved_errno;
71
72 saved_errno = errno;
73
74 #ifndef HAVE_SIGACTION
75 signal(sig, signal_handler);
76 #endif
77
78 pa_write(signal_pipe[1], &sig, sizeof(sig), NULL);
79
80 errno = saved_errno;
81 }
82
83 static void dispatch(pa_mainloop_api*a, int sig) {
84 pa_signal_event *s;
85
86 for (s = signals; s; s = s->next)
87 if (s->sig == sig) {
88 pa_assert(s->callback);
89 s->callback(a, s, sig, s->userdata);
90 break;
91 }
92 }
93
94 static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
95 ssize_t r;
96 int sig;
97
98 pa_assert(a);
99 pa_assert(e);
100 pa_assert(f == PA_IO_EVENT_INPUT);
101 pa_assert(e == io_event);
102 pa_assert(fd == signal_pipe[0]);
103
104 if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) {
105 if (errno == EAGAIN)
106 return;
107
108 pa_log("read(): %s", pa_cstrerror(errno));
109 return;
110 }
111
112 if (r != sizeof(sig)) {
113 pa_log("short read()");
114 return;
115 }
116
117 dispatch(a, sig);
118 }
119
120 int pa_signal_init(pa_mainloop_api *a) {
121
122 pa_assert(a);
123 pa_assert(!api);
124 pa_assert(signal_pipe[0] == -1);
125 pa_assert(signal_pipe[1] == -1);
126 pa_assert(!io_event);
127
128 if (pipe(signal_pipe) < 0) {
129 pa_log("pipe(): %s", pa_cstrerror(errno));
130 return -1;
131 }
132
133 pa_make_fd_nonblock(signal_pipe[0]);
134 pa_make_fd_nonblock(signal_pipe[1]);
135 pa_make_fd_cloexec(signal_pipe[0]);
136 pa_make_fd_cloexec(signal_pipe[1]);
137
138 api = a;
139
140 pa_assert_se(io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL));
141
142 return 0;
143 }
144
145 void pa_signal_done(void) {
146 while (signals)
147 pa_signal_free(signals);
148
149 if (io_event) {
150 pa_assert(api);
151 api->io_free(io_event);
152 io_event = NULL;
153 }
154
155 pa_close_pipe(signal_pipe);
156
157 api = NULL;
158 }
159
160 pa_signal_event* pa_signal_new(int sig, pa_signal_cb_t _callback, void *userdata) {
161 pa_signal_event *e = NULL;
162
163 #ifdef HAVE_SIGACTION
164 struct sigaction sa;
165 #endif
166
167 pa_assert(sig > 0);
168 pa_assert(_callback);
169
170 for (e = signals; e; e = e->next)
171 if (e->sig == sig)
172 goto fail;
173
174 e = pa_xnew(pa_signal_event, 1);
175 e->sig = sig;
176 e->callback = _callback;
177 e->userdata = userdata;
178 e->destroy_callback = NULL;
179
180 #ifdef HAVE_SIGACTION
181 memset(&sa, 0, sizeof(sa));
182 sa.sa_handler = signal_handler;
183 sigemptyset(&sa.sa_mask);
184 sa.sa_flags = SA_RESTART;
185
186 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
187 #else
188 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
189 #endif
190 goto fail;
191
192 e->previous = NULL;
193 e->next = signals;
194 signals = e;
195
196 return e;
197 fail:
198 if (e)
199 pa_xfree(e);
200 return NULL;
201 }
202
203 void pa_signal_free(pa_signal_event *e) {
204 pa_assert(e);
205
206 if (e->next)
207 e->next->previous = e->previous;
208 if (e->previous)
209 e->previous->next = e->next;
210 else
211 signals = e->next;
212
213 #ifdef HAVE_SIGACTION
214 pa_assert_se(sigaction(e->sig, &e->saved_sigaction, NULL) == 0);
215 #else
216 pa_assert_se(signal(e->sig, e->saved_handler) == signal_handler);
217 #endif
218
219 if (e->destroy_callback)
220 e->destroy_callback(api, e, e->userdata);
221
222 pa_xfree(e);
223 }
224
225 void pa_signal_set_destroy(pa_signal_event *e, pa_signal_destroy_cb_t _callback) {
226 pa_assert(e);
227
228 e->destroy_callback = _callback;
229 }