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