blob: ddcb14cae1d95b685b6aa5206a42268477f11955 [file] [log] [blame]
subrata_modak3a14aff2008-12-12 13:17:07 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080013* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modak3a14aff2008-12-12 13:17:07 +000014*
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******************************************************************************/
yaberauneyabe3fdcf2009-11-14 17:52:51 +000046#ifndef _GNU_SOURCE
47#define _GNU_SOURCE
48#endif
subrata_modak3a14aff2008-12-12 13:17:07 +000049#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>
yaberauneyabe3fdcf2009-11-14 17:52:51 +000056#include "test.h"
yaberauneya3e8f77b2009-11-14 23:58:26 +000057#include "linux_syscall_numbers.h"
yaberauneyabe3fdcf2009-11-14 17:52:51 +000058#include "libclone.h"
Jan Stanceked991ae2014-07-29 10:50:27 +020059#include "pidns_helper.h"
subrata_modak3a14aff2008-12-12 13:17:07 +000060
61char *TCID = "pidns31";
62int TST_TOTAL = 1;
63
Garrett Coopercb2dde72010-11-11 00:38:43 -080064char *mqname = "mq1";
subrata_modak3a14aff2008-12-12 13:17:07 +000065int result = TFAIL;
66
67int errno;
68int 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
84struct notify_info {
85 mqd_t mqd;
86 pid_t pid;
87};
88
89static void remove_pipe(int *fd)
90{
91 close(fd[0]);
92 close(fd[1]);
93}
94
95static void remove_mqueue(mqd_t mqd)
96{
yaberauneya3e8f77b2009-11-14 23:58:26 +000097 mq_close(mqd);
Jan Stancek359980f2013-02-15 10:16:05 +010098 ltp_syscall(__NR_mq_unlink, mqname);
subrata_modak3a14aff2008-12-12 13:17:07 +000099}
100
101/*
102 * steps F_STEP_XX : called from main
103 * steps C_STEP_XX : called from child_fn
104 */
105static 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:
yaberauneya3e8f77b2009-11-14 23:58:26 +0000112 mq_close(mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000113 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:
Jan Stancek359980f2013-02-15 10:16:05 +0100121 ltp_syscall(__NR_mq_notify, mqd, NULL);
subrata_modak3a14aff2008-12-12 13:17:07 +0000122 /* 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/*
yaberauneya3e8f77b2009-11-14 23:58:26 +0000136 * cleanup_mqueue() - performs all ONE TIME cleanup for this test at
subrata_modak3a14aff2008-12-12 13:17:07 +0000137 * completion or premature exit.
138 * step == -1 means no local resource to remove.
139 */
yaberauneya3e8f77b2009-11-14 23:58:26 +0000140void cleanup_mqueue(int result, int step, mqd_t mqd)
subrata_modak3a14aff2008-12-12 13:17:07 +0000141{
142 if (step != NO_STEP)
143 cleanup_resources(step, mqd);
144
subrata_modak88c166c2009-06-09 16:01:20 +0000145 tst_exit();
subrata_modak3a14aff2008-12-12 13:17:07 +0000146}
147
148/*
149 * child_fn() - Inside container
150 */
151int 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");
yaberauneya3e8f77b2009-11-14 23:58:26 +0000163 cleanup_mqueue(TBROK, NO_STEP, 0);
subrata_modak3a14aff2008-12-12 13:17:07 +0000164 }
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!");
yaberauneya3e8f77b2009-11-14 23:58:26 +0000173 cleanup_mqueue(TBROK, NO_STEP, 0);
subrata_modak3a14aff2008-12-12 13:17:07 +0000174 }
175 tst_resm(TINFO, "cinit: my father is ready to receive a message");
176
Jan Stancek359980f2013-02-15 10:16:05 +0100177 mqd = ltp_syscall(__NR_mq_open, mqname, O_WRONLY, 0, NULL);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800178 if (mqd == (mqd_t) - 1) {
subrata_modak3a14aff2008-12-12 13:17:07 +0000179 tst_resm(TBROK, "cinit: mq_open() failed (%s)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000181 cleanup_mqueue(TBROK, NO_STEP, 0);
subrata_modak3a14aff2008-12-12 13:17:07 +0000182 }
183 tst_resm(TINFO, "cinit: mq_open succeeded");
184
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185 if (mq_send(mqd, MSG, strlen(MSG), MSG_PRIO) == (mqd_t) - 1) {
subrata_modak3a14aff2008-12-12 13:17:07 +0000186 tst_resm(TBROK, "cinit: mq_send() failed (%s)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000188 cleanup_mqueue(TBROK, C_STEP_0, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000189 }
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 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200static void father_signal_handler(int sig, siginfo_t * si, void *unused)
subrata_modak3a14aff2008-12-12 13:17:07 +0000201{
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",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 strsignal(si->si_signo));
subrata_modak3a14aff2008-12-12 13:17:07 +0000209 return;
210 }
211
212 if (si->si_code != SI_MESGQ) {
213 tst_resm(TBROK, "father: expected signal code SI_MESGQ - "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 "Got %d", si->si_code);
subrata_modak3a14aff2008-12-12 13:17:07 +0000215 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,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800227 "father: expected signal originator PID = %d - Got %d",
228 info->pid, si->si_pid);
subrata_modak3a14aff2008-12-12 13:17:07 +0000229 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 */
yaberauneya3e8f77b2009-11-14 23:58:26 +0000239 if (!mq_getattr(info->mqd, &attr))
240 mq_receive(info->mqd, buf, attr.mq_msgsize, NULL);
subrata_modak3a14aff2008-12-12 13:17:07 +0000241}
242
Jan Stanceked991ae2014-07-29 10:50:27 +0200243static void setup(void)
244{
245 tst_require_root(NULL);
246 check_newpid();
247}
248
subrata_modak3a14aff2008-12-12 13:17:07 +0000249/***********************************************************************
250* M A I N
251***********************************************************************/
252
253int main(int argc, char *argv[])
254{
subrata_modak3a14aff2008-12-12 13:17:07 +0000255 pid_t cpid;
256 mqd_t mqd;
257 struct sigevent notif;
258 struct sigaction sa;
259 int status;
260 struct notify_info info;
261
Jan Stanceked991ae2014-07-29 10:50:27 +0200262 setup();
263
subrata_modak3a14aff2008-12-12 13:17:07 +0000264 if (pipe(father_to_child) == -1) {
265 tst_resm(TBROK, "parent: pipe() failed. aborting!");
yaberauneya3e8f77b2009-11-14 23:58:26 +0000266 cleanup_mqueue(TBROK, NO_STEP, 0);
subrata_modak3a14aff2008-12-12 13:17:07 +0000267 }
268
Jan Stancek359980f2013-02-15 10:16:05 +0100269 ltp_syscall(__NR_mq_unlink, mqname);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800270 mqd =
Jan Stancek359980f2013-02-15 10:16:05 +0100271 ltp_syscall(__NR_mq_open, mqname, O_RDWR | O_CREAT | O_EXCL, 0777,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800272 NULL);
273 if (mqd == (mqd_t) - 1) {
subrata_modak3a14aff2008-12-12 13:17:07 +0000274 tst_resm(TBROK, "parent: mq_open() failed (%s)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800275 strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000276 cleanup_mqueue(TBROK, F_STEP_0, 0);
subrata_modak3a14aff2008-12-12 13:17:07 +0000277 }
278 tst_resm(TINFO, "parent: successfully created posix mqueue");
279
280 /* container creation on PID namespace */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 cpid = ltp_clone_quick(CLONE_NEWPID | SIGCHLD, child_fn, NULL);
subrata_modak3a14aff2008-12-12 13:17:07 +0000282 if (cpid < 0) {
283 tst_resm(TBROK, "parent: clone() failed(%s)", strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000284 cleanup_mqueue(TBROK, F_STEP_1, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000285 }
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;
Jan Stancek359980f2013-02-15 10:16:05 +0100294 if (ltp_syscall(__NR_mq_notify, mqd, &notif) == (mqd_t) -1) {
subrata_modak3a14aff2008-12-12 13:17:07 +0000295 tst_resm(TBROK, "parent: mq_notify() failed (%s)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800296 strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000297 cleanup_mqueue(TBROK, F_STEP_1, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000298 }
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)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800307 strerror(errno));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000308 cleanup_mqueue(TBROK, F_STEP_2, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000309 }
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));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000318 cleanup_mqueue(TBROK, F_STEP_2, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000319 }
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));
yaberauneya3e8f77b2009-11-14 23:58:26 +0000326 cleanup_mqueue(TBROK, F_STEP_1, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000327 }
328
yaberauneya3e8f77b2009-11-14 23:58:26 +0000329 cleanup_mqueue(result, F_STEP_3, mqd);
subrata_modak3a14aff2008-12-12 13:17:07 +0000330
Garrett Cooper2c282152010-12-16 00:55:50 -0800331 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700332}