1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
26 #include "sd-daemon.h"
30 #include "alloc-util.h"
37 #include "process-util.h"
39 #include "signal-util.h"
40 #include "string-table.h"
41 #include "string-util.h"
42 #include "time-util.h"
45 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
47 typedef enum EventSourceType {
51 SOURCE_TIME_MONOTONIC,
52 SOURCE_TIME_REALTIME_ALARM,
53 SOURCE_TIME_BOOTTIME_ALARM,
60 _SOURCE_EVENT_SOURCE_TYPE_MAX,
61 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
64 static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = {
66 [SOURCE_TIME_REALTIME] = "realtime",
67 [SOURCE_TIME_BOOTTIME] = "bootime",
68 [SOURCE_TIME_MONOTONIC] = "monotonic",
69 [SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm",
70 [SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm",
71 [SOURCE_SIGNAL] = "signal",
72 [SOURCE_CHILD] = "child",
73 [SOURCE_DEFER] = "defer",
74 [SOURCE_POST] = "post",
75 [SOURCE_EXIT] = "exit",
76 [SOURCE_WATCHDOG] = "watchdog",
79 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int);
81 /* All objects we use in epoll events start with this value, so that
82 * we know how to dispatch it */
83 typedef enum WakeupType {
89 _WAKEUP_TYPE_INVALID = -1,
92 #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)
94 struct sd_event_source {
101 sd_event_handler_t prepare;
105 EventSourceType type:5;
112 unsigned pending_index;
113 unsigned prepare_index;
114 unsigned pending_iteration;
115 unsigned prepare_iteration;
117 LIST_FIELDS(sd_event_source, sources);
121 sd_event_io_handler_t callback;
128 sd_event_time_handler_t callback;
129 usec_t next, accuracy;
130 unsigned earliest_index;
131 unsigned latest_index;
134 sd_event_signal_handler_t callback;
135 struct signalfd_siginfo siginfo;
139 sd_event_child_handler_t callback;
145 sd_event_handler_t callback;
148 sd_event_handler_t callback;
151 sd_event_handler_t callback;
152 unsigned prioq_index;
161 /* For all clocks we maintain two priority queues each, one
162 * ordered for the earliest times the events may be
163 * dispatched, and one ordered by the latest times they must
164 * have been dispatched. The range between the top entries in
165 * the two prioqs is the time window we can freely schedule
178 /* For each priority we maintain one signal fd, so that we
179 * only have to dequeue a single event per priority at a
185 sd_event_source *current;
197 /* timerfd_create() only supports these five clocks so far. We
198 * can add support for more clocks when the kernel learns to
199 * deal with them, too. */
200 struct clock_data realtime;
201 struct clock_data boottime;
202 struct clock_data monotonic;
203 struct clock_data realtime_alarm;
204 struct clock_data boottime_alarm;
208 sd_event_source **signal_sources; /* indexed by signal number */
209 Hashmap *signal_data; /* indexed by priority */
211 Hashmap *child_sources;
212 unsigned n_enabled_child_sources;
221 dual_timestamp timestamp;
222 usec_t timestamp_boottime;
225 bool exit_requested:1;
226 bool need_process_child:1;
228 bool profile_delays:1;
233 sd_event **default_event_ptr;
235 usec_t watchdog_last, watchdog_period;
239 LIST_HEAD(sd_event_source, sources);
241 usec_t last_run, last_log;
242 unsigned delays[sizeof(usec_t) * 8];
245 static void source_disconnect(sd_event_source *s);
247 static int pending_prioq_compare(const void *a, const void *b) {
248 const sd_event_source *x = a, *y = b;
253 /* Enabled ones first */
254 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
256 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
259 /* Lower priority values first */
260 if (x->priority < y->priority)
262 if (x->priority > y->priority)
265 /* Older entries first */
266 if (x->pending_iteration < y->pending_iteration)
268 if (x->pending_iteration > y->pending_iteration)
274 static int prepare_prioq_compare(const void *a, const void *b) {
275 const sd_event_source *x = a, *y = b;
280 /* Enabled ones first */
281 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
283 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
286 /* Move most recently prepared ones last, so that we can stop
287 * preparing as soon as we hit one that has already been
288 * prepared in the current iteration */
289 if (x->prepare_iteration < y->prepare_iteration)
291 if (x->prepare_iteration > y->prepare_iteration)
294 /* Lower priority values first */
295 if (x->priority < y->priority)
297 if (x->priority > y->priority)
303 static int earliest_time_prioq_compare(const void *a, const void *b) {
304 const sd_event_source *x = a, *y = b;
306 assert(EVENT_SOURCE_IS_TIME(x->type));
307 assert(x->type == y->type);
309 /* Enabled ones first */
310 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
312 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
315 /* Move the pending ones to the end */
316 if (!x->pending && y->pending)
318 if (x->pending && !y->pending)
322 if (x->time.next < y->time.next)
324 if (x->time.next > y->time.next)
330 static int latest_time_prioq_compare(const void *a, const void *b) {
331 const sd_event_source *x = a, *y = b;
333 assert(EVENT_SOURCE_IS_TIME(x->type));
334 assert(x->type == y->type);
336 /* Enabled ones first */
337 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
339 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
342 /* Move the pending ones to the end */
343 if (!x->pending && y->pending)
345 if (x->pending && !y->pending)
349 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
351 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
357 static int exit_prioq_compare(const void *a, const void *b) {
358 const sd_event_source *x = a, *y = b;
360 assert(x->type == SOURCE_EXIT);
361 assert(y->type == SOURCE_EXIT);
363 /* Enabled ones first */
364 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
366 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
369 /* Lower priority values first */
370 if (x->priority < y->priority)
372 if (x->priority > y->priority)
378 static void free_clock_data(struct clock_data *d) {
380 assert(d->wakeup == WAKEUP_CLOCK_DATA);
383 prioq_free(d->earliest);
384 prioq_free(d->latest);
387 static void event_free(sd_event *e) {
392 while ((s = e->sources)) {
394 source_disconnect(s);
395 sd_event_source_unref(s);
398 assert(e->n_sources == 0);
400 if (e->default_event_ptr)
401 *(e->default_event_ptr) = NULL;
403 safe_close(e->epoll_fd);
404 safe_close(e->watchdog_fd);
406 free_clock_data(&e->realtime);
407 free_clock_data(&e->boottime);
408 free_clock_data(&e->monotonic);
409 free_clock_data(&e->realtime_alarm);
410 free_clock_data(&e->boottime_alarm);
412 prioq_free(e->pending);
413 prioq_free(e->prepare);
416 free(e->signal_sources);
417 hashmap_free(e->signal_data);
419 hashmap_free(e->child_sources);
420 set_free(e->post_sources);
424 _public_ int sd_event_new(sd_event** ret) {
428 assert_return(ret, -EINVAL);
430 e = new0(sd_event, 1);
435 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
436 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
437 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
438 e->original_pid = getpid();
439 e->perturb = USEC_INFINITY;
441 r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
445 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
446 if (e->epoll_fd < 0) {
451 if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
452 log_info("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 ... 2^63 us will be logged every 5s.");
453 e->profile_delays = true;
464 _public_ sd_event* sd_event_ref(sd_event *e) {
465 assert_return(e, NULL);
467 assert(e->n_ref >= 1);
473 _public_ sd_event* sd_event_unref(sd_event *e) {
478 assert(e->n_ref >= 1);
487 static bool event_pid_changed(sd_event *e) {
490 /* We don't support people creating an event loop and keeping
491 * it around over a fork(). Let's complain. */
493 return e->original_pid != getpid();
496 static void source_io_unregister(sd_event_source *s) {
500 assert(s->type == SOURCE_IO);
502 if (event_pid_changed(s->event))
505 if (!s->io.registered)
508 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
510 log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll: %m",
511 strna(s->description), event_source_type_to_string(s->type));
513 s->io.registered = false;
516 static int source_io_register(
521 struct epoll_event ev = {};
525 assert(s->type == SOURCE_IO);
526 assert(enabled != SD_EVENT_OFF);
531 if (enabled == SD_EVENT_ONESHOT)
532 ev.events |= EPOLLONESHOT;
534 if (s->io.registered)
535 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
537 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
541 s->io.registered = true;
546 #if 0 /// UNNEEDED by elogind
547 static clockid_t event_source_type_to_clock(EventSourceType t) {
551 case SOURCE_TIME_REALTIME:
552 return CLOCK_REALTIME;
554 case SOURCE_TIME_BOOTTIME:
555 return CLOCK_BOOTTIME;
557 case SOURCE_TIME_MONOTONIC:
558 return CLOCK_MONOTONIC;
560 case SOURCE_TIME_REALTIME_ALARM:
561 return CLOCK_REALTIME_ALARM;
563 case SOURCE_TIME_BOOTTIME_ALARM:
564 return CLOCK_BOOTTIME_ALARM;
567 return (clockid_t) -1;
572 static EventSourceType clock_to_event_source_type(clockid_t clock) {
577 return SOURCE_TIME_REALTIME;
580 return SOURCE_TIME_BOOTTIME;
582 case CLOCK_MONOTONIC:
583 return SOURCE_TIME_MONOTONIC;
585 case CLOCK_REALTIME_ALARM:
586 return SOURCE_TIME_REALTIME_ALARM;
588 case CLOCK_BOOTTIME_ALARM:
589 return SOURCE_TIME_BOOTTIME_ALARM;
592 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
596 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
601 case SOURCE_TIME_REALTIME:
604 case SOURCE_TIME_BOOTTIME:
607 case SOURCE_TIME_MONOTONIC:
608 return &e->monotonic;
610 case SOURCE_TIME_REALTIME_ALARM:
611 return &e->realtime_alarm;
613 case SOURCE_TIME_BOOTTIME_ALARM:
614 return &e->boottime_alarm;
621 static int event_make_signal_data(
624 struct signal_data **ret) {
626 struct epoll_event ev = {};
627 struct signal_data *d;
635 if (event_pid_changed(e))
638 if (e->signal_sources && e->signal_sources[sig])
639 priority = e->signal_sources[sig]->priority;
643 d = hashmap_get(e->signal_data, &priority);
645 if (sigismember(&d->sigset, sig) > 0) {
651 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
655 d = new0(struct signal_data, 1);
659 d->wakeup = WAKEUP_SIGNAL_DATA;
661 d->priority = priority;
663 r = hashmap_put(e->signal_data, &d->priority, d);
671 assert_se(sigaddset(&ss_copy, sig) >= 0);
673 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
692 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
705 d->fd = safe_close(d->fd);
706 hashmap_remove(e->signal_data, &d->priority);
713 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
717 /* Turns off the specified signal in the signal data
718 * object. If the signal mask of the object becomes empty that
721 if (sigismember(&d->sigset, sig) == 0)
724 assert_se(sigdelset(&d->sigset, sig) >= 0);
726 if (sigisemptyset(&d->sigset)) {
728 /* If all the mask is all-zero we can get rid of the structure */
729 hashmap_remove(e->signal_data, &d->priority);
738 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
739 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
742 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
743 struct signal_data *d;
744 static const int64_t zero_priority = 0;
748 /* Rechecks if the specified signal is still something we are
749 * interested in. If not, we'll unmask it, and possibly drop
750 * the signalfd for it. */
752 if (sig == SIGCHLD &&
753 e->n_enabled_child_sources > 0)
756 if (e->signal_sources &&
757 e->signal_sources[sig] &&
758 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
762 * The specified signal might be enabled in three different queues:
764 * 1) the one that belongs to the priority passed (if it is non-NULL)
765 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
766 * 3) the 0 priority (to cover the SIGCHLD case)
768 * Hence, let's remove it from all three here.
772 d = hashmap_get(e->signal_data, priority);
774 event_unmask_signal_data(e, d, sig);
777 if (e->signal_sources && e->signal_sources[sig]) {
778 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
780 event_unmask_signal_data(e, d, sig);
783 d = hashmap_get(e->signal_data, &zero_priority);
785 event_unmask_signal_data(e, d, sig);
788 static void source_disconnect(sd_event_source *s) {
796 assert(s->event->n_sources > 0);
802 source_io_unregister(s);
806 case SOURCE_TIME_REALTIME:
807 case SOURCE_TIME_BOOTTIME:
808 case SOURCE_TIME_MONOTONIC:
809 case SOURCE_TIME_REALTIME_ALARM:
810 case SOURCE_TIME_BOOTTIME_ALARM: {
811 struct clock_data *d;
813 d = event_get_clock_data(s->event, s->type);
816 prioq_remove(d->earliest, s, &s->time.earliest_index);
817 prioq_remove(d->latest, s, &s->time.latest_index);
818 d->needs_rearm = true;
823 if (s->signal.sig > 0) {
825 if (s->event->signal_sources)
826 s->event->signal_sources[s->signal.sig] = NULL;
828 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
834 if (s->child.pid > 0) {
835 if (s->enabled != SD_EVENT_OFF) {
836 assert(s->event->n_enabled_child_sources > 0);
837 s->event->n_enabled_child_sources--;
840 (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
841 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
851 set_remove(s->event->post_sources, s);
855 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
859 assert_not_reached("Wut? I shouldn't exist.");
863 prioq_remove(s->event->pending, s, &s->pending_index);
866 prioq_remove(s->event->prepare, s, &s->prepare_index);
870 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
872 LIST_REMOVE(sources, event->sources, s);
876 sd_event_unref(event);
879 static void source_free(sd_event_source *s) {
882 source_disconnect(s);
883 free(s->description);
887 static int source_set_pending(sd_event_source *s, bool b) {
891 assert(s->type != SOURCE_EXIT);
899 s->pending_iteration = s->event->iteration;
901 r = prioq_put(s->event->pending, s, &s->pending_index);
907 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
909 if (EVENT_SOURCE_IS_TIME(s->type)) {
910 struct clock_data *d;
912 d = event_get_clock_data(s->event, s->type);
915 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
916 prioq_reshuffle(d->latest, s, &s->time.latest_index);
917 d->needs_rearm = true;
920 if (s->type == SOURCE_SIGNAL && !b) {
921 struct signal_data *d;
923 d = hashmap_get(s->event->signal_data, &s->priority);
924 if (d && d->current == s)
931 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
936 s = new0(sd_event_source, 1);
942 s->floating = floating;
944 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
949 LIST_PREPEND(sources, e->sources, s);
955 _public_ int sd_event_add_io(
957 sd_event_source **ret,
960 sd_event_io_handler_t callback,
966 assert_return(e, -EINVAL);
967 assert_return(fd >= 0, -EBADF);
968 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
969 assert_return(callback, -EINVAL);
970 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
971 assert_return(!event_pid_changed(e), -ECHILD);
973 s = source_new(e, !ret, SOURCE_IO);
977 s->wakeup = WAKEUP_EVENT_SOURCE;
979 s->io.events = events;
980 s->io.callback = callback;
981 s->userdata = userdata;
982 s->enabled = SD_EVENT_ON;
984 r = source_io_register(s, s->enabled, events);
996 static void initialize_perturb(sd_event *e) {
997 sd_id128_t bootid = {};
999 /* When we sleep for longer, we try to realign the wakeup to
1000 the same time wihtin each minute/second/250ms, so that
1001 events all across the system can be coalesced into a single
1002 CPU wakeup. However, let's take some system-specific
1003 randomness for this value, so that in a network of systems
1004 with synced clocks timer events are distributed a
1005 bit. Here, we calculate a perturbation usec offset from the
1008 if (_likely_(e->perturb != USEC_INFINITY))
1011 if (sd_id128_get_boot(&bootid) >= 0)
1012 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
1015 static int event_setup_timer_fd(
1017 struct clock_data *d,
1020 struct epoll_event ev = {};
1026 if (_likely_(d->fd >= 0))
1029 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1033 ev.events = EPOLLIN;
1036 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1046 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1049 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1052 _public_ int sd_event_add_time(
1054 sd_event_source **ret,
1058 sd_event_time_handler_t callback,
1061 EventSourceType type;
1063 struct clock_data *d;
1066 assert_return(e, -EINVAL);
1067 assert_return(usec != (uint64_t) -1, -EINVAL);
1068 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1069 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1070 assert_return(!event_pid_changed(e), -ECHILD);
1073 callback = time_exit_callback;
1075 type = clock_to_event_source_type(clock);
1076 assert_return(type >= 0, -EOPNOTSUPP);
1078 d = event_get_clock_data(e, type);
1081 r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare);
1085 r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare);
1090 r = event_setup_timer_fd(e, d, clock);
1095 s = source_new(e, !ret, type);
1099 s->time.next = usec;
1100 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1101 s->time.callback = callback;
1102 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1103 s->userdata = userdata;
1104 s->enabled = SD_EVENT_ONESHOT;
1106 d->needs_rearm = true;
1108 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1112 r = prioq_put(d->latest, s, &s->time.latest_index);
1126 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1129 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1132 _public_ int sd_event_add_signal(
1134 sd_event_source **ret,
1136 sd_event_signal_handler_t callback,
1140 struct signal_data *d;
1144 assert_return(e, -EINVAL);
1145 assert_return(sig > 0, -EINVAL);
1146 assert_return(sig < _NSIG, -EINVAL);
1147 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1148 assert_return(!event_pid_changed(e), -ECHILD);
1151 callback = signal_exit_callback;
1153 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1157 if (!sigismember(&ss, sig))
1160 if (!e->signal_sources) {
1161 e->signal_sources = new0(sd_event_source*, _NSIG);
1162 if (!e->signal_sources)
1164 } else if (e->signal_sources[sig])
1167 s = source_new(e, !ret, SOURCE_SIGNAL);
1171 s->signal.sig = sig;
1172 s->signal.callback = callback;
1173 s->userdata = userdata;
1174 s->enabled = SD_EVENT_ON;
1176 e->signal_sources[sig] = s;
1178 r = event_make_signal_data(e, sig, &d);
1184 /* Use the signal name as description for the event source by default */
1185 (void) sd_event_source_set_description(s, signal_to_string(sig));
1193 #if 0 /// UNNEEDED by elogind
1194 _public_ int sd_event_add_child(
1196 sd_event_source **ret,
1199 sd_event_child_handler_t callback,
1205 assert_return(e, -EINVAL);
1206 assert_return(pid > 1, -EINVAL);
1207 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1208 assert_return(options != 0, -EINVAL);
1209 assert_return(callback, -EINVAL);
1210 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1211 assert_return(!event_pid_changed(e), -ECHILD);
1213 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1217 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1220 s = source_new(e, !ret, SOURCE_CHILD);
1225 s->child.options = options;
1226 s->child.callback = callback;
1227 s->userdata = userdata;
1228 s->enabled = SD_EVENT_ONESHOT;
1230 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1236 e->n_enabled_child_sources ++;
1238 r = event_make_signal_data(e, SIGCHLD, NULL);
1240 e->n_enabled_child_sources--;
1245 e->need_process_child = true;
1253 _public_ int sd_event_add_defer(
1255 sd_event_source **ret,
1256 sd_event_handler_t callback,
1262 assert_return(e, -EINVAL);
1263 assert_return(callback, -EINVAL);
1264 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1265 assert_return(!event_pid_changed(e), -ECHILD);
1267 s = source_new(e, !ret, SOURCE_DEFER);
1271 s->defer.callback = callback;
1272 s->userdata = userdata;
1273 s->enabled = SD_EVENT_ONESHOT;
1275 r = source_set_pending(s, true);
1288 _public_ int sd_event_add_post(
1290 sd_event_source **ret,
1291 sd_event_handler_t callback,
1297 assert_return(e, -EINVAL);
1298 assert_return(callback, -EINVAL);
1299 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1300 assert_return(!event_pid_changed(e), -ECHILD);
1302 r = set_ensure_allocated(&e->post_sources, NULL);
1306 s = source_new(e, !ret, SOURCE_POST);
1310 s->post.callback = callback;
1311 s->userdata = userdata;
1312 s->enabled = SD_EVENT_ON;
1314 r = set_put(e->post_sources, s);
1326 _public_ int sd_event_add_exit(
1328 sd_event_source **ret,
1329 sd_event_handler_t callback,
1335 assert_return(e, -EINVAL);
1336 assert_return(callback, -EINVAL);
1337 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1338 assert_return(!event_pid_changed(e), -ECHILD);
1340 r = prioq_ensure_allocated(&e->exit, exit_prioq_compare);
1344 s = source_new(e, !ret, SOURCE_EXIT);
1348 s->exit.callback = callback;
1349 s->userdata = userdata;
1350 s->exit.prioq_index = PRIOQ_IDX_NULL;
1351 s->enabled = SD_EVENT_ONESHOT;
1353 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1365 #if 0 /// UNNEEDED by elogind
1366 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1367 assert_return(s, NULL);
1369 assert(s->n_ref >= 1);
1376 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1381 assert(s->n_ref >= 1);
1384 if (s->n_ref <= 0) {
1385 /* Here's a special hack: when we are called from a
1386 * dispatch handler we won't free the event source
1387 * immediately, but we will detach the fd from the
1388 * epoll. This way it is safe for the caller to unref
1389 * the event source and immediately close the fd, but
1390 * we still retain a valid event source object after
1393 if (s->dispatching) {
1394 if (s->type == SOURCE_IO)
1395 source_io_unregister(s);
1397 source_disconnect(s);
1405 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1406 assert_return(s, -EINVAL);
1407 assert_return(!event_pid_changed(s->event), -ECHILD);
1409 return free_and_strdup(&s->description, description);
1412 #if 0 /// UNNEEDED by elogind
1413 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1414 assert_return(s, -EINVAL);
1415 assert_return(description, -EINVAL);
1416 assert_return(s->description, -ENXIO);
1417 assert_return(!event_pid_changed(s->event), -ECHILD);
1419 *description = s->description;
1424 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1425 assert_return(s, NULL);
1430 #if 0 /// UNNEEDED by elogind
1431 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1432 assert_return(s, -EINVAL);
1433 assert_return(s->type != SOURCE_EXIT, -EDOM);
1434 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1435 assert_return(!event_pid_changed(s->event), -ECHILD);
1440 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1441 assert_return(s, -EINVAL);
1442 assert_return(s->type == SOURCE_IO, -EDOM);
1443 assert_return(!event_pid_changed(s->event), -ECHILD);
1449 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1452 assert_return(s, -EINVAL);
1453 assert_return(fd >= 0, -EBADF);
1454 assert_return(s->type == SOURCE_IO, -EDOM);
1455 assert_return(!event_pid_changed(s->event), -ECHILD);
1460 if (s->enabled == SD_EVENT_OFF) {
1462 s->io.registered = false;
1466 saved_fd = s->io.fd;
1467 assert(s->io.registered);
1470 s->io.registered = false;
1472 r = source_io_register(s, s->enabled, s->io.events);
1474 s->io.fd = saved_fd;
1475 s->io.registered = true;
1479 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1485 #if 0 /// UNNEEDED by elogind
1486 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1487 assert_return(s, -EINVAL);
1488 assert_return(events, -EINVAL);
1489 assert_return(s->type == SOURCE_IO, -EDOM);
1490 assert_return(!event_pid_changed(s->event), -ECHILD);
1492 *events = s->io.events;
1497 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1500 assert_return(s, -EINVAL);
1501 assert_return(s->type == SOURCE_IO, -EDOM);
1502 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1503 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1504 assert_return(!event_pid_changed(s->event), -ECHILD);
1506 /* edge-triggered updates are never skipped, so we can reset edges */
1507 if (s->io.events == events && !(events & EPOLLET))
1510 if (s->enabled != SD_EVENT_OFF) {
1511 r = source_io_register(s, s->enabled, events);
1516 s->io.events = events;
1517 source_set_pending(s, false);
1522 #if 0 /// UNNEEDED by elogind
1523 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1524 assert_return(s, -EINVAL);
1525 assert_return(revents, -EINVAL);
1526 assert_return(s->type == SOURCE_IO, -EDOM);
1527 assert_return(s->pending, -ENODATA);
1528 assert_return(!event_pid_changed(s->event), -ECHILD);
1530 *revents = s->io.revents;
1534 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1535 assert_return(s, -EINVAL);
1536 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1537 assert_return(!event_pid_changed(s->event), -ECHILD);
1539 return s->signal.sig;
1542 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1543 assert_return(s, -EINVAL);
1544 assert_return(!event_pid_changed(s->event), -ECHILD);
1550 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1553 assert_return(s, -EINVAL);
1554 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1555 assert_return(!event_pid_changed(s->event), -ECHILD);
1557 if (s->priority == priority)
1560 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1561 struct signal_data *old, *d;
1563 /* Move us from the signalfd belonging to the old
1564 * priority to the signalfd of the new priority */
1566 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1568 s->priority = priority;
1570 r = event_make_signal_data(s->event, s->signal.sig, &d);
1572 s->priority = old->priority;
1576 event_unmask_signal_data(s->event, old, s->signal.sig);
1578 s->priority = priority;
1581 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1584 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1586 if (s->type == SOURCE_EXIT)
1587 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1592 #if 0 /// UNNEEDED by elogind
1593 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1594 assert_return(s, -EINVAL);
1595 assert_return(m, -EINVAL);
1596 assert_return(!event_pid_changed(s->event), -ECHILD);
1603 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1606 assert_return(s, -EINVAL);
1607 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1608 assert_return(!event_pid_changed(s->event), -ECHILD);
1610 /* If we are dead anyway, we are fine with turning off
1611 * sources, but everything else needs to fail. */
1612 if (s->event->state == SD_EVENT_FINISHED)
1613 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1615 if (s->enabled == m)
1618 if (m == SD_EVENT_OFF) {
1623 source_io_unregister(s);
1627 case SOURCE_TIME_REALTIME:
1628 case SOURCE_TIME_BOOTTIME:
1629 case SOURCE_TIME_MONOTONIC:
1630 case SOURCE_TIME_REALTIME_ALARM:
1631 case SOURCE_TIME_BOOTTIME_ALARM: {
1632 struct clock_data *d;
1635 d = event_get_clock_data(s->event, s->type);
1638 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1639 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1640 d->needs_rearm = true;
1647 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1653 assert(s->event->n_enabled_child_sources > 0);
1654 s->event->n_enabled_child_sources--;
1656 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1661 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1670 assert_not_reached("Wut? I shouldn't exist.");
1677 r = source_io_register(s, m, s->io.events);
1684 case SOURCE_TIME_REALTIME:
1685 case SOURCE_TIME_BOOTTIME:
1686 case SOURCE_TIME_MONOTONIC:
1687 case SOURCE_TIME_REALTIME_ALARM:
1688 case SOURCE_TIME_BOOTTIME_ALARM: {
1689 struct clock_data *d;
1692 d = event_get_clock_data(s->event, s->type);
1695 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1696 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1697 d->needs_rearm = true;
1705 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1707 s->enabled = SD_EVENT_OFF;
1708 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1716 if (s->enabled == SD_EVENT_OFF)
1717 s->event->n_enabled_child_sources++;
1721 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1723 s->enabled = SD_EVENT_OFF;
1724 s->event->n_enabled_child_sources--;
1725 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1733 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1742 assert_not_reached("Wut? I shouldn't exist.");
1747 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1750 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1755 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1756 assert_return(s, -EINVAL);
1757 assert_return(usec, -EINVAL);
1758 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1759 assert_return(!event_pid_changed(s->event), -ECHILD);
1761 *usec = s->time.next;
1765 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1766 struct clock_data *d;
1768 assert_return(s, -EINVAL);
1769 assert_return(usec != (uint64_t) -1, -EINVAL);
1770 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1771 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1772 assert_return(!event_pid_changed(s->event), -ECHILD);
1774 s->time.next = usec;
1776 source_set_pending(s, false);
1778 d = event_get_clock_data(s->event, s->type);
1781 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1782 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1783 d->needs_rearm = true;
1788 #if 0 /// UNNEEDED by elogind
1789 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1790 assert_return(s, -EINVAL);
1791 assert_return(usec, -EINVAL);
1792 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1793 assert_return(!event_pid_changed(s->event), -ECHILD);
1795 *usec = s->time.accuracy;
1799 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1800 struct clock_data *d;
1802 assert_return(s, -EINVAL);
1803 assert_return(usec != (uint64_t) -1, -EINVAL);
1804 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1805 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1806 assert_return(!event_pid_changed(s->event), -ECHILD);
1809 usec = DEFAULT_ACCURACY_USEC;
1811 s->time.accuracy = usec;
1813 source_set_pending(s, false);
1815 d = event_get_clock_data(s->event, s->type);
1818 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1819 d->needs_rearm = true;
1824 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1825 assert_return(s, -EINVAL);
1826 assert_return(clock, -EINVAL);
1827 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1828 assert_return(!event_pid_changed(s->event), -ECHILD);
1830 *clock = event_source_type_to_clock(s->type);
1834 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1835 assert_return(s, -EINVAL);
1836 assert_return(pid, -EINVAL);
1837 assert_return(s->type == SOURCE_CHILD, -EDOM);
1838 assert_return(!event_pid_changed(s->event), -ECHILD);
1840 *pid = s->child.pid;
1845 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1848 assert_return(s, -EINVAL);
1849 assert_return(s->type != SOURCE_EXIT, -EDOM);
1850 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1851 assert_return(!event_pid_changed(s->event), -ECHILD);
1853 if (s->prepare == callback)
1856 if (callback && s->prepare) {
1857 s->prepare = callback;
1861 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1865 s->prepare = callback;
1868 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1872 prioq_remove(s->event->prepare, s, &s->prepare_index);
1877 #if 0 /// UNNEEDED by elogind
1878 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1879 assert_return(s, NULL);
1884 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1887 assert_return(s, NULL);
1890 s->userdata = userdata;
1896 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1907 initialize_perturb(e);
1910 Find a good time to wake up again between times a and b. We
1911 have two goals here:
1913 a) We want to wake up as seldom as possible, hence prefer
1914 later times over earlier times.
1916 b) But if we have to wake up, then let's make sure to
1917 dispatch as much as possible on the entire system.
1919 We implement this by waking up everywhere at the same time
1920 within any given minute if we can, synchronised via the
1921 perturbation value determined from the boot ID. If we can't,
1922 then we try to find the same spot in every 10s, then 1s and
1923 then 250ms step. Otherwise, we pick the last possible time
1927 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1929 if (_unlikely_(c < USEC_PER_MINUTE))
1932 c -= USEC_PER_MINUTE;
1938 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1940 if (_unlikely_(c < USEC_PER_SEC*10))
1943 c -= USEC_PER_SEC*10;
1949 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1951 if (_unlikely_(c < USEC_PER_SEC))
1960 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1962 if (_unlikely_(c < USEC_PER_MSEC*250))
1965 c -= USEC_PER_MSEC*250;
1974 static int event_arm_timer(
1976 struct clock_data *d) {
1978 struct itimerspec its = {};
1979 sd_event_source *a, *b;
1986 if (!d->needs_rearm)
1989 d->needs_rearm = false;
1991 a = prioq_peek(d->earliest);
1992 if (!a || a->enabled == SD_EVENT_OFF) {
1997 if (d->next == USEC_INFINITY)
2001 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2005 d->next = USEC_INFINITY;
2009 b = prioq_peek(d->latest);
2010 assert_se(b && b->enabled != SD_EVENT_OFF);
2012 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
2016 assert_se(d->fd >= 0);
2019 /* We don' want to disarm here, just mean some time looooong ago. */
2020 its.it_value.tv_sec = 0;
2021 its.it_value.tv_nsec = 1;
2023 timespec_store(&its.it_value, t);
2025 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2033 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2036 assert(s->type == SOURCE_IO);
2038 /* If the event source was already pending, we just OR in the
2039 * new revents, otherwise we reset the value. The ORing is
2040 * necessary to handle EPOLLONESHOT events properly where
2041 * readability might happen independently of writability, and
2042 * we need to keep track of both */
2045 s->io.revents |= revents;
2047 s->io.revents = revents;
2049 return source_set_pending(s, true);
2052 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2059 assert_return(events == EPOLLIN, -EIO);
2061 ss = read(fd, &x, sizeof(x));
2063 if (errno == EAGAIN || errno == EINTR)
2069 if (_unlikely_(ss != sizeof(x)))
2073 *next = USEC_INFINITY;
2078 static int process_timer(
2081 struct clock_data *d) {
2090 s = prioq_peek(d->earliest);
2093 s->enabled == SD_EVENT_OFF ||
2097 r = source_set_pending(s, true);
2101 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2102 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2103 d->needs_rearm = true;
2109 static int process_child(sd_event *e) {
2116 e->need_process_child = false;
2119 So, this is ugly. We iteratively invoke waitid() with P_PID
2120 + WNOHANG for each PID we wait for, instead of using
2121 P_ALL. This is because we only want to get child
2122 information of very specific child processes, and not all
2123 of them. We might not have processed the SIGCHLD even of a
2124 previous invocation and we don't want to maintain a
2125 unbounded *per-child* event queue, hence we really don't
2126 want anything flushed out of the kernel's queue that we
2127 don't care about. Since this is O(n) this means that if you
2128 have a lot of processes you probably want to handle SIGCHLD
2131 We do not reap the children here (by using WNOWAIT), this
2132 is only done after the event source is dispatched so that
2133 the callback still sees the process as a zombie.
2136 HASHMAP_FOREACH(s, e->child_sources, i) {
2137 assert(s->type == SOURCE_CHILD);
2142 if (s->enabled == SD_EVENT_OFF)
2145 zero(s->child.siginfo);
2146 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2147 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2151 if (s->child.siginfo.si_pid != 0) {
2153 s->child.siginfo.si_code == CLD_EXITED ||
2154 s->child.siginfo.si_code == CLD_KILLED ||
2155 s->child.siginfo.si_code == CLD_DUMPED;
2157 if (!zombie && (s->child.options & WEXITED)) {
2158 /* If the child isn't dead then let's
2159 * immediately remove the state change
2160 * from the queue, since there's no
2161 * benefit in leaving it queued */
2163 assert(s->child.options & (WSTOPPED|WCONTINUED));
2164 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2167 r = source_set_pending(s, true);
2176 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2177 bool read_one = false;
2181 assert_return(events == EPOLLIN, -EIO);
2183 /* If there's a signal queued on this priority and SIGCHLD is
2184 on this priority too, then make sure to recheck the
2185 children we watch. This is because we only ever dequeue
2186 the first signal per priority, and if we dequeue one, and
2187 SIGCHLD might be enqueued later we wouldn't know, but we
2188 might have higher priority children we care about hence we
2189 need to check that explicitly. */
2191 if (sigismember(&d->sigset, SIGCHLD))
2192 e->need_process_child = true;
2194 /* If there's already an event source pending for this
2195 * priority we don't read another */
2200 struct signalfd_siginfo si;
2202 sd_event_source *s = NULL;
2204 n = read(d->fd, &si, sizeof(si));
2206 if (errno == EAGAIN || errno == EINTR)
2212 if (_unlikely_(n != sizeof(si)))
2215 assert(si.ssi_signo < _NSIG);
2219 if (e->signal_sources)
2220 s = e->signal_sources[si.ssi_signo];
2226 s->signal.siginfo = si;
2229 r = source_set_pending(s, true);
2237 static int source_dispatch(sd_event_source *s) {
2241 assert(s->pending || s->type == SOURCE_EXIT);
2243 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2244 r = source_set_pending(s, false);
2249 if (s->type != SOURCE_POST) {
2253 /* If we execute a non-post source, let's mark all
2254 * post sources as pending */
2256 SET_FOREACH(z, s->event->post_sources, i) {
2257 if (z->enabled == SD_EVENT_OFF)
2260 r = source_set_pending(z, true);
2266 if (s->enabled == SD_EVENT_ONESHOT) {
2267 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2272 s->dispatching = true;
2277 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2280 case SOURCE_TIME_REALTIME:
2281 case SOURCE_TIME_BOOTTIME:
2282 case SOURCE_TIME_MONOTONIC:
2283 case SOURCE_TIME_REALTIME_ALARM:
2284 case SOURCE_TIME_BOOTTIME_ALARM:
2285 r = s->time.callback(s, s->time.next, s->userdata);
2289 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2292 case SOURCE_CHILD: {
2295 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2296 s->child.siginfo.si_code == CLD_KILLED ||
2297 s->child.siginfo.si_code == CLD_DUMPED;
2299 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2301 /* Now, reap the PID for good. */
2303 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2309 r = s->defer.callback(s, s->userdata);
2313 r = s->post.callback(s, s->userdata);
2317 r = s->exit.callback(s, s->userdata);
2320 case SOURCE_WATCHDOG:
2321 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2322 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2323 assert_not_reached("Wut? I shouldn't exist.");
2326 s->dispatching = false;
2329 log_debug_errno(r, "Event source %s (type %s) returned error, disabling: %m",
2330 strna(s->description), event_source_type_to_string(s->type));
2335 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2340 static int event_prepare(sd_event *e) {
2348 s = prioq_peek(e->prepare);
2349 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2352 s->prepare_iteration = e->iteration;
2353 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2359 s->dispatching = true;
2360 r = s->prepare(s, s->userdata);
2361 s->dispatching = false;
2364 log_debug_errno(r, "Prepare callback of event source %s (type %s) returned error, disabling: %m",
2365 strna(s->description), event_source_type_to_string(s->type));
2370 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2376 static int dispatch_exit(sd_event *e) {
2382 p = prioq_peek(e->exit);
2383 if (!p || p->enabled == SD_EVENT_OFF) {
2384 e->state = SD_EVENT_FINISHED;
2390 e->state = SD_EVENT_EXITING;
2392 r = source_dispatch(p);
2394 e->state = SD_EVENT_INITIAL;
2400 static sd_event_source* event_next_pending(sd_event *e) {
2405 p = prioq_peek(e->pending);
2409 if (p->enabled == SD_EVENT_OFF)
2415 static int arm_watchdog(sd_event *e) {
2416 struct itimerspec its = {};
2421 assert(e->watchdog_fd >= 0);
2423 t = sleep_between(e,
2424 e->watchdog_last + (e->watchdog_period / 2),
2425 e->watchdog_last + (e->watchdog_period * 3 / 4));
2427 timespec_store(&its.it_value, t);
2429 /* Make sure we never set the watchdog to 0, which tells the
2430 * kernel to disable it. */
2431 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2432 its.it_value.tv_nsec = 1;
2434 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2441 static int process_watchdog(sd_event *e) {
2447 /* Don't notify watchdog too often */
2448 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2451 sd_notify(false, "WATCHDOG=1");
2452 e->watchdog_last = e->timestamp.monotonic;
2454 return arm_watchdog(e);
2457 _public_ int sd_event_prepare(sd_event *e) {
2460 assert_return(e, -EINVAL);
2461 assert_return(!event_pid_changed(e), -ECHILD);
2462 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2463 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2465 if (e->exit_requested)
2470 e->state = SD_EVENT_PREPARING;
2471 r = event_prepare(e);
2472 e->state = SD_EVENT_INITIAL;
2476 r = event_arm_timer(e, &e->realtime);
2480 r = event_arm_timer(e, &e->boottime);
2484 r = event_arm_timer(e, &e->monotonic);
2488 r = event_arm_timer(e, &e->realtime_alarm);
2492 r = event_arm_timer(e, &e->boottime_alarm);
2496 if (event_next_pending(e) || e->need_process_child)
2499 e->state = SD_EVENT_ARMED;
2504 e->state = SD_EVENT_ARMED;
2505 r = sd_event_wait(e, 0);
2507 e->state = SD_EVENT_ARMED;
2512 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2513 struct epoll_event *ev_queue;
2514 unsigned ev_queue_max;
2517 assert_return(e, -EINVAL);
2518 assert_return(!event_pid_changed(e), -ECHILD);
2519 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2520 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2522 if (e->exit_requested) {
2523 e->state = SD_EVENT_PENDING;
2527 ev_queue_max = MAX(e->n_sources, 1u);
2528 ev_queue = newa(struct epoll_event, ev_queue_max);
2530 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2531 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2533 if (errno == EINTR) {
2534 e->state = SD_EVENT_PENDING;
2542 dual_timestamp_get(&e->timestamp);
2543 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2545 for (i = 0; i < m; i++) {
2547 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2548 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2550 WakeupType *t = ev_queue[i].data.ptr;
2554 case WAKEUP_EVENT_SOURCE:
2555 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2558 case WAKEUP_CLOCK_DATA: {
2559 struct clock_data *d = ev_queue[i].data.ptr;
2560 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2564 case WAKEUP_SIGNAL_DATA:
2565 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2569 assert_not_reached("Invalid wake-up pointer");
2576 r = process_watchdog(e);
2580 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2584 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2588 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2592 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2596 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2600 if (e->need_process_child) {
2601 r = process_child(e);
2606 if (event_next_pending(e)) {
2607 e->state = SD_EVENT_PENDING;
2615 e->state = SD_EVENT_INITIAL;
2620 _public_ int sd_event_dispatch(sd_event *e) {
2624 assert_return(e, -EINVAL);
2625 assert_return(!event_pid_changed(e), -ECHILD);
2626 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2627 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2629 if (e->exit_requested)
2630 return dispatch_exit(e);
2632 p = event_next_pending(e);
2636 e->state = SD_EVENT_RUNNING;
2637 r = source_dispatch(p);
2638 e->state = SD_EVENT_INITIAL;
2645 e->state = SD_EVENT_INITIAL;
2650 static void event_log_delays(sd_event *e) {
2651 char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1];
2654 for (i = o = 0; i < ELEMENTSOF(e->delays); i++) {
2655 o += snprintf(&b[o], sizeof(b) - o, "%u ", e->delays[i]);
2658 log_info("Event loop iterations: %.*s", o, b);
2661 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2664 assert_return(e, -EINVAL);
2665 assert_return(!event_pid_changed(e), -ECHILD);
2666 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2667 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2669 if (e->profile_delays && e->last_run) {
2673 this_run = now(CLOCK_MONOTONIC);
2675 l = u64log2(this_run - e->last_run);
2676 assert(l < sizeof(e->delays));
2679 if (this_run - e->last_log >= 5*USEC_PER_SEC) {
2680 event_log_delays(e);
2681 e->last_log = this_run;
2685 r = sd_event_prepare(e);
2687 /* There was nothing? Then wait... */
2688 r = sd_event_wait(e, timeout);
2690 if (e->profile_delays)
2691 e->last_run = now(CLOCK_MONOTONIC);
2694 /* There's something now, then let's dispatch it */
2695 r = sd_event_dispatch(e);
2705 #if 0 /// UNNEEDED by elogind
2706 _public_ int sd_event_loop(sd_event *e) {
2709 assert_return(e, -EINVAL);
2710 assert_return(!event_pid_changed(e), -ECHILD);
2711 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2715 while (e->state != SD_EVENT_FINISHED) {
2716 r = sd_event_run(e, (uint64_t) -1);
2728 _public_ int sd_event_get_fd(sd_event *e) {
2730 assert_return(e, -EINVAL);
2731 assert_return(!event_pid_changed(e), -ECHILD);
2737 _public_ int sd_event_get_state(sd_event *e) {
2738 assert_return(e, -EINVAL);
2739 assert_return(!event_pid_changed(e), -ECHILD);
2744 #if 0 /// UNNEEDED by elogind
2745 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2746 assert_return(e, -EINVAL);
2747 assert_return(code, -EINVAL);
2748 assert_return(!event_pid_changed(e), -ECHILD);
2750 if (!e->exit_requested)
2753 *code = e->exit_code;
2758 _public_ int sd_event_exit(sd_event *e, int code) {
2759 assert_return(e, -EINVAL);
2760 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2761 assert_return(!event_pid_changed(e), -ECHILD);
2763 e->exit_requested = true;
2764 e->exit_code = code;
2769 #if 0 /// UNNEEDED by elogind
2770 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2771 assert_return(e, -EINVAL);
2772 assert_return(usec, -EINVAL);
2773 assert_return(!event_pid_changed(e), -ECHILD);
2775 if (!dual_timestamp_is_set(&e->timestamp)) {
2776 /* Implicitly fall back to now() if we never ran
2777 * before and thus have no cached time. */
2784 case CLOCK_REALTIME:
2785 case CLOCK_REALTIME_ALARM:
2786 *usec = e->timestamp.realtime;
2789 case CLOCK_MONOTONIC:
2790 *usec = e->timestamp.monotonic;
2793 case CLOCK_BOOTTIME:
2794 case CLOCK_BOOTTIME_ALARM:
2795 *usec = e->timestamp_boottime;
2803 _public_ int sd_event_default(sd_event **ret) {
2805 static thread_local sd_event *default_event = NULL;
2810 return !!default_event;
2812 if (default_event) {
2813 *ret = sd_event_ref(default_event);
2817 r = sd_event_new(&e);
2821 e->default_event_ptr = &default_event;
2829 #if 0 /// UNNEEDED by elogind
2830 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2831 assert_return(e, -EINVAL);
2832 assert_return(tid, -EINVAL);
2833 assert_return(!event_pid_changed(e), -ECHILD);
2844 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2847 assert_return(e, -EINVAL);
2848 assert_return(!event_pid_changed(e), -ECHILD);
2850 if (e->watchdog == !!b)
2854 struct epoll_event ev = {};
2856 r = sd_watchdog_enabled(false, &e->watchdog_period);
2860 /* Issue first ping immediately */
2861 sd_notify(false, "WATCHDOG=1");
2862 e->watchdog_last = now(CLOCK_MONOTONIC);
2864 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2865 if (e->watchdog_fd < 0)
2868 r = arm_watchdog(e);
2872 ev.events = EPOLLIN;
2873 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2875 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2882 if (e->watchdog_fd >= 0) {
2883 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2884 e->watchdog_fd = safe_close(e->watchdog_fd);
2892 e->watchdog_fd = safe_close(e->watchdog_fd);
2896 #if 0 /// UNNEEDED by elogind
2897 _public_ int sd_event_get_watchdog(sd_event *e) {
2898 assert_return(e, -EINVAL);
2899 assert_return(!event_pid_changed(e), -ECHILD);