4 This file is part of PulseAudio.
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.
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.
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
39 #include <pulsecore/core-error.h>
40 #include <pulse/xmalloc.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/gccmacro.h>
46 #include "mainloop-signal.h"
48 struct pa_signal_event
{
51 struct sigaction saved_sigaction
;
53 void (*saved_handler
)(int sig
);
55 void (*callback
) (pa_mainloop_api
*a
, pa_signal_event
*e
, int sig
, void *userdata
);
57 void (*destroy_callback
) (pa_mainloop_api
*a
, pa_signal_event
*e
, void *userdata
);
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
) {
67 #ifndef HAVE_SIGACTION
68 signal(sig
, signal_handler
);
70 pa_write(signal_pipe
[1], &sig
, sizeof(sig
), NULL
);
73 static void dispatch(pa_mainloop_api
*a
, int sig
) {
76 for (s
= signals
; s
; s
= s
->next
)
79 s
->callback(a
, s
, sig
, s
->userdata
);
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
) {
87 assert(a
&& e
&& f
== PA_IO_EVENT_INPUT
&& e
== io_event
&& fd
== signal_pipe
[0]);
89 if ((r
= pa_read(signal_pipe
[0], &sig
, sizeof(sig
), NULL
)) < 0) {
93 pa_log("read(): %s", pa_cstrerror(errno
));
97 if (r
!= sizeof(sig
)) {
98 pa_log("short read()");
105 int pa_signal_init(pa_mainloop_api
*a
) {
107 assert(!api
&& a
&& signal_pipe
[0] == -1 && signal_pipe
[1] == -1 && !io_event
);
109 if (pipe(signal_pipe
) < 0) {
110 pa_log("pipe(): %s", pa_cstrerror(errno
));
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);
121 io_event
= api
->io_new(api
, signal_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
);
127 void pa_signal_done(void) {
128 assert(api
&& signal_pipe
[0] >= 0 && signal_pipe
[1] >= 0 && io_event
);
131 pa_signal_free(signals
);
133 api
->io_free(io_event
);
136 close(signal_pipe
[0]);
137 close(signal_pipe
[1]);
138 signal_pipe
[0] = signal_pipe
[1] = -1;
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
;
146 #ifdef HAVE_SIGACTION
150 assert(sig
> 0 && _callback
);
152 for (e
= signals
; e
; e
= e
->next
)
156 e
= pa_xmalloc(sizeof(pa_signal_event
));
158 e
->callback
= _callback
;
159 e
->userdata
= userdata
;
160 e
->destroy_callback
= NULL
;
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
;
168 if (sigaction(sig
, &sa
, &e
->saved_sigaction
) < 0)
170 if ((e
->saved_handler
= signal(sig
, signal_handler
)) == SIG_ERR
)
185 void pa_signal_free(pa_signal_event
*e
) {
189 e
->next
->previous
= e
->previous
;
191 e
->previous
->next
= e
->next
;
195 #ifdef HAVE_SIGACTION
196 sigaction(e
->sig
, &e
->saved_sigaction
, NULL
);
198 signal(e
->sig
, e
->saved_handler
);
201 if (e
->destroy_callback
)
202 e
->destroy_callback(api
, e
, e
->userdata
);
207 void pa_signal_set_destroy(pa_signal_event
*e
, void (*_callback
) (pa_mainloop_api
*api
, pa_signal_event
*e
, void *userdata
)) {
209 e
->destroy_callback
= _callback
;