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
|
|
|
*
|
2013-01-13 09:47:51 +01:00
|
|
|
* Copyright (C) 2010-2013 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;
|
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;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
|
|
|
|
int uloop_init(void)
|
|
|
|
{
|
2013-01-04 03:13:29 +01:00
|
|
|
struct timespec timeout = { 0, 0 };
|
|
|
|
struct kevent ev = {};
|
|
|
|
|
2011-02-11 01:25:17 +01:00
|
|
|
if (poll_fd >= 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
poll_fd = kqueue();
|
|
|
|
if (poll_fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
2013-01-04 03:13:29 +01:00
|
|
|
EV_SET(&ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
|
|
|
|
kevent(poll_fd, &ev, 1, NULL, 0, &timeout);
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint16_t get_flags(unsigned int flags, unsigned int mask)
|
|
|
|
{
|
|
|
|
uint16_t kflags = 0;
|
|
|
|
|
|
|
|
if (!(flags & mask))
|
|
|
|
return EV_DELETE;
|
|
|
|
|
|
|
|
kflags = EV_ADD;
|
|
|
|
if (flags & ULOOP_EDGE_TRIGGER)
|
|
|
|
kflags |= EV_CLEAR;
|
|
|
|
|
|
|
|
return kflags;
|
|
|
|
}
|
|
|
|
|
2012-10-21 03:40:35 +02:00
|
|
|
static struct kevent events[ULOOP_MAX_EVENTS];
|
|
|
|
|
2013-01-04 03:17:51 +01:00
|
|
|
static int register_kevent(struct uloop_fd *fd, unsigned int flags)
|
2010-12-05 00:38:58 +01:00
|
|
|
{
|
|
|
|
struct timespec timeout = { 0, 0 };
|
|
|
|
struct kevent ev[2];
|
|
|
|
int nev = 0;
|
2012-10-21 03:40:35 +02:00
|
|
|
unsigned int fl = 0;
|
2013-01-05 16:04:41 +01:00
|
|
|
unsigned int changed;
|
2013-01-04 01:15:45 +01:00
|
|
|
uint16_t kflags;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2013-01-04 03:17:51 +01:00
|
|
|
if (flags & ULOOP_EDGE_DEFER)
|
|
|
|
flags &= ~ULOOP_EDGE_TRIGGER;
|
|
|
|
|
2013-01-05 16:04:41 +01:00
|
|
|
changed = flags ^ fd->flags;
|
|
|
|
if (changed & ULOOP_EDGE_TRIGGER)
|
|
|
|
changed |= flags;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2013-01-05 16:04:41 +01:00
|
|
|
if (changed & ULOOP_READ) {
|
|
|
|
kflags = get_flags(flags, ULOOP_READ);
|
|
|
|
EV_SET(&ev[nev++], fd->fd, EVFILT_READ, kflags, 0, 0, fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changed & ULOOP_WRITE) {
|
|
|
|
kflags = get_flags(flags, ULOOP_WRITE);
|
|
|
|
EV_SET(&ev[nev++], fd->fd, EVFILT_WRITE, kflags, 0, 0, fd);
|
|
|
|
}
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2012-10-21 03:40:35 +02:00
|
|
|
if (!flags)
|
|
|
|
fl |= EV_DELETE;
|
|
|
|
|
2013-01-05 16:04:41 +01:00
|
|
|
fd->flags = flags;
|
2013-01-04 03:17:51 +01:00
|
|
|
if (kevent(poll_fd, ev, nev, NULL, fl, &timeout) == -1)
|
2010-12-05 00:38:58 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-04 03:17:51 +01:00
|
|
|
static int register_poll(struct uloop_fd *fd, unsigned int flags)
|
|
|
|
{
|
|
|
|
if (flags & ULOOP_EDGE_TRIGGER)
|
|
|
|
flags |= ULOOP_EDGE_DEFER;
|
|
|
|
else
|
|
|
|
flags &= ~ULOOP_EDGE_DEFER;
|
|
|
|
|
|
|
|
return register_kevent(fd, flags);
|
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
static int __uloop_fd_delete(struct uloop_fd *fd)
|
2010-12-05 00:38:58 +01:00
|
|
|
{
|
2013-06-11 12:21:09 +02:00
|
|
|
return register_poll(fd, 0);
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
static int uloop_fetch_events(int timeout)
|
2010-12-05 00:38:58 +01:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
int nfds, n;
|
|
|
|
|
2013-01-01 23:37:17 +01:00
|
|
|
if (timeout >= 0) {
|
2010-12-05 00:38:58 +01:00
|
|
|
ts.tv_sec = timeout / 1000;
|
2011-02-12 21:33:32 +01:00
|
|
|
ts.tv_nsec = (timeout % 1000) * 1000000;
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
2013-01-01 23:37:17 +01:00
|
|
|
nfds = kevent(poll_fd, NULL, 0, events, ARRAY_SIZE(events), timeout >= 0 ? &ts : NULL);
|
2013-06-11 12:21:09 +02:00
|
|
|
for (n = 0; n < nfds; n++) {
|
|
|
|
struct uloop_fd_event *cur = &cur_fds[n];
|
2010-12-05 00:38:58 +01:00
|
|
|
struct uloop_fd *u = events[n].udata;
|
|
|
|
unsigned int ev = 0;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
cur->fd = u;
|
2012-10-21 03:40:35 +02:00
|
|
|
if (!u)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (events[n].flags & EV_ERROR) {
|
2010-12-05 00:38:58 +01:00
|
|
|
u->error = true;
|
2014-02-11 09:37:08 +01:00
|
|
|
if (!(u->flags & ULOOP_ERROR_CB))
|
|
|
|
uloop_fd_delete(u);
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(events[n].filter == EVFILT_READ)
|
|
|
|
ev |= ULOOP_READ;
|
|
|
|
else if (events[n].filter == EVFILT_WRITE)
|
|
|
|
ev |= ULOOP_WRITE;
|
|
|
|
|
2012-10-21 03:40:35 +02:00
|
|
|
if (events[n].flags & EV_EOF)
|
2010-12-05 00:38:58 +01:00
|
|
|
u->eof = true;
|
|
|
|
else if (!ev)
|
2013-06-11 12:21:09 +02:00
|
|
|
cur->fd = NULL;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
2013-06-18 11:51:50 +02:00
|
|
|
cur->events = ev;
|
2013-06-11 12:21:09 +02:00
|
|
|
if (u->flags & ULOOP_EDGE_DEFER) {
|
|
|
|
u->flags &= ~ULOOP_EDGE_DEFER;
|
2013-06-18 09:57:39 +02:00
|
|
|
u->flags |= ULOOP_EDGE_TRIGGER;
|
2013-06-11 12:21:09 +02:00
|
|
|
register_kevent(u, u->flags);
|
2012-10-21 03:40:35 +02:00
|
|
|
}
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
2013-06-11 12:21:09 +02:00
|
|
|
return nfds;
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_EPOLL
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FIXME: uClibc < 0.9.30.3 does not define EPOLLRDHUP for Linux >= 2.6.17
|
|
|
|
*/
|
|
|
|
#ifndef EPOLLRDHUP
|
|
|
|
#define EPOLLRDHUP 0x2000
|
|
|
|
#endif
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
int uloop_init(void)
|
|
|
|
{
|
2011-02-11 01:25:17 +01:00
|
|
|
if (poll_fd >= 0)
|
|
|
|
return 0;
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
poll_fd = epoll_create(32);
|
|
|
|
if (poll_fd < 0)
|
|
|
|
return -1;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
fcntl(poll_fd, F_SETFD, fcntl(poll_fd, F_GETFD) | FD_CLOEXEC);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-10-13 21:17:51 +02:00
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
static int register_poll(struct uloop_fd *fd, unsigned int flags)
|
2010-10-13 21:17:51 +02:00
|
|
|
{
|
|
|
|
struct epoll_event ev;
|
2010-12-05 00:38:58 +01:00
|
|
|
int op = fd->registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
2010-10-13 21:17:51 +02:00
|
|
|
|
|
|
|
memset(&ev, 0, sizeof(struct epoll_event));
|
|
|
|
|
|
|
|
if (flags & ULOOP_READ)
|
|
|
|
ev.events |= EPOLLIN | EPOLLRDHUP;
|
|
|
|
|
|
|
|
if (flags & ULOOP_WRITE)
|
|
|
|
ev.events |= EPOLLOUT;
|
|
|
|
|
|
|
|
if (flags & ULOOP_EDGE_TRIGGER)
|
|
|
|
ev.events |= EPOLLET;
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
ev.data.fd = fd->fd;
|
|
|
|
ev.data.ptr = fd;
|
2013-06-18 11:11:04 +02:00
|
|
|
fd->flags = flags;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
|
|
|
return epoll_ctl(poll_fd, op, fd->fd, &ev);
|
|
|
|
}
|
|
|
|
|
2012-06-24 21:06:16 +02:00
|
|
|
static struct epoll_event events[ULOOP_MAX_EVENTS];
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
static int __uloop_fd_delete(struct uloop_fd *sock)
|
2010-12-05 00:38:58 +01:00
|
|
|
{
|
2013-06-18 11:11:04 +02:00
|
|
|
sock->flags = 0;
|
2010-12-05 00:38:58 +01:00
|
|
|
return epoll_ctl(poll_fd, EPOLL_CTL_DEL, sock->fd, 0);
|
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
static int uloop_fetch_events(int timeout)
|
2010-12-05 00:38:58 +01:00
|
|
|
{
|
2012-06-24 21:06:16 +02:00
|
|
|
int n, nfds;
|
2010-12-05 00:38:58 +01:00
|
|
|
|
|
|
|
nfds = epoll_wait(poll_fd, events, ARRAY_SIZE(events), timeout);
|
2013-06-11 12:21:09 +02:00
|
|
|
for (n = 0; n < nfds; ++n) {
|
|
|
|
struct uloop_fd_event *cur = &cur_fds[n];
|
2010-12-05 00:38:58 +01:00
|
|
|
struct uloop_fd *u = events[n].data.ptr;
|
|
|
|
unsigned int ev = 0;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
cur->fd = u;
|
2012-06-24 21:06:16 +02:00
|
|
|
if (!u)
|
|
|
|
continue;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
if (events[n].events & (EPOLLERR|EPOLLHUP)) {
|
2010-12-05 00:38:58 +01:00
|
|
|
u->error = true;
|
2014-02-11 09:37:08 +01:00
|
|
|
if (!(u->flags & ULOOP_ERROR_CB))
|
|
|
|
uloop_fd_delete(u);
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
if(!(events[n].events & (EPOLLRDHUP|EPOLLIN|EPOLLOUT|EPOLLERR|EPOLLHUP))) {
|
|
|
|
cur->fd = NULL;
|
2010-12-05 00:38:58 +01:00
|
|
|
continue;
|
2013-06-11 12:21:09 +02:00
|
|
|
}
|
2010-12-05 00:38:58 +01:00
|
|
|
|
|
|
|
if(events[n].events & EPOLLRDHUP)
|
|
|
|
u->eof = true;
|
|
|
|
|
|
|
|
if(events[n].events & EPOLLIN)
|
|
|
|
ev |= ULOOP_READ;
|
|
|
|
|
|
|
|
if(events[n].events & EPOLLOUT)
|
|
|
|
ev |= ULOOP_WRITE;
|
|
|
|
|
2013-06-11 12:21:09 +02:00
|
|
|
cur->events = ev;
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
2013-06-11 12:21:09 +02:00
|
|
|
|
|
|
|
return nfds;
|
2010-12-05 00:38:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2010-10-13 21:17:51 +02:00
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
static void uloop_handle_sigint(int signo)
|
|
|
|
{
|
2011-02-06 16:13:58 +01:00
|
|
|
uloop_cancelled = true;
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
2011-05-24 21:46:10 +02:00
|
|
|
static void uloop_sigchld(int signo)
|
|
|
|
{
|
|
|
|
do_sigchld = true;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
static struct sigaction old_sigint, old_sigchld, old_sigterm;
|
2013-10-22 10:24:15 +02:00
|
|
|
|
2014-10-13 16:14:28 +02:00
|
|
|
uloop_install_handler(SIGINT, uloop_handle_sigint, &old_sigint, add);
|
|
|
|
uloop_install_handler(SIGTERM, uloop_handle_sigint, &old_sigterm, add);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-10-13 21:17:51 +02:00
|
|
|
void uloop_run(void)
|
|
|
|
{
|
2013-10-22 10:24:15 +02:00
|
|
|
static int recursive_calls = 0;
|
2010-10-13 21:17:51 +02:00
|
|
|
struct timeval tv;
|
|
|
|
|
2013-10-22 10:24:15 +02:00
|
|
|
/*
|
|
|
|
* Handlers are only updated for the first call to uloop_run() (and restored
|
|
|
|
* when this call is done).
|
|
|
|
*/
|
|
|
|
if (!recursive_calls++)
|
|
|
|
uloop_setup_signals(true);
|
|
|
|
|
2014-05-05 02:41:34 +02:00
|
|
|
uloop_cancelled = false;
|
2011-02-06 16:13:58 +01: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);
|
2010-12-05 00:38:58 +01:00
|
|
|
uloop_run_events(uloop_get_next_timeout(&tv));
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
2013-10-22 10:24:15 +02:00
|
|
|
|
|
|
|
if (!--recursive_calls)
|
|
|
|
uloop_setup_signals(false);
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void uloop_done(void)
|
|
|
|
{
|
2011-02-11 01:25:17 +01:00
|
|
|
if (poll_fd < 0)
|
|
|
|
return;
|
|
|
|
|
2010-12-05 00:38:58 +01:00
|
|
|
close(poll_fd);
|
2011-02-11 01:25:17 +01:00
|
|
|
poll_fd = -1;
|
2012-07-08 22:27:40 +02:00
|
|
|
|
|
|
|
uloop_clear_timeouts();
|
|
|
|
uloop_clear_processes();
|
2010-10-13 21:17:51 +02:00
|
|
|
}
|