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>
26 #include "sd-daemon.h"
30 #include "alloc-util.h"
37 #include "process-util.h"
39 #include "signal-util.h"
40 #include "string-util.h"
41 #include "time-util.h"
44 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
46 typedef enum EventSourceType {
50 SOURCE_TIME_MONOTONIC,
51 SOURCE_TIME_REALTIME_ALARM,
52 SOURCE_TIME_BOOTTIME_ALARM,
59 _SOURCE_EVENT_SOURCE_TYPE_MAX,
60 _SOURCE_EVENT_SOURCE_TYPE_INVALID = -1
63 /* All objects we use in epoll events start with this value, so that
64 * we know how to dispatch it */
65 typedef enum WakeupType {
71 _WAKEUP_TYPE_INVALID = -1,
74 #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)
76 struct sd_event_source {
83 sd_event_handler_t prepare;
87 EventSourceType type:5;
94 unsigned pending_index;
95 unsigned prepare_index;
96 unsigned pending_iteration;
97 unsigned prepare_iteration;
99 LIST_FIELDS(sd_event_source, sources);
103 sd_event_io_handler_t callback;
110 sd_event_time_handler_t callback;
111 usec_t next, accuracy;
112 unsigned earliest_index;
113 unsigned latest_index;
116 sd_event_signal_handler_t callback;
117 struct signalfd_siginfo siginfo;
121 sd_event_child_handler_t callback;
127 sd_event_handler_t callback;
130 sd_event_handler_t callback;
133 sd_event_handler_t callback;
134 unsigned prioq_index;
143 /* For all clocks we maintain two priority queues each, one
144 * ordered for the earliest times the events may be
145 * dispatched, and one ordered by the latest times they must
146 * have been dispatched. The range between the top entries in
147 * the two prioqs is the time window we can freely schedule
160 /* For each priority we maintain one signal fd, so that we
161 * only have to dequeue a single event per priority at a
167 sd_event_source *current;
179 /* timerfd_create() only supports these five clocks so far. We
180 * can add support for more clocks when the kernel learns to
181 * deal with them, too. */
182 struct clock_data realtime;
183 struct clock_data boottime;
184 struct clock_data monotonic;
185 struct clock_data realtime_alarm;
186 struct clock_data boottime_alarm;
190 sd_event_source **signal_sources; /* indexed by signal number */
191 Hashmap *signal_data; /* indexed by priority */
193 Hashmap *child_sources;
194 unsigned n_enabled_child_sources;
203 dual_timestamp timestamp;
204 usec_t timestamp_boottime;
207 bool exit_requested:1;
208 bool need_process_child:1;
214 sd_event **default_event_ptr;
216 usec_t watchdog_last, watchdog_period;
220 LIST_HEAD(sd_event_source, sources);
223 static void source_disconnect(sd_event_source *s);
225 static int pending_prioq_compare(const void *a, const void *b) {
226 const sd_event_source *x = a, *y = b;
231 /* Enabled ones first */
232 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
234 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
237 /* Lower priority values first */
238 if (x->priority < y->priority)
240 if (x->priority > y->priority)
243 /* Older entries first */
244 if (x->pending_iteration < y->pending_iteration)
246 if (x->pending_iteration > y->pending_iteration)
252 static int prepare_prioq_compare(const void *a, const void *b) {
253 const sd_event_source *x = a, *y = b;
258 /* Enabled ones first */
259 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
261 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
264 /* Move most recently prepared ones last, so that we can stop
265 * preparing as soon as we hit one that has already been
266 * prepared in the current iteration */
267 if (x->prepare_iteration < y->prepare_iteration)
269 if (x->prepare_iteration > y->prepare_iteration)
272 /* Lower priority values first */
273 if (x->priority < y->priority)
275 if (x->priority > y->priority)
281 static int earliest_time_prioq_compare(const void *a, const void *b) {
282 const sd_event_source *x = a, *y = b;
284 assert(EVENT_SOURCE_IS_TIME(x->type));
285 assert(x->type == y->type);
287 /* Enabled ones first */
288 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
290 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
293 /* Move the pending ones to the end */
294 if (!x->pending && y->pending)
296 if (x->pending && !y->pending)
300 if (x->time.next < y->time.next)
302 if (x->time.next > y->time.next)
308 static int latest_time_prioq_compare(const void *a, const void *b) {
309 const sd_event_source *x = a, *y = b;
311 assert(EVENT_SOURCE_IS_TIME(x->type));
312 assert(x->type == y->type);
314 /* Enabled ones first */
315 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
317 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
320 /* Move the pending ones to the end */
321 if (!x->pending && y->pending)
323 if (x->pending && !y->pending)
327 if (x->time.next + x->time.accuracy < y->time.next + y->time.accuracy)
329 if (x->time.next + x->time.accuracy > y->time.next + y->time.accuracy)
335 static int exit_prioq_compare(const void *a, const void *b) {
336 const sd_event_source *x = a, *y = b;
338 assert(x->type == SOURCE_EXIT);
339 assert(y->type == SOURCE_EXIT);
341 /* Enabled ones first */
342 if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
344 if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
347 /* Lower priority values first */
348 if (x->priority < y->priority)
350 if (x->priority > y->priority)
356 static void free_clock_data(struct clock_data *d) {
358 assert(d->wakeup == WAKEUP_CLOCK_DATA);
361 prioq_free(d->earliest);
362 prioq_free(d->latest);
365 static void event_free(sd_event *e) {
370 while ((s = e->sources)) {
372 source_disconnect(s);
373 sd_event_source_unref(s);
376 assert(e->n_sources == 0);
378 if (e->default_event_ptr)
379 *(e->default_event_ptr) = NULL;
381 safe_close(e->epoll_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);
395 hashmap_free(e->signal_data);
397 hashmap_free(e->child_sources);
398 set_free(e->post_sources);
402 _public_ int sd_event_new(sd_event** ret) {
406 assert_return(ret, -EINVAL);
408 e = new0(sd_event, 1);
413 e->watchdog_fd = e->epoll_fd = e->realtime.fd = e->boottime.fd = e->monotonic.fd = e->realtime_alarm.fd = e->boottime_alarm.fd = -1;
414 e->realtime.next = e->boottime.next = e->monotonic.next = e->realtime_alarm.next = e->boottime_alarm.next = USEC_INFINITY;
415 e->realtime.wakeup = e->boottime.wakeup = e->monotonic.wakeup = e->realtime_alarm.wakeup = e->boottime_alarm.wakeup = WAKEUP_CLOCK_DATA;
416 e->original_pid = getpid();
417 e->perturb = USEC_INFINITY;
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);
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 int event_make_signal_data(
599 struct signal_data **ret) {
601 struct epoll_event ev = {};
602 struct signal_data *d;
610 if (event_pid_changed(e))
613 if (e->signal_sources && e->signal_sources[sig])
614 priority = e->signal_sources[sig]->priority;
618 d = hashmap_get(e->signal_data, &priority);
620 if (sigismember(&d->sigset, sig) > 0) {
626 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
630 d = new0(struct signal_data, 1);
634 d->wakeup = WAKEUP_SIGNAL_DATA;
636 d->priority = priority;
638 r = hashmap_put(e->signal_data, &d->priority, d);
646 assert_se(sigaddset(&ss_copy, sig) >= 0);
648 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
667 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
680 d->fd = safe_close(d->fd);
681 hashmap_remove(e->signal_data, &d->priority);
688 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
692 /* Turns off the specified signal in the signal data
693 * object. If the signal mask of the object becomes empty that
696 if (sigismember(&d->sigset, sig) == 0)
699 assert_se(sigdelset(&d->sigset, sig) >= 0);
701 if (sigisemptyset(&d->sigset)) {
703 /* If all the mask is all-zero we can get rid of the structure */
704 hashmap_remove(e->signal_data, &d->priority);
713 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
714 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
717 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
718 struct signal_data *d;
719 static const int64_t zero_priority = 0;
723 /* Rechecks if the specified signal is still something we are
724 * interested in. If not, we'll unmask it, and possibly drop
725 * the signalfd for it. */
727 if (sig == SIGCHLD &&
728 e->n_enabled_child_sources > 0)
731 if (e->signal_sources &&
732 e->signal_sources[sig] &&
733 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
737 * The specified signal might be enabled in three different queues:
739 * 1) the one that belongs to the priority passed (if it is non-NULL)
740 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
741 * 3) the 0 priority (to cover the SIGCHLD case)
743 * Hence, let's remove it from all three here.
747 d = hashmap_get(e->signal_data, priority);
749 event_unmask_signal_data(e, d, sig);
752 if (e->signal_sources && e->signal_sources[sig]) {
753 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
755 event_unmask_signal_data(e, d, sig);
758 d = hashmap_get(e->signal_data, &zero_priority);
760 event_unmask_signal_data(e, d, sig);
763 static void source_disconnect(sd_event_source *s) {
771 assert(s->event->n_sources > 0);
777 source_io_unregister(s);
781 case SOURCE_TIME_REALTIME:
782 case SOURCE_TIME_BOOTTIME:
783 case SOURCE_TIME_MONOTONIC:
784 case SOURCE_TIME_REALTIME_ALARM:
785 case SOURCE_TIME_BOOTTIME_ALARM: {
786 struct clock_data *d;
788 d = event_get_clock_data(s->event, s->type);
791 prioq_remove(d->earliest, s, &s->time.earliest_index);
792 prioq_remove(d->latest, s, &s->time.latest_index);
793 d->needs_rearm = true;
798 if (s->signal.sig > 0) {
800 if (s->event->signal_sources)
801 s->event->signal_sources[s->signal.sig] = NULL;
803 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
809 if (s->child.pid > 0) {
810 if (s->enabled != SD_EVENT_OFF) {
811 assert(s->event->n_enabled_child_sources > 0);
812 s->event->n_enabled_child_sources--;
815 (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
816 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
826 set_remove(s->event->post_sources, s);
830 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
834 assert_not_reached("Wut? I shouldn't exist.");
838 prioq_remove(s->event->pending, s, &s->pending_index);
841 prioq_remove(s->event->prepare, s, &s->prepare_index);
845 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
847 LIST_REMOVE(sources, event->sources, s);
851 sd_event_unref(event);
854 static void source_free(sd_event_source *s) {
857 source_disconnect(s);
858 free(s->description);
862 static int source_set_pending(sd_event_source *s, bool b) {
866 assert(s->type != SOURCE_EXIT);
874 s->pending_iteration = s->event->iteration;
876 r = prioq_put(s->event->pending, s, &s->pending_index);
882 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
884 if (EVENT_SOURCE_IS_TIME(s->type)) {
885 struct clock_data *d;
887 d = event_get_clock_data(s->event, s->type);
890 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
891 prioq_reshuffle(d->latest, s, &s->time.latest_index);
892 d->needs_rearm = true;
895 if (s->type == SOURCE_SIGNAL && !b) {
896 struct signal_data *d;
898 d = hashmap_get(s->event->signal_data, &s->priority);
899 if (d && d->current == s)
906 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
911 s = new0(sd_event_source, 1);
917 s->floating = floating;
919 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
924 LIST_PREPEND(sources, e->sources, s);
930 _public_ int sd_event_add_io(
932 sd_event_source **ret,
935 sd_event_io_handler_t callback,
941 assert_return(e, -EINVAL);
942 assert_return(fd >= 0, -EBADF);
943 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
944 assert_return(callback, -EINVAL);
945 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
946 assert_return(!event_pid_changed(e), -ECHILD);
948 s = source_new(e, !ret, SOURCE_IO);
952 s->wakeup = WAKEUP_EVENT_SOURCE;
954 s->io.events = events;
955 s->io.callback = callback;
956 s->userdata = userdata;
957 s->enabled = SD_EVENT_ON;
959 r = source_io_register(s, s->enabled, events);
971 static void initialize_perturb(sd_event *e) {
972 sd_id128_t bootid = {};
974 /* When we sleep for longer, we try to realign the wakeup to
975 the same time wihtin each minute/second/250ms, so that
976 events all across the system can be coalesced into a single
977 CPU wakeup. However, let's take some system-specific
978 randomness for this value, so that in a network of systems
979 with synced clocks timer events are distributed a
980 bit. Here, we calculate a perturbation usec offset from the
983 if (_likely_(e->perturb != USEC_INFINITY))
986 if (sd_id128_get_boot(&bootid) >= 0)
987 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
990 static int event_setup_timer_fd(
992 struct clock_data *d,
995 struct epoll_event ev = {};
1001 if (_likely_(d->fd >= 0))
1004 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1008 ev.events = EPOLLIN;
1011 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1021 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1024 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1027 _public_ int sd_event_add_time(
1029 sd_event_source **ret,
1033 sd_event_time_handler_t callback,
1036 EventSourceType type;
1038 struct clock_data *d;
1041 assert_return(e, -EINVAL);
1042 assert_return(usec != (uint64_t) -1, -EINVAL);
1043 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1044 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1045 assert_return(!event_pid_changed(e), -ECHILD);
1048 callback = time_exit_callback;
1050 type = clock_to_event_source_type(clock);
1051 assert_return(type >= 0, -EOPNOTSUPP);
1053 d = event_get_clock_data(e, type);
1057 d->earliest = prioq_new(earliest_time_prioq_compare);
1063 d->latest = prioq_new(latest_time_prioq_compare);
1069 r = event_setup_timer_fd(e, d, clock);
1074 s = source_new(e, !ret, type);
1078 s->time.next = usec;
1079 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1080 s->time.callback = callback;
1081 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1082 s->userdata = userdata;
1083 s->enabled = SD_EVENT_ONESHOT;
1085 d->needs_rearm = true;
1087 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1091 r = prioq_put(d->latest, s, &s->time.latest_index);
1105 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1108 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1111 _public_ int sd_event_add_signal(
1113 sd_event_source **ret,
1115 sd_event_signal_handler_t callback,
1119 struct signal_data *d;
1123 assert_return(e, -EINVAL);
1124 assert_return(sig > 0, -EINVAL);
1125 assert_return(sig < _NSIG, -EINVAL);
1126 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1127 assert_return(!event_pid_changed(e), -ECHILD);
1130 callback = signal_exit_callback;
1132 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1136 if (!sigismember(&ss, sig))
1139 if (!e->signal_sources) {
1140 e->signal_sources = new0(sd_event_source*, _NSIG);
1141 if (!e->signal_sources)
1143 } else if (e->signal_sources[sig])
1146 s = source_new(e, !ret, SOURCE_SIGNAL);
1150 s->signal.sig = sig;
1151 s->signal.callback = callback;
1152 s->userdata = userdata;
1153 s->enabled = SD_EVENT_ON;
1155 e->signal_sources[sig] = s;
1157 r = event_make_signal_data(e, sig, &d);
1163 /* Use the signal name as description for the event source by default */
1164 (void) sd_event_source_set_description(s, signal_to_string(sig));
1172 /// UNNEEDED by elogind
1174 _public_ int sd_event_add_child(
1176 sd_event_source **ret,
1179 sd_event_child_handler_t callback,
1185 assert_return(e, -EINVAL);
1186 assert_return(pid > 1, -EINVAL);
1187 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1188 assert_return(options != 0, -EINVAL);
1189 assert_return(callback, -EINVAL);
1190 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1191 assert_return(!event_pid_changed(e), -ECHILD);
1193 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1197 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1200 s = source_new(e, !ret, SOURCE_CHILD);
1205 s->child.options = options;
1206 s->child.callback = callback;
1207 s->userdata = userdata;
1208 s->enabled = SD_EVENT_ONESHOT;
1210 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1216 e->n_enabled_child_sources ++;
1218 r = event_make_signal_data(e, SIGCHLD, NULL);
1220 e->n_enabled_child_sources--;
1225 e->need_process_child = true;
1233 _public_ int sd_event_add_defer(
1235 sd_event_source **ret,
1236 sd_event_handler_t callback,
1242 assert_return(e, -EINVAL);
1243 assert_return(callback, -EINVAL);
1244 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1245 assert_return(!event_pid_changed(e), -ECHILD);
1247 s = source_new(e, !ret, SOURCE_DEFER);
1251 s->defer.callback = callback;
1252 s->userdata = userdata;
1253 s->enabled = SD_EVENT_ONESHOT;
1255 r = source_set_pending(s, true);
1268 _public_ int sd_event_add_post(
1270 sd_event_source **ret,
1271 sd_event_handler_t callback,
1277 assert_return(e, -EINVAL);
1278 assert_return(callback, -EINVAL);
1279 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1280 assert_return(!event_pid_changed(e), -ECHILD);
1282 r = set_ensure_allocated(&e->post_sources, NULL);
1286 s = source_new(e, !ret, SOURCE_POST);
1290 s->post.callback = callback;
1291 s->userdata = userdata;
1292 s->enabled = SD_EVENT_ON;
1294 r = set_put(e->post_sources, s);
1306 _public_ int sd_event_add_exit(
1308 sd_event_source **ret,
1309 sd_event_handler_t callback,
1315 assert_return(e, -EINVAL);
1316 assert_return(callback, -EINVAL);
1317 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1318 assert_return(!event_pid_changed(e), -ECHILD);
1321 e->exit = prioq_new(exit_prioq_compare);
1326 s = source_new(e, !ret, SOURCE_EXIT);
1330 s->exit.callback = callback;
1331 s->userdata = userdata;
1332 s->exit.prioq_index = PRIOQ_IDX_NULL;
1333 s->enabled = SD_EVENT_ONESHOT;
1335 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1347 /// UNNEEDED by elogind
1349 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1350 assert_return(s, NULL);
1352 assert(s->n_ref >= 1);
1359 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1364 assert(s->n_ref >= 1);
1367 if (s->n_ref <= 0) {
1368 /* Here's a special hack: when we are called from a
1369 * dispatch handler we won't free the event source
1370 * immediately, but we will detach the fd from the
1371 * epoll. This way it is safe for the caller to unref
1372 * the event source and immediately close the fd, but
1373 * we still retain a valid event source object after
1376 if (s->dispatching) {
1377 if (s->type == SOURCE_IO)
1378 source_io_unregister(s);
1380 source_disconnect(s);
1388 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1389 assert_return(s, -EINVAL);
1390 assert_return(!event_pid_changed(s->event), -ECHILD);
1392 return free_and_strdup(&s->description, description);
1395 /// UNNEEDED by elogind
1397 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1398 assert_return(s, -EINVAL);
1399 assert_return(description, -EINVAL);
1400 assert_return(s->description, -ENXIO);
1401 assert_return(!event_pid_changed(s->event), -ECHILD);
1403 *description = s->description;
1408 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1409 assert_return(s, NULL);
1414 /// UNNEEDED by elogind
1416 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1417 assert_return(s, -EINVAL);
1418 assert_return(s->type != SOURCE_EXIT, -EDOM);
1419 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1420 assert_return(!event_pid_changed(s->event), -ECHILD);
1425 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1426 assert_return(s, -EINVAL);
1427 assert_return(s->type == SOURCE_IO, -EDOM);
1428 assert_return(!event_pid_changed(s->event), -ECHILD);
1434 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1437 assert_return(s, -EINVAL);
1438 assert_return(fd >= 0, -EBADF);
1439 assert_return(s->type == SOURCE_IO, -EDOM);
1440 assert_return(!event_pid_changed(s->event), -ECHILD);
1445 if (s->enabled == SD_EVENT_OFF) {
1447 s->io.registered = false;
1451 saved_fd = s->io.fd;
1452 assert(s->io.registered);
1455 s->io.registered = false;
1457 r = source_io_register(s, s->enabled, s->io.events);
1459 s->io.fd = saved_fd;
1460 s->io.registered = true;
1464 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1470 /// UNNEEDED by elogind
1472 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1473 assert_return(s, -EINVAL);
1474 assert_return(events, -EINVAL);
1475 assert_return(s->type == SOURCE_IO, -EDOM);
1476 assert_return(!event_pid_changed(s->event), -ECHILD);
1478 *events = s->io.events;
1483 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1486 assert_return(s, -EINVAL);
1487 assert_return(s->type == SOURCE_IO, -EDOM);
1488 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1489 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1490 assert_return(!event_pid_changed(s->event), -ECHILD);
1492 /* edge-triggered updates are never skipped, so we can reset edges */
1493 if (s->io.events == events && !(events & EPOLLET))
1496 if (s->enabled != SD_EVENT_OFF) {
1497 r = source_io_register(s, s->enabled, events);
1502 s->io.events = events;
1503 source_set_pending(s, false);
1508 /// UNNEEDED by elogind
1510 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1511 assert_return(s, -EINVAL);
1512 assert_return(revents, -EINVAL);
1513 assert_return(s->type == SOURCE_IO, -EDOM);
1514 assert_return(s->pending, -ENODATA);
1515 assert_return(!event_pid_changed(s->event), -ECHILD);
1517 *revents = s->io.revents;
1521 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1522 assert_return(s, -EINVAL);
1523 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1524 assert_return(!event_pid_changed(s->event), -ECHILD);
1526 return s->signal.sig;
1529 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1530 assert_return(s, -EINVAL);
1531 assert_return(!event_pid_changed(s->event), -ECHILD);
1537 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1540 assert_return(s, -EINVAL);
1541 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1542 assert_return(!event_pid_changed(s->event), -ECHILD);
1544 if (s->priority == priority)
1547 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1548 struct signal_data *old, *d;
1550 /* Move us from the signalfd belonging to the old
1551 * priority to the signalfd of the new priority */
1553 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1555 s->priority = priority;
1557 r = event_make_signal_data(s->event, s->signal.sig, &d);
1559 s->priority = old->priority;
1563 event_unmask_signal_data(s->event, old, s->signal.sig);
1565 s->priority = priority;
1568 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1571 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1573 if (s->type == SOURCE_EXIT)
1574 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1579 /// UNNEEDED by elogind
1581 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1582 assert_return(s, -EINVAL);
1583 assert_return(m, -EINVAL);
1584 assert_return(!event_pid_changed(s->event), -ECHILD);
1591 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1594 assert_return(s, -EINVAL);
1595 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1596 assert_return(!event_pid_changed(s->event), -ECHILD);
1598 /* If we are dead anyway, we are fine with turning off
1599 * sources, but everything else needs to fail. */
1600 if (s->event->state == SD_EVENT_FINISHED)
1601 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1603 if (s->enabled == m)
1606 if (m == SD_EVENT_OFF) {
1611 source_io_unregister(s);
1615 case SOURCE_TIME_REALTIME:
1616 case SOURCE_TIME_BOOTTIME:
1617 case SOURCE_TIME_MONOTONIC:
1618 case SOURCE_TIME_REALTIME_ALARM:
1619 case SOURCE_TIME_BOOTTIME_ALARM: {
1620 struct clock_data *d;
1623 d = event_get_clock_data(s->event, s->type);
1626 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1627 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1628 d->needs_rearm = true;
1635 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1641 assert(s->event->n_enabled_child_sources > 0);
1642 s->event->n_enabled_child_sources--;
1644 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1649 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1658 assert_not_reached("Wut? I shouldn't exist.");
1665 r = source_io_register(s, m, s->io.events);
1672 case SOURCE_TIME_REALTIME:
1673 case SOURCE_TIME_BOOTTIME:
1674 case SOURCE_TIME_MONOTONIC:
1675 case SOURCE_TIME_REALTIME_ALARM:
1676 case SOURCE_TIME_BOOTTIME_ALARM: {
1677 struct clock_data *d;
1680 d = event_get_clock_data(s->event, s->type);
1683 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1684 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1685 d->needs_rearm = true;
1693 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1695 s->enabled = SD_EVENT_OFF;
1696 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1704 if (s->enabled == SD_EVENT_OFF)
1705 s->event->n_enabled_child_sources++;
1709 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1711 s->enabled = SD_EVENT_OFF;
1712 s->event->n_enabled_child_sources--;
1713 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1721 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1730 assert_not_reached("Wut? I shouldn't exist.");
1735 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1738 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1743 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1744 assert_return(s, -EINVAL);
1745 assert_return(usec, -EINVAL);
1746 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1747 assert_return(!event_pid_changed(s->event), -ECHILD);
1749 *usec = s->time.next;
1753 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1754 struct clock_data *d;
1756 assert_return(s, -EINVAL);
1757 assert_return(usec != (uint64_t) -1, -EINVAL);
1758 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1759 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1760 assert_return(!event_pid_changed(s->event), -ECHILD);
1762 s->time.next = usec;
1764 source_set_pending(s, false);
1766 d = event_get_clock_data(s->event, s->type);
1769 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1770 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1771 d->needs_rearm = true;
1776 /// UNNEEDED by elogind
1778 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1779 assert_return(s, -EINVAL);
1780 assert_return(usec, -EINVAL);
1781 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1782 assert_return(!event_pid_changed(s->event), -ECHILD);
1784 *usec = s->time.accuracy;
1788 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1789 struct clock_data *d;
1791 assert_return(s, -EINVAL);
1792 assert_return(usec != (uint64_t) -1, -EINVAL);
1793 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1794 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1795 assert_return(!event_pid_changed(s->event), -ECHILD);
1798 usec = DEFAULT_ACCURACY_USEC;
1800 s->time.accuracy = usec;
1802 source_set_pending(s, false);
1804 d = event_get_clock_data(s->event, s->type);
1807 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1808 d->needs_rearm = true;
1813 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1814 assert_return(s, -EINVAL);
1815 assert_return(clock, -EINVAL);
1816 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1817 assert_return(!event_pid_changed(s->event), -ECHILD);
1819 *clock = event_source_type_to_clock(s->type);
1823 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1824 assert_return(s, -EINVAL);
1825 assert_return(pid, -EINVAL);
1826 assert_return(s->type == SOURCE_CHILD, -EDOM);
1827 assert_return(!event_pid_changed(s->event), -ECHILD);
1829 *pid = s->child.pid;
1834 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1837 assert_return(s, -EINVAL);
1838 assert_return(s->type != SOURCE_EXIT, -EDOM);
1839 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1840 assert_return(!event_pid_changed(s->event), -ECHILD);
1842 if (s->prepare == callback)
1845 if (callback && s->prepare) {
1846 s->prepare = callback;
1850 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1854 s->prepare = callback;
1857 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1861 prioq_remove(s->event->prepare, s, &s->prepare_index);
1866 /// UNNEEDED by elogind
1868 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1869 assert_return(s, NULL);
1874 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1877 assert_return(s, NULL);
1880 s->userdata = userdata;
1886 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1897 initialize_perturb(e);
1900 Find a good time to wake up again between times a and b. We
1901 have two goals here:
1903 a) We want to wake up as seldom as possible, hence prefer
1904 later times over earlier times.
1906 b) But if we have to wake up, then let's make sure to
1907 dispatch as much as possible on the entire system.
1909 We implement this by waking up everywhere at the same time
1910 within any given minute if we can, synchronised via the
1911 perturbation value determined from the boot ID. If we can't,
1912 then we try to find the same spot in every 10s, then 1s and
1913 then 250ms step. Otherwise, we pick the last possible time
1917 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1919 if (_unlikely_(c < USEC_PER_MINUTE))
1922 c -= USEC_PER_MINUTE;
1928 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1930 if (_unlikely_(c < USEC_PER_SEC*10))
1933 c -= USEC_PER_SEC*10;
1939 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1941 if (_unlikely_(c < USEC_PER_SEC))
1950 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1952 if (_unlikely_(c < USEC_PER_MSEC*250))
1955 c -= USEC_PER_MSEC*250;
1964 static int event_arm_timer(
1966 struct clock_data *d) {
1968 struct itimerspec its = {};
1969 sd_event_source *a, *b;
1976 if (!d->needs_rearm)
1979 d->needs_rearm = false;
1981 a = prioq_peek(d->earliest);
1982 if (!a || a->enabled == SD_EVENT_OFF) {
1987 if (d->next == USEC_INFINITY)
1991 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1995 d->next = USEC_INFINITY;
1999 b = prioq_peek(d->latest);
2000 assert_se(b && b->enabled != SD_EVENT_OFF);
2002 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
2006 assert_se(d->fd >= 0);
2009 /* We don' want to disarm here, just mean some time looooong ago. */
2010 its.it_value.tv_sec = 0;
2011 its.it_value.tv_nsec = 1;
2013 timespec_store(&its.it_value, t);
2015 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2023 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2026 assert(s->type == SOURCE_IO);
2028 /* If the event source was already pending, we just OR in the
2029 * new revents, otherwise we reset the value. The ORing is
2030 * necessary to handle EPOLLONESHOT events properly where
2031 * readability might happen independently of writability, and
2032 * we need to keep track of both */
2035 s->io.revents |= revents;
2037 s->io.revents = revents;
2039 return source_set_pending(s, true);
2042 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2049 assert_return(events == EPOLLIN, -EIO);
2051 ss = read(fd, &x, sizeof(x));
2053 if (errno == EAGAIN || errno == EINTR)
2059 if (_unlikely_(ss != sizeof(x)))
2063 *next = USEC_INFINITY;
2068 static int process_timer(
2071 struct clock_data *d) {
2080 s = prioq_peek(d->earliest);
2083 s->enabled == SD_EVENT_OFF ||
2087 r = source_set_pending(s, true);
2091 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2092 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2093 d->needs_rearm = true;
2099 static int process_child(sd_event *e) {
2106 e->need_process_child = false;
2109 So, this is ugly. We iteratively invoke waitid() with P_PID
2110 + WNOHANG for each PID we wait for, instead of using
2111 P_ALL. This is because we only want to get child
2112 information of very specific child processes, and not all
2113 of them. We might not have processed the SIGCHLD even of a
2114 previous invocation and we don't want to maintain a
2115 unbounded *per-child* event queue, hence we really don't
2116 want anything flushed out of the kernel's queue that we
2117 don't care about. Since this is O(n) this means that if you
2118 have a lot of processes you probably want to handle SIGCHLD
2121 We do not reap the children here (by using WNOWAIT), this
2122 is only done after the event source is dispatched so that
2123 the callback still sees the process as a zombie.
2126 HASHMAP_FOREACH(s, e->child_sources, i) {
2127 assert(s->type == SOURCE_CHILD);
2132 if (s->enabled == SD_EVENT_OFF)
2135 zero(s->child.siginfo);
2136 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2137 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2141 if (s->child.siginfo.si_pid != 0) {
2143 s->child.siginfo.si_code == CLD_EXITED ||
2144 s->child.siginfo.si_code == CLD_KILLED ||
2145 s->child.siginfo.si_code == CLD_DUMPED;
2147 if (!zombie && (s->child.options & WEXITED)) {
2148 /* If the child isn't dead then let's
2149 * immediately remove the state change
2150 * from the queue, since there's no
2151 * benefit in leaving it queued */
2153 assert(s->child.options & (WSTOPPED|WCONTINUED));
2154 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2157 r = source_set_pending(s, true);
2166 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2167 bool read_one = false;
2171 assert_return(events == EPOLLIN, -EIO);
2173 /* If there's a signal queued on this priority and SIGCHLD is
2174 on this priority too, then make sure to recheck the
2175 children we watch. This is because we only ever dequeue
2176 the first signal per priority, and if we dequeue one, and
2177 SIGCHLD might be enqueued later we wouldn't know, but we
2178 might have higher priority children we care about hence we
2179 need to check that explicitly. */
2181 if (sigismember(&d->sigset, SIGCHLD))
2182 e->need_process_child = true;
2184 /* If there's already an event source pending for this
2185 * priority we don't read another */
2190 struct signalfd_siginfo si;
2192 sd_event_source *s = NULL;
2194 n = read(d->fd, &si, sizeof(si));
2196 if (errno == EAGAIN || errno == EINTR)
2202 if (_unlikely_(n != sizeof(si)))
2205 assert(si.ssi_signo < _NSIG);
2209 if (e->signal_sources)
2210 s = e->signal_sources[si.ssi_signo];
2216 s->signal.siginfo = si;
2219 r = source_set_pending(s, true);
2227 static int source_dispatch(sd_event_source *s) {
2231 assert(s->pending || s->type == SOURCE_EXIT);
2233 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2234 r = source_set_pending(s, false);
2239 if (s->type != SOURCE_POST) {
2243 /* If we execute a non-post source, let's mark all
2244 * post sources as pending */
2246 SET_FOREACH(z, s->event->post_sources, i) {
2247 if (z->enabled == SD_EVENT_OFF)
2250 r = source_set_pending(z, true);
2256 if (s->enabled == SD_EVENT_ONESHOT) {
2257 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2262 s->dispatching = true;
2267 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2270 case SOURCE_TIME_REALTIME:
2271 case SOURCE_TIME_BOOTTIME:
2272 case SOURCE_TIME_MONOTONIC:
2273 case SOURCE_TIME_REALTIME_ALARM:
2274 case SOURCE_TIME_BOOTTIME_ALARM:
2275 r = s->time.callback(s, s->time.next, s->userdata);
2279 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2282 case SOURCE_CHILD: {
2285 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2286 s->child.siginfo.si_code == CLD_KILLED ||
2287 s->child.siginfo.si_code == CLD_DUMPED;
2289 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2291 /* Now, reap the PID for good. */
2293 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2299 r = s->defer.callback(s, s->userdata);
2303 r = s->post.callback(s, s->userdata);
2307 r = s->exit.callback(s, s->userdata);
2310 case SOURCE_WATCHDOG:
2311 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2312 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2313 assert_not_reached("Wut? I shouldn't exist.");
2316 s->dispatching = false;
2320 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2322 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2328 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2333 static int event_prepare(sd_event *e) {
2341 s = prioq_peek(e->prepare);
2342 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2345 s->prepare_iteration = e->iteration;
2346 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2352 s->dispatching = true;
2353 r = s->prepare(s, s->userdata);
2354 s->dispatching = false;
2358 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2360 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2366 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2372 static int dispatch_exit(sd_event *e) {
2378 p = prioq_peek(e->exit);
2379 if (!p || p->enabled == SD_EVENT_OFF) {
2380 e->state = SD_EVENT_FINISHED;
2386 e->state = SD_EVENT_EXITING;
2388 r = source_dispatch(p);
2390 e->state = SD_EVENT_INITIAL;
2396 static sd_event_source* event_next_pending(sd_event *e) {
2401 p = prioq_peek(e->pending);
2405 if (p->enabled == SD_EVENT_OFF)
2411 static int arm_watchdog(sd_event *e) {
2412 struct itimerspec its = {};
2417 assert(e->watchdog_fd >= 0);
2419 t = sleep_between(e,
2420 e->watchdog_last + (e->watchdog_period / 2),
2421 e->watchdog_last + (e->watchdog_period * 3 / 4));
2423 timespec_store(&its.it_value, t);
2425 /* Make sure we never set the watchdog to 0, which tells the
2426 * kernel to disable it. */
2427 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2428 its.it_value.tv_nsec = 1;
2430 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2437 static int process_watchdog(sd_event *e) {
2443 /* Don't notify watchdog too often */
2444 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2447 sd_notify(false, "WATCHDOG=1");
2448 e->watchdog_last = e->timestamp.monotonic;
2450 return arm_watchdog(e);
2453 _public_ int sd_event_prepare(sd_event *e) {
2456 assert_return(e, -EINVAL);
2457 assert_return(!event_pid_changed(e), -ECHILD);
2458 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2459 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2461 if (e->exit_requested)
2466 r = event_prepare(e);
2470 r = event_arm_timer(e, &e->realtime);
2474 r = event_arm_timer(e, &e->boottime);
2478 r = event_arm_timer(e, &e->monotonic);
2482 r = event_arm_timer(e, &e->realtime_alarm);
2486 r = event_arm_timer(e, &e->boottime_alarm);
2490 if (event_next_pending(e) || e->need_process_child)
2493 e->state = SD_EVENT_ARMED;
2498 e->state = SD_EVENT_ARMED;
2499 r = sd_event_wait(e, 0);
2501 e->state = SD_EVENT_ARMED;
2506 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2507 struct epoll_event *ev_queue;
2508 unsigned ev_queue_max;
2511 assert_return(e, -EINVAL);
2512 assert_return(!event_pid_changed(e), -ECHILD);
2513 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2514 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2516 if (e->exit_requested) {
2517 e->state = SD_EVENT_PENDING;
2521 ev_queue_max = MAX(e->n_sources, 1u);
2522 ev_queue = newa(struct epoll_event, ev_queue_max);
2524 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2525 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2527 if (errno == EINTR) {
2528 e->state = SD_EVENT_PENDING;
2536 dual_timestamp_get(&e->timestamp);
2537 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2539 for (i = 0; i < m; i++) {
2541 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2542 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2544 WakeupType *t = ev_queue[i].data.ptr;
2548 case WAKEUP_EVENT_SOURCE:
2549 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2552 case WAKEUP_CLOCK_DATA: {
2553 struct clock_data *d = ev_queue[i].data.ptr;
2554 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2558 case WAKEUP_SIGNAL_DATA:
2559 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2563 assert_not_reached("Invalid wake-up pointer");
2570 r = process_watchdog(e);
2574 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2578 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2582 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2586 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2590 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2594 if (e->need_process_child) {
2595 r = process_child(e);
2600 if (event_next_pending(e)) {
2601 e->state = SD_EVENT_PENDING;
2609 e->state = SD_EVENT_INITIAL;
2614 _public_ int sd_event_dispatch(sd_event *e) {
2618 assert_return(e, -EINVAL);
2619 assert_return(!event_pid_changed(e), -ECHILD);
2620 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2621 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2623 if (e->exit_requested)
2624 return dispatch_exit(e);
2626 p = event_next_pending(e);
2630 e->state = SD_EVENT_RUNNING;
2631 r = source_dispatch(p);
2632 e->state = SD_EVENT_INITIAL;
2639 e->state = SD_EVENT_INITIAL;
2644 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2647 assert_return(e, -EINVAL);
2648 assert_return(!event_pid_changed(e), -ECHILD);
2649 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2650 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2652 r = sd_event_prepare(e);
2654 /* There was nothing? Then wait... */
2655 r = sd_event_wait(e, timeout);
2658 /* There's something now, then let's dispatch it */
2659 r = sd_event_dispatch(e);
2669 /// UNNEEDED by elogind
2671 _public_ int sd_event_loop(sd_event *e) {
2674 assert_return(e, -EINVAL);
2675 assert_return(!event_pid_changed(e), -ECHILD);
2676 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2680 while (e->state != SD_EVENT_FINISHED) {
2681 r = sd_event_run(e, (uint64_t) -1);
2693 _public_ int sd_event_get_fd(sd_event *e) {
2695 assert_return(e, -EINVAL);
2696 assert_return(!event_pid_changed(e), -ECHILD);
2702 _public_ int sd_event_get_state(sd_event *e) {
2703 assert_return(e, -EINVAL);
2704 assert_return(!event_pid_changed(e), -ECHILD);
2709 /// UNNEEDED by elogind
2711 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2712 assert_return(e, -EINVAL);
2713 assert_return(code, -EINVAL);
2714 assert_return(!event_pid_changed(e), -ECHILD);
2716 if (!e->exit_requested)
2719 *code = e->exit_code;
2724 _public_ int sd_event_exit(sd_event *e, int code) {
2725 assert_return(e, -EINVAL);
2726 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2727 assert_return(!event_pid_changed(e), -ECHILD);
2729 e->exit_requested = true;
2730 e->exit_code = code;
2735 /// UNNEEDED by elogind
2737 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2738 assert_return(e, -EINVAL);
2739 assert_return(usec, -EINVAL);
2740 assert_return(!event_pid_changed(e), -ECHILD);
2742 if (!dual_timestamp_is_set(&e->timestamp)) {
2743 /* Implicitly fall back to now() if we never ran
2744 * before and thus have no cached time. */
2751 case CLOCK_REALTIME:
2752 case CLOCK_REALTIME_ALARM:
2753 *usec = e->timestamp.realtime;
2756 case CLOCK_MONOTONIC:
2757 *usec = e->timestamp.monotonic;
2760 case CLOCK_BOOTTIME:
2761 case CLOCK_BOOTTIME_ALARM:
2762 *usec = e->timestamp_boottime;
2770 _public_ int sd_event_default(sd_event **ret) {
2772 static thread_local sd_event *default_event = NULL;
2777 return !!default_event;
2779 if (default_event) {
2780 *ret = sd_event_ref(default_event);
2784 r = sd_event_new(&e);
2788 e->default_event_ptr = &default_event;
2796 /// UNNEEDED by elogind
2798 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2799 assert_return(e, -EINVAL);
2800 assert_return(tid, -EINVAL);
2801 assert_return(!event_pid_changed(e), -ECHILD);
2812 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2815 assert_return(e, -EINVAL);
2816 assert_return(!event_pid_changed(e), -ECHILD);
2818 if (e->watchdog == !!b)
2822 struct epoll_event ev = {};
2824 r = sd_watchdog_enabled(false, &e->watchdog_period);
2828 /* Issue first ping immediately */
2829 sd_notify(false, "WATCHDOG=1");
2830 e->watchdog_last = now(CLOCK_MONOTONIC);
2832 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2833 if (e->watchdog_fd < 0)
2836 r = arm_watchdog(e);
2840 ev.events = EPOLLIN;
2841 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2843 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2850 if (e->watchdog_fd >= 0) {
2851 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2852 e->watchdog_fd = safe_close(e->watchdog_fd);
2860 e->watchdog_fd = safe_close(e->watchdog_fd);
2864 /// UNNEEDED by elogind
2866 _public_ int sd_event_get_watchdog(sd_event *e) {
2867 assert_return(e, -EINVAL);
2868 assert_return(!event_pid_changed(e), -ECHILD);