1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2023 SUSE LLC <wegao@suse.com> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/*\ 7f08c3bdfSopenharmony_ci * [Description] 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This test case check clone3 CLONE_INTO_CGROUP flag 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci */ 12f08c3bdfSopenharmony_ci 13f08c3bdfSopenharmony_ci#define _GNU_SOURCE 14f08c3bdfSopenharmony_ci#include <stdio.h> 15f08c3bdfSopenharmony_ci#include <stdlib.h> 16f08c3bdfSopenharmony_ci#include <sys/wait.h> 17f08c3bdfSopenharmony_ci 18f08c3bdfSopenharmony_ci#include "tst_test.h" 19f08c3bdfSopenharmony_ci#include "lapi/sched.h" 20f08c3bdfSopenharmony_ci#include "lapi/pidfd.h" 21f08c3bdfSopenharmony_ci 22f08c3bdfSopenharmony_ci#define BUF_LEN 20 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_cistatic struct tst_cg_group *cg_child_test_simple; 25f08c3bdfSopenharmony_cistatic int fd; 26f08c3bdfSopenharmony_cistatic struct tst_clone_args *args; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic pid_t clone_into_cgroup(int cgroup_fd) 29f08c3bdfSopenharmony_ci{ 30f08c3bdfSopenharmony_ci 31f08c3bdfSopenharmony_ci args->flags = CLONE_INTO_CGROUP; 32f08c3bdfSopenharmony_ci args->exit_signal = SIGCHLD; 33f08c3bdfSopenharmony_ci args->cgroup = cgroup_fd; 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_ci return tst_clone(args); 36f08c3bdfSopenharmony_ci} 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic void run(void) 39f08c3bdfSopenharmony_ci{ 40f08c3bdfSopenharmony_ci pid_t pid; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_ci pid = clone_into_cgroup(fd); 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ci if (!pid) { 45f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAIT(0); 46f08c3bdfSopenharmony_ci return; 47f08c3bdfSopenharmony_ci } 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci char buf[BUF_LEN]; 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci SAFE_CG_READ(cg_child_test_simple, "cgroup.procs", buf, BUF_LEN); 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci if (atoi(buf) == pid) 54f08c3bdfSopenharmony_ci tst_res(TPASS, "clone3 case pass!"); 55f08c3bdfSopenharmony_ci else 56f08c3bdfSopenharmony_ci tst_brk(TFAIL | TTERRNO, "clone3() failed !"); 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci TST_CHECKPOINT_WAKE(0); 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci SAFE_WAITPID(pid, NULL, 0); 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_ci} 63f08c3bdfSopenharmony_ci 64f08c3bdfSopenharmony_cistatic void setup(void) 65f08c3bdfSopenharmony_ci{ 66f08c3bdfSopenharmony_ci clone3_supported_by_kernel(); 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci cg_child_test_simple = tst_cg_group_mk(tst_cg, "cg_test_simple"); 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_ci fd = tst_cg_group_unified_dir_fd(cg_child_test_simple); 71f08c3bdfSopenharmony_ci 72f08c3bdfSopenharmony_ci if (fd < 0) 73f08c3bdfSopenharmony_ci tst_brk(TBROK, "get dir fd failed!"); 74f08c3bdfSopenharmony_ci} 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_cistatic void cleanup(void) 77f08c3bdfSopenharmony_ci{ 78f08c3bdfSopenharmony_ci cg_child_test_simple = tst_cg_group_rm(cg_child_test_simple); 79f08c3bdfSopenharmony_ci} 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_cistatic struct tst_test test = { 82f08c3bdfSopenharmony_ci .test_all = run, 83f08c3bdfSopenharmony_ci .setup = setup, 84f08c3bdfSopenharmony_ci .cleanup = cleanup, 85f08c3bdfSopenharmony_ci .forks_child = 1, 86f08c3bdfSopenharmony_ci .needs_cgroup_ctrls = (const char *const []){ "base", NULL }, 87f08c3bdfSopenharmony_ci .needs_cgroup_ver = TST_CG_V2, 88f08c3bdfSopenharmony_ci .needs_checkpoints = 1, 89f08c3bdfSopenharmony_ci .min_kver = "5.7", 90f08c3bdfSopenharmony_ci .bufs = (struct tst_buffers []) { 91f08c3bdfSopenharmony_ci {&args, .size = sizeof(*args)}, 92f08c3bdfSopenharmony_ci {}, 93f08c3bdfSopenharmony_ci } 94f08c3bdfSopenharmony_ci}; 95