1 /*
2  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3  * Copyright (c) 2014 Cyril Hrubis <chrubis@suse.cz>
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  *    AUTHOR		: Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
18  *
19  *    DESCRIPTION
20  *	Check for basic errors returned by umount(2) system call.
21  *
22  *	Verify that umount(2) returns -1 and sets errno to
23  *
24  *	1) EBUSY if it cannot be umounted, because dir is still busy.
25  *	2) EFAULT if specialfile or device file points to invalid address space.
26  *	3) ENOENT if pathname was empty or has a nonexistent component.
27  *	4) EINVAL if specialfile or device is invalid or not a mount point.
28  *	5) ENAMETOOLONG if pathname was longer than MAXPATHLEN.
29  *****************************************************************************/
30 
31 #include <errno.h>
32 #include <string.h>
33 #include <sys/mount.h>
34 #include "tst_test.h"
35 
36 #define MNTPOINT	"mntpoint"
37 
38 static char long_path[PATH_MAX + 2];
39 static int mount_flag;
40 static int fd;
41 
42 static struct tcase {
43 	const char *err_desc;
44 	const char *mntpoint;
45 	int exp_errno;
46 } tcases[] = {
47 	{"Already mounted/busy", MNTPOINT, EBUSY},
48 	{"Invalid address", NULL, EFAULT},
49 	{"Directory not found", "nonexistent", ENOENT},
50 	{"Invalid  device", "./", EINVAL},
51 	{"Pathname too long", long_path, ENAMETOOLONG}
52 };
53 
verify_umount(unsigned int n)54 static void verify_umount(unsigned int n)
55 {
56 	struct tcase *tc = &tcases[n];
57 
58 	TEST(umount(tc->mntpoint));
59 
60 	if (TEST_RETURN != -1) {
61 		tst_res(TFAIL, "umount() succeeds unexpectedly");
62 		return;
63 	}
64 
65 	if (tc->exp_errno != TEST_ERRNO) {
66 		tst_res(TFAIL | TTERRNO, "umount() should fail with %s",
67 			tst_strerrno(tc->exp_errno));
68 		return;
69 	}
70 
71 	tst_res(TPASS | TTERRNO, "umount() fails as expected: %s",
72 		tc->err_desc);
73 }
74 
setup(void)75 static void setup(void)
76 {
77 	memset(long_path, 'a', PATH_MAX + 1);
78 
79 	SAFE_MKDIR(MNTPOINT, 0775);
80 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
81 	mount_flag = 1;
82 
83 	fd = SAFE_CREAT(MNTPOINT "/file", 0777);
84 }
85 
cleanup(void)86 static void cleanup(void)
87 {
88 	if (fd > 0)
89 		SAFE_CLOSE(fd);
90 
91 	if (mount_flag)
92 		tst_umount(MNTPOINT);
93 }
94 
95 static struct tst_test test = {
96 	.tcnt = ARRAY_SIZE(tcases),
97 	.needs_root = 1,
98 	.needs_tmpdir = 1,
99 	.format_device = 1,
100 	.setup = setup,
101 	.cleanup = cleanup,
102 	.test = verify_umount,
103 };
104