diff options
| -rw-r--r-- | tools/testing/selftests/pidfd/.gitignore | 1 | ||||
| -rw-r--r-- | tools/testing/selftests/pidfd/Makefile | 3 | ||||
| -rw-r--r-- | tools/testing/selftests/pidfd/pidfd.h | 57 | ||||
| -rw-r--r-- | tools/testing/selftests/pidfd/pidfd_open_test.c | 169 | ||||
| -rw-r--r-- | tools/testing/selftests/pidfd/pidfd_test.c | 48 |
5 files changed, 234 insertions, 44 deletions
diff --git a/tools/testing/selftests/pidfd/.gitignore b/tools/testing/selftests/pidfd/.gitignore index 822a1e63d045..16d84d117bc0 100644 --- a/tools/testing/selftests/pidfd/.gitignore +++ b/tools/testing/selftests/pidfd/.gitignore | |||
| @@ -1 +1,2 @@ | |||
| 1 | pidfd_open_test | ||
| 1 | pidfd_test | 2 | pidfd_test |
diff --git a/tools/testing/selftests/pidfd/Makefile b/tools/testing/selftests/pidfd/Makefile index 4b31c14f273c..720b2d884b3c 100644 --- a/tools/testing/selftests/pidfd/Makefile +++ b/tools/testing/selftests/pidfd/Makefile | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | # SPDX-License-Identifier: GPL-2.0-only | ||
| 1 | CFLAGS += -g -I../../../../usr/include/ -lpthread | 2 | CFLAGS += -g -I../../../../usr/include/ -lpthread |
| 2 | 3 | ||
| 3 | TEST_GEN_PROGS := pidfd_test | 4 | TEST_GEN_PROGS := pidfd_test pidfd_open_test |
| 4 | 5 | ||
| 5 | include ../lib.mk | 6 | include ../lib.mk |
| 6 | 7 | ||
diff --git a/tools/testing/selftests/pidfd/pidfd.h b/tools/testing/selftests/pidfd/pidfd.h new file mode 100644 index 000000000000..8452e910463f --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd.h | |||
| @@ -0,0 +1,57 @@ | |||
| 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 | /* | ||
| 20 | * The kernel reserves 300 pids via RESERVED_PIDS in kernel/pid.c | ||
| 21 | * That means, when it wraps around any pid < 300 will be skipped. | ||
| 22 | * So we need to use a pid > 300 in order to test recycling. | ||
| 23 | */ | ||
| 24 | #define PID_RECYCLE 1000 | ||
| 25 | |||
| 26 | /* | ||
| 27 | * Define a few custom error codes for the child process to clearly indicate | ||
| 28 | * what is happening. This way we can tell the difference between a system | ||
| 29 | * error, a test error, etc. | ||
| 30 | */ | ||
| 31 | #define PIDFD_PASS 0 | ||
| 32 | #define PIDFD_FAIL 1 | ||
| 33 | #define PIDFD_ERROR 2 | ||
| 34 | #define PIDFD_SKIP 3 | ||
| 35 | #define PIDFD_XFAIL 4 | ||
| 36 | |||
| 37 | int wait_for_pid(pid_t pid) | ||
| 38 | { | ||
| 39 | int status, ret; | ||
| 40 | |||
| 41 | again: | ||
| 42 | ret = waitpid(pid, &status, 0); | ||
| 43 | if (ret == -1) { | ||
| 44 | if (errno == EINTR) | ||
| 45 | goto again; | ||
| 46 | |||
| 47 | return -1; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (!WIFEXITED(status)) | ||
| 51 | return -1; | ||
| 52 | |||
| 53 | return WEXITSTATUS(status); | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | #endif /* __PIDFD_H */ | ||
diff --git a/tools/testing/selftests/pidfd/pidfd_open_test.c b/tools/testing/selftests/pidfd/pidfd_open_test.c new file mode 100644 index 000000000000..0377133dd6dc --- /dev/null +++ b/tools/testing/selftests/pidfd/pidfd_open_test.c | |||
| @@ -0,0 +1,169 @@ | |||
| 1 | // SPDX-License-Identifier: GPL-2.0 | ||
| 2 | |||
| 3 | #define _GNU_SOURCE | ||
| 4 | #include <errno.h> | ||
| 5 | #include <fcntl.h> | ||
| 6 | #include <inttypes.h> | ||
| 7 | #include <limits.h> | ||
| 8 | #include <linux/types.h> | ||
| 9 | #include <linux/wait.h> | ||
| 10 | #include <sched.h> | ||
| 11 | #include <signal.h> | ||
| 12 | #include <stdbool.h> | ||
| 13 | #include <stdio.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <string.h> | ||
| 16 | #include <syscall.h> | ||
| 17 | #include <sys/mount.h> | ||
| 18 | #include <sys/prctl.h> | ||
| 19 | #include <sys/wait.h> | ||
| 20 | #include <unistd.h> | ||
| 21 | |||
| 22 | #include "pidfd.h" | ||
| 23 | #include "../kselftest.h" | ||
| 24 | |||
| 25 | static inline int sys_pidfd_open(pid_t pid, unsigned int flags) | ||
| 26 | { | ||
| 27 | return syscall(__NR_pidfd_open, pid, flags); | ||
| 28 | } | ||
| 29 | |||
| 30 | static int safe_int(const char *numstr, int *converted) | ||
| 31 | { | ||
| 32 | char *err = NULL; | ||
| 33 | long sli; | ||
| 34 | |||
| 35 | errno = 0; | ||
| 36 | sli = strtol(numstr, &err, 0); | ||
| 37 | if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN)) | ||
| 38 | return -ERANGE; | ||
| 39 | |||
| 40 | if (errno != 0 && sli == 0) | ||
| 41 | return -EINVAL; | ||
| 42 | |||
| 43 | if (err == numstr || *err != '\0') | ||
| 44 | return -EINVAL; | ||
| 45 | |||
| 46 | if (sli > INT_MAX || sli < INT_MIN) | ||
| 47 | return -ERANGE; | ||
| 48 | |||
| 49 | *converted = (int)sli; | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | static int char_left_gc(const char *buffer, size_t len) | ||
| 54 | { | ||
| 55 | size_t i; | ||
| 56 | |||
| 57 | for (i = 0; i < len; i++) { | ||
| 58 | if (buffer[i] == ' ' || | ||
| 59 | buffer[i] == '\t') | ||
| 60 | continue; | ||
| 61 | |||
| 62 | return i; | ||
| 63 | } | ||
| 64 | |||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | static int char_right_gc(const char *buffer, size_t len) | ||
| 69 | { | ||
| 70 | int i; | ||
| 71 | |||
| 72 | for (i = len - 1; i >= 0; i--) { | ||
| 73 | if (buffer[i] == ' ' || | ||
| 74 | buffer[i] == '\t' || | ||
| 75 | buffer[i] == '\n' || | ||
| 76 | buffer[i] == '\0') | ||
| 77 | continue; | ||
| 78 | |||
| 79 | return i + 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | } | ||
| 84 | |||
| 85 | static char *trim_whitespace_in_place(char *buffer) | ||
| 86 | { | ||
| 87 | buffer += char_left_gc(buffer, strlen(buffer)); | ||
| 88 | buffer[char_right_gc(buffer, strlen(buffer))] = '\0'; | ||
| 89 | return buffer; | ||
| 90 | } | ||
| 91 | |||
| 92 | static pid_t get_pid_from_fdinfo_file(int pidfd, const char *key, size_t keylen) | ||
| 93 | { | ||
| 94 | int ret; | ||
| 95 | char path[512]; | ||
| 96 | FILE *f; | ||
| 97 | size_t n = 0; | ||
| 98 | pid_t result = -1; | ||
| 99 | char *line = NULL; | ||
| 100 | |||
| 101 | snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd); | ||
| 102 | |||
| 103 | f = fopen(path, "re"); | ||
| 104 | if (!f) | ||
| 105 | return -1; | ||
| 106 | |||
| 107 | while (getline(&line, &n, f) != -1) { | ||
| 108 | char *numstr; | ||
| 109 | |||
| 110 | if (strncmp(line, key, keylen)) | ||
| 111 | continue; | ||
| 112 | |||
| 113 | numstr = trim_whitespace_in_place(line + 4); | ||
| 114 | ret = safe_int(numstr, &result); | ||
| 115 | if (ret < 0) | ||
| 116 | goto out; | ||
| 117 | |||
| 118 | break; | ||
| 119 | } | ||
| 120 | |||
| 121 | out: | ||
| 122 | free(line); | ||
| 123 | fclose(f); | ||
| 124 | return result; | ||
| 125 | } | ||
| 126 | |||
| 127 | int main(int argc, char **argv) | ||
| 128 | { | ||
| 129 | int pidfd = -1, ret = 1; | ||
