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"
39 #define EPOLL_QUEUE_MAX 512U
40 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
42 typedef enum EventSourceType {
45 SOURCE_TIME_MONOTONIC,
46 SOURCE_TIME_REALTIME_ALARM,
47 SOURCE_TIME_BOOTTIME_ALARM,
54 _SOUFCE_EVENT_SOURCE_TYPE_MAX,
55 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
58 #define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
60 struct sd_event_source {
65 sd_event_handler_t prepare;
67 EventSourceType type:5;
73 unsigned pending_index;
74 unsigned prepare_index;
75 unsigned pending_iteration;
76 unsigned prepare_iteration;
80 sd_event_io_handler_t callback;
87 sd_event_time_handler_t callback;
88 usec_t next, accuracy;
89 unsigned earliest_index;
90 unsigned latest_index;
93 sd_event_signal_handler_t callback;
94 struct signalfd_siginfo siginfo;
98 sd_event_child_handler_t callback;
104 sd_event_handler_t callback;
107 sd_event_handler_t callback;
110 sd_event_handler_t callback;
111 unsigned prioq_index;
119 /* For all clocks we maintain two priority queues each, one
120 * ordered for the earliest times the events may be
121 * dispatched, and one ordered by the latest times they must
122 * have been dispatched. The range between the top entries in
123 * the two prioqs is the time window we can freely schedule
141 /* timerfd_create() only supports these four clocks so far. We
142 * can add support for more clocks when the kernel learns to
143 * deal with them, too. */
144 struct clock_data realtime;
145 struct clock_data monotonic;
146 struct clock_data realtime_alarm;
147 struct clock_data boottime_alarm;
152 sd_event_source **signal_sources;
154 Hashmap *child_sources;
155 unsigned n_enabled_child_sources;
164 dual_timestamp timestamp;
165 usec_t timestamp_boottime;
168 bool exit_requested:1;
169 bool need_process_child:1;
175 sd_event **default_event_ptr;
177 usec_t watchdog_last, watchdog_period;
182 static int pending_prioq_compare(const void *a, const void *b) {
183 const sd_event_source *x = a, *y = b;
188 /* Enabled ones first */
189 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
191 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
194 /* Lower priority values first */
195 if (x->priority < y->priority)
197 if (x->priority > y->priority)
200 /* Older entries first */
201 if (x->pending_iteration < y->pending_iteration)
203 if (x->pending_iteration > y->pending_iteration)
206 /* Stability for the rest */
215 static int prepare_prioq_compare(const void *a, const void *b) {
216 const sd_event_source *x = a, *y = b;
221 /* Move most recently prepared ones last, so that we can stop
222 * preparing as soon as we hit one that has already been
223 * prepared in the current iteration */
224 if (x->prepare_iteration < y->prepare_iteration)
226 if (x->prepare_iteration > y->prepare_iteration)
229 /* Enabled ones first */
230 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
232 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
235 /* Lower priority values first */
236 if (x->priority < y->priority)
238 if (x->priority > y->priority)
241 /* Stability for the rest */
250 static int earliest_time_prioq_compare(const void *a, const void *b) {
251 const sd_event_source *x = a, *y = b;
253 assert(EVENT_SOURCE_IS_TIME(x->type));
254 assert(x->type == y->type);
256 /* Enabled ones first */
257 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
259 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
262 /* Move the pending ones to the end */
263 if (!x->pending && y->pending)
265 if (x->pending && !y->pending)
269 if (x->time.next < y->time.next)
271 if (x->time.next > y->time.next)
274 /* Stability for the rest */
283 static int latest_time_prioq_compare(const void *a, const void *b) {
284 const sd_event_source *x = a, *y = b;
286 assert(EVENT_SOURCE_IS_TIME(x->type));
287 assert(x->type == y->type);
289 /* Enabled ones first */
290 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
292 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
295 /* Move the pending ones to the end */
296 if (!x->pending && y->pending)
298 if (x->pending && !y->pending)
302 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
304 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
307 /* Stability for the rest */
316 static int exit_prioq_compare(const void *a, const void *b) {
317 const sd_event_source *x = a, *y = b;
319 assert(x->type == SOURCE_EXIT);
320 assert(y->type == SOURCE_EXIT);
322 /* Enabled ones first */
323 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
325 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
328 /* Lower priority values first */
329 if (x->priority < y->priority)
331 if (x->priority > y->priority)
334 /* Stability for the rest */
343 static void free_clock_data(struct clock_data *d) {
347 prioq_free(d->earliest);
348 prioq_free(d->latest);
351 static void event_free(sd_event *e) {
353 assert(e->n_sources == 0);
355 if (e->default_event_ptr)
356 *(e->default_event_ptr) = NULL;
358 safe_close(e->epoll_fd);
359 safe_close(e->signal_fd);
360 safe_close(e->watchdog_fd);
362 free_clock_data(&e->realtime);
363 free_clock_data(&e->monotonic);
364 free_clock_data(&e->realtime_alarm);
365 free_clock_data(&e->boottime_alarm);
367 prioq_free(e->pending);
368 prioq_free(e->prepare);
371 free(e->signal_sources);
373 hashmap_free(e->child_sources);
374 set_free(e->post_sources);
378 _public_ int sd_event_new(sd_event** ret) {
382 assert_return(ret, -EINVAL);
384 e = new0(sd_event, 1);
389 e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
390 e->realtime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = (usec_t) -1;
391 e->original_pid = getpid();
392 e->perturb = (usec_t) -1;
394 assert_se(sigemptyset(&e->sigset) == 0);
396 e->pending = prioq_new(pending_prioq_compare);
402 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
403 if (e->epoll_fd < 0) {
416 _public_ sd_event* sd_event_ref(sd_event *e) {
417 assert_return(e, NULL);
419 assert(e->n_ref >= 1);
425 _public_ sd_event* sd_event_unref(sd_event *e) {
430 assert(e->n_ref >= 1);
439 static bool event_pid_changed(sd_event *e) {
442 /* We don't support people creating am event loop and keeping
443 * it around over a fork(). Let's complain. */
445 return e->original_pid != getpid();
448 static int source_io_unregister(sd_event_source *s) {
452 assert(s->type == SOURCE_IO);
454 if (!s->io.registered)
457 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
461 s->io.registered = false;
465 static int source_io_register(
470 struct epoll_event ev = {};
474 assert(s->type == SOURCE_IO);
475 assert(enabled != SD_EVENT_OFF);
480 if (enabled == SD_EVENT_ONESHOT)
481 ev.events |= EPOLLONESHOT;
483 if (s->io.registered)
484 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
486 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
491 s->io.registered = true;
496 static clockid_t event_source_type_to_clock(EventSourceType t) {
500 case SOURCE_TIME_REALTIME:
501 return CLOCK_REALTIME;
503 case SOURCE_TIME_MONOTONIC:
504 return CLOCK_MONOTONIC;
506 case SOURCE_TIME_REALTIME_ALARM:
507 return CLOCK_REALTIME_ALARM;
509 case SOURCE_TIME_BOOTTIME_ALARM:
510 return CLOCK_BOOTTIME_ALARM;
513 return (clockid_t) -1;
517 static EventSourceType clock_to_event_source_type(clockid_t clock) {
522 return SOURCE_TIME_REALTIME;
524 case CLOCK_MONOTONIC:
525 return SOURCE_TIME_MONOTONIC;
527 case CLOCK_REALTIME_ALARM:
528 return SOURCE_TIME_REALTIME_ALARM;
530 case CLOCK_BOOTTIME_ALARM:
531 return SOURCE_TIME_BOOTTIME_ALARM;
534 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
538 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
543 case SOURCE_TIME_REALTIME:
546 case SOURCE_TIME_MONOTONIC:
547 return &e->monotonic;
549 case SOURCE_TIME_REALTIME_ALARM:
550 return &e->realtime_alarm;
552 case SOURCE_TIME_BOOTTIME_ALARM:
553 return &e->boottime_alarm;
560 static void source_free(sd_event_source *s) {
564 assert(s->event->n_sources > 0);
570 source_io_unregister(s);
574 case SOURCE_TIME_REALTIME:
575 case SOURCE_TIME_MONOTONIC:
576 case SOURCE_TIME_REALTIME_ALARM:
577 case SOURCE_TIME_BOOTTIME_ALARM: {
578 struct clock_data *d;
580 d = event_get_clock_data(s->event, s->type);
583 prioq_remove(d->earliest, s, &s->time.earliest_index);
584 prioq_remove(d->latest, s, &s->time.latest_index);
589 if (s->signal.sig > 0) {
590 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
591 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
593 if (s->event->signal_sources)
594 s->event->signal_sources[s->signal.sig] = NULL;
600 if (s->child.pid > 0) {
601 if (s->enabled != SD_EVENT_OFF) {
602 assert(s->event->n_enabled_child_sources > 0);
603 s->event->n_enabled_child_sources--;
606 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
607 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
609 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
619 set_remove(s->event->post_sources, s);
623 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
627 assert_not_reached("Wut? I shouldn't exist.");
631 prioq_remove(s->event->pending, s, &s->pending_index);
634 prioq_remove(s->event->prepare, s, &s->prepare_index);
636 s->event->n_sources--;
637 sd_event_unref(s->event);
643 static int source_set_pending(sd_event_source *s, bool b) {
647 assert(s->type != SOURCE_EXIT);
655 s->pending_iteration = s->event->iteration;
657 r = prioq_put(s->event->pending, s, &s->pending_index);
663 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
665 if (EVENT_SOURCE_IS_TIME(s->type)) {
666 struct clock_data *d;
668 d = event_get_clock_data(s->event, s->type);
671 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
672 prioq_reshuffle(d->latest, s, &s->time.latest_index);
678 static sd_event_source *source_new(sd_event *e, EventSourceType type) {
683 s = new0(sd_event_source, 1);
688 s->event = sd_event_ref(e);
690 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
697 _public_ int sd_event_add_io(
699 sd_event_source **ret,
702 sd_event_io_handler_t callback,
708 assert_return(e, -EINVAL);
709 assert_return(fd >= 0, -EINVAL);
710 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
711 assert_return(callback, -EINVAL);
712 assert_return(ret, -EINVAL);
713 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
714 assert_return(!event_pid_changed(e), -ECHILD);
716 s = source_new(e, SOURCE_IO);
721 s->io.events = events;
722 s->io.callback = callback;
723 s->userdata = userdata;
724 s->enabled = SD_EVENT_ON;
726 r = source_io_register(s, s->enabled, events);
736 static void initialize_perturb(sd_event *e) {
737 sd_id128_t bootid = {};
739 /* When we sleep for longer, we try to realign the wakeup to
740 the same time wihtin each minute/second/250ms, so that
741 events all across the system can be coalesced into a single
742 CPU wakeup. However, let's take some system-specific
743 randomness for this value, so that in a network of systems
744 with synced clocks timer events are distributed a
745 bit. Here, we calculate a perturbation usec offset from the
748 if (_likely_(e->perturb != (usec_t) -1))
751 if (sd_id128_get_boot(&bootid) >= 0)
752 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
755 static int event_setup_timer_fd(
757 struct clock_data *d,
760 struct epoll_event ev = {};
766 if (_likely_(d->fd >= 0))
769 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
774 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
776 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
786 _public_ int sd_event_add_time(
788 sd_event_source **ret,
792 sd_event_time_handler_t callback,
795 EventSourceType type;
797 struct clock_data *d;
800 assert_return(e, -EINVAL);
801 assert_return(ret, -EINVAL);
802 assert_return(usec != (uint64_t) -1, -EINVAL);
803 assert_return(accuracy != (uint64_t) -1, -EINVAL);
804 assert_return(callback, -EINVAL);
805 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
806 assert_return(!event_pid_changed(e), -ECHILD);
808 type = clock_to_event_source_type(clock);
809 assert_return(type >= 0, -ENOTSUP);
811 d = event_get_clock_data(e, type);
815 d->earliest = prioq_new(earliest_time_prioq_compare);
821 d->latest = prioq_new(latest_time_prioq_compare);
827 r = event_setup_timer_fd(e, d, clock);
832 s = source_new(e, type);
837 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
838 s->time.callback = callback;
839 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
840 s->userdata = userdata;
841 s->enabled = SD_EVENT_ONESHOT;
843 r = prioq_put(d->earliest, s, &s->time.earliest_index);
847 r = prioq_put(d->latest, s, &s->time.latest_index);
859 static int event_update_signal_fd(sd_event *e) {
860 struct epoll_event ev = {};
866 add_to_epoll = e->signal_fd < 0;
868 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
878 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
880 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
882 e->signal_fd = safe_close(e->signal_fd);
889 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
892 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
895 _public_ int sd_event_add_signal(
897 sd_event_source **ret,
899 sd_event_signal_handler_t callback,
906 assert_return(e, -EINVAL);
907 assert_return(sig > 0, -EINVAL);
908 assert_return(sig < _NSIG, -EINVAL);
909 assert_return(ret, -EINVAL);
910 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
911 assert_return(!event_pid_changed(e), -ECHILD);
914 callback = signal_exit_callback;
916 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
920 if (!sigismember(&ss, sig))
923 if (!e->signal_sources) {
924 e->signal_sources = new0(sd_event_source*, _NSIG);
925 if (!e->signal_sources)
927 } else if (e->signal_sources[sig])
930 s = source_new(e, SOURCE_SIGNAL);
935 s->signal.callback = callback;
936 s->userdata = userdata;
937 s->enabled = SD_EVENT_ON;
939 e->signal_sources[sig] = s;
940 assert_se(sigaddset(&e->sigset, sig) == 0);
942 if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
943 r = event_update_signal_fd(e);
954 _public_ int sd_event_add_child(
956 sd_event_source **ret,
959 sd_event_child_handler_t callback,
965 assert_return(e, -EINVAL);
966 assert_return(pid > 1, -EINVAL);
967 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
968 assert_return(options != 0, -EINVAL);
969 assert_return(callback, -EINVAL);
970 assert_return(ret, -EINVAL);
971 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
972 assert_return(!event_pid_changed(e), -ECHILD);
974 r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func);
978 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
981 s = source_new(e, SOURCE_CHILD);
986 s->child.options = options;
987 s->child.callback = callback;
988 s->userdata = userdata;
989 s->enabled = SD_EVENT_ONESHOT;
991 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
997 e->n_enabled_child_sources ++;
999 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1001 if (!e->signal_sources || !e->signal_sources[SIGCHLD]) {
1002 r = event_update_signal_fd(e);
1009 e->need_process_child = true;
1015 _public_ int sd_event_add_defer(
1017 sd_event_source **ret,
1018 sd_event_handler_t callback,
1024 assert_return(e, -EINVAL);
1025 assert_return(callback, -EINVAL);
1026 assert_return(ret, -EINVAL);
1027 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1028 assert_return(!event_pid_changed(e), -ECHILD);
1030 s = source_new(e, SOURCE_DEFER);
1034 s->defer.callback = callback;
1035 s->userdata = userdata;
1036 s->enabled = SD_EVENT_ONESHOT;
1038 r = source_set_pending(s, true);
1048 _public_ int sd_event_add_post(
1050 sd_event_source **ret,
1051 sd_event_handler_t callback,
1057 assert_return(e, -EINVAL);
1058 assert_return(callback, -EINVAL);
1059 assert_return(ret, -EINVAL);
1060 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1061 assert_return(!event_pid_changed(e), -ECHILD);
1063 r = set_ensure_allocated(&e->post_sources, trivial_hash_func, trivial_compare_func);
1067 s = source_new(e, SOURCE_POST);
1071 s->post.callback = callback;
1072 s->userdata = userdata;
1073 s->enabled = SD_EVENT_ON;
1075 r = set_put(e->post_sources, s);
1085 _public_ int sd_event_add_exit(
1087 sd_event_source **ret,
1088 sd_event_handler_t callback,
1094 assert_return(e, -EINVAL);
1095 assert_return(callback, -EINVAL);
1096 assert_return(ret, -EINVAL);
1097 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1098 assert_return(!event_pid_changed(e), -ECHILD);
1101 e->exit = prioq_new(exit_prioq_compare);
1106 s = source_new(e, SOURCE_EXIT);
1110 s->exit.callback = callback;
1111 s->userdata = userdata;
1112 s->exit.prioq_index = PRIOQ_IDX_NULL;
1113 s->enabled = SD_EVENT_ONESHOT;
1115 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1125 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1126 assert_return(s, NULL);
1128 assert(s->n_ref >= 1);
1134 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1139 assert(s->n_ref >= 1);
1142 if (s->n_ref <= 0) {
1143 /* Here's a special hack: when we are called from a
1144 * dispatch handler we won't free the event source
1145 * immediately, but we will detach the fd from the
1146 * epoll. This way it is safe for the caller to unref
1147 * the event source and immediately close the fd, but
1148 * we still retain a valid event source object after
1151 if (s->dispatching) {
1152 if (s->type == SOURCE_IO)
1153 source_io_unregister(s);
1161 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1162 assert_return(s, NULL);
1167 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1168 assert_return(s, -EINVAL);
1169 assert_return(s->type != SOURCE_EXIT, -EDOM);
1170 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1171 assert_return(!event_pid_changed(s->event), -ECHILD);
1176 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1177 assert_return(s, -EINVAL);
1178 assert_return(s->type == SOURCE_IO, -EDOM);
1179 assert_return(!event_pid_changed(s->event), -ECHILD);
1184 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1187 assert_return(s, -EINVAL);
1188 assert_return(fd >= 0, -EINVAL);
1189 assert_return(s->type == SOURCE_IO, -EDOM);
1190 assert_return(!event_pid_changed(s->event), -ECHILD);
1195 if (s->enabled == SD_EVENT_OFF) {
1197 s->io.registered = false;
1201 saved_fd = s->io.fd;
1202 assert(s->io.registered);
1205 s->io.registered = false;
1207 r = source_io_register(s, s->enabled, s->io.events);
1209 s->io.fd = saved_fd;
1210 s->io.registered = true;
1214 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1220 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1221 assert_return(s, -EINVAL);
1222 assert_return(events, -EINVAL);
1223 assert_return(s->type == SOURCE_IO, -EDOM);
1224 assert_return(!event_pid_changed(s->event), -ECHILD);
1226 *events = s->io.events;
1230 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1233 assert_return(s, -EINVAL);
1234 assert_return(s->type == SOURCE_IO, -EDOM);
1235 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1236 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1237 assert_return(!event_pid_changed(s->event), -ECHILD);
1239 if (s->io.events == events)
1242 if (s->enabled != SD_EVENT_OFF) {
1243 r = source_io_register(s, s->enabled, events);
1248 s->io.events = events;
1249 source_set_pending(s, false);
1254 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1255 assert_return(s, -EINVAL);
1256 assert_return(revents, -EINVAL);
1257 assert_return(s->type == SOURCE_IO, -EDOM);
1258 assert_return(s->pending, -ENODATA);
1259 assert_return(!event_pid_changed(s->event), -ECHILD);
1261 *revents = s->io.revents;
1265 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1266 assert_return(s, -EINVAL);
1267 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1268 assert_return(!event_pid_changed(s->event), -ECHILD);
1270 return s->signal.sig;
1273 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1274 assert_return(s, -EINVAL);
1275 assert_return(!event_pid_changed(s->event), -ECHILD);
1280 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1281 assert_return(s, -EINVAL);
1282 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1283 assert_return(!event_pid_changed(s->event), -ECHILD);
1285 if (s->priority == priority)
1288 s->priority = priority;
1291 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1294 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1296 if (s->type == SOURCE_EXIT)
1297 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1302 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1303 assert_return(s, -EINVAL);
1304 assert_return(m, -EINVAL);
1305 assert_return(!event_pid_changed(s->event), -ECHILD);
1311 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1314 assert_return(s, -EINVAL);
1315 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1316 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1317 assert_return(!event_pid_changed(s->event), -ECHILD);
1319 if (s->enabled == m)
1322 if (m == SD_EVENT_OFF) {
1327 r = source_io_unregister(s);
1334 case SOURCE_TIME_REALTIME:
1335 case SOURCE_TIME_MONOTONIC:
1336 case SOURCE_TIME_REALTIME_ALARM:
1337 case SOURCE_TIME_BOOTTIME_ALARM: {
1338 struct clock_data *d;
1341 d = event_get_clock_data(s->event, s->type);
1344 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1345 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1351 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1352 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1353 event_update_signal_fd(s->event);
1361 assert(s->event->n_enabled_child_sources > 0);
1362 s->event->n_enabled_child_sources--;
1364 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1365 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1366 event_update_signal_fd(s->event);
1373 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1382 assert_not_reached("Wut? I shouldn't exist.");
1389 r = source_io_register(s, m, s->io.events);
1396 case SOURCE_TIME_REALTIME:
1397 case SOURCE_TIME_MONOTONIC:
1398 case SOURCE_TIME_REALTIME_ALARM:
1399 case SOURCE_TIME_BOOTTIME_ALARM: {
1400 struct clock_data *d;
1403 d = event_get_clock_data(s->event, s->type);
1406 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1407 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1414 if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
1415 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1416 event_update_signal_fd(s->event);
1421 if (s->enabled == SD_EVENT_OFF) {
1422 s->event->n_enabled_child_sources++;
1424 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
1425 assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
1426 event_update_signal_fd(s->event);
1435 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1444 assert_not_reached("Wut? I shouldn't exist.");
1449 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1452 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1457 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1458 assert_return(s, -EINVAL);
1459 assert_return(usec, -EINVAL);
1460 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1461 assert_return(!event_pid_changed(s->event), -ECHILD);
1463 *usec = s->time.next;
1467 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1468 struct clock_data *d;
1470 assert_return(s, -EINVAL);
1471 assert_return(usec != (uint64_t) -1, -EINVAL);
1472 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1473 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1474 assert_return(!event_pid_changed(s->event), -ECHILD);
1476 s->time.next = usec;
1478 source_set_pending(s, false);
1480 d = event_get_clock_data(s->event, s->type);
1483 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1484 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1489 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1490 assert_return(s, -EINVAL);
1491 assert_return(usec, -EINVAL);
1492 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1493 assert_return(!event_pid_changed(s->event), -ECHILD);
1495 *usec = s->time.accuracy;
1499 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1500 struct clock_data *d;
1502 assert_return(s, -EINVAL);
1503 assert_return(usec != (uint64_t) -1, -EINVAL);
1504 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1505 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1506 assert_return(!event_pid_changed(s->event), -ECHILD);
1509 usec = DEFAULT_ACCURACY_USEC;
1511 s->time.accuracy = usec;
1513 source_set_pending(s, false);
1515 d = event_get_clock_data(s->event, s->type);
1518 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1523 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1524 assert_return(s, -EINVAL);
1525 assert_return(clock, -EINVAL);
1526 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1527 assert_return(!event_pid_changed(s->event), -ECHILD);
1529 *clock = event_source_type_to_clock(s->type);
1533 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1534 assert_return(s, -EINVAL);
1535 assert_return(pid, -EINVAL);
1536 assert_return(s->type == SOURCE_CHILD, -EDOM);
1537 assert_return(!event_pid_changed(s->event), -ECHILD);
1539 *pid = s->child.pid;
1543 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1546 assert_return(s, -EINVAL);
1547 assert_return(s->type != SOURCE_EXIT, -EDOM);
1548 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1549 assert_return(!event_pid_changed(s->event), -ECHILD);
1551 if (s->prepare == callback)
1554 if (callback && s->prepare) {
1555 s->prepare = callback;
1559 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1563 s->prepare = callback;
1566 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1570 prioq_remove(s->event->prepare, s, &s->prepare_index);
1575 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1576 assert_return(s, NULL);
1581 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1584 assert_return(s, NULL);
1587 s->userdata = userdata;
1592 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1603 initialize_perturb(e);
1606 Find a good time to wake up again between times a and b. We
1607 have two goals here:
1609 a) We want to wake up as seldom as possible, hence prefer
1610 later times over earlier times.
1612 b) But if we have to wake up, then let's make sure to
1613 dispatch as much as possible on the entire system.
1615 We implement this by waking up everywhere at the same time
1616 within any given minute if we can, synchronised via the
1617 perturbation value determined from the boot ID. If we can't,
1618 then we try to find the same spot in every 10s, then 1s and
1619 then 250ms step. Otherwise, we pick the last possible time
1623 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1625 if (_unlikely_(c < USEC_PER_MINUTE))
1628 c -= USEC_PER_MINUTE;
1634 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1636 if (_unlikely_(c < USEC_PER_SEC*10))
1639 c -= USEC_PER_SEC*10;
1645 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1647 if (_unlikely_(c < USEC_PER_SEC))
1656 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1658 if (_unlikely_(c < USEC_PER_MSEC*250))
1661 c -= USEC_PER_MSEC*250;
1670 static int event_arm_timer(
1672 struct clock_data *d) {
1674 struct itimerspec its = {};
1675 sd_event_source *a, *b;
1682 a = prioq_peek(d->earliest);
1683 if (!a || a->enabled == SD_EVENT_OFF) {
1688 if (d->next == (usec_t) -1)
1692 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1696 d->next = (usec_t) -1;
1700 b = prioq_peek(d->latest);
1701 assert_se(b && b->enabled != SD_EVENT_OFF);
1703 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1707 assert_se(d->fd >= 0);
1710 /* We don' want to disarm here, just mean some time looooong ago. */
1711 its.it_value.tv_sec = 0;
1712 its.it_value.tv_nsec = 1;
1714 timespec_store(&its.it_value, t);
1716 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1724 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1727 assert(s->type == SOURCE_IO);
1729 /* If the event source was already pending, we just OR in the
1730 * new revents, otherwise we reset the value. The ORing is
1731 * necessary to handle EPOLLONESHOT events properly where
1732 * readability might happen independently of writability, and
1733 * we need to keep track of both */
1736 s->io.revents |= revents;
1738 s->io.revents = revents;
1740 return source_set_pending(s, true);
1743 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1750 assert_return(events == EPOLLIN, -EIO);
1752 ss = read(fd, &x, sizeof(x));
1754 if (errno == EAGAIN || errno == EINTR)
1760 if (_unlikely_(ss != sizeof(x)))
1764 *next = (usec_t) -1;
1769 static int process_timer(
1772 struct clock_data *d) {
1781 s = prioq_peek(d->earliest);
1784 s->enabled == SD_EVENT_OFF ||
1788 r = source_set_pending(s, true);
1792 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1793 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1799 static int process_child(sd_event *e) {
1806 e->need_process_child = false;
1809 So, this is ugly. We iteratively invoke waitid() with P_PID
1810 + WNOHANG for each PID we wait for, instead of using
1811 P_ALL. This is because we only want to get child
1812 information of very specific child processes, and not all
1813 of them. We might not have processed the SIGCHLD even of a
1814 previous invocation and we don't want to maintain a
1815 unbounded *per-child* event queue, hence we really don't
1816 want anything flushed out of the kernel's queue that we
1817 don't care about. Since this is O(n) this means that if you
1818 have a lot of processes you probably want to handle SIGCHLD
1821 We do not reap the children here (by using WNOWAIT), this
1822 is only done after the event source is dispatched so that
1823 the callback still sees the process as a zombie.
1826 HASHMAP_FOREACH(s, e->child_sources, i) {
1827 assert(s->type == SOURCE_CHILD);
1832 if (s->enabled == SD_EVENT_OFF)
1835 zero(s->child.siginfo);
1836 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1837 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
1841 if (s->child.siginfo.si_pid != 0) {
1843 s->child.siginfo.si_code == CLD_EXITED ||
1844 s->child.siginfo.si_code == CLD_KILLED ||
1845 s->child.siginfo.si_code == CLD_DUMPED;
1847 if (!zombie && (s->child.options & WEXITED)) {
1848 /* If the child isn't dead then let's
1849 * immediately remove the state change
1850 * from the queue, since there's no
1851 * benefit in leaving it queued */
1853 assert(s->child.options & (WSTOPPED|WCONTINUED));
1854 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
1857 r = source_set_pending(s, true);
1866 static int process_signal(sd_event *e, uint32_t events) {
1867 bool read_one = false;
1871 assert(e->signal_sources);
1873 assert_return(events == EPOLLIN, -EIO);
1876 struct signalfd_siginfo si;
1880 ss = read(e->signal_fd, &si, sizeof(si));
1882 if (errno == EAGAIN || errno == EINTR)
1888 if (_unlikely_(ss != sizeof(si)))
1893 s = e->signal_sources[si.ssi_signo];
1894 if (si.ssi_signo == SIGCHLD) {
1895 r = process_child(e);
1904 s->signal.siginfo = si;
1905 r = source_set_pending(s, true);
1911 static int source_dispatch(sd_event_source *s) {
1915 assert(s->pending || s->type == SOURCE_EXIT);
1917 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
1918 r = source_set_pending(s, false);
1923 if (s->type != SOURCE_POST) {
1927 /* If we execute a non-post source, let's mark all
1928 * post sources as pending */
1930 SET_FOREACH(z, s->event->post_sources, i) {
1931 if (z->enabled == SD_EVENT_OFF)
1934 r = source_set_pending(z, true);
1940 if (s->enabled == SD_EVENT_ONESHOT) {
1941 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
1946 s->dispatching = true;
1951 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
1954 case SOURCE_TIME_REALTIME:
1955 case SOURCE_TIME_MONOTONIC:
1956 case SOURCE_TIME_REALTIME_ALARM:
1957 case SOURCE_TIME_BOOTTIME_ALARM:
1958 r = s->time.callback(s, s->time.next, s->userdata);
1962 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
1965 case SOURCE_CHILD: {
1968 zombie = s->child.siginfo.si_code == CLD_EXITED ||
1969 s->child.siginfo.si_code == CLD_KILLED ||
1970 s->child.siginfo.si_code == CLD_DUMPED;
1972 r = s->child.callback(s, &s->child.siginfo, s->userdata);
1974 /* Now, reap the PID for good. */
1976 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
1982 r = s->defer.callback(s, s->userdata);
1986 r = s->post.callback(s, s->userdata);
1990 r = s->exit.callback(s, s->userdata);
1993 case SOURCE_WATCHDOG:
1994 case _SOUFCE_EVENT_SOURCE_TYPE_MAX:
1995 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
1996 assert_not_reached("Wut? I shouldn't exist.");
1999 s->dispatching = false;
2002 log_debug("Event source %p returned error, disabling: %s", s, strerror(-r));
2007 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2012 static int event_prepare(sd_event *e) {
2020 s = prioq_peek(e->prepare);
2021 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2024 s->prepare_iteration = e->iteration;
2025 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2031 s->dispatching = true;
2032 r = s->prepare(s, s->userdata);
2033 s->dispatching = false;
2036 log_debug("Prepare callback of event source %p returned error, disabling: %s", s, strerror(-r));
2041 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2047 static int dispatch_exit(sd_event *e) {
2053 p = prioq_peek(e->exit);
2054 if (!p || p->enabled == SD_EVENT_OFF) {
2055 e->state = SD_EVENT_FINISHED;
2061 e->state = SD_EVENT_EXITING;
2063 r = source_dispatch(p);
2065 e->state = SD_EVENT_PASSIVE;
2071 static sd_event_source* event_next_pending(sd_event *e) {
2076 p = prioq_peek(e->pending);
2080 if (p->enabled == SD_EVENT_OFF)
2086 static int arm_watchdog(sd_event *e) {
2087 struct itimerspec its = {};
2092 assert(e->watchdog_fd >= 0);
2094 t = sleep_between(e,
2095 e->watchdog_last + (e->watchdog_period / 2),
2096 e->watchdog_last + (e->watchdog_period * 3 / 4));
2098 timespec_store(&its.it_value, t);
2100 /* Make sure we never set the watchdog to 0, which tells the
2101 * kernel to disable it. */
2102 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2103 its.it_value.tv_nsec = 1;
2105 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2112 static int process_watchdog(sd_event *e) {
2118 /* Don't notify watchdog too often */
2119 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2122 sd_notify(false, "WATCHDOG=1");
2123 e->watchdog_last = e->timestamp.monotonic;
2125 return arm_watchdog(e);
2128 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2129 struct epoll_event *ev_queue;
2130 unsigned ev_queue_max;
2134 assert_return(e, -EINVAL);
2135 assert_return(!event_pid_changed(e), -ECHILD);
2136 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2137 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2139 if (e->exit_requested)
2140 return dispatch_exit(e);
2144 e->state = SD_EVENT_RUNNING;
2146 r = event_prepare(e);
2150 r = event_arm_timer(e, &e->realtime);
2154 r = event_arm_timer(e, &e->monotonic);
2158 r = event_arm_timer(e, &e->realtime_alarm);
2162 r = event_arm_timer(e, &e->boottime_alarm);
2166 if (event_next_pending(e) || e->need_process_child)
2169 ev_queue_max = CLAMP(e->n_sources, 1U, EPOLL_QUEUE_MAX);
2170 ev_queue = newa(struct epoll_event, ev_queue_max);
2172 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2173 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2175 r = errno == EAGAIN || errno == EINTR ? 1 : -errno;
2179 dual_timestamp_get(&e->timestamp);
2180 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2182 for (i = 0; i < m; i++) {
2184 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2185 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2186 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2187 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2188 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2189 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2190 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2191 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2192 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2193 r = process_signal(e, ev_queue[i].events);
2194 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2195 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2197 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2203 r = process_watchdog(e);
2207 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2211 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2215 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2219 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2223 if (e->need_process_child) {
2224 r = process_child(e);
2229 p = event_next_pending(e);
2235 r = source_dispatch(p);
2238 e->state = SD_EVENT_PASSIVE;
2244 _public_ int sd_event_loop(sd_event *e) {
2247 assert_return(e, -EINVAL);
2248 assert_return(!event_pid_changed(e), -ECHILD);
2249 assert_return(e->state == SD_EVENT_PASSIVE, -EBUSY);
2253 while (e->state != SD_EVENT_FINISHED) {
2254 r = sd_event_run(e, (uint64_t) -1);
2266 _public_ int sd_event_get_state(sd_event *e) {
2267 assert_return(e, -EINVAL);
2268 assert_return(!event_pid_changed(e), -ECHILD);
2273 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2274 assert_return(e, -EINVAL);
2275 assert_return(code, -EINVAL);
2276 assert_return(!event_pid_changed(e), -ECHILD);
2278 if (!e->exit_requested)
2281 *code = e->exit_code;
2285 _public_ int sd_event_exit(sd_event *e, int code) {
2286 assert_return(e, -EINVAL);
2287 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2288 assert_return(!event_pid_changed(e), -ECHILD);
2290 e->exit_requested = true;
2291 e->exit_code = code;
2296 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2297 assert_return(e, -EINVAL);
2298 assert_return(usec, -EINVAL);
2299 assert_return(!event_pid_changed(e), -ECHILD);
2301 /* If we haven't run yet, just get the actual time */
2302 if (!dual_timestamp_is_set(&e->timestamp))
2307 case CLOCK_REALTIME:
2308 case CLOCK_REALTIME_ALARM:
2309 *usec = e->timestamp.realtime;
2312 case CLOCK_MONOTONIC:
2313 *usec = e->timestamp.monotonic;
2316 case CLOCK_BOOTTIME_ALARM:
2317 *usec = e->timestamp_boottime;
2324 _public_ int sd_event_default(sd_event **ret) {
2326 static thread_local sd_event *default_event = NULL;
2331 return !!default_event;
2333 if (default_event) {
2334 *ret = sd_event_ref(default_event);
2338 r = sd_event_new(&e);
2342 e->default_event_ptr = &default_event;
2350 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2351 assert_return(e, -EINVAL);
2352 assert_return(tid, -EINVAL);
2353 assert_return(!event_pid_changed(e), -ECHILD);
2363 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2366 assert_return(e, -EINVAL);
2367 assert_return(!event_pid_changed(e), -ECHILD);
2369 if (e->watchdog == !!b)
2373 struct epoll_event ev = {};
2375 r = sd_watchdog_enabled(false, &e->watchdog_period);
2379 /* Issue first ping immediately */
2380 sd_notify(false, "WATCHDOG=1");
2381 e->watchdog_last = now(CLOCK_MONOTONIC);
2383 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2384 if (e->watchdog_fd < 0)
2387 r = arm_watchdog(e);
2391 ev.events = EPOLLIN;
2392 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2394 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2401 if (e->watchdog_fd >= 0) {
2402 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2403 e->watchdog_fd = safe_close(e->watchdog_fd);
2411 e->watchdog_fd = safe_close(e->watchdog_fd);
2415 _public_ int sd_event_get_watchdog(sd_event *e) {
2416 assert_return(e, -EINVAL);
2417 assert_return(!event_pid_changed(e), -ECHILD);