xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 Red Hat, Inc. All rights reserved.
4  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Clone a process with CLONE_NEWPID flag and check if procfs mounted folder
11  * belongs to the new pid namespace by looking at /proc/self .
12  */
13 
14 #include <sys/mount.h>
15 #include "tst_test.h"
16 #include "lapi/sched.h"
17 
18 #define PROCDIR "proc"
19 
child_func(void)20 static void child_func(void)
21 {
22 	char proc_self[10];
23 
24 	SAFE_MOUNT("none", PROCDIR, "proc", MS_RDONLY, NULL);
25 
26 	SAFE_READLINK(PROCDIR"/self", proc_self, sizeof(proc_self) - 1);
27 
28 	SAFE_UMOUNT(PROCDIR);
29 
30 	TST_EXP_PASS(strcmp(proc_self, "1"), PROCDIR"/self contains 1:");
31 }
32 
setup(void)33 static void setup(void)
34 {
35 	SAFE_MKDIR(PROCDIR, 0555);
36 }
37 
cleanup(void)38 static void cleanup(void)
39 {
40 	if (tst_is_mounted_at_tmpdir(PROCDIR))
41 		SAFE_UMOUNT(PROCDIR);
42 }
43 
run(void)44 static void run(void)
45 {
46 	const struct tst_clone_args args = {
47 		.flags = CLONE_NEWPID,
48 		.exit_signal = SIGCHLD,
49 	};
50 
51 	if (!SAFE_CLONE(&args)) {
52 		child_func();
53 		return;
54 	}
55 }
56 
57 static struct tst_test test = {
58 	.test_all = run,
59 	.setup = setup,
60 	.cleanup = cleanup,
61 	.needs_root = 1,
62 	.forks_child = 1,
63 	.needs_tmpdir = 1,
64 	.needs_kconfigs = (const char *[]) {
65 		"CONFIG_PID_NS",
66 		NULL,
67 	},
68 };
69