blob: a8d2748587547442a296e72e7d8de8ddf2d81a90 [file] [log] [blame]
subrata_modak09be6112009-03-04 07:03:41 +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_modak09be6112009-03-04 07:03:41 +000016*
17* Author: Nadia Derbey <Nadia.Derbey@bull.net>
18*
19* Check mqns isolation: father mqns cannot be accessed from newinstance
20*
21* Mount mqueue fs
22* Create a posix mq -->mq1
23* unshare
24* In unshared process:
25* Mount newinstance mqueuefs
26* Check that mq1 is not readable from new ns
27
28***************************************************************************/
29
yaberauneyab5f17ac2009-11-13 11:26:57 +000030#ifndef _GNU_SOURCE
yaberauneyabe3fdcf2009-11-14 17:52:51 +000031#define _GNU_SOURCE
yaberauneyab5f17ac2009-11-13 11:26:57 +000032#endif
subrata_modak09be6112009-03-04 07:03:41 +000033#include <sys/wait.h>
Garrett Cooper355093c2010-11-28 22:23:29 -080034#include <errno.h>
subrata_modak09be6112009-03-04 07:03:41 +000035#include <stdio.h>
36#include <stdlib.h>
subrata_modak09be6112009-03-04 07:03:41 +000037#include <string.h>
Garrett Cooper355093c2010-11-28 22:23:29 -080038#include <unistd.h>
subrata_modak09be6112009-03-04 07:03:41 +000039#include "mqns.h"
40
41char *TCID = "posixmq_namespace_01";
Wanlong Gao354ebb42012-12-07 10:10:04 +080042int TST_TOTAL = 1;
subrata_modak09be6112009-03-04 07:03:41 +000043
44int p1[2];
45int p2[2];
46
47int check_mqueue(void *vtest)
48{
49 char buf[30];
50 mqd_t mqd;
51
52 close(p1[1]);
53 close(p2[0]);
54
Garrett Cooper355093c2010-11-28 22:23:29 -080055 if (read(p1[0], buf, strlen("go") + 1) < 0) {
56 printf("read(p1[0], ...) failed: %s\n", strerror(errno));
57 exit(1);
58 }
Jan Stancek359980f2013-02-15 10:16:05 +010059 mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDONLY);
subrata_modak09be6112009-03-04 07:03:41 +000060 if (mqd == -1) {
Garrett Cooper355093c2010-11-28 22:23:29 -080061 if (write(p2[1], "notfnd", strlen("notfnd") + 1) < 0) {
62 perror("write(p2[1], ...) failed");
63 exit(1);
64 }
subrata_modak09be6112009-03-04 07:03:41 +000065 } else {
Garrett Cooper355093c2010-11-28 22:23:29 -080066 if (write(p2[1], "exists", strlen("exists") + 1) < 0) {
67 perror("write(p2[1], \"exists\", 7) failed");
68 exit(1);
69 } else if (mq_close(mqd) < 0) {
70 perror("mq_close(mqd) failed");
71 exit(1);
72 }
subrata_modak09be6112009-03-04 07:03:41 +000073 }
subrata_modak09be6112009-03-04 07:03:41 +000074
Garrett Cooper355093c2010-11-28 22:23:29 -080075 exit(0);
subrata_modak09be6112009-03-04 07:03:41 +000076}
77
Wanlong Gao354ebb42012-12-07 10:10:04 +080078int main(int argc, char *argv[])
subrata_modak09be6112009-03-04 07:03:41 +000079{
80 int r;
81 mqd_t mqd;
82 char buf[30];
83 int use_clone = T_UNSHARE;
84
85 if (argc == 2 && strcmp(argv[1], "-clone") == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080086 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +080087 "Testing posix mq namespaces through clone(2).");
subrata_modak09be6112009-03-04 07:03:41 +000088 use_clone = T_CLONE;
89 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 tst_resm(TINFO,
Monson Shao45192242013-01-08 11:34:44 +080091 "Testing posix mq namespaces through unshare(2).");
subrata_modak09be6112009-03-04 07:03:41 +000092
Garrett Cooper355093c2010-11-28 22:23:29 -080093 if (pipe(p1) == -1 || pipe(p2) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080094 tst_brkm(TBROK | TERRNO, NULL, "pipe failed");
Garrett Cooper355093c2010-11-28 22:23:29 -080095 }
subrata_modak09be6112009-03-04 07:03:41 +000096
Jan Stancek359980f2013-02-15 10:16:05 +010097 mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL,
98 0777, NULL);
subrata_modak09be6112009-03-04 07:03:41 +000099 if (mqd == -1) {
100 perror("mq_open");
Monson Shao45192242013-01-08 11:34:44 +0800101 tst_resm(TFAIL, "mq_open failed");
subrata_modak88c166c2009-06-09 16:01:20 +0000102 tst_exit();
subrata_modak09be6112009-03-04 07:03:41 +0000103 }
104
Monson Shao45192242013-01-08 11:34:44 +0800105 tst_resm(TINFO, "Checking namespaces isolation from parent to child");
subrata_modak09be6112009-03-04 07:03:41 +0000106 /* fire off the test */
107 r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL);
108 if (r < 0) {
Monson Shao45192242013-01-08 11:34:44 +0800109 tst_resm(TFAIL, "failed clone/unshare");
yaberauneya3e8f77b2009-11-14 23:58:26 +0000110 mq_close(mqd);
Jan Stancek359980f2013-02-15 10:16:05 +0100111 ltp_syscall(__NR_mq_unlink, NOSLASH_MQ1);
subrata_modak88c166c2009-06-09 16:01:20 +0000112 tst_exit();
subrata_modak09be6112009-03-04 07:03:41 +0000113 }
114
115 close(p1[0]);
116 close(p2[1]);
yaberauneyab5f17ac2009-11-13 11:26:57 +0000117 if (write(p1[1], "go", strlen("go") + 1) < 0)
118 tst_resm(TBROK | TERRNO, "write(p1[1], \"go\", ...) failed");
119 else if (read(p2[0], buf, 7) < 0)
120 tst_resm(TBROK | TERRNO, "read(p2[0], buf, ...) failed");
121 else {
122 if (!strcmp(buf, "exists")) {
Monson Shao45192242013-01-08 11:34:44 +0800123 tst_resm(TFAIL, "child process found mqueue");
yaberauneyab5f17ac2009-11-13 11:26:57 +0000124 } else if (!strcmp(buf, "notfnd")) {
Monson Shao45192242013-01-08 11:34:44 +0800125 tst_resm(TPASS, "child process didn't find mqueue");
yaberauneyab5f17ac2009-11-13 11:26:57 +0000126 } else {
Monson Shao45192242013-01-08 11:34:44 +0800127 tst_resm(TFAIL, "UNKNOWN RESULT");
yaberauneyab5f17ac2009-11-13 11:26:57 +0000128 }
subrata_modak09be6112009-03-04 07:03:41 +0000129 }
130
131 /* destroy the mqueue */
Garrett Cooper355093c2010-11-28 22:23:29 -0800132 if (mq_close(mqd) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_brkm(TBROK | TERRNO, NULL, "mq_close failed");
Garrett Cooper355093c2010-11-28 22:23:29 -0800134 }
Jan Stancek359980f2013-02-15 10:16:05 +0100135 ltp_syscall(__NR_mq_unlink, NOSLASH_MQ1);
subrata_modak09be6112009-03-04 07:03:41 +0000136
subrata_modak88c166c2009-06-09 16:01:20 +0000137 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700138}