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 #if 0 /// UNNEEDED by elogind
521 static clockid_t event_source_type_to_clock(EventSourceType t) {
525 case SOURCE_TIME_REALTIME:
526 return CLOCK_REALTIME;
528 case SOURCE_TIME_BOOTTIME:
529 return CLOCK_BOOTTIME;
531 case SOURCE_TIME_MONOTONIC:
532 return CLOCK_MONOTONIC;
534 case SOURCE_TIME_REALTIME_ALARM:
535 return CLOCK_REALTIME_ALARM;
537 case SOURCE_TIME_BOOTTIME_ALARM:
538 return CLOCK_BOOTTIME_ALARM;
541 return (clockid_t) -1;
546 static EventSourceType clock_to_event_source_type(clockid_t clock) {
551 return SOURCE_TIME_REALTIME;
554 return SOURCE_TIME_BOOTTIME;
556 case CLOCK_MONOTONIC:
557 return SOURCE_TIME_MONOTONIC;
559 case CLOCK_REALTIME_ALARM:
560 return SOURCE_TIME_REALTIME_ALARM;
562 case CLOCK_BOOTTIME_ALARM:
563 return SOURCE_TIME_BOOTTIME_ALARM;
566 return _SOURCE_EVENT_SOURCE_TYPE_INVALID;
570 static struct clock_data* event_get_clock_data(sd_event *e, EventSourceType t) {
575 case SOURCE_TIME_REALTIME:
578 case SOURCE_TIME_BOOTTIME:
581 case SOURCE_TIME_MONOTONIC:
582 return &e->monotonic;
584 case SOURCE_TIME_REALTIME_ALARM:
585 return &e->realtime_alarm;
587 case SOURCE_TIME_BOOTTIME_ALARM:
588 return &e->boottime_alarm;
595 static int event_make_signal_data(
598 struct signal_data **ret) {
600 struct epoll_event ev = {};
601 struct signal_data *d;
609 if (event_pid_changed(e))
612 if (e->signal_sources && e->signal_sources[sig])
613 priority = e->signal_sources[sig]->priority;
617 d = hashmap_get(e->signal_data, &priority);
619 if (sigismember(&d->sigset, sig) > 0) {
625 r = hashmap_ensure_allocated(&e->signal_data, &uint64_hash_ops);
629 d = new0(struct signal_data, 1);
633 d->wakeup = WAKEUP_SIGNAL_DATA;
635 d->priority = priority;
637 r = hashmap_put(e->signal_data, &d->priority, d);
645 assert_se(sigaddset(&ss_copy, sig) >= 0);
647 r = signalfd(d->fd, &ss_copy, SFD_NONBLOCK|SFD_CLOEXEC);
666 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, d->fd, &ev);
679 d->fd = safe_close(d->fd);
680 hashmap_remove(e->signal_data, &d->priority);
687 static void event_unmask_signal_data(sd_event *e, struct signal_data *d, int sig) {
691 /* Turns off the specified signal in the signal data
692 * object. If the signal mask of the object becomes empty that
695 if (sigismember(&d->sigset, sig) == 0)
698 assert_se(sigdelset(&d->sigset, sig) >= 0);
700 if (sigisemptyset(&d->sigset)) {
702 /* If all the mask is all-zero we can get rid of the structure */
703 hashmap_remove(e->signal_data, &d->priority);
712 if (signalfd(d->fd, &d->sigset, SFD_NONBLOCK|SFD_CLOEXEC) < 0)
713 log_debug_errno(errno, "Failed to unset signal bit, ignoring: %m");
716 static void event_gc_signal_data(sd_event *e, const int64_t *priority, int sig) {
717 struct signal_data *d;
718 static const int64_t zero_priority = 0;
722 /* Rechecks if the specified signal is still something we are
723 * interested in. If not, we'll unmask it, and possibly drop
724 * the signalfd for it. */
726 if (sig == SIGCHLD &&
727 e->n_enabled_child_sources > 0)
730 if (e->signal_sources &&
731 e->signal_sources[sig] &&
732 e->signal_sources[sig]->enabled != SD_EVENT_OFF)
736 * The specified signal might be enabled in three different queues:
738 * 1) the one that belongs to the priority passed (if it is non-NULL)
739 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
740 * 3) the 0 priority (to cover the SIGCHLD case)
742 * Hence, let's remove it from all three here.
746 d = hashmap_get(e->signal_data, priority);
748 event_unmask_signal_data(e, d, sig);
751 if (e->signal_sources && e->signal_sources[sig]) {
752 d = hashmap_get(e->signal_data, &e->signal_sources[sig]->priority);
754 event_unmask_signal_data(e, d, sig);
757 d = hashmap_get(e->signal_data, &zero_priority);
759 event_unmask_signal_data(e, d, sig);
762 static void source_disconnect(sd_event_source *s) {
770 assert(s->event->n_sources > 0);
776 source_io_unregister(s);
780 case SOURCE_TIME_REALTIME:
781 case SOURCE_TIME_BOOTTIME:
782 case SOURCE_TIME_MONOTONIC:
783 case SOURCE_TIME_REALTIME_ALARM:
784 case SOURCE_TIME_BOOTTIME_ALARM: {
785 struct clock_data *d;
787 d = event_get_clock_data(s->event, s->type);
790 prioq_remove(d->earliest, s, &s->time.earliest_index);
791 prioq_remove(d->latest, s, &s->time.latest_index);
792 d->needs_rearm = true;
797 if (s->signal.sig > 0) {
799 if (s->event->signal_sources)
800 s->event->signal_sources[s->signal.sig] = NULL;
802 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
808 if (s->child.pid > 0) {
809 if (s->enabled != SD_EVENT_OFF) {
810 assert(s->event->n_enabled_child_sources > 0);
811 s->event->n_enabled_child_sources--;
814 (void) hashmap_remove(s->event->child_sources, PID_TO_PTR(s->child.pid));
815 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
825 set_remove(s->event->post_sources, s);
829 prioq_remove(s->event->exit, s, &s->exit.prioq_index);
833 assert_not_reached("Wut? I shouldn't exist.");
837 prioq_remove(s->event->pending, s, &s->pending_index);
840 prioq_remove(s->event->prepare, s, &s->prepare_index);
844 s->type = _SOURCE_EVENT_SOURCE_TYPE_INVALID;
846 LIST_REMOVE(sources, event->sources, s);
850 sd_event_unref(event);
853 static void source_free(sd_event_source *s) {
856 source_disconnect(s);
857 free(s->description);
861 static int source_set_pending(sd_event_source *s, bool b) {
865 assert(s->type != SOURCE_EXIT);
873 s->pending_iteration = s->event->iteration;
875 r = prioq_put(s->event->pending, s, &s->pending_index);
881 assert_se(prioq_remove(s->event->pending, s, &s->pending_index));
883 if (EVENT_SOURCE_IS_TIME(s->type)) {
884 struct clock_data *d;
886 d = event_get_clock_data(s->event, s->type);
889 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
890 prioq_reshuffle(d->latest, s, &s->time.latest_index);
891 d->needs_rearm = true;
894 if (s->type == SOURCE_SIGNAL && !b) {
895 struct signal_data *d;
897 d = hashmap_get(s->event->signal_data, &s->priority);
898 if (d && d->current == s)
905 static sd_event_source *source_new(sd_event *e, bool floating, EventSourceType type) {
910 s = new0(sd_event_source, 1);
916 s->floating = floating;
918 s->pending_index = s->prepare_index = PRIOQ_IDX_NULL;
923 LIST_PREPEND(sources, e->sources, s);
929 _public_ int sd_event_add_io(
931 sd_event_source **ret,
934 sd_event_io_handler_t callback,
940 assert_return(e, -EINVAL);
941 assert_return(fd >= 0, -EBADF);
942 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
943 assert_return(callback, -EINVAL);
944 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
945 assert_return(!event_pid_changed(e), -ECHILD);
947 s = source_new(e, !ret, SOURCE_IO);
951 s->wakeup = WAKEUP_EVENT_SOURCE;
953 s->io.events = events;
954 s->io.callback = callback;
955 s->userdata = userdata;
956 s->enabled = SD_EVENT_ON;
958 r = source_io_register(s, s->enabled, events);
970 static void initialize_perturb(sd_event *e) {
971 sd_id128_t bootid = {};
973 /* When we sleep for longer, we try to realign the wakeup to
974 the same time wihtin each minute/second/250ms, so that
975 events all across the system can be coalesced into a single
976 CPU wakeup. However, let's take some system-specific
977 randomness for this value, so that in a network of systems
978 with synced clocks timer events are distributed a
979 bit. Here, we calculate a perturbation usec offset from the
982 if (_likely_(e->perturb != USEC_INFINITY))
985 if (sd_id128_get_boot(&bootid) >= 0)
986 e->perturb = (bootid.qwords[0] ^ bootid.qwords[1]) % USEC_PER_MINUTE;
989 static int event_setup_timer_fd(
991 struct clock_data *d,
994 struct epoll_event ev = {};
1000 if (_likely_(d->fd >= 0))
1003 fd = timerfd_create(clock, TFD_NONBLOCK|TFD_CLOEXEC);
1007 ev.events = EPOLLIN;
1010 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, fd, &ev);
1020 static int time_exit_callback(sd_event_source *s, uint64_t usec, void *userdata) {
1023 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1026 _public_ int sd_event_add_time(
1028 sd_event_source **ret,
1032 sd_event_time_handler_t callback,
1035 EventSourceType type;
1037 struct clock_data *d;
1040 assert_return(e, -EINVAL);
1041 assert_return(usec != (uint64_t) -1, -EINVAL);
1042 assert_return(accuracy != (uint64_t) -1, -EINVAL);
1043 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1044 assert_return(!event_pid_changed(e), -ECHILD);
1047 callback = time_exit_callback;
1049 type = clock_to_event_source_type(clock);
1050 assert_return(type >= 0, -EOPNOTSUPP);
1052 d = event_get_clock_data(e, type);
1056 d->earliest = prioq_new(earliest_time_prioq_compare);
1062 d->latest = prioq_new(latest_time_prioq_compare);
1068 r = event_setup_timer_fd(e, d, clock);
1073 s = source_new(e, !ret, type);
1077 s->time.next = usec;
1078 s->time.accuracy = accuracy == 0 ? DEFAULT_ACCURACY_USEC : accuracy;
1079 s->time.callback = callback;
1080 s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
1081 s->userdata = userdata;
1082 s->enabled = SD_EVENT_ONESHOT;
1084 d->needs_rearm = true;
1086 r = prioq_put(d->earliest, s, &s->time.earliest_index);
1090 r = prioq_put(d->latest, s, &s->time.latest_index);
1104 static int signal_exit_callback(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
1107 return sd_event_exit(sd_event_source_get_event(s), PTR_TO_INT(userdata));
1110 _public_ int sd_event_add_signal(
1112 sd_event_source **ret,
1114 sd_event_signal_handler_t callback,
1118 struct signal_data *d;
1122 assert_return(e, -EINVAL);
1123 assert_return(sig > 0, -EINVAL);
1124 assert_return(sig < _NSIG, -EINVAL);
1125 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1126 assert_return(!event_pid_changed(e), -ECHILD);
1129 callback = signal_exit_callback;
1131 r = pthread_sigmask(SIG_SETMASK, NULL, &ss);
1135 if (!sigismember(&ss, sig))
1138 if (!e->signal_sources) {
1139 e->signal_sources = new0(sd_event_source*, _NSIG);
1140 if (!e->signal_sources)
1142 } else if (e->signal_sources[sig])
1145 s = source_new(e, !ret, SOURCE_SIGNAL);
1149 s->signal.sig = sig;
1150 s->signal.callback = callback;
1151 s->userdata = userdata;
1152 s->enabled = SD_EVENT_ON;
1154 e->signal_sources[sig] = s;
1156 r = event_make_signal_data(e, sig, &d);
1162 /* Use the signal name as description for the event source by default */
1163 (void) sd_event_source_set_description(s, signal_to_string(sig));
1171 #if 0 /// UNNEEDED by elogind
1172 _public_ int sd_event_add_child(
1174 sd_event_source **ret,
1177 sd_event_child_handler_t callback,
1183 assert_return(e, -EINVAL);
1184 assert_return(pid > 1, -EINVAL);
1185 assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
1186 assert_return(options != 0, -EINVAL);
1187 assert_return(callback, -EINVAL);
1188 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1189 assert_return(!event_pid_changed(e), -ECHILD);
1191 r = hashmap_ensure_allocated(&e->child_sources, NULL);
1195 if (hashmap_contains(e->child_sources, PID_TO_PTR(pid)))
1198 s = source_new(e, !ret, SOURCE_CHILD);
1203 s->child.options = options;
1204 s->child.callback = callback;
1205 s->userdata = userdata;
1206 s->enabled = SD_EVENT_ONESHOT;
1208 r = hashmap_put(e->child_sources, PID_TO_PTR(pid), s);
1214 e->n_enabled_child_sources ++;
1216 r = event_make_signal_data(e, SIGCHLD, NULL);
1218 e->n_enabled_child_sources--;
1223 e->need_process_child = true;
1231 _public_ int sd_event_add_defer(
1233 sd_event_source **ret,
1234 sd_event_handler_t callback,
1240 assert_return(e, -EINVAL);
1241 assert_return(callback, -EINVAL);
1242 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1243 assert_return(!event_pid_changed(e), -ECHILD);
1245 s = source_new(e, !ret, SOURCE_DEFER);
1249 s->defer.callback = callback;
1250 s->userdata = userdata;
1251 s->enabled = SD_EVENT_ONESHOT;
1253 r = source_set_pending(s, true);
1266 _public_ int sd_event_add_post(
1268 sd_event_source **ret,
1269 sd_event_handler_t callback,
1275 assert_return(e, -EINVAL);
1276 assert_return(callback, -EINVAL);
1277 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1278 assert_return(!event_pid_changed(e), -ECHILD);
1280 r = set_ensure_allocated(&e->post_sources, NULL);
1284 s = source_new(e, !ret, SOURCE_POST);
1288 s->post.callback = callback;
1289 s->userdata = userdata;
1290 s->enabled = SD_EVENT_ON;
1292 r = set_put(e->post_sources, s);
1304 _public_ int sd_event_add_exit(
1306 sd_event_source **ret,
1307 sd_event_handler_t callback,
1313 assert_return(e, -EINVAL);
1314 assert_return(callback, -EINVAL);
1315 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
1316 assert_return(!event_pid_changed(e), -ECHILD);
1319 e->exit = prioq_new(exit_prioq_compare);
1324 s = source_new(e, !ret, SOURCE_EXIT);
1328 s->exit.callback = callback;
1329 s->userdata = userdata;
1330 s->exit.prioq_index = PRIOQ_IDX_NULL;
1331 s->enabled = SD_EVENT_ONESHOT;
1333 r = prioq_put(s->event->exit, s, &s->exit.prioq_index);
1345 #if 0 /// UNNEEDED by elogind
1346 _public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
1347 assert_return(s, NULL);
1349 assert(s->n_ref >= 1);
1356 _public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
1361 assert(s->n_ref >= 1);
1364 if (s->n_ref <= 0) {
1365 /* Here's a special hack: when we are called from a
1366 * dispatch handler we won't free the event source
1367 * immediately, but we will detach the fd from the
1368 * epoll. This way it is safe for the caller to unref
1369 * the event source and immediately close the fd, but
1370 * we still retain a valid event source object after
1373 if (s->dispatching) {
1374 if (s->type == SOURCE_IO)
1375 source_io_unregister(s);
1377 source_disconnect(s);
1385 _public_ int sd_event_source_set_description(sd_event_source *s, const char *description) {
1386 assert_return(s, -EINVAL);
1387 assert_return(!event_pid_changed(s->event), -ECHILD);
1389 return free_and_strdup(&s->description, description);
1392 #if 0 /// UNNEEDED by elogind
1393 _public_ int sd_event_source_get_description(sd_event_source *s, const char **description) {
1394 assert_return(s, -EINVAL);
1395 assert_return(description, -EINVAL);
1396 assert_return(s->description, -ENXIO);
1397 assert_return(!event_pid_changed(s->event), -ECHILD);
1399 *description = s->description;
1404 _public_ sd_event *sd_event_source_get_event(sd_event_source *s) {
1405 assert_return(s, NULL);
1410 #if 0 /// UNNEEDED by elogind
1411 _public_ int sd_event_source_get_pending(sd_event_source *s) {
1412 assert_return(s, -EINVAL);
1413 assert_return(s->type != SOURCE_EXIT, -EDOM);
1414 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1415 assert_return(!event_pid_changed(s->event), -ECHILD);
1420 _public_ int sd_event_source_get_io_fd(sd_event_source *s) {
1421 assert_return(s, -EINVAL);
1422 assert_return(s->type == SOURCE_IO, -EDOM);
1423 assert_return(!event_pid_changed(s->event), -ECHILD);
1429 _public_ int sd_event_source_set_io_fd(sd_event_source *s, int fd) {
1432 assert_return(s, -EINVAL);
1433 assert_return(fd >= 0, -EBADF);
1434 assert_return(s->type == SOURCE_IO, -EDOM);
1435 assert_return(!event_pid_changed(s->event), -ECHILD);
1440 if (s->enabled == SD_EVENT_OFF) {
1442 s->io.registered = false;
1446 saved_fd = s->io.fd;
1447 assert(s->io.registered);
1450 s->io.registered = false;
1452 r = source_io_register(s, s->enabled, s->io.events);
1454 s->io.fd = saved_fd;
1455 s->io.registered = true;
1459 epoll_ctl(s->event->epoll_fd, EPOLL_CTL_DEL, saved_fd, NULL);
1465 #if 0 /// UNNEEDED by elogind
1466 _public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
1467 assert_return(s, -EINVAL);
1468 assert_return(events, -EINVAL);
1469 assert_return(s->type == SOURCE_IO, -EDOM);
1470 assert_return(!event_pid_changed(s->event), -ECHILD);
1472 *events = s->io.events;
1477 _public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
1480 assert_return(s, -EINVAL);
1481 assert_return(s->type == SOURCE_IO, -EDOM);
1482 assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP|EPOLLET)), -EINVAL);
1483 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1484 assert_return(!event_pid_changed(s->event), -ECHILD);
1486 /* edge-triggered updates are never skipped, so we can reset edges */
1487 if (s->io.events == events && !(events & EPOLLET))
1490 if (s->enabled != SD_EVENT_OFF) {
1491 r = source_io_register(s, s->enabled, events);
1496 s->io.events = events;
1497 source_set_pending(s, false);
1502 #if 0 /// UNNEEDED by elogind
1503 _public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
1504 assert_return(s, -EINVAL);
1505 assert_return(revents, -EINVAL);
1506 assert_return(s->type == SOURCE_IO, -EDOM);
1507 assert_return(s->pending, -ENODATA);
1508 assert_return(!event_pid_changed(s->event), -ECHILD);
1510 *revents = s->io.revents;
1514 _public_ int sd_event_source_get_signal(sd_event_source *s) {
1515 assert_return(s, -EINVAL);
1516 assert_return(s->type == SOURCE_SIGNAL, -EDOM);
1517 assert_return(!event_pid_changed(s->event), -ECHILD);
1519 return s->signal.sig;
1522 _public_ int sd_event_source_get_priority(sd_event_source *s, int64_t *priority) {
1523 assert_return(s, -EINVAL);
1524 assert_return(!event_pid_changed(s->event), -ECHILD);
1530 _public_ int sd_event_source_set_priority(sd_event_source *s, int64_t priority) {
1533 assert_return(s, -EINVAL);
1534 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1535 assert_return(!event_pid_changed(s->event), -ECHILD);
1537 if (s->priority == priority)
1540 if (s->type == SOURCE_SIGNAL && s->enabled != SD_EVENT_OFF) {
1541 struct signal_data *old, *d;
1543 /* Move us from the signalfd belonging to the old
1544 * priority to the signalfd of the new priority */
1546 assert_se(old = hashmap_get(s->event->signal_data, &s->priority));
1548 s->priority = priority;
1550 r = event_make_signal_data(s->event, s->signal.sig, &d);
1552 s->priority = old->priority;
1556 event_unmask_signal_data(s->event, old, s->signal.sig);
1558 s->priority = priority;
1561 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1564 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1566 if (s->type == SOURCE_EXIT)
1567 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1572 #if 0 /// UNNEEDED by elogind
1573 _public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
1574 assert_return(s, -EINVAL);
1575 assert_return(m, -EINVAL);
1576 assert_return(!event_pid_changed(s->event), -ECHILD);
1583 _public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
1586 assert_return(s, -EINVAL);
1587 assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
1588 assert_return(!event_pid_changed(s->event), -ECHILD);
1590 /* If we are dead anyway, we are fine with turning off
1591 * sources, but everything else needs to fail. */
1592 if (s->event->state == SD_EVENT_FINISHED)
1593 return m == SD_EVENT_OFF ? 0 : -ESTALE;
1595 if (s->enabled == m)
1598 if (m == SD_EVENT_OFF) {
1603 source_io_unregister(s);
1607 case SOURCE_TIME_REALTIME:
1608 case SOURCE_TIME_BOOTTIME:
1609 case SOURCE_TIME_MONOTONIC:
1610 case SOURCE_TIME_REALTIME_ALARM:
1611 case SOURCE_TIME_BOOTTIME_ALARM: {
1612 struct clock_data *d;
1615 d = event_get_clock_data(s->event, s->type);
1618 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1619 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1620 d->needs_rearm = true;
1627 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1633 assert(s->event->n_enabled_child_sources > 0);
1634 s->event->n_enabled_child_sources--;
1636 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1641 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1650 assert_not_reached("Wut? I shouldn't exist.");
1657 r = source_io_register(s, m, s->io.events);
1664 case SOURCE_TIME_REALTIME:
1665 case SOURCE_TIME_BOOTTIME:
1666 case SOURCE_TIME_MONOTONIC:
1667 case SOURCE_TIME_REALTIME_ALARM:
1668 case SOURCE_TIME_BOOTTIME_ALARM: {
1669 struct clock_data *d;
1672 d = event_get_clock_data(s->event, s->type);
1675 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1676 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1677 d->needs_rearm = true;
1685 r = event_make_signal_data(s->event, s->signal.sig, NULL);
1687 s->enabled = SD_EVENT_OFF;
1688 event_gc_signal_data(s->event, &s->priority, s->signal.sig);
1696 if (s->enabled == SD_EVENT_OFF)
1697 s->event->n_enabled_child_sources++;
1701 r = event_make_signal_data(s->event, SIGCHLD, NULL);
1703 s->enabled = SD_EVENT_OFF;
1704 s->event->n_enabled_child_sources--;
1705 event_gc_signal_data(s->event, &s->priority, SIGCHLD);
1713 prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
1722 assert_not_reached("Wut? I shouldn't exist.");
1727 prioq_reshuffle(s->event->pending, s, &s->pending_index);
1730 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
1735 _public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
1736 assert_return(s, -EINVAL);
1737 assert_return(usec, -EINVAL);
1738 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1739 assert_return(!event_pid_changed(s->event), -ECHILD);
1741 *usec = s->time.next;
1745 _public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
1746 struct clock_data *d;
1748 assert_return(s, -EINVAL);
1749 assert_return(usec != (uint64_t) -1, -EINVAL);
1750 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1751 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1752 assert_return(!event_pid_changed(s->event), -ECHILD);
1754 s->time.next = usec;
1756 source_set_pending(s, false);
1758 d = event_get_clock_data(s->event, s->type);
1761 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
1762 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1763 d->needs_rearm = true;
1768 #if 0 /// UNNEEDED by elogind
1769 _public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
1770 assert_return(s, -EINVAL);
1771 assert_return(usec, -EINVAL);
1772 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1773 assert_return(!event_pid_changed(s->event), -ECHILD);
1775 *usec = s->time.accuracy;
1779 _public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
1780 struct clock_data *d;
1782 assert_return(s, -EINVAL);
1783 assert_return(usec != (uint64_t) -1, -EINVAL);
1784 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1785 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1786 assert_return(!event_pid_changed(s->event), -ECHILD);
1789 usec = DEFAULT_ACCURACY_USEC;
1791 s->time.accuracy = usec;
1793 source_set_pending(s, false);
1795 d = event_get_clock_data(s->event, s->type);
1798 prioq_reshuffle(d->latest, s, &s->time.latest_index);
1799 d->needs_rearm = true;
1804 _public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *clock) {
1805 assert_return(s, -EINVAL);
1806 assert_return(clock, -EINVAL);
1807 assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
1808 assert_return(!event_pid_changed(s->event), -ECHILD);
1810 *clock = event_source_type_to_clock(s->type);
1814 _public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
1815 assert_return(s, -EINVAL);
1816 assert_return(pid, -EINVAL);
1817 assert_return(s->type == SOURCE_CHILD, -EDOM);
1818 assert_return(!event_pid_changed(s->event), -ECHILD);
1820 *pid = s->child.pid;
1825 _public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
1828 assert_return(s, -EINVAL);
1829 assert_return(s->type != SOURCE_EXIT, -EDOM);
1830 assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
1831 assert_return(!event_pid_changed(s->event), -ECHILD);
1833 if (s->prepare == callback)
1836 if (callback && s->prepare) {
1837 s->prepare = callback;
1841 r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
1845 s->prepare = callback;
1848 r = prioq_put(s->event->prepare, s, &s->prepare_index);
1852 prioq_remove(s->event->prepare, s, &s->prepare_index);
1857 #if 0 /// UNNEEDED by elogind
1858 _public_ void* sd_event_source_get_userdata(sd_event_source *s) {
1859 assert_return(s, NULL);
1864 _public_ void *sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
1867 assert_return(s, NULL);
1870 s->userdata = userdata;
1876 static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
1887 initialize_perturb(e);
1890 Find a good time to wake up again between times a and b. We
1891 have two goals here:
1893 a) We want to wake up as seldom as possible, hence prefer
1894 later times over earlier times.
1896 b) But if we have to wake up, then let's make sure to
1897 dispatch as much as possible on the entire system.
1899 We implement this by waking up everywhere at the same time
1900 within any given minute if we can, synchronised via the
1901 perturbation value determined from the boot ID. If we can't,
1902 then we try to find the same spot in every 10s, then 1s and
1903 then 250ms step. Otherwise, we pick the last possible time
1907 c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
1909 if (_unlikely_(c < USEC_PER_MINUTE))
1912 c -= USEC_PER_MINUTE;
1918 c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
1920 if (_unlikely_(c < USEC_PER_SEC*10))
1923 c -= USEC_PER_SEC*10;
1929 c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
1931 if (_unlikely_(c < USEC_PER_SEC))
1940 c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
1942 if (_unlikely_(c < USEC_PER_MSEC*250))
1945 c -= USEC_PER_MSEC*250;
1954 static int event_arm_timer(
1956 struct clock_data *d) {
1958 struct itimerspec its = {};
1959 sd_event_source *a, *b;
1966 if (!d->needs_rearm)
1969 d->needs_rearm = false;
1971 a = prioq_peek(d->earliest);
1972 if (!a || a->enabled == SD_EVENT_OFF) {
1977 if (d->next == USEC_INFINITY)
1981 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
1985 d->next = USEC_INFINITY;
1989 b = prioq_peek(d->latest);
1990 assert_se(b && b->enabled != SD_EVENT_OFF);
1992 t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
1996 assert_se(d->fd >= 0);
1999 /* We don' want to disarm here, just mean some time looooong ago. */
2000 its.it_value.tv_sec = 0;
2001 its.it_value.tv_nsec = 1;
2003 timespec_store(&its.it_value, t);
2005 r = timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL);
2013 static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
2016 assert(s->type == SOURCE_IO);
2018 /* If the event source was already pending, we just OR in the
2019 * new revents, otherwise we reset the value. The ORing is
2020 * necessary to handle EPOLLONESHOT events properly where
2021 * readability might happen independently of writability, and
2022 * we need to keep track of both */
2025 s->io.revents |= revents;
2027 s->io.revents = revents;
2029 return source_set_pending(s, true);
2032 static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
2039 assert_return(events == EPOLLIN, -EIO);
2041 ss = read(fd, &x, sizeof(x));
2043 if (errno == EAGAIN || errno == EINTR)
2049 if (_unlikely_(ss != sizeof(x)))
2053 *next = USEC_INFINITY;
2058 static int process_timer(
2061 struct clock_data *d) {
2070 s = prioq_peek(d->earliest);
2073 s->enabled == SD_EVENT_OFF ||
2077 r = source_set_pending(s, true);
2081 prioq_reshuffle(d->earliest, s, &s->time.earliest_index);
2082 prioq_reshuffle(d->latest, s, &s->time.latest_index);
2083 d->needs_rearm = true;
2089 static int process_child(sd_event *e) {
2096 e->need_process_child = false;
2099 So, this is ugly. We iteratively invoke waitid() with P_PID
2100 + WNOHANG for each PID we wait for, instead of using
2101 P_ALL. This is because we only want to get child
2102 information of very specific child processes, and not all
2103 of them. We might not have processed the SIGCHLD even of a
2104 previous invocation and we don't want to maintain a
2105 unbounded *per-child* event queue, hence we really don't
2106 want anything flushed out of the kernel's queue that we
2107 don't care about. Since this is O(n) this means that if you
2108 have a lot of processes you probably want to handle SIGCHLD
2111 We do not reap the children here (by using WNOWAIT), this
2112 is only done after the event source is dispatched so that
2113 the callback still sees the process as a zombie.
2116 HASHMAP_FOREACH(s, e->child_sources, i) {
2117 assert(s->type == SOURCE_CHILD);
2122 if (s->enabled == SD_EVENT_OFF)
2125 zero(s->child.siginfo);
2126 r = waitid(P_PID, s->child.pid, &s->child.siginfo,
2127 WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | s->child.options);
2131 if (s->child.siginfo.si_pid != 0) {
2133 s->child.siginfo.si_code == CLD_EXITED ||
2134 s->child.siginfo.si_code == CLD_KILLED ||
2135 s->child.siginfo.si_code == CLD_DUMPED;
2137 if (!zombie && (s->child.options & WEXITED)) {
2138 /* If the child isn't dead then let's
2139 * immediately remove the state change
2140 * from the queue, since there's no
2141 * benefit in leaving it queued */
2143 assert(s->child.options & (WSTOPPED|WCONTINUED));
2144 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
2147 r = source_set_pending(s, true);
2156 static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
2157 bool read_one = false;
2161 assert_return(events == EPOLLIN, -EIO);
2163 /* If there's a signal queued on this priority and SIGCHLD is
2164 on this priority too, then make sure to recheck the
2165 children we watch. This is because we only ever dequeue
2166 the first signal per priority, and if we dequeue one, and
2167 SIGCHLD might be enqueued later we wouldn't know, but we
2168 might have higher priority children we care about hence we
2169 need to check that explicitly. */
2171 if (sigismember(&d->sigset, SIGCHLD))
2172 e->need_process_child = true;
2174 /* If there's already an event source pending for this
2175 * priority we don't read another */
2180 struct signalfd_siginfo si;
2182 sd_event_source *s = NULL;
2184 n = read(d->fd, &si, sizeof(si));
2186 if (errno == EAGAIN || errno == EINTR)
2192 if (_unlikely_(n != sizeof(si)))
2195 assert(si.ssi_signo < _NSIG);
2199 if (e->signal_sources)
2200 s = e->signal_sources[si.ssi_signo];
2206 s->signal.siginfo = si;
2209 r = source_set_pending(s, true);
2217 static int source_dispatch(sd_event_source *s) {
2221 assert(s->pending || s->type == SOURCE_EXIT);
2223 if (s->type != SOURCE_DEFER && s->type != SOURCE_EXIT) {
2224 r = source_set_pending(s, false);
2229 if (s->type != SOURCE_POST) {
2233 /* If we execute a non-post source, let's mark all
2234 * post sources as pending */
2236 SET_FOREACH(z, s->event->post_sources, i) {
2237 if (z->enabled == SD_EVENT_OFF)
2240 r = source_set_pending(z, true);
2246 if (s->enabled == SD_EVENT_ONESHOT) {
2247 r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
2252 s->dispatching = true;
2257 r = s->io.callback(s, s->io.fd, s->io.revents, s->userdata);
2260 case SOURCE_TIME_REALTIME:
2261 case SOURCE_TIME_BOOTTIME:
2262 case SOURCE_TIME_MONOTONIC:
2263 case SOURCE_TIME_REALTIME_ALARM:
2264 case SOURCE_TIME_BOOTTIME_ALARM:
2265 r = s->time.callback(s, s->time.next, s->userdata);
2269 r = s->signal.callback(s, &s->signal.siginfo, s->userdata);
2272 case SOURCE_CHILD: {
2275 zombie = s->child.siginfo.si_code == CLD_EXITED ||
2276 s->child.siginfo.si_code == CLD_KILLED ||
2277 s->child.siginfo.si_code == CLD_DUMPED;
2279 r = s->child.callback(s, &s->child.siginfo, s->userdata);
2281 /* Now, reap the PID for good. */
2283 waitid(P_PID, s->child.pid, &s->child.siginfo, WNOHANG|WEXITED);
2289 r = s->defer.callback(s, s->userdata);
2293 r = s->post.callback(s, s->userdata);
2297 r = s->exit.callback(s, s->userdata);
2300 case SOURCE_WATCHDOG:
2301 case _SOURCE_EVENT_SOURCE_TYPE_MAX:
2302 case _SOURCE_EVENT_SOURCE_TYPE_INVALID:
2303 assert_not_reached("Wut? I shouldn't exist.");
2306 s->dispatching = false;
2310 log_debug_errno(r, "Event source '%s' returned error, disabling: %m", s->description);
2312 log_debug_errno(r, "Event source %p returned error, disabling: %m", s);
2318 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2323 static int event_prepare(sd_event *e) {
2331 s = prioq_peek(e->prepare);
2332 if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
2335 s->prepare_iteration = e->iteration;
2336 r = prioq_reshuffle(e->prepare, s, &s->prepare_index);
2342 s->dispatching = true;
2343 r = s->prepare(s, s->userdata);
2344 s->dispatching = false;
2348 log_debug_errno(r, "Prepare callback of event source '%s' returned error, disabling: %m", s->description);
2350 log_debug_errno(r, "Prepare callback of event source %p returned error, disabling: %m", s);
2356 sd_event_source_set_enabled(s, SD_EVENT_OFF);
2362 static int dispatch_exit(sd_event *e) {
2368 p = prioq_peek(e->exit);
2369 if (!p || p->enabled == SD_EVENT_OFF) {
2370 e->state = SD_EVENT_FINISHED;
2376 e->state = SD_EVENT_EXITING;
2378 r = source_dispatch(p);
2380 e->state = SD_EVENT_INITIAL;
2386 static sd_event_source* event_next_pending(sd_event *e) {
2391 p = prioq_peek(e->pending);
2395 if (p->enabled == SD_EVENT_OFF)
2401 static int arm_watchdog(sd_event *e) {
2402 struct itimerspec its = {};
2407 assert(e->watchdog_fd >= 0);
2409 t = sleep_between(e,
2410 e->watchdog_last + (e->watchdog_period / 2),
2411 e->watchdog_last + (e->watchdog_period * 3 / 4));
2413 timespec_store(&its.it_value, t);
2415 /* Make sure we never set the watchdog to 0, which tells the
2416 * kernel to disable it. */
2417 if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0)
2418 its.it_value.tv_nsec = 1;
2420 r = timerfd_settime(e->watchdog_fd, TFD_TIMER_ABSTIME, &its, NULL);
2427 static int process_watchdog(sd_event *e) {
2433 /* Don't notify watchdog too often */
2434 if (e->watchdog_last + e->watchdog_period / 4 > e->timestamp.monotonic)
2437 sd_notify(false, "WATCHDOG=1");
2438 e->watchdog_last = e->timestamp.monotonic;
2440 return arm_watchdog(e);
2443 _public_ int sd_event_prepare(sd_event *e) {
2446 assert_return(e, -EINVAL);
2447 assert_return(!event_pid_changed(e), -ECHILD);
2448 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2449 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2451 if (e->exit_requested)
2456 r = event_prepare(e);
2460 r = event_arm_timer(e, &e->realtime);
2464 r = event_arm_timer(e, &e->boottime);
2468 r = event_arm_timer(e, &e->monotonic);
2472 r = event_arm_timer(e, &e->realtime_alarm);
2476 r = event_arm_timer(e, &e->boottime_alarm);
2480 if (event_next_pending(e) || e->need_process_child)
2483 e->state = SD_EVENT_ARMED;
2488 e->state = SD_EVENT_ARMED;
2489 r = sd_event_wait(e, 0);
2491 e->state = SD_EVENT_ARMED;
2496 _public_ int sd_event_wait(sd_event *e, uint64_t timeout) {
2497 struct epoll_event *ev_queue;
2498 unsigned ev_queue_max;
2501 assert_return(e, -EINVAL);
2502 assert_return(!event_pid_changed(e), -ECHILD);
2503 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2504 assert_return(e->state == SD_EVENT_ARMED, -EBUSY);
2506 if (e->exit_requested) {
2507 e->state = SD_EVENT_PENDING;
2511 ev_queue_max = MAX(e->n_sources, 1u);
2512 ev_queue = newa(struct epoll_event, ev_queue_max);
2514 m = epoll_wait(e->epoll_fd, ev_queue, ev_queue_max,
2515 timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
2517 if (errno == EINTR) {
2518 e->state = SD_EVENT_PENDING;
2526 dual_timestamp_get(&e->timestamp);
2527 e->timestamp_boottime = now(CLOCK_BOOTTIME);
2529 for (i = 0; i < m; i++) {
2531 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_WATCHDOG))
2532 r = flush_timer(e, e->watchdog_fd, ev_queue[i].events, NULL);
2534 WakeupType *t = ev_queue[i].data.ptr;
2538 case WAKEUP_EVENT_SOURCE:
2539 r = process_io(e, ev_queue[i].data.ptr, ev_queue[i].events);
2542 case WAKEUP_CLOCK_DATA: {
2543 struct clock_data *d = ev_queue[i].data.ptr;
2544 r = flush_timer(e, d->fd, ev_queue[i].events, &d->next);
2548 case WAKEUP_SIGNAL_DATA:
2549 r = process_signal(e, ev_queue[i].data.ptr, ev_queue[i].events);
2553 assert_not_reached("Invalid wake-up pointer");
2560 r = process_watchdog(e);
2564 r = process_timer(e, e->timestamp.realtime, &e->realtime);
2568 r = process_timer(e, e->timestamp_boottime, &e->boottime);
2572 r = process_timer(e, e->timestamp.monotonic, &e->monotonic);
2576 r = process_timer(e, e->timestamp.realtime, &e->realtime_alarm);
2580 r = process_timer(e, e->timestamp_boottime, &e->boottime_alarm);
2584 if (e->need_process_child) {
2585 r = process_child(e);
2590 if (event_next_pending(e)) {
2591 e->state = SD_EVENT_PENDING;
2599 e->state = SD_EVENT_INITIAL;
2604 _public_ int sd_event_dispatch(sd_event *e) {
2608 assert_return(e, -EINVAL);
2609 assert_return(!event_pid_changed(e), -ECHILD);
2610 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2611 assert_return(e->state == SD_EVENT_PENDING, -EBUSY);
2613 if (e->exit_requested)
2614 return dispatch_exit(e);
2616 p = event_next_pending(e);
2620 e->state = SD_EVENT_RUNNING;
2621 r = source_dispatch(p);
2622 e->state = SD_EVENT_INITIAL;
2629 e->state = SD_EVENT_INITIAL;
2634 _public_ int sd_event_run(sd_event *e, uint64_t timeout) {
2637 assert_return(e, -EINVAL);
2638 assert_return(!event_pid_changed(e), -ECHILD);
2639 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2640 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2642 r = sd_event_prepare(e);
2644 /* There was nothing? Then wait... */
2645 r = sd_event_wait(e, timeout);
2648 /* There's something now, then let's dispatch it */
2649 r = sd_event_dispatch(e);
2659 #if 0 /// UNNEEDED by elogind
2660 _public_ int sd_event_loop(sd_event *e) {
2663 assert_return(e, -EINVAL);
2664 assert_return(!event_pid_changed(e), -ECHILD);
2665 assert_return(e->state == SD_EVENT_INITIAL, -EBUSY);
2669 while (e->state != SD_EVENT_FINISHED) {
2670 r = sd_event_run(e, (uint64_t) -1);
2682 _public_ int sd_event_get_fd(sd_event *e) {
2684 assert_return(e, -EINVAL);
2685 assert_return(!event_pid_changed(e), -ECHILD);
2691 _public_ int sd_event_get_state(sd_event *e) {
2692 assert_return(e, -EINVAL);
2693 assert_return(!event_pid_changed(e), -ECHILD);
2698 #if 0 /// UNNEEDED by elogind
2699 _public_ int sd_event_get_exit_code(sd_event *e, int *code) {
2700 assert_return(e, -EINVAL);
2701 assert_return(code, -EINVAL);
2702 assert_return(!event_pid_changed(e), -ECHILD);
2704 if (!e->exit_requested)
2707 *code = e->exit_code;
2712 _public_ int sd_event_exit(sd_event *e, int code) {
2713 assert_return(e, -EINVAL);
2714 assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
2715 assert_return(!event_pid_changed(e), -ECHILD);
2717 e->exit_requested = true;
2718 e->exit_code = code;
2723 #if 0 /// UNNEEDED by elogind
2724 _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
2725 assert_return(e, -EINVAL);
2726 assert_return(usec, -EINVAL);
2727 assert_return(!event_pid_changed(e), -ECHILD);
2729 if (!dual_timestamp_is_set(&e->timestamp)) {
2730 /* Implicitly fall back to now() if we never ran
2731 * before and thus have no cached time. */
2738 case CLOCK_REALTIME:
2739 case CLOCK_REALTIME_ALARM:
2740 *usec = e->timestamp.realtime;
2743 case CLOCK_MONOTONIC:
2744 *usec = e->timestamp.monotonic;
2747 case CLOCK_BOOTTIME:
2748 case CLOCK_BOOTTIME_ALARM:
2749 *usec = e->timestamp_boottime;
2757 _public_ int sd_event_default(sd_event **ret) {
2759 static thread_local sd_event *default_event = NULL;
2764 return !!default_event;
2766 if (default_event) {
2767 *ret = sd_event_ref(default_event);
2771 r = sd_event_new(&e);
2775 e->default_event_ptr = &default_event;
2783 #if 0 /// UNNEEDED by elogind
2784 _public_ int sd_event_get_tid(sd_event *e, pid_t *tid) {
2785 assert_return(e, -EINVAL);
2786 assert_return(tid, -EINVAL);
2787 assert_return(!event_pid_changed(e), -ECHILD);
2798 _public_ int sd_event_set_watchdog(sd_event *e, int b) {
2801 assert_return(e, -EINVAL);
2802 assert_return(!event_pid_changed(e), -ECHILD);
2804 if (e->watchdog == !!b)
2808 struct epoll_event ev = {};
2810 r = sd_watchdog_enabled(false, &e->watchdog_period);
2814 /* Issue first ping immediately */
2815 sd_notify(false, "WATCHDOG=1");
2816 e->watchdog_last = now(CLOCK_MONOTONIC);
2818 e->watchdog_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC);
2819 if (e->watchdog_fd < 0)
2822 r = arm_watchdog(e);
2826 ev.events = EPOLLIN;
2827 ev.data.ptr = INT_TO_PTR(SOURCE_WATCHDOG);
2829 r = epoll_ctl(e->epoll_fd, EPOLL_CTL_ADD, e->watchdog_fd, &ev);
2836 if (e->watchdog_fd >= 0) {
2837 epoll_ctl(e->epoll_fd, EPOLL_CTL_DEL, e->watchdog_fd, NULL);
2838 e->watchdog_fd = safe_close(e->watchdog_fd);
2846 e->watchdog_fd = safe_close(e->watchdog_fd);
2850 #if 0 /// UNNEEDED by elogind
2851 _public_ int sd_event_get_watchdog(sd_event *e) {
2852 assert_return(e, -EINVAL);
2853 assert_return(!event_pid_changed(e), -ECHILD);