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)
245 /* Stability for the rest */
254 static int prepare_prioq_compare(const void *a, const void *b) {
255 const sd_event_source *x = a, *y = b;
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 /* Enabled ones first */
269 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
271 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
274 /* Lower priority values first */
275 if (x->priority < y->priority)
277 if (x->priority > y->priority)
280 /* Stability for the rest */
289 static int earliest_time_prioq_compare(const void *a, const void *b) {
290 const sd_event_source *x = a, *y = b;
292 assert(EVENT_SOURCE_IS_TIME(x->type));
293 assert(x->type == y->type);
295 /* Enabled ones first */
296 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
298 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
301 /* Move the pending ones to the end */
302 if (!x->pending && y->pending)
304 if (x->pending && !y->pending)
308 if (x->time.next < y->time.next)
310 if (x->time.next > y->time.next)
313 /* Stability for the rest */
322 static int latest_time_prioq_compare(const void *a, const void *b) {
323 const sd_event_source *x = a, *y = b;
325 assert(EVENT_SOURCE_IS_TIME(x->type));
326 assert(x->type == y->type);
328 /* Enabled ones first */
329 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
331 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
334 /* Move the pending ones to the end */
335 if (!x->pending && y->pending)
337 if (x->pending && !y->pending)
341 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
343 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
346 /* Stability for the rest */
355 static int exit_prioq_compare(const void *a, const void *b) {
356 const sd_event_source *x = a, *y = b;
358 assert(x->type == SOURCE_EXIT);
359 assert(y->type == SOURCE_EXIT);
361 /* Enabled ones first */
362 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
364 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
367 /* Lower priority values first */
368 if (x->priority < y->priority)
370 if (x->priority > y->priority)
373 /* Stability for the rest */
382 static void free_clock_data(struct clock_data *d) {
384 assert(d->wakeup == WAKEUP_CLOCK_DATA);
387 prioq_free(d->earliest);
388 prioq_free(d->latest);
391 static void event_free(sd_event *e) {
396 while ((s = e->sources)) {
398 source_disconnect(s);
399 sd_event_source_unref(s);
402 assert(e->n_sources == 0);
404 if (e->default_event_ptr)
405 *(e->default_event_ptr) = NULL;
407 safe_close(e->epoll_fd);
408 safe_close(e->watchdog_fd);
410 free_clock_data(&e->realtime);
411 free_clock_data(&e->boottime);
412 free_clock_data(&e->monotonic);
413 free_clock_data(&e->realtime_alarm);
414 free_clock_data(&e->boottime_alarm);
416 prioq_free(e->pending);
417 prioq_free(e->prepare);
420 free(e->signal_sources);
421 hashmap_free(e->signal_data);
423 hashmap_free(e->child_sources);
424 set_free(e->post_sources);
428 _public_ int sd_event_new(sd_event** ret) {
432 assert_return(ret, -EINVAL);
434 e = new0(sd_event, 1);
439 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
440 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
441 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
442 e->original_pid = getpid();
443 e->perturb = USEC_INFINITY;
445 e->pending = prioq_new(pending_prioq_compare);
451 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
452 if (e->epoll_fd < 0) {
465 _public_ sd_event* sd_event_ref(sd_event *e) {
466 assert_return(e, NULL);
468 assert(e->n_ref >= 1);
474 _public_ sd_event* sd_event_unref(sd_event *e) {
479 assert(e->n_ref >= 1);
488 static bool event_pid_changed(sd_event *e) {
491 /* We don't support people creating an event loop and keeping
492 * it around over a fork(). Let's complain. */
494 return e->original_pid != getpid();
497 static void source_io_unregister(sd_event_source *s) {
501 assert(s->type == SOURCE_IO);
503 if (event_pid_changed(s->event))
506 if (!s->io.registered)
509 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
511 log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
513 s->io.registered = false;
516 static int source_io_register(
521 struct epoll_event ev = {};
525 assert(s->type == SOURCE_IO);
526 assert(enabled != SD_EVENT_OFF);
531 if (enabled == SD_EVENT_ONESHOT)
532 ev.events |= EPOLLONESHOT;
534 if (s->io.registered)
535 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
537 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
541 s->io.registered = true;
546 /// UNNEEDED by elogind
548 static clockid_t event_source_type_to_clock(EventSourceType t) {
552 case SOURCE_TIME_REALTIME:
553 return CLOCK_REALTIME;
555 case SOURCE_TIME_BOOTTIME:
556 return CLOCK_BOOTTIME;
558 case SOURCE_TIME_MONOTONIC:
559 return CLOCK_MONOTONIC;
561 case SOURCE_TIME_REALTIME_ALARM:
562 return CLOCK_REALTIME_ALARM;
564 case SOURCE_TIME_BOOTTIME_ALARM:
565 return CLOCK_BOOTTIME_ALARM;
568 return (clockid_t) -1;
573 static EventSourceType clock_to_event_source_type(clockid_t clock) {
578 return SOURCE_TIME_REALTIME;
581 return SOURCE_TIME_BOOTTIME;
583 case CLOCK_MONOTONIC:
584 return SOURCE_TIME_MONOTONIC;
586 case CLOCK_REALTIME_ALARM:
587 return SOURCE_TIME_REALTIME_ALARM;
589 case CLOCK_BOOTTIME_ALARM:
590 return SOURCE_TIME_BOOTTIME_ALARM;
593 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
597 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
602 case SOURCE_TIME_REALTIME:
605 case SOURCE_TIME_BOOTTIME:
608 case SOURCE_TIME_MONOTONIC:
609 return &e->monotonic;
611 case SOURCE_TIME_REALTIME_ALARM:
612 return &e->realtime_alarm;
614 case SOURCE_TIME_BOOTTIME_ALARM:
615 return &e->boottime_alarm;
622 static int event_make_signal_data(
625 struct signal_data **ret) {
627 struct epoll_event ev = {};
628 struct signal_data *d;
636 if (event_pid_changed(e))
639 if (e->signal_sources && e->signal_sources[sig])
640 priority = e->signal_sources[sig]->priority;
644 d = hashmap_get(e->signal_data, &priority);
646 if (sigismember(&d->sigset, sig) > 0) {
652 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
656 d = new0(struct signal_data, 1);
660 d->wakeup = WAKEUP_SIGNAL_DATA;
662 d->priority = priority;
664 r = hashmap_put(e->signal_data, &d->priority, d);
672 assert_se(sigaddset(&ss_copy, sig) >= 0);
674 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
693 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
706 d->fd = safe_close(d->fd);
707 hashmap_remove(e->signal_data, &d->priority);
714 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
718 /* Turns off the specified signal in the signal data
719 * object. If the signal mask of the object becomes empty that
722 if (sigismember(&d->sigset, sig) == 0)
725 assert_se(sigdelset(&d->sigset, sig) >= 0);
727 if (sigisemptyset(&d->sigset)) {
729 /* If all the mask is all-zero we can get rid of the structure */
730 hashmap_remove(e->signal_data, &d->priority);
739 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
740 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
743 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
744 struct signal_data *d;
745 static const int64_t zero_priority = 0;
749 /* Rechecks if the specified signal is still something we are
750 * interested in. If not, we'll unmask it, and possibly drop
751 * the signalfd for it. */
753 if (sig == SIGCHLD &&
754 e->n_enabled_child_sources > 0)
757 if (e->signal_sources &&
758 e->signal_sources[sig] &&
759 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
763 * The specified signal might be enabled in three different queues:
765 * 1) the one that belongs to the priority passed (if it is non-NULL)
766 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
767 * 3) the 0 priority (to cover the SIGCHLD case)
769 * Hence, let's remove it from all three here.
773 d = hashmap_get(e->signal_data, priority);
775 event_unmask_signal_data(e, d, sig);
778 if (e->signal_sources && e->signal_sources[sig]) {
779 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
781 event_unmask_signal_data(e, d, sig);
784 d = hashmap_get(e->signal_data, &zero_priority);
786 event_unmask_signal_data(e, d, sig);
789 static void source_disconnect(sd_event_source *s) {
797 assert(s->event->n_sources > 0);
803 source_io_unregister(s);
807 case SOURCE_TIME_REALTIME:
808 case SOURCE_TIME_BOOTTIME:
809 case SOURCE_TIME_MONOTONIC:
810 case SOURCE_TIME_REALTIME_ALARM:
811 case SOURCE_TIME_BOOTTIME_ALARM: {
812 struct clock_data *d;
814 d = event_get_clock_data(s->event, s->type);
817 prioq_remove(d->earliest, s, &s->time.earliest_index);
818 prioq_remove(d->latest, s, &s->time.latest_index);
819 d->needs_rearm = true;
824 if (s->signal.sig > 0) {
826 if (s->event->signal_sources)
827 s->event->signal_sources[s->signal.sig] = NULL;
829 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
835 if (s->child.pid > 0) {
836 if (s->enabled != SD_EVENT_OFF) {
837 assert(s->event->n_enabled_child_sources > 0);
838 s->event->n_enabled_child_sources--;
841 (void) hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
842 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
852 set_remove(s->event->post_sources, s);
856 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
860 assert_not_reached("Wut? I shouldn't exist.");
864 prioq_remove(s->event->pending, s, &s->pending_index);
867 prioq_remove(s->event->prepare, s, &s->prepare_index);
871 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
873 LIST_REMOVE(sources, event->sources, s);
877 sd_event_unref(event);
880 static void source_free(sd_event_source *s) {
883 source_disconnect(s);
884 free(s->description);
888 static int source_set_pending(sd_event_source *s, bool b) {
892 assert(s->type != SOURCE_EXIT);
900 s->pending_iteration = s->event->iteration;
902 r = prioq_put(s->event->pending, s, &s->pending_index);
908 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
910 if (EVENT_SOURCE_IS_TIME(s->type)) {
911 struct clock_data *d;
913 d = event_get_clock_data(s->event, s->type);
916 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
917 prioq_reshuffle(d->latest, s, &s->time.latest_index);
918 d->needs_rearm = true;
921 if (s->type == SOURCE_SIGNAL && !b) {
922 struct signal_data *d;
924 d = hashmap_get(s->event->signal_data, &s->priority);
925 if (d && d->current == s)
932 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
937 s = new0(sd_event_source, 1);
943 s->floating = floating;
945 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
950 LIST_PREPEND(sources, e->sources, s);
956 _public_ int sd_event_add_io(
958 sd_event_source **ret,
961 sd_event_io_handler_t callback,
967 assert_return(e, -EINVAL);
968 assert_return(fd >= 0, -EBADF);
969 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
970 assert_return(callback, -EINVAL);
971 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
972 assert_return(!event_pid_changed(e), -ECHILD);
974 s = source_new(e, !ret, SOURCE_IO);
978 s->wakeup = WAKEUP_EVENT_SOURCE;
980 s->io.events = events;
981 s->io.callback = callback;
982 s->userdata = userdata;
983 s->enabled = SD_EVENT_ON;
985 r = source_io_register(s, s->enabled, events);
997 static void initialize_perturb(sd_event *e) {
998 sd_id128_t bootid = {};
1000 /* When we sleep for longer, we try to realign the wakeup to
1001 the same time wihtin each minute/second/250ms, so that
1002 events all across the system can be coalesced into a single
1003 CPU wakeup. However, let's take some system-specific
1004 randomness for this value, so that in a network of systems
1005 with synced clocks timer events are distributed a
1006 bit. Here, we calculate a perturbation usec offset from the
1009 if (_likely_(e->perturb != USEC_INFINITY))
1012 if (sd_id128_get_boot(&bootid) >= 0)
1013 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
1016 static int event_setup_timer_fd(
1018 struct clock_data *d,
1021 struct epoll_event ev = {};
1027 if (_likely_(d->fd >= 0))
1030 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1034 ev.events = EPOLLIN;
1037 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1047 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1050 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1053 _public_ int sd_event_add_time(
1055 sd_event_source **ret,
1059 sd_event_time_handler_t callback,
1062 EventSourceType type;
1064 struct clock_data *d;
1067 assert_return(e, -EINVAL);
1068 assert_return(usec != (uint64_t) -1, -EINVAL);
1069 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1070 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1071 assert_return(!event_pid_changed(e), -ECHILD);
1074 callback = time_exit_callback;
1076 type = clock_to_event_source_type(clock);
1077 assert_return(type >= 0, -EOPNOTSUPP);
1079 d = event_get_clock_data(e, type);
1083 d->earliest = prioq_new(earliest_time_prioq_compare);
1089 d->latest = prioq_new(latest_time_prioq_compare);
1095 r = event_setup_timer_fd(e, d, clock);
1100 s = source_new(e, !ret, type);
1104 s->time.next = usec;
1105 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1106 s->time.callback = callback;
1107 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1108 s->userdata = userdata;
1109 s->enabled = SD_EVENT_ONESHOT;
1111 d->needs_rearm = true;
1113 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1117 r = prioq_put(d->latest, s, &s->time.latest_index);
1131 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1134 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1137 _public_ int sd_event_add_signal(
1139 sd_event_source **ret,
1141 sd_event_signal_handler_t callback,
1145 struct signal_data *d;
1149 assert_return(e, -EINVAL);
1150 assert_return(sig > 0, -EINVAL);
1151 assert_return(sig < _NSIG, -EINVAL);
1152 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1153 assert_return(!event_pid_changed(e), -ECHILD);
1156 callback = signal_exit_callback;
1158 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1162 if (!sigismember(&ss, sig))
1165 if (!e->signal_sources) {
1166 e->signal_sources = new0(sd_event_source*, _NSIG);
1167 if (!e->signal_sources)
1169 } else if (e->signal_sources[sig])
1172 s = source_new(e, !ret, SOURCE_SIGNAL);
1176 s->signal.sig = sig;
1177 s->signal.callback = callback;
1178 s->userdata = userdata;
1179 s->enabled = SD_EVENT_ON;
1181 e->signal_sources[sig] = s;
1183 r = event_make_signal_data(e, sig, &d);
1189 /* Use the signal name as description for the event source by default */
1190 (void) sd_event_source_set_description(s, signal_to_string(sig));
1198 _public_ int sd_event_add_child(
1200 sd_event_source **ret,
1203 sd_event_child_handler_t callback,
1209 assert_return(e, -EINVAL);
1210 assert_return(pid > 1, -EINVAL);
1211 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1212 assert_return(options != 0, -EINVAL);
1213 assert_return(callback, -EINVAL);
1214 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1215 assert_return(!event_pid_changed(e), -ECHILD);
1217 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1221 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1224 s = source_new(e, !ret, SOURCE_CHILD);
1229 s->child.options = options;
1230 s->child.callback = callback;
1231 s->userdata = userdata;
1232 s->enabled = SD_EVENT_ONESHOT;
1234 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1240 e->n_enabled_child_sources ++;
1242 r = event_make_signal_data(e, SIGCHLD, NULL);
1244 e->n_enabled_child_sources--;
1249 e->need_process_child = true;
1257 _public_ int sd_event_add_defer(
1259 sd_event_source **ret,
1260 sd_event_handler_t callback,
1266 assert_return(e, -EINVAL);
1267 assert_return(callback, -EINVAL);
1268 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1269 assert_return(!event_pid_changed(e), -ECHILD);
1271 s = source_new(e, !ret, SOURCE_DEFER);
1275 s->defer.callback = callback;
1276 s->userdata = userdata;
1277 s->enabled = SD_EVENT_ONESHOT;
1279 r = source_set_pending(s, true);
1291 _public_ int sd_event_add_post(
1293 sd_event_source **ret,
1294 sd_event_handler_t callback,
1300 assert_return(e, -EINVAL);
1301 assert_return(callback, -EINVAL);
1302 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1303 assert_return(!event_pid_changed(e), -ECHILD);
1305 r = set_ensure_allocated(&e->post_sources, NULL);
1309 s = source_new(e, !ret, SOURCE_POST);
1313 s->post.callback = callback;
1314 s->userdata = userdata;
1315 s->enabled = SD_EVENT_ON;
1317 r = set_put(e->post_sources, s);
1329 _public_ int sd_event_add_exit(
1331 sd_event_source **ret,
1332 sd_event_handler_t callback,
1338 assert_return(e, -EINVAL);
1339 assert_return(callback, -EINVAL);
1340 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1341 assert_return(!event_pid_changed(e), -ECHILD);
1344 e->exit = prioq_new(exit_prioq_compare);
1349 s = source_new(e, !ret, SOURCE_EXIT);
1353 s->exit.callback = callback;
1354 s->userdata = userdata;
1355 s->exit.prioq_index = PRIOQ_IDX_NULL;
1356 s->enabled = SD_EVENT_ONESHOT;
1358 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1370 /// UNNEEDED by elogind
1372 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1373 assert_return(s, NULL);
1375 assert(s->n_ref >= 1);
1382 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1387 assert(s->n_ref >= 1);
1390 if (s->n_ref <= 0) {
1391 /* Here's a special hack: when we are called from a
1392 * dispatch handler we won't free the event source
1393 * immediately, but we will detach the fd from the
1394 * epoll. This way it is safe for the caller to unref
1395 * the event source and immediately close the fd, but
1396 * we still retain a valid event source object after
1399 if (s->dispatching) {
1400 if (s->type == SOURCE_IO)
1401 source_io_unregister(s);
1403 source_disconnect(s);
1411 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1412 assert_return(s, -EINVAL);
1413 assert_return(!event_pid_changed(s->event), -ECHILD);
1415 return free_and_strdup(&s->description, description);
1418 /// UNNEEDED by elogind
1420 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1421 assert_return(s, -EINVAL);
1422 assert_return(description, -EINVAL);
1423 assert_return(s->description, -ENXIO);
1424 assert_return(!event_pid_changed(s->event), -ECHILD);
1426 *description = s->description;
1431 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1432 assert_return(s, NULL);
1437 /// 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 /// UNNEEDED by elogind
1495 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1496 assert_return(s, -EINVAL);
1497 assert_return(events, -EINVAL);
1498 assert_return(s->type == SOURCE_IO, -EDOM);
1499 assert_return(!event_pid_changed(s->event), -ECHILD);
1501 *events = s->io.events;
1506 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1509 assert_return(s, -EINVAL);
1510 assert_return(s->type == SOURCE_IO, -EDOM);
1511 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1512 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1513 assert_return(!event_pid_changed(s->event), -ECHILD);
1515 /* edge-triggered updates are never skipped, so we can reset edges */
1516 if (s->io.events == events && !(events & EPOLLET))
1519 if (s->enabled != SD_EVENT_OFF) {
1520 r = source_io_register(s, s->enabled, events);
1525 s->io.events = events;
1526 source_set_pending(s, false);
1531 /// UNNEEDED by elogind
1533 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1534 assert_return(s, -EINVAL);
1535 assert_return(revents, -EINVAL);
1536 assert_return(s->type == SOURCE_IO, -EDOM);
1537 assert_return(s->pending, -ENODATA);
1538 assert_return(!event_pid_changed(s->event), -ECHILD);
1540 *revents = s->io.revents;
1544 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1545 assert_return(s, -EINVAL);
1546 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1547 assert_return(!event_pid_changed(s->event), -ECHILD);
1549 return s->signal.sig;
1552 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1553 assert_return(s, -EINVAL);
1554 assert_return(!event_pid_changed(s->event), -ECHILD);
1560 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1563 assert_return(s, -EINVAL);
1564 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1565 assert_return(!event_pid_changed(s->event), -ECHILD);
1567 if (s->priority == priority)
1570 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1571 struct signal_data *old, *d;
1573 /* Move us from the signalfd belonging to the old
1574 * priority to the signalfd of the new priority */
1576 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1578 s->priority = priority;
1580 r = event_make_signal_data(s->event, s->signal.sig, &d);
1582 s->priority = old->priority;
1586 event_unmask_signal_data(s->event, old, s->signal.sig);
1588 s->priority = priority;
1591 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1594 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1596 if (s->type == SOURCE_EXIT)
1597 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1602 /// UNNEEDED by elogind
1604 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1605 assert_return(s, -EINVAL);
1606 assert_return(m, -EINVAL);
1607 assert_return(!event_pid_changed(s->event), -ECHILD);
1614 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1617 assert_return(s, -EINVAL);
1618 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1619 assert_return(!event_pid_changed(s->event), -ECHILD);
1621 /* If we are dead anyway, we are fine with turning off
1622 * sources, but everything else needs to fail. */
1623 if (s->event->state == SD_EVENT_FINISHED)
1624 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1626 if (s->enabled == m)
1629 if (m == SD_EVENT_OFF) {
1634 source_io_unregister(s);
1638 case SOURCE_TIME_REALTIME:
1639 case SOURCE_TIME_BOOTTIME:
1640 case SOURCE_TIME_MONOTONIC:
1641 case SOURCE_TIME_REALTIME_ALARM:
1642 case SOURCE_TIME_BOOTTIME_ALARM: {
1643 struct clock_data *d;
1646 d = event_get_clock_data(s->event, s->type);
1649 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1650 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1651 d->needs_rearm = true;
1658 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1664 assert(s->event->n_enabled_child_sources > 0);
1665 s->event->n_enabled_child_sources--;
1667 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1672 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1681 assert_not_reached("Wut? I shouldn't exist.");
1688 r = source_io_register(s, m, s->io.events);
1695 case SOURCE_TIME_REALTIME:
1696 case SOURCE_TIME_BOOTTIME:
1697 case SOURCE_TIME_MONOTONIC:
1698 case SOURCE_TIME_REALTIME_ALARM:
1699 case SOURCE_TIME_BOOTTIME_ALARM: {
1700 struct clock_data *d;
1703 d = event_get_clock_data(s->event, s->type);
1706 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1707 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1708 d->needs_rearm = true;
1716 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1718 s->enabled = SD_EVENT_OFF;
1719 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1727 if (s->enabled == SD_EVENT_OFF)
1728 s->event->n_enabled_child_sources++;
1732 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1734 s->enabled = SD_EVENT_OFF;
1735 s->event->n_enabled_child_sources--;
1736 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1744 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1753 assert_not_reached("Wut? I shouldn't exist.");
1758 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1761 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1766 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1767 assert_return(s, -EINVAL);
1768 assert_return(usec, -EINVAL);
1769 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1770 assert_return(!event_pid_changed(s->event), -ECHILD);
1772 *usec = s->time.next;
1776 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1777 struct clock_data *d;
1779 assert_return(s, -EINVAL);
1780 assert_return(usec != (uint64_t) -1, -EINVAL);
1781 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1782 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1783 assert_return(!event_pid_changed(s->event), -ECHILD);
1785 s->time.next = usec;
1787 source_set_pending(s, false);
1789 d = event_get_clock_data(s->event, s->type);
1792 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1793 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1794 d->needs_rearm = true;
1799 /// UNNEEDED by elogind
1801 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1802 assert_return(s, -EINVAL);
1803 assert_return(usec, -EINVAL);
1804 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1805 assert_return(!event_pid_changed(s->event), -ECHILD);
1807 *usec = s->time.accuracy;
1811 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1812 struct clock_data *d;
1814 assert_return(s, -EINVAL);
1815 assert_return(usec != (uint64_t) -1, -EINVAL);
1816 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1817 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1818 assert_return(!event_pid_changed(s->event), -ECHILD);
1821 usec = DEFAULT_ACCURACY_USEC;
1823 s->time.accuracy = usec;
1825 source_set_pending(s, false);
1827 d = event_get_clock_data(s->event, s->type);
1830 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1831 d->needs_rearm = true;
1836 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1837 assert_return(s, -EINVAL);
1838 assert_return(clock, -EINVAL);
1839 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1840 assert_return(!event_pid_changed(s->event), -ECHILD);
1842 *clock = event_source_type_to_clock(s->type);
1846 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1847 assert_return(s, -EINVAL);
1848 assert_return(pid, -EINVAL);
1849 assert_return(s->type == SOURCE_CHILD, -EDOM);
1850 assert_return(!event_pid_changed(s->event), -ECHILD);
1852 *pid = s->child.pid;
1857 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1860 assert_return(s, -EINVAL);
1861 assert_return(s->type != SOURCE_EXIT, -EDOM);
1862 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1863 assert_return(!event_pid_changed(s->event), -ECHILD);
1865 if (s->prepare == callback)
1868 if (callback && s->prepare) {
1869 s->prepare = callback;
1873 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1877 s->prepare = callback;
1880 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1884 prioq_remove(s->event->prepare, s, &s->prepare_index);
1889 /// UNNEEDED by elogind
1891 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1892 assert_return(s, NULL);
1897 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1900 assert_return(s, NULL);
1903 s->userdata = userdata;
1909 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1920 initialize_perturb(e);
1923 Find a good time to wake up again between times a and b. We
1924 have two goals here:
1926 a) We want to wake up as seldom as possible, hence prefer
1927 later times over earlier times.
1929 b) But if we have to wake up, then let's make sure to
1930 dispatch as much as possible on the entire system.
1932 We implement this by waking up everywhere at the same time
1933 within any given minute if we can, synchronised via the
1934 perturbation value determined from the boot ID. If we can't,
1935 then we try to find the same spot in every 10s, then 1s and
1936 then 250ms step. Otherwise, we pick the last possible time
1940 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1942 if (_unlikely_(c < USEC_PER_MINUTE))
1945 c -= USEC_PER_MINUTE;
1951 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1953 if (_unlikely_(c < USEC_PER_SEC*10))
1956 c -= USEC_PER_SEC*10;
1962 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1964 if (_unlikely_(c < USEC_PER_SEC))
1973 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1975 if (_unlikely_(c < USEC_PER_MSEC*250))
1978 c -= USEC_PER_MSEC*250;
1987 static int event_arm_timer(
1989 struct clock_data *d) {
1991 struct itimerspec its = {};
1992 sd_event_source *a, *b;
1999 if (!d->needs_rearm)
2002 d->needs_rearm = false;
2004 a = prioq_peek(d->earliest);
2005 if (!a || a->enabled == SD_EVENT_OFF) {
2010 if (d->next == USEC_INFINITY)
2014 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2018 d->next = USEC_INFINITY;
2022 b = prioq_peek(d->latest);
2023 assert_se(b && b->enabled != SD_EVENT_OFF);
2025 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
2029 assert_se(d->fd >= 0);
2032 /* We don' want to disarm here, just mean some time looooong ago. */
2033 its.it_value.tv_sec = 0;
2034 its.it_value.tv_nsec = 1;
2036 timespec_store(&its.it_value, t);
2038 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2046 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2049 assert(s->type == SOURCE_IO);
2051 /* If the event source was already pending, we just OR in the
2052 * new revents, otherwise we reset the value. The ORing is
2053 * necessary to handle EPOLLONESHOT events properly where
2054 * readability might happen independently of writability, and
2055 * we need to keep track of both */
2058 s->io.revents |= revents;
2060 s->io.revents = revents;
2062 return source_set_pending(s, true);
2065 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2072 assert_return(events == EPOLLIN, -EIO);
2074 ss = read(fd, &x, sizeof(x));
2076 if (errno == EAGAIN || errno == EINTR)
2082 if (_unlikely_(ss != sizeof(x)))
2086 *next = USEC_INFINITY;
2091 static int process_timer(
2094 struct clock_data *d) {
2103 s = prioq_peek(d->earliest);
2106 s->enabled == SD_EVENT_OFF ||
2110 r = source_set_pending(s, true);
2114 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2115 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2116 d->needs_rearm = true;
2122 static int process_child(sd_event *e) {
2129 e->need_process_child = false;
2132 So, this is ugly. We iteratively invoke waitid() with P_PID
2133 + WNOHANG for each PID we wait for, instead of using
2134 P_ALL. This is because we only want to get child
2135 information of very specific child processes, and not all
2136 of them. We might not have processed the SIGCHLD even of a
2137 previous invocation and we don't want to maintain a
2138 unbounded *per-child* event queue, hence we really don't
2139 want anything flushed out of the kernel's queue that we
2140 don't care about. Since this is O(n) this means that if you
2141 have a lot of processes you probably want to handle SIGCHLD
2144 We do not reap the children here (by using WNOWAIT), this
2145 is only done after the event source is dispatched so that
2146 the callback still sees the process as a zombie.
2149 HASHMAP_FOREACH(s, e->child_sources, i) {
2150 assert(s->type == SOURCE_CHILD);
2155 if (s->enabled == SD_EVENT_OFF)
2158 zero(s->child.siginfo);
2159 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2160 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2164 if (s->child.siginfo.si_pid != 0) {
2166 s->child.siginfo.si_code == CLD_EXITED ||
2167 s->child.siginfo.si_code == CLD_KILLED ||
2168 s->child.siginfo.si_code == CLD_DUMPED;
2170 if (!zombie && (s->child.options & WEXITED)) {
2171 /* If the child isn't dead then let's
2172 * immediately remove the state change
2173 * from the queue, since there's no
2174 * benefit in leaving it queued */
2176 assert(s->child.options & (WSTOPPED|WCONTINUED));
2177 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2180 r = source_set_pending(s, true);
2189 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2190 bool read_one = false;
2194 assert_return(events == EPOLLIN, -EIO);
2196 /* If there's a signal queued on this priority and SIGCHLD is
2197 on this priority too, then make sure to recheck the
2198 children we watch. This is because we only ever dequeue
2199 the first signal per priority, and if we dequeue one, and
2200 SIGCHLD might be enqueued later we wouldn't know, but we
2201 might have higher priority children we care about hence we
2202 need to check that explicitly. */
2204 if (sigismember(&d->sigset, SIGCHLD))
2205 e->need_process_child = true;
2207 /* If there's already an event source pending for this
2208 * priority we don't read another */
2213 struct signalfd_siginfo si;
2215 sd_event_source *s = NULL;
2217 n = read(d->fd, &si, sizeof(si));
2219 if (errno == EAGAIN || errno == EINTR)
2225 if (_unlikely_(n != sizeof(si)))
2228 assert(si.ssi_signo < _NSIG);
2232 if (e->signal_sources)
2233 s = e->signal_sources[si.ssi_signo];
2239 s->signal.siginfo = si;
2242 r = source_set_pending(s, true);
2250 static int source_dispatch(sd_event_source *s) {
2254 assert(s->pending || s->type == SOURCE_EXIT);
2256 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2257 r = source_set_pending(s, false);
2262 if (s->type != SOURCE_POST) {
2266 /* If we execute a non-post source, let's mark all
2267 * post sources as pending */
2269 SET_FOREACH(z, s->event->post_sources, i) {
2270 if (z->enabled == SD_EVENT_OFF)
2273 r = source_set_pending(z, true);
2279 if (s->enabled == SD_EVENT_ONESHOT) {
2280 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2285 s->dispatching = true;
2290 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2293 case SOURCE_TIME_REALTIME:
2294 case SOURCE_TIME_BOOTTIME:
2295 case SOURCE_TIME_MONOTONIC:
2296 case SOURCE_TIME_REALTIME_ALARM:
2297 case SOURCE_TIME_BOOTTIME_ALARM:
2298 r = s->time.callback(s, s->time.next, s->userdata);
2302 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2305 case SOURCE_CHILD: {
2308 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2309 s->child.siginfo.si_code == CLD_KILLED ||
2310 s->child.siginfo.si_code == CLD_DUMPED;
2312 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2314 /* Now, reap the PID for good. */
2316 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2322 r = s->defer.callback(s, s->userdata);
2326 r = s->post.callback(s, s->userdata);
2330 r = s->exit.callback(s, s->userdata);
2333 case SOURCE_WATCHDOG:
2334 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2335 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2336 assert_not_reached("Wut? I shouldn't exist.");
2339 s->dispatching = false;
2343 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2345 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2351 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2356 static int event_prepare(sd_event *e) {
2364 s = prioq_peek(e->prepare);
2365 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2368 s->prepare_iteration = e->iteration;
2369 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2375 s->dispatching = true;
2376 r = s->prepare(s, s->userdata);
2377 s->dispatching = false;
2381 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2383 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2389 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2395 static int dispatch_exit(sd_event *e) {
2401 p = prioq_peek(e->exit);
2402 if (!p || p->enabled == SD_EVENT_OFF) {
2403 e->state = SD_EVENT_FINISHED;
2409 e->state = SD_EVENT_EXITING;
2411 r = source_dispatch(p);
2413 e->state = SD_EVENT_INITIAL;
2419 static sd_event_source* event_next_pending(sd_event *e) {
2424 p = prioq_peek(e->pending);
2428 if (p->enabled == SD_EVENT_OFF)
2434 static int arm_watchdog(sd_event *e) {
2435 struct itimerspec its = {};
2440 assert(e->watchdog_fd >= 0);
2442 t = sleep_between(e,
2443 e->watchdog_last + (e->watchdog_period / 2),
2444 e->watchdog_last + (e->watchdog_period * 3 / 4));
2446 timespec_store(&its.it_value, t);
2448 /* Make sure we never set the watchdog to 0, which tells the
2449 * kernel to disable it. */
2450 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2451 its.it_value.tv_nsec = 1;
2453 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2460 static int process_watchdog(sd_event *e) {
2466 /* Don't notify watchdog too often */
2467 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2470 sd_notify(false, "WATCHDOG=1");
2471 e->watchdog_last = e->timestamp.monotonic;
2473 return arm_watchdog(e);
2476 _public_ int sd_event_prepare(sd_event *e) {
2479 assert_return(e, -EINVAL);
2480 assert_return(!event_pid_changed(e), -ECHILD);
2481 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2482 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2484 if (e->exit_requested)
2489 r = event_prepare(e);
2493 r = event_arm_timer(e, &e->realtime);
2497 r = event_arm_timer(e, &e->boottime);
2501 r = event_arm_timer(e, &e->monotonic);
2505 r = event_arm_timer(e, &e->realtime_alarm);
2509 r = event_arm_timer(e, &e->boottime_alarm);
2513 if (event_next_pending(e) || e->need_process_child)
2516 e->state = SD_EVENT_ARMED;
2521 e->state = SD_EVENT_ARMED;
2522 r = sd_event_wait(e, 0);
2524 e->state = SD_EVENT_ARMED;
2529 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2530 struct epoll_event *ev_queue;
2531 unsigned ev_queue_max;
2534 assert_return(e, -EINVAL);
2535 assert_return(!event_pid_changed(e), -ECHILD);
2536 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2537 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2539 if (e->exit_requested) {
2540 e->state = SD_EVENT_PENDING;
2544 ev_queue_max = MAX(e->n_sources, 1u);
2545 ev_queue = newa(struct epoll_event, ev_queue_max);
2547 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2548 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2550 if (errno == EINTR) {
2551 e->state = SD_EVENT_PENDING;
2559 dual_timestamp_get(&e->timestamp);
2560 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2562 for (i = 0; i < m; i++) {
2564 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2565 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2567 WakeupType *t = ev_queue[i].data.ptr;
2571 case WAKEUP_EVENT_SOURCE:
2572 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2575 case WAKEUP_CLOCK_DATA: {
2576 struct clock_data *d = ev_queue[i].data.ptr;
2577 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2581 case WAKEUP_SIGNAL_DATA:
2582 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2586 assert_not_reached("Invalid wake-up pointer");
2593 r = process_watchdog(e);
2597 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2601 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2605 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2609 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2613 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2617 if (e->need_process_child) {
2618 r = process_child(e);
2623 if (event_next_pending(e)) {
2624 e->state = SD_EVENT_PENDING;
2632 e->state = SD_EVENT_INITIAL;
2637 _public_ int sd_event_dispatch(sd_event *e) {
2641 assert_return(e, -EINVAL);
2642 assert_return(!event_pid_changed(e), -ECHILD);
2643 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2644 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2646 if (e->exit_requested)
2647 return dispatch_exit(e);
2649 p = event_next_pending(e);
2653 e->state = SD_EVENT_RUNNING;
2654 r = source_dispatch(p);
2655 e->state = SD_EVENT_INITIAL;
2662 e->state = SD_EVENT_INITIAL;
2667 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2670 assert_return(e, -EINVAL);
2671 assert_return(!event_pid_changed(e), -ECHILD);
2672 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2673 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2675 r = sd_event_prepare(e);
2677 /* There was nothing? Then wait... */
2678 r = sd_event_wait(e, timeout);
2681 /* There's something now, then let's dispatch it */
2682 r = sd_event_dispatch(e);
2692 /// UNNEEDED by elogind
2694 _public_ int sd_event_loop(sd_event *e) {
2697 assert_return(e, -EINVAL);
2698 assert_return(!event_pid_changed(e), -ECHILD);
2699 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2703 while (e->state != SD_EVENT_FINISHED) {
2704 r = sd_event_run(e, (uint64_t) -1);
2716 _public_ int sd_event_get_fd(sd_event *e) {
2718 assert_return(e, -EINVAL);
2719 assert_return(!event_pid_changed(e), -ECHILD);
2725 _public_ int sd_event_get_state(sd_event *e) {
2726 assert_return(e, -EINVAL);
2727 assert_return(!event_pid_changed(e), -ECHILD);
2732 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2733 assert_return(e, -EINVAL);
2734 assert_return(code, -EINVAL);
2735 assert_return(!event_pid_changed(e), -ECHILD);
2737 if (!e->exit_requested)
2740 *code = e->exit_code;
2744 _public_ int sd_event_exit(sd_event *e, int code) {
2745 assert_return(e, -EINVAL);
2746 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2747 assert_return(!event_pid_changed(e), -ECHILD);
2749 e->exit_requested = true;
2750 e->exit_code = code;
2755 /// UNNEEDED by elogind
2757 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2758 assert_return(e, -EINVAL);
2759 assert_return(usec, -EINVAL);
2760 assert_return(!event_pid_changed(e), -ECHILD);
2762 if (!dual_timestamp_is_set(&e->timestamp)) {
2763 /* Implicitly fall back to now() if we never ran
2764 * before and thus have no cached time. */
2771 case CLOCK_REALTIME:
2772 case CLOCK_REALTIME_ALARM:
2773 *usec = e->timestamp.realtime;
2776 case CLOCK_MONOTONIC:
2777 *usec = e->timestamp.monotonic;
2780 case CLOCK_BOOTTIME:
2781 case CLOCK_BOOTTIME_ALARM:
2782 *usec = e->timestamp_boottime;
2790 _public_ int sd_event_default(sd_event **ret) {
2792 static thread_local sd_event *default_event = NULL;
2797 return !!default_event;
2799 if (default_event) {
2800 *ret = sd_event_ref(default_event);
2804 r = sd_event_new(&e);
2808 e->default_event_ptr = &default_event;
2816 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2817 assert_return(e, -EINVAL);
2818 assert_return(tid, -EINVAL);
2819 assert_return(!event_pid_changed(e), -ECHILD);
2829 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2832 assert_return(e, -EINVAL);
2833 assert_return(!event_pid_changed(e), -ECHILD);
2835 if (e->watchdog == !!b)
2839 struct epoll_event ev = {};
2841 r = sd_watchdog_enabled(false, &e->watchdog_period);
2845 /* Issue first ping immediately */
2846 sd_notify(false, "WATCHDOG=1");
2847 e->watchdog_last = now(CLOCK_MONOTONIC);
2849 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2850 if (e->watchdog_fd < 0)
2853 r = arm_watchdog(e);
2857 ev.events = EPOLLIN;
2858 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2860 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2867 if (e->watchdog_fd >= 0) {
2868 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2869 e->watchdog_fd = safe_close(e->watchdog_fd);
2877 e->watchdog_fd = safe_close(e->watchdog_fd);
2881 /// UNNEEDED by elogind
2883 _public_ int sd_event_get_watchdog(sd_event *e) {
2884 assert_return(e, -EINVAL);
2885 assert_return(!event_pid_changed(e), -ECHILD);