]> git.meshlink.io Git - meshlink/blob - src/event.c
Add meshlink_get_node_reachability().
[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         splay_node_t *node = splay_insert_node(&loop->ios, &io->node);
75         assert(node);
76         (void)node;
77 }
78
79 void io_set(event_loop_t *loop, io_t *io, int flags) {
80         assert(io->cb);
81
82         io->flags = flags;
83
84         if(flags & IO_READ) {
85                 FD_SET(io->fd, &loop->readfds);
86         } else {
87                 FD_CLR(io->fd, &loop->readfds);
88         }
89
90         if(flags & IO_WRITE) {
91                 FD_SET(io->fd, &loop->writefds);
92         } else {
93                 FD_CLR(io->fd, &loop->writefds);
94         }
95 }
96
97 void io_del(event_loop_t *loop, io_t *io) {
98         assert(io->cb);
99
100         loop->deletion = true;
101
102         io_set(loop, io, 0);
103
104         splay_unlink_node(&loop->ios, &io->node);
105         io->cb = NULL;
106 }
107
108 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
109         timeout->cb = cb;
110         timeout->data = data;
111         timeout->node.data = timeout;
112
113         timeout_set(loop, timeout, tv);
114 }
115
116 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
117         assert(timeout->cb);
118
119         if(timerisset(&timeout->tv)) {
120                 splay_unlink_node(&loop->timeouts, &timeout->node);
121         }
122
123         if(!loop->now.tv_sec) {
124                 gettimeofday(&loop->now, NULL);
125         }
126
127         timeradd(&loop->now, tv, &timeout->tv);
128
129         if(!splay_insert_node(&loop->timeouts, &timeout->node)) {
130                 abort();
131         }
132
133         loop->deletion = true;
134 }
135
136 static void timeout_disable(event_loop_t *loop, timeout_t *timeout) {
137         splay_unlink_node(&loop->timeouts, &timeout->node);
138         timeout->tv = (struct timeval) {
139                 0, 0
140         };
141 }
142
143 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
144         if(!timeout->cb) {
145                 return;
146         }
147
148         if(timerisset(&timeout->tv)) {
149                 timeout_disable(loop, timeout);
150         }
151
152         timeout->cb = NULL;
153         loop->deletion = true;
154 }
155
156 static int signal_compare(const signal_t *a, const signal_t *b) {
157         return (int)a->signum - (int)b->signum;
158 }
159
160 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
161         (void)data;
162         (void)flags;
163         unsigned char signum;
164
165         if(read(loop->pipefd[0], &signum, 1) != 1) {
166                 return;
167         }
168
169         signal_t *sig = splay_search(&loop->signals, &(signal_t) {
170                 .signum = signum
171         });
172
173         if(sig) {
174                 sig->cb(loop, sig->data);
175         }
176 }
177
178 static void pipe_init(event_loop_t *loop) {
179         int result = pipe(loop->pipefd);
180         assert(result == 0);
181
182         if(result == 0) {
183                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
184         }
185 }
186
187 static void pipe_exit(event_loop_t *loop) {
188         io_del(loop, &loop->signalio);
189
190         close(loop->pipefd[0]);
191         close(loop->pipefd[1]);
192
193         loop->pipefd[0] = -1;
194         loop->pipefd[1] = -1;
195 }
196
197 void signal_trigger(event_loop_t *loop, signal_t *sig) {
198         uint8_t signum = sig->signum;
199         write(loop->pipefd[1], &signum, 1);
200         return;
201 }
202
203 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
204         assert(!sig->cb);
205
206         sig->cb = cb;
207         sig->data = data;
208         sig->signum = signum;
209         sig->node.data = sig;
210
211         if(loop->pipefd[0] == -1) {
212                 pipe_init(loop);
213         }
214
215         if(!splay_insert_node(&loop->signals, &sig->node)) {
216                 abort();
217         }
218 }
219
220 void signal_del(event_loop_t *loop, signal_t *sig) {
221         assert(sig->cb);
222
223         loop->deletion = true;
224
225         splay_unlink_node(&loop->signals, &sig->node);
226         sig->cb = NULL;
227
228         if(!loop->signals.count && loop->pipefd[0] != -1) {
229                 pipe_exit(loop);
230         }
231 }
232
233 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
234         loop->idle_cb = cb;
235         loop->idle_data = data;
236 }
237
238 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
239         assert(mutex);
240
241         fd_set readable;
242         fd_set writable;
243
244         while(loop->running) {
245                 gettimeofday(&loop->now, NULL);
246                 struct timeval diff, it, *tv = NULL;
247
248                 while(loop->timeouts.head) {
249                         timeout_t *timeout = loop->timeouts.head->data;
250                         timersub(&timeout->tv, &loop->now, &diff);
251
252                         if(diff.tv_sec < 0) {
253                                 timeout_disable(loop, timeout);
254                                 timeout->cb(loop, timeout->data);
255                         } else {
256                                 tv = &diff;
257                                 break;
258                         }
259                 }
260
261                 if(loop->idle_cb) {
262                         it = loop->idle_cb(loop, loop->idle_data);
263
264                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <))) {
265                                 tv = &it;
266                         }
267                 }
268
269                 memcpy(&readable, &loop->readfds, sizeof(readable));
270                 memcpy(&writable, &loop->writefds, sizeof(writable));
271
272                 int fds = 0;
273
274                 if(loop->ios.tail) {
275                         io_t *last = loop->ios.tail->data;
276                         fds = last->fd + 1;
277                 }
278
279                 // release mesh mutex during select
280                 pthread_mutex_unlock(mutex);
281
282                 int n = select(fds, &readable, &writable, NULL, tv);
283
284                 pthread_mutex_lock(mutex);
285
286                 gettimeofday(&loop->now, NULL);
287
288                 if(n < 0) {
289                         if(sockwouldblock(errno)) {
290                                 continue;
291                         } else {
292                                 return false;
293                         }
294                 }
295
296                 if(!n) {
297                         continue;
298                 }
299
300                 // Normally, splay_each allows the current node to be deleted. However,
301                 // it can be that one io callback triggers the deletion of another io,
302                 // so we have to detect this and break the loop.
303
304                 loop->deletion = false;
305
306                 for splay_each(io_t, io, &loop->ios) {
307                         if(FD_ISSET(io->fd, &writable) && io->cb) {
308                                 io->cb(loop, io->data, IO_WRITE);
309                         }
310
311                         if(loop->deletion) {
312                                 break;
313                         }
314
315                         if(FD_ISSET(io->fd, &readable) && io->cb) {
316                                 io->cb(loop, io->data, IO_READ);
317                         }
318
319                         if(loop->deletion) {
320                                 break;
321                         }
322                 }
323         }
324
325         return true;
326 }
327
328 void event_flush_output(event_loop_t *loop) {
329         for splay_each(io_t, io, &loop->ios)
330                 if(FD_ISSET(io->fd, &loop->writefds)) {
331                         io->cb(loop, io->data, IO_WRITE);
332                 }
333 }
334
335 void event_loop_start(event_loop_t *loop) {
336         loop->running = true;
337 }
338
339 void event_loop_stop(event_loop_t *loop) {
340         loop->running = false;
341 }
342
343 void event_loop_init(event_loop_t *loop) {
344         loop->ios.compare = (splay_compare_t)io_compare;
345         loop->timeouts.compare = (splay_compare_t)timeout_compare;
346         loop->signals.compare = (splay_compare_t)signal_compare;
347         loop->pipefd[0] = -1;
348         loop->pipefd[1] = -1;
349         gettimeofday(&loop->now, NULL);
350 }
351
352 void event_loop_exit(event_loop_t *loop) {
353         assert(!loop->ios.count);
354         assert(!loop->timeouts.count);
355         assert(!loop->signals.count);
356
357         for splay_each(io_t, io, &loop->ios) {
358                 splay_unlink_node(&loop->ios, node);
359         }
360
361         for splay_each(timeout_t, timeout, &loop->timeouts) {
362                 splay_unlink_node(&loop->timeouts, node);
363         }
364
365         for splay_each(signal_t, signal, &loop->signals) {
366                 splay_unlink_node(&loop->signals, node);
367         }
368 }