Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 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> |
Christian Brauner | 6952a4f | 2020-03-08 17:26:32 +0100 | [diff] [blame] | 16 | #include <sys/types.h> |
| 17 | #include <sys/wait.h> |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 18 | |
| 19 | #include "../kselftest.h" |
| 20 | |
Christian Brauner | e63f308 | 2019-07-28 00:22:30 +0200 | [diff] [blame] | 21 | #ifndef P_PIDFD |
| 22 | #define P_PIDFD 3 |
| 23 | #endif |
| 24 | |
Christian Brauner | 55d9ad9 | 2020-07-06 17:49:12 +0200 | [diff] [blame] | 25 | #ifndef CLONE_NEWTIME |
| 26 | #define CLONE_NEWTIME 0x00000080 |
| 27 | #endif |
| 28 | |
Christian Brauner | e63f308 | 2019-07-28 00:22:30 +0200 | [diff] [blame] | 29 | #ifndef CLONE_PIDFD |
| 30 | #define CLONE_PIDFD 0x00001000 |
| 31 | #endif |
| 32 | |
| 33 | #ifndef __NR_pidfd_open |
| 34 | #define __NR_pidfd_open -1 |
| 35 | #endif |
| 36 | |
| 37 | #ifndef __NR_pidfd_send_signal |
| 38 | #define __NR_pidfd_send_signal -1 |
| 39 | #endif |
| 40 | |
| 41 | #ifndef __NR_clone3 |
| 42 | #define __NR_clone3 -1 |
| 43 | #endif |
| 44 | |
Sargun Dhillon | 873dfd7 | 2020-01-07 09:59:27 -0800 | [diff] [blame] | 45 | #ifndef __NR_pidfd_getfd |
| 46 | #define __NR_pidfd_getfd -1 |
| 47 | #endif |
| 48 | |
Christian Brauner | cd89597 | 2020-09-02 12:21:30 +0200 | [diff] [blame] | 49 | #ifndef PIDFD_NONBLOCK |
| 50 | #define PIDFD_NONBLOCK O_NONBLOCK |
| 51 | #endif |
| 52 | |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 53 | /* |
| 54 | * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c |
| 55 | * That means, when it wraps around any pid < 300 will be skipped. |
| 56 | * So we need to use a pid > 300 in order to test recycling. |
| 57 | */ |
| 58 | #define PID_RECYCLE 1000 |
| 59 | |
| 60 | /* |
| 61 | * Define a few custom error codes for the child process to clearly indicate |
| 62 | * what is happening. This way we can tell the difference between a system |
| 63 | * error, a test error, etc. |
| 64 | */ |
| 65 | #define PIDFD_PASS 0 |
| 66 | #define PIDFD_FAIL 1 |
| 67 | #define PIDFD_ERROR 2 |
| 68 | #define PIDFD_SKIP 3 |
| 69 | #define PIDFD_XFAIL 4 |
| 70 | |
Axel Rasmussen | 4cbd93c | 2022-01-27 13:29:51 -0800 | [diff] [blame] | 71 | static inline int wait_for_pid(pid_t pid) |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 72 | { |
| 73 | int status, ret; |
| 74 | |
| 75 | again: |
| 76 | ret = waitpid(pid, &status, 0); |
| 77 | if (ret == -1) { |
| 78 | if (errno == EINTR) |
| 79 | goto again; |
| 80 | |
Axel Rasmussen | 4cbd93c | 2022-01-27 13:29:51 -0800 | [diff] [blame] | 81 | ksft_print_msg("waitpid returned -1, errno=%d\n", errno); |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 82 | return -1; |
| 83 | } |
| 84 | |
Axel Rasmussen | 4cbd93c | 2022-01-27 13:29:51 -0800 | [diff] [blame] | 85 | if (!WIFEXITED(status)) { |
| 86 | ksft_print_msg( |
| 87 | "waitpid !WIFEXITED, WIFSIGNALED=%d, WTERMSIG=%d\n", |
| 88 | WIFSIGNALED(status), WTERMSIG(status)); |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 89 | return -1; |
Axel Rasmussen | 4cbd93c | 2022-01-27 13:29:51 -0800 | [diff] [blame] | 90 | } |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 91 | |
Axel Rasmussen | 4cbd93c | 2022-01-27 13:29:51 -0800 | [diff] [blame] | 92 | ret = WEXITSTATUS(status); |
| 93 | ksft_print_msg("waitpid WEXITSTATUS=%d\n", ret); |
| 94 | return ret; |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Suren Baghdasaryan | 2ec2f99 | 2019-07-26 09:22:25 -0700 | [diff] [blame] | 97 | static inline int sys_pidfd_open(pid_t pid, unsigned int flags) |
| 98 | { |
| 99 | return syscall(__NR_pidfd_open, pid, flags); |
| 100 | } |
| 101 | |
Christian Brauner | e63f308 | 2019-07-28 00:22:30 +0200 | [diff] [blame] | 102 | static inline int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info, |
| 103 | unsigned int flags) |
| 104 | { |
| 105 | return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags); |
| 106 | } |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 107 | |
Sargun Dhillon | 873dfd7 | 2020-01-07 09:59:27 -0800 | [diff] [blame] | 108 | static inline int sys_pidfd_getfd(int pidfd, int fd, int flags) |
| 109 | { |
| 110 | return syscall(__NR_pidfd_getfd, pidfd, fd, flags); |
| 111 | } |
| 112 | |
Christian Brauner | 86f5639 | 2020-06-17 00:48:54 +0200 | [diff] [blame] | 113 | static inline int sys_memfd_create(const char *name, unsigned int flags) |
| 114 | { |
| 115 | return syscall(__NR_memfd_create, name, flags); |
| 116 | } |
| 117 | |
Christian Brauner | 172bb24 | 2019-03-23 12:24:21 +0100 | [diff] [blame] | 118 | #endif /* __PIDFD_H */ |