]> git.meshlink.io Git - meshlink/blob - src/event.c
Only reset UDP SPTPS sessions if the session ID changed.
[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                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
215         }
216 }
217
218 static void pipe_exit(event_loop_t *loop) {
219         io_del(loop, &loop->signalio);
220
221         close(loop->pipefd[0]);
222         close(loop->pipefd[1]);
223
224         loop->pipefd[0] = -1;
225         loop->pipefd[1] = -1;
226 }
227
228 void signal_trigger(event_loop_t *loop, signal_t *sig) {
229 #ifdef HAVE_STDATOMIC_H
230
231         if(atomic_flag_test_and_set(&sig->set)) {
232                 return;
233         }
234
235 #endif
236
237         uint8_t signum = sig->signum;
238         write(loop->pipefd[1], &signum, 1);
239         return;
240 }
241
242 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
243         assert(!sig->cb);
244
245         sig->cb = cb;
246         sig->data = data;
247         sig->signum = signum;
248         sig->node.data = sig;
249
250 #ifdef HAVE_STDATOMIC_H
251         atomic_flag_clear(&sig->set);
252 #endif
253
254         if(loop->pipefd[0] == -1) {
255                 pipe_init(loop);
256         }
257
258         if(!splay_insert_node(&loop->signals, &sig->node)) {
259                 abort();
260         }
261 }
262
263 void signal_del(event_loop_t *loop, signal_t *sig) {
264         assert(sig->cb);
265
266         loop->deletion = true;
267
268         splay_unlink_node(&loop->signals, &sig->node);
269         sig->cb = NULL;
270
271         if(!loop->signals.count && loop->pipefd[0] != -1) {
272                 pipe_exit(loop);
273         }
274 }
275
276 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
277         loop->idle_cb = cb;
278         loop->idle_data = data;
279 }
280
281 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
282         assert(mutex);
283
284         fd_set readable;
285         fd_set writable;
286
287         while(loop->running) {
288                 clock_gettime(EVENT_CLOCK, &loop->now);
289                 struct timespec it, ts = {3600, 0};
290
291                 while(loop->timeouts.head) {
292                         timeout_t *timeout = loop->timeouts.head->data;
293
294                         if(timespec_lt(&timeout->tv, &loop->now)) {
295                                 timeout_disable(loop, timeout);
296                                 timeout->cb(loop, timeout->data);
297                         } else {
298                                 timespec_sub(&timeout->tv, &loop->now, &ts);
299                                 break;
300                         }
301                 }
302
303                 if(loop->idle_cb) {
304                         it = loop->idle_cb(loop, loop->idle_data);
305
306                         if(it.tv_sec >= 0 && timespec_lt(&it, &ts)) {
307                                 ts = it;
308                         }
309                 }
310
311                 memcpy(&readable, &loop->readfds, sizeof(readable));
312                 memcpy(&writable, &loop->writefds, sizeof(writable));
313
314                 int fds = 0;
315
316                 if(loop->ios.tail) {
317                         io_t *last = loop->ios.tail->data;
318                         fds = last->fd + 1;
319                 }
320
321                 // release mesh mutex during select
322                 pthread_mutex_unlock(mutex);
323
324 #ifdef HAVE_PSELECT
325                 int n = pselect(fds, &readable, &writable, NULL, &ts, NULL);
326 #else
327                 struct timeval tv = {ts.tv_sec, ts.tv_nsec / 1000};
328                 int n = select(fds, &readable, &writable, NULL, (struct timeval *)&tv);
329 #endif
330
331                 if(pthread_mutex_lock(mutex) != 0) {
332                         abort();
333                 }
334
335                 clock_gettime(EVENT_CLOCK, &loop->now);
336
337                 if(n < 0) {
338                         if(sockwouldblock(errno)) {
339                                 continue;
340                         } else {
341                                 return false;
342                         }
343                 }
344
345                 if(!n) {
346                         continue;
347                 }
348
349                 // Normally, splay_each allows the current node to be deleted. However,
350                 // it can be that one io callback triggers the deletion of another io,
351                 // so we have to detect this and break the loop.
352
353                 loop->deletion = false;
354
355                 for splay_each(io_t, io, &loop->ios) {
356                         if(FD_ISSET(io->fd, &writable) && io->cb) {
357                                 io->cb(loop, io->data, IO_WRITE);
358                         }
359
360                         if(loop->deletion) {
361                                 break;
362                         }
363
364                         if(FD_ISSET(io->fd, &readable) && io->cb) {
365                                 io->cb(loop, io->data, IO_READ);
366                         }
367
368                         if(loop->deletion) {
369                                 break;
370                         }
371                 }
372         }
373
374         return true;
375 }
376
377 void event_loop_start(event_loop_t *loop) {
378         loop->running = true;
379 }
380
381 void event_loop_stop(event_loop_t *loop) {
382         loop->running = false;
383 }
384
385 void event_loop_init(event_loop_t *loop) {
386         loop->ios.compare = (splay_compare_t)io_compare;
387         loop->timeouts.compare = (splay_compare_t)timeout_compare;
388         loop->signals.compare = (splay_compare_t)signal_compare;
389         loop->pipefd[0] = -1;
390         loop->pipefd[1] = -1;
391         clock_gettime(EVENT_CLOCK, &loop->now);
392 }
393
394 void event_loop_exit(event_loop_t *loop) {
395         assert(!loop->ios.count);
396         assert(!loop->timeouts.count);
397         assert(!loop->signals.count);
398
399         for splay_each(io_t, io, &loop->ios) {
400                 splay_unlink_node(&loop->ios, splay_node);
401         }
402
403         for splay_each(timeout_t, timeout, &loop->timeouts) {
404                 splay_unlink_node(&loop->timeouts, splay_node);
405         }
406
407         for splay_each(signal_t, signal, &loop->signals) {
408                 splay_unlink_node(&loop->signals, splay_node);
409         }
410 }