1 /* SPDX-License-Identifier: LGPL-2.1+ */
4 #include <sys/timerfd.h>
11 #include "alloc-util.h"
12 #include "event-source.h"
18 #include "memory-util.h"
21 #include "process-util.h"
23 #include "signal-util.h"
24 #include "string-table.h"
25 #include "string-util.h"
26 #include "time-util.h"
28 #define DEFAULT_ACCURACY_USEC (250 * USEC_PER_MSEC)
30 static const char* const event_source_type_table
[_SOURCE_EVENT_SOURCE_TYPE_MAX
] = {
32 [SOURCE_TIME_REALTIME
] = "realtime",
33 [SOURCE_TIME_BOOTTIME
] = "bootime",
34 [SOURCE_TIME_MONOTONIC
] = "monotonic",
35 [SOURCE_TIME_REALTIME_ALARM
] = "realtime-alarm",
36 [SOURCE_TIME_BOOTTIME_ALARM
] = "boottime-alarm",
37 [SOURCE_SIGNAL
] = "signal",
38 [SOURCE_CHILD
] = "child",
39 [SOURCE_DEFER
] = "defer",
40 [SOURCE_POST
] = "post",
41 [SOURCE_EXIT
] = "exit",
42 [SOURCE_WATCHDOG
] = "watchdog",
43 [SOURCE_INOTIFY
] = "inotify",
46 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(event_source_type
, int);
48 #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)
59 /* timerfd_create() only supports these five clocks so far. We
60 * can add support for more clocks when the kernel learns to
61 * deal with them, too. */
62 struct clock_data realtime
;
63 struct clock_data boottime
;
64 struct clock_data monotonic
;
65 struct clock_data realtime_alarm
;
66 struct clock_data boottime_alarm
;
70 sd_event_source
**signal_sources
; /* indexed by signal number */
71 Hashmap
*signal_data
; /* indexed by priority */
73 Hashmap
*child_sources
;
74 unsigned n_enabled_child_sources
;
80 Hashmap
*inotify_data
; /* indexed by priority */
82 /* A list of inode structures that still have an fd open, that we need to close before the next loop iteration */
83 LIST_HEAD(struct inode_data
, inode_data_to_close
);
85 /* A list of inotify objects that already have events buffered which aren't processed yet */
86 LIST_HEAD(struct inotify_data
, inotify_data_buffered
);
91 triple_timestamp timestamp
;
94 bool exit_requested
:1;
95 bool need_process_child
:1;
97 bool profile_delays
:1;
102 sd_event
**default_event_ptr
;
104 usec_t watchdog_last
, watchdog_period
;
108 LIST_HEAD(sd_event_source
, sources
);
110 usec_t last_run
, last_log
;
111 unsigned delays
[sizeof(usec_t
) * 8];
114 static thread_local sd_event
*default_event
= NULL
;
116 static void source_disconnect(sd_event_source
*s
);
117 static void event_gc_inode_data(sd_event
*e
, struct inode_data
*d
);
119 static sd_event
*event_resolve(sd_event
*e
) {
120 return e
== SD_EVENT_DEFAULT
? default_event
: e
;
123 static int pending_prioq_compare(const void *a
, const void *b
) {
124 const sd_event_source
*x
= a
, *y
= b
;
130 /* Enabled ones first */
131 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
133 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
136 /* Lower priority values first */
137 r
= CMP(x
->priority
, y
->priority
);
141 /* Older entries first */
142 return CMP(x
->pending_iteration
, y
->pending_iteration
);
145 static int prepare_prioq_compare(const void *a
, const void *b
) {
146 const sd_event_source
*x
= a
, *y
= b
;
152 /* Enabled ones first */
153 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
155 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
158 /* Move most recently prepared ones last, so that we can stop
159 * preparing as soon as we hit one that has already been
160 * prepared in the current iteration */
161 r
= CMP(x
->prepare_iteration
, y
->prepare_iteration
);
165 /* Lower priority values first */
166 return CMP(x
->priority
, y
->priority
);
169 static int earliest_time_prioq_compare(const void *a
, const void *b
) {
170 const sd_event_source
*x
= a
, *y
= b
;
172 assert(EVENT_SOURCE_IS_TIME(x
->type
));
173 assert(x
->type
== y
->type
);
175 /* Enabled ones first */
176 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
178 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
181 /* Move the pending ones to the end */
182 if (!x
->pending
&& y
->pending
)
184 if (x
->pending
&& !y
->pending
)
188 return CMP(x
->time
.next
, y
->time
.next
);
191 static usec_t
time_event_source_latest(const sd_event_source
*s
) {
192 return usec_add(s
->time
.next
, s
->time
.accuracy
);
195 static int latest_time_prioq_compare(const void *a
, const void *b
) {
196 const sd_event_source
*x
= a
, *y
= b
;
198 assert(EVENT_SOURCE_IS_TIME(x
->type
));
199 assert(x
->type
== y
->type
);
201 /* Enabled ones first */
202 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
204 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
207 /* Move the pending ones to the end */
208 if (!x
->pending
&& y
->pending
)
210 if (x
->pending
&& !y
->pending
)
214 return CMP(time_event_source_latest(x
), time_event_source_latest(y
));
217 static int exit_prioq_compare(const void *a
, const void *b
) {
218 const sd_event_source
*x
= a
, *y
= b
;
220 assert(x
->type
== SOURCE_EXIT
);
221 assert(y
->type
== SOURCE_EXIT
);
223 /* Enabled ones first */
224 if (x
->enabled
!= SD_EVENT_OFF
&& y
->enabled
== SD_EVENT_OFF
)
226 if (x
->enabled
== SD_EVENT_OFF
&& y
->enabled
!= SD_EVENT_OFF
)
229 /* Lower priority values first */
230 return CMP(x
->priority
, y
->priority
);
233 static void free_clock_data(struct clock_data
*d
) {
235 assert(d
->wakeup
== WAKEUP_CLOCK_DATA
);
238 prioq_free(d
->earliest
);
239 prioq_free(d
->latest
);
242 static sd_event
*event_free(sd_event
*e
) {
247 while ((s
= e
->sources
)) {
249 source_disconnect(s
);
250 sd_event_source_unref(s
);
253 assert(e
->n_sources
== 0);
255 if (e
->default_event_ptr
)
256 *(e
->default_event_ptr
) = NULL
;
258 safe_close(e
->epoll_fd
);
259 safe_close(e
->watchdog_fd
);
261 free_clock_data(&e
->realtime
);
262 free_clock_data(&e
->boottime
);
263 free_clock_data(&e
->monotonic
);
264 free_clock_data(&e
->realtime_alarm
);
265 free_clock_data(&e
->boottime_alarm
);
267 prioq_free(e
->pending
);
268 prioq_free(e
->prepare
);
271 free(e
->signal_sources
);
272 hashmap_free(e
->signal_data
);
274 hashmap_free(e
->inotify_data
);
276 hashmap_free(e
->child_sources
);
277 set_free(e
->post_sources
);
282 _public_
int sd_event_new(sd_event
** ret
) {
286 assert_return(ret
, -EINVAL
);
288 e
= new(sd_event
, 1);
296 .realtime
.wakeup
= WAKEUP_CLOCK_DATA
,
298 .realtime
.next
= USEC_INFINITY
,
299 .boottime
.wakeup
= WAKEUP_CLOCK_DATA
,
301 .boottime
.next
= USEC_INFINITY
,
302 .monotonic
.wakeup
= WAKEUP_CLOCK_DATA
,
304 .monotonic
.next
= USEC_INFINITY
,
305 .realtime_alarm
.wakeup
= WAKEUP_CLOCK_DATA
,
306 .realtime_alarm
.fd
= -1,
307 .realtime_alarm
.next
= USEC_INFINITY
,
308 .boottime_alarm
.wakeup
= WAKEUP_CLOCK_DATA
,
309 .boottime_alarm
.fd
= -1,
310 .boottime_alarm
.next
= USEC_INFINITY
,
311 .perturb
= USEC_INFINITY
,
312 .original_pid
= getpid_cached(),
315 r
= prioq_ensure_allocated(&e
->pending
, pending_prioq_compare
);
319 e
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
320 if (e
->epoll_fd
< 0) {
325 e
->epoll_fd
= fd_move_above_stdio(e
->epoll_fd
);
327 if (secure_getenv("SD_EVENT_PROFILE_DELAYS")) {
328 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.");
329 e
->profile_delays
= true;
340 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event
, sd_event
, event_free
);
342 static bool event_pid_changed(sd_event
*e
) {
345 /* We don't support people creating an event loop and keeping
346 * it around over a fork(). Let's complain. */
348 return e
->original_pid
!= getpid_cached();
351 static void source_io_unregister(sd_event_source
*s
) {
355 assert(s
->type
== SOURCE_IO
);
357 if (event_pid_changed(s
->event
))
360 if (!s
->io
.registered
)
363 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_DEL
, s
->io
.fd
, NULL
);
365 log_debug_errno(errno
, "Failed to remove source %s (type %s) from epoll: %m",
366 strna(s
->description
), event_source_type_to_string(s
->type
));
368 s
->io
.registered
= false;
371 static int source_io_register(
376 struct epoll_event ev
;
380 assert(s
->type
== SOURCE_IO
);
381 assert(enabled
!= SD_EVENT_OFF
);
383 ev
= (struct epoll_event
) {
384 .events
= events
| (enabled
== SD_EVENT_ONESHOT
? EPOLLONESHOT
: 0),
388 if (s
->io
.registered
)
389 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_MOD
, s
->io
.fd
, &ev
);
391 r
= epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_ADD
, s
->io
.fd
, &ev
);
395 s
->io
.registered
= true;
400 static clockid_t
event_source_type_to_clock(EventSourceType t
) {
404 case SOURCE_TIME_REALTIME
:
405 return CLOCK_REALTIME
;
407 case SOURCE_TIME_BOOTTIME
:
408 return CLOCK_BOOTTIME
;
410 case SOURCE_TIME_MONOTONIC
:
411 return CLOCK_MONOTONIC
;
413 case SOURCE_TIME_REALTIME_ALARM
:
414 return CLOCK_REALTIME_ALARM
;
416 case SOURCE_TIME_BOOTTIME_ALARM
:
417 return CLOCK_BOOTTIME_ALARM
;
420 return (clockid_t
) -1;
424 static EventSourceType
clock_to_event_source_type(clockid_t clock
) {
429 return SOURCE_TIME_REALTIME
;
432 return SOURCE_TIME_BOOTTIME
;
434 case CLOCK_MONOTONIC
:
435 return SOURCE_TIME_MONOTONIC
;
437 case CLOCK_REALTIME_ALARM
:
438 return SOURCE_TIME_REALTIME_ALARM
;
440 case CLOCK_BOOTTIME_ALARM
:
441 return SOURCE_TIME_BOOTTIME_ALARM
;
444 return _SOURCE_EVENT_SOURCE_TYPE_INVALID
;
448 static struct clock_data
* event_get_clock_data(sd_event
*e
, EventSourceType t
) {
453 case SOURCE_TIME_REALTIME
:
456 case SOURCE_TIME_BOOTTIME
:
459 case SOURCE_TIME_MONOTONIC
:
460 return &e
->monotonic
;
462 case SOURCE_TIME_REALTIME_ALARM
:
463 return &e
->realtime_alarm
;
465 case SOURCE_TIME_BOOTTIME_ALARM
:
466 return &e
->boottime_alarm
;
473 static void event_free_signal_data(sd_event
*e
, struct signal_data
*d
) {
479 hashmap_remove(e
->signal_data
, &d
->priority
);
484 static int event_make_signal_data(
487 struct signal_data
**ret
) {
489 struct epoll_event ev
;
490 struct signal_data
*d
;
498 if (event_pid_changed(e
))
501 if (e
->signal_sources
&& e
->signal_sources
[sig
])
502 priority
= e
->signal_sources
[sig
]->priority
;
504 priority
= SD_EVENT_PRIORITY_NORMAL
;
506 d
= hashmap_get(e
->signal_data
, &priority
);
508 if (sigismember(&d
->sigset
, sig
) > 0) {
514 r
= hashmap_ensure_allocated(&e
->signal_data
, &uint64_hash_ops
);
518 d
= new(struct signal_data
, 1);
522 *d
= (struct signal_data
) {
523 .wakeup
= WAKEUP_SIGNAL_DATA
,
525 .priority
= priority
,
528 r
= hashmap_put(e
->signal_data
, &d
->priority
, d
);
538 assert_se(sigaddset(&ss_copy
, sig
) >= 0);
540 r
= signalfd(d
->fd
, &ss_copy
, SFD_NONBLOCK
|SFD_CLOEXEC
);
554 d
->fd
= fd_move_above_stdio(r
);
556 ev
= (struct epoll_event
) {
561 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, d
->fd
, &ev
);
574 event_free_signal_data(e
, d
);
579 static void event_unmask_signal_data(sd_event
*e
, struct signal_data
*d
, int sig
) {
583 /* Turns off the specified signal in the signal data
584 * object. If the signal mask of the object becomes empty that
587 if (sigismember(&d
->sigset
, sig
) == 0)
590 assert_se(sigdelset(&d
->sigset
, sig
) >= 0);
592 if (sigisemptyset(&d
->sigset
)) {
593 /* If all the mask is all-zero we can get rid of the structure */
594 event_free_signal_data(e
, d
);
600 if (signalfd(d
->fd
, &d
->sigset
, SFD_NONBLOCK
|SFD_CLOEXEC
) < 0)
601 log_debug_errno(errno
, "Failed to unset signal bit, ignoring: %m");
604 static void event_gc_signal_data(sd_event
*e
, const int64_t *priority
, int sig
) {
605 struct signal_data
*d
;
606 static const int64_t zero_priority
= 0;
610 /* Rechecks if the specified signal is still something we are
611 * interested in. If not, we'll unmask it, and possibly drop
612 * the signalfd for it. */
614 if (sig
== SIGCHLD
&&
615 e
->n_enabled_child_sources
> 0)
618 if (e
->signal_sources
&&
619 e
->signal_sources
[sig
] &&
620 e
->signal_sources
[sig
]->enabled
!= SD_EVENT_OFF
)
624 * The specified signal might be enabled in three different queues:
626 * 1) the one that belongs to the priority passed (if it is non-NULL)
627 * 2) the one that belongs to the priority of the event source of the signal (if there is one)
628 * 3) the 0 priority (to cover the SIGCHLD case)
630 * Hence, let's remove it from all three here.
634 d
= hashmap_get(e
->signal_data
, priority
);
636 event_unmask_signal_data(e
, d
, sig
);
639 if (e
->signal_sources
&& e
->signal_sources
[sig
]) {
640 d
= hashmap_get(e
->signal_data
, &e
->signal_sources
[sig
]->priority
);
642 event_unmask_signal_data(e
, d
, sig
);
645 d
= hashmap_get(e
->signal_data
, &zero_priority
);
647 event_unmask_signal_data(e
, d
, sig
);
650 static void source_disconnect(sd_event_source
*s
) {
658 assert(s
->event
->n_sources
> 0);
664 source_io_unregister(s
);
668 case SOURCE_TIME_REALTIME
:
669 case SOURCE_TIME_BOOTTIME
:
670 case SOURCE_TIME_MONOTONIC
:
671 case SOURCE_TIME_REALTIME_ALARM
:
672 case SOURCE_TIME_BOOTTIME_ALARM
: {
673 struct clock_data
*d
;
675 d
= event_get_clock_data(s
->event
, s
->type
);
678 prioq_remove(d
->earliest
, s
, &s
->time
.earliest_index
);
679 prioq_remove(d
->latest
, s
, &s
->time
.latest_index
);
680 d
->needs_rearm
= true;
685 if (s
->signal
.sig
> 0) {
687 if (s
->event
->signal_sources
)
688 s
->event
->signal_sources
[s
->signal
.sig
] = NULL
;
690 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
696 if (s
->child
.pid
> 0) {
697 if (s
->enabled
!= SD_EVENT_OFF
) {
698 assert(s
->event
->n_enabled_child_sources
> 0);
699 s
->event
->n_enabled_child_sources
--;
702 (void) hashmap_remove(s
->event
->child_sources
, PID_TO_PTR(s
->child
.pid
));
703 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
713 set_remove(s
->event
->post_sources
, s
);
717 prioq_remove(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
720 case SOURCE_INOTIFY
: {
721 struct inode_data
*inode_data
;
723 inode_data
= s
->inotify
.inode_data
;
725 struct inotify_data
*inotify_data
;
726 assert_se(inotify_data
= inode_data
->inotify_data
);
728 /* Detach this event source from the inode object */
729 LIST_REMOVE(inotify
.by_inode_data
, inode_data
->event_sources
, s
);
730 s
->inotify
.inode_data
= NULL
;
733 assert(inotify_data
->n_pending
> 0);
734 inotify_data
->n_pending
--;
737 /* Note that we don't reduce the inotify mask for the watch descriptor here if the inode is
738 * continued to being watched. That's because inotify doesn't really have an API for that: we
739 * can only change watch masks with access to the original inode either by fd or by path. But
740 * paths aren't stable, and keeping an O_PATH fd open all the time would mean wasting an fd
741 * continuously and keeping the mount busy which we can't really do. We could reconstruct the
742 * original inode from /proc/self/fdinfo/$INOTIFY_FD (as all watch descriptors are listed
743 * there), but given the need for open_by_handle_at() which is privileged and not universally
744 * available this would be quite an incomplete solution. Hence we go the other way, leave the
745 * mask set, even if it is not minimized now, and ignore all events we aren't interested in
746 * anymore after reception. Yes, this sucks, but … Linux … */
748 /* Maybe release the inode data (and its inotify) */
749 event_gc_inode_data(s
->event
, inode_data
);
756 assert_not_reached("Wut? I shouldn't exist.");
760 prioq_remove(s
->event
->pending
, s
, &s
->pending_index
);
763 prioq_remove(s
->event
->prepare
, s
, &s
->prepare_index
);
767 s
->type
= _SOURCE_EVENT_SOURCE_TYPE_INVALID
;
769 LIST_REMOVE(sources
, event
->sources
, s
);
773 sd_event_unref(event
);
776 static void source_free(sd_event_source
*s
) {
779 source_disconnect(s
);
781 if (s
->type
== SOURCE_IO
&& s
->io
.owned
)
782 s
->io
.fd
= safe_close(s
->io
.fd
);
784 if (s
->destroy_callback
)
785 s
->destroy_callback(s
->userdata
);
787 free(s
->description
);
790 DEFINE_TRIVIAL_CLEANUP_FUNC(sd_event_source
*, source_free
);
792 static int source_set_pending(sd_event_source
*s
, bool b
) {
796 assert(s
->type
!= SOURCE_EXIT
);
804 s
->pending_iteration
= s
->event
->iteration
;
806 r
= prioq_put(s
->event
->pending
, s
, &s
->pending_index
);
812 assert_se(prioq_remove(s
->event
->pending
, s
, &s
->pending_index
));
814 if (EVENT_SOURCE_IS_TIME(s
->type
)) {
815 struct clock_data
*d
;
817 d
= event_get_clock_data(s
->event
, s
->type
);
820 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
821 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
822 d
->needs_rearm
= true;
825 if (s
->type
== SOURCE_SIGNAL
&& !b
) {
826 struct signal_data
*d
;
828 d
= hashmap_get(s
->event
->signal_data
, &s
->priority
);
829 if (d
&& d
->current
== s
)
833 if (s
->type
== SOURCE_INOTIFY
) {
835 assert(s
->inotify
.inode_data
);
836 assert(s
->inotify
.inode_data
->inotify_data
);
839 s
->inotify
.inode_data
->inotify_data
->n_pending
++;
841 assert(s
->inotify
.inode_data
->inotify_data
->n_pending
> 0);
842 s
->inotify
.inode_data
->inotify_data
->n_pending
--;
849 static sd_event_source
*source_new(sd_event
*e
, bool floating
, EventSourceType type
) {
854 s
= new(sd_event_source
, 1);
858 *s
= (struct sd_event_source
) {
861 .floating
= floating
,
863 .pending_index
= PRIOQ_IDX_NULL
,
864 .prepare_index
= PRIOQ_IDX_NULL
,
870 LIST_PREPEND(sources
, e
->sources
, s
);
876 _public_
int sd_event_add_io(
878 sd_event_source
**ret
,
881 sd_event_io_handler_t callback
,
884 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
887 assert_return(e
, -EINVAL
);
888 assert_return(e
= event_resolve(e
), -ENOPKG
);
889 assert_return(fd
>= 0, -EBADF
);
890 assert_return(!(events
& ~(EPOLLIN
|EPOLLOUT
|EPOLLRDHUP
|EPOLLPRI
|EPOLLERR
|EPOLLHUP
|EPOLLET
)), -EINVAL
);
891 assert_return(callback
, -EINVAL
);
892 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
893 assert_return(!event_pid_changed(e
), -ECHILD
);
895 s
= source_new(e
, !ret
, SOURCE_IO
);
899 s
->wakeup
= WAKEUP_EVENT_SOURCE
;
901 s
->io
.events
= events
;
902 s
->io
.callback
= callback
;
903 s
->userdata
= userdata
;
904 s
->enabled
= SD_EVENT_ON
;
906 r
= source_io_register(s
, s
->enabled
, events
);
917 static void initialize_perturb(sd_event
*e
) {
918 sd_id128_t bootid
= {};
920 /* When we sleep for longer, we try to realign the wakeup to
921 the same time within each minute/second/250ms, so that
922 events all across the system can be coalesced into a single
923 CPU wakeup. However, let's take some system-specific
924 randomness for this value, so that in a network of systems
925 with synced clocks timer events are distributed a
926 bit. Here, we calculate a perturbation usec offset from the
929 if (_likely_(e
->perturb
!= USEC_INFINITY
))
932 if (sd_id128_get_boot(&bootid
) >= 0)
933 e
->perturb
= (bootid
.qwords
[0] ^ bootid
.qwords
[1]) % USEC_PER_MINUTE
;
936 static int event_setup_timer_fd(
938 struct clock_data
*d
,
941 struct epoll_event ev
;
947 if (_likely_(d
->fd
>= 0))
950 fd
= timerfd_create(clock
, TFD_NONBLOCK
|TFD_CLOEXEC
);
954 fd
= fd_move_above_stdio(fd
);
956 ev
= (struct epoll_event
) {
961 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
);
971 static int time_exit_callback(sd_event_source
*s
, uint64_t usec
, void *userdata
) {
974 return sd_event_exit(sd_event_source_get_event(s
), PTR_TO_INT(userdata
));
977 _public_
int sd_event_add_time(
979 sd_event_source
**ret
,
983 sd_event_time_handler_t callback
,
986 EventSourceType type
;
987 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
988 struct clock_data
*d
;
991 assert_return(e
, -EINVAL
);
992 assert_return(e
= event_resolve(e
), -ENOPKG
);
993 assert_return(accuracy
!= (uint64_t) -1, -EINVAL
);
994 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
995 assert_return(!event_pid_changed(e
), -ECHILD
);
997 if (!clock_supported(clock
)) /* Checks whether the kernel supports the clock */
1000 type
= clock_to_event_source_type(clock
); /* checks whether sd-event supports this clock */
1005 callback
= time_exit_callback
;
1007 d
= event_get_clock_data(e
, type
);
1010 r
= prioq_ensure_allocated(&d
->earliest
, earliest_time_prioq_compare
);
1014 r
= prioq_ensure_allocated(&d
->latest
, latest_time_prioq_compare
);
1019 r
= event_setup_timer_fd(e
, d
, clock
);
1024 s
= source_new(e
, !ret
, type
);
1028 s
->time
.next
= usec
;
1029 s
->time
.accuracy
= accuracy
== 0 ? DEFAULT_ACCURACY_USEC
: accuracy
;
1030 s
->time
.callback
= callback
;
1031 s
->time
.earliest_index
= s
->time
.latest_index
= PRIOQ_IDX_NULL
;
1032 s
->userdata
= userdata
;
1033 s
->enabled
= SD_EVENT_ONESHOT
;
1035 d
->needs_rearm
= true;
1037 r
= prioq_put(d
->earliest
, s
, &s
->time
.earliest_index
);
1041 r
= prioq_put(d
->latest
, s
, &s
->time
.latest_index
);
1052 static int signal_exit_callback(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1055 return sd_event_exit(sd_event_source_get_event(s
), PTR_TO_INT(userdata
));
1058 _public_
int sd_event_add_signal(
1060 sd_event_source
**ret
,
1062 sd_event_signal_handler_t callback
,
1065 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1066 struct signal_data
*d
;
1070 assert_return(e
, -EINVAL
);
1071 assert_return(e
= event_resolve(e
), -ENOPKG
);
1072 assert_return(SIGNAL_VALID(sig
), -EINVAL
);
1073 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1074 assert_return(!event_pid_changed(e
), -ECHILD
);
1077 callback
= signal_exit_callback
;
1079 r
= pthread_sigmask(SIG_SETMASK
, NULL
, &ss
);
1083 if (!sigismember(&ss
, sig
))
1086 if (!e
->signal_sources
) {
1087 e
->signal_sources
= new0(sd_event_source
*, _NSIG
);
1088 if (!e
->signal_sources
)
1090 } else if (e
->signal_sources
[sig
])
1093 s
= source_new(e
, !ret
, SOURCE_SIGNAL
);
1097 s
->signal
.sig
= sig
;
1098 s
->signal
.callback
= callback
;
1099 s
->userdata
= userdata
;
1100 s
->enabled
= SD_EVENT_ON
;
1102 e
->signal_sources
[sig
] = s
;
1104 r
= event_make_signal_data(e
, sig
, &d
);
1108 /* Use the signal name as description for the event source by default */
1109 (void) sd_event_source_set_description(s
, signal_to_string(sig
));
1118 _public_
int sd_event_add_child(
1120 sd_event_source
**ret
,
1123 sd_event_child_handler_t callback
,
1126 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1129 assert_return(e
, -EINVAL
);
1130 assert_return(e
= event_resolve(e
), -ENOPKG
);
1131 assert_return(pid
> 1, -EINVAL
);
1132 assert_return(!(options
& ~(WEXITED
|WSTOPPED
|WCONTINUED
)), -EINVAL
);
1133 assert_return(options
!= 0, -EINVAL
);
1134 assert_return(callback
, -EINVAL
);
1135 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1136 assert_return(!event_pid_changed(e
), -ECHILD
);
1138 r
= hashmap_ensure_allocated(&e
->child_sources
, NULL
);
1142 if (hashmap_contains(e
->child_sources
, PID_TO_PTR(pid
)))
1145 s
= source_new(e
, !ret
, SOURCE_CHILD
);
1150 s
->child
.options
= options
;
1151 s
->child
.callback
= callback
;
1152 s
->userdata
= userdata
;
1153 s
->enabled
= SD_EVENT_ONESHOT
;
1155 r
= hashmap_put(e
->child_sources
, PID_TO_PTR(pid
), s
);
1159 e
->n_enabled_child_sources
++;
1161 r
= event_make_signal_data(e
, SIGCHLD
, NULL
);
1163 e
->n_enabled_child_sources
--;
1167 e
->need_process_child
= true;
1176 _public_
int sd_event_add_defer(
1178 sd_event_source
**ret
,
1179 sd_event_handler_t callback
,
1182 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1185 assert_return(e
, -EINVAL
);
1186 assert_return(e
= event_resolve(e
), -ENOPKG
);
1187 assert_return(callback
, -EINVAL
);
1188 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1189 assert_return(!event_pid_changed(e
), -ECHILD
);
1191 s
= source_new(e
, !ret
, SOURCE_DEFER
);
1195 s
->defer
.callback
= callback
;
1196 s
->userdata
= userdata
;
1197 s
->enabled
= SD_EVENT_ONESHOT
;
1199 r
= source_set_pending(s
, true);
1210 _public_
int sd_event_add_post(
1212 sd_event_source
**ret
,
1213 sd_event_handler_t callback
,
1216 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1219 assert_return(e
, -EINVAL
);
1220 assert_return(e
= event_resolve(e
), -ENOPKG
);
1221 assert_return(callback
, -EINVAL
);
1222 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1223 assert_return(!event_pid_changed(e
), -ECHILD
);
1225 r
= set_ensure_allocated(&e
->post_sources
, NULL
);
1229 s
= source_new(e
, !ret
, SOURCE_POST
);
1233 s
->post
.callback
= callback
;
1234 s
->userdata
= userdata
;
1235 s
->enabled
= SD_EVENT_ON
;
1237 r
= set_put(e
->post_sources
, s
);
1248 _public_
int sd_event_add_exit(
1250 sd_event_source
**ret
,
1251 sd_event_handler_t callback
,
1254 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1257 assert_return(e
, -EINVAL
);
1258 assert_return(e
= event_resolve(e
), -ENOPKG
);
1259 assert_return(callback
, -EINVAL
);
1260 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1261 assert_return(!event_pid_changed(e
), -ECHILD
);
1263 r
= prioq_ensure_allocated(&e
->exit
, exit_prioq_compare
);
1267 s
= source_new(e
, !ret
, SOURCE_EXIT
);
1271 s
->exit
.callback
= callback
;
1272 s
->userdata
= userdata
;
1273 s
->exit
.prioq_index
= PRIOQ_IDX_NULL
;
1274 s
->enabled
= SD_EVENT_ONESHOT
;
1276 r
= prioq_put(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
1287 static void event_free_inotify_data(sd_event
*e
, struct inotify_data
*d
) {
1293 assert(hashmap_isempty(d
->inodes
));
1294 assert(hashmap_isempty(d
->wd
));
1296 if (d
->buffer_filled
> 0)
1297 LIST_REMOVE(buffered
, e
->inotify_data_buffered
, d
);
1299 hashmap_free(d
->inodes
);
1300 hashmap_free(d
->wd
);
1302 assert_se(hashmap_remove(e
->inotify_data
, &d
->priority
) == d
);
1305 if (epoll_ctl(e
->epoll_fd
, EPOLL_CTL_DEL
, d
->fd
, NULL
) < 0)
1306 log_debug_errno(errno
, "Failed to remove inotify fd from epoll, ignoring: %m");
1313 static int event_make_inotify_data(
1316 struct inotify_data
**ret
) {
1318 _cleanup_close_
int fd
= -1;
1319 struct inotify_data
*d
;
1320 struct epoll_event ev
;
1325 d
= hashmap_get(e
->inotify_data
, &priority
);
1332 fd
= inotify_init1(IN_NONBLOCK
|O_CLOEXEC
);
1336 fd
= fd_move_above_stdio(fd
);
1338 r
= hashmap_ensure_allocated(&e
->inotify_data
, &uint64_hash_ops
);
1342 d
= new(struct inotify_data
, 1);
1346 *d
= (struct inotify_data
) {
1347 .wakeup
= WAKEUP_INOTIFY_DATA
,
1349 .priority
= priority
,
1352 r
= hashmap_put(e
->inotify_data
, &d
->priority
, d
);
1354 d
->fd
= safe_close(d
->fd
);
1359 ev
= (struct epoll_event
) {
1364 if (epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, d
->fd
, &ev
) < 0) {
1366 d
->fd
= safe_close(d
->fd
); /* let's close this ourselves, as event_free_inotify_data() would otherwise
1367 * remove the fd from the epoll first, which we don't want as we couldn't
1368 * add it in the first place. */
1369 event_free_inotify_data(e
, d
);
1379 static int inode_data_compare(const struct inode_data
*x
, const struct inode_data
*y
) {
1385 r
= CMP(x
->dev
, y
->dev
);
1389 return CMP(x
->ino
, y
->ino
);
1392 static void inode_data_hash_func(const struct inode_data
*d
, struct siphash
*state
) {
1395 siphash24_compress(&d
->dev
, sizeof(d
->dev
), state
);
1396 siphash24_compress(&d
->ino
, sizeof(d
->ino
), state
);
1399 DEFINE_PRIVATE_HASH_OPS(inode_data_hash_ops
, struct inode_data
, inode_data_hash_func
, inode_data_compare
);
1401 static void event_free_inode_data(
1403 struct inode_data
*d
) {
1410 assert(!d
->event_sources
);
1413 LIST_REMOVE(to_close
, e
->inode_data_to_close
, d
);
1417 if (d
->inotify_data
) {
1420 if (d
->inotify_data
->fd
>= 0) {
1421 /* So here's a problem. At the time this runs the watch descriptor might already be
1422 * invalidated, because an IN_IGNORED event might be queued right the moment we enter
1423 * the syscall. Hence, whenever we get EINVAL, ignore it entirely, since it's a very
1424 * likely case to happen. */
1426 if (inotify_rm_watch(d
->inotify_data
->fd
, d
->wd
) < 0 && errno
!= EINVAL
)
1427 log_debug_errno(errno
, "Failed to remove watch descriptor %i from inotify, ignoring: %m", d
->wd
);
1430 assert_se(hashmap_remove(d
->inotify_data
->wd
, INT_TO_PTR(d
->wd
)) == d
);
1433 assert_se(hashmap_remove(d
->inotify_data
->inodes
, d
) == d
);
1439 static void event_gc_inode_data(
1441 struct inode_data
*d
) {
1443 struct inotify_data
*inotify_data
;
1450 if (d
->event_sources
)
1453 inotify_data
= d
->inotify_data
;
1454 event_free_inode_data(e
, d
);
1456 if (inotify_data
&& hashmap_isempty(inotify_data
->inodes
))
1457 event_free_inotify_data(e
, inotify_data
);
1460 static int event_make_inode_data(
1462 struct inotify_data
*inotify_data
,
1465 struct inode_data
**ret
) {
1467 struct inode_data
*d
, key
;
1471 assert(inotify_data
);
1473 key
= (struct inode_data
) {
1478 d
= hashmap_get(inotify_data
->inodes
, &key
);
1486 r
= hashmap_ensure_allocated(&inotify_data
->inodes
, &inode_data_hash_ops
);
1490 d
= new(struct inode_data
, 1);
1494 *d
= (struct inode_data
) {
1499 .inotify_data
= inotify_data
,
1502 r
= hashmap_put(inotify_data
->inodes
, d
, d
);
1514 static uint32_t inode_data_determine_mask(struct inode_data
*d
) {
1515 bool excl_unlink
= true;
1516 uint32_t combined
= 0;
1521 /* Combines the watch masks of all event sources watching this inode. We generally just OR them together, but
1522 * the IN_EXCL_UNLINK flag is ANDed instead.
1524 * Note that we add all sources to the mask here, regardless whether enabled, disabled or oneshot. That's
1525 * because we cannot change the mask anymore after the event source was created once, since the kernel has no
1526 * API for that. Hence we need to subscribe to the maximum mask we ever might be interested in, and suppress
1527 * events we don't care for client-side. */
1529 LIST_FOREACH(inotify
.by_inode_data
, s
, d
->event_sources
) {
1531 if ((s
->inotify
.mask
& IN_EXCL_UNLINK
) == 0)
1532 excl_unlink
= false;
1534 combined
|= s
->inotify
.mask
;
1537 return (combined
& ~(IN_ONESHOT
|IN_DONT_FOLLOW
|IN_ONLYDIR
|IN_EXCL_UNLINK
)) | (excl_unlink
? IN_EXCL_UNLINK
: 0);
1540 static int inode_data_realize_watch(sd_event
*e
, struct inode_data
*d
) {
1541 uint32_t combined_mask
;
1547 combined_mask
= inode_data_determine_mask(d
);
1549 if (d
->wd
>= 0 && combined_mask
== d
->combined_mask
)
1552 r
= hashmap_ensure_allocated(&d
->inotify_data
->wd
, NULL
);
1556 wd
= inotify_add_watch_fd(d
->inotify_data
->fd
, d
->fd
, combined_mask
);
1561 r
= hashmap_put(d
->inotify_data
->wd
, INT_TO_PTR(wd
), d
);
1563 (void) inotify_rm_watch(d
->inotify_data
->fd
, wd
);
1569 } else if (d
->wd
!= wd
) {
1571 log_debug("Weird, the watch descriptor we already knew for this inode changed?");
1572 (void) inotify_rm_watch(d
->fd
, wd
);
1576 d
->combined_mask
= combined_mask
;
1580 _public_
int sd_event_add_inotify(
1582 sd_event_source
**ret
,
1585 sd_event_inotify_handler_t callback
,
1588 struct inotify_data
*inotify_data
= NULL
;
1589 struct inode_data
*inode_data
= NULL
;
1590 _cleanup_close_
int fd
= -1;
1591 _cleanup_(source_freep
) sd_event_source
*s
= NULL
;
1595 assert_return(e
, -EINVAL
);
1596 assert_return(e
= event_resolve(e
), -ENOPKG
);
1597 assert_return(path
, -EINVAL
);
1598 assert_return(callback
, -EINVAL
);
1599 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1600 assert_return(!event_pid_changed(e
), -ECHILD
);
1602 /* Refuse IN_MASK_ADD since we coalesce watches on the same inode, and hence really don't want to merge
1603 * masks. Or in other words, this whole code exists only to manage IN_MASK_ADD type operations for you, hence
1604 * the user can't use them for us. */
1605 if (mask
& IN_MASK_ADD
)
1608 fd
= open(path
, O_PATH
|O_CLOEXEC
|
1609 (mask
& IN_ONLYDIR
? O_DIRECTORY
: 0)|
1610 (mask
& IN_DONT_FOLLOW
? O_NOFOLLOW
: 0));
1614 if (fstat(fd
, &st
) < 0)
1617 s
= source_new(e
, !ret
, SOURCE_INOTIFY
);
1621 s
->enabled
= mask
& IN_ONESHOT
? SD_EVENT_ONESHOT
: SD_EVENT_ON
;
1622 s
->inotify
.mask
= mask
;
1623 s
->inotify
.callback
= callback
;
1624 s
->userdata
= userdata
;
1626 /* Allocate an inotify object for this priority, and an inode object within it */
1627 r
= event_make_inotify_data(e
, SD_EVENT_PRIORITY_NORMAL
, &inotify_data
);
1631 r
= event_make_inode_data(e
, inotify_data
, st
.st_dev
, st
.st_ino
, &inode_data
);
1633 event_free_inotify_data(e
, inotify_data
);
1637 /* Keep the O_PATH fd around until the first iteration of the loop, so that we can still change the priority of
1638 * the event source, until then, for which we need the original inode. */
1639 if (inode_data
->fd
< 0) {
1640 inode_data
->fd
= TAKE_FD(fd
);
1641 LIST_PREPEND(to_close
, e
->inode_data_to_close
, inode_data
);
1644 /* Link our event source to the inode data object */
1645 LIST_PREPEND(inotify
.by_inode_data
, inode_data
->event_sources
, s
);
1646 s
->inotify
.inode_data
= inode_data
;
1648 /* Actually realize the watch now */
1649 r
= inode_data_realize_watch(e
, inode_data
);
1653 (void) sd_event_source_set_description(s
, path
);
1662 static sd_event_source
* event_source_free(sd_event_source
*s
) {
1666 /* Here's a special hack: when we are called from a
1667 * dispatch handler we won't free the event source
1668 * immediately, but we will detach the fd from the
1669 * epoll. This way it is safe for the caller to unref
1670 * the event source and immediately close the fd, but
1671 * we still retain a valid event source object after
1674 if (s
->dispatching
) {
1675 if (s
->type
== SOURCE_IO
)
1676 source_io_unregister(s
);
1678 source_disconnect(s
);
1685 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(sd_event_source
, sd_event_source
, event_source_free
);
1687 _public_
int sd_event_source_set_description(sd_event_source
*s
, const char *description
) {
1688 assert_return(s
, -EINVAL
);
1689 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1691 return free_and_strdup(&s
->description
, description
);
1694 _public_
int sd_event_source_get_description(sd_event_source
*s
, const char **description
) {
1695 assert_return(s
, -EINVAL
);
1696 assert_return(description
, -EINVAL
);
1697 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1699 if (!s
->description
)
1702 *description
= s
->description
;
1706 _public_ sd_event
*sd_event_source_get_event(sd_event_source
*s
) {
1707 assert_return(s
, NULL
);
1712 _public_
int sd_event_source_get_pending(sd_event_source
*s
) {
1713 assert_return(s
, -EINVAL
);
1714 assert_return(s
->type
!= SOURCE_EXIT
, -EDOM
);
1715 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1716 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1721 _public_
int sd_event_source_get_io_fd(sd_event_source
*s
) {
1722 assert_return(s
, -EINVAL
);
1723 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1724 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1729 _public_
int sd_event_source_set_io_fd(sd_event_source
*s
, int fd
) {
1732 assert_return(s
, -EINVAL
);
1733 assert_return(fd
>= 0, -EBADF
);
1734 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1735 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1740 if (s
->enabled
== SD_EVENT_OFF
) {
1742 s
->io
.registered
= false;
1746 saved_fd
= s
->io
.fd
;
1747 assert(s
->io
.registered
);
1750 s
->io
.registered
= false;
1752 r
= source_io_register(s
, s
->enabled
, s
->io
.events
);
1754 s
->io
.fd
= saved_fd
;
1755 s
->io
.registered
= true;
1759 epoll_ctl(s
->event
->epoll_fd
, EPOLL_CTL_DEL
, saved_fd
, NULL
);
1765 _public_
int sd_event_source_get_io_fd_own(sd_event_source
*s
) {
1766 assert_return(s
, -EINVAL
);
1767 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1772 _public_
int sd_event_source_set_io_fd_own(sd_event_source
*s
, int own
) {
1773 assert_return(s
, -EINVAL
);
1774 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1780 _public_
int sd_event_source_get_io_events(sd_event_source
*s
, uint32_t* events
) {
1781 assert_return(s
, -EINVAL
);
1782 assert_return(events
, -EINVAL
);
1783 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1784 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1786 *events
= s
->io
.events
;
1790 _public_
int sd_event_source_set_io_events(sd_event_source
*s
, uint32_t events
) {
1793 assert_return(s
, -EINVAL
);
1794 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1795 assert_return(!(events
& ~(EPOLLIN
|EPOLLOUT
|EPOLLRDHUP
|EPOLLPRI
|EPOLLERR
|EPOLLHUP
|EPOLLET
)), -EINVAL
);
1796 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1797 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1799 /* edge-triggered updates are never skipped, so we can reset edges */
1800 if (s
->io
.events
== events
&& !(events
& EPOLLET
))
1803 r
= source_set_pending(s
, false);
1807 if (s
->enabled
!= SD_EVENT_OFF
) {
1808 r
= source_io_register(s
, s
->enabled
, events
);
1813 s
->io
.events
= events
;
1818 _public_
int sd_event_source_get_io_revents(sd_event_source
*s
, uint32_t* revents
) {
1819 assert_return(s
, -EINVAL
);
1820 assert_return(revents
, -EINVAL
);
1821 assert_return(s
->type
== SOURCE_IO
, -EDOM
);
1822 assert_return(s
->pending
, -ENODATA
);
1823 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1825 *revents
= s
->io
.revents
;
1829 _public_
int sd_event_source_get_signal(sd_event_source
*s
) {
1830 assert_return(s
, -EINVAL
);
1831 assert_return(s
->type
== SOURCE_SIGNAL
, -EDOM
);
1832 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1834 return s
->signal
.sig
;
1837 _public_
int sd_event_source_get_priority(sd_event_source
*s
, int64_t *priority
) {
1838 assert_return(s
, -EINVAL
);
1839 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1841 *priority
= s
->priority
;
1845 _public_
int sd_event_source_set_priority(sd_event_source
*s
, int64_t priority
) {
1846 bool rm_inotify
= false, rm_inode
= false;
1847 struct inotify_data
*new_inotify_data
= NULL
;
1848 struct inode_data
*new_inode_data
= NULL
;
1851 assert_return(s
, -EINVAL
);
1852 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
1853 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1855 if (s
->priority
== priority
)
1858 if (s
->type
== SOURCE_INOTIFY
) {
1859 struct inode_data
*old_inode_data
;
1861 assert(s
->inotify
.inode_data
);
1862 old_inode_data
= s
->inotify
.inode_data
;
1864 /* We need the original fd to change the priority. If we don't have it we can't change the priority,
1865 * anymore. Note that we close any fds when entering the next event loop iteration, i.e. for inotify
1866 * events we allow priority changes only until the first following iteration. */
1867 if (old_inode_data
->fd
< 0)
1870 r
= event_make_inotify_data(s
->event
, priority
, &new_inotify_data
);
1875 r
= event_make_inode_data(s
->event
, new_inotify_data
, old_inode_data
->dev
, old_inode_data
->ino
, &new_inode_data
);
1880 if (new_inode_data
->fd
< 0) {
1881 /* Duplicate the fd for the new inode object if we don't have any yet */
1882 new_inode_data
->fd
= fcntl(old_inode_data
->fd
, F_DUPFD_CLOEXEC
, 3);
1883 if (new_inode_data
->fd
< 0) {
1888 LIST_PREPEND(to_close
, s
->event
->inode_data_to_close
, new_inode_data
);
1891 /* Move the event source to the new inode data structure */
1892 LIST_REMOVE(inotify
.by_inode_data
, old_inode_data
->event_sources
, s
);
1893 LIST_PREPEND(inotify
.by_inode_data
, new_inode_data
->event_sources
, s
);
1894 s
->inotify
.inode_data
= new_inode_data
;
1896 /* Now create the new watch */
1897 r
= inode_data_realize_watch(s
->event
, new_inode_data
);
1900 LIST_REMOVE(inotify
.by_inode_data
, new_inode_data
->event_sources
, s
);
1901 LIST_PREPEND(inotify
.by_inode_data
, old_inode_data
->event_sources
, s
);
1902 s
->inotify
.inode_data
= old_inode_data
;
1906 s
->priority
= priority
;
1908 event_gc_inode_data(s
->event
, old_inode_data
);
1910 } else if (s
->type
== SOURCE_SIGNAL
&& s
->enabled
!= SD_EVENT_OFF
) {
1911 struct signal_data
*old
, *d
;
1913 /* Move us from the signalfd belonging to the old
1914 * priority to the signalfd of the new priority */
1916 assert_se(old
= hashmap_get(s
->event
->signal_data
, &s
->priority
));
1918 s
->priority
= priority
;
1920 r
= event_make_signal_data(s
->event
, s
->signal
.sig
, &d
);
1922 s
->priority
= old
->priority
;
1926 event_unmask_signal_data(s
->event
, old
, s
->signal
.sig
);
1928 s
->priority
= priority
;
1931 prioq_reshuffle(s
->event
->pending
, s
, &s
->pending_index
);
1934 prioq_reshuffle(s
->event
->prepare
, s
, &s
->prepare_index
);
1936 if (s
->type
== SOURCE_EXIT
)
1937 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
1943 event_free_inode_data(s
->event
, new_inode_data
);
1946 event_free_inotify_data(s
->event
, new_inotify_data
);
1951 _public_
int sd_event_source_get_enabled(sd_event_source
*s
, int *m
) {
1952 assert_return(s
, -EINVAL
);
1953 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1957 return s
->enabled
!= SD_EVENT_OFF
;
1960 _public_
int sd_event_source_set_enabled(sd_event_source
*s
, int m
) {
1963 assert_return(s
, -EINVAL
);
1964 assert_return(IN_SET(m
, SD_EVENT_OFF
, SD_EVENT_ON
, SD_EVENT_ONESHOT
), -EINVAL
);
1965 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
1967 /* If we are dead anyway, we are fine with turning off
1968 * sources, but everything else needs to fail. */
1969 if (s
->event
->state
== SD_EVENT_FINISHED
)
1970 return m
== SD_EVENT_OFF
? 0 : -ESTALE
;
1972 if (s
->enabled
== m
)
1975 if (m
== SD_EVENT_OFF
) {
1977 /* Unset the pending flag when this event source is disabled */
1978 if (!IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
1979 r
= source_set_pending(s
, false);
1987 source_io_unregister(s
);
1991 case SOURCE_TIME_REALTIME
:
1992 case SOURCE_TIME_BOOTTIME
:
1993 case SOURCE_TIME_MONOTONIC
:
1994 case SOURCE_TIME_REALTIME_ALARM
:
1995 case SOURCE_TIME_BOOTTIME_ALARM
: {
1996 struct clock_data
*d
;
1999 d
= event_get_clock_data(s
->event
, s
->type
);
2002 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2003 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2004 d
->needs_rearm
= true;
2011 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
2017 assert(s
->event
->n_enabled_child_sources
> 0);
2018 s
->event
->n_enabled_child_sources
--;
2020 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
2025 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
2030 case SOURCE_INOTIFY
:
2035 assert_not_reached("Wut? I shouldn't exist.");
2040 /* Unset the pending flag when this event source is enabled */
2041 if (s
->enabled
== SD_EVENT_OFF
&& !IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
2042 r
= source_set_pending(s
, false);
2050 r
= source_io_register(s
, m
, s
->io
.events
);
2057 case SOURCE_TIME_REALTIME
:
2058 case SOURCE_TIME_BOOTTIME
:
2059 case SOURCE_TIME_MONOTONIC
:
2060 case SOURCE_TIME_REALTIME_ALARM
:
2061 case SOURCE_TIME_BOOTTIME_ALARM
: {
2062 struct clock_data
*d
;
2065 d
= event_get_clock_data(s
->event
, s
->type
);
2068 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2069 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2070 d
->needs_rearm
= true;
2078 r
= event_make_signal_data(s
->event
, s
->signal
.sig
, NULL
);
2080 s
->enabled
= SD_EVENT_OFF
;
2081 event_gc_signal_data(s
->event
, &s
->priority
, s
->signal
.sig
);
2089 if (s
->enabled
== SD_EVENT_OFF
)
2090 s
->event
->n_enabled_child_sources
++;
2094 r
= event_make_signal_data(s
->event
, SIGCHLD
, NULL
);
2096 s
->enabled
= SD_EVENT_OFF
;
2097 s
->event
->n_enabled_child_sources
--;
2098 event_gc_signal_data(s
->event
, &s
->priority
, SIGCHLD
);
2106 prioq_reshuffle(s
->event
->exit
, s
, &s
->exit
.prioq_index
);
2111 case SOURCE_INOTIFY
:
2116 assert_not_reached("Wut? I shouldn't exist.");
2121 prioq_reshuffle(s
->event
->pending
, s
, &s
->pending_index
);
2124 prioq_reshuffle(s
->event
->prepare
, s
, &s
->prepare_index
);
2129 _public_
int sd_event_source_get_time(sd_event_source
*s
, uint64_t *usec
) {
2130 assert_return(s
, -EINVAL
);
2131 assert_return(usec
, -EINVAL
);
2132 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2133 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2135 *usec
= s
->time
.next
;
2139 _public_
int sd_event_source_set_time(sd_event_source
*s
, uint64_t usec
) {
2140 struct clock_data
*d
;
2143 assert_return(s
, -EINVAL
);
2144 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2145 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2146 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2148 r
= source_set_pending(s
, false);
2152 s
->time
.next
= usec
;
2154 d
= event_get_clock_data(s
->event
, s
->type
);
2157 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2158 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2159 d
->needs_rearm
= true;
2164 _public_
int sd_event_source_get_time_accuracy(sd_event_source
*s
, uint64_t *usec
) {
2165 assert_return(s
, -EINVAL
);
2166 assert_return(usec
, -EINVAL
);
2167 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2168 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2170 *usec
= s
->time
.accuracy
;
2174 _public_
int sd_event_source_set_time_accuracy(sd_event_source
*s
, uint64_t usec
) {
2175 struct clock_data
*d
;
2178 assert_return(s
, -EINVAL
);
2179 assert_return(usec
!= (uint64_t) -1, -EINVAL
);
2180 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2181 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2182 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2184 r
= source_set_pending(s
, false);
2189 usec
= DEFAULT_ACCURACY_USEC
;
2191 s
->time
.accuracy
= usec
;
2193 d
= event_get_clock_data(s
->event
, s
->type
);
2196 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2197 d
->needs_rearm
= true;
2202 _public_
int sd_event_source_get_time_clock(sd_event_source
*s
, clockid_t
*clock
) {
2203 assert_return(s
, -EINVAL
);
2204 assert_return(clock
, -EINVAL
);
2205 assert_return(EVENT_SOURCE_IS_TIME(s
->type
), -EDOM
);
2206 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2208 *clock
= event_source_type_to_clock(s
->type
);
2212 _public_
int sd_event_source_get_child_pid(sd_event_source
*s
, pid_t
*pid
) {
2213 assert_return(s
, -EINVAL
);
2214 assert_return(pid
, -EINVAL
);
2215 assert_return(s
->type
== SOURCE_CHILD
, -EDOM
);
2216 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2218 *pid
= s
->child
.pid
;
2222 _public_
int sd_event_source_get_inotify_mask(sd_event_source
*s
, uint32_t *mask
) {
2223 assert_return(s
, -EINVAL
);
2224 assert_return(mask
, -EINVAL
);
2225 assert_return(s
->type
== SOURCE_INOTIFY
, -EDOM
);
2226 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2228 *mask
= s
->inotify
.mask
;
2232 _public_
int sd_event_source_set_prepare(sd_event_source
*s
, sd_event_handler_t callback
) {
2235 assert_return(s
, -EINVAL
);
2236 assert_return(s
->type
!= SOURCE_EXIT
, -EDOM
);
2237 assert_return(s
->event
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
2238 assert_return(!event_pid_changed(s
->event
), -ECHILD
);
2240 if (s
->prepare
== callback
)
2243 if (callback
&& s
->prepare
) {
2244 s
->prepare
= callback
;
2248 r
= prioq_ensure_allocated(&s
->event
->prepare
, prepare_prioq_compare
);
2252 s
->prepare
= callback
;
2255 r
= prioq_put(s
->event
->prepare
, s
, &s
->prepare_index
);
2259 prioq_remove(s
->event
->prepare
, s
, &s
->prepare_index
);
2264 _public_
void* sd_event_source_get_userdata(sd_event_source
*s
) {
2265 assert_return(s
, NULL
);
2270 _public_
void *sd_event_source_set_userdata(sd_event_source
*s
, void *userdata
) {
2273 assert_return(s
, NULL
);
2276 s
->userdata
= userdata
;
2281 static usec_t
sleep_between(sd_event
*e
, usec_t a
, usec_t b
) {
2288 if (a
>= USEC_INFINITY
)
2289 return USEC_INFINITY
;
2294 initialize_perturb(e
);
2297 Find a good time to wake up again between times a and b. We
2298 have two goals here:
2300 a) We want to wake up as seldom as possible, hence prefer
2301 later times over earlier times.
2303 b) But if we have to wake up, then let's make sure to
2304 dispatch as much as possible on the entire system.
2306 We implement this by waking up everywhere at the same time
2307 within any given minute if we can, synchronised via the
2308 perturbation value determined from the boot ID. If we can't,
2309 then we try to find the same spot in every 10s, then 1s and
2310 then 250ms step. Otherwise, we pick the last possible time
2314 c
= (b
/ USEC_PER_MINUTE
) * USEC_PER_MINUTE
+ e
->perturb
;
2316 if (_unlikely_(c
< USEC_PER_MINUTE
))
2319 c
-= USEC_PER_MINUTE
;
2325 c
= (b
/ (USEC_PER_SEC
*10)) * (USEC_PER_SEC
*10) + (e
->perturb
% (USEC_PER_SEC
*10));
2327 if (_unlikely_(c
< USEC_PER_SEC
*10))
2330 c
-= USEC_PER_SEC
*10;
2336 c
= (b
/ USEC_PER_SEC
) * USEC_PER_SEC
+ (e
->perturb
% USEC_PER_SEC
);
2338 if (_unlikely_(c
< USEC_PER_SEC
))
2347 c
= (b
/ (USEC_PER_MSEC
*250)) * (USEC_PER_MSEC
*250) + (e
->perturb
% (USEC_PER_MSEC
*250));
2349 if (_unlikely_(c
< USEC_PER_MSEC
*250))
2352 c
-= USEC_PER_MSEC
*250;
2361 static int event_arm_timer(
2363 struct clock_data
*d
) {
2365 struct itimerspec its
= {};
2366 sd_event_source
*a
, *b
;
2373 if (!d
->needs_rearm
)
2376 d
->needs_rearm
= false;
2378 a
= prioq_peek(d
->earliest
);
2379 if (!a
|| a
->enabled
== SD_EVENT_OFF
|| a
->time
.next
== USEC_INFINITY
) {
2384 if (d
->next
== USEC_INFINITY
)
2388 r
= timerfd_settime(d
->fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
2392 d
->next
= USEC_INFINITY
;
2396 b
= prioq_peek(d
->latest
);
2397 assert_se(b
&& b
->enabled
!= SD_EVENT_OFF
);
2399 t
= sleep_between(e
, a
->time
.next
, time_event_source_latest(b
));
2403 assert_se(d
->fd
>= 0);
2406 /* We don' want to disarm here, just mean some time looooong ago. */
2407 its
.it_value
.tv_sec
= 0;
2408 its
.it_value
.tv_nsec
= 1;
2410 timespec_store(&its
.it_value
, t
);
2412 r
= timerfd_settime(d
->fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
2420 static int process_io(sd_event
*e
, sd_event_source
*s
, uint32_t revents
) {
2423 assert(s
->type
== SOURCE_IO
);
2425 /* If the event source was already pending, we just OR in the
2426 * new revents, otherwise we reset the value. The ORing is
2427 * necessary to handle EPOLLONESHOT events properly where
2428 * readability might happen independently of writability, and
2429 * we need to keep track of both */
2432 s
->io
.revents
|= revents
;
2434 s
->io
.revents
= revents
;
2436 return source_set_pending(s
, true);
2439 static int flush_timer(sd_event
*e
, int fd
, uint32_t events
, usec_t
*next
) {
2446 assert_return(events
== EPOLLIN
, -EIO
);
2448 ss
= read(fd
, &x
, sizeof(x
));
2450 if (IN_SET(errno
, EAGAIN
, EINTR
))
2456 if (_unlikely_(ss
!= sizeof(x
)))
2460 *next
= USEC_INFINITY
;
2465 static int process_timer(
2468 struct clock_data
*d
) {
2477 s
= prioq_peek(d
->earliest
);
2480 s
->enabled
== SD_EVENT_OFF
||
2484 r
= source_set_pending(s
, true);
2488 prioq_reshuffle(d
->earliest
, s
, &s
->time
.earliest_index
);
2489 prioq_reshuffle(d
->latest
, s
, &s
->time
.latest_index
);
2490 d
->needs_rearm
= true;
2496 static int process_child(sd_event
*e
) {
2503 e
->need_process_child
= false;
2506 So, this is ugly. We iteratively invoke waitid() with P_PID
2507 + WNOHANG for each PID we wait for, instead of using
2508 P_ALL. This is because we only want to get child
2509 information of very specific child processes, and not all
2510 of them. We might not have processed the SIGCHLD even of a
2511 previous invocation and we don't want to maintain a
2512 unbounded *per-child* event queue, hence we really don't
2513 want anything flushed out of the kernel's queue that we
2514 don't care about. Since this is O(n) this means that if you
2515 have a lot of processes you probably want to handle SIGCHLD
2518 We do not reap the children here (by using WNOWAIT), this
2519 is only done after the event source is dispatched so that
2520 the callback still sees the process as a zombie.
2523 HASHMAP_FOREACH(s
, e
->child_sources
, i
) {
2524 assert(s
->type
== SOURCE_CHILD
);
2529 if (s
->enabled
== SD_EVENT_OFF
)
2532 zero(s
->child
.siginfo
);
2533 r
= waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
,
2534 WNOHANG
| (s
->child
.options
& WEXITED
? WNOWAIT
: 0) | s
->child
.options
);
2538 if (s
->child
.siginfo
.si_pid
!= 0) {
2539 bool zombie
= IN_SET(s
->child
.siginfo
.si_code
, CLD_EXITED
, CLD_KILLED
, CLD_DUMPED
);
2541 if (!zombie
&& (s
->child
.options
& WEXITED
)) {
2542 /* If the child isn't dead then let's
2543 * immediately remove the state change
2544 * from the queue, since there's no
2545 * benefit in leaving it queued */
2547 assert(s
->child
.options
& (WSTOPPED
|WCONTINUED
));
2548 waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
, WNOHANG
|(s
->child
.options
& (WSTOPPED
|WCONTINUED
)));
2551 r
= source_set_pending(s
, true);
2560 static int process_signal(sd_event
*e
, struct signal_data
*d
, uint32_t events
) {
2561 bool read_one
= false;
2566 assert_return(events
== EPOLLIN
, -EIO
);
2568 /* If there's a signal queued on this priority and SIGCHLD is
2569 on this priority too, then make sure to recheck the
2570 children we watch. This is because we only ever dequeue
2571 the first signal per priority, and if we dequeue one, and
2572 SIGCHLD might be enqueued later we wouldn't know, but we
2573 might have higher priority children we care about hence we
2574 need to check that explicitly. */
2576 if (sigismember(&d
->sigset
, SIGCHLD
))
2577 e
->need_process_child
= true;
2579 /* If there's already an event source pending for this
2580 * priority we don't read another */
2585 struct signalfd_siginfo si
;
2587 sd_event_source
*s
= NULL
;
2589 n
= read(d
->fd
, &si
, sizeof(si
));
2591 if (IN_SET(errno
, EAGAIN
, EINTR
))
2597 if (_unlikely_(n
!= sizeof(si
)))
2600 assert(SIGNAL_VALID(si
.ssi_signo
));
2604 if (e
->signal_sources
)
2605 s
= e
->signal_sources
[si
.ssi_signo
];
2611 s
->signal
.siginfo
= si
;
2614 r
= source_set_pending(s
, true);
2622 static int event_inotify_data_read(sd_event
*e
, struct inotify_data
*d
, uint32_t revents
) {
2628 assert_return(revents
== EPOLLIN
, -EIO
);
2630 /* If there's already an event source pending for this priority, don't read another */
2631 if (d
->n_pending
> 0)
2634 /* Is the read buffer non-empty? If so, let's not read more */
2635 if (d
->buffer_filled
> 0)
2638 n
= read(d
->fd
, &d
->buffer
, sizeof(d
->buffer
));
2640 if (IN_SET(errno
, EAGAIN
, EINTR
))
2647 d
->buffer_filled
= (size_t) n
;
2648 LIST_PREPEND(buffered
, e
->inotify_data_buffered
, d
);
2653 static void event_inotify_data_drop(sd_event
*e
, struct inotify_data
*d
, size_t sz
) {
2656 assert(sz
<= d
->buffer_filled
);
2661 /* Move the rest to the buffer to the front, in order to get things properly aligned again */
2662 memmove(d
->buffer
.raw
, d
->buffer
.raw
+ sz
, d
->buffer_filled
- sz
);
2663 d
->buffer_filled
-= sz
;
2665 if (d
->buffer_filled
== 0)
2666 LIST_REMOVE(buffered
, e
->inotify_data_buffered
, d
);
2669 static int event_inotify_data_process(sd_event
*e
, struct inotify_data
*d
) {
2675 /* If there's already an event source pending for this priority, don't read another */
2676 if (d
->n_pending
> 0)
2679 while (d
->buffer_filled
> 0) {
2682 /* Let's validate that the event structures are complete */
2683 if (d
->buffer_filled
< offsetof(struct inotify_event
, name
))
2686 sz
= offsetof(struct inotify_event
, name
) + d
->buffer
.ev
.len
;
2687 if (d
->buffer_filled
< sz
)
2690 if (d
->buffer
.ev
.mask
& IN_Q_OVERFLOW
) {
2691 struct inode_data
*inode_data
;
2694 /* The queue overran, let's pass this event to all event sources connected to this inotify
2697 HASHMAP_FOREACH(inode_data
, d
->inodes
, i
) {
2700 LIST_FOREACH(inotify
.by_inode_data
, s
, inode_data
->event_sources
) {
2702 if (s
->enabled
== SD_EVENT_OFF
)
2705 r
= source_set_pending(s
, true);
2711 struct inode_data
*inode_data
;
2714 /* Find the inode object for this watch descriptor. If IN_IGNORED is set we also remove it from
2715 * our watch descriptor table. */
2716 if (d
->buffer
.ev
.mask
& IN_IGNORED
) {
2718 inode_data
= hashmap_remove(d
->wd
, INT_TO_PTR(d
->buffer
.ev
.wd
));
2720 event_inotify_data_drop(e
, d
, sz
);
2724 /* The watch descriptor was removed by the kernel, let's drop it here too */
2725 inode_data
->wd
= -1;
2727 inode_data
= hashmap_get(d
->wd
, INT_TO_PTR(d
->buffer
.ev
.wd
));
2729 event_inotify_data_drop(e
, d
, sz
);
2734 /* Trigger all event sources that are interested in these events. Also trigger all event
2735 * sources if IN_IGNORED or IN_UNMOUNT is set. */
2736 LIST_FOREACH(inotify
.by_inode_data
, s
, inode_data
->event_sources
) {
2738 if (s
->enabled
== SD_EVENT_OFF
)
2741 if ((d
->buffer
.ev
.mask
& (IN_IGNORED
|IN_UNMOUNT
)) == 0 &&
2742 (s
->inotify
.mask
& d
->buffer
.ev
.mask
& IN_ALL_EVENTS
) == 0)
2745 r
= source_set_pending(s
, true);
2751 /* Something pending now? If so, let's finish, otherwise let's read more. */
2752 if (d
->n_pending
> 0)
2759 static int process_inotify(sd_event
*e
) {
2760 struct inotify_data
*d
;
2765 LIST_FOREACH(buffered
, d
, e
->inotify_data_buffered
) {
2766 r
= event_inotify_data_process(e
, d
);
2776 static int source_dispatch(sd_event_source
*s
) {
2777 EventSourceType saved_type
;
2781 assert(s
->pending
|| s
->type
== SOURCE_EXIT
);
2783 /* Save the event source type, here, so that we still know it after the event callback which might invalidate
2785 saved_type
= s
->type
;
2787 if (!IN_SET(s
->type
, SOURCE_DEFER
, SOURCE_EXIT
)) {
2788 r
= source_set_pending(s
, false);
2793 if (s
->type
!= SOURCE_POST
) {
2797 /* If we execute a non-post source, let's mark all
2798 * post sources as pending */
2800 SET_FOREACH(z
, s
->event
->post_sources
, i
) {
2801 if (z
->enabled
== SD_EVENT_OFF
)
2804 r
= source_set_pending(z
, true);
2810 if (s
->enabled
== SD_EVENT_ONESHOT
) {
2811 r
= sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
2816 s
->dispatching
= true;
2821 r
= s
->io
.callback(s
, s
->io
.fd
, s
->io
.revents
, s
->userdata
);
2824 case SOURCE_TIME_REALTIME
:
2825 case SOURCE_TIME_BOOTTIME
:
2826 case SOURCE_TIME_MONOTONIC
:
2827 case SOURCE_TIME_REALTIME_ALARM
:
2828 case SOURCE_TIME_BOOTTIME_ALARM
:
2829 r
= s
->time
.callback(s
, s
->time
.next
, s
->userdata
);
2833 r
= s
->signal
.callback(s
, &s
->signal
.siginfo
, s
->userdata
);
2836 case SOURCE_CHILD
: {
2839 zombie
= IN_SET(s
->child
.siginfo
.si_code
, CLD_EXITED
, CLD_KILLED
, CLD_DUMPED
);
2841 r
= s
->child
.callback(s
, &s
->child
.siginfo
, s
->userdata
);
2843 /* Now, reap the PID for good. */
2845 (void) waitid(P_PID
, s
->child
.pid
, &s
->child
.siginfo
, WNOHANG
|WEXITED
);
2851 r
= s
->defer
.callback(s
, s
->userdata
);
2855 r
= s
->post
.callback(s
, s
->userdata
);
2859 r
= s
->exit
.callback(s
, s
->userdata
);
2862 case SOURCE_INOTIFY
: {
2863 struct sd_event
*e
= s
->event
;
2864 struct inotify_data
*d
;
2867 assert(s
->inotify
.inode_data
);
2868 assert_se(d
= s
->inotify
.inode_data
->inotify_data
);
2870 assert(d
->buffer_filled
>= offsetof(struct inotify_event
, name
));
2871 sz
= offsetof(struct inotify_event
, name
) + d
->buffer
.ev
.len
;
2872 assert(d
->buffer_filled
>= sz
);
2874 r
= s
->inotify
.callback(s
, &d
->buffer
.ev
, s
->userdata
);
2876 /* When no event is pending anymore on this inotify object, then let's drop the event from the
2878 if (d
->n_pending
== 0)
2879 event_inotify_data_drop(e
, d
, sz
);
2884 case SOURCE_WATCHDOG
:
2885 case _SOURCE_EVENT_SOURCE_TYPE_MAX
:
2886 case _SOURCE_EVENT_SOURCE_TYPE_INVALID
:
2887 assert_not_reached("Wut? I shouldn't exist.");
2890 s
->dispatching
= false;
2893 log_debug_errno(r
, "Event source %s (type %s) returned error, disabling: %m",
2894 strna(s
->description
), event_source_type_to_string(saved_type
));
2899 sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
2904 static int event_prepare(sd_event
*e
) {
2912 s
= prioq_peek(e
->prepare
);
2913 if (!s
|| s
->prepare_iteration
== e
->iteration
|| s
->enabled
== SD_EVENT_OFF
)
2916 s
->prepare_iteration
= e
->iteration
;
2917 r
= prioq_reshuffle(e
->prepare
, s
, &s
->prepare_index
);
2923 s
->dispatching
= true;
2924 r
= s
->prepare(s
, s
->userdata
);
2925 s
->dispatching
= false;
2928 log_debug_errno(r
, "Prepare callback of event source %s (type %s) returned error, disabling: %m",
2929 strna(s
->description
), event_source_type_to_string(s
->type
));
2934 sd_event_source_set_enabled(s
, SD_EVENT_OFF
);
2940 static int dispatch_exit(sd_event
*e
) {
2942 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
2947 p
= prioq_peek(e
->exit
);
2948 if (!p
|| p
->enabled
== SD_EVENT_OFF
) {
2949 e
->state
= SD_EVENT_FINISHED
;
2953 ref
= sd_event_ref(e
);
2955 e
->state
= SD_EVENT_EXITING
;
2956 r
= source_dispatch(p
);
2957 e
->state
= SD_EVENT_INITIAL
;
2961 static sd_event_source
* event_next_pending(sd_event
*e
) {
2966 p
= prioq_peek(e
->pending
);
2970 if (p
->enabled
== SD_EVENT_OFF
)
2976 static int arm_watchdog(sd_event
*e
) {
2977 struct itimerspec its
= {};
2982 assert(e
->watchdog_fd
>= 0);
2984 t
= sleep_between(e
,
2985 e
->watchdog_last
+ (e
->watchdog_period
/ 2),
2986 e
->watchdog_last
+ (e
->watchdog_period
* 3 / 4));
2988 timespec_store(&its
.it_value
, t
);
2990 /* Make sure we never set the watchdog to 0, which tells the
2991 * kernel to disable it. */
2992 if (its
.it_value
.tv_sec
== 0 && its
.it_value
.tv_nsec
== 0)
2993 its
.it_value
.tv_nsec
= 1;
2995 r
= timerfd_settime(e
->watchdog_fd
, TFD_TIMER_ABSTIME
, &its
, NULL
);
3002 static int process_watchdog(sd_event
*e
) {
3008 /* Don't notify watchdog too often */
3009 if (e
->watchdog_last
+ e
->watchdog_period
/ 4 > e
->timestamp
.monotonic
)
3012 sd_notify(false, "WATCHDOG=1");
3013 e
->watchdog_last
= e
->timestamp
.monotonic
;
3015 return arm_watchdog(e
);
3018 static void event_close_inode_data_fds(sd_event
*e
) {
3019 struct inode_data
*d
;
3023 /* Close the fds pointing to the inodes to watch now. We need to close them as they might otherwise pin
3024 * filesystems. But we can't close them right-away as we need them as long as the user still wants to make
3025 * adjustments to the even source, such as changing the priority (which requires us to remove and readd a watch
3026 * for the inode). Hence, let's close them when entering the first iteration after they were added, as a
3029 while ((d
= e
->inode_data_to_close
)) {
3031 d
->fd
= safe_close(d
->fd
);
3033 LIST_REMOVE(to_close
, e
->inode_data_to_close
, d
);
3037 _public_
int sd_event_prepare(sd_event
*e
) {
3040 assert_return(e
, -EINVAL
);
3041 assert_return(e
= event_resolve(e
), -ENOPKG
);
3042 assert_return(!event_pid_changed(e
), -ECHILD
);
3043 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3044 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3046 if (e
->exit_requested
)
3051 e
->state
= SD_EVENT_PREPARING
;
3052 r
= event_prepare(e
);
3053 e
->state
= SD_EVENT_INITIAL
;
3057 r
= event_arm_timer(e
, &e
->realtime
);
3061 r
= event_arm_timer(e
, &e
->boottime
);
3065 r
= event_arm_timer(e
, &e
->monotonic
);
3069 r
= event_arm_timer(e
, &e
->realtime_alarm
);
3073 r
= event_arm_timer(e
, &e
->boottime_alarm
);
3077 event_close_inode_data_fds(e
);
3079 if (event_next_pending(e
) || e
->need_process_child
)
3082 e
->state
= SD_EVENT_ARMED
;
3087 e
->state
= SD_EVENT_ARMED
;
3088 r
= sd_event_wait(e
, 0);
3090 e
->state
= SD_EVENT_ARMED
;
3095 _public_
int sd_event_wait(sd_event
*e
, uint64_t timeout
) {
3096 struct epoll_event
*ev_queue
;
3097 unsigned ev_queue_max
;
3100 assert_return(e
, -EINVAL
);
3101 assert_return(e
= event_resolve(e
), -ENOPKG
);
3102 assert_return(!event_pid_changed(e
), -ECHILD
);
3103 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3104 assert_return(e
->state
== SD_EVENT_ARMED
, -EBUSY
);
3106 if (e
->exit_requested
) {
3107 e
->state
= SD_EVENT_PENDING
;
3111 ev_queue_max
= MAX(e
->n_sources
, 1u);
3112 ev_queue
= newa(struct epoll_event
, ev_queue_max
);
3114 /* If we still have inotify data buffered, then query the other fds, but don't wait on it */
3115 if (e
->inotify_data_buffered
)
3118 m
= epoll_wait(e
->epoll_fd
, ev_queue
, ev_queue_max
,
3119 timeout
== (uint64_t) -1 ? -1 : (int) DIV_ROUND_UP(timeout
, USEC_PER_MSEC
));
3121 if (errno
== EINTR
) {
3122 e
->state
= SD_EVENT_PENDING
;
3130 triple_timestamp_get(&e
->timestamp
);
3132 for (i
= 0; i
< m
; i
++) {
3134 if (ev_queue
[i
].data
.ptr
== INT_TO_PTR(SOURCE_WATCHDOG
))
3135 r
= flush_timer(e
, e
->watchdog_fd
, ev_queue
[i
].events
, NULL
);
3137 WakeupType
*t
= ev_queue
[i
].data
.ptr
;
3141 case WAKEUP_EVENT_SOURCE
:
3142 r
= process_io(e
, ev_queue
[i
].data
.ptr
, ev_queue
[i
].events
);
3145 case WAKEUP_CLOCK_DATA
: {
3146 struct clock_data
*d
= ev_queue
[i
].data
.ptr
;
3147 r
= flush_timer(e
, d
->fd
, ev_queue
[i
].events
, &d
->next
);
3151 case WAKEUP_SIGNAL_DATA
:
3152 r
= process_signal(e
, ev_queue
[i
].data
.ptr
, ev_queue
[i
].events
);
3155 case WAKEUP_INOTIFY_DATA
:
3156 r
= event_inotify_data_read(e
, ev_queue
[i
].data
.ptr
, ev_queue
[i
].events
);
3160 assert_not_reached("Invalid wake-up pointer");
3167 r
= process_watchdog(e
);
3171 r
= process_timer(e
, e
->timestamp
.realtime
, &e
->realtime
);
3175 r
= process_timer(e
, e
->timestamp
.boottime
, &e
->boottime
);
3179 r
= process_timer(e
, e
->timestamp
.monotonic
, &e
->monotonic
);
3183 r
= process_timer(e
, e
->timestamp
.realtime
, &e
->realtime_alarm
);
3187 r
= process_timer(e
, e
->timestamp
.boottime
, &e
->boottime_alarm
);
3191 if (e
->need_process_child
) {
3192 r
= process_child(e
);
3197 r
= process_inotify(e
);
3201 if (event_next_pending(e
)) {
3202 e
->state
= SD_EVENT_PENDING
;
3210 e
->state
= SD_EVENT_INITIAL
;
3215 _public_
int sd_event_dispatch(sd_event
*e
) {
3219 assert_return(e
, -EINVAL
);
3220 assert_return(e
= event_resolve(e
), -ENOPKG
);
3221 assert_return(!event_pid_changed(e
), -ECHILD
);
3222 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3223 assert_return(e
->state
== SD_EVENT_PENDING
, -EBUSY
);
3225 if (e
->exit_requested
)
3226 return dispatch_exit(e
);
3228 p
= event_next_pending(e
);
3230 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
3232 ref
= sd_event_ref(e
);
3233 e
->state
= SD_EVENT_RUNNING
;
3234 r
= source_dispatch(p
);
3235 e
->state
= SD_EVENT_INITIAL
;
3239 e
->state
= SD_EVENT_INITIAL
;
3244 static void event_log_delays(sd_event
*e
) {
3245 char b
[ELEMENTSOF(e
->delays
) * DECIMAL_STR_MAX(unsigned) + 1];
3249 for (i
= o
= 0; i
< ELEMENTSOF(e
->delays
); i
++) {
3250 o
+= snprintf(&b
[o
], sizeof(b
) - o
, "%u ", e
->delays
[i
]);
3253 log_debug("Event loop iterations: %.*s", o
, b
);
3256 _public_
int sd_event_run(sd_event
*e
, uint64_t timeout
) {
3259 assert_return(e
, -EINVAL
);
3260 assert_return(e
= event_resolve(e
), -ENOPKG
);
3261 assert_return(!event_pid_changed(e
), -ECHILD
);
3262 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3263 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3265 if (e
->profile_delays
&& e
->last_run
) {
3269 this_run
= now(CLOCK_MONOTONIC
);
3271 l
= u64log2(this_run
- e
->last_run
);
3272 assert(l
< sizeof(e
->delays
));
3275 if (this_run
- e
->last_log
>= 5*USEC_PER_SEC
) {
3276 event_log_delays(e
);
3277 e
->last_log
= this_run
;
3281 r
= sd_event_prepare(e
);
3283 /* There was nothing? Then wait... */
3284 r
= sd_event_wait(e
, timeout
);
3286 if (e
->profile_delays
)
3287 e
->last_run
= now(CLOCK_MONOTONIC
);
3290 /* There's something now, then let's dispatch it */
3291 r
= sd_event_dispatch(e
);
3301 _public_
int sd_event_loop(sd_event
*e
) {
3302 _cleanup_(sd_event_unrefp
) sd_event
*ref
= NULL
;
3305 assert_return(e
, -EINVAL
);
3306 assert_return(e
= event_resolve(e
), -ENOPKG
);
3307 assert_return(!event_pid_changed(e
), -ECHILD
);
3308 assert_return(e
->state
== SD_EVENT_INITIAL
, -EBUSY
);
3310 ref
= sd_event_ref(e
);
3312 while (e
->state
!= SD_EVENT_FINISHED
) {
3313 r
= sd_event_run(e
, (uint64_t) -1);
3318 return e
->exit_code
;
3321 _public_
int sd_event_get_fd(sd_event
*e
) {
3323 assert_return(e
, -EINVAL
);
3324 assert_return(e
= event_resolve(e
), -ENOPKG
);
3325 assert_return(!event_pid_changed(e
), -ECHILD
);
3330 _public_
int sd_event_get_state(sd_event
*e
) {
3331 assert_return(e
, -EINVAL
);
3332 assert_return(e
= event_resolve(e
), -ENOPKG
);
3333 assert_return(!event_pid_changed(e
), -ECHILD
);
3338 _public_
int sd_event_get_exit_code(sd_event
*e
, int *code
) {
3339 assert_return(e
, -EINVAL
);
3340 assert_return(e
= event_resolve(e
), -ENOPKG
);
3341 assert_return(code
, -EINVAL
);
3342 assert_return(!event_pid_changed(e
), -ECHILD
);
3344 if (!e
->exit_requested
)
3347 *code
= e
->exit_code
;
3351 _public_
int sd_event_exit(sd_event
*e
, int code
) {
3352 assert_return(e
, -EINVAL
);
3353 assert_return(e
= event_resolve(e
), -ENOPKG
);
3354 assert_return(e
->state
!= SD_EVENT_FINISHED
, -ESTALE
);
3355 assert_return(!event_pid_changed(e
), -ECHILD
);
3357 e
->exit_requested
= true;
3358 e
->exit_code
= code
;
3363 _public_
int sd_event_now(sd_event
*e
, clockid_t clock
, uint64_t *usec
) {
3364 assert_return(e
, -EINVAL
);
3365 assert_return(e
= event_resolve(e
), -ENOPKG
);
3366 assert_return(usec
, -EINVAL
);
3367 assert_return(!event_pid_changed(e
), -ECHILD
);
3369 if (!TRIPLE_TIMESTAMP_HAS_CLOCK(clock
))
3372 /* Generate a clean error in case CLOCK_BOOTTIME is not available. Note that don't use clock_supported() here,
3373 * for a reason: there are systems where CLOCK_BOOTTIME is supported, but CLOCK_BOOTTIME_ALARM is not, but for
3374 * the purpose of getting the time this doesn't matter. */
3375 if (IN_SET(clock
, CLOCK_BOOTTIME
, CLOCK_BOOTTIME_ALARM
) && !clock_boottime_supported())
3378 if (!triple_timestamp_is_set(&e
->timestamp
)) {
3379 /* Implicitly fall back to now() if we never ran
3380 * before and thus have no cached time. */
3385 *usec
= triple_timestamp_by_clock(&e
->timestamp
, clock
);
3389 _public_
int sd_event_default(sd_event
**ret
) {
3394 return !!default_event
;
3396 if (default_event
) {
3397 *ret
= sd_event_ref(default_event
);
3401 r
= sd_event_new(&e
);
3405 e
->default_event_ptr
= &default_event
;
3413 _public_
int sd_event_get_tid(sd_event
*e
, pid_t
*tid
) {
3414 assert_return(e
, -EINVAL
);
3415 assert_return(e
= event_resolve(e
), -ENOPKG
);
3416 assert_return(tid
, -EINVAL
);
3417 assert_return(!event_pid_changed(e
), -ECHILD
);
3427 _public_
int sd_event_set_watchdog(sd_event
*e
, int b
) {
3430 assert_return(e
, -EINVAL
);
3431 assert_return(e
= event_resolve(e
), -ENOPKG
);
3432 assert_return(!event_pid_changed(e
), -ECHILD
);
3434 if (e
->watchdog
== !!b
)
3438 struct epoll_event ev
;
3440 r
= sd_watchdog_enabled(false, &e
->watchdog_period
);
3444 /* Issue first ping immediately */
3445 sd_notify(false, "WATCHDOG=1");
3446 e
->watchdog_last
= now(CLOCK_MONOTONIC
);
3448 e
->watchdog_fd
= timerfd_create(CLOCK_MONOTONIC
, TFD_NONBLOCK
|TFD_CLOEXEC
);
3449 if (e
->watchdog_fd
< 0)
3452 r
= arm_watchdog(e
);
3456 ev
= (struct epoll_event
) {
3458 .data
.ptr
= INT_TO_PTR(SOURCE_WATCHDOG
),
3461 r
= epoll_ctl(e
->epoll_fd
, EPOLL_CTL_ADD
, e
->watchdog_fd
, &ev
);
3468 if (e
->watchdog_fd
>= 0) {
3469 epoll_ctl(e
->epoll_fd
, EPOLL_CTL_DEL
, e
->watchdog_fd
, NULL
);
3470 e
->watchdog_fd
= safe_close(e
->watchdog_fd
);
3478 e
->watchdog_fd
= safe_close(e
->watchdog_fd
);
3482 _public_
int sd_event_get_watchdog(sd_event
*e
) {
3483 assert_return(e
, -EINVAL
);
3484 assert_return(e
= event_resolve(e
), -ENOPKG
);
3485 assert_return(!event_pid_changed(e
), -ECHILD
);
3490 _public_
int sd_event_get_iteration(sd_event
*e
, uint64_t *ret
) {
3491 assert_return(e
, -EINVAL
);
3492 assert_return(e
= event_resolve(e
), -ENOPKG
);
3493 assert_return(!event_pid_changed(e
), -ECHILD
);
3495 *ret
= e
->iteration
;
3499 _public_
int sd_event_source_set_destroy_callback(sd_event_source
*s
, sd_event_destroy_t callback
) {
3500 assert_return(s
, -EINVAL
);
3502 s
->destroy_callback
= callback
;
3506 _public_
int sd_event_source_get_destroy_callback(sd_event_source
*s
, sd_event_destroy_t
*ret
) {
3507 assert_return(s
, -EINVAL
);
3510 *ret
= s
->destroy_callback
;
3512 return !!s
->destroy_callback
;
3515 _public_
int sd_event_source_get_floating(sd_event_source
*s
) {
3516 assert_return(s
, -EINVAL
);
3521 _public_
int sd_event_source_set_floating(sd_event_source
*s
, int b
) {
3522 assert_return(s
, -EINVAL
);
3524 if (s
->floating
== !!b
)
3527 if (!s
->event
) /* Already disconnected */
3533 sd_event_source_ref(s
);
3534 sd_event_unref(s
->event
);
3536 sd_event_ref(s
->event
);
3537 sd_event_source_unref(s
);