• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
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
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
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  * Author
27  *	Yi Yang <yyangcdl@cn.ibm.com>
28  *
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 <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <signal.h>
44 #include "test.h"
45 #include "safe_macros.h"
46 #include "lapi/syscalls.h"
47 
48 #define TEST_CASES 6
49 #ifndef AT_FDCWD
50 #define AT_FDCWD -100
51 #endif
52 void setup();
53 void cleanup();
54 
55 char *TCID = "faccessat01";
56 int TST_TOTAL = TEST_CASES;
57 static char pathname[256];
58 static char testfile[256];
59 static char testfile2[256];
60 static char testfile3[256];
61 static int fds[TEST_CASES];
62 static char *filenames[TEST_CASES];
63 static int expected_errno[TEST_CASES] = { 0, 0, ENOTDIR, EBADF, 0, 0 };
64 
myfaccessat(int dirfd,const char * filename,int mode)65 int myfaccessat(int dirfd, const char *filename, int mode)
66 {
67 	return ltp_syscall(__NR_faccessat, dirfd, filename, mode);
68 }
69 
main(int ac,char ** av)70 int main(int ac, char **av)
71 {
72 	int lc;
73 	int i;
74 
75 	/* Disable test if the version of the kernel is less than 2.6.16 */
76 	if ((tst_kvercmp(2, 6, 16)) < 0)
77 		tst_brkm(TCONF, NULL, "Test must be run with kernel 2.6.16+");
78 
79 	tst_parse_opts(ac, av, NULL, NULL);
80 
81 	setup();
82 
83 	for (lc = 0; TEST_LOOPING(lc); lc++) {
84 		tst_count = 0;
85 
86 		/*
87 		 * Call faccessat
88 		 */
89 		for (i = 0; i < TST_TOTAL; i++) {
90 			TEST(myfaccessat(fds[i], filenames[i], R_OK));
91 
92 			/* check return code */
93 			if (TEST_ERRNO == expected_errno[i]) {
94 				tst_resm(TPASS,
95 					 "faccessat() returned the expected  errno %d: %s",
96 					 TEST_ERRNO,
97 					 strerror(TEST_ERRNO));
98 			} else {
99 				tst_resm(TFAIL,
100 					 "faccessdat() Failed, errno=%d : %s",
101 					 TEST_ERRNO, strerror(TEST_ERRNO));
102 			}
103 		}
104 	}
105 
106 	cleanup();
107 	tst_exit();
108 }
109 
setup(void)110 void setup(void)
111 {
112 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
113 
114 	tst_tmpdir();
115 
116 	char *abs_path = tst_get_tmpdir();
117 	int p = getpid();
118 
119 	/* Initialize test dir and file names */
120 	sprintf(pathname, "faccessattestdir%d", p);
121 	sprintf(testfile, "faccessattestfile%d.txt", p);
122 	sprintf(testfile2, "%s/faccessattestfile%d.txt", abs_path, p);
123 	sprintf(testfile3, "faccessattestdir%d/faccessattestfile%d.txt", p, p);
124 
125 	free(abs_path);
126 
127 	SAFE_MKDIR(cleanup, pathname, 0700);
128 
129 	fds[0] = SAFE_OPEN(cleanup, pathname, O_DIRECTORY);
130 	fds[1] = fds[4] = fds[0];
131 
132 	SAFE_FILE_PRINTF(cleanup, testfile, "%s", testfile);
133 	SAFE_FILE_PRINTF(cleanup, testfile2, "%s", testfile2);
134 
135 	fds[2] = SAFE_OPEN(cleanup, testfile3, O_CREAT | O_RDWR, 0600);
136 
137 	fds[3] = 100;
138 	fds[5] = AT_FDCWD;
139 
140 	filenames[0] = filenames[2] = filenames[3] = filenames[4] = testfile;
141 	filenames[1] = testfile2;
142 	filenames[5] = testfile3;
143 
144 	TEST_PAUSE;
145 }
146 
cleanup(void)147 void cleanup(void)
148 {
149 	if (fds[0] > 0)
150 		close(fds[0]);
151 	if (fds[2] > 0)
152 		close(fds[2]);
153 
154 	tst_rmdir();
155 }
156