DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Fujitsu Ltd. |
| 3 | * Author: DAN LI <li.dan@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * DESCRIPTION |
| 21 | * Test for feature MS_BIND of mount. |
| 22 | * "Perform a bind mount, making a file or a directory subtree visible |
| 23 | * at another point within a file system." |
| 24 | */ |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <sys/mount.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | |
| 33 | #include "test.h" |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 34 | #include "safe_macros.h" |
| 35 | |
| 36 | static void help(void); |
| 37 | static void setup(void); |
| 38 | static void cleanup(void); |
| 39 | |
| 40 | char *TCID = "mount05"; |
| 41 | int TST_TOTAL = 1; |
| 42 | |
| 43 | #define DIR_MODE (S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP) |
| 44 | |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 45 | static int dflag; |
Cyril Hrubis | e2ecf28 | 2013-10-15 14:33:58 +0200 | [diff] [blame] | 46 | static char *fstype = "ext2"; |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 47 | static char *device; |
| 48 | static const char file_src[] = "mnt_src/tstfile"; |
| 49 | static const char file_des[] = "mnt_des/tstfile"; |
| 50 | static const char mntpoint_src[] = "mnt_src"; |
| 51 | static const char mntpoint_des[] = "mnt_des"; |
| 52 | |
| 53 | static option_t options[] = { |
Cyril Hrubis | e2ecf28 | 2013-10-15 14:33:58 +0200 | [diff] [blame] | 54 | {"T:", NULL, &fstype}, |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 55 | {"D:", &dflag, &device}, |
| 56 | {NULL, NULL, NULL}, |
| 57 | }; |
| 58 | |
| 59 | int main(int argc, char *argv[]) |
| 60 | { |
| 61 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 62 | const char *msg; |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 63 | |
| 64 | msg = parse_opts(argc, argv, options, &help); |
| 65 | if (msg != NULL) |
| 66 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 67 | |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 68 | setup(); |
| 69 | |
| 70 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 71 | |
| 72 | tst_count = 0; |
| 73 | |
Cyril Hrubis | e2ecf28 | 2013-10-15 14:33:58 +0200 | [diff] [blame] | 74 | TEST(mount(mntpoint_src, mntpoint_des, fstype, MS_BIND, NULL)); |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 75 | |
| 76 | if (TEST_RETURN != 0) { |
| 77 | tst_resm(TFAIL | TTERRNO, "mount(2) failed"); |
| 78 | } else { |
| 79 | |
Vinson Lee | fbb12d0 | 2013-07-09 09:55:42 -0700 | [diff] [blame] | 80 | if (open(file_des, O_CREAT | O_EXCL, S_IRWXU) == -1 && |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 81 | errno == EEXIST) |
| 82 | tst_resm(TPASS, "bind mount is ok"); |
| 83 | else |
| 84 | tst_resm(TFAIL, "file %s is not available", |
| 85 | file_des); |
| 86 | |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 87 | TEST(tst_umount(mntpoint_des)); |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 88 | if (TEST_RETURN != 0) |
| 89 | tst_brkm(TBROK | TTERRNO, cleanup, |
| 90 | "umount(2) failed"); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | cleanup(); |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 95 | tst_exit(); |
| 96 | } |
| 97 | |
| 98 | void setup(void) |
| 99 | { |
| 100 | tst_require_root(NULL); |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 101 | |
| 102 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 103 | |
| 104 | tst_tmpdir(); |
| 105 | |
| 106 | SAFE_MKDIR(cleanup, mntpoint_src, DIR_MODE); |
| 107 | SAFE_MKDIR(cleanup, mntpoint_des, DIR_MODE); |
| 108 | |
DAN LI | 3e0e336 | 2013-08-27 11:14:20 +0800 | [diff] [blame] | 109 | if (dflag) { |
Cyril Hrubis | e2ecf28 | 2013-10-15 14:33:58 +0200 | [diff] [blame] | 110 | tst_mkfs(NULL, device, fstype, NULL); |
DAN LI | 3e0e336 | 2013-08-27 11:14:20 +0800 | [diff] [blame] | 111 | |
Cyril Hrubis | e2ecf28 | 2013-10-15 14:33:58 +0200 | [diff] [blame] | 112 | if (mount(device, mntpoint_src, fstype, 0, NULL) == -1) |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 113 | tst_brkm(TBROK | TERRNO, cleanup, "mount failed"); |
DAN LI | 3e0e336 | 2013-08-27 11:14:20 +0800 | [diff] [blame] | 114 | } |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 115 | |
| 116 | SAFE_FILE_PRINTF(cleanup, file_src, "TEST FILE"); |
| 117 | |
| 118 | TEST_PAUSE; |
| 119 | } |
| 120 | |
| 121 | void cleanup(void) |
| 122 | { |
DAN LI | 3e0e336 | 2013-08-27 11:14:20 +0800 | [diff] [blame] | 123 | if (dflag) |
Cyril Hrubis | c60a230 | 2015-01-29 16:28:24 +0100 | [diff] [blame] | 124 | if (tst_umount(mntpoint_src) != 0) |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 125 | tst_brkm(TBROK | TTERRNO, NULL, "umount(2) failed"); |
| 126 | |
DAN LI | 9146d83 | 2013-07-08 13:27:38 +0800 | [diff] [blame] | 127 | tst_rmdir(); |
| 128 | } |
| 129 | |
| 130 | void help(void) |
| 131 | { |
| 132 | printf("-T type : specifies the type of filesystem to be mounted. " |
| 133 | "Default ext2.\n"); |
| 134 | printf("-D device : device used for mounting.\n"); |
| 135 | } |