blob: 45be493e87f4c90fc7b04f6cbfae2c5ccaa22d0c [file] [log] [blame]
vapier073ad122006-09-10 10:06:06 +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
vapier073ad122006-09-10 10:06:06 +000018 *
19 * NAME
20 * faccessat01.c
21 *
22 * DESCRIPTION
23 * This test case will verify basic function of faccessat
24 * added by kernel 2.6.16 or up.
25 *
26 * USAGE: <for command-line>
27 * faccessat01 [-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>
vapier073ad122006-09-10 10:06:06 +000039 *
40 * History
41 * 08/28/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 <fcntl.h>
50#include <unistd.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"
58#include "linux_syscall_numbers.h"
59
subrata_modakbbaeb932008-03-27 12:01:59 +000060#define TEST_CASES 6
vapier073ad122006-09-10 10:06:06 +000061#ifndef AT_FDCWD
62#define AT_FDCWD -100
63#endif
64void setup();
65void cleanup();
66void setup_every_copy();
67
68char *TCID = "faccessat01"; /* Test program identifier. */
69int TST_TOTAL = TEST_CASES; /* Total number of test cases. */
vapier073ad122006-09-10 10:06:06 +000070char pathname[256] = "";
71char testfile[256] = "";
72char testfile2[256] = "";
73char testfile3[256] = "";
74int dirfd, fd, ret;
75int fds[TEST_CASES];
76char *filenames[TEST_CASES];
subrata_modakbbaeb932008-03-27 12:01:59 +000077int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
vapier073ad122006-09-10 10:06:06 +000078
79int myfaccessat(int dirfd, const char *filename, int mode)
80{
Jan Stancek359980f2013-02-15 10:16:05 +010081 return ltp_syscall(__NR_faccessat, dirfd, filename, mode);
vapier073ad122006-09-10 10:06:06 +000082}
83
84int main(int ac, char **av)
85{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020086 int lc;
87 char *msg;
vapier073ad122006-09-10 10:06:06 +000088 int i;
89
subrata_modak56207ce2009-03-23 13:35:39 +000090 /* Disable test if the version of the kernel is less than 2.6.16 */
91 if (((tst_kvercmp(2, 6, 16)) < 0)) {
92 tst_resm(TWARN, "This test can only run on kernels that are ");
93 tst_resm(TWARN, "2.6.16 and higher");
94 exit(0);
95 }
mreed10807cfe52006-09-18 19:03:19 +000096
vapier073ad122006-09-10 10:06:06 +000097 /***************************************************************
98 * parse standard options
99 ***************************************************************/
Garrett Cooper45e285d2010-11-22 12:19:25 -0800100 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800101 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier073ad122006-09-10 10:06:06 +0000102
103 /***************************************************************
104 * perform global setup for test
105 ***************************************************************/
106 setup();
107
108 /***************************************************************
109 * check looping state if -c option given
110 ***************************************************************/
111 for (lc = 0; TEST_LOOPING(lc); lc++) {
112 setup_every_copy();
113
vapier073ad122006-09-10 10:06:06 +0000114 Tst_count = 0;
115
subrata_modak4bb656a2009-02-26 12:02:09 +0000116 /*
117 * Call faccessat
vapier073ad122006-09-10 10:06:06 +0000118 */
119 for (i = 0; i < TST_TOTAL; i++) {
120 TEST(myfaccessat(fds[i], filenames[i], R_OK));
121
122 /* check return code */
123 if (TEST_ERRNO == expected_errno[i]) {
124
125 /***************************************************************
126 * only perform functional verification if flag set (-f not given)
127 ***************************************************************/
128 if (STD_FUNCTIONAL_TEST) {
129 /* No Verification test, yet... */
130 tst_resm(TPASS,
131 "faccessat() returned the expected errno %d: %s",
132 TEST_ERRNO,
133 strerror(TEST_ERRNO));
134 }
135 } else {
136 TEST_ERROR_LOG(TEST_ERRNO);
137 tst_resm(TFAIL,
138 "faccessdat() Failed, errno=%d : %s",
139 TEST_ERRNO, strerror(TEST_ERRNO));
140 }
141 }
142
Garrett Cooper2c282152010-12-16 00:55:50 -0800143 }
vapier073ad122006-09-10 10:06:06 +0000144
145 /***************************************************************
146 * cleanup and exit
147 ***************************************************************/
148 cleanup();
149
150 return (0);
Garrett Cooper2c282152010-12-16 00:55:50 -0800151}
vapier073ad122006-09-10 10:06:06 +0000152
153void setup_every_copy()
154{
155 /* Initialize test dir and file names */
156 sprintf(pathname, "faccessattestdir%d", getpid());
157 sprintf(testfile, "faccessattestfile%d.txt", getpid());
158 sprintf(testfile2, "/tmp/faccessattestfile%d.txt", getpid());
159 sprintf(testfile3, "faccessattestdir%d/faccessattestfile%d.txt",
160 getpid(), getpid());
161
162 ret = mkdir(pathname, 0700);
163 if (ret < 0) {
164 perror("mkdir: ");
165 exit(-1);
166 }
167
168 dirfd = open(pathname, O_DIRECTORY);
169 if (dirfd < 0) {
170 perror("open: ");
171 exit(-1);
172 }
173
174 fd = open(testfile, O_CREAT | O_RDWR, 0600);
175 if (fd < 0) {
176 perror("open: ");
177 exit(-1);
178 }
179
180 fd = open(testfile2, O_CREAT | O_RDWR, 0600);
181 if (fd < 0) {
182 perror("open: ");
183 exit(-1);
184 }
185
186 fd = open(testfile3, O_CREAT | O_RDWR, 0600);
187 if (fd < 0) {
188 perror("open: ");
189 exit(-1);
190 }
191
192 fds[0] = fds[1] = fds[4] = dirfd;
193 fds[2] = fd;
194 fds[3] = 100;
subrata_modakef370272008-04-14 13:55:47 +0000195 fds[5] = AT_FDCWD;
vapier073ad122006-09-10 10:06:06 +0000196
197 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
198 filenames[1] = testfile2;
subrata_modakbbaeb932008-03-27 12:01:59 +0000199 filenames[5] = testfile3;
vapier073ad122006-09-10 10:06:06 +0000200}
201
202/***************************************************************
203 * setup() - performs all ONE TIME setup for this test.
204 ***************************************************************/
205void setup()
206{
Garrett Cooper2c282152010-12-16 00:55:50 -0800207
vapier073ad122006-09-10 10:06:06 +0000208 tst_sig(NOFORK, DEF_HANDLER, cleanup);
209
vapier073ad122006-09-10 10:06:06 +0000210 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800211}
vapier073ad122006-09-10 10:06:06 +0000212
213/***************************************************************
214 * cleanup() - performs all ONE TIME cleanup for this test at
215 * completion or premature exit.
216 ***************************************************************/
217void cleanup()
218{
219 /* Remove them */
subrata_modak56207ce2009-03-23 13:35:39 +0000220 close(fd);
vapier073ad122006-09-10 10:06:06 +0000221 unlink(testfile);
222 unlink(testfile2);
223 unlink(testfile3);
224 rmdir(pathname);
225
226 /*
227 * print timing stats if that option was specified.
228 * print errno log if that option was specified.
229 */
230 TEST_CLEANUP;
231
Chris Dearmanec6edca2012-10-17 19:54:01 -0700232}