blob: 7fea942cac27c4ac91e9b9759fc6ceac717e8600 [file] [log] [blame]
vapierd2816c42006-08-24 04:20:05 +00001/******************************************************************************
2 *
3 * Copyright (c) International Business Machines Corp., 2006
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the 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, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
vapierd2816c42006-08-24 04:20:05 +000018 *
19 * NAME
20 * fstatat01.c
21 *
22 * DESCRIPTION
23 * This test case will verify basic function of fstatat64/newfstatat
24 * added by kernel 2.6.16 or up.
25 *
26 * USAGE: <for command-line>
27 * fstatat01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] [-p]
28 * where:
29 * -c n : Run n copies simultaneously.
30 * -e : Turn on errno logging.
31 * -i n : Execute test n times.
32 * -I x : Execute test for x seconds.
33 * -p : Pause for SIGUSR1 before starting
34 * -P x : Pause for x seconds between iterations.
35 * -t : Turn on syscall timing.
36 *
37 * Author
subrata_modak4bb656a2009-02-26 12:02:09 +000038 * Yi Yang <yyangcdl@cn.ibm.com>
vapierd2816c42006-08-24 04:20:05 +000039 *
40 * History
41 * 08/24/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com>
42 *
43 *****************************************************************************/
44
45#define _GNU_SOURCE
46
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <fcntl.h>
51#include <error.h>
52#include <stdlib.h>
53#include <errno.h>
54#include <string.h>
55#include <signal.h>
56#include "test.h"
57#include "usctest.h"
vapier9cf96d82006-09-10 09:54:16 +000058#include "linux_syscall_numbers.h"
vapierd2816c42006-08-24 04:20:05 +000059
60#define TEST_CASES 6
61#ifndef AT_FDCWD
62#define AT_FDCWD -100
63#endif
64void setup();
65void cleanup();
66void setup_every_copy();
67
68char *TCID = "fstatat01"; /* Test program identifier. */
69int TST_TOTAL = TEST_CASES; /* Total number of test cases. */
vapierd2816c42006-08-24 04:20:05 +000070char pathname[256];
71char testfile[256];
72char testfile2[256];
73char testfile3[256];
74int dirfd, fd, ret;
75int fds[TEST_CASES];
76char *filenames[TEST_CASES];
77int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0 };
78int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0 };
79
Garrett Cooper15697992010-12-18 06:31:28 -080080/* TODO (garrcoop): properly port to fstatat64. */
subrata_modak72ce96c2008-10-23 08:42:15 +000081#if (defined __NR_fstatat64) && (__NR_fstatat64 != 0)
vapierd2816c42006-08-24 04:20:05 +000082struct stat64 statbuf;
83#else
84struct stat statbuf;
85#endif
86
Garrett Cooper15697992010-12-18 06:31:28 -080087/*
88 * XXX (garrcoop): NO NO NO NO NO NO NO NO NO ... use linux_syscall_numbers.h!
89 */
subrata_modak72ce96c2008-10-23 08:42:15 +000090/* __NR_fstatat64 and __NR_fstatat64 if not defined are ALWAYS stubbed by
91 * linux_syscall_numbers.h Need to check for 0 to avoid testing with stubs */
92#if (defined __NR_fstatat64) && (__NR_fstatat64 != 0)
vapierd2816c42006-08-24 04:20:05 +000093int myfstatat(int dirfd, const char *filename, struct stat64 *statbuf,
94 int flags)
95{
Jan Stancek359980f2013-02-15 10:16:05 +010096 return ltp_syscall(__NR_fstatat64, dirfd, filename, statbuf, flags);
vapierd2816c42006-08-24 04:20:05 +000097}
subrata_modak72ce96c2008-10-23 08:42:15 +000098#elif (defined __NR_newfstatat) && (__NR_newfstatat != 0)
vapierd2816c42006-08-24 04:20:05 +000099int myfstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
100{
Jan Stancek359980f2013-02-15 10:16:05 +0100101 return ltp_syscall(__NR_newfstatat, dirfd, filename, statbuf, flags);
vapierd2816c42006-08-24 04:20:05 +0000102}
subrata_modak4bb656a2009-02-26 12:02:09 +0000103#else
subrata_modak72ce96c2008-10-23 08:42:15 +0000104/* stub - will never run */
105int myfstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
106{
Jan Stancek359980f2013-02-15 10:16:05 +0100107 return ltp_syscall(0, dirfd, filename, statbuf, flags);
subrata_modak72ce96c2008-10-23 08:42:15 +0000108}
vapierd2816c42006-08-24 04:20:05 +0000109#endif
110
111int main(int ac, char **av)
112{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200113 int lc;
114 char *msg;
vapierd2816c42006-08-24 04:20:05 +0000115 int i;
116
Garrett Cooper15697992010-12-18 06:31:28 -0800117 if ((tst_kvercmp(2, 6, 16)) < 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800118 tst_brkm(TCONF, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800119 "This test can only run on kernels that are 2.6.16 and "
120 "higher");
mreed10807cfe52006-09-18 19:03:19 +0000121
Garrett Cooper45e285d2010-11-22 12:19:25 -0800122 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800123 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd2816c42006-08-24 04:20:05 +0000124
vapierd2816c42006-08-24 04:20:05 +0000125 setup();
126
vapierd2816c42006-08-24 04:20:05 +0000127 for (lc = 0; TEST_LOOPING(lc); lc++) {
128 setup_every_copy();
129
vapierd2816c42006-08-24 04:20:05 +0000130 Tst_count = 0;
131
vapierd2816c42006-08-24 04:20:05 +0000132 for (i = 0; i < TST_TOTAL; i++) {
133 TEST(myfstatat
134 (fds[i], filenames[i], &statbuf, flags[i]));
135
vapierd2816c42006-08-24 04:20:05 +0000136 if (TEST_ERRNO == expected_errno[i]) {
137
Garrett Cooper15697992010-12-18 06:31:28 -0800138 if (STD_FUNCTIONAL_TEST)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TPASS | TTERRNO,
Garrett Cooper15697992010-12-18 06:31:28 -0800140 "fstatat failed as expected");
141 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 tst_resm(TFAIL | TTERRNO, "fstatat failed");
vapierd2816c42006-08-24 04:20:05 +0000143 }
144
Garrett Cooper2c282152010-12-16 00:55:50 -0800145 }
vapierd2816c42006-08-24 04:20:05 +0000146
vapierd2816c42006-08-24 04:20:05 +0000147 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800148 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800149}
vapierd2816c42006-08-24 04:20:05 +0000150
151void setup_every_copy()
152{
153 /* Initialize test dir and file names */
154 sprintf(pathname, "fstatattestdir%d", getpid());
155 sprintf(testfile, "fstatattestfile%d.txt", getpid());
156 sprintf(testfile2, "fstatattestdir%d/fstatattestfile%d.txt", getpid(),
157 getpid());
Garrett Cooper15697992010-12-18 06:31:28 -0800158 /* XXX (garrcoop): WTF NO. tst_tmpdir!!!! */
vapierd2816c42006-08-24 04:20:05 +0000159 sprintf(testfile3, "/tmp/fstatattestfile%d.txt", getpid());
160
161 ret = mkdir(pathname, 0700);
162 if (ret < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800163 perror("mkdir");
164 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000165 }
166
167 dirfd = open(pathname, O_DIRECTORY);
168 if (dirfd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800169 perror("open");
170 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000171 }
172
173 fd = open(testfile, O_CREAT | O_RDWR, 0600);
174 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800175 perror("open");
176 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000177 }
178
179 fd = open(testfile2, O_CREAT | O_RDWR, 0600);
180 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800181 perror("open");
182 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000183 }
184
185 fd = open(testfile3, O_CREAT | O_RDWR, 0600);
186 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800187 perror("open");
188 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000189 }
190
191 fds[0] = fds[1] = fds[4] = dirfd;
192 fds[2] = fd;
193 fds[3] = 100;
194 fds[5] = AT_FDCWD;
195
196 filenames[0] = filenames[2] = filenames[3] = filenames[4] =
197 filenames[5] = testfile;
198 filenames[1] = testfile3;
199}
200
vapierd2816c42006-08-24 04:20:05 +0000201void setup()
202{
Garrett Cooper2c282152010-12-16 00:55:50 -0800203
vapierd2816c42006-08-24 04:20:05 +0000204 tst_sig(NOFORK, DEF_HANDLER, cleanup);
205
vapierd2816c42006-08-24 04:20:05 +0000206 TEST_PAUSE;
Garrett Cooper53740502010-12-16 00:04:01 -0800207}
vapierd2816c42006-08-24 04:20:05 +0000208
vapierd2816c42006-08-24 04:20:05 +0000209void cleanup()
210{
vapierd2816c42006-08-24 04:20:05 +0000211 unlink(testfile2);
212 unlink(testfile3);
213 unlink(testfile);
214 rmdir(pathname);
215
vapierd2816c42006-08-24 04:20:05 +0000216 TEST_CLEANUP;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700217}