pidns02.c revision ed991ae835a3a59fe7c3f08c076b8805be79148d
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: 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
45#define _GNU_SOURCE
46#include <sys/wait.h>
47#include <assert.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <unistd.h>
51#include <string.h>
52#include <errno.h>
53#include "usctest.h"
54#include "test.h"
55#define CLEANUP cleanup
56#include "libclone.h"
57#include "pidns_helper.h"
58
59char *TCID = "pid_namespace2";
60int TST_TOTAL = 1;
61
62#define PGID	1
63#define SID	1
64
65/*
66 * child_fn1() - Inside container
67 */
68int child_fn1(void *vtest)
69{
70	pid_t pgid, sid;
71
72	setsid();
73
74	pgid = getpgid(0);
75	sid = getsid(0);
76
77	printf("Checking session id & group id inside container\n");
78	if (pgid == PGID && sid == SID) {
79		printf("Success: Got Group ID = %d & Session ID = %d\n",
80		       pgid, sid);
81		exit(0);
82	} else {
83		printf("Got unexpected result of Group ID = %d & Session ID = "
84		       "%d\n", pgid, sid);
85		exit(1);
86	}
87}
88
89static void setup(void)
90{
91	tst_require_root(NULL);
92	check_newpid();
93}
94
95int main(int argc, char *argv[])
96{
97	int status;
98
99	setup();
100
101	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
102	if (TEST_RETURN == -1) {
103		tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed");
104	} else if ((wait(&status)) == -1) {
105		tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed");
106	}
107
108	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
109		tst_resm(TFAIL | TERRNO, "child exited abnormally");
110	} else if (WIFSIGNALED(status)) {
111		tst_resm(TFAIL | TERRNO, "child exited with signal %d",
112			 WTERMSIG(status));
113	}
114
115	CLEANUP();
116	tst_exit();
117
118}
119
120/*
121 * CLEANUP() -	performs all ONE TIME CLEANUP for this test at
122 *		completion or premature exit.
123 */
124void cleanup()
125{
126	/* Clean the test testcase as LTP wants */
127	TEST_CLEANUP;
128}
129