2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
38 #include <pulse/xmalloc.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/core-util.h>
42 #include <pulsecore/i18n.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/macro.h>
46 #include "mainloop-signal.h"
48 struct pa_signal_event
{
51 struct sigaction saved_sigaction
;
53 void (*saved_handler
)(int sig
);
56 pa_signal_cb_t callback
;
57 pa_signal_destroy_cb_t destroy_callback
;
58 pa_signal_event
*previous
, *next
;
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
;
66 static void signal_handler(int sig
) {
71 #ifndef HAVE_SIGACTION
72 signal(sig
, signal_handler
);
75 /* XXX: If writing fails, there's nothing we can do? */
76 (void) pa_write(signal_pipe
[1], &sig
, sizeof(sig
), NULL
);
81 static void dispatch(pa_mainloop_api
*a
, int sig
) {
84 for (s
= signals
; s
; s
= s
->next
)
86 pa_assert(s
->callback
);
87 s
->callback(a
, s
, sig
, s
->userdata
);
92 static void callback(pa_mainloop_api
*a
, pa_io_event
*e
, int fd
, pa_io_event_flags_t f
, void *userdata
) {
98 pa_assert(f
== PA_IO_EVENT_INPUT
);
99 pa_assert(e
== io_event
);
100 pa_assert(fd
== signal_pipe
[0]);
102 if ((r
= pa_read(signal_pipe
[0], &sig
, sizeof(sig
), NULL
)) < 0) {
106 pa_log("read(): %s", pa_cstrerror(errno
));
110 if (r
!= sizeof(sig
)) {
111 pa_log("short read()");
118 int pa_signal_init(pa_mainloop_api
*a
) {
122 pa_assert(signal_pipe
[0] == -1);
123 pa_assert(signal_pipe
[1] == -1);
124 pa_assert(!io_event
);
126 if (pa_pipe_cloexec(signal_pipe
) < 0) {
127 pa_log("pipe(): %s", pa_cstrerror(errno
));
131 pa_make_fd_nonblock(signal_pipe
[0]);
132 pa_make_fd_nonblock(signal_pipe
[1]);
136 pa_assert_se(io_event
= api
->io_new(api
, signal_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
));
141 void pa_signal_done(void) {
143 pa_signal_free(signals
);
147 api
->io_free(io_event
);
151 pa_close_pipe(signal_pipe
);
156 pa_signal_event
* pa_signal_new(int sig
, pa_signal_cb_t _callback
, void *userdata
) {
157 pa_signal_event
*e
= NULL
;
159 #ifdef HAVE_SIGACTION
164 pa_assert(_callback
);
168 for (e
= signals
; e
; e
= e
->next
)
172 e
= pa_xnew(pa_signal_event
, 1);
174 e
->callback
= _callback
;
175 e
->userdata
= userdata
;
176 e
->destroy_callback
= NULL
;
178 #ifdef HAVE_SIGACTION
179 memset(&sa
, 0, sizeof(sa
));
180 sa
.sa_handler
= signal_handler
;
181 sigemptyset(&sa
.sa_mask
);
182 sa
.sa_flags
= SA_RESTART
;
184 if (sigaction(sig
, &sa
, &e
->saved_sigaction
) < 0)
186 if ((e
->saved_handler
= signal(sig
, signal_handler
)) == SIG_ERR
)
200 void pa_signal_free(pa_signal_event
*e
) {
204 e
->next
->previous
= e
->previous
;
206 e
->previous
->next
= e
->next
;
210 #ifdef HAVE_SIGACTION
211 pa_assert_se(sigaction(e
->sig
, &e
->saved_sigaction
, NULL
) == 0);
213 pa_assert_se(signal(e
->sig
, e
->saved_handler
) == signal_handler
);
216 if (e
->destroy_callback
)
217 e
->destroy_callback(api
, e
, e
->userdata
);
222 void pa_signal_set_destroy(pa_signal_event
*e
, pa_signal_destroy_cb_t _callback
) {
225 e
->destroy_callback
= _callback
;