xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns32.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) Huawei Technologies Co., Ltd., 2015
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * Clone a process with CLONE_NEWPID flag and check for the maxium amount of
11*49cdfc7eSAndroid Build Coastguard Worker  * nested containers.
12*49cdfc7eSAndroid Build Coastguard Worker  */
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_atomic.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define MAXNEST 32
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static const struct tst_clone_args args = {
23*49cdfc7eSAndroid Build Coastguard Worker 	.flags = CLONE_NEWPID,
24*49cdfc7eSAndroid Build Coastguard Worker 	.exit_signal = SIGCHLD,
25*49cdfc7eSAndroid Build Coastguard Worker };
26*49cdfc7eSAndroid Build Coastguard Worker static int *level;
27*49cdfc7eSAndroid Build Coastguard Worker 
child_func(void)28*49cdfc7eSAndroid Build Coastguard Worker static pid_t child_func(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	pid_t cpid = 0;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	if (tst_atomic_inc(level) == MAXNEST)
33*49cdfc7eSAndroid Build Coastguard Worker 		return cpid;
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	cpid = SAFE_CLONE(&args);
36*49cdfc7eSAndroid Build Coastguard Worker 	if (!cpid) {
37*49cdfc7eSAndroid Build Coastguard Worker 		child_func();
38*49cdfc7eSAndroid Build Coastguard Worker 		return cpid;
39*49cdfc7eSAndroid Build Coastguard Worker 	}
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	tst_reap_children();
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	return cpid;
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)51*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MUNMAP(level, sizeof(int));
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker 
run(void)56*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
57*49cdfc7eSAndroid Build Coastguard Worker {
58*49cdfc7eSAndroid Build Coastguard Worker 	*level = 0;
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (!child_func())
61*49cdfc7eSAndroid Build Coastguard Worker 		return;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(*level, MAXNEST);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
67*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
68*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
69*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
70*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
71*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
72*49cdfc7eSAndroid Build Coastguard Worker };
73