[systemd-commits] 5 commits - src/libsystemd-bus src/shared src/systemd

Lennart Poettering lennart at kemper.freedesktop.org
Thu Oct 10 17:11:57 PDT 2013


 src/libsystemd-bus/sd-event.c   |  719 +++++++++++++++++++++++-----------------
 src/libsystemd-bus/test-event.c |   28 +
 src/shared/macro.h              |    6 
 src/systemd/sd-event.h          |   39 +-
 4 files changed, 472 insertions(+), 320 deletions(-)

New commits:
commit 305f78bff3904add317847c4a27483b993dec99b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 11 02:11:30 2013 +0200

    event: move all library calls over to new assert_return() macro

diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index 482e3bb..e9f4290 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -333,8 +333,7 @@ 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)
@@ -368,8 +367,7 @@ fail:
 }
 
 sd_event* sd_event_ref(sd_event *e) {
-        if (!e)
-                return NULL;
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref++;
@@ -378,8 +376,7 @@ sd_event* sd_event_ref(sd_event *e) {
 }
 
 sd_event* sd_event_unref(sd_event *e) {
-        if (!e)
-                return NULL;
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref--;
@@ -416,7 +413,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;
 
@@ -561,19 +562,13 @@ int sd_event_add_io(
         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)), -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)
@@ -655,19 +650,13 @@ static int event_add_time_internal(
         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);
@@ -758,23 +747,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) {
+int sd_event_add_signal(
+                sd_event *e,
+                int sig,
+                sd_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 +796,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) {
+int sd_event_add_child(
+                sd_event *e,
+                pid_t pid,
+                int options,
+                sd_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 +857,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) {
+int sd_event_add_defer(
+                sd_event *e,
+                sd_defer_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 +890,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) {
+int sd_event_add_quit(
+                sd_event *e,
+                sd_quit_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);
 
@@ -952,42 +952,33 @@ sd_event_source* sd_event_source_unref(sd_event_source *s) {
 }
 
 sd_event *sd_event_get(sd_event_source *s) {
-        if (!s)
-                return NULL;
+        assert_return(s, NULL);
 
         return s->event;
 }
 
 int sd_event_source_get_pending(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
+        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;
+        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;
+        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;
@@ -996,15 +987,11 @@ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
 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)), -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;
@@ -1021,48 +1008,35 @@ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
 }
 
 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;
+        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;
+        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;
+        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;
+        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,16 +1049,16 @@ 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;
+        assert_return(s, -EINVAL);
+        assert_return(m, -EINVAL);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *m = s->enabled;
         return 0;
@@ -1093,13 +1067,10 @@ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
 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,9 +1121,13 @@ 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,9 +1176,13 @@ 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;
                 }
         }
@@ -1218,29 +1197,21 @@ int sd_event_source_set_enabled(sd_event_source *s, int m) {
 }
 
 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;
+        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;
+        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 (s->time.next == usec)
                 return 0;
@@ -1258,14 +1229,22 @@ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
         return 0;
 }
 
+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;
+}
+
 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;
+        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;
@@ -1283,20 +1262,6 @@ 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;
-
-        *usec = s->time.accuracy;
-        return 0;
-}
-
 int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
         assert_return(s, -EINVAL);
         assert_return(pid, -EINVAL);
@@ -1466,9 +1431,7 @@ static int flush_timer(sd_event *e, int fd, uint32_t events) {
 
         assert(e);
         assert(fd >= 0);
-
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         ss = read(fd, &x, sizeof(x));
         if (ss < 0) {
@@ -1484,7 +1447,12 @@ static int flush_timer(sd_event *e, int fd, uint32_t events) {
         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;
 
@@ -1563,9 +1531,7 @@ static int process_signal(sd_event *e, uint32_t events) {
         int r;
 
         assert(e);
-
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         for (;;) {
                 sd_event_source *s;
@@ -1726,8 +1692,8 @@ static sd_event_source* event_next_pending(sd_event *e) {
 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;
+        int r, i, m;
 
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
index 6ff85ad..d50c124 100644
--- a/src/systemd/sd-event.h
+++ b/src/systemd/sd-event.h
@@ -85,22 +85,22 @@ sd_event *sd_event_get(sd_event_source *s);
 sd_event_source* sd_event_source_ref(sd_event_source *s);
 sd_event_source* sd_event_source_unref(sd_event_source *s);
 
+int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback);
 int sd_event_source_get_pending(sd_event_source *s);
-int sd_event_source_get_io_fd(sd_event_source *s);
-int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
-int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
-int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
-int sd_event_source_get_signal(sd_event_source *s);
 int sd_event_source_get_priority(sd_event_source *s, int *priority);
 int sd_event_source_set_priority(sd_event_source *s, int priority);
 int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
 int sd_event_source_set_enabled(sd_event_source *s, int enabled);
+void* sd_event_source_get_userdata(sd_event_source *s);
+int sd_event_source_get_io_fd(sd_event_source *s);
+int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events);
+int sd_event_source_set_io_events(sd_event_source *s, uint32_t events);
+int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
 int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
 int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
 int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
+int sd_event_source_get_signal(sd_event_source *s);
 int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
-int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback);
-void* sd_event_source_get_userdata(sd_event_source *s);
 
 #endif

commit 4bee801296daef257c64647353a1815d46fe2a34
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 11 01:33:25 2013 +0200

    event: add sd_event_source_get_child_pid() call to query the PID of a child event source

diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index 3094d9e..482e3bb 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -1297,6 +1297,16 @@ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
         return 0;
 }
 
+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);
+
+        *pid = s->child.pid;
+        return 0;
+}
+
 int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback) {
         int r;
 
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
index 1fd5648..6ff85ad 100644
--- a/src/systemd/sd-event.h
+++ b/src/systemd/sd-event.h
@@ -35,8 +35,6 @@
   - Scales better with a large number of time events, since it doesn't require one timerfd each
   - Automatically tries to coalesce timer events system-wide
   - Handles signals and child PIDs
-
-  TODO: rename mute to enable?
 */
 
 typedef struct sd_event sd_event;
@@ -101,6 +99,7 @@ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
 int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);
 int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec);
