xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fstatat/fstatat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2006
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
6*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
8*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
19*49cdfc7eSAndroid Build Coastguard Worker  *	This test case will verify basic function of fstatat64/newfstatat
20*49cdfc7eSAndroid Build Coastguard Worker  *	added by kernel 2.6.16 or up.
21*49cdfc7eSAndroid Build Coastguard Worker  *
22*49cdfc7eSAndroid Build Coastguard Worker  * Author
23*49cdfc7eSAndroid Build Coastguard Worker  *	Yi Yang <[email protected]>
24*49cdfc7eSAndroid Build Coastguard Worker  */
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/time.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
36*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
37*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #define TEST_CASES 6
41*49cdfc7eSAndroid Build Coastguard Worker #ifndef AT_FDCWD
42*49cdfc7eSAndroid Build Coastguard Worker #define AT_FDCWD -100
43*49cdfc7eSAndroid Build Coastguard Worker #endif
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker void setup();
46*49cdfc7eSAndroid Build Coastguard Worker void cleanup();
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fstatat01";
49*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = TEST_CASES;
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static const char pathname[] = "fstatattestdir",
52*49cdfc7eSAndroid Build Coastguard Worker 		  testfile[] = "fstatattestfile.txt",
53*49cdfc7eSAndroid Build Coastguard Worker 		  testfile2[] = "fstatattestdir/fstatattestfile.txt";
54*49cdfc7eSAndroid Build Coastguard Worker static char *testfile3;
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static int fds[TEST_CASES];
57*49cdfc7eSAndroid Build Coastguard Worker static const char *filenames[TEST_CASES];
58*49cdfc7eSAndroid Build Coastguard Worker static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0 };
59*49cdfc7eSAndroid Build Coastguard Worker static const int flags[] = { 0, 0, 0, 0, 9999, 0 };
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker #if !defined(HAVE_FSTATAT)
62*49cdfc7eSAndroid Build Coastguard Worker #if (__NR_fstatat64 > 0)
fstatat(int dirfd,const char * filename,struct stat64 * statbuf,int flags)63*49cdfc7eSAndroid Build Coastguard Worker int fstatat(int dirfd, const char *filename, struct stat64 *statbuf, int flags)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_fstatat64, dirfd, filename, statbuf, flags);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker #elif (__NR_newfstatat > 0)
fstatat(int dirfd,const char * filename,struct stat * statbuf,int flags)68*49cdfc7eSAndroid Build Coastguard Worker int fstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_newfstatat, dirfd, filename, statbuf, flags);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker #else
fstatat(int dirfd,const char * filename,struct stat * statbuf,int flags)73*49cdfc7eSAndroid Build Coastguard Worker int fstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_fstatat, dirfd, filename, statbuf, flags);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker #endif
78*49cdfc7eSAndroid Build Coastguard Worker #endif
79*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)80*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	int lc, i;
83*49cdfc7eSAndroid Build Coastguard Worker #if !defined(HAVE_FSTATAT) && (__NR_fstatat64 > 0)
84*49cdfc7eSAndroid Build Coastguard Worker 	static struct stat64 statbuf;
85*49cdfc7eSAndroid Build Coastguard Worker #else
86*49cdfc7eSAndroid Build Coastguard Worker 	static struct stat statbuf;
87*49cdfc7eSAndroid Build Coastguard Worker #endif
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	setup();
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
94*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < TST_TOTAL; i++) {
97*49cdfc7eSAndroid Build Coastguard Worker 			TEST(fstatat(fds[i], filenames[i], &statbuf, flags[i]));
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_ERRNO == expected_errno[i]) {
100*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS | TTERRNO,
101*49cdfc7eSAndroid Build Coastguard Worker 					 "fstatat failed as expected");
102*49cdfc7eSAndroid Build Coastguard Worker 			} else
103*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TTERRNO, "fstatat failed");
104*49cdfc7eSAndroid Build Coastguard Worker 		}
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	}
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
109*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)112*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
113*49cdfc7eSAndroid Build Coastguard Worker {
114*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
115*49cdfc7eSAndroid Build Coastguard Worker 
116*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 	char *abs_path = tst_get_tmpdir();
119*49cdfc7eSAndroid Build Coastguard Worker 
120*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_ASPRINTF(cleanup, &testfile3, "%s/fstatattestfile3.txt", abs_path);
121*49cdfc7eSAndroid Build Coastguard Worker 	free(abs_path);
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(cleanup, pathname, 0700);
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
126*49cdfc7eSAndroid Build Coastguard Worker 	fds[1] = fds[4] = fds[0];
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(cleanup, testfile, testfile);
129*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	fds[2] =  SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
132*49cdfc7eSAndroid Build Coastguard Worker 
133*49cdfc7eSAndroid Build Coastguard Worker 	fds[3] = 100;
134*49cdfc7eSAndroid Build Coastguard Worker 	fds[5] = AT_FDCWD;
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	filenames[0] = filenames[2] = filenames[3] = filenames[4] =
137*49cdfc7eSAndroid Build Coastguard Worker 	    filenames[5] = testfile;
138*49cdfc7eSAndroid Build Coastguard Worker 	filenames[1] = testfile3;
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)143*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[0] > 0)
146*49cdfc7eSAndroid Build Coastguard Worker 		close(fds[0]);
147*49cdfc7eSAndroid Build Coastguard Worker 	if (fds[2] > 0)
148*49cdfc7eSAndroid Build Coastguard Worker 		close(fds[2]);
149*49cdfc7eSAndroid Build Coastguard Worker 
150*49cdfc7eSAndroid Build Coastguard Worker 	free(testfile3);
151*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
152*49cdfc7eSAndroid Build Coastguard Worker }
153