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/mount.h> 16 17 #include "../kselftest.h" 18 19 #ifndef P_PIDFD 20 #define P_PIDFD 3 21 #endif 22 23 #ifndef CLONE_PIDFD 24 #define CLONE_PIDFD 0x00001000 25 #endif 26 27 #ifndef __NR_pidfd_open 28 #define __NR_pidfd_open -1 29 #endif 30 31 #ifndef __NR_pidfd_send_signal 32 #define __NR_pidfd_send_signal -1 33 #endif 34 35 #ifndef __NR_clone3 36 #define __NR_clone3 -1 37 #endif 38 39 #ifndef __NR_pidfd_getfd 40 #define __NR_pidfd_getfd -1 41 #endif 42 43 /* 44 * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c 45 * That means, when it wraps around any pid < 300 will be skipped. 46 * So we need to use a pid > 300 in order to test recycling. 47 */ 48 #define PID_RECYCLE 1000 49 50 /* 51 * Define a few custom error codes for the child process to clearly indicate 52 * what is happening. This way we can tell the difference between a system 53 * error, a test error, etc. 54 */ 55 #define PIDFD_PASS 0 56 #define PIDFD_FAIL 1 57 #define PIDFD_ERROR 2 58 #define PIDFD_SKIP 3 59 #define PIDFD_XFAIL 4 60 61 int wait_for_pid(pid_t pid) 62 { 63 int status, ret; 64 65 again: 66 ret = waitpid(pid, &status, 0); 67 if (ret == -1) { 68 if (errno == EINTR) 69 goto again; 70 71 return -1; 72 } 73 74 if (!WIFEXITED(status)) 75 return -1; 76 77 return WEXITSTATUS(status); 78 } 79 80 static inline int sys_pidfd_open(pid_t pid, unsigned int flags) 81 { 82 return syscall(__NR_pidfd_open, pid, flags); 83 } 84 85 static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info, 86 unsigned int flags) 87 { 88 return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); 89 } 90 91 static inline int sys_pidfd_getfd(int pidfd, int fd, int flags) 92 { 93 return syscall(__NR_pidfd_getfd, pidfd, fd, flags); 94 } 95 96 #endif /* __PIDFD_H */ 97