• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 *
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>
35 *
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 "test.h"
46 #include "libclone.h"
47 #include "pidns_helper.h"
48 
49 char *TCID = "pidns17";
50 int TST_TOTAL = 1;
51 int errno;
52 
53 int child_fn(void *);
54 
55 #define CHILD_PID       1
56 #define PARENT_PID      0
57 
58 /*
59  * child_fn() - Inside container
60  */
child_fn(void * arg)61 int child_fn(void *arg)
62 {
63 	int children[10], exit_val, i, status;
64 	pid_t pid, ppid;
65 
66 	/* Set process id and parent pid */
67 	pid = getpid();
68 	ppid = getppid();
69 	if (pid != CHILD_PID || ppid != PARENT_PID) {
70 		printf("cinit: pidns was not created\n");
71 		exit(1);
72 	}
73 
74 	exit_val = 0;
75 
76 	/* Spawn many children */
77 	for (i = 0; i < ARRAY_SIZE(children); i++) {
78 		switch ((children[i] = fork())) {
79 		case -1:
80 			perror("fork failed");
81 			exit_val = 1;
82 			break;
83 		case 0:
84 			pause();
85 			/* XXX (garrcoop): why exit with an exit code of 2? */
86 			exit(2);
87 			break;
88 		default:
89 			/* fork succeeded. */
90 			break;
91 		}
92 	}
93 	/* wait for last child to get scheduled */
94 	sleep(1);
95 
96 	if (kill(-1, SIGUSR1) == -1) {
97 		perror("cinit: kill(-1, SIGUSR1) failed");
98 		exit_val = 1;
99 	}
100 
101 	for (i = 0; i < ARRAY_SIZE(children); i++) {
102 		if (waitpid(children[i], &status, 0) == -1) {
103 			perror("cinit: waitpid failed");
104 			kill(children[i], SIGTERM);
105 			waitpid(children[i], &status, 0);
106 			exit_val = 1;
107 		}
108 		if (!(WIFSIGNALED(status) || WTERMSIG(status) == SIGUSR1)) {
109 			/*
110 			 * XXX (garrcoop): this status reporting is overly
111 			 * noisy. Someone obviously needs to read the
112 			 * constraints documented in wait(2) a bit more
113 			 * closely -- in particular the relationship between
114 			 * WIFEXITED and WEXITSTATUS, and WIFSIGNALED and
115 			 * WTERMSIG.
116 			 */
117 			printf("cinit: found a child alive still "
118 			       "%d exit: %d, %d, signal %d, %d", i,
119 			       WIFEXITED(status), WEXITSTATUS(status),
120 			       WIFSIGNALED(status), WTERMSIG(status));
121 			exit_val = 1;
122 		}
123 	}
124 	if (exit_val == 0)
125 		printf("cinit: all children have terminated.\n");
126 
127 	exit(exit_val);
128 }
129 
setup(void)130 static void setup(void)
131 {
132 	tst_require_root();
133 	check_newpid();
134 }
135 
main(int argc,char * argv[])136 int main(int argc, char *argv[])
137 {
138 	int status;
139 	pid_t pid;
140 
141 	setup();
142 
143 	pid = getpid();
144 
145 	/* Container creation on PID namespace */
146 	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL));
147 	if (TEST_RETURN == -1) {
148 		tst_brkm(TBROK | TERRNO, NULL, "clone failed");
149 	}
150 
151 	sleep(1);
152 	if (wait(&status) == -1)
153 		tst_resm(TFAIL, "waitpid failed");
154 
155 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
156 		tst_resm(TFAIL, "container exited abnormally");
157 	else if (WIFSIGNALED(status))
158 		tst_resm(TFAIL,
159 			 "container was signaled with signal = %d",
160 			 WTERMSIG(status));
161 
162 	tst_exit();
163 
164 }
165