1 /* SPDX-License-Identifier: GPL-2.0 */ 2 3 #ifndef _CLONE3_SELFTESTS_H 4 #define _CLONE3_SELFTESTS_H 5 6 #define _GNU_SOURCE 7 #include <sched.h> 8 #include <linux/sched.h> 9 #include <linux/types.h> 10 #include <stdint.h> 11 #include <syscall.h> 12 #include <sys/wait.h> 13 14 #include "../kselftest.h" 15 16 #define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr))) 17 18 #ifndef CLONE_INTO_CGROUP 19 #define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */ 20 #endif 21 22 #ifndef __NR_clone3 23 #define __NR_clone3 -1 24 #endif 25 26 struct __clone_args { 27 __aligned_u64 flags; 28 __aligned_u64 pidfd; 29 __aligned_u64 child_tid; 30 __aligned_u64 parent_tid; 31 __aligned_u64 exit_signal; 32 __aligned_u64 stack; 33 __aligned_u64 stack_size; 34 __aligned_u64 tls; 35 #ifndef CLONE_ARGS_SIZE_VER0 36 #define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */ 37 #endif 38 __aligned_u64 set_tid; 39 __aligned_u64 set_tid_size; 40 #ifndef CLONE_ARGS_SIZE_VER1 41 #define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */ 42 #endif 43 __aligned_u64 cgroup; 44 #ifndef CLONE_ARGS_SIZE_VER2 45 #define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */ 46 #endif 47 }; 48 49 static pid_t sys_clone3(struct __clone_args *args, size_t size) 50 { 51 fflush(stdout); 52 fflush(stderr); 53 return syscall(__NR_clone3, args, size); 54 } 55 56 static inline void test_clone3_supported(void) 57 { 58 pid_t pid; 59 struct __clone_args args = {}; 60 61 if (__NR_clone3 < 0) 62 ksft_exit_skip("clone3() syscall is not supported\n"); 63 64 /* Set to something that will always cause EINVAL. */ 65 args.exit_signal = -1; 66 pid = sys_clone3(&args, sizeof(args)); 67 if (!pid) 68 exit(EXIT_SUCCESS); 69 70 if (pid > 0) { 71 wait(NULL); 72 ksft_exit_fail_msg( 73 "Managed to create child process with invalid exit_signal\n"); 74 } 75 76 if (errno == ENOSYS) 77 ksft_exit_skip("clone3() syscall is not supported\n"); 78 79 ksft_print_msg("clone3() syscall supported\n"); 80 } 81 82 #endif /* _CLONE3_SELFTESTS_H */ 83