xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns06.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) International Business Machines Corp., 2008
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 that parent process can't
11  * be killed from child namespace.
12  */
13 
14 #include "tst_test.h"
15 #include "lapi/sched.h"
16 
child_func(int pid)17 static void child_func(int pid)
18 {
19 	pid_t cpid = tst_getpid();
20 	pid_t ppid = getppid();
21 
22 	TST_EXP_EQ_LI(cpid, 1);
23 	TST_EXP_EQ_LI(ppid, 0);
24 
25 	tst_res(TINFO, "Trying to kill parent from within container");
26 
27 	TST_EXP_FAIL(kill(pid, SIGKILL), ESRCH);
28 }
29 
run(void)30 static void run(void)
31 {
32 	const struct tst_clone_args args = {
33 		.flags = CLONE_NEWPID,
34 		.exit_signal = SIGCHLD,
35 	};
36 	pid_t pid = getpid();
37 
38 	if (!SAFE_CLONE(&args)) {
39 		child_func(pid);
40 		return;
41 	}
42 }
43 
44 static struct tst_test test = {
45 	.test_all = run,
46 	.needs_root = 1,
47 	.forks_child = 1,
48 };
49