chiark / gitweb /
shell-completion: busctl
[elogind.git] / src / libsystemd-bus / sd-event.c
index 3094d9e297e9ab82e04fe35797ca111a63e87fd6..b5ddf71ebede89439e76adf1a3ba33fab830be05 100644 (file)
 #include <sys/timerfd.h>
 #include <sys/wait.h>
 
+#include "sd-id128.h"
 #include "macro.h"
 #include "prioq.h"
 #include "hashmap.h"
 #include "util.h"
 #include "time-util.h"
-#include "sd-id128.h"
+#include "missing.h"
 
 #include "sd-event.h"
 
@@ -50,7 +51,7 @@ struct sd_event_source {
 
         sd_event *event;
         void *userdata;
-        sd_prepare_handler_t prepare;
+        sd_event_handler_t prepare;
 
         EventSourceType type:4;
         int enabled:3;
@@ -64,34 +65,34 @@ struct sd_event_source {
 
         union {
                 struct {
-                        sd_io_handler_t callback;
+                        sd_event_io_handler_t callback;
                         int fd;
                         uint32_t events;
                         uint32_t revents;
                         bool registered:1;
                 } io;
                 struct {
-                        sd_time_handler_t callback;
+                        sd_event_time_handler_t callback;
                         usec_t next, accuracy;
                         unsigned earliest_index;
                         unsigned latest_index;
                 } time;
                 struct {
-                        sd_signal_handler_t callback;
+                        sd_event_signal_handler_t callback;
                         struct signalfd_siginfo siginfo;
                         int sig;
                 } signal;
                 struct {
-                        sd_child_handler_t callback;
+                        sd_event_child_handler_t callback;
                         siginfo_t siginfo;
                         pid_t pid;
                         int options;
                 } child;
                 struct {
-                        sd_defer_handler_t callback;
+                        sd_event_handler_t callback;
                 } defer;
                 struct {
-                        sd_quit_handler_t callback;
+                        sd_event_handler_t callback;
                         unsigned prioq_index;
                 } quit;
         };
@@ -133,10 +134,14 @@ struct sd_event {
         pid_t original_pid;
 
         unsigned iteration;
+        dual_timestamp timestamp;
         int state;
 
         bool quit_requested:1;
         bool need_process_child:1;
+
+        pid_t tid;
+        sd_event **default_event_ptr;
 };
 
 static int pending_prioq_compare(const void *a, const void *b) {
@@ -229,7 +234,7 @@ static int earliest_time_prioq_compare(const void *a, const void *b) {
         if (x->time.next < y->time.next)
                 return -1;
         if (x->time.next > y->time.next)
-                return -1;
+                return 1;
 
         /* Stability for the rest */
         if (x < y)
@@ -262,7 +267,7 @@ static int latest_time_prioq_compare(const void *a, const void *b) {
         if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
                 return -1;
         if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
-                return -1;
+                return 1;
 
         /* Stability for the rest */
         if (x < y)
@@ -303,6 +308,9 @@ static int quit_prioq_compare(const void *a, const void *b) {
 static void event_free(sd_event *e) {
         assert(e);
 
+        if (e->default_event_ptr)
+                *(e->default_event_ptr) = NULL;
+
         if (e->epoll_fd >= 0)
                 close_nointr_nofail(e->epoll_fd);
 
@@ -329,12 +337,11 @@ static void event_free(sd_event *e) {
         free(e);
 }
 
-int sd_event_new(sd_event** ret) {
+_public_ int sd_event_new(sd_event** ret) {
         sd_event *e;
         int r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         e = new0(sd_event, 1);
         if (!e)
@@ -367,9 +374,8 @@ fail:
         return r;
 }
 
-sd_event* sd_event_ref(sd_event *e) {
-        if (!e)
-                return NULL;
+_public_ sd_event* sd_event_ref(sd_event *e) {
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref++;
@@ -377,9 +383,8 @@ sd_event* sd_event_ref(sd_event *e) {
         return e;
 }
 
-sd_event* sd_event_unref(sd_event *e) {
-        if (!e)
-                return NULL;
+_public_ sd_event* sd_event_unref(sd_event *e) {
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref--;
@@ -416,7 +421,11 @@ static int source_io_unregister(sd_event_source *s) {
         return 0;
 }
 
-static int source_io_register(sd_event_source *s, int enabled, uint32_t events) {
+static int source_io_register(
+                sd_event_source *s,
+                int enabled,
+                uint32_t events) {
+
         struct epoll_event ev = {};
         int r;
 
@@ -491,6 +500,10 @@ static void source_free(sd_event_source *s) {
 
                         break;
 
+                case SOURCE_DEFER:
+                        /* nothing */
+                        break;
+
                 case SOURCE_QUIT:
                         prioq_remove(s->event->quit, s, &s->quit.prioq_index);
                         break;
@@ -530,6 +543,14 @@ static int source_set_pending(sd_event_source *s, bool b) {
         } else
                 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
 
+        if (s->type == SOURCE_REALTIME) {
+                prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
+                prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
+        } else if (s->type == SOURCE_MONOTONIC) {
+                prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
+                prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
+        }
+
         return 0;
 }
 
@@ -550,30 +571,24 @@ static sd_event_source *source_new(sd_event *e, EventSourceType type) {
         return s;
 }
 
-int sd_event_add_io(
+_public_ int sd_event_add_io(
                 sd_event *e,
                 int fd,
                 uint32_t events,
-                sd_io_handler_t callback,
+                sd_event_io_handler_t callback,
                 void *userdata,
                 sd_event_source **ret) {
 
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (fd < 0)
-                return -EINVAL;
-        if (events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP))
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        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);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         s = source_new(e, SOURCE_IO);
         if (!s)
@@ -625,15 +640,16 @@ static int event_setup_timer_fd(
         }
 
         /* When we sleep for longer, we try to realign the wakeup to
-           the same time wihtin each second, so that events all across
-           the system can be coalesced into a single CPU
-           wakeup. However, let's take some system-specific randomness
-           for this value, so that in a network of systems with synced
-           clocks timer events are distributed a bit. Here, we
-           calculate a perturbation usec offset from the boot ID. */
+           the same time wihtin each minute/second/250ms, so that
+           events all across the system can be coalesced into a single
+           CPU wakeup. However, let's take some system-specific
+           randomness for this value, so that in a network of systems
+           with synced clocks timer events are distributed a
+           bit. Here, we calculate a perturbation usec offset from the
+           boot ID. */
 
         if (sd_id128_get_boot(&bootid) >= 0)
-                e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_SEC;
+                e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
 
         *timer_fd = fd;
         return 0;
@@ -648,26 +664,20 @@ static int event_add_time_internal(
                 Prioq **latest,
                 uint64_t usec,
                 uint64_t accuracy,
-                sd_time_handler_t callback,
+                sd_event_time_handler_t callback,
                 void *userdata,
                 sd_event_source **ret) {
 
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (usec == (uint64_t) -1)
-                return -EINVAL;
-        if (accuracy == (uint64_t) -1)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(accuracy != (uint64_t) -1, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         assert(timer_fd);
         assert(earliest);
@@ -718,11 +728,23 @@ fail:
         return r;
 }
 
-int sd_event_add_monotonic(sd_event *e, uint64_t usec, uint64_t accuracy, sd_time_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_monotonic(sd_event *e,
+                                    uint64_t usec,
+                                    uint64_t accuracy,
+                                    sd_event_time_handler_t callback,
+                                    void *userdata,
+                                    sd_event_source **ret) {
+
         return event_add_time_internal(e, SOURCE_MONOTONIC, &e->monotonic_fd, CLOCK_MONOTONIC, &e->monotonic_earliest, &e->monotonic_latest, usec, accuracy, callback, userdata, ret);
 }
 
-int sd_event_add_realtime(sd_event *e, uint64_t usec, uint64_t accuracy, sd_time_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_realtime(sd_event *e,
+                                   uint64_t usec,
+                                   uint64_t accuracy,
+                                   sd_event_time_handler_t callback,
+                                   void *userdata,
+                                   sd_event_source **ret) {
+
         return event_add_time_internal(e, SOURCE_REALTIME, &e->realtime_fd, CLOCK_REALTIME, &e->realtime_earliest, &e->monotonic_latest, usec, accuracy, callback, userdata, ret);
 }
 
@@ -758,23 +780,23 @@ static int event_update_signal_fd(sd_event *e) {
         return 0;
 }
 
-int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_signal(
+                sd_event *e,
+                int sig,
+                sd_event_signal_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (sig <= 0)
-                return -EINVAL;
-        if (sig >= _NSIG)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        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);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         if (!e->signal_sources) {
                 e->signal_sources = new0(sd_event_source*, _NSIG);
@@ -807,23 +829,25 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
         return 0;
 }
 
-int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_child(
+                sd_event *e,
+                pid_t pid,
+                int options,
+                sd_event_child_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (pid <= 1)
-                return -EINVAL;
-        if (options & ~(WEXITED|WSTOPPED|WCONTINUED))
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(pid > 1, -EINVAL);
+        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);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func);
         if (r < 0)
@@ -866,17 +890,20 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
         return 0;
 }
 
-int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_defer(
+                sd_event *e,
+                sd_event_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         s = source_new(e, SOURCE_DEFER);
         if (!s)
@@ -896,12 +923,18 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
         return 0;
 }
 
-int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_quit(
+                sd_event *e,
+                sd_event_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
         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);
 
@@ -930,7 +963,7 @@ int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, s
         return 0;
 }
 
-sd_event_source* sd_event_source_ref(sd_event_source *s) {
+_public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
         assert_return(s, NULL);
 
         assert(s->n_ref >= 1);
@@ -939,7 +972,7 @@ sd_event_source* sd_event_source_ref(sd_event_source *s) {
         return s;
 }
 
-sd_event_source* sd_event_source_unref(sd_event_source *s) {
+_public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
         assert_return(s, NULL);
 
         assert(s->n_ref >= 1);
@@ -951,118 +984,93 @@ sd_event_source* sd_event_source_unref(sd_event_source *s) {
         return NULL;
 }
 
-sd_event *sd_event_get(sd_event_source *s) {
-        if (!s)
-                return NULL;
+_public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
+        assert_return(s, NULL);
 
         return s->event;
 }
 
-int sd_event_source_get_pending(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
+_public_ int sd_event_source_get_pending(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type != SOURCE_QUIT, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->pending;
 }
 
-int sd_event_source_get_io_fd(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_fd(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->io.fd;
 }
 
-int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (!events)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
+        assert_return(s, -EINVAL);
+        assert_return(events, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *events = s->io.events;
         return 0;
 }
 
-int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
+_public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
         int r;
 
-        if (!s)
-                return -EINVAL;
-        if (!s->type != SOURCE_IO)
-                return -EDOM;
-        if (events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP))
-                return -EINVAL;
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->io.events == events)
                 return 0;
 
         if (s->enabled != SD_EVENT_OFF) {
-                r = source_io_register(s, s->io.events, events);
+                r = source_io_register(s, s->enabled, events);
                 if (r < 0)
                         return r;
         }
 
         s->io.events = events;
+        source_set_pending(s, false);
 
         return 0;
 }
 
-int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (!revents)
-                return -EINVAL;
-        if (!s->pending)
-                return -ENODATA;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
+        assert_return(s, -EINVAL);
+        assert_return(revents, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(s->pending, -ENODATA);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *revents = s->io.revents;
         return 0;
 }
 
-int sd_event_source_get_signal(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_SIGNAL)
-                return -EDOM;
-        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_signal(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_SIGNAL, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->signal.sig;
 }
 
-int sd_event_source_get_priority(sd_event_source *s, int *priority) {
-        if (!s)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_priority(sd_event_source *s, int *priority) {
+        assert_return(s, -EINVAL);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->priority;
 }
 
-int sd_event_source_set_priority(sd_event_source *s, int priority) {
-        if (!s)
-                return -EINVAL;
+_public_ int sd_event_source_set_priority(sd_event_source *s, int priority) {
+        assert_return(s, -EINVAL);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->priority == priority)
                 return 0;
@@ -1075,31 +1083,28 @@ int sd_event_source_set_priority(sd_event_source *s, int priority) {
         if (s->prepare)
                 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
 
+        if (s->type == SOURCE_QUIT)
+                prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+
         return 0;
 }
 
-int sd_event_source_get_enabled(sd_event_source *s, int *m) {
-        if (!s)
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
+        assert_return(s, -EINVAL);
+        assert_return(m, -EINVAL);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *m = s->enabled;
         return 0;
 }
 
-int sd_event_source_set_enabled(sd_event_source *s, int m) {
+_public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
         int r;
 
-        if (!s)
-                return -EINVAL;
-        if (m != SD_EVENT_OFF && m != SD_EVENT_ON && !SD_EVENT_ONESHOT)
-                return -EINVAL;
+        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);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->enabled == m)
                 return 0;
@@ -1150,8 +1155,12 @@ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 
                         break;
 
-                case SOURCE_DEFER:
                 case SOURCE_QUIT:
+                        s->enabled = m;
+                        prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+                        break;
+
+                case SOURCE_DEFER:
                         s->enabled = m;
                         break;
                 }
@@ -1201,8 +1210,12 @@ int sd_event_source_set_enabled(sd_event_source *s, int m) {
                         }
                         break;
 
-                case SOURCE_DEFER:
                 case SOURCE_QUIT:
+                        s->enabled = m;
+                        prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+                        break;
+
+                case SOURCE_DEFER:
                         s->enabled = m;
                         break;
                 }
@@ -1217,36 +1230,27 @@ int sd_event_source_set_enabled(sd_event_source *s, int m) {
         return 0;
 }
 
-int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
-        if (!s)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (s->type != SOURCE_REALTIME && s->type != SOURCE_MONOTONIC)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *usec = s->time.next;
         return 0;
 }
 
-int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
-        if (!s)
-                return -EINVAL;
-        if (usec == (uint64_t) -1)
-                return -EINVAL;
-        if (s->type != SOURCE_REALTIME && s->type != SOURCE_MONOTONIC)
-                return -EDOM;
+_public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
-
-        if (s->time.next == usec)
-                return 0;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         s->time.next = usec;
 
+        source_set_pending(s, false);
+
         if (s->type == SOURCE_REALTIME) {
                 prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
                 prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
@@ -1258,23 +1262,30 @@ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
         return 0;
 }
 
-int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_MONOTONIC && s->type != SOURCE_REALTIME)
-                return -EDOM;
+_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
+
+        *usec = s->time.accuracy;
+        return 0;
+}
+
+_public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (usec == 0)
                 usec = DEFAULT_ACCURACY_USEC;
 
-        if (s->time.accuracy == usec)
-                return 0;
-
         s->time.accuracy = usec;
 
+        source_set_pending(s, false);
+
         if (s->type == SOURCE_REALTIME)
                 prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
         else
@@ -1283,21 +1294,17 @@ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
         return 0;
 }
 
-int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
-        if (!s)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (s->type != SOURCE_MONOTONIC && s->type != SOURCE_REALTIME)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
+        assert_return(s, -EINVAL);
+        assert_return(pid, -EINVAL);
+        assert_return(s->type == SOURCE_CHILD, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
-        *usec = s->time.accuracy;
+        *pid = s->child.pid;
         return 0;
 }
 
-int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback) {
+_public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
         int r;
 
         assert_return(s, -EINVAL);
@@ -1329,7 +1336,7 @@ int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callbac
         return 0;
 }
 
-void* sd_event_source_get_userdata(sd_event_source *s) {
+_public_ void* sd_event_source_get_userdata(sd_event_source *s) {
         assert_return(s, NULL);
 
         return s->userdata;
@@ -1357,13 +1364,24 @@ static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
              dispatch as much as possible on the entire system.
 
           We implement this by waking up everywhere at the same time
-          within any given second if we can, synchronised via the
+          within any given minute if we can, synchronised via the
           perturbation value determined from the boot ID. If we can't,
-          then we try to find the same spot in every a 250ms
+          then we try to find the same spot in every 1s and then 250ms
           step. Otherwise, we pick the last possible time to wake up.
         */
 
-        c = (b / USEC_PER_SEC) * USEC_PER_SEC + e->perturb;
+        c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
+        if (c >= b) {
+                if (_unlikely_(c < USEC_PER_MINUTE))
+                        return b;
+
+                c -= USEC_PER_MINUTE;
+        }
+
+        if (c >= a)
+                return c;
+
+        c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
         if (c >= b) {
                 if (_unlikely_(c < USEC_PER_SEC))
                         return b;
@@ -1404,8 +1422,23 @@ static int event_arm_timer(
         assert_se(next);
 
         a = prioq_peek(earliest);
-        if (!a || a->enabled == SD_EVENT_OFF)
+        if (!a || a->enabled == SD_EVENT_OFF) {
+
+                if (timer_fd < 0)
+                        return 0;
+
+                if (*next == (usec_t) -1)
+                        return 0;
+
+                /* disarm */
+                r = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
+                if (r < 0)
+                        return r;
+
+                *next = (usec_t) -1;
+
                 return 0;
+        }
 
         b = prioq_peek(latest);
         assert_se(b && b->enabled != SD_EVENT_OFF);
@@ -1438,27 +1471,18 @@ static int process_io(sd_event *e, sd_event_source *s, uint32_t events) {
 
         s->io.revents = events;
 
-        /*
-           If this is a oneshot event source, then we added it to the
-           epoll with EPOLLONESHOT, hence we know it's not registered
-           anymore. We can save a syscall here...
-        */
-
-        if (s->enabled == SD_EVENT_ONESHOT)
-                s->io.registered = false;
-
         return source_set_pending(s, true);
 }
 
-static int flush_timer(sd_event *e, int fd, uint32_t events) {
+static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
         uint64_t x;
         ssize_t ss;
 
         assert(e);
         assert(fd >= 0);
+        assert(next);
 
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         ss = read(fd, &x, sizeof(x));
         if (ss < 0) {
@@ -1471,10 +1495,17 @@ static int flush_timer(sd_event *e, int fd, uint32_t events) {
         if (ss != sizeof(x))
                 return -EIO;
 
+        *next = (usec_t) -1;
+
         return 0;
 }
 
-static int process_timer(sd_event *e, usec_t n, Prioq *earliest, Prioq *latest) {
+static int process_timer(
+                sd_event *e,
+                usec_t n,
+                Prioq *earliest,
+                Prioq *latest) {
+
         sd_event_source *s;
         int r;
 
@@ -1547,17 +1578,17 @@ static int process_child(sd_event *e) {
 }
 
 static int process_signal(sd_event *e, uint32_t events) {
-        struct signalfd_siginfo si;
         bool read_one = false;
-        ssize_t ss;
         int r;
 
         assert(e);
+        assert(e->signal_sources);
 
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         for (;;) {
+                struct signalfd_siginfo si;
+                ssize_t ss;
                 sd_event_source *s;
 
                 ss = read(e->signal_fd, &si, sizeof(si));
@@ -1573,17 +1604,16 @@ static int process_signal(sd_event *e, uint32_t events) {
 
                 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 || !e->signal_sources[si.ssi_signo])
+                        if (r > 0 || !s)
                                 continue;
-                } else {
-                        s = e->signal_sources[si.ssi_signo];
+                } else
                         if (!s)
                                 return -EIO;
-                }
 
                 s->signal.siginfo = si;
                 r = source_set_pending(s, true);
@@ -1596,7 +1626,7 @@ static int process_signal(sd_event *e, uint32_t events) {
 }
 
 static int source_dispatch(sd_event_source *s) {
-        int r;
+        int r = 0;
 
         assert(s);
         assert(s->pending || s->type == SOURCE_QUIT);
@@ -1613,6 +1643,8 @@ static int source_dispatch(sd_event_source *s) {
                         return r;
         }
 
+        sd_event_source_ref(s);
+
         switch (s->type) {
 
         case SOURCE_IO:
@@ -1644,6 +1676,8 @@ static int source_dispatch(sd_event_source *s) {
                 break;
         }
 
+        sd_event_source_unref(s);
+
         return r;
 }
 
@@ -1713,11 +1747,10 @@ static sd_event_source* event_next_pending(sd_event *e) {
         return p;
 }
 
-int sd_event_run(sd_event *e, uint64_t timeout) {
+_public_ int sd_event_run(sd_event *e, uint64_t timeout) {
         struct epoll_event ev_queue[EPOLL_QUEUE_MAX];
         sd_event_source *p;
         int r, i, m;
-        dual_timestamp n;
 
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
@@ -1735,34 +1768,32 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
         if (r < 0)
                 goto finish;
 
-        if (event_next_pending(e) || e->need_process_child)
-                timeout = 0;
+        r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next);
+        if (r < 0)
+                goto finish;
 
-        if (timeout > 0) {
-                r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next);
-                if (r < 0)
-                        goto finish;
+        r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next);
+        if (r < 0)
+                goto finish;
 
-                r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next);
-                if (r < 0)
-                        goto finish;
-        }
+        if (event_next_pending(e) || e->need_process_child)
+                timeout = 0;
 
         m = epoll_wait(e->epoll_fd, ev_queue, EPOLL_QUEUE_MAX,
                        timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
         if (m < 0) {
-                r = m;
+                r = errno == EAGAIN || errno == EINTR ? 0 : -errno;
                 goto finish;
         }
 
-        dual_timestamp_get(&n);
+        dual_timestamp_get(&e->timestamp);
 
         for (i = 0; i < m; i++) {
 
                 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_MONOTONIC))
-                        r = flush_timer(e, e->monotonic_fd, ev_queue[i].events);
+                        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_REALTIME))
-                        r = flush_timer(e, e->realtime_fd, ev_queue[i].events);
+                        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_SIGNAL))
                         r = process_signal(e, ev_queue[i].events);
                 else
@@ -1772,11 +1803,11 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
                         goto finish;
         }
 
-        r = process_timer(e, n.monotonic, e->monotonic_earliest, e->monotonic_latest);
+        r = process_timer(e, e->timestamp.monotonic, e->monotonic_earliest, e->monotonic_latest);
         if (r < 0)
                 goto finish;
 
-        r = process_timer(e, n.realtime, e->realtime_earliest, e->realtime_latest);
+        r = process_timer(e, e->timestamp.realtime, e->realtime_earliest, e->realtime_latest);
         if (r < 0)
                 goto finish;
 
@@ -1801,7 +1832,7 @@ finish:
         return r;
 }
 
-int sd_event_loop(sd_event *e) {
+_public_ int sd_event_loop(sd_event *e) {
         int r;
 
         assert_return(e, -EINVAL);
@@ -1823,21 +1854,21 @@ finish:
         return r;
 }
 
-int sd_event_get_state(sd_event *e) {
+_public_ int sd_event_get_state(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
 
         return e->state;
 }
 
-int sd_event_get_quit(sd_event *e) {
+_public_ int sd_event_get_quit(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
 
         return e->quit_requested;
 }
 
-int sd_event_request_quit(sd_event *e) {
+_public_ int sd_event_request_quit(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         assert_return(!event_pid_changed(e), -ECHILD);
@@ -1845,3 +1876,62 @@ int sd_event_request_quit(sd_event *e) {
         e->quit_requested = true;
         return 0;
 }
+
+_public_ int sd_event_get_now_realtime(sd_event *e, uint64_t *usec) {
+        assert_return(e, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        *usec = e->timestamp.realtime;
+        return 0;
+}
+
+_public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) {
+        assert_return(e, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        *usec = e->timestamp.monotonic;
+        return 0;
+}
+
+_public_ int sd_event_default(sd_event **ret) {
+
+        static __thread sd_event *default_event = NULL;
+        sd_event *e;
+        int r;
+
+        if (!ret)
+                return !!default_event;
+
+        if (default_event) {
+                *ret = sd_event_ref(default_event);
+                return 0;
+        }
+
+        r = sd_event_new(&e);
+        if (r < 0)
+                return r;
+
+        e->default_event_ptr = &default_event;
+        e->tid = gettid();
+        default_event = e;
+
+        *ret = e;
+        return 1;
+}
+
+_public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
+        assert_return(e, -EINVAL);
+        assert_return(tid, -EINVAL);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        if (e->tid != 0) {
+                *tid = e->tid;
+                return 0;
+        }
+
+        return -ENXIO;
+}