• 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 
17 * File: pidns04.c
18 *
19 * Description:
20 *  The pidns04.c testcase builds into the ltp framework to verify
21 *  the basic functionality of PID Namespace.
22 *
23 * Verify that:
24 * 1. When parent clone a process with flag CLONE_NEWPID, the process ID of
25 * child should be one.
26 *
27 * 2. When parent clone a process with flag CLONE_NEWPID, the parent process ID
28 * of should be zero.
29 *
30 * 3. The container init process (one), should not get killed by the SIGKILL in
31 * the childNS
32 *
33 * Total Tests:
34 *
35 * Test Name: pidns04
36 *
37 * Test Assertion & Strategy:
38 *
39 * From main() clone a new child process with passing the clone_flag as
40 * CLONE_NEWPID.
41 * The container init, should not get killed by the SIGKILL inside the child NS.
42 * Usage: <for command-line>
43 * pidns04
44 *
45 * History:
46 *
47 * FLAG DATE     	NAME	   			DESCRIPTION
48 * 08/10/08      Veerendra C <vechandr@in.ibm.com> Verifies killing of cont init.
49 *
50 *******************************************************************************/
51 #define _GNU_SOURCE 1
52 #include <sys/wait.h>
53 #include <assert.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <string.h>
58 #include <errno.h>
59 #define CLEANUP cleanup
60 #include "pidns_helper.h"
61 #include "test.h"
62 
63 #define INIT_PID	1
64 #define CHILD_PID       1
65 #define PARENT_PID      0
66 
67 char *TCID = "pidns04";
68 int TST_TOTAL = 1;
69 int fd[2];
70 
71 /*
72  * child_fn1() - Inside container
73 */
child_fn1(void * ttype)74 static int child_fn1(void *ttype)
75 {
76 	int exit_val;
77 	pid_t cpid, ppid;
78 	cpid = getpid();
79 	ppid = getppid();
80 	char mesg[] = "I was not killed !";
81 	/* Child process closes up read side of pipe */
82 	close(fd[0]);
83 
84 	/* Comparing the values to make sure pidns is created correctly */
85 	if ((cpid == CHILD_PID) && (ppid == PARENT_PID)) {
86 		printf("PIDNS test is running inside container\n");
87 		kill(INIT_PID, SIGKILL);
88 		/* Verifying whether the container init is not killed, "
89 		   If so writing into the pipe created in the parent NS" */
90 
91 		/* Send "mesg" through the write side of pipe */
92 		write(fd[1], mesg, (strlen(mesg) + 1));
93 		exit_val = 0;
94 	} else {
95 		printf("got unexpected result of cpid=%d ppid=%d\n",
96 		       cpid, ppid);
97 		exit_val = 1;
98 	}
99 	exit(exit_val);
100 }
101 
setup(void)102 static void setup(void)
103 {
104 	tst_require_root();
105 	check_newpid();
106 }
107 
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110 	int nbytes, status;
111 	char readbuffer[80];
112 
113 	setup();
114 
115 	pipe(fd);
116 	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
117 	if (TEST_RETURN == -1) {
118 		tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed");
119 	} else if (wait(&status) == -1) {
120 		tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed");
121 	}
122 
123 	/* Parent process closes up write side of pipe */
124 	close(fd[1]);
125 	/* Read in a string from the pipe */
126 	nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
127 
128 	if (0 <= nbytes) {
129 		tst_resm(TPASS, "Container init : %s", readbuffer);
130 	} else {
131 		tst_brkm(TFAIL, CLEANUP,
132 			 "Container init is killed by SIGKILL !!!");
133 	}
134 
135 	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
136 		tst_resm(TFAIL, "Container init pid exited abnormally");
137 	} else if (WIFSIGNALED(status)) {
138 		tst_resm(TFAIL, "Container init pid got killed by signal %d",
139 			 WTERMSIG(status));
140 	}
141 	CLEANUP();
142 
143 	tst_exit();
144 
145 }
146 
cleanup(void)147 static void cleanup(void)
148 {
149 	close(fd[0]);
150 }
151