HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-event/sd-event.c

181,814 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 231ed955b540a6e71c82e47bc4d98bc301ac14ac

Lines 1–1000 of 5,529

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <linux/magic.h>
#include <malloc.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include <threads.h>
#include <unistd.h>
 
#include "sd-daemon.h"
#include "sd-event.h"
#include "sd-future.h"
#include "sd-id128.h"
#include "sd-messages.h"
 
#include "alloc-util.h"
#include "errno-util.h"
#include "event-future.h"
#include "event-source.h"
#include "event-util.h"
#include "fd-util.h"
#include "format-util.h"
#include "glyph-util.h"
#include "hashmap.h"
#include "hexdecoct.h"
#include "list.h"
#include "log.h"
#include "logarithm.h"
#include "memory-util.h"
#include "origin-id.h"
#include "path-util.h"
#include "pidfd-util.h"
#include "prioq.h"
#include "process-util.h"
#include "psi-util.h"
#include "set.h"
#include "signal-util.h"
#include "siphash24.h"
#include "socket-util.h"
#include "stat-util.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "strxcpyx.h"
#include "time-util.h"
 
static bool EVENT_SOURCE_WATCH_PIDFD(const sd_event_source *s) {
        /* Returns true if this is a PID event source and can be implemented by watching EPOLLIN */
        return s &&
                s->type == SOURCE_CHILD &&
                (s->child.options & ~WNOWAIT) == WEXITED;
}
 
static bool event_source_is_online(sd_event_source *s) {
        assert(s);
        return s->enabled != SD_EVENT_OFF && !s->ratelimited;
}
 
static bool event_source_is_offline(sd_event_source *s) {
        assert(s);
        return s->enabled == SD_EVENT_OFF || s->ratelimited;
}
 
