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 {
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_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
61 struct sd_event_source {
66 sd_event_handler_t prepare;
68 EventSourceType type:5;
75 unsigned pending_index;
76 unsigned prepare_index;
77 unsigned pending_iteration;
78 unsigned prepare_iteration;
80 LIST_FIELDS(sd_event_source, sources);
84 sd_event_io_handler_t callback;
91 sd_event_time_handler_t callback;
92 usec_t next, accuracy;
93 unsigned earliest_index;
94 unsigned latest_index;
97 sd_event_signal_handler_t callback;
98 struct signalfd_siginfo siginfo;
102 sd_event_child_handler_t callback;
108 sd_event_handler_t callback;
111 sd_event_handler_t callback;
114 sd_event_handler_t callback;
115 unsigned prioq_index;
123 /* For all clocks we maintain two priority queues each, one
124 * ordered for the earliest times the events may be
125 * dispatched, and one ordered by the latest times they must
126 * have been dispatched. The range between the top entries in
127 * the two prioqs is the time window we can freely schedule
145 /* timerfd_create() only supports these four clocks so far. We
146 * can add support for more clocks when the kernel learns to
147 * deal with them, too. */
148 struct clock_data realtime;
149 struct clock_data monotonic;
150 struct clock_data realtime_alarm;
151 struct clock_data boottime_alarm;
156 sd_event_source **signal_sources;
158 Hashmap *child_sources;
159 unsigned n_enabled_child_sources;
168 dual_timestamp timestamp;
169 usec_t timestamp_boottime;
172 bool exit_requested:1;
173 bool need_process_child:1;
179 sd_event **default_event_ptr;
181 usec_t watchdog_last, watchdog_period;
185 LIST_HEAD(sd_event_source, sources);
188 static void source_disconnect(sd_event_source *s);
190 static int pending_prioq_compare(const void *a, const void *b) {
191 const sd_event_source *x = a, *y = b;
196 /* Enabled ones first */
197 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
199 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
202 /* Lower priority values first */
203 if (x->priority < y->priority)
205 if (x->priority > y->priority)
208 /* Older entries first */
209 if (x->pending_iteration < y->pending_iteration)
211 if (x->pending_iteration > y->pending_iteration)
214 /* Stability for the rest */
223 static int prepare_prioq_compare(const void *a, const void *b) {
224 const sd_event_source *x = a, *y = b;
229 /* Move most recently prepared ones last, so that we can stop
230 * preparing as soon as we hit one that has already been
231 * prepared in the current iteration */
232 if (x->prepare_iteration < y->prepare_iteration)
234 if (x->prepare_iteration > y->prepare_iteration)
237 /* Enabled ones first */
238 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
240 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
243 /* Lower priority values first */
244 if (x->priority < y->priority)
246 if (x->priority > y->priority)
249 /* Stability for the rest */
258 static int earliest_time_prioq_compare(const void *a, const void *b) {
259 const sd_event_source *x = a, *y = b;
261 assert(EVENT_SOURCE_IS_TIME(x->type));
262 assert(x->type == y->type);
264 /* Enabled ones first */
265 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
267 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
270 /* Move the pending ones to the end */
271 if (!x->pending && y->pending)
273 if (x->pending && !y->pending)
277 if (x->time.next < y->time.next)
279 if (x->time.next > y->time.next)
282 /* Stability for the rest */
291 static int latest_time_prioq_compare(const void *a, const void *b) {
292 const sd_event_source *x = a, *y = b;
294 assert(EVENT_SOURCE_IS_TIME(x->type));
295 assert(x->type == y->type);
297 /* Enabled ones first */
298 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
300 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
303 /* Move the pending ones to the end */
304 if (!x->pending && y->pending)
306 if (x->pending && !y->pending)
310 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
312 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
315 /* Stability for the rest */
324 static int exit_prioq_compare(const void *a, const void *b) {
325 const sd_event_source *x = a, *y = b;
327 assert(x->type == SOURCE_EXIT);
328 assert(y->type == SOURCE_EXIT);
330 /* Enabled ones first */
331 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
333 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
336 /* Lower priority values first */
337 if (x->priority < y->priority)
339 if (x->priority > y->priority)
342 /* Stability for the rest */
351 static void free_clock_data(struct clock_data *d) {
355 prioq_free(d->earliest);
356 prioq_free(d->latest);
359 static void event_free(sd_event *e) {
364 while ((s = e->sources)) {
366 source_disconnect(s);
367 sd_event_source_unref(s);
370 assert(e->n_sources == 0);
372 if (e->default_event_ptr)
373 *(e->default_event_ptr) = NULL;
375 safe_close(e->epoll_fd);
376 safe_close(e->signal_fd);
377 safe_close(e->watchdog_fd);
379 free_clock_data(&e->realtime);
380 free_clock_data(&e->monotonic);
381 free_clock_data(&e->realtime_alarm);
382 free_clock_data(&e->boottime_alarm);
384 prioq_free(e->pending);
385 prioq_free(e->prepare);
388 free(e->signal_sources);
390 hashmap_free(e->child_sources);
391 set_free(e->post_sources);
395 _public_ int sd_event_new(sd_event** ret) {
399 assert_return(ret, -EINVAL);
401 e = new0(sd_event, 1);
406 e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
407 e->realtime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = (usec_t) -1;
408 e->original_pid = getpid();
409 e->perturb = (usec_t) -1;
411 assert_se(sigemptyset(&e->sigset) == 0);
413 e->pending = prioq_new(pending_prioq_compare);
419 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
420 if (e->epoll_fd < 0) {
433 _public_ sd_event* sd_event_ref(sd_event *e) {
434 assert_return(e, NULL);
436 assert(e->n_ref >= 1);
442 _public_ sd_event* sd_event_unref(sd_event *e) {
447 assert(e->n_ref >= 1);
456 static bool event_pid_changed(sd_event *e) {
459 /* We don't support people creating am event loop and keeping
460 * it around over a fork(). Let's complain. */
462 return e->original_pid != getpid();
465 static int source_io_unregister(sd_event_source *s) {
469 assert(s->type == SOURCE_IO);
471 if (!s->io.registered)
474 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
478 s->io.registered = false;
482 static int source_io_register(
487 struct epoll_event ev = {};
491 assert(s->type == SOURCE_IO);
492 assert(enabled != SD_EVENT_OFF);
497 if (enabled == SD_EVENT_ONESHOT)
498 ev.events |= EPOLLONESHOT;
500 if (s->io.registered)
501 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
503 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
508 s->io.registered = true;
513 static clockid_t event_source_type_to_clock(EventSourceType t) {
517 case SOURCE_TIME_REALTIME:
518 return CLOCK_REALTIME;
520 case SOURCE_TIME_MONOTONIC:
521 return CLOCK_MONOTONIC;
523 case SOURCE_TIME_REALTIME_ALARM:
524 return CLOCK_REALTIME_ALARM;
526 case SOURCE_TIME_BOOTTIME_ALARM:
527 return CLOCK_BOOTTIME_ALARM;
530 return (clockid_t) -1;
534 static EventSourceType clock_to_event_source_type(clockid_t clock) {
539 return SOURCE_TIME_REALTIME;
541 case CLOCK_MONOTONIC:
542 return SOURCE_TIME_MONOTONIC;
544 case CLOCK_REALTIME_ALARM:
545 return SOURCE_TIME_REALTIME_ALARM;
547 case CLOCK_BOOTTIME_ALARM:
548 return SOURCE_TIME_BOOTTIME_ALARM;
551 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
555 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
560 case SOURCE_TIME_REALTIME:
563 case SOURCE_TIME_MONOTONIC:
564 return &e->monotonic;
566 case SOURCE_TIME_REALTIME_ALARM:
567 return &e->realtime_alarm;
569 case SOURCE_TIME_BOOTTIME_ALARM:
570 return &e->boottime_alarm;
577 static void source_disconnect(sd_event_source *s) {
585 assert(s->event->n_sources > 0);
591 source_io_unregister(s);
595 case SOURCE_TIME_REALTIME:
596 case SOURCE_TIME_MONOTONIC:
597 case SOURCE_TIME_REALTIME_ALARM:
598 case SOURCE_TIME_BOOTTIME_ALARM: {
599 struct clock_data *d;
601 d = event_get_clock_data(s->event, s->type);
604 prioq_remove(d->earliest, s, &s->time.earliest_index);
605 prioq_remove(d->latest, s, &s->time.latest_index);
610 if (s->signal.sig > 0) {
611 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
612 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
614 if (s->event->signal_sources)
615 s->event->signal_sources[s->signal.sig] = NULL;
621 if (s->child.pid > 0) {
622 if (s->enabled != SD_EVENT_OFF) {
623 assert(s->event->n_enabled_child_sources > 0);
624 s->event->n_enabled_child_sources--;
627 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
628 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
630 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
640 set_remove(s->event->post_sources, s);
644 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
648 assert_not_reached("Wut? I shouldn't exist.");
652 prioq_remove(s->event->pending, s, &s->pending_index);
655 prioq_remove(s->event->prepare, s, &s->prepare_index);
659 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
661 LIST_REMOVE(sources, event->sources, s);
665 sd_event_unref(event);
668 static void source_free(sd_event_source *s) {
671 source_disconnect(s);
675 static int source_set_pending(sd_event_source *s, bool b) {
679 assert(s->type != SOURCE_EXIT);
687 s->pending_iteration = s->event->iteration;
689 r = prioq_put(s->event->pending, s, &s->pending_index);
695 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
697 if (EVENT_SOURCE_IS_TIME(s->type)) {
698 struct clock_data *d;
700 d = event_get_clock_data(s->event, s->type);
703 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
704 prioq_reshuffle(d->latest, s, &s->time.latest_index);
710 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
715 s = new0(sd_event_source, 1);
721 s->floating = floating;
723 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
728 LIST_PREPEND(sources, e->sources, s);
734 _public_ int sd_event_add_io(
736 sd_event_source **ret,
739 sd_event_io_handler_t callback,
745 assert_return(e, -EINVAL);
746 assert_return(fd >= 0, -EINVAL);
747 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
748 assert_return(callback, -EINVAL);
749 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
750 assert_return(!event_pid_changed(e), -ECHILD);
752 s = source_new(e, !ret, SOURCE_IO);
757 s->io.events = events;
758 s->io.callback = callback;
759 s->userdata = userdata;
760 s->enabled = SD_EVENT_ON;
762 r = source_io_register(s, s->enabled, events);
774 static void initialize_perturb(sd_event *e) {
775 sd_id128_t bootid = {};
777 /* When we sleep for longer, we try to realign the wakeup to
778 the same time wihtin each minute/second/250ms, so that
779 events all across the system can be coalesced into a single
780 CPU wakeup. However, let's take some system-specific
781 randomness for this value, so that in a network of systems
782 with synced clocks timer events are distributed a
783 bit. Here, we calculate a perturbation usec offset from the
786 if (_likely_(e->perturb != (usec_t) -1))
789 if (sd_id128_get_boot(&bootid) >= 0)
790 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
793 static int event_setup_timer_fd(
795 struct clock_data *d,
798 struct epoll_event ev = {};
804 if (_likely_(d->fd >= 0))
807 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
812 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
814 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
824 _public_ int sd_event_add_time(
826 sd_event_source **ret,
830 sd_event_time_handler_t callback,
833 EventSourceType type;
835 struct clock_data *d;
838 assert_return(e, -EINVAL);
839 assert_return(usec != (uint64_t) -1, -EINVAL);
840 assert_return(accuracy != (uint64_t) -1, -EINVAL);
841 assert_return(callback, -EINVAL);
842 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
843 assert_return(!event_pid_changed(e), -ECHILD);
845 type = clock_to_event_source_type(clock);
846 assert_return(type >= 0, -ENOTSUP);
848 d = event_get_clock_data(e, type);
852 d->earliest = prioq_new(earliest_time_prioq_compare);
858 d->latest = prioq_new(latest_time_prioq_compare);
864 r = event_setup_timer_fd(e, d, clock);
869 s = source_new(e, !ret, type);
874 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
875 s->time.callback = callback;
876 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
877 s->userdata = userdata;
878 s->enabled = SD_EVENT_ONESHOT;
880 r = prioq_put(d->earliest, s, &s->time.earliest_index);
884 r = prioq_put(d->latest, s, &s->time.latest_index);
898 static int event_update_signal_fd(sd_event *e) {
899 struct epoll_event ev = {};
905 add_to_epoll = e->signal_fd < 0;
907 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
917 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
919 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
921 e->signal_fd = safe_close(e->signal_fd);
928 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
931 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
934 _public_ int sd_event_add_signal(
936 sd_event_source **ret,
938 sd_event_signal_handler_t callback,
945 assert_return(e, -EINVAL);
946 assert_return(sig > 0, -EINVAL);
947 assert_return(sig < _NSIG, -EINVAL);
948 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
949 assert_return(!event_pid_changed(e), -ECHILD);
952 callback = signal_exit_callback;
954 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
958 if (!sigismember(&ss, sig))
961 if (!e->signal_sources) {
962 e->signal_sources = new0(sd_event_source*, _NSIG);
963 if (!e->signal_sources)
965 } else if (e->signal_sources[sig])
968 s = source_new(e, !ret, SOURCE_SIGNAL);
973 s->signal.callback = callback;
974 s->userdata = userdata;
975 s->enabled = SD_EVENT_ON;
977 e->signal_sources[sig] = s;
978 assert_se(sigaddset(&e->sigset, sig) == 0);
980 if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
981 r = event_update_signal_fd(e);
994 _public_ int sd_event_add_child(
996 sd_event_source **ret,
999 sd_event_child_handler_t callback,
1005 assert_return(e, -EINVAL);
1006 assert_return(pid > 1, -EINVAL);
1007 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1008 assert_return(options != 0, -EINVAL);
1009 assert_return(callback, -EINVAL);
1010 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1011 assert_return(!event_pid_changed(e), -ECHILD);
1013 r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func);
1017 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1020 s = source_new(e, !ret, SOURCE_CHILD);
1025 s->child.options = options;
1026 s->child.callback = callback;
1027 s->userdata = userdata;
1028 s->enabled = SD_EVENT_ONESHOT;
1030 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1036 e->n_enabled_child_sources ++;
1038 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1040 if (!e->signal_sources || !e->signal_sources[SIGCHLD]) {
1041 r = event_update_signal_fd(e);
1048 e->need_process_child = true;
1056 _public_ int sd_event_add_defer(
1058 sd_event_source **ret,
1059 sd_event_handler_t callback,
1065 assert_return(e, -EINVAL);
1066 assert_return(callback, -EINVAL);
1067 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1068 assert_return(!event_pid_changed(e), -ECHILD);
1070 s = source_new(e, !ret, SOURCE_DEFER);
1074 s->defer.callback = callback;
1075 s->userdata = userdata;
1076 s->enabled = SD_EVENT_ONESHOT;
1078 r = source_set_pending(s, true);
1090 _public_ int sd_event_add_post(
1092 sd_event_source **ret,
1093 sd_event_handler_t callback,
1099 assert_return(e, -EINVAL);
1100 assert_return(callback, -EINVAL);
1101 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1102 assert_return(!event_pid_changed(e), -ECHILD);
1104 r = set_ensure_allocated(&e->post_sources, trivial_hash_func, trivial_compare_func);
1108 s = source_new(e, !ret, SOURCE_POST);
1112 s->post.callback = callback;
1113 s->userdata = userdata;
1114 s->enabled = SD_EVENT_ON;
1116 r = set_put(e->post_sources, s);
1128 _public_ int sd_event_add_exit(
1130 sd_event_source **ret,
1131 sd_event_handler_t callback,
1137 assert_return(e, -EINVAL);
1138 assert_return(callback, -EINVAL);
1139 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1140 assert_return(!event_pid_changed(e), -ECHILD);
1143 e->exit = prioq_new(exit_prioq_compare);
1148 s = source_new(e, !ret, SOURCE_EXIT);
1152 s->exit.callback = callback;
1153 s->userdata = userdata;
1154 s->exit.prioq_index = PRIOQ_IDX_NULL;
1155 s->enabled = SD_EVENT_ONESHOT;
1157 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1169 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1170 assert_return(s, NULL);
1172 assert(s->n_ref >= 1);
1178 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1183 assert(s->n_ref >= 1);
1186 if (s->n_ref <= 0) {
1187 /* Here's a special hack: when we are called from a
1188 * dispatch handler we won't free the event source
1189 * immediately, but we will detach the fd from the
1190 * epoll. This way it is safe for the caller to unref
1191 * the event source and immediately close the fd, but
1192 * we still retain a valid event source object after
1195 if (s->dispatching) {
1196 if (s->type == SOURCE_IO)
1197 source_io_unregister(s);
1199 source_disconnect(s);
1207 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1208 assert_return(s, NULL);
1213 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1214 assert_return(s, -EINVAL);
1215 assert_return(s->type != SOURCE_EXIT, -EDOM);
1216 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1217 assert_return(!event_pid_changed(s->event), -ECHILD);
1222 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1223 assert_return(s, -EINVAL);
1224 assert_return(s->type == SOURCE_IO, -EDOM);
1225 assert_return(!event_pid_changed(s->event), -ECHILD);
1230 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1233 assert_return(s, -EINVAL);
1234 assert_return(fd >= 0, -EINVAL);
1235 assert_return(s->type == SOURCE_IO, -EDOM);
1236 assert_return(!event_pid_changed(s->event), -ECHILD);
1241 if (s->enabled == SD_EVENT_OFF) {
1243 s->io.registered = false;
1247 saved_fd = s->io.fd;
1248 assert(s->io.registered);
1251 s->io.registered = false;
1253 r = source_io_register(s, s->enabled, s->io.events);
1255 s->io.fd = saved_fd;
1256 s->io.registered = true;
1260 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1266 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1267 assert_return(s, -EINVAL);
1268 assert_return(events, -EINVAL);
1269 assert_return(s->type == SOURCE_IO, -EDOM);
1270 assert_return(!event_pid_changed(s->event), -ECHILD);
1272 *events = s->io.events;
1276 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1279 assert_return(s, -EINVAL);
1280 assert_return(s->type == SOURCE_IO, -EDOM);
1281 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1282 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1283 assert_return(!event_pid_changed(s->event), -ECHILD);
1285 if (s->io.events == events)
1288 if (s->enabled != SD_EVENT_OFF) {
1289 r = source_io_register(s, s->enabled, events);
1294 s->io.events = events;
1295 source_set_pending(s, false);
1300 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1301 assert_return(s, -EINVAL);
1302 assert_return(revents, -EINVAL);
1303 assert_return(s->type == SOURCE_IO, -EDOM);
1304 assert_return(s->pending, -ENODATA);
1305 assert_return(!event_pid_changed(s->event), -ECHILD);
1307 *revents = s->io.revents;
1311 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1312 assert_return(s, -EINVAL);
1313 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1314 assert_return(!event_pid_changed(s->event), -ECHILD);
1316 return s->signal.sig;
1319 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1320 assert_return(s, -EINVAL);
1321 assert_return(!event_pid_changed(s->event), -ECHILD);
1326 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1327 assert_return(s, -EINVAL);
1328 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1329 assert_return(!event_pid_changed(s->event), -ECHILD);
1331 if (s->priority == priority)
1334 s->priority = priority;
1337 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1340 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1342 if (s->type == SOURCE_EXIT)
1343 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1348 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1349 assert_return(s, -EINVAL);
1350 assert_return(m, -EINVAL);
1351 assert_return(!event_pid_changed(s->event), -ECHILD);
1357 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1360 assert_return(s, -EINVAL);
1361 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1362 assert_return(!event_pid_changed(s->event), -ECHILD);
1364 /* If we are dead anyway, we are fine with turning off
1365 * sources, but everything else needs to fail. */
1366 if (s->event->state == SD_EVENT_FINISHED)
1367 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1369 if (s->enabled == m)
1372 if (m == SD_EVENT_OFF) {
1377 r = source_io_unregister(s);
1384 case SOURCE_TIME_REALTIME:
1385 case SOURCE_TIME_MONOTONIC:
1386 case SOURCE_TIME_REALTIME_ALARM:
1387 case SOURCE_TIME_BOOTTIME_ALARM: {
1388 struct clock_data *d;
1391 d = event_get_clock_data(s->event, s->type);
1394 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1395 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1401 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1402 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1403 event_update_signal_fd(s->event);
1411 assert(s->event->n_enabled_child_sources > 0);
1412 s->event->n_enabled_child_sources--;
1414 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1415 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1416 event_update_signal_fd(s->event);
1423 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1432 assert_not_reached("Wut? I shouldn't exist.");
1439 r = source_io_register(s, m, s->io.events);
1446 case SOURCE_TIME_REALTIME:
1447 case SOURCE_TIME_MONOTONIC:
1448 case SOURCE_TIME_REALTIME_ALARM:
1449 case SOURCE_TIME_BOOTTIME_ALARM: {
1450 struct clock_data *d;
1453 d = event_get_clock_data(s->event, s->type);
1456 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1457 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1464 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1465 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1466 event_update_signal_fd(s->event);
1471 if (s->enabled == SD_EVENT_OFF) {
1472 s->event->n_enabled_child_sources++;
1474 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1475 assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
1476 event_update_signal_fd(s->event);
1485 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1494 assert_not_reached("Wut? I shouldn't exist.");
1499 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1502 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1507 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1508 assert_return(s, -EINVAL);
1509 assert_return(usec, -EINVAL);
1510 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1511 assert_return(!event_pid_changed(s->event), -ECHILD);
1513 *usec = s->time.next;
1517 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1518 struct clock_data *d;
1520 assert_return(s, -EINVAL);
1521 assert_return(usec != (uint64_t) -1, -EINVAL);
1522 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1523 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1524 assert_return(!event_pid_changed(s->event), -ECHILD);
1526 s->time.next = usec;
1528 source_set_pending(s, false);
1530 d = event_get_clock_data(s->event, s->type);
1533 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1534 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1539 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1540 assert_return(s, -EINVAL);
1541 assert_return(usec, -EINVAL);
1542 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1543 assert_return(!event_pid_changed(s->event), -ECHILD);
1545 *usec = s->time.accuracy;
1549 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1550 struct clock_data *d;
1552 assert_return(s, -EINVAL);
1553 assert_return(usec != (uint64_t) -1, -EINVAL);
1554 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1555 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1556 assert_return(!event_pid_changed(s->event), -ECHILD);
1559 usec = DEFAULT_ACCURACY_USEC;
1561 s->time.accuracy = usec;
1563 source_set_pending(s, false);
1565 d = event_get_clock_data(s->event, s->type);
1568 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1573 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1574 assert_return(s, -EINVAL);
1575 assert_return(clock, -EINVAL);
1576 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1577 assert_return(!event_pid_changed(s->event), -ECHILD);
1579 *clock = event_source_type_to_clock(s->type);
1583 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1584 assert_return(s, -EINVAL);
1585 assert_return(pid, -EINVAL);
1586 assert_return(s->type == SOURCE_CHILD, -EDOM);
1587 assert_return(!event_pid_changed(s->event), -ECHILD);
1589 *pid = s->child.pid;
1593 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1596 assert_return(s, -EINVAL);
1597 assert_return(s->type != SOURCE_EXIT, -EDOM);
1598 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1599 assert_return(!event_pid_changed(s->event), -ECHILD);
1601 if (s->prepare == callback)
1604 if (callback && s->prepare) {
1605 s->prepare = callback;
1609 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1613 s->prepare = callback;
1616 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1620 prioq_remove(s->event->prepare, s, &s->prepare_index);
1625 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1626 assert_return(s, NULL);
1631 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1634 assert_return(s, NULL);
1637 s->userdata = userdata;
1642 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1653 initialize_perturb(e);
1656 Find a good time to wake up again between times a and b. We
1657 have two goals here:
1659 a) We want to wake up as seldom as possible, hence prefer
1660 later times over earlier times.
1662 b) But if we have to wake up, then let's make sure to
1663 dispatch as much as possible on the entire system.
1665 We implement this by waking up everywhere at the same time
1666 within any given minute if we can, synchronised via the
1667 perturbation value determined from the boot ID. If we can't,
1668 then we try to find the same spot in every 10s, then 1s and
1669 then 250ms step. Otherwise, we pick the last possible time
1673 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1675 if (_unlikely_(c < USEC_PER_MINUTE))
1678 c -= USEC_PER_MINUTE;
1684 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1686 if (_unlikely_(c < USEC_PER_SEC*10))
1689 c -= USEC_PER_SEC*10;
1695 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1697 if (_unlikely_(c < USEC_PER_SEC))
1706 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1708 if (_unlikely_(c < USEC_PER_MSEC*250))
1711 c -= USEC_PER_MSEC*250;
1720 static int event_arm_timer(
1722 struct clock_data *d) {
1724 struct itimerspec its = {};
1725 sd_event_source *a, *b;
1732 a = prioq_peek(d->earliest);
1733 if (!a || a->enabled == SD_EVENT_OFF) {
1738 if (d->next == (usec_t) -1)
1742 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1746 d->next = (usec_t) -1;
1750 b = prioq_peek(d->latest);
1751 assert_se(b && b->enabled != SD_EVENT_OFF);
1753 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1757 assert_se(d->fd >= 0);
1760 /* We don' want to disarm here, just mean some time looooong ago. */
1761 its.it_value.tv_sec = 0;
1762 its.it_value.tv_nsec = 1;
1764 timespec_store(&its.it_value, t);
1766 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1774 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1777 assert(s->type == SOURCE_IO);
1779 /* If the event source was already pending, we just OR in the
1780 * new revents, otherwise we reset the value. The ORing is
1781 * necessary to handle EPOLLONESHOT events properly where
1782 * readability might happen independently of writability, and
1783 * we need to keep track of both */
1786 s->io.revents |= revents;
1788 s->io.revents = revents;
1790 return source_set_pending(s, true);
1793 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1800 assert_return(events == EPOLLIN, -EIO);
1802 ss = read(fd, &x, sizeof(x));
1804 if (errno == EAGAIN || errno == EINTR)
1810 if (_unlikely_(ss != sizeof(x)))
1814 *next = (usec_t) -1;
1819 static int process_timer(
1822 struct clock_data *d) {
1831 s = prioq_peek(d->earliest);
1834 s->enabled == SD_EVENT_OFF ||
1838 r = source_set_pending(s, true);
1842 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1843 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1849 static int process_child(sd_event *e) {
1856 e->need_process_child = false;
1859 So, this is ugly. We iteratively invoke waitid() with P_PID
1860 + WNOHANG for each PID we wait for, instead of using
1861 P_ALL. This is because we only want to get child
1862 information of very specific child processes, and not all
1863 of them. We might not have processed the SIGCHLD even of a
1864 previous invocation and we don't want to maintain a
1865 unbounded *per-child* event queue, hence we really don't
1866 want anything flushed out of the kernel's queue that we
1867 don't care about. Since this is O(n) this means that if you
1868 have a lot of processes you probably want to handle SIGCHLD
1871 We do not reap the children here (by using WNOWAIT), this
1872 is only done after the event source is dispatched so that
1873 the callback still sees the process as a zombie.
1876 HASHMAP_FOREACH(s, e->child_sources, i) {
1877 assert(s->type == SOURCE_CHILD);
1882 if (s->enabled == SD_EVENT_OFF)
1885 zero(s->child.siginfo);
1886 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1887 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
1891 if (s->child.siginfo.si_pid != 0) {
1893 s->child.siginfo.si_code == CLD_EXITED ||
1894 s->child.siginfo.si_code == CLD_KILLED ||
1895 s->child.siginfo.si_code == CLD_DUMPED;
1897 if (!zombie && (s->child.options & WEXITED)) {
1898 /* If the child isn't dead then let's
1899 * immediately remove the state change
1900 * from the queue, since there's no
1901 * benefit in leaving it queued */
1903 assert(s->child.options & (WSTOPPED|WCONTINUED));
1904 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
1907 r = source_set_pending(s, true);
1916 static int process_signal(sd_event *e, uint32_t events) {
1917 bool read_one = false;
1921 assert(e->signal_sources);
1923 assert_return(events == EPOLLIN, -EIO);
1926 struct signalfd_siginfo si;
1930 ss = read(e->signal_fd, &si, sizeof(si));
1932 if (errno == EAGAIN || errno == EINTR)
1938 if (_unlikely_(ss != sizeof(si)))
1943 s = e->signal_sources[si.ssi_signo];
1944 if (si.ssi_signo == SIGCHLD) {
1945 r = process_child(e);
1954 s->signal.siginfo = si;
1955 r = source_set_pending(s, true);
1961 static int source_dispatch(sd_event_source *s) {
1965 assert(s->pending || s->type == SOURCE_EXIT);
1967 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
1968 r = source_set_pending(s, false);
1973 if (s->type != SOURCE_POST) {
1977 /* If we execute a non-post source, let's mark all
1978 * post sources as pending */
1980 SET_FOREACH(z, s->event->post_sources, i) {
1981 if (z->enabled == SD_EVENT_OFF)
1984 r = source_set_pending(z, true);
1990 if (s->enabled == SD_EVENT_ONESHOT) {
1991 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
1996 s->dispatching = true;
2001 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2004 case SOURCE_TIME_REALTIME:
2005 case SOURCE_TIME_MONOTONIC:
2006 case SOURCE_TIME_REALTIME_ALARM:
2007 case SOURCE_TIME_BOOTTIME_ALARM:
2008 r = s->time.callback(s, s->time.next, s->userdata);
2012 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2015 case SOURCE_CHILD: {
2018 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2019 s->child.siginfo.si_code == CLD_KILLED ||
2020 s->child.siginfo.si_code == CLD_DUMPED;
2022 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2024 /* Now, reap the PID for good. */
2026 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2032 r = s->defer.callback(s, s->userdata);
2036 r = s->post.callback(s, s->userdata);
2040 r = s->exit.callback(s, s->userdata);
2043 case SOURCE_WATCHDOG:
2044 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2045 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2046 assert_not_reached("Wut? I shouldn't exist.");
2049 s->dispatching = false;
2052 log_debug("Event source %p returned error, disabling: %s", s, strerror(-r));
2057 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2062 static int event_prepare(sd_event *e) {
2070 s = prioq_peek(e->prepare);
2071 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2074 s->prepare_iteration = e->iteration;
2075 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2081 s->dispatching = true;
2082 r = s->prepare(s, s->userdata);
2083 s->dispatching = false;
2086 log_debug("Prepare callback of event source %p returned error, disabling: %s", s, strerror(-r));
2091 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2097 static int dispatch_exit(sd_event *e) {
2103 p = prioq_peek(e->exit);
2104 if (!p || p->enabled == SD_EVENT_OFF) {
2105 e->state = SD_EVENT_FINISHED;
2111 e->state = SD_EVENT_EXITING;
2113 r = source_dispatch(p);
2115 e->state = SD_EVENT_PASSIVE;
2121 static sd_event_source* event_next_pending(sd_event *e) {
2126 p = prioq_peek(e->pending);
2130 if (p->enabled == SD_EVENT_OFF)
2136 static int arm_watchdog(sd_event *e) {
2137 struct itimerspec its = {};
2142 assert(e->watchdog_fd >= 0);
2144 t = sleep_between(e,
2145 e->watchdog_last + (e->watchdog_period / 2),
2146 e->watchdog_last + (e->watchdog_period * 3 / 4));
2148 timespec_store(&its.it_value, t);
2150 /* Make sure we never set the watchdog to 0, which tells the
2151 * kernel to disable it. */
2152 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2153 its.it_value.tv_nsec = 1;
2155 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2162 static int process_watchdog(sd_event *e) {
2168 /* Don't notify watchdog too often */
2169 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2172 sd_notify(false, "WATCHDOG=1");
2173 e->watchdog_last = e->timestamp.monotonic;
2175 return arm_watchdog(e);
2178 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2179 struct epoll_event *ev_queue;
2180 unsigned ev_queue_max;
2185 assert_return(e, -EINVAL);
2186 assert_return(!event_pid_changed(e), -ECHILD);
2187 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2188 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2190 if (e->exit_requested)
2191 return dispatch_exit(e);
2195 e->state = SD_EVENT_RUNNING;
2197 r = event_prepare(e);
2201 r = event_arm_timer(e, &e->realtime);
2205 r = event_arm_timer(e, &e->monotonic);
2209 r = event_arm_timer(e, &e->realtime_alarm);
2213 r = event_arm_timer(e, &e->boottime_alarm);
2217 if (event_next_pending(e) || e->need_process_child)
2220 ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2221 ev_queue = newa(struct epoll_event, ev_queue_max);
2223 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2224 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2226 r = errno == EAGAIN || errno == EINTR ? 1 : -errno;
2232 dual_timestamp_get(&e->timestamp);
2233 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2235 for (i = 0; i < m; i++) {
2237 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2238 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2239 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2240 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2241 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2242 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2243 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2244 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2245 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2246 r = process_signal(e, ev_queue[i].events);
2247 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2248 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2250 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2256 r = process_watchdog(e);
2260 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2264 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2268 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2272 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2276 if (e->need_process_child) {
2277 r = process_child(e);
2282 p = event_next_pending(e);
2288 r = source_dispatch(p);
2291 e->state = SD_EVENT_PASSIVE;
2297 _public_ int sd_event_loop(sd_event *e) {
2300 assert_return(e, -EINVAL);
2301 assert_return(!event_pid_changed(e), -ECHILD);
2302 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2306 while (e->state != SD_EVENT_FINISHED) {
2307 r = sd_event_run(e, (uint64_t) -1);
2319 _public_ int sd_event_get_state(sd_event *e) {
2320 assert_return(e, -EINVAL);
2321 assert_return(!event_pid_changed(e), -ECHILD);
2326 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2327 assert_return(e, -EINVAL);
2328 assert_return(code, -EINVAL);
2329 assert_return(!event_pid_changed(e), -ECHILD);
2331 if (!e->exit_requested)
2334 *code = e->exit_code;
2338 _public_ int sd_event_exit(sd_event *e, int code) {
2339 assert_return(e, -EINVAL);
2340 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2341 assert_return(!event_pid_changed(e), -ECHILD);
2343 e->exit_requested = true;
2344 e->exit_code = code;
2349 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2350 assert_return(e, -EINVAL);
2351 assert_return(usec, -EINVAL);
2352 assert_return(!event_pid_changed(e), -ECHILD);
2354 /* If we haven't run yet, just get the actual time */
2355 if (!dual_timestamp_is_set(&e->timestamp))
2360 case CLOCK_REALTIME:
2361 case CLOCK_REALTIME_ALARM:
2362 *usec = e->timestamp.realtime;
2365 case CLOCK_MONOTONIC:
2366 *usec = e->timestamp.monotonic;
2369 case CLOCK_BOOTTIME_ALARM:
2370 *usec = e->timestamp_boottime;
2377 _public_ int sd_event_default(sd_event **ret) {
2379 static thread_local sd_event *default_event = NULL;
2384 return !!default_event;
2386 if (default_event) {
2387 *ret = sd_event_ref(default_event);
2391 r = sd_event_new(&e);
2395 e->default_event_ptr = &default_event;
2403 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2404 assert_return(e, -EINVAL);
2405 assert_return(tid, -EINVAL);
2406 assert_return(!event_pid_changed(e), -ECHILD);
2416 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2419 assert_return(e, -EINVAL);
2420 assert_return(!event_pid_changed(e), -ECHILD);
2422 if (e->watchdog == !!b)
2426 struct epoll_event ev = {};
2428 r = sd_watchdog_enabled(false, &e->watchdog_period);
2432 /* Issue first ping immediately */
2433 sd_notify(false, "WATCHDOG=1");
2434 e->watchdog_last = now(CLOCK_MONOTONIC);
2436 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2437 if (e->watchdog_fd < 0)
2440 r = arm_watchdog(e);
2444 ev.events = EPOLLIN;
2445 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2447 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2454 if (e->watchdog_fd >= 0) {
2455 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2456 e->watchdog_fd = safe_close(e->watchdog_fd);
2464 e->watchdog_fd = safe_close(e->watchdog_fd);
2468 _public_ int sd_event_get_watchdog(sd_event *e) {
2469 assert_return(e, -EINVAL);
2470 assert_return(!event_pid_changed(e), -ECHILD);