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>
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
47 #define ULOOP_MAX_EVENTS 10
49 static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
50 static struct list_head processes = LIST_HEAD_INIT(processes);
52 static int poll_fd = -1;
53 bool uloop_cancelled = false;
54 bool uloop_handle_sigchld = true;
55 static bool do_sigchld = false;
72 static uint16_t get_flags(unsigned int flags, unsigned int mask)
80 if (flags & ULOOP_EDGE_TRIGGER)
86 static int register_poll(struct uloop_fd *fd, unsigned int flags)
88 struct timespec timeout = { 0, 0 };
93 changed = fd->kqflags ^ flags;
94 if (changed & ULOOP_EDGE_TRIGGER)
97 if (changed & ULOOP_READ) {
98 uint16_t kflags = get_flags(flags, ULOOP_READ);
99 EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
102 if (changed & ULOOP_WRITE) {
103 uint16_t kflags = get_flags(flags, ULOOP_WRITE);
104 EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
107 if (nev && (kevent(poll_fd, ev, nev, NULL, 0, &timeout) == -1))
114 int uloop_fd_delete(struct uloop_fd *sock)
116 sock->registered = false;
117 return register_poll(sock, 0);
120 static void uloop_run_events(int timeout)
122 struct kevent events[ULOOP_MAX_EVENTS];
127 ts.tv_sec = timeout / 1000;
128 ts.tv_nsec = (timeout % 1000) * 1000000;
131 nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout > 0 ? &ts : NULL);
132 for(n = 0; n < nfds; ++n)
134 struct uloop_fd *u = events[n].udata;
137 if(events[n].flags & EV_ERROR) {
142 if(events[n].filter == EVFILT_READ)
144 else if (events[n].filter == EVFILT_WRITE)
147 if(events[n].flags & EV_EOF)
162 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
165 #define EPOLLRDHUP 0x2000
173 poll_fd = epoll_create(32);
177 fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
181 static int register_poll(struct uloop_fd *fd, unsigned int flags)
183 struct epoll_event ev;
184 int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
186 memset(&ev, 0, sizeof(struct epoll_event));
188 if (flags & ULOOP_READ)
189 ev.events |= EPOLLIN | EPOLLRDHUP;
191 if (flags & ULOOP_WRITE)
192 ev.events |= EPOLLOUT;
194 if (flags & ULOOP_EDGE_TRIGGER)
195 ev.events |= EPOLLET;
200 return epoll_ctl(poll_fd, op, fd->fd, &ev);
203 int uloop_fd_delete(struct uloop_fd *sock)
205 sock->registered = false;
206 return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
209 static void uloop_run_events(int timeout)
211 struct epoll_event events[ULOOP_MAX_EVENTS];
214 nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
215 for(n = 0; n < nfds; ++n)
217 struct uloop_fd *u = events[n].data.ptr;
220 if(events[n].events & EPOLLERR) {
225 if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR)))
228 if(events[n].events & EPOLLRDHUP)
231 if(events[n].events & EPOLLIN)
234 if(events[n].events & EPOLLOUT)
244 int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
249 if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
250 fl = fcntl(sock->fd, F_GETFL, 0);
252 fcntl(sock->fd, F_SETFL, fl);
255 ret = register_poll(sock, flags);
259 sock->registered = true;
266 static int tv_diff(struct timeval *t1, struct timeval *t2)
268 if (t1->tv_sec != t2->tv_sec)
269 return (t1->tv_sec - t2->tv_sec) * 1000;
271 return (t1->tv_usec - t2->tv_usec) / 1000;
274 int uloop_timeout_add(struct uloop_timeout *timeout)
276 struct uloop_timeout *tmp;
277 struct list_head *h = &timeouts;
279 if (timeout->pending)
282 list_for_each_entry(tmp, &timeouts, list) {
283 if (tv_diff(&tmp->time, &timeout->time) > 0) {
289 list_add_tail(&timeout->list, h);
290 timeout->pending = true;
295 int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
297 struct timeval *time = &timeout->time;
299 if (timeout->pending)
300 uloop_timeout_cancel(timeout);
302 gettimeofday(&timeout->time, NULL);
304 time->tv_sec += msecs / 1000;
305 time->tv_usec += msecs % 1000;
307 if (time->tv_usec > 1000000) {
309 time->tv_usec %= 100000;
312 return uloop_timeout_add(timeout);
315 int uloop_timeout_cancel(struct uloop_timeout *timeout)
317 if (!timeout->pending)
320 list_del(&timeout->list);
321 timeout->pending = false;
326 int uloop_process_add(struct uloop_process *p)
328 struct uloop_process *tmp;
329 struct list_head *h = &processes;
334 list_for_each_entry(tmp, &processes, list) {
335 if (tmp->pid > p->pid) {
341 list_add_tail(&p->list, h);
347 int uloop_process_delete(struct uloop_process *p)
358 static void uloop_handle_processes(void)
360 struct uloop_process *p, *tmp;
367 pid = waitpid(-1, &ret, WNOHANG);
371 list_for_each_entry_safe(p, tmp, &processes, list) {
378 uloop_process_delete(p);
385 static void uloop_handle_sigint(int signo)
387 uloop_cancelled = true;
390 static void uloop_sigchld(int signo)
395 static void uloop_setup_signals(void)
399 memset(&s, 0, sizeof(struct sigaction));
400 s.sa_handler = uloop_handle_sigint;
402 sigaction(SIGINT, &s, NULL);
404 if (uloop_handle_sigchld) {
405 s.sa_handler = uloop_sigchld;
406 sigaction(SIGCHLD, &s, NULL);
410 static int uloop_get_next_timeout(struct timeval *tv)
412 struct uloop_timeout *timeout;
415 if (list_empty(&timeouts))
418 timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
419 diff = tv_diff(&timeout->time, tv);
426 static void uloop_process_timeouts(struct timeval *tv)
428 struct uloop_timeout *t, *tmp;
430 list_for_each_entry_safe(t, tmp, &timeouts, list) {
431 if (tv_diff(&t->time, tv) > 0)
434 uloop_timeout_cancel(t);
444 uloop_setup_signals();
445 while(!uloop_cancelled)
447 gettimeofday(&tv, NULL);
448 uloop_process_timeouts(&tv);
453 uloop_handle_processes();
454 uloop_run_events(uloop_get_next_timeout(&tv));
458 void uloop_done(void)