static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = {
        [SOURCE_IO]                  = "io",
        [SOURCE_TIME_REALTIME]       = "realtime",
        [SOURCE_TIME_BOOTTIME]       = "boottime",
        [SOURCE_TIME_MONOTONIC]      = "monotonic",
        [SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm",
        [SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm",
        [SOURCE_SIGNAL]              = "signal",
        [SOURCE_CHILD]               = "child",
        [SOURCE_DEFER]               = "defer",
        [SOURCE_POST]                = "post",
        [SOURCE_EXIT]                = "exit",
        [SOURCE_WATCHDOG]            = "watchdog",
        [SOURCE_INOTIFY]             = "inotify",
        [SOURCE_MEMORY_PRESSURE]     = "memory-pressure",
        [SOURCE_CPU_PRESSURE]        = "cpu-pressure",
        [SOURCE_IO_PRESSURE]         = "io-pressure",
};
 
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int);
 
#define EVENT_SOURCE_IS_TIME(t)                 \
        IN_SET((t),                             \
               SOURCE_TIME_REALTIME,            \
               SOURCE_TIME_BOOTTIME,            \
               SOURCE_TIME_MONOTONIC,           \
               SOURCE_TIME_REALTIME_ALARM,      \
               SOURCE_TIME_BOOTTIME_ALARM)
 
#define EVENT_SOURCE_CAN_RATE_LIMIT(t)          \
        IN_SET((t),                             \
               SOURCE_IO,                       \
               SOURCE_TIME_REALTIME,            \
               SOURCE_TIME_BOOTTIME,            \
               SOURCE_TIME_MONOTONIC,           \
               SOURCE_TIME_REALTIME_ALARM,      \
               SOURCE_TIME_BOOTTIME_ALARM,      \
               SOURCE_SIGNAL,                   \
               SOURCE_DEFER,                    \
               SOURCE_INOTIFY,                  \
               SOURCE_MEMORY_PRESSURE,          \
               SOURCE_CPU_PRESSURE,             \
               SOURCE_IO_PRESSURE)
 
/* This is used to assert that we didn't pass an unexpected source type to event_source_time_prioq_put().
 * Time sources and ratelimited sources can be passed, so effectively this is the same as the
 * EVENT_SOURCE_CAN_RATE_LIMIT() macro. */
#define EVENT_SOURCE_USES_TIME_PRIOQ(t) EVENT_SOURCE_CAN_RATE_LIMIT(t)
 
struct sd_event {
        unsigned n_ref;
 
        int epoll_fd;
        int watchdog_fd;
 
        Prioq *pending;
        Prioq *prepare;
 
        /* timerfd_create() only supports these five clocks so far. We
         * can add support for more clocks when the kernel learns to
         * deal with them, too. */
        struct clock_data realtime;
        struct clock_data boottime;
        struct clock_data monotonic;
        struct clock_data realtime_alarm;
        struct clock_data boottime_alarm;
 
        usec_t perturb;
 
        sd_event_source **signal_sources; /* indexed by signal number */
        Hashmap *signal_data; /* indexed by priority */
 
        Hashmap *child_sources;
        unsigned n_online_child_sources;
 
        Set *post_sources;
 
        Prioq *exit;
 
        Hashmap *inotify_data; /* indexed by priority */
 
        /* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */
        LIST_HEAD(InodeData, inode_data_to_close_list);
 
        /* A list of inotify objects that already have events buffered which aren't processed yet */
        LIST_HEAD(InotifyData, buffered_inotify_data_list);
 
        /* A list of pressure event sources that still need their subscription string written */
        LIST_HEAD(sd_event_source, pressure_write_list);
 
        uint64_t origin_id;
 
        uint64_t iteration;
        triple_timestamp timestamp;
        int state;
 
        bool exit_requested:1;
        bool need_process_child:1;
        bool watchdog:1;
        bool profile_delays:1;
        bool exit_on_idle:1;
        bool ratelimit_expired:1;
 
        int exit_code;
 
        pid_t tid;
        sd_event **default_event_ptr;
 
        usec_t watchdog_last, watchdog_period;
 
        unsigned n_sources;
 
        struct epoll_event *event_queue;
 
        LIST_HEAD(sd_event_source, sources);
 
        sd_event_source *sigint_event_source, *sigterm_event_source;
 
        usec_t last_run_usec, last_log_usec;
        unsigned delays[sizeof(usec_t) * 8];
};
 
DEFINE_PRIVATE_ORIGIN_ID_HELPERS(sd_event, event);
 
static thread_local sd_event *default_event = NULL;
 
static void source_disconnect(sd_event_source *s);
static void event_gc_inode_data(sd_event *e, InodeData *d);
 
static sd_event* event_resolve(sd_event *e) {
        return e == SD_EVENT_DEFAULT ? default_event : e;
}
 
static int pending_prioq_compare(const void *a, const void *b) {
        const sd_event_source *x = a, *y = b;
        int r;
 
        assert(x->pending);
        assert(y->pending);
 
        /* Enabled ones first */
        r = CMP(x->enabled == SD_EVENT_OFF, y->enabled == SD_EVENT_OFF);
        if (r != 0)
                return r;
 
        /* Non rate-limited ones first. */
        r = CMP(!!x->ratelimited, !!y->ratelimited);
        if (r != 0)
                return r;
 
        /* Lower priority values first */
        r = CMP(x->priority, y->priority);
        if (r != 0)
                return r;
 
        /* Older entries first */
        return CMP(x->pending_iteration, y->pending_iteration);
}
 
static int prepare_prioq_compare(const void *a, const void *b) {
        const sd_event_source *x = a, *y = b;
        int r;
 
        assert(x->prepare);
        assert(y->prepare);
 
        /* Enabled ones first */
        r = CMP(x->enabled == SD_EVENT_OFF, y->enabled == SD_EVENT_OFF);
        if (r != 0)
                return r;
 
        /* Non rate-limited ones first. */
        r = CMP(!!x->ratelimited, !!y->ratelimited);
        if (r != 0)
                return r;
 
        /* Move most recently prepared ones last, so that we can stop
         * preparing as soon as we hit one that has already been
         * prepared in the current iteration */
        r = CMP(x->prepare_iteration, y->prepare_iteration);
        if (r != 0)
                return r;
 
        /* Lower priority values first */
        return CMP(x->priority, y->priority);
}
 
static usec_t time_event_source_next(const sd_event_source *s) {
        assert(s);
 
        /* We have two kinds of event sources that have elapsation times associated with them: the actual
         * time based ones and the ones for which a ratelimit can be in effect (where we want to be notified
         * once the ratelimit time window ends). Let's return the next elapsing time depending on what we are
         * looking at here. */
 
        if (s->ratelimited) { /* If rate-limited the next elapsation is when the ratelimit time window ends */
                assert(s->rate_limit.begin != 0);
                assert(s->rate_limit.interval != 0);
                return usec_add(s->rate_limit.begin, s->rate_limit.interval);
        }
 
        /* Otherwise this must be a time event source, if not ratelimited */
        if (EVENT_SOURCE_IS_TIME(s->type))
                return s->time.next;
 
        return USEC_INFINITY;
}
 
static usec_t time_event_source_latest(const sd_event_source *s) {
        assert(s);
 
        if (s->ratelimited) { /* For ratelimited stuff the earliest and the latest time shall actually be the
                               * same, as we should avoid adding additional inaccuracy on an inaccuracy time
                               * window */
                assert(s->rate_limit.begin != 0);
                assert(s->rate_limit.interval != 0);
                return usec_add(s->rate_limit.begin, s->rate_limit.interval);
        }
 
        /* Must be a time event source, if not ratelimited */
        if (EVENT_SOURCE_IS_TIME(s->type))
                return usec_add(s->time.next, s->time.accuracy);
 
        return USEC_INFINITY;
}
 
static bool event_source_timer_candidate(const sd_event_source *s) {
        assert(s);
 
        /* Returns true for event sources that either are not pending yet (i.e. where it's worth to mark them pending)
         * or which are currently ratelimited (i.e. where it's worth leaving the ratelimited state) */
        return !s->pending || s->ratelimited;
}
 
static int time_prioq_compare(const void *a, const void *b, usec_t (*time_func)(const sd_event_source *s)) {
        const sd_event_source *x = a, *y = b;
        int r;
 
        /* Enabled ones first */
        r = CMP(x->enabled == SD_EVENT_OFF, y->enabled == SD_EVENT_OFF);
        if (r != 0)
                return r;
 
        /* Order "non-pending OR ratelimited" before "pending AND not-ratelimited" */
        r = CMP(!event_source_timer_candidate(x), !event_source_timer_candidate(y));
        if (r != 0)
                return r;
 
        /* Order by time */
        return CMP(time_func(x), time_func(y));
}
 
static int earliest_time_prioq_compare(const void *a, const void *b) {
        return time_prioq_compare(a, b, time_event_source_next);
}
 
static int latest_time_prioq_compare(const void *a, const void *b) {
        return time_prioq_compare(a, b, time_event_source_latest);
}
 
static int exit_prioq_compare(const void *a, const void *b) {
        const sd_event_source *x = a, *y = b;
        int r;
 
        assert(x->type == SOURCE_EXIT);
        assert(y->type == SOURCE_EXIT);
 
        /* Enabled ones first */
        r = CMP(x->enabled == SD_EVENT_OFF, y->enabled == SD_EVENT_OFF);
        if (r != 0)
                return r;
 
        /* Lower priority values first */
        return CMP(x->priority, y->priority);
}
 
static void free_clock_data(struct clock_data *d) {
        assert(d);
        assert(d->wakeup == WAKEUP_CLOCK_DATA);
 
        safe_close(d->fd);
        prioq_free(d->earliest);
        prioq_free(d->latest);
}
 
static sd_event* event_free(sd_event *e) {
        sd_event_source *s;
 
        assert(e);
 
        e->sigterm_event_source = sd_event_source_unref(e->sigterm_event_source);
        e->sigint_event_source = sd_event_source_unref(e->sigint_event_source);
 
        while ((s = e->sources)) {
                assert(s->floating);
                source_disconnect(s);
                sd_event_source_unref(s);
        }
 
        assert(e->n_sources == 0);
 
        if (e->default_event_ptr)
                *(e->default_event_ptr) = NULL;
 
        safe_close(e->epoll_fd);
        safe_close(e->watchdog_fd);
 
        free_clock_data(&e->realtime);
        free_clock_data(&e->boottime);
        free_clock_data(&e->monotonic);
        free_clock_data(&e->realtime_alarm);
        free_clock_data(&e->boottime_alarm);
 
        prioq_free(e->pending);
        prioq_free(e->prepare);
        prioq_free(e->exit);
 
        free(e->signal_sources);
        hashmap_free(e->signal_data);
 
        hashmap_free(e->inotify_data);
 
        hashmap_free(e->child_sources);
        set_free(e->post_sources);
 
        free(e->event_queue);
 
        return mfree(e);
}
 
_public_ int sd_event_new(sd_event** ret) {
        sd_event *e;
        int r;
 
        assert_return(ret, -EINVAL);
 
        e = new(sd_event, 1);
        if (!e)
                return -ENOMEM;
 
        *e = (sd_event) {
                .n_ref = 1,
                .epoll_fd = -EBADF,
                .watchdog_fd = -EBADF,
                .realtime.wakeup = WAKEUP_CLOCK_DATA,
                .realtime.fd = -EBADF,
                .realtime.next = USEC_INFINITY,
                .boottime.wakeup = WAKEUP_CLOCK_DATA,
                .boottime.fd = -EBADF,
                .boottime.next = USEC_INFINITY,
                .monotonic.wakeup = WAKEUP_CLOCK_DATA,
                .monotonic.fd = -EBADF,
                .monotonic.next = USEC_INFINITY,
                .realtime_alarm.wakeup = WAKEUP_CLOCK_DATA,
                .realtime_alarm.fd = -EBADF,
                .realtime_alarm.next = USEC_INFINITY,
                .boottime_alarm.wakeup = WAKEUP_CLOCK_DATA,
                .boottime_alarm.fd = -EBADF,
                .boottime_alarm.next = USEC_INFINITY,
                .perturb = USEC_INFINITY,
                .origin_id = origin_id_query(),
        };
 
        r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
        if (r < 0)
                goto fail;
 
        e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
        if (e->epoll_fd < 0) {
                r = -errno;
                goto fail;
        }
 
        e->epoll_fd = fd_move_above_stdio(e->epoll_fd);
 
        if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
                log_debug("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 %s 2^63 us will be logged every 5s.",
                          glyph(GLYPH_ELLIPSIS));
                e->profile_delays = true;
        }
 
        *ret = e;
        return 0;
 
fail:
        event_free(e);
        return r;
}
 
