subrata_modak | 717937a | 2009-03-04 07:09:15 +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 | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 15 | * |
| 16 | * Author: Serge Hallyn <serue@us.ibm.com> |
| 17 | * |
| 18 | * Check mqueuefs lifetime |
| 19 | * . parent creates /dev/mqueue2 |
| 20 | * . child mounts mqueue there |
| 21 | * . child does mq_open("/ab") |
| 22 | * . parent checks for /dev/mqueue2 |
| 23 | * . child exits |
| 24 | * . parent checks for /dev/mqueue2 |
| 25 | * . parent tries 'touch /dev/mqueue2/dd' -> should fail |
| 26 | * . parent umounts /dev/mqueue2 |
| 27 | |
| 28 | ***************************************************************************/ |
| 29 | |
yaberauneya | be3fdcf | 2009-11-14 17:52:51 +0000 | [diff] [blame] | 30 | #ifndef _GNU_SOURCE |
| 31 | #define _GNU_SOURCE |
| 32 | #endif |
Garrett Cooper | 2f5ede3 | 2010-12-18 08:53:36 -0800 | [diff] [blame] | 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 35 | #include <sys/wait.h> |
| 36 | #include <assert.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
| 41 | #include <errno.h> |
| 42 | #include "mqns.h" |
| 43 | |
| 44 | char *TCID = "posixmq_namespace_04"; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 45 | int TST_TOTAL = 1; |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 46 | |
| 47 | int p1[2]; |
| 48 | int p2[2]; |
| 49 | |
| 50 | #define FNAM1 DEV_MQUEUE2 SLASH_MQ1 |
| 51 | #define FNAM2 DEV_MQUEUE2 SLASH_MQ2 |
| 52 | |
| 53 | int check_mqueue(void *vtest) |
| 54 | { |
| 55 | char buf[30]; |
| 56 | mqd_t mqd; |
| 57 | int rc; |
| 58 | |
| 59 | close(p1[1]); |
| 60 | close(p2[0]); |
| 61 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 62 | read(p1[0], buf, 3); /* go */ |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 63 | |
Jan Stancek | 359980f | 2013-02-15 10:16:05 +0100 | [diff] [blame^] | 64 | mqd = ltp_syscall(__NR_mq_open, NOSLASH_MQ1, O_RDWR | O_CREAT | O_EXCL, |
| 65 | 0755, NULL); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 66 | if (mqd == -1) { |
| 67 | write(p2[1], "mqfail", 7); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 68 | tst_exit(); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 69 | } |
| 70 | |
yaberauneya | 3e8f77b | 2009-11-14 23:58:26 +0000 | [diff] [blame] | 71 | mq_close(mqd); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 72 | |
| 73 | rc = mount("mqueue", DEV_MQUEUE2, "mqueue", 0, NULL); |
| 74 | if (rc == -1) { |
| 75 | perror("mount"); |
| 76 | write(p2[1], "mount", 6); |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 77 | tst_exit(); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | write(p2[1], "go", 3); |
| 81 | read(p1[0], buf, 3); |
| 82 | |
subrata_modak | 88c166c | 2009-06-09 16:01:20 +0000 | [diff] [blame] | 83 | tst_exit(); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 84 | } |
| 85 | |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 86 | int main(int argc, char *argv[]) |
| 87 | { |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 88 | int rc; |
| 89 | int status; |
| 90 | char buf[30]; |
| 91 | struct stat statbuf; |
| 92 | int use_clone = T_UNSHARE; |
| 93 | |
| 94 | if (argc == 2 && strcmp(argv[1], "-clone") == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 96 | "Testing posix mq namespaces through clone(2)."); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 97 | use_clone = T_CLONE; |
| 98 | } else |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 99 | tst_resm(TINFO, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 100 | "Testing posix mq namespaces through unshare(2)."); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 101 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | if (pipe(p1) == -1) { |
| 103 | perror("pipe"); |
| 104 | exit(EXIT_FAILURE); |
| 105 | } |
| 106 | if (pipe(p2) == -1) { |
| 107 | perror("pipe"); |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 110 | |
| 111 | mkdir(DEV_MQUEUE2, 0755); |
| 112 | |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 113 | tst_resm(TINFO, "Checking mqueue filesystem lifetime"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 114 | |
| 115 | /* fire off the test */ |
| 116 | rc = do_clone_unshare_test(use_clone, CLONE_NEWIPC, check_mqueue, NULL); |
| 117 | if (rc < 0) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 118 | tst_resm(TFAIL, "failed clone/unshare"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 119 | goto fail; |
| 120 | } |
| 121 | |
| 122 | close(p1[0]); |
| 123 | close(p2[1]); |
| 124 | write(p1[1], "go", 3); |
| 125 | |
| 126 | read(p2[0], buf, 7); |
| 127 | if (!strcmp(buf, "mqfail")) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 128 | tst_resm(TFAIL, "child process could not create mqueue"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 129 | goto fail; |
| 130 | } else if (!strcmp(buf, "mount")) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 131 | tst_resm(TFAIL, "child process could not mount mqueue"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 132 | goto fail; |
| 133 | } |
| 134 | |
| 135 | rc = stat(FNAM1, &statbuf); |
| 136 | if (rc == -1) { |
| 137 | perror("stat"); |
| 138 | write(p1[1], "go", 3); |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 139 | tst_resm(TFAIL, "parent could not see child's created mq"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 140 | goto fail; |
| 141 | } |
| 142 | write(p1[1], "go", 3); |
| 143 | |
| 144 | rc = wait(&status); |
| 145 | if (rc == -1) { |
| 146 | perror("wait"); |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 147 | tst_resm(TFAIL, "error while parent waited on child to exit"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 148 | goto fail; |
| 149 | } |
| 150 | if (!WIFEXITED(status)) { |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 151 | tst_resm(TFAIL, "Child did not exit normally (status %d)", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | status); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 153 | goto fail; |
| 154 | } |
| 155 | rc = stat(FNAM1, &statbuf); |
| 156 | if (rc == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 157 | tst_resm(TFAIL, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 158 | "parent's view of child's mq died with child"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 159 | goto fail; |
| 160 | } |
| 161 | |
| 162 | rc = creat(FNAM2, 0755); |
| 163 | if (rc != -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 164 | tst_resm(TFAIL, |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 165 | "parent was able to create a file in dead child's mqfs"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 166 | goto fail; |
| 167 | } |
| 168 | |
Monson Shao | 4519224 | 2013-01-08 11:34:44 +0800 | [diff] [blame] | 169 | tst_resm(TPASS, "Child mqueue fs still visible for parent"); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 170 | |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 171 | fail: |
| 172 | umount(DEV_MQUEUE2); |
| 173 | rmdir(DEV_MQUEUE2); |
subrata_modak | 717937a | 2009-03-04 07:09:15 +0000 | [diff] [blame] | 174 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 175 | tst_exit(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 176 | } |