1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
26 #include "sd-daemon.h"
30 #include "alloc-util.h"
37 #include "process-util.h"
39 #include "signal-util.h"
40 #include "string-util.h"
41 #include "time-util.h"
44 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
46 typedef enum EventSourceType {
50 SOURCE_TIME_MONOTONIC,
51 SOURCE_TIME_REALTIME_ALARM,
52 SOURCE_TIME_BOOTTIME_ALARM,
59 _SOURCE_EVENT_SOURCE_TYPE_MAX,
60 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
63 /* All objects we use in epoll events start with this value, so that
64 * we know how to dispatch it */
65 typedef enum WakeupType {
71 _WAKEUP_TYPE_INVALID = -1,
74 #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)
76 struct sd_event_source {
83 sd_event_handler_t prepare;
87 EventSourceType type:5;
94 unsigned pending_index;
95 unsigned prepare_index;
96 unsigned pending_iteration;
97 unsigned prepare_iteration;
99 LIST_FIELDS(sd_event_source, sources);
103 sd_event_io_handler_t callback;
110 sd_event_time_handler_t callback;
111 usec_t next, accuracy;
112 unsigned earliest_index;
113 unsigned latest_index;
116 sd_event_signal_handler_t callback;
117 struct signalfd_siginfo siginfo;
121 sd_event_child_handler_t callback;
127 sd_event_handler_t callback;
130 sd_event_handler_t callback;
133 sd_event_handler_t callback;
134 unsigned prioq_index;
143 /* For all clocks we maintain two priority queues each, one
144 * ordered for the earliest times the events may be
145 * dispatched, and one ordered by the latest times they must
146 * have been dispatched. The range between the top entries in
147 * the two prioqs is the time window we can freely schedule
160 /* For each priority we maintain one signal fd, so that we
161 * only have to dequeue a single event per priority at a
167 sd_event_source *current;
179 /* timerfd_create() only supports these five clocks so far. We
180 * can add support for more clocks when the kernel learns to
181 * deal with them, too. */
182 struct clock_data realtime;
183 struct clock_data boottime;
184 struct clock_data monotonic;
185 struct clock_data realtime_alarm;
186 struct clock_data boottime_alarm;
190 sd_event_source **signal_sources; /* indexed by signal number */
191 Hashmap *signal_data; /* indexed by priority */
193 Hashmap *child_sources;
194 unsigned n_enabled_child_sources;
203 dual_timestamp timestamp;
204 usec_t timestamp_boottime;
207 bool exit_requested:1;
208 bool need_process_child:1;
214 sd_event **default_event_ptr;
216 usec_t watchdog_last, watchdog_period;
220 LIST_HEAD(sd_event_source, sources);
223 static void source_disconnect(sd_event_source *s);
225 static int pending_prioq_compare(const void *a, const void *b) {
226 const sd_event_source *x = a, *y = b;
231 /* Enabled ones first */
232 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
234 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
237 /* Lower priority values first */
238 if (x->priority < y->priority)
240 if (x->priority > y->priority)
243 /* Older entries first */
244 if (x->pending_iteration < y->pending_iteration)
246 if (x->pending_iteration > y->pending_iteration)
252 static int prepare_prioq_compare(const void *a, const void *b) {
253 const sd_event_source *x = a, *y = b;
258 /* Enabled ones first */
259 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
261 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
264 /* Move most recently prepared ones last, so that we can stop
265 * preparing as soon as we hit one that has already been
266 * prepared in the current iteration */
267 if (x->prepare_iteration < y->prepare_iteration)
269 if (x->prepare_iteration > y->prepare_iteration)
272 /* Lower priority values first */
273 if (x->priority < y->priority)
275 if (x->priority > y->priority)
281 static int earliest_time_prioq_compare(const void *a, const void *b) {
282 const sd_event_source *x = a, *y = b;
284 assert(EVENT_SOURCE_IS_TIME(x->type));
285 assert(x->type == y->type);
287 /* Enabled ones first */
288 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
290 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
293 /* Move the pending ones to the end */
294 if (!x->pending && y->pending)
296 if (x->pending && !y->pending)
300 if (x->time.next < y->time.next)
302 if (x->time.next > y->time.next)
308 static int latest_time_prioq_compare(const void *a, const void *b) {
309 const sd_event_source *x = a, *y = b;
311 assert(EVENT_SOURCE_IS_TIME(x->type));
312 assert(x->type == y->type);
314 /* Enabled ones first */
315 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
317 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
320 /* Move the pending ones to the end */
321 if (!x->pending && y->pending)
323 if (x->pending && !y->pending)
327 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
329 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
335 static int exit_prioq_compare(const void *a, const void *b) {
336 const sd_event_source *x = a, *y = b;
338 assert(x->type == SOURCE_EXIT);
339 assert(y->type == SOURCE_EXIT);
341 /* Enabled ones first */
342 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
344 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
347 /* Lower priority values first */
348 if (x->priority < y->priority)
350 if (x->priority > y->priority)
356 static void free_clock_data(struct clock_data *d) {
358 assert(d->wakeup == WAKEUP_CLOCK_DATA);
361 prioq_free(d->earliest);
362 prioq_free(d->latest);
365 static void event_free(sd_event *e) {
370 while ((s = e->sources)) {
372 source_disconnect(s);
373 sd_event_source_unref(s);
376 assert(e->n_sources == 0);
378 if (e->default_event_ptr)
379 *(e->default_event_ptr) = NULL;
381 safe_close(e->epoll_fd);
382 safe_close(e->watchdog_fd);
384 free_clock_data(&e->realtime);
385 free_clock_data(&e->boottime);
386 free_clock_data(&e->monotonic);
387 free_clock_data(&e->realtime_alarm);
388 free_clock_data(&e->boottime_alarm);
390 prioq_free(e->pending);
391 prioq_free(e->prepare);
394 free(e->signal_sources);
395 hashmap_free(e->signal_data);
397 hashmap_free(e->child_sources);
398 set_free(e->post_sources);
402 _public_ int sd_event_new(sd_event** ret) {
406 assert_return(ret, -EINVAL);
408 e = new0(sd_event, 1);
413 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
414 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
415 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
416 e->original_pid = getpid();
417 e->perturb = USEC_INFINITY;
419 r = prioq_ensure_allocated(&e->pending, pending_prioq_compare);
423 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
424 if (e->epoll_fd < 0) {
437 _public_ sd_event* sd_event_ref(sd_event *e) {
438 assert_return(e, NULL);
440 assert(e->n_ref >= 1);
446 _public_ sd_event* sd_event_unref(sd_event *e) {
451 assert(e->n_ref >= 1);
460 static bool event_pid_changed(sd_event *e) {
463 /* We don't support people creating an event loop and keeping
464 * it around over a fork(). Let's complain. */
466 return e->original_pid != getpid();
469 static void source_io_unregister(sd_event_source *s) {
473 assert(s->type == SOURCE_IO);
475 if (event_pid_changed(s->event))
478 if (!s->io.registered)
481 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
483 log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
485 s->io.registered = false;
488 static int source_io_register(
493 struct epoll_event ev = {};
497 assert(s->type == SOURCE_IO);
498 assert(enabled != SD_EVENT_OFF);
503 if (enabled == SD_EVENT_ONESHOT)
504 ev.events |= EPOLLONESHOT;
506 if (s->io.registered)
507 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
509 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
513 s->io.registered = true;
518 #if 0 /// UNNEEDED by elogind
519 static clockid_t event_source_type_to_clock(EventSourceType t) {
523 case SOURCE_TIME_REALTIME:
524 return CLOCK_REALTIME;
526 case SOURCE_TIME_BOOTTIME:
527 return CLOCK_BOOTTIME;
529 case SOURCE_TIME_MONOTONIC:
530 return CLOCK_MONOTONIC;
532 case SOURCE_TIME_REALTIME_ALARM:
533 return CLOCK_REALTIME_ALARM;
535 case SOURCE_TIME_BOOTTIME_ALARM:
536 return CLOCK_BOOTTIME_ALARM;
539 return (clockid_t) -1;
544 static EventSourceType clock_to_event_source_type(clockid_t clock) {
549 return SOURCE_TIME_REALTIME;
552 return SOURCE_TIME_BOOTTIME;
554 case CLOCK_MONOTONIC:
555 return SOURCE_TIME_MONOTONIC;
557 case CLOCK_REALTIME_ALARM:
558 return SOURCE_TIME_REALTIME_ALARM;
560 case CLOCK_BOOTTIME_ALARM:
561 return SOURCE_TIME_BOOTTIME_ALARM;
564 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
568 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
573 case SOURCE_TIME_REALTIME:
576 case SOURCE_TIME_BOOTTIME:
579 case SOURCE_TIME_MONOTONIC:
580 return &e->monotonic;
582 case SOURCE_TIME_REALTIME_ALARM:
583 return &e->realtime_alarm;
585 case SOURCE_TIME_BOOTTIME_ALARM:
586 return &e->boottime_alarm;
593 static int event_make_signal_data(
596 struct signal_data **ret) {
598 struct epoll_event ev = {};
599 struct signal_data *d;
607 if (event_pid_changed(e))
610 if (e->signal_sources && e->signal_sources[sig])
611 priority = e->signal_sources[sig]->priority;
615 d = hashmap_get(e->signal_data, &priority);
617 if (sigismember(&d->sigset, sig) > 0) {
623 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
627 d = new0(struct signal_data, 1);
631 d->wakeup = WAKEUP_SIGNAL_DATA;
633 d->priority = priority;
635 r = hashmap_put(e->signal_data, &d->priority, d);
643 assert_se(sigaddset(&ss_copy, sig) >= 0);
645 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
664 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
677 d->fd = safe_close(d->fd);
678 hashmap_remove(e->signal_data, &d->priority);
685 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
689 /* Turns off the specified signal in the signal data
690 * object. If the signal mask of the object becomes empty that
693 if (sigismember(&d->sigset, sig) == 0)
696 assert_se(sigdelset(&d->sigset, sig) >= 0);
698 if (sigisemptyset(&d->sigset)) {
700 /* If all the mask is all-zero we can get rid of the structure */
701 hashmap_remove(e->signal_data, &d->priority);
710 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
711 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
714 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
715 struct signal_data *d;
716 static const int64_t zero_priority = 0;
720 /* Rechecks if the specified signal is still something we are
721 * interested in. If not, we'll unmask it, and possibly drop
722 * the signalfd for it. */
724 if (sig == SIGCHLD &&
725 e->n_enabled_child_sources > 0)
728 if (e->signal_sources &&
729 e->signal_sources[sig] &&
730 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
734 * The specified signal might be enabled in three different queues:
736 * 1) the one that belongs to the priority passed (if it is non-NULL)
737 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
738 * 3) the 0 priority (to cover the SIGCHLD case)
740 * Hence, let's remove it from all three here.
744 d = hashmap_get(e->signal_data, priority);
746 event_unmask_signal_data(e, d, sig);
749 if (e->signal_sources && e->signal_sources[sig]) {
750 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
752 event_unmask_signal_data(e, d, sig);
755 d = hashmap_get(e->signal_data, &zero_priority);
757 event_unmask_signal_data(e, d, sig);
760 static void source_disconnect(sd_event_source *s) {
768 assert(s->event->n_sources > 0);
774 source_io_unregister(s);
778 case SOURCE_TIME_REALTIME:
779 case SOURCE_TIME_BOOTTIME:
780 case SOURCE_TIME_MONOTONIC:
781 case SOURCE_TIME_REALTIME_ALARM:
782 case SOURCE_TIME_BOOTTIME_ALARM: {
783 struct clock_data *d;
785 d = event_get_clock_data(s->event, s->type);
788 prioq_remove(d->earliest, s, &s->time.earliest_index);
789 prioq_remove(d->latest, s, &s->time.latest_index);
790 d->needs_rearm = true;
795 if (s->signal.sig > 0) {
797 if (s->event->signal_sources)
798 s->event->signal_sources[s->signal.sig] = NULL;
800 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
806 if (s->child.pid > 0) {
807 if (s->enabled != SD_EVENT_OFF) {
808 assert(s->event->n_enabled_child_sources > 0);
809 s->event->n_enabled_child_sources--;
812 (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
813 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
823 set_remove(s->event->post_sources, s);
827 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
831 assert_not_reached("Wut? I shouldn't exist.");
835 prioq_remove(s->event->pending, s, &s->pending_index);
838 prioq_remove(s->event->prepare, s, &s->prepare_index);
842 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
844 LIST_REMOVE(sources, event->sources, s);
848 sd_event_unref(event);
851 static void source_free(sd_event_source *s) {
854 source_disconnect(s);
855 free(s->description);
859 static int source_set_pending(sd_event_source *s, bool b) {
863 assert(s->type != SOURCE_EXIT);
871 s->pending_iteration = s->event->iteration;
873 r = prioq_put(s->event->pending, s, &s->pending_index);
879 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
881 if (EVENT_SOURCE_IS_TIME(s->type)) {
882 struct clock_data *d;
884 d = event_get_clock_data(s->event, s->type);
887 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
888 prioq_reshuffle(d->latest, s, &s->time.latest_index);
889 d->needs_rearm = true;
892 if (s->type == SOURCE_SIGNAL && !b) {
893 struct signal_data *d;
895 d = hashmap_get(s->event->signal_data, &s->priority);
896 if (d && d->current == s)
903 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
908 s = new0(sd_event_source, 1);
914 s->floating = floating;
916 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
921 LIST_PREPEND(sources, e->sources, s);
927 _public_ int sd_event_add_io(
929 sd_event_source **ret,
932 sd_event_io_handler_t callback,
938 assert_return(e, -EINVAL);
939 assert_return(fd >= 0, -EBADF);
940 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
941 assert_return(callback, -EINVAL);
942 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
943 assert_return(!event_pid_changed(e), -ECHILD);
945 s = source_new(e, !ret, SOURCE_IO);
949 s->wakeup = WAKEUP_EVENT_SOURCE;
951 s->io.events = events;
952 s->io.callback = callback;
953 s->userdata = userdata;
954 s->enabled = SD_EVENT_ON;
956 r = source_io_register(s, s->enabled, events);
968 static void initialize_perturb(sd_event *e) {
969 sd_id128_t bootid = {};
971 /* When we sleep for longer, we try to realign the wakeup to
972 the same time wihtin each minute/second/250ms, so that
973 events all across the system can be coalesced into a single
974 CPU wakeup. However, let's take some system-specific
975 randomness for this value, so that in a network of systems
976 with synced clocks timer events are distributed a
977 bit. Here, we calculate a perturbation usec offset from the
980 if (_likely_(e->perturb != USEC_INFINITY))
983 if (sd_id128_get_boot(&bootid) >= 0)
984 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
987 static int event_setup_timer_fd(
989 struct clock_data *d,
992 struct epoll_event ev = {};
998 if (_likely_(d->fd >= 0))
1001 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1005 ev.events = EPOLLIN;
1008 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1018 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1021 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1024 _public_ int sd_event_add_time(
1026 sd_event_source **ret,
1030 sd_event_time_handler_t callback,
1033 EventSourceType type;
1035 struct clock_data *d;
1038 assert_return(e, -EINVAL);
1039 assert_return(usec != (uint64_t) -1, -EINVAL);
1040 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1041 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1042 assert_return(!event_pid_changed(e), -ECHILD);
1045 callback = time_exit_callback;
1047 type = clock_to_event_source_type(clock);
1048 assert_return(type >= 0, -EOPNOTSUPP);
1050 d = event_get_clock_data(e, type);
1053 r = prioq_ensure_allocated(&d->earliest, earliest_time_prioq_compare);
1057 r = prioq_ensure_allocated(&d->latest, latest_time_prioq_compare);
1062 r = event_setup_timer_fd(e, d, clock);
1067 s = source_new(e, !ret, type);
1071 s->time.next = usec;
1072 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1073 s->time.callback = callback;
1074 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1075 s->userdata = userdata;
1076 s->enabled = SD_EVENT_ONESHOT;
1078 d->needs_rearm = true;
1080 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1084 r = prioq_put(d->latest, s, &s->time.latest_index);
1098 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1101 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1104 _public_ int sd_event_add_signal(
1106 sd_event_source **ret,
1108 sd_event_signal_handler_t callback,
1112 struct signal_data *d;
1116 assert_return(e, -EINVAL);
1117 assert_return(sig > 0, -EINVAL);
1118 assert_return(sig < _NSIG, -EINVAL);
1119 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1120 assert_return(!event_pid_changed(e), -ECHILD);
1123 callback = signal_exit_callback;
1125 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1129 if (!sigismember(&ss, sig))
1132 if (!e->signal_sources) {
1133 e->signal_sources = new0(sd_event_source*, _NSIG);
1134 if (!e->signal_sources)
1136 } else if (e->signal_sources[sig])
1139 s = source_new(e, !ret, SOURCE_SIGNAL);
1143 s->signal.sig = sig;
1144 s->signal.callback = callback;
1145 s->userdata = userdata;
1146 s->enabled = SD_EVENT_ON;
1148 e->signal_sources[sig] = s;
1150 r = event_make_signal_data(e, sig, &d);
1156 /* Use the signal name as description for the event source by default */
1157 (void) sd_event_source_set_description(s, signal_to_string(sig));
1165 #if 0 /// UNNEEDED by elogind
1166 _public_ int sd_event_add_child(
1168 sd_event_source **ret,
1171 sd_event_child_handler_t callback,
1177 assert_return(e, -EINVAL);
1178 assert_return(pid > 1, -EINVAL);
1179 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1180 assert_return(options != 0, -EINVAL);
1181 assert_return(callback, -EINVAL);
1182 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1183 assert_return(!event_pid_changed(e), -ECHILD);
1185 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1189 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1192 s = source_new(e, !ret, SOURCE_CHILD);
1197 s->child.options = options;
1198 s->child.callback = callback;
1199 s->userdata = userdata;
1200 s->enabled = SD_EVENT_ONESHOT;
1202 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1208 e->n_enabled_child_sources ++;
1210 r = event_make_signal_data(e, SIGCHLD, NULL);
1212 e->n_enabled_child_sources--;
1217 e->need_process_child = true;
1225 _public_ int sd_event_add_defer(
1227 sd_event_source **ret,
1228 sd_event_handler_t callback,
1234 assert_return(e, -EINVAL);
1235 assert_return(callback, -EINVAL);
1236 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1237 assert_return(!event_pid_changed(e), -ECHILD);
1239 s = source_new(e, !ret, SOURCE_DEFER);
1243 s->defer.callback = callback;
1244 s->userdata = userdata;
1245 s->enabled = SD_EVENT_ONESHOT;
1247 r = source_set_pending(s, true);
1260 _public_ int sd_event_add_post(
1262 sd_event_source **ret,
1263 sd_event_handler_t callback,
1269 assert_return(e, -EINVAL);
1270 assert_return(callback, -EINVAL);
1271 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1272 assert_return(!event_pid_changed(e), -ECHILD);
1274 r = set_ensure_allocated(&e->post_sources, NULL);
1278 s = source_new(e, !ret, SOURCE_POST);
1282 s->post.callback = callback;
1283 s->userdata = userdata;
1284 s->enabled = SD_EVENT_ON;
1286 r = set_put(e->post_sources, s);
1298 _public_ int sd_event_add_exit(
1300 sd_event_source **ret,
1301 sd_event_handler_t callback,
1307 assert_return(e, -EINVAL);
1308 assert_return(callback, -EINVAL);
1309 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1310 assert_return(!event_pid_changed(e), -ECHILD);
1312 r = prioq_ensure_allocated(&e->exit, exit_prioq_compare);
1316 s = source_new(e, !ret, SOURCE_EXIT);
1320 s->exit.callback = callback;
1321 s->userdata = userdata;
1322 s->exit.prioq_index = PRIOQ_IDX_NULL;
1323 s->enabled = SD_EVENT_ONESHOT;
1325 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1337 #if 0 /// UNNEEDED by elogind
1338 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1339 assert_return(s, NULL);
1341 assert(s->n_ref >= 1);
1348 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1353 assert(s->n_ref >= 1);
1356 if (s->n_ref <= 0) {
1357 /* Here's a special hack: when we are called from a
1358 * dispatch handler we won't free the event source
1359 * immediately, but we will detach the fd from the
1360 * epoll. This way it is safe for the caller to unref
1361 * the event source and immediately close the fd, but
1362 * we still retain a valid event source object after
1365 if (s->dispatching) {
1366 if (s->type == SOURCE_IO)
1367 source_io_unregister(s);
1369 source_disconnect(s);
1377 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1378 assert_return(s, -EINVAL);
1379 assert_return(!event_pid_changed(s->event), -ECHILD);
1381 return free_and_strdup(&s->description, description);
1384 #if 0 /// UNNEEDED by elogind
1385 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1386 assert_return(s, -EINVAL);
1387 assert_return(description, -EINVAL);
1388 assert_return(s->description, -ENXIO);
1389 assert_return(!event_pid_changed(s->event), -ECHILD);
1391 *description = s->description;
1396 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1397 assert_return(s, NULL);
1402 #if 0 /// UNNEEDED by elogind
1403 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1404 assert_return(s, -EINVAL);
1405 assert_return(s->type != SOURCE_EXIT, -EDOM);
1406 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1407 assert_return(!event_pid_changed(s->event), -ECHILD);
1412 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1413 assert_return(s, -EINVAL);
1414 assert_return(s->type == SOURCE_IO, -EDOM);
1415 assert_return(!event_pid_changed(s->event), -ECHILD);
1421 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1424 assert_return(s, -EINVAL);
1425 assert_return(fd >= 0, -EBADF);
1426 assert_return(s->type == SOURCE_IO, -EDOM);
1427 assert_return(!event_pid_changed(s->event), -ECHILD);
1432 if (s->enabled == SD_EVENT_OFF) {
1434 s->io.registered = false;
1438 saved_fd = s->io.fd;
1439 assert(s->io.registered);
1442 s->io.registered = false;
1444 r = source_io_register(s, s->enabled, s->io.events);
1446 s->io.fd = saved_fd;
1447 s->io.registered = true;
1451 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1457 #if 0 /// UNNEEDED by elogind
1458 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1459 assert_return(s, -EINVAL);
1460 assert_return(events, -EINVAL);
1461 assert_return(s->type == SOURCE_IO, -EDOM);
1462 assert_return(!event_pid_changed(s->event), -ECHILD);
1464 *events = s->io.events;
1469 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1472 assert_return(s, -EINVAL);
1473 assert_return(s->type == SOURCE_IO, -EDOM);
1474 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1475 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1476 assert_return(!event_pid_changed(s->event), -ECHILD);
1478 /* edge-triggered updates are never skipped, so we can reset edges */
1479 if (s->io.events == events && !(events & EPOLLET))
1482 if (s->enabled != SD_EVENT_OFF) {
1483 r = source_io_register(s, s->enabled, events);
1488 s->io.events = events;
1489 source_set_pending(s, false);
1494 #if 0 /// UNNEEDED by elogind
1495 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1496 assert_return(s, -EINVAL);
1497 assert_return(revents, -EINVAL);
1498 assert_return(s->type == SOURCE_IO, -EDOM);
1499 assert_return(s->pending, -ENODATA);
1500 assert_return(!event_pid_changed(s->event), -ECHILD);
1502 *revents = s->io.revents;
1506 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1507 assert_return(s, -EINVAL);
1508 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1509 assert_return(!event_pid_changed(s->event), -ECHILD);
1511 return s->signal.sig;
1514 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1515 assert_return(s, -EINVAL);
1516 assert_return(!event_pid_changed(s->event), -ECHILD);
1522 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1525 assert_return(s, -EINVAL);
1526 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1527 assert_return(!event_pid_changed(s->event), -ECHILD);
1529 if (s->priority == priority)
1532 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1533 struct signal_data *old, *d;
1535 /* Move us from the signalfd belonging to the old
1536 * priority to the signalfd of the new priority */
1538 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1540 s->priority = priority;
1542 r = event_make_signal_data(s->event, s->signal.sig, &d);
1544 s->priority = old->priority;
1548 event_unmask_signal_data(s->event, old, s->signal.sig);
1550 s->priority = priority;
1553 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1556 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1558 if (s->type == SOURCE_EXIT)
1559 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1564 #if 0 /// UNNEEDED by elogind
1565 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1566 assert_return(s, -EINVAL);
1567 assert_return(m, -EINVAL);
1568 assert_return(!event_pid_changed(s->event), -ECHILD);
1575 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1578 assert_return(s, -EINVAL);
1579 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1580 assert_return(!event_pid_changed(s->event), -ECHILD);
1582 /* If we are dead anyway, we are fine with turning off
1583 * sources, but everything else needs to fail. */
1584 if (s->event->state == SD_EVENT_FINISHED)
1585 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1587 if (s->enabled == m)
1590 if (m == SD_EVENT_OFF) {
1595 source_io_unregister(s);
1599 case SOURCE_TIME_REALTIME:
1600 case SOURCE_TIME_BOOTTIME:
1601 case SOURCE_TIME_MONOTONIC:
1602 case SOURCE_TIME_REALTIME_ALARM:
1603 case SOURCE_TIME_BOOTTIME_ALARM: {
1604 struct clock_data *d;
1607 d = event_get_clock_data(s->event, s->type);
1610 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1611 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1612 d->needs_rearm = true;
1619 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1625 assert(s->event->n_enabled_child_sources > 0);
1626 s->event->n_enabled_child_sources--;
1628 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1633 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1642 assert_not_reached("Wut? I shouldn't exist.");
1649 r = source_io_register(s, m, s->io.events);
1656 case SOURCE_TIME_REALTIME:
1657 case SOURCE_TIME_BOOTTIME:
1658 case SOURCE_TIME_MONOTONIC:
1659 case SOURCE_TIME_REALTIME_ALARM:
1660 case SOURCE_TIME_BOOTTIME_ALARM: {
1661 struct clock_data *d;
1664 d = event_get_clock_data(s->event, s->type);
1667 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1668 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1669 d->needs_rearm = true;
1677 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1679 s->enabled = SD_EVENT_OFF;
1680 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1688 if (s->enabled == SD_EVENT_OFF)
1689 s->event->n_enabled_child_sources++;
1693 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1695 s->enabled = SD_EVENT_OFF;
1696 s->event->n_enabled_child_sources--;
1697 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1705 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1714 assert_not_reached("Wut? I shouldn't exist.");
1719 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1722 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1727 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1728 assert_return(s, -EINVAL);
1729 assert_return(usec, -EINVAL);
1730 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1731 assert_return(!event_pid_changed(s->event), -ECHILD);
1733 *usec = s->time.next;
1737 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1738 struct clock_data *d;
1740 assert_return(s, -EINVAL);
1741 assert_return(usec != (uint64_t) -1, -EINVAL);
1742 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1743 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1744 assert_return(!event_pid_changed(s->event), -ECHILD);
1746 s->time.next = usec;
1748 source_set_pending(s, false);
1750 d = event_get_clock_data(s->event, s->type);
1753 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1754 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1755 d->needs_rearm = true;
1760 #if 0 /// UNNEEDED by elogind
1761 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1762 assert_return(s, -EINVAL);
1763 assert_return(usec, -EINVAL);
1764 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1765 assert_return(!event_pid_changed(s->event), -ECHILD);
1767 *usec = s->time.accuracy;
1771 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1772 struct clock_data *d;
1774 assert_return(s, -EINVAL);
1775 assert_return(usec != (uint64_t) -1, -EINVAL);
1776 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1777 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1778 assert_return(!event_pid_changed(s->event), -ECHILD);
1781 usec = DEFAULT_ACCURACY_USEC;
1783 s->time.accuracy = usec;
1785 source_set_pending(s, false);
1787 d = event_get_clock_data(s->event, s->type);
1790 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1791 d->needs_rearm = true;
1796 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1797 assert_return(s, -EINVAL);
1798 assert_return(clock, -EINVAL);
1799 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1800 assert_return(!event_pid_changed(s->event), -ECHILD);
1802 *clock = event_source_type_to_clock(s->type);
1806 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1807 assert_return(s, -EINVAL);
1808 assert_return(pid, -EINVAL);
1809 assert_return(s->type == SOURCE_CHILD, -EDOM);
1810 assert_return(!event_pid_changed(s->event), -ECHILD);
1812 *pid = s->child.pid;
1817 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1820 assert_return(s, -EINVAL);
1821 assert_return(s->type != SOURCE_EXIT, -EDOM);
1822 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1823 assert_return(!event_pid_changed(s->event), -ECHILD);
1825 if (s->prepare == callback)
1828 if (callback && s->prepare) {
1829 s->prepare = callback;
1833 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1837 s->prepare = callback;
1840 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1844 prioq_remove(s->event->prepare, s, &s->prepare_index);
1849 #if 0 /// UNNEEDED by elogind
1850 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1851 assert_return(s, NULL);
1856 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1859 assert_return(s, NULL);
1862 s->userdata = userdata;
1868 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1879 initialize_perturb(e);
1882 Find a good time to wake up again between times a and b. We
1883 have two goals here:
1885 a) We want to wake up as seldom as possible, hence prefer
1886 later times over earlier times.
1888 b) But if we have to wake up, then let's make sure to
1889 dispatch as much as possible on the entire system.
1891 We implement this by waking up everywhere at the same time
1892 within any given minute if we can, synchronised via the
1893 perturbation value determined from the boot ID. If we can't,
1894 then we try to find the same spot in every 10s, then 1s and
1895 then 250ms step. Otherwise, we pick the last possible time
1899 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1901 if (_unlikely_(c < USEC_PER_MINUTE))
1904 c -= USEC_PER_MINUTE;
1910 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1912 if (_unlikely_(c < USEC_PER_SEC*10))
1915 c -= USEC_PER_SEC*10;
1921 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1923 if (_unlikely_(c < USEC_PER_SEC))
1932 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1934 if (_unlikely_(c < USEC_PER_MSEC*250))
1937 c -= USEC_PER_MSEC*250;
1946 static int event_arm_timer(
1948 struct clock_data *d) {
1950 struct itimerspec its = {};
1951 sd_event_source *a, *b;
1958 if (!d->needs_rearm)
1961 d->needs_rearm = false;
1963 a = prioq_peek(d->earliest);
1964 if (!a || a->enabled == SD_EVENT_OFF) {
1969 if (d->next == USEC_INFINITY)
1973 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1977 d->next = USEC_INFINITY;
1981 b = prioq_peek(d->latest);
1982 assert_se(b && b->enabled != SD_EVENT_OFF);
1984 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1988 assert_se(d->fd >= 0);
1991 /* We don' want to disarm here, just mean some time looooong ago. */
1992 its.it_value.tv_sec = 0;
1993 its.it_value.tv_nsec = 1;
1995 timespec_store(&its.it_value, t);
1997 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2005 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2008 assert(s->type == SOURCE_IO);
2010 /* If the event source was already pending, we just OR in the
2011 * new revents, otherwise we reset the value. The ORing is
2012 * necessary to handle EPOLLONESHOT events properly where
2013 * readability might happen independently of writability, and
2014 * we need to keep track of both */
2017 s->io.revents |= revents;
2019 s->io.revents = revents;
2021 return source_set_pending(s, true);
2024 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2031 assert_return(events == EPOLLIN, -EIO);
2033 ss = read(fd, &x, sizeof(x));
2035 if (errno == EAGAIN || errno == EINTR)
2041 if (_unlikely_(ss != sizeof(x)))
2045 *next = USEC_INFINITY;
2050 static int process_timer(
2053 struct clock_data *d) {
2062 s = prioq_peek(d->earliest);
2065 s->enabled == SD_EVENT_OFF ||
2069 r = source_set_pending(s, true);
2073 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2074 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2075 d->needs_rearm = true;
2081 static int process_child(sd_event *e) {
2088 e->need_process_child = false;
2091 So, this is ugly. We iteratively invoke waitid() with P_PID
2092 + WNOHANG for each PID we wait for, instead of using
2093 P_ALL. This is because we only want to get child
2094 information of very specific child processes, and not all
2095 of them. We might not have processed the SIGCHLD even of a
2096 previous invocation and we don't want to maintain a
2097 unbounded *per-child* event queue, hence we really don't
2098 want anything flushed out of the kernel's queue that we
2099 don't care about. Since this is O(n) this means that if you
2100 have a lot of processes you probably want to handle SIGCHLD
2103 We do not reap the children here (by using WNOWAIT), this
2104 is only done after the event source is dispatched so that
2105 the callback still sees the process as a zombie.
2108 HASHMAP_FOREACH(s, e->child_sources, i) {
2109 assert(s->type == SOURCE_CHILD);
2114 if (s->enabled == SD_EVENT_OFF)
2117 zero(s->child.siginfo);
2118 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2119 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2123 if (s->child.siginfo.si_pid != 0) {
2125 s->child.siginfo.si_code == CLD_EXITED ||
2126 s->child.siginfo.si_code == CLD_KILLED ||
2127 s->child.siginfo.si_code == CLD_DUMPED;
2129 if (!zombie && (s->child.options & WEXITED)) {
2130 /* If the child isn't dead then let's
2131 * immediately remove the state change
2132 * from the queue, since there's no
2133 * benefit in leaving it queued */
2135 assert(s->child.options & (WSTOPPED|WCONTINUED));
2136 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2139 r = source_set_pending(s, true);
2148 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2149 bool read_one = false;
2153 assert_return(events == EPOLLIN, -EIO);
2155 /* If there's a signal queued on this priority and SIGCHLD is
2156 on this priority too, then make sure to recheck the
2157 children we watch. This is because we only ever dequeue
2158 the first signal per priority, and if we dequeue one, and
2159 SIGCHLD might be enqueued later we wouldn't know, but we
2160 might have higher priority children we care about hence we
2161 need to check that explicitly. */
2163 if (sigismember(&d->sigset, SIGCHLD))
2164 e->need_process_child = true;
2166 /* If there's already an event source pending for this
2167 * priority we don't read another */
2172 struct signalfd_siginfo si;
2174 sd_event_source *s = NULL;
2176 n = read(d->fd, &si, sizeof(si));
2178 if (errno == EAGAIN || errno == EINTR)
2184 if (_unlikely_(n != sizeof(si)))
2187 assert(si.ssi_signo < _NSIG);
2191 if (e->signal_sources)
2192 s = e->signal_sources[si.ssi_signo];
2198 s->signal.siginfo = si;
2201 r = source_set_pending(s, true);
2209 static int source_dispatch(sd_event_source *s) {
2213 assert(s->pending || s->type == SOURCE_EXIT);
2215 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2216 r = source_set_pending(s, false);
2221 if (s->type != SOURCE_POST) {
2225 /* If we execute a non-post source, let's mark all
2226 * post sources as pending */
2228 SET_FOREACH(z, s->event->post_sources, i) {
2229 if (z->enabled == SD_EVENT_OFF)
2232 r = source_set_pending(z, true);
2238 if (s->enabled == SD_EVENT_ONESHOT) {
2239 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2244 s->dispatching = true;
2249 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2252 case SOURCE_TIME_REALTIME:
2253 case SOURCE_TIME_BOOTTIME:
2254 case SOURCE_TIME_MONOTONIC:
2255 case SOURCE_TIME_REALTIME_ALARM:
2256 case SOURCE_TIME_BOOTTIME_ALARM:
2257 r = s->time.callback(s, s->time.next, s->userdata);
2261 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2264 case SOURCE_CHILD: {
2267 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2268 s->child.siginfo.si_code == CLD_KILLED ||
2269 s->child.siginfo.si_code == CLD_DUMPED;
2271 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2273 /* Now, reap the PID for good. */
2275 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2281 r = s->defer.callback(s, s->userdata);
2285 r = s->post.callback(s, s->userdata);
2289 r = s->exit.callback(s, s->userdata);
2292 case SOURCE_WATCHDOG:
2293 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2294 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2295 assert_not_reached("Wut? I shouldn't exist.");
2298 s->dispatching = false;
2302 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2304 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2310 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2315 static int event_prepare(sd_event *e) {
2323 s = prioq_peek(e->prepare);
2324 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2327 s->prepare_iteration = e->iteration;
2328 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2334 s->dispatching = true;
2335 r = s->prepare(s, s->userdata);
2336 s->dispatching = false;
2340 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2342 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2348 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2354 static int dispatch_exit(sd_event *e) {
2360 p = prioq_peek(e->exit);
2361 if (!p || p->enabled == SD_EVENT_OFF) {
2362 e->state = SD_EVENT_FINISHED;
2368 e->state = SD_EVENT_EXITING;
2370 r = source_dispatch(p);
2372 e->state = SD_EVENT_INITIAL;
2378 static sd_event_source* event_next_pending(sd_event *e) {
2383 p = prioq_peek(e->pending);
2387 if (p->enabled == SD_EVENT_OFF)
2393 static int arm_watchdog(sd_event *e) {
2394 struct itimerspec its = {};
2399 assert(e->watchdog_fd >= 0);
2401 t = sleep_between(e,
2402 e->watchdog_last + (e->watchdog_period / 2),
2403 e->watchdog_last + (e->watchdog_period * 3 / 4));
2405 timespec_store(&its.it_value, t);
2407 /* Make sure we never set the watchdog to 0, which tells the
2408 * kernel to disable it. */
2409 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2410 its.it_value.tv_nsec = 1;
2412 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2419 static int process_watchdog(sd_event *e) {
2425 /* Don't notify watchdog too often */
2426 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2429 sd_notify(false, "WATCHDOG=1");
2430 e->watchdog_last = e->timestamp.monotonic;
2432 return arm_watchdog(e);
2435 _public_ int sd_event_prepare(sd_event *e) {
2438 assert_return(e, -EINVAL);
2439 assert_return(!event_pid_changed(e), -ECHILD);
2440 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2441 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2443 if (e->exit_requested)
2448 e->state = SD_EVENT_PREPARING;
2449 r = event_prepare(e);
2450 e->state = SD_EVENT_INITIAL;
2454 r = event_arm_timer(e, &e->realtime);
2458 r = event_arm_timer(e, &e->boottime);
2462 r = event_arm_timer(e, &e->monotonic);
2466 r = event_arm_timer(e, &e->realtime_alarm);
2470 r = event_arm_timer(e, &e->boottime_alarm);
2474 if (event_next_pending(e) || e->need_process_child)
2477 e->state = SD_EVENT_ARMED;
2482 e->state = SD_EVENT_ARMED;
2483 r = sd_event_wait(e, 0);
2485 e->state = SD_EVENT_ARMED;
2490 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2491 struct epoll_event *ev_queue;
2492 unsigned ev_queue_max;
2495 assert_return(e, -EINVAL);
2496 assert_return(!event_pid_changed(e), -ECHILD);
2497 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2498 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2500 if (e->exit_requested) {
2501 e->state = SD_EVENT_PENDING;
2505 ev_queue_max = MAX(e->n_sources, 1u);
2506 ev_queue = newa(struct epoll_event, ev_queue_max);
2508 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2509 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2511 if (errno == EINTR) {
2512 e->state = SD_EVENT_PENDING;
2520 dual_timestamp_get(&e->timestamp);
2521 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2523 for (i = 0; i < m; i++) {
2525 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2526 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2528 WakeupType *t = ev_queue[i].data.ptr;
2532 case WAKEUP_EVENT_SOURCE:
2533 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2536 case WAKEUP_CLOCK_DATA: {
2537 struct clock_data *d = ev_queue[i].data.ptr;
2538 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2542 case WAKEUP_SIGNAL_DATA:
2543 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2547 assert_not_reached("Invalid wake-up pointer");
2554 r = process_watchdog(e);
2558 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2562 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2566 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2570 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2574 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2578 if (e->need_process_child) {
2579 r = process_child(e);
2584 if (event_next_pending(e)) {
2585 e->state = SD_EVENT_PENDING;
2593 e->state = SD_EVENT_INITIAL;
2598 _public_ int sd_event_dispatch(sd_event *e) {
2602 assert_return(e, -EINVAL);
2603 assert_return(!event_pid_changed(e), -ECHILD);
2604 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2605 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2607 if (e->exit_requested)
2608 return dispatch_exit(e);
2610 p = event_next_pending(e);
2614 e->state = SD_EVENT_RUNNING;
2615 r = source_dispatch(p);
2616 e->state = SD_EVENT_INITIAL;
2623 e->state = SD_EVENT_INITIAL;
2628 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2631 assert_return(e, -EINVAL);
2632 assert_return(!event_pid_changed(e), -ECHILD);
2633 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2634 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2636 r = sd_event_prepare(e);
2638 /* There was nothing? Then wait... */
2639 r = sd_event_wait(e, timeout);
2642 /* There's something now, then let's dispatch it */
2643 r = sd_event_dispatch(e);
2653 #if 0 /// UNNEEDED by elogind
2654 _public_ int sd_event_loop(sd_event *e) {
2657 assert_return(e, -EINVAL);
2658 assert_return(!event_pid_changed(e), -ECHILD);
2659 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2663 while (e->state != SD_EVENT_FINISHED) {
2664 r = sd_event_run(e, (uint64_t) -1);
2676 _public_ int sd_event_get_fd(sd_event *e) {
2678 assert_return(e, -EINVAL);
2679 assert_return(!event_pid_changed(e), -ECHILD);
2685 _public_ int sd_event_get_state(sd_event *e) {
2686 assert_return(e, -EINVAL);
2687 assert_return(!event_pid_changed(e), -ECHILD);
2692 #if 0 /// UNNEEDED by elogind
2693 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2694 assert_return(e, -EINVAL);
2695 assert_return(code, -EINVAL);
2696 assert_return(!event_pid_changed(e), -ECHILD);
2698 if (!e->exit_requested)
2701 *code = e->exit_code;
2706 _public_ int sd_event_exit(sd_event *e, int code) {
2707 assert_return(e, -EINVAL);
2708 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2709 assert_return(!event_pid_changed(e), -ECHILD);
2711 e->exit_requested = true;
2712 e->exit_code = code;
2717 #if 0 /// UNNEEDED by elogind
2718 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2719 assert_return(e, -EINVAL);
2720 assert_return(usec, -EINVAL);
2721 assert_return(!event_pid_changed(e), -ECHILD);
2723 if (!dual_timestamp_is_set(&e->timestamp)) {
2724 /* Implicitly fall back to now() if we never ran
2725 * before and thus have no cached time. */
2732 case CLOCK_REALTIME:
2733 case CLOCK_REALTIME_ALARM:
2734 *usec = e->timestamp.realtime;
2737 case CLOCK_MONOTONIC:
2738 *usec = e->timestamp.monotonic;
2741 case CLOCK_BOOTTIME:
2742 case CLOCK_BOOTTIME_ALARM:
2743 *usec = e->timestamp_boottime;
2751 _public_ int sd_event_default(sd_event **ret) {
2753 static thread_local sd_event *default_event = NULL;
2758 return !!default_event;
2760 if (default_event) {
2761 *ret = sd_event_ref(default_event);
2765 r = sd_event_new(&e);
2769 e->default_event_ptr = &default_event;
2777 #if 0 /// UNNEEDED by elogind
2778 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2779 assert_return(e, -EINVAL);
2780 assert_return(tid, -EINVAL);
2781 assert_return(!event_pid_changed(e), -ECHILD);
2792 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2795 assert_return(e, -EINVAL);
2796 assert_return(!event_pid_changed(e), -ECHILD);
2798 if (e->watchdog == !!b)
2802 struct epoll_event ev = {};
2804 r = sd_watchdog_enabled(false, &e->watchdog_period);
2808 /* Issue first ping immediately */
2809 sd_notify(false, "WATCHDOG=1");
2810 e->watchdog_last = now(CLOCK_MONOTONIC);
2812 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2813 if (e->watchdog_fd < 0)
2816 r = arm_watchdog(e);
2820 ev.events = EPOLLIN;
2821 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2823 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2830 if (e->watchdog_fd >= 0) {
2831 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2832 e->watchdog_fd = safe_close(e->watchdog_fd);
2840 e->watchdog_fd = safe_close(e->watchdog_fd);
2844 #if 0 /// UNNEEDED by elogind
2845 _public_ int sd_event_get_watchdog(sd_event *e) {
2846 assert_return(e, -EINVAL);
2847 assert_return(!event_pid_changed(e), -ECHILD);