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