/* Define manually so we can add the origin check */
_public_ sd_event* sd_event_ref(sd_event *e) {
        if (!e)
                return NULL;
        if (event_origin_changed(e))
                return NULL;
 
        e->n_ref++;
 
        return e;
}
 
_public_ sd_event* sd_event_unref(sd_event *e) {
        if (!e)
                return NULL;
        if (event_origin_changed(e))
                return NULL;
 
        assert(e->n_ref > 0);
        if (--e->n_ref > 0)
                return NULL;
 
        return event_free(e);
}
 
_public_ sd_event_source* sd_event_source_disable_unref(sd_event_source *s) {
        int r;
 
        r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
        if (r < 0)
                log_debug_errno(r, "Failed to disable event source %p (%s): %m",
                                s, strna(s->description));
 
        return sd_event_source_unref(s);
}
 
static void source_io_unregister(sd_event_source *s) {
        assert(s);
        assert(s->type == SOURCE_IO);
 
        if (event_origin_changed(s->event))
                return;
 
        if (!s->io.registered)
                return;
 
        if (epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL) < 0)
                log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll, ignoring: %m",
                                strna(s->description), event_source_type_to_string(s->type));
 
        s->io.registered = false;
}
 
static int source_io_register(
                sd_event_source *s,
                int enabled,
                uint32_t events) {
 
        assert(s);
        assert(s->type == SOURCE_IO);
        assert(enabled != SD_EVENT_OFF);
 
        struct epoll_event ev = {
                .events = events | (enabled == SD_EVENT_ONESHOT ? EPOLLONESHOT : 0),
                .data.ptr = s,
        };
 
        if (epoll_ctl(s->event->epoll_fd,
                      s->io.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD,
                      s->io.fd, &ev) < 0)
                return -errno;
 
        s->io.registered = true;
 
        return 0;
}
 
