blob: 8fab439616ab8eeef854a54f8025b818aa3358ec [file] [log] [blame]
subrata_modakb264caf2009-03-04 07:05:49 +00001/*
2* Copyright (c) International Business Machines Corp., 2009
3* Copyright (c) Nadia Derbey, 2009
4* This program is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License as published by
6* the Free Software Foundation; either version 2 of the License, or
7* (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12* the GNU General Public License for more details.
13* You should have received a copy of the GNU General Public License
14* along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080015* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modakb264caf2009-03-04 07:05:49 +000016*
17* Author: Nadia Derbey <Nadia.Derbey@bull.net>
18*
19* Check mqns isolation: child mqns cannot be accessed from father
20*
21* Mount mqueue fs
22* unshare
23* In unshared process:
24* Mount newinstance mqueuefs
25* Create a posix mq -->mq1
26* Check that mq1 is not readable from father
27*
28* Changelog:
Monson Shao45192242013-01-08 11:34:44 +080029* Dec 16: accomodate new mqns semantics (Serge Hallyn)
subrata_modakb264caf2009-03-04 07:05:49 +000030
31***************************************************************************/
32
yaberauneyabe3fdcf2009-11-14 17:52:51 +000033#ifndef _GNU_SOURCE
34#define _GNU_SOURCE
35#endif
subrata_modakb264caf2009-03-04 07:05:49 +000036#include <sys/wait.h>
Garrett Cooper355093c2010-11-28 22:23:29 -080037#include <errno.h>
subrata_modakb264caf2009-03-04 07:05:49 +000038#include <stdio.h>
39#include <stdlib.h>
subrata_modakb264caf2009-03-04 07:05:49 +000040#include <string.h>
Garrett Cooper355093c2010-11-28 22:23:29 -080041#include <unistd.h>
subrata_modakb264caf2009-03-04 07:05:49 +000042#include "mqns.h"
43
44char *TCID = "posixmq_namespace_02";
Wanlong Gao354ebb42012-12-07 10:10:04 +080045int TST_TOTAL = 1;
subrata_modakb264caf2009-03-04 07:05:49 +000046
47int p1[2];
48int p2[2];
49
50int check_mqueue(void *vtest)
51{
52 char buf[30];
53 mqd_t mqd;
54
55 close(p1[1]);
56 close(p2[0]);
57
Garrett Cooperad14e902010-12-16 10:03:44 -080058 if (read(p1[0], buf, 3) < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -080059 perror("read(p1[0], ..) failed");
60 exit(1);
Garrett Cooperad14e902010-12-16 10:03:44 -080061 } else {
subrata_modakb264caf2009-03-04 07:05:49 +000062
Wanlong Gao354ebb42012-12-07 10:10:04 +080063 mqd =
Jan Stancek359980f2013-02-15 10:16:05 +010064 ltp_syscall(__NR_mq_open, NOSLASH_MQ1,
Wanlong Gao354ebb42012-12-07 10:10:04 +080065 O_RDWR | O_CREAT | O_EXCL, 0777, NULL);
yaberauneyaf2478672009-11-12 20:44:46 +000066 if (mqd == -1) {
67 if (write(p2[1], "mqfail", strlen("mqfail") + 1) < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -080068 perror("write(p2[1], \"mqfail\", ..) failed");
69 exit(1);
yaberauneyaf2478672009-11-12 20:44:46 +000070 }
yaberauneyaf2478672009-11-12 20:44:46 +000071 } else {
72
Garrett Cooper355093c2010-11-28 22:23:29 -080073 if (write(p2[1], "mqopen", strlen("mqopen") + 1) < 0) {
74 perror("write(p2[1], \"mqopen\", ..) failed");
75 exit(1);
76 } else {
yaberauneyaf2478672009-11-12 20:44:46 +000077
Garrett Cooper355093c2010-11-28 22:23:29 -080078 if (read(p1[0], buf, 5) < 0) {
79 perror("read(p1[0], ..) failed");
80 exit(1);
81 } else {
yaberauneyaf2478672009-11-12 20:44:46 +000082
83 /* destroy the mqueue */
yaberauneya3e8f77b2009-11-14 23:58:26 +000084 if (mq_close(mqd) < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -080085 perror("mq_close(mqd) failed");
86 exit(1);
Jan Stancek359980f2013-02-15 10:16:05 +010087 } else if (ltp_syscall(__NR_mq_unlink,
Wanlong Gao354ebb42012-12-07 10:10:04 +080088 NOSLASH_MQ1) < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -080089 perror("mq_unlink(" NOSLASH_MQ1
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 ") failed");
Garrett Cooper355093c2010-11-28 22:23:29 -080091 exit(1);
yaberauneyabe3fdcf2009-11-14 17:52:51 +000092 } else if (write(p2[1], "done",
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 strlen("done") + 1)
94 < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -080095 perror("write(p2[1], "
Wanlong Gao354ebb42012-12-07 10:10:04 +080096 "\"done\", ..) failed");
Garrett Cooper355093c2010-11-28 22:23:29 -080097 exit(1);
yaberauneyaf2478672009-11-12 20:44:46 +000098 }
99
100 }
101
102 }
103
104 }
105
subrata_modakb264caf2009-03-04 07:05:49 +0000106 }
Garrett Cooper355093c2010-11-28 22:23:29 -0800107 exit(0);
yaberauneyaf2478672009-11-12 20:44:46 +0000108
subrata_modakb264caf2009-03-04 07:05:49 +0000109}
110
subrata_modakb264caf2009-03-04 07:05:49 +0000111int main(int argc, char *argv[])
112{
113 int r;
114 mqd_t mqd;
115 char buf[30];
116 int use_clone = T_UNSHARE;
117
118 if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +0800120 "Testing posix mq namespaces through clone(2).");
subrata_modakb264caf2009-03-04 07:05:49 +0000121 use_clone = T_CLONE;
122 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +0800124 "Testing posix mq namespaces through unshare(2).");
subrata_modakb264caf2009-03-04 07:05:49 +0000125
Garrett Cooper355093c2010-11-28 22:23:29 -0800126 if (pipe(p1) == -1 || pipe(p2) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_brkm(TBROK | TERRNO, NULL, "pipe");
Garrett Cooper355093c2010-11-28 22:23:29 -0800128 }
subrata_modakb264caf2009-03-04 07:05:49 +0000129
130 /* fire off the test */
131 r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
132 if (r < 0) {
Monson Shao45192242013-01-08 11:34:44 +0800133 tst_brkm(TFAIL, NULL, "failed clone/unshare");
subrata_modakb264caf2009-03-04 07:05:49 +0000134 }
135
Monson Shao45192242013-01-08 11:34:44 +0800136 tst_resm(TINFO, "Checking namespaces isolation (child to parent)");
subrata_modakb264caf2009-03-04 07:05:49 +0000137
138 close(p1[0]);
139 close(p2[1]);
yaberauneyaf2478672009-11-12 20:44:46 +0000140 if (write(p1[1], "go", strlen("go") + 1) < 0) {
Garrett Cooper355093c2010-11-28 22:23:29 -0800141 tst_brkm(TBROK, NULL, "write(p1[1], \"go\", ..) failed");
yaberauneyaf2478672009-11-12 20:44:46 +0000142 }
subrata_modakb264caf2009-03-04 07:05:49 +0000143
yaberauneyaf2478672009-11-12 20:44:46 +0000144 if (read(p2[0], buf, 7) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_resm(TBROK | TERRNO, "read(p2[0], ..) failed");
yaberauneyaf2478672009-11-12 20:44:46 +0000146 } else if (!strcmp(buf, "mqfail")) {
Monson Shao45192242013-01-08 11:34:44 +0800147 tst_resm(TFAIL, "child process could not create mqueue");
subrata_modakb264caf2009-03-04 07:05:49 +0000148 umount(DEV_MQUEUE);
subrata_modakb264caf2009-03-04 07:05:49 +0000149 } else if (strcmp(buf, "mqopen")) {
Monson Shao45192242013-01-08 11:34:44 +0800150 tst_resm(TFAIL, "child process could not create mqueue");
subrata_modakb264caf2009-03-04 07:05:49 +0000151 umount(DEV_MQUEUE);
subrata_modakb264caf2009-03-04 07:05:49 +0000152 } else {
Jan Stancek359980f2013-02-15 10:16:05 +0100153 mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDONLY);
yaberauneyaf2478672009-11-12 20:44:46 +0000154 if (mqd == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 tst_resm(TPASS,
Monson Shao45192242013-01-08 11:34:44 +0800156 "Parent process can't see the mqueue");
yaberauneyaf2478672009-11-12 20:44:46 +0000157 } else {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 tst_resm(TFAIL | TERRNO,
Monson Shao45192242013-01-08 11:34:44 +0800159 "Parent process found mqueue");
yaberauneya3e8f77b2009-11-14 23:58:26 +0000160 mq_close(mqd);
yaberauneyaf2478672009-11-12 20:44:46 +0000161 }
Rishikesh K Rajak742cfad2010-04-29 18:56:12 +0530162 if (write(p1[1], "cont", 5) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800163 tst_resm(TBROK | TERRNO, "write(p1[1], ..) failed");
yaberauneyaf2478672009-11-12 20:44:46 +0000164 }
Garrett Cooper355093c2010-11-28 22:23:29 -0800165 read(p2[0], buf, 7);
yaberauneyaf2478672009-11-12 20:44:46 +0000166 }
subrata_modakb264caf2009-03-04 07:05:49 +0000167
subrata_modak88c166c2009-06-09 16:01:20 +0000168 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700169}