#include "time-util.h"
#include "missing.h"
#include "set.h"
+#include "list.h"
#include "sd-event.h"
typedef enum EventSourceType {
SOURCE_IO,
SOURCE_TIME_REALTIME,
+ SOURCE_TIME_BOOTTIME,
SOURCE_TIME_MONOTONIC,
SOURCE_TIME_REALTIME_ALARM,
SOURCE_TIME_BOOTTIME_ALARM,
SOURCE_POST,
SOURCE_EXIT,
SOURCE_WATCHDOG,
- _SOUFCE_EVENT_SOURCE_TYPE_MAX,
+ _SOURCE_EVENT_SOURCE_TYPE_MAX,
_SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
} EventSourceType;
-#define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
+#define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
struct sd_event_source {
unsigned n_ref;
int enabled:3;
bool pending:1;
bool dispatching:1;
+ bool floating:1;
int64_t priority;
unsigned pending_index;
unsigned pending_iteration;
unsigned prepare_iteration;
+ LIST_FIELDS(sd_event_source, sources);
+
union {
struct {
sd_event_io_handler_t callback;
Prioq *earliest;
Prioq *latest;
usec_t next;
+
+ bool needs_rearm:1;
};
struct sd_event {
Prioq *pending;
Prioq *prepare;
- /* timerfd_create() only supports these four clocks so far. We
+ /* timerfd_create() only supports these five clocks so far. We
* can add support for more clocks when the kernel learns to
* deal with them, too. */
struct clock_data realtime;
+ struct clock_data boottime;
struct clock_data monotonic;
struct clock_data realtime_alarm;
struct clock_data boottime_alarm;
usec_t watchdog_last, watchdog_period;
unsigned n_sources;
+
+ LIST_HEAD(sd_event_source, sources);
};
+static void source_disconnect(sd_event_source *s);
+
static int pending_prioq_compare(const void *a, const void *b) {
const sd_event_source *x = a, *y = b;
}
static void event_free(sd_event *e) {
+ sd_event_source *s;
+
assert(e);
+
+ while ((s = e->sources)) {
+ assert(s->floating);
+ source_disconnect(s);
+ sd_event_source_unref(s);
+ }
+
assert(e->n_sources == 0);
if (e->default_event_ptr)
safe_close(e->watchdog_fd);
free_clock_data(&e->realtime);
+ free_clock_data(&e->boottime);
free_clock_data(&e->monotonic);
free_clock_data(&e->realtime_alarm);
free_clock_data(&e->boottime_alarm);
return -ENOMEM;
e->n_ref = 1;
- e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
- e->realtime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = (usec_t) -1;
+ e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
+ e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
e->original_pid = getpid();
- e->perturb = (usec_t) -1;
+ e->perturb = USEC_INFINITY;
assert_se(sigemptyset(&e->sigset) == 0);
case SOURCE_TIME_REALTIME:
return CLOCK_REALTIME;
+ case SOURCE_TIME_BOOTTIME:
+ return CLOCK_BOOTTIME;
+
case SOURCE_TIME_MONOTONIC:
return CLOCK_MONOTONIC;
case CLOCK_REALTIME:
return SOURCE_TIME_REALTIME;
+ case CLOCK_BOOTTIME:
+ return SOURCE_TIME_BOOTTIME;
+
case CLOCK_MONOTONIC:
return SOURCE_TIME_MONOTONIC;
case SOURCE_TIME_REALTIME:
return &e->realtime;
+ case SOURCE_TIME_BOOTTIME:
+ return &e->boottime;
+
case SOURCE_TIME_MONOTONIC:
return &e->monotonic;
}
}
-static void source_free(sd_event_source *s) {
+static void source_disconnect(sd_event_source *s) {
+ sd_event *event;
+
assert(s);
- if (s->event) {
- assert(s->event->n_sources > 0);
+ if (!s->event)
+ return;
- switch (s->type) {
+ assert(s->event->n_sources > 0);
- case SOURCE_IO:
- if (s->io.fd >= 0)
- source_io_unregister(s);
+ switch (s->type) {
- break;
+ case SOURCE_IO:
+ if (s->io.fd >= 0)
+ source_io_unregister(s);
- case SOURCE_TIME_REALTIME:
- case SOURCE_TIME_MONOTONIC:
- case SOURCE_TIME_REALTIME_ALARM:
- case SOURCE_TIME_BOOTTIME_ALARM: {
- struct clock_data *d;
+ break;
- d = event_get_clock_data(s->event, s->type);
- assert(d);
+ case SOURCE_TIME_REALTIME:
+ case SOURCE_TIME_BOOTTIME:
+ case SOURCE_TIME_MONOTONIC:
+ case SOURCE_TIME_REALTIME_ALARM:
+ case SOURCE_TIME_BOOTTIME_ALARM: {
+ struct clock_data *d;
- prioq_remove(d->earliest, s, &s->time.earliest_index);
- prioq_remove(d->latest, s, &s->time.latest_index);
- break;
+ d = event_get_clock_data(s->event, s->type);
+ assert(d);
+
+ prioq_remove(d->earliest, s, &s->time.earliest_index);
+ prioq_remove(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
+ break;
+ }
+
+ case SOURCE_SIGNAL:
+ if (s->signal.sig > 0) {
+ if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
+ assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
+
+ if (s->event->signal_sources)
+ s->event->signal_sources[s->signal.sig] = NULL;
}
- case SOURCE_SIGNAL:
- if (s->signal.sig > 0) {
- if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
- assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
+ break;
- if (s->event->signal_sources)
- s->event->signal_sources[s->signal.sig] = NULL;
+ case SOURCE_CHILD:
+ if (s->child.pid > 0) {
+ if (s->enabled != SD_EVENT_OFF) {
+ assert(s->event->n_enabled_child_sources > 0);
+ s->event->n_enabled_child_sources--;
}
- break;
+ if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
+ assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
- case SOURCE_CHILD:
- if (s->child.pid > 0) {
- if (s->enabled != SD_EVENT_OFF) {
- assert(s->event->n_enabled_child_sources > 0);
- s->event->n_enabled_child_sources--;
- }
+ hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
+ }
- if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
- assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
+ break;
- hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
- }
+ case SOURCE_DEFER:
+ /* nothing */
+ break;
- break;
+ case SOURCE_POST:
+ set_remove(s->event->post_sources, s);
+ break;
- case SOURCE_DEFER:
- /* nothing */
- break;
+ case SOURCE_EXIT:
+ prioq_remove(s->event->exit, s, &s->exit.prioq_index);
+ break;
- case SOURCE_POST:
- set_remove(s->event->post_sources, s);
- break;
+ default:
+ assert_not_reached("Wut? I shouldn't exist.");
+ }
- case SOURCE_EXIT:
- prioq_remove(s->event->exit, s, &s->exit.prioq_index);
- break;
+ if (s->pending)
+ prioq_remove(s->event->pending, s, &s->pending_index);
- default:
- assert_not_reached("Wut? I shouldn't exist.");
- }
+ if (s->prepare)
+ prioq_remove(s->event->prepare, s, &s->prepare_index);
- if (s->pending)
- prioq_remove(s->event->pending, s, &s->pending_index);
+ event = s->event;
- if (s->prepare)
- prioq_remove(s->event->prepare, s, &s->prepare_index);
+ s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
+ s->event = NULL;
+ LIST_REMOVE(sources, event->sources, s);
+ event->n_sources--;
- s->event->n_sources--;
- sd_event_unref(s->event);
- }
+ if (!s->floating)
+ sd_event_unref(event);
+}
+
+static void source_free(sd_event_source *s) {
+ assert(s);
+ source_disconnect(s);
free(s);
}
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
}
return 0;
}
-static sd_event_source *source_new(sd_event *e, EventSourceType type) {
+static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
sd_event_source *s;
assert(e);
return NULL;
s->n_ref = 1;
- s->event = sd_event_ref(e);
+ s->event = e;
+ s->floating = floating;
s->type = type;
s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
+ if (!floating)
+ sd_event_ref(e);
+
+ LIST_PREPEND(sources, e->sources, s);
e->n_sources ++;
return s;
assert_return(fd >= 0, -EINVAL);
assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
- s = source_new(e, SOURCE_IO);
+ s = source_new(e, !ret, SOURCE_IO);
if (!s)
return -ENOMEM;
r = source_io_register(s, s->enabled, events);
if (r < 0) {
source_free(s);
- return -errno;
+ return r;
}
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
bit. Here, we calculate a perturbation usec offset from the
boot ID. */
- if (_likely_(e->perturb != (usec_t) -1))
+ if (_likely_(e->perturb != USEC_INFINITY))
return;
if (sd_id128_get_boot(&bootid) >= 0)
int r;
assert_return(e, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(usec != (uint64_t) -1, -EINVAL);
assert_return(accuracy != (uint64_t) -1, -EINVAL);
assert_return(callback, -EINVAL);
return r;
}
- s = source_new(e, type);
+ s = source_new(e, !ret, type);
if (!s)
return -ENOMEM;
s->userdata = userdata;
s->enabled = SD_EVENT_ONESHOT;
+ d->needs_rearm = true;
+
r = prioq_put(d->earliest, s, &s->time.earliest_index);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
fail:
return 0;
}
+static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
+ assert(s);
+
+ return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
+}
+
_public_ int sd_event_add_signal(
sd_event *e,
sd_event_source **ret,
assert_return(e, -EINVAL);
assert_return(sig > 0, -EINVAL);
assert_return(sig < _NSIG, -EINVAL);
- assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
+ if (!callback)
+ callback = signal_exit_callback;
+
r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
if (r < 0)
return -errno;
} else if (e->signal_sources[sig])
return -EBUSY;
- s = source_new(e, SOURCE_SIGNAL);
+ s = source_new(e, !ret, SOURCE_SIGNAL);
if (!s)
return -ENOMEM;
}
}
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
assert_return(options != 0, -EINVAL);
assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
return -EBUSY;
- s = source_new(e, SOURCE_CHILD);
+ s = source_new(e, !ret, SOURCE_CHILD);
if (!s)
return -ENOMEM;
r = event_update_signal_fd(e);
if (r < 0) {
source_free(s);
- return -errno;
+ return r;
}
}
e->need_process_child = true;
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
assert_return(e, -EINVAL);
assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
- s = source_new(e, SOURCE_DEFER);
+ s = source_new(e, !ret, SOURCE_DEFER);
if (!s)
return -ENOMEM;
return r;
}
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
assert_return(e, -EINVAL);
assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
if (r < 0)
return r;
- s = source_new(e, SOURCE_POST);
+ s = source_new(e, !ret, SOURCE_POST);
if (!s)
return -ENOMEM;
return r;
}
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
assert_return(e, -EINVAL);
assert_return(callback, -EINVAL);
- assert_return(ret, -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
return -ENOMEM;
}
- s = source_new(e, SOURCE_EXIT);
+ s = source_new(e, !ret, SOURCE_EXIT);
if (!s)
return -ENOMEM;
return r;
}
- *ret = s;
+ if (ret)
+ *ret = s;
+
return 0;
}
if (s->dispatching) {
if (s->type == SOURCE_IO)
source_io_unregister(s);
+
+ source_disconnect(s);
} else
source_free(s);
}
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD);
- if (s->io.events == events)
+ /* edge-triggered updates are never skipped, so we can reset edges */
+ if (s->io.events == events && !(events & EPOLLET))
return 0;
if (s->enabled != SD_EVENT_OFF) {
assert_return(s, -EINVAL);
assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
- assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(s->event), -ECHILD);
+ /* If we are dead anyway, we are fine with turning off
+ * sources, but everything else needs to fail. */
+ if (s->event->state == SD_EVENT_FINISHED)
+ return m == SD_EVENT_OFF ? 0 : -ESTALE;
+
if (s->enabled == m)
return 0;
break;
case SOURCE_TIME_REALTIME:
+ case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM: {
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
break;
}
break;
case SOURCE_TIME_REALTIME:
+ case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM: {
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
break;
}
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
return 0;
}
assert(d);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
return 0;
}
assert(e);
assert(d);
+ if (!d->needs_rearm)
+ return 0;
+ else
+ d->needs_rearm = false;
+
a = prioq_peek(d->earliest);
if (!a || a->enabled == SD_EVENT_OFF) {
if (d->fd < 0)
return 0;
- if (d->next == (usec_t) -1)
+ if (d->next == USEC_INFINITY)
return 0;
/* disarm */
if (r < 0)
return r;
- d->next = (usec_t) -1;
+ d->next = USEC_INFINITY;
return 0;
}
return -EIO;
if (next)
- *next = (usec_t) -1;
+ *next = USEC_INFINITY;
return 0;
}
prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
prioq_reshuffle(d->latest, s, &s->time.latest_index);
+ d->needs_rearm = true;
}
return 0;
int r;
assert(e);
- assert(e->signal_sources);
assert_return(events == EPOLLIN, -EIO);
for (;;) {
struct signalfd_siginfo si;
ssize_t ss;
- sd_event_source *s;
+ sd_event_source *s = NULL;
ss = read(e->signal_fd, &si, sizeof(si));
if (ss < 0) {
read_one = true;
- s = e->signal_sources[si.ssi_signo];
if (si.ssi_signo == SIGCHLD) {
r = process_child(e);
if (r < 0)
return r;
- if (r > 0 || !s)
+ if (r > 0)
continue;
- } else
- if (!s)
- return -EIO;
+ }
+
+ if (e->signal_sources)
+ s = e->signal_sources[si.ssi_signo];
+
+ if (!s)
+ continue;
s->signal.siginfo = si;
r = source_set_pending(s, true);
break;
case SOURCE_TIME_REALTIME:
+ case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM:
break;
case SOURCE_WATCHDOG:
+ case _SOURCE_EVENT_SOURCE_TYPE_MAX:
+ case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
assert_not_reached("Wut? I shouldn't exist.");
}
timespec_store(&its.it_value, t);
+ /* Make sure we never set the watchdog to 0, which tells the
+ * kernel to disable it. */
+ if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
+ its.it_value.tv_nsec = 1;
+
r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
if (r < 0)
return -errno;
return arm_watchdog(e);
}
-_public_ int sd_event_run(sd_event *e, uint64_t timeout) {
- struct epoll_event *ev_queue;
- unsigned ev_queue_max;
- sd_event_source *p;
- int r, i, m;
+_public_ int sd_event_prepare(sd_event *e) {
+ int r;
assert_return(e, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD);
assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
if (e->exit_requested)
- return dispatch_exit(e);
+ goto pending;
- sd_event_ref(e);
e->iteration++;
- e->state = SD_EVENT_RUNNING;
r = event_prepare(e);
if (r < 0)
- goto finish;
+ return r;
r = event_arm_timer(e, &e->realtime);
if (r < 0)
- goto finish;
+ return r;
+
+ r = event_arm_timer(e, &e->boottime);
+ if (r < 0)
+ return r;
r = event_arm_timer(e, &e->monotonic);
if (r < 0)
- goto finish;
+ return r;
r = event_arm_timer(e, &e->realtime_alarm);
if (r < 0)
- goto finish;
+ return r;
r = event_arm_timer(e, &e->boottime_alarm);
if (r < 0)
- goto finish;
+ return r;
if (event_next_pending(e) || e->need_process_child)
- timeout = 0;
+ goto pending;
+
+ e->state = SD_EVENT_PREPARED;
+
+ return 0;
+
+pending:
+ e->state = SD_EVENT_PREPARED;
+ r = sd_event_wait(e, 0);
+ if (r == 0)
+ e->state = SD_EVENT_PREPARED;
+
+ return r;
+}
+
+_public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
+ struct epoll_event *ev_queue;
+ unsigned ev_queue_max;
+ int r, m, i;
+
+ assert_return(e, -EINVAL);
+ assert_return(!event_pid_changed(e), -ECHILD);
+ assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
+ assert_return(e->state == SD_EVENT_PREPARED, -EBUSY);
+
+ if (e->exit_requested) {
+ e->state = SD_EVENT_PENDING;
+ return 1;
+ }
ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
ev_queue = newa(struct epoll_event, ev_queue_max);
m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
if (m < 0) {
- r = errno == EAGAIN || errno == EINTR ? 1 : -errno;
+ if (errno == EINTR) {
+ e->state = SD_EVENT_PENDING;
+ return 1;
+ }
+
+ r = -errno;
+
goto finish;
}
if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
+ else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
+ r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
if (r < 0)
goto finish;
+ r = process_timer(e, e->timestamp_boottime, &e->boottime);
+ if (r < 0)
+ goto finish;
+
r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
if (r < 0)
goto finish;
goto finish;
}
- p = event_next_pending(e);
- if (!p) {
- r = 1;
- goto finish;
+ if (event_next_pending(e)) {
+ e->state = SD_EVENT_PENDING;
+
+ return 1;
}
- r = source_dispatch(p);
+ r = 0;
finish:
e->state = SD_EVENT_PASSIVE;
- sd_event_unref(e);
return r;
}
+_public_ int sd_event_dispatch(sd_event *e) {
+ sd_event_source *p;
+ int r;
+
+ assert_return(e, -EINVAL);
+ assert_return(!event_pid_changed(e), -ECHILD);
+ assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
+ assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
+
+ if (e->exit_requested)
+ return dispatch_exit(e);
+
+ p = event_next_pending(e);
+ if (p) {
+ sd_event_ref(e);
+
+ e->state = SD_EVENT_RUNNING;
+ r = source_dispatch(p);
+ e->state = SD_EVENT_PASSIVE;
+
+ sd_event_unref(e);
+
+ return r;
+ }
+
+ e->state = SD_EVENT_PASSIVE;
+
+ return 1;
+}
+
+_public_ int sd_event_run(sd_event *e, uint64_t timeout) {
+ int r;
+
+ assert_return(e, -EINVAL);
+ assert_return(!event_pid_changed(e), -ECHILD);
+ assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
+ assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
+
+ r = sd_event_prepare(e);
+ if (r > 0)
+ return sd_event_dispatch(e);
+ else if (r < 0)
+ return r;
+
+ r = sd_event_wait(e, timeout);
+ if (r > 0)
+ return sd_event_dispatch(e);
+ else
+ return r;
+}
+
_public_ int sd_event_loop(sd_event *e) {
int r;
return r;
}
+_public_ int sd_event_get_fd(sd_event *e) {
+
+ assert_return(e, -EINVAL);
+ assert_return(!event_pid_changed(e), -ECHILD);
+
+ return e->epoll_fd;
+}
+
_public_ int sd_event_get_state(sd_event *e) {
assert_return(e, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD);
*usec = e->timestamp.monotonic;
break;
+ case CLOCK_BOOTTIME:
case CLOCK_BOOTTIME_ALARM:
*usec = e->timestamp_boottime;
break;