xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/pidns/pidns13.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2007
4  * Copyright (c) SUSE LLC 2021
5  *
6  * History:
7  * 23/10/08  Gowrishankar M 			Created test scenarion.
8  *            <[email protected]>
9  */
10 
11 /*\
12  * [Description]
13  *
14  * The pidns13.c testcase checks container init, for async I/O
15  * triggered by peer namespace process.
16  *
17  * [Algorithm]
18  * * create a pipe in parent namespace
19  * * create two PID namespace containers(cinit1 and cinit2)
20  * * in cinit1, set pipe read end to send SIGUSR1 for asynchronous I/O
21  * * let cinit2 trigger async I/O on pipe write end
22  * * in signal info, check si_code to be POLL_IN and si_fd to be pipe read fd
23  */
24 
25 #define _GNU_SOURCE 1
26 #include <sys/wait.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <stdlib.h>
31 
32 #include "tst_test.h"
33 #include "tst_clone.h"
34 #include "lapi/sched.h"
35 
36 static int pipe_fd[2];
37 
38 #define CHILD_PID       1
39 #define PARENT_PID      0
40 
child_signal_handler(int sig,siginfo_t * si,void * unused LTP_ATTRIBUTE_UNUSED)41 static void child_signal_handler(int sig, siginfo_t *si,
42 				 void *unused LTP_ATTRIBUTE_UNUSED)
43 {
44 	tst_res(TWARN, "cinit(pid %d): Caught signal! sig=%d, si_fd=%d, si_code=%d",
45 		tst_getpid(), sig, si->si_fd, si->si_code);
46 }
47 
child_fn(unsigned int cinit_no)48 static void child_fn(unsigned int cinit_no)
49 {
50 	struct sigaction sa;
51 	sigset_t newset;
52 	siginfo_t info;
53 	struct timespec timeout;
54 	pid_t pid, ppid;
55 	int flags;
56 
57 	pid = tst_getpid();
58 	ppid = getppid();
59 	if (pid != CHILD_PID || ppid != PARENT_PID)
60 		tst_brk(TBROK, "cinit%u: pidns not created.", cinit_no);
61 
62 	if (cinit_no == 1) {
63 		SAFE_CLOSE(pipe_fd[1]);
64 
65 		sigemptyset(&newset);
66 		sigaddset(&newset, SIGUSR1);
67 		SAFE_SIGPROCMASK(SIG_BLOCK, &newset, NULL);
68 
69 		SAFE_FCNTL(pipe_fd[0], F_SETOWN, pid);
70 		SAFE_FCNTL(pipe_fd[0], F_SETSIG, SIGUSR1);
71 		flags = SAFE_FCNTL(pipe_fd[0], F_GETFL);
72 		SAFE_FCNTL(pipe_fd[0], F_SETFL, flags | O_ASYNC);
73 
74 		sa.sa_flags = SA_SIGINFO;
75 		sigfillset(&sa.sa_mask);
76 		sa.sa_sigaction = child_signal_handler;
77 		SAFE_SIGACTION(SIGUSR1, &sa, NULL);
78 
79 		TST_CHECKPOINT_WAKE(1);
80 
81 		timeout.tv_sec = 10;
82 		timeout.tv_nsec = 0;
83 
84 		if (sigtimedwait(&newset, &info, &timeout) != SIGUSR1) {
85 			tst_brk(TBROK | TERRNO,
86 				"cinit1: sigtimedwait() failed.");
87 		}
88 
89 		if (info.si_fd == pipe_fd[0] && info.si_code == POLL_IN)
90 			tst_res(TPASS, "cinit1: si_fd is %d, si_code is %d",
91 				info.si_fd, info.si_code);
92 		else
93 			tst_res(TFAIL, "cinit1: si_fd is %d, si_code is %d",
94 				info.si_fd, info.si_code);
95 	} else {
96 		SAFE_CLOSE(pipe_fd[0]);
97 
98 		TST_CHECKPOINT_WAIT(1);
99 		SAFE_WRITE(SAFE_WRITE_ALL, pipe_fd[1], "test\n", 5);
100 	}
101 
102 	exit(0);
103 }
104 
run(void)105 static void run(void)
106 {
107 	const struct tst_clone_args cargs = {
108 		.flags = CLONE_NEWPID,
109 		.exit_signal = SIGCHLD,
110 	};
111 
112 	SAFE_PIPE(pipe_fd);
113 
114 	if (!SAFE_CLONE(&cargs))
115 		child_fn(1);
116 
117 	if (!SAFE_CLONE(&cargs))
118 		child_fn(2);
119 
120 	SAFE_CLOSE(pipe_fd[0]);
121 	SAFE_CLOSE(pipe_fd[1]);
122 }
123 
124 static struct tst_test test = {
125 	.test_all = run,
126 	.needs_root = 1,
127 	.needs_checkpoints = 1,
128 	.forks_child = 1,
129 };
130