chiark / gitweb /
gpt-auto-generator: skip nonexistent devices
[elogind.git] / src / libsystemd-bus / sd-event.c
index 946ec0fc4fc053cbc2d09ebcfdb92ab1c67cd9e0..0b7b71d16e5ed5f893dfe8d4ba402e887a1af6f6 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/epoll.h>
 #include <sys/timerfd.h>
 #include <sys/wait.h>
+#include <pthread.h>
 
 #include "sd-id128.h"
 #include "sd-daemon.h"
@@ -34,7 +35,7 @@
 
 #include "sd-event.h"
 
-#define EPOLL_QUEUE_MAX 64
+#define EPOLL_QUEUE_MAX 512U
 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
 
 typedef enum EventSourceType {
@@ -151,6 +152,8 @@ struct sd_event {
         sd_event **default_event_ptr;
 
         usec_t watchdog_last, watchdog_period;
+
+        unsigned n_sources;
 };
 
 static int pending_prioq_compare(const void *a, const void *b) {
@@ -316,6 +319,7 @@ static int exit_prioq_compare(const void *a, const void *b) {
 
 static void event_free(sd_event *e) {
         assert(e);
+        assert(e->n_sources == 0);
 
         if (e->default_event_ptr)
                 *(e->default_event_ptr) = NULL;
@@ -470,6 +474,8 @@ static void source_free(sd_event_source *s) {
         assert(s);
 
         if (s->event) {
+                assert(s->event->n_sources > 0);
+
                 switch (s->type) {
 
                 case SOURCE_IO:
@@ -532,6 +538,7 @@ static void source_free(sd_event_source *s) {
                 if (s->prepare)
                         prioq_remove(s->event->prepare, s, &s->prepare_index);
 
+                s->event->n_sources--;
                 sd_event_unref(s->event);
         }
 
@@ -585,6 +592,8 @@ static sd_event_source *source_new(sd_event *e, EventSourceType type) {
         s->type = type;
         s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
 
+        e->n_sources ++;
+
         return s;
 }
 
@@ -805,6 +814,7 @@ _public_ int sd_event_add_signal(
                 sd_event_source **ret) {
 
         sd_event_source *s;
+        sigset_t ss;
         int r;
 
         assert_return(e, -EINVAL);
@@ -815,6 +825,13 @@ _public_ int sd_event_add_signal(
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         assert_return(!event_pid_changed(e), -ECHILD);
 
+        r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
+        if (r < 0)
+                return -errno;
+
+        if (!sigismember(&ss, sig))
+                return -EBUSY;
+
         if (!e->signal_sources) {
                 e->signal_sources = new0(sd_event_source*, _NSIG);
                 if (!e->signal_sources)
@@ -1561,12 +1578,21 @@ static int event_arm_timer(
         return 0;
 }
 
-static int process_io(sd_event *e, sd_event_source *s, uint32_t events) {
+static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
         assert(e);
         assert(s);
         assert(s->type == SOURCE_IO);
 
-        s->io.revents = events;
+        /* If the event source was already pending, we just OR in the
+         * new revents, otherwise we reset the value. The ORing is
+         * necessary to handle EPOLLONESHOT events properly where
+         * readability might happen independently of writability, and
+         * we need to keep track of both */
+
+        if (s->pending)
+                s->io.revents |= revents;
+        else
+                s->io.revents = revents;
 
         return source_set_pending(s, true);
 }
@@ -1588,7 +1614,7 @@ static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
                 return -errno;
         }
 
-        if (ss != sizeof(x))
+        if (_unlikely_(ss != sizeof(x)))
                 return -EIO;
 
         if (next)
@@ -1716,7 +1742,7 @@ static int process_signal(sd_event *e, uint32_t events) {
                         return -errno;
                 }
 
-                if (ss != sizeof(si))
+                if (_unlikely_(ss != sizeof(si)))
                         return -EIO;
 
                 read_one = true;
@@ -1932,7 +1958,8 @@ static int process_watchdog(sd_event *e) {
 }
 
 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
-        struct epoll_event ev_queue[EPOLL_QUEUE_MAX];
+        struct epoll_event *ev_queue;
+        unsigned ev_queue_max;
         sd_event_source *p;
         int r, i, m;
 
@@ -1962,11 +1989,13 @@ _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
 
         if (event_next_pending(e) || e->need_process_child)
                 timeout = 0;
+        ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
+        ev_queue = newa(struct epoll_event, ev_queue_max);
 
-        m = epoll_wait(e->epoll_fd, ev_queue, EPOLL_QUEUE_MAX,
+        m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
                        timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
         if (m < 0) {
-                r = errno == EAGAIN || errno == EINTR ? 0 : -errno;
+                r = errno == EAGAIN || errno == EINTR ? 1 : -errno;
                 goto finish;
         }
 
@@ -2009,7 +2038,7 @@ _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
 
         p = event_next_pending(e);
         if (!p) {
-                r = 0;
+                r = 1;
                 goto finish;
         }
 
@@ -2096,7 +2125,7 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) {
 
 _public_ int sd_event_default(sd_event **ret) {
 
-        static __thread sd_event *default_event = NULL;
+        static thread_local sd_event *default_event = NULL;
         sd_event *e;
         int r;
 
@@ -2144,17 +2173,10 @@ _public_ int sd_event_set_watchdog(sd_event *e, int b) {
 
         if (b) {
                 struct epoll_event ev = {};
-                const char *env;
 
-                env = getenv("WATCHDOG_USEC");
-                if (!env)
-                        return false;
-
-                r = safe_atou64(env, &e->watchdog_period);
-                if (r < 0)
+                r = sd_watchdog_enabled(false, &e->watchdog_period);
+                if (r <= 0)
                         return r;
-                if (e->watchdog_period <= 0)
-                        return -EIO;
 
                 /* Issue first ping immediately */
                 sd_notify(false, "WATCHDOG=1");