xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/statfs/statfs02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *	07/2001 Ported by Wayne Boyer
5  *
6  */
7 
8 /*\
9  * [Description]
10  *
11  * Tests for failures:
12  *
13  * - ENOTDIR A component of the pathname, which is not a directory.
14  * - ENOENT A filename which doesn't exist.
15  * - ENAMETOOLONG A pathname which is longer than MAXNAMLEN.
16  * - EFAULT A pathname pointer outside the address space of the process.
17  * - EFAULT A buf pointer outside the address space of the process.
18  * - ELOOP A filename which has too many symbolic links.
19  */
20 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdlib.h>
24 #include <sys/statfs.h>
25 #include <sys/wait.h>
26 #include "tst_test.h"
27 #include "tst_safe_macros.h"
28 
29 #define TEST_FILE		"statfs_file"
30 #define TEST_FILE1		TEST_FILE"/statfs_file1"
31 #define TEST_NOEXIST		"statfs_noexist"
32 #define TEST_SYMLINK		"statfs_symlink"
33 
34 static int fd;
35 static char test_toolong[PATH_MAX+2];
36 static struct statfs buf;
37 
38 static struct test_case_t {
39 	char *path;
40 	struct statfs *buf;
41 	int exp_error;
42 } tests[] = {
43 	{TEST_FILE1, &buf, ENOTDIR},
44 	{TEST_NOEXIST, &buf, ENOENT},
45 	{test_toolong, &buf, ENAMETOOLONG},
46 	{(char *)-1, &buf, EFAULT},
47 	{TEST_FILE, (struct statfs *)-1, EFAULT},
48 	{TEST_SYMLINK, &buf, ELOOP},
49 };
50 
statfs_verify(unsigned int n)51 static void statfs_verify(unsigned int n)
52 {
53 	int pid, status;
54 
55 	pid = SAFE_FORK();
56 	if (!pid) {
57 		TST_EXP_FAIL(statfs(tests[n].path, tests[n].buf), tests[n].exp_error, "statfs()");
58 		exit(0);
59 	}
60 
61 	SAFE_WAITPID(pid, &status, 0);
62 
63 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
64 		return;
65 
66 	if (tests[n].exp_error == EFAULT &&
67 	    WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
68 		tst_res(TPASS, "Got SIGSEGV instead of EFAULT");
69 		return;
70 	}
71 
72 	tst_res(TFAIL, "Child %s", tst_strstatus(status));
73 }
74 
setup(void)75 static void setup(void)
76 {
77 	unsigned int i;
78 
79 	fd = SAFE_CREAT(TEST_FILE, 0444);
80 
81 	memset(test_toolong, 'a', PATH_MAX+1);
82 
83 	for (i = 0; i < ARRAY_SIZE(tests); i++) {
84 		if (tests[i].path == (char *)-1)
85 			tests[i].path = tst_get_bad_addr(NULL);
86 	}
87 
88 	SAFE_SYMLINK(TEST_SYMLINK, "statfs_symlink_2");
89 	SAFE_SYMLINK("statfs_symlink_2", TEST_SYMLINK);
90 }
91 
cleanup(void)92 static void cleanup(void)
93 {
94 	if (fd > 0)
95 		close(fd);
96 }
97 
98 static struct tst_test test = {
99 	.test = statfs_verify,
100 	.tcnt = ARRAY_SIZE(tests),
101 	.setup = setup,
102 	.cleanup = cleanup,
103 	.needs_tmpdir = 1,
104 	.forks_child = 1,
105 };
106