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_COREDUMPED 152 #define PIDFD_COREDUMPED (1U << 0) /* Did crash and... */ 153 #endif 154 155 #ifndef PIDFD_COREDUMP_SKIP 156 #define PIDFD_COREDUMP_SKIP (1U << 1) /* coredumping generation was skipped. */ 157 #endif 158 159 #ifndef PIDFD_COREDUMP_USER 160 #define PIDFD_COREDUMP_USER (1U << 2) /* coredump was done as the user. */ 161 #endif 162 163 #ifndef PIDFD_COREDUMP_ROOT 164 #define PIDFD_COREDUMP_ROOT (1U << 3) /* coredump was done as root. */ 165 #endif 166 167 #ifndef PIDFD_THREAD 168 #define PIDFD_THREAD O_EXCL 169 #endif 170 171 struct pidfd_info { 172 __u64 mask; 173 __u64 cgroupid; 174 __u32 pid; 175 __u32 tgid; 176 __u32 ppid; 177 __u32 ruid; 178 __u32 rgid; 179 __u32 euid; 180 __u32 egid; 181 __u32 suid; 182 __u32 sgid; 183 __u32 fsuid; 184 __u32 fsgid; 185 __s32 exit_code; 186 __u32 coredump_mask; 187 __u32 __spare1; 188 }; 189 190 /* 191 * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c 192 * That means, when it wraps around any pid < 300 will be skipped. 193 * So we need to use a pid > 300 in order to test recycling. 194 */ 195 #define PID_RECYCLE 1000 196 197 /* 198 * Define a few custom error codes for the child process to clearly indicate 199 * what is happening. This way we can tell the difference between a system 200 * error, a test error, etc. 201 */ 202 #define PIDFD_PASS 0 203 #define PIDFD_FAIL 1 204 #define PIDFD_ERROR 2 205 #define PIDFD_SKIP 3 206 #define PIDFD_XFAIL 4 207 208 static inline int sys_waitid(int which, pid_t pid, siginfo_t *info, int options) 209 { 210 return syscall(__NR_waitid, which, pid, info, options, NULL); 211 } 212 213 static inline int wait_for_pid(pid_t pid) 214 { 215 int status, ret; 216 217 again: 218 ret = waitpid(pid, &status, 0); 219 if (ret == -1) { 220 if (errno == EINTR) 221 goto again; 222 223 ksft_print_msg("waitpid returned -1, errno=%d\n", errno); 224 return -1; 225 } 226 227 if (!WIFEXITED(status)) { 228 ksft_print_msg( 229 "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n", 230 WIFSIGNALED(status), WTERMSIG(status)); 231 return -1; 232 } 233 234 ret = WEXITSTATUS(status); 235 return ret; 236 } 237 238 static inline int sys_pidfd_open(pid_t pid, unsigned int flags) 239 { 240 return syscall(__NR_pidfd_open, pid, flags); 241 } 242 243 static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info, 244 unsigned int flags) 245 { 246 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); 247 } 248 249 static inline int sys_pidfd_getfd(int pidfd, int fd, int flags) 250 { 251 return syscall(__NR_pidfd_getfd, pidfd, fd, flags); 252 } 253 254 static inline int sys_memfd_create(const char *name, unsigned int flags) 255 { 256 return syscall(__NR_memfd_create, name, flags); 257 } 258 259 static inline pid_t create_child(int *pidfd, unsigned flags) 260 { 261 struct __clone_args args = { 262 .flags = CLONE_PIDFD | flags, 263 .exit_signal = SIGCHLD, 264 .pidfd = ptr_to_u64(pidfd), 265 }; 266 267 return sys_clone3(&args, sizeof(struct __clone_args)); 268 } 269 270 static inline ssize_t read_nointr(int fd, void *buf, size_t count) 271 { 272 ssize_t ret; 273 274 do { 275 ret = read(fd, buf, count); 276 } while (ret < 0 && errno == EINTR); 277 278 return ret; 279 } 280 281 static inline ssize_t write_nointr(int fd, const void *buf, size_t count) 282 { 283 ssize_t ret; 284 285 do { 286 ret = write(fd, buf, count); 287 } while (ret < 0 && errno == EINTR); 288 289 return ret; 290 } 291 292 static inline int sys_execveat(int dirfd, const char *pathname, 293 char *const argv[], char *const envp[], 294 int flags) 295 { 296 return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags); 297 } 298 299 #endif /* __PIDFD_H */ 300