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 13* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 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> 49#include "test.h" 50#include <libclone.h> 51#include "pidns_helper.h" 52 53char *TCID = "pidns20"; 54int TST_TOTAL = 1; 55 56int errno; 57int parent_cinit[2]; 58int cinit_parent[2]; 59int broken = 1; /* broken should be 0 when test completes properly */ 60 61#define CHILD_PID 1 62#define PARENT_PID 0 63 64/* 65 * child_signal_handler() - to handle SIGUSR1 66 */ 67static void child_signal_handler(int sig, siginfo_t * si, void *unused) 68{ 69 if (si->si_signo != SIGUSR1) 70 tst_resm(TBROK, "cinit: recieved %s unexpectedly!", 71 strsignal(si->si_signo)); 72 else 73 tst_resm(TPASS, "cinit: user function is called as expected"); 74 75 /* Disable broken flag */ 76 broken = 0; 77} 78 79/* 80 * child_fn() - Inside container 81 */ 82int child_fn(void *arg) 83{ 84 pid_t pid, ppid; 85 sigset_t newset; 86 struct sigaction sa; 87 char buf[5]; 88 89 /* Setup pipe read and write ends */ 90 pid = getpid(); 91 ppid = getppid(); 92 93 if (pid != CHILD_PID || ppid != PARENT_PID) { 94 printf("cinit: pidns was not created properly\n"); 95 exit(1); 96 } 97 98 /* Setup pipes to communicate with parent */ 99 close(cinit_parent[0]); 100 close(parent_cinit[1]); 101 102 /* Block SIGUSR1 signal */ 103 sigemptyset(&newset); 104 sigaddset(&newset, SIGUSR1); 105 if (sigprocmask(SIG_BLOCK, &newset, 0) == -1) { 106 perror("cinit: sigprocmask() failed"); 107 exit(1); 108 } 109 tst_resm(TINFO, "cinit: blocked SIGUSR1"); 110 111 /* Let parent to queue SIGUSR1 in pending */ 112 if (write(cinit_parent[1], "c:go", 5) != 5) { 113 perror("cinit: pipe is broken to write"); 114 exit(1); 115 } 116 117 /* Check if parent has queued up SIGUSR1 */ 118 read(parent_cinit[0], buf, 5); 119 if (strcmp(buf, "p:go") != 0) { 120 printf("cinit: parent did not respond!\n"); 121 exit(1); 122 } 123 124 /* Now redefine handler for SIGUSR1 */ 125 sa.sa_flags = SA_SIGINFO; 126 sigfillset(&sa.sa_mask); 127 sa.sa_sigaction = child_signal_handler; 128 if (sigaction(SIGUSR1, &sa, NULL) == -1) { 129 perror("cinit: sigaction failed"); 130 exit(1); 131 } 132 133 /* Unblock traffic on SIGUSR1 queue */ 134 tst_resm(TINFO, "cinit: unblocking SIGUSR1"); 135 sigprocmask(SIG_UNBLOCK, &newset, 0); 136 137 /* Check if new handler is called */ 138 if (broken == 1) { 139 printf("cinit: broken flag didn't change\n"); 140 exit(1); 141 } 142 143 /* Cleanup and exit */ 144 close(cinit_parent[1]); 145 close(parent_cinit[0]); 146 exit(0); 147} 148 149static void setup(void) 150{ 151 tst_require_root(); 152 check_newpid(); 153} 154 155int main(int argc, char *argv[]) 156{ 157 int status; 158 char buf[5]; 159 pid_t cpid; 160 161 setup(); 162 163 /* Create pipes for intercommunication */ 164 if (pipe(parent_cinit) == -1 || pipe(cinit_parent) == -1) { 165 tst_brkm(TBROK | TERRNO, NULL, "pipe failed"); 166 } 167 168 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL); 169 if (cpid == -1) { 170 tst_brkm(TBROK | TERRNO, NULL, "clone failed"); 171 } 172 173 /* Setup pipe read and write ends */ 174 close(cinit_parent[1]); 175 close(parent_cinit[0]); 176 177 /* Is container ready */ 178 read(cinit_parent[0], buf, 5); 179 if (strcmp(buf, "c:go") != 0) { 180 tst_brkm(TBROK, NULL, "parent: container did not respond!"); 181 } 182 183 /* Enqueue SIGUSR1 in pending signal queue of container */ 184 if (kill(cpid, SIGUSR1) == -1) { 185 tst_brkm(TBROK | TERRNO, NULL, "kill() failed"); 186 } 187 188 tst_resm(TINFO, "parent: signalled SIGUSR1 to container"); 189 if (write(parent_cinit[1], "p:go", 5) != 5) { 190 tst_brkm(TBROK | TERRNO, NULL, "write failed"); 191 } 192 193 /* collect exit status of child */ 194 if (wait(&status) == -1) { 195 tst_brkm(TBROK | TERRNO, NULL, "wait failed"); 196 } 197 198 if (WIFSIGNALED(status)) { 199 if (WTERMSIG(status) == SIGUSR1) 200 tst_resm(TFAIL, 201 "user function was not called inside cinit"); 202 else 203 tst_resm(TBROK, 204 "cinit was terminated by %d", 205 WTERMSIG(status)); 206 } 207 208 /* Cleanup and exit */ 209 close(parent_cinit[1]); 210 close(cinit_parent[0]); 211 tst_exit(); 212} 213