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