projects/linux·blob master · refs

315 lines · 6.8 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2
3#ifndef __PIDFD_H
4#define __PIDFD_H
5
6#define _GNU_SOURCE
7#include <errno.h>
8#include <fcntl.h>
9#include <sched.h>
10#include <signal.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <syscall.h>
15#include <sys/ioctl.h>
16#include <sys/types.h>
17#include <sys/wait.h>
18
19/*
20 * Remove the userspace definitions of the following preprocessor symbols
21 * to avoid duplicate-definition warnings from the subsequent in-kernel
22 * definitions.
23 */
24#undef SCHED_NORMAL
25#undef SCHED_FLAG_KEEP_ALL
26#undef SCHED_FLAG_UTIL_CLAMP
27
28#include "kselftest.h"
29#include "../clone3/clone3_selftests.h"
30
31#ifndef FD_PIDFS_ROOT
32#define FD_PIDFS_ROOT -10002
33#endif
34
35#ifndef P_PIDFD
36#define P_PIDFD 3
37#endif
38
39#ifndef CLONE_NEWTIME
40#define CLONE_NEWTIME 0x00000080
41#endif
42
43#ifndef CLONE_PIDFD
44#define CLONE_PIDFD 0x00001000
45#endif
46
47#ifndef __NR_pidfd_open
48#define __NR_pidfd_open 434
49#endif
50
51#ifndef __NR_pidfd_send_signal
52#define __NR_pidfd_send_signal 424
53#endif
54
55#ifndef __NR_clone3
56#define __NR_clone3 435
57#endif
58
59#ifndef __NR_pidfd_getfd
60#define __NR_pidfd_getfd 438
61#endif
62
63#ifndef PIDFD_NONBLOCK
64#define PIDFD_NONBLOCK O_NONBLOCK
65#endif
66
67#ifndef PIDFD_SELF_THREAD
68#define PIDFD_SELF_THREAD -10000 /* Current thread. */
69#endif
70
71#ifndef PIDFD_SELF_THREAD_GROUP
72#define PIDFD_SELF_THREAD_GROUP -10001 /* Current thread group leader. */
73#endif
74
75#ifndef PIDFD_SELF
76#define PIDFD_SELF PIDFD_SELF_THREAD
77#endif
78
79#ifndef PIDFD_SELF_PROCESS
80#define PIDFD_SELF_PROCESS PIDFD_SELF_THREAD_GROUP
81#endif
82
83#ifndef PIDFS_IOCTL_MAGIC
84#define PIDFS_IOCTL_MAGIC 0xFF
85#endif
86
87#ifndef PIDFD_GET_CGROUP_NAMESPACE
88#define PIDFD_GET_CGROUP_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 1)
89#endif
90
91#ifndef PIDFD_GET_IPC_NAMESPACE
92#define PIDFD_GET_IPC_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 2)
93#endif
94
95#ifndef PIDFD_GET_MNT_NAMESPACE
96#define PIDFD_GET_MNT_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 3)
97#endif
98
99#ifndef PIDFD_GET_NET_NAMESPACE
100#define PIDFD_GET_NET_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 4)
101#endif
102
103#ifndef PIDFD_GET_PID_NAMESPACE
104#define PIDFD_GET_PID_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 5)
105#endif
106
107#ifndef PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE
108#define PIDFD_GET_PID_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 6)
109#endif
110
111#ifndef PIDFD_GET_TIME_NAMESPACE
112#define PIDFD_GET_TIME_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 7)
113#endif
114
115#ifndef PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE
116#define PIDFD_GET_TIME_FOR_CHILDREN_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 8)
117#endif
118
119#ifndef PIDFD_GET_USER_NAMESPACE
120#define PIDFD_GET_USER_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 9)
121#endif
122
123#ifndef PIDFD_GET_UTS_NAMESPACE
124#define PIDFD_GET_UTS_NAMESPACE _IO(PIDFS_IOCTL_MAGIC, 10)
125#endif
126
127#ifndef PIDFD_GET_INFO
128#define PIDFD_GET_INFO _IOWR(PIDFS_IOCTL_MAGIC, 11, struct pidfd_info)
129#endif
130
131#ifndef PIDFD_INFO_PID
132#define PIDFD_INFO_PID (1UL << 0) /* Always returned, even if not requested */
133#endif
134
135#ifndef PIDFD_INFO_CREDS
136#define PIDFD_INFO_CREDS (1UL << 1) /* Always returned, even if not requested */
137#endif
138
139#ifndef PIDFD_INFO_CGROUPID
140#define PIDFD_INFO_CGROUPID (1UL << 2) /* Always returned if available, even if not requested */
141#endif
142
143#ifndef PIDFD_INFO_EXIT
144#define PIDFD_INFO_EXIT (1UL << 3) /* Always returned if available, even if not requested */
145#endif
146
147#ifndef PIDFD_INFO_COREDUMP
148#define PIDFD_INFO_COREDUMP (1UL << 4)
149#endif
150
151#ifndef PIDFD_INFO_SUPPORTED_MASK
152#define PIDFD_INFO_SUPPORTED_MASK (1UL << 5)
153#endif
154
155#ifndef PIDFD_INFO_COREDUMP_SIGNAL
156#define PIDFD_INFO_COREDUMP_SIGNAL (1UL << 6)
157#endif
158
159#ifndef PIDFD_INFO_COREDUMP_CODE
160#define PIDFD_INFO_COREDUMP_CODE (1UL << 7)
161#endif
162
163#ifndef PIDFD_COREDUMPED
164#define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */
165#endif
166
167#ifndef PIDFD_COREDUMP_SKIP
168#define PIDFD_COREDUMP_SKIP (1U << 1) /* coredumping generation was skipped. */
169#endif
170
171#ifndef PIDFD_COREDUMP_USER
172#define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */
173#endif
174
175#ifndef PIDFD_COREDUMP_ROOT
176#define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */
177#endif
178
179#ifndef PIDFD_THREAD
180#define PIDFD_THREAD O_EXCL
181#endif
182
183struct pidfd_info {
184 __u64 mask;
185 __u64 cgroupid;
186 __u32 pid;
187 __u32 tgid;
188 __u32 ppid;
189 __u32 ruid;
190 __u32 rgid;
191 __u32 euid;
192 __u32 egid;
193 __u32 suid;
194 __u32 sgid;
195 __u32 fsuid;
196 __u32 fsgid;
197 __s32 exit_code;
198 struct {
199 __u32 coredump_mask;
200 __u32 coredump_signal;
201 __u32 coredump_code;
202 };
203 __u64 supported_mask;
204};
205
206/*
207 * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c
208 * That means, when it wraps around any pid < 300 will be skipped.
209 * So we need to use a pid > 300 in order to test recycling.
210 */
211#define PID_RECYCLE 1000
212
213/*
214 * Define a few custom error codes for the child process to clearly indicate
215 * what is happening. This way we can tell the difference between a system
216 * error, a test error, etc.
217 */
218#define PIDFD_PASS 0
219#define PIDFD_FAIL 1
220#define PIDFD_ERROR 2
221#define PIDFD_SKIP 3
222#define PIDFD_XFAIL 4
223
224static inline int sys_waitid(int which, pid_t pid, siginfo_t *info, int options)
225{
226 return syscall(__NR_waitid, which, pid, info, options, NULL);
227}
228
229static inline int wait_for_pid(pid_t pid)
230{
231 int status, ret;
232
233again:
234 ret = waitpid(pid, &status, 0);
235 if (ret == -1) {
236 if (errno == EINTR)
237 goto again;
238
239 ksft_print_msg("waitpid returned -1, errno=%d\n", errno);
240 return -1;
241 }
242
243 if (!WIFEXITED(status)) {
244 ksft_print_msg(
245 "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n",
246 WIFSIGNALED(status), WTERMSIG(status));
247 return -1;
248 }
249
250 ret = WEXITSTATUS(status);
251 return ret;
252}
253
254static inline int sys_pidfd_open(pid_t pid, unsigned int flags)
255{
256 return syscall(__NR_pidfd_open, pid, flags);
257}
258
259static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
260 unsigned int flags)
261{
262 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
263}
264
265static inline int sys_pidfd_getfd(int pidfd, int fd, int flags)
266{
267 return syscall(__NR_pidfd_getfd, pidfd, fd, flags);
268}
269
270static inline int sys_memfd_create(const char *name, unsigned int flags)
271{
272 return syscall(__NR_memfd_create, name, flags);
273}
274
275static inline pid_t create_child(int *pidfd, unsigned flags)
276{
277 struct __clone_args args = {
278 .flags = CLONE_PIDFD | flags,
279 .exit_signal = SIGCHLD,
280 .pidfd = ptr_to_u64(pidfd),
281 };
282
283 return sys_clone3(&args, sizeof(struct __clone_args));
284}
285
286static inline ssize_t read_nointr(int fd, void *buf, size_t count)
287{
288 ssize_t ret;
289
290 do {
291 ret = read(fd, buf, count);
292 } while (ret < 0 && errno == EINTR);
293
294 return ret;
295}
296
297static inline ssize_t write_nointr(int fd, const void *buf, size_t count)
298{
299 ssize_t ret;
300
301 do {
302 ret = write(fd, buf, count);
303 } while (ret < 0 && errno == EINTR);
304
305 return ret;
306}
307
308static inline int sys_execveat(int dirfd, const char *pathname,
309 char *const argv[], char *const envp[],
310 int flags)
311{
312 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
313}
314
315#endif /* __PIDFD_H */