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