2 event.c -- I/O, timeout and signal event handling
3 Copyright (C) 2012-2013 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
29 static fd_set readfds;
30 static fd_set writefds;
31 static volatile bool running;
33 static int io_compare(const io_t *a, const io_t *b) {
37 static int timeout_compare(const timeout_t *a, const timeout_t *b) {
39 timersub(&a->tv, &b->tv, &diff);
55 static splay_tree_t io_tree = {.compare = (splay_compare_t)io_compare};
56 static splay_tree_t timeout_tree = {.compare = (splay_compare_t)timeout_compare};
58 void io_add(io_t *io, io_cb_t cb, void *data, int fd, int flags) {
69 if(!splay_insert_node(&io_tree, &io->node))
73 void io_set(io_t *io, int flags) {
77 FD_SET(io->fd, &readfds);
79 FD_CLR(io->fd, &readfds);
82 FD_SET(io->fd, &writefds);
84 FD_CLR(io->fd, &writefds);
87 void io_del(io_t *io) {
93 splay_unlink_node(&io_tree, &io->node);
97 void timeout_add(timeout_t *timeout, timeout_cb_t cb, void *data, struct timeval *tv) {
100 timeout->node.data = timeout;
102 timeout_set(timeout, tv);
105 void timeout_set(timeout_t *timeout, struct timeval *tv) {
106 if(timerisset(&timeout->tv))
107 splay_unlink_node(&timeout_tree, &timeout->node);
110 gettimeofday(&now, NULL);
112 timeradd(&now, tv, &timeout->tv);
114 if(!splay_insert_node(&timeout_tree, &timeout->node))
118 void timeout_del(timeout_t *timeout) {
122 splay_unlink_node(&timeout_tree, &timeout->node);
124 timeout->tv = (struct timeval){0, 0};
128 static int signal_compare(const signal_t *a, const signal_t *b) {
129 return a->signum - b->signum;
132 static io_t signalio;
133 static int pipefd[2] = {-1, -1};
134 static splay_tree_t signal_tree = {.compare = (splay_compare_t)signal_compare};
136 static void signal_handler(int signum) {
137 unsigned char num = signum;
138 write(pipefd[1], &num, 1);
141 static void signalio_handler(void *data, int flags) {
142 unsigned char signum;
143 if(read(pipefd[0], &signum, 1) != 1)
146 signal_t *sig = splay_search(&signal_tree, &((signal_t){.signum = signum}));
151 static void pipe_init(void) {
153 io_add(&signalio, signalio_handler, NULL, pipefd[0], IO_READ);
156 void signal_add(signal_t *sig, signal_cb_t cb, void *data, int signum) {
162 sig->signum = signum;
163 sig->node.data = sig;
168 signal(sig->signum, signal_handler);
170 if(!splay_insert_node(&signal_tree, &sig->node))
174 void signal_del(signal_t *sig) {
178 signal(sig->signum, SIG_DFL);
180 splay_unlink_node(&signal_tree, &sig->node);
185 bool event_loop(void) {
192 gettimeofday(&now, NULL);
193 struct timeval diff, *tv = NULL;
195 while(timeout_tree.head) {
196 timeout_t *timeout = timeout_tree.head->data;
197 timersub(&timeout->tv, &now, &diff);
199 if(diff.tv_sec < 0) {
200 timeout->cb(timeout->data);
201 if(timercmp(&timeout->tv, &now, <))
202 timeout_del(timeout);
209 memcpy(&readable, &readfds, sizeof readable);
210 memcpy(&writable, &writefds, sizeof writable);
215 io_t *last = io_tree.tail->data;
220 LeaveCriticalSection(&mutex);
222 int n = select(fds, &readable, &writable, NULL, tv);
224 EnterCriticalSection(&mutex);
228 if(sockwouldblock(errno))
237 for splay_each(io_t, io, &io_tree) {
238 if(FD_ISSET(io->fd, &writable))
239 io->cb(io->data, IO_WRITE);
240 else if(FD_ISSET(io->fd, &readable))
241 io->cb(io->data, IO_READ);
248 void event_flush_output(void) {
249 for splay_each(io_t, io, &io_tree)
250 if(FD_ISSET(io->fd, &writefds))
251 io->cb(io->data, IO_WRITE);
254 void event_exit(void) {