static void source_child_pidfd_unregister(sd_event_source *s) {
        assert(s);
        assert(s->type == SOURCE_CHILD);
 
        if (event_origin_changed(s->event))
                return;
 
        if (!s->child.registered)
                return;
 
        if (EVENT_SOURCE_WATCH_PIDFD(s))
                if (epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->child.pidfd, NULL) < 0)
                        log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll, ignoring: %m",
                                        strna(s->description), event_source_type_to_string(s->type));
 
        s->child.registered = false;
}
 
static int source_child_pidfd_register(sd_event_source *s, int enabled) {
        assert(s);
        assert(s->type == SOURCE_CHILD);
        assert(enabled != SD_EVENT_OFF);
 
        if (EVENT_SOURCE_WATCH_PIDFD(s)) {
                struct epoll_event ev = {
                        .events = EPOLLIN | (enabled == SD_EVENT_ONESHOT ? EPOLLONESHOT : 0),
                        .data.ptr = s,
                };
 
                if (epoll_ctl(s->event->epoll_fd,
                              s->child.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD,
                              s->child.pidfd, &ev) < 0)
                        return -errno;
        }
 
        s->child.registered = true;
        return 0;
}
 
#define EVENT_SOURCE_IS_PRESSURE(s) IN_SET((s)->type, SOURCE_MEMORY_PRESSURE, SOURCE_CPU_PRESSURE, SOURCE_IO_PRESSURE)
 
