subrata_modak | d757ea1 | 2008-12-12 14:39:20 +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 | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | |
| 17 | * * Test Assertion. |
| 18 | * *---------------- |
| 19 | * * kill -USR1 container_init |
| 20 | * * - from the parent process and also inside a container |
| 21 | * * - Where init has defined a custom handler for USR1 |
| 22 | * * - Should call the handler and |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 23 | * * - Verify whether the signal handler is called from the proper process. |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 24 | * * |
| 25 | * * Description: |
| 26 | * * Create PID namespace container. |
| 27 | * * Container init defines the handler for SIGUSR1 and waits indefinetly. |
| 28 | * * Parent sends SIGUSR1 to container init. |
| 29 | * * The signal handler is handled and the cont-init resumes normally. |
| 30 | * * From the container, again the signal SIGUSR1 is sent. |
| 31 | * * In the sig-handler check if it's invoked from correct pid(parent/container) |
| 32 | * * If cont-init wakes up properly - |
| 33 | * * it will return expected value at exit which is verified at the end. |
| 34 | * * |
| 35 | * * History: |
| 36 | * * DATE NAME DESCRIPTION |
| 37 | * * 04/11/08 Veerendra C <vechandr@in.ibm.com> Verifying cont init kill -USR1 |
| 38 | * |
| 39 | *******************************************************************************/ |
subrata_modak | 4235284 | 2009-01-08 08:54:39 +0000 | [diff] [blame] | 40 | #include "config.h" |
| 41 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 42 | #define _GNU_SOURCE 1 |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 45 | #include <sys/wait.h> |
| 46 | #include <sys/types.h> |
| 47 | #include <signal.h> |
| 48 | #include <unistd.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 49 | #include "test.h" |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 50 | #include <libclone.h> |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 51 | #include "pidns_helper.h" |
| 52 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 53 | #define CHILD_PID 1 |
| 54 | #define PARENT_PID 0 |
| 55 | |
| 56 | char *TCID = "pidns16"; |
Monson Shao | a5eeee9 | 2012-12-26 16:48:36 +0800 | [diff] [blame] | 57 | int TST_TOTAL = 3; |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 61 | * completion or premature exit. |
| 62 | */ |
| 63 | void cleanup() |
| 64 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 65 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 68 | void child_signal_handler(int sig, siginfo_t * si, void *unused) |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 69 | { |
| 70 | static int c = 1; |
Monson Shao | c19b210 | 2012-12-26 16:48:35 +0800 | [diff] [blame] | 71 | pid_t expected_pid; |
| 72 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 73 | /* Verifying from which process the signal handler is signalled */ |
| 74 | |
Monson Shao | c19b210 | 2012-12-26 16:48:35 +0800 | [diff] [blame] | 75 | switch (c) { |
| 76 | case 1: |
Monson Shao | 38bf6cd | 2012-12-26 16:48:37 +0800 | [diff] [blame] | 77 | expected_pid = PARENT_PID; |
Monson Shao | c19b210 | 2012-12-26 16:48:35 +0800 | [diff] [blame] | 78 | break; |
| 79 | case 2: |
| 80 | expected_pid = CHILD_PID; |
| 81 | break; |
| 82 | default: |
| 83 | tst_resm(TBROK, "child should NOT be signalled 3+ times"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (si->si_pid == expected_pid) |
Monson Shao | a5eeee9 | 2012-12-26 16:48:36 +0800 | [diff] [blame] | 88 | tst_resm(TPASS, "child is signalled from expected pid %d", |
Monson Shao | c19b210 | 2012-12-26 16:48:35 +0800 | [diff] [blame] | 89 | expected_pid); |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 90 | else |
Monson Shao | a5eeee9 | 2012-12-26 16:48:36 +0800 | [diff] [blame] | 91 | tst_resm(TFAIL, "child is signalled from unexpected pid %d," |
Monson Shao | c19b210 | 2012-12-26 16:48:35 +0800 | [diff] [blame] | 92 | " expecting pid %d", si->si_pid, expected_pid); |
| 93 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 94 | c++; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * child_fn() - Inside container |
| 99 | */ |
| 100 | int child_fn(void *ttype) |
| 101 | { |
| 102 | struct sigaction sa; |
| 103 | pid_t pid, ppid; |
| 104 | |
| 105 | /* Set process id and parent pid */ |
| 106 | pid = getpid(); |
| 107 | ppid = getppid(); |
| 108 | |
| 109 | if ((pid != CHILD_PID) || (ppid != PARENT_PID)) |
| 110 | tst_resm(TBROK, "pidns is not created."); |
| 111 | |
| 112 | /* Set signal handler for SIGUSR1, also mask other signals */ |
| 113 | sa.sa_flags = SA_SIGINFO; |
| 114 | sigemptyset(&sa.sa_mask); |
| 115 | sa.sa_sigaction = child_signal_handler; |
| 116 | if (sigaction(SIGUSR1, &sa, NULL) == -1) |
| 117 | tst_resm(TBROK, "%d: sigaction() failed", pid); |
| 118 | |
| 119 | pause(); |
| 120 | tst_resm(TINFO, "Container: Resumed after receiving SIGUSR1 " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | "from parentNS "); |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 122 | if (kill(pid, SIGUSR1) != 0) { |
| 123 | tst_resm(TFAIL, "kill(SIGUSR1) fails."); |
| 124 | cleanup(); |
| 125 | } |
| 126 | tst_resm(TINFO, "Container: Resumed after sending SIGUSR1 " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | "from container itself"); |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 128 | _exit(10); |
| 129 | } |
| 130 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 131 | static void setup(void) |
| 132 | { |
| 133 | tst_require_root(NULL); |
| 134 | check_newpid(); |
| 135 | } |
| 136 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 137 | /*********************************************************************** |
| 138 | * M A I N |
| 139 | ***********************************************************************/ |
| 140 | int main(int argc, char *argv[]) |
| 141 | { |
subrata_modak | ce23f15 | 2009-01-19 09:23:02 +0000 | [diff] [blame] | 142 | int status; |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 143 | pid_t cpid; |
| 144 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 145 | setup(); |
| 146 | |
vapier | f6d7f09 | 2009-11-03 20:07:35 +0000 | [diff] [blame] | 147 | cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL); |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 148 | |
| 149 | if (cpid < 0) { |
| 150 | tst_resm(TBROK, "clone() failed."); |
| 151 | cleanup(); |
| 152 | } |
| 153 | |
| 154 | sleep(1); |
| 155 | if (kill(cpid, SIGUSR1) != 0) { |
| 156 | tst_resm(TFAIL, "kill(SIGUSR1) fails."); |
| 157 | cleanup(); |
| 158 | } |
| 159 | sleep(1); |
| 160 | if (waitpid(cpid, &status, 0) < 0) |
| 161 | tst_resm(TWARN, "waitpid() failed."); |
| 162 | |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 163 | if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10)) |
| 164 | tst_resm(TPASS, "container init continued successfuly, " |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 165 | "after handling signal -USR1"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 166 | else |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 167 | tst_resm(TFAIL, "c-init failed to continue after " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | "passing kill -USR1"); |
subrata_modak | d757ea1 | 2008-12-12 14:39:20 +0000 | [diff] [blame] | 169 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 171 | } |