xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fsmount/fsmount02.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) 2020 Viresh Kumar <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Basic fsmount() failure tests.
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
8*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fsmount.h"
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker int fd = -1, invalid_fd = -1;
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT	"mntpoint"
13*49cdfc7eSAndroid Build Coastguard Worker 
14*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
15*49cdfc7eSAndroid Build Coastguard Worker 	char *name;
16*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
17*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int flags;
18*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int mount_attrs;
19*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
20*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
21*49cdfc7eSAndroid Build Coastguard Worker 	{"invalid-fd", &invalid_fd, FSMOUNT_CLOEXEC, 0, EBADF},
22*49cdfc7eSAndroid Build Coastguard Worker 	{"invalid-flags", &fd, 0x02, 0, EINVAL},
23*49cdfc7eSAndroid Build Coastguard Worker 	{"invalid-attrs", &fd, FSMOUNT_CLOEXEC, 0x100, EINVAL},
24*49cdfc7eSAndroid Build Coastguard Worker };
25*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)26*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
27*49cdfc7eSAndroid Build Coastguard Worker {
28*49cdfc7eSAndroid Build Coastguard Worker 	if (fd != -1)
29*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
30*49cdfc7eSAndroid Build Coastguard Worker }
31*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)32*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
33*49cdfc7eSAndroid Build Coastguard Worker {
34*49cdfc7eSAndroid Build Coastguard Worker 	fsopen_supported_by_kernel();
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fd = fsopen(tst_device->fs_type, 0));
37*49cdfc7eSAndroid Build Coastguard Worker 	if (fd == -1)
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "fsopen() failed");
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fsconfig(fd, FSCONFIG_SET_STRING, "source", tst_device->dev, 0));
41*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
42*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_SET_STRING) failed");
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fsconfig(fd, FSCONFIG_CMD_CREATE, NULL, NULL, 0));
45*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
46*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK | TTERRNO, "fsconfig(FSCONFIG_CMD_CREATE) failed");
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int n)49*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
50*49cdfc7eSAndroid Build Coastguard Worker {
51*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fsmount(*tc->fd, tc->flags, tc->mount_attrs));
54*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != -1) {
55*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(TST_RET);
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "%s: fsmount() succeeded unexpectedly (index: %d)",
57*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, n);
58*49cdfc7eSAndroid Build Coastguard Worker 		return;
59*49cdfc7eSAndroid Build Coastguard Worker 	}
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->exp_errno != TST_ERR) {
62*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "%s: fsmount() should fail with %s",
63*49cdfc7eSAndroid Build Coastguard Worker 			tc->name, tst_strerrno(tc->exp_errno));
64*49cdfc7eSAndroid Build Coastguard Worker 		return;
65*49cdfc7eSAndroid Build Coastguard Worker 	}
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS | TTERRNO, "%s: fsmount() failed as expected", tc->name);
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
71*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
72*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
73*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
74*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
75*49cdfc7eSAndroid Build Coastguard Worker 	.needs_root = 1,
76*49cdfc7eSAndroid Build Coastguard Worker 	.mntpoint = MNTPOINT,
77*49cdfc7eSAndroid Build Coastguard Worker 	.format_device = 1,
78*49cdfc7eSAndroid Build Coastguard Worker 	.all_filesystems = 1,
79*49cdfc7eSAndroid Build Coastguard Worker 	.skip_filesystems = (const char *const []){"fuse", NULL},
80*49cdfc7eSAndroid Build Coastguard Worker };
81