• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_MOVE of mount(2).
22  *	"Move an existing mount point to the new location."
23  */
24 
25 #include <errno.h>
26 #include <sys/mount.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 
30 #include "test.h"
31 #include "safe_macros.h"
32 
33 #ifndef MS_MOVE
34 #define MS_MOVE	8192
35 #endif
36 
37 #ifndef MS_PRIVATE
38 #define MS_PRIVATE	(1 << 18)
39 #endif
40 
41 #define MNTPOINT_SRC	"mnt_src"
42 #define MNTPOINT_DES	"mnt_des"
43 #define LINELENGTH	256
44 #define DIR_MODE	(S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP)
45 
46 static int ismount(char *mntpoint);
47 static void setup(void);
48 static void cleanup(void);
49 
50 char *TCID = "mount06";
51 int TST_TOTAL = 1;
52 
53 static const char *fs_type;
54 static const char *device;
55 static char path_name[PATH_MAX];
56 static char mntpoint_src[PATH_MAX];
57 static char mntpoint_des[PATH_MAX];
58 static int mount_flag;
59 
main(int argc,char * argv[])60 int main(int argc, char *argv[])
61 {
62 	int lc;
63 
64 	tst_parse_opts(argc, argv, NULL, NULL);
65 
66 	setup();
67 
68 	for (lc = 0; TEST_LOOPING(lc); lc++) {
69 
70 		tst_count = 0;
71 
72 		SAFE_MOUNT(cleanup, device, mntpoint_src, fs_type, 0, NULL);
73 
74 		TEST(mount(mntpoint_src, mntpoint_des, fs_type, MS_MOVE, NULL));
75 
76 		if (TEST_RETURN != 0) {
77 			tst_resm(TFAIL | TTERRNO, "mount(2) failed");
78 		} else {
79 
80 			if (!ismount(mntpoint_src) && ismount(mntpoint_des))
81 				tst_resm(TPASS, "move mount is ok");
82 			else
83 				tst_resm(TFAIL, "move mount does not work");
84 
85 			TEST(tst_umount(mntpoint_des));
86 			if (TEST_RETURN != 0)
87 				tst_brkm(TBROK | TTERRNO, cleanup,
88 					 "umount(2) failed");
89 		}
90 	}
91 
92 	cleanup();
93 	tst_exit();
94 }
95 
ismount(char * mntpoint)96 int ismount(char *mntpoint)
97 {
98 	int ret = 0;
99 	FILE *file;
100 	char line[LINELENGTH];
101 
102 	file = fopen("/proc/mounts", "r");
103 	if (file == NULL)
104 		tst_brkm(TFAIL | TERRNO, NULL, "Open /proc/mounts failed");
105 
106 	while (fgets(line, LINELENGTH, file) != NULL) {
107 		if (strstr(line, mntpoint) != NULL) {
108 			ret = 1;
109 			break;
110 		}
111 	}
112 	fclose(file);
113 	return ret;
114 }
115 
setup(void)116 static void setup(void)
117 {
118 	tst_require_root();
119 
120 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
121 
122 	tst_tmpdir();
123 
124 	fs_type = tst_dev_fs_type();
125 	device = tst_acquire_device(cleanup);
126 
127 	if (!device)
128 		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
129 
130 	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
131 
132 	if (getcwd(path_name, sizeof(path_name)) == NULL)
133 		tst_brkm(TBROK, cleanup, "getcwd failed");
134 
135 	/*
136 	 * Turn current dir into a private mount point being a parent
137 	 * mount which is required by move mount.
138 	 */
139 	SAFE_MOUNT(cleanup, path_name, path_name, "none", MS_BIND, NULL);
140 
141 	mount_flag = 1;
142 
143 	SAFE_MOUNT(cleanup, "none", path_name, "none", MS_PRIVATE, NULL);
144 
145 	snprintf(mntpoint_src, PATH_MAX, "%s/%s", path_name, MNTPOINT_SRC);
146 	snprintf(mntpoint_des, PATH_MAX, "%s/%s", path_name, MNTPOINT_DES);
147 
148 	SAFE_MKDIR(cleanup, mntpoint_src, DIR_MODE);
149 	SAFE_MKDIR(cleanup, mntpoint_des, DIR_MODE);
150 
151 	TEST_PAUSE;
152 }
153 
cleanup(void)154 static void cleanup(void)
155 {
156 	if (mount_flag && tst_umount(path_name) != 0)
157 		tst_resm(TWARN | TERRNO, "umount(2) %s failed", path_name);
158 
159 	if (device)
160 		tst_release_device(device);
161 
162 	tst_rmdir();
163 }
164