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 int uloop_fd_delete(struct uloop_fd
*sock
)
206 sock
->registered
= false;
207 return epoll_ctl(poll_fd
, EPOLL_CTL_DEL
, sock
->fd
, 0);
210 static void uloop_run_events(int timeout
)
212 struct epoll_event events
[ULOOP_MAX_EVENTS
];
215 nfds
= epoll_wait(poll_fd
, events
, ARRAY_SIZE(events
), timeout
);
216 for(n
= 0; n
< nfds
; ++n
)
218 struct uloop_fd
*u
= events
[n
].data
.ptr
;
221 if(events
[n
].events
& EPOLLERR
) {
226 if(!(events
[n
].events
& (EPOLLRDHUP
|EPOLLIN
|EPOLLOUT
|EPOLLERR
)))
229 if(events
[n
].events
& EPOLLRDHUP
)
232 if(events
[n
].events
& EPOLLIN
)
235 if(events
[n
].events
& EPOLLOUT
)
245 int uloop_fd_add(struct uloop_fd
*sock
, unsigned int flags
)
250 if (!sock
->registered
&& !(flags
& ULOOP_BLOCKING
)) {
251 fl
= fcntl(sock
->fd
, F_GETFL
, 0);
253 fcntl(sock
->fd
, F_SETFL
, fl
);
256 ret
= register_poll(sock
, flags
);
260 sock
->registered
= true;
267 static int tv_diff(struct timeval
*t1
, struct timeval
*t2
)
269 if (t1
->tv_sec
!= t2
->tv_sec
)
270 return (t1
->tv_sec
- t2
->tv_sec
) * 1000;
272 return (t1
->tv_usec
- t2
->tv_usec
) / 1000;
275 int uloop_timeout_add(struct uloop_timeout
*timeout
)
277 struct uloop_timeout
*tmp
;
278 struct list_head
*h
= &timeouts
;
280 if (timeout
->pending
)
283 list_for_each_entry(tmp
, &timeouts
, list
) {
284 if (tv_diff(&tmp
->time
, &timeout
->time
) > 0) {
290 list_add_tail(&timeout
->list
, h
);
291 timeout
->pending
= true;
296 int uloop_timeout_set(struct uloop_timeout
*timeout
, int msecs
)
298 struct timeval
*time
= &timeout
->time
;
300 if (timeout
->pending
)
301 uloop_timeout_cancel(timeout
);
303 gettimeofday(&timeout
->time
, NULL
);
305 time
->tv_sec
+= msecs
/ 1000;
306 time
->tv_usec
+= msecs
% 1000;
308 if (time
->tv_usec
> 1000000) {
310 time
->tv_usec
%= 100000;
313 return uloop_timeout_add(timeout
);
316 int uloop_timeout_cancel(struct uloop_timeout
*timeout
)
318 if (!timeout
->pending
)
321 list_del(&timeout
->list
);
322 timeout
->pending
= false;
327 int uloop_process_add(struct uloop_process
*p
)
329 struct uloop_process
*tmp
;
330 struct list_head
*h
= &processes
;
335 list_for_each_entry(tmp
, &processes
, list
) {
336 if (tmp
->pid
> p
->pid
) {
342 list_add_tail(&p
->list
, h
);
348 int uloop_process_delete(struct uloop_process
*p
)
359 static void uloop_handle_processes(void)
361 struct uloop_process
*p
, *tmp
;
368 pid
= waitpid(-1, &ret
, WNOHANG
);
372 list_for_each_entry_safe(p
, tmp
, &processes
, list
) {
379 uloop_process_delete(p
);
386 static void uloop_handle_sigint(int signo
)
388 uloop_cancelled
= true;
391 static void uloop_sigchld(int signo
)
396 static void uloop_setup_signals(void)
400 memset(&s
, 0, sizeof(struct sigaction
));
401 s
.sa_handler
= uloop_handle_sigint
;
403 sigaction(SIGINT
, &s
, NULL
);
405 if (uloop_handle_sigchld
) {
406 s
.sa_handler
= uloop_sigchld
;
407 sigaction(SIGCHLD
, &s
, NULL
);
411 static int uloop_get_next_timeout(struct timeval
*tv
)
413 struct uloop_timeout
*timeout
;
416 if (list_empty(&timeouts
))
419 timeout
= list_first_entry(&timeouts
, struct uloop_timeout
, list
);
420 diff
= tv_diff(&timeout
->time
, tv
);
427 static void uloop_process_timeouts(struct timeval
*tv
)
429 struct uloop_timeout
*t
, *tmp
;
431 list_for_each_entry_safe(t
, tmp
, &timeouts
, list
) {
432 if (tv_diff(&t
->time
, tv
) > 0)
435 uloop_timeout_cancel(t
);
445 uloop_setup_signals();
446 while(!uloop_cancelled
)
448 gettimeofday(&tv
, NULL
);
449 uloop_process_timeouts(&tv
);
454 uloop_handle_processes();
455 uloop_run_events(uloop_get_next_timeout(&tv
));
459 void uloop_done(void)