blob: 80d25a6e6cee089d7b5574c0932595a8e277fb87 [file] [log] [blame]
subrata_modak717937a2009-03-04 07:09:15 +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_modak717937a2009-03-04 07:09:15 +000015*
16* Author: Serge Hallyn <serue@us.ibm.com>
17*
18* Check mqueuefs lifetime
19* . parent creates /dev/mqueue2
20* . child mounts mqueue there
21* . child does mq_open("/ab")
22* . parent checks for /dev/mqueue2
23* . child exits
24* . parent checks for /dev/mqueue2
25* . parent tries 'touch /dev/mqueue2/dd' -> should fail
26* . parent umounts /dev/mqueue2
27
28***************************************************************************/
29
yaberauneyabe3fdcf2009-11-14 17:52:51 +000030#ifndef _GNU_SOURCE
31#define _GNU_SOURCE
32#endif
Garrett Cooper2f5ede32010-12-18 08:53:36 -080033#include <sys/types.h>
34#include <sys/stat.h>
subrata_modak717937a2009-03-04 07:09:15 +000035#include <sys/wait.h>
36#include <assert.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <string.h>
41#include <errno.h>
42#include "mqns.h"
43
44char *TCID = "posixmq_namespace_04";
Wanlong Gao354ebb42012-12-07 10:10:04 +080045int TST_TOTAL = 1;
subrata_modak717937a2009-03-04 07:09:15 +000046
47int p1[2];
48int p2[2];
49
50#define FNAM1 DEV_MQUEUE2 SLASH_MQ1
51#define FNAM2 DEV_MQUEUE2 SLASH_MQ2
52
53int check_mqueue(void *vtest)
54{
55 char buf[30];
56 mqd_t mqd;
57 int rc;
58
59 close(p1[1]);
60 close(p2[0]);
61
Wanlong Gao354ebb42012-12-07 10:10:04 +080062 read(p1[0], buf, 3); /* go */
subrata_modak717937a2009-03-04 07:09:15 +000063
Jan Stancek359980f2013-02-15 10:16:05 +010064 mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL,
65 0755, NULL);
subrata_modak717937a2009-03-04 07:09:15 +000066 if (mqd == -1) {
67 write(p2[1], "mqfail", 7);
subrata_modak88c166c2009-06-09 16:01:20 +000068 tst_exit();
subrata_modak717937a2009-03-04 07:09:15 +000069 }
70
yaberauneya3e8f77b2009-11-14 23:58:26 +000071 mq_close(mqd);
subrata_modak717937a2009-03-04 07:09:15 +000072
73 rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL);
74 if (rc == -1) {
75 perror("mount");
76 write(p2[1], "mount", 6);
subrata_modak88c166c2009-06-09 16:01:20 +000077 tst_exit();
subrata_modak717937a2009-03-04 07:09:15 +000078 }
79
80 write(p2[1], "go", 3);
81 read(p1[0], buf, 3);
82
subrata_modak88c166c2009-06-09 16:01:20 +000083 tst_exit();
subrata_modak717937a2009-03-04 07:09:15 +000084}
85
subrata_modak717937a2009-03-04 07:09:15 +000086int main(int argc, char *argv[])
87{
subrata_modak717937a2009-03-04 07:09:15 +000088 int rc;
89 int status;
90 char buf[30];
91 struct stat statbuf;
92 int use_clone = T_UNSHARE;
93
94 if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +080096 "Testing posix mq namespaces through clone(2).");
subrata_modak717937a2009-03-04 07:09:15 +000097 use_clone = T_CLONE;
98 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +080099 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +0800100 "Testing posix mq namespaces through unshare(2).");
subrata_modak717937a2009-03-04 07:09:15 +0000101
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 if (pipe(p1) == -1) {
103 perror("pipe");
104 exit(EXIT_FAILURE);
105 }
106 if (pipe(p2) == -1) {
107 perror("pipe");
108 exit(EXIT_FAILURE);
109 }
subrata_modak717937a2009-03-04 07:09:15 +0000110
111 mkdir(DEV_MQUEUE2, 0755);
112
Monson Shao45192242013-01-08 11:34:44 +0800113 tst_resm(TINFO, "Checking mqueue filesystem lifetime");
subrata_modak717937a2009-03-04 07:09:15 +0000114
115 /* fire off the test */
116 rc = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
117 if (rc < 0) {
Monson Shao45192242013-01-08 11:34:44 +0800118 tst_resm(TFAIL, "failed clone/unshare");
subrata_modak717937a2009-03-04 07:09:15 +0000119 goto fail;
120 }
121
122 close(p1[0]);
123 close(p2[1]);
124 write(p1[1], "go", 3);
125
126 read(p2[0], buf, 7);
127 if (!strcmp(buf, "mqfail")) {
Monson Shao45192242013-01-08 11:34:44 +0800128 tst_resm(TFAIL, "child process could not create mqueue");
subrata_modak717937a2009-03-04 07:09:15 +0000129 goto fail;
130 } else if (!strcmp(buf, "mount")) {
Monson Shao45192242013-01-08 11:34:44 +0800131 tst_resm(TFAIL, "child process could not mount mqueue");
subrata_modak717937a2009-03-04 07:09:15 +0000132 goto fail;
133 }
134
135 rc = stat(FNAM1, &statbuf);
136 if (rc == -1) {
137 perror("stat");
138 write(p1[1], "go", 3);
Monson Shao45192242013-01-08 11:34:44 +0800139 tst_resm(TFAIL, "parent could not see child's created mq");
subrata_modak717937a2009-03-04 07:09:15 +0000140 goto fail;
141 }
142 write(p1[1], "go", 3);
143
144 rc = wait(&status);
145 if (rc == -1) {
146 perror("wait");
Monson Shao45192242013-01-08 11:34:44 +0800147 tst_resm(TFAIL, "error while parent waited on child to exit");
subrata_modak717937a2009-03-04 07:09:15 +0000148 goto fail;
149 }
150 if (!WIFEXITED(status)) {
Monson Shao45192242013-01-08 11:34:44 +0800151 tst_resm(TFAIL, "Child did not exit normally (status %d)",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 status);
subrata_modak717937a2009-03-04 07:09:15 +0000153 goto fail;
154 }
155 rc = stat(FNAM1, &statbuf);
156 if (rc == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800157 tst_resm(TFAIL,
Monson Shao45192242013-01-08 11:34:44 +0800158 "parent's view of child's mq died with child");
subrata_modak717937a2009-03-04 07:09:15 +0000159 goto fail;
160 }
161
162 rc = creat(FNAM2, 0755);
163 if (rc != -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800164 tst_resm(TFAIL,
Monson Shao45192242013-01-08 11:34:44 +0800165 "parent was able to create a file in dead child's mqfs");
subrata_modak717937a2009-03-04 07:09:15 +0000166 goto fail;
167 }
168
Monson Shao45192242013-01-08 11:34:44 +0800169 tst_resm(TPASS, "Child mqueue fs still visible for parent");
subrata_modak717937a2009-03-04 07:09:15 +0000170
subrata_modak717937a2009-03-04 07:09:15 +0000171fail:
172 umount(DEV_MQUEUE2);
173 rmdir(DEV_MQUEUE2);
subrata_modak717937a2009-03-04 07:09:15 +0000174
Garrett Cooper2c282152010-12-16 00:55:50 -0800175 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700176}