HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-event/test-event.c
Git blob: e0bfad50c0c663403ac038cfeff51c8cf2f63014
Lines 1001–1255 of 1,255
_cleanup_(sd_event_source_unrefp) sd_event_source *io = NULL, *defer = NULL;
unsigned count = 0;
bool dispatched = false;
ASSERT_OK(sd_event_default(&e));
ASSERT_OK_ERRNO(pipe2(p, O_CLOEXEC|O_NONBLOCK));
ASSERT_OK(sd_event_add_io(e, &io, p[0], EPOLLIN, ratelimit_io_handler, &count));
ASSERT_OK(sd_event_source_set_ratelimit(io, 1 * USEC_PER_SEC, 1));
ASSERT_OK(sd_event_source_set_ratelimit_expire_callback(io, expire_drain_callback));
ASSERT_OK_EQ_ERRNO(write(p[1], "x", 1), 1);
/* The first dispatch opens the ratelimit window, the second one hits the burst limit. */
ASSERT_OK_POSITIVE(sd_event_run(e, 0));
ASSERT_OK_POSITIVE(sd_event_run(e, 0));
ASSERT_OK_POSITIVE(sd_event_source_is_ratelimited(io));
/* Arm the CLOCK_BOOTTIME timer for the end of the ratelimit window, and let it elapse. */
ASSERT_OK_ZERO(sd_event_run(e, 0));
ASSERT_OK(usleep_safe(1100 * USEC_PER_MSEC));
/* Now have an event source pending only in userspace, which never makes the epoll fd readable. */
ASSERT_OK(sd_event_add_defer(e, &defer, defer_post_handler, &dispatched));
/* sd_event_prepare() will see the pending defer source, and its sd_event_wait() call will run the
* ratelimit expiry callback. It must not report "nothing pending" afterwards, as otherwise callers
* like sd_event_run() would block indefinitely in epoll_wait(). */
ASSERT_OK_POSITIVE(sd_event_prepare(e));
ASSERT_EQ(expire_drain_count, 1U);
ASSERT_EQ(count, 1U);
ASSERT_OK_ZERO(sd_event_source_is_ratelimited(io));
/* No other callback is dispatched in the same iteration as the ratelimit expiry callback. */
ASSERT_OK_POSITIVE(sd_event_dispatch(e));
ASSERT_EQ(count, 1U);
ASSERT_FALSE(dispatched);
/* The IO event that hit the ratelimit stayed pending and is delivered first, then the defer source. */
ASSERT_OK_POSITIVE(sd_event_run(e, 0));
ASSERT_EQ(count, 2U);
ASSERT_FALSE(dispatched);
ASSERT_OK_POSITIVE(sd_event_run(e, 0));
ASSERT_TRUE(dispatched);
ASSERT_EQ(expire_drain_count, 1U);
}
static int child_handler_wnowait(sd_event_source *s, const siginfo_t *si, void *userdata) {
int *counter = ASSERT_PTR(userdata);
(*counter)++;
if (*counter == 5)
ASSERT_OK(sd_event_exit(sd_event_source_get_event(s), 0));
return 0;
}
TEST(child_wnowait) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
ASSERT_OK(sd_event_default(&e));
/* Fork a subprocess */
pid_t pid;
ASSERT_OK_ERRNO(pid = fork());
if (pid == 0)
/* Child process - exit with a specific code */
_exit(42);
/* Add a child source with WNOWAIT flag */
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
int counter = 0;
ASSERT_OK(sd_event_add_child(e, &s, pid, WEXITED|WNOWAIT, child_handler_wnowait, &counter));
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ON));
/* Run the event loop - this should call the handler */
ASSERT_OK(sd_event_loop(e));
ASSERT_EQ(counter, 5);
/* Since we used WNOWAIT, the child should still be waitable */
siginfo_t si = {};
ASSERT_OK_ERRNO(waitid(P_PID, pid, &si, WEXITED|WNOHANG));
ASSERT_EQ(si.si_pid, pid);
ASSERT_EQ(si.si_code, CLD_EXITED);
ASSERT_EQ(si.si_status, 42);
}
TEST(child_pidfd_wnowait) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
ASSERT_OK(sd_event_default(&e));
/* Fork a subprocess */
pid_t pid;
ASSERT_OK_ERRNO(pid = fork());
if (pid == 0)
/* Child process - exit with a specific code */
_exit(42);
_cleanup_close_ int pidfd = -EBADF;
ASSERT_OK_ERRNO(pidfd = pidfd_open(pid, 0));
/* Add a child source with WNOWAIT flag */
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
int counter = 0;
ASSERT_OK(sd_event_add_child_pidfd(e, &s, pidfd, WEXITED|WNOWAIT, child_handler_wnowait, &counter));
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_ON));
/* Run the event loop - this should call the handler */
ASSERT_OK(sd_event_loop(e));
ASSERT_EQ(counter, 5);
/* Since we used WNOWAIT, the child should still be waitable */
siginfo_t si = {};
ASSERT_OK_ERRNO(waitid(P_PIDFD, pidfd, &si, WEXITED|WNOHANG));
ASSERT_EQ(si.si_pid, pid);
ASSERT_EQ(si.si_code, CLD_EXITED);
ASSERT_EQ(si.si_status, 42);
}
static int exit_on_idle_defer_handler(sd_event_source *s, void *userdata) {
unsigned *c = ASSERT_PTR(userdata);
/* Should not be reached on third call because the event loop should exit before */
ASSERT_LT(*c, 2u);
(*c)++;
/* Disable ourselves, which should trigger exit-on-idle after the second iteration */
if (*c == 2)
ASSERT_OK(sd_event_source_set_enabled(s, SD_EVENT_OFF));
return 0;
}
static int exit_on_idle_post_handler(sd_event_source *s, void *userdata) {
unsigned *c = ASSERT_PTR(userdata);
/* Should not be reached on third call because the event loop should exit before */
ASSERT_LT(*c, 2u);
(*c)++;
return 0;
}
static int exit_on_idle_exit_handler(sd_event_source *s, void *userdata) {
return 0;
}
TEST(exit_on_idle) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
ASSERT_OK(sd_event_new(&e));
ASSERT_OK(sd_event_set_exit_on_idle(e, true));
ASSERT_OK_POSITIVE(sd_event_get_exit_on_idle(e));
/* Create a recurring defer event source. */
_cleanup_(sd_event_source_unrefp) sd_event_source *d = NULL;
unsigned dc = 0;
ASSERT_OK(sd_event_add_defer(e, &d, exit_on_idle_defer_handler, &dc));
ASSERT_OK(sd_event_source_set_enabled(d, SD_EVENT_ON));
/* This post event source should not keep the event loop running after the defer source is disabled. */
_cleanup_(sd_event_source_unrefp) sd_event_source *p = NULL;
unsigned pc = 0;
ASSERT_OK(sd_event_add_post(e, &p, exit_on_idle_post_handler, &pc));
ASSERT_OK(sd_event_source_set_enabled(p, SD_EVENT_ON));
ASSERT_OK(sd_event_source_set_priority(p, SD_EVENT_PRIORITY_IMPORTANT));
/* And neither should this exit event source. */
ASSERT_OK(sd_event_add_exit(e, NULL, exit_on_idle_exit_handler, NULL));
/* Run the event loop - it should exit after we disable the event source */
ASSERT_OK(sd_event_loop(e));
ASSERT_EQ(dc, 2u);
ASSERT_EQ(pc, 2u);
}
TEST(exit_on_idle_no_sources) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
ASSERT_OK(sd_event_new(&e));
ASSERT_OK(sd_event_set_exit_on_idle(e, true));
/* Running loop with no sources should return immediately with success */
ASSERT_OK(sd_event_loop(e));
}
static int defer_fair_handler(sd_event_source *s, void *userdata) {
unsigned *counter = ASSERT_PTR(userdata);
/* If we're about to increment above 5, exit the event loop */
if (*counter >= 5)
return sd_event_exit(sd_event_source_get_event(s), 0);
(*counter)++;
return 0;
}
TEST(defer_fair_scheduling) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
sd_event_source *sources[5] = {};
unsigned counters[5] = {};
ASSERT_OK(sd_event_new(&e));
ASSERT_OK(sd_event_set_exit_on_idle(e, true));
/* Create 5 defer sources with equal priority */
for (unsigned i = 0; i < 5; i++) {
ASSERT_OK(sd_event_add_defer(e, &sources[i], defer_fair_handler, &counters[i]));
ASSERT_OK(sd_event_source_set_enabled(sources[i], SD_EVENT_ON));
}
/* Run the event loop until one of the handlers exits */
ASSERT_OK(sd_event_loop(e));
/* All counters should be equal to 5, demonstrating fair scheduling */
for (unsigned i = 0; i < 5; i++) {
ASSERT_EQ(counters[i], 5u);
sd_event_source_unref(sources[i]);
}
}
TEST(child_autoreap_ebusy) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL;
_cleanup_(pidref_done_sigkill_wait) PidRef pidref = PIDREF_NULL;
/* Test that sd_event_add_child() fails with EBUSY when kernel autoreaping is enabled
* by setting SIGCHLD disposition to SIG_IGN */
ASSERT_OK(sd_event_new(&e));
/* First, verify that adding a child source works with default signal disposition */
ASSERT_OK_POSITIVE(pidref_safe_fork("(child-autoreaping-ebusy)", FORK_DEATHSIG_SIGKILL|FORK_FREEZE, &pidref));
ASSERT_OK(event_add_child_pidref(e, &s, &pidref, WEXITED, NULL, NULL));
s = sd_event_source_unref(s);
/* Now set SIGCHLD to SIG_IGN to enable kernel autoreaping */
struct sigaction old_sa, new_sa = {};
new_sa.sa_handler = SIG_IGN;
ASSERT_OK_ERRNO(sigaction(SIGCHLD, &new_sa, &old_sa));
ASSERT_ERROR(event_add_child_pidref(e, &s, &pidref, WEXITED, NULL, NULL), EBUSY);
/* Restore original SIGCHLD disposition */
ASSERT_OK_ERRNO(sigaction(SIGCHLD, &old_sa, NULL));
}
DEFINE_TEST_MAIN(LOG_DEBUG);