Skip to content

Commit f0ece18

Browse files
tych0brauner
authored andcommitted
selftests: add ESRCH tests for pidfd_getfd()
Ensure that pidfd_getfd() reports -ESRCH if the task is already exiting. Signed-off-by: Tycho Andersen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 0c9bd6b commit f0ece18

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

tools/testing/selftests/pidfd/pidfd_getfd_test.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <fcntl.h>
66
#include <limits.h>
77
#include <linux/types.h>
8+
#include <poll.h>
89
#include <sched.h>
910
#include <signal.h>
1011
#include <stdio.h>
@@ -129,6 +130,7 @@ FIXTURE(child)
129130
* When it is closed, the child will exit.
130131
*/
131132
int sk;
133+
bool ignore_child_result;
132134
};
133135

134136
FIXTURE_SETUP(child)
@@ -165,10 +167,14 @@ FIXTURE_SETUP(child)
165167

166168
FIXTURE_TEARDOWN(child)
167169
{
170+
int ret;
171+
168172
EXPECT_EQ(0, close(self->pidfd));
169173
EXPECT_EQ(0, close(self->sk));
170174

171-
EXPECT_EQ(0, wait_for_pid(self->pid));
175+
ret = wait_for_pid(self->pid);
176+
if (!self->ignore_child_result)
177+
EXPECT_EQ(0, ret);
172178
}
173179

174180
TEST_F(child, disable_ptrace)
@@ -235,6 +241,29 @@ TEST(flags_set)
235241
EXPECT_EQ(errno, EINVAL);
236242
}
237243

244+
TEST_F(child, no_strange_EBADF)
245+
{
246+
struct pollfd fds;
247+
248+
self->ignore_child_result = true;
249+
250+
fds.fd = self->pidfd;
251+
fds.events = POLLIN;
252+
253+
ASSERT_EQ(kill(self->pid, SIGKILL), 0);
254+
ASSERT_EQ(poll(&fds, 1, 5000), 1);
255+
256+
/*
257+
* It used to be that pidfd_getfd() could race with the exiting thread
258+
* between exit_files() and release_task(), and get a non-null task
259+
* with a NULL files struct, and you'd get EBADF, which was slightly
260+
* confusing.
261+
*/
262+
errno = 0;
263+
EXPECT_EQ(sys_pidfd_getfd(self->pidfd, self->remote_fd, 0), -1);
264+
EXPECT_EQ(errno, ESRCH);
265+
}
266+
238267
#if __NR_pidfd_getfd == -1
239268
int main(void)
240269
{

0 commit comments

Comments
 (0)