1 /*
2 * Copyright (c) Bull S.A.S. 2008
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: pidns30.c
17 *
18 *   Description:
19 *    This testcase checks if the si_pid is correctly set when a process
20 *    that has registered for notification on a posix mqueue is in a
21 *    descendant namespace wrt the process that sends a message to that posix
22 *    mqueue.
23 *
24 *   Test Assertion & Strategy:
25 *    Parent                                   Child
26 *    --------------------------------------------------------------------------
27 *    Create a POSIX mqueue.
28 *    Create a PID namespace container.
29 *                                             Open that mqueue for reading
30 *                                             Register for notification when a
31 *                                                message arrives in that mqueue
32 *                                             Install a handler for SIGUSR1.
33 *    Write something to the mqueue.
34 *                                             Inside the handler, check that
35 *                                                si_pid is set to 0
36 *
37 *   Usage: <for command-line>
38 *    pidns30
39 *
40 *   History:
41 *    DATE      NAME                             DESCRIPTION
42 *    01/12/08  Nadia Derbey               Creation of this test.
43 *              <Nadia.Derbey@bull.net>
44 *
45 ******************************************************************************/
46 #define _GNU_SOURCE 1
47 #include <sys/wait.h>
48 #include <sys/types.h>
49 #include <signal.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <stdio.h>
53 #include <mqueue.h>
54 #include "lapi/syscalls.h"
55 #include "pidns_helper.h"
56 #include "test.h"
57 
58 char *TCID = "pidns30";
59 int TST_TOTAL = 1;
60 
61 char *mqname = "mq1";
62 int result = TFAIL;
63 
64 int father_to_child[2];
65 int child_to_father[2];
66 
67 #define CHILD_PID       1
68 #define PARENT_PID      0
69 
70 #define MSG      "HOW ARE YOU"
71 #define MSG_PRIO 1
72 
73 #define NO_STEP	-1
74 #define F_STEP_0 0x00
75 #define F_STEP_1 0x01
76 #define F_STEP_2 0x02
77 #define F_STEP_3 0x03
78 #define C_STEP_0 0x10
79 #define C_STEP_1 0x11
80 #define C_STEP_2 0x12
81 
82 mqd_t rc = -1;
83 mqd_t mqd = -1;
84 
remove_pipe(int * fd)85 static void remove_pipe(int *fd)
86 {
87 	close(fd[0]);
88 	close(fd[1]);
89 }
90 
remove_mqueue(mqd_t mqd)91 static void remove_mqueue(mqd_t mqd)
92 {
93 	mq_close(mqd);
94 	ltp_syscall(__NR_mq_unlink, mqname);
95 }
96 
cleanup(void)97 static void cleanup(void)
98 {
99 	if (mqd != -1) {
100 		remove_mqueue(mqd);
101 	}
102 	if (rc != -1) {
103 		remove_mqueue(rc);
104 	}
105 	remove_pipe(father_to_child);
106 	remove_pipe(child_to_father);
107 }
108 
cleanup_child(void)109 static void cleanup_child(void)
110 {
111 	if (mqd != -1) {
112 		ltp_syscall(__NR_mq_notify, mqd, NULL);
113 	}
114 	cleanup();
115 }
116 
117 /*
118  * child_signal_handler() - to handle SIGUSR1
119  *
120  * XXX (garrcoop): add calls to cleanup_child() -- or should this be handled
121  * from the libltp signal handler?
122  */
child_signal_handler(int sig,siginfo_t * si,void * unused)123 static void child_signal_handler(int sig, siginfo_t * si, void *unused)
124 {
125 	char buf[256];
126 	struct mq_attr attr;
127 
128 	if (si->si_signo != SIGUSR1) {
129 		printf("received signal = %d unexpectedly\n", si->si_signo);
130 		return;
131 	}
132 
133 	if (si->si_code != SI_MESGQ) {
134 		printf("expected signal code SI_MESGQ; got %d instead\n",
135 		       si->si_code);
136 		return;
137 	}
138 
139 	if (si->si_pid) {
140 		printf("expected signal originator PID = 0; got %d instead\n",
141 		       si->si_pid);
142 		return;
143 	} else {
144 		printf("signal originator PID = 0\n");
145 		result = TPASS;
146 	}
147 
148 	/*
149 	 * Now read the message - Be silent on errors since this is not the
150 	 * test purpose.
151 	 */
152 	rc = mq_getattr(si->si_int, &attr);
153 	if (rc != -1)
154 		mq_receive(si->si_int, buf, attr.mq_msgsize, NULL);
155 }
156 
157 /*
158  * child_fn() - Inside container
159  *
160  * XXX (garrcoop): add more calls to cleanup_child()?
161  */
child_fn(void * arg)162 int child_fn(void *arg)
163 {
164 	pid_t pid, ppid;
165 	struct sigaction sa;
166 	struct sigevent notif;
167 	char buf[5];
168 
169 	/* Set process id and parent pid */
170 	pid = getpid();
171 	ppid = getppid();
172 
173 	if (pid != CHILD_PID || ppid != PARENT_PID) {
174 		printf("pidns was not created\n");
175 		return 1;
176 	}
177 
178 	/* Close the appropriate end of each pipe */
179 	close(child_to_father[0]);
180 	close(father_to_child[1]);
181 
182 	while (read(father_to_child[0], buf, 1) != 1)
183 		sleep(1);
184 
185 	mqd = ltp_syscall(__NR_mq_open, mqname, O_RDONLY, 0, NULL);
186 	if (mqd == -1) {
187 		perror("mq_open failed");
188 		return 1;
189 	} else
190 		printf("mq_open succeeded\n");
191 
192 	/* Register for notification on message arrival */
193 	notif.sigev_notify = SIGEV_SIGNAL;
194 	notif.sigev_signo = SIGUSR1;
195 	notif.sigev_value.sival_int = mqd;
196 	if (ltp_syscall(__NR_mq_notify, mqd, &notif) == -1) {
197 		perror("mq_notify failed");
198 		return 1;
199 	} else
200 		printf("successfully registered for notification\n");
201 
202 	/* Define handler for SIGUSR1 */
203 	sa.sa_flags = SA_SIGINFO;
204 	sigemptyset(&sa.sa_mask);
205 	sa.sa_sigaction = child_signal_handler;
206 	if (sigaction(SIGUSR1, &sa, NULL) == -1) {
207 		perror("sigaction failed");
208 		return 1;
209 	} else
210 		printf("successfully registered handler for SIGUSR1\n");
211 
212 	/* Ask parent to send a message to the mqueue */
213 	if (write(child_to_father[1], "c:ok", 5) != 5) {
214 		perror("write failed");
215 		return 1;
216 	}
217 
218 	sleep(3);
219 
220 	/* Has parent sent a message? */
221 	read(father_to_child[0], buf, 5);
222 	if (strcmp(buf, "f:ok") != 0) {
223 		printf("parent did not send the message!\n");
224 		return 1;
225 	}
226 	printf("parent is done - cleaning up\n");
227 
228 	cleanup_child();
229 
230 	exit(0);
231 }
232 
setup(void)233 static void setup(void)
234 {
235 	tst_require_root();
236 	check_newpid();
237 }
238 
main(int argc,char * argv[])239 int main(int argc, char *argv[])
240 {
241 	int status;
242 	char buf[5];
243 	pid_t cpid;
244 
245 	setup();
246 
247 	if (pipe(child_to_father) == -1 || pipe(father_to_child) == -1) {
248 		tst_brkm(TBROK | TERRNO, cleanup, "pipe failed");
249 	}
250 
251 	ltp_syscall(__NR_mq_unlink, mqname);
252 
253 	/* container creation on PID namespace */
254 	cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
255 	if (cpid == -1)
256 		tst_brkm(TBROK | TERRNO, cleanup, "clone failed");
257 
258 	mqd =
259 	    ltp_syscall(__NR_mq_open, mqname, O_RDWR | O_CREAT | O_EXCL, 0777,
260 		    NULL);
261 	if (mqd == -1)
262 		tst_brkm(TBROK | TERRNO, cleanup, "mq_open failed");
263 	else
264 		tst_resm(TINFO, "successfully created posix mqueue");
265 
266 	if (write(father_to_child[1], buf, 1) != 1)
267 		tst_brkm(TBROK | TERRNO, cleanup, "write failed");
268 
269 	/* Close the appropriate end of each pipe */
270 	close(child_to_father[1]);
271 	close(father_to_child[0]);
272 
273 	/* Is container ready */
274 	read(child_to_father[0], buf, 5);
275 	if (strcmp(buf, "c:ok") != 0)
276 		tst_brkm(TBROK, cleanup,
277 			 "container did not respond as expected!");
278 
279 	rc = mq_send(mqd, MSG, strlen(MSG), MSG_PRIO);
280 	if (rc == -1)
281 		tst_brkm(TBROK | TERRNO, cleanup, "mq_send failed");
282 	else
283 		tst_resm(TINFO, "mq_send succeeded");
284 
285 	/* Tell the child the message has been sent */
286 	if (write(father_to_child[1], "f:ok", 5) != 5)
287 		tst_brkm(TBROK | TERRNO, cleanup, "write failed");
288 
289 	/* Wait for child to finish */
290 	if (wait(&status) == -1)
291 		tst_resm(TBROK | TERRNO, "wait failed");
292 
293 	cleanup();
294 
295 	tst_exit();
296 }
297