]> code.delx.au - pulseaudio/blob - polyp/mainloop-signal.c
6e79767a9566830622e3527b033c62dc17481944
[pulseaudio] / 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 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 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
34 #include "mainloop-signal.h"
35 #include "util.h"
36 #include "xmalloc.h"
37
38 struct signal_info {
39 int sig;
40 struct sigaction saved_sigaction;
41 void (*callback) (void *id, int signal, void *userdata);
42 void *userdata;
43 struct signal_info *previous, *next;
44 };
45
46 static struct pa_mainloop_api *api = NULL;
47 static int signal_pipe[2] = { -1, -1 };
48 static void* mainloop_source = NULL;
49 static struct signal_info *signals = NULL;
50
51 static void signal_handler(int sig) {
52 write(signal_pipe[1], &sig, sizeof(sig));
53 }
54
55 static void callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
56 assert(a && id && events == PA_MAINLOOP_API_IO_EVENT_INPUT && id == mainloop_source && fd == signal_pipe[0]);
57
58 for (;;) {
59 ssize_t r;
60 int sig;
61 struct signal_info*s;
62
63 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
64 if (errno == EAGAIN)
65 return;
66
67 fprintf(stderr, "signal.c: read(): %s\n", strerror(errno));
68 return;
69 }
70
71 if (r != sizeof(sig)) {
72 fprintf(stderr, "signal.c: short read()\n");
73 return;
74 }
75
76 for (s = signals; s; s = s->next)
77 if (s->sig == sig) {
78 assert(s->callback);
79 s->callback(s, sig, s->userdata);
80 break;
81 }
82 }
83 }
84
85 int pa_signal_init(struct pa_mainloop_api *a) {
86 assert(a);
87 if (pipe(signal_pipe) < 0) {
88 fprintf(stderr, "pipe() failed: %s\n", strerror(errno));
89 return -1;
90 }
91
92 pa_make_nonblock_fd(signal_pipe[0]);
93 pa_make_nonblock_fd(signal_pipe[1]);
94
95 api = a;
96 mainloop_source = api->source_io(api, signal_pipe[0], PA_MAINLOOP_API_IO_EVENT_INPUT, callback, NULL);
97 assert(mainloop_source);
98 return 0;
99 }
100
101 void pa_signal_done(void) {
102 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && mainloop_source);
103
104 api->cancel_io(api, mainloop_source);
105 mainloop_source = NULL;
106
107 close(signal_pipe[0]);
108 close(signal_pipe[1]);
109 signal_pipe[0] = signal_pipe[1] = -1;
110
111 while (signals)
112 pa_signal_unregister(signals);
113
114 api = NULL;
115 }
116
117 void* pa_signal_register(int sig, void (*callback) (void *id, int signal, void *userdata), void *userdata) {
118 struct signal_info *s = NULL;
119 struct sigaction sa;
120 assert(sig > 0 && callback);
121
122 for (s = signals; s; s = s->next)
123 if (s->sig == sig)
124 goto fail;
125
126 s = pa_xmalloc(sizeof(struct signal_info));
127 s->sig = sig;
128 s->callback = callback;
129 s->userdata = userdata;
130
131 memset(&sa, 0, sizeof(sa));
132 sa.sa_handler = signal_handler;
133 sigemptyset(&sa.sa_mask);
134 sa.sa_flags = SA_RESTART;
135
136 if (sigaction(sig, &sa, &s->saved_sigaction) < 0)
137 goto fail;
138
139 s->previous = NULL;
140 s->next = signals;
141 signals = s;
142
143 return s;
144 fail:
145 if (s)
146 pa_xfree(s);
147 return NULL;
148 }
149
150 void pa_signal_unregister(void *id) {
151 struct signal_info *s = id;
152 assert(s);
153
154 if (s->next)
155 s->next->previous = s->previous;
156 if (s->previous)
157 s->previous->next = s->next;
158 else
159 signals = s->next;
160
161 sigaction(s->sig, &s->saved_sigaction, NULL);
162 pa_xfree(s);
163 }