subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 14 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 15 | * |
| 16 | * Author: Serge Hallyn <serue@us.ibm.com> |
| 17 | * |
| 18 | * Check ipcns+sb longevity |
| 19 | * |
| 20 | * Mount mqueue fs |
| 21 | * unshare |
| 22 | * In unshared process: |
| 23 | * Create "/mq1" with mq_open() |
| 24 | * Mount mqueuefs |
| 25 | * Check that /mq1 exists |
| 26 | * Create /dev/mqueue/mq2 through vfs (create(2)) |
| 27 | * Umount /dev/mqueue |
| 28 | * Remount /dev/mqueue |
| 29 | * Check that both /mq1 and /mq2 exist |
| 30 | |
| 31 | ***************************************************************************/ |
| 32 | |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 33 | #ifndef _GNU_SOURCE |
| 34 | #define _GNU_SOURCE |
| 35 | #endif |
Garrett Cooper | 2f5ede3 | 2010-12-18 08:53:36 -0800 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 38 | #include <sys/wait.h> |
| 39 | #include <assert.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <unistd.h> |
| 43 | #include <string.h> |
| 44 | #include <errno.h> |
| 45 | #include "mqns.h" |
| 46 | |
| 47 | char *TCID = "posixmq_namespace_03"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 48 | int TST_TOTAL = 1; |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 49 | |
| 50 | int p1[2]; |
| 51 | int p2[2]; |
| 52 | |
| 53 | #define FNAM1 DEV_MQUEUE2 SLASH_MQ1 |
| 54 | #define FNAM2 DEV_MQUEUE2 SLASH_MQ2 |
| 55 | |
| 56 | int check_mqueue(void *vtest) |
| 57 | { |
| 58 | char buf[30]; |
| 59 | mqd_t mqd; |
| 60 | int rc; |
| 61 | struct stat statbuf; |
| 62 | |
| 63 | close(p1[1]); |
| 64 | close(p2[0]); |
| 65 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 66 | if (read(p1[0], buf, 3) != 3) { /* go */ |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 67 | perror("read failed"); |
| 68 | exit(1); |
| 69 | } |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 70 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 71 | mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL, |
| 72 | 0755, NULL); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 73 | if (mqd == -1) { |
| 74 | write(p2[1], "mqfail", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 75 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 78 | mq_close(mqd); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 79 | |
| 80 | rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL); |
| 81 | if (rc == -1) { |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 82 | write(p2[1], "mount1", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 83 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | rc = stat(FNAM1, &statbuf); |
| 87 | if (rc == -1) { |
| 88 | write(p2[1], "stat1", 6); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 89 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | rc = creat(FNAM2, 0755); |
| 93 | if (rc == -1) { |
| 94 | write(p2[1], "creat", 6); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 95 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | close(rc); |
| 99 | |
| 100 | rc = umount(DEV_MQUEUE2); |
| 101 | if (rc == -1) { |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 102 | write(p2[1], "umount", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 103 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL); |
| 107 | if (rc == -1) { |
| 108 | write(p2[1], "mount2", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 109 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | rc = stat(FNAM1, &statbuf); |
| 113 | if (rc == -1) { |
| 114 | write(p2[1], "stat2", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 115 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | rc = stat(FNAM2, &statbuf); |
| 119 | if (rc == -1) { |
| 120 | write(p2[1], "stat3", 7); |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 121 | exit(1); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | write(p2[1], "done", 5); |
| 125 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 126 | exit(0); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 127 | } |
| 128 | |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 129 | int main(int argc, char *argv[]) |
| 130 | { |
| 131 | int r; |
| 132 | char buf[30]; |
| 133 | int use_clone = T_UNSHARE; |
| 134 | |
| 135 | if (argc == 2 && strcmp(argv[1], "-clone") == 0) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 136 | tst_resm(TINFO, "Testing posix mq namespaces through clone(2)"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 137 | use_clone = T_CLONE; |
| 138 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | tst_resm(TINFO, |
| 140 | "Testing posix mq namespaces through unshare(2)"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 141 | |
Garrett Cooper | 2f5ede3 | 2010-12-18 08:53:36 -0800 | [diff] [blame] | 142 | if (pipe(p1) == -1 || pipe(p2) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | tst_brkm(TBROK | TERRNO, NULL, "pipe failed"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 144 | |
| 145 | /* fire off the test */ |
| 146 | r = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL); |
| 147 | if (r < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 148 | tst_brkm(TBROK | TERRNO, NULL, "failed clone/unshare"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 151 | tst_resm(TINFO, "Checking correct umount+remount of mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 152 | |
| 153 | mkdir(DEV_MQUEUE2, 0755); |
| 154 | |
| 155 | close(p1[0]); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 156 | close(p2[1]); |
| 157 | write(p1[1], "go", 3); |
| 158 | |
| 159 | read(p2[0], buf, 7); |
| 160 | r = TFAIL; |
| 161 | if (!strcmp(buf, "mqfail")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 162 | tst_resm(TFAIL, "child process could not create mqueue"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 163 | goto fail; |
| 164 | } else if (!strcmp(buf, "mount1")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 165 | tst_resm(TFAIL, "child process could not mount mqueue"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 166 | goto fail; |
| 167 | } else if (!strcmp(buf, "stat1x")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 168 | tst_resm(TFAIL, "mq created by child is not in mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 169 | goto fail; |
| 170 | } else if (!strcmp(buf, "creat")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 171 | tst_resm(TFAIL, "child couldn't creat mq through mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 172 | goto fail; |
| 173 | } else if (!strcmp(buf, "umount")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 174 | tst_resm(TFAIL, "child couldn't umount mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 175 | goto fail; |
| 176 | } else if (!strcmp(buf, "mount2")) { |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 177 | tst_resm(TFAIL, "child couldn't remount mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 178 | goto fail; |
| 179 | } else if (!strcmp(buf, "stat2")) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_resm(TFAIL, |
| 181 | "mq_open()d file gone after remount of mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 182 | goto fail; |
| 183 | } else if (!strcmp(buf, "stat3")) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | tst_resm(TFAIL, |
| 185 | "creat(2)'d file gone after remount of mqueuefs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 186 | goto fail; |
| 187 | } |
| 188 | |
Garrett Cooper | 355093c | 2010-11-28 22:23:29 -0800 | [diff] [blame] | 189 | tst_resm(TPASS, "umount+remount of mqueuefs remounted the right fs"); |
subrata_modak | a83feac | 2009-03-04 07:08:02 +0000 | [diff] [blame] | 190 | |
| 191 | r = 0; |
| 192 | fail: |
| 193 | umount(DEV_MQUEUE2); |
| 194 | rmdir(DEV_MQUEUE2); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 195 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 196 | } |