blob: 710adb1b08db354e397176695b4af3a048561aca [file] [log] [blame]
DAN LI9146d832013-07-08 13:27:38 +08001/*
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 LI9146d832013-07-08 13:27:38 +080034#include "safe_macros.h"
35
36static void help(void);
37static void setup(void);
38static void cleanup(void);
39
40char *TCID = "mount05";
41int TST_TOTAL = 1;
42
43#define DIR_MODE (S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP)
44
DAN LI9146d832013-07-08 13:27:38 +080045static int dflag;
Cyril Hrubise2ecf282013-10-15 14:33:58 +020046static char *fstype = "ext2";
DAN LI9146d832013-07-08 13:27:38 +080047static char *device;
48static const char file_src[] = "mnt_src/tstfile";
49static const char file_des[] = "mnt_des/tstfile";
50static const char mntpoint_src[] = "mnt_src";
51static const char mntpoint_des[] = "mnt_des";
52
53static option_t options[] = {
Cyril Hrubise2ecf282013-10-15 14:33:58 +020054 {"T:", NULL, &fstype},
DAN LI9146d832013-07-08 13:27:38 +080055 {"D:", &dflag, &device},
56 {NULL, NULL, NULL},
57};
58
59int main(int argc, char *argv[])
60{
61 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020062 const char *msg;
DAN LI9146d832013-07-08 13:27:38 +080063
64 msg = parse_opts(argc, argv, options, &help);
65 if (msg != NULL)
66 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
67
DAN LI9146d832013-07-08 13:27:38 +080068 setup();
69
70 for (lc = 0; TEST_LOOPING(lc); lc++) {
71
72 tst_count = 0;
73
Cyril Hrubise2ecf282013-10-15 14:33:58 +020074 TEST(mount(mntpoint_src, mntpoint_des, fstype, MS_BIND, NULL));
DAN LI9146d832013-07-08 13:27:38 +080075
76 if (TEST_RETURN != 0) {
77 tst_resm(TFAIL | TTERRNO, "mount(2) failed");
78 } else {
79
Vinson Leefbb12d02013-07-09 09:55:42 -070080 if (open(file_des, O_CREAT | O_EXCL, S_IRWXU) == -1 &&
DAN LI9146d832013-07-08 13:27:38 +080081 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 Hrubisc60a2302015-01-29 16:28:24 +010087 TEST(tst_umount(mntpoint_des));
DAN LI9146d832013-07-08 13:27:38 +080088 if (TEST_RETURN != 0)
89 tst_brkm(TBROK | TTERRNO, cleanup,
90 "umount(2) failed");
91 }
92 }
93
94 cleanup();
DAN LI9146d832013-07-08 13:27:38 +080095 tst_exit();
96}
97
98void setup(void)
99{
100 tst_require_root(NULL);
DAN LI9146d832013-07-08 13:27:38 +0800101
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 LI3e0e3362013-08-27 11:14:20 +0800109 if (dflag) {
Cyril Hrubise2ecf282013-10-15 14:33:58 +0200110 tst_mkfs(NULL, device, fstype, NULL);
DAN LI3e0e3362013-08-27 11:14:20 +0800111
Cyril Hrubise2ecf282013-10-15 14:33:58 +0200112 if (mount(device, mntpoint_src, fstype, 0, NULL) == -1)
DAN LI9146d832013-07-08 13:27:38 +0800113 tst_brkm(TBROK | TERRNO, cleanup, "mount failed");
DAN LI3e0e3362013-08-27 11:14:20 +0800114 }
DAN LI9146d832013-07-08 13:27:38 +0800115
116 SAFE_FILE_PRINTF(cleanup, file_src, "TEST FILE");
117
118 TEST_PAUSE;
119}
120
121void cleanup(void)
122{
DAN LI3e0e3362013-08-27 11:14:20 +0800123 if (dflag)
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100124 if (tst_umount(mntpoint_src) != 0)
DAN LI9146d832013-07-08 13:27:38 +0800125 tst_brkm(TBROK | TTERRNO, NULL, "umount(2) failed");
126
DAN LI9146d832013-07-08 13:27:38 +0800127 tst_rmdir();
128}
129
130void 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}