subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 16 | * |
| 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 Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 29 | * Dec 16: accomodate new mqns semantics (Serge Hallyn) |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 30 | |
| 31 | ***************************************************************************/ |
| 32 | |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 33 | #ifndef _GNU_SOURCE |
| 34 | #define _GNU_SOURCE |
| 35 | #endif |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 36 | #include <sys/wait.h> |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 37 | #include <errno.h> |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 40 | #include <string.h> |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 42 | #include "mqns.h" |
| 43 | |
| 44 | char *TCID = "posixmq_namespace_02"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | int TST_TOTAL = 1; |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 46 | |
| 47 | int p1[2]; |
| 48 | int p2[2]; |
| 49 | |
| 50 | int check_mqueue(void *vtest) |
| 51 | { |
| 52 | char buf[30]; |
| 53 | mqd_t mqd; |
| 54 | |
| 55 | close(p1[1]); |
| 56 | close(p2[0]); |
| 57 | |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 58 | if (read(p1[0], buf, 3) < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 59 | perror("read(p1[0], ..) failed"); |
| 60 | exit(1); |
Garrett Cooper | ad14e90 | 2010-12-16 10:03:44 -0800 | [diff] [blame] | 61 | } else { |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 62 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 63 | mqd = |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 64 | ltp_syscall(__NR_mq_open, NOSLASH_MQ1, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 65 | O_RDWR | O_CREAT | O_EXCL, 0777, NULL); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 66 | if (mqd == -1) { |
| 67 | if (write(p2[1], "mqfail", strlen("mqfail") + 1) < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 68 | perror("write(p2[1], \"mqfail\", ..) failed"); |
| 69 | exit(1); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 70 | } |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 71 | } else { |
| 72 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 73 | if (write(p2[1], "mqopen", strlen("mqopen") + 1) < 0) { |
| 74 | perror("write(p2[1], \"mqopen\", ..) failed"); |
| 75 | exit(1); |
| 76 | } else { |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 78 | if (read(p1[0], buf, 5) < 0) { |
| 79 | perror("read(p1[0], ..) failed"); |
| 80 | exit(1); |
| 81 | } else { |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 82 | |
| 83 | /* destroy the mqueue */ |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 84 | if (mq_close(mqd) < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 85 | perror("mq_close(mqd) failed"); |
| 86 | exit(1); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 87 | } else if (ltp_syscall(__NR_mq_unlink, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 88 | NOSLASH_MQ1) < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 89 | perror("mq_unlink(" NOSLASH_MQ1 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | ") failed"); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 91 | exit(1); |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 92 | } else if (write(p2[1], "done", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | strlen("done") + 1) |
| 94 | < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 95 | perror("write(p2[1], " |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 96 | "\"done\", ..) failed"); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 97 | exit(1); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | } |
| 101 | |
| 102 | } |
| 103 | |
| 104 | } |
| 105 | |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 106 | } |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 107 | exit(0); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 108 | |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 109 | } |
| 110 | |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 111 | int 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 120 | "Testing posix mq namespaces through clone(2)."); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 121 | use_clone = T_CLONE; |
| 122 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 123 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 124 | "Testing posix mq namespaces through unshare(2)."); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 125 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 126 | if (pipe(p1) == -1 || pipe(p2) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_brkm(TBROK | TERRNO, NULL, "pipe"); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 128 | } |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 129 | |
| 130 | /* fire off the test */ |
| 131 | r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL); |
| 132 | if (r < 0) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 133 | tst_brkm(TFAIL, NULL, "failed clone/unshare"); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 136 | tst_resm(TINFO, "Checking namespaces isolation (child to parent)"); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 137 | |
| 138 | close(p1[0]); |
| 139 | close(p2[1]); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 140 | if (write(p1[1], "go", strlen("go") + 1) < 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 141 | tst_brkm(TBROK, NULL, "write(p1[1], \"go\", ..) failed"); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 142 | } |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 143 | |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 144 | if (read(p2[0], buf, 7) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 145 | tst_resm(TBROK | TERRNO, "read(p2[0], ..) failed"); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 146 | } else if (!strcmp(buf, "mqfail")) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 147 | tst_resm(TFAIL, "child process could not create mqueue"); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 148 | umount(DEV_MQUEUE); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 149 | } else if (strcmp(buf, "mqopen")) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 150 | tst_resm(TFAIL, "child process could not create mqueue"); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 151 | umount(DEV_MQUEUE); |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 152 | } else { |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 153 | mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDONLY); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 154 | if (mqd == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 155 | tst_resm(TPASS, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 156 | "Parent process can't see the mqueue"); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 157 | } else { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | tst_resm(TFAIL | TERRNO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 159 | "Parent process found mqueue"); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 160 | mq_close(mqd); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 161 | } |
Rishikesh K Rajak | 742cfad | 2010-04-29 18:56:12 +0530 | [diff] [blame] | 162 | if (write(p1[1], "cont", 5) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 163 | tst_resm(TBROK | TERRNO, "write(p1[1], ..) failed"); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 164 | } |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 165 | read(p2[0], buf, 7); |
yaberauneya | f247867 | 2009-11-12 20:44:46 +0000 | [diff] [blame] | 166 | } |
subrata_modak | b264caf | 2009-03-04 07:05:49 +0000 | [diff] [blame] | 167 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 168 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 169 | } |