HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-future/test-fiber-io.c

46,830 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: aef38950b6332e32bbe2299eed73461d9c84788f

Lines 1001–1389 of 1,389

                char buf[1];
                if (read(fds[i].fd, buf, 1) != 1 || buf[0] != (char) ('A' + i))
                        return -EIO;
        }
 
        return 0;
}
 
TEST(fiber_poll_multiple) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        /* Create three pipes */
        int pipes[3][2];
        for (size_t i = 0; i < 3; i++)
                ASSERT_OK_ERRNO(pipe2(pipes[i], O_CLOEXEC | O_NONBLOCK));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-multiple", poll_multiple_fiber, pipes, NULL, &f));
 
        /* Run once - fiber will suspend waiting for data */
        ASSERT_OK_POSITIVE(sd_event_run(e, 0));
 
        /* Write to all three pipes in different order */
        ASSERT_OK_EQ_ERRNO(write(pipes[2][1], "C", 1), 1);
        ASSERT_OK_EQ_ERRNO(write(pipes[0][1], "A", 1), 1);
        ASSERT_OK_EQ_ERRNO(write(pipes[1][1], "B", 1), 1);
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
 
        for (size_t i = 0; i < 3; i++)
                safe_close_pair(pipes[i]);
}
 
/* Test: poll with POLLOUT (write readiness) */
static int poll_pollout_fiber(void *userdata) {
        int *pipefd = userdata;
        struct pollfd fds[] = {
                { .fd = pipefd[1], .events = POLLOUT },
        };
        int r;
 
        r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL);
        if (r < 0)
                return r;
 
        if (r != 1 || !(fds[0].revents & POLLOUT))
                return -EIO;
 
        /* Pipe should be writable */
        if (write(pipefd[1], "Z", 1) != 1)
                return -errno;
 
        return 0;
}
 
TEST(fiber_poll_pollout) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-pollout", poll_pollout_fiber, pipefd, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
 
        /* Verify data was written */
        char buf[1];
        ASSERT_OK_EQ_ERRNO(read(pipefd[0], buf, 1), 1);
        ASSERT_EQ(buf[0], 'Z');
}
 
/* Test: poll with timeout that expires */
static int poll_timeout_fiber(void *userdata) {
        int *pipefd = userdata;
        struct pollfd fds[] = {
                { .fd = pipefd[0], .events = POLLIN },
        };
        int r;
 
        /* Poll with 100ms timeout - no data will arrive */
        r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), &(struct timespec) { .tv_nsec = 100 * NSEC_PER_MSEC }, NULL);
        if (r < 0)
                return r;
 
        /* Should timeout with no fds ready */
        if (r != 0)
                return -EIO;
 
        return 0;
}
 
TEST(fiber_poll_timeout) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-timeout", poll_timeout_fiber, pipefd, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
}
 
/* Test: poll with zero timeout (should not block) */
static int poll_zero_timeout_fiber(void *userdata) {
        int *pipefd = userdata;
        struct pollfd fds[] = {
                { .fd = pipefd[0], .events = POLLIN },
        };
        int r;
 
        /* Poll with zero timeout - should return immediately */
        r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), &(struct timespec) {}, NULL);
        if (r < 0)
                return r;
 
        /* No data available, so should return 0 */
        if (r != 0)
                return -EIO;
 
        /* Now write data */
        if (write(pipefd[1], "Q", 1) != 1)
                return -errno;
 
        /* Poll again with zero timeout - should see data */
        r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL);
        if (r < 0)
                return r;
 
        if (r != 1 || !(fds[0].revents & POLLIN))
                return -EIO;
 
        return 0;
}
 
TEST(fiber_poll_zero_timeout) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-zero-timeout", poll_zero_timeout_fiber, pipefd, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
}
 
/* Test: poll with zero fds and zero timeout (should return immediately) */
static int poll_zero_fds_fiber(void *userdata) {
        return sd_fiber_ppoll(NULL, 0, &(struct timespec) {}, NULL);
}
 
TEST(fiber_poll_zero_fds) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-zero-fds", poll_zero_fds_fiber, NULL, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK_EQ(sd_future_result(f), 0);
}
 
/* Test: poll with zero fds and no timeout has no possible wakeup, must reject with -EINVAL */
static int poll_zero_fds_no_timeout_fiber(void *userdata) {
        return sd_fiber_ppoll(NULL, 0, NULL, NULL);
}
 
TEST(fiber_poll_zero_fds_no_timeout) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-zero-fds-no-timeout", poll_zero_fds_no_timeout_fiber, NULL, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_ERROR(sd_future_result(f), EINVAL);
}
 
/* Test: poll with negative fd (should be ignored) */
static int poll_negative_fd_fiber(void *userdata) {
        int *pipefd = userdata;
        struct pollfd fds[] = {
                { .fd = -1, .events = POLLIN },
                { .fd = pipefd[0], .events = POLLIN },
        };
        int r;
 
        r = sd_fiber_ppoll(fds, ELEMENTSOF(fds), NULL, NULL);
        if (r < 0)
                return r;
 
        /* Only the second fd should be ready */
        if (r != 1 || !(fds[1].revents & POLLIN))
                return -EIO;
 
        /* First fd should have no events */
        if (fds[0].revents != 0)
                return -EIO;
 
        return 0;
}
 
TEST(fiber_poll_negative_fd) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
 
        /* Write data before creating fiber */
        ASSERT_OK_EQ_ERRNO(write(pipefd[1], "N", 1), 1);
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "poll-negative-fd", poll_negative_fd_fiber, pipefd, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
}
 
