1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
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
20/*
21 * Test Description:
22 *  Verify that,
23 *   1. access() fails with -1 return value and sets errno to EACCES
24 *      if the permission bits of the file mode do not permit the
25 *	 requested (Read/Write/Execute) access.
26 *   2. access() fails with -1 return value and sets errno to EINVAL
27 *	if the specified access mode argument is invalid.
28 *   3. access() fails with -1 return value and sets errno to EFAULT
29 *	if the pathname points outside allocate address space for the
30 *	process.
31 *   4. access() fails with -1 return value and sets errno to ENOENT
32 *	if the specified file doesn't exist (or pathname is NULL).
33 *   5. access() fails with -1 return value and sets errno to ENAMETOOLONG
34 *      if the pathname size is > PATH_MAX characters.
35 *   6. access() fails with -1 return value and sets errno to ENOTDIR
36 *      if a component used as a directory in pathname is not a directory.
37 *   7. access() fails with -1 return value and sets errno to ELOOP
38 *      if too many symbolic links were encountered in resolving pathname.
39 *
40 *   07/2001 Ported by Wayne Boyer
41 */
42
43#include <stdio.h>
44#include <errno.h>
45#include <unistd.h>
46#include <fcntl.h>
47#include <string.h>
48#include <signal.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <sys/mman.h>
52#include <pwd.h>
53
54#include "test.h"
55#include "safe_macros.h"
56
57#define INV_OK		-1
58#define TEST_FILE1	"test_file1"
59#define TEST_FILE2	"test_file2"
60#define TEST_FILE3	"test_file3"
61#define TEST_FILE4	"test_file4"
62#define TEST_FILE5	"test_file5/test_file5"
63#define TEST_FILE6	"test_file6"
64
65
66#if !defined(UCLINUX)
67static char high_address_node[64];
68#endif
69
70static char longpathname[PATH_MAX + 2];
71
72static struct test_case_t {
73	char *pathname;
74	int a_mode;
75	int exp_errno;
76} test_cases[] = {
77	{TEST_FILE1, R_OK, EACCES},
78	{TEST_FILE2, W_OK, EACCES},
79	{TEST_FILE3, X_OK, EACCES},
80	{TEST_FILE4, INV_OK, EINVAL},
81#if !defined(UCLINUX)
82	{(char *)-1, R_OK, EFAULT},
83	{high_address_node, R_OK, EFAULT},
84#endif
85	{"", W_OK, ENOENT},
86	{longpathname, R_OK, ENAMETOOLONG},
87	{TEST_FILE5, R_OK, ENOTDIR},
88	{TEST_FILE6, R_OK, ELOOP},
89};
90
91char *TCID = "access05";
92int TST_TOTAL = ARRAY_SIZE(test_cases);
93
94static const char nobody_uid[] = "nobody";
95static struct passwd *ltpuser;
96
97static void setup(void);
98static void access_verify(int i);
99static void cleanup(void);
100
101static char *bad_addr;
102
103int main(int ac, char **av)
104{
105	int lc;
106	int i;
107
108	tst_parse_opts(ac, av, NULL, NULL);
109
110	setup();
111
112	for (lc = 0; TEST_LOOPING(lc); lc++) {
113		tst_count = 0;
114
115		for (i = 0; i < TST_TOTAL; i++)
116			access_verify(i);
117	}
118
119	cleanup();
120	tst_exit();
121}
122
123static void setup(void)
124{
125	int fd;
126
127	tst_sig(NOFORK, DEF_HANDLER, cleanup);
128	tst_require_root();
129
130	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
131	SAFE_SETUID(cleanup, ltpuser->pw_uid);
132	TEST_PAUSE;
133
134#if !defined(UCLINUX)
135	bad_addr = mmap(0, 1, PROT_NONE,
136			MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
137	if (bad_addr == MAP_FAILED)
138		tst_brkm(TBROK | TERRNO, NULL, "mmap failed");
139	test_cases[4].pathname = bad_addr;
140
141	test_cases[5].pathname = get_high_address();
142#endif
143
144	tst_tmpdir();
145
146	/*
147	 * create TEST_FILE1 to test R_OK EACCESS
148	 */
149	fd = SAFE_CREAT(cleanup, TEST_FILE1, 0333);
150	SAFE_CLOSE(cleanup, fd);
151
152	/*
153	 * create TEST_FILE2 to test W_OK EACCESS
154	 */
155	fd = SAFE_CREAT(cleanup, TEST_FILE2, 0555);
156	SAFE_CLOSE(cleanup, fd);
157
158	/*
159	 * create TEST_FILE3 to test X_OK EACCESS
160	 */
161	fd = SAFE_CREAT(cleanup, TEST_FILE3, 0666);
162	SAFE_CLOSE(cleanup, fd);
163
164	/*
165	 * create TEST_FILE4 to test EINVAL
166	 */
167	fd = SAFE_CREAT(cleanup, TEST_FILE4, 0333);
168	SAFE_CLOSE(cleanup, fd);
169
170	/*
171	 *setup to create a node with a name length exceeding
172	 *the MAX length of PATH_MAX.
173	 */
174	memset(longpathname, 'a', sizeof(longpathname) - 1);
175
176	/* create test_file5 for test ENOTDIR. */
177	SAFE_TOUCH(cleanup, "test_file5", 0644, NULL);
178
179	/*
180	 * create two symbolic links who point to each other for
181	 * test ELOOP.
182	 */
183	SAFE_SYMLINK(cleanup, "test_file6", "test_file7");
184	SAFE_SYMLINK(cleanup, "test_file7", "test_file6");
185}
186
187static void access_verify(int i)
188{
189	char *file_name;
190	int access_mode;
191
192	file_name = test_cases[i].pathname;
193	access_mode = test_cases[i].a_mode;
194
195	TEST(access(file_name, access_mode));
196
197	if (TEST_RETURN != -1) {
198		tst_resm(TFAIL, "access(%s, %#o) succeeded unexpectedly",
199			 file_name, access_mode);
200		return;
201	}
202
203	if (TEST_ERRNO == test_cases[i].exp_errno) {
204		tst_resm(TPASS | TTERRNO, "access failed as expected");
205	} else {
206		tst_resm(TFAIL | TTERRNO,
207			 "access failed unexpectedly; expected: "
208			 "%d - %s", test_cases[i].exp_errno,
209			 strerror(test_cases[i].exp_errno));
210	}
211}
212
213static void cleanup(void)
214{
215	tst_rmdir();
216}
217