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: pidns31.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 an
21 * ancestor 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 * Register for notification when a
30 * message arrives in that mqueue
31 * Install a handler for SIGUSR1.
32 * Open that mqueue for writing
33 * Write something to the mqueue.
34 * Inside the handler, check that
35 * si_pid is set to the child's pid
36 *
37 * Usage: <for command-line>
38 * pidns31
39 *
40 * History:
41 * DATE NAME DESCRIPTION
42 * 04/12/08 Nadia Derbey Creation of this test.
43 * <Nadia.Derbey@bull.net>
44 *
45 ******************************************************************************/
46 #ifndef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #endif
49 #include <sys/wait.h>
50 #include <sys/types.h>
51 #include <signal.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <stdio.h>
55 #include <mqueue.h>
56 #include "test.h"
57 #include "linux_syscall_numbers.h"
58 #include "libclone.h"
59 #include "pidns_helper.h"
60
61 char *TCID = "pidns31";
62 int TST_TOTAL = 1;
63
64 char *mqname = "mq1";
65 int result = TFAIL;
66
67 int errno;
68 int father_to_child[2];
69
70 #define CHILD_PID 1
71 #define PARENT_PID 0
72
73 #define MSG "HOW ARE YOU"
74 #define MSG_PRIO 1
75
76 #define NO_STEP -1
77 #define F_STEP_0 0x00
78 #define F_STEP_1 0x01
79 #define F_STEP_2 0x02
80 #define F_STEP_3 0x03
81 #define C_STEP_0 0x10
82 #define C_STEP_1 0x11
83
84 struct notify_info {
85 mqd_t mqd;
86 pid_t pid;
87 };
88
remove_pipe(int * fd)89 static void remove_pipe(int *fd)
90 {
91 close(fd[0]);
92 close(fd[1]);
93 }
94
remove_mqueue(mqd_t mqd)95 static void remove_mqueue(mqd_t mqd)
96 {
97 mq_close(mqd);
98 ltp_syscall(__NR_mq_unlink, mqname);
99 }
100
101 /*
102 * steps F_STEP_XX : called from main
103 * steps C_STEP_XX : called from child_fn
104 */
cleanup_resources(int step,mqd_t mqd)105 static void cleanup_resources(int step, mqd_t mqd)
106 {
107 switch (step) {
108 case C_STEP_1:
109 close(father_to_child[0]);
110 /* fall through */
111 case C_STEP_0:
112 mq_close(mqd);
113 break;
114
115 case F_STEP_3:
116 remove_mqueue(mqd);
117 close(father_to_child[1]);
118 break;
119
120 case F_STEP_2:
121 ltp_syscall(__NR_mq_notify, mqd, NULL);
122 /* fall through */
123 case F_STEP_1:
124 remove_mqueue(mqd);
125 /* fall through */
126 case F_STEP_0:
127 remove_pipe(father_to_child);
128 break;
129 default:
130 tst_resm(TWARN, "Unknown code - no resource removed.");
131 break;
132 }
133 }
134
135 /*
136 * cleanup_mqueue() - performs all ONE TIME cleanup for this test at
137 * completion or premature exit.
138 * step == -1 means no local resource to remove.
139 */
cleanup_mqueue(int result,int step,mqd_t mqd)140 void cleanup_mqueue(int result, int step, mqd_t mqd)
141 {
142 if (step != NO_STEP)
143 cleanup_resources(step, mqd);
144
145 tst_exit();
146 }
147
148 /*
149 * child_fn() - Inside container
150 */
child_fn(void * arg)151 int child_fn(void *arg)
152 {
153 pid_t pid, ppid;
154 mqd_t mqd;
155 char buf[5];
156
157 /* Set process id and parent pid */
158 pid = getpid();
159 ppid = getppid();
160
161 if (pid != CHILD_PID || ppid != PARENT_PID) {
162 tst_resm(TBROK, "cinit: pidns is not created");
163 cleanup_mqueue(TBROK, NO_STEP, 0);
164 }
165
166 /* Close the appropriate end of pipe */
167 close(father_to_child[1]);
168
169 /* Is parent ready to receive a message? */
170 read(father_to_child[0], buf, 5);
171 if (strcmp(buf, "f:ok")) {
172 tst_resm(TBROK, "cinit: parent did not send the message!");
173 cleanup_mqueue(TBROK, NO_STEP, 0);
174 }
175 tst_resm(TINFO, "cinit: my father is ready to receive a message");
176
177 mqd = ltp_syscall(__NR_mq_open, mqname, O_WRONLY, 0, NULL);
178 if (mqd == (mqd_t) - 1) {
179 tst_resm(TBROK, "cinit: mq_open() failed (%s)",
180 strerror(errno));
181 cleanup_mqueue(TBROK, NO_STEP, 0);
182 }
183 tst_resm(TINFO, "cinit: mq_open succeeded");
184
185 if (mq_send(mqd, MSG, strlen(MSG), MSG_PRIO) == (mqd_t) - 1) {
186 tst_resm(TBROK, "cinit: mq_send() failed (%s)",
187 strerror(errno));
188 cleanup_mqueue(TBROK, C_STEP_0, mqd);
189 }
190 tst_resm(TINFO, "cinit: mq_send() succeeded");
191
192 /* Cleanup and exit */
193 cleanup_resources(C_STEP_1, mqd);
194 exit(0);
195 }
196
197 /*
198 * father_signal_handler()
199 */
father_signal_handler(int sig,siginfo_t * si,void * unused)200 static void father_signal_handler(int sig, siginfo_t * si, void *unused)
201 {
202 char buf[256];
203 struct mq_attr attr;
204 struct notify_info *info;
205
206 if (si->si_signo != SIGUSR1) {
207 tst_resm(TBROK, "father: received %s unexpectedly",
208 strsignal(si->si_signo));
209 return;
210 }
211
212 if (si->si_code != SI_MESGQ) {
213 tst_resm(TBROK, "father: expected signal code SI_MESGQ - "
214 "Got %d", si->si_code);
215 return;
216 }
217
218 if (!si->si_ptr) {
219 tst_resm(TBROK, "father: expected si_ptr - Got NULL");
220 return;
221 }
222
223 info = (struct notify_info *)si->si_ptr;
224
225 if (si->si_pid != info->pid) {
226 tst_resm(TFAIL,
227 "father: expected signal originator PID = %d - Got %d",
228 info->pid, si->si_pid);
229 return;
230 }
231
232 tst_resm(TPASS, "father: signal originator PID = %d", si->si_pid);
233 result = TPASS;
234
235 /*
236 * Now read the message - Be silent on errors since this is not the
237 * test purpose.
238 */
239 if (!mq_getattr(info->mqd, &attr))
240 mq_receive(info->mqd, buf, attr.mq_msgsize, NULL);
241 }
242
setup(void)243 static void setup(void)
244 {
245 tst_require_root();
246 check_newpid();
247 }
248
249 /***********************************************************************
250 * M A I N
251 ***********************************************************************/
252
main(int argc,char * argv[])253 int main(int argc, char *argv[])
254 {
255 pid_t cpid;
256 mqd_t mqd;
257 struct sigevent notif;
258 struct sigaction sa;
259 int status;
260 struct notify_info info;
261
262 setup();
263
264 if (pipe(father_to_child) == -1) {
265 tst_resm(TBROK, "parent: pipe() failed. aborting!");
266 cleanup_mqueue(TBROK, NO_STEP, 0);
267 }
268
269 ltp_syscall(__NR_mq_unlink, mqname);
270 mqd =
271 ltp_syscall(__NR_mq_open, mqname, O_RDWR | O_CREAT | O_EXCL, 0777,
272 NULL);
273 if (mqd == (mqd_t) - 1) {
274 tst_resm(TBROK, "parent: mq_open() failed (%s)",
275 strerror(errno));
276 cleanup_mqueue(TBROK, F_STEP_0, 0);
277 }
278 tst_resm(TINFO, "parent: successfully created posix mqueue");
279
280 /* container creation on PID namespace */
281 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
282 if (cpid < 0) {
283 tst_resm(TBROK, "parent: clone() failed(%s)", strerror(errno));
284 cleanup_mqueue(TBROK, F_STEP_1, mqd);
285 }
286 tst_resm(TINFO, "parent: successfully created child (pid = %d)", cpid);
287
288 /* Register for notification on message arrival */
289 notif.sigev_notify = SIGEV_SIGNAL;
290 notif.sigev_signo = SIGUSR1;
291 info.mqd = mqd;
292 info.pid = cpid;
293 notif.sigev_value.sival_ptr = &info;
294 if (ltp_syscall(__NR_mq_notify, mqd, ¬if) == (mqd_t) -1) {
295 tst_resm(TBROK, "parent: mq_notify() failed (%s)",
296 strerror(errno));
297 cleanup_mqueue(TBROK, F_STEP_1, mqd);
298 }
299 tst_resm(TINFO, "parent: successfully registered for notification");
300
301 /* Define handler for SIGUSR1 */
302 sa.sa_flags = SA_SIGINFO;
303 sigemptyset(&sa.sa_mask);
304 sa.sa_sigaction = father_signal_handler;
305 if (sigaction(SIGUSR1, &sa, NULL) == -1) {
306 tst_resm(TBROK, "parent: sigaction() failed(%s)",
307 strerror(errno));
308 cleanup_mqueue(TBROK, F_STEP_2, mqd);
309 }
310 tst_resm(TINFO, "parent: successfully registered handler for SIGUSR1");
311
312 /* Close the appropriate end of pipe */
313 close(father_to_child[0]);
314
315 /* Tell the child a message can be sent */
316 if (write(father_to_child[1], "f:ok", 5) != 5) {
317 tst_resm(TBROK, "parent: pipe is broken(%s)", strerror(errno));
318 cleanup_mqueue(TBROK, F_STEP_2, mqd);
319 }
320
321 sleep(3);
322
323 /* Wait for child to finish */
324 if (wait(&status) == -1) {
325 tst_resm(TBROK, "parent: wait() failed(%s)", strerror(errno));
326 cleanup_mqueue(TBROK, F_STEP_1, mqd);
327 }
328
329 cleanup_mqueue(result, F_STEP_3, mqd);
330
331 tst_exit();
332 }
333