subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2008 |
| 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 | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 14 | |
| 15 | ************************************************************************* |
| 16 | * Description: |
| 17 | * Testcase tries killing of the parent namespace pid by the container-init. |
| 18 | * It also tries killing of non-existent PID, by the container-init. |
| 19 | * Returns Success if Unable to kill, and proper error number is set. |
| 20 | * else Returns Failure |
| 21 | * |
| 22 | * Steps: |
| 23 | * 1. Parent process clone a process with flag CLONE_NEWPID |
| 24 | * 2. The pid of the parent namespace is passed to the container. |
| 25 | * 3. Container receieves the PID and passes SIGKILL to this PID. |
| 26 | * 4. If kill() is unsuccessful and the errno is set to 'No Such process' |
| 27 | * then sets PASS |
| 28 | * else, |
| 29 | * sets FAIL |
| 30 | * 5. It also verifies by passing SIGKILL to FAKE_PID |
| 31 | * 6. If kill() is unsuccessful and the errno is set to 'No Such process' |
| 32 | * then sets PASS |
| 33 | * else, |
| 34 | * sets FAIL |
| 35 | * |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 36 | *******************************************************************************/ |
| 37 | #define _GNU_SOURCE 1 |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <sys/wait.h> |
| 41 | #include <assert.h> |
| 42 | #include <unistd.h> |
| 43 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 44 | #include "test.h" |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 45 | #include <libclone.h> |
| 46 | #include <signal.h> |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 47 | #include "pidns_helper.h" |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 48 | |
| 49 | #define CINIT_PID 1 |
| 50 | #define PARENT_PID 0 |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 51 | #define FAKE_PID -1 |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "pidns06"; |
| 54 | int TST_TOTAL = 1; |
| 55 | |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 56 | void cleanup() |
| 57 | { |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /* |
| 61 | * kill_pid_in_childfun() |
| 62 | * Cont-init tries to kill the parent-process using parent's global Pid. |
| 63 | * Also checks passing SIGKILL to non existent PID in the container. |
| 64 | */ |
| 65 | static int kill_pid_in_childfun(void *vtest) |
| 66 | { |
| 67 | int cpid, ppid, *par_pid; |
| 68 | int ret = 0; |
| 69 | cpid = getpid(); |
| 70 | ppid = getppid(); |
| 71 | par_pid = (int *)vtest; |
| 72 | |
| 73 | /* Checking the values to make sure pidns is created correctly */ |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 74 | if (cpid != CINIT_PID || ppid != PARENT_PID) { |
| 75 | printf("Unexpected result for Container: init " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | "pid=%d ppid=%d\n", cpid, ppid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 77 | exit(1); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 81 | * While trying kill() of the pid of the parent namespace.. |
| 82 | * Check to see if the errno was set to the expected, value of 3 : ESRCH |
| 83 | */ |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 84 | ret = kill(*par_pid, SIGKILL); |
| 85 | if (ret == -1 && errno == ESRCH) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 86 | printf("Container: killing parent pid=%d failed as expected " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 87 | "with ESRCH\n", *par_pid); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 88 | } else { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 89 | printf("Container: killing parent pid=%d, didn't fail as " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | "expected with ESRCH (%d) and a return value of -1. Got " |
| 91 | "%d (\"%s\") and a return value of %d instead.\n", |
| 92 | *par_pid, ESRCH, errno, strerror(errno), ret); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 93 | exit(1); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 94 | } |
| 95 | /* |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 96 | * While killing non-existent pid in the container, |
| 97 | * Check to see if the errno was set to the expected, value of 3 : ESRCH |
| 98 | */ |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 99 | ret = kill(FAKE_PID, SIGKILL); |
| 100 | if (ret == -1 && errno == ESRCH) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 101 | printf("Container: killing non-existent pid failed as expected " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | "with ESRCH\n"); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 103 | } else { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 104 | printf("Container: killing non-existent pid, didn't fail as " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | "expected with ESRCH (%d) and a return value of -1. Got " |
| 106 | "%d (\"%s\") and a return value of %d instead.\n", |
| 107 | ESRCH, errno, strerror(errno), ret); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 108 | exit(1); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 111 | exit(0); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 114 | static void setup(void) |
| 115 | { |
| 116 | tst_require_root(NULL); |
| 117 | check_newpid(); |
| 118 | } |
| 119 | |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 120 | int main() |
| 121 | { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 122 | int status; |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 123 | |
| 124 | setup(); |
| 125 | |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 126 | pid_t pid = getpid(); |
| 127 | |
| 128 | tst_resm(TINFO, "Parent: Passing the pid of the process %d", pid); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 129 | TEST(do_clone_unshare_test(T_CLONE, CLONE_NEWPID, kill_pid_in_childfun, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | (void *)&pid)); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 131 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_brkm(TFAIL | TERRNO, cleanup, "clone failed"); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 133 | } else if (wait(&status) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 134 | tst_brkm(TFAIL | TERRNO, cleanup, "wait failed"); |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 135 | } |
| 136 | |
subrata_modak | 66e0850 | 2008-11-07 09:06:01 +0000 | [diff] [blame] | 137 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 138 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 139 | } |