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