2019-11-15 20:36:21 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
|
|
|
|
|
|
|
#ifndef _CLONE3_SELFTESTS_H
|
|
|
|
#define _CLONE3_SELFTESTS_H
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sched.h>
|
2020-02-05 21:26:23 +08:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/types.h>
|
2019-11-15 20:36:21 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <syscall.h>
|
2020-02-05 21:26:23 +08:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
#include "../kselftest.h"
|
2019-11-15 20:36:21 +08:00
|
|
|
|
|
|
|
#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
|
|
|
|
|
2020-02-05 21:26:23 +08:00
|
|
|
#ifndef CLONE_INTO_CGROUP
|
|
|
|
#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
|
|
|
|
#endif
|
|
|
|
|
2019-11-15 20:36:21 +08:00
|
|
|
#ifndef __NR_clone3
|
|
|
|
#define __NR_clone3 -1
|
2020-09-12 19:08:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
struct __clone_args {
|
2019-11-15 20:36:21 +08:00
|
|
|
__aligned_u64 flags;
|
|
|
|
__aligned_u64 pidfd;
|
|
|
|
__aligned_u64 child_tid;
|
|
|
|
__aligned_u64 parent_tid;
|
|
|
|
__aligned_u64 exit_signal;
|
|
|
|
__aligned_u64 stack;
|
|
|
|
__aligned_u64 stack_size;
|
|
|
|
__aligned_u64 tls;
|
2020-09-12 19:08:19 +08:00
|
|
|
#ifndef CLONE_ARGS_SIZE_VER0
|
|
|
|
#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
|
|
|
|
#endif
|
2019-11-15 20:36:21 +08:00
|
|
|
__aligned_u64 set_tid;
|
|
|
|
__aligned_u64 set_tid_size;
|
2020-09-12 19:08:19 +08:00
|
|
|
#ifndef CLONE_ARGS_SIZE_VER1
|
|
|
|
#define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
|
|
|
|
#endif
|
2020-02-05 21:26:23 +08:00
|
|
|
__aligned_u64 cgroup;
|
2020-09-12 19:08:19 +08:00
|
|
|
#ifndef CLONE_ARGS_SIZE_VER2
|
|
|
|
#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
|
|
|
|
#endif
|
2019-11-15 20:36:21 +08:00
|
|
|
};
|
|
|
|
|
2020-09-12 19:08:19 +08:00
|
|
|
static pid_t sys_clone3(struct __clone_args *args, size_t size)
|
2019-11-15 20:36:21 +08:00
|
|
|
{
|
2019-11-18 14:47:48 +08:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2019-11-15 20:36:21 +08:00
|
|
|
return syscall(__NR_clone3, args, size);
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:49:44 +08:00
|
|
|
static inline void test_clone3_supported(void)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
2020-09-12 19:08:19 +08:00
|
|
|
struct __clone_args args = {};
|
2019-11-18 15:49:44 +08:00
|
|
|
|
|
|
|
if (__NR_clone3 < 0)
|
|
|
|
ksft_exit_skip("clone3() syscall is not supported\n");
|
|
|
|
|
|
|
|
/* Set to something that will always cause EINVAL. */
|
|
|
|
args.exit_signal = -1;
|
|
|
|
pid = sys_clone3(&args, sizeof(args));
|
|
|
|
if (!pid)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
if (pid > 0) {
|
|
|
|
wait(NULL);
|
|
|
|
ksft_exit_fail_msg(
|
|
|
|
"Managed to create child process with invalid exit_signal\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
ksft_exit_skip("clone3() syscall is not supported\n");
|
|
|
|
|
|
|
|
ksft_print_msg("clone3() syscall supported\n");
|
|
|
|
}
|
|
|
|
|
2019-11-15 20:36:21 +08:00
|
|
|
#endif /* _CLONE3_SELFTESTS_H */
|