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>
28 #include "sd-daemon.h"
33 #include "time-util.h"
40 #define EPOLL_QUEUE_MAX 512U
41 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
43 typedef enum EventSourceType {
47 SOURCE_TIME_MONOTONIC,
48 SOURCE_TIME_REALTIME_ALARM,
49 SOURCE_TIME_BOOTTIME_ALARM,
56 _SOURCE_EVENT_SOURCE_TYPE_MAX,
57 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
60 #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)
62 struct sd_event_source {
67 sd_event_handler_t prepare;
71 EventSourceType type:5;
78 unsigned pending_index;
79 unsigned prepare_index;
80 unsigned pending_iteration;
81 unsigned prepare_iteration;
83 LIST_FIELDS(sd_event_source, sources);
87 sd_event_io_handler_t callback;
94 sd_event_time_handler_t callback;
95 usec_t next, accuracy;
96 unsigned earliest_index;
97 unsigned latest_index;
100 sd_event_signal_handler_t callback;
101 struct signalfd_siginfo siginfo;
105 sd_event_child_handler_t callback;
111 sd_event_handler_t callback;
114 sd_event_handler_t callback;
117 sd_event_handler_t callback;
118 unsigned prioq_index;
126 /* For all clocks we maintain two priority queues each, one
127 * ordered for the earliest times the events may be
128 * dispatched, and one ordered by the latest times they must
129 * have been dispatched. The range between the top entries in
130 * the two prioqs is the time window we can freely schedule
150 /* timerfd_create() only supports these five clocks so far. We
151 * can add support for more clocks when the kernel learns to
152 * deal with them, too. */
153 struct clock_data realtime;
154 struct clock_data boottime;
155 struct clock_data monotonic;
156 struct clock_data realtime_alarm;
157 struct clock_data boottime_alarm;
162 sd_event_source **signal_sources;
164 Hashmap *child_sources;
165 unsigned n_enabled_child_sources;
174 dual_timestamp timestamp;
175 usec_t timestamp_boottime;
178 bool exit_requested:1;
179 bool need_process_child:1;
185 sd_event **default_event_ptr;
187 usec_t watchdog_last, watchdog_period;
191 LIST_HEAD(sd_event_source, sources);
194 static void source_disconnect(sd_event_source *s);
196 static int pending_prioq_compare(const void *a, const void *b) {
197 const sd_event_source *x = a, *y = b;
202 /* Enabled ones first */
203 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
205 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
208 /* Lower priority values first */
209 if (x->priority < y->priority)
211 if (x->priority > y->priority)
214 /* Older entries first */
215 if (x->pending_iteration < y->pending_iteration)
217 if (x->pending_iteration > y->pending_iteration)
220 /* Stability for the rest */
229 static int prepare_prioq_compare(const void *a, const void *b) {
230 const sd_event_source *x = a, *y = b;
235 /* Move most recently prepared ones last, so that we can stop
236 * preparing as soon as we hit one that has already been
237 * prepared in the current iteration */
238 if (x->prepare_iteration < y->prepare_iteration)
240 if (x->prepare_iteration > y->prepare_iteration)
243 /* Enabled ones first */
244 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
246 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
249 /* Lower priority values first */
250 if (x->priority < y->priority)
252 if (x->priority > y->priority)
255 /* Stability for the rest */
264 static int earliest_time_prioq_compare(const void *a, const void *b) {
265 const sd_event_source *x = a, *y = b;
267 assert(EVENT_SOURCE_IS_TIME(x->type));
268 assert(x->type == y->type);
270 /* Enabled ones first */
271 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
273 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
276 /* Move the pending ones to the end */
277 if (!x->pending && y->pending)
279 if (x->pending && !y->pending)
283 if (x->time.next < y->time.next)
285 if (x->time.next > y->time.next)
288 /* Stability for the rest */
297 static int latest_time_prioq_compare(const void *a, const void *b) {
298 const sd_event_source *x = a, *y = b;
300 assert(EVENT_SOURCE_IS_TIME(x->type));
301 assert(x->type == y->type);
303 /* Enabled ones first */
304 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
306 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
309 /* Move the pending ones to the end */
310 if (!x->pending && y->pending)
312 if (x->pending && !y->pending)
316 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
318 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
321 /* Stability for the rest */
330 static int exit_prioq_compare(const void *a, const void *b) {
331 const sd_event_source *x = a, *y = b;
333 assert(x->type == SOURCE_EXIT);
334 assert(y->type == SOURCE_EXIT);
336 /* Enabled ones first */
337 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
339 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
342 /* Lower priority values first */
343 if (x->priority < y->priority)
345 if (x->priority > y->priority)
348 /* Stability for the rest */
357 static void free_clock_data(struct clock_data *d) {
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->signal_fd);
383 safe_close(e->watchdog_fd);
385 free_clock_data(&e->realtime);
386 free_clock_data(&e->boottime);
387 free_clock_data(&e->monotonic);
388 free_clock_data(&e->realtime_alarm);
389 free_clock_data(&e->boottime_alarm);
391 prioq_free(e->pending);
392 prioq_free(e->prepare);
395 free(e->signal_sources);
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->signal_fd = 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->original_pid = getpid();
416 e->perturb = USEC_INFINITY;
418 assert_se(sigemptyset(&e->sigset) == 0);
420 e->pending = prioq_new(pending_prioq_compare);
426 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
427 if (e->epoll_fd < 0) {
440 _public_ sd_event* sd_event_ref(sd_event *e) {
441 assert_return(e, NULL);
443 assert(e->n_ref >= 1);
449 _public_ sd_event* sd_event_unref(sd_event *e) {
454 assert(e->n_ref >= 1);
463 static bool event_pid_changed(sd_event *e) {
466 /* We don't support people creating am event loop and keeping
467 * it around over a fork(). Let's complain. */
469 return e->original_pid != getpid();
472 static int source_io_unregister(sd_event_source *s) {
476 assert(s->type == SOURCE_IO);
478 if (!s->io.registered)
481 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
485 s->io.registered = false;
489 static int source_io_register(
494 struct epoll_event ev = {};
498 assert(s->type == SOURCE_IO);
499 assert(enabled != SD_EVENT_OFF);
504 if (enabled == SD_EVENT_ONESHOT)
505 ev.events |= EPOLLONESHOT;
507 if (s->io.registered)
508 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
510 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
515 s->io.registered = true;
520 static clockid_t event_source_type_to_clock(EventSourceType t) {
524 case SOURCE_TIME_REALTIME:
525 return CLOCK_REALTIME;
527 case SOURCE_TIME_BOOTTIME:
528 return CLOCK_BOOTTIME;
530 case SOURCE_TIME_MONOTONIC:
531 return CLOCK_MONOTONIC;
533 case SOURCE_TIME_REALTIME_ALARM:
534 return CLOCK_REALTIME_ALARM;
536 case SOURCE_TIME_BOOTTIME_ALARM:
537 return CLOCK_BOOTTIME_ALARM;
540 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 bool need_signal(sd_event *e, int signal) {
594 return (e->signal_sources && e->signal_sources[signal] &&
595 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
597 (signal == SIGCHLD &&
598 e->n_enabled_child_sources > 0);
601 static int event_update_signal_fd(sd_event *e) {
602 struct epoll_event ev = {};
608 add_to_epoll = e->signal_fd < 0;
610 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
620 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
622 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
624 e->signal_fd = safe_close(e->signal_fd);
631 static void source_disconnect(sd_event_source *s) {
639 assert(s->event->n_sources > 0);
645 source_io_unregister(s);
649 case SOURCE_TIME_REALTIME:
650 case SOURCE_TIME_BOOTTIME:
651 case SOURCE_TIME_MONOTONIC:
652 case SOURCE_TIME_REALTIME_ALARM:
653 case SOURCE_TIME_BOOTTIME_ALARM: {
654 struct clock_data *d;
656 d = event_get_clock_data(s->event, s->type);
659 prioq_remove(d->earliest, s, &s->time.earliest_index);
660 prioq_remove(d->latest, s, &s->time.latest_index);
661 d->needs_rearm = true;
666 if (s->signal.sig > 0) {
667 if (s->event->signal_sources)
668 s->event->signal_sources[s->signal.sig] = NULL;
670 /* If the signal was on and now it is off... */
671 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
672 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
674 (void) event_update_signal_fd(s->event);
675 /* If disabling failed, we might get a spurious event,
676 * but otherwise nothing bad should happen. */
683 if (s->child.pid > 0) {
684 if (s->enabled != SD_EVENT_OFF) {
685 assert(s->event->n_enabled_child_sources > 0);
686 s->event->n_enabled_child_sources--;
688 /* We know the signal was on, if it is off now... */
689 if (!need_signal(s->event, SIGCHLD)) {
690 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
692 (void) event_update_signal_fd(s->event);
693 /* If disabling failed, we might get a spurious event,
694 * but otherwise nothing bad should happen. */
698 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
708 set_remove(s->event->post_sources, s);
712 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
716 assert_not_reached("Wut? I shouldn't exist.");
720 prioq_remove(s->event->pending, s, &s->pending_index);
723 prioq_remove(s->event->prepare, s, &s->prepare_index);
727 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
729 LIST_REMOVE(sources, event->sources, s);
733 sd_event_unref(event);
736 static void source_free(sd_event_source *s) {
739 source_disconnect(s);
740 free(s->description);
744 static int source_set_pending(sd_event_source *s, bool b) {
748 assert(s->type != SOURCE_EXIT);
756 s->pending_iteration = s->event->iteration;
758 r = prioq_put(s->event->pending, s, &s->pending_index);
764 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
766 if (EVENT_SOURCE_IS_TIME(s->type)) {
767 struct clock_data *d;
769 d = event_get_clock_data(s->event, s->type);
772 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
773 prioq_reshuffle(d->latest, s, &s->time.latest_index);
774 d->needs_rearm = true;
780 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
785 s = new0(sd_event_source, 1);
791 s->floating = floating;
793 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
798 LIST_PREPEND(sources, e->sources, s);
804 _public_ int sd_event_add_io(
806 sd_event_source **ret,
809 sd_event_io_handler_t callback,
815 assert_return(e, -EINVAL);
816 assert_return(fd >= 0, -EINVAL);
817 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
818 assert_return(callback, -EINVAL);
819 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
820 assert_return(!event_pid_changed(e), -ECHILD);
822 s = source_new(e, !ret, SOURCE_IO);
827 s->io.events = events;
828 s->io.callback = callback;
829 s->userdata = userdata;
830 s->enabled = SD_EVENT_ON;
832 r = source_io_register(s, s->enabled, events);
844 static void initialize_perturb(sd_event *e) {
845 sd_id128_t bootid = {};
847 /* When we sleep for longer, we try to realign the wakeup to
848 the same time wihtin each minute/second/250ms, so that
849 events all across the system can be coalesced into a single
850 CPU wakeup. However, let's take some system-specific
851 randomness for this value, so that in a network of systems
852 with synced clocks timer events are distributed a
853 bit. Here, we calculate a perturbation usec offset from the
856 if (_likely_(e->perturb != USEC_INFINITY))
859 if (sd_id128_get_boot(&bootid) >= 0)
860 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
863 static int event_setup_timer_fd(
865 struct clock_data *d,
868 struct epoll_event ev = {};
874 if (_likely_(d->fd >= 0))
877 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
882 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
884 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
894 _public_ int sd_event_add_time(
896 sd_event_source **ret,
900 sd_event_time_handler_t callback,
903 EventSourceType type;
905 struct clock_data *d;
908 assert_return(e, -EINVAL);
909 assert_return(usec != (uint64_t) -1, -EINVAL);
910 assert_return(accuracy != (uint64_t) -1, -EINVAL);
911 assert_return(callback, -EINVAL);
912 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
913 assert_return(!event_pid_changed(e), -ECHILD);
915 type = clock_to_event_source_type(clock);
916 assert_return(type >= 0, -ENOTSUP);
918 d = event_get_clock_data(e, type);
922 d->earliest = prioq_new(earliest_time_prioq_compare);
928 d->latest = prioq_new(latest_time_prioq_compare);
934 r = event_setup_timer_fd(e, d, clock);
939 s = source_new(e, !ret, type);
944 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
945 s->time.callback = callback;
946 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
947 s->userdata = userdata;
948 s->enabled = SD_EVENT_ONESHOT;
950 d->needs_rearm = true;
952 r = prioq_put(d->earliest, s, &s->time.earliest_index);
956 r = prioq_put(d->latest, s, &s->time.latest_index);
970 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
973 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
976 _public_ int sd_event_add_signal(
978 sd_event_source **ret,
980 sd_event_signal_handler_t callback,
988 assert_return(e, -EINVAL);
989 assert_return(sig > 0, -EINVAL);
990 assert_return(sig < _NSIG, -EINVAL);
991 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
992 assert_return(!event_pid_changed(e), -ECHILD);
995 callback = signal_exit_callback;
997 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1001 if (!sigismember(&ss, sig))
1004 if (!e->signal_sources) {
1005 e->signal_sources = new0(sd_event_source*, _NSIG);
1006 if (!e->signal_sources)
1008 } else if (e->signal_sources[sig])
1011 previous = need_signal(e, sig);
1013 s = source_new(e, !ret, SOURCE_SIGNAL);
1017 s->signal.sig = sig;
1018 s->signal.callback = callback;
1019 s->userdata = userdata;
1020 s->enabled = SD_EVENT_ON;
1022 e->signal_sources[sig] = s;
1025 assert_se(sigaddset(&e->sigset, sig) == 0);
1027 r = event_update_signal_fd(e);
1034 /* Use the signal name as description for the event source by default */
1035 (void) sd_event_source_set_description(s, signal_to_string(sig));
1043 _public_ int sd_event_add_child(
1045 sd_event_source **ret,
1048 sd_event_child_handler_t callback,
1055 assert_return(e, -EINVAL);
1056 assert_return(pid > 1, -EINVAL);
1057 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1058 assert_return(options != 0, -EINVAL);
1059 assert_return(callback, -EINVAL);
1060 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1061 assert_return(!event_pid_changed(e), -ECHILD);
1063 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1067 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1070 previous = need_signal(e, SIGCHLD);
1072 s = source_new(e, !ret, SOURCE_CHILD);
1077 s->child.options = options;
1078 s->child.callback = callback;
1079 s->userdata = userdata;
1080 s->enabled = SD_EVENT_ONESHOT;
1082 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1088 e->n_enabled_child_sources ++;
1091 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1093 r = event_update_signal_fd(e);
1100 e->need_process_child = true;
1108 _public_ int sd_event_add_defer(
1110 sd_event_source **ret,
1111 sd_event_handler_t callback,
1117 assert_return(e, -EINVAL);
1118 assert_return(callback, -EINVAL);
1119 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1120 assert_return(!event_pid_changed(e), -ECHILD);
1122 s = source_new(e, !ret, SOURCE_DEFER);
1126 s->defer.callback = callback;
1127 s->userdata = userdata;
1128 s->enabled = SD_EVENT_ONESHOT;
1130 r = source_set_pending(s, true);
1142 _public_ int sd_event_add_post(
1144 sd_event_source **ret,
1145 sd_event_handler_t callback,
1151 assert_return(e, -EINVAL);
1152 assert_return(callback, -EINVAL);
1153 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1154 assert_return(!event_pid_changed(e), -ECHILD);
1156 r = set_ensure_allocated(&e->post_sources, NULL);
1160 s = source_new(e, !ret, SOURCE_POST);
1164 s->post.callback = callback;
1165 s->userdata = userdata;
1166 s->enabled = SD_EVENT_ON;
1168 r = set_put(e->post_sources, s);
1180 _public_ int sd_event_add_exit(
1182 sd_event_source **ret,
1183 sd_event_handler_t callback,
1189 assert_return(e, -EINVAL);
1190 assert_return(callback, -EINVAL);
1191 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1192 assert_return(!event_pid_changed(e), -ECHILD);
1195 e->exit = prioq_new(exit_prioq_compare);
1200 s = source_new(e, !ret, SOURCE_EXIT);
1204 s->exit.callback = callback;
1205 s->userdata = userdata;
1206 s->exit.prioq_index = PRIOQ_IDX_NULL;
1207 s->enabled = SD_EVENT_ONESHOT;
1209 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1221 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1222 assert_return(s, NULL);
1224 assert(s->n_ref >= 1);
1230 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1235 assert(s->n_ref >= 1);
1238 if (s->n_ref <= 0) {
1239 /* Here's a special hack: when we are called from a
1240 * dispatch handler we won't free the event source
1241 * immediately, but we will detach the fd from the
1242 * epoll. This way it is safe for the caller to unref
1243 * the event source and immediately close the fd, but
1244 * we still retain a valid event source object after
1247 if (s->dispatching) {
1248 if (s->type == SOURCE_IO)
1249 source_io_unregister(s);
1251 source_disconnect(s);
1259 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1260 assert_return(s, -EINVAL);
1261 assert_return(!event_pid_changed(s->event), -ECHILD);
1263 return free_and_strdup(&s->description, description);
1266 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1267 assert_return(s, -EINVAL);
1268 assert_return(description, -EINVAL);
1269 assert_return(s->description, -ENXIO);
1270 assert_return(!event_pid_changed(s->event), -ECHILD);
1272 *description = s->description;
1276 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1277 assert_return(s, NULL);
1282 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1283 assert_return(s, -EINVAL);
1284 assert_return(s->type != SOURCE_EXIT, -EDOM);
1285 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1286 assert_return(!event_pid_changed(s->event), -ECHILD);
1291 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1292 assert_return(s, -EINVAL);
1293 assert_return(s->type == SOURCE_IO, -EDOM);
1294 assert_return(!event_pid_changed(s->event), -ECHILD);
1299 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1302 assert_return(s, -EINVAL);
1303 assert_return(fd >= 0, -EINVAL);
1304 assert_return(s->type == SOURCE_IO, -EDOM);
1305 assert_return(!event_pid_changed(s->event), -ECHILD);
1310 if (s->enabled == SD_EVENT_OFF) {
1312 s->io.registered = false;
1316 saved_fd = s->io.fd;
1317 assert(s->io.registered);
1320 s->io.registered = false;
1322 r = source_io_register(s, s->enabled, s->io.events);
1324 s->io.fd = saved_fd;
1325 s->io.registered = true;
1329 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1335 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1336 assert_return(s, -EINVAL);
1337 assert_return(events, -EINVAL);
1338 assert_return(s->type == SOURCE_IO, -EDOM);
1339 assert_return(!event_pid_changed(s->event), -ECHILD);
1341 *events = s->io.events;
1345 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1348 assert_return(s, -EINVAL);
1349 assert_return(s->type == SOURCE_IO, -EDOM);
1350 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1351 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1352 assert_return(!event_pid_changed(s->event), -ECHILD);
1354 /* edge-triggered updates are never skipped, so we can reset edges */
1355 if (s->io.events == events && !(events & EPOLLET))
1358 if (s->enabled != SD_EVENT_OFF) {
1359 r = source_io_register(s, s->enabled, events);
1364 s->io.events = events;
1365 source_set_pending(s, false);
1370 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1371 assert_return(s, -EINVAL);
1372 assert_return(revents, -EINVAL);
1373 assert_return(s->type == SOURCE_IO, -EDOM);
1374 assert_return(s->pending, -ENODATA);
1375 assert_return(!event_pid_changed(s->event), -ECHILD);
1377 *revents = s->io.revents;
1381 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1382 assert_return(s, -EINVAL);
1383 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1384 assert_return(!event_pid_changed(s->event), -ECHILD);
1386 return s->signal.sig;
1389 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1390 assert_return(s, -EINVAL);
1391 assert_return(!event_pid_changed(s->event), -ECHILD);
1396 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1397 assert_return(s, -EINVAL);
1398 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1399 assert_return(!event_pid_changed(s->event), -ECHILD);
1401 if (s->priority == priority)
1404 s->priority = priority;
1407 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1410 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1412 if (s->type == SOURCE_EXIT)
1413 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1418 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1419 assert_return(s, -EINVAL);
1420 assert_return(m, -EINVAL);
1421 assert_return(!event_pid_changed(s->event), -ECHILD);
1427 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1430 assert_return(s, -EINVAL);
1431 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1432 assert_return(!event_pid_changed(s->event), -ECHILD);
1434 /* If we are dead anyway, we are fine with turning off
1435 * sources, but everything else needs to fail. */
1436 if (s->event->state == SD_EVENT_FINISHED)
1437 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1439 if (s->enabled == m)
1442 if (m == SD_EVENT_OFF) {
1447 r = source_io_unregister(s);
1454 case SOURCE_TIME_REALTIME:
1455 case SOURCE_TIME_BOOTTIME:
1456 case SOURCE_TIME_MONOTONIC:
1457 case SOURCE_TIME_REALTIME_ALARM:
1458 case SOURCE_TIME_BOOTTIME_ALARM: {
1459 struct clock_data *d;
1462 d = event_get_clock_data(s->event, s->type);
1465 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1466 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1467 d->needs_rearm = true;
1472 assert(need_signal(s->event, s->signal.sig));
1476 if (!need_signal(s->event, s->signal.sig)) {
1477 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1479 (void) event_update_signal_fd(s->event);
1480 /* If disabling failed, we might get a spurious event,
1481 * but otherwise nothing bad should happen. */
1487 assert(need_signal(s->event, SIGCHLD));
1491 assert(s->event->n_enabled_child_sources > 0);
1492 s->event->n_enabled_child_sources--;
1494 if (!need_signal(s->event, SIGCHLD)) {
1495 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1497 (void) event_update_signal_fd(s->event);
1504 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1513 assert_not_reached("Wut? I shouldn't exist.");
1520 r = source_io_register(s, m, s->io.events);
1527 case SOURCE_TIME_REALTIME:
1528 case SOURCE_TIME_BOOTTIME:
1529 case SOURCE_TIME_MONOTONIC:
1530 case SOURCE_TIME_REALTIME_ALARM:
1531 case SOURCE_TIME_BOOTTIME_ALARM: {
1532 struct clock_data *d;
1535 d = event_get_clock_data(s->event, s->type);
1538 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1539 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1540 d->needs_rearm = true;
1545 /* Check status before enabling. */
1546 if (!need_signal(s->event, s->signal.sig)) {
1547 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1549 r = event_update_signal_fd(s->event);
1551 s->enabled = SD_EVENT_OFF;
1560 /* Check status before enabling. */
1561 if (s->enabled == SD_EVENT_OFF) {
1562 if (!need_signal(s->event, SIGCHLD)) {
1563 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1565 r = event_update_signal_fd(s->event);
1567 s->enabled = SD_EVENT_OFF;
1572 s->event->n_enabled_child_sources++;
1580 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1589 assert_not_reached("Wut? I shouldn't exist.");
1594 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1597 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1602 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1603 assert_return(s, -EINVAL);
1604 assert_return(usec, -EINVAL);
1605 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1606 assert_return(!event_pid_changed(s->event), -ECHILD);
1608 *usec = s->time.next;
1612 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1613 struct clock_data *d;
1615 assert_return(s, -EINVAL);
1616 assert_return(usec != (uint64_t) -1, -EINVAL);
1617 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1618 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1619 assert_return(!event_pid_changed(s->event), -ECHILD);
1621 s->time.next = usec;
1623 source_set_pending(s, false);
1625 d = event_get_clock_data(s->event, s->type);
1628 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1629 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1630 d->needs_rearm = true;
1635 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1636 assert_return(s, -EINVAL);
1637 assert_return(usec, -EINVAL);
1638 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1639 assert_return(!event_pid_changed(s->event), -ECHILD);
1641 *usec = s->time.accuracy;
1645 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1646 struct clock_data *d;
1648 assert_return(s, -EINVAL);
1649 assert_return(usec != (uint64_t) -1, -EINVAL);
1650 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1651 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1652 assert_return(!event_pid_changed(s->event), -ECHILD);
1655 usec = DEFAULT_ACCURACY_USEC;
1657 s->time.accuracy = usec;
1659 source_set_pending(s, false);
1661 d = event_get_clock_data(s->event, s->type);
1664 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1665 d->needs_rearm = true;
1670 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1671 assert_return(s, -EINVAL);
1672 assert_return(clock, -EINVAL);
1673 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1674 assert_return(!event_pid_changed(s->event), -ECHILD);
1676 *clock = event_source_type_to_clock(s->type);
1680 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1681 assert_return(s, -EINVAL);
1682 assert_return(pid, -EINVAL);
1683 assert_return(s->type == SOURCE_CHILD, -EDOM);
1684 assert_return(!event_pid_changed(s->event), -ECHILD);
1686 *pid = s->child.pid;
1690 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1693 assert_return(s, -EINVAL);
1694 assert_return(s->type != SOURCE_EXIT, -EDOM);
1695 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1696 assert_return(!event_pid_changed(s->event), -ECHILD);
1698 if (s->prepare == callback)
1701 if (callback && s->prepare) {
1702 s->prepare = callback;
1706 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1710 s->prepare = callback;
1713 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1717 prioq_remove(s->event->prepare, s, &s->prepare_index);
1722 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1723 assert_return(s, NULL);
1728 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1731 assert_return(s, NULL);
1734 s->userdata = userdata;
1739 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1750 initialize_perturb(e);
1753 Find a good time to wake up again between times a and b. We
1754 have two goals here:
1756 a) We want to wake up as seldom as possible, hence prefer
1757 later times over earlier times.
1759 b) But if we have to wake up, then let's make sure to
1760 dispatch as much as possible on the entire system.
1762 We implement this by waking up everywhere at the same time
1763 within any given minute if we can, synchronised via the
1764 perturbation value determined from the boot ID. If we can't,
1765 then we try to find the same spot in every 10s, then 1s and
1766 then 250ms step. Otherwise, we pick the last possible time
1770 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1772 if (_unlikely_(c < USEC_PER_MINUTE))
1775 c -= USEC_PER_MINUTE;
1781 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1783 if (_unlikely_(c < USEC_PER_SEC*10))
1786 c -= USEC_PER_SEC*10;
1792 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1794 if (_unlikely_(c < USEC_PER_SEC))
1803 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1805 if (_unlikely_(c < USEC_PER_MSEC*250))
1808 c -= USEC_PER_MSEC*250;
1817 static int event_arm_timer(
1819 struct clock_data *d) {
1821 struct itimerspec its = {};
1822 sd_event_source *a, *b;
1829 if (!d->needs_rearm)
1832 d->needs_rearm = false;
1834 a = prioq_peek(d->earliest);
1835 if (!a || a->enabled == SD_EVENT_OFF) {
1840 if (d->next == USEC_INFINITY)
1844 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1848 d->next = USEC_INFINITY;
1852 b = prioq_peek(d->latest);
1853 assert_se(b && b->enabled != SD_EVENT_OFF);
1855 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1859 assert_se(d->fd >= 0);
1862 /* We don' want to disarm here, just mean some time looooong ago. */
1863 its.it_value.tv_sec = 0;
1864 its.it_value.tv_nsec = 1;
1866 timespec_store(&its.it_value, t);
1868 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1876 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1879 assert(s->type == SOURCE_IO);
1881 /* If the event source was already pending, we just OR in the
1882 * new revents, otherwise we reset the value. The ORing is
1883 * necessary to handle EPOLLONESHOT events properly where
1884 * readability might happen independently of writability, and
1885 * we need to keep track of both */
1888 s->io.revents |= revents;
1890 s->io.revents = revents;
1892 return source_set_pending(s, true);
1895 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1902 assert_return(events == EPOLLIN, -EIO);
1904 ss = read(fd, &x, sizeof(x));
1906 if (errno == EAGAIN || errno == EINTR)
1912 if (_unlikely_(ss != sizeof(x)))
1916 *next = USEC_INFINITY;
1921 static int process_timer(
1924 struct clock_data *d) {
1933 s = prioq_peek(d->earliest);
1936 s->enabled == SD_EVENT_OFF ||
1940 r = source_set_pending(s, true);
1944 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1945 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1946 d->needs_rearm = true;
1952 static int process_child(sd_event *e) {
1959 e->need_process_child = false;
1962 So, this is ugly. We iteratively invoke waitid() with P_PID
1963 + WNOHANG for each PID we wait for, instead of using
1964 P_ALL. This is because we only want to get child
1965 information of very specific child processes, and not all
1966 of them. We might not have processed the SIGCHLD even of a
1967 previous invocation and we don't want to maintain a
1968 unbounded *per-child* event queue, hence we really don't
1969 want anything flushed out of the kernel's queue that we
1970 don't care about. Since this is O(n) this means that if you
1971 have a lot of processes you probably want to handle SIGCHLD
1974 We do not reap the children here (by using WNOWAIT), this
1975 is only done after the event source is dispatched so that
1976 the callback still sees the process as a zombie.
1979 HASHMAP_FOREACH(s, e->child_sources, i) {
1980 assert(s->type == SOURCE_CHILD);
1985 if (s->enabled == SD_EVENT_OFF)
1988 zero(s->child.siginfo);
1989 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1990 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
1994 if (s->child.siginfo.si_pid != 0) {
1996 s->child.siginfo.si_code == CLD_EXITED ||
1997 s->child.siginfo.si_code == CLD_KILLED ||
1998 s->child.siginfo.si_code == CLD_DUMPED;
2000 if (!zombie && (s->child.options & WEXITED)) {
2001 /* If the child isn't dead then let's
2002 * immediately remove the state change
2003 * from the queue, since there's no
2004 * benefit in leaving it queued */
2006 assert(s->child.options & (WSTOPPED|WCONTINUED));
2007 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2010 r = source_set_pending(s, true);
2019 static int process_signal(sd_event *e, uint32_t events) {
2020 bool read_one = false;
2025 assert_return(events == EPOLLIN, -EIO);
2028 struct signalfd_siginfo si;
2030 sd_event_source *s = NULL;
2032 n = read(e->signal_fd, &si, sizeof(si));
2034 if (errno == EAGAIN || errno == EINTR)
2040 if (_unlikely_(n != sizeof(si)))
2043 assert(si.ssi_signo < _NSIG);
2047 if (si.ssi_signo == SIGCHLD) {
2048 r = process_child(e);
2055 if (e->signal_sources)
2056 s = e->signal_sources[si.ssi_signo];
2061 s->signal.siginfo = si;
2062 r = source_set_pending(s, true);
2068 static int source_dispatch(sd_event_source *s) {
2072 assert(s->pending || s->type == SOURCE_EXIT);
2074 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2075 r = source_set_pending(s, false);
2080 if (s->type != SOURCE_POST) {
2084 /* If we execute a non-post source, let's mark all
2085 * post sources as pending */
2087 SET_FOREACH(z, s->event->post_sources, i) {
2088 if (z->enabled == SD_EVENT_OFF)
2091 r = source_set_pending(z, true);
2097 if (s->enabled == SD_EVENT_ONESHOT) {
2098 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2103 s->dispatching = true;
2108 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2111 case SOURCE_TIME_REALTIME:
2112 case SOURCE_TIME_BOOTTIME:
2113 case SOURCE_TIME_MONOTONIC:
2114 case SOURCE_TIME_REALTIME_ALARM:
2115 case SOURCE_TIME_BOOTTIME_ALARM:
2116 r = s->time.callback(s, s->time.next, s->userdata);
2120 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2123 case SOURCE_CHILD: {
2126 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2127 s->child.siginfo.si_code == CLD_KILLED ||
2128 s->child.siginfo.si_code == CLD_DUMPED;
2130 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2132 /* Now, reap the PID for good. */
2134 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2140 r = s->defer.callback(s, s->userdata);
2144 r = s->post.callback(s, s->userdata);
2148 r = s->exit.callback(s, s->userdata);
2151 case SOURCE_WATCHDOG:
2152 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2153 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2154 assert_not_reached("Wut? I shouldn't exist.");
2157 s->dispatching = false;
2161 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2163 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2169 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2174 static int event_prepare(sd_event *e) {
2182 s = prioq_peek(e->prepare);
2183 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2186 s->prepare_iteration = e->iteration;
2187 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2193 s->dispatching = true;
2194 r = s->prepare(s, s->userdata);
2195 s->dispatching = false;
2199 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2201 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2207 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2213 static int dispatch_exit(sd_event *e) {
2219 p = prioq_peek(e->exit);
2220 if (!p || p->enabled == SD_EVENT_OFF) {
2221 e->state = SD_EVENT_FINISHED;
2227 e->state = SD_EVENT_EXITING;
2229 r = source_dispatch(p);
2231 e->state = SD_EVENT_PASSIVE;
2237 static sd_event_source* event_next_pending(sd_event *e) {
2242 p = prioq_peek(e->pending);
2246 if (p->enabled == SD_EVENT_OFF)
2252 static int arm_watchdog(sd_event *e) {
2253 struct itimerspec its = {};
2258 assert(e->watchdog_fd >= 0);
2260 t = sleep_between(e,
2261 e->watchdog_last + (e->watchdog_period / 2),
2262 e->watchdog_last + (e->watchdog_period * 3 / 4));
2264 timespec_store(&its.it_value, t);
2266 /* Make sure we never set the watchdog to 0, which tells the
2267 * kernel to disable it. */
2268 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2269 its.it_value.tv_nsec = 1;
2271 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2278 static int process_watchdog(sd_event *e) {
2284 /* Don't notify watchdog too often */
2285 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2288 sd_notify(false, "WATCHDOG=1");
2289 e->watchdog_last = e->timestamp.monotonic;
2291 return arm_watchdog(e);
2294 _public_ int sd_event_prepare(sd_event *e) {
2297 assert_return(e, -EINVAL);
2298 assert_return(!event_pid_changed(e), -ECHILD);
2299 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2300 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2302 if (e->exit_requested)
2307 r = event_prepare(e);
2311 r = event_arm_timer(e, &e->realtime);
2315 r = event_arm_timer(e, &e->boottime);
2319 r = event_arm_timer(e, &e->monotonic);
2323 r = event_arm_timer(e, &e->realtime_alarm);
2327 r = event_arm_timer(e, &e->boottime_alarm);
2331 if (event_next_pending(e) || e->need_process_child)
2334 e->state = SD_EVENT_PREPARED;
2339 e->state = SD_EVENT_PREPARED;
2340 r = sd_event_wait(e, 0);
2342 e->state = SD_EVENT_PREPARED;
2347 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2348 struct epoll_event *ev_queue;
2349 unsigned ev_queue_max;
2352 assert_return(e, -EINVAL);
2353 assert_return(!event_pid_changed(e), -ECHILD);
2354 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2355 assert_return(e->state == SD_EVENT_PREPARED, -EBUSY);
2357 if (e->exit_requested) {
2358 e->state = SD_EVENT_PENDING;
2362 ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2363 ev_queue = newa(struct epoll_event, ev_queue_max);
2365 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2366 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2368 if (errno == EINTR) {
2369 e->state = SD_EVENT_PENDING;
2378 dual_timestamp_get(&e->timestamp);
2379 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2381 for (i = 0; i < m; i++) {
2383 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2384 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2385 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2386 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2387 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2388 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2389 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2390 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2391 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2392 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2393 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2394 r = process_signal(e, ev_queue[i].events);
2395 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2396 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2398 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2404 r = process_watchdog(e);
2408 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2412 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2416 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2420 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2424 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2428 if (e->need_process_child) {
2429 r = process_child(e);
2434 if (event_next_pending(e)) {
2435 e->state = SD_EVENT_PENDING;
2443 e->state = SD_EVENT_PASSIVE;
2448 _public_ int sd_event_dispatch(sd_event *e) {
2452 assert_return(e, -EINVAL);
2453 assert_return(!event_pid_changed(e), -ECHILD);
2454 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2455 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2457 if (e->exit_requested)
2458 return dispatch_exit(e);
2460 p = event_next_pending(e);
2464 e->state = SD_EVENT_RUNNING;
2465 r = source_dispatch(p);
2466 e->state = SD_EVENT_PASSIVE;
2473 e->state = SD_EVENT_PASSIVE;
2478 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2481 assert_return(e, -EINVAL);
2482 assert_return(!event_pid_changed(e), -ECHILD);
2483 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2484 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2486 r = sd_event_prepare(e);
2488 return sd_event_dispatch(e);
2492 r = sd_event_wait(e, timeout);
2494 return sd_event_dispatch(e);
2499 _public_ int sd_event_loop(sd_event *e) {
2502 assert_return(e, -EINVAL);
2503 assert_return(!event_pid_changed(e), -ECHILD);
2504 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2508 while (e->state != SD_EVENT_FINISHED) {
2509 r = sd_event_run(e, (uint64_t) -1);
2521 _public_ int sd_event_get_fd(sd_event *e) {
2523 assert_return(e, -EINVAL);
2524 assert_return(!event_pid_changed(e), -ECHILD);
2529 _public_ int sd_event_get_state(sd_event *e) {
2530 assert_return(e, -EINVAL);
2531 assert_return(!event_pid_changed(e), -ECHILD);
2536 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2537 assert_return(e, -EINVAL);
2538 assert_return(code, -EINVAL);
2539 assert_return(!event_pid_changed(e), -ECHILD);
2541 if (!e->exit_requested)
2544 *code = e->exit_code;
2548 _public_ int sd_event_exit(sd_event *e, int code) {
2549 assert_return(e, -EINVAL);
2550 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2551 assert_return(!event_pid_changed(e), -ECHILD);
2553 e->exit_requested = true;
2554 e->exit_code = code;
2559 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2560 assert_return(e, -EINVAL);
2561 assert_return(usec, -EINVAL);
2562 assert_return(!event_pid_changed(e), -ECHILD);
2564 /* If we haven't run yet, just get the actual time */
2565 if (!dual_timestamp_is_set(&e->timestamp))
2570 case CLOCK_REALTIME:
2571 case CLOCK_REALTIME_ALARM:
2572 *usec = e->timestamp.realtime;
2575 case CLOCK_MONOTONIC:
2576 *usec = e->timestamp.monotonic;
2579 case CLOCK_BOOTTIME:
2580 case CLOCK_BOOTTIME_ALARM:
2581 *usec = e->timestamp_boottime;
2588 _public_ int sd_event_default(sd_event **ret) {
2590 static thread_local sd_event *default_event = NULL;
2595 return !!default_event;
2597 if (default_event) {
2598 *ret = sd_event_ref(default_event);
2602 r = sd_event_new(&e);
2606 e->default_event_ptr = &default_event;
2614 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2615 assert_return(e, -EINVAL);
2616 assert_return(tid, -EINVAL);
2617 assert_return(!event_pid_changed(e), -ECHILD);
2627 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2630 assert_return(e, -EINVAL);
2631 assert_return(!event_pid_changed(e), -ECHILD);
2633 if (e->watchdog == !!b)
2637 struct epoll_event ev = {};
2639 r = sd_watchdog_enabled(false, &e->watchdog_period);
2643 /* Issue first ping immediately */
2644 sd_notify(false, "WATCHDOG=1");
2645 e->watchdog_last = now(CLOCK_MONOTONIC);
2647 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2648 if (e->watchdog_fd < 0)
2651 r = arm_watchdog(e);
2655 ev.events = EPOLLIN;
2656 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2658 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2665 if (e->watchdog_fd >= 0) {
2666 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2667 e->watchdog_fd = safe_close(e->watchdog_fd);
2675 e->watchdog_fd = safe_close(e->watchdog_fd);
2679 _public_ int sd_event_get_watchdog(sd_event *e) {
2680 assert_return(e, -EINVAL);
2681 assert_return(!event_pid_changed(e), -ECHILD);