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;
57 static int cur_fd
, cur_nfds
;
74 static uint16_t get_flags(unsigned int flags
, unsigned int mask
)
82 if (flags
& ULOOP_EDGE_TRIGGER
)
88 static struct kevent events
[ULOOP_MAX_EVENTS
];
90 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
92 struct timespec timeout
= { 0, 0 };
98 changed
= fd
->kqflags
^ flags
;
99 if (changed
& ULOOP_EDGE_TRIGGER
)
102 if (changed
& ULOOP_READ
) {
103 uint16_t kflags
= get_flags(flags
, ULOOP_READ
);
104 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_READ
, kflags
, 0, 0, fd
);
107 if (changed
& ULOOP_WRITE
) {
108 uint16_t kflags
= get_flags(flags
, ULOOP_WRITE
);
109 EV_SET(&ev
[nev
++], fd
->fd
, EVFILT_WRITE
, kflags
, 0, 0, fd
);
115 if (nev
&& (kevent(poll_fd
, ev
, nev
, NULL
, fl
, &timeout
) == -1))
122 int uloop_fd_delete(struct uloop_fd
*sock
)
126 for (i
= cur_fd
+ 1; i
< cur_nfds
; i
++) {
127 if (events
[i
].udata
!= sock
)
130 events
[i
].udata
= NULL
;
133 sock
->registered
= false;
134 return register_poll(sock
, 0);
137 static void uloop_run_events(int timeout
)
143 ts
.tv_sec
= timeout
/ 1000;
144 ts
.tv_nsec
= (timeout
% 1000) * 1000000;
147 nfds
= kevent(poll_fd
, NULL
, 0, events
, ARRAY_SIZE(events
), timeout
> 0 ? &ts
: NULL
);
148 for(n
= 0; n
< nfds
; ++n
)
150 struct uloop_fd
*u
= events
[n
].udata
;
156 if (events
[n
].flags
& EV_ERROR
) {
161 if(events
[n
].filter
== EVFILT_READ
)
163 else if (events
[n
].filter
== EVFILT_WRITE
)
166 if (events
[n
].flags
& EV_EOF
)
185 * FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
188 #define EPOLLRDHUP 0x2000
196 poll_fd
= epoll_create(32);
200 fcntl(poll_fd
, F_SETFD
, fcntl(poll_fd
, F_GETFD
) | FD_CLOEXEC
);
204 static int register_poll(struct uloop_fd
*fd
, unsigned int flags
)
206 struct epoll_event ev
;
207 int op
= fd
->registered
? EPOLL_CTL_MOD
: EPOLL_CTL_ADD
;
209 memset(&ev
, 0, sizeof(struct epoll_event
));
211 if (flags
& ULOOP_READ
)
212 ev
.events
|= EPOLLIN
| EPOLLRDHUP
;
214 if (flags
& ULOOP_WRITE
)
215 ev
.events
|= EPOLLOUT
;
217 if (flags
& ULOOP_EDGE_TRIGGER
)
218 ev
.events
|= EPOLLET
;
223 return epoll_ctl(poll_fd
, op
, fd
->fd
, &ev
);
226 static struct epoll_event events
[ULOOP_MAX_EVENTS
];
228 int uloop_fd_delete(struct uloop_fd
*sock
)
232 for (i
= cur_fd
+ 1; i
< cur_nfds
; i
++) {
233 if (events
[i
].data
.ptr
!= sock
)
236 events
[i
].data
.ptr
= NULL
;
238 sock
->registered
= false;
239 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
242 static void uloop_run_events(int timeout
)
246 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
247 for(n
= 0; n
< nfds
; ++n
)
249 struct uloop_fd
*u
= events
[n
].data
.ptr
;
255 if(events
[n
].events
& (EPOLLERR
|EPOLLHUP
)) {
260 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
|EPOLLHUP
)))
263 if(events
[n
].events
& EPOLLRDHUP
)
266 if(events
[n
].events
& EPOLLIN
)
269 if(events
[n
].events
& EPOLLOUT
)
283 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
288 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
289 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
291 fcntl(sock
->fd
, F_SETFL
, fl
);
294 ret
= register_poll(sock
, flags
);
298 sock
->registered
= true;
305 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
307 if (t1
->tv_sec
!= t2
->tv_sec
)
308 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
310 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
313 int uloop_timeout_add(struct uloop_timeout
*timeout
)
315 struct uloop_timeout
*tmp
;
316 struct list_head
*h
= &timeouts
;
318 if (timeout
->pending
)
321 list_for_each_entry(tmp
, &timeouts
, list
) {
322 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
328 list_add_tail(&timeout
->list
, h
);
329 timeout
->pending
= true;
334 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
336 struct timeval
*time
= &timeout
->time
;
338 if (timeout
->pending
)
339 uloop_timeout_cancel(timeout
);
341 gettimeofday(&timeout
->time
, NULL
);
343 time
->tv_sec
+= msecs
/ 1000;
344 time
->tv_usec
+= msecs
% 1000;
346 if (time
->tv_usec
> 1000000) {
348 time
->tv_usec
%= 100000;
351 return uloop_timeout_add(timeout
);
354 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
356 if (!timeout
->pending
)
359 list_del(&timeout
->list
);
360 timeout
->pending
= false;
365 int uloop_process_add(struct uloop_process
*p
)
367 struct uloop_process
*tmp
;
368 struct list_head
*h
= &processes
;
373 list_for_each_entry(tmp
, &processes
, list
) {
374 if (tmp
->pid
> p
->pid
) {
380 list_add_tail(&p
->list
, h
);
386 int uloop_process_delete(struct uloop_process
*p
)
397 static void uloop_handle_processes(void)
399 struct uloop_process
*p
, *tmp
;
406 pid
= waitpid(-1, &ret
, WNOHANG
);
410 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
417 uloop_process_delete(p
);
424 static void uloop_handle_sigint(int signo
)
426 uloop_cancelled
= true;
429 static void uloop_sigchld(int signo
)
434 static void uloop_setup_signals(void)
438 memset(&s
, 0, sizeof(struct sigaction
));
439 s
.sa_handler
= uloop_handle_sigint
;
441 sigaction(SIGINT
, &s
, NULL
);
443 if (uloop_handle_sigchld
) {
444 s
.sa_handler
= uloop_sigchld
;
445 sigaction(SIGCHLD
, &s
, NULL
);
449 static int uloop_get_next_timeout(struct timeval
*tv
)
451 struct uloop_timeout
*timeout
;
454 if (list_empty(&timeouts
))
457 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
458 diff
= tv_diff(&timeout
->time
, tv
);
465 static void uloop_process_timeouts(struct timeval
*tv
)
467 struct uloop_timeout
*t
, *tmp
;
469 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
) {
470 if (tv_diff(&t
->time
, tv
) > 0)
473 uloop_timeout_cancel(t
);
479 static void uloop_clear_timeouts(void)
481 struct uloop_timeout
*t
, *tmp
;
483 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
)
484 uloop_timeout_cancel(t
);
487 static void uloop_clear_processes(void)
489 struct uloop_process
*p
, *tmp
;
491 list_for_each_entry_safe(p
, tmp
, &processes
, list
)
492 uloop_process_delete(p
);
499 uloop_setup_signals();
500 while(!uloop_cancelled
)
502 gettimeofday(&tv
, NULL
);
503 uloop_process_timeouts(&tv
);
508 uloop_handle_processes();
509 uloop_run_events(uloop_get_next_timeout(&tv
));
513 void uloop_done(void)
521 uloop_clear_timeouts();
522 uloop_clear_processes();