2010-10-13 21:17:51 +02:00
|
|
|
/*
|
2013-01-13 09:47:51 +01:00
|
|
|
* uloop - event loop implementation
|
2010-10-13 21:17:51 +02:00
|
|
|
*
|
2016-05-17 12:24:04 +02:00
|
|
|
* Copyright (C) 2010-2016 Felix Fietkau <nbd@openwrt.org>
|
2010-10-13 21:17:51 +02:00
|
|
|
*
|
2013-01-13 09:47:51 +01:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2010-10-13 21:17:51 +02:00
|
|
|
*
|
2013-01-13 09:47:51 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2010-10-13 21:17:51 +02:00
|
|
|
*/
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
#include "uloop.h"
|
2012-12-31 15:02:31 +01:00
|
|
|
#include "utils.h"
|
2010-12-05 00:38:58 +01:00
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
|
|
|
#ifdef USE_EPOLL
|
2010-10-13 21:17:51 +02:00
|
|
|
#include <sys/epoll.h>
|
2010-12-05 00:38:58 +01:00
|
|
|
#endif
|
2011-08-11 23:37:48 +02:00
|
|
|
#include <sys/wait.h>
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
struct uloop_fd_event {
|
|
|
|
struct uloop_fd *fd;
|
|
|
|
unsigned int events;
|
|
|
|
};
|
|
|
|
|
2013-06-18 12:01:08 +02:00
|
|
|
struct uloop_fd_stack {
|
|
|
|
struct uloop_fd_stack *next;
|
|
|
|
struct uloop_fd *fd;
|
|
|
|
unsigned int events;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct uloop_fd_stack *fd_stack = NULL;
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
#define ULOOP_MAX_EVENTS 10
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
static struct list_head timeouts = LIST_HEAD_INIT(timeouts);
|
|
|
|
static struct list_head processes = LIST_HEAD_INIT(processes);
|
|
|
|
|
2011-02-11 01:25:17 +01:00
|
|
|
static int poll_fd = -1;
|
2011-02-06 16:13:58 +01:00
|
|
|
bool uloop_cancelled = false;
|
2017-07-03 13:37:56 +02:00
|
|
|
bool uloop_handle_sigchld = true;
|
2016-06-21 17:19:11 +02:00
|
|
|
static int uloop_status = 0;
|
2011-05-24 21:46:10 +02:00
|
|
|
static bool do_sigchld = false;
|
2013-06-11 12:21:09 +02:00
|
|
|
|
|
|
|
static struct uloop_fd_event cur_fds[ULOOP_MAX_EVENTS];
|
2012-10-21 03:40:35 +02:00
|
|
|
static int cur_fd, cur_nfds;
|
2017-02-03 16:52:17 +01:00
|
|
|
static int uloop_run_depth = 0;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags);
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
#ifdef USE_KQUEUE
|
2016-05-17 12:24:04 +02:00
|
|
|
#include "uloop-kqueue.c"
|
2010-12-05 00:38:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_EPOLL
|
2016-05-17 12:25:19 +02:00
|
|
|
#include "uloop-epoll.c"
|
2010-12-05 00:38:58 +01:00
|
|
|
#endif
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
static void waker_consume(struct uloop_fd *fd, unsigned int events)
|
|
|
|
{
|
|
|
|
char buf[4];
|
|
|
|
|
|
|
|
while (read(fd->fd, buf, 4) > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int waker_pipe = -1;
|
|
|
|
static struct uloop_fd waker_fd = {
|
|
|
|
.fd = -1,
|
|
|
|
.cb = waker_consume,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void waker_init_fd(int fd)
|
|
|
|
{
|
|
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
|
|
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int waker_init(void)
|
|
|
|
{
|
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
if (waker_pipe >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pipe(fds) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
waker_init_fd(fds[0]);
|
|
|
|
waker_init_fd(fds[1]);
|
2016-06-15 15:14:51 +02:00
|
|
|
waker_pipe = fds[1];
|
2016-06-09 04:20:32 +02:00
|
|
|
|
|
|
|
waker_fd.fd = fds[0];
|
|
|
|
waker_fd.cb = waker_consume;
|
|
|
|
uloop_fd_add(&waker_fd, ULOOP_READ);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-12 13:12:31 +02:00
|
|
|
static void uloop_setup_signals(bool add);
|
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
int uloop_init(void)
|
|
|
|
{
|
|
|
|
if (uloop_init_pollfd() < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (waker_init() < 0) {
|
|
|
|
uloop_done();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-12 13:12:31 +02:00
|
|
|
uloop_setup_signals(true);
|
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-18 12:01:08 +02:00
|
|
|
static bool uloop_fd_stack_event(struct uloop_fd *fd, int events)
|
|
|
|
{
|
|
|
|
struct uloop_fd_stack *cur;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not buffer events for level-triggered fds, they will keep firing.
|
|
|
|
* Caller needs to take care of recursion issues.
|
|
|
|
*/
|
|
|
|
if (!(fd->flags & ULOOP_EDGE_TRIGGER))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (cur = fd_stack; cur; cur = cur->next) {
|
|
|
|
if (cur->fd != fd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (events < 0)
|
|
|
|
cur->fd = NULL;
|
|
|
|
else
|
|
|
|
cur->events |= events | ULOOP_EVENT_BUFFERED;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
static void uloop_run_events(int timeout)
|
|
|
|
{
|
|
|
|
struct uloop_fd_event *cur;
|
|
|
|
struct uloop_fd *fd;
|
|
|
|
|
|
|
|
if (!cur_nfds) {
|
|
|
|
cur_fd = 0;
|
|
|
|
cur_nfds = uloop_fetch_events(timeout);
|
|
|
|
if (cur_nfds < 0)
|
|
|
|
cur_nfds = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (cur_nfds > 0) {
|
2013-06-18 12:01:08 +02:00
|
|
|
struct uloop_fd_stack stack_cur;
|
|
|
|
unsigned int events;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
cur = &cur_fds[cur_fd++];
|
|
|
|
cur_nfds--;
|
|
|
|
|
|
|
|
fd = cur->fd;
|
2013-06-18 12:01:08 +02:00
|
|
|
events = cur->events;
|
2013-06-11 12:21:09 +02:00
|
|
|
if (!fd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!fd->cb)
|
|
|
|
continue;
|
|
|
|
|
2013-06-18 12:01:08 +02:00
|
|
|
if (uloop_fd_stack_event(fd, cur->events))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
stack_cur.next = fd_stack;
|
|
|
|
stack_cur.fd = fd;
|
|
|
|
fd_stack = &stack_cur;
|
|
|
|
do {
|
|
|
|
stack_cur.events = 0;
|
|
|
|
fd->cb(fd, events);
|
|
|
|
events = stack_cur.events & ULOOP_EVENT_MASK;
|
|
|
|
} while (stack_cur.fd && events);
|
|
|
|
fd_stack = stack_cur.next;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
int uloop_fd_add(struct uloop_fd *sock, unsigned int flags)
|
|
|
|
{
|
|
|
|
unsigned int fl;
|
|
|
|
int ret;
|
|
|
|
|
2013-01-31 16:43:00 +01:00
|
|
|
if (!(flags & (ULOOP_READ | ULOOP_WRITE)))
|
|
|
|
return uloop_fd_delete(sock);
|
|
|
|
|
2010-12-06 16:54:13 +01:00
|
|
|
if (!sock->registered && !(flags & ULOOP_BLOCKING)) {
|
2010-12-05 00:38:58 +01:00
|
|
|
fl = fcntl(sock->fd, F_GETFL, 0);
|
|
|
|
fl |= O_NONBLOCK;
|
|
|
|
fcntl(sock->fd, F_SETFL, fl);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = register_poll(sock, flags);
|
2010-10-13 21:17:51 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
sock->registered = true;
|
|
|
|
sock->eof = false;
|
2014-04-26 16:55:17 +02:00
|
|
|
sock->error = false;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
int uloop_fd_delete(struct uloop_fd *fd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < cur_nfds; i++) {
|
|
|
|
if (cur_fds[cur_fd + i].fd != fd)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cur_fds[cur_fd + i].fd = NULL;
|
|
|
|
}
|
2013-08-01 00:01:00 +02:00
|
|
|
|
|
|
|
if (!fd->registered)
|
|
|
|
return 0;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
fd->registered = false;
|
2013-06-18 12:01:08 +02:00
|
|
|
uloop_fd_stack_event(fd, -1);
|
2013-06-11 12:21:09 +02:00
|
|
|
return __uloop_fd_delete(fd);
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
static int tv_diff(struct timeval *t1, struct timeval *t2)
|
|
|
|
{
|
2012-10-31 20:28:11 +01:00
|
|
|
return
|
|
|
|
(t1->tv_sec - t2->tv_sec) * 1000 +
|
|
|
|
(t1->tv_usec - t2->tv_usec) / 1000;
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uloop_timeout_add(struct uloop_timeout *timeout)
|
|
|
|
{
|
2011-05-24 21:46:10 +02:00
|
|
|
struct uloop_timeout *tmp;
|
|
|
|
struct list_head *h = &timeouts;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
if (timeout->pending)
|
|
|
|
return -1;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
list_for_each_entry(tmp, &timeouts, list) {
|
|
|
|
if (tv_diff(&tmp->time, &timeout->time) > 0) {
|
|
|
|
h = &tmp->list;
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
2011-05-24 21:46:10 +02:00
|
|
|
}
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
list_add_tail(&timeout->list, h);
|
2010-10-13 21:17:51 +02:00
|
|
|
timeout->pending = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-06 03:04:04 +01:00
|
|
|
static void uloop_gettime(struct timeval *tv)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
tv->tv_sec = ts.tv_sec;
|
|
|
|
tv->tv_usec = ts.tv_nsec / 1000;
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
|
|
|
|
{
|
|
|
|
struct timeval *time = &timeout->time;
|
|
|
|
|
|
|
|
if (timeout->pending)
|
|
|
|
uloop_timeout_cancel(timeout);
|
|
|
|
|
2015-01-21 14:21:27 +01:00
|
|
|
uloop_gettime(time);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
time->tv_sec += msecs / 1000;
|
2012-10-31 20:03:45 +01:00
|
|
|
time->tv_usec += (msecs % 1000) * 1000;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
if (time->tv_usec > 1000000) {
|
|
|
|
time->tv_sec++;
|
2015-01-21 14:21:27 +01:00
|
|
|
time->tv_usec -= 1000000;
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return uloop_timeout_add(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int uloop_timeout_cancel(struct uloop_timeout *timeout)
|
|
|
|
{
|
|
|
|
if (!timeout->pending)
|
|
|
|
return -1;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
list_del(&timeout->list);
|
|
|
|
timeout->pending = false;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2013-01-23 19:33:12 +01:00
|
|
|
int uloop_timeout_remaining(struct uloop_timeout *timeout)
|
2013-01-22 15:50:09 +01:00
|
|
|
{
|
|
|
|
struct timeval now;
|
|
|
|
|
|
|
|
if (!timeout->pending)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
uloop_gettime(&now);
|
|
|
|
|
|
|
|
return tv_diff(&timeout->time, &now);
|
|
|
|
}
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
int uloop_process_add(struct uloop_process *p)
|
|
|
|
{
|
|
|
|
struct uloop_process *tmp;
|
|
|
|
struct list_head *h = &processes;
|
|
|
|
|
|
|
|
if (p->pending)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_for_each_entry(tmp, &processes, list) {
|
|
|
|
if (tmp->pid > p->pid) {
|
|
|
|
h = &tmp->list;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&p->list, h);
|
|
|
|
p->pending = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uloop_process_delete(struct uloop_process *p)
|
|
|
|
{
|
|
|
|
if (!p->pending)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_del(&p->list);
|
|
|
|
p->pending = false;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
static void uloop_handle_processes(void)
|
|
|
|
{
|
|
|
|
struct uloop_process *p, *tmp;
|
|
|
|
pid_t pid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
do_sigchld = false;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
pid = waitpid(-1, &ret, WNOHANG);
|
2016-05-17 13:27:44 +02:00
|
|
|
if (pid < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
if (pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(p, tmp, &processes, list) {
|
|
|
|
if (p->pid < pid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p->pid > pid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
uloop_process_delete(p);
|
|
|
|
p->cb(p, ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
static void uloop_signal_wake(void)
|
|
|
|
{
|
2016-06-20 07:36:47 +02:00
|
|
|
do {
|
|
|
|
if (write(waker_pipe, "w", 1) < 0) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} while (1);
|
2016-06-09 04:20:32 +02:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:59:46 +02:00
|
|
|
static void uloop_handle_sigint(int signo)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
2016-06-21 17:19:11 +02:00
|
|
|
uloop_status = signo;
|
2016-05-19 10:59:46 +02:00
|
|
|
uloop_cancelled = true;
|
2016-06-09 04:20:32 +02:00
|
|
|
uloop_signal_wake();
|
2016-05-19 10:59:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void uloop_sigchld(int signo)
|
|
|
|
{
|
|
|
|
do_sigchld = true;
|
2016-06-09 04:20:32 +02:00
|
|
|
uloop_signal_wake();
|
2011-05-24 21:46:10 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:14:28 +02:00
|
|
|
static void uloop_install_handler(int signum, void (*handler)(int), struct sigaction* old, bool add)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
|
|
|
struct sigaction s;
|
2014-10-13 16:14:28 +02:00
|
|
|
struct sigaction *act;
|
2011-05-24 21:46:10 +02:00
|
|
|
|
2014-10-13 16:14:28 +02:00
|
|
|
act = NULL;
|
|
|
|
sigaction(signum, NULL, &s);
|
2011-05-24 21:46:10 +02:00
|
|
|
|
2013-10-22 10:24:15 +02:00
|
|
|
if (add) {
|
2014-10-13 16:14:28 +02:00
|
|
|
if (s.sa_handler == SIG_DFL) { /* Do not override existing custom signal handlers */
|
|
|
|
memcpy(old, &s, sizeof(struct sigaction));
|
|
|
|
s.sa_handler = handler;
|
|
|
|
s.sa_flags = 0;
|
|
|
|
act = &s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (s.sa_handler == handler) { /* Do not restore if someone modified our handler */
|
|
|
|
act = old;
|
2011-05-24 21:46:10 +02:00
|
|
|
}
|
2013-10-22 10:24:15 +02:00
|
|
|
|
2014-10-13 16:14:28 +02:00
|
|
|
if (act != NULL)
|
|
|
|
sigaction(signum, act, NULL);
|
|
|
|
}
|
2013-10-22 10:24:15 +02:00
|
|
|
|
2015-01-27 08:19:31 +01:00
|
|
|
static void uloop_ignore_signal(int signum, bool ignore)
|
|
|
|
{
|
|
|
|
struct sigaction s;
|
|
|
|
void *new_handler = NULL;
|
|
|
|
|
|
|
|
sigaction(signum, NULL, &s);
|
|
|
|
|
|
|
|
if (ignore) {
|
|
|
|
if (s.sa_handler == SIG_DFL) /* Ignore only if there isn't any custom handler */
|
|
|
|
new_handler = SIG_IGN;
|
|
|
|
} else {
|
|
|
|
if (s.sa_handler == SIG_IGN) /* Restore only if noone modified our SIG_IGN */
|
|
|
|
new_handler = SIG_DFL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_handler) {
|
|
|
|
s.sa_handler = new_handler;
|
|
|
|
s.sa_flags = 0;
|
|
|
|
sigaction(signum, &s, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 16:14:28 +02:00
|
|
|
static void uloop_setup_signals(bool add)
|
|
|
|
{
|
2016-05-19 10:59:46 +02:00
|
|
|
static struct sigaction old_sigint, old_sigchld, old_sigterm;
|
2016-05-17 13:23:28 +02:00
|
|
|
|
2016-05-19 10:59:46 +02:00
|
|
|
uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
|
|
|
|
uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
|
2017-07-03 13:37:56 +02:00
|
|
|
|
|
|
|
if (uloop_handle_sigchld)
|
|
|
|
uloop_install_handler(SIGCHLD, uloop_sigchld, &old_sigchld, add);
|
2015-01-27 08:19:31 +01:00
|
|
|
|
|
|
|
uloop_ignore_signal(SIGPIPE, add);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int uloop_get_next_timeout(struct timeval *tv)
|
|
|
|
{
|
2011-05-24 21:46:10 +02:00
|
|
|
struct uloop_timeout *timeout;
|
2010-10-13 21:17:51 +02:00
|
|
|
int diff;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
if (list_empty(&timeouts))
|
2010-10-13 21:17:51 +02:00
|
|
|
return -1;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
timeout = list_first_entry(&timeouts, struct uloop_timeout, list);
|
|
|
|
diff = tv_diff(&timeout->time, tv);
|
2010-10-13 21:17:51 +02:00
|
|
|
if (diff < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uloop_process_timeouts(struct timeval *tv)
|
|
|
|
{
|
2012-10-23 03:41:45 +02:00
|
|
|
struct uloop_timeout *t;
|
|
|
|
|
|
|
|
while (!list_empty(&timeouts)) {
|
|
|
|
t = list_first_entry(&timeouts, struct uloop_timeout, list);
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
if (tv_diff(&t->time, tv) > 0)
|
2010-10-13 21:17:51 +02:00
|
|
|
break;
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
uloop_timeout_cancel(t);
|
|
|
|
if (t->cb)
|
|
|
|
t->cb(t);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-08 22:27:40 +02:00
|
|
|
static void uloop_clear_timeouts(void)
|
|
|
|
{
|
|
|
|
struct uloop_timeout *t, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(t, tmp, &timeouts, list)
|
|
|
|
uloop_timeout_cancel(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uloop_clear_processes(void)
|
|
|
|
{
|
|
|
|
struct uloop_process *p, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(p, tmp, &processes, list)
|
|
|
|
uloop_process_delete(p);
|
|
|
|
}
|
|
|
|
|
2017-02-03 16:52:17 +01:00
|
|
|
bool uloop_cancelling(void)
|
|
|
|
{
|
|
|
|
return uloop_run_depth > 0 && uloop_cancelled;
|
|
|
|
}
|
|
|
|
|
2017-06-01 11:24:37 +02:00
|
|
|
int uloop_run_timeout(int timeout)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
2017-06-01 11:24:37 +02:00
|
|
|
int next_time = 0;
|
2010-10-13 21:17:51 +02:00
|
|
|
struct timeval tv;
|
|
|
|
|
2017-09-12 13:12:31 +02:00
|
|
|
uloop_run_depth++;
|
2013-10-22 10:24:15 +02:00
|
|
|
|
2016-06-21 17:19:11 +02:00
|
|
|
uloop_status = 0;
|
2014-05-05 02:41:34 +02:00
|
|
|
uloop_cancelled = false;
|
2016-06-21 17:19:11 +02:00
|
|
|
while (!uloop_cancelled)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
2013-01-06 03:04:04 +01:00
|
|
|
uloop_gettime(&tv);
|
2010-10-13 21:17:51 +02:00
|
|
|
uloop_process_timeouts(&tv);
|
2011-05-24 21:46:10 +02:00
|
|
|
|
|
|
|
if (do_sigchld)
|
|
|
|
uloop_handle_processes();
|
2015-03-21 05:49:38 +01:00
|
|
|
|
|
|
|
if (uloop_cancelled)
|
|
|
|
break;
|
|
|
|
|
2013-07-24 14:51:55 +02:00
|
|
|
uloop_gettime(&tv);
|
2017-06-01 11:24:37 +02:00
|
|
|
|
|
|
|
next_time = uloop_get_next_timeout(&tv);
|
2017-06-17 11:47:21 +02:00
|
|
|
if (timeout >= 0 && timeout < next_time)
|
2017-06-17 11:39:24 +02:00
|
|
|
next_time = timeout;
|
|
|
|
uloop_run_events(next_time);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
2013-10-22 10:24:15 +02:00
|
|
|
|
2017-09-12 13:12:31 +02:00
|
|
|
--uloop_run_depth;
|
2016-06-21 17:19:11 +02:00
|
|
|
|
|
|
|
return uloop_status;
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uloop_done(void)
|
|
|
|
{
|
2017-09-12 13:12:31 +02:00
|
|
|
uloop_setup_signals(false);
|
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
if (poll_fd >= 0) {
|
|
|
|
close(poll_fd);
|
|
|
|
poll_fd = -1;
|
|
|
|
}
|
2011-02-11 01:25:17 +01:00
|
|
|
|
2016-06-09 04:20:32 +02:00
|
|
|
if (waker_pipe >= 0) {
|
|
|
|
uloop_fd_delete(&waker_fd);
|
|
|
|
close(waker_pipe);
|
|
|
|
close(waker_fd.fd);
|
|
|
|
waker_pipe = -1;
|
|
|
|
}
|
2012-07-08 22:27:40 +02:00
|
|
|
|
|
|
|
uloop_clear_timeouts();
|
|
|
|
uloop_clear_processes();
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|