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 <error.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <signal.h>
36 #include "config.h"
37 #include "test.h"
38 #include "safe_macros.h"
39 #include "linux_syscall_numbers.h"
40
41 #define TEST_CASES 6
42 #ifndef AT_FDCWD
43 #define AT_FDCWD -100
44 #endif
45
46 void setup();
47 void cleanup();
48
49 char *TCID = "fstatat01";
50 int TST_TOTAL = TEST_CASES;
51
52 static const char pathname[] = "fstatattestdir",
53 testfile[] = "fstatattestfile.txt",
54 testfile2[] = "fstatattestdir/fstatattestfile.txt";
55 static char *testfile3;
56
57 static int fds[TEST_CASES];
58 static const char *filenames[TEST_CASES];
59 static const int expected_errno[] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0 };
60 static const int flags[] = { 0, 0, 0, 0, 9999, 0 };
61
62 #if !defined(HAVE_FSTATAT)
63 #if (__NR_fstatat64 > 0)
fstatat(int dirfd,const char * filename,struct stat64 * statbuf,int flags)64 int fstatat(int dirfd, const char *filename, struct stat64 *statbuf, int flags)
65 {
66 return ltp_syscall(__NR_fstatat64, dirfd, filename, statbuf, flags);
67 }
68 #elif (__NR_newfstatat > 0)
fstatat(int dirfd,const char * filename,struct stat * statbuf,int flags)69 int fstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
70 {
71 return ltp_syscall(__NR_newfstatat, dirfd, filename, statbuf, flags);
72 }
73 #else
fstatat(int dirfd,const char * filename,struct stat * statbuf,int flags)74 int fstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
75 {
76 return ltp_syscall(__NR_fstatat, dirfd, filename, statbuf, flags);
77 }
78 #endif
79 #endif
80
main(int ac,char ** av)81 int main(int ac, char **av)
82 {
83 int lc, i;
84 #if !defined(HAVE_FSTATAT) && (__NR_fstatat64 > 0)
85 static struct stat64 statbuf;
86 #else
87 static struct stat statbuf;
88 #endif
89
90 if (tst_kvercmp(2, 6, 16) < 0)
91 tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
92
93 tst_parse_opts(ac, av, NULL, NULL);
94
95 setup();
96
97 for (lc = 0; TEST_LOOPING(lc); lc++) {
98 tst_count = 0;
99
100 for (i = 0; i < TST_TOTAL; i++) {
101 TEST(fstatat(fds[i], filenames[i], &statbuf, flags[i]));
102
103 if (TEST_ERRNO == expected_errno[i]) {
104 tst_resm(TPASS | TTERRNO,
105 "fstatat failed as expected");
106 } else
107 tst_resm(TFAIL | TTERRNO, "fstatat failed");
108 }
109
110 }
111
112 cleanup();
113 tst_exit();
114 }
115
setup(void)116 void setup(void)
117 {
118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
120 tst_tmpdir();
121
122 char *abs_path = tst_get_tmpdir();
123
124 SAFE_ASPRINTF(cleanup, &testfile3, "%s/fstatattestfile3.txt", abs_path);
125 free(abs_path);
126
127 SAFE_MKDIR(cleanup, pathname, 0700);
128
129 fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
130 fds[1] = fds[4] = fds[0];
131
132 SAFE_FILE_PRINTF(cleanup, testfile, testfile);
133 SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
134
135 fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
136
137 fds[3] = 100;
138 fds[5] = AT_FDCWD;
139
140 filenames[0] = filenames[2] = filenames[3] = filenames[4] =
141 filenames[5] = testfile;
142 filenames[1] = testfile3;
143
144 TEST_PAUSE;
145 }
146
cleanup(void)147 void cleanup(void)
148 {
149 if (fds[0] > 0)
150 close(fds[0]);
151 if (fds[2] > 0)
152 close(fds[2]);
153
154 free(testfile3);
155 tst_rmdir();
156 }
157