1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <sys/timerfd.h>
11 #include "alloc-util.h"
13 #include "event-source.h"
19 #include "memory-util.h"
20 #include "missing_syscall.h"
22 #include "process-util.h"
24 #include "signal-util.h"
25 #include "string-table.h"
26 #include "string-util.h"
28 #include "time-util.h"
30 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
32 static bool EVENT_SOURCE_WATCH_PIDFD(sd_event_source
*s
) {
33 /* Returns true if this is a PID event source and can be implemented by watching EPOLLIN */
35 s
->type
== SOURCE_CHILD
&&
36 s
->child
.pidfd
>= 0 &&
37 s
->child
.options
== WEXITED
;
40 static const char* const event_source_type_table
[_SOURCE_EVENT_SOURCE_TYPE_MAX
] = {
42 [SOURCE_TIME_REALTIME
] = "realtime",
43 [SOURCE_TIME_BOOTTIME
] = "bootime",
44 [SOURCE_TIME_MONOTONIC
] = "monotonic",
45 [SOURCE_TIME_REALTIME_ALARM
] = "realtime-alarm",
46 [SOURCE_TIME_BOOTTIME_ALARM
] = "boottime-alarm",
47 [SOURCE_SIGNAL
] = "signal",
48 [SOURCE_CHILD
] = "child",
49 [SOURCE_DEFER
] = "defer",
50 [SOURCE_POST
] = "post",
51 [SOURCE_EXIT
] = "exit",
52 [SOURCE_WATCHDOG
] = "watchdog",
53 [SOURCE_INOTIFY
] = "inotify",
56 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type
, int);
58 #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)
69 /* timerfd_create() only supports these five clocks so far. We
70 * can add support for more clocks when the kernel learns to
71 * deal with them, too. */
72 struct clock_data realtime
;
73 struct clock_data boottime
;
74 struct clock_data monotonic
;
75 struct clock_data realtime_alarm
;
76 struct clock_data boottime_alarm
;
80 sd_event_source
**signal_sources
; /* indexed by signal number */
81 Hashmap
*signal_data
; /* indexed by priority */
83 Hashmap
*child_sources
;
84 unsigned n_enabled_child_sources
;
90 Hashmap
*inotify_data
; /* indexed by priority */
92 /* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */
93 LIST_HEAD(struct inode_data
, inode_data_to_close
);
95 /* A list of inotify objects that already have events buffered which aren't processed yet */
96 LIST_HEAD(struct inotify_data
, inotify_data_buffered
);
101 triple_timestamp timestamp
;
104 bool exit_requested
:1;
105 bool need_process_child
:1;
107 bool profile_delays
:1;
112 sd_event
**default_event_ptr
;
114 usec_t watchdog_last
, watchdog_period
;
118 struct epoll_event
*event_queue
;
119 size_t event_queue_allocated
;
121 LIST_HEAD(sd_event_source
, sources
);
123 usec_t last_run
, last_log
;
124 unsigned delays
[sizeof(usec_t
) * 8];
127 static thread_local sd_event
*default_event
= NULL
;
129 static void source_disconnect(sd_event_source
*s
);
130 static void event_gc_inode_data(sd_event
*e
, struct inode_data
*d
);
132 static sd_event
*event_resolve(sd_event
*e
) {
133 return e
== SD_EVENT_DEFAULT
? default_event
: e
;
136 static int pending_prioq_compare(const void *a
, const void *b
) {
137 const sd_event_source
*x
= a
, *y
= b
;
143 /* Enabled ones first */
144 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
146 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
149 /* Lower priority values first */
150 r
= CMP(x
->priority
, y
->priority
);
154 /* Older entries first */
155 return CMP(x
->pending_iteration
, y
->pending_iteration
);
158 static int prepare_prioq_compare(const void *a
, const void *b
) {
159 const sd_event_source
*x
= a
, *y
= b
;
165 /* Enabled ones first */
166 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
168 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
171 /* Move most recently prepared ones last, so that we can stop
172 * preparing as soon as we hit one that has already been
173 * prepared in the current iteration */
174 r
= CMP(x
->prepare_iteration
, y
->prepare_iteration
);
178 /* Lower priority values first */
179 return CMP(x
->priority
, y
->priority
);
182 static int earliest_time_prioq_compare(const void *a
, const void *b
) {
183 const sd_event_source
*x
= a
, *y
= b
;
185 assert(EVENT_SOURCE_IS_TIME(x
->type
));
186 assert(x
->type
== y
->type
);
188 /* Enabled ones first */
189 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
191 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
194 /* Move the pending ones to the end */
195 if (!x
->pending
&& y
->pending
)
197 if (x
->pending
&& !y
->pending
)
201 return CMP(x
->time
.next
, y
->time
.next
);
204 static usec_t
time_event_source_latest(const sd_event_source
*s
) {
205 return usec_add(s
->time
.next
, s
->time
.accuracy
);
208 static int latest_time_prioq_compare(const void *a
, const void *b
) {
209 const sd_event_source
*x
= a
, *y
= b
;
211 assert(EVENT_SOURCE_IS_TIME(x
->type
));
212 assert(x
->type
== y
->type
);
214 /* Enabled ones first */
215 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
217 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
220 /* Move the pending ones to the end */
221 if (!x
->pending
&& y
->pending
)
223 if (x
->pending
&& !y
->pending
)
227 return CMP(time_event_source_latest(x
), time_event_source_latest(y
));
230 static int exit_prioq_compare(const void *a
, const void *b
) {
231 const sd_event_source
*x
= a
, *y
= b
;
233 assert(x
->type
== SOURCE_EXIT
);
234 assert(y
->type
== SOURCE_EXIT
);
236 /* Enabled ones first */
237 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
239 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
242 /* Lower priority values first */
243 return CMP(x
->priority
, y
->priority
);
246 static void free_clock_data(struct clock_data
*d
) {
248 assert(d
->wakeup
== WAKEUP_CLOCK_DATA
);
251 prioq_free(d
->earliest
);
252 prioq_free(d
->latest
);
255 static sd_event
*event_free(sd_event
*e
) {
260 while ((s
= e
->sources
)) {
262 source_disconnect(s
);
263 sd_event_source_unref(s
);
266 assert(e
->n_sources
== 0);
268 if (e
->default_event_ptr
)
269 *(e
->default_event_ptr
) = NULL
;
271 safe_close(e
->epoll_fd
);
272 safe_close(e
->watchdog_fd
);
274 free_clock_data(&e
->realtime
);
275 free_clock_data(&e
->boottime
);
276 free_clock_data(&e
->monotonic
);
277 free_clock_data(&e
->realtime_alarm
);
278 free_clock_data(&e
->boottime_alarm
);
280 prioq_free(e
->pending
);
281 prioq_free(e
->prepare
);
284 free(e
->signal_sources
);
285 hashmap_free(e
->signal_data
);
287 hashmap_free(e
->inotify_data
);
289 hashmap_free(e
->child_sources
);
290 set_free(e
->post_sources
);
292 free(e
->event_queue
);
297 _public_
int sd_event_new(sd_event
** ret
) {
301 assert_return(ret
, -EINVAL
);
303 e
= new(sd_event
, 1);
311 .realtime
.wakeup
= WAKEUP_CLOCK_DATA
,
313 .realtime
.next
= USEC_INFINITY
,
314 .boottime
.wakeup
= WAKEUP_CLOCK_DATA
,
316 .boottime
.next
= USEC_INFINITY
,
317 .monotonic
.wakeup
= WAKEUP_CLOCK_DATA
,
319 .monotonic
.next
= USEC_INFINITY
,
320 .realtime_alarm
.wakeup
= WAKEUP_CLOCK_DATA
,
321 .realtime_alarm
.fd
= -1,
322 .realtime_alarm
.next
= USEC_INFINITY
,
323 .boottime_alarm
.wakeup
= WAKEUP_CLOCK_DATA
,
324 .boottime_alarm
.fd
= -1,
325 .boottime_alarm
.next
= USEC_INFINITY
,
326 .perturb
= USEC_INFINITY
,
327 .original_pid
= getpid_cached(),
330 r
= prioq_ensure_allocated(&e
->pending
, pending_prioq_compare
);
334 e
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
335 if (e
->epoll_fd
< 0) {
340 e
->epoll_fd
= fd_move_above_stdio(e
->epoll_fd
);
342 if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
343 log_debug("Event loop profiling enabled. Logarithmic histogram of event loop iterations in the range 2^0 ... 2^63 us will be logged every 5s.");
344 e
->profile_delays
= true;
355 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event
, sd_event
, event_free
);
357 _public_ sd_event_source
* sd_event_source_disable_unref(sd_event_source
*s
) {
359 (void) sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
360 return sd_event_source_unref(s
);
363 static bool event_pid_changed(sd_event
*e
) {
366 /* We don't support people creating an event loop and keeping
367 * it around over a fork(). Let's complain. */
369 return e
->original_pid
!= getpid_cached();
372 static void source_io_unregister(sd_event_source
*s
) {
374 assert(s
->type
== SOURCE_IO
);
376 if (event_pid_changed(s
->event
))
379 if (!s
->io
.registered
)
382 if (epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_DEL
, s
->io
.fd
, NULL
) < 0)
383 log_debug_errno(errno
, "Failed to remove source %s (type %s) from epoll: %m",
384 strna(s
->description
), event_source_type_to_string(s
->type
));
386 s
->io
.registered
= false;
389 static int source_io_register(
394 struct epoll_event ev
;
398 assert(s
->type
== SOURCE_IO
);
399 assert(enabled
!= SD_EVENT_OFF
);
401 ev
= (struct epoll_event
) {
402 .events
= events
| (enabled
== SD_EVENT_ONESHOT
? EPOLLONESHOT
: 0),
406 if (s
->io
.registered
)
407 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_MOD
, s
->io
.fd
, &ev
);
409 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_ADD
, s
->io
.fd
, &ev
);
413 s
->io
.registered
= true;
418 static void source_child_pidfd_unregister(sd_event_source
*s
) {
420 assert(s
->type
== SOURCE_CHILD
);
422 if (event_pid_changed(s
->event
))
425 if (!s
->child
.registered
)
428 if (EVENT_SOURCE_WATCH_PIDFD(s
))
429 if (epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_DEL
, s
->child
.pidfd
, NULL
) < 0)
430 log_debug_errno(errno
, "Failed to remove source %s (type %s) from epoll: %m",
431 strna(s
->description
), event_source_type_to_string(s
->type
));
433 s
->child
.registered
= false;
436 static int source_child_pidfd_register(sd_event_source
*s
, int enabled
) {
440 assert(s
->type
== SOURCE_CHILD
);
441 assert(enabled
!= SD_EVENT_OFF
);
443 if (EVENT_SOURCE_WATCH_PIDFD(s
)) {
444 struct epoll_event ev
;
446 ev
= (struct epoll_event
) {
447 .events
= EPOLLIN
| (enabled
== SD_EVENT_ONESHOT
? EPOLLONESHOT
: 0),
451 if (s
->child
.registered
)
452 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_MOD
, s
->child
.pidfd
, &ev
);
454 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_ADD
, s
->child
.pidfd
, &ev
);
459 s
->child
.registered
= true;
463 static clockid_t
event_source_type_to_clock(EventSourceType t
) {
467 case SOURCE_TIME_REALTIME
:
468 return CLOCK_REALTIME
;
470 case SOURCE_TIME_BOOTTIME
:
471 return CLOCK_BOOTTIME
;
473 case SOURCE_TIME_MONOTONIC
:
474 return CLOCK_MONOTONIC
;
476 case SOURCE_TIME_REALTIME_ALARM
:
477 return CLOCK_REALTIME_ALARM
;
479 case SOURCE_TIME_BOOTTIME_ALARM
:
480 return CLOCK_BOOTTIME_ALARM
;
483 return (clockid_t
) -1;
487 static EventSourceType
clock_to_event_source_type(clockid_t clock
) {
492 return SOURCE_TIME_REALTIME
;
495 return SOURCE_TIME_BOOTTIME
;
497 case CLOCK_MONOTONIC
:
498 return SOURCE_TIME_MONOTONIC
;
500 case CLOCK_REALTIME_ALARM
:
501 return SOURCE_TIME_REALTIME_ALARM
;
503 case CLOCK_BOOTTIME_ALARM
:
504 return SOURCE_TIME_BOOTTIME_ALARM
;
507 return _SOURCE_EVENT_SOURCE_TYPE_INVALID
;
511 static struct clock_data
* event_get_clock_data(sd_event
*e
, EventSourceType t
) {
516 case SOURCE_TIME_REALTIME
:
519 case SOURCE_TIME_BOOTTIME
:
522 case SOURCE_TIME_MONOTONIC
:
523 return &e
->monotonic
;
525 case SOURCE_TIME_REALTIME_ALARM
:
526 return &e
->realtime_alarm
;
528 case SOURCE_TIME_BOOTTIME_ALARM
:
529 return &e
->boottime_alarm
;
536 static void event_free_signal_data(sd_event
*e
, struct signal_data
*d
) {
542 hashmap_remove(e
->signal_data
, &d
->priority
);
547 static int event_make_signal_data(
550 struct signal_data
**ret
) {
552 struct epoll_event ev
;
553 struct signal_data
*d
;
561 if (event_pid_changed(e
))
564 if (e
->signal_sources
&& e
->signal_sources
[sig
])
565 priority
= e
->signal_sources
[sig
]->priority
;
567 priority
= SD_EVENT_PRIORITY_NORMAL
;
569 d
= hashmap_get(e
->signal_data
, &priority
);
571 if (sigismember(&d
->sigset
, sig
) > 0) {
577 r
= hashmap_ensure_allocated(&e
->signal_data
, &uint64_hash_ops
);
581 d
= new(struct signal_data
, 1);
585 *d
= (struct signal_data
) {
586 .wakeup
= WAKEUP_SIGNAL_DATA
,
588 .priority
= priority
,
591 r
= hashmap_put(e
->signal_data
, &d
->priority
, d
);
601 assert_se(sigaddset(&ss_copy
, sig
) >= 0);
603 r
= signalfd(d
->fd
, &ss_copy
, SFD_NONBLOCK
|SFD_CLOEXEC
);
617 d
->fd
= fd_move_above_stdio(r
);
619 ev
= (struct epoll_event
) {
624 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, d
->fd
, &ev
);
637 event_free_signal_data(e
, d
);
642 static void event_unmask_signal_data(sd_event
*e
, struct signal_data
*d
, int sig
) {
646 /* Turns off the specified signal in the signal data
647 * object. If the signal mask of the object becomes empty that
650 if (sigismember(&d
->sigset
, sig
) == 0)
653 assert_se(sigdelset(&d
->sigset
, sig
) >= 0);
655 if (sigisemptyset(&d
->sigset
)) {
656 /* If all the mask is all-zero we can get rid of the structure */
657 event_free_signal_data(e
, d
);
663 if (signalfd(d
->fd
, &d
->sigset
, SFD_NONBLOCK
|SFD_CLOEXEC
) < 0)
664 log_debug_errno(errno
, "Failed to unset signal bit, ignoring: %m");
667 static void event_gc_signal_data(sd_event
*e
, const int64_t *priority
, int sig
) {
668 struct signal_data
*d
;
669 static const int64_t zero_priority
= 0;
673 /* Rechecks if the specified signal is still something we are interested in. If not, we'll unmask it,
674 * and possibly drop the signalfd for it. */
676 if (sig
== SIGCHLD
&&
677 e
->n_enabled_child_sources
> 0)
680 if (e
->signal_sources
&&
681 e
->signal_sources
[sig
] &&
682 e
->signal_sources
[sig
]->enabled
!= SD_EVENT_OFF
)
686 * The specified signal might be enabled in three different queues:
688 * 1) the one that belongs to the priority passed (if it is non-NULL)
689 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
690 * 3) the 0 priority (to cover the SIGCHLD case)
692 * Hence, let's remove it from all three here.
696 d
= hashmap_get(e
->signal_data
, priority
);
698 event_unmask_signal_data(e
, d
, sig
);
701 if (e
->signal_sources
&& e
->signal_sources
[sig
]) {
702 d
= hashmap_get(e
->signal_data
, &e
->signal_sources
[sig
]->priority
);
704 event_unmask_signal_data(e
, d
, sig
);
707 d
= hashmap_get(e
->signal_data
, &zero_priority
);
709 event_unmask_signal_data(e
, d
, sig
);
712 static void source_disconnect(sd_event_source
*s
) {
720 assert(s
->event
->n_sources
> 0);
726 source_io_unregister(s
);
730 case SOURCE_TIME_REALTIME
:
731 case SOURCE_TIME_BOOTTIME
:
732 case SOURCE_TIME_MONOTONIC
:
733 case SOURCE_TIME_REALTIME_ALARM
:
734 case SOURCE_TIME_BOOTTIME_ALARM
: {
735 struct clock_data
*d
;
737 d
= event_get_clock_data(s
->event
, s
->type
);
740 prioq_remove(d
->earliest
, s
, &s
->time
.earliest_index
);
741 prioq_remove(d
->latest
, s
, &s
->time
.latest_index
);
742 d
->needs_rearm
= true;
747 if (s
->signal
.sig
> 0) {
749 if (s
->event
->signal_sources
)
750 s
->event
->signal_sources
[s
->signal
.sig
] = NULL
;
752 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
758 if (s
->child
.pid
> 0) {
759 if (s
->enabled
!= SD_EVENT_OFF
) {
760 assert(s
->event
->n_enabled_child_sources
> 0);
761 s
->event
->n_enabled_child_sources
--;
764 (void) hashmap_remove(s
->event
->child_sources
, PID_TO_PTR(s
->child
.pid
));
767 if (EVENT_SOURCE_WATCH_PIDFD(s
))
768 source_child_pidfd_unregister(s
);
770 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
779 set_remove(s
->event
->post_sources
, s
);
783 prioq_remove(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
786 case SOURCE_INOTIFY
: {
787 struct inode_data
*inode_data
;
789 inode_data
= s
->inotify
.inode_data
;
791 struct inotify_data
*inotify_data
;
792 assert_se(inotify_data
= inode_data
->inotify_data
);
794 /* Detach this event source from the inode object */
795 LIST_REMOVE(inotify
.by_inode_data
, inode_data
->event_sources
, s
);
796 s
->inotify
.inode_data
= NULL
;
799 assert(inotify_data
->n_pending
> 0);
800 inotify_data
->n_pending
--;
803 /* Note that we don't reduce the inotify mask for the watch descriptor here if the inode is
804 * continued to being watched. That's because inotify doesn't really have an API for that: we
805 * can only change watch masks with access to the original inode either by fd or by path. But
806 * paths aren't stable, and keeping an O_PATH fd open all the time would mean wasting an fd
807 * continuously and keeping the mount busy which we can't really do. We could reconstruct the
808 * original inode from /proc/self/fdinfo/$INOTIFY_FD (as all watch descriptors are listed
809 * there), but given the need for open_by_handle_at() which is privileged and not universally
810 * available this would be quite an incomplete solution. Hence we go the other way, leave the
811 * mask set, even if it is not minimized now, and ignore all events we aren't interested in
812 * anymore after reception. Yes, this sucks, but … Linux … */
814 /* Maybe release the inode data (and its inotify) */
815 event_gc_inode_data(s
->event
, inode_data
);
822 assert_not_reached("Wut? I shouldn't exist.");
826 prioq_remove(s
->event
->pending
, s
, &s
->pending_index
);
829 prioq_remove(s
->event
->prepare
, s
, &s
->prepare_index
);
831 event
= TAKE_PTR(s
->event
);
832 LIST_REMOVE(sources
, event
->sources
, s
);
835 /* Note that we don't invalidate the type here, since we still need it in order to close the fd or
836 * pidfd associated with this event source, which we'll do only on source_free(). */
839 sd_event_unref(event
);
842 static void source_free(sd_event_source
*s
) {
845 source_disconnect(s
);
847 if (s
->type
== SOURCE_IO
&& s
->io
.owned
)
848 s
->io
.fd
= safe_close(s
->io
.fd
);
850 if (s
->type
== SOURCE_CHILD
) {
851 /* Eventually the kernel will do this automatically for us, but for now let's emulate this (unreliably) in userspace. */
853 if (s
->child
.process_owned
) {
855 if (!s
->child
.exited
) {
858 if (s
->child
.pidfd
>= 0) {
859 if (pidfd_send_signal(s
->child
.pidfd
, SIGKILL
, NULL
, 0) < 0) {
860 if (errno
== ESRCH
) /* Already dead */
862 else if (!ERRNO_IS_NOT_SUPPORTED(errno
))
863 log_debug_errno(errno
, "Failed to kill process " PID_FMT
" via pidfd_send_signal(), re-trying via kill(): %m",
870 if (kill(s
->child
.pid
, SIGKILL
) < 0)
871 if (errno
!= ESRCH
) /* Already dead */
872 log_debug_errno(errno
, "Failed to kill process " PID_FMT
" via kill(), ignoring: %m",
876 if (!s
->child
.waited
) {
879 /* Reap the child if we can */
880 (void) waitid(P_PID
, s
->child
.pid
, &si
, WEXITED
);
884 if (s
->child
.pidfd_owned
)
885 s
->child
.pidfd
= safe_close(s
->child
.pidfd
);
888 if (s
->destroy_callback
)
889 s
->destroy_callback(s
->userdata
);
891 free(s
->description
);
894 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source
*, source_free
);
896 static int source_set_pending(sd_event_source
*s
, bool b
) {
900 assert(s
->type
!= SOURCE_EXIT
);
908 s
->pending_iteration
= s
->event
->iteration
;
910 r
= prioq_put(s
->event
->pending
, s
, &s
->pending_index
);
916 assert_se(prioq_remove(s
->event
->pending
, s
, &s
->pending_index
));
918 if (EVENT_SOURCE_IS_TIME(s
->type
)) {
919 struct clock_data
*d
;
921 d
= event_get_clock_data(s
->event
, s
->type
);
924 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
925 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
926 d
->needs_rearm
= true;
929 if (s
->type
== SOURCE_SIGNAL
&& !b
) {
930 struct signal_data
*d
;
932 d
= hashmap_get(s
->event
->signal_data
, &s
->priority
);
933 if (d
&& d
->current
== s
)
937 if (s
->type
== SOURCE_INOTIFY
) {
939 assert(s
->inotify
.inode_data
);
940 assert(s
->inotify
.inode_data
->inotify_data
);
943 s
->inotify
.inode_data
->inotify_data
->n_pending
++;
945 assert(s
->inotify
.inode_data
->inotify_data
->n_pending
> 0);
946 s
->inotify
.inode_data
->inotify_data
->n_pending
--;
953 static sd_event_source
*source_new(sd_event
*e
, bool floating
, EventSourceType type
) {
958 s
= new(sd_event_source
, 1);
962 *s
= (struct sd_event_source
) {
965 .floating
= floating
,
967 .pending_index
= PRIOQ_IDX_NULL
,
968 .prepare_index
= PRIOQ_IDX_NULL
,
974 LIST_PREPEND(sources
, e
->sources
, s
);
980 _public_
int sd_event_add_io(
982 sd_event_source
**ret
,
985 sd_event_io_handler_t callback
,
988 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
991 assert_return(e
, -EINVAL
);
992 assert_return(e
= event_resolve(e
), -ENOPKG
);
993 assert_return(fd
>= 0, -EBADF
);
994 assert_return(!(events
& ~(EPOLLIN
|EPOLLOUT
|EPOLLRDHUP
|EPOLLPRI
|EPOLLERR
|EPOLLHUP
|EPOLLET
)), -EINVAL
);
995 assert_return(callback
, -EINVAL
);
996 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
997 assert_return(!event_pid_changed(e
), -ECHILD
);
999 s
= source_new(e
, !ret
, SOURCE_IO
);
1003 s
->wakeup
= WAKEUP_EVENT_SOURCE
;
1005 s
->io
.events
= events
;
1006 s
->io
.callback
= callback
;
1007 s
->userdata
= userdata
;
1008 s
->enabled
= SD_EVENT_ON
;
1010 r
= source_io_register(s
, s
->enabled
, events
);
1021 static void initialize_perturb(sd_event
*e
) {
1022 sd_id128_t bootid
= {};
1024 /* When we sleep for longer, we try to realign the wakeup to
1025 the same time within each minute/second/250ms, so that
1026 events all across the system can be coalesced into a single
1027 CPU wakeup. However, let's take some system-specific
1028 randomness for this value, so that in a network of systems
1029 with synced clocks timer events are distributed a
1030 bit. Here, we calculate a perturbation usec offset from the
1033 if (_likely_(e
->perturb
!= USEC_INFINITY
))
1036 if (sd_id128_get_boot(&bootid
) >= 0)
1037 e
->perturb
= (bootid
.qwords
[0] ^ bootid
.qwords
[1]) % USEC_PER_MINUTE
;
1040 static int event_setup_timer_fd(
1042 struct clock_data
*d
,
1045 struct epoll_event ev
;
1051 if (_likely_(d
->fd
>= 0))
1054 fd
= timerfd_create(clock
, TFD_NONBLOCK
|TFD_CLOEXEC
);
1058 fd
= fd_move_above_stdio(fd
);
1060 ev
= (struct epoll_event
) {
1065 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
);
1075 static int time_exit_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
1078 return sd_event_exit(sd_event_source_get_event(s
), PTR_TO_INT(userdata
));
1081 _public_
int sd_event_add_time(
1083 sd_event_source
**ret
,
1087 sd_event_time_handler_t callback
,
1090 EventSourceType type
;
1091 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1092 struct clock_data
*d
;
1095 assert_return(e
, -EINVAL
);
1096 assert_return(e
= event_resolve(e
), -ENOPKG
);
1097 assert_return(accuracy
!= (uint64_t) -1, -EINVAL
);
1098 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1099 assert_return(!event_pid_changed(e
), -ECHILD
);
1101 if (!clock_supported(clock
)) /* Checks whether the kernel supports the clock */
1104 type
= clock_to_event_source_type(clock
); /* checks whether sd-event supports this clock */
1109 callback
= time_exit_callback
;
1111 d
= event_get_clock_data(e
, type
);
1114 r
= prioq_ensure_allocated(&d
->earliest
, earliest_time_prioq_compare
);
1118 r
= prioq_ensure_allocated(&d
->latest
, latest_time_prioq_compare
);
1123 r
= event_setup_timer_fd(e
, d
, clock
);
1128 s
= source_new(e
, !ret
, type
);
1132 s
->time
.next
= usec
;
1133 s
->time
.accuracy
= accuracy
== 0 ? DEFAULT_ACCURACY_USEC
: accuracy
;
1134 s
->time
.callback
= callback
;
1135 s
->time
.earliest_index
= s
->time
.latest_index
= PRIOQ_IDX_NULL
;
1136 s
->userdata
= userdata
;
1137 s
->enabled
= SD_EVENT_ONESHOT
;
1139 d
->needs_rearm
= true;
1141 r
= prioq_put(d
->earliest
, s
, &s
->time
.earliest_index
);
1145 r
= prioq_put(d
->latest
, s
, &s
->time
.latest_index
);
1156 static int signal_exit_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1159 return sd_event_exit(sd_event_source_get_event(s
), PTR_TO_INT(userdata
));
1162 _public_
int sd_event_add_signal(
1164 sd_event_source
**ret
,
1166 sd_event_signal_handler_t callback
,
1169 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1170 struct signal_data
*d
;
1173 assert_return(e
, -EINVAL
);
1174 assert_return(e
= event_resolve(e
), -ENOPKG
);
1175 assert_return(SIGNAL_VALID(sig
), -EINVAL
);
1176 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1177 assert_return(!event_pid_changed(e
), -ECHILD
);
1180 callback
= signal_exit_callback
;
1182 r
= signal_is_blocked(sig
);
1188 if (!e
->signal_sources
) {
1189 e
->signal_sources
= new0(sd_event_source
*, _NSIG
);
1190 if (!e
->signal_sources
)
1192 } else if (e
->signal_sources
[sig
])
1195 s
= source_new(e
, !ret
, SOURCE_SIGNAL
);
1199 s
->signal
.sig
= sig
;
1200 s
->signal
.callback
= callback
;
1201 s
->userdata
= userdata
;
1202 s
->enabled
= SD_EVENT_ON
;
1204 e
->signal_sources
[sig
] = s
;
1206 r
= event_make_signal_data(e
, sig
, &d
);
1210 /* Use the signal name as description for the event source by default */
1211 (void) sd_event_source_set_description(s
, signal_to_string(sig
));
1220 static bool shall_use_pidfd(void) {
1221 /* Mostly relevant for debugging, i.e. this is used in test-event.c to test the event loop once with and once without pidfd */
1222 return getenv_bool_secure("SYSTEMD_PIDFD") != 0;
1225 _public_
int sd_event_add_child(
1227 sd_event_source
**ret
,
1230 sd_event_child_handler_t callback
,
1233 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1236 assert_return(e
, -EINVAL
);
1237 assert_return(e
= event_resolve(e
), -ENOPKG
);
1238 assert_return(pid
> 1, -EINVAL
);
1239 assert_return(!(options
& ~(WEXITED
|WSTOPPED
|WCONTINUED
)), -EINVAL
);
1240 assert_return(options
!= 0, -EINVAL
);
1241 assert_return(callback
, -EINVAL
);
1242 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1243 assert_return(!event_pid_changed(e
), -ECHILD
);
1245 if (e
->n_enabled_child_sources
== 0) {
1246 /* Caller must block SIGCHLD before using us to watch children, even if pidfd is available,
1247 * for compatibility with pre-pidfd and because we don't want the reap the child processes
1248 * ourselves, i.e. call waitid(), and don't want Linux' default internal logic for that to
1251 * (As an optimization we only do this check on the first child event source created.) */
1252 r
= signal_is_blocked(SIGCHLD
);
1259 r
= hashmap_ensure_allocated(&e
->child_sources
, NULL
);
1263 if (hashmap_contains(e
->child_sources
, PID_TO_PTR(pid
)))
1266 s
= source_new(e
, !ret
, SOURCE_CHILD
);
1270 s
->wakeup
= WAKEUP_EVENT_SOURCE
;
1272 s
->child
.options
= options
;
1273 s
->child
.callback
= callback
;
1274 s
->userdata
= userdata
;
1275 s
->enabled
= SD_EVENT_ONESHOT
;
1277 /* We always take a pidfd here if we can, even if we wait for anything else than WEXITED, so that we
1278 * pin the PID, and make regular waitid() handling race-free. */
1280 if (shall_use_pidfd()) {
1281 s
->child
.pidfd
= pidfd_open(s
->child
.pid
, 0);
1282 if (s
->child
.pidfd
< 0) {
1283 /* Propagate errors unless the syscall is not supported or blocked */
1284 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && !ERRNO_IS_PRIVILEGE(errno
))
1287 s
->child
.pidfd_owned
= true; /* If we allocate the pidfd we own it by default */
1289 s
->child
.pidfd
= -1;
1291 r
= hashmap_put(e
->child_sources
, PID_TO_PTR(pid
), s
);
1295 e
->n_enabled_child_sources
++;
1297 if (EVENT_SOURCE_WATCH_PIDFD(s
)) {
1298 /* We have a pidfd and we only want to watch for exit */
1300 r
= source_child_pidfd_register(s
, s
->enabled
);
1302 e
->n_enabled_child_sources
--;
1306 /* We have no pidfd or we shall wait for some other event than WEXITED */
1308 r
= event_make_signal_data(e
, SIGCHLD
, NULL
);
1310 e
->n_enabled_child_sources
--;
1314 e
->need_process_child
= true;
1324 _public_
int sd_event_add_child_pidfd(
1326 sd_event_source
**ret
,
1329 sd_event_child_handler_t callback
,
1333 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1337 assert_return(e
, -EINVAL
);
1338 assert_return(e
= event_resolve(e
), -ENOPKG
);
1339 assert_return(pidfd
>= 0, -EBADF
);
1340 assert_return(!(options
& ~(WEXITED
|WSTOPPED
|WCONTINUED
)), -EINVAL
);
1341 assert_return(options
!= 0, -EINVAL
);
1342 assert_return(callback
, -EINVAL
);
1343 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1344 assert_return(!event_pid_changed(e
), -ECHILD
);
1346 if (e
->n_enabled_child_sources
== 0) {
1347 r
= signal_is_blocked(SIGCHLD
);
1354 r
= hashmap_ensure_allocated(&e
->child_sources
, NULL
);
1358 r
= pidfd_get_pid(pidfd
, &pid
);
1362 if (hashmap_contains(e
->child_sources
, PID_TO_PTR(pid
)))
1365 s
= source_new(e
, !ret
, SOURCE_CHILD
);
1369 s
->wakeup
= WAKEUP_EVENT_SOURCE
;
1370 s
->child
.pidfd
= pidfd
;
1372 s
->child
.options
= options
;
1373 s
->child
.callback
= callback
;
1374 s
->child
.pidfd_owned
= false; /* If we got the pidfd passed in we don't own it by default (similar to the IO fd case) */
1375 s
->userdata
= userdata
;
1376 s
->enabled
= SD_EVENT_ONESHOT
;
1378 r
= hashmap_put(e
->child_sources
, PID_TO_PTR(pid
), s
);
1382 e
->n_enabled_child_sources
++;
1384 if (EVENT_SOURCE_WATCH_PIDFD(s
)) {
1385 /* We only want to watch for WEXITED */
1387 r
= source_child_pidfd_register(s
, s
->enabled
);
1389 e
->n_enabled_child_sources
--;
1393 /* We shall wait for some other event than WEXITED */
1395 r
= event_make_signal_data(e
, SIGCHLD
, NULL
);
1397 e
->n_enabled_child_sources
--;
1401 e
->need_process_child
= true;
1411 _public_
int sd_event_add_defer(
1413 sd_event_source
**ret
,
1414 sd_event_handler_t callback
,
1417 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1420 assert_return(e
, -EINVAL
);
1421 assert_return(e
= event_resolve(e
), -ENOPKG
);
1422 assert_return(callback
, -EINVAL
);
1423 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1424 assert_return(!event_pid_changed(e
), -ECHILD
);
1426 s
= source_new(e
, !ret
, SOURCE_DEFER
);
1430 s
->defer
.callback
= callback
;
1431 s
->userdata
= userdata
;
1432 s
->enabled
= SD_EVENT_ONESHOT
;
1434 r
= source_set_pending(s
, true);
1445 _public_
int sd_event_add_post(
1447 sd_event_source
**ret
,
1448 sd_event_handler_t callback
,
1451 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1454 assert_return(e
, -EINVAL
);
1455 assert_return(e
= event_resolve(e
), -ENOPKG
);
1456 assert_return(callback
, -EINVAL
);
1457 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1458 assert_return(!event_pid_changed(e
), -ECHILD
);
1460 r
= set_ensure_allocated(&e
->post_sources
, NULL
);
1464 s
= source_new(e
, !ret
, SOURCE_POST
);
1468 s
->post
.callback
= callback
;
1469 s
->userdata
= userdata
;
1470 s
->enabled
= SD_EVENT_ON
;
1472 r
= set_put(e
->post_sources
, s
);
1483 _public_
int sd_event_add_exit(
1485 sd_event_source
**ret
,
1486 sd_event_handler_t callback
,
1489 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1492 assert_return(e
, -EINVAL
);
1493 assert_return(e
= event_resolve(e
), -ENOPKG
);
1494 assert_return(callback
, -EINVAL
);
1495 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1496 assert_return(!event_pid_changed(e
), -ECHILD
);
1498 r
= prioq_ensure_allocated(&e
->exit
, exit_prioq_compare
);
1502 s
= source_new(e
, !ret
, SOURCE_EXIT
);
1506 s
->exit
.callback
= callback
;
1507 s
->userdata
= userdata
;
1508 s
->exit
.prioq_index
= PRIOQ_IDX_NULL
;
1509 s
->enabled
= SD_EVENT_ONESHOT
;
1511 r
= prioq_put(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
1522 static void event_free_inotify_data(sd_event
*e
, struct inotify_data
*d
) {
1528 assert(hashmap_isempty(d
->inodes
));
1529 assert(hashmap_isempty(d
->wd
));
1531 if (d
->buffer_filled
> 0)
1532 LIST_REMOVE(buffered
, e
->inotify_data_buffered
, d
);
1534 hashmap_free(d
->inodes
);
1535 hashmap_free(d
->wd
);
1537 assert_se(hashmap_remove(e
->inotify_data
, &d
->priority
) == d
);
1540 if (epoll_ctl(e
->epoll_fd
, EPOLL_CTL_DEL
, d
->fd
, NULL
) < 0)
1541 log_debug_errno(errno
, "Failed to remove inotify fd from epoll, ignoring: %m");
1548 static int event_make_inotify_data(
1551 struct inotify_data
**ret
) {
1553 _cleanup_close_
int fd
= -1;
1554 struct inotify_data
*d
;
1555 struct epoll_event ev
;
1560 d
= hashmap_get(e
->inotify_data
, &priority
);
1567 fd
= inotify_init1(IN_NONBLOCK
|O_CLOEXEC
);
1571 fd
= fd_move_above_stdio(fd
);
1573 r
= hashmap_ensure_allocated(&e
->inotify_data
, &uint64_hash_ops
);
1577 d
= new(struct inotify_data
, 1);
1581 *d
= (struct inotify_data
) {
1582 .wakeup
= WAKEUP_INOTIFY_DATA
,
1584 .priority
= priority
,
1587 r
= hashmap_put(e
->inotify_data
, &d
->priority
, d
);
1589 d
->fd
= safe_close(d
->fd
);
1594 ev
= (struct epoll_event
) {
1599 if (epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, d
->fd
, &ev
) < 0) {
1601 d
->fd
= safe_close(d
->fd
); /* let's close this ourselves, as event_free_inotify_data() would otherwise
1602 * remove the fd from the epoll first, which we don't want as we couldn't
1603 * add it in the first place. */
1604 event_free_inotify_data(e
, d
);
1614 static int inode_data_compare(const struct inode_data
*x
, const struct inode_data
*y
) {
1620 r
= CMP(x
->dev
, y
->dev
);
1624 return CMP(x
->ino
, y
->ino
);
1627 static void inode_data_hash_func(const struct inode_data
*d
, struct siphash
*state
) {
1630 siphash24_compress(&d
->dev
, sizeof(d
->dev
), state
);
1631 siphash24_compress(&d
->ino
, sizeof(d
->ino
), state
);
1634 DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops
, struct inode_data
, inode_data_hash_func
, inode_data_compare
);
1636 static void event_free_inode_data(
1638 struct inode_data
*d
) {
1645 assert(!d
->event_sources
);
1648 LIST_REMOVE(to_close
, e
->inode_data_to_close
, d
);
1652 if (d
->inotify_data
) {
1655 if (d
->inotify_data
->fd
>= 0) {
1656 /* So here's a problem. At the time this runs the watch descriptor might already be
1657 * invalidated, because an IN_IGNORED event might be queued right the moment we enter
1658 * the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very
1659 * likely case to happen. */
1661 if (inotify_rm_watch(d
->inotify_data
->fd
, d
->wd
) < 0 && errno
!= EINVAL
)
1662 log_debug_errno(errno
, "Failed to remove watch descriptor %i from inotify, ignoring: %m", d
->wd
);
1665 assert_se(hashmap_remove(d
->inotify_data
->wd
, INT_TO_PTR(d
->wd
)) == d
);
1668 assert_se(hashmap_remove(d
->inotify_data
->inodes
, d
) == d
);
1674 static void event_gc_inode_data(
1676 struct inode_data
*d
) {
1678 struct inotify_data
*inotify_data
;
1685 if (d
->event_sources
)
1688 inotify_data
= d
->inotify_data
;
1689 event_free_inode_data(e
, d
);
1691 if (inotify_data
&& hashmap_isempty(inotify_data
->inodes
))
1692 event_free_inotify_data(e
, inotify_data
);
1695 static int event_make_inode_data(
1697 struct inotify_data
*inotify_data
,
1700 struct inode_data
**ret
) {
1702 struct inode_data
*d
, key
;
1706 assert(inotify_data
);
1708 key
= (struct inode_data
) {
1713 d
= hashmap_get(inotify_data
->inodes
, &key
);
1721 r
= hashmap_ensure_allocated(&inotify_data
->inodes
, &inode_data_hash_ops
);
1725 d
= new(struct inode_data
, 1);
1729 *d
= (struct inode_data
) {
1734 .inotify_data
= inotify_data
,
1737 r
= hashmap_put(inotify_data
->inodes
, d
, d
);
1749 static uint32_t inode_data_determine_mask(struct inode_data
*d
) {
1750 bool excl_unlink
= true;
1751 uint32_t combined
= 0;
1756 /* Combines the watch masks of all event sources watching this inode. We generally just OR them together, but
1757 * the IN_EXCL_UNLINK flag is ANDed instead.
1759 * Note that we add all sources to the mask here, regardless whether enabled, disabled or oneshot. That's
1760 * because we cannot change the mask anymore after the event source was created once, since the kernel has no
1761 * API for that. Hence we need to subscribe to the maximum mask we ever might be interested in, and suppress
1762 * events we don't care for client-side. */
1764 LIST_FOREACH(inotify
.by_inode_data
, s
, d
->event_sources
) {
1766 if ((s
->inotify
.mask
& IN_EXCL_UNLINK
) == 0)
1767 excl_unlink
= false;
1769 combined
|= s
->inotify
.mask
;
1772 return (combined
& ~(IN_ONESHOT
|IN_DONT_FOLLOW
|IN_ONLYDIR
|IN_EXCL_UNLINK
)) | (excl_unlink
? IN_EXCL_UNLINK
: 0);
1775 static int inode_data_realize_watch(sd_event
*e
, struct inode_data
*d
) {
1776 uint32_t combined_mask
;
1782 combined_mask
= inode_data_determine_mask(d
);
1784 if (d
->wd
>= 0 && combined_mask
== d
->combined_mask
)
1787 r
= hashmap_ensure_allocated(&d
->inotify_data
->wd
, NULL
);
1791 wd
= inotify_add_watch_fd(d
->inotify_data
->fd
, d
->fd
, combined_mask
);
1796 r
= hashmap_put(d
->inotify_data
->wd
, INT_TO_PTR(wd
), d
);
1798 (void) inotify_rm_watch(d
->inotify_data
->fd
, wd
);
1804 } else if (d
->wd
!= wd
) {
1806 log_debug("Weird, the watch descriptor we already knew for this inode changed?");
1807 (void) inotify_rm_watch(d
->fd
, wd
);
1811 d
->combined_mask
= combined_mask
;
1815 _public_
int sd_event_add_inotify(
1817 sd_event_source
**ret
,
1820 sd_event_inotify_handler_t callback
,
1823 struct inotify_data
*inotify_data
= NULL
;
1824 struct inode_data
*inode_data
= NULL
;
1825 _cleanup_close_
int fd
= -1;
1826 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1830 assert_return(e
, -EINVAL
);
1831 assert_return(e
= event_resolve(e
), -ENOPKG
);
1832 assert_return(path
, -EINVAL
);
1833 assert_return(callback
, -EINVAL
);
1834 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1835 assert_return(!event_pid_changed(e
), -ECHILD
);
1837 /* Refuse IN_MASK_ADD since we coalesce watches on the same inode, and hence really don't want to merge
1838 * masks. Or in other words, this whole code exists only to manage IN_MASK_ADD type operations for you, hence
1839 * the user can't use them for us. */
1840 if (mask
& IN_MASK_ADD
)
1843 fd
= open(path
, O_PATH
|O_CLOEXEC
|
1844 (mask
& IN_ONLYDIR
? O_DIRECTORY
: 0)|
1845 (mask
& IN_DONT_FOLLOW
? O_NOFOLLOW
: 0));
1849 if (fstat(fd
, &st
) < 0)
1852 s
= source_new(e
, !ret
, SOURCE_INOTIFY
);
1856 s
->enabled
= mask
& IN_ONESHOT
? SD_EVENT_ONESHOT
: SD_EVENT_ON
;
1857 s
->inotify
.mask
= mask
;
1858 s
->inotify
.callback
= callback
;
1859 s
->userdata
= userdata
;
1861 /* Allocate an inotify object for this priority, and an inode object within it */
1862 r
= event_make_inotify_data(e
, SD_EVENT_PRIORITY_NORMAL
, &inotify_data
);
1866 r
= event_make_inode_data(e
, inotify_data
, st
.st_dev
, st
.st_ino
, &inode_data
);
1868 event_free_inotify_data(e
, inotify_data
);
1872 /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
1873 * the event source, until then, for which we need the original inode. */
1874 if (inode_data
->fd
< 0) {
1875 inode_data
->fd
= TAKE_FD(fd
);
1876 LIST_PREPEND(to_close
, e
->inode_data_to_close
, inode_data
);
1879 /* Link our event source to the inode data object */
1880 LIST_PREPEND(inotify
.by_inode_data
, inode_data
->event_sources
, s
);
1881 s
->inotify
.inode_data
= inode_data
;
1883 /* Actually realize the watch now */
1884 r
= inode_data_realize_watch(e
, inode_data
);
1888 (void) sd_event_source_set_description(s
, path
);
1897 static sd_event_source
* event_source_free(sd_event_source
*s
) {
1901 /* Here's a special hack: when we are called from a
1902 * dispatch handler we won't free the event source
1903 * immediately, but we will detach the fd from the
1904 * epoll. This way it is safe for the caller to unref
1905 * the event source and immediately close the fd, but
1906 * we still retain a valid event source object after
1909 if (s
->dispatching
) {
1910 if (s
->type
== SOURCE_IO
)
1911 source_io_unregister(s
);
1913 source_disconnect(s
);
1920 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source
, sd_event_source
, event_source_free
);
1922 _public_
int sd_event_source_set_description(sd_event_source
*s
, const char *description
) {
1923 assert_return(s
, -EINVAL
);
1924 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1926 return free_and_strdup(&s
->description
, description
);
1929 _public_
int sd_event_source_get_description(sd_event_source
*s
, const char **description
) {
1930 assert_return(s
, -EINVAL
);
1931 assert_return(description
, -EINVAL
);
1932 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1934 if (!s
->description
)
1937 *description
= s
->description
;
1941 _public_ sd_event
*sd_event_source_get_event(sd_event_source
*s
) {
1942 assert_return(s
, NULL
);
1947 _public_
int sd_event_source_get_pending(sd_event_source
*s
) {
1948 assert_return(s
, -EINVAL
);
1949 assert_return(s
->type
!= SOURCE_EXIT
, -EDOM
);
1950 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1951 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1956 _public_
int sd_event_source_get_io_fd(sd_event_source
*s
) {
1957 assert_return(s
, -EINVAL
);
1958 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1959 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1964 _public_
int sd_event_source_set_io_fd(sd_event_source
*s
, int fd
) {
1967 assert_return(s
, -EINVAL
);
1968 assert_return(fd
>= 0, -EBADF
);
1969 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1970 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1975 if (s
->enabled
== SD_EVENT_OFF
) {
1977 s
->io
.registered
= false;
1981 saved_fd
= s
->io
.fd
;
1982 assert(s
->io
.registered
);
1985 s
->io
.registered
= false;
1987 r
= source_io_register(s
, s
->enabled
, s
->io
.events
);
1989 s
->io
.fd
= saved_fd
;
1990 s
->io
.registered
= true;
1994 (void) epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_DEL
, saved_fd
, NULL
);
2000 _public_
int sd_event_source_get_io_fd_own(sd_event_source
*s
) {
2001 assert_return(s
, -EINVAL
);
2002 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
2007 _public_
int sd_event_source_set_io_fd_own(sd_event_source
*s
, int own
) {
2008 assert_return(s
, -EINVAL
);
2009 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
2015 _public_
int sd_event_source_get_io_events(sd_event_source
*s
, uint32_t* events
) {
2016 assert_return(s
, -EINVAL
);
2017 assert_return(events
, -EINVAL
);
2018 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
2019 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2021 *events
= s
->io
.events
;
2025 _public_
int sd_event_source_set_io_events(sd_event_source
*s
, uint32_t events
) {
2028 assert_return(s
, -EINVAL
);
2029 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
2030 assert_return(!(events
& ~(EPOLLIN
|EPOLLOUT
|EPOLLRDHUP
|EPOLLPRI
|EPOLLERR
|EPOLLHUP
|EPOLLET
)), -EINVAL
);
2031 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2032 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2034 /* edge-triggered updates are never skipped, so we can reset edges */
2035 if (s
->io
.events
== events
&& !(events
& EPOLLET
))
2038 r
= source_set_pending(s
, false);
2042 if (s
->enabled
!= SD_EVENT_OFF
) {
2043 r
= source_io_register(s
, s
->enabled
, events
);
2048 s
->io
.events
= events
;
2053 _public_
int sd_event_source_get_io_revents(sd_event_source
*s
, uint32_t* revents
) {
2054 assert_return(s
, -EINVAL
);
2055 assert_return(revents
, -EINVAL
);
2056 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
2057 assert_return(s
->pending
, -ENODATA
);
2058 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2060 *revents
= s
->io
.revents
;
2064 _public_
int sd_event_source_get_signal(sd_event_source
*s
) {
2065 assert_return(s
, -EINVAL
);
2066 assert_return(s
->type
== SOURCE_SIGNAL
, -EDOM
);
2067 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2069 return s
->signal
.sig
;
2072 _public_
int sd_event_source_get_priority(sd_event_source
*s
, int64_t *priority
) {
2073 assert_return(s
, -EINVAL
);
2074 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2076 *priority
= s
->priority
;
2080 _public_
int sd_event_source_set_priority(sd_event_source
*s
, int64_t priority
) {
2081 bool rm_inotify
= false, rm_inode
= false;
2082 struct inotify_data
*new_inotify_data
= NULL
;
2083 struct inode_data
*new_inode_data
= NULL
;
2086 assert_return(s
, -EINVAL
);
2087 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2088 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2090 if (s
->priority
== priority
)
2093 if (s
->type
== SOURCE_INOTIFY
) {
2094 struct inode_data
*old_inode_data
;
2096 assert(s
->inotify
.inode_data
);
2097 old_inode_data
= s
->inotify
.inode_data
;
2099 /* We need the original fd to change the priority. If we don't have it we can't change the priority,
2100 * anymore. Note that we close any fds when entering the next event loop iteration, i.e. for inotify
2101 * events we allow priority changes only until the first following iteration. */
2102 if (old_inode_data
->fd
< 0)
2105 r
= event_make_inotify_data(s
->event
, priority
, &new_inotify_data
);
2110 r
= event_make_inode_data(s
->event
, new_inotify_data
, old_inode_data
->dev
, old_inode_data
->ino
, &new_inode_data
);
2115 if (new_inode_data
->fd
< 0) {
2116 /* Duplicate the fd for the new inode object if we don't have any yet */
2117 new_inode_data
->fd
= fcntl(old_inode_data
->fd
, F_DUPFD_CLOEXEC
, 3);
2118 if (new_inode_data
->fd
< 0) {
2123 LIST_PREPEND(to_close
, s
->event
->inode_data_to_close
, new_inode_data
);
2126 /* Move the event source to the new inode data structure */
2127 LIST_REMOVE(inotify
.by_inode_data
, old_inode_data
->event_sources
, s
);
2128 LIST_PREPEND(inotify
.by_inode_data
, new_inode_data
->event_sources
, s
);
2129 s
->inotify
.inode_data
= new_inode_data
;
2131 /* Now create the new watch */
2132 r
= inode_data_realize_watch(s
->event
, new_inode_data
);
2135 LIST_REMOVE(inotify
.by_inode_data
, new_inode_data
->event_sources
, s
);
2136 LIST_PREPEND(inotify
.by_inode_data
, old_inode_data
->event_sources
, s
);
2137 s
->inotify
.inode_data
= old_inode_data
;
2141 s
->priority
= priority
;
2143 event_gc_inode_data(s
->event
, old_inode_data
);
2145 } else if (s
->type
== SOURCE_SIGNAL
&& s
->enabled
!= SD_EVENT_OFF
) {
2146 struct signal_data
*old
, *d
;
2148 /* Move us from the signalfd belonging to the old
2149 * priority to the signalfd of the new priority */
2151 assert_se(old
= hashmap_get(s
->event
->signal_data
, &s
->priority
));
2153 s
->priority
= priority
;
2155 r
= event_make_signal_data(s
->event
, s
->signal
.sig
, &d
);
2157 s
->priority
= old
->priority
;
2161 event_unmask_signal_data(s
->event
, old
, s
->signal
.sig
);
2163 s
->priority
= priority
;
2166 prioq_reshuffle(s
->event
->pending
, s
, &s
->pending_index
);
2169 prioq_reshuffle(s
->event
->prepare
, s
, &s
->prepare_index
);
2171 if (s
->type
== SOURCE_EXIT
)
2172 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
2178 event_free_inode_data(s
->event
, new_inode_data
);
2181 event_free_inotify_data(s
->event
, new_inotify_data
);
2186 _public_
int sd_event_source_get_enabled(sd_event_source
*s
, int *m
) {
2187 assert_return(s
, -EINVAL
);
2188 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2192 return s
->enabled
!= SD_EVENT_OFF
;
2195 _public_
int sd_event_source_set_enabled(sd_event_source
*s
, int m
) {
2198 assert_return(s
, -EINVAL
);
2199 assert_return(IN_SET(m
, SD_EVENT_OFF
, SD_EVENT_ON
, SD_EVENT_ONESHOT
), -EINVAL
);
2200 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2202 /* If we are dead anyway, we are fine with turning off
2203 * sources, but everything else needs to fail. */
2204 if (s
->event
->state
== SD_EVENT_FINISHED
)
2205 return m
== SD_EVENT_OFF
? 0 : -ESTALE
;
2207 if (s
->enabled
== m
)
2210 if (m
== SD_EVENT_OFF
) {
2212 /* Unset the pending flag when this event source is disabled */
2213 if (!IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
2214 r
= source_set_pending(s
, false);
2222 source_io_unregister(s
);
2226 case SOURCE_TIME_REALTIME
:
2227 case SOURCE_TIME_BOOTTIME
:
2228 case SOURCE_TIME_MONOTONIC
:
2229 case SOURCE_TIME_REALTIME_ALARM
:
2230 case SOURCE_TIME_BOOTTIME_ALARM
: {
2231 struct clock_data
*d
;
2234 d
= event_get_clock_data(s
->event
, s
->type
);
2237 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2238 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2239 d
->needs_rearm
= true;
2246 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
2252 assert(s
->event
->n_enabled_child_sources
> 0);
2253 s
->event
->n_enabled_child_sources
--;
2255 if (EVENT_SOURCE_WATCH_PIDFD(s
))
2256 source_child_pidfd_unregister(s
);
2258 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
2264 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
2269 case SOURCE_INOTIFY
:
2274 assert_not_reached("Wut? I shouldn't exist.");
2279 /* Unset the pending flag when this event source is enabled */
2280 if (s
->enabled
== SD_EVENT_OFF
&& !IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
2281 r
= source_set_pending(s
, false);
2289 r
= source_io_register(s
, m
, s
->io
.events
);
2296 case SOURCE_TIME_REALTIME
:
2297 case SOURCE_TIME_BOOTTIME
:
2298 case SOURCE_TIME_MONOTONIC
:
2299 case SOURCE_TIME_REALTIME_ALARM
:
2300 case SOURCE_TIME_BOOTTIME_ALARM
: {
2301 struct clock_data
*d
;
2304 d
= event_get_clock_data(s
->event
, s
->type
);
2307 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2308 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2309 d
->needs_rearm
= true;
2317 r
= event_make_signal_data(s
->event
, s
->signal
.sig
, NULL
);
2319 s
->enabled
= SD_EVENT_OFF
;
2320 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
2328 if (s
->enabled
== SD_EVENT_OFF
)
2329 s
->event
->n_enabled_child_sources
++;
2333 if (EVENT_SOURCE_WATCH_PIDFD(s
)) {
2334 /* yes, we have pidfd */
2336 r
= source_child_pidfd_register(s
, s
->enabled
);
2338 s
->enabled
= SD_EVENT_OFF
;
2339 s
->event
->n_enabled_child_sources
--;
2343 /* no pidfd, or something other to watch for than WEXITED */
2345 r
= event_make_signal_data(s
->event
, SIGCHLD
, NULL
);
2347 s
->enabled
= SD_EVENT_OFF
;
2348 s
->event
->n_enabled_child_sources
--;
2349 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
2358 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
2363 case SOURCE_INOTIFY
:
2368 assert_not_reached("Wut? I shouldn't exist.");
2373 prioq_reshuffle(s
->event
->pending
, s
, &s
->pending_index
);
2376 prioq_reshuffle(s
->event
->prepare
, s
, &s
->prepare_index
);
2381 _public_
int sd_event_source_get_time(sd_event_source
*s
, uint64_t *usec
) {
2382 assert_return(s
, -EINVAL
);
2383 assert_return(usec
, -EINVAL
);
2384 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2385 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2387 *usec
= s
->time
.next
;
2391 _public_
int sd_event_source_set_time(sd_event_source
*s
, uint64_t usec
) {
2392 struct clock_data
*d
;
2395 assert_return(s
, -EINVAL
);
2396 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2397 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2398 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2400 r
= source_set_pending(s
, false);
2404 s
->time
.next
= usec
;
2406 d
= event_get_clock_data(s
->event
, s
->type
);
2409 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2410 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2411 d
->needs_rearm
= true;
2416 _public_
int sd_event_source_get_time_accuracy(sd_event_source
*s
, uint64_t *usec
) {
2417 assert_return(s
, -EINVAL
);
2418 assert_return(usec
, -EINVAL
);
2419 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2420 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2422 *usec
= s
->time
.accuracy
;
2426 _public_
int sd_event_source_set_time_accuracy(sd_event_source
*s
, uint64_t usec
) {
2427 struct clock_data
*d
;
2430 assert_return(s
, -EINVAL
);
2431 assert_return(usec
!= (uint64_t) -1, -EINVAL
);
2432 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2433 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2434 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2436 r
= source_set_pending(s
, false);
2441 usec
= DEFAULT_ACCURACY_USEC
;
2443 s
->time
.accuracy
= usec
;
2445 d
= event_get_clock_data(s
->event
, s
->type
);
2448 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2449 d
->needs_rearm
= true;
2454 _public_
int sd_event_source_get_time_clock(sd_event_source
*s
, clockid_t
*clock
) {
2455 assert_return(s
, -EINVAL
);
2456 assert_return(clock
, -EINVAL
);
2457 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2458 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2460 *clock
= event_source_type_to_clock(s
->type
);
2464 _public_
int sd_event_source_get_child_pid(sd_event_source
*s
, pid_t
*pid
) {
2465 assert_return(s
, -EINVAL
);
2466 assert_return(pid
, -EINVAL
);
2467 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2468 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2470 *pid
= s
->child
.pid
;
2474 _public_
int sd_event_source_get_child_pidfd(sd_event_source
*s
) {
2475 assert_return(s
, -EINVAL
);
2476 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2477 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2479 if (s
->child
.pidfd
< 0)
2482 return s
->child
.pidfd
;
2485 _public_
int sd_event_source_send_child_signal(sd_event_source
*s
, int sig
, const siginfo_t
*si
, unsigned flags
) {
2486 assert_return(s
, -EINVAL
);
2487 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2488 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2489 assert_return(SIGNAL_VALID(sig
), -EINVAL
);
2491 /* If we already have seen indication the process exited refuse sending a signal early. This way we
2492 * can be sure we don't accidentally kill the wrong process on PID reuse when pidfds are not
2494 if (s
->child
.exited
)
2497 if (s
->child
.pidfd
>= 0) {
2500 /* pidfd_send_signal() changes the siginfo_t argument. This is weird, let's hence copy the
2505 if (pidfd_send_signal(s
->child
.pidfd
, sig
, si
? ©
: NULL
, 0) < 0) {
2506 /* Let's propagate the error only if the system call is not implemented or prohibited */
2507 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && !ERRNO_IS_PRIVILEGE(errno
))
2513 /* Flags are only supported for pidfd_send_signal(), not for rt_sigqueueinfo(), hence let's refuse
2519 /* We use rt_sigqueueinfo() only if siginfo_t is specified. */
2520 siginfo_t copy
= *si
;
2522 if (rt_sigqueueinfo(s
->child
.pid
, sig
, ©
) < 0)
2524 } else if (kill(s
->child
.pid
, sig
) < 0)
2530 _public_
int sd_event_source_get_child_pidfd_own(sd_event_source
*s
) {
2531 assert_return(s
, -EINVAL
);
2532 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2534 if (s
->child
.pidfd
< 0)
2537 return s
->child
.pidfd_owned
;
2540 _public_
int sd_event_source_set_child_pidfd_own(sd_event_source
*s
, int own
) {
2541 assert_return(s
, -EINVAL
);
2542 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2544 if (s
->child
.pidfd
< 0)
2547 s
->child
.pidfd_owned
= own
;
2551 _public_
int sd_event_source_get_child_process_own(sd_event_source
*s
) {
2552 assert_return(s
, -EINVAL
);
2553 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2555 return s
->child
.process_owned
;
2558 _public_
int sd_event_source_set_child_process_own(sd_event_source
*s
, int own
) {
2559 assert_return(s
, -EINVAL
);
2560 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2562 s
->child
.process_owned
= own
;
2566 _public_
int sd_event_source_get_inotify_mask(sd_event_source
*s
, uint32_t *mask
) {
2567 assert_return(s
, -EINVAL
);
2568 assert_return(mask
, -EINVAL
);
2569 assert_return(s
->type
== SOURCE_INOTIFY
, -EDOM
);
2570 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2572 *mask
= s
->inotify
.mask
;
2576 _public_
int sd_event_source_set_prepare(sd_event_source
*s
, sd_event_handler_t callback
) {
2579 assert_return(s
, -EINVAL
);
2580 assert_return(s
->type
!= SOURCE_EXIT
, -EDOM
);
2581 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2582 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2584 if (s
->prepare
== callback
)
2587 if (callback
&& s
->prepare
) {
2588 s
->prepare
= callback
;
2592 r
= prioq_ensure_allocated(&s
->event
->prepare
, prepare_prioq_compare
);
2596 s
->prepare
= callback
;
2599 r
= prioq_put(s
->event
->prepare
, s
, &s
->prepare_index
);
2603 prioq_remove(s
->event
->prepare
, s
, &s
->prepare_index
);
2608 _public_
void* sd_event_source_get_userdata(sd_event_source
*s
) {
2609 assert_return(s
, NULL
);
2614 _public_
void *sd_event_source_set_userdata(sd_event_source
*s
, void *userdata
) {
2617 assert_return(s
, NULL
);
2620 s
->userdata
= userdata
;
2625 static usec_t
sleep_between(sd_event
*e
, usec_t a
, usec_t b
) {
2632 if (a
>= USEC_INFINITY
)
2633 return USEC_INFINITY
;
2638 initialize_perturb(e
);
2641 Find a good time to wake up again between times a and b. We
2642 have two goals here:
2644 a) We want to wake up as seldom as possible, hence prefer
2645 later times over earlier times.
2647 b) But if we have to wake up, then let's make sure to
2648 dispatch as much as possible on the entire system.
2650 We implement this by waking up everywhere at the same time
2651 within any given minute if we can, synchronised via the
2652 perturbation value determined from the boot ID. If we can't,
2653 then we try to find the same spot in every 10s, then 1s and
2654 then 250ms step. Otherwise, we pick the last possible time
2658 c
= (b
/ USEC_PER_MINUTE
) * USEC_PER_MINUTE
+ e
->perturb
;
2660 if (_unlikely_(c
< USEC_PER_MINUTE
))
2663 c
-= USEC_PER_MINUTE
;
2669 c
= (b
/ (USEC_PER_SEC
*10)) * (USEC_PER_SEC
*10) + (e
->perturb
% (USEC_PER_SEC
*10));
2671 if (_unlikely_(c
< USEC_PER_SEC
*10))
2674 c
-= USEC_PER_SEC
*10;
2680 c
= (b
/ USEC_PER_SEC
) * USEC_PER_SEC
+ (e
->perturb
% USEC_PER_SEC
);
2682 if (_unlikely_(c
< USEC_PER_SEC
))
2691 c
= (b
/ (USEC_PER_MSEC
*250)) * (USEC_PER_MSEC
*250) + (e
->perturb
% (USEC_PER_MSEC
*250));
2693 if (_unlikely_(c
< USEC_PER_MSEC
*250))
2696 c
-= USEC_PER_MSEC
*250;
2705 static int event_arm_timer(
2707 struct clock_data
*d
) {
2709 struct itimerspec its
= {};
2710 sd_event_source
*a
, *b
;
2717 if (!d
->needs_rearm
)
2720 d
->needs_rearm
= false;
2722 a
= prioq_peek(d
->earliest
);
2723 if (!a
|| a
->enabled
== SD_EVENT_OFF
|| a
->time
.next
== USEC_INFINITY
) {
2728 if (d
->next
== USEC_INFINITY
)
2732 r
= timerfd_settime(d
->fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
2736 d
->next
= USEC_INFINITY
;
2740 b
= prioq_peek(d
->latest
);
2741 assert_se(b
&& b
->enabled
!= SD_EVENT_OFF
);
2743 t
= sleep_between(e
, a
->time
.next
, time_event_source_latest(b
));
2747 assert_se(d
->fd
>= 0);
2750 /* We don' want to disarm here, just mean some time looooong ago. */
2751 its
.it_value
.tv_sec
= 0;
2752 its
.it_value
.tv_nsec
= 1;
2754 timespec_store(&its
.it_value
, t
);
2756 r
= timerfd_settime(d
->fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
2764 static int process_io(sd_event
*e
, sd_event_source
*s
, uint32_t revents
) {
2767 assert(s
->type
== SOURCE_IO
);
2769 /* If the event source was already pending, we just OR in the
2770 * new revents, otherwise we reset the value. The ORing is
2771 * necessary to handle EPOLLONESHOT events properly where
2772 * readability might happen independently of writability, and
2773 * we need to keep track of both */
2776 s
->io
.revents
|= revents
;
2778 s
->io
.revents
= revents
;
2780 return source_set_pending(s
, true);
2783 static int flush_timer(sd_event
*e
, int fd
, uint32_t events
, usec_t
*next
) {
2790 assert_return(events
== EPOLLIN
, -EIO
);
2792 ss
= read(fd
, &x
, sizeof(x
));
2794 if (IN_SET(errno
, EAGAIN
, EINTR
))
2800 if (_unlikely_(ss
!= sizeof(x
)))
2804 *next
= USEC_INFINITY
;
2809 static int process_timer(
2812 struct clock_data
*d
) {
2821 s
= prioq_peek(d
->earliest
);
2824 s
->enabled
== SD_EVENT_OFF
||
2828 r
= source_set_pending(s
, true);
2832 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2833 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2834 d
->needs_rearm
= true;
2840 static int process_child(sd_event
*e
) {
2847 e
->need_process_child
= false;
2850 So, this is ugly. We iteratively invoke waitid() with P_PID
2851 + WNOHANG for each PID we wait for, instead of using
2852 P_ALL. This is because we only want to get child
2853 information of very specific child processes, and not all
2854 of them. We might not have processed the SIGCHLD even of a
2855 previous invocation and we don't want to maintain a
2856 unbounded *per-child* event queue, hence we really don't
2857 want anything flushed out of the kernel's queue that we
2858 don't care about. Since this is O(n) this means that if you
2859 have a lot of processes you probably want to handle SIGCHLD
2862 We do not reap the children here (by using WNOWAIT), this
2863 is only done after the event source is dispatched so that
2864 the callback still sees the process as a zombie.
2867 HASHMAP_FOREACH(s
, e
->child_sources
, i
) {
2868 assert(s
->type
== SOURCE_CHILD
);
2873 if (s
->enabled
== SD_EVENT_OFF
)
2876 if (s
->child
.exited
)
2879 if (EVENT_SOURCE_WATCH_PIDFD(s
)) /* There's a usable pidfd known for this event source? then don't waitid() for it here */
2882 zero(s
->child
.siginfo
);
2883 r
= waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
,
2884 WNOHANG
| (s
->child
.options
& WEXITED
? WNOWAIT
: 0) | s
->child
.options
);
2888 if (s
->child
.siginfo
.si_pid
!= 0) {
2889 bool zombie
= IN_SET(s
->child
.siginfo
.si_code
, CLD_EXITED
, CLD_KILLED
, CLD_DUMPED
);
2892 s
->child
.exited
= true;
2894 if (!zombie
&& (s
->child
.options
& WEXITED
)) {
2895 /* If the child isn't dead then let's
2896 * immediately remove the state change
2897 * from the queue, since there's no
2898 * benefit in leaving it queued */
2900 assert(s
->child
.options
& (WSTOPPED
|WCONTINUED
));
2901 (void) waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
, WNOHANG
|(s
->child
.options
& (WSTOPPED
|WCONTINUED
)));
2904 r
= source_set_pending(s
, true);
2913 static int process_pidfd(sd_event
*e
, sd_event_source
*s
, uint32_t revents
) {
2916 assert(s
->type
== SOURCE_CHILD
);
2921 if (s
->enabled
== SD_EVENT_OFF
)
2924 if (!EVENT_SOURCE_WATCH_PIDFD(s
))
2927 zero(s
->child
.siginfo
);
2928 if (waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
, WNOHANG
| WNOWAIT
| s
->child
.options
) < 0)
2931 if (s
->child
.siginfo
.si_pid
== 0)
2934 if (IN_SET(s
->child
.siginfo
.si_code
, CLD_EXITED
, CLD_KILLED
, CLD_DUMPED
))
2935 s
->child
.exited
= true;
2937 return source_set_pending(s
, true);
2940 static int process_signal(sd_event
*e
, struct signal_data
*d
, uint32_t events
) {
2941 bool read_one
= false;
2946 assert_return(events
== EPOLLIN
, -EIO
);
2948 /* If there's a signal queued on this priority and SIGCHLD is
2949 on this priority too, then make sure to recheck the
2950 children we watch. This is because we only ever dequeue
2951 the first signal per priority, and if we dequeue one, and
2952 SIGCHLD might be enqueued later we wouldn't know, but we
2953 might have higher priority children we care about hence we
2954 need to check that explicitly. */
2956 if (sigismember(&d
->sigset
, SIGCHLD
))
2957 e
->need_process_child
= true;
2959 /* If there's already an event source pending for this
2960 * priority we don't read another */
2965 struct signalfd_siginfo si
;
2967 sd_event_source
*s
= NULL
;
2969 n
= read(d
->fd
, &si
, sizeof(si
));
2971 if (IN_SET(errno
, EAGAIN
, EINTR
))
2977 if (_unlikely_(n
!= sizeof(si
)))
2980 assert(SIGNAL_VALID(si
.ssi_signo
));
2984 if (e
->signal_sources
)
2985 s
= e
->signal_sources
[si
.ssi_signo
];
2991 s
->signal
.siginfo
= si
;
2994 r
= source_set_pending(s
, true);
3002 static int event_inotify_data_read(sd_event
*e
, struct inotify_data
*d
, uint32_t revents
) {
3008 assert_return(revents
== EPOLLIN
, -EIO
);
3010 /* If there's already an event source pending for this priority, don't read another */
3011 if (d
->n_pending
> 0)
3014 /* Is the read buffer non-empty? If so, let's not read more */
3015 if (d
->buffer_filled
> 0)
3018 n
= read(d
->fd
, &d
->buffer
, sizeof(d
->buffer
));
3020 if (IN_SET(errno
, EAGAIN
, EINTR
))
3027 d
->buffer_filled
= (size_t) n
;
3028 LIST_PREPEND(buffered
, e
->inotify_data_buffered
, d
);
3033 static void event_inotify_data_drop(sd_event
*e
, struct inotify_data
*d
, size_t sz
) {
3036 assert(sz
<= d
->buffer_filled
);
3041 /* Move the rest to the buffer to the front, in order to get things properly aligned again */
3042 memmove(d
->buffer
.raw
, d
->buffer
.raw
+ sz
, d
->buffer_filled
- sz
);
3043 d
->buffer_filled
-= sz
;
3045 if (d
->buffer_filled
== 0)
3046 LIST_REMOVE(buffered
, e
->inotify_data_buffered
, d
);
3049 static int event_inotify_data_process(sd_event
*e
, struct inotify_data
*d
) {
3055 /* If there's already an event source pending for this priority, don't read another */
3056 if (d
->n_pending
> 0)
3059 while (d
->buffer_filled
> 0) {
3062 /* Let's validate that the event structures are complete */
3063 if (d
->buffer_filled
< offsetof(struct inotify_event
, name
))
3066 sz
= offsetof(struct inotify_event
, name
) + d
->buffer
.ev
.len
;
3067 if (d
->buffer_filled
< sz
)
3070 if (d
->buffer
.ev
.mask
& IN_Q_OVERFLOW
) {
3071 struct inode_data
*inode_data
;
3074 /* The queue overran, let's pass this event to all event sources connected to this inotify
3077 HASHMAP_FOREACH(inode_data
, d
->inodes
, i
) {
3080 LIST_FOREACH(inotify
.by_inode_data
, s
, inode_data
->event_sources
) {
3082 if (s
->enabled
== SD_EVENT_OFF
)
3085 r
= source_set_pending(s
, true);
3091 struct inode_data
*inode_data
;
3094 /* Find the inode object for this watch descriptor. If IN_IGNORED is set we also remove it from
3095 * our watch descriptor table. */
3096 if (d
->buffer
.ev
.mask
& IN_IGNORED
) {
3098 inode_data
= hashmap_remove(d
->wd
, INT_TO_PTR(d
->buffer
.ev
.wd
));
3100 event_inotify_data_drop(e
, d
, sz
);
3104 /* The watch descriptor was removed by the kernel, let's drop it here too */
3105 inode_data
->wd
= -1;
3107 inode_data
= hashmap_get(d
->wd
, INT_TO_PTR(d
->buffer
.ev
.wd
));
3109 event_inotify_data_drop(e
, d
, sz
);
3114 /* Trigger all event sources that are interested in these events. Also trigger all event
3115 * sources if IN_IGNORED or IN_UNMOUNT is set. */
3116 LIST_FOREACH(inotify
.by_inode_data
, s
, inode_data
->event_sources
) {
3118 if (s
->enabled
== SD_EVENT_OFF
)
3121 if ((d
->buffer
.ev
.mask
& (IN_IGNORED
|IN_UNMOUNT
)) == 0 &&
3122 (s
->inotify
.mask
& d
->buffer
.ev
.mask
& IN_ALL_EVENTS
) == 0)
3125 r
= source_set_pending(s
, true);
3131 /* Something pending now? If so, let's finish, otherwise let's read more. */
3132 if (d
->n_pending
> 0)
3139 static int process_inotify(sd_event
*e
) {
3140 struct inotify_data
*d
;
3145 LIST_FOREACH(buffered
, d
, e
->inotify_data_buffered
) {
3146 r
= event_inotify_data_process(e
, d
);
3156 static int source_dispatch(sd_event_source
*s
) {
3157 EventSourceType saved_type
;
3161 assert(s
->pending
|| s
->type
== SOURCE_EXIT
);
3163 /* Save the event source type, here, so that we still know it after the event callback which might invalidate
3165 saved_type
= s
->type
;
3167 if (!IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
3168 r
= source_set_pending(s
, false);
3173 if (s
->type
!= SOURCE_POST
) {
3177 /* If we execute a non-post source, let's mark all
3178 * post sources as pending */
3180 SET_FOREACH(z
, s
->event
->post_sources
, i
) {
3181 if (z
->enabled
== SD_EVENT_OFF
)
3184 r
= source_set_pending(z
, true);
3190 if (s
->enabled
== SD_EVENT_ONESHOT
) {
3191 r
= sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
3196 s
->dispatching
= true;
3201 r
= s
->io
.callback(s
, s
->io
.fd
, s
->io
.revents
, s
->userdata
);
3204 case SOURCE_TIME_REALTIME
:
3205 case SOURCE_TIME_BOOTTIME
:
3206 case SOURCE_TIME_MONOTONIC
:
3207 case SOURCE_TIME_REALTIME_ALARM
:
3208 case SOURCE_TIME_BOOTTIME_ALARM
:
3209 r
= s
->time
.callback(s
, s
->time
.next
, s
->userdata
);
3213 r
= s
->signal
.callback(s
, &s
->signal
.siginfo
, s
->userdata
);
3216 case SOURCE_CHILD
: {
3219 zombie
= IN_SET(s
->child
.siginfo
.si_code
, CLD_EXITED
, CLD_KILLED
, CLD_DUMPED
);
3221 r
= s
->child
.callback(s
, &s
->child
.siginfo
, s
->userdata
);
3223 /* Now, reap the PID for good. */
3225 (void) waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
, WNOHANG
|WEXITED
);
3226 s
->child
.waited
= true;
3233 r
= s
->defer
.callback(s
, s
->userdata
);
3237 r
= s
->post
.callback(s
, s
->userdata
);
3241 r
= s
->exit
.callback(s
, s
->userdata
);
3244 case SOURCE_INOTIFY
: {
3245 struct sd_event
*e
= s
->event
;
3246 struct inotify_data
*d
;
3249 assert(s
->inotify
.inode_data
);
3250 assert_se(d
= s
->inotify
.inode_data
->inotify_data
);
3252 assert(d
->buffer_filled
>= offsetof(struct inotify_event
, name
));
3253 sz
= offsetof(struct inotify_event
, name
) + d
->buffer
.ev
.len
;
3254 assert(d
->buffer_filled
>= sz
);
3256 r
= s
->inotify
.callback(s
, &d
->buffer
.ev
, s
->userdata
);
3258 /* When no event is pending anymore on this inotify object, then let's drop the event from the
3260 if (d
->n_pending
== 0)
3261 event_inotify_data_drop(e
, d
, sz
);
3266 case SOURCE_WATCHDOG
:
3267 case _SOURCE_EVENT_SOURCE_TYPE_MAX
:
3268 case _SOURCE_EVENT_SOURCE_TYPE_INVALID
:
3269 assert_not_reached("Wut? I shouldn't exist.");
3272 s
->dispatching
= false;
3275 log_debug_errno(r
, "Event source %s (type %s) returned error, disabling: %m",
3276 strna(s
->description
), event_source_type_to_string(saved_type
));
3281 sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
3286 static int event_prepare(sd_event
*e
) {
3294 s
= prioq_peek(e
->prepare
);
3295 if (!s
|| s
->prepare_iteration
== e
->iteration
|| s
->enabled
== SD_EVENT_OFF
)
3298 s
->prepare_iteration
= e
->iteration
;
3299 r
= prioq_reshuffle(e
->prepare
, s
, &s
->prepare_index
);
3305 s
->dispatching
= true;
3306 r
= s
->prepare(s
, s
->userdata
);
3307 s
->dispatching
= false;
3310 log_debug_errno(r
, "Prepare callback of event source %s (type %s) returned error, disabling: %m",
3311 strna(s
->description
), event_source_type_to_string(s
->type
));
3316 sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
3322 static int dispatch_exit(sd_event
*e
) {
3324 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
3329 p
= prioq_peek(e
->exit
);
3330 if (!p
|| p
->enabled
== SD_EVENT_OFF
) {
3331 e
->state
= SD_EVENT_FINISHED
;
3335 ref
= sd_event_ref(e
);
3337 e
->state
= SD_EVENT_EXITING
;
3338 r
= source_dispatch(p
);
3339 e
->state
= SD_EVENT_INITIAL
;
3343 static sd_event_source
* event_next_pending(sd_event
*e
) {
3348 p
= prioq_peek(e
->pending
);
3352 if (p
->enabled
== SD_EVENT_OFF
)
3358 static int arm_watchdog(sd_event
*e
) {
3359 struct itimerspec its
= {};
3364 assert(e
->watchdog_fd
>= 0);
3366 t
= sleep_between(e
,
3367 e
->watchdog_last
+ (e
->watchdog_period
/ 2),
3368 e
->watchdog_last
+ (e
->watchdog_period
* 3 / 4));
3370 timespec_store(&its
.it_value
, t
);
3372 /* Make sure we never set the watchdog to 0, which tells the
3373 * kernel to disable it. */
3374 if (its
.it_value
.tv_sec
== 0 && its
.it_value
.tv_nsec
== 0)
3375 its
.it_value
.tv_nsec
= 1;
3377 r
= timerfd_settime(e
->watchdog_fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
3384 static int process_watchdog(sd_event
*e
) {
3390 /* Don't notify watchdog too often */
3391 if (e
->watchdog_last
+ e
->watchdog_period
/ 4 > e
->timestamp
.monotonic
)
3394 sd_notify(false, "WATCHDOG=1");
3395 e
->watchdog_last
= e
->timestamp
.monotonic
;
3397 return arm_watchdog(e
);
3400 static void event_close_inode_data_fds(sd_event
*e
) {
3401 struct inode_data
*d
;
3405 /* Close the fds pointing to the inodes to watch now. We need to close them as they might otherwise pin
3406 * filesystems. But we can't close them right-away as we need them as long as the user still wants to make
3407 * adjustments to the even source, such as changing the priority (which requires us to remove and re-add a watch
3408 * for the inode). Hence, let's close them when entering the first iteration after they were added, as a
3411 while ((d
= e
->inode_data_to_close
)) {
3413 d
->fd
= safe_close(d
->fd
);
3415 LIST_REMOVE(to_close
, e
->inode_data_to_close
, d
);
3419 _public_
int sd_event_prepare(sd_event
*e
) {
3422 assert_return(e
, -EINVAL
);
3423 assert_return(e
= event_resolve(e
), -ENOPKG
);
3424 assert_return(!event_pid_changed(e
), -ECHILD
);
3425 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3426 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3428 /* Let's check that if we are a default event loop we are executed in the correct thread. We only do
3429 * this check here once, since gettid() is typically not cached, and thus want to minimize
3431 assert_return(!e
->default_event_ptr
|| e
->tid
== gettid(), -EREMOTEIO
);
3433 if (e
->exit_requested
)
3438 e
->state
= SD_EVENT_PREPARING
;
3439 r
= event_prepare(e
);
3440 e
->state
= SD_EVENT_INITIAL
;
3444 r
= event_arm_timer(e
, &e
->realtime
);
3448 r
= event_arm_timer(e
, &e
->boottime
);
3452 r
= event_arm_timer(e
, &e
->monotonic
);
3456 r
= event_arm_timer(e
, &e
->realtime_alarm
);
3460 r
= event_arm_timer(e
, &e
->boottime_alarm
);
3464 event_close_inode_data_fds(e
);
3466 if (event_next_pending(e
) || e
->need_process_child
)
3469 e
->state
= SD_EVENT_ARMED
;
3474 e
->state
= SD_EVENT_ARMED
;
3475 r
= sd_event_wait(e
, 0);
3477 e
->state
= SD_EVENT_ARMED
;
3482 _public_
int sd_event_wait(sd_event
*e
, uint64_t timeout
) {
3483 size_t event_queue_max
;
3486 assert_return(e
, -EINVAL
);
3487 assert_return(e
= event_resolve(e
), -ENOPKG
);
3488 assert_return(!event_pid_changed(e
), -ECHILD
);
3489 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3490 assert_return(e
->state
== SD_EVENT_ARMED
, -EBUSY
);
3492 if (e
->exit_requested
) {
3493 e
->state
= SD_EVENT_PENDING
;
3497 event_queue_max
= MAX(e
->n_sources
, 1u);
3498 if (!GREEDY_REALLOC(e
->event_queue
, e
->event_queue_allocated
, event_queue_max
))
3501 /* If we still have inotify data buffered, then query the other fds, but don't wait on it */
3502 if (e
->inotify_data_buffered
)
3505 m
= epoll_wait(e
->epoll_fd
, e
->event_queue
, event_queue_max
,
3506 timeout
== (uint64_t) -1 ? -1 : (int) DIV_ROUND_UP(timeout
, USEC_PER_MSEC
));
3508 if (errno
== EINTR
) {
3509 e
->state
= SD_EVENT_PENDING
;
3517 triple_timestamp_get(&e
->timestamp
);
3519 for (i
= 0; i
< m
; i
++) {
3521 if (e
->event_queue
[i
].data
.ptr
== INT_TO_PTR(SOURCE_WATCHDOG
))
3522 r
= flush_timer(e
, e
->watchdog_fd
, e
->event_queue
[i
].events
, NULL
);
3524 WakeupType
*t
= e
->event_queue
[i
].data
.ptr
;
3528 case WAKEUP_EVENT_SOURCE
: {
3529 sd_event_source
*s
= e
->event_queue
[i
].data
.ptr
;
3536 r
= process_io(e
, s
, e
->event_queue
[i
].events
);
3540 r
= process_pidfd(e
, s
, e
->event_queue
[i
].events
);
3544 assert_not_reached("Unexpected event source type");
3550 case WAKEUP_CLOCK_DATA
: {
3551 struct clock_data
*d
= e
->event_queue
[i
].data
.ptr
;
3555 r
= flush_timer(e
, d
->fd
, e
->event_queue
[i
].events
, &d
->next
);
3559 case WAKEUP_SIGNAL_DATA
:
3560 r
= process_signal(e
, e
->event_queue
[i
].data
.ptr
, e
->event_queue
[i
].events
);
3563 case WAKEUP_INOTIFY_DATA
:
3564 r
= event_inotify_data_read(e
, e
->event_queue
[i
].data
.ptr
, e
->event_queue
[i
].events
);
3568 assert_not_reached("Invalid wake-up pointer");
3575 r
= process_watchdog(e
);
3579 r
= process_timer(e
, e
->timestamp
.realtime
, &e
->realtime
);
3583 r
= process_timer(e
, e
->timestamp
.boottime
, &e
->boottime
);
3587 r
= process_timer(e
, e
->timestamp
.monotonic
, &e
->monotonic
);
3591 r
= process_timer(e
, e
->timestamp
.realtime
, &e
->realtime_alarm
);
3595 r
= process_timer(e
, e
->timestamp
.boottime
, &e
->boottime_alarm
);
3599 if (e
->need_process_child
) {
3600 r
= process_child(e
);
3605 r
= process_inotify(e
);
3609 if (event_next_pending(e
)) {
3610 e
->state
= SD_EVENT_PENDING
;
3618 e
->state
= SD_EVENT_INITIAL
;
3623 _public_
int sd_event_dispatch(sd_event
*e
) {
3627 assert_return(e
, -EINVAL
);
3628 assert_return(e
= event_resolve(e
), -ENOPKG
);
3629 assert_return(!event_pid_changed(e
), -ECHILD
);
3630 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3631 assert_return(e
->state
== SD_EVENT_PENDING
, -EBUSY
);
3633 if (e
->exit_requested
)
3634 return dispatch_exit(e
);
3636 p
= event_next_pending(e
);
3638 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
3640 ref
= sd_event_ref(e
);
3641 e
->state
= SD_EVENT_RUNNING
;
3642 r
= source_dispatch(p
);
3643 e
->state
= SD_EVENT_INITIAL
;
3647 e
->state
= SD_EVENT_INITIAL
;
3652 static void event_log_delays(sd_event
*e
) {
3653 char b
[ELEMENTSOF(e
->delays
) * DECIMAL_STR_MAX(unsigned) + 1], *p
;
3658 for (i
= 0; i
< ELEMENTSOF(e
->delays
); i
++) {
3659 l
= strpcpyf(&p
, l
, "%u ", e
->delays
[i
]);
3662 log_debug("Event loop iterations: %s", b
);
3665 _public_
int sd_event_run(sd_event
*e
, uint64_t timeout
) {
3668 assert_return(e
, -EINVAL
);
3669 assert_return(e
= event_resolve(e
), -ENOPKG
);
3670 assert_return(!event_pid_changed(e
), -ECHILD
);
3671 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3672 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3674 if (e
->profile_delays
&& e
->last_run
) {
3678 this_run
= now(CLOCK_MONOTONIC
);
3680 l
= u64log2(this_run
- e
->last_run
);
3681 assert(l
< sizeof(e
->delays
));
3684 if (this_run
- e
->last_log
>= 5*USEC_PER_SEC
) {
3685 event_log_delays(e
);
3686 e
->last_log
= this_run
;
3690 r
= sd_event_prepare(e
);
3692 /* There was nothing? Then wait... */
3693 r
= sd_event_wait(e
, timeout
);
3695 if (e
->profile_delays
)
3696 e
->last_run
= now(CLOCK_MONOTONIC
);
3699 /* There's something now, then let's dispatch it */
3700 r
= sd_event_dispatch(e
);
3710 _public_
int sd_event_loop(sd_event
*e
) {
3711 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
3714 assert_return(e
, -EINVAL
);
3715 assert_return(e
= event_resolve(e
), -ENOPKG
);
3716 assert_return(!event_pid_changed(e
), -ECHILD
);
3717 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3719 ref
= sd_event_ref(e
);
3721 while (e
->state
!= SD_EVENT_FINISHED
) {
3722 r
= sd_event_run(e
, (uint64_t) -1);
3727 return e
->exit_code
;
3730 _public_
int sd_event_get_fd(sd_event
*e
) {
3732 assert_return(e
, -EINVAL
);
3733 assert_return(e
= event_resolve(e
), -ENOPKG
);
3734 assert_return(!event_pid_changed(e
), -ECHILD
);
3739 _public_
int sd_event_get_state(sd_event
*e
) {
3740 assert_return(e
, -EINVAL
);
3741 assert_return(e
= event_resolve(e
), -ENOPKG
);
3742 assert_return(!event_pid_changed(e
), -ECHILD
);
3747 _public_
int sd_event_get_exit_code(sd_event
*e
, int *code
) {
3748 assert_return(e
, -EINVAL
);
3749 assert_return(e
= event_resolve(e
), -ENOPKG
);
3750 assert_return(code
, -EINVAL
);
3751 assert_return(!event_pid_changed(e
), -ECHILD
);
3753 if (!e
->exit_requested
)
3756 *code
= e
->exit_code
;
3760 _public_
int sd_event_exit(sd_event
*e
, int code
) {
3761 assert_return(e
, -EINVAL
);
3762 assert_return(e
= event_resolve(e
), -ENOPKG
);
3763 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3764 assert_return(!event_pid_changed(e
), -ECHILD
);
3766 e
->exit_requested
= true;
3767 e
->exit_code
= code
;
3772 _public_
int sd_event_now(sd_event
*e
, clockid_t clock
, uint64_t *usec
) {
3773 assert_return(e
, -EINVAL
);
3774 assert_return(e
= event_resolve(e
), -ENOPKG
);
3775 assert_return(usec
, -EINVAL
);
3776 assert_return(!event_pid_changed(e
), -ECHILD
);
3778 if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock
))
3781 /* Generate a clean error in case CLOCK_BOOTTIME is not available. Note that don't use clock_supported() here,
3782 * for a reason: there are systems where CLOCK_BOOTTIME is supported, but CLOCK_BOOTTIME_ALARM is not, but for
3783 * the purpose of getting the time this doesn't matter. */
3784 if (IN_SET(clock
, CLOCK_BOOTTIME
, CLOCK_BOOTTIME_ALARM
) && !clock_boottime_supported())
3787 if (!triple_timestamp_is_set(&e
->timestamp
)) {
3788 /* Implicitly fall back to now() if we never ran
3789 * before and thus have no cached time. */
3794 *usec
= triple_timestamp_by_clock(&e
->timestamp
, clock
);
3798 _public_
int sd_event_default(sd_event
**ret
) {
3803 return !!default_event
;
3805 if (default_event
) {
3806 *ret
= sd_event_ref(default_event
);
3810 r
= sd_event_new(&e
);
3814 e
->default_event_ptr
= &default_event
;
3822 _public_
int sd_event_get_tid(sd_event
*e
, pid_t
*tid
) {
3823 assert_return(e
, -EINVAL
);
3824 assert_return(e
= event_resolve(e
), -ENOPKG
);
3825 assert_return(tid
, -EINVAL
);
3826 assert_return(!event_pid_changed(e
), -ECHILD
);
3836 _public_
int sd_event_set_watchdog(sd_event
*e
, int b
) {
3839 assert_return(e
, -EINVAL
);
3840 assert_return(e
= event_resolve(e
), -ENOPKG
);
3841 assert_return(!event_pid_changed(e
), -ECHILD
);
3843 if (e
->watchdog
== !!b
)
3847 struct epoll_event ev
;
3849 r
= sd_watchdog_enabled(false, &e
->watchdog_period
);
3853 /* Issue first ping immediately */
3854 sd_notify(false, "WATCHDOG=1");
3855 e
->watchdog_last
= now(CLOCK_MONOTONIC
);
3857 e
->watchdog_fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
);
3858 if (e
->watchdog_fd
< 0)
3861 r
= arm_watchdog(e
);
3865 ev
= (struct epoll_event
) {
3867 .data
.ptr
= INT_TO_PTR(SOURCE_WATCHDOG
),
3870 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, e
->watchdog_fd
, &ev
);
3877 if (e
->watchdog_fd
>= 0) {
3878 (void) epoll_ctl(e
->epoll_fd
, EPOLL_CTL_DEL
, e
->watchdog_fd
, NULL
);
3879 e
->watchdog_fd
= safe_close(e
->watchdog_fd
);
3887 e
->watchdog_fd
= safe_close(e
->watchdog_fd
);
3891 _public_
int sd_event_get_watchdog(sd_event
*e
) {
3892 assert_return(e
, -EINVAL
);
3893 assert_return(e
= event_resolve(e
), -ENOPKG
);
3894 assert_return(!event_pid_changed(e
), -ECHILD
);
3899 _public_
int sd_event_get_iteration(sd_event
*e
, uint64_t *ret
) {
3900 assert_return(e
, -EINVAL
);
3901 assert_return(e
= event_resolve(e
), -ENOPKG
);
3902 assert_return(!event_pid_changed(e
), -ECHILD
);
3904 *ret
= e
->iteration
;
3908 _public_
int sd_event_source_set_destroy_callback(sd_event_source
*s
, sd_event_destroy_t callback
) {
3909 assert_return(s
, -EINVAL
);
3911 s
->destroy_callback
= callback
;
3915 _public_
int sd_event_source_get_destroy_callback(sd_event_source
*s
, sd_event_destroy_t
*ret
) {
3916 assert_return(s
, -EINVAL
);
3919 *ret
= s
->destroy_callback
;
3921 return !!s
->destroy_callback
;
3924 _public_
int sd_event_source_get_floating(sd_event_source
*s
) {
3925 assert_return(s
, -EINVAL
);
3930 _public_
int sd_event_source_set_floating(sd_event_source
*s
, int b
) {
3931 assert_return(s
, -EINVAL
);
3933 if (s
->floating
== !!b
)
3936 if (!s
->event
) /* Already disconnected */
3942 sd_event_source_ref(s
);
3943 sd_event_unref(s
->event
);
3945 sd_event_ref(s
->event
);
3946 sd_event_source_unref(s
);