static void source_pressure_unregister(sd_event_source *s) {
        assert(s);
        assert(EVENT_SOURCE_IS_PRESSURE(s));
 
        if (event_origin_changed(s->event))
                return;
 
        if (!s->pressure.registered)
                return;
 
        if (epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->pressure.fd, NULL) < 0)
                log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll, ignoring: %m",
                                strna(s->description), event_source_type_to_string(s->type));
 
        s->pressure.registered = false;
}
 
static int source_pressure_register(sd_event_source *s, int enabled) {
        assert(s);
        assert(EVENT_SOURCE_IS_PRESSURE(s));
        assert(enabled != SD_EVENT_OFF);
 
        struct epoll_event ev = {
                .events = s->pressure.write_buffer_size > 0 ? EPOLLOUT :
                          (s->pressure.events | (enabled == SD_EVENT_ONESHOT ? EPOLLONESHOT : 0)),
                .data.ptr = s,
        };
 
        if (epoll_ctl(s->event->epoll_fd,
                      s->pressure.registered ? EPOLL_CTL_MOD : EPOLL_CTL_ADD,
                      s->pressure.fd, &ev) < 0)
                return -errno;
 
        s->pressure.registered = true;
        return 0;
}
 
static void source_pressure_add_to_write_list(sd_event_source *s) {
        assert(s);
        assert(EVENT_SOURCE_IS_PRESSURE(s));
 
        if (s->pressure.in_write_list)
                return;
 
        LIST_PREPEND(pressure.write_list, s->event->pressure_write_list, s);
        s->pressure.in_write_list = true;
}
 
static void source_pressure_remove_from_write_list(sd_event_source *s) {
        assert(s);
        assert(EVENT_SOURCE_IS_PRESSURE(s));
 
        if (!s->pressure.in_write_list)
                return;
 
        LIST_REMOVE(pressure.write_list, s->event->pressure_write_list, s);
        s->pressure.in_write_list = false;
}
 
static clockid_t event_source_type_to_clock(EventSourceType t) {
 
        switch (t) {
 
        case SOURCE_TIME_REALTIME:
                return CLOCK_REALTIME;
 
        case SOURCE_TIME_BOOTTIME:
                return CLOCK_BOOTTIME;
 
        case SOURCE_TIME_MONOTONIC:
                return CLOCK_MONOTONIC;
 
        case SOURCE_TIME_REALTIME_ALARM:
                return CLOCK_REALTIME_ALARM;
 
        case SOURCE_TIME_BOOTTIME_ALARM:
                return CLOCK_BOOTTIME_ALARM;
 
        default:
                return (clockid_t) -1;
        }
}
 
static EventSourceType clock_to_event_source_type(clockid_t clock) {
 
        switch (clock) {
 
        case CLOCK_REALTIME:
                return SOURCE_TIME_REALTIME;
 
        case CLOCK_BOOTTIME:
                return SOURCE_TIME_BOOTTIME;
 
        case CLOCK_MONOTONIC:
                return SOURCE_TIME_MONOTONIC;
 
        case CLOCK_REALTIME_ALARM:
                return SOURCE_TIME_REALTIME_ALARM;
 
        case CLOCK_BOOTTIME_ALARM:
                return SOURCE_TIME_BOOTTIME_ALARM;
 
        default:
                return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
        }
}
 
