xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ustat/ustat02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * Test whether ustat(2) system call returns appropriate error number for
6*49cdfc7eSAndroid Build Coastguard Worker  * invalid dev_t parameter and for bad address paramater.
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #if defined(HAVE_SYS_USTAT_H) || defined(HAVE_LINUX_TYPES_H)
13*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
19*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/ustat.h"
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker static dev_t invalid_dev = -1;
22*49cdfc7eSAndroid Build Coastguard Worker static dev_t root_dev;
23*49cdfc7eSAndroid Build Coastguard Worker struct ustat ubuf;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
26*49cdfc7eSAndroid Build Coastguard Worker 	char *err_desc;
27*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
28*49cdfc7eSAndroid Build Coastguard Worker 	char *exp_errval;
29*49cdfc7eSAndroid Build Coastguard Worker 	dev_t *dev;
30*49cdfc7eSAndroid Build Coastguard Worker 	struct ustat *buf;
31*49cdfc7eSAndroid Build Coastguard Worker } tc[] = {
32*49cdfc7eSAndroid Build Coastguard Worker 	{"Invalid parameter", EINVAL, "EINVAL", &invalid_dev, &ubuf},
33*49cdfc7eSAndroid Build Coastguard Worker 	{"Bad address", EFAULT, "EFAULT", &root_dev, (void*)-1}
34*49cdfc7eSAndroid Build Coastguard Worker };
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(tc);
37*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int test)38*49cdfc7eSAndroid Build Coastguard Worker void run(unsigned int test)
39*49cdfc7eSAndroid Build Coastguard Worker {
40*49cdfc7eSAndroid Build Coastguard Worker 	TEST(tst_syscall(__NR_ustat, (unsigned int)*tc[test].dev, tc[test].buf));
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	if ((TST_RET == -1) && (TST_ERR == tc[test].exp_errno))
43*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS | TTERRNO, "ustat(2) expected failure");
44*49cdfc7eSAndroid Build Coastguard Worker 	else
45*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO,
46*49cdfc7eSAndroid Build Coastguard Worker 			"ustat(2) failed to produce expected error; %d, errno"
47*49cdfc7eSAndroid Build Coastguard Worker 			": %s", tc[test].exp_errno, tc[test].exp_errval);
48*49cdfc7eSAndroid Build Coastguard Worker }
49*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)50*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	/* Find a valid device number */
55*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT("/", &buf);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	root_dev = buf.st_dev;
58*49cdfc7eSAndroid Build Coastguard Worker }
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
61*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
62*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
63*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tc),
64*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
65*49cdfc7eSAndroid Build Coastguard Worker 		{"known-fail", "ustat() is known to fail with EINVAL on Btrfs, see "
66*49cdfc7eSAndroid Build Coastguard Worker 			"https://lore.kernel.org/linux-btrfs/[email protected]/"
67*49cdfc7eSAndroid Build Coastguard Worker 		},
68*49cdfc7eSAndroid Build Coastguard Worker 		{}
69*49cdfc7eSAndroid Build Coastguard Worker 	}
70*49cdfc7eSAndroid Build Coastguard Worker };
71*49cdfc7eSAndroid Build Coastguard Worker #else
72*49cdfc7eSAndroid Build Coastguard Worker TST_TEST_TCONF("testing ustat requires <sys/ustat.h> or <linux/types.h>");
73*49cdfc7eSAndroid Build Coastguard Worker #endif
74