Skip to content

Commit cd89597

Browse files
author
Christian Brauner
committed
tests: add waitid() tests for non-blocking pidfds
Verify that the PIDFD_NONBLOCK flag works with pidfd_open() and that waitid() with a non-blocking pidfd returns EAGAIN: TAP version 13 1..3 # Starting 3 tests from 1 test cases. # RUN global.wait_simple ... # OK global.wait_simple ok 1 global.wait_simple # RUN global.wait_states ... # OK global.wait_states ok 2 global.wait_states # RUN global.wait_nonblock ... # OK global.wait_nonblock ok 3 global.wait_nonblock # PASSED: 3 / 3 tests passed. # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0 Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com> Reviewed-by: Josh Triplett <josh@joshtriplett.org> Cc: Shuah Khan <shuah@kernel.org> Cc: linux-kselftest@vger.kernel.org Link: https://lore.kernel.org/r/20200902102130.147672-5-christian.brauner@ubuntu.com
1 parent 09d1de1 commit cd89597

2 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎tools/testing/selftests/pidfd/pidfd.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
#define __NR_pidfd_getfd -1
4747
#endif
4848

49+
#ifndef PIDFD_NONBLOCK
50+
#define PIDFD_NONBLOCK O_NONBLOCK
51+
#endif
52+
4953
/*
5054
* The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
5155
* That means, when it wraps around any pid < 300 will be skipped.

‎tools/testing/selftests/pidfd/pidfd_wait.c‎

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121

2222
#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
2323

24+
/* Attempt to de-conflict with the selftests tree. */
25+
#ifndef SKIP
26+
#define SKIP(s, ...) XFAIL(s, ##__VA_ARGS__)
27+
#endif
28+
2429
static pid_t sys_clone3(struct clone_args *args)
2530
{
2631
return syscall(__NR_clone3, args, sizeof(struct clone_args));
@@ -65,7 +70,7 @@ TEST(wait_simple)
6570
pidfd = -1;
6671

6772
pid = sys_clone3(&args);
68-
ASSERT_GE(pid, 1);
73+
ASSERT_GE(pid, 0);
6974

7075
if (pid == 0)
7176
exit(EXIT_SUCCESS);
@@ -133,4 +138,88 @@ TEST(wait_states)
133138
EXPECT_EQ(close(pidfd), 0);
134139
}
135140

141+
TEST(wait_nonblock)
142+
{
143+
int pidfd, status = 0;
144+
unsigned int flags = 0;
145+
pid_t parent_tid = -1;
146+
struct clone_args args = {
147+
.parent_tid = ptr_to_u64(&parent_tid),
148+
.flags = CLONE_PARENT_SETTID,
149+
.exit_signal = SIGCHLD,
150+
};
151+
int ret;
152+
pid_t pid;
153+
siginfo_t info = {
154+
.si_signo = 0,
155+
};
156+
157+
/*
158+
* Callers need to see ECHILD with non-blocking pidfds when no child
159+
* processes exists.
160+
*/
161+
pidfd = sys_pidfd_open(getpid(), PIDFD_NONBLOCK);
162+
EXPECT_GE(pidfd, 0) {
163+
/* pidfd_open() doesn't support PIDFD_NONBLOCK. */
164+
ASSERT_EQ(errno, EINVAL);
165+
SKIP(return, "Skipping PIDFD_NONBLOCK test");
166+
}
167+
168+
ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
169+
ASSERT_LT(ret, 0);
170+
ASSERT_EQ(errno, ECHILD);
171+
EXPECT_EQ(close(pidfd), 0);
172+
173+
pid = sys_clone3(&args);
174+
ASSERT_GE(pid, 0);
175+
176+
if (pid == 0) {
177+
kill(getpid(), SIGSTOP);
178+
exit(EXIT_SUCCESS);
179+
}
180+
181+
pidfd = sys_pidfd_open(pid, PIDFD_NONBLOCK);
182+
EXPECT_GE(pidfd, 0) {
183+
/* pidfd_open() doesn't support PIDFD_NONBLOCK. */
184+
ASSERT_EQ(errno, EINVAL);
185+
SKIP(return, "Skipping PIDFD_NONBLOCK test");
186+
}
187+
188+
flags = fcntl(pidfd, F_GETFL, 0);
189+
ASSERT_GT(flags, 0);
190+
ASSERT_GT((flags & O_NONBLOCK), 0);
191+
192+
/*
193+
* Callers need to see EAGAIN/EWOULDBLOCK with non-blocking pidfd when
194+
* child processes exist but none have exited.
195+
*/
196+
ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL);
197+
ASSERT_LT(ret, 0);
198+
ASSERT_EQ(errno, EAGAIN);
199+
200+
/*
201+
* Callers need to continue seeing 0 with non-blocking pidfd and
202+
* WNOHANG raised explicitly when child processes exist but none have
203+
* exited.
204+
*/
205+
ret = sys_waitid(P_PIDFD, pidfd, &info, WEXITED | WNOHANG, NULL);
206+
ASSERT_EQ(ret, 0);
207+
208+
ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WSTOPPED, NULL), 0);
209+
ASSERT_EQ(info.si_signo, SIGCHLD);
210+
ASSERT_EQ(info.si_code, CLD_STOPPED);
211+
ASSERT_EQ(info.si_pid, parent_tid);
212+
213+
ASSERT_EQ(sys_pidfd_send_signal(pidfd, SIGCONT, NULL, 0), 0);
214+
215+
ASSERT_EQ(fcntl(pidfd, F_SETFL, (flags & ~O_NONBLOCK)), 0);
216+
217+
ASSERT_EQ(sys_waitid(P_PIDFD, pidfd, &info, WEXITED, NULL), 0);
218+
ASSERT_EQ(info.si_signo, SIGCHLD);
219+
ASSERT_EQ(info.si_code, CLD_EXITED);
220+
ASSERT_EQ(info.si_pid, parent_tid);
221+
222+
EXPECT_EQ(close(pidfd), 0);
223+
}
224+
136225
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)