static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
        assert(e);
 
        switch (t) {
 
        case SOURCE_TIME_REALTIME:
                return &e->realtime;
 
        case SOURCE_TIME_BOOTTIME:
                return &e->boottime;
 
        case SOURCE_TIME_MONOTONIC:
                return &e->monotonic;
 
        case SOURCE_TIME_REALTIME_ALARM:
                return &e->realtime_alarm;
 
        case SOURCE_TIME_BOOTTIME_ALARM:
                return &e->boottime_alarm;
 
        default:
                return NULL;
        }
}
 
static void event_free_signal_data(sd_event *e, struct signal_data *d) {
        assert(e);
 
        if (!d)
                return;
 
        hashmap_remove(e->signal_data, &d->priority);
        safe_close(d->fd);
        free(d);
}
 
static int event_make_signal_data(
                sd_event *e,
                int sig,
                struct signal_data **ret) {
 
        struct signal_data *d;
        bool added = false;
        sigset_t ss_copy;
        int64_t priority;
        int r;
 
        assert(e);
 
        if (event_origin_changed(e))
                return -ECHILD;
 
        if (e->signal_sources && e->signal_sources[sig])
                priority = e->signal_sources[sig]->priority;
        else
                priority = SD_EVENT_PRIORITY_NORMAL;
 
        d = hashmap_get(e->signal_data, &priority);
        if (d) {
                if (sigismember(&d->sigset, sig) > 0) {
                        if (ret)
                                *ret = d;
                        return 0;
                }
        } else {
                d = new(struct signal_data, 1);
                if (!d)
                        return -ENOMEM;
 
                *d = (struct signal_data) {
                        .wakeup = WAKEUP_SIGNAL_DATA,
                        .fd = -EBADF,
                        .priority = priority,
                };
 
                r = hashmap_ensure_put(&e->signal_data, &uint64_hash_ops, &d->priority, d);
                if (r < 0) {
                        free(d);
                        return r;
                }
 
                added = true;
        }
 
        ss_copy = d->sigset;
        assert_se(sigaddset(&ss_copy, sig) >= 0);
 
        r = signalfd(d->fd >= 0 ? d->fd : -1,   /* the first arg must be -1 or a valid signalfd */
                     &ss_copy,
                     SFD_NONBLOCK|SFD_CLOEXEC);
        if (r < 0) {
                r = -errno;
                goto fail;
        }
 
        d->sigset = ss_copy;
 
        if (d->fd >= 0) {
                if (ret)
                        *ret = d;
                return 0;
        }
 
        d->fd = fd_move_above_stdio(r);
 
        struct epoll_event ev = {
                .events = EPOLLIN,
                .data.ptr = d,
        };
 
        if (epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev) < 0) {
                r = -errno;
                goto fail;
        }
 
        if (ret)
                *ret = d;
 
        return 0;
 
fail:
        if (added)
                event_free_signal_data(e, d);
 
        return r;
}
 
static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
        assert(e);
        assert(d);
 
        /* Turns off the specified signal in the signal data
         * object. If the signal mask of the object becomes empty that
         * way removes it. */
 
        if (sigismember(&d->sigset, sig) == 0)
                return;
 
        assert_se(sigdelset(&d->sigset, sig) >= 0);
 
        if (sigisemptyset(&d->sigset)) {
                /* If all the mask is all-zero we can get rid of the structure */
                event_free_signal_data(e, d);
                return;
        }
 
        if (event_origin_changed(e))
                return;
 
        assert(d->fd >= 0);
 
        if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
                log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
}
 
static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
        struct signal_data *d;
        static const int64_t zero_priority = 0;
 
        assert(e);
 
        /* Rechecks if the specified signal is still something we are interested in. If not, we'll unmask it,
         * and possibly drop the signalfd for it. */
 
        if (sig == SIGCHLD &&
            e->n_online_child_sources > 0)
                return;
 
        if (e->signal_sources &&
            e->signal_sources[sig] &&
            event_source_is_online(e->signal_sources[sig]))
                return;
 
        /*
         * The specified signal might be enabled in three different queues:
         *
         * 1) the one that belongs to the priority passed (if it is non-NULL)
         * 2) the one that belongs to the priority of the event source of the signal (if there is one)
         * 3) the 0 priority (to cover the SIGCHLD case)
         *
         * Hence, let's remove it from all three here.
         */
 
        if (priority) {
                d = hashmap_get(e->signal_data, priority);
                if (d)
                        event_unmask_signal_data(e, d, sig);
        }
 
        if (e->signal_sources && e->signal_sources[sig]) {
                d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
                if (d)
                        event_unmask_signal_data(e, d, sig);
        }
 
        d = hashmap_get(e->signal_data, &zero_priority);
        if (d)
                event_unmask_signal_data(e, d, sig);
}
 
