Imported Upstream version 20081219
[ltp-debian.git] / testcases / kernel / containers / pidns / pidns17.c
blobf2653b33e4c2efd1ed05b6da8d9d9532a42bfcb7
1 /*
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
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 ***************************************************************************
16 * File: pidns17.c
17 * *
18 * * Description:
19 * * The pidns17.c testcase verifies inside the container, if kill(-1, SIGUSR1)
20 * * terminates all children running inside.
21 * *
22 * * Test Assertion & Strategy:
23 * * Create a PID namespace container.
24 * * Spawn many children inside it.
25 * * Invoke kill(-1, SIGUSR1) inside container and check if it terminates
26 * * all children.
27 * *
28 * * Usage: <for command-line>
29 * * pidns17
30 * *
31 * * History:
32 * * DATE NAME DESCRIPTION
33 * * 13/11/08 Gowrishankar M Creation of this test.
34 * * <gowrishankar.m@in.ibm.com>
36 ******************************************************************************/
37 #define _GNU_SOURCE 1
38 #include <sys/wait.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <usctest.h>
46 #include <test.h>
47 #include <libclone.h>
49 char *TCID = "pidns17";
50 int TST_TOTAL = 1;
51 int errno;
53 int child_fn(void *);
54 void cleanup(void);
56 #define CHILD_PID 1
57 #define PARENT_PID 0
60 * child_fn() - Inside container
62 int child_fn(void *arg)
64 pid_t pid, ppid;
65 int i, children[10], status;
67 /* Set process id and parent pid */
68 pid = getpid();
69 ppid = getppid();
70 if (pid != CHILD_PID || ppid != PARENT_PID) {
71 tst_resm(TBROK, "cinit: pidns is not created.");
72 cleanup();
75 /* Spawn many children */
76 for (i = 0; i < 10; i++)
77 if ((children[i] = fork()) == 0)
78 sleep(10);
80 /* wait for last child to get scheduled */
81 sleep(1);
83 if (kill(-1, SIGUSR1) == -1) {
84 tst_resm(TBROK, "cinit: kill(-1, SIGUSR1) failed");
85 cleanup();
88 for (i = 0; i < 10; i++) {
89 if (waitpid(children[i], &status, WNOHANG) == -1) {
90 tst_resm(TBROK, "cinit: waitpid() failed(%s)",\
91 strerror(errno));
92 cleanup();
94 if (!(WIFSIGNALED(&status) && WTERMSIG(status) == SIGUSR1)) {
95 tst_resm(TFAIL, "cinit: found a child alive still.");
96 cleanup();
99 tst_resm(TPASS, "cinit: all children are terminated.");
101 /* cleanup and exit */
102 cleanup();
103 exit(0);
106 /***********************************************************************
107 * M A I N
108 ***********************************************************************/
110 int main(int argc, char *argv[])
112 int status, ret;
113 pid_t pid;
115 pid = getpid();
117 /* Container creation on PID namespace */
118 ret = do_clone_unshare_test(T_CLONE,\
119 CLONE_NEWPID, child_fn, NULL);
120 if (ret != 0) {
121 tst_resm(TBROK, "parent: clone() failed. rc=%d(%s)",\
122 ret, strerror(errno));
123 /* Cleanup & continue with next test case */
124 cleanup();
127 sleep(1);
128 if (wait(&status) < 0)
129 tst_resm(TWARN, "parent: waitpid() failed.");
131 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
132 tst_resm(TWARN, "parent: container was terminated by %s",\
133 strsignal(WTERMSIG(status)));
135 /* cleanup and exit */
136 cleanup();
137 exit(0);
138 } /* End main */
141 * cleanup() - performs all ONE TIME cleanup for this test at
142 * completion or premature exit.
144 void cleanup()
146 /* Clean the test testcase as LTP wants*/
147 TEST_CLEANUP;
149 /* exit with return code appropriate for results */
150 tst_exit();