]> code.delx.au - pulseaudio/blob - src/pulse/mainloop-signal.c
remove all occurences of
[pulseaudio] / src / pulse / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulsecore/core-error.h>
40 #include <pulse/xmalloc.h>
41
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/gccmacro.h>
45
46 #include "mainloop-signal.h"
47
48 struct pa_signal_event {
49 int sig;
50 #ifdef HAVE_SIGACTION
51 struct sigaction saved_sigaction;
52 #else
53 void (*saved_handler)(int sig);
54 #endif
55 void (*callback) (pa_mainloop_api*a, pa_signal_event *e, int sig, void *userdata);
56 void *userdata;
57 void (*destroy_callback) (pa_mainloop_api*a, pa_signal_event*e, void *userdata);
58 pa_signal_event *previous, *next;
59 };
60
61 static pa_mainloop_api *api = NULL;
62 static int signal_pipe[2] = { -1, -1 };
63 static pa_io_event* io_event = NULL;
64 static pa_signal_event *signals = NULL;
65
66 static void signal_handler(int sig) {
67 #ifndef HAVE_SIGACTION
68 signal(sig, signal_handler);
69 #endif
70 pa_write(signal_pipe[1], &sig, sizeof(sig), NULL);
71 }
72
73 static void dispatch(pa_mainloop_api*a, int sig) {
74 pa_signal_event*s;
75
76 for (s = signals; s; s = s->next)
77 if (s->sig == sig) {
78 assert(s->callback);
79 s->callback(a, s, sig, s->userdata);
80 break;
81 }
82 }
83
84 static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
85 ssize_t r;
86 int sig;
87 assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
88
89 if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) {
90 if (errno == EAGAIN)
91 return;
92
93 pa_log("read(): %s", pa_cstrerror(errno));
94 return;
95 }
96
97 if (r != sizeof(sig)) {
98 pa_log("short read()");
99 return;
100 }
101
102 dispatch(a, sig);
103 }
104
105 int pa_signal_init(pa_mainloop_api *a) {
106
107 assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event);
108
109 if (pipe(signal_pipe) < 0) {
110 pa_log("pipe(): %s", pa_cstrerror(errno));
111 return -1;
112 }
113
114 pa_make_nonblock_fd(signal_pipe[0]);
115 pa_make_nonblock_fd(signal_pipe[1]);
116 pa_fd_set_cloexec(signal_pipe[0], 1);
117 pa_fd_set_cloexec(signal_pipe[1], 1);
118
119 api = a;
120
121 io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
122 assert(io_event);
123
124 return 0;
125 }
126
127 void pa_signal_done(void) {
128 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && io_event);
129
130 while (signals)
131 pa_signal_free(signals);
132
133 api->io_free(io_event);
134 io_event = NULL;
135
136 close(signal_pipe[0]);
137 close(signal_pipe[1]);
138 signal_pipe[0] = signal_pipe[1] = -1;
139
140 api = NULL;
141 }
142
143 pa_signal_event* pa_signal_new(int sig, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata), void *userdata) {
144 pa_signal_event *e = NULL;
145
146 #ifdef HAVE_SIGACTION
147 struct sigaction sa;
148 #endif
149
150 assert(sig > 0 && _callback);
151
152 for (e = signals; e; e = e->next)
153 if (e->sig == sig)
154 goto fail;
155
156 e = pa_xmalloc(sizeof(pa_signal_event));
157 e->sig = sig;
158 e->callback = _callback;
159 e->userdata = userdata;
160 e->destroy_callback = NULL;
161
162 #ifdef HAVE_SIGACTION
163 memset(&sa, 0, sizeof(sa));
164 sa.sa_handler = signal_handler;
165 sigemptyset(&sa.sa_mask);
166 sa.sa_flags = SA_RESTART;
167
168 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
169 #else
170 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
171 #endif
172 goto fail;
173
174 e->previous = NULL;
175 e->next = signals;
176 signals = e;
177
178 return e;
179 fail:
180 if (e)
181 pa_xfree(e);
182 return NULL;
183 }
184
185 void pa_signal_free(pa_signal_event *e) {
186 assert(e);
187
188 if (e->next)
189 e->next->previous = e->previous;
190 if (e->previous)
191 e->previous->next = e->next;
192 else
193 signals = e->next;
194
195 #ifdef HAVE_SIGACTION
196 sigaction(e->sig, &e->saved_sigaction, NULL);
197 #else
198 signal(e->sig, e->saved_handler);
199 #endif
200
201 if (e->destroy_callback)
202 e->destroy_callback(api, e, e->userdata);
203
204 pa_xfree(e);
205 }
206
207 void pa_signal_set_destroy(pa_signal_event *e, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)) {
208 assert(e);
209 e->destroy_callback = _callback;
210 }