blob: 89baa97c2520a0badd8507ad799eb970574d0720 [file] [log] [blame]
subrata_modakc7d5c362007-12-28 09:40:55 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080013* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modakc7d5c362007-12-28 09:40:55 +000014*
15***************************************************************************
16
17* File: pidns02.c
18*
19* Description:
Garrett Cooperad14e902010-12-16 10:03:44 -080020* The pidns02.c testcase builds into the ltp framework to verify
21* the basic functionality of PID Namespace.
subrata_modakc7d5c362007-12-28 09:40:55 +000022*
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*
subrata_modak1e9d84b2008-02-06 06:51:13 +000030* Total Tests
subrata_modakc7d5c362007-12-28 09:40:55 +000031*
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
Garrett Cooperad14e902010-12-16 10:03:44 -080043*/
subrata_modakc7d5c362007-12-28 09:40:55 +000044
yaberauneya7f140ab2009-12-06 20:55:15 +000045#define _GNU_SOURCE
subrata_modakc7d5c362007-12-28 09:40:55 +000046#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>
Garrett Coopere8530df2010-12-21 11:37:57 -080053#include "test.h"
yaberauneya7f140ab2009-12-06 20:55:15 +000054#define CLEANUP cleanup
55#include "libclone.h"
Jan Stanceked991ae2014-07-29 10:50:27 +020056#include "pidns_helper.h"
subrata_modakc7d5c362007-12-28 09:40:55 +000057
58char *TCID = "pid_namespace2";
Garrett Cooperad14e902010-12-16 10:03:44 -080059int TST_TOTAL = 1;
subrata_modakc7d5c362007-12-28 09:40:55 +000060
Garrett Cooperad14e902010-12-16 10:03:44 -080061#define PGID 1
subrata_modakc7d5c362007-12-28 09:40:55 +000062#define SID 1
63
64/*
65 * child_fn1() - Inside container
66 */
67int child_fn1(void *vtest)
68{
69 pid_t pgid, sid;
70
71 setsid();
72
73 pgid = getpgid(0);
Garrett Cooperad14e902010-12-16 10:03:44 -080074 sid = getsid(0);
subrata_modakc7d5c362007-12-28 09:40:55 +000075
Garrett Cooperad14e902010-12-16 10:03:44 -080076 printf("Checking session id & group id inside container\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 if (pgid == PGID && sid == SID) {
Garrett Cooperad14e902010-12-16 10:03:44 -080078 printf("Success: Got Group ID = %d & Session ID = %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 pgid, sid);
Garrett Cooperad14e902010-12-16 10:03:44 -080080 exit(0);
Wanlong Gao354ebb42012-12-07 10:10:04 +080081 } else {
Garrett Cooperad14e902010-12-16 10:03:44 -080082 printf("Got unexpected result of Group ID = %d & Session ID = "
Wanlong Gao354ebb42012-12-07 10:10:04 +080083 "%d\n", pgid, sid);
Garrett Cooperad14e902010-12-16 10:03:44 -080084 exit(1);
85 }
subrata_modakc7d5c362007-12-28 09:40:55 +000086}
87
Jan Stanceked991ae2014-07-29 10:50:27 +020088static void setup(void)
89{
90 tst_require_root(NULL);
91 check_newpid();
92}
93
subrata_modakc7d5c362007-12-28 09:40:55 +000094int main(int argc, char *argv[])
95{
Garrett Cooperad14e902010-12-16 10:03:44 -080096 int status;
subrata_modakc7d5c362007-12-28 09:40:55 +000097
Jan Stanceked991ae2014-07-29 10:50:27 +020098 setup();
99
Garrett Cooperad14e902010-12-16 10:03:44 -0800100 TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
101 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed");
Garrett Cooperad14e902010-12-16 10:03:44 -0800103 } else if ((wait(&status)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed");
subrata_modakc7d5c362007-12-28 09:40:55 +0000105 }
106
Garrett Cooperad14e902010-12-16 10:03:44 -0800107 if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_resm(TFAIL | TERRNO, "child exited abnormally");
Garrett Cooperad14e902010-12-16 10:03:44 -0800109 } else if (WIFSIGNALED(status)) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 tst_resm(TFAIL | TERRNO, "child exited with signal %d",
subrata_modakc7d5c362007-12-28 09:40:55 +0000111 WTERMSIG(status));
112 }
113
yaberauneya7f140ab2009-12-06 20:55:15 +0000114 CLEANUP();
115 tst_exit();
subrata_modakc7d5c362007-12-28 09:40:55 +0000116
Garrett Cooper2c282152010-12-16 00:55:50 -0800117}
subrata_modakc7d5c362007-12-28 09:40:55 +0000118
119/*
Garrett Cooperad14e902010-12-16 10:03:44 -0800120 * CLEANUP() - performs all ONE TIME CLEANUP for this test at
121 * completion or premature exit.
subrata_modakc7d5c362007-12-28 09:40:55 +0000122 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123void cleanup()
subrata_modakc7d5c362007-12-28 09:40:55 +0000124{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700125}