1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/timerfd.h>
27 #include "sd-daemon.h"
32 #include "time-util.h"
39 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
41 typedef enum EventSourceType {
45 SOURCE_TIME_MONOTONIC,
46 SOURCE_TIME_REALTIME_ALARM,
47 SOURCE_TIME_BOOTTIME_ALARM,
54 _SOURCE_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_BOOTTIME, SOURCE_TIME_MONOTONIC, SOURCE_TIME_REALTIME_ALARM, SOURCE_TIME_BOOTTIME_ALARM)
60 struct sd_event_source {
65 sd_event_handler_t prepare;
69 EventSourceType type:5;
76 unsigned pending_index;
77 unsigned prepare_index;
78 unsigned pending_iteration;
79 unsigned prepare_iteration;
81 LIST_FIELDS(sd_event_source, sources);
85 sd_event_io_handler_t callback;
92 sd_event_time_handler_t callback;
93 usec_t next, accuracy;
94 unsigned earliest_index;
95 unsigned latest_index;
98 sd_event_signal_handler_t callback;
99 struct signalfd_siginfo siginfo;
103 sd_event_child_handler_t callback;
109 sd_event_handler_t callback;
112 sd_event_handler_t callback;
115 sd_event_handler_t callback;
116 unsigned prioq_index;
124 /* For all clocks we maintain two priority queues each, one
125 * ordered for the earliest times the events may be
126 * dispatched, and one ordered by the latest times they must
127 * have been dispatched. The range between the top entries in
128 * the two prioqs is the time window we can freely schedule
148 /* timerfd_create() only supports these five clocks so far. We
149 * can add support for more clocks when the kernel learns to
150 * deal with them, too. */
151 struct clock_data realtime;
152 struct clock_data boottime;
153 struct clock_data monotonic;
154 struct clock_data realtime_alarm;
155 struct clock_data boottime_alarm;
160 sd_event_source **signal_sources;
162 Hashmap *child_sources;
163 unsigned n_enabled_child_sources;
172 dual_timestamp timestamp;
173 usec_t timestamp_boottime;
176 bool exit_requested:1;
177 bool need_process_child:1;
183 sd_event **default_event_ptr;
185 usec_t watchdog_last, watchdog_period;
189 LIST_HEAD(sd_event_source, sources);
192 static void source_disconnect(sd_event_source *s);
194 static int pending_prioq_compare(const void *a, const void *b) {
195 const sd_event_source *x = a, *y = b;
200 /* Enabled ones first */
201 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
203 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
206 /* Lower priority values first */
207 if (x->priority < y->priority)
209 if (x->priority > y->priority)
212 /* Older entries first */
213 if (x->pending_iteration < y->pending_iteration)
215 if (x->pending_iteration > y->pending_iteration)
218 /* Stability for the rest */
227 static int prepare_prioq_compare(const void *a, const void *b) {
228 const sd_event_source *x = a, *y = b;
233 /* Move most recently prepared ones last, so that we can stop
234 * preparing as soon as we hit one that has already been
235 * prepared in the current iteration */
236 if (x->prepare_iteration < y->prepare_iteration)
238 if (x->prepare_iteration > y->prepare_iteration)
241 /* Enabled ones first */
242 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
244 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
247 /* Lower priority values first */
248 if (x->priority < y->priority)
250 if (x->priority > y->priority)
253 /* Stability for the rest */
262 static int earliest_time_prioq_compare(const void *a, const void *b) {
263 const sd_event_source *x = a, *y = b;
265 assert(EVENT_SOURCE_IS_TIME(x->type));
266 assert(x->type == y->type);
268 /* Enabled ones first */
269 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
271 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
274 /* Move the pending ones to the end */
275 if (!x->pending && y->pending)
277 if (x->pending && !y->pending)
281 if (x->time.next < y->time.next)
283 if (x->time.next > y->time.next)
286 /* Stability for the rest */
295 static int latest_time_prioq_compare(const void *a, const void *b) {
296 const sd_event_source *x = a, *y = b;
298 assert(EVENT_SOURCE_IS_TIME(x->type));
299 assert(x->type == y->type);
301 /* Enabled ones first */
302 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
304 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
307 /* Move the pending ones to the end */
308 if (!x->pending && y->pending)
310 if (x->pending && !y->pending)
314 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
316 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
319 /* Stability for the rest */
328 static int exit_prioq_compare(const void *a, const void *b) {
329 const sd_event_source *x = a, *y = b;
331 assert(x->type == SOURCE_EXIT);
332 assert(y->type == SOURCE_EXIT);
334 /* Enabled ones first */
335 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
337 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
340 /* Lower priority values first */
341 if (x->priority < y->priority)
343 if (x->priority > y->priority)
346 /* Stability for the rest */
355 static void free_clock_data(struct clock_data *d) {
359 prioq_free(d->earliest);
360 prioq_free(d->latest);
363 static void event_free(sd_event *e) {
368 while ((s = e->sources)) {
370 source_disconnect(s);
371 sd_event_source_unref(s);
374 assert(e->n_sources == 0);
376 if (e->default_event_ptr)
377 *(e->default_event_ptr) = NULL;
379 safe_close(e->epoll_fd);
380 safe_close(e->signal_fd);
381 safe_close(e->watchdog_fd);
383 free_clock_data(&e->realtime);
384 free_clock_data(&e->boottime);
385 free_clock_data(&e->monotonic);
386 free_clock_data(&e->realtime_alarm);
387 free_clock_data(&e->boottime_alarm);
389 prioq_free(e->pending);
390 prioq_free(e->prepare);
393 free(e->signal_sources);
395 hashmap_free(e->child_sources);
396 set_free(e->post_sources);
400 _public_ int sd_event_new(sd_event** ret) {
404 assert_return(ret, -EINVAL);
406 e = new0(sd_event, 1);
411 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;
412 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
413 e->original_pid = getpid();
414 e->perturb = USEC_INFINITY;
416 assert_se(sigemptyset(&e->sigset) == 0);
418 e->pending = prioq_new(pending_prioq_compare);
424 e->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
425 if (e->epoll_fd < 0) {
438 _public_ sd_event* sd_event_ref(sd_event *e) {
439 assert_return(e, NULL);
441 assert(e->n_ref >= 1);
447 _public_ sd_event* sd_event_unref(sd_event *e) {
452 assert(e->n_ref >= 1);
461 static bool event_pid_changed(sd_event *e) {
464 /* We don't support people creating an event loop and keeping
465 * it around over a fork(). Let's complain. */
467 return e->original_pid != getpid();
470 static int source_io_unregister(sd_event_source *s) {
474 assert(s->type == SOURCE_IO);
476 if (!s->io.registered)
479 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, s->io.fd, NULL);
483 s->io.registered = false;
487 static int source_io_register(
492 struct epoll_event ev = {};
496 assert(s->type == SOURCE_IO);
497 assert(enabled != SD_EVENT_OFF);
502 if (enabled == SD_EVENT_ONESHOT)
503 ev.events |= EPOLLONESHOT;
505 if (s->io.registered)
506 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
508 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
513 s->io.registered = true;
518 static clockid_t event_source_type_to_clock(EventSourceType t) {
522 case SOURCE_TIME_REALTIME:
523 return CLOCK_REALTIME;
525 case SOURCE_TIME_BOOTTIME:
526 return CLOCK_BOOTTIME;
528 case SOURCE_TIME_MONOTONIC:
529 return CLOCK_MONOTONIC;
531 case SOURCE_TIME_REALTIME_ALARM:
532 return CLOCK_REALTIME_ALARM;
534 case SOURCE_TIME_BOOTTIME_ALARM:
535 return CLOCK_BOOTTIME_ALARM;
538 return (clockid_t) -1;
542 static EventSourceType clock_to_event_source_type(clockid_t clock) {
547 return SOURCE_TIME_REALTIME;
550 return SOURCE_TIME_BOOTTIME;
552 case CLOCK_MONOTONIC:
553 return SOURCE_TIME_MONOTONIC;
555 case CLOCK_REALTIME_ALARM:
556 return SOURCE_TIME_REALTIME_ALARM;
558 case CLOCK_BOOTTIME_ALARM:
559 return SOURCE_TIME_BOOTTIME_ALARM;
562 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
566 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
571 case SOURCE_TIME_REALTIME:
574 case SOURCE_TIME_BOOTTIME:
577 case SOURCE_TIME_MONOTONIC:
578 return &e->monotonic;
580 case SOURCE_TIME_REALTIME_ALARM:
581 return &e->realtime_alarm;
583 case SOURCE_TIME_BOOTTIME_ALARM:
584 return &e->boottime_alarm;
591 static bool need_signal(sd_event *e, int signal) {
592 return (e->signal_sources && e->signal_sources[signal] &&
593 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
595 (signal == SIGCHLD &&
596 e->n_enabled_child_sources > 0);
599 static int event_update_signal_fd(sd_event *e) {
600 struct epoll_event ev = {};
606 add_to_epoll = e->signal_fd < 0;
608 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
618 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
620 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
622 e->signal_fd = safe_close(e->signal_fd);
629 static void source_disconnect(sd_event_source *s) {
637 assert(s->event->n_sources > 0);
643 source_io_unregister(s);
647 case SOURCE_TIME_REALTIME:
648 case SOURCE_TIME_BOOTTIME:
649 case SOURCE_TIME_MONOTONIC:
650 case SOURCE_TIME_REALTIME_ALARM:
651 case SOURCE_TIME_BOOTTIME_ALARM: {
652 struct clock_data *d;
654 d = event_get_clock_data(s->event, s->type);
657 prioq_remove(d->earliest, s, &s->time.earliest_index);
658 prioq_remove(d->latest, s, &s->time.latest_index);
659 d->needs_rearm = true;
664 if (s->signal.sig > 0) {
665 if (s->event->signal_sources)
666 s->event->signal_sources[s->signal.sig] = NULL;
668 /* If the signal was on and now it is off... */
669 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
670 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
672 (void) event_update_signal_fd(s->event);
673 /* If disabling failed, we might get a spurious event,
674 * but otherwise nothing bad should happen. */
681 if (s->child.pid > 0) {
682 if (s->enabled != SD_EVENT_OFF) {
683 assert(s->event->n_enabled_child_sources > 0);
684 s->event->n_enabled_child_sources--;
686 /* We know the signal was on, if it is off now... */
687 if (!need_signal(s->event, SIGCHLD)) {
688 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
690 (void) event_update_signal_fd(s->event);
691 /* If disabling failed, we might get a spurious event,
692 * but otherwise nothing bad should happen. */
696 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
706 set_remove(s->event->post_sources, s);
710 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
714 assert_not_reached("Wut? I shouldn't exist.");
718 prioq_remove(s->event->pending, s, &s->pending_index);
721 prioq_remove(s->event->prepare, s, &s->prepare_index);
725 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
727 LIST_REMOVE(sources, event->sources, s);
731 sd_event_unref(event);
734 static void source_free(sd_event_source *s) {
737 source_disconnect(s);
738 free(s->description);
742 static int source_set_pending(sd_event_source *s, bool b) {
746 assert(s->type != SOURCE_EXIT);
754 s->pending_iteration = s->event->iteration;
756 r = prioq_put(s->event->pending, s, &s->pending_index);
762 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
764 if (EVENT_SOURCE_IS_TIME(s->type)) {
765 struct clock_data *d;
767 d = event_get_clock_data(s->event, s->type);
770 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
771 prioq_reshuffle(d->latest, s, &s->time.latest_index);
772 d->needs_rearm = true;
778 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
783 s = new0(sd_event_source, 1);
789 s->floating = floating;
791 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
796 LIST_PREPEND(sources, e->sources, s);
802 _public_ int sd_event_add_io(
804 sd_event_source **ret,
807 sd_event_io_handler_t callback,
813 assert_return(e, -EINVAL);
814 assert_return(fd >= 0, -EINVAL);
815 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
816 assert_return(callback, -EINVAL);
817 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
818 assert_return(!event_pid_changed(e), -ECHILD);
820 s = source_new(e, !ret, SOURCE_IO);
825 s->io.events = events;
826 s->io.callback = callback;
827 s->userdata = userdata;
828 s->enabled = SD_EVENT_ON;
830 r = source_io_register(s, s->enabled, events);
842 static void initialize_perturb(sd_event *e) {
843 sd_id128_t bootid = {};
845 /* When we sleep for longer, we try to realign the wakeup to
846 the same time wihtin each minute/second/250ms, so that
847 events all across the system can be coalesced into a single
848 CPU wakeup. However, let's take some system-specific
849 randomness for this value, so that in a network of systems
850 with synced clocks timer events are distributed a
851 bit. Here, we calculate a perturbation usec offset from the
854 if (_likely_(e->perturb != USEC_INFINITY))
857 if (sd_id128_get_boot(&bootid) >= 0)
858 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
861 static int event_setup_timer_fd(
863 struct clock_data *d,
866 struct epoll_event ev = {};
872 if (_likely_(d->fd >= 0))
875 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
880 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
882 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
892 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
895 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
898 _public_ int sd_event_add_time(
900 sd_event_source **ret,
904 sd_event_time_handler_t callback,
907 EventSourceType type;
909 struct clock_data *d;
912 assert_return(e, -EINVAL);
913 assert_return(usec != (uint64_t) -1, -EINVAL);
914 assert_return(accuracy != (uint64_t) -1, -EINVAL);
915 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
916 assert_return(!event_pid_changed(e), -ECHILD);
919 callback = time_exit_callback;
921 type = clock_to_event_source_type(clock);
922 assert_return(type >= 0, -EOPNOTSUPP);
924 d = event_get_clock_data(e, type);
928 d->earliest = prioq_new(earliest_time_prioq_compare);
934 d->latest = prioq_new(latest_time_prioq_compare);
940 r = event_setup_timer_fd(e, d, clock);
945 s = source_new(e, !ret, type);
950 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
951 s->time.callback = callback;
952 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
953 s->userdata = userdata;
954 s->enabled = SD_EVENT_ONESHOT;
956 d->needs_rearm = true;
958 r = prioq_put(d->earliest, s, &s->time.earliest_index);
962 r = prioq_put(d->latest, s, &s->time.latest_index);
976 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
979 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
982 _public_ int sd_event_add_signal(
984 sd_event_source **ret,
986 sd_event_signal_handler_t callback,
994 assert_return(e, -EINVAL);
995 assert_return(sig > 0, -EINVAL);
996 assert_return(sig < _NSIG, -EINVAL);
997 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
998 assert_return(!event_pid_changed(e), -ECHILD);
1001 callback = signal_exit_callback;
1003 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1007 if (!sigismember(&ss, sig))
1010 if (!e->signal_sources) {
1011 e->signal_sources = new0(sd_event_source*, _NSIG);
1012 if (!e->signal_sources)
1014 } else if (e->signal_sources[sig])
1017 previous = need_signal(e, sig);
1019 s = source_new(e, !ret, SOURCE_SIGNAL);
1023 s->signal.sig = sig;
1024 s->signal.callback = callback;
1025 s->userdata = userdata;
1026 s->enabled = SD_EVENT_ON;
1028 e->signal_sources[sig] = s;
1031 assert_se(sigaddset(&e->sigset, sig) == 0);
1033 r = event_update_signal_fd(e);
1040 /* Use the signal name as description for the event source by default */
1041 (void) sd_event_source_set_description(s, signal_to_string(sig));
1049 _public_ int sd_event_add_child(
1051 sd_event_source **ret,
1054 sd_event_child_handler_t callback,
1061 assert_return(e, -EINVAL);
1062 assert_return(pid > 1, -EINVAL);
1063 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1064 assert_return(options != 0, -EINVAL);
1065 assert_return(callback, -EINVAL);
1066 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1067 assert_return(!event_pid_changed(e), -ECHILD);
1069 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1073 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1076 previous = need_signal(e, SIGCHLD);
1078 s = source_new(e, !ret, SOURCE_CHILD);
1083 s->child.options = options;
1084 s->child.callback = callback;
1085 s->userdata = userdata;
1086 s->enabled = SD_EVENT_ONESHOT;
1088 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1094 e->n_enabled_child_sources ++;
1097 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1099 r = event_update_signal_fd(e);
1106 e->need_process_child = true;
1114 _public_ int sd_event_add_defer(
1116 sd_event_source **ret,
1117 sd_event_handler_t callback,
1123 assert_return(e, -EINVAL);
1124 assert_return(callback, -EINVAL);
1125 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1126 assert_return(!event_pid_changed(e), -ECHILD);
1128 s = source_new(e, !ret, SOURCE_DEFER);
1132 s->defer.callback = callback;
1133 s->userdata = userdata;
1134 s->enabled = SD_EVENT_ONESHOT;
1136 r = source_set_pending(s, true);
1148 _public_ int sd_event_add_post(
1150 sd_event_source **ret,
1151 sd_event_handler_t callback,
1157 assert_return(e, -EINVAL);
1158 assert_return(callback, -EINVAL);
1159 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1160 assert_return(!event_pid_changed(e), -ECHILD);
1162 r = set_ensure_allocated(&e->post_sources, NULL);
1166 s = source_new(e, !ret, SOURCE_POST);
1170 s->post.callback = callback;
1171 s->userdata = userdata;
1172 s->enabled = SD_EVENT_ON;
1174 r = set_put(e->post_sources, s);
1186 _public_ int sd_event_add_exit(
1188 sd_event_source **ret,
1189 sd_event_handler_t callback,
1195 assert_return(e, -EINVAL);
1196 assert_return(callback, -EINVAL);
1197 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1198 assert_return(!event_pid_changed(e), -ECHILD);
1201 e->exit = prioq_new(exit_prioq_compare);
1206 s = source_new(e, !ret, SOURCE_EXIT);
1210 s->exit.callback = callback;
1211 s->userdata = userdata;
1212 s->exit.prioq_index = PRIOQ_IDX_NULL;
1213 s->enabled = SD_EVENT_ONESHOT;
1215 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1227 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1228 assert_return(s, NULL);
1230 assert(s->n_ref >= 1);
1236 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1241 assert(s->n_ref >= 1);
1244 if (s->n_ref <= 0) {
1245 /* Here's a special hack: when we are called from a
1246 * dispatch handler we won't free the event source
1247 * immediately, but we will detach the fd from the
1248 * epoll. This way it is safe for the caller to unref
1249 * the event source and immediately close the fd, but
1250 * we still retain a valid event source object after
1253 if (s->dispatching) {
1254 if (s->type == SOURCE_IO)
1255 source_io_unregister(s);
1257 source_disconnect(s);
1265 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1266 assert_return(s, -EINVAL);
1267 assert_return(!event_pid_changed(s->event), -ECHILD);
1269 return free_and_strdup(&s->description, description);
1272 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1273 assert_return(s, -EINVAL);
1274 assert_return(description, -EINVAL);
1275 assert_return(s->description, -ENXIO);
1276 assert_return(!event_pid_changed(s->event), -ECHILD);
1278 *description = s->description;
1282 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1283 assert_return(s, NULL);
1288 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1289 assert_return(s, -EINVAL);
1290 assert_return(s->type != SOURCE_EXIT, -EDOM);
1291 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1292 assert_return(!event_pid_changed(s->event), -ECHILD);
1297 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1298 assert_return(s, -EINVAL);
1299 assert_return(s->type == SOURCE_IO, -EDOM);
1300 assert_return(!event_pid_changed(s->event), -ECHILD);
1305 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1308 assert_return(s, -EINVAL);
1309 assert_return(fd >= 0, -EINVAL);
1310 assert_return(s->type == SOURCE_IO, -EDOM);
1311 assert_return(!event_pid_changed(s->event), -ECHILD);
1316 if (s->enabled == SD_EVENT_OFF) {
1318 s->io.registered = false;
1322 saved_fd = s->io.fd;
1323 assert(s->io.registered);
1326 s->io.registered = false;
1328 r = source_io_register(s, s->enabled, s->io.events);
1330 s->io.fd = saved_fd;
1331 s->io.registered = true;
1335 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1341 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1342 assert_return(s, -EINVAL);
1343 assert_return(events, -EINVAL);
1344 assert_return(s->type == SOURCE_IO, -EDOM);
1345 assert_return(!event_pid_changed(s->event), -ECHILD);
1347 *events = s->io.events;
1351 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1354 assert_return(s, -EINVAL);
1355 assert_return(s->type == SOURCE_IO, -EDOM);
1356 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1357 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1358 assert_return(!event_pid_changed(s->event), -ECHILD);
1360 /* edge-triggered updates are never skipped, so we can reset edges */
1361 if (s->io.events == events && !(events & EPOLLET))
1364 if (s->enabled != SD_EVENT_OFF) {
1365 r = source_io_register(s, s->enabled, events);
1370 s->io.events = events;
1371 source_set_pending(s, false);
1376 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1377 assert_return(s, -EINVAL);
1378 assert_return(revents, -EINVAL);
1379 assert_return(s->type == SOURCE_IO, -EDOM);
1380 assert_return(s->pending, -ENODATA);
1381 assert_return(!event_pid_changed(s->event), -ECHILD);
1383 *revents = s->io.revents;
1387 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1388 assert_return(s, -EINVAL);
1389 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1390 assert_return(!event_pid_changed(s->event), -ECHILD);
1392 return s->signal.sig;
1395 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1396 assert_return(s, -EINVAL);
1397 assert_return(!event_pid_changed(s->event), -ECHILD);
1402 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1403 assert_return(s, -EINVAL);
1404 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1405 assert_return(!event_pid_changed(s->event), -ECHILD);
1407 if (s->priority == priority)
1410 s->priority = priority;
1413 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1416 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1418 if (s->type == SOURCE_EXIT)
1419 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1424 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1425 assert_return(s, -EINVAL);
1426 assert_return(m, -EINVAL);
1427 assert_return(!event_pid_changed(s->event), -ECHILD);
1433 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1436 assert_return(s, -EINVAL);
1437 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1438 assert_return(!event_pid_changed(s->event), -ECHILD);
1440 /* If we are dead anyway, we are fine with turning off
1441 * sources, but everything else needs to fail. */
1442 if (s->event->state == SD_EVENT_FINISHED)
1443 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1445 if (s->enabled == m)
1448 if (m == SD_EVENT_OFF) {
1453 r = source_io_unregister(s);
1460 case SOURCE_TIME_REALTIME:
1461 case SOURCE_TIME_BOOTTIME:
1462 case SOURCE_TIME_MONOTONIC:
1463 case SOURCE_TIME_REALTIME_ALARM:
1464 case SOURCE_TIME_BOOTTIME_ALARM: {
1465 struct clock_data *d;
1468 d = event_get_clock_data(s->event, s->type);
1471 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1472 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1473 d->needs_rearm = true;
1478 assert(need_signal(s->event, s->signal.sig));
1482 if (!need_signal(s->event, s->signal.sig)) {
1483 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1485 (void) event_update_signal_fd(s->event);
1486 /* If disabling failed, we might get a spurious event,
1487 * but otherwise nothing bad should happen. */
1493 assert(need_signal(s->event, SIGCHLD));
1497 assert(s->event->n_enabled_child_sources > 0);
1498 s->event->n_enabled_child_sources--;
1500 if (!need_signal(s->event, SIGCHLD)) {
1501 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1503 (void) event_update_signal_fd(s->event);
1510 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1519 assert_not_reached("Wut? I shouldn't exist.");
1526 r = source_io_register(s, m, s->io.events);
1533 case SOURCE_TIME_REALTIME:
1534 case SOURCE_TIME_BOOTTIME:
1535 case SOURCE_TIME_MONOTONIC:
1536 case SOURCE_TIME_REALTIME_ALARM:
1537 case SOURCE_TIME_BOOTTIME_ALARM: {
1538 struct clock_data *d;
1541 d = event_get_clock_data(s->event, s->type);
1544 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1545 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1546 d->needs_rearm = true;
1551 /* Check status before enabling. */
1552 if (!need_signal(s->event, s->signal.sig)) {
1553 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1555 r = event_update_signal_fd(s->event);
1557 s->enabled = SD_EVENT_OFF;
1566 /* Check status before enabling. */
1567 if (s->enabled == SD_EVENT_OFF) {
1568 if (!need_signal(s->event, SIGCHLD)) {
1569 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1571 r = event_update_signal_fd(s->event);
1573 s->enabled = SD_EVENT_OFF;
1578 s->event->n_enabled_child_sources++;
1586 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1595 assert_not_reached("Wut? I shouldn't exist.");
1600 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1603 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1608 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1609 assert_return(s, -EINVAL);
1610 assert_return(usec, -EINVAL);
1611 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1612 assert_return(!event_pid_changed(s->event), -ECHILD);
1614 *usec = s->time.next;
1618 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1619 struct clock_data *d;
1621 assert_return(s, -EINVAL);
1622 assert_return(usec != (uint64_t) -1, -EINVAL);
1623 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1624 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1625 assert_return(!event_pid_changed(s->event), -ECHILD);
1627 s->time.next = usec;
1629 source_set_pending(s, false);
1631 d = event_get_clock_data(s->event, s->type);
1634 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1635 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1636 d->needs_rearm = true;
1641 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1642 assert_return(s, -EINVAL);
1643 assert_return(usec, -EINVAL);
1644 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1645 assert_return(!event_pid_changed(s->event), -ECHILD);
1647 *usec = s->time.accuracy;
1651 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1652 struct clock_data *d;
1654 assert_return(s, -EINVAL);
1655 assert_return(usec != (uint64_t) -1, -EINVAL);
1656 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1657 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1658 assert_return(!event_pid_changed(s->event), -ECHILD);
1661 usec = DEFAULT_ACCURACY_USEC;
1663 s->time.accuracy = usec;
1665 source_set_pending(s, false);
1667 d = event_get_clock_data(s->event, s->type);
1670 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1671 d->needs_rearm = true;
1676 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1677 assert_return(s, -EINVAL);
1678 assert_return(clock, -EINVAL);
1679 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1680 assert_return(!event_pid_changed(s->event), -ECHILD);
1682 *clock = event_source_type_to_clock(s->type);
1686 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1687 assert_return(s, -EINVAL);
1688 assert_return(pid, -EINVAL);
1689 assert_return(s->type == SOURCE_CHILD, -EDOM);
1690 assert_return(!event_pid_changed(s->event), -ECHILD);
1692 *pid = s->child.pid;
1696 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1699 assert_return(s, -EINVAL);
1700 assert_return(s->type != SOURCE_EXIT, -EDOM);
1701 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1702 assert_return(!event_pid_changed(s->event), -ECHILD);
1704 if (s->prepare == callback)
1707 if (callback && s->prepare) {
1708 s->prepare = callback;
1712 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1716 s->prepare = callback;
1719 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1723 prioq_remove(s->event->prepare, s, &s->prepare_index);
1728 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1729 assert_return(s, NULL);
1734 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1737 assert_return(s, NULL);
1740 s->userdata = userdata;
1745 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1756 initialize_perturb(e);
1759 Find a good time to wake up again between times a and b. We
1760 have two goals here:
1762 a) We want to wake up as seldom as possible, hence prefer
1763 later times over earlier times.
1765 b) But if we have to wake up, then let's make sure to
1766 dispatch as much as possible on the entire system.
1768 We implement this by waking up everywhere at the same time
1769 within any given minute if we can, synchronised via the
1770 perturbation value determined from the boot ID. If we can't,
1771 then we try to find the same spot in every 10s, then 1s and
1772 then 250ms step. Otherwise, we pick the last possible time
1776 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1778 if (_unlikely_(c < USEC_PER_MINUTE))
1781 c -= USEC_PER_MINUTE;
1787 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1789 if (_unlikely_(c < USEC_PER_SEC*10))
1792 c -= USEC_PER_SEC*10;
1798 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1800 if (_unlikely_(c < USEC_PER_SEC))
1809 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1811 if (_unlikely_(c < USEC_PER_MSEC*250))
1814 c -= USEC_PER_MSEC*250;
1823 static int event_arm_timer(
1825 struct clock_data *d) {
1827 struct itimerspec its = {};
1828 sd_event_source *a, *b;
1835 if (!d->needs_rearm)
1838 d->needs_rearm = false;
1840 a = prioq_peek(d->earliest);
1841 if (!a || a->enabled == SD_EVENT_OFF) {
1846 if (d->next == USEC_INFINITY)
1850 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1854 d->next = USEC_INFINITY;
1858 b = prioq_peek(d->latest);
1859 assert_se(b && b->enabled != SD_EVENT_OFF);
1861 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1865 assert_se(d->fd >= 0);
1868 /* We don' want to disarm here, just mean some time looooong ago. */
1869 its.it_value.tv_sec = 0;
1870 its.it_value.tv_nsec = 1;
1872 timespec_store(&its.it_value, t);
1874 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1882 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1885 assert(s->type == SOURCE_IO);
1887 /* If the event source was already pending, we just OR in the
1888 * new revents, otherwise we reset the value. The ORing is
1889 * necessary to handle EPOLLONESHOT events properly where
1890 * readability might happen independently of writability, and
1891 * we need to keep track of both */
1894 s->io.revents |= revents;
1896 s->io.revents = revents;
1898 return source_set_pending(s, true);
1901 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1908 assert_return(events == EPOLLIN, -EIO);
1910 ss = read(fd, &x, sizeof(x));
1912 if (errno == EAGAIN || errno == EINTR)
1918 if (_unlikely_(ss != sizeof(x)))
1922 *next = USEC_INFINITY;
1927 static int process_timer(
1930 struct clock_data *d) {
1939 s = prioq_peek(d->earliest);
1942 s->enabled == SD_EVENT_OFF ||
1946 r = source_set_pending(s, true);
1950 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1951 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1952 d->needs_rearm = true;
1958 static int process_child(sd_event *e) {
1965 e->need_process_child = false;
1968 So, this is ugly. We iteratively invoke waitid() with P_PID
1969 + WNOHANG for each PID we wait for, instead of using
1970 P_ALL. This is because we only want to get child
1971 information of very specific child processes, and not all
1972 of them. We might not have processed the SIGCHLD even of a
1973 previous invocation and we don't want to maintain a
1974 unbounded *per-child* event queue, hence we really don't
1975 want anything flushed out of the kernel's queue that we
1976 don't care about. Since this is O(n) this means that if you
1977 have a lot of processes you probably want to handle SIGCHLD
1980 We do not reap the children here (by using WNOWAIT), this
1981 is only done after the event source is dispatched so that
1982 the callback still sees the process as a zombie.
1985 HASHMAP_FOREACH(s, e->child_sources, i) {
1986 assert(s->type == SOURCE_CHILD);
1991 if (s->enabled == SD_EVENT_OFF)
1994 zero(s->child.siginfo);
1995 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1996 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2000 if (s->child.siginfo.si_pid != 0) {
2002 s->child.siginfo.si_code == CLD_EXITED ||
2003 s->child.siginfo.si_code == CLD_KILLED ||
2004 s->child.siginfo.si_code == CLD_DUMPED;
2006 if (!zombie && (s->child.options & WEXITED)) {
2007 /* If the child isn't dead then let's
2008 * immediately remove the state change
2009 * from the queue, since there's no
2010 * benefit in leaving it queued */
2012 assert(s->child.options & (WSTOPPED|WCONTINUED));
2013 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2016 r = source_set_pending(s, true);
2025 static int process_signal(sd_event *e, uint32_t events) {
2026 bool read_one = false;
2031 assert_return(events == EPOLLIN, -EIO);
2034 struct signalfd_siginfo si;
2036 sd_event_source *s = NULL;
2038 n = read(e->signal_fd, &si, sizeof(si));
2040 if (errno == EAGAIN || errno == EINTR)
2046 if (_unlikely_(n != sizeof(si)))
2049 assert(si.ssi_signo < _NSIG);
2053 if (si.ssi_signo == SIGCHLD) {
2054 r = process_child(e);
2061 if (e->signal_sources)
2062 s = e->signal_sources[si.ssi_signo];
2067 s->signal.siginfo = si;
2068 r = source_set_pending(s, true);
2074 static int source_dispatch(sd_event_source *s) {
2078 assert(s->pending || s->type == SOURCE_EXIT);
2080 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2081 r = source_set_pending(s, false);
2086 if (s->type != SOURCE_POST) {
2090 /* If we execute a non-post source, let's mark all
2091 * post sources as pending */
2093 SET_FOREACH(z, s->event->post_sources, i) {
2094 if (z->enabled == SD_EVENT_OFF)
2097 r = source_set_pending(z, true);
2103 if (s->enabled == SD_EVENT_ONESHOT) {
2104 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2109 s->dispatching = true;
2114 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2117 case SOURCE_TIME_REALTIME:
2118 case SOURCE_TIME_BOOTTIME:
2119 case SOURCE_TIME_MONOTONIC:
2120 case SOURCE_TIME_REALTIME_ALARM:
2121 case SOURCE_TIME_BOOTTIME_ALARM:
2122 r = s->time.callback(s, s->time.next, s->userdata);
2126 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2129 case SOURCE_CHILD: {
2132 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2133 s->child.siginfo.si_code == CLD_KILLED ||
2134 s->child.siginfo.si_code == CLD_DUMPED;
2136 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2138 /* Now, reap the PID for good. */
2140 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2146 r = s->defer.callback(s, s->userdata);
2150 r = s->post.callback(s, s->userdata);
2154 r = s->exit.callback(s, s->userdata);
2157 case SOURCE_WATCHDOG:
2158 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2159 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2160 assert_not_reached("Wut? I shouldn't exist.");
2163 s->dispatching = false;
2167 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2169 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2175 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2180 static int event_prepare(sd_event *e) {
2188 s = prioq_peek(e->prepare);
2189 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2192 s->prepare_iteration = e->iteration;
2193 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2199 s->dispatching = true;
2200 r = s->prepare(s, s->userdata);
2201 s->dispatching = false;
2205 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2207 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2213 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2219 static int dispatch_exit(sd_event *e) {
2225 p = prioq_peek(e->exit);
2226 if (!p || p->enabled == SD_EVENT_OFF) {
2227 e->state = SD_EVENT_FINISHED;
2233 e->state = SD_EVENT_EXITING;
2235 r = source_dispatch(p);
2237 e->state = SD_EVENT_INITIAL;
2243 static sd_event_source* event_next_pending(sd_event *e) {
2248 p = prioq_peek(e->pending);
2252 if (p->enabled == SD_EVENT_OFF)
2258 static int arm_watchdog(sd_event *e) {
2259 struct itimerspec its = {};
2264 assert(e->watchdog_fd >= 0);
2266 t = sleep_between(e,
2267 e->watchdog_last + (e->watchdog_period / 2),
2268 e->watchdog_last + (e->watchdog_period * 3 / 4));
2270 timespec_store(&its.it_value, t);
2272 /* Make sure we never set the watchdog to 0, which tells the
2273 * kernel to disable it. */
2274 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2275 its.it_value.tv_nsec = 1;
2277 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2284 static int process_watchdog(sd_event *e) {
2290 /* Don't notify watchdog too often */
2291 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2294 sd_notify(false, "WATCHDOG=1");
2295 e->watchdog_last = e->timestamp.monotonic;
2297 return arm_watchdog(e);
2300 _public_ int sd_event_prepare(sd_event *e) {
2303 assert_return(e, -EINVAL);
2304 assert_return(!event_pid_changed(e), -ECHILD);
2305 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2306 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2308 if (e->exit_requested)
2313 r = event_prepare(e);
2317 r = event_arm_timer(e, &e->realtime);
2321 r = event_arm_timer(e, &e->boottime);
2325 r = event_arm_timer(e, &e->monotonic);
2329 r = event_arm_timer(e, &e->realtime_alarm);
2333 r = event_arm_timer(e, &e->boottime_alarm);
2337 if (event_next_pending(e) || e->need_process_child)
2340 e->state = SD_EVENT_ARMED;
2345 e->state = SD_EVENT_ARMED;
2346 r = sd_event_wait(e, 0);
2348 e->state = SD_EVENT_ARMED;
2353 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2354 struct epoll_event *ev_queue;
2355 unsigned ev_queue_max;
2358 assert_return(e, -EINVAL);
2359 assert_return(!event_pid_changed(e), -ECHILD);
2360 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2361 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2363 if (e->exit_requested) {
2364 e->state = SD_EVENT_PENDING;
2368 ev_queue_max = MAX(e->n_sources, 1u);
2369 ev_queue = newa(struct epoll_event, ev_queue_max);
2371 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2372 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2374 if (errno == EINTR) {
2375 e->state = SD_EVENT_PENDING;
2384 dual_timestamp_get(&e->timestamp);
2385 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2387 for (i = 0; i < m; i++) {
2389 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2390 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2391 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2392 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2393 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2394 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2395 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2396 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2397 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2398 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2399 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2400 r = process_signal(e, ev_queue[i].events);
2401 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2402 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2404 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2410 r = process_watchdog(e);
2414 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2418 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2422 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2426 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2430 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2434 if (e->need_process_child) {
2435 r = process_child(e);
2440 if (event_next_pending(e)) {
2441 e->state = SD_EVENT_PENDING;
2449 e->state = SD_EVENT_INITIAL;
2454 _public_ int sd_event_dispatch(sd_event *e) {
2458 assert_return(e, -EINVAL);
2459 assert_return(!event_pid_changed(e), -ECHILD);
2460 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2461 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2463 if (e->exit_requested)
2464 return dispatch_exit(e);
2466 p = event_next_pending(e);
2470 e->state = SD_EVENT_RUNNING;
2471 r = source_dispatch(p);
2472 e->state = SD_EVENT_INITIAL;
2479 e->state = SD_EVENT_INITIAL;
2484 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2487 assert_return(e, -EINVAL);
2488 assert_return(!event_pid_changed(e), -ECHILD);
2489 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2490 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2492 r = sd_event_prepare(e);
2494 /* There was nothing? Then wait... */
2495 r = sd_event_wait(e, timeout);
2498 /* There's something now, then let's dispatch it */
2499 r = sd_event_dispatch(e);
2509 _public_ int sd_event_loop(sd_event *e) {
2512 assert_return(e, -EINVAL);
2513 assert_return(!event_pid_changed(e), -ECHILD);
2514 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2518 while (e->state != SD_EVENT_FINISHED) {
2519 r = sd_event_run(e, (uint64_t) -1);
2531 _public_ int sd_event_get_fd(sd_event *e) {
2533 assert_return(e, -EINVAL);
2534 assert_return(!event_pid_changed(e), -ECHILD);
2539 _public_ int sd_event_get_state(sd_event *e) {
2540 assert_return(e, -EINVAL);
2541 assert_return(!event_pid_changed(e), -ECHILD);
2546 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2547 assert_return(e, -EINVAL);
2548 assert_return(code, -EINVAL);
2549 assert_return(!event_pid_changed(e), -ECHILD);
2551 if (!e->exit_requested)
2554 *code = e->exit_code;
2558 _public_ int sd_event_exit(sd_event *e, int code) {
2559 assert_return(e, -EINVAL);
2560 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2561 assert_return(!event_pid_changed(e), -ECHILD);
2563 e->exit_requested = true;
2564 e->exit_code = code;
2569 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2570 assert_return(e, -EINVAL);
2571 assert_return(usec, -EINVAL);
2572 assert_return(!event_pid_changed(e), -ECHILD);
2574 /* If we haven't run yet, just get the actual time */
2575 if (!dual_timestamp_is_set(&e->timestamp))
2580 case CLOCK_REALTIME:
2581 case CLOCK_REALTIME_ALARM:
2582 *usec = e->timestamp.realtime;
2585 case CLOCK_MONOTONIC:
2586 *usec = e->timestamp.monotonic;
2589 case CLOCK_BOOTTIME:
2590 case CLOCK_BOOTTIME_ALARM:
2591 *usec = e->timestamp_boottime;
2598 _public_ int sd_event_default(sd_event **ret) {
2600 static thread_local sd_event *default_event = NULL;
2605 return !!default_event;
2607 if (default_event) {
2608 *ret = sd_event_ref(default_event);
2612 r = sd_event_new(&e);
2616 e->default_event_ptr = &default_event;
2624 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2625 assert_return(e, -EINVAL);
2626 assert_return(tid, -EINVAL);
2627 assert_return(!event_pid_changed(e), -ECHILD);
2637 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2640 assert_return(e, -EINVAL);
2641 assert_return(!event_pid_changed(e), -ECHILD);
2643 if (e->watchdog == !!b)
2647 struct epoll_event ev = {};
2649 r = sd_watchdog_enabled(false, &e->watchdog_period);
2653 /* Issue first ping immediately */
2654 sd_notify(false, "WATCHDOG=1");
2655 e->watchdog_last = now(CLOCK_MONOTONIC);
2657 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2658 if (e->watchdog_fd < 0)
2661 r = arm_watchdog(e);
2665 ev.events = EPOLLIN;
2666 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2668 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2675 if (e->watchdog_fd >= 0) {
2676 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2677 e->watchdog_fd = safe_close(e->watchdog_fd);
2685 e->watchdog_fd = safe_close(e->watchdog_fd);
2689 _public_ int sd_event_get_watchdog(sd_event *e) {
2690 assert_return(e, -EINVAL);
2691 assert_return(!event_pid_changed(e), -ECHILD);