blob: b3ff8fe6af982b11dda217320ca758374900e069 [file] [log] [blame]
subrata_modake7942972008-12-18 05:22:59 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080013* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modake7942972008-12-18 05:22:59 +000014*
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 Coopere8530df2010-12-21 11:37:57 -080049#include "test.h"
subrata_modake7942972008-12-18 05:22:59 +000050#include <libclone.h>
Jan Stanceked991ae2014-07-29 10:50:27 +020051#include "pidns_helper.h"
subrata_modake7942972008-12-18 05:22:59 +000052
53char *TCID = "pidns20";
54int TST_TOTAL = 1;
55
56int errno;
57int parent_cinit[2];
58int cinit_parent[2];
Wanlong Gao354ebb42012-12-07 10:10:04 +080059int broken = 1; /* broken should be 0 when test completes properly */
subrata_modake7942972008-12-18 05:22:59 +000060
61#define CHILD_PID 1
62#define PARENT_PID 0
63
subrata_modake7942972008-12-18 05:22:59 +000064void cleanup()
65{
subrata_modake7942972008-12-18 05:22:59 +000066}
67
68/*
69 * child_signal_handler() - to handle SIGUSR1
70 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080071static void child_signal_handler(int sig, siginfo_t * si, void *unused)
subrata_modake7942972008-12-18 05:22:59 +000072{
73 if (si->si_signo != SIGUSR1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080074 tst_resm(TBROK, "cinit: recieved %s unexpectedly!",
75 strsignal(si->si_signo));
subrata_modake7942972008-12-18 05:22:59 +000076 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 */
86int 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 Cooperad14e902010-12-16 10:03:44 -080098 printf("cinit: pidns was not created properly\n");
99 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000100 }
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 Cooperad14e902010-12-16 10:03:44 -0800110 perror("cinit: sigprocmask() failed");
111 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000112 }
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 Cooperad14e902010-12-16 10:03:44 -0800117 perror("cinit: pipe is broken to write");
118 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000119 }
120
121 /* Check if parent has queued up SIGUSR1 */
122 read(parent_cinit[0], buf, 5);
123 if (strcmp(buf, "p:go") != 0) {
Garrett Cooperad14e902010-12-16 10:03:44 -0800124 printf("cinit: parent did not respond!\n");
125 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000126 }
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 Cooperad14e902010-12-16 10:03:44 -0800133 perror("cinit: sigaction failed");
134 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000135 }
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 Cooperad14e902010-12-16 10:03:44 -0800143 printf("cinit: broken flag didn't change\n");
144 exit(1);
subrata_modake7942972008-12-18 05:22:59 +0000145 }
146
147 /* Cleanup and exit */
148 close(cinit_parent[1]);
149 close(parent_cinit[0]);
subrata_modake7942972008-12-18 05:22:59 +0000150 exit(0);
151}
152
Jan Stanceked991ae2014-07-29 10:50:27 +0200153static void setup(void)
154{
155 tst_require_root(NULL);
156 check_newpid();
157}
158
subrata_modake7942972008-12-18 05:22:59 +0000159int main(int argc, char *argv[])
160{
subrata_modake7942972008-12-18 05:22:59 +0000161 int status;
162 char buf[5];
163 pid_t cpid;
164
Jan Stanceked991ae2014-07-29 10:50:27 +0200165 setup();
166
subrata_modake7942972008-12-18 05:22:59 +0000167 /* Create pipes for intercommunication */
168 if (pipe(parent_cinit) == -1 || pipe(cinit_parent) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
subrata_modake7942972008-12-18 05:22:59 +0000170 }
171
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
Garrett Cooperad14e902010-12-16 10:03:44 -0800173 if (cpid == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
subrata_modake7942972008-12-18 05:22:59 +0000175 }
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 Cooperad14e902010-12-16 10:03:44 -0800184 tst_brkm(TBROK, cleanup, "parent: container did not respond!");
subrata_modake7942972008-12-18 05:22:59 +0000185 }
186
187 /* Enqueue SIGUSR1 in pending signal queue of container */
188 if (kill(cpid, SIGUSR1) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800189 tst_brkm(TBROK | TERRNO, cleanup, "kill() failed");
subrata_modake7942972008-12-18 05:22:59 +0000190 }
191
192 tst_resm(TINFO, "parent: signalled SIGUSR1 to container");
193 if (write(parent_cinit[1], "p:go", 5) != 5) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800194 tst_brkm(TBROK | TERRNO, cleanup, "write failed");
subrata_modake7942972008-12-18 05:22:59 +0000195 }
196
197 /* collect exit status of child */
198 if (wait(&status) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800199 tst_brkm(TBROK | TERRNO, cleanup, "wait failed");
subrata_modake7942972008-12-18 05:22:59 +0000200 }
201
202 if (WIFSIGNALED(status)) {
203 if (WTERMSIG(status) == SIGUSR1)
Garrett Cooperad14e902010-12-16 10:03:44 -0800204 tst_resm(TFAIL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205 "user function was not called inside cinit");
subrata_modake7942972008-12-18 05:22:59 +0000206 else
Garrett Cooperad14e902010-12-16 10:03:44 -0800207 tst_resm(TBROK,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 "cinit was terminated by %d",
209 WTERMSIG(status));
subrata_modake7942972008-12-18 05:22:59 +0000210 }
211
212 /* Cleanup and exit */
213 close(parent_cinit[1]);
214 close(cinit_parent[0]);
215 cleanup();
Garrett Cooperad14e902010-12-16 10:03:44 -0800216 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700217}