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>
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.
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.
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.
23 #include <sys/types.h>
38 #include <sys/event.h>
41 #include <sys/epoll.h>
45 #define ULOOP_MAX_EVENTS 10
47 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
48 static struct list_head processes = LIST_HEAD_INIT(processes);
50 static int poll_fd = -1;
51 bool uloop_cancelled = false;
52 bool uloop_handle_sigchld = true;
53 static bool do_sigchld = false;
54 static int cur_fd, cur_nfds;
60 struct timespec timeout = { 0, 0 };
61 struct kevent ev = {};
70 EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
71 kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
77 static uint16_t get_flags(unsigned int flags, unsigned int mask)
85 if (flags & ULOOP_EDGE_TRIGGER)
91 static struct kevent events[ULOOP_MAX_EVENTS];
93 static int register_kevent(struct uloop_fd *fd, unsigned int flags)
95 struct timespec timeout = { 0, 0 };
102 if (flags & ULOOP_EDGE_DEFER)
103 flags &= ~ULOOP_EDGE_TRIGGER;
105 changed = flags ^ fd->flags;
106 if (changed & ULOOP_EDGE_TRIGGER)
109 if (changed & ULOOP_READ) {
110 kflags = get_flags(flags, ULOOP_READ);
111 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
114 if (changed & ULOOP_WRITE) {
115 kflags = get_flags(flags, ULOOP_WRITE);
116 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
123 if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
129 static int register_poll(struct uloop_fd *fd, unsigned int flags)
131 if (flags & ULOOP_EDGE_TRIGGER)
132 flags |= ULOOP_EDGE_DEFER;
134 flags &= ~ULOOP_EDGE_DEFER;
136 return register_kevent(fd, flags);
139 int uloop_fd_delete(struct uloop_fd *sock)
143 for (i = cur_fd + 1; i < cur_nfds; i++) {
144 if (events[i].udata != sock)
147 events[i].udata = NULL;
150 sock->registered = false;
151 return register_poll(sock, 0);
154 static void uloop_run_events(int timeout)
160 ts.tv_sec = timeout / 1000;
161 ts.tv_nsec = (timeout % 1000) * 1000000;
164 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
165 for(n = 0; n < nfds; ++n)
167 struct uloop_fd *u = events[n].udata;
173 if (events[n].flags & EV_ERROR) {
178 if(events[n].filter == EVFILT_READ)
180 else if (events[n].filter == EVFILT_WRITE)
183 if (events[n].flags & EV_EOF)
192 if (u->flags & ULOOP_EDGE_DEFER) {
193 u->flags &= ~ULOOP_EDGE_DEFER;
194 register_kevent(u, u->flags);
206 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
209 #define EPOLLRDHUP 0x2000
217 poll_fd = epoll_create(32);
221 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
225 static int register_poll(struct uloop_fd *fd, unsigned int flags)
227 struct epoll_event ev;
228 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
230 memset(&ev, 0, sizeof(struct epoll_event));
232 if (flags & ULOOP_READ)
233 ev.events |= EPOLLIN | EPOLLRDHUP;
235 if (flags & ULOOP_WRITE)
236 ev.events |= EPOLLOUT;
238 if (flags & ULOOP_EDGE_TRIGGER)
239 ev.events |= EPOLLET;
244 return epoll_ctl(poll_fd, op, fd->fd, &ev);
247 static struct epoll_event events[ULOOP_MAX_EVENTS];
249 int uloop_fd_delete(struct uloop_fd *sock)
253 for (i = cur_fd + 1; i < cur_nfds; i++) {
254 if (events[i].data.ptr != sock)
257 events[i].data.ptr = NULL;
259 sock->registered = false;
260 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
263 static void uloop_run_events(int timeout)
267 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
268 for(n = 0; n < nfds; ++n)
270 struct uloop_fd *u = events[n].data.ptr;
276 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
281 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
284 if(events[n].events & EPOLLRDHUP)
287 if(events[n].events & EPOLLIN)
290 if(events[n].events & EPOLLOUT)
304 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
309 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
310 fl = fcntl(sock->fd, F_GETFL, 0);
312 fcntl(sock->fd, F_SETFL, fl);
315 ret = register_poll(sock, flags);
319 sock->registered = true;
326 static int tv_diff(struct timeval *t1, struct timeval *t2)
329 (t1->tv_sec - t2->tv_sec) * 1000 +
330 (t1->tv_usec - t2->tv_usec) / 1000;
333 int uloop_timeout_add(struct uloop_timeout *timeout)
335 struct uloop_timeout *tmp;
336 struct list_head *h = &timeouts;
338 if (timeout->pending)
341 list_for_each_entry(tmp, &timeouts, list) {
342 if (tv_diff(&tmp->time, &timeout->time) > 0) {
348 list_add_tail(&timeout->list, h);
349 timeout->pending = true;
354 static void uloop_gettime(struct timeval *tv)
358 clock_gettime(CLOCK_MONOTONIC, &ts);
359 tv->tv_sec = ts.tv_sec;
360 tv->tv_usec = ts.tv_nsec / 1000;
363 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
365 struct timeval *time = &timeout->time;
367 if (timeout->pending)
368 uloop_timeout_cancel(timeout);
370 uloop_gettime(&timeout->time);
372 time->tv_sec += msecs / 1000;
373 time->tv_usec += (msecs % 1000) * 1000;
375 if (time->tv_usec > 1000000) {
377 time->tv_usec %= 1000000;
380 return uloop_timeout_add(timeout);
383 int uloop_timeout_cancel(struct uloop_timeout *timeout)
385 if (!timeout->pending)
388 list_del(&timeout->list);
389 timeout->pending = false;
394 int uloop_process_add(struct uloop_process *p)
396 struct uloop_process *tmp;
397 struct list_head *h = &processes;
402 list_for_each_entry(tmp, &processes, list) {
403 if (tmp->pid > p->pid) {
409 list_add_tail(&p->list, h);
415 int uloop_process_delete(struct uloop_process *p)
426 static void uloop_handle_processes(void)
428 struct uloop_process *p, *tmp;
435 pid = waitpid(-1, &ret, WNOHANG);
439 list_for_each_entry_safe(p, tmp, &processes, list) {
446 uloop_process_delete(p);
453 static void uloop_handle_sigint(int signo)
455 uloop_cancelled = true;
458 static void uloop_sigchld(int signo)
463 static void uloop_setup_signals(void)
467 memset(&s, 0, sizeof(struct sigaction));
468 s.sa_handler = uloop_handle_sigint;
470 sigaction(SIGINT, &s, NULL);
472 if (uloop_handle_sigchld) {
473 s.sa_handler = uloop_sigchld;
474 sigaction(SIGCHLD, &s, NULL);
478 static int uloop_get_next_timeout(struct timeval *tv)
480 struct uloop_timeout *timeout;
483 if (list_empty(&timeouts))
486 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
487 diff = tv_diff(&timeout->time, tv);
494 static void uloop_process_timeouts(struct timeval *tv)
496 struct uloop_timeout *t;
498 while (!list_empty(&timeouts)) {
499 t = list_first_entry(&timeouts, struct uloop_timeout, list);
501 if (tv_diff(&t->time, tv) > 0)
504 uloop_timeout_cancel(t);
510 static void uloop_clear_timeouts(void)
512 struct uloop_timeout *t, *tmp;
514 list_for_each_entry_safe(t, tmp, &timeouts, list)
515 uloop_timeout_cancel(t);
518 static void uloop_clear_processes(void)
520 struct uloop_process *p, *tmp;
522 list_for_each_entry_safe(p, tmp, &processes, list)
523 uloop_process_delete(p);
530 uloop_setup_signals();
531 while(!uloop_cancelled)
534 uloop_process_timeouts(&tv);
539 uloop_handle_processes();
540 uloop_run_events(uloop_get_next_timeout(&tv));
544 void uloop_done(void)
552 uloop_clear_timeouts();
553 uloop_clear_processes();