Add default gateway and DHCP handling (not fully working yet because of a kernel...
[project/relayd.git] / uloop.c
1 /*
2  *   Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
3  *   Copyright (C) 2010 John Crispin <blogic@openwrt.org>
4  *   Copyright (C) 2010 Steven Barth <steven@midlink.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */
21
22 #include <sys/time.h>
23 #include <sys/types.h>
24 #include <sys/epoll.h>
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <poll.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdbool.h>
35
36 #include "uloop.h"
37
38 /**
39  * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
40  */
41 #ifndef EPOLLRDHUP
42 #define EPOLLRDHUP 0x2000
43 #endif
44
45 #ifndef ARRAY_SIZE
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #endif
48
49 struct uloop_timeout *first_timeout;
50 static int epoll_fd;
51 static bool cancel;
52
53 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
54 {
55         struct epoll_event ev;
56         int op = sock->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
57         unsigned int fl;
58         int ret;
59
60         fl = fcntl(sock->fd, F_GETFL, 0);
61         fl |= O_NONBLOCK;
62         fcntl(sock->fd, F_SETFL, fl);
63
64         memset(&ev, 0, sizeof(struct epoll_event));
65
66         if (flags & ULOOP_READ)
67                 ev.events |= EPOLLIN | EPOLLRDHUP;
68
69         if (flags & ULOOP_WRITE)
70                 ev.events |= EPOLLOUT;
71
72         if (flags & ULOOP_EDGE_TRIGGER)
73                 ev.events |= EPOLLET;
74
75         ev.data.fd = sock->fd;
76         ev.data.ptr = sock;
77
78         ret = epoll_ctl(epoll_fd, op, sock->fd, &ev);
79         if (ret < 0)
80                 goto out;
81
82         sock->registered = true;
83         sock->eof = false;
84
85 out:
86         return ret;
87 }
88
89 int uloop_fd_delete(struct uloop_fd *sock)
90 {
91         sock->registered = false;
92         return epoll_ctl(epoll_fd, EPOLL_CTL_DEL, sock->fd, 0);
93 }
94
95 static int tv_diff(struct timeval *t1, struct timeval *t2)
96 {
97         if (t1->tv_sec != t2->tv_sec)
98                 return (t1->tv_sec - t2->tv_sec) * 1000;
99         else
100                 return (t1->tv_usec - t2->tv_usec) / 1000;
101 }
102
103 int uloop_timeout_add(struct uloop_timeout *timeout)
104 {
105         struct uloop_timeout **head = &first_timeout;
106         struct uloop_timeout *prev = NULL;
107
108         if (timeout->pending)
109                 return -1;
110
111         while (*head) {
112                 if (tv_diff(&(*head)->time, &timeout->time) > 0)
113                         break;
114
115                 prev = *head;
116                 head = &(*head)->next;
117         }
118
119         timeout->prev = prev;
120         timeout->next = *head;
121         *head = timeout;
122         timeout->pending = true;
123
124         return 0;
125 }
126
127 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
128 {
129         struct timeval *time = &timeout->time;
130
131         if (timeout->pending)
132                 uloop_timeout_cancel(timeout);
133
134         gettimeofday(&timeout->time, NULL);
135
136         time->tv_sec += msecs / 1000;
137         time->tv_usec += msecs % 1000;
138
139         if (time->tv_usec > 1000000) {
140                 time->tv_sec++;
141                 time->tv_usec %= 100000;
142         }
143
144         return uloop_timeout_add(timeout);
145 }
146
147 int uloop_timeout_cancel(struct uloop_timeout *timeout)
148 {
149         if (!timeout->pending)
150                 return -1;
151
152         if (timeout->prev)
153                 timeout->prev->next = timeout->next;
154         else
155                 first_timeout = timeout->next;
156
157         if (timeout->next)
158                 timeout->next->prev = timeout->prev;
159
160         timeout->pending = false;
161
162         return 0;
163 }
164
165 static void uloop_handle_sigint(int signo)
166 {
167         cancel = true;
168 }
169
170 static void uloop_setup_signals(void)
171 {
172         struct sigaction s;
173         memset(&s, 0, sizeof(struct sigaction));
174         s.sa_handler = uloop_handle_sigint;
175         s.sa_flags = 0;
176         sigaction(SIGINT, &s, NULL);
177 }
178
179 static int uloop_get_next_timeout(struct timeval *tv)
180 {
181         int diff;
182
183         if (!first_timeout)
184                 return -1;
185
186         diff = tv_diff(&first_timeout->time, tv);
187         if (diff < 0)
188                 return 0;
189
190         return diff;
191 }
192
193 static void uloop_process_timeouts(struct timeval *tv)
194 {
195         struct uloop_timeout *timeout;
196
197         while (first_timeout) {
198                 if (tv_diff(&first_timeout->time, tv) > 0)
199                         break;
200
201                 timeout = first_timeout;
202                 uloop_timeout_cancel(timeout);
203                 if (timeout->cb)
204                         timeout->cb(timeout);
205         }
206 }
207
208 void uloop_end(void)
209 {
210         cancel = true;
211 }
212
213 int uloop_init(void)
214 {
215         epoll_fd = epoll_create(32);
216         if (epoll_fd < 0)
217                 return -1;
218
219         fcntl(epoll_fd, F_SETFD, fcntl(epoll_fd, F_GETFD) | FD_CLOEXEC);
220         return 0;
221 }
222
223 void uloop_run(void)
224 {
225         struct epoll_event events[10];
226         struct timeval tv;
227         int timeout;
228         int nfds, n;
229
230         uloop_setup_signals();
231         while(!cancel)
232         {
233                 gettimeofday(&tv, NULL);
234                 uloop_process_timeouts(&tv);
235                 timeout = uloop_get_next_timeout(&tv);
236                 nfds = epoll_wait(epoll_fd, events, ARRAY_SIZE(events), timeout);
237                 for(n = 0; n < nfds; ++n)
238                 {
239                         struct uloop_fd *u = events[n].data.ptr;
240                         unsigned int ev = 0;
241
242                         if(events[n].events & EPOLLERR) {
243                                 u->error = true;
244                                 uloop_fd_delete(u);
245                         }
246
247                         if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
248                                 continue;
249
250                         if(events[n].events & EPOLLRDHUP)
251                                 u->eof = true;
252
253                         if(events[n].events & EPOLLIN)
254                                 ev |= ULOOP_READ;
255
256                         if(events[n].events & EPOLLOUT)
257                                 ev |= ULOOP_WRITE;
258
259                         if(u->cb)
260                                 u->cb(u, ev);
261                 }
262         }
263 }
264
265 void uloop_done(void)
266 {
267         close(epoll_fd);
268 }