Commit 09d1de1a authored by Christian Brauner's avatar Christian Brauner

tests: port pidfd_wait to kselftest harness

All of the new pidfd selftests already use the new kselftest harness
infrastructure. It makes for clearer output, makes the code easier to
understand, and makes adding new tests way simpler.
Signed-off-by: default avatarChristian Brauner <christian.brauner@ubuntu.com>
Reviewed-by: default avatarJosh Triplett <josh@joshtriplett.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: linux-kselftest@vger.kernel.org
Link: https://lore.kernel.org/r/20200902102130.147672-4-christian.brauner@ubuntu.com
parent 6da73d15
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include <unistd.h> #include <unistd.h>
#include "pidfd.h" #include "pidfd.h"
#include "../kselftest.h" #include "../kselftest_harness.h"
#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr))) #define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
...@@ -32,9 +32,8 @@ static int sys_waitid(int which, pid_t pid, siginfo_t *info, int options, ...@@ -32,9 +32,8 @@ static int sys_waitid(int which, pid_t pid, siginfo_t *info, int options,
return syscall(__NR_waitid, which, pid, info, options, ru); return syscall(__NR_waitid, which, pid, info, options, ru);
} }
static int test_pidfd_wait_simple(void) TEST(wait_simple)
{ {
const char *test_name = "pidfd wait simple";
int pidfd = -1, status = 0; int pidfd = -1, status = 0;
pid_t parent_tid = -1; pid_t parent_tid = -1;
struct clone_args args = { struct clone_args args = {
...@@ -50,76 +49,40 @@ static int test_pidfd_wait_simple(void) ...@@ -50,76 +49,40 @@ static int test_pidfd_wait_simple(void)
}; };
pidfd = open("/proc/self", O_DIRECTORY | O_RDONLY | O_CLOEXEC); pidfd = open("/proc/self", O_DIRECTORY | O_RDONLY | O_CLOEXEC);
if (pidfd < 0) ASSERT_GE(pidfd, 0);
ksft_exit_fail_msg("%s test: failed to open /proc/self %s\n",
test_name, strerror(errno));
pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
if (pid == 0) ASSERT_NE(pid, 0);
ksft_exit_fail_msg( EXPECT_EQ(close(pidfd), 0);
"%s test: succeeded to wait on invalid pidfd %s\n",
test_name, strerror(errno));
close(pidfd);
pidfd = -1; pidfd = -1;
pidfd = open("/dev/null", O_RDONLY | O_CLOEXEC); pidfd = open("/dev/null", O_RDONLY | O_CLOEXEC);
if (pidfd == 0) ASSERT_GE(pidfd, 0);
ksft_exit_fail_msg("%s test: failed to open /dev/null %s\n",
test_name, strerror(errno));
pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
if (pid == 0) ASSERT_NE(pid, 0);
ksft_exit_fail_msg( EXPECT_EQ(close(pidfd), 0);
"%s test: succeeded to wait on invalid pidfd %s\n",
test_name, strerror(errno));
close(pidfd);
pidfd = -1; pidfd = -1;
pid = sys_clone3(&args); pid = sys_clone3(&args);
if (pid < 0) ASSERT_GE(pid, 1);
ksft_exit_fail_msg("%s test: failed to create new process %s\n",
test_name, strerror(errno));
if (pid == 0) if (pid == 0)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL); pid = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
if (pid < 0) ASSERT_GE(pid, 0);
ksft_exit_fail_msg( ASSERT_EQ(WIFEXITED(info.si_status), true);
"%s test: failed to wait on process with pid %d and pidfd %d: %s\n", ASSERT_EQ(WEXITSTATUS(info.si_status), 0);
test_name, parent_tid, pidfd, strerror(errno)); EXPECT_EQ(close(pidfd), 0);
if (!WIFEXITED(info.si_status) || WEXITSTATUS(info.si_status)) ASSERT_EQ(info.si_signo, SIGCHLD);
ksft_exit_fail_msg( ASSERT_EQ(info.si_code, CLD_EXITED);
"%s test: unexpected status received after waiting on process with pid %d and pidfd %d: %s\n", ASSERT_EQ(info.si_pid, parent_tid);
test_name, parent_tid, pidfd, strerror(errno));
close(pidfd);
if (info.si_signo != SIGCHLD)
ksft_exit_fail_msg(
"%s test: unexpected si_signo value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_signo, parent_tid, pidfd,
strerror(errno));
if (info.si_code != CLD_EXITED)
ksft_exit_fail_msg(
"%s test: unexpected si_code value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_code, parent_tid, pidfd,
strerror(errno));
if (info.si_pid != parent_tid)
ksft_exit_fail_msg(
"%s test: unexpected si_pid value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_pid, parent_tid, pidfd,
strerror(errno));
ksft_test_result_pass("%s test: Passed\n", test_name);
return 0;
} }
static int test_pidfd_wait_states(void) TEST(wait_states)
{ {
const char *test_name = "pidfd wait states";
int pidfd = -1, status = 0; int pidfd = -1, status = 0;
pid_t parent_tid = -1; pid_t parent_tid = -1;
struct clone_args args = { struct clone_args args = {
...@@ -135,9 +98,7 @@ static int test_pidfd_wait_states(void) ...@@ -135,9 +98,7 @@ static int test_pidfd_wait_states(void)
}; };
pid = sys_clone3(&args); pid = sys_clone3(&args);
if (pid < 0) ASSERT_GE(pid, 0);
ksft_exit_fail_msg("%s test: failed to create new process %s\n",
test_name, strerror(errno));
if (pid == 0) { if (pid == 0) {
kill(getpid(), SIGSTOP); kill(getpid(), SIGSTOP);
...@@ -145,127 +106,31 @@ static int test_pidfd_wait_states(void) ...@@ -145,127 +106,31 @@ static int test_pidfd_wait_states(void)
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
ret = sys_waitid(P_PIDFD, pidfd, &info, WSTOPPED, NULL); ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WSTOPPED, NULL), 0);
if (ret < 0) ASSERT_EQ(info.si_signo, SIGCHLD);
ksft_exit_fail_msg( ASSERT_EQ(info.si_code, CLD_STOPPED);
"%s test: failed to wait on WSTOPPED process with pid %d and pidfd %d: %s\n", ASSERT_EQ(info.si_pid, parent_tid);
test_name, parent_tid, pidfd, strerror(errno));
if (info.si_signo != SIGCHLD)
ksft_exit_fail_msg(
"%s test: unexpected si_signo value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_signo, parent_tid, pidfd,
strerror(errno));
if (info.si_code != CLD_STOPPED)
ksft_exit_fail_msg(
"%s test: unexpected si_code value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_code, parent_tid, pidfd,
strerror(errno));
if (info.si_pid != parent_tid)
ksft_exit_fail_msg(
"%s test: unexpected si_pid value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_pid, parent_tid, pidfd,
strerror(errno));
ret = sys_pidfd_send_signal(pidfd, SIGCONT, NULL, 0);
if (ret < 0)
ksft_exit_fail_msg(
"%s test: failed to send signal to process with pid %d and pidfd %d: %s\n",
test_name, parent_tid, pidfd, strerror(errno));
ret = sys_waitid(P_PIDFD, pidfd, &info, WCONTINUED, NULL);
if (ret < 0)
ksft_exit_fail_msg(
"%s test: failed to wait WCONTINUED on process with pid %d and pidfd %d: %s\n",
test_name, parent_tid, pidfd, strerror(errno));
if (info.si_signo != SIGCHLD) ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGCONT, NULL, 0), 0);
ksft_exit_fail_msg(
"%s test: unexpected si_signo value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_signo, parent_tid, pidfd,
strerror(errno));
if (info.si_code != CLD_CONTINUED) ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WCONTINUED, NULL), 0);
ksft_exit_fail_msg( ASSERT_EQ(info.si_signo, SIGCHLD);
"%s test: unexpected si_code value %d received after waiting on process with pid %d and pidfd %d: %s\n", ASSERT_EQ(info.si_code, CLD_CONTINUED);
test_name, info.si_code, parent_tid, pidfd, ASSERT_EQ(info.si_pid, parent_tid);
strerror(errno));
if (info.si_pid != parent_tid) ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WUNTRACED, NULL), 0);
ksft_exit_fail_msg( ASSERT_EQ(info.si_signo, SIGCHLD);
"%s test: unexpected si_pid value %d received after waiting on process with pid %d and pidfd %d: %s\n", ASSERT_EQ(info.si_code, CLD_STOPPED);
test_name, info.si_pid, parent_tid, pidfd, ASSERT_EQ(info.si_pid, parent_tid);
strerror(errno));
ret = sys_waitid(P_PIDFD, pidfd, &info, WUNTRACED, NULL); ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0), 0);
if (ret < 0)
ksft_exit_fail_msg(
"%s test: failed to wait on WUNTRACED process with pid %d and pidfd %d: %s\n",
test_name, parent_tid, pidfd, strerror(errno));
if (info.si_signo != SIGCHLD) ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL), 0);
ksft_exit_fail_msg( ASSERT_EQ(info.si_signo, SIGCHLD);
"%s test: unexpected si_signo value %d received after waiting on process with pid %d and pidfd %d: %s\n", ASSERT_EQ(info.si_code, CLD_KILLED);
test_name, info.si_signo, parent_tid, pidfd, ASSERT_EQ(info.si_pid, parent_tid);
strerror(errno));
if (info.si_code != CLD_STOPPED) EXPECT_EQ(close(pidfd), 0);
ksft_exit_fail_msg(
"%s test: unexpected si_code value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_code, parent_tid, pidfd,
strerror(errno));
if (info.si_pid != parent_tid)
ksft_exit_fail_msg(
"%s test: unexpected si_pid value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_pid, parent_tid, pidfd,
strerror(errno));
ret = sys_pidfd_send_signal(pidfd, SIGKILL, NULL, 0);
if (ret < 0)
ksft_exit_fail_msg(
"%s test: failed to send SIGKILL to process with pid %d and pidfd %d: %s\n",
test_name, parent_tid, pidfd, strerror(errno));
ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
if (ret < 0)
ksft_exit_fail_msg(
"%s test: failed to wait on WEXITED process with pid %d and pidfd %d: %s\n",
test_name, parent_tid, pidfd, strerror(errno));
if (info.si_signo != SIGCHLD)
ksft_exit_fail_msg(
"%s test: unexpected si_signo value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_signo, parent_tid, pidfd,
strerror(errno));
if (info.si_code != CLD_KILLED)
ksft_exit_fail_msg(
"%s test: unexpected si_code value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_code, parent_tid, pidfd,
strerror(errno));
if (info.si_pid != parent_tid)
ksft_exit_fail_msg(
"%s test: unexpected si_pid value %d received after waiting on process with pid %d and pidfd %d: %s\n",
test_name, info.si_pid, parent_tid, pidfd,
strerror(errno));
close(pidfd);
ksft_test_result_pass("%s test: Passed\n", test_name);
return 0;
} }
int main(int argc, char **argv) TEST_HARNESS_MAIN
{
ksft_print_header();
ksft_set_plan(2);
test_pidfd_wait_simple();
test_pidfd_wait_states();
return ksft_exit_pass();
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment