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