pidns02.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: pidns02.c
18*
19* Description:
20*  The pidns02.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 session ID of
25* child should be always one.
26*
27* 2. When parent clone a process with flag CLONE_NEWPID, the parent process group ID
28* should be always one.
29*
30* Total Tests
31*
32* Test Name: pidns02
33*
34* Test Assertion & Strategy:
35*
36* From main() clone a new child process with passing the clone_flag as CLONE_NEWPID,
37* Call the setid() inside container.
38* Inside the cloned pid check for the getsid(0) and getpgid(0)
39* Verify with global macro defined value for parent pid & child pid.
40*
41* Usage: <for command-line>
42* pidns02
43*
44* History:
45*
46* FLAG DATE     	NAME           		DESCRIPTION
47* 27/12/07  RISHIKESH K RAJAK <risrajak@in.ibm.com> Created this test
48*
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#ifndef NO_LTP
60#include <usctest.h>
61#include <test.h>
62#include <libclone.h>
63#else
64#include "../../../../include/usctest.h"
65#include "../libclone/libclone.h"
66#endif
67
68char *TCID = "pid_namespace2";
69int TST_TOTAL=1;
70
71void cleanup(void);
72
73#ifdef NO_LTP
74#define TFAIL "FAILURE: "
75#define TPASS "PASS: "
76#define TINFO "INFO: "
77#define TWARN "WARN: "
78#define tst_resm(x, format, arg...) printf("%s:" format, x, ## arg)
79#define tst_exit() exit(1)
80#endif
81
82#define PGID  	1
83#define SID	1
84
85/*
86 * child_fn1() - Inside container
87 */
88int child_fn1(void *vtest)
89{
90	pid_t pgid, sid;
91
92	setsid();
93
94	pgid = getpgid(0);
95	sid  = getsid(0);
96
97	tst_resm(TINFO, "Checking session id & group id inside container\n");
98	if(( pgid == PGID) && ( sid == SID ) )
99	{
100                tst_resm(TPASS, "Success: Got Group ID = %d"
101				" & Session ID = %d \n",pgid, sid);
102	}
103	else
104		tst_resm(TFAIL, "Got unexpected result of"
105			"Group ID = %d & Session ID = %d\n", pgid, sid);
106	cleanup();
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	/* check return code */
121	if (ret == -1) {
122		tst_resm(TFAIL, "clone() Failed, errno = %d :"
123			" %s", ret, strerror(ret));
124		/* Cleanup & continue with next test case */
125		cleanup();
126	}
127
128	/* Wait for child to finish */
129	if ((wait(&status)) < 0) {
130		tst_resm(TWARN, "wait() failed, skipping this"
131			" test case");
132		/* Cleanup & continue with next test case */
133		cleanup();
134	}
135
136	if (WTERMSIG(status)) {
137		tst_resm(TWARN, "child exited with signal %d",
138			 WTERMSIG(status));
139	}
140
141        /* cleanup and exit */
142	cleanup();
143
144	/*NOTREACHED*/
145	return 0;
146
147}	/* End main */
148
149/*
150 * cleanup() - performs all ONE TIME cleanup for this test at
151 *             completion or premature exit.
152 */
153void
154cleanup()
155{
156
157#ifndef NO_LTP
158	/* Clean the test testcase as LTP wants*/
159	TEST_CLEANUP;
160#endif
161
162	/* exit with return code appropriate for results */
163	tst_exit();
164}
165