subrata_modak | e794297 | 2008-12-18 05:22:59 +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 | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 14 | * |
| 15 | *************************************************************************** |
| 16 | * File: pidns20.c |
| 17 | * * |
| 18 | * * Description: |
| 19 | * * The pidns20.c testcase verifies that signal handler of SIGUSR1 is called |
| 20 | * * (and cinit is NOT terminated) when: |
| 21 | * * - container-init blocks SIGUSR1, |
| 22 | * * - parent queues SIGUSR1 and |
| 23 | * * - a handler is specified for SIGUSR1 before it is unblocked. |
| 24 | * * |
| 25 | * * Test Assertion & Strategy: |
| 26 | * * Create a PID namespace container. |
| 27 | * * Block SIGUSR1 signal inside it. |
| 28 | * * Let parent to deliver SIGUSR1 signal to container. |
| 29 | * * Redefine SIGUSR1 handler of cinit to user function. |
| 30 | * * Unblock SIGUSR1 from blocked queue. |
| 31 | * * Check if user function is called. |
| 32 | * * |
| 33 | * * Usage: <for command-line> |
| 34 | * * pidns20 |
| 35 | * * |
| 36 | * * History: |
| 37 | * * DATE NAME DESCRIPTION |
| 38 | * * 13/11/08 Gowrishankar M Creation of this test. |
| 39 | * * <gowrishankar.m@in.ibm.com> |
| 40 | * |
| 41 | ******************************************************************************/ |
| 42 | #define _GNU_SOURCE 1 |
| 43 | #include <sys/wait.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <signal.h> |
| 46 | #include <stdlib.h> |
| 47 | #include <unistd.h> |
| 48 | #include <stdio.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 49 | #include "test.h" |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 50 | #include <libclone.h> |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 51 | #include "pidns_helper.h" |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 52 | |
| 53 | char *TCID = "pidns20"; |
| 54 | int TST_TOTAL = 1; |
| 55 | |
| 56 | int errno; |
| 57 | int parent_cinit[2]; |
| 58 | int cinit_parent[2]; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 59 | int broken = 1; /* broken should be 0 when test completes properly */ |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 60 | |
| 61 | #define CHILD_PID 1 |
| 62 | #define PARENT_PID 0 |
| 63 | |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 64 | void cleanup() |
| 65 | { |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * child_signal_handler() - to handle SIGUSR1 |
| 70 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | static void child_signal_handler(int sig, siginfo_t * si, void *unused) |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 72 | { |
| 73 | if (si->si_signo != SIGUSR1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 74 | tst_resm(TBROK, "cinit: recieved %s unexpectedly!", |
| 75 | strsignal(si->si_signo)); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 76 | else |
| 77 | tst_resm(TPASS, "cinit: user function is called as expected"); |
| 78 | |
| 79 | /* Disable broken flag */ |
| 80 | broken = 0; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * child_fn() - Inside container |
| 85 | */ |
| 86 | int child_fn(void *arg) |
| 87 | { |
| 88 | pid_t pid, ppid; |
| 89 | sigset_t newset; |
| 90 | struct sigaction sa; |
| 91 | char buf[5]; |
| 92 | |
| 93 | /* Setup pipe read and write ends */ |
| 94 | pid = getpid(); |
| 95 | ppid = getppid(); |
| 96 | |
| 97 | if (pid != CHILD_PID || ppid != PARENT_PID) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 98 | printf("cinit: pidns was not created properly\n"); |
| 99 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Setup pipes to communicate with parent */ |
| 103 | close(cinit_parent[0]); |
| 104 | close(parent_cinit[1]); |
| 105 | |
| 106 | /* Block SIGUSR1 signal */ |
| 107 | sigemptyset(&newset); |
| 108 | sigaddset(&newset, SIGUSR1); |
| 109 | if (sigprocmask(SIG_BLOCK, &newset, 0) == -1) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 110 | perror("cinit: sigprocmask() failed"); |
| 111 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 112 | } |
| 113 | tst_resm(TINFO, "cinit: blocked SIGUSR1"); |
| 114 | |
| 115 | /* Let parent to queue SIGUSR1 in pending */ |
| 116 | if (write(cinit_parent[1], "c:go", 5) != 5) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 117 | perror("cinit: pipe is broken to write"); |
| 118 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* Check if parent has queued up SIGUSR1 */ |
| 122 | read(parent_cinit[0], buf, 5); |
| 123 | if (strcmp(buf, "p:go") != 0) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 124 | printf("cinit: parent did not respond!\n"); |
| 125 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /* Now redefine handler for SIGUSR1 */ |
| 129 | sa.sa_flags = SA_SIGINFO; |
| 130 | sigfillset(&sa.sa_mask); |
| 131 | sa.sa_sigaction = child_signal_handler; |
| 132 | if (sigaction(SIGUSR1, &sa, NULL) == -1) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 133 | perror("cinit: sigaction failed"); |
| 134 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Unblock traffic on SIGUSR1 queue */ |
| 138 | tst_resm(TINFO, "cinit: unblocking SIGUSR1"); |
| 139 | sigprocmask(SIG_UNBLOCK, &newset, 0); |
| 140 | |
| 141 | /* Check if new handler is called */ |
| 142 | if (broken == 1) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 143 | printf("cinit: broken flag didn't change\n"); |
| 144 | exit(1); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* Cleanup and exit */ |
| 148 | close(cinit_parent[1]); |
| 149 | close(parent_cinit[0]); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 150 | exit(0); |
| 151 | } |
| 152 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 153 | static void setup(void) |
| 154 | { |
| 155 | tst_require_root(NULL); |
| 156 | check_newpid(); |
| 157 | } |
| 158 | |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 159 | int main(int argc, char *argv[]) |
| 160 | { |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 161 | int status; |
| 162 | char buf[5]; |
| 163 | pid_t cpid; |
| 164 | |
Jan Stancek | ed991ae | 2014-07-29 10:50:27 +0200 | [diff] [blame] | 165 | setup(); |
| 166 | |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 167 | /* Create pipes for intercommunication */ |
| 168 | if (pipe(parent_cinit) == -1 || pipe(cinit_parent) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 169 | tst_brkm(TBROK | TERRNO, cleanup, "pipe failed"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 173 | if (cpid == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TBROK | TERRNO, cleanup, "clone failed"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Setup pipe read and write ends */ |
| 178 | close(cinit_parent[1]); |
| 179 | close(parent_cinit[0]); |
| 180 | |
| 181 | /* Is container ready */ |
| 182 | read(cinit_parent[0], buf, 5); |
| 183 | if (strcmp(buf, "c:go") != 0) { |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 184 | tst_brkm(TBROK, cleanup, "parent: container did not respond!"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* Enqueue SIGUSR1 in pending signal queue of container */ |
| 188 | if (kill(cpid, SIGUSR1) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 189 | tst_brkm(TBROK | TERRNO, cleanup, "kill() failed"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | tst_resm(TINFO, "parent: signalled SIGUSR1 to container"); |
| 193 | if (write(parent_cinit[1], "p:go", 5) != 5) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 194 | tst_brkm(TBROK | TERRNO, cleanup, "write failed"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* collect exit status of child */ |
| 198 | if (wait(&status) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | tst_brkm(TBROK | TERRNO, cleanup, "wait failed"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (WIFSIGNALED(status)) { |
| 203 | if (WTERMSIG(status) == SIGUSR1) |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 204 | tst_resm(TFAIL, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 205 | "user function was not called inside cinit"); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 206 | else |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 207 | tst_resm(TBROK, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 208 | "cinit was terminated by %d", |
| 209 | WTERMSIG(status)); |
subrata_modak | e794297 | 2008-12-18 05:22:59 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* Cleanup and exit */ |
| 213 | close(parent_cinit[1]); |
| 214 | close(cinit_parent[0]); |
| 215 | cleanup(); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 216 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 217 | } |