xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2007
4  *               27/12/07  Rishikesh K Rajak <[email protected]>
5  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Clone a process with CLONE_NEWPID flag and check:
12  *
13  * - child process ID must be 1
14  * - parent process ID must be 0
15  */
16 
17 #include "tst_test.h"
18 #include "lapi/sched.h"
19 
child_func(void)20 static void child_func(void)
21 {
22 	pid_t cpid, ppid;
23 
24 	cpid = tst_getpid();
25 	ppid = getppid();
26 
27 	TST_EXP_EQ_LI(cpid, 1);
28 	TST_EXP_EQ_LI(ppid, 0);
29 }
30 
run(void)31 static void run(void)
32 {
33 	const struct tst_clone_args args = {
34 		.flags = CLONE_NEWPID,
35 		.exit_signal = SIGCHLD,
36 	};
37 
38 	if (!SAFE_CLONE(&args)) {
39 		child_func();
40 		return;
41 	}
42 }
43 
44 static struct tst_test test = {
45 	.test_all = run,
46 	.needs_root = 1,
47 	.forks_child = 1,
48 	.needs_kconfigs = (const char *[]) {
49 		"CONFIG_PID_NS",
50 		NULL,
51 	},
52 };
53