subrata_modak | 09be611 | 2009-03-04 07:03:41 +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 | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 16 | * |
| 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 | |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 30 | #ifndef _GNU_SOURCE |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 31 | #define _GNU_SOURCE |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 32 | #endif |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 34 | #include <errno.h> |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 37 | #include <string.h> |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 38 | #include <unistd.h> |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 39 | #include "mqns.h" |
| 40 | |
| 41 | char *TCID = "posixmq_namespace_01"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 42 | int TST_TOTAL = 1; |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 43 | |
| 44 | int p1[2]; |
| 45 | int p2[2]; |
| 46 | |
| 47 | int check_mqueue(void *vtest) |
| 48 | { |
| 49 | char buf[30]; |
| 50 | mqd_t mqd; |
| 51 | |
| 52 | close(p1[1]); |
| 53 | close(p2[0]); |
| 54 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 55 | if (read(p1[0], buf, strlen("go") + 1) < 0) { |
| 56 | printf("read(p1[0], ...) failed: %s\n", strerror(errno)); |
| 57 | exit(1); |
| 58 | } |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 59 | mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDONLY); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 60 | if (mqd == -1) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 61 | if (write(p2[1], "notfnd", strlen("notfnd") + 1) < 0) { |
| 62 | perror("write(p2[1], ...) failed"); |
| 63 | exit(1); |
| 64 | } |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 65 | } else { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 66 | 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_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 73 | } |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 74 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 75 | exit(0); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 78 | int main(int argc, char *argv[]) |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 79 | { |
| 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 87 | "Testing posix mq namespaces through clone(2)."); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 88 | use_clone = T_CLONE; |
| 89 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 91 | "Testing posix mq namespaces through unshare(2)."); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 92 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 93 | if (pipe(p1) == -1 || pipe(p2) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | tst_brkm(TBROK | TERRNO, NULL, "pipe failed"); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 95 | } |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 96 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 97 | mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL, |
| 98 | 0777, NULL); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 99 | if (mqd == -1) { |
| 100 | perror("mq_open"); |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 101 | tst_resm(TFAIL, "mq_open failed"); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 102 | tst_exit(); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 105 | tst_resm(TINFO, "Checking namespaces isolation from parent to child"); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 106 | /* fire off the test */ |
| 107 | r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL); |
| 108 | if (r < 0) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 109 | tst_resm(TFAIL, "failed clone/unshare"); |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 110 | mq_close(mqd); |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 111 | ltp_syscall(__NR_mq_unlink, NOSLASH_MQ1); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 112 | tst_exit(); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | close(p1[0]); |
| 116 | close(p2[1]); |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 117 | 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 Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 123 | tst_resm(TFAIL, "child process found mqueue"); |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 124 | } else if (!strcmp(buf, "notfnd")) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 125 | tst_resm(TPASS, "child process didn't find mqueue"); |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 126 | } else { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 127 | tst_resm(TFAIL, "UNKNOWN RESULT"); |
yaberauneya | b5f17ac | 2009-11-13 11:26:57 +0000 | [diff] [blame] | 128 | } |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* destroy the mqueue */ |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 132 | if (mq_close(mqd) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | tst_brkm(TBROK | TERRNO, NULL, "mq_close failed"); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 134 | } |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 135 | ltp_syscall(__NR_mq_unlink, NOSLASH_MQ1); |
subrata_modak | 09be611 | 2009-03-04 07:03:41 +0000 | [diff] [blame] | 136 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 137 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 138 | } |