blob: 33a6b953276e591c5d34433c8742babf246a8b89 [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"
vapier9cf96d82006-09-10 09:54:16 +000057#include "linux_syscall_numbers.h"
vapierd2816c42006-08-24 04:20:05 +000058
59#define TEST_CASES 6
60#ifndef AT_FDCWD
61#define AT_FDCWD -100
62#endif
63void setup();
64void cleanup();
65void setup_every_copy();
66
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020067char *TCID = "fstatat01";
68int TST_TOTAL = TEST_CASES;
vapierd2816c42006-08-24 04:20:05 +000069char pathname[256];
70char testfile[256];
71char testfile2[256];
72char testfile3[256];
73int dirfd, fd, ret;
74int fds[TEST_CASES];
75char *filenames[TEST_CASES];
76int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, EINVAL, 0 };
77int flags[TEST_CASES] = { 0, 0, 0, 0, 9999, 0 };
78
Garrett Cooper15697992010-12-18 06:31:28 -080079/* TODO (garrcoop): properly port to fstatat64. */
subrata_modak72ce96c2008-10-23 08:42:15 +000080#if (defined __NR_fstatat64) && (__NR_fstatat64 != 0)
vapierd2816c42006-08-24 04:20:05 +000081struct stat64 statbuf;
82#else
83struct stat statbuf;
84#endif
85
Garrett Cooper15697992010-12-18 06:31:28 -080086/*
87 * XXX (garrcoop): NO NO NO NO NO NO NO NO NO ... use linux_syscall_numbers.h!
88 */
subrata_modak72ce96c2008-10-23 08:42:15 +000089/* __NR_fstatat64 and __NR_fstatat64 if not defined are ALWAYS stubbed by
90 * linux_syscall_numbers.h Need to check for 0 to avoid testing with stubs */
91#if (defined __NR_fstatat64) && (__NR_fstatat64 != 0)
vapierd2816c42006-08-24 04:20:05 +000092int myfstatat(int dirfd, const char *filename, struct stat64 *statbuf,
93 int flags)
94{
Jan Stancek359980f2013-02-15 10:16:05 +010095 return ltp_syscall(__NR_fstatat64, dirfd, filename, statbuf, flags);
vapierd2816c42006-08-24 04:20:05 +000096}
subrata_modak72ce96c2008-10-23 08:42:15 +000097#elif (defined __NR_newfstatat) && (__NR_newfstatat != 0)
vapierd2816c42006-08-24 04:20:05 +000098int myfstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
99{
Jan Stancek359980f2013-02-15 10:16:05 +0100100 return ltp_syscall(__NR_newfstatat, dirfd, filename, statbuf, flags);
vapierd2816c42006-08-24 04:20:05 +0000101}
subrata_modak4bb656a2009-02-26 12:02:09 +0000102#else
subrata_modak72ce96c2008-10-23 08:42:15 +0000103/* stub - will never run */
104int myfstatat(int dirfd, const char *filename, struct stat *statbuf, int flags)
105{
Jan Stancek359980f2013-02-15 10:16:05 +0100106 return ltp_syscall(0, dirfd, filename, statbuf, flags);
subrata_modak72ce96c2008-10-23 08:42:15 +0000107}
vapierd2816c42006-08-24 04:20:05 +0000108#endif
109
110int main(int ac, char **av)
111{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200112 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200113 const char *msg;
vapierd2816c42006-08-24 04:20:05 +0000114 int i;
115
Garrett Cooper15697992010-12-18 06:31:28 -0800116 if ((tst_kvercmp(2, 6, 16)) < 0)
Garrett Cooper53740502010-12-16 00:04:01 -0800117 tst_brkm(TCONF, NULL,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 "This test can only run on kernels that are 2.6.16 and "
119 "higher");
mreed10807cfe52006-09-18 19:03:19 +0000120
Garrett Cooper45e285d2010-11-22 12:19:25 -0800121 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800122 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapierd2816c42006-08-24 04:20:05 +0000123
vapierd2816c42006-08-24 04:20:05 +0000124 setup();
125
vapierd2816c42006-08-24 04:20:05 +0000126 for (lc = 0; TEST_LOOPING(lc); lc++) {
127 setup_every_copy();
128
Caspar Zhangd59a6592013-03-07 14:59:12 +0800129 tst_count = 0;
vapierd2816c42006-08-24 04:20:05 +0000130
vapierd2816c42006-08-24 04:20:05 +0000131 for (i = 0; i < TST_TOTAL; i++) {
132 TEST(myfstatat
133 (fds[i], filenames[i], &statbuf, flags[i]));
134
vapierd2816c42006-08-24 04:20:05 +0000135 if (TEST_ERRNO == expected_errno[i]) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200136 tst_resm(TPASS | TTERRNO,
137 "fstatat failed as expected");
Garrett Cooper15697992010-12-18 06:31:28 -0800138 } else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 tst_resm(TFAIL | TTERRNO, "fstatat failed");
vapierd2816c42006-08-24 04:20:05 +0000140 }
141
Garrett Cooper2c282152010-12-16 00:55:50 -0800142 }
vapierd2816c42006-08-24 04:20:05 +0000143
vapierd2816c42006-08-24 04:20:05 +0000144 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800145 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800146}
vapierd2816c42006-08-24 04:20:05 +0000147
Mike Frysingerc57fba52014-04-09 18:56:30 -0400148void setup_every_copy(void)
vapierd2816c42006-08-24 04:20:05 +0000149{
150 /* Initialize test dir and file names */
151 sprintf(pathname, "fstatattestdir%d", getpid());
152 sprintf(testfile, "fstatattestfile%d.txt", getpid());
153 sprintf(testfile2, "fstatattestdir%d/fstatattestfile%d.txt", getpid(),
154 getpid());
Garrett Cooper15697992010-12-18 06:31:28 -0800155 /* XXX (garrcoop): WTF NO. tst_tmpdir!!!! */
vapierd2816c42006-08-24 04:20:05 +0000156 sprintf(testfile3, "/tmp/fstatattestfile%d.txt", getpid());
157
158 ret = mkdir(pathname, 0700);
159 if (ret < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800160 perror("mkdir");
161 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000162 }
163
164 dirfd = open(pathname, O_DIRECTORY);
165 if (dirfd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800166 perror("open");
167 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000168 }
169
170 fd = open(testfile, O_CREAT | O_RDWR, 0600);
171 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800172 perror("open");
173 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000174 }
175
176 fd = open(testfile2, O_CREAT | O_RDWR, 0600);
177 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800178 perror("open");
179 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000180 }
181
182 fd = open(testfile3, O_CREAT | O_RDWR, 0600);
183 if (fd < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800184 perror("open");
185 exit(1);
vapierd2816c42006-08-24 04:20:05 +0000186 }
187
188 fds[0] = fds[1] = fds[4] = dirfd;
189 fds[2] = fd;
190 fds[3] = 100;
191 fds[5] = AT_FDCWD;
192
193 filenames[0] = filenames[2] = filenames[3] = filenames[4] =
194 filenames[5] = testfile;
195 filenames[1] = testfile3;
196}
197
Mike Frysingerc57fba52014-04-09 18:56:30 -0400198void setup(void)
vapierd2816c42006-08-24 04:20:05 +0000199{
Garrett Cooper2c282152010-12-16 00:55:50 -0800200
vapierd2816c42006-08-24 04:20:05 +0000201 tst_sig(NOFORK, DEF_HANDLER, cleanup);
202
vapierd2816c42006-08-24 04:20:05 +0000203 TEST_PAUSE;
Garrett Cooper53740502010-12-16 00:04:01 -0800204}
vapierd2816c42006-08-24 04:20:05 +0000205
Mike Frysingerc57fba52014-04-09 18:56:30 -0400206void cleanup(void)
vapierd2816c42006-08-24 04:20:05 +0000207{
vapierd2816c42006-08-24 04:20:05 +0000208 unlink(testfile2);
209 unlink(testfile3);
210 unlink(testfile);
211 rmdir(pathname);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700212}