static void event_source_pp_prioq_reshuffle(sd_event_source *s) {
        assert(s);
 
        /* Reshuffles the pending + prepare prioqs. Called whenever the dispatch order changes, i.e. when
         * they are enabled/disabled or marked pending and such. */
 
        if (s->pending)
                prioq_reshuffle(s->event->pending, s, &s->pending_index);
 
        if (s->prepare)
                prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
}
 
static void event_source_time_prioq_reshuffle(sd_event_source *s) {
        struct clock_data *d;
 
        assert(s);
 
        /* Called whenever the event source's timer ordering properties changed, i.e. time, accuracy,
         * pending, enable state, and ratelimiting state. Makes sure the two prioq's are ordered
         * properly again. */
 
        if (s->ratelimited)
                d = &s->event->boottime;
        else if (EVENT_SOURCE_IS_TIME(s->type))
                assert_se(d = event_get_clock_data(s->event, s->type));
        else
                return; /* no-op for an event source which is neither a timer nor ratelimited. */
 
        prioq_reshuffle(d->earliest, s, &s->earliest_index);
        prioq_reshuffle(d->latest, s, &s->latest_index);
        d->needs_rearm = true;
}
 
static void event_source_time_prioq_remove(
                sd_event_source *s,
                struct clock_data *d) {
 
        assert(s);
        assert(d);
 
        prioq_remove(d->earliest, s, &s->earliest_index);
        prioq_remove(d->latest, s, &s->latest_index);
        s->earliest_index = s->latest_index = PRIOQ_IDX_NULL;
        d->needs_rearm = true;
}
 
static void source_disconnect(sd_event_source *s) {
        sd_event *event;
        int r;
 
        assert(s);
 
        if (!s->event)
                return;
 
        assert(s->event->n_sources > 0);
 
        switch (s->type) {
 
        case SOURCE_IO:
                if (s->io.fd >= 0)
                        source_io_unregister(s);
 
                break;
 
        case SOURCE_TIME_REALTIME:
        case SOURCE_TIME_BOOTTIME:
        case SOURCE_TIME_MONOTONIC:
        case SOURCE_TIME_REALTIME_ALARM:
        case SOURCE_TIME_BOOTTIME_ALARM:
                /* Only remove this event source from the time event source here if it is not ratelimited. If
                 * it is ratelimited, we'll remove it below, separately. Why? Because the clock used might
                 * differ: ratelimiting always uses CLOCK_BOOTTIME, but timer events might use any clock */
 
                if (!s->ratelimited) {
                        struct clock_data *d;
                        assert_se(d = event_get_clock_data(s->event, s->type));
                        event_source_time_prioq_remove(s, d);
                }
 
                break;
 
        case SOURCE_SIGNAL:
                if (s->signal.sig > 0) {
 
                        if (s->event->signal_sources)
                                s->event->signal_sources[s->signal.sig] = NULL;
 
                        event_gc_signal_data(s->event, &s->priority, s->signal.sig);
 
                        if (s->signal.unblock) {
                                sigset_t new_ss;
 
                                if (sigemptyset(&new_ss) < 0)
                                        log_debug_errno(errno, "Failed to reset signal set, ignoring: %m");
                                else if (sigaddset(&new_ss, s->signal.sig) < 0)
                                        log_debug_errno(errno, "Failed to add signal %i to signal mask, ignoring: %m", s->signal.sig);
                                else {
                                        r = pthread_sigmask(SIG_UNBLOCK, &new_ss, NULL);
                                        if (r != 0)
                                                log_debug_errno(r, "Failed to unblock signal %i, ignoring: %m", s->signal.sig);
                                }
                        }
                }
 
                break;
 
        case SOURCE_CHILD:
                if (event_origin_changed(s->event))
                        s->child.process_owned = false;
 
                if (s->child.pid > 0) {
                        if (event_source_is_online(s)) {
                                assert(s->event->n_online_child_sources > 0);
                                s->event->n_online_child_sources--;
                        }
 
                        assert_se(hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid)));
                }
 
                if (EVENT_SOURCE_WATCH_PIDFD(s))