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