xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns04.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 child container does
11  * not get kill itself with SIGKILL.
12  */
13 
14 #include <sys/wait.h>
15 #include "tst_test.h"
16 #include "lapi/sched.h"
17 
child_func(void)18 static void child_func(void)
19 {
20 	pid_t cpid = tst_getpid();
21 	pid_t ppid = getppid();
22 
23 	TST_EXP_EQ_LI(cpid, 1);
24 	TST_EXP_EQ_LI(ppid, 0);
25 
26 	tst_res(TINFO, "Trying to kill container from within container");
27 
28 	SAFE_KILL(1, SIGKILL);
29 
30 	tst_res(TINFO, "Container is up and running");
31 }
32 
run(void)33 static void run(void)
34 {
35 	const struct tst_clone_args args = {
36 		.flags = CLONE_NEWPID,
37 		.exit_signal = SIGCHLD,
38 	};
39 	pid_t pid;
40 
41 	pid = SAFE_CLONE(&args);
42 	if (!pid) {
43 		child_func();
44 		return;
45 	}
46 
47 	tst_reap_children();
48 }
49 
50 static struct tst_test test = {
51 	.test_all = run,
52 	.needs_root = 1,
53 	.forks_child = 1,
54 };
55