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>
27 #include "sd-daemon.h"
32 #include "time-util.h"
36 #include "signal-util.h"
40 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
42 typedef enum EventSourceType {
46 SOURCE_TIME_MONOTONIC,
47 SOURCE_TIME_REALTIME_ALARM,
48 SOURCE_TIME_BOOTTIME_ALARM,
55 _SOURCE_EVENT_SOURCE_TYPE_MAX,
56 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
59 /* All objects we use in epoll events start with this value, so that
60 * we know how to dispatch it */
61 typedef enum WakeupType {
67 _WAKEUP_TYPE_INVALID = -1,
70 #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)
72 struct sd_event_source {
79 sd_event_handler_t prepare;
83 EventSourceType type:5;
90 unsigned pending_index;
91 unsigned prepare_index;
92 unsigned pending_iteration;
93 unsigned prepare_iteration;
95 LIST_FIELDS(sd_event_source, sources);
99 sd_event_io_handler_t callback;
106 sd_event_time_handler_t callback;
107 usec_t next, accuracy;
108 unsigned earliest_index;
109 unsigned latest_index;
112 sd_event_signal_handler_t callback;
113 struct signalfd_siginfo siginfo;
117 sd_event_child_handler_t callback;
123 sd_event_handler_t callback;
126 sd_event_handler_t callback;
129 sd_event_handler_t callback;
130 unsigned prioq_index;
139 /* For all clocks we maintain two priority queues each, one
140 * ordered for the earliest times the events may be
141 * dispatched, and one ordered by the latest times they must
142 * have been dispatched. The range between the top entries in
143 * the two prioqs is the time window we can freely schedule
156 /* For each priority we maintain one signal fd, so that we
157 * only have to dequeue a single event per priority at a
163 sd_event_source *current;
175 /* timerfd_create() only supports these five clocks so far. We
176 * can add support for more clocks when the kernel learns to
177 * deal with them, too. */
178 struct clock_data realtime;
179 struct clock_data boottime;
180 struct clock_data monotonic;
181 struct clock_data realtime_alarm;
182 struct clock_data boottime_alarm;
186 sd_event_source **signal_sources; /* indexed by signal number */
187 Hashmap *signal_data; /* indexed by priority */
189 Hashmap *child_sources;
190 unsigned n_enabled_child_sources;
199 dual_timestamp timestamp;
200 usec_t timestamp_boottime;
203 bool exit_requested:1;
204 bool need_process_child:1;
210 sd_event **default_event_ptr;
212 usec_t watchdog_last, watchdog_period;
216 LIST_HEAD(sd_event_source, sources);
219 static void source_disconnect(sd_event_source *s);
221 static int pending_prioq_compare(const void *a, const void *b) {
222 const sd_event_source *x = a, *y = b;
227 /* Enabled ones first */
228 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
230 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
233 /* Lower priority values first */
234 if (x->priority < y->priority)
236 if (x->priority > y->priority)
239 /* Older entries first */
240 if (x->pending_iteration < y->pending_iteration)
242 if (x->pending_iteration > y->pending_iteration)
248 static int prepare_prioq_compare(const void *a, const void *b) {
249 const sd_event_source *x = a, *y = b;
254 /* Enabled ones first */
255 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
257 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
260 /* Move most recently prepared ones last, so that we can stop
261 * preparing as soon as we hit one that has already been
262 * prepared in the current iteration */
263 if (x->prepare_iteration < y->prepare_iteration)
265 if (x->prepare_iteration > y->prepare_iteration)
268 /* Lower priority values first */
269 if (x->priority < y->priority)
271 if (x->priority > y->priority)
277 static int earliest_time_prioq_compare(const void *a, const void *b) {
278 const sd_event_source *x = a, *y = b;
280 assert(EVENT_SOURCE_IS_TIME(x->type));
281 assert(x->type == y->type);
283 /* Enabled ones first */
284 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
286 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
289 /* Move the pending ones to the end */
290 if (!x->pending && y->pending)
292 if (x->pending && !y->pending)
296 if (x->time.next < y->time.next)
298 if (x->time.next > y->time.next)
304 static int latest_time_prioq_compare(const void *a, const void *b) {
305 const sd_event_source *x = a, *y = b;
307 assert(EVENT_SOURCE_IS_TIME(x->type));
308 assert(x->type == y->type);
310 /* Enabled ones first */
311 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
313 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
316 /* Move the pending ones to the end */
317 if (!x->pending && y->pending)
319 if (x->pending && !y->pending)
323 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
325 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
331 static int exit_prioq_compare(const void *a, const void *b) {
332 const sd_event_source *x = a, *y = b;
334 assert(x->type == SOURCE_EXIT);
335 assert(y->type == SOURCE_EXIT);
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 /* Lower priority values first */
344 if (x->priority < y->priority)
346 if (x->priority > y->priority)
352 static void free_clock_data(struct clock_data *d) {
354 assert(d->wakeup == WAKEUP_CLOCK_DATA);
357 prioq_free(d->earliest);
358 prioq_free(d->latest);
361 static void event_free(sd_event *e) {
366 while ((s = e->sources)) {
368 source_disconnect(s);
369 sd_event_source_unref(s);
372 assert(e->n_sources == 0);
374 if (e->default_event_ptr)
375 *(e->default_event_ptr) = NULL;
377 safe_close(e->epoll_fd);
378 safe_close(e->watchdog_fd);
380 free_clock_data(&e->realtime);
381 free_clock_data(&e->boottime);
382 free_clock_data(&e->monotonic);
383 free_clock_data(&e->realtime_alarm);
384 free_clock_data(&e->boottime_alarm);
386 prioq_free(e->pending);
387 prioq_free(e->prepare);
390 free(e->signal_sources);
391 hashmap_free(e->signal_data);
393 hashmap_free(e->child_sources);
394 set_free(e->post_sources);
398 _public_ int sd_event_new(sd_event** ret) {
402 assert_return(ret, -EINVAL);
404 e = new0(sd_event, 1);
409 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
410 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
411 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
412 e->original_pid = getpid();
413 e->perturb = USEC_INFINITY;
415 e->pending = prioq_new(pending_prioq_compare);
421 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
422 if (e->epoll_fd < 0) {
435 _public_ sd_event* sd_event_ref(sd_event *e) {
436 assert_return(e, NULL);
438 assert(e->n_ref >= 1);
444 _public_ sd_event* sd_event_unref(sd_event *e) {
449 assert(e->n_ref >= 1);
458 static bool event_pid_changed(sd_event *e) {
461 /* We don't support people creating an event loop and keeping
462 * it around over a fork(). Let's complain. */
464 return e->original_pid != getpid();
467 static void source_io_unregister(sd_event_source *s) {
471 assert(s->type == SOURCE_IO);
473 if (event_pid_changed(s->event))
476 if (!s->io.registered)
479 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
481 log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
483 s->io.registered = false;
486 static int source_io_register(
491 struct epoll_event ev = {};
495 assert(s->type == SOURCE_IO);
496 assert(enabled != SD_EVENT_OFF);
501 if (enabled == SD_EVENT_ONESHOT)
502 ev.events |= EPOLLONESHOT;
504 if (s->io.registered)
505 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
507 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
511 s->io.registered = true;
516 /// UNNEEDED by elogind
518 static clockid_t event_source_type_to_clock(EventSourceType t) {
522 case SOURCE_TIME_REALTIME:
523 return CLOCK_REALTIME;
525 case SOURCE_TIME_BOOTTIME:
526 return CLOCK_BOOTTIME;
528 case SOURCE_TIME_MONOTONIC:
529 return CLOCK_MONOTONIC;
531 case SOURCE_TIME_REALTIME_ALARM:
532 return CLOCK_REALTIME_ALARM;
534 case SOURCE_TIME_BOOTTIME_ALARM:
535 return CLOCK_BOOTTIME_ALARM;
538 return (clockid_t) -1;
543 static EventSourceType clock_to_event_source_type(clockid_t clock) {
548 return SOURCE_TIME_REALTIME;
551 return SOURCE_TIME_BOOTTIME;
553 case CLOCK_MONOTONIC:
554 return SOURCE_TIME_MONOTONIC;
556 case CLOCK_REALTIME_ALARM:
557 return SOURCE_TIME_REALTIME_ALARM;
559 case CLOCK_BOOTTIME_ALARM:
560 return SOURCE_TIME_BOOTTIME_ALARM;
563 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
567 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
572 case SOURCE_TIME_REALTIME:
575 case SOURCE_TIME_BOOTTIME:
578 case SOURCE_TIME_MONOTONIC:
579 return &e->monotonic;
581 case SOURCE_TIME_REALTIME_ALARM:
582 return &e->realtime_alarm;
584 case SOURCE_TIME_BOOTTIME_ALARM:
585 return &e->boottime_alarm;
592 static int event_make_signal_data(
595 struct signal_data **ret) {
597 struct epoll_event ev = {};
598 struct signal_data *d;
606 if (event_pid_changed(e))
609 if (e->signal_sources && e->signal_sources[sig])
610 priority = e->signal_sources[sig]->priority;
614 d = hashmap_get(e->signal_data, &priority);
616 if (sigismember(&d->sigset, sig) > 0) {
622 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
626 d = new0(struct signal_data, 1);
630 d->wakeup = WAKEUP_SIGNAL_DATA;
632 d->priority = priority;
634 r = hashmap_put(e->signal_data, &d->priority, d);
642 assert_se(sigaddset(&ss_copy, sig) >= 0);
644 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
663 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
676 d->fd = safe_close(d->fd);
677 hashmap_remove(e->signal_data, &d->priority);
684 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
688 /* Turns off the specified signal in the signal data
689 * object. If the signal mask of the object becomes empty that
692 if (sigismember(&d->sigset, sig) == 0)
695 assert_se(sigdelset(&d->sigset, sig) >= 0);
697 if (sigisemptyset(&d->sigset)) {
699 /* If all the mask is all-zero we can get rid of the structure */
700 hashmap_remove(e->signal_data, &d->priority);
709 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
710 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
713 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
714 struct signal_data *d;
715 static const int64_t zero_priority = 0;
719 /* Rechecks if the specified signal is still something we are
720 * interested in. If not, we'll unmask it, and possibly drop
721 * the signalfd for it. */
723 if (sig == SIGCHLD &&
724 e->n_enabled_child_sources > 0)
727 if (e->signal_sources &&
728 e->signal_sources[sig] &&
729 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
733 * The specified signal might be enabled in three different queues:
735 * 1) the one that belongs to the priority passed (if it is non-NULL)
736 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
737 * 3) the 0 priority (to cover the SIGCHLD case)
739 * Hence, let's remove it from all three here.
743 d = hashmap_get(e->signal_data, priority);
745 event_unmask_signal_data(e, d, sig);
748 if (e->signal_sources && e->signal_sources[sig]) {
749 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
751 event_unmask_signal_data(e, d, sig);
754 d = hashmap_get(e->signal_data, &zero_priority);
756 event_unmask_signal_data(e, d, sig);
759 static void source_disconnect(sd_event_source *s) {
767 assert(s->event->n_sources > 0);
773 source_io_unregister(s);
777 case SOURCE_TIME_REALTIME:
778 case SOURCE_TIME_BOOTTIME:
779 case SOURCE_TIME_MONOTONIC:
780 case SOURCE_TIME_REALTIME_ALARM:
781 case SOURCE_TIME_BOOTTIME_ALARM: {
782 struct clock_data *d;
784 d = event_get_clock_data(s->event, s->type);
787 prioq_remove(d->earliest, s, &s->time.earliest_index);
788 prioq_remove(d->latest, s, &s->time.latest_index);
789 d->needs_rearm = true;
794 if (s->signal.sig > 0) {
796 if (s->event->signal_sources)
797 s->event->signal_sources[s->signal.sig] = NULL;
799 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
805 if (s->child.pid > 0) {
806 if (s->enabled != SD_EVENT_OFF) {
807 assert(s->event->n_enabled_child_sources > 0);
808 s->event->n_enabled_child_sources--;
811 (void) hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
812 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
822 set_remove(s->event->post_sources, s);
826 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
830 assert_not_reached("Wut? I shouldn't exist.");
834 prioq_remove(s->event->pending, s, &s->pending_index);
837 prioq_remove(s->event->prepare, s, &s->prepare_index);
841 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
843 LIST_REMOVE(sources, event->sources, s);
847 sd_event_unref(event);
850 static void source_free(sd_event_source *s) {
853 source_disconnect(s);
854 free(s->description);
858 static int source_set_pending(sd_event_source *s, bool b) {
862 assert(s->type != SOURCE_EXIT);
870 s->pending_iteration = s->event->iteration;
872 r = prioq_put(s->event->pending, s, &s->pending_index);
878 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
880 if (EVENT_SOURCE_IS_TIME(s->type)) {
881 struct clock_data *d;
883 d = event_get_clock_data(s->event, s->type);
886 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
887 prioq_reshuffle(d->latest, s, &s->time.latest_index);
888 d->needs_rearm = true;
891 if (s->type == SOURCE_SIGNAL && !b) {
892 struct signal_data *d;
894 d = hashmap_get(s->event->signal_data, &s->priority);
895 if (d && d->current == s)
902 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
907 s = new0(sd_event_source, 1);
913 s->floating = floating;
915 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
920 LIST_PREPEND(sources, e->sources, s);
926 _public_ int sd_event_add_io(
928 sd_event_source **ret,
931 sd_event_io_handler_t callback,
937 assert_return(e, -EINVAL);
938 assert_return(fd >= 0, -EBADF);
939 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
940 assert_return(callback, -EINVAL);
941 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
942 assert_return(!event_pid_changed(e), -ECHILD);
944 s = source_new(e, !ret, SOURCE_IO);
948 s->wakeup = WAKEUP_EVENT_SOURCE;
950 s->io.events = events;
951 s->io.callback = callback;
952 s->userdata = userdata;
953 s->enabled = SD_EVENT_ON;
955 r = source_io_register(s, s->enabled, events);
967 static void initialize_perturb(sd_event *e) {
968 sd_id128_t bootid = {};
970 /* When we sleep for longer, we try to realign the wakeup to
971 the same time wihtin each minute/second/250ms, so that
972 events all across the system can be coalesced into a single
973 CPU wakeup. However, let's take some system-specific
974 randomness for this value, so that in a network of systems
975 with synced clocks timer events are distributed a
976 bit. Here, we calculate a perturbation usec offset from the
979 if (_likely_(e->perturb != USEC_INFINITY))
982 if (sd_id128_get_boot(&bootid) >= 0)
983 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
986 static int event_setup_timer_fd(
988 struct clock_data *d,
991 struct epoll_event ev = {};
997 if (_likely_(d->fd >= 0))
1000 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1004 ev.events = EPOLLIN;
1007 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1017 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1020 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1023 _public_ int sd_event_add_time(
1025 sd_event_source **ret,
1029 sd_event_time_handler_t callback,
1032 EventSourceType type;
1034 struct clock_data *d;
1037 assert_return(e, -EINVAL);
1038 assert_return(usec != (uint64_t) -1, -EINVAL);
1039 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1040 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1041 assert_return(!event_pid_changed(e), -ECHILD);
1044 callback = time_exit_callback;
1046 type = clock_to_event_source_type(clock);
1047 assert_return(type >= 0, -EOPNOTSUPP);
1049 d = event_get_clock_data(e, type);
1053 d->earliest = prioq_new(earliest_time_prioq_compare);
1059 d->latest = prioq_new(latest_time_prioq_compare);
1065 r = event_setup_timer_fd(e, d, clock);
1070 s = source_new(e, !ret, type);
1074 s->time.next = usec;
1075 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1076 s->time.callback = callback;
1077 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1078 s->userdata = userdata;
1079 s->enabled = SD_EVENT_ONESHOT;
1081 d->needs_rearm = true;
1083 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1087 r = prioq_put(d->latest, s, &s->time.latest_index);
1101 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1104 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1107 _public_ int sd_event_add_signal(
1109 sd_event_source **ret,
1111 sd_event_signal_handler_t callback,
1115 struct signal_data *d;
1119 assert_return(e, -EINVAL);
1120 assert_return(sig > 0, -EINVAL);
1121 assert_return(sig < _NSIG, -EINVAL);
1122 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1123 assert_return(!event_pid_changed(e), -ECHILD);
1126 callback = signal_exit_callback;
1128 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1132 if (!sigismember(&ss, sig))
1135 if (!e->signal_sources) {
1136 e->signal_sources = new0(sd_event_source*, _NSIG);
1137 if (!e->signal_sources)
1139 } else if (e->signal_sources[sig])
1142 s = source_new(e, !ret, SOURCE_SIGNAL);
1146 s->signal.sig = sig;
1147 s->signal.callback = callback;
1148 s->userdata = userdata;
1149 s->enabled = SD_EVENT_ON;
1151 e->signal_sources[sig] = s;
1153 r = event_make_signal_data(e, sig, &d);
1159 /* Use the signal name as description for the event source by default */
1160 (void) sd_event_source_set_description(s, signal_to_string(sig));
1168 /// UNNEEDED by elogind
1170 _public_ int sd_event_add_child(
1172 sd_event_source **ret,
1175 sd_event_child_handler_t callback,
1181 assert_return(e, -EINVAL);
1182 assert_return(pid > 1, -EINVAL);
1183 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1184 assert_return(options != 0, -EINVAL);
1185 assert_return(callback, -EINVAL);
1186 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1187 assert_return(!event_pid_changed(e), -ECHILD);
1189 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1193 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1196 s = source_new(e, !ret, SOURCE_CHILD);
1201 s->child.options = options;
1202 s->child.callback = callback;
1203 s->userdata = userdata;
1204 s->enabled = SD_EVENT_ONESHOT;
1206 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1212 e->n_enabled_child_sources ++;
1214 r = event_make_signal_data(e, SIGCHLD, NULL);
1216 e->n_enabled_child_sources--;
1221 e->need_process_child = true;
1229 _public_ int sd_event_add_defer(
1231 sd_event_source **ret,
1232 sd_event_handler_t callback,
1238 assert_return(e, -EINVAL);
1239 assert_return(callback, -EINVAL);
1240 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1241 assert_return(!event_pid_changed(e), -ECHILD);
1243 s = source_new(e, !ret, SOURCE_DEFER);
1247 s->defer.callback = callback;
1248 s->userdata = userdata;
1249 s->enabled = SD_EVENT_ONESHOT;
1251 r = source_set_pending(s, true);
1264 _public_ int sd_event_add_post(
1266 sd_event_source **ret,
1267 sd_event_handler_t callback,
1273 assert_return(e, -EINVAL);
1274 assert_return(callback, -EINVAL);
1275 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1276 assert_return(!event_pid_changed(e), -ECHILD);
1278 r = set_ensure_allocated(&e->post_sources, NULL);
1282 s = source_new(e, !ret, SOURCE_POST);
1286 s->post.callback = callback;
1287 s->userdata = userdata;
1288 s->enabled = SD_EVENT_ON;
1290 r = set_put(e->post_sources, s);
1302 _public_ int sd_event_add_exit(
1304 sd_event_source **ret,
1305 sd_event_handler_t callback,
1311 assert_return(e, -EINVAL);
1312 assert_return(callback, -EINVAL);
1313 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1314 assert_return(!event_pid_changed(e), -ECHILD);
1317 e->exit = prioq_new(exit_prioq_compare);
1322 s = source_new(e, !ret, SOURCE_EXIT);
1326 s->exit.callback = callback;
1327 s->userdata = userdata;
1328 s->exit.prioq_index = PRIOQ_IDX_NULL;
1329 s->enabled = SD_EVENT_ONESHOT;
1331 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1343 /// UNNEEDED by elogind
1345 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1346 assert_return(s, NULL);
1348 assert(s->n_ref >= 1);
1355 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1360 assert(s->n_ref >= 1);
1363 if (s->n_ref <= 0) {
1364 /* Here's a special hack: when we are called from a
1365 * dispatch handler we won't free the event source
1366 * immediately, but we will detach the fd from the
1367 * epoll. This way it is safe for the caller to unref
1368 * the event source and immediately close the fd, but
1369 * we still retain a valid event source object after
1372 if (s->dispatching) {
1373 if (s->type == SOURCE_IO)
1374 source_io_unregister(s);
1376 source_disconnect(s);
1384 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1385 assert_return(s, -EINVAL);
1386 assert_return(!event_pid_changed(s->event), -ECHILD);
1388 return free_and_strdup(&s->description, description);
1391 /// UNNEEDED by elogind
1393 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1394 assert_return(s, -EINVAL);
1395 assert_return(description, -EINVAL);
1396 assert_return(s->description, -ENXIO);
1397 assert_return(!event_pid_changed(s->event), -ECHILD);
1399 *description = s->description;
1404 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1405 assert_return(s, NULL);
1410 /// UNNEEDED by elogind
1412 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1413 assert_return(s, -EINVAL);
1414 assert_return(s->type != SOURCE_EXIT, -EDOM);
1415 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1416 assert_return(!event_pid_changed(s->event), -ECHILD);
1421 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1422 assert_return(s, -EINVAL);
1423 assert_return(s->type == SOURCE_IO, -EDOM);
1424 assert_return(!event_pid_changed(s->event), -ECHILD);
1430 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1433 assert_return(s, -EINVAL);
1434 assert_return(fd >= 0, -EBADF);
1435 assert_return(s->type == SOURCE_IO, -EDOM);
1436 assert_return(!event_pid_changed(s->event), -ECHILD);
1441 if (s->enabled == SD_EVENT_OFF) {
1443 s->io.registered = false;
1447 saved_fd = s->io.fd;
1448 assert(s->io.registered);
1451 s->io.registered = false;
1453 r = source_io_register(s, s->enabled, s->io.events);
1455 s->io.fd = saved_fd;
1456 s->io.registered = true;
1460 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1466 /// UNNEEDED by elogind
1468 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1469 assert_return(s, -EINVAL);
1470 assert_return(events, -EINVAL);
1471 assert_return(s->type == SOURCE_IO, -EDOM);
1472 assert_return(!event_pid_changed(s->event), -ECHILD);
1474 *events = s->io.events;
1479 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1482 assert_return(s, -EINVAL);
1483 assert_return(s->type == SOURCE_IO, -EDOM);
1484 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1485 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1486 assert_return(!event_pid_changed(s->event), -ECHILD);
1488 /* edge-triggered updates are never skipped, so we can reset edges */
1489 if (s->io.events == events && !(events & EPOLLET))
1492 if (s->enabled != SD_EVENT_OFF) {
1493 r = source_io_register(s, s->enabled, events);
1498 s->io.events = events;
1499 source_set_pending(s, false);
1504 /// UNNEEDED by elogind
1506 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1507 assert_return(s, -EINVAL);
1508 assert_return(revents, -EINVAL);
1509 assert_return(s->type == SOURCE_IO, -EDOM);
1510 assert_return(s->pending, -ENODATA);
1511 assert_return(!event_pid_changed(s->event), -ECHILD);
1513 *revents = s->io.revents;
1517 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1518 assert_return(s, -EINVAL);
1519 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1520 assert_return(!event_pid_changed(s->event), -ECHILD);
1522 return s->signal.sig;
1525 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1526 assert_return(s, -EINVAL);
1527 assert_return(!event_pid_changed(s->event), -ECHILD);
1533 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1536 assert_return(s, -EINVAL);
1537 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1538 assert_return(!event_pid_changed(s->event), -ECHILD);
1540 if (s->priority == priority)
1543 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1544 struct signal_data *old, *d;
1546 /* Move us from the signalfd belonging to the old
1547 * priority to the signalfd of the new priority */
1549 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1551 s->priority = priority;
1553 r = event_make_signal_data(s->event, s->signal.sig, &d);
1555 s->priority = old->priority;
1559 event_unmask_signal_data(s->event, old, s->signal.sig);
1561 s->priority = priority;
1564 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1567 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1569 if (s->type == SOURCE_EXIT)
1570 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1575 /// UNNEEDED by elogind
1577 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1578 assert_return(s, -EINVAL);
1579 assert_return(m, -EINVAL);
1580 assert_return(!event_pid_changed(s->event), -ECHILD);
1587 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1590 assert_return(s, -EINVAL);
1591 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1592 assert_return(!event_pid_changed(s->event), -ECHILD);
1594 /* If we are dead anyway, we are fine with turning off
1595 * sources, but everything else needs to fail. */
1596 if (s->event->state == SD_EVENT_FINISHED)
1597 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1599 if (s->enabled == m)
1602 if (m == SD_EVENT_OFF) {
1607 source_io_unregister(s);
1611 case SOURCE_TIME_REALTIME:
1612 case SOURCE_TIME_BOOTTIME:
1613 case SOURCE_TIME_MONOTONIC:
1614 case SOURCE_TIME_REALTIME_ALARM:
1615 case SOURCE_TIME_BOOTTIME_ALARM: {
1616 struct clock_data *d;
1619 d = event_get_clock_data(s->event, s->type);
1622 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1623 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1624 d->needs_rearm = true;
1631 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1637 assert(s->event->n_enabled_child_sources > 0);
1638 s->event->n_enabled_child_sources--;
1640 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1645 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1654 assert_not_reached("Wut? I shouldn't exist.");
1661 r = source_io_register(s, m, s->io.events);
1668 case SOURCE_TIME_REALTIME:
1669 case SOURCE_TIME_BOOTTIME:
1670 case SOURCE_TIME_MONOTONIC:
1671 case SOURCE_TIME_REALTIME_ALARM:
1672 case SOURCE_TIME_BOOTTIME_ALARM: {
1673 struct clock_data *d;
1676 d = event_get_clock_data(s->event, s->type);
1679 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1680 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1681 d->needs_rearm = true;
1689 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1691 s->enabled = SD_EVENT_OFF;
1692 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1700 if (s->enabled == SD_EVENT_OFF)
1701 s->event->n_enabled_child_sources++;
1705 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1707 s->enabled = SD_EVENT_OFF;
1708 s->event->n_enabled_child_sources--;
1709 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1717 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1726 assert_not_reached("Wut? I shouldn't exist.");
1731 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1734 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1739 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1740 assert_return(s, -EINVAL);
1741 assert_return(usec, -EINVAL);
1742 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1743 assert_return(!event_pid_changed(s->event), -ECHILD);
1745 *usec = s->time.next;
1749 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1750 struct clock_data *d;
1752 assert_return(s, -EINVAL);
1753 assert_return(usec != (uint64_t) -1, -EINVAL);
1754 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1755 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1756 assert_return(!event_pid_changed(s->event), -ECHILD);
1758 s->time.next = usec;
1760 source_set_pending(s, false);
1762 d = event_get_clock_data(s->event, s->type);
1765 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1766 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1767 d->needs_rearm = true;
1772 /// UNNEEDED by elogind
1774 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1775 assert_return(s, -EINVAL);
1776 assert_return(usec, -EINVAL);
1777 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1778 assert_return(!event_pid_changed(s->event), -ECHILD);
1780 *usec = s->time.accuracy;
1784 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1785 struct clock_data *d;
1787 assert_return(s, -EINVAL);
1788 assert_return(usec != (uint64_t) -1, -EINVAL);
1789 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1790 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1791 assert_return(!event_pid_changed(s->event), -ECHILD);
1794 usec = DEFAULT_ACCURACY_USEC;
1796 s->time.accuracy = usec;
1798 source_set_pending(s, false);
1800 d = event_get_clock_data(s->event, s->type);
1803 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1804 d->needs_rearm = true;
1809 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1810 assert_return(s, -EINVAL);
1811 assert_return(clock, -EINVAL);
1812 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1813 assert_return(!event_pid_changed(s->event), -ECHILD);
1815 *clock = event_source_type_to_clock(s->type);
1819 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1820 assert_return(s, -EINVAL);
1821 assert_return(pid, -EINVAL);
1822 assert_return(s->type == SOURCE_CHILD, -EDOM);
1823 assert_return(!event_pid_changed(s->event), -ECHILD);
1825 *pid = s->child.pid;
1830 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1833 assert_return(s, -EINVAL);
1834 assert_return(s->type != SOURCE_EXIT, -EDOM);
1835 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1836 assert_return(!event_pid_changed(s->event), -ECHILD);
1838 if (s->prepare == callback)
1841 if (callback && s->prepare) {
1842 s->prepare = callback;
1846 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1850 s->prepare = callback;
1853 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1857 prioq_remove(s->event->prepare, s, &s->prepare_index);
1862 /// UNNEEDED by elogind
1864 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1865 assert_return(s, NULL);
1870 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1873 assert_return(s, NULL);
1876 s->userdata = userdata;
1882 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1893 initialize_perturb(e);
1896 Find a good time to wake up again between times a and b. We
1897 have two goals here:
1899 a) We want to wake up as seldom as possible, hence prefer
1900 later times over earlier times.
1902 b) But if we have to wake up, then let's make sure to
1903 dispatch as much as possible on the entire system.
1905 We implement this by waking up everywhere at the same time
1906 within any given minute if we can, synchronised via the
1907 perturbation value determined from the boot ID. If we can't,
1908 then we try to find the same spot in every 10s, then 1s and
1909 then 250ms step. Otherwise, we pick the last possible time
1913 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1915 if (_unlikely_(c < USEC_PER_MINUTE))
1918 c -= USEC_PER_MINUTE;
1924 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1926 if (_unlikely_(c < USEC_PER_SEC*10))
1929 c -= USEC_PER_SEC*10;
1935 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1937 if (_unlikely_(c < USEC_PER_SEC))
1946 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1948 if (_unlikely_(c < USEC_PER_MSEC*250))
1951 c -= USEC_PER_MSEC*250;
1960 static int event_arm_timer(
1962 struct clock_data *d) {
1964 struct itimerspec its = {};
1965 sd_event_source *a, *b;
1972 if (!d->needs_rearm)
1975 d->needs_rearm = false;
1977 a = prioq_peek(d->earliest);
1978 if (!a || a->enabled == SD_EVENT_OFF) {
1983 if (d->next == USEC_INFINITY)
1987 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1991 d->next = USEC_INFINITY;
1995 b = prioq_peek(d->latest);
1996 assert_se(b && b->enabled != SD_EVENT_OFF);
1998 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
2002 assert_se(d->fd >= 0);
2005 /* We don' want to disarm here, just mean some time looooong ago. */
2006 its.it_value.tv_sec = 0;
2007 its.it_value.tv_nsec = 1;
2009 timespec_store(&its.it_value, t);
2011 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2019 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2022 assert(s->type == SOURCE_IO);
2024 /* If the event source was already pending, we just OR in the
2025 * new revents, otherwise we reset the value. The ORing is
2026 * necessary to handle EPOLLONESHOT events properly where
2027 * readability might happen independently of writability, and
2028 * we need to keep track of both */
2031 s->io.revents |= revents;
2033 s->io.revents = revents;
2035 return source_set_pending(s, true);
2038 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2045 assert_return(events == EPOLLIN, -EIO);
2047 ss = read(fd, &x, sizeof(x));
2049 if (errno == EAGAIN || errno == EINTR)
2055 if (_unlikely_(ss != sizeof(x)))
2059 *next = USEC_INFINITY;
2064 static int process_timer(
2067 struct clock_data *d) {
2076 s = prioq_peek(d->earliest);
2079 s->enabled == SD_EVENT_OFF ||
2083 r = source_set_pending(s, true);
2087 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2088 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2089 d->needs_rearm = true;
2095 static int process_child(sd_event *e) {
2102 e->need_process_child = false;
2105 So, this is ugly. We iteratively invoke waitid() with P_PID
2106 + WNOHANG for each PID we wait for, instead of using
2107 P_ALL. This is because we only want to get child
2108 information of very specific child processes, and not all
2109 of them. We might not have processed the SIGCHLD even of a
2110 previous invocation and we don't want to maintain a
2111 unbounded *per-child* event queue, hence we really don't
2112 want anything flushed out of the kernel's queue that we
2113 don't care about. Since this is O(n) this means that if you
2114 have a lot of processes you probably want to handle SIGCHLD
2117 We do not reap the children here (by using WNOWAIT), this
2118 is only done after the event source is dispatched so that
2119 the callback still sees the process as a zombie.
2122 HASHMAP_FOREACH(s, e->child_sources, i) {
2123 assert(s->type == SOURCE_CHILD);
2128 if (s->enabled == SD_EVENT_OFF)
2131 zero(s->child.siginfo);
2132 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2133 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2137 if (s->child.siginfo.si_pid != 0) {
2139 s->child.siginfo.si_code == CLD_EXITED ||
2140 s->child.siginfo.si_code == CLD_KILLED ||
2141 s->child.siginfo.si_code == CLD_DUMPED;
2143 if (!zombie && (s->child.options & WEXITED)) {
2144 /* If the child isn't dead then let's
2145 * immediately remove the state change
2146 * from the queue, since there's no
2147 * benefit in leaving it queued */
2149 assert(s->child.options & (WSTOPPED|WCONTINUED));
2150 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2153 r = source_set_pending(s, true);
2162 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2163 bool read_one = false;
2167 assert_return(events == EPOLLIN, -EIO);
2169 /* If there's a signal queued on this priority and SIGCHLD is
2170 on this priority too, then make sure to recheck the
2171 children we watch. This is because we only ever dequeue
2172 the first signal per priority, and if we dequeue one, and
2173 SIGCHLD might be enqueued later we wouldn't know, but we
2174 might have higher priority children we care about hence we
2175 need to check that explicitly. */
2177 if (sigismember(&d->sigset, SIGCHLD))
2178 e->need_process_child = true;
2180 /* If there's already an event source pending for this
2181 * priority we don't read another */
2186 struct signalfd_siginfo si;
2188 sd_event_source *s = NULL;
2190 n = read(d->fd, &si, sizeof(si));
2192 if (errno == EAGAIN || errno == EINTR)
2198 if (_unlikely_(n != sizeof(si)))
2201 assert(si.ssi_signo < _NSIG);
2205 if (e->signal_sources)
2206 s = e->signal_sources[si.ssi_signo];
2212 s->signal.siginfo = si;
2215 r = source_set_pending(s, true);
2223 static int source_dispatch(sd_event_source *s) {
2227 assert(s->pending || s->type == SOURCE_EXIT);
2229 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2230 r = source_set_pending(s, false);
2235 if (s->type != SOURCE_POST) {
2239 /* If we execute a non-post source, let's mark all
2240 * post sources as pending */
2242 SET_FOREACH(z, s->event->post_sources, i) {
2243 if (z->enabled == SD_EVENT_OFF)
2246 r = source_set_pending(z, true);
2252 if (s->enabled == SD_EVENT_ONESHOT) {
2253 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2258 s->dispatching = true;
2263 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2266 case SOURCE_TIME_REALTIME:
2267 case SOURCE_TIME_BOOTTIME:
2268 case SOURCE_TIME_MONOTONIC:
2269 case SOURCE_TIME_REALTIME_ALARM:
2270 case SOURCE_TIME_BOOTTIME_ALARM:
2271 r = s->time.callback(s, s->time.next, s->userdata);
2275 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2278 case SOURCE_CHILD: {
2281 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2282 s->child.siginfo.si_code == CLD_KILLED ||
2283 s->child.siginfo.si_code == CLD_DUMPED;
2285 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2287 /* Now, reap the PID for good. */
2289 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2295 r = s->defer.callback(s, s->userdata);
2299 r = s->post.callback(s, s->userdata);
2303 r = s->exit.callback(s, s->userdata);
2306 case SOURCE_WATCHDOG:
2307 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2308 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2309 assert_not_reached("Wut? I shouldn't exist.");
2312 s->dispatching = false;
2316 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2318 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2324 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2329 static int event_prepare(sd_event *e) {
2337 s = prioq_peek(e->prepare);
2338 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2341 s->prepare_iteration = e->iteration;
2342 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2348 s->dispatching = true;
2349 r = s->prepare(s, s->userdata);
2350 s->dispatching = false;
2354 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2356 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2362 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2368 static int dispatch_exit(sd_event *e) {
2374 p = prioq_peek(e->exit);
2375 if (!p || p->enabled == SD_EVENT_OFF) {
2376 e->state = SD_EVENT_FINISHED;
2382 e->state = SD_EVENT_EXITING;
2384 r = source_dispatch(p);
2386 e->state = SD_EVENT_INITIAL;
2392 static sd_event_source* event_next_pending(sd_event *e) {
2397 p = prioq_peek(e->pending);
2401 if (p->enabled == SD_EVENT_OFF)
2407 static int arm_watchdog(sd_event *e) {
2408 struct itimerspec its = {};
2413 assert(e->watchdog_fd >= 0);
2415 t = sleep_between(e,
2416 e->watchdog_last + (e->watchdog_period / 2),
2417 e->watchdog_last + (e->watchdog_period * 3 / 4));
2419 timespec_store(&its.it_value, t);
2421 /* Make sure we never set the watchdog to 0, which tells the
2422 * kernel to disable it. */
2423 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2424 its.it_value.tv_nsec = 1;
2426 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2433 static int process_watchdog(sd_event *e) {
2439 /* Don't notify watchdog too often */
2440 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2443 sd_notify(false, "WATCHDOG=1");
2444 e->watchdog_last = e->timestamp.monotonic;
2446 return arm_watchdog(e);
2449 _public_ int sd_event_prepare(sd_event *e) {
2452 assert_return(e, -EINVAL);
2453 assert_return(!event_pid_changed(e), -ECHILD);
2454 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2455 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2457 if (e->exit_requested)
2462 r = event_prepare(e);
2466 r = event_arm_timer(e, &e->realtime);
2470 r = event_arm_timer(e, &e->boottime);
2474 r = event_arm_timer(e, &e->monotonic);
2478 r = event_arm_timer(e, &e->realtime_alarm);
2482 r = event_arm_timer(e, &e->boottime_alarm);
2486 if (event_next_pending(e) || e->need_process_child)
2489 e->state = SD_EVENT_ARMED;
2494 e->state = SD_EVENT_ARMED;
2495 r = sd_event_wait(e, 0);
2497 e->state = SD_EVENT_ARMED;
2502 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2503 struct epoll_event *ev_queue;
2504 unsigned ev_queue_max;
2507 assert_return(e, -EINVAL);
2508 assert_return(!event_pid_changed(e), -ECHILD);
2509 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2510 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2512 if (e->exit_requested) {
2513 e->state = SD_EVENT_PENDING;
2517 ev_queue_max = MAX(e->n_sources, 1u);
2518 ev_queue = newa(struct epoll_event, ev_queue_max);
2520 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2521 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2523 if (errno == EINTR) {
2524 e->state = SD_EVENT_PENDING;
2532 dual_timestamp_get(&e->timestamp);
2533 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2535 for (i = 0; i < m; i++) {
2537 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2538 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2540 WakeupType *t = ev_queue[i].data.ptr;
2544 case WAKEUP_EVENT_SOURCE:
2545 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2548 case WAKEUP_CLOCK_DATA: {
2549 struct clock_data *d = ev_queue[i].data.ptr;
2550 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2554 case WAKEUP_SIGNAL_DATA:
2555 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2559 assert_not_reached("Invalid wake-up pointer");
2566 r = process_watchdog(e);
2570 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2574 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2578 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2582 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2586 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2590 if (e->need_process_child) {
2591 r = process_child(e);
2596 if (event_next_pending(e)) {
2597 e->state = SD_EVENT_PENDING;
2605 e->state = SD_EVENT_INITIAL;
2610 _public_ int sd_event_dispatch(sd_event *e) {
2614 assert_return(e, -EINVAL);
2615 assert_return(!event_pid_changed(e), -ECHILD);
2616 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2617 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2619 if (e->exit_requested)
2620 return dispatch_exit(e);
2622 p = event_next_pending(e);
2626 e->state = SD_EVENT_RUNNING;
2627 r = source_dispatch(p);
2628 e->state = SD_EVENT_INITIAL;
2635 e->state = SD_EVENT_INITIAL;
2640 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2643 assert_return(e, -EINVAL);
2644 assert_return(!event_pid_changed(e), -ECHILD);
2645 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2646 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2648 r = sd_event_prepare(e);
2650 /* There was nothing? Then wait... */
2651 r = sd_event_wait(e, timeout);
2654 /* There's something now, then let's dispatch it */
2655 r = sd_event_dispatch(e);
2665 /// UNNEEDED by elogind
2667 _public_ int sd_event_loop(sd_event *e) {
2670 assert_return(e, -EINVAL);
2671 assert_return(!event_pid_changed(e), -ECHILD);
2672 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2676 while (e->state != SD_EVENT_FINISHED) {
2677 r = sd_event_run(e, (uint64_t) -1);
2689 _public_ int sd_event_get_fd(sd_event *e) {
2691 assert_return(e, -EINVAL);
2692 assert_return(!event_pid_changed(e), -ECHILD);
2698 _public_ int sd_event_get_state(sd_event *e) {
2699 assert_return(e, -EINVAL);
2700 assert_return(!event_pid_changed(e), -ECHILD);
2705 /// UNNEEDED by elogind
2707 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2708 assert_return(e, -EINVAL);
2709 assert_return(code, -EINVAL);
2710 assert_return(!event_pid_changed(e), -ECHILD);
2712 if (!e->exit_requested)
2715 *code = e->exit_code;
2720 _public_ int sd_event_exit(sd_event *e, int code) {
2721 assert_return(e, -EINVAL);
2722 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2723 assert_return(!event_pid_changed(e), -ECHILD);
2725 e->exit_requested = true;
2726 e->exit_code = code;
2731 /// UNNEEDED by elogind
2733 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2734 assert_return(e, -EINVAL);
2735 assert_return(usec, -EINVAL);
2736 assert_return(!event_pid_changed(e), -ECHILD);
2738 if (!dual_timestamp_is_set(&e->timestamp)) {
2739 /* Implicitly fall back to now() if we never ran
2740 * before and thus have no cached time. */
2747 case CLOCK_REALTIME:
2748 case CLOCK_REALTIME_ALARM:
2749 *usec = e->timestamp.realtime;
2752 case CLOCK_MONOTONIC:
2753 *usec = e->timestamp.monotonic;
2756 case CLOCK_BOOTTIME:
2757 case CLOCK_BOOTTIME_ALARM:
2758 *usec = e->timestamp_boottime;
2766 _public_ int sd_event_default(sd_event **ret) {
2768 static thread_local sd_event *default_event = NULL;
2773 return !!default_event;
2775 if (default_event) {
2776 *ret = sd_event_ref(default_event);
2780 r = sd_event_new(&e);
2784 e->default_event_ptr = &default_event;
2792 /// UNNEEDED by elogind
2794 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2795 assert_return(e, -EINVAL);
2796 assert_return(tid, -EINVAL);
2797 assert_return(!event_pid_changed(e), -ECHILD);
2808 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2811 assert_return(e, -EINVAL);
2812 assert_return(!event_pid_changed(e), -ECHILD);
2814 if (e->watchdog == !!b)
2818 struct epoll_event ev = {};
2820 r = sd_watchdog_enabled(false, &e->watchdog_period);
2824 /* Issue first ping immediately */
2825 sd_notify(false, "WATCHDOG=1");
2826 e->watchdog_last = now(CLOCK_MONOTONIC);
2828 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2829 if (e->watchdog_fd < 0)
2832 r = arm_watchdog(e);
2836 ev.events = EPOLLIN;
2837 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2839 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2846 if (e->watchdog_fd >= 0) {
2847 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2848 e->watchdog_fd = safe_close(e->watchdog_fd);
2856 e->watchdog_fd = safe_close(e->watchdog_fd);
2860 /// UNNEEDED by elogind
2862 _public_ int sd_event_get_watchdog(sd_event *e) {
2863 assert_return(e, -EINVAL);
2864 assert_return(!event_pid_changed(e), -ECHILD);