blob: 989664e08000c9fb15b6cb07a16a4788867b8c51 [file] [log] [blame]
subrata_modak910f93a2008-12-16 12:34:49 +00001/*
2* Copyright (c) International Business Machines Corp., 2007
3* This program is free software; you can redistribute it and/or modify
4* it under the terms of the GNU General Public License as published by
5* the Free Software Foundation; either version 2 of the License, or
6* (at your option) any later version.
7* This program is distributed in the hope that it will be useful,
8* but WITHOUT ANY WARRANTY; without even the implied warranty of
9* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
10* the GNU General Public License for more details.
11* You should have received a copy of the GNU General Public License
12* along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080013* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak910f93a2008-12-16 12:34:49 +000014*
15***************************************************************************
16* File: pidns13.c
17* *
18* * Description:
19* * The pidns13.c testcase checks container init, for async I/O
20* * triggered by peer namespace process.
21* *
22* * Test Assertion & Strategy:
23* * Create a pipe in parent namespace.
24* * Create two PID namespace containers(cinit1 and cinit2).
25* * In cinit1, set pipe read end to send SIGUSR1.
26* * for asynchronous I/O.
27* * Let cinit2 to trigger async I/O on pipe write end.
28* * In signal info, check si_code to be POLL_IN and si_fd to be pipe read fd.
29* *
30* * Usage: <for command-line>
31* * pidns13
32* *
33* * History:
34* * DATE NAME DESCRIPTION
35* * 23/10/08 Gowrishankar M Created test scenarion.
36* * <gowrishankar.m@in.ibm.com>
37*
38******************************************************************************/
39#define _GNU_SOURCE 1
40#include <sys/wait.h>
41#include <sys/types.h>
42#include <fcntl.h>
43#include <signal.h>
44#include <string.h>
45#include <stdlib.h>
46#include <unistd.h>
47#include <stdio.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080048#include "test.h"
subrata_modak910f93a2008-12-16 12:34:49 +000049#include <libclone.h>
Jan Stanceked991ae2014-07-29 10:50:27 +020050#include "pidns_helper.h"
subrata_modak910f93a2008-12-16 12:34:49 +000051
52char *TCID = "pidns13";
53int TST_TOTAL = 1;
54int pipe_fd[2];
55
56#define CHILD_PID 1
57#define PARENT_PID 0
58
59/*
60 * cleanup() - performs all ONE TIME cleanup for this test at
61 * completion or premature exit.
62 */
63void cleanup()
64{
subrata_modak910f93a2008-12-16 12:34:49 +000065
subrata_modak910f93a2008-12-16 12:34:49 +000066}
67
68/*
69 * child_signal_handler() - dummy function for sigaction()
70 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080071static void child_signal_handler(int sig, siginfo_t * si, void *unused)
subrata_modak910f93a2008-12-16 12:34:49 +000072{
73 /* sigtimedwait() traps siginfo details, so this wont be called */
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 tst_resm(TWARN, "cinit(pid %d): control should have not reached here!",
75 getpid());
subrata_modak910f93a2008-12-16 12:34:49 +000076}
77
78/*
79 * child_fn() - Inside container
80 */
81int child_fn(void *arg)
82{
83 struct sigaction sa;
84 sigset_t newset;
85 siginfo_t info;
86 struct timespec timeout;
87 pid_t pid, ppid;
88 int cinit_no = *((int *)arg);
89
90 /* Set process id and parent pid */
91 pid = getpid();
92 ppid = getppid();
93 if (pid != CHILD_PID || ppid != PARENT_PID) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 tst_resm(TBROK, "cinit%d: pidns is not created.", cinit_no);
subrata_modak910f93a2008-12-16 12:34:49 +000095 cleanup();
96 }
97
98 if (cinit_no == 1) {
99 /* in container 1 */
100 /* close pipe write descriptor */
101 if (close(pipe_fd[1]) == -1) {
102 tst_resm(TBROK, "cinit1: close(pipe_fd[1]) failed");
103 cleanup();
104 }
105
106 /* Let cinit1 to get SIGUSR1 on I/O availability */
107 if (fcntl(pipe_fd[0], F_SETOWN, pid) == -1) {
108 tst_resm(TBROK, "cinit1: fcntl(F_SETOWN) failed");
109 cleanup();
110 }
111
112 if (fcntl(pipe_fd[0], F_SETSIG, SIGUSR1) == -1) {
113 tst_resm(TBROK, "cinit1: fcntl(F_SETSIG) failed");
114 cleanup();
115 }
116
Wanlong Gao354ebb42012-12-07 10:10:04 +0800117 if (fcntl(pipe_fd[0], F_SETFL,
118 fcntl(pipe_fd[0], F_GETFL) | O_ASYNC) == -1) {
subrata_modak910f93a2008-12-16 12:34:49 +0000119 tst_resm(TBROK, "cinit1: fcntl(F_SETFL) failed");
120 cleanup();
121 }
122
123 /* Set signal handler for SIGUSR1, also mask other signals */
124 sa.sa_flags = SA_SIGINFO;
125 sigfillset(&sa.sa_mask);
126 sa.sa_sigaction = child_signal_handler;
127 if (sigaction(SIGUSR1, &sa, NULL) == -1) {
128 tst_resm(TBROK, "cinit1: sigaction() failed");
129 cleanup();
130 }
131
132 tst_resm(TINFO, "cinit1: setup handler for async I/O on pipe");
133
134 /* Set timeout for sigtimedwait */
135 timeout.tv_sec = 10;
136 timeout.tv_nsec = 0;
137
138 /* Set mask to wait for SIGUSR1 signal */
139 sigemptyset(&newset);
140 sigaddset(&newset, SIGUSR1);
141
142 /* Wait for SIGUSR1 */
143 if (sigtimedwait(&newset, &info, &timeout) != SIGUSR1) {
144 tst_resm(TBROK, "cinit1: sigtimedwait() failed.");
145 cleanup();
146 }
147
148 /* Recieved SIGUSR1. Check details. */
Garrett Cooper8fb1cdb2010-11-28 22:56:35 -0800149 if (info.si_fd == pipe_fd[0] && info.si_code == POLL_IN)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800150 tst_resm(TPASS, "cinit1: si_fd is %d, si_code is %d",
151 info.si_fd, info.si_code);
subrata_modak910f93a2008-12-16 12:34:49 +0000152 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_resm(TFAIL, "cinit1: si_fd is %d, si_code is %d",
154 info.si_fd, info.si_code);
subrata_modak910f93a2008-12-16 12:34:49 +0000155
156 /* all done, close the descriptors opened */
157 close(pipe_fd[0]);
158
159 } else {
160 /* in container 2 */
161 /* close pipe read descriptor */
162 if (close(pipe_fd[0]) == -1) {
163 tst_resm(TBROK, "cinit2: close(pipe_fd[0]) failed");
164 cleanup();
165 }
166
167 /* sleep for few seconds to avoid race with cinit1 */
168 sleep(2);
169
170 /* Write some data in pipe to SIGUSR1 cinit1 */
171 tst_resm(TINFO, "cinit2: writing some data in pipe");
172 if (write(pipe_fd[1], "test\n", 5) == -1) {
173 tst_resm(TBROK, "cinit2: write() failed");
174 cleanup();
175 }
176
177 /* all done, close the descriptors opened */
178 close(pipe_fd[1]);
179 }
180
181 /* cleanup and exit */
182 cleanup();
183 exit(0);
184}
185
Jan Stanceked991ae2014-07-29 10:50:27 +0200186static void setup(void)
187{
188 tst_require_root(NULL);
189 check_newpid();
190}
191
subrata_modak910f93a2008-12-16 12:34:49 +0000192/***********************************************************************
193* M A I N
194***********************************************************************/
195
196int main(int argc, char *argv[])
197{
subrata_modakce23f152009-01-19 09:23:02 +0000198 int status;
subrata_modak15052002009-01-05 06:58:05 +0000199 int *cinit_no = malloc(sizeof(int));
subrata_modak910f93a2008-12-16 12:34:49 +0000200 pid_t cpid1, cpid2;
201
Jan Stanceked991ae2014-07-29 10:50:27 +0200202 setup();
203
subrata_modak910f93a2008-12-16 12:34:49 +0000204 /* create pipe */
205 if (pipe(pipe_fd) == -1) {
206 tst_resm(TBROK, "parent: pipe creation failed");
207 cleanup();
208 }
209
210 /* container creation on PID namespace */
subrata_modakce23f152009-01-19 09:23:02 +0000211 if (!cinit_no) {
subrata_modak15052002009-01-05 06:58:05 +0000212 tst_resm(TBROK, "memory allocation failed.");
subrata_modak910f93a2008-12-16 12:34:49 +0000213 cleanup();
214 }
215
subrata_modak910f93a2008-12-16 12:34:49 +0000216 /* Create container 1 */
217 *cinit_no = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800218 cpid1 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, cinit_no);
subrata_modak910f93a2008-12-16 12:34:49 +0000219
220 /* Create container 2 */
221 *cinit_no = 2;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 cpid2 = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, cinit_no);
subrata_modak910f93a2008-12-16 12:34:49 +0000223 if (cpid1 < 0 || cpid2 < 0) {
224 tst_resm(TBROK, "parent: clone() failed.");
225 cleanup();
226 }
227
228 /* Close unwanted descriptors */
229 close(pipe_fd[0]);
230 close(pipe_fd[1]);
231
232 /* Wait for containers to exit */
233 if (waitpid(cpid2, &status, 0) < 0)
234 tst_resm(TWARN, "parent: waitpid(cpid2) failed.");
235
236 if (WIFSIGNALED(status) && WTERMSIG(status))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800237 tst_resm(TWARN, "parent: cinit2 is terminated by signal(%s)",
238 strsignal(WTERMSIG(status)));
subrata_modak910f93a2008-12-16 12:34:49 +0000239
240 if (waitpid(cpid1, &status, 0) < 0)
241 tst_resm(TWARN, "parent: waitpid(cpid1) failed.");
242
243 if (WIFSIGNALED(status) && WTERMSIG(status))
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 tst_resm(TWARN, "parent: cinit1 is terminated by signal(%s)",
245 strsignal(WTERMSIG(status)));
subrata_modak910f93a2008-12-16 12:34:49 +0000246
247 /* Cleanup and exit */
248 cleanup();
249
250 /* Control won't reach below */
251 exit(0);
252
Chris Dearmanec6edca2012-10-17 19:54:01 -0700253}