+int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid);
 int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback);
 void* sd_event_source_get_userdata(sd_event_source *s);
 

commit baf762833224267ef5380fb83b22f294628695a8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 11 01:08:15 2013 +0200

    event: rename the "mute" field of event sources to "enabled"
    
    In addition, the states "UNMUTED" and "MUTED" become "ON" and "OFF".
    
    This has the benefit that a possible value of this field is not
    identical to its name, thus minimizing confusion.

diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index c52e4cd..3094d9e 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -53,7 +53,7 @@ struct sd_event_source {
         sd_prepare_handler_t prepare;
 
         EventSourceType type:4;
-        int mute:3;
+        int enabled:3;
         bool pending:1;
 
         int priority;
@@ -126,7 +126,7 @@ struct sd_event {
         sd_event_source **signal_sources;
 
         Hashmap *child_sources;
-        unsigned n_unmuted_child_sources;
+        unsigned n_enabled_child_sources;
 
         Prioq *quit;
 
@@ -145,10 +145,10 @@ static int pending_prioq_compare(const void *a, const void *b) {
         assert(x->pending);
         assert(y->pending);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -186,10 +186,10 @@ static int prepare_prioq_compare(const void *a, const void *b) {
         if (x->prepare_iteration > y->prepare_iteration)
                 return 1;
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -213,10 +213,10 @@ static int earliest_time_prioq_compare(const void *a, const void *b) {
         assert(x->type == SOURCE_MONOTONIC || x->type == SOURCE_REALTIME);
         assert(y->type == SOURCE_MONOTONIC || y->type == SOURCE_REALTIME);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Move the pending ones to the end */
@@ -246,10 +246,10 @@ static int latest_time_prioq_compare(const void *a, const void *b) {
         assert((x->type == SOURCE_MONOTONIC && y->type == SOURCE_MONOTONIC) ||
                (x->type == SOURCE_REALTIME && y->type == SOURCE_REALTIME));
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Move the pending ones to the end */
@@ -279,10 +279,10 @@ static int quit_prioq_compare(const void *a, const void *b) {
         assert(x->type == SOURCE_QUIT);
         assert(y->type == SOURCE_QUIT);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -416,18 +416,18 @@ static int source_io_unregister(sd_event_source *s) {
         return 0;
 }
 
-static int source_io_register(sd_event_source *s, int mute, uint32_t events) {
+static int source_io_register(sd_event_source *s, int enabled, uint32_t events) {
         struct epoll_event ev = {};
         int r;
 
         assert(s);
         assert(s->type == SOURCE_IO);
-        assert(mute != SD_EVENT_MUTED);
+        assert(enabled != SD_EVENT_OFF);
 
         ev.events = events;
         ev.data.ptr = s;
 
-        if (mute == SD_EVENT_ONESHOT)
+        if (enabled == SD_EVENT_ONESHOT)
                 ev.events |= EPOLLONESHOT;
 
         if (s->io.registered)
@@ -467,7 +467,7 @@ static void source_free(sd_event_source *s) {
 
                 case SOURCE_SIGNAL:
                         if (s->signal.sig > 0) {
-                                if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 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)
@@ -478,9 +478,9 @@ static void source_free(sd_event_source *s) {
 
                 case SOURCE_CHILD:
                         if (s->child.pid > 0) {
-                                if (s->mute != SD_EVENT_MUTED) {
-                                        assert(s->event->n_unmuted_child_sources > 0);
-                                        s->event->n_unmuted_child_sources--;
+                                if (s->enabled != SD_EVENT_OFF) {
+                                        assert(s->event->n_enabled_child_sources > 0);
+                                        s->event->n_enabled_child_sources--;
                                 }
 
                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
@@ -583,9 +583,9 @@ int sd_event_add_io(
         s->io.events = events;
         s->io.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_UNMUTED;
+        s->enabled = SD_EVENT_ON;
 
-        r = source_io_register(s, s->mute, events);
+        r = source_io_register(s, s->enabled, events);
         if (r < 0) {
                 source_free(s);
                 return -errno;
@@ -700,7 +700,7 @@ static int event_add_time_internal(
         s->time.callback = callback;
         s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = prioq_put(*earliest, s, &s->time.earliest_index);
         if (r < 0)
@@ -790,12 +790,12 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
         s->signal.sig = sig;
         s->signal.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_UNMUTED;
+        s->enabled = SD_EVENT_ON;
 
         e->signal_sources[sig] = s;
         assert_se(sigaddset(&e->sigset, sig) == 0);
 
-        if (sig != SIGCHLD || e->n_unmuted_child_sources == 0) {
+        if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
                 r = event_update_signal_fd(e);
                 if (r < 0) {
                         source_free(s);
@@ -840,7 +840,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
         s->child.options = options;
         s->child.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
         if (r < 0) {
@@ -848,7 +848,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
                 return r;
         }
 
-        e->n_unmuted_child_sources ++;
+        e->n_enabled_child_sources ++;
 
         assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
 
@@ -884,7 +884,7 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
 
         s->defer.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = source_set_pending(s, true);
         if (r < 0) {
@@ -918,7 +918,7 @@ int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, s
         s->quit.callback = callback;
         s->userdata = userdata;
         s->quit.prioq_index = PRIOQ_IDX_NULL;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = prioq_put(s->event->quit, s, &s->quit.prioq_index);
         if (r < 0) {
@@ -1009,7 +1009,7 @@ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
         if (s->io.events == events)
                 return 0;
 
-        if (s->mute != SD_EVENT_MUTED) {
+        if (s->enabled != SD_EVENT_OFF) {
                 r = source_io_register(s, s->io.events, events);
                 if (r < 0)
                         return r;
@@ -1078,7 +1078,7 @@ int sd_event_source_set_priority(sd_event_source *s, int priority) {
         return 0;
 }
 
-int sd_event_source_get_mute(sd_event_source *s, int *m) {
+int sd_event_source_get_enabled(sd_event_source *s, int *m) {
         if (!s)
                 return -EINVAL;
         if (!m)
@@ -1086,25 +1086,25 @@ int sd_event_source_get_mute(sd_event_source *s, int *m) {
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
-        *m = s->mute;
+        *m = s->enabled;
         return 0;
 }
 
-int sd_event_source_set_mute(sd_event_source *s, int m) {
+int sd_event_source_set_enabled(sd_event_source *s, int m) {
         int r;
 
         if (!s)
                 return -EINVAL;
-        if (m != SD_EVENT_MUTED && m != SD_EVENT_UNMUTED && !SD_EVENT_ONESHOT)
+        if (m != SD_EVENT_OFF && m != SD_EVENT_ON && !SD_EVENT_ONESHOT)
                 return -EINVAL;
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
-        if (s->mute == m)
+        if (s->enabled == m)
                 return 0;
 
-        if (m == SD_EVENT_MUTED) {
+        if (m == SD_EVENT_OFF) {
 
                 switch (s->type) {
 
@@ -1113,24 +1113,24 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         if (r < 0)
                                 return r;
 
-                        s->mute = m;
+                        s->enabled = m;
                         break;
 
                 case SOURCE_MONOTONIC:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_REALTIME:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_SIGNAL:
-                        s->mute = m;
-                        if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 0) {
+                        s->enabled = m;
+                        if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
                                 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
                                 event_update_signal_fd(s->event);
                         }
@@ -1138,10 +1138,10 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         break;
 
                 case SOURCE_CHILD:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        assert(s->event->n_unmuted_child_sources > 0);
-                        s->event->n_unmuted_child_sources--;
+                        assert(s->event->n_enabled_child_sources > 0);
+                        s->event->n_enabled_child_sources--;
 
                         if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
                                 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
@@ -1152,7 +1152,7 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
 
                 case SOURCE_DEFER:
                 case SOURCE_QUIT:
-                        s->mute = m;
+                        s->enabled = m;
                         break;
                 }
 
@@ -1164,35 +1164,35 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         if (r < 0)
                                 return r;
 
-                        s->mute = m;
+                        s->enabled = m;
                         break;
 
                 case SOURCE_MONOTONIC:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_REALTIME:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_SIGNAL:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 0)  {
+                        if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)  {
                                 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
                                 event_update_signal_fd(s->event);
                         }
                         break;
 
                 case SOURCE_CHILD:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        if (s->mute == SD_EVENT_MUTED) {
-                                s->event->n_unmuted_child_sources++;
+                        if (s->enabled == SD_EVENT_OFF) {
+                                s->event->n_enabled_child_sources++;
 
                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
                                         assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
@@ -1203,7 +1203,7 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
 
                 case SOURCE_DEFER:
                 case SOURCE_QUIT:
-                        s->mute = m;
+                        s->enabled = m;
                         break;
                 }
         }
@@ -1404,11 +1404,11 @@ static int event_arm_timer(
         assert_se(next);
 
         a = prioq_peek(earliest);
-        if (!a || a->mute == SD_EVENT_MUTED)
+        if (!a || a->enabled == SD_EVENT_OFF)
                 return 0;
 
         b = prioq_peek(latest);
-        assert_se(b && b->mute != SD_EVENT_MUTED);
+        assert_se(b && b->enabled != SD_EVENT_OFF);
 
         t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
         if (*next == t)
@@ -1444,7 +1444,7 @@ static int process_io(sd_event *e, sd_event_source *s, uint32_t events) {
            anymore. We can save a syscall here...
         */
 
-        if (s->mute == SD_EVENT_ONESHOT)
+        if (s->enabled == SD_EVENT_ONESHOT)
                 s->io.registered = false;
 
         return source_set_pending(s, true);
@@ -1484,7 +1484,7 @@ static int process_timer(sd_event *e, usec_t n, Prioq *earliest, Prioq *latest)
                 s = prioq_peek(earliest);
                 if (!s ||
                     s->time.next > n ||
-                    s->mute == SD_EVENT_MUTED ||
+                    s->enabled == SD_EVENT_OFF ||
                     s->pending)
                         break;
 
@@ -1528,7 +1528,7 @@ static int process_child(sd_event *e) {
                 if (s->pending)
                         continue;
 
-                if (s->mute == SD_EVENT_MUTED)
+                if (s->enabled == SD_EVENT_OFF)
                         continue;
 
                 zero(s->child.siginfo);
@@ -1607,8 +1607,8 @@ static int source_dispatch(sd_event_source *s) {
                         return r;
         }
 
-        if (s->mute == SD_EVENT_ONESHOT) {
-                r = sd_event_source_set_mute(s, SD_EVENT_MUTED);
+        if (s->enabled == SD_EVENT_ONESHOT) {
+                r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
                 if (r < 0)
                         return r;
         }
@@ -1656,7 +1656,7 @@ static int event_prepare(sd_event *e) {
                 sd_event_source *s;
 
                 s = prioq_peek(e->prepare);
-                if (!s || s->prepare_iteration == e->iteration || s->mute == SD_EVENT_MUTED)
+                if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
                         break;
 
                 s->prepare_iteration = e->iteration;
@@ -1681,7 +1681,7 @@ static int dispatch_quit(sd_event *e) {
         assert(e);
 
         p = prioq_peek(e->quit);
-        if (!p || p->mute == SD_EVENT_MUTED) {
+        if (!p || p->enabled == SD_EVENT_OFF) {
                 e->state = SD_EVENT_FINISHED;
                 return 0;
         }
@@ -1707,7 +1707,7 @@ static sd_event_source* event_next_pending(sd_event *e) {
         if (!p)
                 return NULL;
 
-        if (p->mute == SD_EVENT_MUTED)
+        if (p->enabled == SD_EVENT_OFF)
                 return NULL;
 
         return p;
diff --git a/src/libsystemd-bus/test-event.c b/src/libsystemd-bus/test-event.c
index 7004f61..80aa28e 100644
--- a/src/libsystemd-bus/test-event.c
+++ b/src/libsystemd-bus/test-event.c
@@ -35,7 +35,7 @@ static int io_handler(sd_event_source *s, int fd, uint32_t revents, void *userda
         log_info("got IO on %c", PTR_TO_INT(userdata));
 
         if (userdata == INT_TO_PTR('a')) {
-                assert_se(sd_event_source_set_mute(s, SD_EVENT_MUTED) >= 0);
+                assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
                 assert_se(!got_a);
                 got_a = true;
         } else if (userdata == INT_TO_PTR('b')) {
@@ -85,7 +85,7 @@ static int signal_handler(sd_event_source *s, const struct signalfd_siginfo *si,
                 _exit(0);
 
         assert_se(sd_event_add_child(sd_event_get(s), pid, WEXITED, child_handler, INT_TO_PTR('f'), &p) >= 0);
-        assert_se(sd_event_source_set_mute(p, SD_EVENT_ONESHOT) >= 0);
+        assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
 
         sd_event_source_unref(s);
 
@@ -106,7 +106,7 @@ static int defer_handler(sd_event_source *s, void *userdata) {
         assert_se(sigaddset(&ss, SIGUSR1) >= 0);
         assert_se(sigprocmask(SIG_BLOCK, &ss, NULL) >= 0);
         assert_se(sd_event_add_signal(sd_event_get(s), SIGUSR1, signal_handler, INT_TO_PTR('e'), &p) >= 0);
-        assert_se(sd_event_source_set_mute(p, SD_EVENT_ONESHOT) >= 0);
+        assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
         raise(SIGUSR1);
 
         sd_event_source_unref(s);
@@ -125,7 +125,7 @@ static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
                         sd_event_source *p;
 
                         assert_se(sd_event_add_defer(sd_event_get(s), defer_handler, INT_TO_PTR('d'), &p) >= 0);
-                        assert_se(sd_event_source_set_mute(p, SD_EVENT_ONESHOT) >= 0);
+                        assert_se(sd_event_source_set_enabled(p, SD_EVENT_ONESHOT) >= 0);
                 } else {
                         assert(!got_c);
                         got_c = true;
@@ -165,10 +165,10 @@ int main(int argc, char *argv[]) {
         assert_se(sd_event_add_quit(e, quit_handler, INT_TO_PTR('g'), &q) >= 0);
 
         assert_se(sd_event_source_set_priority(x, 99) >= 0);
-        assert_se(sd_event_source_set_mute(y, SD_EVENT_ONESHOT) >= 0);
+        assert_se(sd_event_source_set_enabled(y, SD_EVENT_ONESHOT) >= 0);
         assert_se(sd_event_source_set_prepare(x, prepare_handler) >= 0);
         assert_se(sd_event_source_set_priority(z, 50) >= 0);
-        assert_se(sd_event_source_set_mute(z, SD_EVENT_ONESHOT) >= 0);
+        assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
         assert_se(sd_event_source_set_prepare(z, prepare_handler) >= 0);
 
         assert_se(write(a[1], &ch, 1) >= 0);
@@ -193,7 +193,7 @@ int main(int argc, char *argv[]) {
 
         do_quit = true;
         assert_se(sd_event_source_set_time(z, now(CLOCK_MONOTONIC) + 200 * USEC_PER_MSEC) >= 0);
-        assert_se(sd_event_source_set_mute(z, SD_EVENT_ONESHOT) >= 0);
+        assert_se(sd_event_source_set_enabled(z, SD_EVENT_ONESHOT) >= 0);
 
         assert_se(sd_event_loop(e) >= 0);
 
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
index 3d0b820..1fd5648 100644
--- a/src/systemd/sd-event.h
+++ b/src/systemd/sd-event.h
@@ -43,8 +43,8 @@ typedef struct sd_event sd_event;
 typedef struct sd_event_source sd_event_source;
 
 enum {
-        SD_EVENT_MUTED = 0,
-        SD_EVENT_UNMUTED = 1,
+        SD_EVENT_OFF = 0,
+        SD_EVENT_ON = 1,
         SD_EVENT_ONESHOT = -1
 };
 
@@ -95,8 +95,8 @@ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
 int sd_event_source_get_signal(sd_event_source *s);
 int sd_event_source_get_priority(sd_event_source *s, int *priority);
 int sd_event_source_set_priority(sd_event_source *s, int priority);
-int sd_event_source_get_mute(sd_event_source *s, int *m);
-int sd_event_source_set_mute(sd_event_source *s, int m);
+int sd_event_source_get_enabled(sd_event_source *s, int *enabled);
+int sd_event_source_set_enabled(sd_event_source *s, int enabled);
 int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
 int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);

commit da7e457c5b9339721454ae8401a03ffdd781e6a9
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 11 00:49:11 2013 +0200

    event: implement quit handlers
    
    Quit handlers are executed when an event loop is terminated via
    sd_event_request_quit(). They are in a way atexit() handlers that are
    executed in a well-defined environment, time and thread: from the event
    loop thread when the event loop finishes.

diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c
index e3110c3..c52e4cd 100644
--- a/src/libsystemd-bus/sd-event.c
+++ b/src/libsystemd-bus/sd-event.c
@@ -24,7 +24,6 @@
 #include <sys/wait.h>
 
 #include "macro.h"
-#include "refcnt.h"
 #include "prioq.h"
 #include "hashmap.h"
 #include "util.h"
@@ -42,18 +41,19 @@ typedef enum EventSourceType {
         SOURCE_REALTIME,
         SOURCE_SIGNAL,
         SOURCE_CHILD,
-        SOURCE_DEFER
+        SOURCE_DEFER,
+        SOURCE_QUIT
 } EventSourceType;
 
 struct sd_event_source {
-        RefCount n_ref;
+        unsigned n_ref;
 
         sd_event *event;
         void *userdata;
         sd_prepare_handler_t prepare;
 
         EventSourceType type:4;
-        sd_event_mute_t mute:3;
+        int mute:3;
         bool pending:1;
 
         int priority;
@@ -90,11 +90,15 @@ struct sd_event_source {
                 struct {
                         sd_defer_handler_t callback;
                 } defer;
+                struct {
+                        sd_quit_handler_t callback;
+                        unsigned prioq_index;
+                } quit;
         };
 };
 
 struct sd_event {
-        RefCount n_ref;
+        unsigned n_ref;
 
         int epoll_fd;
         int signal_fd;
@@ -115,21 +119,24 @@ struct sd_event {
         Prioq *realtime_earliest;
         Prioq *realtime_latest;
 
+        usec_t realtime_next, monotonic_next;
+        usec_t perturb;
+
         sigset_t sigset;
         sd_event_source **signal_sources;
 
         Hashmap *child_sources;
         unsigned n_unmuted_child_sources;
 
-        unsigned iteration;
+        Prioq *quit;
 
-        usec_t realtime_next, monotonic_next;
-        usec_t perturb;
+        pid_t original_pid;
 
-        bool quit:1;
-        bool need_process_child:1;
+        unsigned iteration;
+        int state;
 
-        pid_t original_pid;
+        bool quit_requested:1;
+        bool need_process_child:1;
 };
 
 static int pending_prioq_compare(const void *a, const void *b) {
@@ -236,8 +243,8 @@ static int earliest_time_prioq_compare(const void *a, const void *b) {
 static int latest_time_prioq_compare(const void *a, const void *b) {
         const sd_event_source *x = a, *y = b;
 
-        assert(x->type == SOURCE_MONOTONIC || x->type == SOURCE_REALTIME);
-        assert(y->type == SOURCE_MONOTONIC || y->type == SOURCE_REALTIME);
+        assert((x->type == SOURCE_MONOTONIC && y->type == SOURCE_MONOTONIC) ||
+               (x->type == SOURCE_REALTIME && y->type == SOURCE_REALTIME));
 
         /* Unmuted ones first */
         if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
@@ -266,6 +273,33 @@ static int latest_time_prioq_compare(const void *a, const void *b) {
         return 0;
 }
 
+static int quit_prioq_compare(const void *a, const void *b) {
+        const sd_event_source *x = a, *y = b;
+
+        assert(x->type == SOURCE_QUIT);
+        assert(y->type == SOURCE_QUIT);
+
+        /* Unmuted ones first */
+        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+                return -1;
+        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+                return 1;
+
+        /* Lower priority values first */
+        if (x->priority < y->priority)
+                return -1;
+        if (x->priority > y->priority)
+                return 1;
+
+        /* Stability for the rest */
+        if (x < y)
+                return -1;
+        if (x > y)
+                return 1;
+
+        return 0;
+}
+
 static void event_free(sd_event *e) {
         assert(e);
 
@@ -287,6 +321,7 @@ static void event_free(sd_event *e) {
         prioq_free(e->monotonic_latest);
         prioq_free(e->realtime_earliest);
         prioq_free(e->realtime_latest);
+        prioq_free(e->quit);
 
         free(e->signal_sources);
 
@@ -305,7 +340,7 @@ int sd_event_new(sd_event** ret) {
         if (!e)
                 return -ENOMEM;
 
-        e->n_ref = REFCNT_INIT;
+        e->n_ref = 1;
         e->signal_fd = e->realtime_fd = e->monotonic_fd = e->epoll_fd = -1;
         e->realtime_next = e->monotonic_next = (usec_t) -1;
         e->original_pid = getpid();
@@ -336,7 +371,8 @@ sd_event* sd_event_ref(sd_event *e) {
         if (!e)
                 return NULL;
 
-        assert_se(REFCNT_INC(e->n_ref) >= 2);
+        assert(e->n_ref >= 1);
+        e->n_ref++;
 
         return e;
 }
@@ -345,7 +381,10 @@ sd_event* sd_event_unref(sd_event *e) {
         if (!e)
                 return NULL;
 
-        if (REFCNT_DEC(e->n_ref) <= 0)
+        assert(e->n_ref >= 1);
+        e->n_ref--;
+
+        if (e->n_ref <= 0)
                 event_free(e);
 
         return NULL;
@@ -377,18 +416,18 @@ static int source_io_unregister(sd_event_source *s) {
         return 0;
 }
 
-static int source_io_register(sd_event_source *s, sd_event_mute_t m, uint32_t events) {
+static int source_io_register(sd_event_source *s, int mute, uint32_t events) {
         struct epoll_event ev = {};
         int r;
 
         assert(s);
         assert(s->type == SOURCE_IO);
-        assert(m != SD_EVENT_MUTED);
+        assert(mute != SD_EVENT_MUTED);
 
         ev.events = events;
         ev.data.ptr = s;
 
-        if (m == SD_EVENT_ONESHOT)
+        if (mute == SD_EVENT_ONESHOT)
                 ev.events |= EPOLLONESHOT;
 
         if (s->io.registered)
@@ -451,6 +490,10 @@ static void source_free(sd_event_source *s) {
                         }
 
                         break;
+
+                case SOURCE_QUIT:
+                        prioq_remove(s->event->quit, s, &s->quit.prioq_index);
+                        break;
                 }
 
                 if (s->pending)
@@ -469,6 +512,7 @@ static int source_set_pending(sd_event_source *s, bool b) {
         int r;
 
         assert(s);
+        assert(s->type != SOURCE_QUIT);
 
         if (s->pending == b)
                 return 0;
@@ -498,10 +542,9 @@ static sd_event_source *source_new(sd_event *e, EventSourceType type) {
         if (!s)
                 return NULL;
 
-        s->n_ref = REFCNT_INIT;
+        s->n_ref = 1;
         s->event = sd_event_ref(e);
         s->type = type;
-        s->mute = SD_EVENT_UNMUTED;
         s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
 
         return s;
@@ -528,6 +571,7 @@ int sd_event_add_io(
                 return -EINVAL;
         if (!ret)
                 return -EINVAL;
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(e))
                 return -ECHILD;
 
@@ -539,6 +583,7 @@ int sd_event_add_io(
         s->io.events = events;
         s->io.callback = callback;
         s->userdata = userdata;
+        s->mute = SD_EVENT_UNMUTED;
 
         r = source_io_register(s, s->mute, events);
         if (r < 0) {
@@ -620,6 +665,7 @@ static int event_add_time_internal(
                 return -EINVAL;
         if (accuracy == (uint64_t) -1)
                 return -EINVAL;
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(e))
                 return -ECHILD;
 
@@ -652,9 +698,9 @@ static int event_add_time_internal(
         s->time.next = usec;
         s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
         s->time.callback = callback;
-        s->time.earliest_index = PRIOQ_IDX_NULL;
-        s->time.latest_index = PRIOQ_IDX_NULL;
+        s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
         s->userdata = userdata;
+        s->mute = SD_EVENT_ONESHOT;
 
         r = prioq_put(*earliest, s, &s->time.earliest_index);
         if (r < 0)
@@ -726,6 +772,7 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
                 return -EINVAL;
         if (!ret)
                 return -EINVAL;
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(e))
                 return -ECHILD;
 
@@ -743,6 +790,7 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
         s->signal.sig = sig;
         s->signal.callback = callback;
         s->userdata = userdata;
+        s->mute = SD_EVENT_UNMUTED;
 
         e->signal_sources[sig] = s;
         assert_se(sigaddset(&e->sigset, sig) == 0);
@@ -773,6 +821,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
                 return -EINVAL;
         if (!ret)
                 return -EINVAL;
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(e))
                 return -ECHILD;
 
@@ -791,6 +840,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
         s->child.options = options;
         s->child.callback = callback;
         s->userdata = userdata;
+        s->mute = SD_EVENT_ONESHOT;
 
         r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
         if (r < 0) {
@@ -824,6 +874,7 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
                 return -EINVAL;
         if (!ret)
                 return -EINVAL;
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(e))
                 return -ECHILD;
 
@@ -833,6 +884,7 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
 
         s->defer.callback = callback;
         s->userdata = userdata;
+        s->mute = SD_EVENT_ONESHOT;
 
         r = source_set_pending(s, true);
         if (r < 0) {
@@ -844,26 +896,61 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
         return 0;
 }
 
-sd_event_source* sd_event_source_ref(sd_event_source *s) {
+int sd_event_add_quit(sd_event *e, sd_quit_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(e->state != SD_EVENT_FINISHED, -ESTALE);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        if (!e->quit) {
+                e->quit = prioq_new(quit_prioq_compare);
+                if (!e->quit)
+                        return -ENOMEM;
+        }
+
+        s = source_new(e, SOURCE_QUIT);
         if (!s)
-                return NULL;
+                return -ENOMEM;
 
-        assert_se(REFCNT_INC(s->n_ref) >= 2);
+        s->quit.callback = callback;
+        s->userdata = userdata;
+        s->quit.prioq_index = PRIOQ_IDX_NULL;
+        s->mute = SD_EVENT_ONESHOT;
+
+        r = prioq_put(s->event->quit, s, &s->quit.prioq_index);
+        if (r < 0) {
+                source_free(s);
+                return r;
+        }
+
+        *ret = s;
+        return 0;
+}
+
+sd_event_source* sd_event_source_ref(sd_event_source *s) {
+        assert_return(s, NULL);
+
+        assert(s->n_ref >= 1);
+        s->n_ref++;
 
         return s;
 }
 
 sd_event_source* sd_event_source_unref(sd_event_source *s) {
-        if (!s)
-                return NULL;
+        assert_return(s, NULL);
 
-        if (REFCNT_DEC(s->n_ref) <= 0)
+        assert(s->n_ref >= 1);
+        s->n_ref--;
+
+        if (s->n_ref <= 0)
                 source_free(s);
 
         return NULL;
 }
 
-
 sd_event *sd_event_get(sd_event_source *s) {
         if (!s)
                 return NULL;
@@ -874,6 +961,7 @@ sd_event *sd_event_get(sd_event_source *s) {
 int sd_event_source_get_pending(sd_event_source *s) {
         if (!s)
                 return -EINVAL;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -914,6 +1002,7 @@ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
                 return -EDOM;
         if (events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP))
                 return -EINVAL;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -952,6 +1041,7 @@ int sd_event_source_get_signal(sd_event_source *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;
 
@@ -970,6 +1060,7 @@ int sd_event_source_get_priority(sd_event_source *s, int *priority) {
 int sd_event_source_set_priority(sd_event_source *s, int priority) {
         if (!s)
                 return -EINVAL;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -987,7 +1078,7 @@ int sd_event_source_set_priority(sd_event_source *s, int priority) {
         return 0;
 }
 
-int sd_event_source_get_mute(sd_event_source *s, sd_event_mute_t *m) {
+int sd_event_source_get_mute(sd_event_source *s, int *m) {
         if (!s)
                 return -EINVAL;
         if (!m)
@@ -999,13 +1090,14 @@ int sd_event_source_get_mute(sd_event_source *s, sd_event_mute_t *m) {
         return 0;
 }
 
-int sd_event_source_set_mute(sd_event_source *s, sd_event_mute_t m) {
+int sd_event_source_set_mute(sd_event_source *s, int m) {
         int r;
 
         if (!s)
                 return -EINVAL;
         if (m != SD_EVENT_MUTED && m != SD_EVENT_UNMUTED && !SD_EVENT_ONESHOT)
                 return -EINVAL;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -1059,6 +1151,7 @@ int sd_event_source_set_mute(sd_event_source *s, sd_event_mute_t m) {
                         break;
 
                 case SOURCE_DEFER:
+                case SOURCE_QUIT:
                         s->mute = m;
                         break;
                 }
@@ -1109,6 +1202,7 @@ int sd_event_source_set_mute(sd_event_source *s, sd_event_mute_t m) {
                         break;
 
                 case SOURCE_DEFER:
+                case SOURCE_QUIT:
                         s->mute = m;
                         break;
                 }
@@ -1144,6 +1238,7 @@ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
                 return -EINVAL;
         if (s->type != SOURCE_REALTIME && s->type != SOURCE_MONOTONIC)
                 return -EDOM;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -1168,6 +1263,7 @@ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
                 return -EINVAL;
         if (s->type != SOURCE_MONOTONIC && s->type != SOURCE_REALTIME)
                 return -EDOM;
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
         if (event_pid_changed(s->event))
                 return -ECHILD;
 
@@ -1177,7 +1273,6 @@ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
         if (s->time.accuracy == usec)
                 return 0;
 
-
         s->time.accuracy = usec;
 
         if (s->type == SOURCE_REALTIME)
@@ -1205,10 +1300,10 @@ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
 int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback) {
         int r;
 
-        if (!s)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(s, -EINVAL);
+        assert_return(s->type != SOURCE_QUIT, -EDOM);
+        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->prepare == callback)
                 return 0;
@@ -1235,8 +1330,7 @@ int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callbac
 }
 
 void* sd_event_source_get_userdata(sd_event_source *s) {
-        if (!s)
-                return NULL;
+        assert_return(s, NULL);
 
         return s->userdata;
 }
@@ -1361,6 +1455,7 @@ static int flush_timer(sd_event *e, int fd, uint32_t events) {
         ssize_t ss;
 
         assert(e);
+        assert(fd >= 0);
 
         if (events != EPOLLIN)
                 return -EIO;
@@ -1457,6 +1552,8 @@ static int process_signal(sd_event *e, uint32_t events) {
         ssize_t ss;
         int r;
 
+        assert(e);
+
         if (events != EPOLLIN)
                 return -EIO;
 
@@ -1502,11 +1599,13 @@ static int source_dispatch(sd_event_source *s) {
         int r;
 
         assert(s);
-        assert(s->pending);
+        assert(s->pending || s->type == SOURCE_QUIT);
 
-        r = source_set_pending(s, false);
-        if (r < 0)
-                return r;
+        if (s->type != SOURCE_DEFER && s->type != SOURCE_QUIT) {
+                r = source_set_pending(s, false);
+                if (r < 0)
+                        return r;
+        }
 
         if (s->mute == SD_EVENT_ONESHOT) {
                 r = sd_event_source_set_mute(s, SD_EVENT_MUTED);
@@ -1539,6 +1638,10 @@ static int source_dispatch(sd_event_source *s) {
         case SOURCE_DEFER:
                 r = s->defer.callback(s, s->userdata);
                 break;
+
+        case SOURCE_QUIT:
+                r = s->quit.callback(s, s->userdata);
+                break;
         }
 
         return r;
@@ -1571,9 +1674,35 @@ static int event_prepare(sd_event *e) {
         return 0;
 }
 
+static int dispatch_quit(sd_event *e) {
+        sd_event_source *p;
+        int r;
+
+        assert(e);
+
+        p = prioq_peek(e->quit);
+        if (!p || p->mute == SD_EVENT_MUTED) {
+                e->state = SD_EVENT_FINISHED;
+                return 0;
+        }
+
+        sd_event_ref(e);
+        e->iteration++;
+        e->state = SD_EVENT_QUITTING;
+
+        r = source_dispatch(p);
+
+        e->state = SD_EVENT_PASSIVE;
+        sd_event_unref(e);
+
+        return r;
+}
+
 static sd_event_source* event_next_pending(sd_event *e) {
         sd_event_source *p;
 
+        assert(e);
+
         p = prioq_peek(e->pending);
         if (!p)
                 return NULL;
@@ -1590,18 +1719,21 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
         int r, i, m;
         dual_timestamp n;
 
-        if (!e)
-                return -EINVAL;
-        if (e->quit)
-                return -ESTALE;
-        if (event_pid_changed(e))
-                return -ECHILD;
+        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);
+
+        if (e->quit_requested)
+                return dispatch_quit(e);
 
+        sd_event_ref(e);
         e->iteration++;
+        e->state = SD_EVENT_RUNNING;
 
         r = event_prepare(e);
         if (r < 0)
-                return r;
+                goto finish;
 
         if (event_next_pending(e) || e->need_process_child)
                 timeout = 0;
@@ -1609,17 +1741,19 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
         if (timeout > 0) {
                 r = event_arm_timer(e, e->monotonic_fd, e->monotonic_earliest, e->monotonic_latest, &e->monotonic_next);
                 if (r < 0)
-                        return r;
+                        goto finish;
 
                 r = event_arm_timer(e, e->realtime_fd, e->realtime_earliest, e->realtime_latest, &e->realtime_next);
                 if (r < 0)
-                        return r;
+                        goto finish;
         }
 
         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)
-                return m;
+        if (m < 0) {
+                r = m;
+                goto finish;
+        }
 
         dual_timestamp_get(&n);
 
@@ -1635,62 +1769,79 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
                         r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
 
                 if (r < 0)
-                        return r;
+                        goto finish;
         }
 
         r = process_timer(e, n.monotonic, e->monotonic_earliest, e->monotonic_latest);
         if (r < 0)
-                return r;
+                goto finish;
 
         r = process_timer(e, n.realtime, e->realtime_earliest, e->realtime_latest);
         if (r < 0)
-                return r;
+                goto finish;
 
         if (e->need_process_child) {
                 r = process_child(e);
                 if (r < 0)
-                        return r;
+                        goto finish;
         }
 
         p = event_next_pending(e);
-        if (!p)
-                return 0;
+        if (!p) {
+                r = 0;
+                goto finish;
+        }
+
+        r = source_dispatch(p);
 
-        return source_dispatch(p);
+finish:
+        e->state = SD_EVENT_PASSIVE;
+        sd_event_unref(e);
+
+        return r;
 }
 
 int sd_event_loop(sd_event *e) {
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(e, -EINVAL);
+        assert_return(!event_pid_changed(e), -ECHILD);
+        assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
+
+        sd_event_ref(e);
 
-        while (!e->quit) {
+        while (e->state != SD_EVENT_FINISHED) {
                 r = sd_event_run(e, (uint64_t) -1);
                 if (r < 0)
-                        return r;
+                        goto finish;
         }
 
-        return 0;
+        r = 0;
+
+finish:
+        sd_event_unref(e);
+        return r;
 }
 
-int sd_event_quit(sd_event *e) {
-        if (!e)
-                return EINVAL;
-        if (event_pid_changed(e))
-                return -ECHILD;
+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) {
+        assert_return(e, -EINVAL);
+        assert_return(!event_pid_changed(e), -ECHILD);
 
-        return e->quit;
+        return e->quit_requested;
 }
 
 int sd_event_request_quit(sd_event *e) {
-        if (!e)
-                return -EINVAL;
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(e, -EINVAL);
+        assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
+        assert_return(!event_pid_changed(e), -ECHILD);
 
-        e->quit = true;
+        e->quit_requested = true;
         return 0;
 }
diff --git a/src/libsystemd-bus/test-event.c b/src/libsystemd-bus/test-event.c
index acb5cee..7004f61 100644
--- a/src/libsystemd-bus/test-event.c
+++ b/src/libsystemd-bus/test-event.c
@@ -136,9 +136,19 @@ static int time_handler(sd_event_source *s, uint64_t usec, void *userdata) {
         return 2;
 }
 
+static bool got_quit = false;
+
+static int quit_handler(sd_event_source *s, void *userdata) {
+        log_info("got quit handler on %c", PTR_TO_INT(userdata));
+
+        got_quit = true;
+
+        return 3;
+}
+
 int main(int argc, char *argv[]) {
         sd_event *e = NULL;
-        sd_event_source *x = NULL, *y = NULL, *z = NULL;
+        sd_event_source *x = NULL, *y = NULL, *z = NULL, *q = NULL;
         static const char ch = 'x';
         int a[2] = { -1, -1 }, b[2] = { -1, -1};
 
@@ -152,6 +162,7 @@ int main(int argc, char *argv[]) {
         assert_se(sd_event_add_io(e, a[0], EPOLLIN, io_handler, INT_TO_PTR('a'), &x) >= 0);
         assert_se(sd_event_add_io(e, b[0], EPOLLIN, io_handler, INT_TO_PTR('b'), &y) >= 0);
         assert_se(sd_event_add_monotonic(e, 0, 0, time_handler, INT_TO_PTR('c'), &z) >= 0);
+        assert_se(sd_event_add_quit(e, quit_handler, INT_TO_PTR('g'), &q) >= 0);
 
         assert_se(sd_event_source_set_priority(x, 99) >= 0);
         assert_se(sd_event_source_set_mute(y, SD_EVENT_ONESHOT) >= 0);
@@ -187,6 +198,7 @@ int main(int argc, char *argv[]) {
         assert_se(sd_event_loop(e) >= 0);
 
         sd_event_source_unref(z);
+        sd_event_source_unref(q);
 
         sd_event_unref(e);
 
diff --git a/src/systemd/sd-event.h b/src/systemd/sd-event.h
index 8908c84..3d0b820 100644
--- a/src/systemd/sd-event.h
+++ b/src/systemd/sd-event.h
@@ -36,19 +36,24 @@
   - Automatically tries to coalesce timer events system-wide
   - Handles signals and child PIDs
 
-  TODO:
-
-  - quit hooks
+  TODO: rename mute to enable?
 */
 
 typedef struct sd_event sd_event;
 typedef struct sd_event_source sd_event_source;
 
-typedef enum sd_event_mute {
+enum {
         SD_EVENT_MUTED = 0,
         SD_EVENT_UNMUTED = 1,
         SD_EVENT_ONESHOT = -1
-} sd_event_mute_t;
+};
+
+enum {
+        SD_EVENT_PASSIVE,
+        SD_EVENT_RUNNING,
+        SD_EVENT_QUITTING,
+        SD_EVENT_FINISHED
+};
 
 typedef int (*sd_io_handler_t)(sd_event_source *s, int fd, uint32_t revents, void *userdata);
 typedef int (*sd_time_handler_t)(sd_event_source *s, uint64_t usec, void *userdata);
@@ -56,6 +61,7 @@ typedef int (*sd_signal_handler_t)(sd_event_source *s, const struct signalfd_sig
 typedef int (*sd_child_handler_t)(sd_event_source *s, const siginfo_t *si, void *userdata);
 typedef int (*sd_defer_handler_t)(sd_event_source *s, void *userdata);
 typedef int (*sd_prepare_handler_t)(sd_event_source *s, void *userdata);
+typedef int (*sd_quit_handler_t)(sd_event_source *s, void *userdata);
 
 int sd_event_new(sd_event **e);
 sd_event* sd_event_ref(sd_event *e);
@@ -67,11 +73,13 @@ int sd_event_add_realtime(sd_event *e, uint64_t usec, uint64_t accuracy, sd_time
 int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void *userdata, sd_event_source **s);
 int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t callback, void *userdata, sd_event_source **s);
 int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata, sd_event_source **s);
+int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, sd_event_source **s);
 
 int sd_event_run(sd_event *e, uint64_t timeout);
 int sd_event_loop(sd_event *e);
 
-int sd_event_quit(sd_event *e);
+int sd_event_get_state(sd_event *e);
+int sd_event_get_quit(sd_event *e);
 int sd_event_request_quit(sd_event *e);
 
 sd_event *sd_event_get(sd_event_source *s);
@@ -87,8 +95,8 @@ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents);
 int sd_event_source_get_signal(sd_event_source *s);
 int sd_event_source_get_priority(sd_event_source *s, int *priority);
 int sd_event_source_set_priority(sd_event_source *s, int priority);
-int sd_event_source_get_mute(sd_event_source *s, sd_event_mute_t *m);
-int sd_event_source_set_mute(sd_event_source *s, sd_event_mute_t m);
+int sd_event_source_get_mute(sd_event_source *s, int *m);
+int sd_event_source_set_mute(sd_event_source *s, int m);
 int sd_event_source_get_time(sd_event_source *s, uint64_t *usec);
 int sd_event_source_set_time(sd_event_source *s, uint64_t usec);
 int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec);

commit 18387b5983150181dd9dee8edf1573285eecbaa4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Fri Oct 11 00:45:47 2013 +0200

    macro: add new assert_return() macro for early parameter checking in functions
    
    For the library functions we expose we currently repeatedly use checks
    like the following:
    
    if (!value_is_ok(parameter1))
            return -EINVAL;
    if (!value_is_ok(parameter2))
            return -EINVAL;
    
    And so on. Let's turn this into a macro:
    
    assert_return(value_is_ok(parameter1), -EINVAL);
    assert_return(value_is_ok(paramater2), -EINVAL);
    
    This makes our code a bit shorter and simpler, and also allows us to add
    a _unlikely_() around the check.

diff --git a/src/shared/macro.h b/src/shared/macro.h
index d4f92b6..06e16cd 100644
--- a/src/shared/macro.h
+++ b/src/shared/macro.h
@@ -159,6 +159,12 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         } while (false)
 #endif
 
+#define assert_return(expr, r)                    \
+        do {                                      \
+                if (!(expr))                      \
+                        return (r);               \
+        } while (false)
+
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))



More information about the systemd-commits mailing list