linux/tools/testing/selftests/clone3/clone3_selftests.h
Christian Brauner 9bd5910d7f selftests/cgroup: add tests for cloning into cgroups
Expand the cgroup test-suite to include tests for CLONE_INTO_CGROUP.
This adds the following tests:
- CLONE_INTO_CGROUP manages to clone a process directly into a correctly
  delegated cgroup
- CLONE_INTO_CGROUP fails to clone a process into a cgroup that has been
  removed after we've opened an fd to it
- CLONE_INTO_CGROUP fails to clone a process into an invalid domain
  cgroup
- CLONE_INTO_CGROUP adheres to the no internal process constraint
- CLONE_INTO_CGROUP works with the freezer feature

Cc: Tejun Heo <tj@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: cgroups@vger.kernel.org
Cc: linux-kselftest@vger.kernel.org
Acked-by: Roman Gushchin <guro@fb.com>
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
2020-02-12 17:57:51 -05:00

79 lines
1.7 KiB
C

/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _CLONE3_SELFTESTS_H
#define _CLONE3_SELFTESTS_H
#define _GNU_SOURCE
#include <sched.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <stdint.h>
#include <syscall.h>
#include <sys/wait.h>
#include "../kselftest.h"
#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
#ifndef CLONE_INTO_CGROUP
#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
#endif
#ifndef CLONE_ARGS_SIZE_VER0
#define CLONE_ARGS_SIZE_VER0 64
#endif
#ifndef __NR_clone3
#define __NR_clone3 -1
struct clone_args {
__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;
#define CLONE_ARGS_SIZE_VER1 80
__aligned_u64 set_tid;
__aligned_u64 set_tid_size;
#define CLONE_ARGS_SIZE_VER2 88
__aligned_u64 cgroup;
};
#endif /* __NR_clone3 */
static pid_t sys_clone3(struct clone_args *args, size_t size)
{
fflush(stdout);
fflush(stderr);
return syscall(__NR_clone3, args, size);
}
static inline void test_clone3_supported(void)
{
pid_t pid;
struct clone_args args = {};
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");
}
#endif /* _CLONE3_SELFTESTS_H */