xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns16.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2007
4  *               04/11/08  Veerendra C  <[email protected]>
5  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Clone a process with CLONE_NEWPID flag and verifies that siginfo->si_pid is
12  * set to 0 if sender (parent process) sent the signal. Then send signal from
13  * container itself and check if siginfo->si_pid is set to 1.
14  */
15 
16 #define _GNU_SOURCE 1
17 #include <signal.h>
18 #include "tst_test.h"
19 #include "lapi/sched.h"
20 
21 static volatile int signal_pid;
22 
child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig,siginfo_t * si,LTP_ATTRIBUTE_UNUSED void * unused)23 static void child_signal_handler(LTP_ATTRIBUTE_UNUSED int sig, siginfo_t *si, LTP_ATTRIBUTE_UNUSED void *unused)
24 {
25 	signal_pid = si->si_pid;
26 }
27 
child_func(void)28 static void child_func(void)
29 {
30 	struct sigaction sa;
31 	pid_t cpid, ppid;
32 
33 	cpid = tst_getpid();
34 	ppid = getppid();
35 
36 	TST_EXP_EQ_LI(cpid, 1);
37 	TST_EXP_EQ_LI(ppid, 0);
38 
39 	tst_res(TINFO, "Catching SIGUSR1 signal");
40 
41 	sa.sa_flags = SA_SIGINFO;
42 	SAFE_SIGFILLSET(&sa.sa_mask);
43 	sa.sa_sigaction = child_signal_handler;
44 	SAFE_SIGACTION(SIGUSR1, &sa, NULL);
45 
46 	TST_CHECKPOINT_WAKE_AND_WAIT(0);
47 
48 	TST_EXP_EQ_LI(signal_pid, 0);
49 
50 	tst_res(TINFO, "Sending SIGUSR1 from container itself");
51 
52 	SAFE_KILL(cpid, SIGUSR1);
53 
54 	TST_EXP_EQ_LI(signal_pid, 1);
55 }
56 
run(void)57 static void run(void)
58 {
59 	const struct tst_clone_args args = {
60 		.flags = CLONE_NEWPID,
61 		.exit_signal = SIGCHLD,
62 	};
63 	pid_t pid;
64 
65 	signal_pid = -1;
66 
67 	pid = SAFE_CLONE(&args);
68 	if (!pid) {
69 		child_func();
70 		return;
71 	}
72 
73 	TST_CHECKPOINT_WAIT(0);
74 
75 	tst_res(TINFO, "Sending SIGUSR1 from parent");
76 
77 	SAFE_KILL(pid, SIGUSR1);
78 
79 	TST_CHECKPOINT_WAKE(0);
80 }
81 
82 static struct tst_test test = {
83 	.test_all = run,
84 	.needs_root = 1,
85 	.needs_checkpoints = 1,
86 	.forks_child = 1,
87 };
88