xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fstatfs/fstatfs02.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check if fstatfs() sets errno correctly.
10*49cdfc7eSAndroid Build Coastguard Worker  */
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/statfs.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct statfs buf;
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static int fd;
23*49cdfc7eSAndroid Build Coastguard Worker static int bad_fd = -1;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t {
26*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
27*49cdfc7eSAndroid Build Coastguard Worker 	struct statfs *sbuf;
28*49cdfc7eSAndroid Build Coastguard Worker 	int error;
29*49cdfc7eSAndroid Build Coastguard Worker } tests[] = {
30*49cdfc7eSAndroid Build Coastguard Worker 	{&bad_fd, &buf, EBADF},
31*49cdfc7eSAndroid Build Coastguard Worker 	{&fd, (void *)-1, EFAULT},
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker 
fstatfs_verify(unsigned int n)34*49cdfc7eSAndroid Build Coastguard Worker static void fstatfs_verify(unsigned int n)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	int pid, status;
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
39*49cdfc7eSAndroid Build Coastguard Worker 	if (!pid) {
40*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(fstatfs(*tests[n].fd, tests[n].sbuf), tests[n].error, "fstatfs()");
41*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
42*49cdfc7eSAndroid Build Coastguard Worker 	}
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, &status, 0);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
47*49cdfc7eSAndroid Build Coastguard Worker 		return;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	if (tests[n].error == EFAULT &&
50*49cdfc7eSAndroid Build Coastguard Worker 	    WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV) {
51*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "Got SIGSEGV instead of EFAULT");
52*49cdfc7eSAndroid Build Coastguard Worker 		return;
53*49cdfc7eSAndroid Build Coastguard Worker 	}
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL, "Child %s", tst_strstatus(status));
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)58*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("tempfile", O_RDWR | O_CREAT, 0700);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)63*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	if (fd > 0)
66*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
67*49cdfc7eSAndroid Build Coastguard Worker }
68*49cdfc7eSAndroid Build Coastguard Worker 
69*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
70*49cdfc7eSAndroid Build Coastguard Worker 	.test = fstatfs_verify,
71*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tests),
72*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
73*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
74*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
75*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
76*49cdfc7eSAndroid Build Coastguard Worker };
77