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: 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 *
30 * Total Tests:
31 *
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 *
45 * FLAG DATE NAME DESCRIPTION
46 * 27/12/07 RISHIKESH K RAJAK <risrajak@in.ibm.com> Created this test
47 *
48 *******************************************************************************************/
49 #define _GNU_SOURCE
50 #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>
57 #include "test.h"
58 #include "libclone.h"
59 #include "pidns_helper.h"
60
61 char *TCID = "pidns01";
62 int TST_TOTAL = 1;
63
64 #define CHILD_PID 1
65 #define PARENT_PID 0
66
67 /*
68 * child_fn1() - Inside container
69 */
child_fn1(void * ttype LTP_ATTRIBUTE_UNUSED)70 int child_fn1(void *ttype LTP_ATTRIBUTE_UNUSED)
71 {
72 int exit_val;
73 pid_t cpid, ppid;
74 cpid = getpid();
75 ppid = getppid();
76
77 tst_resm(TINFO, "PIDNS test is running inside container");
78 if (cpid == CHILD_PID && ppid == PARENT_PID) {
79 printf("Got expected cpid and ppid\n");
80 exit_val = 0;
81 } else {
82 printf("Got unexpected result of cpid=%d ppid=%d\n",
83 cpid, ppid);
84 exit_val = 1;
85 }
86
87 return exit_val;
88 }
89
setup(void)90 static void setup(void)
91 {
92 tst_require_root();
93 check_newpid();
94 }
95
main(int argc,char * argv[])96 int main(int argc, char *argv[])
97 {
98 int status;
99 tst_parse_opts(argc, argv, NULL, NULL);
100 setup();
101
102 TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL));
103
104 if (TEST_RETURN == -1) {
105 tst_brkm(TFAIL | TTERRNO, NULL, "clone failed");
106 } else if ((wait(&status)) == -1) {
107 tst_brkm(TWARN | TERRNO, NULL, "wait failed");
108 }
109
110 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
111 tst_resm(TFAIL, "child exited abnormally");
112 else if (WIFSIGNALED(status)) {
113 tst_resm(TFAIL, "child was killed with signal = %d",
114 WTERMSIG(status));
115 }
116
117 tst_exit();
118 }
119
120