From: Guus Sliepen Date: Thu, 14 Dec 2006 18:53:38 +0000 (+0000) Subject: Remove event and I/O loop code, we will use libevent for that. X-Git-Url: https://git.tinc-vpn.org/git/browse?a=commitdiff_plain;h=80e6cb10e8dff4104b24dc591d54ae387b9551d0;p=tinc Remove event and I/O loop code, we will use libevent for that. --- diff --git a/fd/Makefile.am b/fd/Makefile.am deleted file mode 100644 index 511a1e6c..00000000 --- a/fd/Makefile.am +++ /dev/null @@ -1,6 +0,0 @@ -noinst_LIBRARIES = libfd.a - -noinst_HEADERS = event.h fd.h - -libfd_a_SOURCES = event.c fd.c - diff --git a/fd/event.c b/fd/event.c deleted file mode 100644 index 56d70374..00000000 --- a/fd/event.c +++ /dev/null @@ -1,137 +0,0 @@ -/* - event.c -- event queue - - Copyright (C) 2003-2004 Guus Sliepen , - 2003-2004 Ivo Timmermans - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ -*/ - -#include "system.h" - -#include "support/avl.h" -#include "support/xalloc.h" -#include "fd/event.h" - -avl_tree_t *events; - -static event_id_t id; - -static int timevalcmp(struct timeval a, struct timeval b) { - return a.tv_sec - b.tv_sec ?: a.tv_usec - b.tv_usec; -} - -static int event_compare(const event_t *a, const event_t *b) { - return timevalcmp(a->tv, b->tv) ?: a->id - b->id; -} - -bool event_init(void) { - events = avl_tree_new((avl_compare_t)event_compare, (avl_action_t)event_free); - - return true; -} - -bool event_exit(void) { - avl_tree_del(events); - - return true; -} - -event_t *event_new(void) { - event_t *event; - - return clear(new(event)); -} - -void event_free(event_t *event) { - free(event); -} - -void event_set(event_t *event, struct timeval timeout, event_handler_t handler, void *data) { - gettimeofday(&event->tv, NULL); - event_update(event, timeout); - event->interval = timeout; - event->handler = handler; - event->data = data; -} - -void event_update(event_t *event, struct timeval timeout) { - event->tv.tv_sec += timeout.tv_sec; - event->tv.tv_usec += timeout.tv_usec; - event->tv.tv_sec += event->tv.tv_usec / 1000000; - event->tv.tv_usec %= 1000000; -} - -bool event_add(event_t *event) { - event->id = ++id; - return avl_add(events, event); -} - -bool event_del(event_t *event) { - return avl_del(events, event); -} - -void event_handle(void) { - struct timeval now; - event_t *event; - avl_node_t *avl; - - gettimeofday(&now, NULL); - - avl_foreach_node(events, avl, { - event = avl->data; - - if(timercmp(&event->tv, &now, <)) { - avl_unlink_node(events, avl); - if(event->handler(event)) - avl_add_node(events, avl); - else - avl_node_free(events, avl); - } else { - break; - } - }); -} - -struct timeval event_timeout(void) { - struct timeval tv, now; - event_t *event; - - gettimeofday(&now, NULL); - - if(events->head) { - event = events->head->data; - - tv.tv_sec = event->tv.tv_sec - now.tv_sec; - tv.tv_usec = event->tv.tv_usec - now.tv_usec; - - if(tv.tv_usec < 0) { - tv.tv_usec += 1e6; - tv.tv_sec--; - } - - if(tv.tv_sec < 0) { - tv.tv_sec = 0; - tv.tv_usec = 0; - } - } else { - tv.tv_sec = -1; - tv.tv_usec = -1; - } - - return tv; -} diff --git a/fd/event.h b/fd/event.h deleted file mode 100644 index e54b6787..00000000 --- a/fd/event.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - event.h -- event queue - - Copyright (C) 2003-2004 Guus Sliepen , - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ -*/ - -#ifndef __EVENT_H__ -#define __EVENT_H__ - -typedef int event_id_t; - -struct event; - -typedef bool (*event_handler_t)(struct event *event); - -typedef struct event { - struct timeval tv; - struct timeval interval; - event_id_t id; - event_handler_t handler; - void *data; -} event_t; - -extern bool event_init(void); -extern bool event_exit(void); -extern bool event_add(struct event *event); -extern bool event_del(struct event *event); -extern struct event *event_new(void); -extern void event_free(struct event *); -extern void event_set(struct event *, struct timeval, event_handler_t, void *); -extern void event_update(struct event *, struct timeval); -extern void event_handle(void); -extern struct timeval event_timeout(void); - -#endif /* __EVENT_H__ */ diff --git a/fd/fd.c b/fd/fd.c deleted file mode 100644 index e658c1d0..00000000 --- a/fd/fd.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - fd.c -- I/O and event multiplexing - - Copyright (C) 2003-2004 Guus Sliepen , - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ -*/ - -#include "system.h" - -#include "support/avl.h" -#include "support/xalloc.h" -#include "fd/event.h" -#include "fd/fd.h" - -static fd_set readset, writeset, errorset; -static int max_fd; -static avl_tree_t *fds; - -volatile bool fd_running = false; - -int fd_compare(struct fd *a, struct fd *b) { - return a->fd - b->fd; -}; - -bool fd_init(void) { - int i; - - FD_ZERO(&readset); - FD_ZERO(&writeset); - FD_ZERO(&errorset); - - fds = avl_tree_new((avl_compare_t)fd_compare, NULL); - - event_init(); -} - -bool fd_exit(void) { - event_exit(); - - avl_tree_del(fds); -} - -bool fd_add(struct fd *fd) { - if(!avl_add(fds, fd)) - return false; - - if(fd->read) - FD_SET(fd->fd, &readset); - - if(fd->write) - FD_SET(fd->fd, &writeset); - - if(fd->error) - FD_SET(fd->fd, &errorset); - - if(fd->fd > max_fd) - max_fd = fd->fd; - - return true; -}; - -bool fd_del(struct fd *fd) { - FD_CLR(fd->fd, &readset); - FD_CLR(fd->fd, &writeset); - FD_CLR(fd->fd, &errorset); - - avl_del(fds, fd); - - if(fds->tail) - max_fd = ((struct fd *)fds->tail->data)->fd; - else - max_fd = 0; - - return true; -}; - -bool fd_mod(struct fd *fd) { - if(fd->read) - FD_SET(fd->fd, &readset); - else - FD_CLR(fd->fd, &readset); - - if(fd->write) - FD_SET(fd->fd, &writeset); - else - FD_CLR(fd->fd, &writeset); - - if(fd->error) - FD_SET(fd->fd, &errorset); - else - FD_CLR(fd->fd, &errorset); -} - -bool fd_run(void) { - struct timeval tv; - int result; - fd_set readtmp, writetmp, errortmp; - - fd_running = true; - - logger(LOG_INFO, "fd: running"); - - while(fd_running) { - readtmp = readset; - writetmp = writeset; - errortmp = errorset; - - tv = event_timeout(); - - result = select(max_fd + 1, &readtmp, &writetmp, &errortmp, tv.tv_sec >= 0 ? &tv : NULL); - - if(result < 0) { - if(errno != EINTR && errno != EAGAIN) { - logger(LOG_ERR, _("fd: error while waiting for input: %s"), strerror(errno)); - return false; - } - - continue; - } - - if(result) { - struct fd *fd; - - avl_foreach(fds, fd, { - if(fd->read && FD_ISSET(fd->fd, &readtmp)) - fd->read(fd); - if(fd->write && FD_ISSET(fd->fd, &writetmp)) - fd->write(fd); - if(fd->error && FD_ISSET(fd->fd, &errortmp)) - fd->error(fd); - }); - } else { - event_handle(); - } - } - - logger(LOG_INFO, "fd: stopping"); - - return true; -} - -void fd_stop(void) { - fd_running = false; -} diff --git a/fd/fd.h b/fd/fd.h deleted file mode 100644 index 29cad35f..00000000 --- a/fd/fd.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - fd.h -- I/O and event multiplexing - - Copyright (C) 2003-2004 Guus Sliepen , - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id$ -*/ - -#ifndef __FD_H__ -#define __FD_H__ - -struct fd; - -typedef bool (*fd_handler_t)(struct fd *); - -typedef struct fd { - int fd; - fd_handler_t read; - fd_handler_t write; - fd_handler_t error; - void *data; -} fd_t; - -extern bool fd_init(void); -extern bool fd_exit(void); -extern bool fd_add(struct fd *fd); -extern bool fd_del(struct fd *fd); -extern bool fd_mod(struct fd *fd); -extern bool fd_run(void); -extern void fd_stop(void); - -#endif diff --git a/fd/fd_epoll.c b/fd/fd_epoll.c deleted file mode 100644 index 43bdc06e..00000000 --- a/fd/fd_epoll.c +++ /dev/null @@ -1,166 +0,0 @@ -/* - fd_epoll.c -- I/O and event multiplexing using epoll - - Copyright (C) 2003-2004 Guus Sliepen , - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id: fd.c 1375 2004-03-22 12:30:39Z guus $ -*/ - -#include "system.h" - -#include "support/avl.h" -#include "support/xalloc.h" -#include "fd/event.h" -#include "fd/fd.h" - -static int epollfd; -static avl_tree_t *fds; - -volatile bool fd_running = false; - -int fd_compare(struct fd *a, struct fd *b) { - return (a->fd - b->fd) ?: (a->mode - b->mode); -}; - -bool fd_init(void) { - int i; - - epollfd = epoll_create(32); - - if(epollfd == -1) { - logger(LOG_ERR, "fd: could not open an epoll file descriptor: %s", strerror(errno)); - return false; - } - - fds = avl_tree_new((avl_compare_t)fd_compare, NULL); - - event_init(); -} - -bool fd_exit(void) { - event_exit(); - - avl_tree_del(fds); - - close(epollfd); -} - -bool fd_add(struct fd *fd) { - if(!avl_add(fds, fd)) - return false; - - fd->event.events = 0; - - if(fd->read) - fd->event.events |= EPOLLIN; - - if(fd->write) - fd->event.events |= EPOLLOUT; - - if(fd->error) - fd->event.events |= EPOLLPRI | EPOLLERR | EPOLLHUP; - - fd->event.data.ptr = fd; - - if(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd->fd, &fd->event) == -1) { - logger(LOG_ERR, "fd: failed to add file descriptor: %s", strerror(errno)); - return false; - } - - return true; -}; - -bool fd_del(struct fd *fd) { - if(epoll_ctl(epollfd, EPOLL_CTL_DEL, fd->fd, &fd->event) == -1) { - logger(LOG_ERR, "fd: failed to delete file descriptor: %s", strerror(errno)); - return false; - } - - return avl_del(fds, fd); -}; - -bool fd_mod(struct fd *fd) { - fd->event.events = 0; - - if(fd->read) - fd->event.events |= EPOLLIN; - - if(fd->write) - fd->event.events |= EPOLLOUT; - - if(fd->error) - fd->event.events |= EPOLLPRI | EPOLLERR | EPOLLHUP; - - if(epoll_ctl(epollfd, EPOLL_CTL_MOD, fd->fd, &fd->event) == -1) { - logger(LOG_ERR, "fd: failed to modify file descriptor: %s", strerror(errno)); - return false; - } - - return true; -} - -bool fd_run(void) { - struct timeval tv; - int result; - struct epoll_event *events[10]; - - fd_running = true; - - logger(LOG_INFO, "fd: running"); - - while(fd_running) { - tv = event_timeout(); - - result = epoll_wait(epollfd, events, sizeof events / sizeof *events, tv.tv_sec >= 0 ? tv.tv_sec * 1000 + tv.tv_usec / 1000: -1); - - if(result < 0) { - if(errno != EINTR && errno != EAGAIN) { - logger(LOG_ERR, _("fd: error while waiting for input: %s"), strerror(errno)); - return false; - } - - continue; - } - - if(result) { - struct fd *fd; - - while(result--) { - fd = events[result].data.ptr; - - if(events[result].events & EPOLLIN) - fd->read(fd); - - if(events[result].events & EPOLLOUT) - fd->write(fd); - - if(events[result].events & (EPOLLPRI | EPOLLERR | EPOLLHUP)) - fd->error(fd); - } - } else { - event_handle(); - } - } - - logger(LOG_INFO, "fd: stopping"); - - return true; -} - -void fd_stop(void) { - fd_running = false; -} diff --git a/fd/fd_epoll.h b/fd/fd_epoll.h deleted file mode 100644 index 21e39a5b..00000000 --- a/fd/fd_epoll.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - fd_epoll.h -- I/O and event multiplexing using epoll - - Copyright (C) 2003-2004 Guus Sliepen , - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - $Id: fd.h 1375 2004-03-22 12:30:39Z guus $ -*/ - -#ifndef __FD_H__ -#define __FD_H__ - -struct fd; - -typedef bool (*fd_handler_t)(struct fd *); - -typedef struct fd { - int fd; - fd_handler_t read; - fd_handler_t write; - fd_handler_t error; - void *data; - - /* Private */ - - struct epoll_event event; -} fd_t; - -extern bool fd_init(void); -extern bool fd_exit(void); -extern bool fd_add(struct fd *fd); -extern bool fd_del(struct fd *fd); -extern bool fd_mod(struct fd *fd); -extern bool fd_run(void); -extern void fd_stop(void); - -#endif