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