]> git.meshlink.io Git - meshlink/blob - src/event.c
Fix signal pipe creation when compiling with -DNDEBUG.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "dropin.h"
23 #include "event.h"
24 #include "net.h"
25 #include "splay_tree.h"
26 #include "utils.h"
27 #include "xalloc.h"
28
29 static int io_compare(const io_t *a, const io_t *b) {
30         return a->fd - b->fd;
31 }
32
33 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
34         struct timeval diff;
35         timersub(&a->tv, &b->tv, &diff);
36
37         if(diff.tv_sec < 0) {
38                 return -1;
39         }
40
41         if(diff.tv_sec > 0) {
42                 return 1;
43         }
44
45         if(diff.tv_usec < 0) {
46                 return -1;
47         }
48
49         if(diff.tv_usec > 0) {
50                 return 1;
51         }
52
53         if(a < b) {
54                 return -1;
55         }
56
57         if(a > b) {
58                 return 1;
59         }
60
61         return 0;
62 }
63
64 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
65         assert(!io->cb);
66
67         io->fd = fd;
68         io->cb = cb;
69         io->data = data;
70         io->node.data = io;
71
72         io_set(loop, io, flags);
73
74         assert(splay_insert_node(&loop->ios, &io->node));
75 }
76
77 void io_set(event_loop_t *loop, io_t *io, int flags) {
78         assert(io->cb);
79
80         io->flags = flags;
81
82         if(flags & IO_READ) {
83                 FD_SET(io->fd, &loop->readfds);
84         } else {
85                 FD_CLR(io->fd, &loop->readfds);
86         }
87
88         if(flags & IO_WRITE) {
89                 FD_SET(io->fd, &loop->writefds);
90         } else {
91                 FD_CLR(io->fd, &loop->writefds);
92         }
93 }
94
95 void io_del(event_loop_t *loop, io_t *io) {
96         assert(io->cb);
97
98         loop->deletion = true;
99
100         io_set(loop, io, 0);
101
102         splay_unlink_node(&loop->ios, &io->node);
103         io->cb = NULL;
104 }
105
106 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
107         timeout->cb = cb;
108         timeout->data = data;
109         timeout->node.data = timeout;
110
111         timeout_set(loop, timeout, tv);
112 }
113
114 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
115         assert(timeout->cb);
116
117         if(timerisset(&timeout->tv)) {
118                 splay_unlink_node(&loop->timeouts, &timeout->node);
119         }
120
121         if(!loop->now.tv_sec) {
122                 gettimeofday(&loop->now, NULL);
123         }
124
125         timeradd(&loop->now, tv, &timeout->tv);
126
127         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
128                 abort();
129         }
130
131         loop->deletion = true;
132 }
133
134 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
135         splay_unlink_node(&loop->timeouts, &timeout->node);
136         timeout->tv = (struct timeval) {
137                 0, 0
138         };
139 }
140
141 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
142         if(!timeout->cb) {
143                 return;
144         }
145
146         if(timerisset(&timeout->tv)) {
147                 timeout_disable(loop, timeout);
148         }
149
150         timeout->cb = NULL;
151         loop->deletion = true;
152 }
153
154 static int signal_compare(const signal_t *a, const signal_t *b) {
155         return (int)a->signum - (int)b->signum;
156 }
157
158 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
159         (void)data;
160         (void)flags;
161         unsigned char signum;
162
163         if(read(loop->pipefd[0], &signum, 1) != 1) {
164                 return;
165         }
166
167         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
168                 .signum = signum
169         });
170
171         if(sig) {
172                 sig->cb(loop, sig->data);
173         }
174 }
175
176 static void pipe_init(event_loop_t *loop) {
177         int result = pipe(loop->pipefd);
178         assert(result == 0);
179
180         if(result == 0) {
181                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
182         }
183 }
184
185 static void pipe_exit(event_loop_t *loop) {
186         io_del(loop, &loop->signalio);
187
188         close(loop->pipefd[0]);
189         close(loop->pipefd[1]);
190
191         loop->pipefd[0] = -1;
192         loop->pipefd[1] = -1;
193 }
194
195 void signal_trigger(event_loop_t *loop, signal_t *sig) {
196         uint8_t signum = sig->signum;
197         write(loop->pipefd[1], &signum, 1);
198         return;
199 }
200
201 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
202         assert(!sig->cb);
203
204         sig->cb = cb;
205         sig->data = data;
206         sig->signum = signum;
207         sig->node.data = sig;
208
209         if(loop->pipefd[0] == -1) {
210                 pipe_init(loop);
211         }
212
213         if(!splay_insert_node(&loop->signals, &sig->node)) {
214                 abort();
215         }
216 }
217
218 void signal_del(event_loop_t *loop, signal_t *sig) {
219         assert(sig->cb);
220
221         loop->deletion = true;
222
223         splay_unlink_node(&loop->signals, &sig->node);
224         sig->cb = NULL;
225
226         if(!loop->signals.count && loop->pipefd[0] != -1) {
227                 pipe_exit(loop);
228         }
229 }
230
231 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
232         loop->idle_cb = cb;
233         loop->idle_data = data;
234 }
235
236 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
237         assert(mutex);
238
239         fd_set readable;
240         fd_set writable;
241
242         while(loop->running) {
243                 gettimeofday(&loop->now, NULL);
244                 struct timeval diff, it, *tv = NULL;
245
246                 while(loop->timeouts.head) {
247                         timeout_t *timeout = loop->timeouts.head->data;
248                         timersub(&timeout->tv, &loop->now, &diff);
249
250                         if(diff.tv_sec < 0) {
251                                 timeout_disable(loop, timeout);
252                                 timeout->cb(loop, timeout->data);
253                         } else {
254                                 tv = &diff;
255                                 break;
256                         }
257                 }
258
259                 if(loop->idle_cb) {
260                         it = loop->idle_cb(loop, loop->idle_data);
261
262                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
263                                 tv = &it;
264                         }
265                 }
266
267                 memcpy(&readable, &loop->readfds, sizeof(readable));
268                 memcpy(&writable, &loop->writefds, sizeof(writable));
269
270                 int fds = 0;
271
272                 if(loop->ios.tail) {
273                         io_t *last = loop->ios.tail->data;
274                         fds = last->fd + 1;
275                 }
276
277                 // release mesh mutex during select
278                 pthread_mutex_unlock(mutex);
279
280                 int n = select(fds, &readable, &writable, NULL, tv);
281
282                 pthread_mutex_lock(mutex);
283
284                 gettimeofday(&loop->now, NULL);
285
286                 if(n < 0) {
287                         if(sockwouldblock(errno)) {
288                                 continue;
289                         } else {
290                                 return false;
291                         }
292                 }
293
294                 if(!n) {
295                         continue;
296                 }
297
298                 // Normally, splay_each allows the current node to be deleted. However,
299                 // it can be that one io callback triggers the deletion of another io,
300                 // so we have to detect this and break the loop.
301
302                 loop->deletion = false;
303
304                 for splay_each(io_t, io, &loop->ios) {
305                         if(FD_ISSET(io->fd, &writable) && io->cb) {
306                                 io->cb(loop, io->data, IO_WRITE);
307                         }
308
309                         if(loop->deletion) {
310                                 break;
311                         }
312
313                         if(FD_ISSET(io->fd, &readable) && io->cb) {
314                                 io->cb(loop, io->data, IO_READ);
315                         }
316
317                         if(loop->deletion) {
318                                 break;
319                         }
320                 }
321         }
322
323         return true;
324 }
325
326 void event_flush_output(event_loop_t *loop) {
327         for splay_each(io_t, io, &loop->ios)
328                 if(FD_ISSET(io->fd, &loop->writefds)) {
329                         io->cb(loop, io->data, IO_WRITE);
330                 }
331 }
332
333 void event_loop_start(event_loop_t *loop) {
334         loop->running = true;
335 }
336
337 void event_loop_stop(event_loop_t *loop) {
338         loop->running = false;
339 }
340
341 void event_loop_init(event_loop_t *loop) {
342         loop->ios.compare = (splay_compare_t)io_compare;
343         loop->timeouts.compare = (splay_compare_t)timeout_compare;
344         loop->signals.compare = (splay_compare_t)signal_compare;
345         loop->pipefd[0] = -1;
346         loop->pipefd[1] = -1;
347         gettimeofday(&loop->now, NULL);
348 }
349
350 void event_loop_exit(event_loop_t *loop) {
351         assert(!loop->ios.count);
352         assert(!loop->timeouts.count);
353         assert(!loop->signals.count);
354
355         for splay_each(io_t, io, &loop->ios) {
356                 splay_unlink_node(&loop->ios, node);
357         }
358
359         for splay_each(timeout_t, timeout, &loop->timeouts) {
360                 splay_unlink_node(&loop->timeouts, node);
361         }
362
363         for splay_each(signal_t, signal, &loop->signals) {
364                 splay_unlink_node(&loop->signals, node);
365         }
366 }