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;
71 static uint16_t get_flags(unsigned int flags, unsigned int mask)
79 if (flags & ULOOP_EDGE_TRIGGER)
85 static struct kevent events[ULOOP_MAX_EVENTS];
87 static int register_poll(struct uloop_fd *fd, unsigned int flags)
89 struct timespec timeout = { 0, 0 };
95 changed = fd->kqflags ^ flags;
96 if (changed & ULOOP_EDGE_TRIGGER)
99 if (changed & ULOOP_READ) {
100 uint16_t kflags = get_flags(flags, ULOOP_READ);
101 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
104 if (changed & ULOOP_WRITE) {
105 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
106 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
112 if (nev && (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1))
119 int uloop_fd_delete(struct uloop_fd *sock)
123 for (i = cur_fd + 1; i < cur_nfds; i++) {
124 if (events[i].udata != sock)
127 events[i].udata = NULL;
130 sock->registered = false;
131 return register_poll(sock, 0);
134 static void uloop_run_events(int timeout)
140 ts.tv_sec = timeout / 1000;
141 ts.tv_nsec = (timeout % 1000) * 1000000;
144 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
145 for(n = 0; n < nfds; ++n)
147 struct uloop_fd *u = events[n].udata;
153 if (events[n].flags & EV_ERROR) {
158 if(events[n].filter == EVFILT_READ)
160 else if (events[n].filter == EVFILT_WRITE)
163 if (events[n].flags & EV_EOF)
182 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
185 #define EPOLLRDHUP 0x2000
193 poll_fd = epoll_create(32);
197 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
201 static int register_poll(struct uloop_fd *fd, unsigned int flags)
203 struct epoll_event ev;
204 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
206 memset(&ev, 0, sizeof(struct epoll_event));
208 if (flags & ULOOP_READ)
209 ev.events |= EPOLLIN | EPOLLRDHUP;
211 if (flags & ULOOP_WRITE)
212 ev.events |= EPOLLOUT;
214 if (flags & ULOOP_EDGE_TRIGGER)
215 ev.events |= EPOLLET;
220 return epoll_ctl(poll_fd, op, fd->fd, &ev);
223 static struct epoll_event events[ULOOP_MAX_EVENTS];
225 int uloop_fd_delete(struct uloop_fd *sock)
229 for (i = cur_fd + 1; i < cur_nfds; i++) {
230 if (events[i].data.ptr != sock)
233 events[i].data.ptr = NULL;
235 sock->registered = false;
236 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
239 static void uloop_run_events(int timeout)
243 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
244 for(n = 0; n < nfds; ++n)
246 struct uloop_fd *u = events[n].data.ptr;
252 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
257 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
260 if(events[n].events & EPOLLRDHUP)
263 if(events[n].events & EPOLLIN)
266 if(events[n].events & EPOLLOUT)
280 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
285 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
286 fl = fcntl(sock->fd, F_GETFL, 0);
288 fcntl(sock->fd, F_SETFL, fl);
291 ret = register_poll(sock, flags);
295 sock->registered = true;
302 static int tv_diff(struct timeval *t1, struct timeval *t2)
305 (t1->tv_sec - t2->tv_sec) * 1000 +
306 (t1->tv_usec - t2->tv_usec) / 1000;
309 int uloop_timeout_add(struct uloop_timeout *timeout)
311 struct uloop_timeout *tmp;
312 struct list_head *h = &timeouts;
314 if (timeout->pending)
317 list_for_each_entry(tmp, &timeouts, list) {
318 if (tv_diff(&tmp->time, &timeout->time) > 0) {
324 list_add_tail(&timeout->list, h);
325 timeout->pending = true;
330 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
332 struct timeval *time = &timeout->time;
334 if (timeout->pending)
335 uloop_timeout_cancel(timeout);
337 gettimeofday(&timeout->time, NULL);
339 time->tv_sec += msecs / 1000;
340 time->tv_usec += (msecs % 1000) * 1000;
342 if (time->tv_usec > 1000000) {
344 time->tv_usec %= 1000000;
347 return uloop_timeout_add(timeout);
350 int uloop_timeout_cancel(struct uloop_timeout *timeout)
352 if (!timeout->pending)
355 list_del(&timeout->list);
356 timeout->pending = false;
361 int uloop_process_add(struct uloop_process *p)
363 struct uloop_process *tmp;
364 struct list_head *h = &processes;
369 list_for_each_entry(tmp, &processes, list) {
370 if (tmp->pid > p->pid) {
376 list_add_tail(&p->list, h);
382 int uloop_process_delete(struct uloop_process *p)
393 static void uloop_handle_processes(void)
395 struct uloop_process *p, *tmp;
402 pid = waitpid(-1, &ret, WNOHANG);
406 list_for_each_entry_safe(p, tmp, &processes, list) {
413 uloop_process_delete(p);
420 static void uloop_handle_sigint(int signo)
422 uloop_cancelled = true;
425 static void uloop_sigchld(int signo)
430 static void uloop_setup_signals(void)
434 memset(&s, 0, sizeof(struct sigaction));
435 s.sa_handler = uloop_handle_sigint;
437 sigaction(SIGINT, &s, NULL);
439 if (uloop_handle_sigchld) {
440 s.sa_handler = uloop_sigchld;
441 sigaction(SIGCHLD, &s, NULL);
445 static int uloop_get_next_timeout(struct timeval *tv)
447 struct uloop_timeout *timeout;
450 if (list_empty(&timeouts))
453 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
454 diff = tv_diff(&timeout->time, tv);
461 static void uloop_process_timeouts(struct timeval *tv)
463 struct uloop_timeout *t;
465 while (!list_empty(&timeouts)) {
466 t = list_first_entry(&timeouts, struct uloop_timeout, list);
468 if (tv_diff(&t->time, tv) > 0)
471 uloop_timeout_cancel(t);
477 static void uloop_clear_timeouts(void)
479 struct uloop_timeout *t, *tmp;
481 list_for_each_entry_safe(t, tmp, &timeouts, list)
482 uloop_timeout_cancel(t);
485 static void uloop_clear_processes(void)
487 struct uloop_process *p, *tmp;
489 list_for_each_entry_safe(p, tmp, &processes, list)
490 uloop_process_delete(p);
497 uloop_setup_signals();
498 while(!uloop_cancelled)
500 gettimeofday(&tv, NULL);
501 uloop_process_timeouts(&tv);
506 uloop_handle_processes();
507 uloop_run_events(uloop_get_next_timeout(&tv));
511 void uloop_done(void)
519 uloop_clear_timeouts();
520 uloop_clear_processes();