Add copyright notices to all relevant files. (based on svn log)
[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 <assert.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37
38 #ifdef HAVE_WINDOWS_H
39 #include <windows.h>
40 #endif
41
42 #include <pulsecore/core-error.h>
43 #include <pulse/xmalloc.h>
44
45 #include <pulsecore/core-util.h>
46 #include <pulsecore/log.h>
47 #include <pulsecore/gccmacro.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             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     assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
91
92     if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig), NULL)) < 0) {
93         if (errno == EAGAIN)
94             return;
95
96         pa_log("read(): %s", pa_cstrerror(errno));
97         return;
98     }
99
100     if (r != sizeof(sig)) {
101         pa_log("short read()");
102         return;
103     }
104
105     dispatch(a, sig);
106 }
107
108 int pa_signal_init(pa_mainloop_api *a) {
109
110     assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event);
111
112     if (pipe(signal_pipe) < 0) {
113         pa_log("pipe(): %s", pa_cstrerror(errno));
114         return -1;
115     }
116
117     pa_make_nonblock_fd(signal_pipe[0]);
118     pa_make_nonblock_fd(signal_pipe[1]);
119     pa_fd_set_cloexec(signal_pipe[0], 1);
120     pa_fd_set_cloexec(signal_pipe[1], 1);
121
122     api = a;
123
124     io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
125     assert(io_event);
126
127     return 0;
128 }
129
130 void pa_signal_done(void) {
131     assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && io_event);
132
133     while (signals)
134         pa_signal_free(signals);
135
136     api->io_free(io_event);
137     io_event = NULL;
138
139     close(signal_pipe[0]);
140     close(signal_pipe[1]);
141     signal_pipe[0] = signal_pipe[1] = -1;
142
143     api = NULL;
144 }
145
146 pa_signal_event* pa_signal_new(int sig, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata), void *userdata) {
147     pa_signal_event *e = NULL;
148
149 #ifdef HAVE_SIGACTION
150     struct sigaction sa;
151 #endif
152
153     assert(sig > 0 && _callback);
154
155     for (e = signals; e; e = e->next)
156         if (e->sig == sig)
157             goto fail;
158
159     e = pa_xmalloc(sizeof(pa_signal_event));
160     e->sig = sig;
161     e->callback = _callback;
162     e->userdata = userdata;
163     e->destroy_callback = NULL;
164
165 #ifdef HAVE_SIGACTION
166     memset(&sa, 0, sizeof(sa));
167     sa.sa_handler = signal_handler;
168     sigemptyset(&sa.sa_mask);
169     sa.sa_flags = SA_RESTART;
170
171     if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
172 #else
173     if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
174 #endif
175         goto fail;
176
177     e->previous = NULL;
178     e->next = signals;
179     signals = e;
180
181     return e;
182 fail:
183     if (e)
184         pa_xfree(e);
185     return NULL;
186 }
187
188 void pa_signal_free(pa_signal_event *e) {
189     assert(e);
190
191     if (e->next)
192         e->next->previous = e->previous;
193     if (e->previous)
194         e->previous->next = e->next;
195     else
196         signals = e->next;
197
198 #ifdef HAVE_SIGACTION
199     sigaction(e->sig, &e->saved_sigaction, NULL);
200 #else
201     signal(e->sig, e->saved_handler);
202 #endif
203
204     if (e->destroy_callback)
205         e->destroy_callback(api, e, e->userdata);
206
207     pa_xfree(e);
208 }
209
210 void pa_signal_set_destroy(pa_signal_event *e, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)) {
211     assert(e);
212     e->destroy_callback = _callback;
213 }