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