da418b7168d2fe885779c647f29293e67d54013e
[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         if (timeout->next)
122                 timeout->next->prev = timeout;
123         *head = timeout;
124         timeout->pending = true;
125
126         return 0;
127 }
128
129 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
130 {
131         struct timeval *time = &timeout->time;
132
133         if (timeout->pending)
134                 uloop_timeout_cancel(timeout);
135
136         gettimeofday(&timeout->time, NULL);
137
138         time->tv_sec += msecs / 1000;
139         time->tv_usec += msecs % 1000;
140
141         if (time->tv_usec > 1000000) {
142                 time->tv_sec++;
143                 time->tv_usec %= 100000;
144         }
145
146         return uloop_timeout_add(timeout);
147 }
148
149 int uloop_timeout_cancel(struct uloop_timeout *timeout)
150 {
151         if (!timeout->pending)
152                 return -1;
153
154         if (timeout->prev)
155                 timeout->prev->next = timeout->next;
156         else
157                 first_timeout = timeout->next;
158
159         if (timeout->next)
160                 timeout->next->prev = timeout->prev;
161
162         timeout->pending = false;
163
164         return 0;
165 }
166
167 static void uloop_handle_sigint(int signo)
168 {
169         cancel = true;
170 }
171
172 static void uloop_setup_signals(void)
173 {
174         struct sigaction s;
175         memset(&s, 0, sizeof(struct sigaction));
176         s.sa_handler = uloop_handle_sigint;
177         s.sa_flags = 0;
178         sigaction(SIGINT, &s, NULL);
179 }
180
181 static int uloop_get_next_timeout(struct timeval *tv)
182 {
183         int diff;
184
185         if (!first_timeout)
186                 return -1;
187
188         diff = tv_diff(&first_timeout->time, tv);
189         if (diff < 0)
190                 return 0;
191
192         return diff;
193 }
194
195 static void uloop_process_timeouts(struct timeval *tv)
196 {
197         struct uloop_timeout *timeout;
198
199         while (first_timeout) {
200                 if (tv_diff(&first_timeout->time, tv) > 0)
201                         break;
202
203                 timeout = first_timeout;
204                 uloop_timeout_cancel(timeout);
205                 if (timeout->cb)
206                         timeout->cb(timeout);
207         }
208 }
209
210 void uloop_end(void)
211 {
212         cancel = true;
213 }
214
215 int uloop_init(void)
216 {
217         epoll_fd = epoll_create(32);
218         if (epoll_fd < 0)
219                 return -1;
220
221         fcntl(epoll_fd, F_SETFD, fcntl(epoll_fd, F_GETFD) | FD_CLOEXEC);
222         return 0;
223 }
224
225 void uloop_run(void)
226 {
227         struct epoll_event events[10];
228         struct timeval tv;
229         int timeout;
230         int nfds, n;
231
232         uloop_setup_signals();
233         while(!cancel)
234         {
235                 gettimeofday(&tv, NULL);
236                 uloop_process_timeouts(&tv);
237                 timeout = uloop_get_next_timeout(&tv);
238                 nfds = epoll_wait(epoll_fd, events, ARRAY_SIZE(events), timeout);
239                 for(n = 0; n < nfds; ++n)
240                 {
241                         struct uloop_fd *u = events[n].data.ptr;
242                         unsigned int ev = 0;
243
244                         if(events[n].events & EPOLLERR) {
245                                 u->error = true;
246                                 uloop_fd_delete(u);
247                         }
248
249                         if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
250                                 continue;
251
252                         if(events[n].events & EPOLLRDHUP)
253                                 u->eof = true;
254
255                         if(events[n].events & EPOLLIN)
256                                 ev |= ULOOP_READ;
257
258                         if(events[n].events & EPOLLOUT)
259                                 ev |= ULOOP_WRITE;
260
261                         if(u->cb)
262                                 u->cb(u, ev);
263                 }
264         }
265 }
266
267 void uloop_done(void)
268 {
269         close(epoll_fd);
270 }