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 void source_disconnect(sd_event_source *s) {
601 assert(s->event->n_sources > 0);
607 source_io_unregister(s);
611 case SOURCE_TIME_REALTIME:
612 case SOURCE_TIME_BOOTTIME:
613 case SOURCE_TIME_MONOTONIC:
614 case SOURCE_TIME_REALTIME_ALARM:
615 case SOURCE_TIME_BOOTTIME_ALARM: {
616 struct clock_data *d;
618 d = event_get_clock_data(s->event, s->type);
621 prioq_remove(d->earliest, s, &s->time.earliest_index);
622 prioq_remove(d->latest, s, &s->time.latest_index);
623 d->needs_rearm = true;
628 if (s->signal.sig > 0) {
629 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
630 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
632 if (s->event->signal_sources)
633 s->event->signal_sources[s->signal.sig] = NULL;
639 if (s->child.pid > 0) {
640 if (s->enabled != SD_EVENT_OFF) {
641 assert(s->event->n_enabled_child_sources > 0);
642 s->event->n_enabled_child_sources--;
645 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
646 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
648 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
658 set_remove(s->event->post_sources, s);
662 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
666 assert_not_reached("Wut? I shouldn't exist.");
670 prioq_remove(s->event->pending, s, &s->pending_index);
673 prioq_remove(s->event->prepare, s, &s->prepare_index);
677 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
679 LIST_REMOVE(sources, event->sources, s);
683 sd_event_unref(event);
686 static void source_free(sd_event_source *s) {
689 source_disconnect(s);
694 static int source_set_pending(sd_event_source *s, bool b) {
698 assert(s->type != SOURCE_EXIT);
706 s->pending_iteration = s->event->iteration;
708 r = prioq_put(s->event->pending, s, &s->pending_index);
714 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
716 if (EVENT_SOURCE_IS_TIME(s->type)) {
717 struct clock_data *d;
719 d = event_get_clock_data(s->event, s->type);
722 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
723 prioq_reshuffle(d->latest, s, &s->time.latest_index);
724 d->needs_rearm = true;
730 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
735 s = new0(sd_event_source, 1);
741 s->floating = floating;
743 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
748 LIST_PREPEND(sources, e->sources, s);
754 _public_ int sd_event_add_io(
756 sd_event_source **ret,
759 sd_event_io_handler_t callback,
765 assert_return(e, -EINVAL);
766 assert_return(fd >= 0, -EINVAL);
767 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
768 assert_return(callback, -EINVAL);
769 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
770 assert_return(!event_pid_changed(e), -ECHILD);
772 s = source_new(e, !ret, SOURCE_IO);
777 s->io.events = events;
778 s->io.callback = callback;
779 s->userdata = userdata;
780 s->enabled = SD_EVENT_ON;
782 r = source_io_register(s, s->enabled, events);
794 static void initialize_perturb(sd_event *e) {
795 sd_id128_t bootid = {};
797 /* When we sleep for longer, we try to realign the wakeup to
798 the same time wihtin each minute/second/250ms, so that
799 events all across the system can be coalesced into a single
800 CPU wakeup. However, let's take some system-specific
801 randomness for this value, so that in a network of systems
802 with synced clocks timer events are distributed a
803 bit. Here, we calculate a perturbation usec offset from the
806 if (_likely_(e->perturb != USEC_INFINITY))
809 if (sd_id128_get_boot(&bootid) >= 0)
810 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
813 static int event_setup_timer_fd(
815 struct clock_data *d,
818 struct epoll_event ev = {};
824 if (_likely_(d->fd >= 0))
827 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
832 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
834 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
844 _public_ int sd_event_add_time(
846 sd_event_source **ret,
850 sd_event_time_handler_t callback,
853 EventSourceType type;
855 struct clock_data *d;
858 assert_return(e, -EINVAL);
859 assert_return(usec != (uint64_t) -1, -EINVAL);
860 assert_return(accuracy != (uint64_t) -1, -EINVAL);
861 assert_return(callback, -EINVAL);
862 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
863 assert_return(!event_pid_changed(e), -ECHILD);
865 type = clock_to_event_source_type(clock);
866 assert_return(type >= 0, -ENOTSUP);
868 d = event_get_clock_data(e, type);
872 d->earliest = prioq_new(earliest_time_prioq_compare);
878 d->latest = prioq_new(latest_time_prioq_compare);
884 r = event_setup_timer_fd(e, d, clock);
889 s = source_new(e, !ret, type);
894 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
895 s->time.callback = callback;
896 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
897 s->userdata = userdata;
898 s->enabled = SD_EVENT_ONESHOT;
900 d->needs_rearm = true;
902 r = prioq_put(d->earliest, s, &s->time.earliest_index);
906 r = prioq_put(d->latest, s, &s->time.latest_index);
920 static int event_update_signal_fd(sd_event *e) {
921 struct epoll_event ev = {};
927 add_to_epoll = e->signal_fd < 0;
929 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
939 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
941 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
943 e->signal_fd = safe_close(e->signal_fd);
950 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
953 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
956 _public_ int sd_event_add_signal(
958 sd_event_source **ret,
960 sd_event_signal_handler_t callback,
967 assert_return(e, -EINVAL);
968 assert_return(sig > 0, -EINVAL);
969 assert_return(sig < _NSIG, -EINVAL);
970 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
971 assert_return(!event_pid_changed(e), -ECHILD);
974 callback = signal_exit_callback;
976 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
980 if (!sigismember(&ss, sig))
983 if (!e->signal_sources) {
984 e->signal_sources = new0(sd_event_source*, _NSIG);
985 if (!e->signal_sources)
987 } else if (e->signal_sources[sig])
990 s = source_new(e, !ret, SOURCE_SIGNAL);
995 s->signal.callback = callback;
996 s->userdata = userdata;
997 s->enabled = SD_EVENT_ON;
999 e->signal_sources[sig] = s;
1000 assert_se(sigaddset(&e->sigset, sig) == 0);
1002 if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
1003 r = event_update_signal_fd(e);
1016 _public_ int sd_event_add_child(
1018 sd_event_source **ret,
1021 sd_event_child_handler_t callback,
1027 assert_return(e, -EINVAL);
1028 assert_return(pid > 1, -EINVAL);
1029 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1030 assert_return(options != 0, -EINVAL);
1031 assert_return(callback, -EINVAL);
1032 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1033 assert_return(!event_pid_changed(e), -ECHILD);
1035 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1039 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1042 s = source_new(e, !ret, SOURCE_CHILD);
1047 s->child.options = options;
1048 s->child.callback = callback;
1049 s->userdata = userdata;
1050 s->enabled = SD_EVENT_ONESHOT;
1052 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1058 e->n_enabled_child_sources ++;
1060 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1062 if (!e->signal_sources || !e->signal_sources[SIGCHLD]) {
1063 r = event_update_signal_fd(e);
1070 e->need_process_child = true;
1078 _public_ int sd_event_add_defer(
1080 sd_event_source **ret,
1081 sd_event_handler_t callback,
1087 assert_return(e, -EINVAL);
1088 assert_return(callback, -EINVAL);
1089 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1090 assert_return(!event_pid_changed(e), -ECHILD);
1092 s = source_new(e, !ret, SOURCE_DEFER);
1096 s->defer.callback = callback;
1097 s->userdata = userdata;
1098 s->enabled = SD_EVENT_ONESHOT;
1100 r = source_set_pending(s, true);
1112 _public_ int sd_event_add_post(
1114 sd_event_source **ret,
1115 sd_event_handler_t callback,
1121 assert_return(e, -EINVAL);
1122 assert_return(callback, -EINVAL);
1123 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1124 assert_return(!event_pid_changed(e), -ECHILD);
1126 r = set_ensure_allocated(&e->post_sources, NULL);
1130 s = source_new(e, !ret, SOURCE_POST);
1134 s->post.callback = callback;
1135 s->userdata = userdata;
1136 s->enabled = SD_EVENT_ON;
1138 r = set_put(e->post_sources, s);
1150 _public_ int sd_event_add_exit(
1152 sd_event_source **ret,
1153 sd_event_handler_t callback,
1159 assert_return(e, -EINVAL);
1160 assert_return(callback, -EINVAL);
1161 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1162 assert_return(!event_pid_changed(e), -ECHILD);
1165 e->exit = prioq_new(exit_prioq_compare);
1170 s = source_new(e, !ret, SOURCE_EXIT);
1174 s->exit.callback = callback;
1175 s->userdata = userdata;
1176 s->exit.prioq_index = PRIOQ_IDX_NULL;
1177 s->enabled = SD_EVENT_ONESHOT;
1179 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1191 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1192 assert_return(s, NULL);
1194 assert(s->n_ref >= 1);
1200 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1205 assert(s->n_ref >= 1);
1208 if (s->n_ref <= 0) {
1209 /* Here's a special hack: when we are called from a
1210 * dispatch handler we won't free the event source
1211 * immediately, but we will detach the fd from the
1212 * epoll. This way it is safe for the caller to unref
1213 * the event source and immediately close the fd, but
1214 * we still retain a valid event source object after
1217 if (s->dispatching) {
1218 if (s->type == SOURCE_IO)
1219 source_io_unregister(s);
1221 source_disconnect(s);
1229 _public_ int sd_event_source_set_name(sd_event_source *s, const char *name) {
1230 assert_return(s, -EINVAL);
1232 return free_and_strdup(&s->name, name);
1235 _public_ int sd_event_source_get_name(sd_event_source *s, const char **name) {
1236 assert_return(s, -EINVAL);
1237 assert_return(name, -EINVAL);
1244 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1245 assert_return(s, NULL);
1250 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1251 assert_return(s, -EINVAL);
1252 assert_return(s->type != SOURCE_EXIT, -EDOM);
1253 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1254 assert_return(!event_pid_changed(s->event), -ECHILD);
1259 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1260 assert_return(s, -EINVAL);
1261 assert_return(s->type == SOURCE_IO, -EDOM);
1262 assert_return(!event_pid_changed(s->event), -ECHILD);
1267 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1270 assert_return(s, -EINVAL);
1271 assert_return(fd >= 0, -EINVAL);
1272 assert_return(s->type == SOURCE_IO, -EDOM);
1273 assert_return(!event_pid_changed(s->event), -ECHILD);
1278 if (s->enabled == SD_EVENT_OFF) {
1280 s->io.registered = false;
1284 saved_fd = s->io.fd;
1285 assert(s->io.registered);
1288 s->io.registered = false;
1290 r = source_io_register(s, s->enabled, s->io.events);
1292 s->io.fd = saved_fd;
1293 s->io.registered = true;
1297 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1303 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1304 assert_return(s, -EINVAL);
1305 assert_return(events, -EINVAL);
1306 assert_return(s->type == SOURCE_IO, -EDOM);
1307 assert_return(!event_pid_changed(s->event), -ECHILD);
1309 *events = s->io.events;
1313 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1316 assert_return(s, -EINVAL);
1317 assert_return(s->type == SOURCE_IO, -EDOM);
1318 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1319 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1320 assert_return(!event_pid_changed(s->event), -ECHILD);
1322 /* edge-triggered updates are never skipped, so we can reset edges */
1323 if (s->io.events == events && !(events & EPOLLET))
1326 if (s->enabled != SD_EVENT_OFF) {
1327 r = source_io_register(s, s->enabled, events);
1332 s->io.events = events;
1333 source_set_pending(s, false);
1338 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1339 assert_return(s, -EINVAL);
1340 assert_return(revents, -EINVAL);
1341 assert_return(s->type == SOURCE_IO, -EDOM);
1342 assert_return(s->pending, -ENODATA);
1343 assert_return(!event_pid_changed(s->event), -ECHILD);
1345 *revents = s->io.revents;
1349 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1350 assert_return(s, -EINVAL);
1351 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1352 assert_return(!event_pid_changed(s->event), -ECHILD);
1354 return s->signal.sig;
1357 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1358 assert_return(s, -EINVAL);
1359 assert_return(!event_pid_changed(s->event), -ECHILD);
1364 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1365 assert_return(s, -EINVAL);
1366 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1367 assert_return(!event_pid_changed(s->event), -ECHILD);
1369 if (s->priority == priority)
1372 s->priority = priority;
1375 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1378 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1380 if (s->type == SOURCE_EXIT)
1381 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1386 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1387 assert_return(s, -EINVAL);
1388 assert_return(m, -EINVAL);
1389 assert_return(!event_pid_changed(s->event), -ECHILD);
1395 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1398 assert_return(s, -EINVAL);
1399 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1400 assert_return(!event_pid_changed(s->event), -ECHILD);
1402 /* If we are dead anyway, we are fine with turning off
1403 * sources, but everything else needs to fail. */
1404 if (s->event->state == SD_EVENT_FINISHED)
1405 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1407 if (s->enabled == m)
1410 if (m == SD_EVENT_OFF) {
1415 r = source_io_unregister(s);
1422 case SOURCE_TIME_REALTIME:
1423 case SOURCE_TIME_BOOTTIME:
1424 case SOURCE_TIME_MONOTONIC:
1425 case SOURCE_TIME_REALTIME_ALARM:
1426 case SOURCE_TIME_BOOTTIME_ALARM: {
1427 struct clock_data *d;
1430 d = event_get_clock_data(s->event, s->type);
1433 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1434 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1435 d->needs_rearm = true;
1441 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1442 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1443 event_update_signal_fd(s->event);
1451 assert(s->event->n_enabled_child_sources > 0);
1452 s->event->n_enabled_child_sources--;
1454 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1455 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1456 event_update_signal_fd(s->event);
1463 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1472 assert_not_reached("Wut? I shouldn't exist.");
1479 r = source_io_register(s, m, s->io.events);
1486 case SOURCE_TIME_REALTIME:
1487 case SOURCE_TIME_BOOTTIME:
1488 case SOURCE_TIME_MONOTONIC:
1489 case SOURCE_TIME_REALTIME_ALARM:
1490 case SOURCE_TIME_BOOTTIME_ALARM: {
1491 struct clock_data *d;
1494 d = event_get_clock_data(s->event, s->type);
1497 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1498 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1499 d->needs_rearm = true;
1506 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1507 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1508 event_update_signal_fd(s->event);
1513 if (s->enabled == SD_EVENT_OFF) {
1514 s->event->n_enabled_child_sources++;
1516 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1517 assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
1518 event_update_signal_fd(s->event);
1527 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1536 assert_not_reached("Wut? I shouldn't exist.");
1541 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1544 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1549 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1550 assert_return(s, -EINVAL);
1551 assert_return(usec, -EINVAL);
1552 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1553 assert_return(!event_pid_changed(s->event), -ECHILD);
1555 *usec = s->time.next;
1559 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1560 struct clock_data *d;
1562 assert_return(s, -EINVAL);
1563 assert_return(usec != (uint64_t) -1, -EINVAL);
1564 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1565 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1566 assert_return(!event_pid_changed(s->event), -ECHILD);
1568 s->time.next = usec;
1570 source_set_pending(s, false);
1572 d = event_get_clock_data(s->event, s->type);
1575 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1576 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1577 d->needs_rearm = true;
1582 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1583 assert_return(s, -EINVAL);
1584 assert_return(usec, -EINVAL);
1585 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1586 assert_return(!event_pid_changed(s->event), -ECHILD);
1588 *usec = s->time.accuracy;
1592 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1593 struct clock_data *d;
1595 assert_return(s, -EINVAL);
1596 assert_return(usec != (uint64_t) -1, -EINVAL);
1597 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1598 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1599 assert_return(!event_pid_changed(s->event), -ECHILD);
1602 usec = DEFAULT_ACCURACY_USEC;
1604 s->time.accuracy = usec;
1606 source_set_pending(s, false);
1608 d = event_get_clock_data(s->event, s->type);
1611 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1612 d->needs_rearm = true;
1617 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1618 assert_return(s, -EINVAL);
1619 assert_return(clock, -EINVAL);
1620 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1621 assert_return(!event_pid_changed(s->event), -ECHILD);
1623 *clock = event_source_type_to_clock(s->type);
1627 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1628 assert_return(s, -EINVAL);
1629 assert_return(pid, -EINVAL);
1630 assert_return(s->type == SOURCE_CHILD, -EDOM);
1631 assert_return(!event_pid_changed(s->event), -ECHILD);
1633 *pid = s->child.pid;
1637 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1640 assert_return(s, -EINVAL);
1641 assert_return(s->type != SOURCE_EXIT, -EDOM);
1642 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1643 assert_return(!event_pid_changed(s->event), -ECHILD);
1645 if (s->prepare == callback)
1648 if (callback && s->prepare) {
1649 s->prepare = callback;
1653 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1657 s->prepare = callback;
1660 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1664 prioq_remove(s->event->prepare, s, &s->prepare_index);
1669 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1670 assert_return(s, NULL);
1675 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1678 assert_return(s, NULL);
1681 s->userdata = userdata;
1686 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1697 initialize_perturb(e);
1700 Find a good time to wake up again between times a and b. We
1701 have two goals here:
1703 a) We want to wake up as seldom as possible, hence prefer
1704 later times over earlier times.
1706 b) But if we have to wake up, then let's make sure to
1707 dispatch as much as possible on the entire system.
1709 We implement this by waking up everywhere at the same time
1710 within any given minute if we can, synchronised via the
1711 perturbation value determined from the boot ID. If we can't,
1712 then we try to find the same spot in every 10s, then 1s and
1713 then 250ms step. Otherwise, we pick the last possible time
1717 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1719 if (_unlikely_(c < USEC_PER_MINUTE))
1722 c -= USEC_PER_MINUTE;
1728 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1730 if (_unlikely_(c < USEC_PER_SEC*10))
1733 c -= USEC_PER_SEC*10;
1739 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1741 if (_unlikely_(c < USEC_PER_SEC))
1750 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1752 if (_unlikely_(c < USEC_PER_MSEC*250))
1755 c -= USEC_PER_MSEC*250;
1764 static int event_arm_timer(
1766 struct clock_data *d) {
1768 struct itimerspec its = {};
1769 sd_event_source *a, *b;
1776 if (!d->needs_rearm)
1779 d->needs_rearm = false;
1781 a = prioq_peek(d->earliest);
1782 if (!a || a->enabled == SD_EVENT_OFF) {
1787 if (d->next == USEC_INFINITY)
1791 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1795 d->next = USEC_INFINITY;
1799 b = prioq_peek(d->latest);
1800 assert_se(b && b->enabled != SD_EVENT_OFF);
1802 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1806 assert_se(d->fd >= 0);
1809 /* We don' want to disarm here, just mean some time looooong ago. */
1810 its.it_value.tv_sec = 0;
1811 its.it_value.tv_nsec = 1;
1813 timespec_store(&its.it_value, t);
1815 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1823 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1826 assert(s->type == SOURCE_IO);
1828 /* If the event source was already pending, we just OR in the
1829 * new revents, otherwise we reset the value. The ORing is
1830 * necessary to handle EPOLLONESHOT events properly where
1831 * readability might happen independently of writability, and
1832 * we need to keep track of both */
1835 s->io.revents |= revents;
1837 s->io.revents = revents;
1839 return source_set_pending(s, true);
1842 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1849 assert_return(events == EPOLLIN, -EIO);
1851 ss = read(fd, &x, sizeof(x));
1853 if (errno == EAGAIN || errno == EINTR)
1859 if (_unlikely_(ss != sizeof(x)))
1863 *next = USEC_INFINITY;
1868 static int process_timer(
1871 struct clock_data *d) {
1880 s = prioq_peek(d->earliest);
1883 s->enabled == SD_EVENT_OFF ||
1887 r = source_set_pending(s, true);
1891 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1892 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1893 d->needs_rearm = true;
1899 static int process_child(sd_event *e) {
1906 e->need_process_child = false;
1909 So, this is ugly. We iteratively invoke waitid() with P_PID
1910 + WNOHANG for each PID we wait for, instead of using
1911 P_ALL. This is because we only want to get child
1912 information of very specific child processes, and not all
1913 of them. We might not have processed the SIGCHLD even of a
1914 previous invocation and we don't want to maintain a
1915 unbounded *per-child* event queue, hence we really don't
1916 want anything flushed out of the kernel's queue that we
1917 don't care about. Since this is O(n) this means that if you
1918 have a lot of processes you probably want to handle SIGCHLD
1921 We do not reap the children here (by using WNOWAIT), this
1922 is only done after the event source is dispatched so that
1923 the callback still sees the process as a zombie.
1926 HASHMAP_FOREACH(s, e->child_sources, i) {
1927 assert(s->type == SOURCE_CHILD);
1932 if (s->enabled == SD_EVENT_OFF)
1935 zero(s->child.siginfo);
1936 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1937 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
1941 if (s->child.siginfo.si_pid != 0) {
1943 s->child.siginfo.si_code == CLD_EXITED ||
1944 s->child.siginfo.si_code == CLD_KILLED ||
1945 s->child.siginfo.si_code == CLD_DUMPED;
1947 if (!zombie && (s->child.options & WEXITED)) {
1948 /* If the child isn't dead then let's
1949 * immediately remove the state change
1950 * from the queue, since there's no
1951 * benefit in leaving it queued */
1953 assert(s->child.options & (WSTOPPED|WCONTINUED));
1954 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
1957 r = source_set_pending(s, true);
1966 static int process_signal(sd_event *e, uint32_t events) {
1967 bool read_one = false;
1972 assert_return(events == EPOLLIN, -EIO);
1975 struct signalfd_siginfo si;
1977 sd_event_source *s = NULL;
1979 n = read(e->signal_fd, &si, sizeof(si));
1981 if (errno == EAGAIN || errno == EINTR)
1987 if (_unlikely_(n != sizeof(si)))
1990 assert(si.ssi_signo < _NSIG);
1994 if (si.ssi_signo == SIGCHLD) {
1995 r = process_child(e);
2002 if (e->signal_sources)
2003 s = e->signal_sources[si.ssi_signo];
2008 s->signal.siginfo = si;
2009 r = source_set_pending(s, true);
2015 static int source_dispatch(sd_event_source *s) {
2019 assert(s->pending || s->type == SOURCE_EXIT);
2021 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2022 r = source_set_pending(s, false);
2027 if (s->type != SOURCE_POST) {
2031 /* If we execute a non-post source, let's mark all
2032 * post sources as pending */
2034 SET_FOREACH(z, s->event->post_sources, i) {
2035 if (z->enabled == SD_EVENT_OFF)
2038 r = source_set_pending(z, true);
2044 if (s->enabled == SD_EVENT_ONESHOT) {
2045 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2050 s->dispatching = true;
2055 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2058 case SOURCE_TIME_REALTIME:
2059 case SOURCE_TIME_BOOTTIME:
2060 case SOURCE_TIME_MONOTONIC:
2061 case SOURCE_TIME_REALTIME_ALARM:
2062 case SOURCE_TIME_BOOTTIME_ALARM:
2063 r = s->time.callback(s, s->time.next, s->userdata);
2067 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2070 case SOURCE_CHILD: {
2073 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2074 s->child.siginfo.si_code == CLD_KILLED ||
2075 s->child.siginfo.si_code == CLD_DUMPED;
2077 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2079 /* Now, reap the PID for good. */
2081 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2087 r = s->defer.callback(s, s->userdata);
2091 r = s->post.callback(s, s->userdata);
2095 r = s->exit.callback(s, s->userdata);
2098 case SOURCE_WATCHDOG:
2099 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2100 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2101 assert_not_reached("Wut? I shouldn't exist.");
2104 s->dispatching = false;
2108 log_debug("Event source '%s' returned error, disabling: %s", s->name, strerror(-r));
2110 log_debug("Event source %p returned error, disabling: %s", s, strerror(-r));
2116 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2121 static int event_prepare(sd_event *e) {
2129 s = prioq_peek(e->prepare);
2130 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2133 s->prepare_iteration = e->iteration;
2134 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2140 s->dispatching = true;
2141 r = s->prepare(s, s->userdata);
2142 s->dispatching = false;
2146 log_debug("Prepare callback of event source '%s' returned error, disabling: %s", s->name, strerror(-r));
2148 log_debug("Prepare callback of event source %p returned error, disabling: %s", s, strerror(-r));
2154 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2160 static int dispatch_exit(sd_event *e) {
2166 p = prioq_peek(e->exit);
2167 if (!p || p->enabled == SD_EVENT_OFF) {
2168 e->state = SD_EVENT_FINISHED;
2174 e->state = SD_EVENT_EXITING;
2176 r = source_dispatch(p);
2178 e->state = SD_EVENT_PASSIVE;
2184 static sd_event_source* event_next_pending(sd_event *e) {
2189 p = prioq_peek(e->pending);
2193 if (p->enabled == SD_EVENT_OFF)
2199 static int arm_watchdog(sd_event *e) {
2200 struct itimerspec its = {};
2205 assert(e->watchdog_fd >= 0);
2207 t = sleep_between(e,
2208 e->watchdog_last + (e->watchdog_period / 2),
2209 e->watchdog_last + (e->watchdog_period * 3 / 4));
2211 timespec_store(&its.it_value, t);
2213 /* Make sure we never set the watchdog to 0, which tells the
2214 * kernel to disable it. */
2215 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2216 its.it_value.tv_nsec = 1;
2218 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2225 static int process_watchdog(sd_event *e) {
2231 /* Don't notify watchdog too often */
2232 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2235 sd_notify(false, "WATCHDOG=1");
2236 e->watchdog_last = e->timestamp.monotonic;
2238 return arm_watchdog(e);
2241 _public_ int sd_event_prepare(sd_event *e) {
2244 assert_return(e, -EINVAL);
2245 assert_return(!event_pid_changed(e), -ECHILD);
2246 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2247 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2249 if (e->exit_requested)
2254 r = event_prepare(e);
2258 r = event_arm_timer(e, &e->realtime);
2262 r = event_arm_timer(e, &e->boottime);
2266 r = event_arm_timer(e, &e->monotonic);
2270 r = event_arm_timer(e, &e->realtime_alarm);
2274 r = event_arm_timer(e, &e->boottime_alarm);
2278 if (event_next_pending(e) || e->need_process_child)
2281 e->state = SD_EVENT_PREPARED;
2286 e->state = SD_EVENT_PREPARED;
2287 r = sd_event_wait(e, 0);
2289 e->state = SD_EVENT_PREPARED;
2294 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2295 struct epoll_event *ev_queue;
2296 unsigned ev_queue_max;
2299 assert_return(e, -EINVAL);
2300 assert_return(!event_pid_changed(e), -ECHILD);
2301 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2302 assert_return(e->state == SD_EVENT_PREPARED, -EBUSY);
2304 if (e->exit_requested) {
2305 e->state = SD_EVENT_PENDING;
2309 ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2310 ev_queue = newa(struct epoll_event, ev_queue_max);
2312 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2313 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2315 if (errno == EINTR) {
2316 e->state = SD_EVENT_PENDING;
2325 dual_timestamp_get(&e->timestamp);
2326 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2328 for (i = 0; i < m; i++) {
2330 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2331 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2332 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2333 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2334 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2335 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2336 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2337 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2338 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2339 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2340 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2341 r = process_signal(e, ev_queue[i].events);
2342 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2343 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2345 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2351 r = process_watchdog(e);
2355 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2359 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2363 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2367 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2371 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2375 if (e->need_process_child) {
2376 r = process_child(e);
2381 if (event_next_pending(e)) {
2382 e->state = SD_EVENT_PENDING;
2390 e->state = SD_EVENT_PASSIVE;
2395 _public_ int sd_event_dispatch(sd_event *e) {
2399 assert_return(e, -EINVAL);
2400 assert_return(!event_pid_changed(e), -ECHILD);
2401 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2402 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2404 if (e->exit_requested)
2405 return dispatch_exit(e);
2407 p = event_next_pending(e);
2411 e->state = SD_EVENT_RUNNING;
2412 r = source_dispatch(p);
2413 e->state = SD_EVENT_PASSIVE;
2420 e->state = SD_EVENT_PASSIVE;
2425 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2428 assert_return(e, -EINVAL);
2429 assert_return(!event_pid_changed(e), -ECHILD);
2430 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2431 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2433 r = sd_event_prepare(e);
2435 return sd_event_dispatch(e);
2439 r = sd_event_wait(e, timeout);
2441 return sd_event_dispatch(e);
2446 _public_ int sd_event_loop(sd_event *e) {
2449 assert_return(e, -EINVAL);
2450 assert_return(!event_pid_changed(e), -ECHILD);
2451 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2455 while (e->state != SD_EVENT_FINISHED) {
2456 r = sd_event_run(e, (uint64_t) -1);
2468 _public_ int sd_event_get_fd(sd_event *e) {
2470 assert_return(e, -EINVAL);
2471 assert_return(!event_pid_changed(e), -ECHILD);
2476 _public_ int sd_event_get_state(sd_event *e) {
2477 assert_return(e, -EINVAL);
2478 assert_return(!event_pid_changed(e), -ECHILD);
2483 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2484 assert_return(e, -EINVAL);
2485 assert_return(code, -EINVAL);
2486 assert_return(!event_pid_changed(e), -ECHILD);
2488 if (!e->exit_requested)
2491 *code = e->exit_code;
2495 _public_ int sd_event_exit(sd_event *e, int code) {
2496 assert_return(e, -EINVAL);
2497 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2498 assert_return(!event_pid_changed(e), -ECHILD);
2500 e->exit_requested = true;
2501 e->exit_code = code;
2506 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2507 assert_return(e, -EINVAL);
2508 assert_return(usec, -EINVAL);
2509 assert_return(!event_pid_changed(e), -ECHILD);
2511 /* If we haven't run yet, just get the actual time */
2512 if (!dual_timestamp_is_set(&e->timestamp))
2517 case CLOCK_REALTIME:
2518 case CLOCK_REALTIME_ALARM:
2519 *usec = e->timestamp.realtime;
2522 case CLOCK_MONOTONIC:
2523 *usec = e->timestamp.monotonic;
2526 case CLOCK_BOOTTIME:
2527 case CLOCK_BOOTTIME_ALARM:
2528 *usec = e->timestamp_boottime;
2535 _public_ int sd_event_default(sd_event **ret) {
2537 static thread_local sd_event *default_event = NULL;
2542 return !!default_event;
2544 if (default_event) {
2545 *ret = sd_event_ref(default_event);
2549 r = sd_event_new(&e);
2553 e->default_event_ptr = &default_event;
2561 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2562 assert_return(e, -EINVAL);
2563 assert_return(tid, -EINVAL);
2564 assert_return(!event_pid_changed(e), -ECHILD);
2574 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2577 assert_return(e, -EINVAL);
2578 assert_return(!event_pid_changed(e), -ECHILD);
2580 if (e->watchdog == !!b)
2584 struct epoll_event ev = {};
2586 r = sd_watchdog_enabled(false, &e->watchdog_period);
2590 /* Issue first ping immediately */
2591 sd_notify(false, "WATCHDOG=1");
2592 e->watchdog_last = now(CLOCK_MONOTONIC);
2594 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2595 if (e->watchdog_fd < 0)
2598 r = arm_watchdog(e);
2602 ev.events = EPOLLIN;
2603 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2605 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2612 if (e->watchdog_fd >= 0) {
2613 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2614 e->watchdog_fd = safe_close(e->watchdog_fd);
2622 e->watchdog_fd = safe_close(e->watchdog_fd);
2626 _public_ int sd_event_get_watchdog(sd_event *e) {
2627 assert_return(e, -EINVAL);
2628 assert_return(!event_pid_changed(e), -ECHILD);