uloop: fix infinite waiting with kqueue
[project/libubox.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
25 #include "uloop.h"
26
27 #ifdef USE_KQUEUE
28 #include <sys/event.h>
29 #endif
30 #ifdef USE_EPOLL
31 #include <sys/epoll.h>
32 #endif
33
34 #include <unistd.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <errno.h>
38 #include <poll.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <signal.h>
42 #include <stdbool.h>
43
44 #ifndef ARRAY_SIZE
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46 #endif
47 #define ULOOP_MAX_EVENTS 10
48
49 struct uloop_timeout *first_timeout;
50 static int poll_fd;
51 static bool cancel;
52
53 #ifdef USE_KQUEUE
54
55 int uloop_init(void)
56 {
57 poll_fd = kqueue();
58 if (poll_fd < 0)
59 return -1;
60
61 return 0;
62 }
63
64
65 static uint16_t get_flags(unsigned int flags, unsigned int mask)
66 {
67 uint16_t kflags = 0;
68
69 if (!(flags & mask))
70 return EV_DELETE;
71
72 kflags = EV_ADD;
73 if (flags & ULOOP_EDGE_TRIGGER)
74 kflags |= EV_CLEAR;
75
76 return kflags;
77 }
78
79 static int register_poll(struct uloop_fd *fd, unsigned int flags)
80 {
81 struct timespec timeout = { 0, 0 };
82 struct kevent ev[2];
83 unsigned int changed;
84 int nev = 0;
85
86 changed = fd->kqflags ^ flags;
87 if (changed & ULOOP_EDGE_TRIGGER)
88 changed |= flags;
89
90 if (changed & ULOOP_READ) {
91 uint16_t kflags = get_flags(flags, ULOOP_READ);
92 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
93 }
94
95 if (changed & ULOOP_WRITE) {
96 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
97 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
98 }
99
100 if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
101 return -1;
102
103 fd->kqflags = flags;
104 return 0;
105 }
106
107 int uloop_fd_delete(struct uloop_fd *sock)
108 {
109 sock->registered = false;
110 return register_poll(sock, 0);
111 }
112
113 static void uloop_run_events(int timeout)
114 {
115 struct kevent events[ULOOP_MAX_EVENTS];
116 struct timespec ts;
117 int nfds, n;
118
119 if (timeout > 0) {
120 ts.tv_sec = timeout / 1000;
121 ts.tv_nsec = timeout * 1000000;
122 }
123
124 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL);
125 for(n = 0; n < nfds; ++n)
126 {
127 struct uloop_fd *u = events[n].udata;
128 unsigned int ev = 0;
129
130 if(events[n].flags & EV_ERROR) {
131 u->error = true;
132 uloop_fd_delete(u);
133 }
134
135 if(events[n].filter == EVFILT_READ)
136 ev |= ULOOP_READ;
137 else if (events[n].filter == EVFILT_WRITE)
138 ev |= ULOOP_WRITE;
139
140 if(events[n].flags & EV_EOF)
141 u->eof = true;
142 else if (!ev)
143 continue;
144
145 if(u->cb)
146 u->cb(u, ev);
147 }
148 }
149
150 #endif
151
152 #ifdef USE_EPOLL
153
154 /**
155 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
156 */
157 #ifndef EPOLLRDHUP
158 #define EPOLLRDHUP 0x2000
159 #endif
160
161 int uloop_init(void)
162 {
163 poll_fd = epoll_create(32);
164 if (poll_fd < 0)
165 return -1;
166
167 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
168 return 0;
169 }
170
171 static int register_poll(struct uloop_fd *fd, unsigned int flags)
172 {
173 struct epoll_event ev;
174 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
175
176 memset(&ev, 0, sizeof(struct epoll_event));
177
178 if (flags & ULOOP_READ)
179 ev.events |= EPOLLIN | EPOLLRDHUP;
180
181 if (flags & ULOOP_WRITE)
182 ev.events |= EPOLLOUT;
183
184 if (flags & ULOOP_EDGE_TRIGGER)
185 ev.events |= EPOLLET;
186
187 ev.data.fd = fd->fd;
188 ev.data.ptr = fd;
189
190 return epoll_ctl(poll_fd, op, fd->fd, &ev);
191 }
192
193 int uloop_fd_delete(struct uloop_fd *sock)
194 {
195 sock->registered = false;
196 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
197 }
198
199 static void uloop_run_events(int timeout)
200 {
201 struct epoll_event events[ULOOP_MAX_EVENTS];
202 int nfds, n;
203
204 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
205 for(n = 0; n < nfds; ++n)
206 {
207 struct uloop_fd *u = events[n].data.ptr;
208 unsigned int ev = 0;
209
210 if(events[n].events & EPOLLERR) {
211 u->error = true;
212 uloop_fd_delete(u);
213 }
214
215 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
216 continue;
217
218 if(events[n].events & EPOLLRDHUP)
219 u->eof = true;
220
221 if(events[n].events & EPOLLIN)
222 ev |= ULOOP_READ;
223
224 if(events[n].events & EPOLLOUT)
225 ev |= ULOOP_WRITE;
226
227 if(u->cb)
228 u->cb(u, ev);
229 }
230 }
231
232 #endif
233
234 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
235 {
236 unsigned int fl;
237 int ret;
238
239 if (!sock->registered) {
240 fl = fcntl(sock->fd, F_GETFL, 0);
241 fl |= O_NONBLOCK;
242 fcntl(sock->fd, F_SETFL, fl);
243 }
244
245 ret = register_poll(sock, flags);
246 if (ret < 0)
247 goto out;
248
249 sock->registered = true;
250 sock->eof = false;
251
252 out:
253 return ret;
254 }
255
256 static int tv_diff(struct timeval *t1, struct timeval *t2)
257 {
258 if (t1->tv_sec != t2->tv_sec)
259 return (t1->tv_sec - t2->tv_sec) * 1000;
260 else
261 return (t1->tv_usec - t2->tv_usec) / 1000;
262 }
263
264 int uloop_timeout_add(struct uloop_timeout *timeout)
265 {
266 struct uloop_timeout **head = &first_timeout;
267 struct uloop_timeout *prev = NULL;
268
269 if (timeout->pending)
270 return -1;
271
272 while (*head) {
273 if (tv_diff(&(*head)->time, &timeout->time) > 0)
274 break;
275
276 prev = *head;
277 head = &(*head)->next;
278 }
279
280 timeout->prev = prev;
281 timeout->next = *head;
282 if (timeout->next)
283 timeout->next->prev = timeout;
284 *head = timeout;
285 timeout->pending = true;
286
287 return 0;
288 }
289
290 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
291 {
292 struct timeval *time = &timeout->time;
293
294 if (timeout->pending)
295 uloop_timeout_cancel(timeout);
296
297 gettimeofday(&timeout->time, NULL);
298
299 time->tv_sec += msecs / 1000;
300 time->tv_usec += msecs % 1000;
301
302 if (time->tv_usec > 1000000) {
303 time->tv_sec++;
304 time->tv_usec %= 100000;
305 }
306
307 return uloop_timeout_add(timeout);
308 }
309
310 int uloop_timeout_cancel(struct uloop_timeout *timeout)
311 {
312 if (!timeout->pending)
313 return -1;
314
315 if (timeout->prev)
316 timeout->prev->next = timeout->next;
317 else
318 first_timeout = timeout->next;
319
320 if (timeout->next)
321 timeout->next->prev = timeout->prev;
322
323 timeout->pending = false;
324
325 return 0;
326 }
327
328 static void uloop_handle_sigint(int signo)
329 {
330 cancel = true;
331 }
332
333 static void uloop_setup_signals(void)
334 {
335 struct sigaction s;
336 memset(&s, 0, sizeof(struct sigaction));
337 s.sa_handler = uloop_handle_sigint;
338 s.sa_flags = 0;
339 sigaction(SIGINT, &s, NULL);
340 }
341
342 static int uloop_get_next_timeout(struct timeval *tv)
343 {
344 int diff;
345
346 if (!first_timeout)
347 return -1;
348
349 diff = tv_diff(&first_timeout->time, tv);
350 if (diff < 0)
351 return 0;
352
353 return diff;
354 }
355
356 static void uloop_process_timeouts(struct timeval *tv)
357 {
358 struct uloop_timeout *timeout;
359
360 while (first_timeout) {
361 if (tv_diff(&first_timeout->time, tv) > 0)
362 break;
363
364 timeout = first_timeout;
365 uloop_timeout_cancel(timeout);
366 if (timeout->cb)
367 timeout->cb(timeout);
368 }
369 }
370
371 void uloop_end(void)
372 {
373 cancel = true;
374 }
375
376 void uloop_run(void)
377 {
378 struct timeval tv;
379
380 uloop_setup_signals();
381 while(!cancel)
382 {
383 gettimeofday(&tv, NULL);
384 uloop_process_timeouts(&tv);
385 uloop_run_events(uloop_get_next_timeout(&tv));
386 }
387 }
388
389 void uloop_done(void)
390 {
391 close(poll_fd);
392 }