blob: d9efc93056eea527baa52d78784874e56b277a9c [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: 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*
subrata_modak1e9d84b2008-02-06 06:51:13 +000030* Total Tests:
subrata_modakc7d5c362007-12-28 09:40:55 +000031*
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*
Monson Shao45192242013-01-08 11:34:44 +080045* FLAG DATE NAME DESCRIPTION
subrata_modakc7d5c362007-12-28 09:40:55 +000046* 27/12/07 RISHIKESH K RAJAK <risrajak@in.ibm.com> Created this test
47*
48*******************************************************************************************/
yaberauneya6aa27372009-12-06 19:54:10 +000049#define _GNU_SOURCE
subrata_modakc7d5c362007-12-28 09:40:55 +000050#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>
Garrett Coopere8530df2010-12-21 11:37:57 -080057#include "test.h"
yaberauneya7f140ab2009-12-06 20:55:15 +000058#define CLEANUP cleanup
yaberauneya6aa27372009-12-06 19:54:10 +000059#include "libclone.h"
Jan Stanceked991ae2014-07-29 10:50:27 +020060#include "pidns_helper.h"
subrata_modakc7d5c362007-12-28 09:40:55 +000061
62char *TCID = "pid_namespace1";
Garrett Cooperad14e902010-12-16 10:03:44 -080063int TST_TOTAL = 1;
subrata_modakc7d5c362007-12-28 09:40:55 +000064
subrata_modakc7d5c362007-12-28 09:40:55 +000065#define CHILD_PID 1
66#define PARENT_PID 0
67
subrata_modakc7d5c362007-12-28 09:40:55 +000068/*
69 * child_fn1() - Inside container
70 */
71int child_fn1(void *ttype)
72{
Garrett Cooperad14e902010-12-16 10:03:44 -080073 int exit_val;
subrata_modakc7d5c362007-12-28 09:40:55 +000074 pid_t cpid, ppid;
75 cpid = getpid();
76 ppid = getppid();
77
Monson Shao45192242013-01-08 11:34:44 +080078 tst_resm(TINFO, "PIDNS test is running inside container");
Garrett Cooperad14e902010-12-16 10:03:44 -080079 if (cpid == CHILD_PID && ppid == PARENT_PID) {
80 printf("Got expected cpid and ppid\n");
81 exit_val = 0;
82 } else {
83 printf("Got unexpected result of cpid=%d ppid=%d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +080084 cpid, ppid);
Garrett Cooperad14e902010-12-16 10:03:44 -080085 exit_val = 1;
subrata_modakc7d5c362007-12-28 09:40:55 +000086 }
subrata_modak1e9d84b2008-02-06 06:51:13 +000087
Garrett Cooperad14e902010-12-16 10:03:44 -080088 return exit_val;
subrata_modakc7d5c362007-12-28 09:40:55 +000089}
90
Jan Stanceked991ae2014-07-29 10:50:27 +020091static void setup(void)
92{
93 tst_require_root(NULL);
94 check_newpid();
95}
96
subrata_modakc7d5c362007-12-28 09:40:55 +000097int main(int argc, char *argv[])
98{
Garrett Cooperad14e902010-12-16 10:03:44 -080099 int status;
subrata_modakc7d5c362007-12-28 09:40:55 +0000100
Jan Stanceked991ae2014-07-29 10:50:27 +0200101 setup();
102
Garrett Cooperad14e902010-12-16 10:03:44 -0800103 TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
subrata_modakc7d5c362007-12-28 09:40:55 +0000104
Garrett Cooperad14e902010-12-16 10:03:44 -0800105 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_brkm(TFAIL | TTERRNO, cleanup, "clone failed");
Garrett Cooperad14e902010-12-16 10:03:44 -0800107 } else if ((wait(&status)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800108 tst_brkm(TWARN | TERRNO, cleanup, "wait failed");
subrata_modakc7d5c362007-12-28 09:40:55 +0000109 }
110
Garrett Cooperad14e902010-12-16 10:03:44 -0800111 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
112 tst_resm(TFAIL, "child exited abnormally");
113 else if (WIFSIGNALED(status)) {
114 tst_resm(TFAIL, "child was killed with signal = %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800115 WTERMSIG(status));
subrata_modakc7d5c362007-12-28 09:40:55 +0000116 }
117
subrata_modakc7d5c362007-12-28 09:40:55 +0000118 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800119 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800120}
subrata_modakc7d5c362007-12-28 09:40:55 +0000121
yaberauneya7f140ab2009-12-06 20:55:15 +0000122static void cleanup()
subrata_modakc7d5c362007-12-28 09:40:55 +0000123{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}