]> git.meshlink.io Git - meshlink/blob - src/event.c
Fix crash in timeout handling.
[meshlink] / src / event.c
1 /*
2     event.c -- I/O, timeout and signal event handling
3     Copyright (C) 2012 Guus Sliepen <guus@tinc-vpn.org>
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 "event.h"
23 #include "net.h"
24 #include "utils.h"
25
26 struct timeval now;
27
28 static fd_set readfds;
29 static fd_set writefds;
30 static volatile bool running;
31
32 static int io_compare(const io_t *a, const io_t *b) {
33         return a->fd - b->fd;
34 }
35
36 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
37         struct timeval diff;
38         timersub(&a->tv, &b->tv, &diff);
39         if(diff.tv_sec < 0)
40                 return -1;
41         if(diff.tv_sec > 0)
42                 return 1;
43         if(diff.tv_usec < 0)
44                 return -1;
45         if(diff.tv_usec > 0)
46                 return 1;
47         if(a < b)
48                 return -1;
49         if(a > b)
50                 return 1;
51         return 0;
52 }
53
54 static int signal_compare(const signal_t *a, const signal_t *b) {
55         return a->signum - b->signum;
56 }
57
58 static splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
59 static splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
60 static splay_tree_t signal_tree = {.compare = (splay_compare_t)signal_compare};
61
62 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
63         if(io->cb)
64                 return;
65
66         io->fd = fd;
67         io->cb = cb;
68         io->data = data;
69         io->node.data = io;
70
71         io_set(io, flags);
72
73         if(!splay_insert_node(&io_tree, &io->node))
74                 abort();
75 }
76
77 void io_set(io_t *io, int flags) {
78         io->flags = flags;
79
80         if(flags & IO_READ)
81                 FD_SET(io->fd, &readfds);
82         else
83                 FD_CLR(io->fd, &readfds);
84
85         if(flags & IO_WRITE)
86                 FD_SET(io->fd, &writefds);
87         else
88                 FD_CLR(io->fd, &writefds);
89 }
90
91 void io_del(io_t *io) {
92         if(!io->cb)
93                 return;
94
95         io_set(io, 0);
96
97         splay_unlink_node(&io_tree, &io->node);
98         io->cb = NULL;
99 }
100
101 void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
102         timeout->cb = cb;
103         timeout->data = data;
104         timeout->node.data = timeout;
105
106         timeout_set(timeout, tv);
107 }
108
109 void timeout_set(timeout_t *timeout, struct timeval *tv) {
110         if(timerisset(&timeout->tv))
111                 splay_unlink_node(&timeout_tree, &timeout->node);
112
113         if(!now.tv_sec)
114                 gettimeofday(&now, NULL);
115
116         timeradd(&now, tv, &timeout->tv);
117
118         if(!splay_insert_node(&timeout_tree, &timeout->node))
119                 abort();
120 }
121
122 void timeout_del(timeout_t *timeout) {
123         if(!timeout->cb)
124                 return;
125
126         splay_unlink_node(&timeout_tree, &timeout->node);
127         timeout->cb = 0;
128         timeout->tv = (struct timeval){0, 0};
129 }
130
131 #ifndef HAVE_MINGW
132 static io_t signalio;
133 static int pipefd[2] = {-1, -1};
134
135 static void signal_handler(int signum) {
136         unsigned char num = signum;
137         write(pipefd[1], &num, 1);
138 }
139
140 static void signalio_handler(void *data, int flags) {
141         unsigned char signum;
142         if(read(pipefd[0], &signum, 1) != 1)
143                 return;
144
145         signal_t *sig = splay_search(&signal_tree, &((signal_t){.signum = signum}));
146         if(sig)
147                 sig->cb(sig->data);
148 }
149
150 static void pipe_init(void) {
151         if(!pipe(pipefd))
152                 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
153 }
154
155 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
156         if(sig->cb)
157                 return;
158
159         sig->cb = cb;
160         sig->data = data;
161         sig->signum = signum;
162         sig->node.data = sig;
163
164         if(pipefd[0] == -1)
165                 pipe_init();
166
167         signal(sig->signum, signal_handler);
168
169         if(!splay_insert_node(&signal_tree, &sig->node))
170                 abort();
171 }
172
173 void signal_del(signal_t *sig) {
174         if(!sig->cb)
175                 return;
176
177         signal(sig->signum, SIG_DFL);
178
179         splay_unlink_node(&signal_tree, &sig->node);
180         sig->cb = NULL;
181 }
182 #endif
183
184 bool event_loop(void) {
185         running = true;
186
187         fd_set readable;
188         fd_set writable;
189
190         while(running) {
191                 gettimeofday(&now, NULL);
192                 struct timeval diff, *tv = NULL;
193
194                 while(timeout_tree.head) {
195                         timeout_t *timeout = timeout_tree.head->data;
196                         timersub(&timeout->tv, &now, &diff);
197
198                         if(diff.tv_sec < 0) {
199                                 timeout->cb(timeout->data);
200                                 if(timercmp(&timeout->tv, &now, <))
201                                         timeout_del(timeout);
202                         } else {
203                                 tv = &diff;
204                                 break;
205                         }
206                 }
207
208                 memcpy(&readable, &readfds, sizeof readable);
209                 memcpy(&writable, &writefds, sizeof writable);
210
211                 int fds = 0;
212
213                 if(io_tree.tail) {
214                         io_t *last = io_tree.tail->data;
215                         fds = last->fd + 1;
216                 }
217
218 #ifdef HAVE_MINGW
219                 LeaveCriticalSection(&mutex);
220 #endif
221                 int n = select(fds, &readable, &writable, NULL, tv);
222 #ifdef HAVE_MINGW
223                 EnterCriticalSection(&mutex);
224 #endif
225
226                 if(n < 0) {
227                         if(sockwouldblock(errno))
228                                 continue;
229                         else
230                                 return false;
231                 }
232
233                 if(!n)
234                         continue;
235
236                 for splay_each(io_t, io, &io_tree) {
237                         if(FD_ISSET(io->fd, &writable))
238                                 io->cb(io->data, IO_WRITE);
239                         else if(FD_ISSET(io->fd, &readable))
240                                 io->cb(io->data, IO_READ);
241                 }
242         }
243
244         return true;
245 }
246
247 void event_exit(void) {
248         running = false;
249 }