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 | |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 22 | /* |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1. getdents() fails with -1 return value and sets errno to EBADF |
| 26 | * if file descriptor fd is invalid. |
| 27 | * 2. getdents() fails with -1 return value and sets errno to EINVAL |
| 28 | * if result buffer is too small. |
| 29 | * 3. getdents() fails with -1 return value and sets errno to ENOTDIR |
| 30 | * if file descriptor does not refer to a directory. |
| 31 | * 4. getdents() fails with -1 return value and sets errno to ENOENT |
| 32 | * if there is no such directory. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #define _GNU_SOURCE |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #include <errno.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <sys/stat.h> |
| 41 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | #include "test.h" |
Cyril Hrubis | 85f662d | 2013-03-18 19:22:39 +0100 | [diff] [blame] | 44 | #include "getdents.h" |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 45 | #include "safe_macros.h" |
| 46 | |
| 47 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 48 | S_IXGRP|S_IROTH|S_IXOTH) |
| 49 | #define TEST_DIR "test_dir" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 51 | static void cleanup(void); |
| 52 | static void setup(void); |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 53 | static void print_test_result(int err, int exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | char *TCID = "getdents02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 57 | static void test_ebadf(void); |
| 58 | static void test_einval(void); |
| 59 | static void test_enotdir(void); |
| 60 | static void test_enoent(void); |
| 61 | |
| 62 | static void (*testfunc[])(void) = { test_ebadf, test_einval, |
| 63 | test_enotdir, test_enoent }; |
| 64 | |
| 65 | int TST_TOTAL = ARRAY_SIZE(testfunc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 67 | static int longsyscall; |
| 68 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 69 | option_t options[] = { |
| 70 | /* -l long option. Tests getdents64 */ |
| 71 | {"l", &longsyscall, NULL}, |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 72 | {NULL, NULL, NULL} |
| 73 | }; |
| 74 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 75 | static void help(void) |
Markos Chandras | 28894f5 | 2013-03-18 10:54:53 +0000 | [diff] [blame] | 76 | { |
| 77 | printf(" -l Test the getdents64 system call\n"); |
| 78 | } |
| 79 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 80 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | { |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 82 | int lc, i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 83 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 85 | if ((msg = parse_opts(ac, av, options, &help)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 86 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 88 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
| 90 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 91 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 93 | for (i = 0; i < TST_TOTAL; i++) |
| 94 | (*testfunc[i])(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 98 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 101 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | tst_tmpdir(); |
| 106 | |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 107 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 110 | static void print_test_result(int err, int exp_errno) |
| 111 | { |
Xiaoguang Wang | e906e33 | 2014-02-12 12:50:24 +0800 | [diff] [blame] | 112 | if (err == 0) { |
| 113 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 114 | } else if (err == exp_errno) { |
| 115 | tst_resm(TPASS, "getdents failed as expected: %s", |
| 116 | strerror(err)); |
| 117 | } else if (err == ENOSYS) { |
| 118 | tst_resm(TCONF, "syscall not implemented"); |
| 119 | } else { |
| 120 | tst_resm(TFAIL, "getdents failed unexpectedly: %s", |
| 121 | strerror(err)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void test_ebadf(void) |
| 126 | { |
| 127 | int fd = -5; |
| 128 | struct linux_dirent64 dirp64; |
| 129 | struct linux_dirent dirp; |
| 130 | |
| 131 | if (longsyscall) |
| 132 | getdents64(fd, &dirp64, sizeof(dirp64)); |
| 133 | else |
| 134 | getdents(fd, &dirp, sizeof(dirp)); |
| 135 | |
| 136 | print_test_result(errno, EBADF); |
| 137 | } |
| 138 | |
| 139 | static void test_einval(void) |
| 140 | { |
| 141 | int fd; |
| 142 | char buf[1]; |
| 143 | |
| 144 | fd = SAFE_OPEN(cleanup, ".", O_RDONLY); |
| 145 | |
| 146 | /* Pass one byte long buffer. The result should be EINVAL */ |
| 147 | if (longsyscall) |
| 148 | getdents64(fd, (void *)buf, sizeof(buf)); |
| 149 | else |
| 150 | getdents(fd, (void *)buf, sizeof(buf)); |
| 151 | |
| 152 | print_test_result(errno, EINVAL); |
| 153 | |
| 154 | SAFE_CLOSE(cleanup, fd); |
| 155 | } |
| 156 | |
| 157 | static void test_enotdir(void) |
| 158 | { |
| 159 | int fd; |
| 160 | struct linux_dirent64 dir64; |
| 161 | struct linux_dirent dir; |
| 162 | |
| 163 | fd = SAFE_OPEN(cleanup, "test", O_CREAT | O_RDWR); |
| 164 | |
| 165 | if (longsyscall) |
| 166 | getdents64(fd, &dir64, sizeof(dir64)); |
| 167 | else |
| 168 | getdents(fd, &dir, sizeof(dir)); |
| 169 | |
| 170 | print_test_result(errno, ENOTDIR); |
| 171 | |
| 172 | SAFE_CLOSE(cleanup, fd); |
| 173 | } |
| 174 | |
| 175 | static void test_enoent(void) |
| 176 | { |
| 177 | int fd; |
| 178 | struct linux_dirent64 dir64; |
| 179 | struct linux_dirent dir; |
| 180 | |
| 181 | SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE); |
| 182 | |
| 183 | fd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY); |
| 184 | if (rmdir(TEST_DIR) == -1) { |
| 185 | tst_brkm(TBROK | TERRNO, cleanup, |
| 186 | "rmdir(%s) failed", TEST_DIR); |
| 187 | } |
| 188 | |
| 189 | if (longsyscall) |
| 190 | getdents64(fd, &dir64, sizeof(dir64)); |
| 191 | else |
| 192 | getdents(fd, &dir, sizeof(dir)); |
| 193 | |
| 194 | print_test_result(errno, ENOENT); |
| 195 | |
| 196 | SAFE_CLOSE(cleanup, fd); |
| 197 | } |
| 198 | |
Cyril Hrubis | caf3d34 | 2013-03-18 14:10:32 +0100 | [diff] [blame] | 199 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | { |
Garrett Cooper | 2cb5291 | 2011-03-04 01:21:59 -0800 | [diff] [blame] | 201 | tst_rmdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | } |