pidns01.c revision 1e9d84b1c4da16705e6c67baa46e250a9d5fa66c
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
14*
15***************************************************************************
16
17* File: pidns01.c
18*
19* Description:
20*  The pidns01.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 always one.
26*
27* 2. When parent clone a process with flag CLONE_NEWPID, the parent process ID of
28* should be always zero.
29*
30* Total Tests:
31*
32* Test Name: pidns01
33*
34* Test Assertion & Strategy:
35*
36* From main() clone a new child process with passing the clone_flag as CLONE_NEWPID,
37* Inside the cloned pid check for the getpid() and getppid()
38* Verify with global macro defined value for parent pid & child pid.
39*
40* Usage: <for command-line>
41* pidns01
42*
43* History:
44*
45* FLAG DATE     	NAME           		DESCRIPTION
46* 27/12/07  RISHIKESH K RAJAK <risrajak@in.ibm.com> Created this test
47*
48*******************************************************************************************/
49#define _GNU_SOURCE 1
50#include <sys/wait.h>
51#include <assert.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <unistd.h>
55#include <string.h>
56#include <errno.h>
57#ifndef NO_LTP
58#include <usctest.h>
59#include <test.h>
60#include <libclone.h>
61#else
62#include "../../../../include/usctest.h"
63#include "../libclone/libclone.h"
64#endif
65
66char *TCID = "pid_namespace1";
67int TST_TOTAL=1;
68
69void cleanup(void);
70
71#ifdef NO_LTP
72#define TFAIL "FAILURE: "
73#define TPASS "PASS: "
74#define TINFO "INFO: "
75#define TWARN "WARN: "
76#define tst_resm(x, format, arg...) printf("%s:" format, x, ## arg)
77#define tst_exit() exit(1)
78#endif
79
80#define CHILD_PID       1
81#define PARENT_PID      0
82
83
84/*
85 * child_fn1() - Inside container
86 */
87int child_fn1(void *ttype)
88{
89	pid_t cpid, ppid;
90	cpid = getpid();
91	ppid = getppid();
92
93	tst_resm(TINFO, "PIDNS test is running inside container\n");
94	if(( cpid == CHILD_PID) &&
95		( ppid == PARENT_PID ) )
96	{
97                tst_resm(TPASS, "Success:" );
98	}
99	else
100	{
101		tst_resm(TFAIL, "FAIL: Got unexpected result of"
102			" cpid=%d ppid=%d\n", cpid, ppid);
103	}
104	cleanup();
105
106	/* NOT REACHED */
107	return 0;
108}
109
110/***********************************************************************
111*   M A I N
112***********************************************************************/
113
114int main(int argc, char *argv[])
115{
116	int ret, status;
117
118	ret = do_clone_unshare_test(T_CLONE,
119				CLONE_NEWPID, child_fn1, NULL);
120
121	/* check return code */
122	if (ret == -1) {
123		tst_resm(TFAIL, "clone() Failed, errno = %d :"
124			" %s", ret, strerror(ret));
125		/* Cleanup & continue with next test case */
126		cleanup();
127	}
128
129	/* Wait for child to finish */
130	if ((wait(&status)) < 0) {
131		tst_resm(TWARN, "wait() failed, skipping this"
132			" test case");
133		/* Cleanup & continue with next test case */
134		cleanup();
135	}
136
137	if (WTERMSIG(status)) {
138		tst_resm(TWARN, "child exited with signal %d",
139			 WTERMSIG(status));
140	}
141
142        /* cleanup and exit */
143	cleanup();
144
145	/*NOTREACHED*/
146	return 0;
147
148}	/* End main */
149
150/*
151 * cleanup() - performs all ONE TIME cleanup for this test at
152 *             completion or premature exit.
153 */
154void
155cleanup()
156{
157
158#ifndef NO_LTP
159	/* Clean the test testcase as LTP wants*/
160	TEST_CLEANUP;
161#endif
162
163	/* exit with return code appropriate for results */
164	tst_exit();
165}
166