/* Test: Multiple fibers waiting on the same fd */
typedef struct SharedFdArgs {
        int pipefd;
        int *counter;
} SharedFdArgs;
 
static int shared_fd_read_fiber(void *userdata) {
        SharedFdArgs *args = ASSERT_PTR(userdata);
        char buf[1];
        ssize_t n;
 
        n = sd_fiber_read(args->pipefd, buf, sizeof(buf));
        if (n < 0)
                return (int) n;
 
        if (n != 1)
                return -EIO;
 
        /* Increment counter to track successful reads */
        (*args->counter)++;
 
        return 0;
}
 
TEST(fiber_io_same_fd_multiple_fibers) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC | O_NONBLOCK));
 
        /* Create 3 fibers all waiting on the same pipe read end */
        sd_future *fibers[3] = {};
        CLEANUP_ELEMENTS(fibers, sd_future_unref_array_clear);
        SharedFdArgs args[3];
        int counter = 0;
 
        for (size_t i = 0; i < ELEMENTSOF(fibers); i++) {
                args[i].pipefd = pipefd[0];
                args[i].counter = &counter;
                ASSERT_OK(sd_fiber_new(e, "shared-fd-read", shared_fd_read_fiber, &args[i], NULL, &fibers[i]));
        }
 
        /* All fibers should suspend waiting for data */
        for (size_t i = 0; i < ELEMENTSOF(fibers); i++)
                ASSERT_OK_POSITIVE(sd_event_run(e, 0));
 
        /* Write 3 bytes - each byte will wake one fiber */
        ASSERT_OK_EQ_ERRNO(write(pipefd[1], "ABC", 3), 3);
 
        /* Run until all fibers complete */
        ASSERT_OK(sd_event_loop(e));
 
        /* All should complete successfully and each should have read one byte */
        for (size_t i = 0; i < ELEMENTSOF(fibers); i++)
                ASSERT_OK(sd_future_result(fibers[i]));
 
        ASSERT_EQ(counter, 3);
}
 
static int blocking_fd_preserve_fiber(void *userdata) {
        int *pipefd = ASSERT_PTR(userdata);
        char buf[8] = {};
        ssize_t n;
 
        /* The pipe has data pre-filled, so this should succeed immediately on the fast path.
         * This exercises the fd blocking state restore: fiber_io_operation() temporarily sets the fd
         * to nonblocking, and must restore it to blocking on the success path. */
        n = sd_fiber_read(pipefd[0], buf, sizeof(buf));
        if (n < 0)
                return (int) n;
 
        if ((size_t) n != sizeof(buf) || memcmp(buf, "blocking", sizeof(buf)) != 0)
                return -EIO;
 
        return 0;
}
 
TEST(fiber_io_blocking_fd_preserved) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        /* Create a blocking pipe (no O_NONBLOCK) */
        _cleanup_close_pair_ int pipefd[2] = EBADF_PAIR;
        ASSERT_OK_ERRNO(pipe2(pipefd, O_CLOEXEC));
 
        /* Pre-fill the pipe so the read will succeed immediately */
        ASSERT_OK_EQ_ERRNO(write(pipefd[1], "blocking", 8), 8);
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "blocking-fd-preserve", blocking_fd_preserve_fiber, pipefd, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
 
        /* Verify the read end is still in blocking mode after the fiber completed */
        ASSERT_OK_ZERO(fd_nonblock(pipefd[0], false));
}
 
static int socket_connect_blocking_fiber(void *userdata) {
        struct sockaddr_un *addr = userdata;
        _cleanup_close_ int sockfd = -EBADF;
 
        /* Use a blocking socket (no SOCK_NONBLOCK). sd_fiber_connect() should temporarily set it
         * to nonblocking, handle the EINPROGRESS path with getsockopt(SO_ERROR), and restore
         * the blocking state. */
        sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
        if (sockfd < 0)
                return -errno;
 
        int r = sd_fiber_connect(sockfd, (struct sockaddr*) addr, sizeof(*addr));
        if (r < 0)
                return r;
 
        /* Verify the socket is back in blocking mode */
        r = fd_nonblock(sockfd, false);
        if (r < 0)
                return r;
        if (r > 0)
                return -EBUSY; /* fd was nonblocking, but should have been restored to blocking */
 
        return 0;
}
 
TEST(fiber_io_connect_blocking) {
        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
        ASSERT_OK(sd_event_new(&e));
        ASSERT_OK(sd_event_set_exit_on_idle(e, true));
 
        /* Create listening socket */
        _cleanup_close_ int listen_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
        ASSERT_OK(listen_fd);
 
        struct sockaddr_un addr = {
                .sun_family = AF_UNIX,
        };
        addr.sun_path[0] = '\0';
        snprintf(addr.sun_path + 1, sizeof(addr.sun_path) - 1, "test-fiber-connect-blocking-%d", getpid());
 
        ASSERT_OK(bind(listen_fd, (struct sockaddr*) &addr, sizeof(addr)));
        ASSERT_OK(listen(listen_fd, 1));
 
        _cleanup_(sd_future_unrefp) sd_future *f = NULL;
        ASSERT_OK(sd_fiber_new(e, "connect-blocking", socket_connect_blocking_fiber, &addr, NULL, &f));
 
        ASSERT_OK(sd_event_loop(e));
        ASSERT_OK(sd_future_result(f));
}
 
DEFINE_TEST_MAIN(LOG_DEBUG);