43bdc06eae33bab345b4dae728215f20b42ca886
[tinc] / fd / fd_epoll.c
1 /*
2     fd_epoll.c -- I/O and event multiplexing using epoll
3
4     Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: fd.c 1375 2004-03-22 12:30:39Z guus $
21 */
22
23 #include "system.h"
24
25 #include "support/avl.h"
26 #include "support/xalloc.h"
27 #include "fd/event.h"
28 #include "fd/fd.h"
29
30 static int epollfd;
31 static avl_tree_t *fds;
32
33 volatile bool fd_running = false;
34
35 int fd_compare(struct fd *a, struct fd *b) {
36         return (a->fd - b->fd) ?: (a->mode - b->mode);
37 };
38
39 bool fd_init(void) {
40         int i;
41
42         epollfd = epoll_create(32);
43
44         if(epollfd == -1) {
45                 logger(LOG_ERR, "fd: could not open an epoll file descriptor: %s", strerror(errno));
46                 return false;
47         }
48
49         fds = avl_tree_new((avl_compare_t)fd_compare, NULL);
50
51         event_init();
52 }
53
54 bool fd_exit(void) {
55         event_exit();
56
57         avl_tree_del(fds);
58
59         close(epollfd);
60 }
61
62 bool fd_add(struct fd *fd) {
63         if(!avl_add(fds, fd))
64                 return false;
65         
66         fd->event.events = 0;
67
68         if(fd->read)
69                 fd->event.events |= EPOLLIN;
70
71         if(fd->write)
72                 fd->event.events |= EPOLLOUT;
73
74         if(fd->error)
75                 fd->event.events |= EPOLLPRI | EPOLLERR | EPOLLHUP;
76
77         fd->event.data.ptr = fd;
78
79         if(epoll_ctl(epollfd, EPOLL_CTL_ADD, fd->fd, &fd->event) == -1) {
80                 logger(LOG_ERR, "fd: failed to add file descriptor: %s", strerror(errno));
81                 return false;
82         }
83
84         return true;
85 };
86
87 bool fd_del(struct fd *fd) {
88         if(epoll_ctl(epollfd, EPOLL_CTL_DEL, fd->fd, &fd->event) == -1) {
89                 logger(LOG_ERR, "fd: failed to delete file descriptor: %s", strerror(errno));
90                 return false;
91         }
92
93         return avl_del(fds, fd);
94 };
95
96 bool fd_mod(struct fd *fd) {
97         fd->event.events = 0;
98
99         if(fd->read)
100                 fd->event.events |= EPOLLIN;
101
102         if(fd->write)
103                 fd->event.events |= EPOLLOUT;
104
105         if(fd->error)
106                 fd->event.events |= EPOLLPRI | EPOLLERR | EPOLLHUP;
107
108         if(epoll_ctl(epollfd, EPOLL_CTL_MOD, fd->fd, &fd->event) == -1) {
109                 logger(LOG_ERR, "fd: failed to modify file descriptor: %s", strerror(errno));
110                 return false;
111         }
112
113         return true;
114 }       
115
116 bool fd_run(void) {
117         struct timeval tv;
118         int result;
119         struct epoll_event *events[10];
120
121         fd_running = true;
122
123         logger(LOG_INFO, "fd: running");
124                 
125         while(fd_running) {
126                 tv = event_timeout();
127
128                 result = epoll_wait(epollfd, events, sizeof events / sizeof *events, tv.tv_sec >= 0 ? tv.tv_sec * 1000 + tv.tv_usec / 1000: -1);
129
130                 if(result < 0) {
131                         if(errno != EINTR && errno != EAGAIN) {
132                                 logger(LOG_ERR, _("fd: error while waiting for input: %s"), strerror(errno));
133                                 return false;
134                         }
135
136                         continue;
137                 }
138
139                 if(result) {
140                         struct fd *fd;
141
142                         while(result--) {
143                                 fd = events[result].data.ptr;
144
145                                 if(events[result].events & EPOLLIN)
146                                         fd->read(fd);
147
148                                 if(events[result].events & EPOLLOUT)
149                                         fd->write(fd);
150
151                                 if(events[result].events & (EPOLLPRI | EPOLLERR | EPOLLHUP))
152                                         fd->error(fd);
153                         }
154                 } else {
155                         event_handle();
156                 }
157         }
158
159         logger(LOG_INFO, "fd: stopping");
160
161         return true;
162 }
163
164 void fd_stop(void) {
165         fd_running = false;
166 }