plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 3 | * written by Wayne Boyer |
| 4 | * Copyright (c) 2013 Markos Chandras |
| 5 | * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 6 | * |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 11 | * |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU General Public License for more details. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 16 | * |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <errno.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | #include "test.h" |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 29 | #include "safe_macros.h" |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 30 | #include "getdents.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 32 | static void cleanup(void); |
| 33 | static void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 35 | static void reset_flags(void); |
| 36 | static void check_flags(void); |
| 37 | static void set_flag(const char *name); |
| 38 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | char *TCID = "getdents01"; |
| 40 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 42 | static int longsyscall; |
| 43 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 44 | static option_t options[] = { |
| 45 | /* -l long option. Tests getdents64 */ |
| 46 | {"l", &longsyscall, NULL}, |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 47 | {NULL, NULL, NULL} |
| 48 | }; |
| 49 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 50 | static void help(void) |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 51 | { |
| 52 | printf(" -l Test the getdents64 system call\n"); |
| 53 | } |
| 54 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 55 | enum entry_type { |
| 56 | ENTRY_DIR, |
| 57 | ENTRY_FILE, |
| 58 | ENTRY_SYMLINK, |
| 59 | }; |
| 60 | |
| 61 | struct testcase { |
| 62 | const char *name; |
| 63 | enum entry_type type; |
| 64 | int create:1; |
| 65 | int found:1; |
| 66 | }; |
| 67 | |
| 68 | struct testcase testcases[] = { |
| 69 | {.name = ".", .create = 0, .type = ENTRY_DIR}, |
| 70 | {.name = "..", .create = 0, .type = ENTRY_DIR}, |
| 71 | {.name = "dir", .create = 1, .type = ENTRY_DIR}, |
| 72 | {.name = "file", .create = 1, .type = ENTRY_FILE}, |
| 73 | {.name = "symlink", .create = 1, .type = ENTRY_SYMLINK}, |
| 74 | }; |
| 75 | |
| 76 | /* |
| 77 | * Big enough for dirp entires + data, the current size returned |
| 78 | * by kernel is 128 bytes. |
| 79 | */ |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 80 | #define BUFSIZE 512 |
| 81 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 82 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | { |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 84 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 85 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | int rval, fd; |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 87 | struct linux_dirent64 *dirp64; |
| 88 | struct linux_dirent *dirp; |
| 89 | void *buf; |
| 90 | |
| 91 | /* The buffer is allocated to make sure it's suitably aligned */ |
| 92 | buf = malloc(BUFSIZE); |
| 93 | |
| 94 | if (buf == NULL) |
| 95 | tst_brkm(TBROK, NULL, "malloc failed"); |
| 96 | |
| 97 | dirp64 = buf; |
| 98 | dirp = buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 100 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 103 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 106 | const char *d_name; |
| 107 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 108 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 110 | if ((fd = open(".", O_RDONLY)) == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | tst_brkm(TBROK, cleanup, "open of directory failed"); |
robbiew | 6eaecb2 | 2005-12-22 20:18:22 +0000 | [diff] [blame] | 112 | |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 113 | if (longsyscall) |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 114 | rval = getdents64(fd, dirp64, BUFSIZE); |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 115 | else |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 116 | rval = getdents(fd, dirp, BUFSIZE); |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 117 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 118 | if (rval < 0) { |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 119 | if (errno == ENOSYS) |
| 120 | tst_resm(TCONF, "syscall not implemented"); |
| 121 | else |
| 122 | tst_resm(TFAIL | TERRNO, |
| 123 | "getdents failed unexpectedly"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | continue; |
| 125 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 126 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 127 | if (rval == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | tst_resm(TFAIL, |
| 129 | "getdents failed - returned end of directory"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | continue; |
| 131 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 132 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 133 | reset_flags(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 135 | do { |
| 136 | size_t d_reclen; |
| 137 | |
| 138 | if (longsyscall) { |
| 139 | d_reclen = dirp64->d_reclen; |
| 140 | d_name = dirp64->d_name; |
| 141 | } else { |
| 142 | d_reclen = dirp->d_reclen; |
| 143 | d_name = dirp->d_name; |
| 144 | } |
| 145 | |
| 146 | set_flag(d_name); |
| 147 | |
| 148 | tst_resm(TINFO, "Found '%s'", d_name); |
| 149 | |
| 150 | rval -= d_reclen; |
| 151 | |
| 152 | if (longsyscall) |
| 153 | dirp64 = (void*)dirp64 + d_reclen; |
| 154 | else |
| 155 | dirp = (void*)dirp + d_reclen; |
| 156 | |
| 157 | } while (rval > 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 159 | if (close(fd) == -1) |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 160 | tst_brkm(TBROK | TERRNO, cleanup, "Directory close failed"); |
| 161 | |
| 162 | check_flags(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 165 | free(buf); |
| 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 168 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 171 | static void reset_flags(void) |
| 172 | { |
| 173 | int i; |
| 174 | |
| 175 | for (i = 0; i < ARRAY_SIZE(testcases); i++) |
| 176 | testcases[i].found = 0; |
| 177 | } |
| 178 | |
| 179 | static void check_flags(void) |
| 180 | { |
| 181 | int i, err = 0; |
| 182 | |
| 183 | for (i = 0; i < ARRAY_SIZE(testcases); i++) { |
| 184 | if (!testcases[i].found) { |
| 185 | tst_resm(TINFO, "Entry '%s' not found", testcases[i].name); |
| 186 | err++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (err) |
| 191 | tst_resm(TFAIL, "Some entires not found"); |
| 192 | else |
| 193 | tst_resm(TPASS, "All entires found"); |
| 194 | } |
| 195 | |
| 196 | static void set_flag(const char *name) |
| 197 | { |
| 198 | int i; |
| 199 | |
| 200 | for (i = 0; i < ARRAY_SIZE(testcases); i++) { |
| 201 | if (!strcmp(name, testcases[i].name)) { |
| 202 | testcases[i].found = 1; |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | tst_resm(TFAIL, "Unexpected entry '%s' found", name); |
| 208 | } |
| 209 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 210 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 211 | { |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 212 | int i; |
| 213 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 215 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | tst_tmpdir(); |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 217 | |
Cyril Hrubis | c023535 | 2013-04-15 19:42:31 +0200 | [diff] [blame] | 218 | for (i = 0; i < ARRAY_SIZE(testcases); i++) { |
| 219 | |
| 220 | if (!testcases[i].create) |
| 221 | continue; |
| 222 | |
| 223 | switch (testcases[i].type) { |
| 224 | case ENTRY_DIR: |
| 225 | SAFE_MKDIR(cleanup, testcases[i].name, 0777); |
| 226 | break; |
| 227 | case ENTRY_FILE: |
| 228 | SAFE_FILE_PRINTF(cleanup, testcases[i].name, " "); |
| 229 | break; |
| 230 | case ENTRY_SYMLINK: |
| 231 | SAFE_SYMLINK(cleanup, "nonexistent", testcases[i].name); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 236 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 239 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | { |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 241 | tst_rmdir(); |
| 242 | } |