subrata_modak | 910f93a | 2008-12-16 12:34:49 +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 | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | * File: pidns12.c |
| 17 | * * |
| 18 | * * Description: |
| 19 | * * The pidns12.c testcase verifies that siginfo->si_pid is set to 0 |
| 20 | * * if sender (parent process) is not in receiver's namespace. |
| 21 | * * |
| 22 | * * Test Assertion & Strategy: |
| 23 | * * Create a PID namespace container. |
| 24 | * * Initialise signal handler for SIGUSR1 in container. |
| 25 | * * Let parent send SIGUSR1 to container. |
| 26 | * * Check if sender pid is set to 0 from signal info. |
| 27 | * * |
| 28 | * * Usage: <for command-line> |
| 29 | * * pidns12 |
| 30 | * * |
| 31 | * * History: |
| 32 | * * DATE NAME DESCRIPTION |
| 33 | * * 13/11/08 Gowrishankar M Creation of this test. |
| 34 | * * <gowrishankar.m@in.ibm.com> |
| 35 | * |
| 36 | ******************************************************************************/ |
| 37 | #define _GNU_SOURCE 1 |
| 38 | #include <sys/wait.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <signal.h> |
| 41 | #include <string.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <unistd.h> |
| 44 | #include <stdio.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 45 | #include "test.h" |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 46 | #include <libclone.h> |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 47 | #include "pidns_helper.h" |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 48 | |
| 49 | char *TCID = "pidns12"; |
| 50 | int TST_TOTAL = 1; |
| 51 | int errno; |
| 52 | int pipefd[2]; |
| 53 | |
| 54 | #define CHILD_PID 1 |
| 55 | #define PARENT_PID 0 |
| 56 | |
| 57 | /* |
| 58 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 59 | * completion or premature exit. |
| 60 | */ |
| 61 | void cleanup() |
| 62 | { |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 63 | |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * child_signal_handler() - dummy function for sigaction() |
| 68 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 69 | static void child_signal_handler(int sig, siginfo_t * si, void *unused) |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 70 | { |
subrata_modak | c6ca86c | 2009-05-26 07:26:51 +0000 | [diff] [blame] | 71 | /* Recieved SIGUSR1. Check sender pid */ |
| 72 | if (si->si_pid == 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 73 | tst_resm(TPASS, "cinit: signalling PID (from other namespace)" |
| 74 | " is 0 as expected"); |
subrata_modak | c6ca86c | 2009-05-26 07:26:51 +0000 | [diff] [blame] | 75 | else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 76 | tst_resm(TFAIL, "cinit: signalling PID (from other namespace)" |
| 77 | " is not 0, but %d.", si->si_pid); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | * child_fn() - Inside container |
| 82 | */ |
| 83 | int child_fn(void *arg) |
| 84 | { |
| 85 | struct sigaction sa; |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 86 | pid_t pid, ppid; |
| 87 | |
| 88 | /* Set process id and parent pid */ |
| 89 | pid = getpid(); |
| 90 | ppid = getppid(); |
| 91 | if (pid != CHILD_PID || ppid != PARENT_PID) { |
| 92 | tst_resm(TBROK, "cinit: pidns is not created."); |
| 93 | cleanup(); |
| 94 | } |
| 95 | |
| 96 | /* Close read end of pipe */ |
| 97 | close(pipefd[0]); |
| 98 | |
| 99 | /* Set signal handler for SIGUSR1 */ |
| 100 | sa.sa_flags = SA_SIGINFO; |
| 101 | sigfillset(&sa.sa_mask); |
| 102 | sa.sa_sigaction = child_signal_handler; |
| 103 | if (sigaction(SIGUSR1, &sa, NULL) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 104 | tst_resm(TBROK, "cinit: sigaction() failed(%s).", |
| 105 | strerror(errno)); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 106 | cleanup(); |
| 107 | } |
| 108 | |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 109 | /* Let parent to signal SIGUSR1 */ |
| 110 | if (write(pipefd[1], "c:go\0", 5) != 5) { |
| 111 | tst_resm(TBROK, "cinit: pipe is broken to write"); |
| 112 | cleanup(); |
| 113 | } |
| 114 | |
subrata_modak | c6ca86c | 2009-05-26 07:26:51 +0000 | [diff] [blame] | 115 | sleep(3); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 116 | |
| 117 | /* cleanup and exit */ |
| 118 | close(pipefd[1]); |
| 119 | cleanup(); |
| 120 | |
| 121 | /* Control won't reach below */ |
| 122 | exit(0); |
| 123 | } |
| 124 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 125 | static void setup(void) |
| 126 | { |
| 127 | tst_require_root(NULL); |
| 128 | check_newpid(); |
| 129 | } |
| 130 | |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 131 | /*********************************************************************** |
| 132 | * M A I N |
| 133 | ***********************************************************************/ |
| 134 | |
| 135 | int main(int argc, char *argv[]) |
| 136 | { |
subrata_modak | ce23f15 | 2009-01-19 09:23:02 +0000 | [diff] [blame] | 137 | int status; |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 138 | pid_t pid, cpid; |
| 139 | char buf[5]; |
| 140 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 141 | setup(); |
| 142 | |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 143 | pid = getpid(); |
| 144 | tst_resm(TINFO, "parent: PID is %d", pid); |
| 145 | |
| 146 | /* Create pipe for intercommunication */ |
| 147 | if (pipe(pipefd) == -1) { |
| 148 | tst_resm(TBROK, "parent: pipe() failed. aborting!"); |
| 149 | cleanup(); |
| 150 | } |
| 151 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 153 | if (cpid < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 154 | tst_resm(TBROK, "parent: clone() failed(%s).", strerror(errno)); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 155 | cleanup(); |
| 156 | } |
| 157 | |
| 158 | /* Close write end of pipe */ |
| 159 | close(pipefd[1]); |
| 160 | |
| 161 | /* Check if container is ready */ |
| 162 | read(pipefd[0], buf, 5); |
| 163 | if (strcmp(buf, "c:go") != 0) { |
| 164 | tst_resm(TBROK, "parent: container did not respond!"); |
| 165 | cleanup(); |
| 166 | } |
| 167 | |
| 168 | /* Send SIGUSR1 to container init */ |
| 169 | if (kill(cpid, SIGUSR1) == -1) { |
| 170 | tst_resm(TBROK, "parent: kill() failed(%s).", strerror(errno)); |
| 171 | cleanup(); |
| 172 | } |
| 173 | |
| 174 | if (waitpid(cpid, &status, 0) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_resm(TWARN, "parent: waitpid() failed(%s).", |
| 176 | strerror(errno)); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 177 | |
| 178 | if (WIFSIGNALED(status) && WTERMSIG(status)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 179 | tst_resm(TBROK, "child is terminated by signal(%s)", |
| 180 | strsignal(WTERMSIG(status))); |
subrata_modak | 910f93a | 2008-12-16 12:34:49 +0000 | [diff] [blame] | 181 | |
| 182 | /* Cleanup and exit */ |
| 183 | close(pipefd[0]); |
| 184 | cleanup(); |
| 185 | |
| 186 | /* Control won't reach below */ |
| 187 | exit(0); |
| 188 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 189 | } |