plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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. |
Wang, Xiaoguang/王 小光 | c7b23c2 | 2013-11-05 13:51:47 +0800 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | * |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 40 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 51 | #include <sys/mman.h> |
robbiew | 73c2db7 | 2001-09-05 16:49:27 +0000 | [diff] [blame] | 52 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
| 54 | #include "test.h" |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 55 | #include "safe_macros.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
Wang, Xiaoguang/王 小光 | c7b23c2 | 2013-11-05 13:51:47 +0800 | [diff] [blame] | 62 | #define TEST_FILE5 "test_file5/test_file5" |
| 63 | #define TEST_FILE6 "test_file6" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 66 | #if !defined(UCLINUX) |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 67 | static char high_address_node[64]; |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 68 | #endif |
robbiew | 15226cd | 2002-04-10 16:10:40 +0000 | [diff] [blame] | 69 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 70 | static char longpathname[PATH_MAX + 2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 72 | static struct test_case_t { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | char *pathname; |
| 74 | int a_mode; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | int exp_errno; |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 76 | } test_cases[] = { |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 77 | {TEST_FILE1, R_OK, EACCES}, |
| 78 | {TEST_FILE2, W_OK, EACCES}, |
| 79 | {TEST_FILE3, X_OK, EACCES}, |
| 80 | {TEST_FILE4, INV_OK, EINVAL}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 81 | #if !defined(UCLINUX) |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 82 | {(char *)-1, R_OK, EFAULT}, |
| 83 | {high_address_node, R_OK, EFAULT}, |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 84 | #endif |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 85 | {"", W_OK, ENOENT}, |
| 86 | {longpathname, R_OK, ENAMETOOLONG}, |
Wang, Xiaoguang/王 小光 | c7b23c2 | 2013-11-05 13:51:47 +0800 | [diff] [blame] | 87 | {TEST_FILE5, R_OK, ENOTDIR}, |
| 88 | {TEST_FILE6, R_OK, ELOOP}, |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 89 | }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 91 | char *TCID = "access05"; |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 92 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 94 | static const char nobody_uid[] = "nobody"; |
| 95 | static struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 97 | static void setup(void); |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 98 | static void access_verify(int i); |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 99 | static void cleanup(void); |
| 100 | |
| 101 | static char *bad_addr; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 105 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 106 | const char *msg; |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 107 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 109 | msg = parse_opts(ac, av, NULL, NULL); |
| 110 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 111 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | setup(); |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 116 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 118 | for (i = 0; i < TST_TOTAL; i++) |
| 119 | access_verify(i); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | cleanup(); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 123 | tst_exit(); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 126 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | { |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 128 | int fd; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 131 | tst_require_root(NULL); |
| 132 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 133 | ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid); |
| 134 | SAFE_SETUID(cleanup, ltpuser->pw_uid); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | TEST_PAUSE; |
| 136 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 137 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 138 | bad_addr = mmap(0, 1, PROT_NONE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 139 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 140 | if (bad_addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_brkm(TBROK | TERRNO, NULL, "mmap failed"); |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 142 | test_cases[4].pathname = bad_addr; |
| 143 | |
| 144 | test_cases[5].pathname = get_high_address(); |
mreed10 | 81534c3 | 2006-08-03 05:21:24 +0000 | [diff] [blame] | 145 | #endif |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 146 | |
Garrett Cooper | 187e27c | 2010-12-17 13:30:26 -0800 | [diff] [blame] | 147 | tst_tmpdir(); |
| 148 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 149 | /* |
| 150 | * create TEST_FILE1 to test R_OK EACCESS |
| 151 | */ |
| 152 | fd = SAFE_CREAT(cleanup, TEST_FILE1, 0333); |
| 153 | SAFE_CLOSE(cleanup, fd); |
| 154 | |
| 155 | /* |
| 156 | * create TEST_FILE2 to test W_OK EACCESS |
| 157 | */ |
| 158 | fd = SAFE_CREAT(cleanup, TEST_FILE2, 0555); |
| 159 | SAFE_CLOSE(cleanup, fd); |
| 160 | |
| 161 | /* |
| 162 | * create TEST_FILE3 to test X_OK EACCESS |
| 163 | */ |
| 164 | fd = SAFE_CREAT(cleanup, TEST_FILE3, 0666); |
| 165 | SAFE_CLOSE(cleanup, fd); |
| 166 | |
| 167 | /* |
| 168 | * create TEST_FILE4 to test EINVAL |
| 169 | */ |
| 170 | fd = SAFE_CREAT(cleanup, TEST_FILE4, 0333); |
| 171 | SAFE_CLOSE(cleanup, fd); |
| 172 | |
| 173 | /* |
| 174 | *setup to create a node with a name length exceeding |
| 175 | *the MAX length of PATH_MAX. |
| 176 | */ |
| 177 | memset(longpathname, 'a', sizeof(longpathname) - 1); |
Wang, Xiaoguang/王 小光 | c7b23c2 | 2013-11-05 13:51:47 +0800 | [diff] [blame] | 178 | |
| 179 | /* create test_file5 for test ENOTDIR. */ |
| 180 | SAFE_TOUCH(cleanup, "test_file5", 0644, NULL); |
| 181 | |
| 182 | /* |
| 183 | * create two symbolic links who point to each other for |
| 184 | * test ELOOP. |
| 185 | */ |
| 186 | SAFE_SYMLINK(cleanup, "test_file6", "test_file7"); |
| 187 | SAFE_SYMLINK(cleanup, "test_file7", "test_file6"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 190 | static void access_verify(int i) |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 191 | { |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 192 | char *file_name; |
| 193 | int access_mode; |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 194 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 195 | file_name = test_cases[i].pathname; |
| 196 | access_mode = test_cases[i].a_mode; |
vapier | 6f55017 | 2009-08-28 10:46:41 +0000 | [diff] [blame] | 197 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 198 | TEST(access(file_name, access_mode)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 200 | if (TEST_RETURN != -1) { |
| 201 | tst_resm(TFAIL, "access(%s, %#o) succeeded unexpectedly", |
| 202 | file_name, access_mode); |
| 203 | return; |
| 204 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | |
Wang, Xiaoguang | 49e5868 | 2013-10-21 16:30:16 +0800 | [diff] [blame] | 206 | if (TEST_ERRNO == test_cases[i].exp_errno) { |
| 207 | tst_resm(TPASS | TTERRNO, "access failed as expected"); |
| 208 | } else { |
| 209 | tst_resm(TFAIL | TTERRNO, |
| 210 | "access failed unexpectedly; expected: " |
| 211 | "%d - %s", test_cases[i].exp_errno, |
| 212 | strerror(test_cases[i].exp_errno)); |
| 213 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Wanlong Gao | b3e9e5e | 2013-04-26 14:55:25 +0800 | [diff] [blame] | 216 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 219 | } |