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