Skip to content

Commit 853ab1f

Browse files
committed
selftests/pidfd: add first PIDFD_INFO_EXIT selftest
Add a selftest for PIDFD_INFO_EXIT behavior. Link: https://lore.kernel.org/r/20250305-work-pidfs-kill_on_last_close-v3-10-c8c3d8361705@kernel.org Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent ddf5315 commit 853ab1f

4 files changed

Lines changed: 153 additions & 2 deletions

File tree

tools/testing/selftests/pidfd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ pidfd_getfd_test
88
pidfd_setns_test
99
pidfd_file_handle_test
1010
pidfd_bind_mount
11+
pidfd_info_test

tools/testing/selftests/pidfd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CFLAGS += -g $(KHDR_INCLUDES) -pthread -Wall
33

44
TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \
55
pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test \
6-
pidfd_file_handle_test pidfd_bind_mount
6+
pidfd_file_handle_test pidfd_bind_mount pidfd_info_test
77

88
include ../lib.mk
99

tools/testing/selftests/pidfd/pidfd.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@
127127
#define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, even if not requested */
128128
#endif
129129

130+
#ifndef PIDFD_INFO_EXIT
131+
#define PIDFD_INFO_EXIT (1UL << 3) /* Always returned if available, even if not requested */
132+
#endif
133+
130134
struct pidfd_info {
131135
__u64 mask;
132136
__u64 cgroupid;
@@ -141,7 +145,7 @@ struct pidfd_info {
141145
__u32 sgid;
142146
__u32 fsuid;
143147
__u32 fsgid;
144-
__u32 spare0[1];
148+
__s32 exit_code;
145149
};
146150

147151
/*
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#define _GNU_SOURCE
4+
#include <errno.h>
5+
#include <fcntl.h>
6+
#include <limits.h>
7+
#include <linux/types.h>
8+
#include <poll.h>
9+
#include <pthread.h>
10+
#include <sched.h>
11+
#include <signal.h>
12+
#include <stdio.h>
13+
#include <stdlib.h>
14+
#include <string.h>
15+
#include <syscall.h>
16+
#include <sys/prctl.h>
17+
#include <sys/wait.h>
18+
#include <unistd.h>
19+
#include <sys/socket.h>
20+
#include <linux/kcmp.h>
21+
#include <sys/stat.h>
22+
23+
#include "pidfd.h"
24+
#include "../kselftest_harness.h"
25+
26+
FIXTURE(pidfd_info)
27+
{
28+
pid_t child_pid1;
29+
int child_pidfd1;
30+
31+
pid_t child_pid2;
32+
int child_pidfd2;
33+
34+
pid_t child_pid3;
35+
int child_pidfd3;
36+
37+
pid_t child_pid4;
38+
int child_pidfd4;
39+
};
40+
41+
FIXTURE_SETUP(pidfd_info)
42+
{
43+
int ret;
44+
int ipc_sockets[2];
45+
char c;
46+
47+
ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
48+
EXPECT_EQ(ret, 0);
49+
50+
self->child_pid1 = create_child(&self->child_pidfd1, 0);
51+
EXPECT_GE(self->child_pid1, 0);
52+
53+
if (self->child_pid1 == 0) {
54+
close(ipc_sockets[0]);
55+
56+
if (write_nointr(ipc_sockets[1], "1", 1) < 0)
57+
_exit(EXIT_FAILURE);
58+
59+
close(ipc_sockets[1]);
60+
61+
pause();
62+
_exit(EXIT_SUCCESS);
63+
}
64+
65+
EXPECT_EQ(close(ipc_sockets[1]), 0);
66+
ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
67+
EXPECT_EQ(close(ipc_sockets[0]), 0);
68+
69+
/* SIGKILL but don't reap. */
70+
EXPECT_EQ(sys_pidfd_send_signal(self->child_pidfd1, SIGKILL, NULL, 0), 0);
71+
72+
ret = socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, ipc_sockets);
73+
EXPECT_EQ(ret, 0);
74+
75+
self->child_pid2 = create_child(&self->child_pidfd2, 0);
76+
EXPECT_GE(self->child_pid2, 0);
77+
78+
if (self->child_pid2 == 0) {
79+
close(ipc_sockets[0]);
80+
81+
if (write_nointr(ipc_sockets[1], "1", 1) < 0)
82+
_exit(EXIT_FAILURE);
83+
84+
close(ipc_sockets[1]);
85+
86+
pause();
87+
_exit(EXIT_SUCCESS);
88+
}
89+
90+
EXPECT_EQ(close(ipc_sockets[1]), 0);
91+
ASSERT_EQ(read_nointr(ipc_sockets[0], &c, 1), 1);
92+
EXPECT_EQ(close(ipc_sockets[0]), 0);
93+
94+
/* SIGKILL and reap. */
95+
EXPECT_EQ(sys_pidfd_send_signal(self->child_pidfd2, SIGKILL, NULL, 0), 0);
96+
EXPECT_EQ(sys_waitid(P_PID, self->child_pid2, NULL, WEXITED), 0);
97+
98+
self->child_pid3 = create_child(&self->child_pidfd3, CLONE_NEWUSER | CLONE_NEWPID);
99+
EXPECT_GE(self->child_pid3, 0);
100+
101+
if (self->child_pid3 == 0)
102+
_exit(EXIT_SUCCESS);
103+
104+
self->child_pid4 = create_child(&self->child_pidfd4, CLONE_NEWUSER | CLONE_NEWPID);
105+
EXPECT_GE(self->child_pid4, 0);
106+
107+
if (self->child_pid4 == 0)
108+
_exit(EXIT_SUCCESS);
109+
110+
EXPECT_EQ(sys_waitid(P_PID, self->child_pid4, NULL, WEXITED), 0);
111+
}
112+
113+
FIXTURE_TEARDOWN(pidfd_info)
114+
{
115+
sys_pidfd_send_signal(self->child_pidfd1, SIGKILL, NULL, 0);
116+
if (self->child_pidfd1 >= 0)
117+
EXPECT_EQ(0, close(self->child_pidfd1));
118+
119+
sys_waitid(P_PID, self->child_pid1, NULL, WEXITED);
120+
121+
sys_pidfd_send_signal(self->child_pidfd2, SIGKILL, NULL, 0);
122+
if (self->child_pidfd2 >= 0)
123+
EXPECT_EQ(0, close(self->child_pidfd2));
124+
125+
sys_waitid(P_PID, self->child_pid2, NULL, WEXITED);
126+
sys_waitid(P_PID, self->child_pid3, NULL, WEXITED);
127+
sys_waitid(P_PID, self->child_pid4, NULL, WEXITED);
128+
}
129+
130+
TEST_F(pidfd_info, sigkill_exit)
131+
{
132+
struct pidfd_info info = {
133+
.mask = PIDFD_INFO_CGROUPID,
134+
};
135+
136+
/* Process has exited but not been reaped so this must work. */
137+
ASSERT_EQ(ioctl(self->child_pidfd1, PIDFD_GET_INFO, &info), 0);
138+
139+
info.mask = PIDFD_INFO_CGROUPID | PIDFD_INFO_EXIT;
140+
ASSERT_EQ(ioctl(self->child_pidfd1, PIDFD_GET_INFO, &info), 0);
141+
ASSERT_TRUE(!!(info.mask & PIDFD_INFO_CREDS));
142+
/* Process has exited but not been reaped, so no PIDFD_INFO_EXIT information yet. */
143+
ASSERT_FALSE(!!(info.mask & PIDFD_INFO_EXIT));
144+
}
145+
146+
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)