xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/umount/umount02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 <[email protected]>
5  * Copyright (c) Linux Test Project, 2002-2023
6  * Author: Nirmala Devi Dhanasekar <[email protected]>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Check for basic errors returned by umount(2) system call.
13  *
14  * Verify that umount(2) returns -1 and sets errno to
15  *
16  * 1. EBUSY if it cannot be umounted, because dir is still busy.
17  * 2. EFAULT if specialfile or device file points to invalid address space.
18  * 3. ENOENT if pathname was empty or has a nonexistent component.
19  * 4. EINVAL if specialfile or device is invalid or not a mount point.
20  * 5. ENAMETOOLONG if pathname was longer than MAXPATHLEN.
21  */
22 
23 #include <sys/mount.h>
24 #include "tst_test.h"
25 
26 #define MNTPOINT	"mntpoint"
27 
28 static char long_path[PATH_MAX + 2];
29 static int mount_flag;
30 static int fd;
31 
32 static struct tcase {
33 	const char *err_desc;
34 	const char *mntpoint;
35 	int exp_errno;
36 } tcases[] = {
37 	{"Already mounted/busy", MNTPOINT, EBUSY},
38 	{"Invalid address", NULL, EFAULT},
39 	{"Directory not found", "nonexistent", ENOENT},
40 	{"Invalid  device", "./", EINVAL},
41 	{"Pathname too long", long_path, ENAMETOOLONG}
42 };
43 
verify_umount(unsigned int n)44 static void verify_umount(unsigned int n)
45 {
46 	struct tcase *tc = &tcases[n];
47 
48 	TST_EXP_FAIL(umount(tc->mntpoint), tc->exp_errno);
49 }
50 
setup(void)51 static void setup(void)
52 {
53 	memset(long_path, 'a', PATH_MAX + 1);
54 
55 	SAFE_MKDIR(MNTPOINT, 0775);
56 	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
57 	mount_flag = 1;
58 
59 	fd = SAFE_CREAT(MNTPOINT "/file", 0777);
60 }
61 
cleanup(void)62 static void cleanup(void)
63 {
64 	if (fd > 0)
65 		SAFE_CLOSE(fd);
66 
67 	if (mount_flag)
68 		tst_umount(MNTPOINT);
69 }
70 
71 static struct tst_test test = {
72 	.tcnt = ARRAY_SIZE(tcases),
73 	.needs_root = 1,
74 	.format_device = 1,
75 	.setup = setup,
76 	.cleanup = cleanup,
77 	.test = verify_umount,
78 };
79