xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fstat/fstat03.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  *  07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  *  05/2019 Ported to new library: Christian Amann <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker /*
8*49cdfc7eSAndroid Build Coastguard Worker  * Tests different error scenarios:
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * 1) Calls fstat() with closed file descriptor
11*49cdfc7eSAndroid Build Coastguard Worker  *    -> EBADF
12*49cdfc7eSAndroid Build Coastguard Worker  * 2) Calls fstat() with an invalid address for stat structure
13*49cdfc7eSAndroid Build Coastguard Worker  *    -> EFAULT (or receive signal SIGSEGV)
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <wait.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE	"test_file"
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker static int fd_ok;
28*49cdfc7eSAndroid Build Coastguard Worker static int fd_ebadf = -1;
29*49cdfc7eSAndroid Build Coastguard Worker static struct stat stat_buf;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
32*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
33*49cdfc7eSAndroid Build Coastguard Worker 	struct stat *stat_buf;
34*49cdfc7eSAndroid Build Coastguard Worker 	int exp_err;
35*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
36*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_ebadf, &stat_buf, EBADF},
37*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_ok, NULL, EFAULT},
38*49cdfc7eSAndroid Build Coastguard Worker };
39*49cdfc7eSAndroid Build Coastguard Worker 
check_fstat(unsigned int tc_num)40*49cdfc7eSAndroid Build Coastguard Worker static void check_fstat(unsigned int tc_num)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[tc_num];
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	TEST(fstat(*tc->fd, tc->stat_buf));
45*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1) {
46*49cdfc7eSAndroid Build Coastguard Worker 		if (tc->exp_err == TST_ERR) {
47*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TPASS,
48*49cdfc7eSAndroid Build Coastguard Worker 				 "fstat() fails with expected error %s",
49*49cdfc7eSAndroid Build Coastguard Worker 				 tst_strerrno(tc->exp_err));
50*49cdfc7eSAndroid Build Coastguard Worker 		} else {
51*49cdfc7eSAndroid Build Coastguard Worker 			tst_res(TFAIL | TTERRNO,
52*49cdfc7eSAndroid Build Coastguard Worker 				 "fstat() did not fail with %s, but with",
53*49cdfc7eSAndroid Build Coastguard Worker 				 tst_strerrno(tc->exp_err));
54*49cdfc7eSAndroid Build Coastguard Worker 		}
55*49cdfc7eSAndroid Build Coastguard Worker 	} else {
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "fstat() returned %ld, expected -1",
57*49cdfc7eSAndroid Build Coastguard Worker 			 TST_RET);
58*49cdfc7eSAndroid Build Coastguard Worker 	}
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
run(unsigned int tc_num)61*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int tc_num)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
64*49cdfc7eSAndroid Build Coastguard Worker 	int status;
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 	pid = SAFE_FORK();
67*49cdfc7eSAndroid Build Coastguard Worker 	if (pid == 0) {
68*49cdfc7eSAndroid Build Coastguard Worker 		check_fstat(tc_num);
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 	}
71*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WAITPID(pid, &status, 0);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (tcases[tc_num].exp_err == EFAULT && WTERMSIG(status) == SIGSEGV) {
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "fstat() failed as expected with SIGSEGV");
75*49cdfc7eSAndroid Build Coastguard Worker 		return;
76*49cdfc7eSAndroid Build Coastguard Worker 	}
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
79*49cdfc7eSAndroid Build Coastguard Worker 		return;
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TFAIL, "child %s", tst_strstatus(status));
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker 	fd_ok = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, 0644);
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)89*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
90*49cdfc7eSAndroid Build Coastguard Worker {
91*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_ok > 0)
92*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_ok);
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
96*49cdfc7eSAndroid Build Coastguard Worker 	.test = run,
97*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
98*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
99*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
100*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
101*49cdfc7eSAndroid Build Coastguard Worker 	.forks_child = 1,
102*49cdfc7eSAndroid Build Coastguard Worker };
103