• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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
23 * *	- Verify whether the signal handler is called from the proper process.
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 *******************************************************************************/
40 #include "config.h"
41 
42 #define _GNU_SOURCE 1
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <sys/wait.h>
46 #include <sys/types.h>
47 #include <signal.h>
48 #include <unistd.h>
49 #include "pidns_helper.h"
50 #include "test.h"
51 
52 #define CHILD_PID	1
53 #define PARENT_PID	0
54 
55 char *TCID = "pidns16";
56 int TST_TOTAL = 3;
57 
child_signal_handler(int sig,siginfo_t * si,void * unused)58 void child_signal_handler(int sig, siginfo_t * si, void *unused)
59 {
60 	static int c = 1;
61 	pid_t expected_pid;
62 
63 	/* Verifying from which process the signal handler is signalled */
64 
65 	switch (c) {
66 	case 1:
67 		expected_pid = PARENT_PID;
68 		break;
69 	case 2:
70 		expected_pid = CHILD_PID;
71 		break;
72 	default:
73 		tst_resm(TBROK, "child should NOT be signalled 3+ times");
74 		return;
75 	}
76 
77 	if (si->si_pid == expected_pid)
78 		tst_resm(TPASS, "child is signalled from expected pid %d",
79 			 expected_pid);
80 	else
81 		tst_resm(TFAIL, "child is signalled from unexpected pid %d,"
82 			 " expecting pid %d", si->si_pid, expected_pid);
83 
84 	c++;
85 }
86 
87 /*
88  * child_fn() - Inside container
89  */
child_fn(void * ttype)90 int child_fn(void *ttype)
91 {
92 	struct sigaction sa;
93 	pid_t pid, ppid;
94 
95 	/* Set process id and parent pid */
96 	pid = getpid();
97 	ppid = getppid();
98 
99 	if ((pid != CHILD_PID) || (ppid != PARENT_PID))
100 		tst_resm(TBROK, "pidns is not created.");
101 
102 	/* Set signal handler for SIGUSR1, also mask other signals */
103 	sa.sa_flags = SA_SIGINFO;
104 	sigemptyset(&sa.sa_mask);
105 	sa.sa_sigaction = child_signal_handler;
106 	if (sigaction(SIGUSR1, &sa, NULL) == -1)
107 		tst_resm(TBROK, "%d: sigaction() failed", pid);
108 
109 	pause();
110 	tst_resm(TINFO, "Container: Resumed after receiving SIGUSR1 "
111 		 "from parentNS ");
112 	if (kill(pid, SIGUSR1) != 0) {
113 		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
114 	}
115 	tst_resm(TINFO, "Container: Resumed after sending SIGUSR1 "
116 		 "from container itself");
117 	_exit(10);
118 }
119 
setup(void)120 static void setup(void)
121 {
122 	tst_require_root();
123 	check_newpid();
124 }
125 
126 /***********************************************************************
127 *   M A I N
128 ***********************************************************************/
main(int argc,char * argv[])129 int main(int argc, char *argv[])
130 {
131 	int status;
132 	pid_t cpid;
133 
134 	setup();
135 
136 	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
137 
138 	if (cpid < 0) {
139 		tst_resm(TBROK, "clone() failed.");
140 	}
141 
142 	sleep(1);
143 	if (kill(cpid, SIGUSR1) != 0) {
144 		tst_resm(TFAIL, "kill(SIGUSR1) fails.");
145 	}
146 	sleep(1);
147 	if (waitpid(cpid, &status, 0) < 0)
148 		tst_resm(TWARN, "waitpid() failed.");
149 
150 	if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 10))
151 		tst_resm(TPASS, "container init continued successfuly, "
152 			 "after handling signal -USR1");
153 	else
154 		tst_resm(TFAIL, "c-init failed to continue after "
155 			 "passing kill -USR1");
156 	tst_exit();
157 }
158