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>
37 #include <sys/event.h>
40 #include <sys/epoll.h>
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
48 #define ULOOP_MAX_EVENTS 10
50 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
51 static struct list_head processes = LIST_HEAD_INIT(processes);
53 static int poll_fd = -1;
54 bool uloop_cancelled = false;
55 bool uloop_handle_sigchld = true;
56 static bool do_sigchld = false;
73 static uint16_t get_flags(unsigned int flags, unsigned int mask)
81 if (flags & ULOOP_EDGE_TRIGGER)
87 static int register_poll(struct uloop_fd *fd, unsigned int flags)
89 struct timespec timeout = { 0, 0 };
94 changed = fd->kqflags ^ flags;
95 if (changed & ULOOP_EDGE_TRIGGER)
98 if (changed & ULOOP_READ) {
99 uint16_t kflags = get_flags(flags, ULOOP_READ);
100 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
103 if (changed & ULOOP_WRITE) {
104 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
105 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
108 if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
115 int uloop_fd_delete(struct uloop_fd *sock)
117 sock->registered = false;
118 return register_poll(sock, 0);
121 static void uloop_run_events(int timeout)
123 struct kevent events[ULOOP_MAX_EVENTS];
128 ts.tv_sec = timeout / 1000;
129 ts.tv_nsec = (timeout % 1000) * 1000000;
132 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL);
133 for(n = 0; n < nfds; ++n)
135 struct uloop_fd *u = events[n].udata;
138 if(events[n].flags & EV_ERROR) {
143 if(events[n].filter == EVFILT_READ)
145 else if (events[n].filter == EVFILT_WRITE)
148 if(events[n].flags & EV_EOF)
163 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
166 #define EPOLLRDHUP 0x2000
174 poll_fd = epoll_create(32);
178 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
182 static int register_poll(struct uloop_fd *fd, unsigned int flags)
184 struct epoll_event ev;
185 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
187 memset(&ev, 0, sizeof(struct epoll_event));
189 if (flags & ULOOP_READ)
190 ev.events |= EPOLLIN | EPOLLRDHUP;
192 if (flags & ULOOP_WRITE)
193 ev.events |= EPOLLOUT;
195 if (flags & ULOOP_EDGE_TRIGGER)
196 ev.events |= EPOLLET;
201 return epoll_ctl(poll_fd, op, fd->fd, &ev);
204 static int cur_fd, cur_nfds;
205 static struct epoll_event events[ULOOP_MAX_EVENTS];
207 int uloop_fd_delete(struct uloop_fd *sock)
211 for (i = cur_fd + 1; i < cur_nfds; i++) {
212 if (events[i].data.ptr != sock)
215 events[i].data.ptr = NULL;
217 sock->registered = false;
218 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
221 static void uloop_run_events(int timeout)
225 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
226 for(n = 0; n < nfds; ++n)
228 struct uloop_fd *u = events[n].data.ptr;
234 if(events[n].events & (EPOLLERR|EPOLLHUP)) {
239 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP)))
242 if(events[n].events & EPOLLRDHUP)
245 if(events[n].events & EPOLLIN)
248 if(events[n].events & EPOLLOUT)
262 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
267 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
268 fl = fcntl(sock->fd, F_GETFL, 0);
270 fcntl(sock->fd, F_SETFL, fl);
273 ret = register_poll(sock, flags);
277 sock->registered = true;
284 static int tv_diff(struct timeval *t1, struct timeval *t2)
286 if (t1->tv_sec != t2->tv_sec)
287 return (t1->tv_sec - t2->tv_sec) * 1000;
289 return (t1->tv_usec - t2->tv_usec) / 1000;
292 int uloop_timeout_add(struct uloop_timeout *timeout)
294 struct uloop_timeout *tmp;
295 struct list_head *h = &timeouts;
297 if (timeout->pending)
300 list_for_each_entry(tmp, &timeouts, list) {
301 if (tv_diff(&tmp->time, &timeout->time) > 0) {
307 list_add_tail(&timeout->list, h);
308 timeout->pending = true;
313 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
315 struct timeval *time = &timeout->time;
317 if (timeout->pending)
318 uloop_timeout_cancel(timeout);
320 gettimeofday(&timeout->time, NULL);
322 time->tv_sec += msecs / 1000;
323 time->tv_usec += msecs % 1000;
325 if (time->tv_usec > 1000000) {
327 time->tv_usec %= 100000;
330 return uloop_timeout_add(timeout);
333 int uloop_timeout_cancel(struct uloop_timeout *timeout)
335 if (!timeout->pending)
338 list_del(&timeout->list);
339 timeout->pending = false;
344 int uloop_process_add(struct uloop_process *p)
346 struct uloop_process *tmp;
347 struct list_head *h = &processes;
352 list_for_each_entry(tmp, &processes, list) {
353 if (tmp->pid > p->pid) {
359 list_add_tail(&p->list, h);
365 int uloop_process_delete(struct uloop_process *p)
376 static void uloop_handle_processes(void)
378 struct uloop_process *p, *tmp;
385 pid = waitpid(-1, &ret, WNOHANG);
389 list_for_each_entry_safe(p, tmp, &processes, list) {
396 uloop_process_delete(p);
403 static void uloop_handle_sigint(int signo)
405 uloop_cancelled = true;
408 static void uloop_sigchld(int signo)
413 static void uloop_setup_signals(void)
417 memset(&s, 0, sizeof(struct sigaction));
418 s.sa_handler = uloop_handle_sigint;
420 sigaction(SIGINT, &s, NULL);
422 if (uloop_handle_sigchld) {
423 s.sa_handler = uloop_sigchld;
424 sigaction(SIGCHLD, &s, NULL);
428 static int uloop_get_next_timeout(struct timeval *tv)
430 struct uloop_timeout *timeout;
433 if (list_empty(&timeouts))
436 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
437 diff = tv_diff(&timeout->time, tv);
444 static void uloop_process_timeouts(struct timeval *tv)
446 struct uloop_timeout *t, *tmp;
448 list_for_each_entry_safe(t, tmp, &timeouts, list) {
449 if (tv_diff(&t->time, tv) > 0)
452 uloop_timeout_cancel(t);
458 static void uloop_clear_timeouts(void)
460 struct uloop_timeout *t, *tmp;
462 list_for_each_entry_safe(t, tmp, &timeouts, list)
463 uloop_timeout_cancel(t);
466 static void uloop_clear_processes(void)
468 struct uloop_process *p, *tmp;
470 list_for_each_entry_safe(p, tmp, &processes, list)
471 uloop_process_delete(p);
478 uloop_setup_signals();
479 while(!uloop_cancelled)
481 gettimeofday(&tv, NULL);
482 uloop_process_timeouts(&tv);
487 uloop_handle_processes();
488 uloop_run_events(uloop_get_next_timeout(&tv));
492 void uloop_done(void)
500 uloop_clear_timeouts();
501 uloop_clear_processes();