subrata_modak | c476f1d | 2008-10-15 16:46:46 +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 | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | |
| 17 | * File: pidns04.c |
| 18 | * |
| 19 | * Description: |
| 20 | * The pidns04.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 one. |
| 26 | * |
| 27 | * 2. When parent clone a process with flag CLONE_NEWPID, the parent process ID |
| 28 | * of should be zero. |
| 29 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * 3. The container init process (one), should not get killed by the SIGKILL in |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 31 | * the childNS |
| 32 | * |
| 33 | * Total Tests: |
| 34 | * |
| 35 | * Test Name: pidns04 |
| 36 | * |
| 37 | * Test Assertion & Strategy: |
| 38 | * |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 39 | * From main() clone a new child process with passing the clone_flag as |
| 40 | * CLONE_NEWPID. |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 41 | * The container init, should not get killed by the SIGKILL inside the child NS. |
| 42 | * Usage: <for command-line> |
| 43 | * pidns04 |
| 44 | * |
| 45 | * History: |
| 46 | * |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 47 | * FLAG DATE NAME DESCRIPTION |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 48 | * 08/10/08 Veerendra C <vechandr@in.ibm.com> Verifies killing of cont init. |
| 49 | * |
| 50 | *******************************************************************************/ |
| 51 | #define _GNU_SOURCE 1 |
| 52 | #include <sys/wait.h> |
| 53 | #include <assert.h> |
| 54 | #include <stdio.h> |
| 55 | #include <stdlib.h> |
| 56 | #include <unistd.h> |
| 57 | #include <string.h> |
| 58 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 59 | #include "test.h" |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 60 | #define CLEANUP cleanup |
| 61 | #include "libclone.h" |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 62 | #include "pidns_helper.h" |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 63 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 64 | #define INIT_PID 1 |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 65 | #define CHILD_PID 1 |
| 66 | #define PARENT_PID 0 |
| 67 | |
| 68 | char *TCID = "pid_namespace4"; |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 69 | int TST_TOTAL = 1; |
| 70 | int fd[2]; |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * child_fn1() - Inside container |
| 74 | */ |
| 75 | static int child_fn1(void *ttype) |
| 76 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 77 | int exit_val; |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 78 | pid_t cpid, ppid; |
| 79 | cpid = getpid(); |
| 80 | ppid = getppid(); |
| 81 | char mesg[] = "I was not killed !"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | /* Child process closes up read side of pipe */ |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 83 | close(fd[0]); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 84 | |
| 85 | /* Comparing the values to make sure pidns is created correctly */ |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 86 | if ((cpid == CHILD_PID) && (ppid == PARENT_PID)) { |
| 87 | printf("PIDNS test is running inside container\n"); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 88 | kill(INIT_PID, SIGKILL); |
| 89 | /* Verifying whether the container init is not killed, " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | If so writing into the pipe created in the parent NS" */ |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 91 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 92 | /* Send "mesg" through the write side of pipe */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | write(fd[1], mesg, (strlen(mesg) + 1)); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 94 | exit_val = 0; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | } else { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 96 | printf("got unexpected result of cpid=%d ppid=%d\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | cpid, ppid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 98 | exit_val = 1; |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 99 | } |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 100 | exit(exit_val); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 103 | static void setup(void) |
| 104 | { |
| 105 | tst_require_root(NULL); |
| 106 | check_newpid(); |
| 107 | } |
| 108 | |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 109 | int main(int argc, char *argv[]) |
| 110 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 111 | int nbytes, status; |
| 112 | char readbuffer[80]; |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 113 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 114 | setup(); |
| 115 | |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 116 | pipe(fd); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 117 | TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL)); |
| 118 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 120 | } else if (wait(&status) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed"); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* Parent process closes up write side of pipe */ |
| 125 | close(fd[1]); |
| 126 | /* Read in a string from the pipe */ |
| 127 | nbytes = read(fd[0], readbuffer, sizeof(readbuffer)); |
| 128 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 129 | if (0 <= nbytes) { |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 130 | tst_resm(TPASS, "Container init : %s", readbuffer); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 131 | } else { |
| 132 | tst_brkm(TFAIL, CLEANUP, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | "Container init is killed by SIGKILL !!!"); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 136 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
| 137 | tst_resm(TFAIL, "Container init pid exited abnormally"); |
| 138 | } else if (WIFSIGNALED(status)) { |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 139 | tst_resm(TFAIL, "Container init pid got killed by signal %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | WTERMSIG(status)); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 141 | } |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 142 | CLEANUP(); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 143 | |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 144 | tst_exit(); |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 145 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 147 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | static void cleanup(void) |
subrata_modak | c476f1d | 2008-10-15 16:46:46 +0000 | [diff] [blame] | 149 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 150 | close(fd[0]); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 151 | } |