blob: 428ff67c7a1661570f2ed49b3da7079b087060d7 [file] [log] [blame]
subrata_modakd757ea12008-12-12 14:39:20 +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_modakd757ea12008-12-12 14:39:20 +000014*
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 Shao45192242013-01-08 11:34:44 +080023* * - Verify whether the signal handler is called from the proper process.
subrata_modakd757ea12008-12-12 14:39:20 +000024* *
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_modak42352842009-01-08 08:54:39 +000040#include "config.h"
41
subrata_modakd757ea12008-12-12 14:39:20 +000042#define _GNU_SOURCE 1
43#include <stdio.h>
44#include <stdlib.h>
subrata_modakd757ea12008-12-12 14:39:20 +000045#include <sys/wait.h>
46#include <sys/types.h>
47#include <signal.h>
48#include <unistd.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080049#include "test.h"
subrata_modakd757ea12008-12-12 14:39:20 +000050#include <libclone.h>
Jan Stanceked991ae2014-07-29 10:50:27 +020051#include "pidns_helper.h"
52
subrata_modakd757ea12008-12-12 14:39:20 +000053#define CHILD_PID 1
54#define PARENT_PID 0
55
56char *TCID = "pidns16";
Monson Shaoa5eeee92012-12-26 16:48:36 +080057int TST_TOTAL = 3;
subrata_modakd757ea12008-12-12 14:39:20 +000058
59/*
60 * cleanup() - performs all ONE TIME cleanup for this test at
61 * completion or premature exit.
62 */
63void cleanup()
64{
Garrett Cooper2c282152010-12-16 00:55:50 -080065
subrata_modakd757ea12008-12-12 14:39:20 +000066}
67
Wanlong Gao354ebb42012-12-07 10:10:04 +080068void child_signal_handler(int sig, siginfo_t * si, void *unused)
subrata_modakd757ea12008-12-12 14:39:20 +000069{
70 static int c = 1;
Monson Shaoc19b2102012-12-26 16:48:35 +080071 pid_t expected_pid;
72
subrata_modakd757ea12008-12-12 14:39:20 +000073 /* Verifying from which process the signal handler is signalled */
74
Monson Shaoc19b2102012-12-26 16:48:35 +080075 switch (c) {
76 case 1:
Monson Shao38bf6cd2012-12-26 16:48:37 +080077 expected_pid = PARENT_PID;
Monson Shaoc19b2102012-12-26 16:48:35 +080078 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 Shaoa5eeee92012-12-26 16:48:36 +080088 tst_resm(TPASS, "child is signalled from expected pid %d",
Monson Shaoc19b2102012-12-26 16:48:35 +080089 expected_pid);
subrata_modakd757ea12008-12-12 14:39:20 +000090 else
Monson Shaoa5eeee92012-12-26 16:48:36 +080091 tst_resm(TFAIL, "child is signalled from unexpected pid %d,"
Monson Shaoc19b2102012-12-26 16:48:35 +080092 " expecting pid %d", si->si_pid, expected_pid);
93
subrata_modakd757ea12008-12-12 14:39:20 +000094 c++;
95}
96
97/*
98 * child_fn() - Inside container
99 */
100int 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 Gao354ebb42012-12-07 10:10:04 +0800121 "from parentNS ");
subrata_modakd757ea12008-12-12 14:39:20 +0000122 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 Gao354ebb42012-12-07 10:10:04 +0800127 "from container itself");
subrata_modakd757ea12008-12-12 14:39:20 +0000128 _exit(10);
129}
130
Jan Stanceked991ae2014-07-29 10:50:27 +0200131static void setup(void)
132{
133 tst_require_root(NULL);
134 check_newpid();
135}
136
subrata_modakd757ea12008-12-12 14:39:20 +0000137/***********************************************************************
138* M A I N
139***********************************************************************/
140int main(int argc, char *argv[])
141{
subrata_modakce23f152009-01-19 09:23:02 +0000142 int status;
subrata_modakd757ea12008-12-12 14:39:20 +0000143 pid_t cpid;
144
Jan Stanceked991ae2014-07-29 10:50:27 +0200145 setup();
146
vapierf6d7f092009-11-03 20:07:35 +0000147 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
subrata_modakd757ea12008-12-12 14:39:20 +0000148
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_modakd757ea12008-12-12 14:39:20 +0000163 if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
164 tst_resm(TPASS, "container init continued successfuly, "
Monson Shao45192242013-01-08 11:34:44 +0800165 "after handling signal -USR1");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800166 else
subrata_modakd757ea12008-12-12 14:39:20 +0000167 tst_resm(TFAIL, "c-init failed to continue after "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 "passing kill -USR1");
subrata_modakd757ea12008-12-12 14:39:20 +0000169 cleanup();
Garrett Cooper2c282152010-12-16 00:55:50 -0800170 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700171}