4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU 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 polypaudio 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 General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include "mainloop-signal.h"
38 struct pa_signal_event
{
40 struct sigaction saved_sigaction
;
41 void (*callback
) (struct pa_mainloop_api
*a
, struct pa_signal_event
*e
, int signal
, void *userdata
);
43 void (*destroy_callback
) (struct pa_mainloop_api
*a
, struct pa_signal_event
*e
, void *userdata
);
44 struct pa_signal_event
*previous
, *next
;
47 static struct pa_mainloop_api
*api
= NULL
;
48 static int signal_pipe
[2] = { -1, -1 };
49 static struct pa_io_event
* io_event
= NULL
;
50 static struct pa_signal_event
*signals
= NULL
;
52 static void signal_handler(int sig
) {
53 write(signal_pipe
[1], &sig
, sizeof(sig
));
56 static void callback(struct pa_mainloop_api
*a
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
59 struct pa_signal_event
*s
;
60 assert(a
&& e
&& f
== PA_IO_EVENT_INPUT
&& e
== io_event
&& fd
== signal_pipe
[0]);
63 if ((r
= read(signal_pipe
[0], &sig
, sizeof(sig
))) < 0) {
67 fprintf(stderr
, "signal.c: read(): %s\n", strerror(errno
));
71 if (r
!= sizeof(sig
)) {
72 fprintf(stderr
, "signal.c: short read()\n");
76 for (s
= signals
; s
; s
= s
->next
)
79 s
->callback(a
, s
, sig
, s
->userdata
);
84 int pa_signal_init(struct pa_mainloop_api
*a
) {
85 assert(!api
&& a
&& signal_pipe
[0] == -1 && signal_pipe
[1] == -1 && !io_event
);
87 if (pipe(signal_pipe
) < 0) {
88 fprintf(stderr
, "pipe() failed: %s\n", strerror(errno
));
92 pa_make_nonblock_fd(signal_pipe
[0]);
93 pa_make_nonblock_fd(signal_pipe
[1]);
94 pa_fd_set_cloexec(signal_pipe
[0], 1);
95 pa_fd_set_cloexec(signal_pipe
[1], 1);
98 io_event
= api
->io_new(api
, signal_pipe
[0], PA_IO_EVENT_INPUT
, callback
, NULL
);
103 void pa_signal_done(void) {
104 assert(api
&& signal_pipe
[0] >= 0 && signal_pipe
[1] >= 0 && io_event
);
107 pa_signal_free(signals
);
110 api
->io_free(io_event
);
113 close(signal_pipe
[0]);
114 close(signal_pipe
[1]);
115 signal_pipe
[0] = signal_pipe
[1] = -1;
120 struct pa_signal_event
* pa_signal_new(int sig
, void (*callback
) (struct pa_mainloop_api
*api
, struct pa_signal_event
*e
, int sig
, void *userdata
), void *userdata
) {
121 struct pa_signal_event
*e
= NULL
;
123 assert(sig
> 0 && callback
);
125 for (e
= signals
; e
; e
= e
->next
)
129 e
= pa_xmalloc(sizeof(struct pa_signal_event
));
131 e
->callback
= callback
;
132 e
->userdata
= userdata
;
133 e
->destroy_callback
= NULL
;
135 memset(&sa
, 0, sizeof(sa
));
136 sa
.sa_handler
= signal_handler
;
137 sigemptyset(&sa
.sa_mask
);
138 sa
.sa_flags
= SA_RESTART
;
140 if (sigaction(sig
, &sa
, &e
->saved_sigaction
) < 0)
154 void pa_signal_free(struct pa_signal_event
*e
) {
158 e
->next
->previous
= e
->previous
;
160 e
->previous
->next
= e
->next
;
164 sigaction(e
->sig
, &e
->saved_sigaction
, NULL
);
166 if (e
->destroy_callback
)
167 e
->destroy_callback(api
, e
, e
->userdata
);
172 void pa_signal_set_destroy(struct pa_signal_event
*e
, void (*callback
) (struct pa_mainloop_api
*api
, struct pa_signal_event
*e
, void *userdata
)) {
174 e
->destroy_callback
= callback
;