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 /// UNNEEDED by elogind
522 static clockid_t event_source_type_to_clock(EventSourceType t) {
526 case SOURCE_TIME_REALTIME:
527 return CLOCK_REALTIME;
529 case SOURCE_TIME_BOOTTIME:
530 return CLOCK_BOOTTIME;
532 case SOURCE_TIME_MONOTONIC:
533 return CLOCK_MONOTONIC;
535 case SOURCE_TIME_REALTIME_ALARM:
536 return CLOCK_REALTIME_ALARM;
538 case SOURCE_TIME_BOOTTIME_ALARM:
539 return CLOCK_BOOTTIME_ALARM;
542 return (clockid_t) -1;
547 static EventSourceType clock_to_event_source_type(clockid_t clock) {
552 return SOURCE_TIME_REALTIME;
555 return SOURCE_TIME_BOOTTIME;
557 case CLOCK_MONOTONIC:
558 return SOURCE_TIME_MONOTONIC;
560 case CLOCK_REALTIME_ALARM:
561 return SOURCE_TIME_REALTIME_ALARM;
563 case CLOCK_BOOTTIME_ALARM:
564 return SOURCE_TIME_BOOTTIME_ALARM;
567 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
571 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
576 case SOURCE_TIME_REALTIME:
579 case SOURCE_TIME_BOOTTIME:
582 case SOURCE_TIME_MONOTONIC:
583 return &e->monotonic;
585 case SOURCE_TIME_REALTIME_ALARM:
586 return &e->realtime_alarm;
588 case SOURCE_TIME_BOOTTIME_ALARM:
589 return &e->boottime_alarm;
596 static bool need_signal(sd_event *e, int signal) {
597 return (e->signal_sources && e->signal_sources[signal] &&
598 e->signal_sources[signal]->enabled != SD_EVENT_OFF)
600 (signal == SIGCHLD &&
601 e->n_enabled_child_sources > 0);
604 static int event_update_signal_fd(sd_event *e) {
605 struct epoll_event ev = {};
611 if (event_pid_changed(e))
614 add_to_epoll = e->signal_fd < 0;
616 r = signalfd(e->signal_fd, &e->sigset, SFD_NONBLOCK|SFD_CLOEXEC);
626 ev.data.ptr = INT_TO_PTR(SOURCE_SIGNAL);
628 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->signal_fd, &ev);
630 e->signal_fd = safe_close(e->signal_fd);
637 static void source_disconnect(sd_event_source *s) {
645 assert(s->event->n_sources > 0);
651 source_io_unregister(s);
655 case SOURCE_TIME_REALTIME:
656 case SOURCE_TIME_BOOTTIME:
657 case SOURCE_TIME_MONOTONIC:
658 case SOURCE_TIME_REALTIME_ALARM:
659 case SOURCE_TIME_BOOTTIME_ALARM: {
660 struct clock_data *d;
662 d = event_get_clock_data(s->event, s->type);
665 prioq_remove(d->earliest, s, &s->time.earliest_index);
666 prioq_remove(d->latest, s, &s->time.latest_index);
667 d->needs_rearm = true;
672 if (s->signal.sig > 0) {
673 if (s->event->signal_sources)
674 s->event->signal_sources[s->signal.sig] = NULL;
676 /* If the signal was on and now it is off... */
677 if (s->enabled != SD_EVENT_OFF && !need_signal(s->event, s->signal.sig)) {
678 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
680 (void) event_update_signal_fd(s->event);
681 /* If disabling failed, we might get a spurious event,
682 * but otherwise nothing bad should happen. */
689 if (s->child.pid > 0) {
690 if (s->enabled != SD_EVENT_OFF) {
691 assert(s->event->n_enabled_child_sources > 0);
692 s->event->n_enabled_child_sources--;
694 /* We know the signal was on, if it is off now... */
695 if (!need_signal(s->event, SIGCHLD)) {
696 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
698 (void) event_update_signal_fd(s->event);
699 /* If disabling failed, we might get a spurious event,
700 * but otherwise nothing bad should happen. */
704 hashmap_remove(s->event->child_sources, INT_TO_PTR(s->child.pid));
714 set_remove(s->event->post_sources, s);
718 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
722 assert_not_reached("Wut? I shouldn't exist.");
726 prioq_remove(s->event->pending, s, &s->pending_index);
729 prioq_remove(s->event->prepare, s, &s->prepare_index);
733 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
735 LIST_REMOVE(sources, event->sources, s);
739 sd_event_unref(event);
742 static void source_free(sd_event_source *s) {
745 source_disconnect(s);
746 free(s->description);
750 static int source_set_pending(sd_event_source *s, bool b) {
754 assert(s->type != SOURCE_EXIT);
762 s->pending_iteration = s->event->iteration;
764 r = prioq_put(s->event->pending, s, &s->pending_index);
770 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
772 if (EVENT_SOURCE_IS_TIME(s->type)) {
773 struct clock_data *d;
775 d = event_get_clock_data(s->event, s->type);
778 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
779 prioq_reshuffle(d->latest, s, &s->time.latest_index);
780 d->needs_rearm = true;
786 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
791 s = new0(sd_event_source, 1);
797 s->floating = floating;
799 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
804 LIST_PREPEND(sources, e->sources, s);
810 _public_ int sd_event_add_io(
812 sd_event_source **ret,
815 sd_event_io_handler_t callback,
821 assert_return(e, -EINVAL);
822 assert_return(fd >= 0, -EBADF);
823 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
824 assert_return(callback, -EINVAL);
825 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
826 assert_return(!event_pid_changed(e), -ECHILD);
828 s = source_new(e, !ret, SOURCE_IO);
833 s->io.events = events;
834 s->io.callback = callback;
835 s->userdata = userdata;
836 s->enabled = SD_EVENT_ON;
838 r = source_io_register(s, s->enabled, events);
850 static void initialize_perturb(sd_event *e) {
851 sd_id128_t bootid = {};
853 /* When we sleep for longer, we try to realign the wakeup to
854 the same time wihtin each minute/second/250ms, so that
855 events all across the system can be coalesced into a single
856 CPU wakeup. However, let's take some system-specific
857 randomness for this value, so that in a network of systems
858 with synced clocks timer events are distributed a
859 bit. Here, we calculate a perturbation usec offset from the
862 if (_likely_(e->perturb != USEC_INFINITY))
865 if (sd_id128_get_boot(&bootid) >= 0)
866 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
869 static int event_setup_timer_fd(
871 struct clock_data *d,
874 struct epoll_event ev = {};
880 if (_likely_(d->fd >= 0))
883 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
888 ev.data.ptr = INT_TO_PTR(clock_to_event_source_type(clock));
890 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
900 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
903 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
906 _public_ int sd_event_add_time(
908 sd_event_source **ret,
912 sd_event_time_handler_t callback,
915 EventSourceType type;
917 struct clock_data *d;
920 assert_return(e, -EINVAL);
921 assert_return(usec != (uint64_t) -1, -EINVAL);
922 assert_return(accuracy != (uint64_t) -1, -EINVAL);
923 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
924 assert_return(!event_pid_changed(e), -ECHILD);
927 callback = time_exit_callback;
929 type = clock_to_event_source_type(clock);
930 assert_return(type >= 0, -EOPNOTSUPP);
932 d = event_get_clock_data(e, type);
936 d->earliest = prioq_new(earliest_time_prioq_compare);
942 d->latest = prioq_new(latest_time_prioq_compare);
948 r = event_setup_timer_fd(e, d, clock);
953 s = source_new(e, !ret, type);
958 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
959 s->time.callback = callback;
960 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
961 s->userdata = userdata;
962 s->enabled = SD_EVENT_ONESHOT;
964 d->needs_rearm = true;
966 r = prioq_put(d->earliest, s, &s->time.earliest_index);
970 r = prioq_put(d->latest, s, &s->time.latest_index);
984 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
987 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
990 _public_ int sd_event_add_signal(
992 sd_event_source **ret,
994 sd_event_signal_handler_t callback,
1002 assert_return(e, -EINVAL);
1003 assert_return(sig > 0, -EINVAL);
1004 assert_return(sig < _NSIG, -EINVAL);
1005 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1006 assert_return(!event_pid_changed(e), -ECHILD);
1009 callback = signal_exit_callback;
1011 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1015 if (!sigismember(&ss, sig))
1018 if (!e->signal_sources) {
1019 e->signal_sources = new0(sd_event_source*, _NSIG);
1020 if (!e->signal_sources)
1022 } else if (e->signal_sources[sig])
1025 previous = need_signal(e, sig);
1027 s = source_new(e, !ret, SOURCE_SIGNAL);
1031 s->signal.sig = sig;
1032 s->signal.callback = callback;
1033 s->userdata = userdata;
1034 s->enabled = SD_EVENT_ON;
1036 e->signal_sources[sig] = s;
1039 assert_se(sigaddset(&e->sigset, sig) == 0);
1041 r = event_update_signal_fd(e);
1048 /* Use the signal name as description for the event source by default */
1049 (void) sd_event_source_set_description(s, signal_to_string(sig));
1057 _public_ int sd_event_add_child(
1059 sd_event_source **ret,
1062 sd_event_child_handler_t callback,
1069 assert_return(e, -EINVAL);
1070 assert_return(pid > 1, -EINVAL);
1071 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1072 assert_return(options != 0, -EINVAL);
1073 assert_return(callback, -EINVAL);
1074 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1075 assert_return(!event_pid_changed(e), -ECHILD);
1077 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1081 if (hashmap_contains(e->child_sources, INT_TO_PTR(pid)))
1084 previous = need_signal(e, SIGCHLD);
1086 s = source_new(e, !ret, SOURCE_CHILD);
1091 s->child.options = options;
1092 s->child.callback = callback;
1093 s->userdata = userdata;
1094 s->enabled = SD_EVENT_ONESHOT;
1096 r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
1102 e->n_enabled_child_sources ++;
1105 assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
1107 r = event_update_signal_fd(e);
1114 e->need_process_child = true;
1122 _public_ int sd_event_add_defer(
1124 sd_event_source **ret,
1125 sd_event_handler_t callback,
1131 assert_return(e, -EINVAL);
1132 assert_return(callback, -EINVAL);
1133 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1134 assert_return(!event_pid_changed(e), -ECHILD);
1136 s = source_new(e, !ret, SOURCE_DEFER);
1140 s->defer.callback = callback;
1141 s->userdata = userdata;
1142 s->enabled = SD_EVENT_ONESHOT;
1144 r = source_set_pending(s, true);
1156 _public_ int sd_event_add_post(
1158 sd_event_source **ret,
1159 sd_event_handler_t callback,
1165 assert_return(e, -EINVAL);
1166 assert_return(callback, -EINVAL);
1167 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1168 assert_return(!event_pid_changed(e), -ECHILD);
1170 r = set_ensure_allocated(&e->post_sources, NULL);
1174 s = source_new(e, !ret, SOURCE_POST);
1178 s->post.callback = callback;
1179 s->userdata = userdata;
1180 s->enabled = SD_EVENT_ON;
1182 r = set_put(e->post_sources, s);
1194 _public_ int sd_event_add_exit(
1196 sd_event_source **ret,
1197 sd_event_handler_t callback,
1203 assert_return(e, -EINVAL);
1204 assert_return(callback, -EINVAL);
1205 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1206 assert_return(!event_pid_changed(e), -ECHILD);
1209 e->exit = prioq_new(exit_prioq_compare);
1214 s = source_new(e, !ret, SOURCE_EXIT);
1218 s->exit.callback = callback;
1219 s->userdata = userdata;
1220 s->exit.prioq_index = PRIOQ_IDX_NULL;
1221 s->enabled = SD_EVENT_ONESHOT;
1223 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1235 /// UNNEEDED by elogind
1237 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1238 assert_return(s, NULL);
1240 assert(s->n_ref >= 1);
1247 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1252 assert(s->n_ref >= 1);
1255 if (s->n_ref <= 0) {
1256 /* Here's a special hack: when we are called from a
1257 * dispatch handler we won't free the event source
1258 * immediately, but we will detach the fd from the
1259 * epoll. This way it is safe for the caller to unref
1260 * the event source and immediately close the fd, but
1261 * we still retain a valid event source object after
1264 if (s->dispatching) {
1265 if (s->type == SOURCE_IO)
1266 source_io_unregister(s);
1268 source_disconnect(s);
1276 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1277 assert_return(s, -EINVAL);
1278 assert_return(!event_pid_changed(s->event), -ECHILD);
1280 return free_and_strdup(&s->description, description);
1283 /// UNNEEDED by elogind
1285 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1286 assert_return(s, -EINVAL);
1287 assert_return(description, -EINVAL);
1288 assert_return(s->description, -ENXIO);
1289 assert_return(!event_pid_changed(s->event), -ECHILD);
1291 *description = s->description;
1296 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1297 assert_return(s, NULL);
1302 /// UNNEEDED by elogind
1304 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1305 assert_return(s, -EINVAL);
1306 assert_return(s->type != SOURCE_EXIT, -EDOM);
1307 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1308 assert_return(!event_pid_changed(s->event), -ECHILD);
1313 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1314 assert_return(s, -EINVAL);
1315 assert_return(s->type == SOURCE_IO, -EDOM);
1316 assert_return(!event_pid_changed(s->event), -ECHILD);
1322 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1325 assert_return(s, -EINVAL);
1326 assert_return(fd >= 0, -EBADF);
1327 assert_return(s->type == SOURCE_IO, -EDOM);
1328 assert_return(!event_pid_changed(s->event), -ECHILD);
1333 if (s->enabled == SD_EVENT_OFF) {
1335 s->io.registered = false;
1339 saved_fd = s->io.fd;
1340 assert(s->io.registered);
1343 s->io.registered = false;
1345 r = source_io_register(s, s->enabled, s->io.events);
1347 s->io.fd = saved_fd;
1348 s->io.registered = true;
1352 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1358 /// UNNEEDED by elogind
1360 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1361 assert_return(s, -EINVAL);
1362 assert_return(events, -EINVAL);
1363 assert_return(s->type == SOURCE_IO, -EDOM);
1364 assert_return(!event_pid_changed(s->event), -ECHILD);
1366 *events = s->io.events;
1371 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1374 assert_return(s, -EINVAL);
1375 assert_return(s->type == SOURCE_IO, -EDOM);
1376 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1377 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1378 assert_return(!event_pid_changed(s->event), -ECHILD);
1380 /* edge-triggered updates are never skipped, so we can reset edges */
1381 if (s->io.events == events && !(events & EPOLLET))
1384 if (s->enabled != SD_EVENT_OFF) {
1385 r = source_io_register(s, s->enabled, events);
1390 s->io.events = events;
1391 source_set_pending(s, false);
1396 /// UNNEEDED by elogind
1398 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1399 assert_return(s, -EINVAL);
1400 assert_return(revents, -EINVAL);
1401 assert_return(s->type == SOURCE_IO, -EDOM);
1402 assert_return(s->pending, -ENODATA);
1403 assert_return(!event_pid_changed(s->event), -ECHILD);
1405 *revents = s->io.revents;
1409 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1410 assert_return(s, -EINVAL);
1411 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1412 assert_return(!event_pid_changed(s->event), -ECHILD);
1414 return s->signal.sig;
1417 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1418 assert_return(s, -EINVAL);
1419 assert_return(!event_pid_changed(s->event), -ECHILD);
1425 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1426 assert_return(s, -EINVAL);
1427 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1428 assert_return(!event_pid_changed(s->event), -ECHILD);
1430 if (s->priority == priority)
1433 s->priority = priority;
1436 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1439 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1441 if (s->type == SOURCE_EXIT)
1442 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1447 /// UNNEEDED by elogind
1449 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1450 assert_return(s, -EINVAL);
1451 assert_return(m, -EINVAL);
1452 assert_return(!event_pid_changed(s->event), -ECHILD);
1459 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1462 assert_return(s, -EINVAL);
1463 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1464 assert_return(!event_pid_changed(s->event), -ECHILD);
1466 /* If we are dead anyway, we are fine with turning off
1467 * sources, but everything else needs to fail. */
1468 if (s->event->state == SD_EVENT_FINISHED)
1469 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1471 if (s->enabled == m)
1474 if (m == SD_EVENT_OFF) {
1479 source_io_unregister(s);
1483 case SOURCE_TIME_REALTIME:
1484 case SOURCE_TIME_BOOTTIME:
1485 case SOURCE_TIME_MONOTONIC:
1486 case SOURCE_TIME_REALTIME_ALARM:
1487 case SOURCE_TIME_BOOTTIME_ALARM: {
1488 struct clock_data *d;
1491 d = event_get_clock_data(s->event, s->type);
1494 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1495 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1496 d->needs_rearm = true;
1501 assert(need_signal(s->event, s->signal.sig));
1505 if (!need_signal(s->event, s->signal.sig)) {
1506 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
1508 (void) event_update_signal_fd(s->event);
1509 /* If disabling failed, we might get a spurious event,
1510 * but otherwise nothing bad should happen. */
1516 assert(need_signal(s->event, SIGCHLD));
1520 assert(s->event->n_enabled_child_sources > 0);
1521 s->event->n_enabled_child_sources--;
1523 if (!need_signal(s->event, SIGCHLD)) {
1524 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
1526 (void) event_update_signal_fd(s->event);
1533 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1542 assert_not_reached("Wut? I shouldn't exist.");
1549 r = source_io_register(s, m, s->io.events);
1556 case SOURCE_TIME_REALTIME:
1557 case SOURCE_TIME_BOOTTIME:
1558 case SOURCE_TIME_MONOTONIC:
1559 case SOURCE_TIME_REALTIME_ALARM:
1560 case SOURCE_TIME_BOOTTIME_ALARM: {
1561 struct clock_data *d;
1564 d = event_get_clock_data(s->event, s->type);
1567 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1568 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1569 d->needs_rearm = true;
1574 /* Check status before enabling. */
1575 if (!need_signal(s->event, s->signal.sig)) {
1576 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1578 r = event_update_signal_fd(s->event);
1580 s->enabled = SD_EVENT_OFF;
1589 /* Check status before enabling. */
1590 if (s->enabled == SD_EVENT_OFF) {
1591 if (!need_signal(s->event, SIGCHLD)) {
1592 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
1594 r = event_update_signal_fd(s->event);
1596 s->enabled = SD_EVENT_OFF;
1601 s->event->n_enabled_child_sources++;
1609 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1618 assert_not_reached("Wut? I shouldn't exist.");
1623 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1626 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1631 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1632 assert_return(s, -EINVAL);
1633 assert_return(usec, -EINVAL);
1634 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1635 assert_return(!event_pid_changed(s->event), -ECHILD);
1637 *usec = s->time.next;
1641 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1642 struct clock_data *d;
1644 assert_return(s, -EINVAL);
1645 assert_return(usec != (uint64_t) -1, -EINVAL);
1646 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1647 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1648 assert_return(!event_pid_changed(s->event), -ECHILD);
1650 s->time.next = usec;
1652 source_set_pending(s, false);
1654 d = event_get_clock_data(s->event, s->type);
1657 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1658 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1659 d->needs_rearm = true;
1664 /// UNNEEDED by elogind
1666 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1667 assert_return(s, -EINVAL);
1668 assert_return(usec, -EINVAL);
1669 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1670 assert_return(!event_pid_changed(s->event), -ECHILD);
1672 *usec = s->time.accuracy;
1676 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1677 struct clock_data *d;
1679 assert_return(s, -EINVAL);
1680 assert_return(usec != (uint64_t) -1, -EINVAL);
1681 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1682 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1683 assert_return(!event_pid_changed(s->event), -ECHILD);
1686 usec = DEFAULT_ACCURACY_USEC;
1688 s->time.accuracy = usec;
1690 source_set_pending(s, false);
1692 d = event_get_clock_data(s->event, s->type);
1695 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1696 d->needs_rearm = true;
1701 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1702 assert_return(s, -EINVAL);
1703 assert_return(clock, -EINVAL);
1704 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1705 assert_return(!event_pid_changed(s->event), -ECHILD);
1707 *clock = event_source_type_to_clock(s->type);
1711 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1712 assert_return(s, -EINVAL);
1713 assert_return(pid, -EINVAL);
1714 assert_return(s->type == SOURCE_CHILD, -EDOM);
1715 assert_return(!event_pid_changed(s->event), -ECHILD);
1717 *pid = s->child.pid;
1722 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1725 assert_return(s, -EINVAL);
1726 assert_return(s->type != SOURCE_EXIT, -EDOM);
1727 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1728 assert_return(!event_pid_changed(s->event), -ECHILD);
1730 if (s->prepare == callback)
1733 if (callback && s->prepare) {
1734 s->prepare = callback;
1738 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1742 s->prepare = callback;
1745 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1749 prioq_remove(s->event->prepare, s, &s->prepare_index);
1754 /// UNNEEDED by elogind
1756 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1757 assert_return(s, NULL);
1762 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1765 assert_return(s, NULL);
1768 s->userdata = userdata;
1774 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1785 initialize_perturb(e);
1788 Find a good time to wake up again between times a and b. We
1789 have two goals here:
1791 a) We want to wake up as seldom as possible, hence prefer
1792 later times over earlier times.
1794 b) But if we have to wake up, then let's make sure to
1795 dispatch as much as possible on the entire system.
1797 We implement this by waking up everywhere at the same time
1798 within any given minute if we can, synchronised via the
1799 perturbation value determined from the boot ID. If we can't,
1800 then we try to find the same spot in every 10s, then 1s and
1801 then 250ms step. Otherwise, we pick the last possible time
1805 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1807 if (_unlikely_(c < USEC_PER_MINUTE))
1810 c -= USEC_PER_MINUTE;
1816 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1818 if (_unlikely_(c < USEC_PER_SEC*10))
1821 c -= USEC_PER_SEC*10;
1827 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1829 if (_unlikely_(c < USEC_PER_SEC))
1838 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1840 if (_unlikely_(c < USEC_PER_MSEC*250))
1843 c -= USEC_PER_MSEC*250;
1852 static int event_arm_timer(
1854 struct clock_data *d) {
1856 struct itimerspec its = {};
1857 sd_event_source *a, *b;
1864 if (!d->needs_rearm)
1867 d->needs_rearm = false;
1869 a = prioq_peek(d->earliest);
1870 if (!a || a->enabled == SD_EVENT_OFF) {
1875 if (d->next == USEC_INFINITY)
1879 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1883 d->next = USEC_INFINITY;
1887 b = prioq_peek(d->latest);
1888 assert_se(b && b->enabled != SD_EVENT_OFF);
1890 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1894 assert_se(d->fd >= 0);
1897 /* We don' want to disarm here, just mean some time looooong ago. */
1898 its.it_value.tv_sec = 0;
1899 its.it_value.tv_nsec = 1;
1901 timespec_store(&its.it_value, t);
1903 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1911 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
1914 assert(s->type == SOURCE_IO);
1916 /* If the event source was already pending, we just OR in the
1917 * new revents, otherwise we reset the value. The ORing is
1918 * necessary to handle EPOLLONESHOT events properly where
1919 * readability might happen independently of writability, and
1920 * we need to keep track of both */
1923 s->io.revents |= revents;
1925 s->io.revents = revents;
1927 return source_set_pending(s, true);
1930 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
1937 assert_return(events == EPOLLIN, -EIO);
1939 ss = read(fd, &x, sizeof(x));
1941 if (errno == EAGAIN || errno == EINTR)
1947 if (_unlikely_(ss != sizeof(x)))
1951 *next = USEC_INFINITY;
1956 static int process_timer(
1959 struct clock_data *d) {
1968 s = prioq_peek(d->earliest);
1971 s->enabled == SD_EVENT_OFF ||
1975 r = source_set_pending(s, true);
1979 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1980 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1981 d->needs_rearm = true;
1987 static int process_child(sd_event *e) {
1994 e->need_process_child = false;
1997 So, this is ugly. We iteratively invoke waitid() with P_PID
1998 + WNOHANG for each PID we wait for, instead of using
1999 P_ALL. This is because we only want to get child
2000 information of very specific child processes, and not all
2001 of them. We might not have processed the SIGCHLD even of a
2002 previous invocation and we don't want to maintain a
2003 unbounded *per-child* event queue, hence we really don't
2004 want anything flushed out of the kernel's queue that we
2005 don't care about. Since this is O(n) this means that if you
2006 have a lot of processes you probably want to handle SIGCHLD
2009 We do not reap the children here (by using WNOWAIT), this
2010 is only done after the event source is dispatched so that
2011 the callback still sees the process as a zombie.
2014 HASHMAP_FOREACH(s, e->child_sources, i) {
2015 assert(s->type == SOURCE_CHILD);
2020 if (s->enabled == SD_EVENT_OFF)
2023 zero(s->child.siginfo);
2024 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2025 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2029 if (s->child.siginfo.si_pid != 0) {
2031 s->child.siginfo.si_code == CLD_EXITED ||
2032 s->child.siginfo.si_code == CLD_KILLED ||
2033 s->child.siginfo.si_code == CLD_DUMPED;
2035 if (!zombie && (s->child.options & WEXITED)) {
2036 /* If the child isn't dead then let's
2037 * immediately remove the state change
2038 * from the queue, since there's no
2039 * benefit in leaving it queued */
2041 assert(s->child.options & (WSTOPPED|WCONTINUED));
2042 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2045 r = source_set_pending(s, true);
2054 static int process_signal(sd_event *e, uint32_t events) {
2055 bool read_one = false;
2060 assert_return(events == EPOLLIN, -EIO);
2063 struct signalfd_siginfo si;
2065 sd_event_source *s = NULL;
2067 n = read(e->signal_fd, &si, sizeof(si));
2069 if (errno == EAGAIN || errno == EINTR)
2075 if (_unlikely_(n != sizeof(si)))
2078 assert(si.ssi_signo < _NSIG);
2082 if (si.ssi_signo == SIGCHLD) {
2083 r = process_child(e);
2090 if (e->signal_sources)
2091 s = e->signal_sources[si.ssi_signo];
2096 s->signal.siginfo = si;
2097 r = source_set_pending(s, true);
2103 static int source_dispatch(sd_event_source *s) {
2107 assert(s->pending || s->type == SOURCE_EXIT);
2109 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2110 r = source_set_pending(s, false);
2115 if (s->type != SOURCE_POST) {
2119 /* If we execute a non-post source, let's mark all
2120 * post sources as pending */
2122 SET_FOREACH(z, s->event->post_sources, i) {
2123 if (z->enabled == SD_EVENT_OFF)
2126 r = source_set_pending(z, true);
2132 if (s->enabled == SD_EVENT_ONESHOT) {
2133 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2138 s->dispatching = true;
2143 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2146 case SOURCE_TIME_REALTIME:
2147 case SOURCE_TIME_BOOTTIME:
2148 case SOURCE_TIME_MONOTONIC:
2149 case SOURCE_TIME_REALTIME_ALARM:
2150 case SOURCE_TIME_BOOTTIME_ALARM:
2151 r = s->time.callback(s, s->time.next, s->userdata);
2155 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2158 case SOURCE_CHILD: {
2161 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2162 s->child.siginfo.si_code == CLD_KILLED ||
2163 s->child.siginfo.si_code == CLD_DUMPED;
2165 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2167 /* Now, reap the PID for good. */
2169 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2175 r = s->defer.callback(s, s->userdata);
2179 r = s->post.callback(s, s->userdata);
2183 r = s->exit.callback(s, s->userdata);
2186 case SOURCE_WATCHDOG:
2187 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2188 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2189 assert_not_reached("Wut? I shouldn't exist.");
2192 s->dispatching = false;
2196 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2198 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2204 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2209 static int event_prepare(sd_event *e) {
2217 s = prioq_peek(e->prepare);
2218 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2221 s->prepare_iteration = e->iteration;
2222 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2228 s->dispatching = true;
2229 r = s->prepare(s, s->userdata);
2230 s->dispatching = false;
2234 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2236 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2242 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2248 static int dispatch_exit(sd_event *e) {
2254 p = prioq_peek(e->exit);
2255 if (!p || p->enabled == SD_EVENT_OFF) {
2256 e->state = SD_EVENT_FINISHED;
2262 e->state = SD_EVENT_EXITING;
2264 r = source_dispatch(p);
2266 e->state = SD_EVENT_INITIAL;
2272 static sd_event_source* event_next_pending(sd_event *e) {
2277 p = prioq_peek(e->pending);
2281 if (p->enabled == SD_EVENT_OFF)
2287 static int arm_watchdog(sd_event *e) {
2288 struct itimerspec its = {};
2293 assert(e->watchdog_fd >= 0);
2295 t = sleep_between(e,
2296 e->watchdog_last + (e->watchdog_period / 2),
2297 e->watchdog_last + (e->watchdog_period * 3 / 4));
2299 timespec_store(&its.it_value, t);
2301 /* Make sure we never set the watchdog to 0, which tells the
2302 * kernel to disable it. */
2303 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2304 its.it_value.tv_nsec = 1;
2306 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2313 static int process_watchdog(sd_event *e) {
2319 /* Don't notify watchdog too often */
2320 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2323 sd_notify(false, "WATCHDOG=1");
2324 e->watchdog_last = e->timestamp.monotonic;
2326 return arm_watchdog(e);
2329 _public_ int sd_event_prepare(sd_event *e) {
2332 assert_return(e, -EINVAL);
2333 assert_return(!event_pid_changed(e), -ECHILD);
2334 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2335 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2337 if (e->exit_requested)
2342 r = event_prepare(e);
2346 r = event_arm_timer(e, &e->realtime);
2350 r = event_arm_timer(e, &e->boottime);
2354 r = event_arm_timer(e, &e->monotonic);
2358 r = event_arm_timer(e, &e->realtime_alarm);
2362 r = event_arm_timer(e, &e->boottime_alarm);
2366 if (event_next_pending(e) || e->need_process_child)
2369 e->state = SD_EVENT_ARMED;
2374 e->state = SD_EVENT_ARMED;
2375 r = sd_event_wait(e, 0);
2377 e->state = SD_EVENT_ARMED;
2382 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2383 struct epoll_event *ev_queue;
2384 unsigned ev_queue_max;
2387 assert_return(e, -EINVAL);
2388 assert_return(!event_pid_changed(e), -ECHILD);
2389 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2390 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2392 if (e->exit_requested) {
2393 e->state = SD_EVENT_PENDING;
2397 ev_queue_max = MAX(e->n_sources, 1u);
2398 ev_queue = newa(struct epoll_event, ev_queue_max);
2400 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2401 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2403 if (errno == EINTR) {
2404 e->state = SD_EVENT_PENDING;
2412 dual_timestamp_get(&e->timestamp);
2413 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2415 for (i = 0; i < m; i++) {
2417 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME))
2418 r = flush_timer(e, e->realtime.fd, ev_queue[i].events, &e->realtime.next);
2419 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME))
2420 r = flush_timer(e, e->boottime.fd, ev_queue[i].events, &e->boottime.next);
2421 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_MONOTONIC))
2422 r = flush_timer(e, e->monotonic.fd, ev_queue[i].events, &e->monotonic.next);
2423 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_REALTIME_ALARM))
2424 r = flush_timer(e, e->realtime_alarm.fd, ev_queue[i].events, &e->realtime_alarm.next);
2425 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_TIME_BOOTTIME_ALARM))
2426 r = flush_timer(e, e->boottime_alarm.fd, ev_queue[i].events, &e->boottime_alarm.next);
2427 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
2428 r = process_signal(e, ev_queue[i].events);
2429 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2430 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2432 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2438 r = process_watchdog(e);
2442 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2446 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2450 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2454 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2458 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2462 if (e->need_process_child) {
2463 r = process_child(e);
2468 if (event_next_pending(e)) {
2469 e->state = SD_EVENT_PENDING;
2477 e->state = SD_EVENT_INITIAL;
2482 _public_ int sd_event_dispatch(sd_event *e) {
2486 assert_return(e, -EINVAL);
2487 assert_return(!event_pid_changed(e), -ECHILD);
2488 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2489 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2491 if (e->exit_requested)
2492 return dispatch_exit(e);
2494 p = event_next_pending(e);
2498 e->state = SD_EVENT_RUNNING;
2499 r = source_dispatch(p);
2500 e->state = SD_EVENT_INITIAL;
2507 e->state = SD_EVENT_INITIAL;
2512 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2515 assert_return(e, -EINVAL);
2516 assert_return(!event_pid_changed(e), -ECHILD);
2517 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2518 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2520 r = sd_event_prepare(e);
2522 /* There was nothing? Then wait... */
2523 r = sd_event_wait(e, timeout);
2526 /* There's something now, then let's dispatch it */
2527 r = sd_event_dispatch(e);
2537 /// UNNEEDED by elogind
2539 _public_ int sd_event_loop(sd_event *e) {
2542 assert_return(e, -EINVAL);
2543 assert_return(!event_pid_changed(e), -ECHILD);
2544 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2548 while (e->state != SD_EVENT_FINISHED) {
2549 r = sd_event_run(e, (uint64_t) -1);
2561 _public_ int sd_event_get_fd(sd_event *e) {
2563 assert_return(e, -EINVAL);
2564 assert_return(!event_pid_changed(e), -ECHILD);
2570 _public_ int sd_event_get_state(sd_event *e) {
2571 assert_return(e, -EINVAL);
2572 assert_return(!event_pid_changed(e), -ECHILD);
2577 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2578 assert_return(e, -EINVAL);
2579 assert_return(code, -EINVAL);
2580 assert_return(!event_pid_changed(e), -ECHILD);
2582 if (!e->exit_requested)
2585 *code = e->exit_code;
2589 _public_ int sd_event_exit(sd_event *e, int code) {
2590 assert_return(e, -EINVAL);
2591 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2592 assert_return(!event_pid_changed(e), -ECHILD);
2594 e->exit_requested = true;
2595 e->exit_code = code;
2600 /// UNNEEDED by elogind
2602 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2603 assert_return(e, -EINVAL);
2604 assert_return(usec, -EINVAL);
2605 assert_return(!event_pid_changed(e), -ECHILD);
2607 if (!dual_timestamp_is_set(&e->timestamp)) {
2608 /* Implicitly fall back to now() if we never ran
2609 * before and thus have no cached time. */
2616 case CLOCK_REALTIME:
2617 case CLOCK_REALTIME_ALARM:
2618 *usec = e->timestamp.realtime;
2621 case CLOCK_MONOTONIC:
2622 *usec = e->timestamp.monotonic;
2625 case CLOCK_BOOTTIME:
2626 case CLOCK_BOOTTIME_ALARM:
2627 *usec = e->timestamp_boottime;
2635 _public_ int sd_event_default(sd_event **ret) {
2637 static thread_local sd_event *default_event = NULL;
2642 return !!default_event;
2644 if (default_event) {
2645 *ret = sd_event_ref(default_event);
2649 r = sd_event_new(&e);
2653 e->default_event_ptr = &default_event;
2661 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2662 assert_return(e, -EINVAL);
2663 assert_return(tid, -EINVAL);
2664 assert_return(!event_pid_changed(e), -ECHILD);
2674 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2677 assert_return(e, -EINVAL);
2678 assert_return(!event_pid_changed(e), -ECHILD);
2680 if (e->watchdog == !!b)
2684 struct epoll_event ev = {};
2686 r = sd_watchdog_enabled(false, &e->watchdog_period);
2690 /* Issue first ping immediately */
2691 sd_notify(false, "WATCHDOG=1");
2692 e->watchdog_last = now(CLOCK_MONOTONIC);
2694 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2695 if (e->watchdog_fd < 0)
2698 r = arm_watchdog(e);
2702 ev.events = EPOLLIN;
2703 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2705 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2712 if (e->watchdog_fd >= 0) {
2713 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2714 e->watchdog_fd = safe_close(e->watchdog_fd);
2722 e->watchdog_fd = safe_close(e->watchdog_fd);
2726 /// UNNEEDED by elogind
2728 _public_ int sd_event_get_watchdog(sd_event *e) {
2729 assert_return(e, -EINVAL);
2730 assert_return(!event_pid_changed(e), -ECHILD);