blob: f9b3cd4204ea1bd7ed91834f8b6d18a85a11ab99 [file] [log] [blame]
subrata_modaka83feac2009-03-04 07:08:02 +00001/*
2* Copyright (c) International Business Machines Corp., 2009
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*
8* This program is distributed in the hope that it will be useful,
9* but WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11* the GNU General Public License for more details.
12* You should have received a copy of the GNU General Public License
13* along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080014* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modaka83feac2009-03-04 07:08:02 +000015*
16* Author: Serge Hallyn <serue@us.ibm.com>
17*
18* Check ipcns+sb longevity
19*
20* Mount mqueue fs
21* unshare
22* In unshared process:
23* Create "/mq1" with mq_open()
24* Mount mqueuefs
25* Check that /mq1 exists
26* Create /dev/mqueue/mq2 through vfs (create(2))
27* Umount /dev/mqueue
28* Remount /dev/mqueue
29* Check that both /mq1 and /mq2 exist
30
31***************************************************************************/
32
yaberauneyabe3fdcf2009-11-14 17:52:51 +000033#ifndef _GNU_SOURCE
34#define _GNU_SOURCE
35#endif
Garrett Cooper2f5ede32010-12-18 08:53:36 -080036#include <sys/types.h>
37#include <sys/stat.h>
subrata_modaka83feac2009-03-04 07:08:02 +000038#include <sys/wait.h>
39#include <assert.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <string.h>
44#include <errno.h>
45#include "mqns.h"
46
47char *TCID = "posixmq_namespace_03";
Wanlong Gao354ebb42012-12-07 10:10:04 +080048int TST_TOTAL = 1;
subrata_modaka83feac2009-03-04 07:08:02 +000049
50int p1[2];
51int p2[2];
52
53#define FNAM1 DEV_MQUEUE2 SLASH_MQ1
54#define FNAM2 DEV_MQUEUE2 SLASH_MQ2
55
56int check_mqueue(void *vtest)
57{
58 char buf[30];
59 mqd_t mqd;
60 int rc;
61 struct stat statbuf;
62
63 close(p1[1]);
64 close(p2[0]);
65
Wanlong Gao354ebb42012-12-07 10:10:04 +080066 if (read(p1[0], buf, 3) != 3) { /* go */
Garrett Cooper355093c2010-11-28 22:23:29 -080067 perror("read failed");
68 exit(1);
69 }
subrata_modaka83feac2009-03-04 07:08:02 +000070
Jan Stancek359980f2013-02-15 10:16:05 +010071 mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL,
72 0755, NULL);
subrata_modaka83feac2009-03-04 07:08:02 +000073 if (mqd == -1) {
74 write(p2[1], "mqfail", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -080075 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +000076 }
77
yaberauneya3e8f77b2009-11-14 23:58:26 +000078 mq_close(mqd);
subrata_modaka83feac2009-03-04 07:08:02 +000079
80 rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL);
81 if (rc == -1) {
subrata_modaka83feac2009-03-04 07:08:02 +000082 write(p2[1], "mount1", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -080083 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +000084 }
85
86 rc = stat(FNAM1, &statbuf);
87 if (rc == -1) {
88 write(p2[1], "stat1", 6);
Garrett Cooper355093c2010-11-28 22:23:29 -080089 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +000090 }
91
92 rc = creat(FNAM2, 0755);
93 if (rc == -1) {
94 write(p2[1], "creat", 6);
Garrett Cooper355093c2010-11-28 22:23:29 -080095 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +000096 }
97
98 close(rc);
99
100 rc = umount(DEV_MQUEUE2);
101 if (rc == -1) {
subrata_modaka83feac2009-03-04 07:08:02 +0000102 write(p2[1], "umount", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -0800103 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +0000104 }
105
106 rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL);
107 if (rc == -1) {
108 write(p2[1], "mount2", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -0800109 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +0000110 }
111
112 rc = stat(FNAM1, &statbuf);
113 if (rc == -1) {
114 write(p2[1], "stat2", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -0800115 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +0000116 }
117
118 rc = stat(FNAM2, &statbuf);
119 if (rc == -1) {
120 write(p2[1], "stat3", 7);
Garrett Cooper355093c2010-11-28 22:23:29 -0800121 exit(1);
subrata_modaka83feac2009-03-04 07:08:02 +0000122 }
123
124 write(p2[1], "done", 5);
125
Garrett Cooper355093c2010-11-28 22:23:29 -0800126 exit(0);
subrata_modaka83feac2009-03-04 07:08:02 +0000127}
128
subrata_modaka83feac2009-03-04 07:08:02 +0000129int main(int argc, char *argv[])
130{
131 int r;
132 char buf[30];
133 int use_clone = T_UNSHARE;
134
135 if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800136 tst_resm(TINFO, "Testing posix mq namespaces through clone(2)");
subrata_modaka83feac2009-03-04 07:08:02 +0000137 use_clone = T_CLONE;
138 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TINFO,
140 "Testing posix mq namespaces through unshare(2)");
subrata_modaka83feac2009-03-04 07:08:02 +0000141
Garrett Cooper2f5ede32010-12-18 08:53:36 -0800142 if (pipe(p1) == -1 || pipe(p2) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 tst_brkm(TBROK | TERRNO, NULL, "pipe failed");
subrata_modaka83feac2009-03-04 07:08:02 +0000144
145 /* fire off the test */
146 r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
147 if (r < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800148 tst_brkm(TBROK | TERRNO, NULL, "failed clone/unshare");
subrata_modaka83feac2009-03-04 07:08:02 +0000149 }
150
Garrett Cooper355093c2010-11-28 22:23:29 -0800151 tst_resm(TINFO, "Checking correct umount+remount of mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000152
153 mkdir(DEV_MQUEUE2, 0755);
154
155 close(p1[0]);
subrata_modaka83feac2009-03-04 07:08:02 +0000156 close(p2[1]);
157 write(p1[1], "go", 3);
158
159 read(p2[0], buf, 7);
160 r = TFAIL;
161 if (!strcmp(buf, "mqfail")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800162 tst_resm(TFAIL, "child process could not create mqueue");
subrata_modaka83feac2009-03-04 07:08:02 +0000163 goto fail;
164 } else if (!strcmp(buf, "mount1")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800165 tst_resm(TFAIL, "child process could not mount mqueue");
subrata_modaka83feac2009-03-04 07:08:02 +0000166 goto fail;
167 } else if (!strcmp(buf, "stat1x")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800168 tst_resm(TFAIL, "mq created by child is not in mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000169 goto fail;
170 } else if (!strcmp(buf, "creat")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800171 tst_resm(TFAIL, "child couldn't creat mq through mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000172 goto fail;
173 } else if (!strcmp(buf, "umount")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800174 tst_resm(TFAIL, "child couldn't umount mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000175 goto fail;
176 } else if (!strcmp(buf, "mount2")) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800177 tst_resm(TFAIL, "child couldn't remount mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000178 goto fail;
179 } else if (!strcmp(buf, "stat2")) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_resm(TFAIL,
181 "mq_open()d file gone after remount of mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000182 goto fail;
183 } else if (!strcmp(buf, "stat3")) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 tst_resm(TFAIL,
185 "creat(2)'d file gone after remount of mqueuefs");
subrata_modaka83feac2009-03-04 07:08:02 +0000186 goto fail;
187 }
188
Garrett Cooper355093c2010-11-28 22:23:29 -0800189 tst_resm(TPASS, "umount+remount of mqueuefs remounted the right fs");
subrata_modaka83feac2009-03-04 07:08:02 +0000190
191 r = 0;
192fail:
193 umount(DEV_MQUEUE2);
194 rmdir(DEV_MQUEUE2);
subrata_modak88c166c2009-06-09 16:01:20 +0000195 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700196}