]> code.delx.au - pulseaudio/blob - src/polyp/mainloop-signal.c
remove superfluous log message
[pulseaudio] / src / polyp / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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.
10
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.
15
16 You should have received a copy of the GNU Lesser 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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38
39 #include <polypcore/util.h>
40 #include <polypcore/xmalloc.h>
41 #include <polypcore/log.h>
42 #include <polypcore/gccmacro.h>
43
44 #include "mainloop-signal.h"
45
46 struct pa_signal_event {
47 int sig;
48 #ifdef HAVE_SIGACTION
49 struct sigaction saved_sigaction;
50 #else
51 void (*saved_handler)(int sig);
52 #endif
53 void (*callback) (pa_mainloop_api*a, pa_signal_event *e, int sig, void *userdata);
54 void *userdata;
55 void (*destroy_callback) (pa_mainloop_api*a, pa_signal_event*e, void *userdata);
56 pa_signal_event *previous, *next;
57 };
58
59 static pa_mainloop_api *api = NULL;
60 static int signal_pipe[2] = { -1, -1 };
61 static pa_io_event* io_event = NULL;
62 static pa_signal_event *signals = NULL;
63
64 static void signal_handler(int sig) {
65 #ifndef HAVE_SIGACTION
66 signal(sig, signal_handler);
67 #endif
68 pa_write(signal_pipe[1], &sig, sizeof(sig));
69 }
70
71 static void dispatch(pa_mainloop_api*a, int sig) {
72 pa_signal_event*s;
73
74 for (s = signals; s; s = s->next)
75 if (s->sig == sig) {
76 assert(s->callback);
77 s->callback(a, s, sig, s->userdata);
78 break;
79 }
80 }
81
82 static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
83 ssize_t r;
84 int sig;
85 assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
86
87 if ((r = pa_read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
88 if (errno == EAGAIN)
89 return;
90
91 pa_log(__FILE__": read(): %s", strerror(errno));
92 return;
93 }
94
95 if (r != sizeof(sig)) {
96 pa_log(__FILE__": short read()");
97 return;
98 }
99
100 dispatch(a, sig);
101 }
102
103 int pa_signal_init(pa_mainloop_api *a) {
104
105 assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event);
106
107 if (pipe(signal_pipe) < 0) {
108 pa_log(__FILE__": pipe() failed: %s", strerror(errno));
109 return -1;
110 }
111
112 pa_make_nonblock_fd(signal_pipe[0]);
113 pa_make_nonblock_fd(signal_pipe[1]);
114 pa_fd_set_cloexec(signal_pipe[0], 1);
115 pa_fd_set_cloexec(signal_pipe[1], 1);
116
117 api = a;
118
119 io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
120 assert(io_event);
121
122 return 0;
123 }
124
125 void pa_signal_done(void) {
126 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && io_event);
127
128 while (signals)
129 pa_signal_free(signals);
130
131 api->io_free(io_event);
132 io_event = NULL;
133
134 close(signal_pipe[0]);
135 close(signal_pipe[1]);
136 signal_pipe[0] = signal_pipe[1] = -1;
137
138 api = NULL;
139 }
140
141 pa_signal_event* pa_signal_new(int sig, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata), void *userdata) {
142 pa_signal_event *e = NULL;
143
144 #ifdef HAVE_SIGACTION
145 struct sigaction sa;
146 #endif
147
148 assert(sig > 0 && _callback);
149
150 for (e = signals; e; e = e->next)
151 if (e->sig == sig)
152 goto fail;
153
154 e = pa_xmalloc(sizeof(pa_signal_event));
155 e->sig = sig;
156 e->callback = _callback;
157 e->userdata = userdata;
158 e->destroy_callback = NULL;
159
160 #ifdef HAVE_SIGACTION
161 memset(&sa, 0, sizeof(sa));
162 sa.sa_handler = signal_handler;
163 sigemptyset(&sa.sa_mask);
164 sa.sa_flags = SA_RESTART;
165
166 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
167 #else
168 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
169 #endif
170 goto fail;
171
172 e->previous = NULL;
173 e->next = signals;
174 signals = e;
175
176 return e;
177 fail:
178 if (e)
179 pa_xfree(e);
180 return NULL;
181 }
182
183 void pa_signal_free(pa_signal_event *e) {
184 assert(e);
185
186 if (e->next)
187 e->next->previous = e->previous;
188 if (e->previous)
189 e->previous->next = e->next;
190 else
191 signals = e->next;
192
193 #ifdef HAVE_SIGACTION
194 sigaction(e->sig, &e->saved_sigaction, NULL);
195 #else
196 signal(e->sig, e->saved_handler);
197 #endif
198
199 if (e->destroy_callback)
200 e->destroy_callback(api, e, e->userdata);
201
202 pa_xfree(e);
203 }
204
205 void pa_signal_set_destroy(pa_signal_event *e, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)) {
206 assert(e);
207 e->destroy_callback = _callback;
208 }