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 <[email protected]>
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 "lapi/syscalls.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 tst_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 tst_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 tst_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 tst_parse_opts(ac, av, NULL, NULL);
90
91 setup();
92
93 for (lc = 0; TEST_LOOPING(lc); lc++) {
94 tst_count = 0;
95
96 for (i = 0; i < TST_TOTAL; i++) {
97 TEST(fstatat(fds[i], filenames[i], &statbuf, flags[i]));
98
99 if (TEST_ERRNO == expected_errno[i]) {
100 tst_resm(TPASS | TTERRNO,
101 "fstatat failed as expected");
102 } else
103 tst_resm(TFAIL | TTERRNO, "fstatat failed");
104 }
105
106 }
107
108 cleanup();
109 tst_exit();
110 }
111
setup(void)112 void setup(void)
113 {
114 tst_sig(NOFORK, DEF_HANDLER, cleanup);
115
116 tst_tmpdir();
117
118 char *abs_path = tst_get_tmpdir();
119
120 SAFE_ASPRINTF(cleanup, &testfile3, "%s/fstatattestfile3.txt", abs_path);
121 free(abs_path);
122
123 SAFE_MKDIR(cleanup, pathname, 0700);
124
125 fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
126 fds[1] = fds[4] = fds[0];
127
128 SAFE_FILE_PRINTF(cleanup, testfile, testfile);
129 SAFE_FILE_PRINTF(cleanup, testfile2, testfile2);
130
131 fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
132
133 fds[3] = 100;
134 fds[5] = AT_FDCWD;
135
136 filenames[0] = filenames[2] = filenames[3] = filenames[4] =
137 filenames[5] = testfile;
138 filenames[1] = testfile3;
139
140 TEST_PAUSE;
141 }
142
cleanup(void)143 void cleanup(void)
144 {
145 if (fds[0] > 0)
146 close(fds[0]);
147 if (fds[2] > 0)
148 close(fds[2]);
149
150 free(testfile3);
151 tst_rmdir();
152 }
153