2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/epoll.h>
21 #include <sys/timerfd.h>
24 #include "sd-daemon.h"
28 #include "alloc-util.h"
35 #include "process-util.h"
37 #include "signal-util.h"
38 #include "string-table.h"
39 #include "string-util.h"
40 #include "time-util.h"
43 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
45 typedef enum EventSourceType {
49 SOURCE_TIME_MONOTONIC,
50 SOURCE_TIME_REALTIME_ALARM,
51 SOURCE_TIME_BOOTTIME_ALARM,
58 _SOURCE_EVENT_SOURCE_TYPE_MAX,
59 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
62 static const char* const event_source_type_table[_SOURCE_EVENT_SOURCE_TYPE_MAX] = {
64 [SOURCE_TIME_REALTIME] = "realtime",
65 [SOURCE_TIME_BOOTTIME] = "bootime",
66 [SOURCE_TIME_MONOTONIC] = "monotonic",
67 [SOURCE_TIME_REALTIME_ALARM] = "realtime-alarm",
68 [SOURCE_TIME_BOOTTIME_ALARM] = "boottime-alarm",
69 [SOURCE_SIGNAL] = "signal",
70 [SOURCE_CHILD] = "child",
71 [SOURCE_DEFER] = "defer",
72 [SOURCE_POST] = "post",
73 [SOURCE_EXIT] = "exit",
74 [SOURCE_WATCHDOG] = "watchdog",
77 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type, int);
79 /* All objects we use in epoll events start with this value, so that
80 * we know how to dispatch it */
81 typedef enum WakeupType {
87 _WAKEUP_TYPE_INVALID = -1,
90 #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)
92 struct sd_event_source {
99 sd_event_handler_t prepare;
103 EventSourceType type:5;
110 unsigned pending_index;
111 unsigned prepare_index;
112 uint64_t pending_iteration;
113 uint64_t prepare_iteration;
115 LIST_FIELDS(sd_event_source, sources);
119 sd_event_io_handler_t callback;
126 sd_event_time_handler_t callback;
127 usec_t next, accuracy;
128 unsigned earliest_index;
129 unsigned latest_index;
132 sd_event_signal_handler_t callback;
133 struct signalfd_siginfo siginfo;
137 sd_event_child_handler_t callback;
143 sd_event_handler_t callback;
146 sd_event_handler_t callback;
149 sd_event_handler_t callback;
150 unsigned prioq_index;
159 /* For all clocks we maintain two priority queues each, one
160 * ordered for the earliest times the events may be
161 * dispatched, and one ordered by the latest times they must
162 * have been dispatched. The range between the top entries in
163 * the two prioqs is the time window we can freely schedule
176 /* For each priority we maintain one signal fd, so that we
177 * only have to dequeue a single event per priority at a
183 sd_event_source *current;
195 /* timerfd_create() only supports these five clocks so far. We
196 * can add support for more clocks when the kernel learns to
197 * deal with them, too. */
198 struct clock_data realtime;
199 struct clock_data boottime;
200 struct clock_data monotonic;
201 struct clock_data realtime_alarm;
202 struct clock_data boottime_alarm;
206 sd_event_source **signal_sources; /* indexed by signal number */
207 Hashmap *signal_data; /* indexed by priority */
209 Hashmap *child_sources;
210 unsigned n_enabled_child_sources;
219 triple_timestamp timestamp;
222 bool exit_requested:1;
223 bool need_process_child:1;
225 bool profile_delays:1;
230 sd_event **default_event_ptr;
232 usec_t watchdog_last, watchdog_period;
236 LIST_HEAD(sd_event_source, sources);
238 usec_t last_run, last_log;
239 unsigned delays[sizeof(usec_t) * 8];
242 static void source_disconnect(sd_event_source *s);
244 static int pending_prioq_compare(const void *a, const void *b) {
245 const sd_event_source *x = a, *y = b;
250 /* Enabled ones first */
251 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
253 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
256 /* Lower priority values first */
257 if (x->priority < y->priority)
259 if (x->priority > y->priority)
262 /* Older entries first */
263 if (x->pending_iteration < y->pending_iteration)
265 if (x->pending_iteration > y->pending_iteration)
271 static int prepare_prioq_compare(const void *a, const void *b) {
272 const sd_event_source *x = a, *y = b;
277 /* Enabled ones first */
278 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
280 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
283 /* Move most recently prepared ones last, so that we can stop
284 * preparing as soon as we hit one that has already been
285 * prepared in the current iteration */
286 if (x->prepare_iteration < y->prepare_iteration)
288 if (x->prepare_iteration > y->prepare_iteration)
291 /* Lower priority values first */
292 if (x->priority < y->priority)
294 if (x->priority > y->priority)
300 static int earliest_time_prioq_compare(const void *a, const void *b) {
301 const sd_event_source *x = a, *y = b;
303 assert(EVENT_SOURCE_IS_TIME(x->type));
304 assert(x->type == y->type);
306 /* Enabled ones first */
307 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
309 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
312 /* Move the pending ones to the end */
313 if (!x->pending && y->pending)
315 if (x->pending && !y->pending)
319 if (x->time.next < y->time.next)
321 if (x->time.next > y->time.next)
327 static usec_t time_event_source_latest(const sd_event_source *s) {
328 return usec_add(s->time.next, s->time.accuracy);
331 static int latest_time_prioq_compare(const void *a, const void *b) {
332 const sd_event_source *x = a, *y = b;
334 assert(EVENT_SOURCE_IS_TIME(x->type));
335 assert(x->type == y->type);
337 /* Enabled ones first */
338 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
340 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
343 /* Move the pending ones to the end */
344 if (!x->pending && y->pending)
346 if (x->pending && !y->pending)
350 if (time_event_source_latest(x) < time_event_source_latest(y))
352 if (time_event_source_latest(x) > time_event_source_latest(y))
358 static int exit_prioq_compare(const void *a, const void *b) {
359 const sd_event_source *x = a, *y = b;
361 assert(x->type == SOURCE_EXIT);
362 assert(y->type == SOURCE_EXIT);
364 /* Enabled ones first */
365 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
367 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
370 /* Lower priority values first */
371 if (x->priority < y->priority)
373 if (x->priority > y->priority)
379 static void free_clock_data(struct clock_data *d) {
381 assert(d->wakeup == WAKEUP_CLOCK_DATA);
384 prioq_free(d->earliest);
385 prioq_free(d->latest);
388 static void event_free(sd_event *e) {
393 while ((s = e->sources)) {
395 source_disconnect(s);
396 sd_event_source_unref(s);
399 assert(e->n_sources == 0);
401 if (e->default_event_ptr)
402 *(e->default_event_ptr) = NULL;
404 safe_close(e->epoll_fd);
405 safe_close(e->watchdog_fd);
407 free_clock_data(&e->realtime);
408 free_clock_data(&e->boottime);
409 free_clock_data(&e->monotonic);
410 free_clock_data(&e->realtime_alarm);
411 free_clock_data(&e->boottime_alarm);
413 prioq_free(e->pending);
414 prioq_free(e->prepare);
417 free(e->signal_sources);
418 hashmap_free(e->signal_data);
420 hashmap_free(e->child_sources);
421 set_free(e->post_sources);
425 _public_ int sd_event_new(sd_event** ret) {
429 assert_return(ret, -EINVAL);
431 e = new0(sd_event, 1);
436 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
437 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
438 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
439 e->original_pid = getpid();
440 e->perturb = USEC_INFINITY;
442 r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
446 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
447 if (e->epoll_fd < 0) {
452 if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
453 log_debug("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 ... 2^63 us will be logged every 5s.");
454 e->profile_delays = true;
465 _public_ sd_event* sd_event_ref(sd_event *e) {
470 assert(e->n_ref >= 1);
476 _public_ sd_event* sd_event_unref(sd_event *e) {
481 assert(e->n_ref >= 1);
490 static bool event_pid_changed(sd_event *e) {
493 /* We don't support people creating an event loop and keeping
494 * it around over a fork(). Let's complain. */
496 return e->original_pid != getpid();
499 static void source_io_unregister(sd_event_source *s) {
503 assert(s->type == SOURCE_IO);
505 if (event_pid_changed(s->event))
508 if (!s->io.registered)
511 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
513 log_debug_errno(errno, "Failed to remove source %s (type %s) from epoll: %m",
514 strna(s->description), event_source_type_to_string(s->type));
516 s->io.registered = false;
519 static int source_io_register(
524 struct epoll_event ev = {};
528 assert(s->type == SOURCE_IO);
529 assert(enabled != SD_EVENT_OFF);
534 if (enabled == SD_EVENT_ONESHOT)
535 ev.events |= EPOLLONESHOT;
537 if (s->io.registered)
538 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
540 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
544 s->io.registered = true;
549 #if 0 /// UNNEEDED by elogind
550 static clockid_t event_source_type_to_clock(EventSourceType t) {
554 case SOURCE_TIME_REALTIME:
555 return CLOCK_REALTIME;
557 case SOURCE_TIME_BOOTTIME:
558 return CLOCK_BOOTTIME;
560 case SOURCE_TIME_MONOTONIC:
561 return CLOCK_MONOTONIC;
563 case SOURCE_TIME_REALTIME_ALARM:
564 return CLOCK_REALTIME_ALARM;
566 case SOURCE_TIME_BOOTTIME_ALARM:
567 return CLOCK_BOOTTIME_ALARM;
570 return (clockid_t) -1;
575 static EventSourceType clock_to_event_source_type(clockid_t clock) {
580 return SOURCE_TIME_REALTIME;
583 return SOURCE_TIME_BOOTTIME;
585 case CLOCK_MONOTONIC:
586 return SOURCE_TIME_MONOTONIC;
588 case CLOCK_REALTIME_ALARM:
589 return SOURCE_TIME_REALTIME_ALARM;
591 case CLOCK_BOOTTIME_ALARM:
592 return SOURCE_TIME_BOOTTIME_ALARM;
595 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
599 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
604 case SOURCE_TIME_REALTIME:
607 case SOURCE_TIME_BOOTTIME:
610 case SOURCE_TIME_MONOTONIC:
611 return &e->monotonic;
613 case SOURCE_TIME_REALTIME_ALARM:
614 return &e->realtime_alarm;
616 case SOURCE_TIME_BOOTTIME_ALARM:
617 return &e->boottime_alarm;
624 static int event_make_signal_data(
627 struct signal_data **ret) {
629 struct epoll_event ev = {};
630 struct signal_data *d;
638 if (event_pid_changed(e))
641 if (e->signal_sources && e->signal_sources[sig])
642 priority = e->signal_sources[sig]->priority;
646 d = hashmap_get(e->signal_data, &priority);
648 if (sigismember(&d->sigset, sig) > 0) {
654 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
658 d = new0(struct signal_data, 1);
662 d->wakeup = WAKEUP_SIGNAL_DATA;
664 d->priority = priority;
666 r = hashmap_put(e->signal_data, &d->priority, d);
676 assert_se(sigaddset(&ss_copy, sig) >= 0);
678 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
697 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
710 d->fd = safe_close(d->fd);
711 hashmap_remove(e->signal_data, &d->priority);
718 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
722 /* Turns off the specified signal in the signal data
723 * object. If the signal mask of the object becomes empty that
726 if (sigismember(&d->sigset, sig) == 0)
729 assert_se(sigdelset(&d->sigset, sig) >= 0);
731 if (sigisemptyset(&d->sigset)) {
733 /* If all the mask is all-zero we can get rid of the structure */
734 hashmap_remove(e->signal_data, &d->priority);
742 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
743 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
746 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
747 struct signal_data *d;
748 static const int64_t zero_priority = 0;
752 /* Rechecks if the specified signal is still something we are
753 * interested in. If not, we'll unmask it, and possibly drop
754 * the signalfd for it. */
756 if (sig == SIGCHLD &&
757 e->n_enabled_child_sources > 0)
760 if (e->signal_sources &&
761 e->signal_sources[sig] &&
762 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
766 * The specified signal might be enabled in three different queues:
768 * 1) the one that belongs to the priority passed (if it is non-NULL)
769 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
770 * 3) the 0 priority (to cover the SIGCHLD case)
772 * Hence, let's remove it from all three here.
776 d = hashmap_get(e->signal_data, priority);
778 event_unmask_signal_data(e, d, sig);
781 if (e->signal_sources && e->signal_sources[sig]) {
782 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
784 event_unmask_signal_data(e, d, sig);
787 d = hashmap_get(e->signal_data, &zero_priority);
789 event_unmask_signal_data(e, d, sig);
792 static void source_disconnect(sd_event_source *s) {
800 assert(s->event->n_sources > 0);
806 source_io_unregister(s);
810 case SOURCE_TIME_REALTIME:
811 case SOURCE_TIME_BOOTTIME:
812 case SOURCE_TIME_MONOTONIC:
813 case SOURCE_TIME_REALTIME_ALARM:
814 case SOURCE_TIME_BOOTTIME_ALARM: {
815 struct clock_data *d;
817 d = event_get_clock_data(s->event, s->type);
820 prioq_remove(d->earliest, s, &s->time.earliest_index);
821 prioq_remove(d->latest, s, &s->time.latest_index);
822 d->needs_rearm = true;
827 if (s->signal.sig > 0) {
829 if (s->event->signal_sources)
830 s->event->signal_sources[s->signal.sig] = NULL;
832 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
838 if (s->child.pid > 0) {
839 if (s->enabled != SD_EVENT_OFF) {
840 assert(s->event->n_enabled_child_sources > 0);
841 s->event->n_enabled_child_sources--;
844 (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
845 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
855 set_remove(s->event->post_sources, s);
859 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
863 assert_not_reached("Wut? I shouldn't exist.");
867 prioq_remove(s->event->pending, s, &s->pending_index);
870 prioq_remove(s->event->prepare, s, &s->prepare_index);
874 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
876 LIST_REMOVE(sources, event->sources, s);
880 sd_event_unref(event);
883 static void source_free(sd_event_source *s) {
886 source_disconnect(s);
887 free(s->description);
891 static int source_set_pending(sd_event_source *s, bool b) {
895 assert(s->type != SOURCE_EXIT);
903 s->pending_iteration = s->event->iteration;
905 r = prioq_put(s->event->pending, s, &s->pending_index);
911 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
913 if (EVENT_SOURCE_IS_TIME(s->type)) {
914 struct clock_data *d;
916 d = event_get_clock_data(s->event, s->type);
919 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
920 prioq_reshuffle(d->latest, s, &s->time.latest_index);
921 d->needs_rearm = true;
924 if (s->type == SOURCE_SIGNAL && !b) {
925 struct signal_data *d;
927 d = hashmap_get(s->event->signal_data, &s->priority);
928 if (d && d->current == s)
935 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
940 s = new0(sd_event_source, 1);
946 s->floating = floating;
948 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
953 LIST_PREPEND(sources, e->sources, s);
959 _public_ int sd_event_add_io(
961 sd_event_source **ret,
964 sd_event_io_handler_t callback,
970 assert_return(e, -EINVAL);
971 assert_return(fd >= 0, -EBADF);
972 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
973 assert_return(callback, -EINVAL);
974 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
975 assert_return(!event_pid_changed(e), -ECHILD);
977 s = source_new(e, !ret, SOURCE_IO);
981 s->wakeup = WAKEUP_EVENT_SOURCE;
983 s->io.events = events;
984 s->io.callback = callback;
985 s->userdata = userdata;
986 s->enabled = SD_EVENT_ON;
988 r = source_io_register(s, s->enabled, events);
1000 static void initialize_perturb(sd_event *e) {
1001 sd_id128_t bootid = {};
1003 /* When we sleep for longer, we try to realign the wakeup to
1004 the same time wihtin each minute/second/250ms, so that
1005 events all across the system can be coalesced into a single
1006 CPU wakeup. However, let's take some system-specific
1007 randomness for this value, so that in a network of systems
1008 with synced clocks timer events are distributed a
1009 bit. Here, we calculate a perturbation usec offset from the
1012 if (_likely_(e->perturb != USEC_INFINITY))
1015 if (sd_id128_get_boot(&bootid) >= 0)
1016 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
1019 static int event_setup_timer_fd(
1021 struct clock_data *d,
1024 struct epoll_event ev = {};
1030 if (_likely_(d->fd >= 0))
1033 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1037 ev.events = EPOLLIN;
1040 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1050 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1053 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1056 _public_ int sd_event_add_time(
1058 sd_event_source **ret,
1062 sd_event_time_handler_t callback,
1065 EventSourceType type;
1067 struct clock_data *d;
1070 assert_return(e, -EINVAL);
1071 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1072 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1073 assert_return(!event_pid_changed(e), -ECHILD);
1075 if (!clock_supported(clock)) /* Checks whether the kernel supports the clock */
1078 type = clock_to_event_source_type(clock); /* checks whether sd-event supports this clock */
1083 callback = time_exit_callback;
1085 d = event_get_clock_data(e, type);
1088 r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare);
1092 r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare);
1097 r = event_setup_timer_fd(e, d, clock);
1102 s = source_new(e, !ret, type);
1106 s->time.next = usec;
1107 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1108 s->time.callback = callback;
1109 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1110 s->userdata = userdata;
1111 s->enabled = SD_EVENT_ONESHOT;
1113 d->needs_rearm = true;
1115 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1119 r = prioq_put(d->latest, s, &s->time.latest_index);
1133 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1136 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1139 _public_ int sd_event_add_signal(
1141 sd_event_source **ret,
1143 sd_event_signal_handler_t callback,
1147 struct signal_data *d;
1151 assert_return(e, -EINVAL);
1152 assert_return(SIGNAL_VALID(sig), -EINVAL);
1153 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1154 assert_return(!event_pid_changed(e), -ECHILD);
1157 callback = signal_exit_callback;
1159 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1163 if (!sigismember(&ss, sig))
1166 if (!e->signal_sources) {
1167 e->signal_sources = new0(sd_event_source*, _NSIG);
1168 if (!e->signal_sources)
1170 } else if (e->signal_sources[sig])
1173 s = source_new(e, !ret, SOURCE_SIGNAL);
1177 s->signal.sig = sig;
1178 s->signal.callback = callback;
1179 s->userdata = userdata;
1180 s->enabled = SD_EVENT_ON;
1182 e->signal_sources[sig] = s;
1184 r = event_make_signal_data(e, sig, &d);
1190 /* Use the signal name as description for the event source by default */
1191 (void) sd_event_source_set_description(s, signal_to_string(sig));
1199 #if 0 /// UNNEEDED by elogind
1200 _public_ int sd_event_add_child(
1202 sd_event_source **ret,
1205 sd_event_child_handler_t callback,
1211 assert_return(e, -EINVAL);
1212 assert_return(pid > 1, -EINVAL);
1213 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1214 assert_return(options != 0, -EINVAL);
1215 assert_return(callback, -EINVAL);
1216 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1217 assert_return(!event_pid_changed(e), -ECHILD);
1219 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1223 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1226 s = source_new(e, !ret, SOURCE_CHILD);
1231 s->child.options = options;
1232 s->child.callback = callback;
1233 s->userdata = userdata;
1234 s->enabled = SD_EVENT_ONESHOT;
1236 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1242 e->n_enabled_child_sources++;
1244 r = event_make_signal_data(e, SIGCHLD, NULL);
1246 e->n_enabled_child_sources--;
1251 e->need_process_child = true;
1259 _public_ int sd_event_add_defer(
1261 sd_event_source **ret,
1262 sd_event_handler_t callback,
1268 assert_return(e, -EINVAL);
1269 assert_return(callback, -EINVAL);
1270 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1271 assert_return(!event_pid_changed(e), -ECHILD);
1273 s = source_new(e, !ret, SOURCE_DEFER);
1277 s->defer.callback = callback;
1278 s->userdata = userdata;
1279 s->enabled = SD_EVENT_ONESHOT;
1281 r = source_set_pending(s, true);
1294 _public_ int sd_event_add_post(
1296 sd_event_source **ret,
1297 sd_event_handler_t callback,
1303 assert_return(e, -EINVAL);
1304 assert_return(callback, -EINVAL);
1305 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1306 assert_return(!event_pid_changed(e), -ECHILD);
1308 r = set_ensure_allocated(&e->post_sources, NULL);
1312 s = source_new(e, !ret, SOURCE_POST);
1316 s->post.callback = callback;
1317 s->userdata = userdata;
1318 s->enabled = SD_EVENT_ON;
1320 r = set_put(e->post_sources, s);
1332 _public_ int sd_event_add_exit(
1334 sd_event_source **ret,
1335 sd_event_handler_t callback,
1341 assert_return(e, -EINVAL);
1342 assert_return(callback, -EINVAL);
1343 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1344 assert_return(!event_pid_changed(e), -ECHILD);
1346 r = prioq_ensure_allocated(&e->exit, exit_prioq_compare);
1350 s = source_new(e, !ret, SOURCE_EXIT);
1354 s->exit.callback = callback;
1355 s->userdata = userdata;
1356 s->exit.prioq_index = PRIOQ_IDX_NULL;
1357 s->enabled = SD_EVENT_ONESHOT;
1359 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1371 #if 0 /// UNNEEDED by elogind
1372 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1377 assert(s->n_ref >= 1);
1384 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1389 assert(s->n_ref >= 1);
1392 if (s->n_ref <= 0) {
1393 /* Here's a special hack: when we are called from a
1394 * dispatch handler we won't free the event source
1395 * immediately, but we will detach the fd from the
1396 * epoll. This way it is safe for the caller to unref
1397 * the event source and immediately close the fd, but
1398 * we still retain a valid event source object after
1401 if (s->dispatching) {
1402 if (s->type == SOURCE_IO)
1403 source_io_unregister(s);
1405 source_disconnect(s);
1413 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1414 assert_return(s, -EINVAL);
1415 assert_return(!event_pid_changed(s->event), -ECHILD);
1417 return free_and_strdup(&s->description, description);
1420 #if 0 /// UNNEEDED by elogind
1421 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1422 assert_return(s, -EINVAL);
1423 assert_return(description, -EINVAL);
1424 assert_return(s->description, -ENXIO);
1425 assert_return(!event_pid_changed(s->event), -ECHILD);
1427 *description = s->description;
1432 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1433 assert_return(s, NULL);
1438 #if 0 /// UNNEEDED by elogind
1439 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1440 assert_return(s, -EINVAL);
1441 assert_return(s->type != SOURCE_EXIT, -EDOM);
1442 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1443 assert_return(!event_pid_changed(s->event), -ECHILD);
1448 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1449 assert_return(s, -EINVAL);
1450 assert_return(s->type == SOURCE_IO, -EDOM);
1451 assert_return(!event_pid_changed(s->event), -ECHILD);
1457 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1460 assert_return(s, -EINVAL);
1461 assert_return(fd >= 0, -EBADF);
1462 assert_return(s->type == SOURCE_IO, -EDOM);
1463 assert_return(!event_pid_changed(s->event), -ECHILD);
1468 if (s->enabled == SD_EVENT_OFF) {
1470 s->io.registered = false;
1474 saved_fd = s->io.fd;
1475 assert(s->io.registered);
1478 s->io.registered = false;
1480 r = source_io_register(s, s->enabled, s->io.events);
1482 s->io.fd = saved_fd;
1483 s->io.registered = true;
1487 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1493 #if 0 /// UNNEEDED by elogind
1494 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1495 assert_return(s, -EINVAL);
1496 assert_return(events, -EINVAL);
1497 assert_return(s->type == SOURCE_IO, -EDOM);
1498 assert_return(!event_pid_changed(s->event), -ECHILD);
1500 *events = s->io.events;
1505 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1508 assert_return(s, -EINVAL);
1509 assert_return(s->type == SOURCE_IO, -EDOM);
1510 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1511 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1512 assert_return(!event_pid_changed(s->event), -ECHILD);
1514 /* edge-triggered updates are never skipped, so we can reset edges */
1515 if (s->io.events == events && !(events & EPOLLET))
1518 if (s->enabled != SD_EVENT_OFF) {
1519 r = source_io_register(s, s->enabled, events);
1524 s->io.events = events;
1525 source_set_pending(s, false);
1530 #if 0 /// UNNEEDED by elogind
1531 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1532 assert_return(s, -EINVAL);
1533 assert_return(revents, -EINVAL);
1534 assert_return(s->type == SOURCE_IO, -EDOM);
1535 assert_return(s->pending, -ENODATA);
1536 assert_return(!event_pid_changed(s->event), -ECHILD);
1538 *revents = s->io.revents;
1542 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1543 assert_return(s, -EINVAL);
1544 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1545 assert_return(!event_pid_changed(s->event), -ECHILD);
1547 return s->signal.sig;
1550 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1551 assert_return(s, -EINVAL);
1552 assert_return(!event_pid_changed(s->event), -ECHILD);
1554 *priority = s->priority;
1559 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1562 assert_return(s, -EINVAL);
1563 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1564 assert_return(!event_pid_changed(s->event), -ECHILD);
1566 if (s->priority == priority)
1569 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1570 struct signal_data *old, *d;
1572 /* Move us from the signalfd belonging to the old
1573 * priority to the signalfd of the new priority */
1575 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1577 s->priority = priority;
1579 r = event_make_signal_data(s->event, s->signal.sig, &d);
1581 s->priority = old->priority;
1585 event_unmask_signal_data(s->event, old, s->signal.sig);
1587 s->priority = priority;
1590 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1593 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1595 if (s->type == SOURCE_EXIT)
1596 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1601 #if 0 /// UNNEEDED by elogind
1602 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1603 assert_return(s, -EINVAL);
1604 assert_return(m, -EINVAL);
1605 assert_return(!event_pid_changed(s->event), -ECHILD);
1612 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1615 assert_return(s, -EINVAL);
1616 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1617 assert_return(!event_pid_changed(s->event), -ECHILD);
1619 /* If we are dead anyway, we are fine with turning off
1620 * sources, but everything else needs to fail. */
1621 if (s->event->state == SD_EVENT_FINISHED)
1622 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1624 if (s->enabled == m)
1627 if (m == SD_EVENT_OFF) {
1632 source_io_unregister(s);
1636 case SOURCE_TIME_REALTIME:
1637 case SOURCE_TIME_BOOTTIME:
1638 case SOURCE_TIME_MONOTONIC:
1639 case SOURCE_TIME_REALTIME_ALARM:
1640 case SOURCE_TIME_BOOTTIME_ALARM: {
1641 struct clock_data *d;
1644 d = event_get_clock_data(s->event, s->type);
1647 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1648 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1649 d->needs_rearm = true;
1656 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1662 assert(s->event->n_enabled_child_sources > 0);
1663 s->event->n_enabled_child_sources--;
1665 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1670 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1679 assert_not_reached("Wut? I shouldn't exist.");
1686 r = source_io_register(s, m, s->io.events);
1693 case SOURCE_TIME_REALTIME:
1694 case SOURCE_TIME_BOOTTIME:
1695 case SOURCE_TIME_MONOTONIC:
1696 case SOURCE_TIME_REALTIME_ALARM:
1697 case SOURCE_TIME_BOOTTIME_ALARM: {
1698 struct clock_data *d;
1701 d = event_get_clock_data(s->event, s->type);
1704 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1705 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1706 d->needs_rearm = true;
1714 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1716 s->enabled = SD_EVENT_OFF;
1717 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1725 if (s->enabled == SD_EVENT_OFF)
1726 s->event->n_enabled_child_sources++;
1730 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1732 s->enabled = SD_EVENT_OFF;
1733 s->event->n_enabled_child_sources--;
1734 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1742 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1751 assert_not_reached("Wut? I shouldn't exist.");
1756 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1759 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1764 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1765 assert_return(s, -EINVAL);
1766 assert_return(usec, -EINVAL);
1767 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1768 assert_return(!event_pid_changed(s->event), -ECHILD);
1770 *usec = s->time.next;
1774 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1775 struct clock_data *d;
1777 assert_return(s, -EINVAL);
1778 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1779 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1780 assert_return(!event_pid_changed(s->event), -ECHILD);
1782 s->time.next = usec;
1784 source_set_pending(s, false);
1786 d = event_get_clock_data(s->event, s->type);
1789 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1790 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1791 d->needs_rearm = true;
1796 #if 0 /// UNNEEDED by elogind
1797 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1798 assert_return(s, -EINVAL);
1799 assert_return(usec, -EINVAL);
1800 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1801 assert_return(!event_pid_changed(s->event), -ECHILD);
1803 *usec = s->time.accuracy;
1807 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1808 struct clock_data *d;
1810 assert_return(s, -EINVAL);
1811 assert_return(usec != (uint64_t) -1, -EINVAL);
1812 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1813 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1814 assert_return(!event_pid_changed(s->event), -ECHILD);
1817 usec = DEFAULT_ACCURACY_USEC;
1819 s->time.accuracy = usec;
1821 source_set_pending(s, false);
1823 d = event_get_clock_data(s->event, s->type);
1826 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1827 d->needs_rearm = true;
1832 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1833 assert_return(s, -EINVAL);
1834 assert_return(clock, -EINVAL);
1835 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1836 assert_return(!event_pid_changed(s->event), -ECHILD);
1838 *clock = event_source_type_to_clock(s->type);
1842 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1843 assert_return(s, -EINVAL);
1844 assert_return(pid, -EINVAL);
1845 assert_return(s->type == SOURCE_CHILD, -EDOM);
1846 assert_return(!event_pid_changed(s->event), -ECHILD);
1848 *pid = s->child.pid;
1853 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1856 assert_return(s, -EINVAL);
1857 assert_return(s->type != SOURCE_EXIT, -EDOM);
1858 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1859 assert_return(!event_pid_changed(s->event), -ECHILD);
1861 if (s->prepare == callback)
1864 if (callback && s->prepare) {
1865 s->prepare = callback;
1869 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1873 s->prepare = callback;
1876 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1880 prioq_remove(s->event->prepare, s, &s->prepare_index);
1885 #if 0 /// UNNEEDED by elogind
1886 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1887 assert_return(s, NULL);
1892 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1895 assert_return(s, NULL);
1898 s->userdata = userdata;
1904 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1911 if (a >= USEC_INFINITY)
1912 return USEC_INFINITY;
1917 initialize_perturb(e);
1920 Find a good time to wake up again between times a and b. We
1921 have two goals here:
1923 a) We want to wake up as seldom as possible, hence prefer
1924 later times over earlier times.
1926 b) But if we have to wake up, then let's make sure to
1927 dispatch as much as possible on the entire system.
1929 We implement this by waking up everywhere at the same time
1930 within any given minute if we can, synchronised via the
1931 perturbation value determined from the boot ID. If we can't,
1932 then we try to find the same spot in every 10s, then 1s and
1933 then 250ms step. Otherwise, we pick the last possible time
1937 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1939 if (_unlikely_(c < USEC_PER_MINUTE))
1942 c -= USEC_PER_MINUTE;
1948 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1950 if (_unlikely_(c < USEC_PER_SEC*10))
1953 c -= USEC_PER_SEC*10;
1959 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1961 if (_unlikely_(c < USEC_PER_SEC))
1970 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1972 if (_unlikely_(c < USEC_PER_MSEC*250))
1975 c -= USEC_PER_MSEC*250;
1984 static int event_arm_timer(
1986 struct clock_data *d) {
1988 struct itimerspec its = {};
1989 sd_event_source *a, *b;
1996 if (!d->needs_rearm)
1999 d->needs_rearm = false;
2001 a = prioq_peek(d->earliest);
2002 if (!a || a->enabled == SD_EVENT_OFF || a->time.next == USEC_INFINITY) {
2007 if (d->next == USEC_INFINITY)
2011 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2015 d->next = USEC_INFINITY;
2019 b = prioq_peek(d->latest);
2020 assert_se(b && b->enabled != SD_EVENT_OFF);
2022 t = sleep_between(e, a->time.next, time_event_source_latest(b));
2026 assert_se(d->fd >= 0);
2029 /* We don' want to disarm here, just mean some time looooong ago. */
2030 its.it_value.tv_sec = 0;
2031 its.it_value.tv_nsec = 1;
2033 timespec_store(&its.it_value, t);
2035 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2043 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2046 assert(s->type == SOURCE_IO);
2048 /* If the event source was already pending, we just OR in the
2049 * new revents, otherwise we reset the value. The ORing is
2050 * necessary to handle EPOLLONESHOT events properly where
2051 * readability might happen independently of writability, and
2052 * we need to keep track of both */
2055 s->io.revents |= revents;
2057 s->io.revents = revents;
2059 return source_set_pending(s, true);
2062 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2069 assert_return(events == EPOLLIN, -EIO);
2071 ss = read(fd, &x, sizeof(x));
2073 if (errno == EAGAIN || errno == EINTR)
2079 if (_unlikely_(ss != sizeof(x)))
2083 *next = USEC_INFINITY;
2088 static int process_timer(
2091 struct clock_data *d) {
2100 s = prioq_peek(d->earliest);
2103 s->enabled == SD_EVENT_OFF ||
2107 r = source_set_pending(s, true);
2111 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2112 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2113 d->needs_rearm = true;
2119 static int process_child(sd_event *e) {
2126 e->need_process_child = false;
2129 So, this is ugly. We iteratively invoke waitid() with P_PID
2130 + WNOHANG for each PID we wait for, instead of using
2131 P_ALL. This is because we only want to get child
2132 information of very specific child processes, and not all
2133 of them. We might not have processed the SIGCHLD even of a
2134 previous invocation and we don't want to maintain a
2135 unbounded *per-child* event queue, hence we really don't
2136 want anything flushed out of the kernel's queue that we
2137 don't care about. Since this is O(n) this means that if you
2138 have a lot of processes you probably want to handle SIGCHLD
2141 We do not reap the children here (by using WNOWAIT), this
2142 is only done after the event source is dispatched so that
2143 the callback still sees the process as a zombie.
2146 HASHMAP_FOREACH(s, e->child_sources, i) {
2147 assert(s->type == SOURCE_CHILD);
2152 if (s->enabled == SD_EVENT_OFF)
2155 zero(s->child.siginfo);
2156 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2157 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2161 if (s->child.siginfo.si_pid != 0) {
2163 s->child.siginfo.si_code == CLD_EXITED ||
2164 s->child.siginfo.si_code == CLD_KILLED ||
2165 s->child.siginfo.si_code == CLD_DUMPED;
2167 if (!zombie && (s->child.options & WEXITED)) {
2168 /* If the child isn't dead then let's
2169 * immediately remove the state change
2170 * from the queue, since there's no
2171 * benefit in leaving it queued */
2173 assert(s->child.options & (WSTOPPED|WCONTINUED));
2174 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2177 r = source_set_pending(s, true);
2186 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2187 bool read_one = false;
2191 assert_return(events == EPOLLIN, -EIO);
2193 /* If there's a signal queued on this priority and SIGCHLD is
2194 on this priority too, then make sure to recheck the
2195 children we watch. This is because we only ever dequeue
2196 the first signal per priority, and if we dequeue one, and
2197 SIGCHLD might be enqueued later we wouldn't know, but we
2198 might have higher priority children we care about hence we
2199 need to check that explicitly. */
2201 if (sigismember(&d->sigset, SIGCHLD))
2202 e->need_process_child = true;
2204 /* If there's already an event source pending for this
2205 * priority we don't read another */
2210 struct signalfd_siginfo si;
2212 sd_event_source *s = NULL;
2214 n = read(d->fd, &si, sizeof(si));
2216 if (errno == EAGAIN || errno == EINTR)
2222 if (_unlikely_(n != sizeof(si)))
2225 assert(SIGNAL_VALID(si.ssi_signo));
2229 if (e->signal_sources)
2230 s = e->signal_sources[si.ssi_signo];
2236 s->signal.siginfo = si;
2239 r = source_set_pending(s, true);
2247 static int source_dispatch(sd_event_source *s) {
2248 EventSourceType saved_type;
2252 assert(s->pending || s->type == SOURCE_EXIT);
2254 /* Save the event source type, here, so that we still know it after the event callback which might invalidate
2256 saved_type = s->type;
2258 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2259 r = source_set_pending(s, false);
2264 if (s->type != SOURCE_POST) {
2268 /* If we execute a non-post source, let's mark all
2269 * post sources as pending */
2271 SET_FOREACH(z, s->event->post_sources, i) {
2272 if (z->enabled == SD_EVENT_OFF)
2275 r = source_set_pending(z, true);
2281 if (s->enabled == SD_EVENT_ONESHOT) {
2282 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2287 s->dispatching = true;
2292 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2295 case SOURCE_TIME_REALTIME:
2296 case SOURCE_TIME_BOOTTIME:
2297 case SOURCE_TIME_MONOTONIC:
2298 case SOURCE_TIME_REALTIME_ALARM:
2299 case SOURCE_TIME_BOOTTIME_ALARM:
2300 r = s->time.callback(s, s->time.next, s->userdata);
2304 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2307 case SOURCE_CHILD: {
2310 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2311 s->child.siginfo.si_code == CLD_KILLED ||
2312 s->child.siginfo.si_code == CLD_DUMPED;
2314 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2316 /* Now, reap the PID for good. */
2318 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2324 r = s->defer.callback(s, s->userdata);
2328 r = s->post.callback(s, s->userdata);
2332 r = s->exit.callback(s, s->userdata);
2335 case SOURCE_WATCHDOG:
2336 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2337 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2338 assert_not_reached("Wut? I shouldn't exist.");
2341 s->dispatching = false;
2344 log_debug_errno(r, "Event source %s (type %s) returned error, disabling: %m",
2345 strna(s->description), event_source_type_to_string(saved_type));
2350 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2355 static int event_prepare(sd_event *e) {
2363 s = prioq_peek(e->prepare);
2364 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2367 s->prepare_iteration = e->iteration;
2368 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2374 s->dispatching = true;
2375 r = s->prepare(s, s->userdata);
2376 s->dispatching = false;
2379 log_debug_errno(r, "Prepare callback of event source %s (type %s) returned error, disabling: %m",
2380 strna(s->description), event_source_type_to_string(s->type));
2385 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2391 static int dispatch_exit(sd_event *e) {
2397 p = prioq_peek(e->exit);
2398 if (!p || p->enabled == SD_EVENT_OFF) {
2399 e->state = SD_EVENT_FINISHED;
2405 e->state = SD_EVENT_EXITING;
2407 r = source_dispatch(p);
2409 e->state = SD_EVENT_INITIAL;
2415 static sd_event_source* event_next_pending(sd_event *e) {
2420 p = prioq_peek(e->pending);
2424 if (p->enabled == SD_EVENT_OFF)
2430 static int arm_watchdog(sd_event *e) {
2431 struct itimerspec its = {};
2436 assert(e->watchdog_fd >= 0);
2438 t = sleep_between(e,
2439 e->watchdog_last + (e->watchdog_period / 2),
2440 e->watchdog_last + (e->watchdog_period * 3 / 4));
2442 timespec_store(&its.it_value, t);
2444 /* Make sure we never set the watchdog to 0, which tells the
2445 * kernel to disable it. */
2446 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2447 its.it_value.tv_nsec = 1;
2449 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2456 static int process_watchdog(sd_event *e) {
2462 /* Don't notify watchdog too often */
2463 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2466 sd_notify(false, "WATCHDOG=1");
2467 e->watchdog_last = e->timestamp.monotonic;
2469 return arm_watchdog(e);
2472 _public_ int sd_event_prepare(sd_event *e) {
2475 assert_return(e, -EINVAL);
2476 assert_return(!event_pid_changed(e), -ECHILD);
2477 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2478 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2480 if (e->exit_requested)
2485 e->state = SD_EVENT_PREPARING;
2486 r = event_prepare(e);
2487 e->state = SD_EVENT_INITIAL;
2491 r = event_arm_timer(e, &e->realtime);
2495 r = event_arm_timer(e, &e->boottime);
2499 r = event_arm_timer(e, &e->monotonic);
2503 r = event_arm_timer(e, &e->realtime_alarm);
2507 r = event_arm_timer(e, &e->boottime_alarm);
2511 if (event_next_pending(e) || e->need_process_child)
2514 e->state = SD_EVENT_ARMED;
2519 e->state = SD_EVENT_ARMED;
2520 r = sd_event_wait(e, 0);
2522 e->state = SD_EVENT_ARMED;
2527 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2528 struct epoll_event *ev_queue;
2529 unsigned ev_queue_max;
2532 assert_return(e, -EINVAL);
2533 assert_return(!event_pid_changed(e), -ECHILD);
2534 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2535 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2537 if (e->exit_requested) {
2538 e->state = SD_EVENT_PENDING;
2542 ev_queue_max = MAX(e->n_sources, 1u);
2543 ev_queue = newa(struct epoll_event, ev_queue_max);
2545 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2546 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2548 if (errno == EINTR) {
2549 e->state = SD_EVENT_PENDING;
2557 triple_timestamp_get(&e->timestamp);
2559 for (i = 0; i < m; i++) {
2561 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2562 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2564 WakeupType *t = ev_queue[i].data.ptr;
2568 case WAKEUP_EVENT_SOURCE:
2569 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2572 case WAKEUP_CLOCK_DATA: {
2573 struct clock_data *d = ev_queue[i].data.ptr;
2574 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2578 case WAKEUP_SIGNAL_DATA:
2579 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2583 assert_not_reached("Invalid wake-up pointer");
2590 r = process_watchdog(e);
2594 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2598 r = process_timer(e, e->timestamp.boottime, &e->boottime);
2602 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2606 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2610 r = process_timer(e, e->timestamp.boottime, &e->boottime_alarm);
2614 if (e->need_process_child) {
2615 r = process_child(e);
2620 if (event_next_pending(e)) {
2621 e->state = SD_EVENT_PENDING;
2629 e->state = SD_EVENT_INITIAL;
2634 _public_ int sd_event_dispatch(sd_event *e) {
2638 assert_return(e, -EINVAL);
2639 assert_return(!event_pid_changed(e), -ECHILD);
2640 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2641 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2643 if (e->exit_requested)
2644 return dispatch_exit(e);
2646 p = event_next_pending(e);
2650 e->state = SD_EVENT_RUNNING;
2651 r = source_dispatch(p);
2652 e->state = SD_EVENT_INITIAL;
2659 e->state = SD_EVENT_INITIAL;
2664 static void event_log_delays(sd_event *e) {
2665 char b[ELEMENTSOF(e->delays) * DECIMAL_STR_MAX(unsigned) + 1];
2669 for (i = o = 0; i < ELEMENTSOF(e->delays); i++) {
2670 o += snprintf(&b[o], sizeof(b) - o, "%u ", e->delays[i]);
2673 log_debug("Event loop iterations: %.*s", o, b);
2676 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2679 assert_return(e, -EINVAL);
2680 assert_return(!event_pid_changed(e), -ECHILD);
2681 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2682 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2684 if (e->profile_delays && e->last_run) {
2688 this_run = now(CLOCK_MONOTONIC);
2690 l = u64log2(this_run - e->last_run);
2691 assert(l < sizeof(e->delays));
2694 if (this_run - e->last_log >= 5*USEC_PER_SEC) {
2695 event_log_delays(e);
2696 e->last_log = this_run;
2700 r = sd_event_prepare(e);
2702 /* There was nothing? Then wait... */
2703 r = sd_event_wait(e, timeout);
2705 if (e->profile_delays)
2706 e->last_run = now(CLOCK_MONOTONIC);
2709 /* There's something now, then let's dispatch it */
2710 r = sd_event_dispatch(e);
2720 #if 0 /// UNNEEDED by elogind
2721 _public_ int sd_event_loop(sd_event *e) {
2724 assert_return(e, -EINVAL);
2725 assert_return(!event_pid_changed(e), -ECHILD);
2726 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2730 while (e->state != SD_EVENT_FINISHED) {
2731 r = sd_event_run(e, (uint64_t) -1);
2743 _public_ int sd_event_get_fd(sd_event *e) {
2745 assert_return(e, -EINVAL);
2746 assert_return(!event_pid_changed(e), -ECHILD);
2752 _public_ int sd_event_get_state(sd_event *e) {
2753 assert_return(e, -EINVAL);
2754 assert_return(!event_pid_changed(e), -ECHILD);
2759 #if 0 /// UNNEEDED by elogind
2760 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2761 assert_return(e, -EINVAL);
2762 assert_return(code, -EINVAL);
2763 assert_return(!event_pid_changed(e), -ECHILD);
2765 if (!e->exit_requested)
2768 *code = e->exit_code;
2773 _public_ int sd_event_exit(sd_event *e, int code) {
2774 assert_return(e, -EINVAL);
2775 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2776 assert_return(!event_pid_changed(e), -ECHILD);
2778 e->exit_requested = true;
2779 e->exit_code = code;
2784 #if 0 /// UNNEEDED by elogind
2785 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2786 assert_return(e, -EINVAL);
2787 assert_return(usec, -EINVAL);
2788 assert_return(!event_pid_changed(e), -ECHILD);
2790 if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock))
2793 /* Generate a clean error in case CLOCK_BOOTTIME is not available. Note that don't use clock_supported() here,
2794 * for a reason: there are systems where CLOCK_BOOTTIME is supported, but CLOCK_BOOTTIME_ALARM is not, but for
2795 * the purpose of getting the time this doesn't matter. */
2796 if (IN_SET(clock, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM) && !clock_boottime_supported())
2799 if (!triple_timestamp_is_set(&e->timestamp)) {
2800 /* Implicitly fall back to now() if we never ran
2801 * before and thus have no cached time. */
2806 *usec = triple_timestamp_by_clock(&e->timestamp, clock);
2811 _public_ int sd_event_default(sd_event **ret) {
2813 static thread_local sd_event *default_event = NULL;
2818 return !!default_event;
2820 if (default_event) {
2821 *ret = sd_event_ref(default_event);
2825 r = sd_event_new(&e);
2829 e->default_event_ptr = &default_event;
2837 #if 0 /// UNNEEDED by elogind
2838 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2839 assert_return(e, -EINVAL);
2840 assert_return(tid, -EINVAL);
2841 assert_return(!event_pid_changed(e), -ECHILD);
2852 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2855 assert_return(e, -EINVAL);
2856 assert_return(!event_pid_changed(e), -ECHILD);
2858 if (e->watchdog == !!b)
2862 struct epoll_event ev = {};
2864 r = sd_watchdog_enabled(false, &e->watchdog_period);
2868 /* Issue first ping immediately */
2869 sd_notify(false, "WATCHDOG=1");
2870 e->watchdog_last = now(CLOCK_MONOTONIC);
2872 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2873 if (e->watchdog_fd < 0)
2876 r = arm_watchdog(e);
2880 ev.events = EPOLLIN;
2881 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2883 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2890 if (e->watchdog_fd >= 0) {
2891 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2892 e->watchdog_fd = safe_close(e->watchdog_fd);
2900 e->watchdog_fd = safe_close(e->watchdog_fd);
2904 #if 0 /// UNNEEDED by elogind
2905 _public_ int sd_event_get_watchdog(sd_event *e) {
2906 assert_return(e, -EINVAL);
2907 assert_return(!event_pid_changed(e), -ECHILD);
2913 _public_ int sd_event_get_iteration(sd_event *e, uint64_t *ret) {
2914 assert_return(e, -EINVAL);
2915 assert_return(!event_pid_changed(e), -ECHILD);
2917 *ret = e->iteration;