From e75cbb054571b67709e05288afe257e84013cbea Mon Sep 17 00:00:00 2001 From: jiangjianfeng Date: Thu, 19 Jun 2025 03:09:13 +0000 Subject: [PATCH] Add pidfd regression test --- test/src/apps/process/pidfd.c | 92 ++++++++++++++++++++++++++++++++ test/src/apps/scripts/process.sh | 1 + 2 files changed, 93 insertions(+) create mode 100644 test/src/apps/process/pidfd.c diff --git a/test/src/apps/process/pidfd.c b/test/src/apps/process/pidfd.c new file mode 100644 index 000000000..a7c5035e8 --- /dev/null +++ b/test/src/apps/process/pidfd.c @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: MPL-2.0 + +#include "../test.h" + +#include +#include +#include +#include +#include +#include +#include + +static int child_pid; +static int pid_fd; + +FN_SETUP(create_child) +{ + child_pid = CHECK(fork()); + + if (child_pid == 0) { + while (1) { + usleep(100); + } + exit(EXIT_SUCCESS); + } +} +END_SETUP() + +FN_TEST(pidfd_open) +{ + pid_fd = TEST_SUCC(syscall(SYS_pidfd_open, child_pid, 0)); +} +END_TEST() + +FN_TEST(read_write) +{ + char buf[1] = {}; + TEST_ERRNO(read(pid_fd, buf, 1), EINVAL); + TEST_ERRNO(pread(pid_fd, buf, 1, 0), ESPIPE); + TEST_ERRNO(write(pid_fd, "a", 1), EINVAL); + TEST_ERRNO(pwrite(pid_fd, "b", 1, 0), ESPIPE); +} +END_TEST() + +FN_TEST(set_nonblocking) +{ + int flags = + TEST_RES(fcntl(pid_fd, F_GETFL, 0), (_ret & O_NONBLOCK) == 0); + TEST_SUCC(fcntl(pid_fd, F_SETFL, flags | O_NONBLOCK)); + TEST_RES(fcntl(pid_fd, F_GETFL, 0), (_ret & O_NONBLOCK) != 0); +} +END_TEST() + +FN_TEST(file_stat) +{ + struct stat file_info; + TEST_RES(fstat(pid_fd, &file_info), + file_info.st_mode == 0600 && file_info.st_size == 0 && + file_info.st_blksize == 4096); +} +END_TEST() + +#define POLL_EVENTS (POLLIN | POLLOUT | POLLHUP | POLLERR) +static struct pollfd pfd; + +FN_TEST(poll) +{ + pfd.fd = pid_fd; + pfd.events = POLL_EVENTS; + TEST_RES(poll(&pfd, 1, 0), pfd.revents == 0); + + TEST_SUCC(kill(child_pid, SIGKILL)); + sleep(1); + TEST_RES(poll(&pfd, 1, 0), pfd.revents == POLLIN); +} +END_TEST() + +FN_TEST(wait) +{ +#define P_PIDFD 3 + TEST_SUCC(waitid(P_PIDFD, pid_fd, NULL, WNOHANG | WEXITED)); + pfd.revents = 0; + TEST_RES(poll(&pfd, 1, 0), pfd.revents == POLLIN); + TEST_ERRNO(waitid(P_PIDFD, pid_fd, NULL, WNOHANG | WEXITED), ECHILD); +} +END_TEST() + +FN_SETUP(cleanup) +{ + CHECK(close(pid_fd)); +} +END_SETUP() \ No newline at end of file diff --git a/test/src/apps/scripts/process.sh b/test/src/apps/scripts/process.sh index a090ea8b8..4a46104ea 100755 --- a/test/src/apps/scripts/process.sh +++ b/test/src/apps/scripts/process.sh @@ -35,6 +35,7 @@ mmap/mmap_readahead mmap/mmap_vmrss process/group_session process/job_control +process/pidfd process/wait4 pthread/pthread_test pty/open_pty