subrata_modak | 698b9aa | 2008-12-15 13:13:16 +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 | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | * File: pidns10.c |
| 17 | * * |
| 18 | * * Description: |
| 19 | * * The pidns10.c testcase verifies inside the container, if kill(-1, signal) |
| 20 | * * fails with ESRCH when there are no processes in container. |
| 21 | * * |
| 22 | * * Test Assertion & Strategy: |
| 23 | * * Create a PID namespace container. |
| 24 | * * Invoke kill(-1, SIGUSR1) inside container and check return code and error. |
| 25 | * * kill() should have failed;except swapper & init, no process is inside. |
| 26 | * * |
| 27 | * * Usage: <for command-line> |
| 28 | * * pidns10 |
| 29 | * * |
| 30 | * * History: |
| 31 | * * DATE NAME DESCRIPTION |
| 32 | * * 13/11/08 Gowrishankar M Creation of this test. |
| 33 | * * <gowrishankar.m@in.ibm.com> |
| 34 | * |
| 35 | ******************************************************************************/ |
| 36 | #define _GNU_SOURCE 1 |
| 37 | #include <sys/wait.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <string.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <unistd.h> |
| 42 | #include <stdio.h> |
| 43 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 44 | #include "test.h" |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 45 | #define CLEANUP cleanup |
| 46 | #include "libclone.h" |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 47 | #include "pidns_helper.h" |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 48 | |
| 49 | char *TCID = "pidns10"; |
| 50 | int TST_TOTAL = 1; |
| 51 | int errno; |
| 52 | |
| 53 | int child_fn(void *); |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 54 | |
| 55 | #define CHILD_PID 1 |
| 56 | #define PARENT_PID 0 |
| 57 | |
| 58 | /* |
| 59 | * child_fn() - Inside container |
| 60 | */ |
| 61 | int child_fn(void *arg) |
| 62 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 63 | int exit_val, ret; |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 64 | pid_t pid, ppid; |
| 65 | |
| 66 | /* Set process id and parent pid */ |
| 67 | pid = getpid(); |
| 68 | ppid = getppid(); |
| 69 | if (pid != CHILD_PID || ppid != PARENT_PID) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 70 | printf("cinit: pidns was not created.\n"); |
| 71 | return 1; |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 74 | if ((ret = kill(-1, SIGUSR1)) == -1 && errno == ESRCH) { |
| 75 | printf("cinit: kill(-1, sig) failed with -1 / ESRCH as " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | "expected\n"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 77 | exit_val = 0; |
| 78 | } else { |
| 79 | printf("cinit: kill(-1, sig) didn't fail with -1 / ESRCH " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | "(%d); failed with %d / %d instead", ESRCH, ret, errno); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 81 | exit_val = 1; |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 82 | } |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 83 | exit(exit_val); |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 86 | static void setup(void) |
| 87 | { |
| 88 | tst_require_root(NULL); |
| 89 | check_newpid(); |
| 90 | } |
| 91 | |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 92 | int main(int argc, char *argv[]) |
| 93 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 94 | int status; |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 95 | pid_t pid; |
| 96 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 97 | setup(); |
| 98 | |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 99 | pid = getpid(); |
| 100 | |
| 101 | /* Container creation on PID namespace */ |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 102 | TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, child_fn, NULL)); |
| 103 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | tst_brkm(TBROK | TTERRNO, CLEANUP, "clone failed"); |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | sleep(1); |
| 108 | if (wait(&status) < 0) |
| 109 | tst_resm(TWARN, "parent: waitpid() failed."); |
| 110 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 111 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 112 | tst_resm(TBROK, "container was terminated abnormally"); |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 113 | |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 114 | CLEANUP(); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 115 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | } |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 119 | * cleanup() - performs all ONE TIME CLEANUP for this test at |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 120 | * completion or premature exit. |
| 121 | */ |
yaberauneya | 7f140ab | 2009-12-06 20:55:15 +0000 | [diff] [blame] | 122 | static void cleanup() |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 123 | { |
subrata_modak | 698b9aa | 2008-12-15 13:13:16 +0000 | [diff] [blame] | 124 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 125 | } |