1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
27 #include "sd-daemon.h"
32 #include "time-util.h"
39 #define EPOLL_QUEUE_MAX 512U
40 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
42 typedef enum EventSourceType {
46 SOURCE_TIME_MONOTONIC,
47 SOURCE_TIME_REALTIME_ALARM,
48 SOURCE_TIME_BOOTTIME_ALARM,
55 _SOURCE_EVENT_SOURCE_TYPE_MAX,
56 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
59 #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)
61 struct sd_event_source {
66 sd_event_handler_t prepare;
70 EventSourceType type:5;
77 unsigned pending_index;
78 unsigned prepare_index;
79 unsigned pending_iteration;
80 unsigned prepare_iteration;
82 LIST_FIELDS(sd_event_source, sources);
86 sd_event_io_handler_t callback;
93 sd_event_time_handler_t callback;
94 usec_t next, accuracy;
95 unsigned earliest_index;
96 unsigned latest_index;
99 sd_event_signal_handler_t callback;
100 struct signalfd_siginfo siginfo;
104 sd_event_child_handler_t callback;
110 sd_event_handler_t callback;
113 sd_event_handler_t callback;
116 sd_event_handler_t callback;
117 unsigned prioq_index;
125 /* For all clocks we maintain two priority queues each, one
126 * ordered for the earliest times the events may be
127 * dispatched, and one ordered by the latest times they must
128 * have been dispatched. The range between the top entries in
129 * the two prioqs is the time window we can freely schedule
149 /* timerfd_create() only supports these five clocks so far. We
150 * can add support for more clocks when the kernel learns to
151 * deal with them, too. */
152 struct clock_data realtime;
153 struct clock_data boottime;
154 struct clock_data monotonic;
155 struct clock_data realtime_alarm;
156 struct clock_data boottime_alarm;
161 sd_event_source **signal_sources;
163 Hashmap *child_sources;
164 unsigned n_enabled_child_sources;
173 dual_timestamp timestamp;
174 usec_t timestamp_boottime;
177 bool exit_requested:1;
178 bool need_process_child:1;
184 sd_event **default_event_ptr;
186 usec_t watchdog_last, watchdog_period;
190 LIST_HEAD(sd_event_source, sources);
193 static void source_disconnect(sd_event_source *s);
195 static int pending_prioq_compare(const void *a, const void *b) {
196 const sd_event_source *x = a, *y = b;
201 /* Enabled ones first */
202 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
204 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
207 /* Lower priority values first */
208 if (x->priority < y->priority)
210 if (x->priority > y->priority)
213 /* Older entries first */
214 if (x->pending_iteration < y->pending_iteration)
216 if (x->pending_iteration > y->pending_iteration)
219 /* Stability for the rest */
228 static int prepare_prioq_compare(const void *a, const void *b) {
229 const sd_event_source *x = a, *y = b;
234 /* Move most recently prepared ones last, so that we can stop
235 * preparing as soon as we hit one that has already been
236 * prepared in the current iteration */
237 if (x->prepare_iteration < y->prepare_iteration)
239 if (x->prepare_iteration > y->prepare_iteration)
242 /* Enabled ones first */
243 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
245 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
248 /* Lower priority values first */
249 if (x->priority < y->priority)
251 if (x->priority > y->priority)
254 /* Stability for the rest */
263 static int earliest_time_prioq_compare(const void *a, const void *b) {
264 const sd_event_source *x = a, *y = b;
266 assert(EVENT_SOURCE_IS_TIME(x->type));
267 assert(x->type == y->type);
269 /* Enabled ones first */
270 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
272 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
275 /* Move the pending ones to the end */
276 if (!x->pending && y->pending)
278 if (x->pending && !y->pending)
282 if (x->time.next < y->time.next)
284 if (x->time.next > y->time.next)
287 /* Stability for the rest */
296 static int latest_time_prioq_compare(const void *a, const void *b) {
297 const sd_event_source *x = a, *y = b;
299 assert(EVENT_SOURCE_IS_TIME(x->type));
300 assert(x->type == y->type);
302 /* Enabled ones first */
303 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
305 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
308 /* Move the pending ones to the end */
309 if (!x->pending && y->pending)
311 if (x->pending && !y->pending)
315 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
317 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
320 /* Stability for the rest */
329 static int exit_prioq_compare(const void *a, const void *b) {
330 const sd_event_source *x = a, *y = b;
332 assert(x->type == SOURCE_EXIT);
333 assert(y->type == SOURCE_EXIT);
335 /* Enabled ones first */
336 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
338 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
341 /* Lower priority values first */
342 if (x->priority < y->priority)
344 if (x->priority > y->priority)
347 /* Stability for the rest */
356 static void free_clock_data(struct clock_data *d) {
360 prioq_free(d->earliest);
361 prioq_free(d->latest);
364 static void event_free(sd_event *e) {
369 while ((s = e->sources)) {
371 source_disconnect(s);
372 sd_event_source_unref(s);
375 assert(e->n_sources == 0);
377 if (e->default_event_ptr)
378 *(e->default_event_ptr) = NULL;
380 safe_close(e->epoll_fd);
381 safe_close(e->signal_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);
396 hashmap_free(e->child_sources);
397 set_free(e->post_sources);
401 _public_ int sd_event_new(sd_event** ret) {
405 assert_return(ret, -EINVAL);
407 e = new0(sd_event, 1);
412 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;
413 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
414 e->original_pid = getpid();
415 e->perturb = USEC_INFINITY;
417 assert_se(sigemptyset(&e->sigset) == 0);
419 e->pending = prioq_new(pending_prioq_compare);
425 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
426 if (e->epoll_fd < 0) {
439 _public_ sd_event* sd_event_ref(sd_event *e) {
440 assert_return(e, NULL);
442 assert(e->n_ref >= 1);
448 _public_ sd_event* sd_event_unref(sd_event *e) {
453 assert(e->n_ref >= 1);
462 static bool event_pid_changed(sd_event *e) {
465 /* We don't support people creating am event loop and keeping
466 * it around over a fork(). Let's complain. */
468 return e->original_pid != getpid();
471 static int source_io_unregister(sd_event_source *s) {
475 assert(s->type == SOURCE_IO);
477 if (!s->io.registered)
480 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
484 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);
514 s->io.registered = true;
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;
543 static EventSourceType clock_to_event_source_type(clockid_t clock) {
548 return SOURCE_TIME_REALTIME;
551 return SOURCE_TIME_BOOTTIME;
553 case CLOCK_MONOTONIC:
554 return SOURCE_TIME_MONOTONIC;
556 case CLOCK_REALTIME_ALARM:
557 return SOURCE_TIME_REALTIME_ALARM;
559 case CLOCK_BOOTTIME_ALARM:
560 return SOURCE_TIME_BOOTTIME_ALARM;
563 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
567 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
572 case SOURCE_TIME_REALTIME:
575 case SOURCE_TIME_BOOTTIME:
578 case SOURCE_TIME_MONOTONIC:
579 return &e->monotonic;
581 case SOURCE_TIME_REALTIME_ALARM:
582 return &e->realtime_alarm;
584 case SOURCE_TIME_BOOTTIME_ALARM:
585 return &e->boottime_alarm;
592 static bool need_signal(sd_event *e, int signal) {
593 return (e->signal_sources && e->signal_sources[signal] &&
594 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
596 (signal == SIGCHLD &&
597 e->n_enabled_child_sources > 0);
600 static int event_update_signal_fd(sd_event *e) {
601 struct epoll_event ev = {};
607 add_to_epoll = e->signal_fd < 0;
609 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
619 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
621 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
623 e->signal_fd = safe_close(e->signal_fd);
630 static void source_disconnect(sd_event_source *s) {
638 assert(s->event->n_sources > 0);
644 source_io_unregister(s);
648 case SOURCE_TIME_REALTIME:
649 case SOURCE_TIME_BOOTTIME:
650 case SOURCE_TIME_MONOTONIC:
651 case SOURCE_TIME_REALTIME_ALARM:
652 case SOURCE_TIME_BOOTTIME_ALARM: {
653 struct clock_data *d;
655 d = event_get_clock_data(s->event, s->type);
658 prioq_remove(d->earliest, s, &s->time.earliest_index);
659 prioq_remove(d->latest, s, &s->time.latest_index);
660 d->needs_rearm = true;
665 if (s->signal.sig > 0) {
666 if (s->event->signal_sources)
667 s->event->signal_sources[s->signal.sig] = NULL;
669 /* If the signal was on and now it is off... */
670 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
671 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
673 (void) event_update_signal_fd(s->event);
674 /* If disabling failed, we might get a spurious event,
675 * but otherwise nothing bad should happen. */
682 if (s->child.pid > 0) {
683 if (s->enabled != SD_EVENT_OFF) {
684 assert(s->event->n_enabled_child_sources > 0);
685 s->event->n_enabled_child_sources--;
687 /* We know the signal was on, if it is off now... */
688 if (!need_signal(s->event, SIGCHLD)) {
689 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
691 (void) event_update_signal_fd(s->event);
692 /* If disabling failed, we might get a spurious event,
693 * but otherwise nothing bad should happen. */
697 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
707 set_remove(s->event->post_sources, s);
711 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
715 assert_not_reached("Wut? I shouldn't exist.");
719 prioq_remove(s->event->pending, s, &s->pending_index);
722 prioq_remove(s->event->prepare, s, &s->prepare_index);
726 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
728 LIST_REMOVE(sources, event->sources, s);
732 sd_event_unref(event);
735 static void source_free(sd_event_source *s) {
738 source_disconnect(s);
739 free(s->description);
743 static int source_set_pending(sd_event_source *s, bool b) {
747 assert(s->type != SOURCE_EXIT);
755 s->pending_iteration = s->event->iteration;
757 r = prioq_put(s->event->pending, s, &s->pending_index);
763 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
765 if (EVENT_SOURCE_IS_TIME(s->type)) {
766 struct clock_data *d;
768 d = event_get_clock_data(s->event, s->type);
771 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
772 prioq_reshuffle(d->latest, s, &s->time.latest_index);
773 d->needs_rearm = true;
779 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
784 s = new0(sd_event_source, 1);
790 s->floating = floating;
792 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
797 LIST_PREPEND(sources, e->sources, s);
803 _public_ int sd_event_add_io(
805 sd_event_source **ret,
808 sd_event_io_handler_t callback,
814 assert_return(e, -EINVAL);
815 assert_return(fd >= 0, -EINVAL);
816 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
817 assert_return(callback, -EINVAL);
818 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
819 assert_return(!event_pid_changed(e), -ECHILD);
821 s = source_new(e, !ret, SOURCE_IO);
826 s->io.events = events;
827 s->io.callback = callback;
828 s->userdata = userdata;
829 s->enabled = SD_EVENT_ON;
831 r = source_io_register(s, s->enabled, events);
843 static void initialize_perturb(sd_event *e) {
844 sd_id128_t bootid = {};
846 /* When we sleep for longer, we try to realign the wakeup to
847 the same time wihtin each minute/second/250ms, so that
848 events all across the system can be coalesced into a single
849 CPU wakeup. However, let's take some system-specific
850 randomness for this value, so that in a network of systems
851 with synced clocks timer events are distributed a
852 bit. Here, we calculate a perturbation usec offset from the
855 if (_likely_(e->perturb != USEC_INFINITY))
858 if (sd_id128_get_boot(&bootid) >= 0)
859 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
862 static int event_setup_timer_fd(
864 struct clock_data *d,
867 struct epoll_event ev = {};
873 if (_likely_(d->fd >= 0))
876 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
881 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
883 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
893 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
896 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
899 _public_ int sd_event_add_time(
901 sd_event_source **ret,
905 sd_event_time_handler_t callback,
908 EventSourceType type;
910 struct clock_data *d;
913 assert_return(e, -EINVAL);
914 assert_return(usec != (uint64_t) -1, -EINVAL);
915 assert_return(accuracy != (uint64_t) -1, -EINVAL);
916 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
917 assert_return(!event_pid_changed(e), -ECHILD);
920 callback = time_exit_callback;
922 type = clock_to_event_source_type(clock);
923 assert_return(type >= 0, -ENOTSUP);
925 d = event_get_clock_data(e, type);
929 d->earliest = prioq_new(earliest_time_prioq_compare);
935 d->latest = prioq_new(latest_time_prioq_compare);
941 r = event_setup_timer_fd(e, d, clock);
946 s = source_new(e, !ret, type);
951 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
952 s->time.callback = callback;
953 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
954 s->userdata = userdata;
955 s->enabled = SD_EVENT_ONESHOT;
957 d->needs_rearm = true;
959 r = prioq_put(d->earliest, s, &s->time.earliest_index);
963 r = prioq_put(d->latest, s, &s->time.latest_index);
977 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
980 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
983 _public_ int sd_event_add_signal(
985 sd_event_source **ret,
987 sd_event_signal_handler_t callback,
995 assert_return(e, -EINVAL);
996 assert_return(sig > 0, -EINVAL);
997 assert_return(sig < _NSIG, -EINVAL);
998 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
999 assert_return(!event_pid_changed(e), -ECHILD);
1002 callback = signal_exit_callback;
1004 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1008 if (!sigismember(&ss, sig))
1011 if (!e->signal_sources) {
1012 e->signal_sources = new0(sd_event_source*, _NSIG);
1013 if (!e->signal_sources)
1015 } else if (e->signal_sources[sig])
1018 previous = need_signal(e, sig);
1020 s = source_new(e, !ret, SOURCE_SIGNAL);
1024 s->signal.sig = sig;
1025 s->signal.callback = callback;
1026 s->userdata = userdata;
1027 s->enabled = SD_EVENT_ON;
1029 e->signal_sources[sig] = s;
1032 assert_se(sigaddset(&e->sigset, sig) == 0);
1034 r = event_update_signal_fd(e);
1041 /* Use the signal name as description for the event source by default */
1042 (void) sd_event_source_set_description(s, signal_to_string(sig));
1050 _public_ int sd_event_add_child(
1052 sd_event_source **ret,
1055 sd_event_child_handler_t callback,
1062 assert_return(e, -EINVAL);
1063 assert_return(pid > 1, -EINVAL);
1064 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1065 assert_return(options != 0, -EINVAL);
1066 assert_return(callback, -EINVAL);
1067 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1068 assert_return(!event_pid_changed(e), -ECHILD);
1070 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1074 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1077 previous = need_signal(e, SIGCHLD);
1079 s = source_new(e, !ret, SOURCE_CHILD);
1084 s->child.options = options;
1085 s->child.callback = callback;
1086 s->userdata = userdata;
1087 s->enabled = SD_EVENT_ONESHOT;
1089 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1095 e->n_enabled_child_sources ++;
1098 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1100 r = event_update_signal_fd(e);
1107 e->need_process_child = true;
1115 _public_ int sd_event_add_defer(
1117 sd_event_source **ret,
1118 sd_event_handler_t callback,
1124 assert_return(e, -EINVAL);
1125 assert_return(callback, -EINVAL);
1126 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1127 assert_return(!event_pid_changed(e), -ECHILD);
1129 s = source_new(e, !ret, SOURCE_DEFER);
1133 s->defer.callback = callback;
1134 s->userdata = userdata;
1135 s->enabled = SD_EVENT_ONESHOT;
1137 r = source_set_pending(s, true);
1149 _public_ int sd_event_add_post(
1151 sd_event_source **ret,
1152 sd_event_handler_t callback,
1158 assert_return(e, -EINVAL);
1159 assert_return(callback, -EINVAL);
1160 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1161 assert_return(!event_pid_changed(e), -ECHILD);
1163 r = set_ensure_allocated(&e->post_sources, NULL);
1167 s = source_new(e, !ret, SOURCE_POST);
1171 s->post.callback = callback;
1172 s->userdata = userdata;
1173 s->enabled = SD_EVENT_ON;
1175 r = set_put(e->post_sources, s);
1187 _public_ int sd_event_add_exit(
1189 sd_event_source **ret,
1190 sd_event_handler_t callback,
1196 assert_return(e, -EINVAL);
1197 assert_return(callback, -EINVAL);
1198 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1199 assert_return(!event_pid_changed(e), -ECHILD);
1202 e->exit = prioq_new(exit_prioq_compare);
1207 s = source_new(e, !ret, SOURCE_EXIT);
1211 s->exit.callback = callback;
1212 s->userdata = userdata;
1213 s->exit.prioq_index = PRIOQ_IDX_NULL;
1214 s->enabled = SD_EVENT_ONESHOT;
1216 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1228 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1229 assert_return(s, NULL);
1231 assert(s->n_ref >= 1);
1237 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1242 assert(s->n_ref >= 1);
1245 if (s->n_ref <= 0) {
1246 /* Here's a special hack: when we are called from a
1247 * dispatch handler we won't free the event source
1248 * immediately, but we will detach the fd from the
1249 * epoll. This way it is safe for the caller to unref
1250 * the event source and immediately close the fd, but
1251 * we still retain a valid event source object after
1254 if (s->dispatching) {
1255 if (s->type == SOURCE_IO)
1256 source_io_unregister(s);
1258 source_disconnect(s);
1266 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1267 assert_return(s, -EINVAL);
1268 assert_return(!event_pid_changed(s->event), -ECHILD);
1270 return free_and_strdup(&s->description, description);
1273 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1274 assert_return(s, -EINVAL);
1275 assert_return(description, -EINVAL);
1276 assert_return(s->description, -ENXIO);
1277 assert_return(!event_pid_changed(s->event), -ECHILD);
1279 *description = s->description;
1283 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1284 assert_return(s, NULL);
1289 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1290 assert_return(s, -EINVAL);
1291 assert_return(s->type != SOURCE_EXIT, -EDOM);
1292 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1293 assert_return(!event_pid_changed(s->event), -ECHILD);
1298 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1299 assert_return(s, -EINVAL);
1300 assert_return(s->type == SOURCE_IO, -EDOM);
1301 assert_return(!event_pid_changed(s->event), -ECHILD);
1306 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1309 assert_return(s, -EINVAL);
1310 assert_return(fd >= 0, -EINVAL);
1311 assert_return(s->type == SOURCE_IO, -EDOM);
1312 assert_return(!event_pid_changed(s->event), -ECHILD);
1317 if (s->enabled == SD_EVENT_OFF) {
1319 s->io.registered = false;
1323 saved_fd = s->io.fd;
1324 assert(s->io.registered);
1327 s->io.registered = false;
1329 r = source_io_register(s, s->enabled, s->io.events);
1331 s->io.fd = saved_fd;
1332 s->io.registered = true;
1336 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1342 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1343 assert_return(s, -EINVAL);
1344 assert_return(events, -EINVAL);
1345 assert_return(s->type == SOURCE_IO, -EDOM);
1346 assert_return(!event_pid_changed(s->event), -ECHILD);
1348 *events = s->io.events;
1352 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1355 assert_return(s, -EINVAL);
1356 assert_return(s->type == SOURCE_IO, -EDOM);
1357 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1358 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1359 assert_return(!event_pid_changed(s->event), -ECHILD);
1361 /* edge-triggered updates are never skipped, so we can reset edges */
1362 if (s->io.events == events && !(events & EPOLLET))
1365 if (s->enabled != SD_EVENT_OFF) {
1366 r = source_io_register(s, s->enabled, events);
1371 s->io.events = events;
1372 source_set_pending(s, false);
1377 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1378 assert_return(s, -EINVAL);
1379 assert_return(revents, -EINVAL);
1380 assert_return(s->type == SOURCE_IO, -EDOM);
1381 assert_return(s->pending, -ENODATA);
1382 assert_return(!event_pid_changed(s->event), -ECHILD);
1384 *revents = s->io.revents;
1388 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1389 assert_return(s, -EINVAL);
1390 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1391 assert_return(!event_pid_changed(s->event), -ECHILD);
1393 return s->signal.sig;
1396 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1397 assert_return(s, -EINVAL);
1398 assert_return(!event_pid_changed(s->event), -ECHILD);
1403 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1404 assert_return(s, -EINVAL);
1405 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1406 assert_return(!event_pid_changed(s->event), -ECHILD);
1408 if (s->priority == priority)
1411 s->priority = priority;
1414 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1417 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1419 if (s->type == SOURCE_EXIT)
1420 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1425 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1426 assert_return(s, -EINVAL);
1427 assert_return(m, -EINVAL);
1428 assert_return(!event_pid_changed(s->event), -ECHILD);
1434 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1437 assert_return(s, -EINVAL);
1438 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1439 assert_return(!event_pid_changed(s->event), -ECHILD);
1441 /* If we are dead anyway, we are fine with turning off
1442 * sources, but everything else needs to fail. */
1443 if (s->event->state == SD_EVENT_FINISHED)
1444 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1446 if (s->enabled == m)
1449 if (m == SD_EVENT_OFF) {
1454 r = source_io_unregister(s);
1461 case SOURCE_TIME_REALTIME:
1462 case SOURCE_TIME_BOOTTIME:
1463 case SOURCE_TIME_MONOTONIC:
1464 case SOURCE_TIME_REALTIME_ALARM:
1465 case SOURCE_TIME_BOOTTIME_ALARM: {
1466 struct clock_data *d;
1469 d = event_get_clock_data(s->event, s->type);
1472 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1473 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1474 d->needs_rearm = true;
1479 assert(need_signal(s->event, s->signal.sig));
1483 if (!need_signal(s->event, s->signal.sig)) {
1484 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1486 (void) event_update_signal_fd(s->event);
1487 /* If disabling failed, we might get a spurious event,
1488 * but otherwise nothing bad should happen. */
1494 assert(need_signal(s->event, SIGCHLD));
1498 assert(s->event->n_enabled_child_sources > 0);
1499 s->event->n_enabled_child_sources--;
1501 if (!need_signal(s->event, SIGCHLD)) {
1502 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1504 (void) event_update_signal_fd(s->event);
1511 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1520 assert_not_reached("Wut? I shouldn't exist.");
1527 r = source_io_register(s, m, s->io.events);
1534 case SOURCE_TIME_REALTIME:
1535 case SOURCE_TIME_BOOTTIME:
1536 case SOURCE_TIME_MONOTONIC:
1537 case SOURCE_TIME_REALTIME_ALARM:
1538 case SOURCE_TIME_BOOTTIME_ALARM: {
1539 struct clock_data *d;
1542 d = event_get_clock_data(s->event, s->type);
1545 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1546 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1547 d->needs_rearm = true;
1552 /* Check status before enabling. */
1553 if (!need_signal(s->event, s->signal.sig)) {
1554 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1556 r = event_update_signal_fd(s->event);
1558 s->enabled = SD_EVENT_OFF;
1567 /* Check status before enabling. */
1568 if (s->enabled == SD_EVENT_OFF) {
1569 if (!need_signal(s->event, SIGCHLD)) {
1570 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1572 r = event_update_signal_fd(s->event);
1574 s->enabled = SD_EVENT_OFF;
1579 s->event->n_enabled_child_sources++;
1587 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1596 assert_not_reached("Wut? I shouldn't exist.");
1601 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1604 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1609 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1610 assert_return(s, -EINVAL);
1611 assert_return(usec, -EINVAL);
1612 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1613 assert_return(!event_pid_changed(s->event), -ECHILD);
1615 *usec = s->time.next;
1619 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1620 struct clock_data *d;
1622 assert_return(s, -EINVAL);
1623 assert_return(usec != (uint64_t) -1, -EINVAL);
1624 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1625 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1626 assert_return(!event_pid_changed(s->event), -ECHILD);
1628 s->time.next = usec;
1630 source_set_pending(s, false);
1632 d = event_get_clock_data(s->event, s->type);
1635 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1636 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1637 d->needs_rearm = true;
1642 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1643 assert_return(s, -EINVAL);
1644 assert_return(usec, -EINVAL);
1645 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1646 assert_return(!event_pid_changed(s->event), -ECHILD);
1648 *usec = s->time.accuracy;
1652 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1653 struct clock_data *d;
1655 assert_return(s, -EINVAL);
1656 assert_return(usec != (uint64_t) -1, -EINVAL);
1657 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1658 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1659 assert_return(!event_pid_changed(s->event), -ECHILD);
1662 usec = DEFAULT_ACCURACY_USEC;
1664 s->time.accuracy = usec;
1666 source_set_pending(s, false);
1668 d = event_get_clock_data(s->event, s->type);
1671 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1672 d->needs_rearm = true;
1677 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1678 assert_return(s, -EINVAL);
1679 assert_return(clock, -EINVAL);
1680 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1681 assert_return(!event_pid_changed(s->event), -ECHILD);
1683 *clock = event_source_type_to_clock(s->type);
1687 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1688 assert_return(s, -EINVAL);
1689 assert_return(pid, -EINVAL);
1690 assert_return(s->type == SOURCE_CHILD, -EDOM);
1691 assert_return(!event_pid_changed(s->event), -ECHILD);
1693 *pid = s->child.pid;
1697 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1700 assert_return(s, -EINVAL);
1701 assert_return(s->type != SOURCE_EXIT, -EDOM);
1702 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1703 assert_return(!event_pid_changed(s->event), -ECHILD);
1705 if (s->prepare == callback)
1708 if (callback && s->prepare) {
1709 s->prepare = callback;
1713 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1717 s->prepare = callback;
1720 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1724 prioq_remove(s->event->prepare, s, &s->prepare_index);
1729 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1730 assert_return(s, NULL);
1735 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1738 assert_return(s, NULL);
1741 s->userdata = userdata;
1746 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1757 initialize_perturb(e);
1760 Find a good time to wake up again between times a and b. We
1761 have two goals here:
1763 a) We want to wake up as seldom as possible, hence prefer
1764 later times over earlier times.
1766 b) But if we have to wake up, then let's make sure to
1767 dispatch as much as possible on the entire system.
1769 We implement this by waking up everywhere at the same time
1770 within any given minute if we can, synchronised via the
1771 perturbation value determined from the boot ID. If we can't,
1772 then we try to find the same spot in every 10s, then 1s and
1773 then 250ms step. Otherwise, we pick the last possible time
1777 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1779 if (_unlikely_(c < USEC_PER_MINUTE))
1782 c -= USEC_PER_MINUTE;
1788 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1790 if (_unlikely_(c < USEC_PER_SEC*10))
1793 c -= USEC_PER_SEC*10;
1799 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1801 if (_unlikely_(c < USEC_PER_SEC))
1810 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1812 if (_unlikely_(c < USEC_PER_MSEC*250))
1815 c -= USEC_PER_MSEC*250;
1824 static int event_arm_timer(
1826 struct clock_data *d) {
1828 struct itimerspec its = {};
1829 sd_event_source *a, *b;
1836 if (!d->needs_rearm)
1839 d->needs_rearm = false;
1841 a = prioq_peek(d->earliest);
1842 if (!a || a->enabled == SD_EVENT_OFF) {
1847 if (d->next == USEC_INFINITY)
1851 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1855 d->next = USEC_INFINITY;
1859 b = prioq_peek(d->latest);
1860 assert_se(b && b->enabled != SD_EVENT_OFF);
1862 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1866 assert_se(d->fd >= 0);
1869 /* We don' want to disarm here, just mean some time looooong ago. */
1870 its.it_value.tv_sec = 0;
1871 its.it_value.tv_nsec = 1;
1873 timespec_store(&its.it_value, t);
1875 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1883 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1886 assert(s->type == SOURCE_IO);
1888 /* If the event source was already pending, we just OR in the
1889 * new revents, otherwise we reset the value. The ORing is
1890 * necessary to handle EPOLLONESHOT events properly where
1891 * readability might happen independently of writability, and
1892 * we need to keep track of both */
1895 s->io.revents |= revents;
1897 s->io.revents = revents;
1899 return source_set_pending(s, true);
1902 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1909 assert_return(events == EPOLLIN, -EIO);
1911 ss = read(fd, &x, sizeof(x));
1913 if (errno == EAGAIN || errno == EINTR)
1919 if (_unlikely_(ss != sizeof(x)))
1923 *next = USEC_INFINITY;
1928 static int process_timer(
1931 struct clock_data *d) {
1940 s = prioq_peek(d->earliest);
1943 s->enabled == SD_EVENT_OFF ||
1947 r = source_set_pending(s, true);
1951 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1952 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1953 d->needs_rearm = true;
1959 static int process_child(sd_event *e) {
1966 e->need_process_child = false;
1969 So, this is ugly. We iteratively invoke waitid() with P_PID
1970 + WNOHANG for each PID we wait for, instead of using
1971 P_ALL. This is because we only want to get child
1972 information of very specific child processes, and not all
1973 of them. We might not have processed the SIGCHLD even of a
1974 previous invocation and we don't want to maintain a
1975 unbounded *per-child* event queue, hence we really don't
1976 want anything flushed out of the kernel's queue that we
1977 don't care about. Since this is O(n) this means that if you
1978 have a lot of processes you probably want to handle SIGCHLD
1981 We do not reap the children here (by using WNOWAIT), this
1982 is only done after the event source is dispatched so that
1983 the callback still sees the process as a zombie.
1986 HASHMAP_FOREACH(s, e->child_sources, i) {
1987 assert(s->type == SOURCE_CHILD);
1992 if (s->enabled == SD_EVENT_OFF)
1995 zero(s->child.siginfo);
1996 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1997 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2001 if (s->child.siginfo.si_pid != 0) {
2003 s->child.siginfo.si_code == CLD_EXITED ||
2004 s->child.siginfo.si_code == CLD_KILLED ||
2005 s->child.siginfo.si_code == CLD_DUMPED;
2007 if (!zombie && (s->child.options & WEXITED)) {
2008 /* If the child isn't dead then let's
2009 * immediately remove the state change
2010 * from the queue, since there's no
2011 * benefit in leaving it queued */
2013 assert(s->child.options & (WSTOPPED|WCONTINUED));
2014 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2017 r = source_set_pending(s, true);
2026 static int process_signal(sd_event *e, uint32_t events) {
2027 bool read_one = false;
2032 assert_return(events == EPOLLIN, -EIO);
2035 struct signalfd_siginfo si;
2037 sd_event_source *s = NULL;
2039 n = read(e->signal_fd, &si, sizeof(si));
2041 if (errno == EAGAIN || errno == EINTR)
2047 if (_unlikely_(n != sizeof(si)))
2050 assert(si.ssi_signo < _NSIG);
2054 if (si.ssi_signo == SIGCHLD) {
2055 r = process_child(e);
2062 if (e->signal_sources)
2063 s = e->signal_sources[si.ssi_signo];
2068 s->signal.siginfo = si;
2069 r = source_set_pending(s, true);
2075 static int source_dispatch(sd_event_source *s) {
2079 assert(s->pending || s->type == SOURCE_EXIT);
2081 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2082 r = source_set_pending(s, false);
2087 if (s->type != SOURCE_POST) {
2091 /* If we execute a non-post source, let's mark all
2092 * post sources as pending */
2094 SET_FOREACH(z, s->event->post_sources, i) {
2095 if (z->enabled == SD_EVENT_OFF)
2098 r = source_set_pending(z, true);
2104 if (s->enabled == SD_EVENT_ONESHOT) {
2105 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2110 s->dispatching = true;
2115 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2118 case SOURCE_TIME_REALTIME:
2119 case SOURCE_TIME_BOOTTIME:
2120 case SOURCE_TIME_MONOTONIC:
2121 case SOURCE_TIME_REALTIME_ALARM:
2122 case SOURCE_TIME_BOOTTIME_ALARM:
2123 r = s->time.callback(s, s->time.next, s->userdata);
2127 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2130 case SOURCE_CHILD: {
2133 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2134 s->child.siginfo.si_code == CLD_KILLED ||
2135 s->child.siginfo.si_code == CLD_DUMPED;
2137 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2139 /* Now, reap the PID for good. */
2141 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2147 r = s->defer.callback(s, s->userdata);
2151 r = s->post.callback(s, s->userdata);
2155 r = s->exit.callback(s, s->userdata);
2158 case SOURCE_WATCHDOG:
2159 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2160 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2161 assert_not_reached("Wut? I shouldn't exist.");
2164 s->dispatching = false;
2168 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2170 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2176 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2181 static int event_prepare(sd_event *e) {
2189 s = prioq_peek(e->prepare);
2190 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2193 s->prepare_iteration = e->iteration;
2194 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2200 s->dispatching = true;
2201 r = s->prepare(s, s->userdata);
2202 s->dispatching = false;
2206 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2208 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2214 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2220 static int dispatch_exit(sd_event *e) {
2226 p = prioq_peek(e->exit);
2227 if (!p || p->enabled == SD_EVENT_OFF) {
2228 e->state = SD_EVENT_FINISHED;
2234 e->state = SD_EVENT_EXITING;
2236 r = source_dispatch(p);
2238 e->state = SD_EVENT_PASSIVE;
2244 static sd_event_source* event_next_pending(sd_event *e) {
2249 p = prioq_peek(e->pending);
2253 if (p->enabled == SD_EVENT_OFF)
2259 static int arm_watchdog(sd_event *e) {
2260 struct itimerspec its = {};
2265 assert(e->watchdog_fd >= 0);
2267 t = sleep_between(e,
2268 e->watchdog_last + (e->watchdog_period / 2),
2269 e->watchdog_last + (e->watchdog_period * 3 / 4));
2271 timespec_store(&its.it_value, t);
2273 /* Make sure we never set the watchdog to 0, which tells the
2274 * kernel to disable it. */
2275 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2276 its.it_value.tv_nsec = 1;
2278 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2285 static int process_watchdog(sd_event *e) {
2291 /* Don't notify watchdog too often */
2292 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2295 sd_notify(false, "WATCHDOG=1");
2296 e->watchdog_last = e->timestamp.monotonic;
2298 return arm_watchdog(e);
2301 _public_ int sd_event_prepare(sd_event *e) {
2304 assert_return(e, -EINVAL);
2305 assert_return(!event_pid_changed(e), -ECHILD);
2306 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2307 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2309 if (e->exit_requested)
2314 r = event_prepare(e);
2318 r = event_arm_timer(e, &e->realtime);
2322 r = event_arm_timer(e, &e->boottime);
2326 r = event_arm_timer(e, &e->monotonic);
2330 r = event_arm_timer(e, &e->realtime_alarm);
2334 r = event_arm_timer(e, &e->boottime_alarm);
2338 if (event_next_pending(e) || e->need_process_child)
2341 e->state = SD_EVENT_PREPARED;
2346 e->state = SD_EVENT_PREPARED;
2347 r = sd_event_wait(e, 0);
2349 e->state = SD_EVENT_PREPARED;
2354 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2355 struct epoll_event *ev_queue;
2356 unsigned ev_queue_max;
2359 assert_return(e, -EINVAL);
2360 assert_return(!event_pid_changed(e), -ECHILD);
2361 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2362 assert_return(e->state == SD_EVENT_PREPARED, -EBUSY);
2364 if (e->exit_requested) {
2365 e->state = SD_EVENT_PENDING;
2369 ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2370 ev_queue = newa(struct epoll_event, ev_queue_max);
2372 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2373 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2375 if (errno == EINTR) {
2376 e->state = SD_EVENT_PENDING;
2385 dual_timestamp_get(&e->timestamp);
2386 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2388 for (i = 0; i < m; i++) {
2390 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2391 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2392 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2393 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2394 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2395 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2396 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2397 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2398 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2399 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2400 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2401 r = process_signal(e, ev_queue[i].events);
2402 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2403 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2405 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2411 r = process_watchdog(e);
2415 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2419 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2423 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2427 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2431 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2435 if (e->need_process_child) {
2436 r = process_child(e);
2441 if (event_next_pending(e)) {
2442 e->state = SD_EVENT_PENDING;
2450 e->state = SD_EVENT_PASSIVE;
2455 _public_ int sd_event_dispatch(sd_event *e) {
2459 assert_return(e, -EINVAL);
2460 assert_return(!event_pid_changed(e), -ECHILD);
2461 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2462 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2464 if (e->exit_requested)
2465 return dispatch_exit(e);
2467 p = event_next_pending(e);
2471 e->state = SD_EVENT_RUNNING;
2472 r = source_dispatch(p);
2473 e->state = SD_EVENT_PASSIVE;
2480 e->state = SD_EVENT_PASSIVE;
2485 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2488 assert_return(e, -EINVAL);
2489 assert_return(!event_pid_changed(e), -ECHILD);
2490 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2491 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2493 r = sd_event_prepare(e);
2495 return sd_event_dispatch(e);
2499 r = sd_event_wait(e, timeout);
2501 return sd_event_dispatch(e);
2506 _public_ int sd_event_loop(sd_event *e) {
2509 assert_return(e, -EINVAL);
2510 assert_return(!event_pid_changed(e), -ECHILD);
2511 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2515 while (e->state != SD_EVENT_FINISHED) {
2516 r = sd_event_run(e, (uint64_t) -1);
2528 _public_ int sd_event_get_fd(sd_event *e) {
2530 assert_return(e, -EINVAL);
2531 assert_return(!event_pid_changed(e), -ECHILD);
2536 _public_ int sd_event_get_state(sd_event *e) {
2537 assert_return(e, -EINVAL);
2538 assert_return(!event_pid_changed(e), -ECHILD);
2543 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2544 assert_return(e, -EINVAL);
2545 assert_return(code, -EINVAL);
2546 assert_return(!event_pid_changed(e), -ECHILD);
2548 if (!e->exit_requested)
2551 *code = e->exit_code;
2555 _public_ int sd_event_exit(sd_event *e, int code) {
2556 assert_return(e, -EINVAL);
2557 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2558 assert_return(!event_pid_changed(e), -ECHILD);
2560 e->exit_requested = true;
2561 e->exit_code = code;
2566 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2567 assert_return(e, -EINVAL);
2568 assert_return(usec, -EINVAL);
2569 assert_return(!event_pid_changed(e), -ECHILD);
2571 /* If we haven't run yet, just get the actual time */
2572 if (!dual_timestamp_is_set(&e->timestamp))
2577 case CLOCK_REALTIME:
2578 case CLOCK_REALTIME_ALARM:
2579 *usec = e->timestamp.realtime;
2582 case CLOCK_MONOTONIC:
2583 *usec = e->timestamp.monotonic;
2586 case CLOCK_BOOTTIME:
2587 case CLOCK_BOOTTIME_ALARM:
2588 *usec = e->timestamp_boottime;
2595 _public_ int sd_event_default(sd_event **ret) {
2597 static thread_local sd_event *default_event = NULL;
2602 return !!default_event;
2604 if (default_event) {
2605 *ret = sd_event_ref(default_event);
2609 r = sd_event_new(&e);
2613 e->default_event_ptr = &default_event;
2621 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2622 assert_return(e, -EINVAL);
2623 assert_return(tid, -EINVAL);
2624 assert_return(!event_pid_changed(e), -ECHILD);
2634 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2637 assert_return(e, -EINVAL);
2638 assert_return(!event_pid_changed(e), -ECHILD);
2640 if (e->watchdog == !!b)
2644 struct epoll_event ev = {};
2646 r = sd_watchdog_enabled(false, &e->watchdog_period);
2650 /* Issue first ping immediately */
2651 sd_notify(false, "WATCHDOG=1");
2652 e->watchdog_last = now(CLOCK_MONOTONIC);
2654 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2655 if (e->watchdog_fd < 0)
2658 r = arm_watchdog(e);
2662 ev.events = EPOLLIN;
2663 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2665 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2672 if (e->watchdog_fd >= 0) {
2673 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2674 e->watchdog_fd = safe_close(e->watchdog_fd);
2682 e->watchdog_fd = safe_close(e->watchdog_fd);
2686 _public_ int sd_event_get_watchdog(sd_event *e) {
2687 assert_return(e, -EINVAL);
2688 assert_return(!event_pid_changed(e), -ECHILD);