uloop: fix event flags processing on mac os x
[project/libubox.git] / uloop.c
1 /*
2 * uloop - event loop implementation
3 *
4 * Copyright (C) 2010-2013 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include <sys/time.h>
19 #include <sys/types.h>
20
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <poll.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <stdbool.h>
29
30 #include "uloop.h"
31 #include "utils.h"
32
33 #ifdef USE_KQUEUE
34 #include <sys/event.h>
35 #endif
36 #ifdef USE_EPOLL
37 #include <sys/epoll.h>
38 #endif
39 #include <sys/wait.h>
40
41 struct uloop_fd_event {
42 struct uloop_fd *fd;
43 unsigned int events;
44 };
45
46 #define ULOOP_MAX_EVENTS 10
47
48 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
49 static struct list_head processes = LIST_HEAD_INIT(processes);
50
51 static int poll_fd = -1;
52 bool uloop_cancelled = false;
53 bool uloop_handle_sigchld = true;
54 static bool do_sigchld = false;
55
56 static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
57 static int cur_fd, cur_nfds;
58
59 #ifdef USE_KQUEUE
60
61 int uloop_init(void)
62 {
63 struct timespec timeout = { 0, 0 };
64 struct kevent ev = {};
65
66 if (poll_fd >= 0)
67 return 0;
68
69 poll_fd = kqueue();
70 if (poll_fd < 0)
71 return -1;
72
73 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
74 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
75
76 return 0;
77 }
78
79
80 static uint16_t get_flags(unsigned int flags, unsigned int mask)
81 {
82 uint16_t kflags = 0;
83
84 if (!(flags & mask))
85 return EV_DELETE;
86
87 kflags = EV_ADD;
88 if (flags & ULOOP_EDGE_TRIGGER)
89 kflags |= EV_CLEAR;
90
91 return kflags;
92 }
93
94 static struct kevent events[ULOOP_MAX_EVENTS];
95
96 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
97 {
98 struct timespec timeout = { 0, 0 };
99 struct kevent ev[2];
100 int nev = 0;
101 unsigned int fl = 0;
102 unsigned int changed;
103 uint16_t kflags;
104
105 if (flags & ULOOP_EDGE_DEFER)
106 flags &= ~ULOOP_EDGE_TRIGGER;
107
108 changed = flags ^ fd->flags;
109 if (changed & ULOOP_EDGE_TRIGGER)
110 changed |= flags;
111
112 if (changed & ULOOP_READ) {
113 kflags = get_flags(flags, ULOOP_READ);
114 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
115 }
116
117 if (changed & ULOOP_WRITE) {
118 kflags = get_flags(flags, ULOOP_WRITE);
119 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
120 }
121
122 if (!flags)
123 fl |= EV_DELETE;
124
125 fd->flags = flags;
126 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
127 return -1;
128
129 return 0;
130 }
131
132 static int register_poll(struct uloop_fd *fd, unsigned int flags)
133 {
134 if (flags & ULOOP_EDGE_TRIGGER)
135 flags |= ULOOP_EDGE_DEFER;
136 else
137 flags &= ~ULOOP_EDGE_DEFER;
138
139 return register_kevent(fd, flags);
140 }
141
142 static int __uloop_fd_delete(struct uloop_fd *fd)
143 {
144 return register_poll(fd, 0);
145 }
146
147 static int uloop_fetch_events(int timeout)
148 {
149 struct timespec ts;
150 int nfds, n;
151
152 if (timeout >= 0) {
153 ts.tv_sec = timeout / 1000;
154 ts.tv_nsec = (timeout % 1000) * 1000000;
155 }
156
157 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
158 for (n = 0; n < nfds; n++) {
159 struct uloop_fd_event *cur = &cur_fds[n];
160 struct uloop_fd *u = events[n].udata;
161 unsigned int ev = 0;
162
163 cur->fd = u;
164 if (!u)
165 continue;
166
167 if (events[n].flags & EV_ERROR) {
168 u->error = true;
169 uloop_fd_delete(u);
170 }
171
172 if(events[n].filter == EVFILT_READ)
173 ev |= ULOOP_READ;
174 else if (events[n].filter == EVFILT_WRITE)
175 ev |= ULOOP_WRITE;
176
177 if (events[n].flags & EV_EOF)
178 u->eof = true;
179 else if (!ev)
180 cur->fd = NULL;
181
182 cur->events = ev;
183 if (u->flags & ULOOP_EDGE_DEFER) {
184 u->flags &= ~ULOOP_EDGE_DEFER;
185 u->flags |= ULOOP_EDGE_TRIGGER;
186 register_kevent(u, u->flags);
187 }
188 }
189 return nfds;
190 }
191
192 #endif
193
194 #ifdef USE_EPOLL
195
196 /**
197 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
198 */
199 #ifndef EPOLLRDHUP
200 #define EPOLLRDHUP 0x2000
201 #endif
202
203 int uloop_init(void)
204 {
205 if (poll_fd >= 0)
206 return 0;
207
208 poll_fd = epoll_create(32);
209 if (poll_fd < 0)
210 return -1;
211
212 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
213 return 0;
214 }
215
216 static int register_poll(struct uloop_fd *fd, unsigned int flags)
217 {
218 struct epoll_event ev;
219 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
220
221 memset(&ev, 0, sizeof(struct epoll_event));
222
223 if (flags & ULOOP_READ)
224 ev.events |= EPOLLIN | EPOLLRDHUP;
225
226 if (flags & ULOOP_WRITE)
227 ev.events |= EPOLLOUT;
228
229 if (flags & ULOOP_EDGE_TRIGGER)
230 ev.events |= EPOLLET;
231
232 ev.data.fd = fd->fd;
233 ev.data.ptr = fd;
234 fd->flags = flags;
235
236 return epoll_ctl(poll_fd, op, fd->fd, &ev);
237 }
238
239 static struct epoll_event events[ULOOP_MAX_EVENTS];
240
241 static int __uloop_fd_delete(struct uloop_fd *sock)
242 {
243 sock->flags = 0;
244 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
245 }
246
247 static int uloop_fetch_events(int timeout)
248 {
249 int n, nfds;
250
251 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
252 for (n = 0; n < nfds; ++n) {
253 struct uloop_fd_event *cur = &cur_fds[n];
254 struct uloop_fd *u = events[n].data.ptr;
255 unsigned int ev = 0;
256
257 cur->fd = u;
258 if (!u)
259 continue;
260
261 if (events[n].events & (EPOLLERR|EPOLLHUP)) {
262 u->error = true;
263 uloop_fd_delete(u);
264 }
265
266 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
267 cur->fd = NULL;
268 continue;
269 }
270
271 if(events[n].events & EPOLLRDHUP)
272 u->eof = true;
273
274 if(events[n].events & EPOLLIN)
275 ev |= ULOOP_READ;
276
277 if(events[n].events & EPOLLOUT)
278 ev |= ULOOP_WRITE;
279
280 cur->events = ev;
281 }
282
283 return nfds;
284 }
285
286 #endif
287
288 static void uloop_run_events(int timeout)
289 {
290 struct uloop_fd_event *cur;
291 struct uloop_fd *fd;
292
293 if (!cur_nfds) {
294 cur_fd = 0;
295 cur_nfds = uloop_fetch_events(timeout);
296 if (cur_nfds < 0)
297 cur_nfds = 0;
298 }
299
300 while (cur_nfds > 0) {
301 cur = &cur_fds[cur_fd++];
302 cur_nfds--;
303
304 fd = cur->fd;
305 if (!fd)
306 continue;
307
308 if (!fd->cb)
309 continue;
310
311 fd->cb(fd, cur->events);
312 return;
313 }
314 }
315
316 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
317 {
318 unsigned int fl;
319 int ret;
320
321 if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
322 return uloop_fd_delete(sock);
323
324 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
325 fl = fcntl(sock->fd, F_GETFL, 0);
326 fl |= O_NONBLOCK;
327 fcntl(sock->fd, F_SETFL, fl);
328 }
329
330 ret = register_poll(sock, flags);
331 if (ret < 0)
332 goto out;
333
334 sock->registered = true;
335 sock->eof = false;
336
337 out:
338 return ret;
339 }
340
341 int uloop_fd_delete(struct uloop_fd *fd)
342 {
343 int i;
344
345 if (!fd->registered)
346 return 0;
347
348 for (i = 0; i < cur_nfds; i++) {
349 if (cur_fds[cur_fd + i].fd != fd)
350 continue;
351
352 cur_fds[cur_fd + i].fd = NULL;
353 }
354 fd->registered = false;
355 return __uloop_fd_delete(fd);
356 }
357
358 static int tv_diff(struct timeval *t1, struct timeval *t2)
359 {
360 return
361 (t1->tv_sec - t2->tv_sec) * 1000 +
362 (t1->tv_usec - t2->tv_usec) / 1000;
363 }
364
365 int uloop_timeout_add(struct uloop_timeout *timeout)
366 {
367 struct uloop_timeout *tmp;
368 struct list_head *h = &timeouts;
369
370 if (timeout->pending)
371 return -1;
372
373 list_for_each_entry(tmp, &timeouts, list) {
374 if (tv_diff(&tmp->time, &timeout->time) > 0) {
375 h = &tmp->list;
376 break;
377 }
378 }
379
380 list_add_tail(&timeout->list, h);
381 timeout->pending = true;
382
383 return 0;
384 }
385
386 static void uloop_gettime(struct timeval *tv)
387 {
388 struct timespec ts;
389
390 clock_gettime(CLOCK_MONOTONIC, &ts);
391 tv->tv_sec = ts.tv_sec;
392 tv->tv_usec = ts.tv_nsec / 1000;
393 }
394
395 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
396 {
397 struct timeval *time = &timeout->time;
398
399 if (timeout->pending)
400 uloop_timeout_cancel(timeout);
401
402 uloop_gettime(&timeout->time);
403
404 time->tv_sec += msecs / 1000;
405 time->tv_usec += (msecs % 1000) * 1000;
406
407 if (time->tv_usec > 1000000) {
408 time->tv_sec++;
409 time->tv_usec %= 1000000;
410 }
411
412 return uloop_timeout_add(timeout);
413 }
414
415 int uloop_timeout_cancel(struct uloop_timeout *timeout)
416 {
417 if (!timeout->pending)
418 return -1;
419
420 list_del(&timeout->list);
421 timeout->pending = false;
422
423 return 0;
424 }
425
426 int uloop_timeout_remaining(struct uloop_timeout *timeout)
427 {
428 struct timeval now;
429
430 if (!timeout->pending)
431 return -1;
432
433 uloop_gettime(&now);
434
435 return tv_diff(&timeout->time, &now);
436 }
437
438 int uloop_process_add(struct uloop_process *p)
439 {
440 struct uloop_process *tmp;
441 struct list_head *h = &processes;
442
443 if (p->pending)
444 return -1;
445
446 list_for_each_entry(tmp, &processes, list) {
447 if (tmp->pid > p->pid) {
448 h = &tmp->list;
449 break;
450 }
451 }
452
453 list_add_tail(&p->list, h);
454 p->pending = true;
455
456 return 0;
457 }
458
459 int uloop_process_delete(struct uloop_process *p)
460 {
461 if (!p->pending)
462 return -1;
463
464 list_del(&p->list);
465 p->pending = false;
466
467 return 0;
468 }
469
470 static void uloop_handle_processes(void)
471 {
472 struct uloop_process *p, *tmp;
473 pid_t pid;
474 int ret;
475
476 do_sigchld = false;
477
478 while (1) {
479 pid = waitpid(-1, &ret, WNOHANG);
480 if (pid <= 0)
481 return;
482
483 list_for_each_entry_safe(p, tmp, &processes, list) {
484 if (p->pid < pid)
485 continue;
486
487 if (p->pid > pid)
488 break;
489
490 uloop_process_delete(p);
491 p->cb(p, ret);
492 }
493 }
494
495 }
496
497 static void uloop_handle_sigint(int signo)
498 {
499 uloop_cancelled = true;
500 }
501
502 static void uloop_sigchld(int signo)
503 {
504 do_sigchld = true;
505 }
506
507 static void uloop_setup_signals(void)
508 {
509 struct sigaction s;
510
511 memset(&s, 0, sizeof(struct sigaction));
512 s.sa_handler = uloop_handle_sigint;
513 s.sa_flags = 0;
514 sigaction(SIGINT, &s, NULL);
515
516 if (uloop_handle_sigchld) {
517 s.sa_handler = uloop_sigchld;
518 sigaction(SIGCHLD, &s, NULL);
519 }
520 }
521
522 static int uloop_get_next_timeout(struct timeval *tv)
523 {
524 struct uloop_timeout *timeout;
525 int diff;
526
527 if (list_empty(&timeouts))
528 return -1;
529
530 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
531 diff = tv_diff(&timeout->time, tv);
532 if (diff < 0)
533 return 0;
534
535 return diff;
536 }
537
538 static void uloop_process_timeouts(struct timeval *tv)
539 {
540 struct uloop_timeout *t;
541
542 while (!list_empty(&timeouts)) {
543 t = list_first_entry(&timeouts, struct uloop_timeout, list);
544
545 if (tv_diff(&t->time, tv) > 0)
546 break;
547
548 uloop_timeout_cancel(t);
549 if (t->cb)
550 t->cb(t);
551 }
552 }
553
554 static void uloop_clear_timeouts(void)
555 {
556 struct uloop_timeout *t, *tmp;
557
558 list_for_each_entry_safe(t, tmp, &timeouts, list)
559 uloop_timeout_cancel(t);
560 }
561
562 static void uloop_clear_processes(void)
563 {
564 struct uloop_process *p, *tmp;
565
566 list_for_each_entry_safe(p, tmp, &processes, list)
567 uloop_process_delete(p);
568 }
569
570 void uloop_run(void)
571 {
572 struct timeval tv;
573
574 uloop_setup_signals();
575 while(!uloop_cancelled)
576 {
577 uloop_gettime(&tv);
578 uloop_process_timeouts(&tv);
579 if (uloop_cancelled)
580 break;
581
582 if (do_sigchld)
583 uloop_handle_processes();
584 uloop_run_events(uloop_get_next_timeout(&tv));
585 }
586 }
587
588 void uloop_done(void)
589 {
590 if (poll_fd < 0)
591 return;
592
593 close(poll_fd);
594 poll_fd = -1;
595
596 uloop_clear_timeouts();
597 uloop_clear_processes();
598 }