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: pidns02.c |
| 18 | * |
| 19 | * Description: |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 20 | * The pidns02.c testcase builds into the ltp framework to verify |
| 21 | * the basic functionality of PID Namespace. |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 22 | * |
| 23 | * Verify that: |
| 24 | * 1. When parent clone a process with flag CLONE_NEWPID, the session ID of |
| 25 | * child should be always one. |
| 26 | * |
| 27 | * 2. When parent clone a process with flag CLONE_NEWPID, the parent process group ID |
| 28 | * should be always one. |
| 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: pidns02 |
| 33 | * |
| 34 | * Test Assertion & Strategy: |
| 35 | * |
| 36 | * From main() clone a new child process with passing the clone_flag as CLONE_NEWPID, |
| 37 | * Call the setid() inside container. |
| 38 | * Inside the cloned pid check for the getsid(0) and getpgid(0) |
| 39 | * Verify with global macro defined value for parent pid & child pid. |
| 40 | * |
| 41 | * Usage: <for command-line> |
| 42 | * pidns02 |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 43 | */ |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 44 | |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 45 | #define _GNU_SOURCE |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 46 | #include <sys/wait.h> |
| 47 | #include <assert.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <unistd.h> |
| 51 | #include <string.h> |
| 52 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 53 | #include "test.h" |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 54 | #define CLEANUP cleanup |
| 55 | #include "libclone.h" |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 56 | #include "pidns_helper.h" |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 57 | |
| 58 | char *TCID = "pid_namespace2"; |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 59 | int TST_TOTAL = 1; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 60 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 61 | #define PGID 1 |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 62 | #define SID 1 |
| 63 | |
| 64 | /* |
| 65 | * child_fn1() - Inside container |
| 66 | */ |
| 67 | int child_fn1(void *vtest) |
| 68 | { |
| 69 | pid_t pgid, sid; |
| 70 | |
| 71 | setsid(); |
| 72 | |
| 73 | pgid = getpgid(0); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 74 | sid = getsid(0); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 75 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 76 | printf("Checking session id & group id inside container\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | if (pgid == PGID && sid == SID) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 78 | printf("Success: Got Group ID = %d & Session ID = %d\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | pgid, sid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 80 | exit(0); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 81 | } else { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 82 | printf("Got unexpected result of Group ID = %d & Session ID = " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 83 | "%d\n", pgid, sid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 84 | exit(1); |
| 85 | } |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 88 | static void setup(void) |
| 89 | { |
| 90 | tst_require_root(NULL); |
| 91 | check_newpid(); |
| 92 | } |
| 93 | |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 94 | int main(int argc, char *argv[]) |
| 95 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 96 | int status; |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 97 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 98 | setup(); |
| 99 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 100 | TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn1, NULL)); |
| 101 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | tst_brkm(TFAIL | TTERRNO, CLEANUP, "clone failed"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 103 | } else if ((wait(&status)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | tst_brkm(TFAIL | TERRNO, CLEANUP, "wait failed"); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 107 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 108 | tst_resm(TFAIL | TERRNO, "child exited abnormally"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 109 | } else if (WIFSIGNALED(status)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | tst_resm(TFAIL | TERRNO, "child exited with signal %d", |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 111 | WTERMSIG(status)); |
| 112 | } |
| 113 | |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 114 | CLEANUP(); |
| 115 | tst_exit(); |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 116 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 117 | } |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 118 | |
| 119 | /* |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 120 | * CLEANUP() - performs all ONE TIME CLEANUP for this test at |
| 121 | * completion or premature exit. |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 122 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | void cleanup() |
subrata_modak | c7d5c36 | 2007-12-28 09:40:55 +0000 | [diff] [blame] | 124 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 125 | } |