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 void 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);
486 s->io.registered = false;
489 static int source_io_register(
494 struct epoll_event ev = {};
498 assert(s->type == SOURCE_IO);
499 assert(enabled != SD_EVENT_OFF);
504 if (enabled == SD_EVENT_ONESHOT)
505 ev.events |= EPOLLONESHOT;
507 if (s->io.registered)
508 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
510 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
515 s->io.registered = true;
520 static clockid_t event_source_type_to_clock(EventSourceType t) {
524 case SOURCE_TIME_REALTIME:
525 return CLOCK_REALTIME;
527 case SOURCE_TIME_BOOTTIME:
528 return CLOCK_BOOTTIME;
530 case SOURCE_TIME_MONOTONIC:
531 return CLOCK_MONOTONIC;
533 case SOURCE_TIME_REALTIME_ALARM:
534 return CLOCK_REALTIME_ALARM;
536 case SOURCE_TIME_BOOTTIME_ALARM:
537 return CLOCK_BOOTTIME_ALARM;
540 return (clockid_t) -1;
544 static EventSourceType clock_to_event_source_type(clockid_t clock) {
549 return SOURCE_TIME_REALTIME;
552 return SOURCE_TIME_BOOTTIME;
554 case CLOCK_MONOTONIC:
555 return SOURCE_TIME_MONOTONIC;
557 case CLOCK_REALTIME_ALARM:
558 return SOURCE_TIME_REALTIME_ALARM;
560 case CLOCK_BOOTTIME_ALARM:
561 return SOURCE_TIME_BOOTTIME_ALARM;
564 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
568 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
573 case SOURCE_TIME_REALTIME:
576 case SOURCE_TIME_BOOTTIME:
579 case SOURCE_TIME_MONOTONIC:
580 return &e->monotonic;
582 case SOURCE_TIME_REALTIME_ALARM:
583 return &e->realtime_alarm;
585 case SOURCE_TIME_BOOTTIME_ALARM:
586 return &e->boottime_alarm;
593 static bool need_signal(sd_event *e, int signal) {
594 return (e->signal_sources && e->signal_sources[signal] &&
595 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
597 (signal == SIGCHLD &&
598 e->n_enabled_child_sources > 0);
601 static int event_update_signal_fd(sd_event *e) {
602 struct epoll_event ev = {};
608 if (event_pid_changed(e))
611 add_to_epoll = e->signal_fd < 0;
613 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
623 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
625 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
627 e->signal_fd = safe_close(e->signal_fd);
634 static void source_disconnect(sd_event_source *s) {
642 assert(s->event->n_sources > 0);
648 source_io_unregister(s);
652 case SOURCE_TIME_REALTIME:
653 case SOURCE_TIME_BOOTTIME:
654 case SOURCE_TIME_MONOTONIC:
655 case SOURCE_TIME_REALTIME_ALARM:
656 case SOURCE_TIME_BOOTTIME_ALARM: {
657 struct clock_data *d;
659 d = event_get_clock_data(s->event, s->type);
662 prioq_remove(d->earliest, s, &s->time.earliest_index);
663 prioq_remove(d->latest, s, &s->time.latest_index);
664 d->needs_rearm = true;
669 if (s->signal.sig > 0) {
670 if (s->event->signal_sources)
671 s->event->signal_sources[s->signal.sig] = NULL;
673 /* If the signal was on and now it is off... */
674 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
675 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
677 (void) event_update_signal_fd(s->event);
678 /* If disabling failed, we might get a spurious event,
679 * but otherwise nothing bad should happen. */
686 if (s->child.pid > 0) {
687 if (s->enabled != SD_EVENT_OFF) {
688 assert(s->event->n_enabled_child_sources > 0);
689 s->event->n_enabled_child_sources--;
691 /* We know the signal was on, if it is off now... */
692 if (!need_signal(s->event, SIGCHLD)) {
693 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
695 (void) event_update_signal_fd(s->event);
696 /* If disabling failed, we might get a spurious event,
697 * but otherwise nothing bad should happen. */
701 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
711 set_remove(s->event->post_sources, s);
715 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
719 assert_not_reached("Wut? I shouldn't exist.");
723 prioq_remove(s->event->pending, s, &s->pending_index);
726 prioq_remove(s->event->prepare, s, &s->prepare_index);
730 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
732 LIST_REMOVE(sources, event->sources, s);
736 sd_event_unref(event);
739 static void source_free(sd_event_source *s) {
742 source_disconnect(s);
743 free(s->description);
747 static int source_set_pending(sd_event_source *s, bool b) {
751 assert(s->type != SOURCE_EXIT);
759 s->pending_iteration = s->event->iteration;
761 r = prioq_put(s->event->pending, s, &s->pending_index);
767 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
769 if (EVENT_SOURCE_IS_TIME(s->type)) {
770 struct clock_data *d;
772 d = event_get_clock_data(s->event, s->type);
775 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
776 prioq_reshuffle(d->latest, s, &s->time.latest_index);
777 d->needs_rearm = true;
783 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
788 s = new0(sd_event_source, 1);
794 s->floating = floating;
796 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
801 LIST_PREPEND(sources, e->sources, s);
807 _public_ int sd_event_add_io(
809 sd_event_source **ret,
812 sd_event_io_handler_t callback,
818 assert_return(e, -EINVAL);
819 assert_return(fd >= 0, -EINVAL);
820 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
821 assert_return(callback, -EINVAL);
822 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
823 assert_return(!event_pid_changed(e), -ECHILD);
825 s = source_new(e, !ret, SOURCE_IO);
830 s->io.events = events;
831 s->io.callback = callback;
832 s->userdata = userdata;
833 s->enabled = SD_EVENT_ON;
835 r = source_io_register(s, s->enabled, events);
847 static void initialize_perturb(sd_event *e) {
848 sd_id128_t bootid = {};
850 /* When we sleep for longer, we try to realign the wakeup to
851 the same time wihtin each minute/second/250ms, so that
852 events all across the system can be coalesced into a single
853 CPU wakeup. However, let's take some system-specific
854 randomness for this value, so that in a network of systems
855 with synced clocks timer events are distributed a
856 bit. Here, we calculate a perturbation usec offset from the
859 if (_likely_(e->perturb != USEC_INFINITY))
862 if (sd_id128_get_boot(&bootid) >= 0)
863 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
866 static int event_setup_timer_fd(
868 struct clock_data *d,
871 struct epoll_event ev = {};
877 if (_likely_(d->fd >= 0))
880 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
885 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
887 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
897 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
900 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
903 _public_ int sd_event_add_time(
905 sd_event_source **ret,
909 sd_event_time_handler_t callback,
912 EventSourceType type;
914 struct clock_data *d;
917 assert_return(e, -EINVAL);
918 assert_return(usec != (uint64_t) -1, -EINVAL);
919 assert_return(accuracy != (uint64_t) -1, -EINVAL);
920 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
921 assert_return(!event_pid_changed(e), -ECHILD);
924 callback = time_exit_callback;
926 type = clock_to_event_source_type(clock);
927 assert_return(type >= 0, -EOPNOTSUPP);
929 d = event_get_clock_data(e, type);
933 d->earliest = prioq_new(earliest_time_prioq_compare);
939 d->latest = prioq_new(latest_time_prioq_compare);
945 r = event_setup_timer_fd(e, d, clock);
950 s = source_new(e, !ret, type);
955 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
956 s->time.callback = callback;
957 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
958 s->userdata = userdata;
959 s->enabled = SD_EVENT_ONESHOT;
961 d->needs_rearm = true;
963 r = prioq_put(d->earliest, s, &s->time.earliest_index);
967 r = prioq_put(d->latest, s, &s->time.latest_index);
981 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
984 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
987 _public_ int sd_event_add_signal(
989 sd_event_source **ret,
991 sd_event_signal_handler_t callback,
999 assert_return(e, -EINVAL);
1000 assert_return(sig > 0, -EINVAL);
1001 assert_return(sig < _NSIG, -EINVAL);
1002 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1003 assert_return(!event_pid_changed(e), -ECHILD);
1006 callback = signal_exit_callback;
1008 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1012 if (!sigismember(&ss, sig))
1015 if (!e->signal_sources) {
1016 e->signal_sources = new0(sd_event_source*, _NSIG);
1017 if (!e->signal_sources)
1019 } else if (e->signal_sources[sig])
1022 previous = need_signal(e, sig);
1024 s = source_new(e, !ret, SOURCE_SIGNAL);
1028 s->signal.sig = sig;
1029 s->signal.callback = callback;
1030 s->userdata = userdata;
1031 s->enabled = SD_EVENT_ON;
1033 e->signal_sources[sig] = s;
1036 assert_se(sigaddset(&e->sigset, sig) == 0);
1038 r = event_update_signal_fd(e);
1045 /* Use the signal name as description for the event source by default */
1046 (void) sd_event_source_set_description(s, signal_to_string(sig));
1054 _public_ int sd_event_add_child(
1056 sd_event_source **ret,
1059 sd_event_child_handler_t callback,
1066 assert_return(e, -EINVAL);
1067 assert_return(pid > 1, -EINVAL);
1068 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1069 assert_return(options != 0, -EINVAL);
1070 assert_return(callback, -EINVAL);
1071 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1072 assert_return(!event_pid_changed(e), -ECHILD);
1074 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1078 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1081 previous = need_signal(e, SIGCHLD);
1083 s = source_new(e, !ret, SOURCE_CHILD);
1088 s->child.options = options;
1089 s->child.callback = callback;
1090 s->userdata = userdata;
1091 s->enabled = SD_EVENT_ONESHOT;
1093 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1099 e->n_enabled_child_sources ++;
1102 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1104 r = event_update_signal_fd(e);
1111 e->need_process_child = true;
1119 _public_ int sd_event_add_defer(
1121 sd_event_source **ret,
1122 sd_event_handler_t callback,
1128 assert_return(e, -EINVAL);
1129 assert_return(callback, -EINVAL);
1130 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1131 assert_return(!event_pid_changed(e), -ECHILD);
1133 s = source_new(e, !ret, SOURCE_DEFER);
1137 s->defer.callback = callback;
1138 s->userdata = userdata;
1139 s->enabled = SD_EVENT_ONESHOT;
1141 r = source_set_pending(s, true);
1153 _public_ int sd_event_add_post(
1155 sd_event_source **ret,
1156 sd_event_handler_t callback,
1162 assert_return(e, -EINVAL);
1163 assert_return(callback, -EINVAL);
1164 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1165 assert_return(!event_pid_changed(e), -ECHILD);
1167 r = set_ensure_allocated(&e->post_sources, NULL);
1171 s = source_new(e, !ret, SOURCE_POST);
1175 s->post.callback = callback;
1176 s->userdata = userdata;
1177 s->enabled = SD_EVENT_ON;
1179 r = set_put(e->post_sources, s);
1191 _public_ int sd_event_add_exit(
1193 sd_event_source **ret,
1194 sd_event_handler_t callback,
1200 assert_return(e, -EINVAL);
1201 assert_return(callback, -EINVAL);
1202 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1203 assert_return(!event_pid_changed(e), -ECHILD);
1206 e->exit = prioq_new(exit_prioq_compare);
1211 s = source_new(e, !ret, SOURCE_EXIT);
1215 s->exit.callback = callback;
1216 s->userdata = userdata;
1217 s->exit.prioq_index = PRIOQ_IDX_NULL;
1218 s->enabled = SD_EVENT_ONESHOT;
1220 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1232 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1233 assert_return(s, NULL);
1235 assert(s->n_ref >= 1);
1241 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1246 assert(s->n_ref >= 1);
1249 if (s->n_ref <= 0) {
1250 /* Here's a special hack: when we are called from a
1251 * dispatch handler we won't free the event source
1252 * immediately, but we will detach the fd from the
1253 * epoll. This way it is safe for the caller to unref
1254 * the event source and immediately close the fd, but
1255 * we still retain a valid event source object after
1258 if (s->dispatching) {
1259 if (s->type == SOURCE_IO)
1260 source_io_unregister(s);
1262 source_disconnect(s);
1270 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1271 assert_return(s, -EINVAL);
1272 assert_return(!event_pid_changed(s->event), -ECHILD);
1274 return free_and_strdup(&s->description, description);
1277 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1278 assert_return(s, -EINVAL);
1279 assert_return(description, -EINVAL);
1280 assert_return(s->description, -ENXIO);
1281 assert_return(!event_pid_changed(s->event), -ECHILD);
1283 *description = s->description;
1287 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1288 assert_return(s, NULL);
1293 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1294 assert_return(s, -EINVAL);
1295 assert_return(s->type != SOURCE_EXIT, -EDOM);
1296 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1297 assert_return(!event_pid_changed(s->event), -ECHILD);
1302 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1303 assert_return(s, -EINVAL);
1304 assert_return(s->type == SOURCE_IO, -EDOM);
1305 assert_return(!event_pid_changed(s->event), -ECHILD);
1310 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1313 assert_return(s, -EINVAL);
1314 assert_return(fd >= 0, -EINVAL);
1315 assert_return(s->type == SOURCE_IO, -EDOM);
1316 assert_return(!event_pid_changed(s->event), -ECHILD);
1321 if (s->enabled == SD_EVENT_OFF) {
1323 s->io.registered = false;
1327 saved_fd = s->io.fd;
1328 assert(s->io.registered);
1331 s->io.registered = false;
1333 r = source_io_register(s, s->enabled, s->io.events);
1335 s->io.fd = saved_fd;
1336 s->io.registered = true;
1340 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1346 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1347 assert_return(s, -EINVAL);
1348 assert_return(events, -EINVAL);
1349 assert_return(s->type == SOURCE_IO, -EDOM);
1350 assert_return(!event_pid_changed(s->event), -ECHILD);
1352 *events = s->io.events;
1356 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1359 assert_return(s, -EINVAL);
1360 assert_return(s->type == SOURCE_IO, -EDOM);
1361 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1362 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1363 assert_return(!event_pid_changed(s->event), -ECHILD);
1365 /* edge-triggered updates are never skipped, so we can reset edges */
1366 if (s->io.events == events && !(events & EPOLLET))
1369 if (s->enabled != SD_EVENT_OFF) {
1370 r = source_io_register(s, s->enabled, events);
1375 s->io.events = events;
1376 source_set_pending(s, false);
1381 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1382 assert_return(s, -EINVAL);
1383 assert_return(revents, -EINVAL);
1384 assert_return(s->type == SOURCE_IO, -EDOM);
1385 assert_return(s->pending, -ENODATA);
1386 assert_return(!event_pid_changed(s->event), -ECHILD);
1388 *revents = s->io.revents;
1392 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1393 assert_return(s, -EINVAL);
1394 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1395 assert_return(!event_pid_changed(s->event), -ECHILD);
1397 return s->signal.sig;
1400 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1401 assert_return(s, -EINVAL);
1402 assert_return(!event_pid_changed(s->event), -ECHILD);
1407 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1408 assert_return(s, -EINVAL);
1409 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1410 assert_return(!event_pid_changed(s->event), -ECHILD);
1412 if (s->priority == priority)
1415 s->priority = priority;
1418 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1421 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1423 if (s->type == SOURCE_EXIT)
1424 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1429 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1430 assert_return(s, -EINVAL);
1431 assert_return(m, -EINVAL);
1432 assert_return(!event_pid_changed(s->event), -ECHILD);
1438 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1441 assert_return(s, -EINVAL);
1442 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1443 assert_return(!event_pid_changed(s->event), -ECHILD);
1445 /* If we are dead anyway, we are fine with turning off
1446 * sources, but everything else needs to fail. */
1447 if (s->event->state == SD_EVENT_FINISHED)
1448 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1450 if (s->enabled == m)
1453 if (m == SD_EVENT_OFF) {
1458 source_io_unregister(s);
1462 case SOURCE_TIME_REALTIME:
1463 case SOURCE_TIME_BOOTTIME:
1464 case SOURCE_TIME_MONOTONIC:
1465 case SOURCE_TIME_REALTIME_ALARM:
1466 case SOURCE_TIME_BOOTTIME_ALARM: {
1467 struct clock_data *d;
1470 d = event_get_clock_data(s->event, s->type);
1473 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1474 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1475 d->needs_rearm = true;
1480 assert(need_signal(s->event, s->signal.sig));
1484 if (!need_signal(s->event, s->signal.sig)) {
1485 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1487 (void) event_update_signal_fd(s->event);
1488 /* If disabling failed, we might get a spurious event,
1489 * but otherwise nothing bad should happen. */
1495 assert(need_signal(s->event, SIGCHLD));
1499 assert(s->event->n_enabled_child_sources > 0);
1500 s->event->n_enabled_child_sources--;
1502 if (!need_signal(s->event, SIGCHLD)) {
1503 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1505 (void) event_update_signal_fd(s->event);
1512 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1521 assert_not_reached("Wut? I shouldn't exist.");
1528 r = source_io_register(s, m, s->io.events);
1535 case SOURCE_TIME_REALTIME:
1536 case SOURCE_TIME_BOOTTIME:
1537 case SOURCE_TIME_MONOTONIC:
1538 case SOURCE_TIME_REALTIME_ALARM:
1539 case SOURCE_TIME_BOOTTIME_ALARM: {
1540 struct clock_data *d;
1543 d = event_get_clock_data(s->event, s->type);
1546 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1547 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1548 d->needs_rearm = true;
1553 /* Check status before enabling. */
1554 if (!need_signal(s->event, s->signal.sig)) {
1555 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1557 r = event_update_signal_fd(s->event);
1559 s->enabled = SD_EVENT_OFF;
1568 /* Check status before enabling. */
1569 if (s->enabled == SD_EVENT_OFF) {
1570 if (!need_signal(s->event, SIGCHLD)) {
1571 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1573 r = event_update_signal_fd(s->event);
1575 s->enabled = SD_EVENT_OFF;
1580 s->event->n_enabled_child_sources++;
1588 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1597 assert_not_reached("Wut? I shouldn't exist.");
1602 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1605 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1610 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1611 assert_return(s, -EINVAL);
1612 assert_return(usec, -EINVAL);
1613 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1614 assert_return(!event_pid_changed(s->event), -ECHILD);
1616 *usec = s->time.next;
1620 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1621 struct clock_data *d;
1623 assert_return(s, -EINVAL);
1624 assert_return(usec != (uint64_t) -1, -EINVAL);
1625 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1626 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1627 assert_return(!event_pid_changed(s->event), -ECHILD);
1629 s->time.next = usec;
1631 source_set_pending(s, false);
1633 d = event_get_clock_data(s->event, s->type);
1636 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1637 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1638 d->needs_rearm = true;
1643 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1644 assert_return(s, -EINVAL);
1645 assert_return(usec, -EINVAL);
1646 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1647 assert_return(!event_pid_changed(s->event), -ECHILD);
1649 *usec = s->time.accuracy;
1653 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1654 struct clock_data *d;
1656 assert_return(s, -EINVAL);
1657 assert_return(usec != (uint64_t) -1, -EINVAL);
1658 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1659 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1660 assert_return(!event_pid_changed(s->event), -ECHILD);
1663 usec = DEFAULT_ACCURACY_USEC;
1665 s->time.accuracy = usec;
1667 source_set_pending(s, false);
1669 d = event_get_clock_data(s->event, s->type);
1672 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1673 d->needs_rearm = true;
1678 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1679 assert_return(s, -EINVAL);
1680 assert_return(clock, -EINVAL);
1681 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1682 assert_return(!event_pid_changed(s->event), -ECHILD);
1684 *clock = event_source_type_to_clock(s->type);
1688 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1689 assert_return(s, -EINVAL);
1690 assert_return(pid, -EINVAL);
1691 assert_return(s->type == SOURCE_CHILD, -EDOM);
1692 assert_return(!event_pid_changed(s->event), -ECHILD);
1694 *pid = s->child.pid;
1698 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1701 assert_return(s, -EINVAL);
1702 assert_return(s->type != SOURCE_EXIT, -EDOM);
1703 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1704 assert_return(!event_pid_changed(s->event), -ECHILD);
1706 if (s->prepare == callback)
1709 if (callback && s->prepare) {
1710 s->prepare = callback;
1714 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1718 s->prepare = callback;
1721 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1725 prioq_remove(s->event->prepare, s, &s->prepare_index);
1730 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1731 assert_return(s, NULL);
1736 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1739 assert_return(s, NULL);
1742 s->userdata = userdata;
1747 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1758 initialize_perturb(e);
1761 Find a good time to wake up again between times a and b. We
1762 have two goals here:
1764 a) We want to wake up as seldom as possible, hence prefer
1765 later times over earlier times.
1767 b) But if we have to wake up, then let's make sure to
1768 dispatch as much as possible on the entire system.
1770 We implement this by waking up everywhere at the same time
1771 within any given minute if we can, synchronised via the
1772 perturbation value determined from the boot ID. If we can't,
1773 then we try to find the same spot in every 10s, then 1s and
1774 then 250ms step. Otherwise, we pick the last possible time
1778 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1780 if (_unlikely_(c < USEC_PER_MINUTE))
1783 c -= USEC_PER_MINUTE;
1789 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1791 if (_unlikely_(c < USEC_PER_SEC*10))
1794 c -= USEC_PER_SEC*10;
1800 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1802 if (_unlikely_(c < USEC_PER_SEC))
1811 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1813 if (_unlikely_(c < USEC_PER_MSEC*250))
1816 c -= USEC_PER_MSEC*250;
1825 static int event_arm_timer(
1827 struct clock_data *d) {
1829 struct itimerspec its = {};
1830 sd_event_source *a, *b;
1837 if (!d->needs_rearm)
1840 d->needs_rearm = false;
1842 a = prioq_peek(d->earliest);
1843 if (!a || a->enabled == SD_EVENT_OFF) {
1848 if (d->next == USEC_INFINITY)
1852 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1856 d->next = USEC_INFINITY;
1860 b = prioq_peek(d->latest);
1861 assert_se(b && b->enabled != SD_EVENT_OFF);
1863 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1867 assert_se(d->fd >= 0);
1870 /* We don' want to disarm here, just mean some time looooong ago. */
1871 its.it_value.tv_sec = 0;
1872 its.it_value.tv_nsec = 1;
1874 timespec_store(&its.it_value, t);
1876 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1884 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1887 assert(s->type == SOURCE_IO);
1889 /* If the event source was already pending, we just OR in the
1890 * new revents, otherwise we reset the value. The ORing is
1891 * necessary to handle EPOLLONESHOT events properly where
1892 * readability might happen independently of writability, and
1893 * we need to keep track of both */
1896 s->io.revents |= revents;
1898 s->io.revents = revents;
1900 return source_set_pending(s, true);
1903 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1910 assert_return(events == EPOLLIN, -EIO);
1912 ss = read(fd, &x, sizeof(x));
1914 if (errno == EAGAIN || errno == EINTR)
1920 if (_unlikely_(ss != sizeof(x)))
1924 *next = USEC_INFINITY;
1929 static int process_timer(
1932 struct clock_data *d) {
1941 s = prioq_peek(d->earliest);
1944 s->enabled == SD_EVENT_OFF ||
1948 r = source_set_pending(s, true);
1952 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1953 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1954 d->needs_rearm = true;
1960 static int process_child(sd_event *e) {
1967 e->need_process_child = false;
1970 So, this is ugly. We iteratively invoke waitid() with P_PID
1971 + WNOHANG for each PID we wait for, instead of using
1972 P_ALL. This is because we only want to get child
1973 information of very specific child processes, and not all
1974 of them. We might not have processed the SIGCHLD even of a
1975 previous invocation and we don't want to maintain a
1976 unbounded *per-child* event queue, hence we really don't
1977 want anything flushed out of the kernel's queue that we
1978 don't care about. Since this is O(n) this means that if you
1979 have a lot of processes you probably want to handle SIGCHLD
1982 We do not reap the children here (by using WNOWAIT), this
1983 is only done after the event source is dispatched so that
1984 the callback still sees the process as a zombie.
1987 HASHMAP_FOREACH(s, e->child_sources, i) {
1988 assert(s->type == SOURCE_CHILD);
1993 if (s->enabled == SD_EVENT_OFF)
1996 zero(s->child.siginfo);
1997 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
1998 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2002 if (s->child.siginfo.si_pid != 0) {
2004 s->child.siginfo.si_code == CLD_EXITED ||
2005 s->child.siginfo.si_code == CLD_KILLED ||
2006 s->child.siginfo.si_code == CLD_DUMPED;
2008 if (!zombie && (s->child.options & WEXITED)) {
2009 /* If the child isn't dead then let's
2010 * immediately remove the state change
2011 * from the queue, since there's no
2012 * benefit in leaving it queued */
2014 assert(s->child.options & (WSTOPPED|WCONTINUED));
2015 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2018 r = source_set_pending(s, true);
2027 static int process_signal(sd_event *e, uint32_t events) {
2028 bool read_one = false;
2033 assert_return(events == EPOLLIN, -EIO);
2036 struct signalfd_siginfo si;
2038 sd_event_source *s = NULL;
2040 n = read(e->signal_fd, &si, sizeof(si));
2042 if (errno == EAGAIN || errno == EINTR)
2048 if (_unlikely_(n != sizeof(si)))
2051 assert(si.ssi_signo < _NSIG);
2055 if (si.ssi_signo == SIGCHLD) {
2056 r = process_child(e);
2063 if (e->signal_sources)
2064 s = e->signal_sources[si.ssi_signo];
2069 s->signal.siginfo = si;
2070 r = source_set_pending(s, true);
2076 static int source_dispatch(sd_event_source *s) {
2080 assert(s->pending || s->type == SOURCE_EXIT);
2082 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2083 r = source_set_pending(s, false);
2088 if (s->type != SOURCE_POST) {
2092 /* If we execute a non-post source, let's mark all
2093 * post sources as pending */
2095 SET_FOREACH(z, s->event->post_sources, i) {
2096 if (z->enabled == SD_EVENT_OFF)
2099 r = source_set_pending(z, true);
2105 if (s->enabled == SD_EVENT_ONESHOT) {
2106 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2111 s->dispatching = true;
2116 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2119 case SOURCE_TIME_REALTIME:
2120 case SOURCE_TIME_BOOTTIME:
2121 case SOURCE_TIME_MONOTONIC:
2122 case SOURCE_TIME_REALTIME_ALARM:
2123 case SOURCE_TIME_BOOTTIME_ALARM:
2124 r = s->time.callback(s, s->time.next, s->userdata);
2128 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2131 case SOURCE_CHILD: {
2134 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2135 s->child.siginfo.si_code == CLD_KILLED ||
2136 s->child.siginfo.si_code == CLD_DUMPED;
2138 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2140 /* Now, reap the PID for good. */
2142 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2148 r = s->defer.callback(s, s->userdata);
2152 r = s->post.callback(s, s->userdata);
2156 r = s->exit.callback(s, s->userdata);
2159 case SOURCE_WATCHDOG:
2160 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2161 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2162 assert_not_reached("Wut? I shouldn't exist.");
2165 s->dispatching = false;
2169 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2171 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2177 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2182 static int event_prepare(sd_event *e) {
2190 s = prioq_peek(e->prepare);
2191 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2194 s->prepare_iteration = e->iteration;
2195 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2201 s->dispatching = true;
2202 r = s->prepare(s, s->userdata);
2203 s->dispatching = false;
2207 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2209 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2215 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2221 static int dispatch_exit(sd_event *e) {
2227 p = prioq_peek(e->exit);
2228 if (!p || p->enabled == SD_EVENT_OFF) {
2229 e->state = SD_EVENT_FINISHED;
2235 e->state = SD_EVENT_EXITING;
2237 r = source_dispatch(p);
2239 e->state = SD_EVENT_INITIAL;
2245 static sd_event_source* event_next_pending(sd_event *e) {
2250 p = prioq_peek(e->pending);
2254 if (p->enabled == SD_EVENT_OFF)
2260 static int arm_watchdog(sd_event *e) {
2261 struct itimerspec its = {};
2266 assert(e->watchdog_fd >= 0);
2268 t = sleep_between(e,
2269 e->watchdog_last + (e->watchdog_period / 2),
2270 e->watchdog_last + (e->watchdog_period * 3 / 4));
2272 timespec_store(&its.it_value, t);
2274 /* Make sure we never set the watchdog to 0, which tells the
2275 * kernel to disable it. */
2276 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2277 its.it_value.tv_nsec = 1;
2279 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2286 static int process_watchdog(sd_event *e) {
2292 /* Don't notify watchdog too often */
2293 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2296 sd_notify(false, "WATCHDOG=1");
2297 e->watchdog_last = e->timestamp.monotonic;
2299 return arm_watchdog(e);
2302 _public_ int sd_event_prepare(sd_event *e) {
2305 assert_return(e, -EINVAL);
2306 assert_return(!event_pid_changed(e), -ECHILD);
2307 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2308 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2310 if (e->exit_requested)
2315 r = event_prepare(e);
2319 r = event_arm_timer(e, &e->realtime);
2323 r = event_arm_timer(e, &e->boottime);
2327 r = event_arm_timer(e, &e->monotonic);
2331 r = event_arm_timer(e, &e->realtime_alarm);
2335 r = event_arm_timer(e, &e->boottime_alarm);
2339 if (event_next_pending(e) || e->need_process_child)
2342 e->state = SD_EVENT_ARMED;
2347 e->state = SD_EVENT_ARMED;
2348 r = sd_event_wait(e, 0);
2350 e->state = SD_EVENT_ARMED;
2355 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2356 struct epoll_event *ev_queue;
2357 unsigned ev_queue_max;
2360 assert_return(e, -EINVAL);
2361 assert_return(!event_pid_changed(e), -ECHILD);
2362 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2363 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2365 if (e->exit_requested) {
2366 e->state = SD_EVENT_PENDING;
2370 ev_queue_max = MAX(e->n_sources, 1u);
2371 ev_queue = newa(struct epoll_event, ev_queue_max);
2373 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2374 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2376 if (errno == EINTR) {
2377 e->state = SD_EVENT_PENDING;
2385 dual_timestamp_get(&e->timestamp);
2386 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2388 for (i = 0; i < m; i++) {
2390 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2391 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2392 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2393 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2394 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2395 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2396 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2397 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2398 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2399 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2400 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2401 r = process_signal(e, ev_queue[i].events);
2402 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2403 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2405 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2411 r = process_watchdog(e);
2415 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2419 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2423 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2427 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2431 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2435 if (e->need_process_child) {
2436 r = process_child(e);
2441 if (event_next_pending(e)) {
2442 e->state = SD_EVENT_PENDING;
2450 e->state = SD_EVENT_INITIAL;
2455 _public_ int sd_event_dispatch(sd_event *e) {
2459 assert_return(e, -EINVAL);
2460 assert_return(!event_pid_changed(e), -ECHILD);
2461 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2462 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2464 if (e->exit_requested)
2465 return dispatch_exit(e);
2467 p = event_next_pending(e);
2471 e->state = SD_EVENT_RUNNING;
2472 r = source_dispatch(p);
2473 e->state = SD_EVENT_INITIAL;
2480 e->state = SD_EVENT_INITIAL;
2485 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2488 assert_return(e, -EINVAL);
2489 assert_return(!event_pid_changed(e), -ECHILD);
2490 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2491 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2493 r = sd_event_prepare(e);
2495 /* There was nothing? Then wait... */
2496 r = sd_event_wait(e, timeout);
2499 /* There's something now, then let's dispatch it */
2500 r = sd_event_dispatch(e);
2510 _public_ int sd_event_loop(sd_event *e) {
2513 assert_return(e, -EINVAL);
2514 assert_return(!event_pid_changed(e), -ECHILD);
2515 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2519 while (e->state != SD_EVENT_FINISHED) {
2520 r = sd_event_run(e, (uint64_t) -1);
2532 _public_ int sd_event_get_fd(sd_event *e) {
2534 assert_return(e, -EINVAL);
2535 assert_return(!event_pid_changed(e), -ECHILD);
2540 _public_ int sd_event_get_state(sd_event *e) {
2541 assert_return(e, -EINVAL);
2542 assert_return(!event_pid_changed(e), -ECHILD);
2547 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2548 assert_return(e, -EINVAL);
2549 assert_return(code, -EINVAL);
2550 assert_return(!event_pid_changed(e), -ECHILD);
2552 if (!e->exit_requested)
2555 *code = e->exit_code;
2559 _public_ int sd_event_exit(sd_event *e, int code) {
2560 assert_return(e, -EINVAL);
2561 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2562 assert_return(!event_pid_changed(e), -ECHILD);
2564 e->exit_requested = true;
2565 e->exit_code = code;
2570 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2571 assert_return(e, -EINVAL);
2572 assert_return(usec, -EINVAL);
2573 assert_return(!event_pid_changed(e), -ECHILD);
2575 /* If we haven't run yet, just get the actual time */
2576 if (!dual_timestamp_is_set(&e->timestamp))
2581 case CLOCK_REALTIME:
2582 case CLOCK_REALTIME_ALARM:
2583 *usec = e->timestamp.realtime;
2586 case CLOCK_MONOTONIC:
2587 *usec = e->timestamp.monotonic;
2590 case CLOCK_BOOTTIME:
2591 case CLOCK_BOOTTIME_ALARM:
2592 *usec = e->timestamp_boottime;
2599 _public_ int sd_event_default(sd_event **ret) {
2601 static thread_local sd_event *default_event = NULL;
2606 return !!default_event;
2608 if (default_event) {
2609 *ret = sd_event_ref(default_event);
2613 r = sd_event_new(&e);
2617 e->default_event_ptr = &default_event;
2625 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2626 assert_return(e, -EINVAL);
2627 assert_return(tid, -EINVAL);
2628 assert_return(!event_pid_changed(e), -ECHILD);
2638 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2641 assert_return(e, -EINVAL);
2642 assert_return(!event_pid_changed(e), -ECHILD);
2644 if (e->watchdog == !!b)
2648 struct epoll_event ev = {};
2650 r = sd_watchdog_enabled(false, &e->watchdog_period);
2654 /* Issue first ping immediately */
2655 sd_notify(false, "WATCHDOG=1");
2656 e->watchdog_last = now(CLOCK_MONOTONIC);
2658 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2659 if (e->watchdog_fd < 0)
2662 r = arm_watchdog(e);
2666 ev.events = EPOLLIN;
2667 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2669 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2676 if (e->watchdog_fd >= 0) {
2677 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2678 e->watchdog_fd = safe_close(e->watchdog_fd);
2686 e->watchdog_fd = safe_close(e->watchdog_fd);
2690 _public_ int sd_event_get_watchdog(sd_event *e) {
2691 assert_return(e, -EINVAL);
2692 assert_return(!event_pid_changed(e), -ECHILD);