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