subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 13 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 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 | * |
subrata_modak | 1e9d84b | 2008-02-06 06:51:13 +0000 | [diff] [blame] | 30 | * Total Tests: |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 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 | * |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 45 | * FLAG DATE NAME DESCRIPTION |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 46 | * 27/12/07 RISHIKESH K RAJAK <risrajak@in.ibm.com> Created this test |
| 47 | * |
| 48 | *******************************************************************************************/ |
yaberauneya | 6aa2737 | 2009-12-06 19:54:10 +0000 | [diff] [blame] | 49 | #define _GNU_SOURCE |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 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> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 57 | #include "test.h" |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 58 | #define CLEANUP cleanup |
yaberauneya | 6aa2737 | 2009-12-06 19:54:10 +0000 | [diff] [blame] | 59 | #include "libclone.h" |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 60 | #include "pidns_helper.h" |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 61 | |
| 62 | char *TCID = "pid_namespace1"; |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 63 | int TST_TOTAL = 1; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 64 | |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 65 | #define CHILD_PID 1 |
| 66 | #define PARENT_PID 0 |
| 67 | |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 68 | /* |
| 69 | * child_fn1() - Inside container |
| 70 | */ |
| 71 | int child_fn1(void *ttype) |
| 72 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 73 | int exit_val; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 74 | pid_t cpid, ppid; |
| 75 | cpid = getpid(); |
| 76 | ppid = getppid(); |
| 77 | |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 78 | tst_resm(TINFO, "PIDNS test is running inside container"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 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", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | cpid, ppid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 85 | exit_val = 1; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 86 | } |
subrata_modak | 1e9d84b | 2008-02-06 06:51:13 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 88 | return exit_val; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 91 | static void setup(void) |
| 92 | { |
| 93 | tst_require_root(NULL); |
| 94 | check_newpid(); |
| 95 | } |
| 96 | |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 97 | int main(int argc, char *argv[]) |
| 98 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 99 | int status; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 100 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 101 | setup(); |
| 102 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 103 | TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL)); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 104 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 105 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_brkm(TFAIL | TTERRNO, cleanup, "clone failed"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 107 | } else if ((wait(&status)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_brkm(TWARN | TERRNO, cleanup, "wait failed"); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 111 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 115 | WTERMSIG(status)); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 118 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 119 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | } |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 121 | |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 122 | static void cleanup() |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 123 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 124 | } |