xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns12.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., 2007
4*49cdfc7eSAndroid Build Coastguard Worker  *               13/11/08  Gowrishankar M <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
12*49cdfc7eSAndroid Build Coastguard Worker  * set to 0 if sender (parent process) is not in the receiver's namespace.
13*49cdfc7eSAndroid Build Coastguard Worker  */
14*49cdfc7eSAndroid Build Coastguard Worker 
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test_macros.h"
16*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
17*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/sched.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static volatile pid_t sig_pid = -1;
22*49cdfc7eSAndroid Build Coastguard Worker 
child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig,siginfo_t * si,LTP_ATTRIBUTE_UNUSED void * unused)23*49cdfc7eSAndroid Build Coastguard Worker static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker 	sig_pid = si->si_pid;
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
child_func(void)28*49cdfc7eSAndroid Build Coastguard Worker static void child_func(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(tst_getpid(), 1);
33*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(getppid(), 0);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_flags = SA_SIGINFO;
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGFILLSET(&sa.sa_mask);
37*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_sigaction = child_signal_handler;
38*49cdfc7eSAndroid Build Coastguard Worker 
39*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_EQ_LI(sig_pid, 0);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker 
run(void)46*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	const struct tst_clone_args args = {
49*49cdfc7eSAndroid Build Coastguard Worker 		.flags = CLONE_NEWPID,
50*49cdfc7eSAndroid Build Coastguard Worker 		.exit_signal = SIGCHLD,
51*49cdfc7eSAndroid Build Coastguard Worker 	};
52*49cdfc7eSAndroid Build Coastguard Worker 	int pid;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_CLONE(&args);
55*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
56*49cdfc7eSAndroid Build Coastguard Worker 		child_func();
57*49cdfc7eSAndroid Build Coastguard Worker 		return;
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAIT(0);
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_KILL(pid, SIGUSR1);
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	TST_CHECKPOINT_WAKE(0);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
68*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = run,
69*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
70*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
71*49cdfc7eSAndroid Build Coastguard Worker 	.needs_checkpoints = 1,
72*49cdfc7eSAndroid Build Coastguard Worker 	.needs_kconfigs = (const char *[]) {
73*49cdfc7eSAndroid Build Coastguard Worker 		"CONFIG_PID_NS",
74*49cdfc7eSAndroid Build Coastguard Worker 		NULL,
75*49cdfc7eSAndroid Build Coastguard Worker 	},
76*49cdfc7eSAndroid Build Coastguard Worker };
77