]> git.meshlink.io Git - meshlink/blob - src/event.c
Handle io_t's being deleted out of order while going through loop->ios.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2014 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){0, 0};
95
96         timeout->cb = cb;
97         timeout->data = data;
98         timeout->node.data = timeout;
99
100         timeout_set(loop, timeout, tv);
101 }
102
103 void timeout_set(event_loop_t *loop, timeout_t *timeout, struct timeval *tv) {
104         if(timerisset(&timeout->tv))
105                 splay_unlink_node(&loop->timeouts, &timeout->node);
106
107         if(!loop->now.tv_sec)
108                 gettimeofday(&loop->now, NULL);
109
110         timeradd(&loop->now, tv, &timeout->tv);
111
112         if(!splay_insert_node(&loop->timeouts, &timeout->node))
113                 abort();
114 }
115
116 void timeout_del(event_loop_t *loop, timeout_t *timeout) {
117         if(!timeout->cb)
118                 return;
119
120         loop->deletion = true;
121
122         splay_unlink_node(&loop->timeouts, &timeout->node);
123         timeout->cb = 0;
124         timeout->tv = (struct timeval){0, 0};
125 }
126
127 static int signal_compare(const signal_t *a, const signal_t *b) {
128         return (int)a->signum - (int)b->signum;
129 }
130
131 static void signalio_handler(event_loop_t *loop, void *data, int flags) {
132         unsigned char signum;
133         if(read(loop->pipefd[0], &signum, 1) != 1)
134                 return;
135
136         signal_t *sig = splay_search(&loop->signals, &((signal_t){.signum = signum}));
137         if(sig)
138                 sig->cb(loop, sig->data);
139 }
140
141 static void pipe_init(event_loop_t *loop) {
142         if(!pipe(loop->pipefd))
143                 io_add(loop, &loop->signalio, signalio_handler, NULL, loop->pipefd[0], IO_READ);
144 }
145
146 void signal_trigger(event_loop_t *loop, signal_t *sig) {
147
148         uint8_t signum = sig->signum;
149         write(loop->pipefd[1], &signum, 1);
150         return;
151
152 }
153
154 void signal_add(event_loop_t *loop, signal_t *sig, signal_cb_t cb, void *data, uint8_t signum) {
155         if(sig->cb)
156                 return;
157
158         sig->cb = cb;
159         sig->data = data;
160         sig->signum = signum;
161         sig->node.data = sig;
162
163         if(loop->pipefd[0] == -1)
164                 pipe_init(loop);
165
166         if(!splay_insert_node(&loop->signals, &sig->node))
167                 abort();
168 }
169
170 void signal_del(event_loop_t *loop, signal_t *sig) {
171         if(!sig->cb)
172                 return;
173
174         loop->deletion = true;
175
176         splay_unlink_node(&loop->signals, &sig->node);
177         sig->cb = NULL;
178 }
179
180 bool event_loop_run(event_loop_t *loop) {
181         loop->running = true;
182
183         fd_set readable;
184         fd_set writable;
185
186         while(loop->running) {
187                 gettimeofday(&loop->now, NULL);
188                 struct timeval diff, *tv = NULL;
189
190                 while(loop->timeouts.head) {
191                         timeout_t *timeout = loop->timeouts.head->data;
192                         timersub(&timeout->tv, &loop->now, &diff);
193
194                         if(diff.tv_sec < 0) {
195                                 timeout->cb(loop, timeout->data);
196                                 if(timercmp(&timeout->tv, &loop->now, <))
197                                         timeout_del(loop, timeout);
198                         } else {
199                                 tv = &diff;
200                                 break;
201                         }
202                 }
203
204                 memcpy(&readable, &loop->readfds, sizeof readable);
205                 memcpy(&writable, &loop->writefds, sizeof writable);
206
207                 int fds = 0;
208
209                 if(loop->ios.tail) {
210                         io_t *last = loop->ios.tail->data;
211                         fds = last->fd + 1;
212                 }
213
214                 int n = select(fds, &readable, &writable, NULL, tv);
215
216                 if(n < 0) {
217                         if(sockwouldblock(errno))
218                                 continue;
219                         else
220                                 return false;
221                 }
222
223                 if(!n)
224                         continue;
225
226                 // Normally, splay_each allows the current node to be deleted. However,
227                 // it can be that one io callback triggers the deletion of another io,
228                 // so we have to detect this and break the loop.
229
230                 loop->deletion = false;
231
232                 for splay_each(io_t, io, &loop->ios) {
233                         if(FD_ISSET(io->fd, &writable) && io->cb)
234                                 io->cb(loop, io->data, IO_WRITE);
235                         if(loop->deletion)
236                                 break;
237                         if(FD_ISSET(io->fd, &readable) && io->cb)
238                                 io->cb(loop, io->data, IO_READ);
239                         if(loop->deletion)
240                                 break;
241                 }
242         }
243
244         return true;
245 }
246
247 void event_flush_output(event_loop_t *loop) {
248         for splay_each(io_t, io, &loop->ios)
249                 if(FD_ISSET(io->fd, &loop->writefds))
250                         io->cb(loop, io->data, IO_WRITE);
251 }
252
253 void event_loop_stop(event_loop_t *loop) {
254         loop->running = false;
255 }
256
257 void event_loop_init(event_loop_t *loop) {
258         loop->ios.compare = (splay_compare_t)io_compare;
259         loop->timeouts.compare = (splay_compare_t)timeout_compare;
260         loop->signals.compare = (splay_compare_t)signal_compare;
261         loop->pipefd[0] = -1;
262         loop->pipefd[1] = -1;
263         gettimeofday(&loop->now, NULL);
264 }
265
266 void event_loop_exit(event_loop_t *loop) {
267         for splay_each(io_t, io, &loop->ios)
268                 splay_unlink_node(&loop->ios, node);
269         for splay_each(timeout_t, timeout, &loop->timeouts)
270                 splay_unlink_node(&loop->timeouts, node);
271         for splay_each(signal_t, signal, &loop->signals)
272                 splay_unlink_node(&loop->signals, node);
273 }