1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
27 #include "sd-daemon.h"
32 #include "time-util.h"
36 #include "signal-util.h"
40 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
42 typedef enum EventSourceType {
46 SOURCE_TIME_MONOTONIC,
47 SOURCE_TIME_REALTIME_ALARM,
48 SOURCE_TIME_BOOTTIME_ALARM,
55 _SOURCE_EVENT_SOURCE_TYPE_MAX,
56 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
59 #define EVENT_SOURCE_IS_TIME(t) IN_SET((t), SOURCE_TIME_REALTIME, SOURCE_TIME_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
61 struct sd_event_source {
66 sd_event_handler_t prepare;
70 EventSourceType type:5;
77 unsigned pending_index;
78 unsigned prepare_index;
79 unsigned pending_iteration;
80 unsigned prepare_iteration;
82 LIST_FIELDS(sd_event_source, sources);
86 sd_event_io_handler_t callback;
93 sd_event_time_handler_t callback;
94 usec_t next, accuracy;
95 unsigned earliest_index;
96 unsigned latest_index;
99 sd_event_signal_handler_t callback;
100 struct signalfd_siginfo siginfo;
104 sd_event_child_handler_t callback;
110 sd_event_handler_t callback;
113 sd_event_handler_t callback;
116 sd_event_handler_t callback;
117 unsigned prioq_index;
125 /* For all clocks we maintain two priority queues each, one
126 * ordered for the earliest times the events may be
127 * dispatched, and one ordered by the latest times they must
128 * have been dispatched. The range between the top entries in
129 * the two prioqs is the time window we can freely schedule
149 /* timerfd_create() only supports these five clocks so far. We
150 * can add support for more clocks when the kernel learns to
151 * deal with them, too. */
152 struct clock_data realtime;
153 struct clock_data boottime;
154 struct clock_data monotonic;
155 struct clock_data realtime_alarm;
156 struct clock_data boottime_alarm;
161 sd_event_source **signal_sources;
163 Hashmap *child_sources;
164 unsigned n_enabled_child_sources;
173 dual_timestamp timestamp;
174 usec_t timestamp_boottime;
177 bool exit_requested:1;
178 bool need_process_child:1;
184 sd_event **default_event_ptr;
186 usec_t watchdog_last, watchdog_period;
190 LIST_HEAD(sd_event_source, sources);
193 static void source_disconnect(sd_event_source *s);
195 static int pending_prioq_compare(const void *a, const void *b) {
196 const sd_event_source *x = a, *y = b;
201 /* Enabled ones first */
202 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
204 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
207 /* Lower priority values first */
208 if (x->priority < y->priority)
210 if (x->priority > y->priority)
213 /* Older entries first */
214 if (x->pending_iteration < y->pending_iteration)
216 if (x->pending_iteration > y->pending_iteration)
219 /* Stability for the rest */
228 static int prepare_prioq_compare(const void *a, const void *b) {
229 const sd_event_source *x = a, *y = b;
234 /* Move most recently prepared ones last, so that we can stop
235 * preparing as soon as we hit one that has already been
236 * prepared in the current iteration */
237 if (x->prepare_iteration < y->prepare_iteration)
239 if (x->prepare_iteration > y->prepare_iteration)
242 /* Enabled ones first */
243 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
245 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
248 /* Lower priority values first */
249 if (x->priority < y->priority)
251 if (x->priority > y->priority)
254 /* Stability for the rest */
263 static int earliest_time_prioq_compare(const void *a, const void *b) {
264 const sd_event_source *x = a, *y = b;
266 assert(EVENT_SOURCE_IS_TIME(x->type));
267 assert(x->type == y->type);
269 /* Enabled ones first */
270 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
272 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
275 /* Move the pending ones to the end */
276 if (!x->pending && y->pending)
278 if (x->pending && !y->pending)
282 if (x->time.next < y->time.next)
284 if (x->time.next > y->time.next)
287 /* Stability for the rest */
296 static int latest_time_prioq_compare(const void *a, const void *b) {
297 const sd_event_source *x = a, *y = b;
299 assert(EVENT_SOURCE_IS_TIME(x->type));
300 assert(x->type == y->type);
302 /* Enabled ones first */
303 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
305 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
308 /* Move the pending ones to the end */
309 if (!x->pending && y->pending)
311 if (x->pending && !y->pending)
315 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
317 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
320 /* Stability for the rest */
329 static int exit_prioq_compare(const void *a, const void *b) {
330 const sd_event_source *x = a, *y = b;
332 assert(x->type == SOURCE_EXIT);
333 assert(y->type == SOURCE_EXIT);
335 /* Enabled ones first */
336 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
338 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
341 /* Lower priority values first */
342 if (x->priority < y->priority)
344 if (x->priority > y->priority)
347 /* Stability for the rest */
356 static void free_clock_data(struct clock_data *d) {
360 prioq_free(d->earliest);
361 prioq_free(d->latest);
364 static void event_free(sd_event *e) {
369 while ((s = e->sources)) {
371 source_disconnect(s);
372 sd_event_source_unref(s);
375 assert(e->n_sources == 0);
377 if (e->default_event_ptr)
378 *(e->default_event_ptr) = NULL;
380 safe_close(e->epoll_fd);
381 safe_close(e->signal_fd);
382 safe_close(e->watchdog_fd);
384 free_clock_data(&e->realtime);
385 free_clock_data(&e->boottime);
386 free_clock_data(&e->monotonic);
387 free_clock_data(&e->realtime_alarm);
388 free_clock_data(&e->boottime_alarm);
390 prioq_free(e->pending);
391 prioq_free(e->prepare);
394 free(e->signal_sources);
396 hashmap_free(e->child_sources);
397 set_free(e->post_sources);
401 _public_ int sd_event_new(sd_event** ret) {
405 assert_return(ret, -EINVAL);
407 e = new0(sd_event, 1);
412 e->signal_fd = e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
413 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
414 e->original_pid = getpid();
415 e->perturb = USEC_INFINITY;
417 assert_se(sigemptyset(&e->sigset) == 0);
419 e->pending = prioq_new(pending_prioq_compare);
425 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
426 if (e->epoll_fd < 0) {
439 _public_ sd_event* sd_event_ref(sd_event *e) {
440 assert_return(e, NULL);
442 assert(e->n_ref >= 1);
448 _public_ sd_event* sd_event_unref(sd_event *e) {
453 assert(e->n_ref >= 1);
462 static bool event_pid_changed(sd_event *e) {
465 /* We don't support people creating an event loop and keeping
466 * it around over a fork(). Let's complain. */
468 return e->original_pid != getpid();
471 static int source_io_unregister(sd_event_source *s) {
475 assert(s->type == SOURCE_IO);
477 if (event_pid_changed(s->event))
480 if (!s->io.registered)
483 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
487 s->io.registered = false;
491 static int source_io_register(
496 struct epoll_event ev = {};
500 assert(s->type == SOURCE_IO);
501 assert(enabled != SD_EVENT_OFF);
506 if (enabled == SD_EVENT_ONESHOT)
507 ev.events |= EPOLLONESHOT;
509 if (s->io.registered)
510 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
512 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
517 s->io.registered = true;
522 static clockid_t event_source_type_to_clock(EventSourceType t) {
526 case SOURCE_TIME_REALTIME:
527 return CLOCK_REALTIME;
529 case SOURCE_TIME_BOOTTIME:
530 return CLOCK_BOOTTIME;
532 case SOURCE_TIME_MONOTONIC:
533 return CLOCK_MONOTONIC;
535 case SOURCE_TIME_REALTIME_ALARM:
536 return CLOCK_REALTIME_ALARM;
538 case SOURCE_TIME_BOOTTIME_ALARM:
539 return CLOCK_BOOTTIME_ALARM;
542 return (clockid_t) -1;
546 static EventSourceType clock_to_event_source_type(clockid_t clock) {
551 return SOURCE_TIME_REALTIME;
554 return SOURCE_TIME_BOOTTIME;
556 case CLOCK_MONOTONIC:
557 return SOURCE_TIME_MONOTONIC;
559 case CLOCK_REALTIME_ALARM:
560 return SOURCE_TIME_REALTIME_ALARM;
562 case CLOCK_BOOTTIME_ALARM:
563 return SOURCE_TIME_BOOTTIME_ALARM;
566 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
570 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
575 case SOURCE_TIME_REALTIME:
578 case SOURCE_TIME_BOOTTIME:
581 case SOURCE_TIME_MONOTONIC:
582 return &e->monotonic;
584 case SOURCE_TIME_REALTIME_ALARM:
585 return &e->realtime_alarm;
587 case SOURCE_TIME_BOOTTIME_ALARM:
588 return &e->boottime_alarm;
595 static bool need_signal(sd_event *e, int signal) {
596 return (e->signal_sources && e->signal_sources[signal] &&
597 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
599 (signal == SIGCHLD &&
600 e->n_enabled_child_sources > 0);
603 static int event_update_signal_fd(sd_event *e) {
604 struct epoll_event ev = {};
610 if (event_pid_changed(e))
613 add_to_epoll = e->signal_fd < 0;
615 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
625 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
627 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
629 e->signal_fd = safe_close(e->signal_fd);
636 static void source_disconnect(sd_event_source *s) {
644 assert(s->event->n_sources > 0);
650 source_io_unregister(s);
654 case SOURCE_TIME_REALTIME:
655 case SOURCE_TIME_BOOTTIME:
656 case SOURCE_TIME_MONOTONIC:
657 case SOURCE_TIME_REALTIME_ALARM:
658 case SOURCE_TIME_BOOTTIME_ALARM: {
659 struct clock_data *d;
661 d = event_get_clock_data(s->event, s->type);
664 prioq_remove(d->earliest, s, &s->time.earliest_index);
665 prioq_remove(d->latest, s, &s->time.latest_index);
666 d->needs_rearm = true;
671 if (s->signal.sig > 0) {
672 if (s->event->signal_sources)
673 s->event->signal_sources[s->signal.sig] = NULL;
675 /* If the signal was on and now it is off... */
676 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
677 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
679 (void) event_update_signal_fd(s->event);
680 /* If disabling failed, we might get a spurious event,
681 * but otherwise nothing bad should happen. */
688 if (s->child.pid > 0) {
689 if (s->enabled != SD_EVENT_OFF) {
690 assert(s->event->n_enabled_child_sources > 0);
691 s->event->n_enabled_child_sources--;
693 /* We know the signal was on, if it is off now... */
694 if (!need_signal(s->event, SIGCHLD)) {
695 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
697 (void) event_update_signal_fd(s->event);
698 /* If disabling failed, we might get a spurious event,
699 * but otherwise nothing bad should happen. */
703 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
713 set_remove(s->event->post_sources, s);
717 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
721 assert_not_reached("Wut? I shouldn't exist.");
725 prioq_remove(s->event->pending, s, &s->pending_index);
728 prioq_remove(s->event->prepare, s, &s->prepare_index);
732 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
734 LIST_REMOVE(sources, event->sources, s);
738 sd_event_unref(event);
741 static void source_free(sd_event_source *s) {
744 source_disconnect(s);
745 free(s->description);
749 static int source_set_pending(sd_event_source *s, bool b) {
753 assert(s->type != SOURCE_EXIT);
761 s->pending_iteration = s->event->iteration;
763 r = prioq_put(s->event->pending, s, &s->pending_index);
769 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
771 if (EVENT_SOURCE_IS_TIME(s->type)) {
772 struct clock_data *d;
774 d = event_get_clock_data(s->event, s->type);
777 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
778 prioq_reshuffle(d->latest, s, &s->time.latest_index);
779 d->needs_rearm = true;
785 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
790 s = new0(sd_event_source, 1);
796 s->floating = floating;
798 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
803 LIST_PREPEND(sources, e->sources, s);
809 _public_ int sd_event_add_io(
811 sd_event_source **ret,
814 sd_event_io_handler_t callback,
820 assert_return(e, -EINVAL);
821 assert_return(fd >= 0, -EINVAL);
822 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
823 assert_return(callback, -EINVAL);
824 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
825 assert_return(!event_pid_changed(e), -ECHILD);
827 s = source_new(e, !ret, SOURCE_IO);
832 s->io.events = events;
833 s->io.callback = callback;
834 s->userdata = userdata;
835 s->enabled = SD_EVENT_ON;
837 r = source_io_register(s, s->enabled, events);
849 static void initialize_perturb(sd_event *e) {
850 sd_id128_t bootid = {};
852 /* When we sleep for longer, we try to realign the wakeup to
853 the same time wihtin each minute/second/250ms, so that
854 events all across the system can be coalesced into a single
855 CPU wakeup. However, let's take some system-specific
856 randomness for this value, so that in a network of systems
857 with synced clocks timer events are distributed a
858 bit. Here, we calculate a perturbation usec offset from the
861 if (_likely_(e->perturb != USEC_INFINITY))
864 if (sd_id128_get_boot(&bootid) >= 0)
865 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
868 static int event_setup_timer_fd(
870 struct clock_data *d,
873 struct epoll_event ev = {};
879 if (_likely_(d->fd >= 0))
882 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
887 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
889 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
899 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
902 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
905 _public_ int sd_event_add_time(
907 sd_event_source **ret,
911 sd_event_time_handler_t callback,
914 EventSourceType type;
916 struct clock_data *d;
919 assert_return(e, -EINVAL);
920 assert_return(usec != (uint64_t) -1, -EINVAL);
921 assert_return(accuracy != (uint64_t) -1, -EINVAL);
922 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
923 assert_return(!event_pid_changed(e), -ECHILD);
926 callback = time_exit_callback;
928 type = clock_to_event_source_type(clock);
929 assert_return(type >= 0, -EOPNOTSUPP);
931 d = event_get_clock_data(e, type);
935 d->earliest = prioq_new(earliest_time_prioq_compare);
941 d->latest = prioq_new(latest_time_prioq_compare);
947 r = event_setup_timer_fd(e, d, clock);
952 s = source_new(e, !ret, type);
957 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
958 s->time.callback = callback;
959 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
960 s->userdata = userdata;
961 s->enabled = SD_EVENT_ONESHOT;
963 d->needs_rearm = true;
965 r = prioq_put(d->earliest, s, &s->time.earliest_index);
969 r = prioq_put(d->latest, s, &s->time.latest_index);
983 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
986 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
989 _public_ int sd_event_add_signal(
991 sd_event_source **ret,
993 sd_event_signal_handler_t callback,
1001 assert_return(e, -EINVAL);
1002 assert_return(sig > 0, -EINVAL);
1003 assert_return(sig < _NSIG, -EINVAL);
1004 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1005 assert_return(!event_pid_changed(e), -ECHILD);
1008 callback = signal_exit_callback;
1010 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1014 if (!sigismember(&ss, sig))
1017 if (!e->signal_sources) {
1018 e->signal_sources = new0(sd_event_source*, _NSIG);
1019 if (!e->signal_sources)
1021 } else if (e->signal_sources[sig])
1024 previous = need_signal(e, sig);
1026 s = source_new(e, !ret, SOURCE_SIGNAL);
1030 s->signal.sig = sig;
1031 s->signal.callback = callback;
1032 s->userdata = userdata;
1033 s->enabled = SD_EVENT_ON;
1035 e->signal_sources[sig] = s;
1038 assert_se(sigaddset(&e->sigset, sig) == 0);
1040 r = event_update_signal_fd(e);
1047 /* Use the signal name as description for the event source by default */
1048 (void) sd_event_source_set_description(s, signal_to_string(sig));
1056 _public_ int sd_event_add_child(
1058 sd_event_source **ret,
1061 sd_event_child_handler_t callback,
1068 assert_return(e, -EINVAL);
1069 assert_return(pid > 1, -EINVAL);
1070 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1071 assert_return(options != 0, -EINVAL);
1072 assert_return(callback, -EINVAL);
1073 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1074 assert_return(!event_pid_changed(e), -ECHILD);
1076 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1080 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1083 previous = need_signal(e, SIGCHLD);
1085 s = source_new(e, !ret, SOURCE_CHILD);
1090 s->child.options = options;
1091 s->child.callback = callback;
1092 s->userdata = userdata;
1093 s->enabled = SD_EVENT_ONESHOT;
1095 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1101 e->n_enabled_child_sources ++;
1104 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1106 r = event_update_signal_fd(e);
1113 e->need_process_child = true;
1121 _public_ int sd_event_add_defer(
1123 sd_event_source **ret,
1124 sd_event_handler_t callback,
1130 assert_return(e, -EINVAL);
1131 assert_return(callback, -EINVAL);
1132 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1133 assert_return(!event_pid_changed(e), -ECHILD);
1135 s = source_new(e, !ret, SOURCE_DEFER);
1139 s->defer.callback = callback;
1140 s->userdata = userdata;
1141 s->enabled = SD_EVENT_ONESHOT;
1143 r = source_set_pending(s, true);
1155 _public_ int sd_event_add_post(
1157 sd_event_source **ret,
1158 sd_event_handler_t callback,
1164 assert_return(e, -EINVAL);
1165 assert_return(callback, -EINVAL);
1166 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1167 assert_return(!event_pid_changed(e), -ECHILD);
1169 r = set_ensure_allocated(&e->post_sources, NULL);
1173 s = source_new(e, !ret, SOURCE_POST);
1177 s->post.callback = callback;
1178 s->userdata = userdata;
1179 s->enabled = SD_EVENT_ON;
1181 r = set_put(e->post_sources, s);
1193 _public_ int sd_event_add_exit(
1195 sd_event_source **ret,
1196 sd_event_handler_t callback,
1202 assert_return(e, -EINVAL);
1203 assert_return(callback, -EINVAL);
1204 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1205 assert_return(!event_pid_changed(e), -ECHILD);
1208 e->exit = prioq_new(exit_prioq_compare);
1213 s = source_new(e, !ret, SOURCE_EXIT);
1217 s->exit.callback = callback;
1218 s->userdata = userdata;
1219 s->exit.prioq_index = PRIOQ_IDX_NULL;
1220 s->enabled = SD_EVENT_ONESHOT;
1222 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1234 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1235 assert_return(s, NULL);
1237 assert(s->n_ref >= 1);
1243 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1248 assert(s->n_ref >= 1);
1251 if (s->n_ref <= 0) {
1252 /* Here's a special hack: when we are called from a
1253 * dispatch handler we won't free the event source
1254 * immediately, but we will detach the fd from the
1255 * epoll. This way it is safe for the caller to unref
1256 * the event source and immediately close the fd, but
1257 * we still retain a valid event source object after
1260 if (s->dispatching) {
1261 if (s->type == SOURCE_IO)
1262 source_io_unregister(s);
1264 source_disconnect(s);
1272 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1273 assert_return(s, -EINVAL);
1274 assert_return(!event_pid_changed(s->event), -ECHILD);
1276 return free_and_strdup(&s->description, description);
1279 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1280 assert_return(s, -EINVAL);
1281 assert_return(description, -EINVAL);
1282 assert_return(s->description, -ENXIO);
1283 assert_return(!event_pid_changed(s->event), -ECHILD);
1285 *description = s->description;
1289 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1290 assert_return(s, NULL);
1295 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1296 assert_return(s, -EINVAL);
1297 assert_return(s->type != SOURCE_EXIT, -EDOM);
1298 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1299 assert_return(!event_pid_changed(s->event), -ECHILD);
1304 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1305 assert_return(s, -EINVAL);
1306 assert_return(s->type == SOURCE_IO, -EDOM);
1307 assert_return(!event_pid_changed(s->event), -ECHILD);
1312 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1315 assert_return(s, -EINVAL);
1316 assert_return(fd >= 0, -EINVAL);
1317 assert_return(s->type == SOURCE_IO, -EDOM);
1318 assert_return(!event_pid_changed(s->event), -ECHILD);
1323 if (s->enabled == SD_EVENT_OFF) {
1325 s->io.registered = false;
1329 saved_fd = s->io.fd;
1330 assert(s->io.registered);
1333 s->io.registered = false;
1335 r = source_io_register(s, s->enabled, s->io.events);
1337 s->io.fd = saved_fd;
1338 s->io.registered = true;
1342 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1348 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1349 assert_return(s, -EINVAL);
1350 assert_return(events, -EINVAL);
1351 assert_return(s->type == SOURCE_IO, -EDOM);
1352 assert_return(!event_pid_changed(s->event), -ECHILD);
1354 *events = s->io.events;
1358 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1361 assert_return(s, -EINVAL);
1362 assert_return(s->type == SOURCE_IO, -EDOM);
1363 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1364 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1365 assert_return(!event_pid_changed(s->event), -ECHILD);
1367 /* edge-triggered updates are never skipped, so we can reset edges */
1368 if (s->io.events == events && !(events & EPOLLET))
1371 if (s->enabled != SD_EVENT_OFF) {
1372 r = source_io_register(s, s->enabled, events);
1377 s->io.events = events;
1378 source_set_pending(s, false);
1383 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1384 assert_return(s, -EINVAL);
1385 assert_return(revents, -EINVAL);
1386 assert_return(s->type == SOURCE_IO, -EDOM);
1387 assert_return(s->pending, -ENODATA);
1388 assert_return(!event_pid_changed(s->event), -ECHILD);
1390 *revents = s->io.revents;
1394 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1395 assert_return(s, -EINVAL);
1396 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1397 assert_return(!event_pid_changed(s->event), -ECHILD);
1399 return s->signal.sig;
1402 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1403 assert_return(s, -EINVAL);
1404 assert_return(!event_pid_changed(s->event), -ECHILD);
1409 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1410 assert_return(s, -EINVAL);
1411 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1412 assert_return(!event_pid_changed(s->event), -ECHILD);
1414 if (s->priority == priority)
1417 s->priority = priority;
1420 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1423 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1425 if (s->type == SOURCE_EXIT)
1426 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1431 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1432 assert_return(s, -EINVAL);
1433 assert_return(m, -EINVAL);
1434 assert_return(!event_pid_changed(s->event), -ECHILD);
1440 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1443 assert_return(s, -EINVAL);
1444 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1445 assert_return(!event_pid_changed(s->event), -ECHILD);
1447 /* If we are dead anyway, we are fine with turning off
1448 * sources, but everything else needs to fail. */
1449 if (s->event->state == SD_EVENT_FINISHED)
1450 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1452 if (s->enabled == m)
1455 if (m == SD_EVENT_OFF) {
1460 r = source_io_unregister(s);
1467 case SOURCE_TIME_REALTIME:
1468 case SOURCE_TIME_BOOTTIME:
1469 case SOURCE_TIME_MONOTONIC:
1470 case SOURCE_TIME_REALTIME_ALARM:
1471 case SOURCE_TIME_BOOTTIME_ALARM: {
1472 struct clock_data *d;
1475 d = event_get_clock_data(s->event, s->type);
1478 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1479 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1480 d->needs_rearm = true;
1485 assert(need_signal(s->event, s->signal.sig));
1489 if (!need_signal(s->event, s->signal.sig)) {
1490 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1492 (void) event_update_signal_fd(s->event);
1493 /* If disabling failed, we might get a spurious event,
1494 * but otherwise nothing bad should happen. */
1500 assert(need_signal(s->event, SIGCHLD));
1504 assert(s->event->n_enabled_child_sources > 0);
1505 s->event->n_enabled_child_sources--;
1507 if (!need_signal(s->event, SIGCHLD)) {
1508 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1510 (void) event_update_signal_fd(s->event);
1517 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1526 assert_not_reached("Wut? I shouldn't exist.");
1533 r = source_io_register(s, m, s->io.events);
1540 case SOURCE_TIME_REALTIME:
1541 case SOURCE_TIME_BOOTTIME:
1542 case SOURCE_TIME_MONOTONIC:
1543 case SOURCE_TIME_REALTIME_ALARM:
1544 case SOURCE_TIME_BOOTTIME_ALARM: {
1545 struct clock_data *d;
1548 d = event_get_clock_data(s->event, s->type);
1551 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1552 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1553 d->needs_rearm = true;
1558 /* Check status before enabling. */
1559 if (!need_signal(s->event, s->signal.sig)) {
1560 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1562 r = event_update_signal_fd(s->event);
1564 s->enabled = SD_EVENT_OFF;
1573 /* Check status before enabling. */
1574 if (s->enabled == SD_EVENT_OFF) {
1575 if (!need_signal(s->event, SIGCHLD)) {
1576 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1578 r = event_update_signal_fd(s->event);
1580 s->enabled = SD_EVENT_OFF;
1585 s->event->n_enabled_child_sources++;
1593 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1602 assert_not_reached("Wut? I shouldn't exist.");
1607 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1610 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1615 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1616 assert_return(s, -EINVAL);
1617 assert_return(usec, -EINVAL);
1618 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1619 assert_return(!event_pid_changed(s->event), -ECHILD);
1621 *usec = s->time.next;
1625 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1626 struct clock_data *d;
1628 assert_return(s, -EINVAL);
1629 assert_return(usec != (uint64_t) -1, -EINVAL);
1630 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1631 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1632 assert_return(!event_pid_changed(s->event), -ECHILD);
1634 s->time.next = usec;
1636 source_set_pending(s, false);
1638 d = event_get_clock_data(s->event, s->type);
1641 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1642 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1643 d->needs_rearm = true;
1648 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1649 assert_return(s, -EINVAL);
1650 assert_return(usec, -EINVAL);
1651 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1652 assert_return(!event_pid_changed(s->event), -ECHILD);
1654 *usec = s->time.accuracy;
1658 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1659 struct clock_data *d;
1661 assert_return(s, -EINVAL);
1662 assert_return(usec != (uint64_t) -1, -EINVAL);
1663 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1664 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1665 assert_return(!event_pid_changed(s->event), -ECHILD);
1668 usec = DEFAULT_ACCURACY_USEC;
1670 s->time.accuracy = usec;
1672 source_set_pending(s, false);
1674 d = event_get_clock_data(s->event, s->type);
1677 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1678 d->needs_rearm = true;
1683 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1684 assert_return(s, -EINVAL);
1685 assert_return(clock, -EINVAL);
1686 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1687 assert_return(!event_pid_changed(s->event), -ECHILD);
1689 *clock = event_source_type_to_clock(s->type);
1693 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1694 assert_return(s, -EINVAL);
1695 assert_return(pid, -EINVAL);
1696 assert_return(s->type == SOURCE_CHILD, -EDOM);
1697 assert_return(!event_pid_changed(s->event), -ECHILD);
1699 *pid = s->child.pid;
1703 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1706 assert_return(s, -EINVAL);
1707 assert_return(s->type != SOURCE_EXIT, -EDOM);
1708 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1709 assert_return(!event_pid_changed(s->event), -ECHILD);
1711 if (s->prepare == callback)
1714 if (callback && s->prepare) {
1715 s->prepare = callback;
1719 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1723 s->prepare = callback;
1726 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1730 prioq_remove(s->event->prepare, s, &s->prepare_index);
1735 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1736 assert_return(s, NULL);
1741 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1744 assert_return(s, NULL);
1747 s->userdata = userdata;
1752 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1763 initialize_perturb(e);
1766 Find a good time to wake up again between times a and b. We
1767 have two goals here:
1769 a) We want to wake up as seldom as possible, hence prefer
1770 later times over earlier times.
1772 b) But if we have to wake up, then let's make sure to
1773 dispatch as much as possible on the entire system.
1775 We implement this by waking up everywhere at the same time
1776 within any given minute if we can, synchronised via the
1777 perturbation value determined from the boot ID. If we can't,
1778 then we try to find the same spot in every 10s, then 1s and
1779 then 250ms step. Otherwise, we pick the last possible time
1783 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1785 if (_unlikely_(c < USEC_PER_MINUTE))
1788 c -= USEC_PER_MINUTE;
1794 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1796 if (_unlikely_(c < USEC_PER_SEC*10))
1799 c -= USEC_PER_SEC*10;
1805 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1807 if (_unlikely_(c < USEC_PER_SEC))
1816 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1818 if (_unlikely_(c < USEC_PER_MSEC*250))
1821 c -= USEC_PER_MSEC*250;
1830 static int event_arm_timer(
1832 struct clock_data *d) {
1834 struct itimerspec its = {};
1835 sd_event_source *a, *b;
1842 if (!d->needs_rearm)
1845 d->needs_rearm = false;
1847 a = prioq_peek(d->earliest);
1848 if (!a || a->enabled == SD_EVENT_OFF) {
1853 if (d->next == USEC_INFINITY)
1857 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1861 d->next = USEC_INFINITY;
1865 b = prioq_peek(d->latest);
1866 assert_se(b && b->enabled != SD_EVENT_OFF);
1868 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1872 assert_se(d->fd >= 0);
1875 /* We don' want to disarm here, just mean some time looooong ago. */
1876 its.it_value.tv_sec = 0;
1877 its.it_value.tv_nsec = 1;
1879 timespec_store(&its.it_value, t);
1881 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1889 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1892 assert(s->type == SOURCE_IO);
1894 /* If the event source was already pending, we just OR in the
1895 * new revents, otherwise we reset the value. The ORing is
1896 * necessary to handle EPOLLONESHOT events properly where
1897 * readability might happen independently of writability, and
1898 * we need to keep track of both */
1901 s->io.revents |= revents;
1903 s->io.revents = revents;
1905 return source_set_pending(s, true);
1908 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1915 assert_return(events == EPOLLIN, -EIO);
1917 ss = read(fd, &x, sizeof(x));
1919 if (errno == EAGAIN || errno == EINTR)
1925 if (_unlikely_(ss != sizeof(x)))
1929 *next = USEC_INFINITY;
1934 static int process_timer(
1937 struct clock_data *d) {
1946 s = prioq_peek(d->earliest);
1949 s->enabled == SD_EVENT_OFF ||
1953 r = source_set_pending(s, true);
1957 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1958 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1959 d->needs_rearm = true;
1965 static int process_child(sd_event *e) {
1972 e->need_process_child = false;
1975 So, this is ugly. We iteratively invoke waitid() with P_PID
1976 + WNOHANG for each PID we wait for, instead of using
1977 P_ALL. This is because we only want to get child
1978 information of very specific child processes, and not all
1979 of them. We might not have processed the SIGCHLD even of a
1980 previous invocation and we don't want to maintain a
1981 unbounded *per-child* event queue, hence we really don't
1982 want anything flushed out of the kernel's queue that we
1983 don't care about. Since this is O(n) this means that if you
1984 have a lot of processes you probably want to handle SIGCHLD
1987 We do not reap the children here (by using WNOWAIT), this
1988 is only done after the event source is dispatched so that
1989 the callback still sees the process as a zombie.
1992 HASHMAP_FOREACH(s, e->child_sources, i) {
1993 assert(s->type == SOURCE_CHILD);
1998 if (s->enabled == SD_EVENT_OFF)
2001 zero(s->child.siginfo);
2002 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2003 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2007 if (s->child.siginfo.si_pid != 0) {
2009 s->child.siginfo.si_code == CLD_EXITED ||
2010 s->child.siginfo.si_code == CLD_KILLED ||
2011 s->child.siginfo.si_code == CLD_DUMPED;
2013 if (!zombie && (s->child.options & WEXITED)) {
2014 /* If the child isn't dead then let's
2015 * immediately remove the state change
2016 * from the queue, since there's no
2017 * benefit in leaving it queued */
2019 assert(s->child.options & (WSTOPPED|WCONTINUED));
2020 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2023 r = source_set_pending(s, true);
2032 static int process_signal(sd_event *e, uint32_t events) {
2033 bool read_one = false;
2038 assert_return(events == EPOLLIN, -EIO);
2041 struct signalfd_siginfo si;
2043 sd_event_source *s = NULL;
2045 n = read(e->signal_fd, &si, sizeof(si));
2047 if (errno == EAGAIN || errno == EINTR)
2053 if (_unlikely_(n != sizeof(si)))
2056 assert(si.ssi_signo < _NSIG);
2060 if (si.ssi_signo == SIGCHLD) {
2061 r = process_child(e);
2068 if (e->signal_sources)
2069 s = e->signal_sources[si.ssi_signo];
2074 s->signal.siginfo = si;
2075 r = source_set_pending(s, true);
2081 static int source_dispatch(sd_event_source *s) {
2085 assert(s->pending || s->type == SOURCE_EXIT);
2087 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2088 r = source_set_pending(s, false);
2093 if (s->type != SOURCE_POST) {
2097 /* If we execute a non-post source, let's mark all
2098 * post sources as pending */
2100 SET_FOREACH(z, s->event->post_sources, i) {
2101 if (z->enabled == SD_EVENT_OFF)
2104 r = source_set_pending(z, true);
2110 if (s->enabled == SD_EVENT_ONESHOT) {
2111 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2116 s->dispatching = true;
2121 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2124 case SOURCE_TIME_REALTIME:
2125 case SOURCE_TIME_BOOTTIME:
2126 case SOURCE_TIME_MONOTONIC:
2127 case SOURCE_TIME_REALTIME_ALARM:
2128 case SOURCE_TIME_BOOTTIME_ALARM:
2129 r = s->time.callback(s, s->time.next, s->userdata);
2133 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2136 case SOURCE_CHILD: {
2139 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2140 s->child.siginfo.si_code == CLD_KILLED ||
2141 s->child.siginfo.si_code == CLD_DUMPED;
2143 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2145 /* Now, reap the PID for good. */
2147 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2153 r = s->defer.callback(s, s->userdata);
2157 r = s->post.callback(s, s->userdata);
2161 r = s->exit.callback(s, s->userdata);
2164 case SOURCE_WATCHDOG:
2165 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2166 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2167 assert_not_reached("Wut? I shouldn't exist.");
2170 s->dispatching = false;
2174 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2176 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2182 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2187 static int event_prepare(sd_event *e) {
2195 s = prioq_peek(e->prepare);
2196 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2199 s->prepare_iteration = e->iteration;
2200 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2206 s->dispatching = true;
2207 r = s->prepare(s, s->userdata);
2208 s->dispatching = false;
2212 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2214 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2220 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2226 static int dispatch_exit(sd_event *e) {
2232 p = prioq_peek(e->exit);
2233 if (!p || p->enabled == SD_EVENT_OFF) {
2234 e->state = SD_EVENT_FINISHED;
2240 e->state = SD_EVENT_EXITING;
2242 r = source_dispatch(p);
2244 e->state = SD_EVENT_INITIAL;
2250 static sd_event_source* event_next_pending(sd_event *e) {
2255 p = prioq_peek(e->pending);
2259 if (p->enabled == SD_EVENT_OFF)
2265 static int arm_watchdog(sd_event *e) {
2266 struct itimerspec its = {};
2271 assert(e->watchdog_fd >= 0);
2273 t = sleep_between(e,
2274 e->watchdog_last + (e->watchdog_period / 2),
2275 e->watchdog_last + (e->watchdog_period * 3 / 4));
2277 timespec_store(&its.it_value, t);
2279 /* Make sure we never set the watchdog to 0, which tells the
2280 * kernel to disable it. */
2281 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2282 its.it_value.tv_nsec = 1;
2284 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2291 static int process_watchdog(sd_event *e) {
2297 /* Don't notify watchdog too often */
2298 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2301 sd_notify(false, "WATCHDOG=1");
2302 e->watchdog_last = e->timestamp.monotonic;
2304 return arm_watchdog(e);
2307 _public_ int sd_event_prepare(sd_event *e) {
2310 assert_return(e, -EINVAL);
2311 assert_return(!event_pid_changed(e), -ECHILD);
2312 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2313 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2315 if (e->exit_requested)
2320 r = event_prepare(e);
2324 r = event_arm_timer(e, &e->realtime);
2328 r = event_arm_timer(e, &e->boottime);
2332 r = event_arm_timer(e, &e->monotonic);
2336 r = event_arm_timer(e, &e->realtime_alarm);
2340 r = event_arm_timer(e, &e->boottime_alarm);
2344 if (event_next_pending(e) || e->need_process_child)
2347 e->state = SD_EVENT_ARMED;
2352 e->state = SD_EVENT_ARMED;
2353 r = sd_event_wait(e, 0);
2355 e->state = SD_EVENT_ARMED;
2360 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2361 struct epoll_event *ev_queue;
2362 unsigned ev_queue_max;
2365 assert_return(e, -EINVAL);
2366 assert_return(!event_pid_changed(e), -ECHILD);
2367 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2368 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2370 if (e->exit_requested) {
2371 e->state = SD_EVENT_PENDING;
2375 ev_queue_max = MAX(e->n_sources, 1u);
2376 ev_queue = newa(struct epoll_event, ev_queue_max);
2378 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2379 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2381 if (errno == EINTR) {
2382 e->state = SD_EVENT_PENDING;
2390 dual_timestamp_get(&e->timestamp);
2391 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2393 for (i = 0; i < m; i++) {
2395 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2396 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2397 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2398 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2399 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2400 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2401 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2402 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2403 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2404 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2405 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2406 r = process_signal(e, ev_queue[i].events);
2407 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2408 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2410 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2416 r = process_watchdog(e);
2420 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2424 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2428 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2432 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2436 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2440 if (e->need_process_child) {
2441 r = process_child(e);
2446 if (event_next_pending(e)) {
2447 e->state = SD_EVENT_PENDING;
2455 e->state = SD_EVENT_INITIAL;
2460 _public_ int sd_event_dispatch(sd_event *e) {
2464 assert_return(e, -EINVAL);
2465 assert_return(!event_pid_changed(e), -ECHILD);
2466 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2467 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2469 if (e->exit_requested)
2470 return dispatch_exit(e);
2472 p = event_next_pending(e);
2476 e->state = SD_EVENT_RUNNING;
2477 r = source_dispatch(p);
2478 e->state = SD_EVENT_INITIAL;
2485 e->state = SD_EVENT_INITIAL;
2490 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2493 assert_return(e, -EINVAL);
2494 assert_return(!event_pid_changed(e), -ECHILD);
2495 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2496 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2498 r = sd_event_prepare(e);
2500 /* There was nothing? Then wait... */
2501 r = sd_event_wait(e, timeout);
2504 /* There's something now, then let's dispatch it */
2505 r = sd_event_dispatch(e);
2515 _public_ int sd_event_loop(sd_event *e) {
2518 assert_return(e, -EINVAL);
2519 assert_return(!event_pid_changed(e), -ECHILD);
2520 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2524 while (e->state != SD_EVENT_FINISHED) {
2525 r = sd_event_run(e, (uint64_t) -1);
2537 _public_ int sd_event_get_fd(sd_event *e) {
2539 assert_return(e, -EINVAL);
2540 assert_return(!event_pid_changed(e), -ECHILD);
2545 _public_ int sd_event_get_state(sd_event *e) {
2546 assert_return(e, -EINVAL);
2547 assert_return(!event_pid_changed(e), -ECHILD);
2552 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2553 assert_return(e, -EINVAL);
2554 assert_return(code, -EINVAL);
2555 assert_return(!event_pid_changed(e), -ECHILD);
2557 if (!e->exit_requested)
2560 *code = e->exit_code;
2564 _public_ int sd_event_exit(sd_event *e, int code) {
2565 assert_return(e, -EINVAL);
2566 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2567 assert_return(!event_pid_changed(e), -ECHILD);
2569 e->exit_requested = true;
2570 e->exit_code = code;
2575 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2576 assert_return(e, -EINVAL);
2577 assert_return(usec, -EINVAL);
2578 assert_return(!event_pid_changed(e), -ECHILD);
2580 /* If we haven't run yet, just get the actual time */
2581 if (!dual_timestamp_is_set(&e->timestamp))
2586 case CLOCK_REALTIME:
2587 case CLOCK_REALTIME_ALARM:
2588 *usec = e->timestamp.realtime;
2591 case CLOCK_MONOTONIC:
2592 *usec = e->timestamp.monotonic;
2595 case CLOCK_BOOTTIME:
2596 case CLOCK_BOOTTIME_ALARM:
2597 *usec = e->timestamp_boottime;
2604 _public_ int sd_event_default(sd_event **ret) {
2606 static thread_local sd_event *default_event = NULL;
2611 return !!default_event;
2613 if (default_event) {
2614 *ret = sd_event_ref(default_event);
2618 r = sd_event_new(&e);
2622 e->default_event_ptr = &default_event;
2630 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2631 assert_return(e, -EINVAL);
2632 assert_return(tid, -EINVAL);
2633 assert_return(!event_pid_changed(e), -ECHILD);
2643 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2646 assert_return(e, -EINVAL);
2647 assert_return(!event_pid_changed(e), -ECHILD);
2649 if (e->watchdog == !!b)
2653 struct epoll_event ev = {};
2655 r = sd_watchdog_enabled(false, &e->watchdog_period);
2659 /* Issue first ping immediately */
2660 sd_notify(false, "WATCHDOG=1");
2661 e->watchdog_last = now(CLOCK_MONOTONIC);
2663 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2664 if (e->watchdog_fd < 0)
2667 r = arm_watchdog(e);
2671 ev.events = EPOLLIN;
2672 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2674 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2681 if (e->watchdog_fd >= 0) {
2682 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2683 e->watchdog_fd = safe_close(e->watchdog_fd);
2691 e->watchdog_fd = safe_close(e->watchdog_fd);
2695 _public_ int sd_event_get_watchdog(sd_event *e) {
2696 assert_return(e, -EINVAL);
2697 assert_return(!event_pid_changed(e), -ECHILD);