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