]> code.delx.au - pulseaudio/blob - polyp/mainloop-signal.c
rename src to polyp
[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
37 struct signal_info {
38 int sig;
39 struct sigaction saved_sigaction;
40 void (*callback) (void *id, int signal, void *userdata);
41 void *userdata;
42 struct signal_info *previous, *next;
43 };
44
45 static struct pa_mainloop_api *api = NULL;
46 static int signal_pipe[2] = { -1, -1 };
47 static void* mainloop_source = NULL;
48 static struct signal_info *signals = NULL;
49
50 static void signal_handler(int sig) {
51 write(signal_pipe[1], &sig, sizeof(sig));
52 }
53
54 static void callback(struct pa_mainloop_api*a, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
55 assert(a && id && events == PA_MAINLOOP_API_IO_EVENT_INPUT && id == mainloop_source && fd == signal_pipe[0]);
56
57 for (;;) {
58 ssize_t r;
59 int sig;
60 struct signal_info*s;
61
62 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
63 if (errno == EAGAIN)
64 return;
65
66 fprintf(stderr, "signal.c: read(): %s\n", strerror(errno));
67 return;
68 }
69
70 if (r != sizeof(sig)) {
71 fprintf(stderr, "signal.c: short read()\n");
72 return;
73 }
74
75 for (s = signals; s; s = s->next)
76 if (s->sig == sig) {
77 assert(s->callback);
78 s->callback(s, sig, s->userdata);
79 break;
80 }
81 }
82 }
83
84 int pa_signal_init(struct pa_mainloop_api *a) {
85 assert(a);
86 if (pipe(signal_pipe) < 0) {
87 fprintf(stderr, "pipe() failed: %s\n", strerror(errno));
88 return -1;
89 }
90
91 pa_make_nonblock_fd(signal_pipe[0]);
92 pa_make_nonblock_fd(signal_pipe[1]);
93
94 api = a;
95 mainloop_source = api->source_io(api, signal_pipe[0], PA_MAINLOOP_API_IO_EVENT_INPUT, callback, NULL);
96 assert(mainloop_source);
97 return 0;
98 }
99
100 void pa_signal_done(void) {
101 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && mainloop_source);
102
103 api->cancel_io(api, mainloop_source);
104 mainloop_source = NULL;
105
106 close(signal_pipe[0]);
107 close(signal_pipe[1]);
108 signal_pipe[0] = signal_pipe[1] = -1;
109
110 while (signals)
111 pa_signal_unregister(signals);
112
113 api = NULL;
114 }
115
116 void* pa_signal_register(int sig, void (*callback) (void *id, int signal, void *userdata), void *userdata) {
117 struct signal_info *s = NULL;
118 struct sigaction sa;
119 assert(sig > 0 && callback);
120
121 for (s = signals; s; s = s->next)
122 if (s->sig == sig)
123 goto fail;
124
125 s = malloc(sizeof(struct signal_info));
126 assert(s);
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 free(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 free(s);
163 }