HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-event/sd-event.c
Git blob: 231ed955b540a6e71c82e47bc4d98bc301ac14ac
Lines 3001–4000 of 5,529
return 1;
}
static int event_source_online(
sd_event_source *s,
int enabled,
bool ratelimited) {
bool was_online;
int r;
assert(s);
assert(enabled != SD_EVENT_OFF || !ratelimited);
/* Unset the pending flag when this event source is enabled */
if (s->enabled == SD_EVENT_OFF &&
enabled != SD_EVENT_OFF &&
!IN_SET(s->type, SOURCE_DEFER, SOURCE_EXIT)) {
r = source_set_pending(s, false);
if (r < 0)
return r;
}
/* Are we really ready for onlining? */
if (enabled == SD_EVENT_OFF || ratelimited) {
/* Nope, we are not ready for onlining, then just update the precise state and exit */
s->enabled = enabled;
s->ratelimited = ratelimited;
return 0;
}
was_online = event_source_is_online(s);
switch (s->type) {
case SOURCE_IO:
r = source_io_register(s, enabled, s->io.events);
if (r < 0)
return r;
break;
case SOURCE_SIGNAL:
r = event_make_signal_data(s->event, s->signal.sig, NULL);
if (r < 0) {
event_gc_signal_data(s->event, &s->priority, s->signal.sig);
return r;
}
break;
case SOURCE_CHILD:
if (EVENT_SOURCE_WATCH_PIDFD(s)) {
/* yes, we can rely on pidfd */
r = source_child_pidfd_register(s, enabled);
if (r < 0)
return r;
} else {
/* something other to watch for than WEXITED */
r = event_make_signal_data(s->event, SIGCHLD, NULL);
if (r < 0) {
event_gc_signal_data(s->event, &s->priority, SIGCHLD);
return r;
}
}
if (!was_online)
s->event->n_online_child_sources++;
break;
case SOURCE_MEMORY_PRESSURE:
case SOURCE_CPU_PRESSURE:
case SOURCE_IO_PRESSURE:
/* As documented in sd_event_add_{memory,cpu,io}_pressure(), we can only register the PSI fd
* with epoll after writing the watch string. */
if (s->pressure.write_buffer_size == 0) {
r = source_pressure_register(s, enabled);
if (r < 0)
return r;
}
break;
case SOURCE_TIME_REALTIME:
case SOURCE_TIME_BOOTTIME:
case SOURCE_TIME_MONOTONIC:
case SOURCE_TIME_REALTIME_ALARM:
case SOURCE_TIME_BOOTTIME_ALARM:
case SOURCE_EXIT:
case SOURCE_DEFER:
case SOURCE_POST:
case SOURCE_INOTIFY:
break;
default:
assert_not_reached();
}
s->enabled = enabled;
s->ratelimited = ratelimited;
/* Non-failing operations below */
if (s->type == SOURCE_EXIT)
prioq_reshuffle(s->event->exit, s, &s->exit.prioq_index);
/* Always reshuffle time prioq, as the ratelimited flag may be changed. */
event_source_time_prioq_reshuffle(s);
return 1;
}
_public_ int sd_event_source_set_enabled(sd_event_source *s, int enabled) {
int r;
assert_return(IN_SET(enabled, SD_EVENT_OFF, SD_EVENT_ON, SD_EVENT_ONESHOT), -EINVAL);
/* Quick mode: if the source doesn't exist, SD_EVENT_OFF is a noop. */
if (enabled == SD_EVENT_OFF && !s)
return 0;
assert_return(s, -EINVAL);
assert_return(!event_origin_changed(s->event), -ECHILD);
/* If we are dead anyway, we are fine with turning off sources, but everything else needs to fail. */
if (s->event->state == SD_EVENT_FINISHED)
return enabled == SD_EVENT_OFF ? 0 : -ESTALE;
if (s->enabled == enabled) /* No change? */
return 0;
if (enabled == SD_EVENT_OFF)
r = event_source_offline(s, enabled, s->ratelimited);
else
r = event_source_online(s, enabled, s->ratelimited);
if (r < 0)
return r;
event_source_pp_prioq_reshuffle(s);
return 0;
}
_public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
*ret = s->time.next;
return 0;
}
_public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
int r;
assert_return(s, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_origin_changed(s->event), -ECHILD);
r = source_set_pending(s, false);
if (r < 0)
return r;
s->time.next = usec;
event_source_time_prioq_reshuffle(s);
return 0;
}
_public_ int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec) {
usec_t t;
int r;
assert_return(s, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (usec == USEC_INFINITY)
return sd_event_source_set_time(s, USEC_INFINITY);
r = sd_event_now(s->event, event_source_type_to_clock(s->type), &t);
if (r < 0)
return r;
usec = usec_add(t, usec);
if (usec == USEC_INFINITY)
return -EOVERFLOW;
return sd_event_source_set_time(s, usec);
}
_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
*ret = s->time.accuracy;
return 0;
}
_public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
int r;
assert_return(s, -EINVAL);
assert_return(usec != UINT64_MAX, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_origin_changed(s->event), -ECHILD);
r = source_set_pending(s, false);
if (r < 0)
return r;
if (usec == 0)
usec = EVENT_DEFAULT_ACCURACY_USEC;
s->time.accuracy = usec;
event_source_time_prioq_reshuffle(s);
return 0;
}
_public_ int sd_event_source_get_time_clock(sd_event_source *s, clockid_t *ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(EVENT_SOURCE_IS_TIME(s->type), -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
*ret = event_source_type_to_clock(s->type);
return 0;
}
_public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
*ret = s->child.pid;
return 0;
}
_public_ int sd_event_source_get_child_pidfd(sd_event_source *s) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->child.pidfd;
}
_public_ int sd_event_source_send_child_signal(sd_event_source *s, int sig, const siginfo_t *si, unsigned flags) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
assert_return(SIGNAL_VALID(sig), -EINVAL);
assert(s->child.pidfd >= 0);
/* If we already have seen indication the process exited refuse sending a signal early. */
if (s->child.exited)
return -ESRCH;
assert(!s->child.waited);
/* pidfd_send_signal() changes the siginfo_t argument. This is weird, let's hence copy the structure here. */
siginfo_t copy;
if (si)
copy = *si;
return RET_NERRNO(pidfd_send_signal(s->child.pidfd, sig, si ? © : NULL, flags));
}
_public_ int sd_event_source_get_child_pidfd_own(sd_event_source *s) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
assert(s->child.pidfd >= 0);
return s->child.pidfd_owned;
}
_public_ int sd_event_source_set_child_pidfd_own(sd_event_source *s, int own) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
assert(s->child.pidfd >= 0);
s->child.pidfd_owned = own;
return 0;
}
_public_ int sd_event_source_get_child_process_own(sd_event_source *s) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
return s->child.process_owned;
}
_public_ int sd_event_source_set_child_process_own(sd_event_source *s, int own) {
assert_return(s, -EINVAL);
assert_return(s->type == SOURCE_CHILD, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
s->child.process_owned = own;
return 0;
}
_public_ int sd_event_source_get_inotify_mask(sd_event_source *s, uint32_t *ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(s->type == SOURCE_INOTIFY, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
*ret = s->inotify.mask;
return 0;
}
_public_ int sd_event_source_get_inotify_path(sd_event_source *s, const char **ret) {
assert_return(s, -EINVAL);
assert_return(ret, -EINVAL);
assert_return(s->type == SOURCE_INOTIFY, -EDOM);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (!s->inotify.inode_data)
return -ESTALE; /* already disconnected. */
if (!s->inotify.inode_data->path)
return -ENOSYS; /* /proc was not mounted? */
*ret = s->inotify.inode_data->path;
return 0;
}
_public_ int sd_event_source_set_prepare(sd_event_source *s, sd_event_handler_t callback) {
int r;
assert_return(s, -EINVAL);
assert_return(s->type != SOURCE_EXIT, -EDOM);
assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_origin_changed(s->event), -ECHILD);
if (s->prepare == callback)
return 0;
if (callback && s->prepare) {
s->prepare = callback;
return 0;
}
r = prioq_ensure_allocated(&s->event->prepare, prepare_prioq_compare);
if (r < 0)
return r;
s->prepare = callback;
if (callback) {
r = prioq_put(s->event->prepare, s, &s->prepare_index);
if (r < 0)
return r;
} else
prioq_remove(s->event->prepare, s, &s->prepare_index);
return 0;
}
_public_ void* sd_event_source_get_userdata(sd_event_source *s) {
assert_return(s, NULL);
assert_return(!event_origin_changed(s->event), NULL);
return s->userdata;
}
_public_ void* sd_event_source_set_userdata(sd_event_source *s, void *userdata) {
void *ret;
assert_return(s, NULL);
assert_return(!event_origin_changed(s->event), NULL);
ret = s->userdata;
s->userdata = userdata;
return ret;
}
static int event_source_enter_ratelimited(sd_event_source *s) {
int r;
assert(s);
/* When an event source becomes ratelimited, we place it in the CLOCK_BOOTTIME priority queue, with
* the end of the rate limit time window, much as if it was a timer event source. */
if (s->ratelimited)
return 0; /* Already ratelimited, this is a NOP hence */
/* Make sure we can install a CLOCK_BOOTTIME event further down. */
r = setup_clock_data(s->event, &s->event->boottime, CLOCK_BOOTTIME);
if (r < 0)
return r;
/* Timer event sources are already using the earliest/latest queues for the timer scheduling. Let's
* first remove them from the prioq appropriate for their own clock, so that we can use the prioq
* fields of the event source then for adding it to the CLOCK_BOOTTIME prioq instead. */
if (EVENT_SOURCE_IS_TIME(s->type))
event_source_time_prioq_remove(s, event_get_clock_data(s->event, s->type));
/* Now, let's add the event source to the boottime clock instead */
r = event_source_time_prioq_put(s, &s->event->boottime);
if (r < 0)
goto fail;
/* And let's take the event source officially offline */
r = event_source_offline(s, s->enabled, /* ratelimited= */ true);
if (r < 0) {
event_source_time_prioq_remove(s, &s->event->boottime);
goto fail;
}
event_source_pp_prioq_reshuffle(s);
log_debug("Event source %p (%s) entered rate limit state.", s, strna(s->description));
return 0;
fail:
/* Reinstall time event sources in the priority queue as before. This shouldn't fail, since the queue
* space for it should already be allocated. */
if (EVENT_SOURCE_IS_TIME(s->type))
assert_se(event_source_time_prioq_put(s, event_get_clock_data(s->event, s->type)) >= 0);
return r;
}
static int event_source_leave_ratelimit(sd_event_source *s, bool run_callback) {
int r;
assert(s);
if (!s->ratelimited)
return 0;
/* Let's take the event source out of the boottime prioq first. */
event_source_time_prioq_remove(s, &s->event->boottime);
/* Let's then add the event source to its native clock prioq again — if this is a timer event source */
if (EVENT_SOURCE_IS_TIME(s->type)) {
r = event_source_time_prioq_put(s, event_get_clock_data(s->event, s->type));
if (r < 0)
goto fail;
}
/* Let's try to take it online again. */
r = event_source_online(s, s->enabled, /* ratelimited= */ false);
if (r < 0) {
/* Do something roughly sensible when this failed: undo the two prioq ops above */
if (EVENT_SOURCE_IS_TIME(s->type))
event_source_time_prioq_remove(s, event_get_clock_data(s->event, s->type));
goto fail;
}
event_source_pp_prioq_reshuffle(s);
ratelimit_reset(&s->rate_limit);
log_debug("Event source %p (%s) left rate limit state.", s, strna(s->description));
if (run_callback && s->ratelimit_expire_callback) {
s->dispatching = true;
r = s->ratelimit_expire_callback(s, s->userdata);
s->dispatching = false;
if (r < 0) {
log_debug_errno(r, "Ratelimit expiry callback of event source %s (type %s) returned error, %s: %m",
strna(s->description),
event_source_type_to_string(s->type),
s->exit_on_failure ? "exiting" : "disabling");
if (s->exit_on_failure)
(void) sd_event_exit(s->event, r);
}
if (s->n_ref == 0)
source_free(s);
else if (r < 0)
assert_se(sd_event_source_set_enabled(s, SD_EVENT_OFF) >= 0);
return 1;
}
return 0;
fail:
/* Do something somewhat reasonable when we cannot move an event sources out of ratelimited mode:
* simply put it back in it, maybe we can then process it more successfully next iteration. */
assert_se(event_source_time_prioq_put(s, &s->event->boottime) >= 0);
return r;
}
static usec_t sleep_between(sd_event *e, usec_t a, usec_t b) {
usec_t c;
assert(e);
assert(a <= b);
if (a <= 0)
return 0;
if (a >= USEC_INFINITY)
return USEC_INFINITY;
if (b <= a + 1)
return a;
initialize_perturb(e);
/*
Find a good time to wake up again between times a and b. We
have two goals here:
a) We want to wake up as seldom as possible, hence prefer
later times over earlier times.
b) But if we have to wake up, then let's make sure to
dispatch as much as possible on the entire system.
We implement this by waking up everywhere at the same time
within any given minute if we can, synchronised via the
perturbation value determined from the boot ID. If we can't,
then we try to find the same spot in every 10s, then 1s and
then 250ms step. Otherwise, we pick the last possible time
to wake up.
*/
c = (b / USEC_PER_MINUTE) * USEC_PER_MINUTE + e->perturb;
if (c >= b) {
if (_unlikely_(c < USEC_PER_MINUTE))
return b;
c -= USEC_PER_MINUTE;
}
if (c >= a)
return c;
c = (b / (USEC_PER_SEC*10)) * (USEC_PER_SEC*10) + (e->perturb % (USEC_PER_SEC*10));
if (c >= b) {
if (_unlikely_(c < USEC_PER_SEC*10))
return b;
c -= USEC_PER_SEC*10;
}
if (c >= a)
return c;
c = (b / USEC_PER_SEC) * USEC_PER_SEC + (e->perturb % USEC_PER_SEC);
if (c >= b) {
if (_unlikely_(c < USEC_PER_SEC))
return b;
c -= USEC_PER_SEC;
}
if (c >= a)
return c;
c = (b / (USEC_PER_MSEC*250)) * (USEC_PER_MSEC*250) + (e->perturb % (USEC_PER_MSEC*250));
if (c >= b) {
if (_unlikely_(c < USEC_PER_MSEC*250))
return b;
c -= USEC_PER_MSEC*250;
}
if (c >= a)
return c;
return b;
}
static int event_arm_timer(
sd_event *e,
struct clock_data *d) {
struct itimerspec its = {};
sd_event_source *a, *b;
usec_t t;
assert(e);
assert(d);
if (!d->needs_rearm)
return 0;
d->needs_rearm = false;
a = prioq_peek(d->earliest);
assert(!a || EVENT_SOURCE_USES_TIME_PRIOQ(a->type));
if (!a || a->enabled == SD_EVENT_OFF || time_event_source_next(a) == USEC_INFINITY) {
if (d->fd < 0)
return 0;
if (d->next == USEC_INFINITY)
return 0;
/* disarm */
if (timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL) < 0)
return -errno;
d->next = USEC_INFINITY;
return 0;
}
b = prioq_peek(d->latest);
assert(!b || EVENT_SOURCE_USES_TIME_PRIOQ(b->type));
assert(b && b->enabled != SD_EVENT_OFF);
t = sleep_between(e, time_event_source_next(a), time_event_source_latest(b));
if (d->next == t)
return 0;
assert_se(d->fd >= 0);
if (t == 0) {
/* We don't want to disarm here, just mean some time looooong ago. */
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 1;
} else
timespec_store(&its.it_value, t);
if (timerfd_settime(d->fd, TFD_TIMER_ABSTIME, &its, NULL) < 0)
return -errno;
d->next = t;
return 0;
}
static int process_io(sd_event *e, sd_event_source *s, uint32_t revents) {
assert(e);
assert(s);
assert(s->type == SOURCE_IO);
/* If the event source was already pending, we just OR in the
* new revents, otherwise we reset the value. The ORing is
* necessary to handle EPOLLONESHOT events properly where
* readability might happen independently of writability, and
* we need to keep track of both */
if (s->pending)
s->io.revents |= revents;
else
s->io.revents = revents;
return source_set_pending(s, true);
}
static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
uint64_t x;
ssize_t ss;
assert(e);
assert(fd >= 0);
assert_return(events == EPOLLIN, -EIO);
ss = read(fd, &x, sizeof(x));
if (ss < 0) {
if (ERRNO_IS_TRANSIENT(errno))
return 0;
return -errno;
}
if (_unlikely_(ss != sizeof(x)))
return -EIO;
if (next)
*next = USEC_INFINITY;
return 0;
}
static int process_timer(
sd_event *e,
usec_t n,
struct clock_data *d) {
sd_event_source *s;
bool callback_invoked = false;
int r;
assert(e);
assert(d);
for (;;) {
s = prioq_peek(d->earliest);
assert(!s || EVENT_SOURCE_USES_TIME_PRIOQ(s->type));
if (!s || time_event_source_next(s) > n)
break;
if (s->ratelimited) {
/* This is an event sources whose ratelimit window has ended. Let's turn it on
* again. */
assert(s->ratelimited);
r = event_source_leave_ratelimit(s, /* run_callback= */ true);
if (r < 0)
return r;
else if (r == 1)
callback_invoked = true;
continue;
}
if (s->enabled == SD_EVENT_OFF || s->pending)
break;
r = source_set_pending(s, true);
if (r < 0)
return r;
event_source_time_prioq_reshuffle(s);
}
return callback_invoked;
}
static int process_child(sd_event *e, int64_t threshold, int64_t *ret_min_priority) {
int64_t min_priority = threshold;
bool something_new = false;
sd_event_source *s;
int r;
assert(e);
assert(ret_min_priority);
if (!e->need_process_child) {
*ret_min_priority = min_priority;
return 0;
}
e->need_process_child = false;
/* So, this is ugly. We iteratively invoke waitid() + WNOHANG with each child process we shall wait for,
* instead of using P_ALL. This is because we only want to get child information of very specific
* child processes, and not all of them. We might not have processed the SIGCHLD event
* of a previous invocation and we don't want to maintain a unbounded *per-child* event queue,
* hence we really don't want anything flushed out of the kernel's queue that we don't care
* about. Since this is O(n) this means that if you have a lot of processes you probably want
* to handle SIGCHLD yourself.
*
* We do not reap the children here (by using WNOWAIT), this is only done after the event
* source is dispatched so that the callback still sees the process as a zombie. */
HASHMAP_FOREACH(s, e->child_sources) {
assert(s->type == SOURCE_CHILD);
assert(s->child.pidfd >= 0);
if (s->priority > threshold)
continue;
if (s->pending)
continue;
if (event_source_is_offline(s))
continue;
if (s->child.exited)
continue;
if (EVENT_SOURCE_WATCH_PIDFD(s))
/* There's a usable pidfd known for this event source? Then don't waitid() for
* it here */
continue;
zero(s->child.siginfo);
if (waitid(P_PIDFD, s->child.pidfd, &s->child.siginfo,
WNOHANG | (s->child.options & WEXITED ? WNOWAIT : 0) | (s->child.options & ~WNOWAIT)) < 0)
return negative_errno();
if (s->child.siginfo.si_pid != 0) {
bool zombie = SIGINFO_CODE_IS_DEAD(s->child.siginfo.si_code);
if (zombie)
s->child.exited = true;
else if (s->child.options & WEXITED) {
/* If the child isn't dead then let's immediately remove the state change
* from the queue, since there's no benefit in leaving it queued. */
assert(s->child.options & (WSTOPPED|WCONTINUED));
(void) waitid(P_PIDFD, s->child.pidfd, &s->child.siginfo, WNOHANG|(s->child.options & (WSTOPPED|WCONTINUED)));
}
r = source_set_pending(s, true);
if (r < 0)
return r;
if (r > 0) {
something_new = true;
min_priority = MIN(min_priority, s->priority);
}
}
}
*ret_min_priority = min_priority;
return something_new;
}
static int process_pidfd(sd_event *e, sd_event_source *s, uint32_t revents) {
assert(e);
assert(s);
assert(s->type == SOURCE_CHILD);
assert(s->child.pidfd >= 0);
if (s->pending)
return 0;
if (event_source_is_offline(s))
return 0;
if (!EVENT_SOURCE_WATCH_PIDFD(s))
return 0;
/* Note that pidfd would also generate EPOLLHUP when the process gets reaped. But at this point we
* only permit EPOLLIN, under the assumption that upon EPOLLHUP the child source should already
* be set to pending, and we would have returned early above. */
zero(s->child.siginfo);
if (waitid(P_PIDFD, s->child.pidfd, &s->child.siginfo, WNOHANG | WNOWAIT | s->child.options) < 0)
return -errno;
if (s->child.siginfo.si_pid == 0)
return 0;
if (SIGINFO_CODE_IS_DEAD(s->child.siginfo.si_code))
s->child.exited = true;
return source_set_pending(s, true);
}
static int process_signal(sd_event *e, struct signal_data *d, uint32_t events, int64_t *min_priority) {
int r;
assert(e);
assert(d);
assert_return(events == EPOLLIN, -EIO);
assert(min_priority);
/* If there's a signal queued on this priority and SIGCHLD is on this priority too, then make
* sure to recheck the children we watch. This is because we only ever dequeue the first signal
* per priority, and if we dequeue one, and SIGCHLD might be enqueued later we wouldn't know,
* but we might have higher priority children we care about hence we need to check that
* explicitly. */
if (sigismember(&d->sigset, SIGCHLD))
e->need_process_child = true;
/* If there's already an event source pending for this priority we don't read another */
if (d->current)
return 0;
for (;;) {
struct signalfd_siginfo si;
ssize_t n;
sd_event_source *s = NULL;
n = read(d->fd, &si, sizeof(si));
if (n < 0) {
if (ERRNO_IS_TRANSIENT(errno))
return 0;
return -errno;
}
if (_unlikely_(n != sizeof(si)))
return -EIO;
if (_unlikely_(si.ssi_signo > INT_MAX)) /* Ensure value fits in int before casting */
return -EIO;
if (_unlikely_(!SIGNAL_VALID(si.ssi_signo)))
return -EIO;
if (e->signal_sources)
s = e->signal_sources[si.ssi_signo];
if (!s)
continue;
if (s->pending)
continue;
s->signal.siginfo = si;
d->current = s;
r = source_set_pending(s, true);
if (r < 0)
return r;
if (r > 0 && *min_priority >= s->priority) {
*min_priority = s->priority;
return 1; /* an event source with smaller priority is queued. */
}
return 0;
}
}
static int event_inotify_data_read(sd_event *e, InotifyData *d, uint32_t revents, int64_t threshold) {
ssize_t n;
assert(e);
assert(d);
assert_return(revents == EPOLLIN, -EIO);
/* If there's already an event source pending for this priority, don't read another */
if (d->n_pending > 0)
return 0;
/* Is the read buffer non-empty? If so, let's not read more */
if (d->buffer_filled > 0)
return 0;
if (d->priority > threshold)
return 0;
n = read(d->fd, &d->buffer, sizeof(d->buffer));
if (n < 0) {
if (ERRNO_IS_TRANSIENT(errno))
return 0;
return -errno;
}
assert(n > 0);
d->buffer_filled = (size_t) n;
LIST_PREPEND(buffered, e->buffered_inotify_data_list, d);
return 1;
}
static void event_inotify_data_drop(sd_event *e, InotifyData *d, size_t sz) {
assert(e);
assert(d);
assert(sz <= d->buffer_filled);
if (sz == 0)
return;
/* Move the rest to the buffer to the front, in order to get things properly aligned again */
memmove(d->buffer.raw, d->buffer.raw + sz, d->buffer_filled - sz);
d->buffer_filled -= sz;
if (d->buffer_filled == 0)
LIST_REMOVE(buffered, e->buffered_inotify_data_list, d);
}
static int event_inotify_data_process(sd_event *e, InotifyData *d) {
int r;
assert(e);
assert(d);
/* If there's already an event source pending for this priority, don't read another */
if (d->n_pending > 0)
return 0;
while (d->buffer_filled > 0) {
size_t sz;
/* Let's validate that the event structures are complete */
if (d->buffer_filled < offsetof(struct inotify_event, name))
return -EIO;
sz = offsetof(struct inotify_event, name) + d->buffer.ev.len;
if (d->buffer_filled < sz)
return -EIO;
if (d->buffer.ev.mask & IN_Q_OVERFLOW) {
InodeData *inode_data;
/* The queue overran, let's pass this event to all event sources connected to this inotify
* object */
HASHMAP_FOREACH(inode_data, d->inodes)
LIST_FOREACH(inotify.by_inode_data, s, inode_data->event_sources) {
if (event_source_is_offline(s))
continue;
r = source_set_pending(s, true);
if (r < 0)
return r;
}
} else {
InodeData *inode_data;
/* Find the inode object for this watch descriptor. If IN_IGNORED is set we also remove it from
* our watch descriptor table. */
if (d->buffer.ev.mask & IN_IGNORED) {
inode_data = hashmap_remove(d->wd, INT_TO_PTR(d->buffer.ev.wd));