blob: 485d96bdfb313f8f8c2ee9ade7d7a221ee34acb9 [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 *
vapier073ad122006-09-10 10:06:06 +000026 * Author
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * Yi Yang <yyangcdl@cn.ibm.com>
vapier073ad122006-09-10 10:06:06 +000028 *
29 * History
30 * 08/28/2006 Created first by Yi Yang <yyangcdl@cn.ibm.com>
31 *
32 *****************************************************************************/
33
34#define _GNU_SOURCE
35
36#include <sys/types.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <error.h>
41#include <stdlib.h>
42#include <errno.h>
43#include <string.h>
44#include <signal.h>
45#include "test.h"
vapier073ad122006-09-10 10:06:06 +000046#include "linux_syscall_numbers.h"
47
subrata_modakbbaeb932008-03-27 12:01:59 +000048#define TEST_CASES 6
vapier073ad122006-09-10 10:06:06 +000049#ifndef AT_FDCWD
50#define AT_FDCWD -100
51#endif
52void setup();
53void cleanup();
54void setup_every_copy();
55
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020056char *TCID = "faccessat01";
57int TST_TOTAL = TEST_CASES;
vapier073ad122006-09-10 10:06:06 +000058char pathname[256] = "";
59char testfile[256] = "";
60char testfile2[256] = "";
61char testfile3[256] = "";
62int dirfd, fd, ret;
63int fds[TEST_CASES];
64char *filenames[TEST_CASES];
subrata_modakbbaeb932008-03-27 12:01:59 +000065int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
vapier073ad122006-09-10 10:06:06 +000066
67int myfaccessat(int dirfd, const char *filename, int mode)
68{
Jan Stancek359980f2013-02-15 10:16:05 +010069 return ltp_syscall(__NR_faccessat, dirfd, filename, mode);
vapier073ad122006-09-10 10:06:06 +000070}
71
72int main(int ac, char **av)
73{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020074 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020075 const char *msg;
vapier073ad122006-09-10 10:06:06 +000076 int i;
77
subrata_modak56207ce2009-03-23 13:35:39 +000078 /* Disable test if the version of the kernel is less than 2.6.16 */
79 if (((tst_kvercmp(2, 6, 16)) < 0)) {
80 tst_resm(TWARN, "This test can only run on kernels that are ");
81 tst_resm(TWARN, "2.6.16 and higher");
82 exit(0);
83 }
mreed10807cfe52006-09-18 19:03:19 +000084
Garrett Cooper45e285d2010-11-22 12:19:25 -080085 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080086 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier073ad122006-09-10 10:06:06 +000087
vapier073ad122006-09-10 10:06:06 +000088 setup();
89
vapier073ad122006-09-10 10:06:06 +000090 for (lc = 0; TEST_LOOPING(lc); lc++) {
91 setup_every_copy();
92
Caspar Zhangd59a6592013-03-07 14:59:12 +080093 tst_count = 0;
vapier073ad122006-09-10 10:06:06 +000094
subrata_modak4bb656a2009-02-26 12:02:09 +000095 /*
96 * Call faccessat
vapier073ad122006-09-10 10:06:06 +000097 */
98 for (i = 0; i < TST_TOTAL; i++) {
99 TEST(myfaccessat(fds[i], filenames[i], R_OK));
100
101 /* check return code */
102 if (TEST_ERRNO == expected_errno[i]) {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 tst_resm(TPASS,
104 "faccessat() returned the expected errno %d: %s",
105 TEST_ERRNO,
106 strerror(TEST_ERRNO));
vapier073ad122006-09-10 10:06:06 +0000107 } else {
vapier073ad122006-09-10 10:06:06 +0000108 tst_resm(TFAIL,
109 "faccessdat() Failed, errno=%d : %s",
110 TEST_ERRNO, strerror(TEST_ERRNO));
111 }
112 }
113
Garrett Cooper2c282152010-12-16 00:55:50 -0800114 }
vapier073ad122006-09-10 10:06:06 +0000115
vapier073ad122006-09-10 10:06:06 +0000116 cleanup();
Cyril Hrubis4dff8542014-09-23 12:11:59 +0200117 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800118}
vapier073ad122006-09-10 10:06:06 +0000119
Mike Frysingerc57fba52014-04-09 18:56:30 -0400120void setup_every_copy(void)
vapier073ad122006-09-10 10:06:06 +0000121{
122 /* Initialize test dir and file names */
123 sprintf(pathname, "faccessattestdir%d", getpid());
124 sprintf(testfile, "faccessattestfile%d.txt", getpid());
125 sprintf(testfile2, "/tmp/faccessattestfile%d.txt", getpid());
126 sprintf(testfile3, "faccessattestdir%d/faccessattestfile%d.txt",
127 getpid(), getpid());
128
129 ret = mkdir(pathname, 0700);
130 if (ret < 0) {
131 perror("mkdir: ");
132 exit(-1);
133 }
134
135 dirfd = open(pathname, O_DIRECTORY);
136 if (dirfd < 0) {
137 perror("open: ");
138 exit(-1);
139 }
140
141 fd = open(testfile, O_CREAT | O_RDWR, 0600);
142 if (fd < 0) {
143 perror("open: ");
144 exit(-1);
145 }
146
147 fd = open(testfile2, O_CREAT | O_RDWR, 0600);
148 if (fd < 0) {
149 perror("open: ");
150 exit(-1);
151 }
152
153 fd = open(testfile3, O_CREAT | O_RDWR, 0600);
154 if (fd < 0) {
155 perror("open: ");
156 exit(-1);
157 }
158
159 fds[0] = fds[1] = fds[4] = dirfd;
160 fds[2] = fd;
161 fds[3] = 100;
subrata_modakef370272008-04-14 13:55:47 +0000162 fds[5] = AT_FDCWD;
vapier073ad122006-09-10 10:06:06 +0000163
164 filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
165 filenames[1] = testfile2;
subrata_modakbbaeb932008-03-27 12:01:59 +0000166 filenames[5] = testfile3;
vapier073ad122006-09-10 10:06:06 +0000167}
168
Mike Frysingerc57fba52014-04-09 18:56:30 -0400169void setup(void)
vapier073ad122006-09-10 10:06:06 +0000170{
Garrett Cooper2c282152010-12-16 00:55:50 -0800171
vapier073ad122006-09-10 10:06:06 +0000172 tst_sig(NOFORK, DEF_HANDLER, cleanup);
173
vapier073ad122006-09-10 10:06:06 +0000174 TEST_PAUSE;
Garrett Cooper2c282152010-12-16 00:55:50 -0800175}
vapier073ad122006-09-10 10:06:06 +0000176
Mike Frysingerc57fba52014-04-09 18:56:30 -0400177void cleanup(void)
vapier073ad122006-09-10 10:06:06 +0000178{
subrata_modak56207ce2009-03-23 13:35:39 +0000179 close(fd);
vapier073ad122006-09-10 10:06:06 +0000180 unlink(testfile);
181 unlink(testfile2);
182 unlink(testfile3);
183 rmdir(pathname);
Chris Dearmanec6edca2012-10-17 19:54:01 -0700184}