]> git.meshlink.io Git - meshlink/blob - src/event.c
Fix compiling with -Wall -W.
[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         if(diff.tv_sec < 0)
37                 return -1;
38         if(diff.tv_sec > 0)
39                 return 1;
40         if(diff.tv_usec < 0)
41                 return -1;
42         if(diff.tv_usec > 0)
43                 return 1;
44         if(a < b)
45                 return -1;
46         if(a > b)
47                 return 1;
48         return 0;
49 }
50
51 void io_add(event_loop_t *loop, io_t *io, io_cb_t cb, void *data, int fd, int flags) {
52         if(io->cb)
53                 return;
54
55         io->fd = fd;
56         io->cb = cb;
57         io->data = data;
58         io->node.data = io;
59
60         io_set(loop, io, flags);
61
62         if(!splay_insert_node(&loop->ios, &io->node))
63                 abort();
64 }
65
66 void io_set(event_loop_t *loop, io_t *io, int flags) {
67         io->flags = flags;
68
69         if(flags & IO_READ)
70                 FD_SET(io->fd, &loop->readfds);
71         else
72                 FD_CLR(io->fd, &loop->readfds);
73
74         if(flags & IO_WRITE)
75                 FD_SET(io->fd, &loop->writefds);
76         else
77                 FD_CLR(io->fd, &loop->writefds);
78 }
79
80 void io_del(event_loop_t *loop, io_t *io) {
81         if(!io->cb)
82                 return;
83
84         loop->deletion = true;
85
86         io_set(loop, io, 0);
87
88         splay_unlink_node(&loop->ios, &io->node);
89         io->cb = NULL;
90 }
91
92 void timeout_add(event_loop_t *loop, timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
93         if(!timeout->cb)
94                 timeout->tv = (struct timeval) {
95                 0, 0
96         };
97
98         timeout->cb = cb;
99         timeout->data = data;
100         timeout->node.data = timeout;
101
102         timeout_set(loop, timeout, tv);
103 }
104
105 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
106         if(timerisset(&timeout->tv))
107                 splay_unlink_node(&loop->timeouts, &timeout->node);
108
109         if(!loop->now.tv_sec)
110                 gettimeofday(&loop->now, NULL);
111
112         timeradd(&loop->now, tv, &timeout->tv);
113
114         if(!splay_insert_node(&loop->timeouts, &timeout->node))
115                 abort();
116 }
117
118 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
119         if(!timeout->cb)
120                 return;
121
122         loop->deletion = true;
123
124         splay_unlink_node(&loop->timeouts, &timeout->node);
125         timeout->cb = 0;
126         timeout->tv = (struct timeval) {
127                 0, 0
128         };
129 }
130
131 static int signal_compare(const signal_t *a, const signal_t *b) {
132         return (int)a->signum - (int)b->signum;
133 }
134
135 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
136         (void)data;
137         (void)flags;
138         unsigned char signum;
139         if(read(loop->pipefd[0], &signum, 1) != 1)
140                 return;
141
142         signal_t *sig = splay_search(&loop->signals, &((signal_t) {
143                 .signum = signum
144         }));
145         if(sig)
146                 sig->cb(loop, sig->data);
147 }
148
149 static void pipe_init(event_loop_t *loop) {
150         if(!pipe(loop->pipefd))
151                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
152 }
153
154 void signal_trigger(event_loop_t *loop, signal_t *sig) {
155
156         uint8_t signum = sig->signum;
157         write(loop->pipefd[1], &signum, 1);
158         return;
159
160 }
161
162 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
163         if(sig->cb)
164                 return;
165
166         sig->cb = cb;
167         sig->data = data;
168         sig->signum = signum;
169         sig->node.data = sig;
170
171         if(loop->pipefd[0] == -1)
172                 pipe_init(loop);
173
174         if(!splay_insert_node(&loop->signals, &sig->node))
175                 abort();
176 }
177
178 void signal_del(event_loop_t *loop, signal_t *sig) {
179         if(!sig->cb)
180                 return;
181
182         loop->deletion = true;
183
184         splay_unlink_node(&loop->signals, &sig->node);
185         sig->cb = NULL;
186 }
187
188 void idle_set(event_loop_t *loop, idle_cb_t cb, void *data) {
189         loop->idle_cb = cb;
190         loop->idle_data = data;
191 }
192
193 bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) {
194         fd_set readable;
195         fd_set writable;
196
197         while(loop->running) {
198                 gettimeofday(&loop->now, NULL);
199                 struct timeval diff, it, *tv = NULL;
200
201                 while(loop->timeouts.head) {
202                         timeout_t *timeout = loop->timeouts.head->data;
203                         timersub(&timeout->tv, &loop->now, &diff);
204
205                         if(diff.tv_sec < 0) {
206                                 timeout->cb(loop, timeout->data);
207                                 if(timercmp(&timeout->tv, &loop->now, <))
208                                         timeout_del(loop, timeout);
209                         } else {
210                                 tv = &diff;
211                                 break;
212                         }
213                 }
214
215                 if(loop->idle_cb) {
216                         it = loop->idle_cb(loop, loop->idle_data);
217                         if(it.tv_sec >= 0 && (!tv || timercmp(&it, tv, <)))
218                                 tv = &it;
219                 }
220
221                 memcpy(&readable, &loop->readfds, sizeof(readable));
222                 memcpy(&writable, &loop->writefds, sizeof(writable));
223
224                 int fds = 0;
225
226                 if(loop->ios.tail) {
227                         io_t *last = loop->ios.tail->data;
228                         fds = last->fd + 1;
229                 }
230
231                 // release mesh mutex during select
232                 if(mutex)
233                         pthread_mutex_unlock(mutex);
234                 int n = select(fds, &readable, &writable, NULL, tv);
235                 if(mutex)
236                         pthread_mutex_lock(mutex);
237
238                 if(n < 0) {
239                         if(sockwouldblock(errno))
240                                 continue;
241                         else
242                                 return false;
243                 }
244
245                 if(!n)
246                         continue;
247
248                 // Normally, splay_each allows the current node to be deleted. However,
249                 // it can be that one io callback triggers the deletion of another io,
250                 // so we have to detect this and break the loop.
251
252                 loop->deletion = false;
253
254                 for splay_each(io_t, io, &loop->ios) {
255                         if(FD_ISSET(io->fd, &writable) && io->cb)
256                                 io->cb(loop, io->data, IO_WRITE);
257                         if(loop->deletion)
258                                 break;
259                         if(FD_ISSET(io->fd, &readable) && io->cb)
260                                 io->cb(loop, io->data, IO_READ);
261                         if(loop->deletion)
262                                 break;
263                 }
264         }
265
266         return true;
267 }
268
269 void event_flush_output(event_loop_t *loop) {
270         for splay_each(io_t, io, &loop->ios)
271                 if(FD_ISSET(io->fd, &loop->writefds))
272                         io->cb(loop, io->data, IO_WRITE);
273 }
274
275 void event_loop_start(event_loop_t *loop) {
276         loop->running = true;
277 }
278
279 void event_loop_stop(event_loop_t *loop) {
280         loop->running = false;
281 }
282
283 void event_loop_init(event_loop_t *loop) {
284         loop->ios.compare = (splay_compare_t)io_compare;
285         loop->timeouts.compare = (splay_compare_t)timeout_compare;
286         loop->signals.compare = (splay_compare_t)signal_compare;
287         loop->pipefd[0] = -1;
288         loop->pipefd[1] = -1;
289         gettimeofday(&loop->now, NULL);
290 }
291
292 void event_loop_exit(event_loop_t *loop) {
293         for splay_each(io_t, io, &loop->ios)
294                 splay_unlink_node(&loop->ios, node);
295         for splay_each(timeout_t, timeout, &loop->timeouts)
296                 splay_unlink_node(&loop->timeouts, node);
297         for splay_each(signal_t, signal, &loop->signals)
298                 splay_unlink_node(&loop->signals, node);
299 }