]> git.meshlink.io Git - meshlink/blob - src/event.c
82133eb471826ce1d9921d0f4a3fbac83df9387a
[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 #ifndef EVENT_CLOCK
30 #if defined(CLOCK_MONOTONIC_RAW) && defined(__x86_64__)
31 #define EVENT_CLOCK CLOCK_MONOTONIC_RAW
32 #else
33 #define EVENT_CLOCK CLOCK_MONOTONIC
34 #endif
35 #endif
36
37 static void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *r) {
38         r->tv_sec = a->tv_sec + b->tv_sec;
39         r->tv_nsec = a->tv_nsec + b->tv_nsec;
40
41         if(r->tv_nsec > 1000000000) {
42                 r->tv_sec++, r->tv_nsec -= 1000000000;
43         }
44 }
45
46 static void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *r) {
47         r->tv_sec = a->tv_sec - b->tv_sec;
48         r->tv_nsec = a->tv_nsec - b->tv_nsec;
49
50         if(r->tv_nsec < 0) {
51                 r->tv_sec--, r->tv_nsec += 1000000000;
52         }
53 }
54
55 static bool timespec_lt(const struct timespec *a, const struct timespec *b) {
56         if(a->tv_sec == b->tv_sec) {
57                 return a->tv_nsec < b->tv_nsec;
58         } else {
59                 return a->tv_sec < b->tv_sec;
60         }
61 }
62
63 static void timespec_clear(struct timespec *a) {
64         a->tv_sec = 0;
65 }
66
67 static int io_compare(const io_t *a, const io_t *b) {
68         return a->fd - b->fd;
69 }
70
71 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
72         if(a->tv.tv_sec < b->tv.tv_sec) {
73                 return -1;
74         } else if(a->tv.tv_sec > b->tv.tv_sec) {
75                 return 1;
76         } else if(a->tv.tv_nsec < b->tv.tv_nsec) {
77                 return -1;
78         } else if(a->tv.tv_nsec > b->tv.tv_nsec) {
79                 return 1;
80         } else if(a < b) {
81                 return -1;
82         } else if(a > b) {
83                 return 1;
84         } else {
85                 return 0;
86         }
87 }
88
89 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
90         assert(!io->cb);
91
92         io->fd = fd;
93         io->cb = cb;
94         io->data = data;
95         io->node.data = io;
96
97         io_set(loop, io, flags);
98
99         splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
100         assert(node);
101         (void)node;
102 }
103
104 void io_set(event_loop_t *loop, io_t *io, int flags) {
105         assert(io->cb);
106
107         io->flags = flags;
108
109         if(flags & IO_READ) {
110                 FD_SET(io->fd, &loop->readfds);
111         } else {
112                 FD_CLR(io->fd, &loop->readfds);
113         }
114
115         if(flags & IO_WRITE) {
116                 FD_SET(io->fd, &loop->writefds);
117         } else {
118                 FD_CLR(io->fd, &loop->writefds);
119         }
120 }
121
122 void io_del(event_loop_t *loop, io_t *io) {
123         assert(io->cb);
124
125         loop->deletion = true;
126
127         io_set(loop, io, 0);
128
129         splay_unlink_node(&loop->ios, &io->node);
130         io->cb = NULL;
131 }
132
133 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timespec *tv) {
134         timeout->cb = cb;
135         timeout->data = data;
136
137         timeout_set(loop, timeout, tv);
138 }
139
140 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timespec *tv) {
141         assert(timeout->cb);
142
143         if(timeout->node.data) {
144                 splay_unlink_node(&loop->timeouts, &timeout->node);
145         } else {
146                 timeout->node.data = timeout;
147         }
148
149         if(!loop->now.tv_sec) {
150                 clock_gettime(EVENT_CLOCK, &loop->now);
151         }
152
153         timespec_add(&loop->now, tv, &timeout->tv);
154
155         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
156                 abort();
157         }
158
159         loop->deletion = true;
160 }
161
162 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
163         if(timeout->node.data) {
164                 splay_unlink_node(&loop->timeouts, &timeout->node);
165                 timeout->node.data = NULL;
166         }
167
168         timespec_clear(&timeout->tv);
169 }
170
171 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
172         if(!timeout->cb) {
173                 return;
174         }
175
176         if(timeout->node.data) {
177                 timeout_disable(loop, timeout);
178         }
179
180         timeout->cb = NULL;
181         loop->deletion = true;
182 }
183
184 static int signal_compare(const signal_t *a, const signal_t *b) {
185         return (int)a->signum - (int)b->signum;
186 }
187
188 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
189         (void)data;
190         (void)flags;
191         unsigned char signum;
192
193         if(read(loop->pipefd[0], &signum, 1) != 1) {
194                 return;
195         }
196
197         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
198                 .signum = signum
199         });
200
201         if(sig) {
202 #ifdef HAVE_STDATOMIC_H
203                 atomic_flag_clear(&sig->set);
204 #endif
205                 sig->cb(loop, sig->data);
206         }
207 }
208
209 static void pipe_init(event_loop_t *loop) {
210         int result = pipe(loop->pipefd);
211         assert(result == 0);
212
213         if(result == 0) {
214 #ifdef O_NONBLOCK
215                 fcntl(loop->pipefd[0], F_SETFL, O_NONBLOCK);
216                 fcntl(loop->pipefd[1], F_SETFL, O_NONBLOCK);
217 #endif
218                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
219         }
220 }
221
222 static void pipe_exit(event_loop_t *loop) {
223         io_del(loop, &loop->signalio);
224
225         close(loop->pipefd[0]);
226         close(loop->pipefd[1]);
227
228         loop->pipefd[0] = -1;
229         loop->pipefd[1] = -1;
230 }
231
232 void signal_trigger(event_loop_t *loop, signal_t *sig) {
233 #ifdef HAVE_STDATOMIC_H
234
235         if(atomic_flag_test_and_set(&sig->set)) {
236                 return;
237         }
238
239 #endif
240
241         uint8_t signum = sig->signum;
242         write(loop->pipefd[1], &signum, 1);
243         return;
244 }
245
246 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
247         assert(!sig->cb);
248
249         sig->cb = cb;
250         sig->data = data;
251         sig->signum = signum;
252         sig->node.data = sig;
253
254 #ifdef HAVE_STDATOMIC_H
255         atomic_flag_clear(&sig->set);
256 #endif
257
258         if(loop->pipefd[0] == -1) {
259                 pipe_init(loop);
260         }
261
262         if(!splay_insert_node(&loop->signals, &sig->node)) {
263                 abort();
264         }
265 }
266
267 void signal_del(event_loop_t *loop, signal_t *sig) {
268         assert(sig->cb);
269
270         loop->deletion = true;
271
272         splay_unlink_node(&loop->signals, &sig->node);
273         sig->cb = NULL;
274
275         if(!loop->signals.count && loop->pipefd[0] != -1) {
276                 pipe_exit(loop);
277         }
278 }
279
280 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
281         loop->idle_cb = cb;
282         loop->idle_data = data;
283 }
284
285 static void check_bad_fds(event_loop_t *loop) {
286         // Just call all registered callbacks and have them check their fds
287
288         do {
289                 loop->deletion = false;
290
291                 for splay_each(io_t, io, &loop->ios) {
292                         if(io->flags & IO_WRITE) {
293                                 io->cb(loop, io->data, IO_WRITE);
294                         }
295
296                         if(loop->deletion) {
297                                 break;
298                         }
299
300                         if(io->flags & IO_READ) {
301                                 io->cb(loop, io->data, IO_READ);
302                         }
303
304                         if(loop->deletion) {
305                                 break;
306                         }
307                 }
308         } while(loop->deletion);
309
310         // Rebuild the fdsets
311
312         memset(&loop->readfds, 0, sizeof(loop->readfds));
313         memset(&loop->writefds, 0, sizeof(loop->writefds));
314
315         for splay_each(io_t, io, &loop->ios) {
316                 if(io->flags & IO_READ) {
317                         FD_SET(io->fd, &loop->readfds);
318                         io->cb(loop, io->data, IO_READ);
319                 }
320
321                 if(io->flags & IO_WRITE) {
322                         FD_SET(io->fd, &loop->writefds);
323                         io->cb(loop, io->data, IO_WRITE);
324                 }
325
326         }
327 }
328
329 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
330         assert(mutex);
331
332         fd_set readable;
333         fd_set writable;
334         int errors = 0;
335
336         while(loop->running) {
337                 clock_gettime(EVENT_CLOCK, &loop->now);
338                 struct timespec it, ts = {3600, 0};
339
340                 while(loop->timeouts.head) {
341                         timeout_t *timeout = loop->timeouts.head->data;
342
343                         if(timespec_lt(&timeout->tv, &loop->now)) {
344                                 timeout_disable(loop, timeout);
345                                 timeout->cb(loop, timeout->data);
346                         } else {
347                                 timespec_sub(&timeout->tv, &loop->now, &ts);
348                                 break;
349                         }
350                 }
351
352                 if(loop->idle_cb) {
353                         it = loop->idle_cb(loop, loop->idle_data);
354
355                         if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
356                                 ts = it;
357                         }
358                 }
359
360                 memcpy(&readable, &loop->readfds, sizeof(readable));
361                 memcpy(&writable, &loop->writefds, sizeof(writable));
362
363                 int fds = 0;
364
365                 if(loop->ios.tail) {
366                         io_t *last = loop->ios.tail->data;
367                         fds = last->fd + 1;
368                 }
369
370                 // release mesh mutex during select
371                 pthread_mutex_unlock(mutex);
372
373 #ifdef HAVE_PSELECT
374                 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
375 #else
376                 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
377                 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
378 #endif
379
380                 if(pthread_mutex_lock(mutex) != 0) {
381                         abort();
382                 }
383
384                 clock_gettime(EVENT_CLOCK, &loop->now);
385
386                 if(n < 0) {
387                         if(sockwouldblock(errno)) {
388                                 continue;
389                         } else {
390                                 errors++;
391
392                                 if(errors > 10) {
393                                         return false;
394                                 }
395
396                                 check_bad_fds(loop);
397                                 continue;
398                         }
399                 }
400
401                 errors = 0;
402
403                 if(!n) {
404                         continue;
405                 }
406
407                 // Normally, splay_each allows the current node to be deleted. However,
408                 // it can be that one io callback triggers the deletion of another io,
409                 // so we have to detect this and break the loop.
410
411                 loop->deletion = false;
412
413                 for splay_each(io_t, io, &loop->ios) {
414                         if(FD_ISSET(io->fd, &writable) && io->cb) {
415                                 io->cb(loop, io->data, IO_WRITE);
416                         }
417
418                         if(loop->deletion) {
419                                 break;
420                         }
421
422                         if(FD_ISSET(io->fd, &readable) && io->cb) {
423                                 io->cb(loop, io->data, IO_READ);
424                         }
425
426                         if(loop->deletion) {
427                                 break;
428                         }
429                 }
430         }
431
432         return true;
433 }
434
435 void event_loop_start(event_loop_t *loop) {
436         loop->running = true;
437 }
438
439 void event_loop_stop(event_loop_t *loop) {
440         loop->running = false;
441 }
442
443 void event_loop_init(event_loop_t *loop) {
444         loop->ios.compare = (splay_compare_t)io_compare;
445         loop->timeouts.compare = (splay_compare_t)timeout_compare;
446         loop->signals.compare = (splay_compare_t)signal_compare;
447         loop->pipefd[0] = -1;
448         loop->pipefd[1] = -1;
449         clock_gettime(EVENT_CLOCK, &loop->now);
450 }
451
452 void event_loop_exit(event_loop_t *loop) {
453         assert(!loop->ios.count);
454         assert(!loop->timeouts.count);
455         assert(!loop->signals.count);
456
457         for splay_each(io_t, io, &loop->ios) {
458                 splay_unlink_node(&loop->ios, splay_node);
459         }
460
461         for splay_each(timeout_t, timeout, &loop->timeouts) {
462                 splay_unlink_node(&loop->timeouts, splay_node);
463         }
464
465         for splay_each(signal_t, signal, &loop->signals) {
466                 splay_unlink_node(&loop->signals, splay_node);
467         }
468 }