1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Fujitsu Ltd.
3f08c3bdfSopenharmony_ci * Author: Guangwen Feng <fenggw-fnst@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
14f08c3bdfSopenharmony_ci * alone with this program.
15f08c3bdfSopenharmony_ci */
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci/*
18f08c3bdfSopenharmony_ci * DESCRIPTION
19f08c3bdfSopenharmony_ci *  Test for feature UMOUNT_NOFOLLOW of umount2().
20f08c3bdfSopenharmony_ci *  "Don't dereference target if it is a symbolic link,
21f08c3bdfSopenharmony_ci *   and fails with the error EINVAL."
22f08c3bdfSopenharmony_ci */
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci#include <errno.h>
25f08c3bdfSopenharmony_ci#include <sys/mount.h>
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#include "test.h"
28f08c3bdfSopenharmony_ci#include "safe_macros.h"
29f08c3bdfSopenharmony_ci#include "lapi/mount.h"
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci#include "umount2.h"
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
34f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
35f08c3bdfSopenharmony_ci#define SYMLINK	"symlink"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic void setup(void);
38f08c3bdfSopenharmony_cistatic void test_umount2(int i);
39f08c3bdfSopenharmony_cistatic void verify_failure(int i);
40f08c3bdfSopenharmony_cistatic void verify_success(int i);
41f08c3bdfSopenharmony_cistatic void cleanup(void);
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic const char *device;
44f08c3bdfSopenharmony_cistatic const char *fs_type;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic int mount_flag;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic struct test_case_t {
49f08c3bdfSopenharmony_ci	const char *mntpoint;
50f08c3bdfSopenharmony_ci	int exp_errno;
51f08c3bdfSopenharmony_ci	const char *desc;
52f08c3bdfSopenharmony_ci} test_cases[] = {
53f08c3bdfSopenharmony_ci	{SYMLINK, EINVAL,
54f08c3bdfSopenharmony_ci		"umount2('symlink', UMOUNT_NOFOLLOW) expected EINVAL"},
55f08c3bdfSopenharmony_ci	{MNTPOINT, 0,
56f08c3bdfSopenharmony_ci		"umount2('mntpoint', UMOUNT_NOFOLLOW) expected success"},
57f08c3bdfSopenharmony_ci};
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cichar *TCID = "umount2_03";
60f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(test_cases);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ciint main(int ac, char **av)
63f08c3bdfSopenharmony_ci{
64f08c3bdfSopenharmony_ci	int lc;
65f08c3bdfSopenharmony_ci	int tc;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci	setup();
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
72f08c3bdfSopenharmony_ci		tst_count = 0;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci		for (tc = 0; tc < TST_TOTAL; tc++)
75f08c3bdfSopenharmony_ci			test_umount2(tc);
76f08c3bdfSopenharmony_ci	}
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	cleanup();
79f08c3bdfSopenharmony_ci	tst_exit();
80f08c3bdfSopenharmony_ci}
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cistatic void setup(void)
83f08c3bdfSopenharmony_ci{
84f08c3bdfSopenharmony_ci	tst_require_root();
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	if ((tst_kvercmp(2, 6, 34)) < 0) {
87f08c3bdfSopenharmony_ci		tst_brkm(TCONF, NULL, "This test can only run on kernels "
88f08c3bdfSopenharmony_ci			 "that are 2.6.34 or higher");
89f08c3bdfSopenharmony_ci	}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, NULL);
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	tst_tmpdir();
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	fs_type = tst_dev_fs_type();
96f08c3bdfSopenharmony_ci	device = tst_acquire_device(cleanup);
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	if (!device)
99f08c3bdfSopenharmony_ci		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, MNTPOINT, DIR_MODE);
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci	SAFE_SYMLINK(cleanup, MNTPOINT, SYMLINK);
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	TEST_PAUSE;
108f08c3bdfSopenharmony_ci}
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cistatic void test_umount2(int i)
111f08c3bdfSopenharmony_ci{
112f08c3bdfSopenharmony_ci	SAFE_MOUNT(cleanup, device, MNTPOINT, fs_type, 0, NULL);
113f08c3bdfSopenharmony_ci	mount_flag = 1;
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	TEST(umount2_retry(test_cases[i].mntpoint, UMOUNT_NOFOLLOW));
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	if (test_cases[i].exp_errno != 0)
118f08c3bdfSopenharmony_ci		verify_failure(i);
119f08c3bdfSopenharmony_ci	else
120f08c3bdfSopenharmony_ci		verify_success(i);
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci	if (mount_flag) {
123f08c3bdfSopenharmony_ci		if (tst_umount(MNTPOINT))
124f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "umount() failed");
125f08c3bdfSopenharmony_ci		mount_flag = 0;
126f08c3bdfSopenharmony_ci	}
127f08c3bdfSopenharmony_ci}
128f08c3bdfSopenharmony_ci
129f08c3bdfSopenharmony_cistatic void verify_failure(int i)
130f08c3bdfSopenharmony_ci{
131f08c3bdfSopenharmony_ci	if (TEST_RETURN == 0) {
132f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "%s passed unexpectedly", test_cases[i].desc);
133f08c3bdfSopenharmony_ci		mount_flag = 0;
134f08c3bdfSopenharmony_ci		return;
135f08c3bdfSopenharmony_ci	}
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	if (TEST_ERRNO != test_cases[i].exp_errno) {
138f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "%s failed unexpectedly",
139f08c3bdfSopenharmony_ci			 test_cases[i].desc);
140f08c3bdfSopenharmony_ci		return;
141f08c3bdfSopenharmony_ci	}
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	tst_resm(TPASS | TTERRNO, "umount2(2) failed as expected");
144f08c3bdfSopenharmony_ci}
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_cistatic void verify_success(int i)
147f08c3bdfSopenharmony_ci{
148f08c3bdfSopenharmony_ci	if (TEST_RETURN != 0) {
149f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "%s failed unexpectedly",
150f08c3bdfSopenharmony_ci			 test_cases[i].desc);
151f08c3bdfSopenharmony_ci		return;
152f08c3bdfSopenharmony_ci	}
153f08c3bdfSopenharmony_ci
154f08c3bdfSopenharmony_ci	tst_resm(TPASS, "umount2(2) succeeded as expected");
155f08c3bdfSopenharmony_ci	mount_flag = 0;
156f08c3bdfSopenharmony_ci}
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_cistatic void cleanup(void)
159f08c3bdfSopenharmony_ci{
160f08c3bdfSopenharmony_ci	if (mount_flag && tst_umount(MNTPOINT))
161f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "Failed to unmount");
162f08c3bdfSopenharmony_ci
163f08c3bdfSopenharmony_ci	if (device)
164f08c3bdfSopenharmony_ci		tst_release_device(device);
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_ci	tst_rmdir();
167f08c3bdfSopenharmony_ci}
168