xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/statvfs/statvfs02.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 /*
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2014 Fujitsu Ltd.
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: Zeng Linggang <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 SUSE LLC Avinesh Kumar <[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  * Verify that statvfs() fails with:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT when path points to an invalid address.
15*49cdfc7eSAndroid Build Coastguard Worker  * - ELOOP when too many symbolic links were encountered in translating path.
16*49cdfc7eSAndroid Build Coastguard Worker  * - ENAMETOOLONG when path is too long.
17*49cdfc7eSAndroid Build Coastguard Worker  * - ENOENT when the file referred to by path does not exist.
18*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTDIR a component of the path prefix of path is not a directory.
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/statvfs.h>
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define TEST_SYMLINK	"statvfs_symlink"
26*49cdfc7eSAndroid Build Coastguard Worker #define TEST_FILE	"statvfs_file"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static struct statvfs buf;
29*49cdfc7eSAndroid Build Coastguard Worker static char nametoolong[PATH_MAX+2];
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
32*49cdfc7eSAndroid Build Coastguard Worker 	char *path;
33*49cdfc7eSAndroid Build Coastguard Worker 	struct statvfs *buf;
34*49cdfc7eSAndroid Build Coastguard Worker 	int exp_errno;
35*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
36*49cdfc7eSAndroid Build Coastguard Worker 	{(char *)-1, &buf, EFAULT},
37*49cdfc7eSAndroid Build Coastguard Worker 	{TEST_SYMLINK, &buf, ELOOP},
38*49cdfc7eSAndroid Build Coastguard Worker 	{nametoolong, &buf, ENAMETOOLONG},
39*49cdfc7eSAndroid Build Coastguard Worker 	{"filenoexist", &buf, ENOENT},
40*49cdfc7eSAndroid Build Coastguard Worker 	{"statvfs_file/test", &buf, ENOTDIR},
41*49cdfc7eSAndroid Build Coastguard Worker };
42*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)43*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK(TEST_SYMLINK, "symlink_2");
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SYMLINK("symlink_2", TEST_SYMLINK);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	memset(nametoolong, 'a', PATH_MAX+1);
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(TEST_FILE, 0644, NULL);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < ARRAY_SIZE(tcases); i++) {
54*49cdfc7eSAndroid Build Coastguard Worker 		if (tcases[i].path == (char *)-1)
55*49cdfc7eSAndroid Build Coastguard Worker 			tcases[i].path = tst_get_bad_addr(NULL);
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int i)59*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(statvfs(tc->path, tc->buf), tc->exp_errno);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
67*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
68*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
69*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
70*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1
71*49cdfc7eSAndroid Build Coastguard Worker };
72