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