pidns02.c revision 38e181cbc686d59dcfd1b1b90ee0d8e5f1dd7505
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 "pidns_helper.h"
54#include "test.h"
55
56char *TCID = "pidns02";
57int TST_TOTAL = 1;
58
59#define PGID	1
60#define SID	1
61
62/*
63 * child_fn1() - Inside container
64 */
65int child_fn1(void *vtest)
66{
67	pid_t pgid, sid;
68
69	setsid();
70
71	pgid = getpgid(0);
72	sid = getsid(0);
73
74	printf("Checking session id & group id inside container\n");
75	if (pgid == PGID && sid == SID) {
76		printf("Success: Got Group ID = %d & Session ID = %d\n",
77		       pgid, sid);
78		exit(0);
79	} else {
80		printf("Got unexpected result of Group ID = %d & Session ID = "
81		       "%d\n", pgid, sid);
82		exit(1);
83	}
84}
85
86static void setup(void)
87{
88	tst_require_root();
89	check_newpid();
90}
91
92int main(int argc, char *argv[])
93{
94	int status;
95
96	setup();
97
98	TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
99	if (TEST_RETURN == -1) {
100		tst_brkm(TFAIL | TTERRNO, NULL, "clone failed");
101	} else if ((wait(&status)) == -1) {
102		tst_brkm(TFAIL | TERRNO, NULL, "wait failed");
103	}
104
105	if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
106		tst_resm(TFAIL | TERRNO, "child exited abnormally");
107	} else if (WIFSIGNALED(status)) {
108		tst_resm(TFAIL | TERRNO, "child exited with signal %d",
109			 WTERMSIG(status));
110	}
111
112	tst_exit();
113
114}
115