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);
485 log_debug_errno(errno, "Failed to remove source %s from epoll: %m", strna(s->description));
487 s->io.registered = false;
490 static int source_io_register(
495 struct epoll_event ev = {};
499 assert(s->type == SOURCE_IO);
500 assert(enabled != SD_EVENT_OFF);
505 if (enabled == SD_EVENT_ONESHOT)
506 ev.events |= EPOLLONESHOT;
508 if (s->io.registered)
509 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_MOD, s->io.fd, &ev);
511 r = epoll_ctl(s->event->epoll_fd, EPOLL_CTL_ADD, s->io.fd, &ev);
516 s->io.registered = true;
521 /// UNNEEDED by elogind
523 static clockid_t event_source_type_to_clock(EventSourceType t) {
527 case SOURCE_TIME_REALTIME:
528 return CLOCK_REALTIME;
530 case SOURCE_TIME_BOOTTIME:
531 return CLOCK_BOOTTIME;
533 case SOURCE_TIME_MONOTONIC:
534 return CLOCK_MONOTONIC;
536 case SOURCE_TIME_REALTIME_ALARM:
537 return CLOCK_REALTIME_ALARM;
539 case SOURCE_TIME_BOOTTIME_ALARM:
540 return CLOCK_BOOTTIME_ALARM;
543 return (clockid_t) -1;
548 static EventSourceType clock_to_event_source_type(clockid_t clock) {
553 return SOURCE_TIME_REALTIME;
556 return SOURCE_TIME_BOOTTIME;
558 case CLOCK_MONOTONIC:
559 return SOURCE_TIME_MONOTONIC;
561 case CLOCK_REALTIME_ALARM:
562 return SOURCE_TIME_REALTIME_ALARM;
564 case CLOCK_BOOTTIME_ALARM:
565 return SOURCE_TIME_BOOTTIME_ALARM;
568 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
572 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
577 case SOURCE_TIME_REALTIME:
580 case SOURCE_TIME_BOOTTIME:
583 case SOURCE_TIME_MONOTONIC:
584 return &e->monotonic;
586 case SOURCE_TIME_REALTIME_ALARM:
587 return &e->realtime_alarm;
589 case SOURCE_TIME_BOOTTIME_ALARM:
590 return &e->boottime_alarm;
597 static bool need_signal(sd_event *e, int signal) {
598 return (e->signal_sources && e->signal_sources[signal] &&
599 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
601 (signal == SIGCHLD &&
602 e->n_enabled_child_sources > 0);
605 static int event_update_signal_fd(sd_event *e) {
606 struct epoll_event ev = {};
612 if (event_pid_changed(e))
615 add_to_epoll = e->signal_fd < 0;
617 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
627 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
629 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
631 e->signal_fd = safe_close(e->signal_fd);
638 static void source_disconnect(sd_event_source *s) {
646 assert(s->event->n_sources > 0);
652 source_io_unregister(s);
656 case SOURCE_TIME_REALTIME:
657 case SOURCE_TIME_BOOTTIME:
658 case SOURCE_TIME_MONOTONIC:
659 case SOURCE_TIME_REALTIME_ALARM:
660 case SOURCE_TIME_BOOTTIME_ALARM: {
661 struct clock_data *d;
663 d = event_get_clock_data(s->event, s->type);
666 prioq_remove(d->earliest, s, &s->time.earliest_index);
667 prioq_remove(d->latest, s, &s->time.latest_index);
668 d->needs_rearm = true;
673 if (s->signal.sig > 0) {
674 if (s->event->signal_sources)
675 s->event->signal_sources[s->signal.sig] = NULL;
677 /* If the signal was on and now it is off... */
678 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
679 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
681 (void) event_update_signal_fd(s->event);
682 /* If disabling failed, we might get a spurious event,
683 * but otherwise nothing bad should happen. */
690 if (s->child.pid > 0) {
691 if (s->enabled != SD_EVENT_OFF) {
692 assert(s->event->n_enabled_child_sources > 0);
693 s->event->n_enabled_child_sources--;
695 /* We know the signal was on, if it is off now... */
696 if (!need_signal(s->event, SIGCHLD)) {
697 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
699 (void) event_update_signal_fd(s->event);
700 /* If disabling failed, we might get a spurious event,
701 * but otherwise nothing bad should happen. */
705 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
715 set_remove(s->event->post_sources, s);
719 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
723 assert_not_reached("Wut? I shouldn't exist.");
727 prioq_remove(s->event->pending, s, &s->pending_index);
730 prioq_remove(s->event->prepare, s, &s->prepare_index);
734 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
736 LIST_REMOVE(sources, event->sources, s);
740 sd_event_unref(event);
743 static void source_free(sd_event_source *s) {
746 source_disconnect(s);
747 free(s->description);
751 static int source_set_pending(sd_event_source *s, bool b) {
755 assert(s->type != SOURCE_EXIT);
763 s->pending_iteration = s->event->iteration;
765 r = prioq_put(s->event->pending, s, &s->pending_index);
771 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
773 if (EVENT_SOURCE_IS_TIME(s->type)) {
774 struct clock_data *d;
776 d = event_get_clock_data(s->event, s->type);
779 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
780 prioq_reshuffle(d->latest, s, &s->time.latest_index);
781 d->needs_rearm = true;
787 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
792 s = new0(sd_event_source, 1);
798 s->floating = floating;
800 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
805 LIST_PREPEND(sources, e->sources, s);
811 _public_ int sd_event_add_io(
813 sd_event_source **ret,
816 sd_event_io_handler_t callback,
822 assert_return(e, -EINVAL);
823 assert_return(fd >= 0, -EBADF);
824 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
825 assert_return(callback, -EINVAL);
826 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
827 assert_return(!event_pid_changed(e), -ECHILD);
829 s = source_new(e, !ret, SOURCE_IO);
834 s->io.events = events;
835 s->io.callback = callback;
836 s->userdata = userdata;
837 s->enabled = SD_EVENT_ON;
839 r = source_io_register(s, s->enabled, events);
851 static void initialize_perturb(sd_event *e) {
852 sd_id128_t bootid = {};
854 /* When we sleep for longer, we try to realign the wakeup to
855 the same time wihtin each minute/second/250ms, so that
856 events all across the system can be coalesced into a single
857 CPU wakeup. However, let's take some system-specific
858 randomness for this value, so that in a network of systems
859 with synced clocks timer events are distributed a
860 bit. Here, we calculate a perturbation usec offset from the
863 if (_likely_(e->perturb != USEC_INFINITY))
866 if (sd_id128_get_boot(&bootid) >= 0)
867 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
870 static int event_setup_timer_fd(
872 struct clock_data *d,
875 struct epoll_event ev = {};
881 if (_likely_(d->fd >= 0))
884 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
889 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
891 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
901 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
904 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
907 _public_ int sd_event_add_time(
909 sd_event_source **ret,
913 sd_event_time_handler_t callback,
916 EventSourceType type;
918 struct clock_data *d;
921 assert_return(e, -EINVAL);
922 assert_return(usec != (uint64_t) -1, -EINVAL);
923 assert_return(accuracy != (uint64_t) -1, -EINVAL);
924 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
925 assert_return(!event_pid_changed(e), -ECHILD);
928 callback = time_exit_callback;
930 type = clock_to_event_source_type(clock);
931 assert_return(type >= 0, -EOPNOTSUPP);
933 d = event_get_clock_data(e, type);
937 d->earliest = prioq_new(earliest_time_prioq_compare);
943 d->latest = prioq_new(latest_time_prioq_compare);
949 r = event_setup_timer_fd(e, d, clock);
954 s = source_new(e, !ret, type);
959 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
960 s->time.callback = callback;
961 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
962 s->userdata = userdata;
963 s->enabled = SD_EVENT_ONESHOT;
965 d->needs_rearm = true;
967 r = prioq_put(d->earliest, s, &s->time.earliest_index);
971 r = prioq_put(d->latest, s, &s->time.latest_index);
985 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
988 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
991 _public_ int sd_event_add_signal(
993 sd_event_source **ret,
995 sd_event_signal_handler_t callback,
1003 assert_return(e, -EINVAL);
1004 assert_return(sig > 0, -EINVAL);
1005 assert_return(sig < _NSIG, -EINVAL);
1006 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1007 assert_return(!event_pid_changed(e), -ECHILD);
1010 callback = signal_exit_callback;
1012 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1016 if (!sigismember(&ss, sig))
1019 if (!e->signal_sources) {
1020 e->signal_sources = new0(sd_event_source*, _NSIG);
1021 if (!e->signal_sources)
1023 } else if (e->signal_sources[sig])
1026 previous = need_signal(e, sig);
1028 s = source_new(e, !ret, SOURCE_SIGNAL);
1032 s->signal.sig = sig;
1033 s->signal.callback = callback;
1034 s->userdata = userdata;
1035 s->enabled = SD_EVENT_ON;
1037 e->signal_sources[sig] = s;
1040 assert_se(sigaddset(&e->sigset, sig) == 0);
1042 r = event_update_signal_fd(e);
1049 /* Use the signal name as description for the event source by default */
1050 (void) sd_event_source_set_description(s, signal_to_string(sig));
1058 _public_ int sd_event_add_child(
1060 sd_event_source **ret,
1063 sd_event_child_handler_t callback,
1070 assert_return(e, -EINVAL);
1071 assert_return(pid > 1, -EINVAL);
1072 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1073 assert_return(options != 0, -EINVAL);
1074 assert_return(callback, -EINVAL);
1075 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1076 assert_return(!event_pid_changed(e), -ECHILD);
1078 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1082 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1085 previous = need_signal(e, SIGCHLD);
1087 s = source_new(e, !ret, SOURCE_CHILD);
1092 s->child.options = options;
1093 s->child.callback = callback;
1094 s->userdata = userdata;
1095 s->enabled = SD_EVENT_ONESHOT;
1097 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1103 e->n_enabled_child_sources ++;
1106 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1108 r = event_update_signal_fd(e);
1115 e->need_process_child = true;
1123 _public_ int sd_event_add_defer(
1125 sd_event_source **ret,
1126 sd_event_handler_t callback,
1132 assert_return(e, -EINVAL);
1133 assert_return(callback, -EINVAL);
1134 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1135 assert_return(!event_pid_changed(e), -ECHILD);
1137 s = source_new(e, !ret, SOURCE_DEFER);
1141 s->defer.callback = callback;
1142 s->userdata = userdata;
1143 s->enabled = SD_EVENT_ONESHOT;
1145 r = source_set_pending(s, true);
1157 _public_ int sd_event_add_post(
1159 sd_event_source **ret,
1160 sd_event_handler_t callback,
1166 assert_return(e, -EINVAL);
1167 assert_return(callback, -EINVAL);
1168 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1169 assert_return(!event_pid_changed(e), -ECHILD);
1171 r = set_ensure_allocated(&e->post_sources, NULL);
1175 s = source_new(e, !ret, SOURCE_POST);
1179 s->post.callback = callback;
1180 s->userdata = userdata;
1181 s->enabled = SD_EVENT_ON;
1183 r = set_put(e->post_sources, s);
1195 _public_ int sd_event_add_exit(
1197 sd_event_source **ret,
1198 sd_event_handler_t callback,
1204 assert_return(e, -EINVAL);
1205 assert_return(callback, -EINVAL);
1206 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1207 assert_return(!event_pid_changed(e), -ECHILD);
1210 e->exit = prioq_new(exit_prioq_compare);
1215 s = source_new(e, !ret, SOURCE_EXIT);
1219 s->exit.callback = callback;
1220 s->userdata = userdata;
1221 s->exit.prioq_index = PRIOQ_IDX_NULL;
1222 s->enabled = SD_EVENT_ONESHOT;
1224 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1236 /// UNNEEDED by elogind
1238 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1239 assert_return(s, NULL);
1241 assert(s->n_ref >= 1);
1248 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1253 assert(s->n_ref >= 1);
1256 if (s->n_ref <= 0) {
1257 /* Here's a special hack: when we are called from a
1258 * dispatch handler we won't free the event source
1259 * immediately, but we will detach the fd from the
1260 * epoll. This way it is safe for the caller to unref
1261 * the event source and immediately close the fd, but
1262 * we still retain a valid event source object after
1265 if (s->dispatching) {
1266 if (s->type == SOURCE_IO)
1267 source_io_unregister(s);
1269 source_disconnect(s);
1277 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1278 assert_return(s, -EINVAL);
1279 assert_return(!event_pid_changed(s->event), -ECHILD);
1281 return free_and_strdup(&s->description, description);
1284 /// UNNEEDED by elogind
1286 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1287 assert_return(s, -EINVAL);
1288 assert_return(description, -EINVAL);
1289 assert_return(s->description, -ENXIO);
1290 assert_return(!event_pid_changed(s->event), -ECHILD);
1292 *description = s->description;
1297 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1298 assert_return(s, NULL);
1303 /// UNNEEDED by elogind
1305 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1306 assert_return(s, -EINVAL);
1307 assert_return(s->type != SOURCE_EXIT, -EDOM);
1308 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1309 assert_return(!event_pid_changed(s->event), -ECHILD);
1314 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1315 assert_return(s, -EINVAL);
1316 assert_return(s->type == SOURCE_IO, -EDOM);
1317 assert_return(!event_pid_changed(s->event), -ECHILD);
1323 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1326 assert_return(s, -EINVAL);
1327 assert_return(fd >= 0, -EBADF);
1328 assert_return(s->type == SOURCE_IO, -EDOM);
1329 assert_return(!event_pid_changed(s->event), -ECHILD);
1334 if (s->enabled == SD_EVENT_OFF) {
1336 s->io.registered = false;
1340 saved_fd = s->io.fd;
1341 assert(s->io.registered);
1344 s->io.registered = false;
1346 r = source_io_register(s, s->enabled, s->io.events);
1348 s->io.fd = saved_fd;
1349 s->io.registered = true;
1353 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1359 /// UNNEEDED by elogind
1361 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1362 assert_return(s, -EINVAL);
1363 assert_return(events, -EINVAL);
1364 assert_return(s->type == SOURCE_IO, -EDOM);
1365 assert_return(!event_pid_changed(s->event), -ECHILD);
1367 *events = s->io.events;
1372 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1375 assert_return(s, -EINVAL);
1376 assert_return(s->type == SOURCE_IO, -EDOM);
1377 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1378 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1379 assert_return(!event_pid_changed(s->event), -ECHILD);
1381 /* edge-triggered updates are never skipped, so we can reset edges */
1382 if (s->io.events == events && !(events & EPOLLET))
1385 if (s->enabled != SD_EVENT_OFF) {
1386 r = source_io_register(s, s->enabled, events);
1391 s->io.events = events;
1392 source_set_pending(s, false);
1397 /// UNNEEDED by elogind
1399 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1400 assert_return(s, -EINVAL);
1401 assert_return(revents, -EINVAL);
1402 assert_return(s->type == SOURCE_IO, -EDOM);
1403 assert_return(s->pending, -ENODATA);
1404 assert_return(!event_pid_changed(s->event), -ECHILD);
1406 *revents = s->io.revents;
1410 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1411 assert_return(s, -EINVAL);
1412 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1413 assert_return(!event_pid_changed(s->event), -ECHILD);
1415 return s->signal.sig;
1418 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1419 assert_return(s, -EINVAL);
1420 assert_return(!event_pid_changed(s->event), -ECHILD);
1426 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1427 assert_return(s, -EINVAL);
1428 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1429 assert_return(!event_pid_changed(s->event), -ECHILD);
1431 if (s->priority == priority)
1434 s->priority = priority;
1437 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1440 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1442 if (s->type == SOURCE_EXIT)
1443 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1448 /// UNNEEDED by elogind
1450 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1451 assert_return(s, -EINVAL);
1452 assert_return(m, -EINVAL);
1453 assert_return(!event_pid_changed(s->event), -ECHILD);
1460 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1463 assert_return(s, -EINVAL);
1464 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1465 assert_return(!event_pid_changed(s->event), -ECHILD);
1467 /* If we are dead anyway, we are fine with turning off
1468 * sources, but everything else needs to fail. */
1469 if (s->event->state == SD_EVENT_FINISHED)
1470 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1472 if (s->enabled == m)
1475 if (m == SD_EVENT_OFF) {
1480 source_io_unregister(s);
1484 case SOURCE_TIME_REALTIME:
1485 case SOURCE_TIME_BOOTTIME:
1486 case SOURCE_TIME_MONOTONIC:
1487 case SOURCE_TIME_REALTIME_ALARM:
1488 case SOURCE_TIME_BOOTTIME_ALARM: {
1489 struct clock_data *d;
1492 d = event_get_clock_data(s->event, s->type);
1495 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1496 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1497 d->needs_rearm = true;
1502 assert(need_signal(s->event, s->signal.sig));
1506 if (!need_signal(s->event, s->signal.sig)) {
1507 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1509 (void) event_update_signal_fd(s->event);
1510 /* If disabling failed, we might get a spurious event,
1511 * but otherwise nothing bad should happen. */
1517 assert(need_signal(s->event, SIGCHLD));
1521 assert(s->event->n_enabled_child_sources > 0);
1522 s->event->n_enabled_child_sources--;
1524 if (!need_signal(s->event, SIGCHLD)) {
1525 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1527 (void) event_update_signal_fd(s->event);
1534 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1543 assert_not_reached("Wut? I shouldn't exist.");
1550 r = source_io_register(s, m, s->io.events);
1557 case SOURCE_TIME_REALTIME:
1558 case SOURCE_TIME_BOOTTIME:
1559 case SOURCE_TIME_MONOTONIC:
1560 case SOURCE_TIME_REALTIME_ALARM:
1561 case SOURCE_TIME_BOOTTIME_ALARM: {
1562 struct clock_data *d;
1565 d = event_get_clock_data(s->event, s->type);
1568 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1569 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1570 d->needs_rearm = true;
1575 /* Check status before enabling. */
1576 if (!need_signal(s->event, s->signal.sig)) {
1577 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1579 r = event_update_signal_fd(s->event);
1581 s->enabled = SD_EVENT_OFF;
1590 /* Check status before enabling. */
1591 if (s->enabled == SD_EVENT_OFF) {
1592 if (!need_signal(s->event, SIGCHLD)) {
1593 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1595 r = event_update_signal_fd(s->event);
1597 s->enabled = SD_EVENT_OFF;
1602 s->event->n_enabled_child_sources++;
1610 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1619 assert_not_reached("Wut? I shouldn't exist.");
1624 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1627 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1632 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1633 assert_return(s, -EINVAL);
1634 assert_return(usec, -EINVAL);
1635 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1636 assert_return(!event_pid_changed(s->event), -ECHILD);
1638 *usec = s->time.next;
1642 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1643 struct clock_data *d;
1645 assert_return(s, -EINVAL);
1646 assert_return(usec != (uint64_t) -1, -EINVAL);
1647 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1648 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1649 assert_return(!event_pid_changed(s->event), -ECHILD);
1651 s->time.next = usec;
1653 source_set_pending(s, false);
1655 d = event_get_clock_data(s->event, s->type);
1658 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1659 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1660 d->needs_rearm = true;
1665 /// UNNEEDED by elogind
1667 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1668 assert_return(s, -EINVAL);
1669 assert_return(usec, -EINVAL);
1670 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1671 assert_return(!event_pid_changed(s->event), -ECHILD);
1673 *usec = s->time.accuracy;
1677 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1678 struct clock_data *d;
1680 assert_return(s, -EINVAL);
1681 assert_return(usec != (uint64_t) -1, -EINVAL);
1682 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1683 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1684 assert_return(!event_pid_changed(s->event), -ECHILD);
1687 usec = DEFAULT_ACCURACY_USEC;
1689 s->time.accuracy = usec;
1691 source_set_pending(s, false);
1693 d = event_get_clock_data(s->event, s->type);
1696 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1697 d->needs_rearm = true;
1702 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1703 assert_return(s, -EINVAL);
1704 assert_return(clock, -EINVAL);
1705 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1706 assert_return(!event_pid_changed(s->event), -ECHILD);
1708 *clock = event_source_type_to_clock(s->type);
1712 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1713 assert_return(s, -EINVAL);
1714 assert_return(pid, -EINVAL);
1715 assert_return(s->type == SOURCE_CHILD, -EDOM);
1716 assert_return(!event_pid_changed(s->event), -ECHILD);
1718 *pid = s->child.pid;
1723 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1726 assert_return(s, -EINVAL);
1727 assert_return(s->type != SOURCE_EXIT, -EDOM);
1728 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1729 assert_return(!event_pid_changed(s->event), -ECHILD);
1731 if (s->prepare == callback)
1734 if (callback && s->prepare) {
1735 s->prepare = callback;
1739 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1743 s->prepare = callback;
1746 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1750 prioq_remove(s->event->prepare, s, &s->prepare_index);
1755 /// UNNEEDED by elogind
1757 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1758 assert_return(s, NULL);
1763 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1766 assert_return(s, NULL);
1769 s->userdata = userdata;
1775 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1786 initialize_perturb(e);
1789 Find a good time to wake up again between times a and b. We
1790 have two goals here:
1792 a) We want to wake up as seldom as possible, hence prefer
1793 later times over earlier times.
1795 b) But if we have to wake up, then let's make sure to
1796 dispatch as much as possible on the entire system.
1798 We implement this by waking up everywhere at the same time
1799 within any given minute if we can, synchronised via the
1800 perturbation value determined from the boot ID. If we can't,
1801 then we try to find the same spot in every 10s, then 1s and
1802 then 250ms step. Otherwise, we pick the last possible time
1806 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1808 if (_unlikely_(c < USEC_PER_MINUTE))
1811 c -= USEC_PER_MINUTE;
1817 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1819 if (_unlikely_(c < USEC_PER_SEC*10))
1822 c -= USEC_PER_SEC*10;
1828 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1830 if (_unlikely_(c < USEC_PER_SEC))
1839 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1841 if (_unlikely_(c < USEC_PER_MSEC*250))
1844 c -= USEC_PER_MSEC*250;
1853 static int event_arm_timer(
1855 struct clock_data *d) {
1857 struct itimerspec its = {};
1858 sd_event_source *a, *b;
1865 if (!d->needs_rearm)
1868 d->needs_rearm = false;
1870 a = prioq_peek(d->earliest);
1871 if (!a || a->enabled == SD_EVENT_OFF) {
1876 if (d->next == USEC_INFINITY)
1880 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1884 d->next = USEC_INFINITY;
1888 b = prioq_peek(d->latest);
1889 assert_se(b && b->enabled != SD_EVENT_OFF);
1891 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1895 assert_se(d->fd >= 0);
1898 /* We don' want to disarm here, just mean some time looooong ago. */
1899 its.it_value.tv_sec = 0;
1900 its.it_value.tv_nsec = 1;
1902 timespec_store(&its.it_value, t);
1904 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1912 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1915 assert(s->type == SOURCE_IO);
1917 /* If the event source was already pending, we just OR in the
1918 * new revents, otherwise we reset the value. The ORing is
1919 * necessary to handle EPOLLONESHOT events properly where
1920 * readability might happen independently of writability, and
1921 * we need to keep track of both */
1924 s->io.revents |= revents;
1926 s->io.revents = revents;
1928 return source_set_pending(s, true);
1931 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1938 assert_return(events == EPOLLIN, -EIO);
1940 ss = read(fd, &x, sizeof(x));
1942 if (errno == EAGAIN || errno == EINTR)
1948 if (_unlikely_(ss != sizeof(x)))
1952 *next = USEC_INFINITY;
1957 static int process_timer(
1960 struct clock_data *d) {
1969 s = prioq_peek(d->earliest);
1972 s->enabled == SD_EVENT_OFF ||
1976 r = source_set_pending(s, true);
1980 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1981 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1982 d->needs_rearm = true;
1988 static int process_child(sd_event *e) {
1995 e->need_process_child = false;
1998 So, this is ugly. We iteratively invoke waitid() with P_PID
1999 + WNOHANG for each PID we wait for, instead of using
2000 P_ALL. This is because we only want to get child
2001 information of very specific child processes, and not all
2002 of them. We might not have processed the SIGCHLD even of a
2003 previous invocation and we don't want to maintain a
2004 unbounded *per-child* event queue, hence we really don't
2005 want anything flushed out of the kernel's queue that we
2006 don't care about. Since this is O(n) this means that if you
2007 have a lot of processes you probably want to handle SIGCHLD
2010 We do not reap the children here (by using WNOWAIT), this
2011 is only done after the event source is dispatched so that
2012 the callback still sees the process as a zombie.
2015 HASHMAP_FOREACH(s, e->child_sources, i) {
2016 assert(s->type == SOURCE_CHILD);
2021 if (s->enabled == SD_EVENT_OFF)
2024 zero(s->child.siginfo);
2025 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2026 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2030 if (s->child.siginfo.si_pid != 0) {
2032 s->child.siginfo.si_code == CLD_EXITED ||
2033 s->child.siginfo.si_code == CLD_KILLED ||
2034 s->child.siginfo.si_code == CLD_DUMPED;
2036 if (!zombie && (s->child.options & WEXITED)) {
2037 /* If the child isn't dead then let's
2038 * immediately remove the state change
2039 * from the queue, since there's no
2040 * benefit in leaving it queued */
2042 assert(s->child.options & (WSTOPPED|WCONTINUED));
2043 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2046 r = source_set_pending(s, true);
2055 static int process_signal(sd_event *e, uint32_t events) {
2056 bool read_one = false;
2061 assert_return(events == EPOLLIN, -EIO);
2064 struct signalfd_siginfo si;
2066 sd_event_source *s = NULL;
2068 n = read(e->signal_fd, &si, sizeof(si));
2070 if (errno == EAGAIN || errno == EINTR)
2076 if (_unlikely_(n != sizeof(si)))
2079 assert(si.ssi_signo < _NSIG);
2083 if (si.ssi_signo == SIGCHLD) {
2084 r = process_child(e);
2091 if (e->signal_sources)
2092 s = e->signal_sources[si.ssi_signo];
2097 s->signal.siginfo = si;
2098 r = source_set_pending(s, true);
2104 static int source_dispatch(sd_event_source *s) {
2108 assert(s->pending || s->type == SOURCE_EXIT);
2110 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2111 r = source_set_pending(s, false);
2116 if (s->type != SOURCE_POST) {
2120 /* If we execute a non-post source, let's mark all
2121 * post sources as pending */
2123 SET_FOREACH(z, s->event->post_sources, i) {
2124 if (z->enabled == SD_EVENT_OFF)
2127 r = source_set_pending(z, true);
2133 if (s->enabled == SD_EVENT_ONESHOT) {
2134 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2139 s->dispatching = true;
2144 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2147 case SOURCE_TIME_REALTIME:
2148 case SOURCE_TIME_BOOTTIME:
2149 case SOURCE_TIME_MONOTONIC:
2150 case SOURCE_TIME_REALTIME_ALARM:
2151 case SOURCE_TIME_BOOTTIME_ALARM:
2152 r = s->time.callback(s, s->time.next, s->userdata);
2156 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2159 case SOURCE_CHILD: {
2162 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2163 s->child.siginfo.si_code == CLD_KILLED ||
2164 s->child.siginfo.si_code == CLD_DUMPED;
2166 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2168 /* Now, reap the PID for good. */
2170 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2176 r = s->defer.callback(s, s->userdata);
2180 r = s->post.callback(s, s->userdata);
2184 r = s->exit.callback(s, s->userdata);
2187 case SOURCE_WATCHDOG:
2188 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2189 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2190 assert_not_reached("Wut? I shouldn't exist.");
2193 s->dispatching = false;
2197 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2199 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2205 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2210 static int event_prepare(sd_event *e) {
2218 s = prioq_peek(e->prepare);
2219 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2222 s->prepare_iteration = e->iteration;
2223 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2229 s->dispatching = true;
2230 r = s->prepare(s, s->userdata);
2231 s->dispatching = false;
2235 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2237 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2243 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2249 static int dispatch_exit(sd_event *e) {
2255 p = prioq_peek(e->exit);
2256 if (!p || p->enabled == SD_EVENT_OFF) {
2257 e->state = SD_EVENT_FINISHED;
2263 e->state = SD_EVENT_EXITING;
2265 r = source_dispatch(p);
2267 e->state = SD_EVENT_INITIAL;
2273 static sd_event_source* event_next_pending(sd_event *e) {
2278 p = prioq_peek(e->pending);
2282 if (p->enabled == SD_EVENT_OFF)
2288 static int arm_watchdog(sd_event *e) {
2289 struct itimerspec its = {};
2294 assert(e->watchdog_fd >= 0);
2296 t = sleep_between(e,
2297 e->watchdog_last + (e->watchdog_period / 2),
2298 e->watchdog_last + (e->watchdog_period * 3 / 4));
2300 timespec_store(&its.it_value, t);
2302 /* Make sure we never set the watchdog to 0, which tells the
2303 * kernel to disable it. */
2304 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2305 its.it_value.tv_nsec = 1;
2307 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2314 static int process_watchdog(sd_event *e) {
2320 /* Don't notify watchdog too often */
2321 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2324 sd_notify(false, "WATCHDOG=1");
2325 e->watchdog_last = e->timestamp.monotonic;
2327 return arm_watchdog(e);
2330 _public_ int sd_event_prepare(sd_event *e) {
2333 assert_return(e, -EINVAL);
2334 assert_return(!event_pid_changed(e), -ECHILD);
2335 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2336 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2338 if (e->exit_requested)
2343 r = event_prepare(e);
2347 r = event_arm_timer(e, &e->realtime);
2351 r = event_arm_timer(e, &e->boottime);
2355 r = event_arm_timer(e, &e->monotonic);
2359 r = event_arm_timer(e, &e->realtime_alarm);
2363 r = event_arm_timer(e, &e->boottime_alarm);
2367 if (event_next_pending(e) || e->need_process_child)
2370 e->state = SD_EVENT_ARMED;
2375 e->state = SD_EVENT_ARMED;
2376 r = sd_event_wait(e, 0);
2378 e->state = SD_EVENT_ARMED;
2383 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2384 struct epoll_event *ev_queue;
2385 unsigned ev_queue_max;
2388 assert_return(e, -EINVAL);
2389 assert_return(!event_pid_changed(e), -ECHILD);
2390 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2391 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2393 if (e->exit_requested) {
2394 e->state = SD_EVENT_PENDING;
2398 ev_queue_max = MAX(e->n_sources, 1u);
2399 ev_queue = newa(struct epoll_event, ev_queue_max);
2401 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2402 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2404 if (errno == EINTR) {
2405 e->state = SD_EVENT_PENDING;
2413 dual_timestamp_get(&e->timestamp);
2414 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2416 for (i = 0; i < m; i++) {
2418 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2419 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2420 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2421 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2422 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2423 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2424 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2425 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2426 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2427 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2428 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2429 r = process_signal(e, ev_queue[i].events);
2430 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2431 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2433 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2439 r = process_watchdog(e);
2443 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2447 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2451 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2455 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2459 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2463 if (e->need_process_child) {
2464 r = process_child(e);
2469 if (event_next_pending(e)) {
2470 e->state = SD_EVENT_PENDING;
2478 e->state = SD_EVENT_INITIAL;
2483 _public_ int sd_event_dispatch(sd_event *e) {
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_PENDING, -EBUSY);
2492 if (e->exit_requested)
2493 return dispatch_exit(e);
2495 p = event_next_pending(e);
2499 e->state = SD_EVENT_RUNNING;
2500 r = source_dispatch(p);
2501 e->state = SD_EVENT_INITIAL;
2508 e->state = SD_EVENT_INITIAL;
2513 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2516 assert_return(e, -EINVAL);
2517 assert_return(!event_pid_changed(e), -ECHILD);
2518 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2519 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2521 r = sd_event_prepare(e);
2523 /* There was nothing? Then wait... */
2524 r = sd_event_wait(e, timeout);
2527 /* There's something now, then let's dispatch it */
2528 r = sd_event_dispatch(e);
2538 /// UNNEEDED by elogind
2540 _public_ int sd_event_loop(sd_event *e) {
2543 assert_return(e, -EINVAL);
2544 assert_return(!event_pid_changed(e), -ECHILD);
2545 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2549 while (e->state != SD_EVENT_FINISHED) {
2550 r = sd_event_run(e, (uint64_t) -1);
2562 _public_ int sd_event_get_fd(sd_event *e) {
2564 assert_return(e, -EINVAL);
2565 assert_return(!event_pid_changed(e), -ECHILD);
2571 _public_ int sd_event_get_state(sd_event *e) {
2572 assert_return(e, -EINVAL);
2573 assert_return(!event_pid_changed(e), -ECHILD);
2578 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2579 assert_return(e, -EINVAL);
2580 assert_return(code, -EINVAL);
2581 assert_return(!event_pid_changed(e), -ECHILD);
2583 if (!e->exit_requested)
2586 *code = e->exit_code;
2590 _public_ int sd_event_exit(sd_event *e, int code) {
2591 assert_return(e, -EINVAL);
2592 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2593 assert_return(!event_pid_changed(e), -ECHILD);
2595 e->exit_requested = true;
2596 e->exit_code = code;
2601 /// UNNEEDED by elogind
2603 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2604 assert_return(e, -EINVAL);
2605 assert_return(usec, -EINVAL);
2606 assert_return(!event_pid_changed(e), -ECHILD);
2608 if (!dual_timestamp_is_set(&e->timestamp)) {
2609 /* Implicitly fall back to now() if we never ran
2610 * before and thus have no cached time. */
2617 case CLOCK_REALTIME:
2618 case CLOCK_REALTIME_ALARM:
2619 *usec = e->timestamp.realtime;
2622 case CLOCK_MONOTONIC:
2623 *usec = e->timestamp.monotonic;
2626 case CLOCK_BOOTTIME:
2627 case CLOCK_BOOTTIME_ALARM:
2628 *usec = e->timestamp_boottime;
2636 _public_ int sd_event_default(sd_event **ret) {
2638 static thread_local sd_event *default_event = NULL;
2643 return !!default_event;
2645 if (default_event) {
2646 *ret = sd_event_ref(default_event);
2650 r = sd_event_new(&e);
2654 e->default_event_ptr = &default_event;
2662 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2663 assert_return(e, -EINVAL);
2664 assert_return(tid, -EINVAL);
2665 assert_return(!event_pid_changed(e), -ECHILD);
2675 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2678 assert_return(e, -EINVAL);
2679 assert_return(!event_pid_changed(e), -ECHILD);
2681 if (e->watchdog == !!b)
2685 struct epoll_event ev = {};
2687 r = sd_watchdog_enabled(false, &e->watchdog_period);
2691 /* Issue first ping immediately */
2692 sd_notify(false, "WATCHDOG=1");
2693 e->watchdog_last = now(CLOCK_MONOTONIC);
2695 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2696 if (e->watchdog_fd < 0)
2699 r = arm_watchdog(e);
2703 ev.events = EPOLLIN;
2704 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2706 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2713 if (e->watchdog_fd >= 0) {
2714 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2715 e->watchdog_fd = safe_close(e->watchdog_fd);
2723 e->watchdog_fd = safe_close(e->watchdog_fd);
2727 /// UNNEEDED by elogind
2729 _public_ int sd_event_get_watchdog(sd_event *e) {
2730 assert_return(e, -EINVAL);
2731 assert_return(!event_pid_changed(e), -ECHILD);