Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Fujitsu Ltd. |
| 3 | * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2 of the GNU General Public License as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | /* |
| 18 | * Test Description: |
| 19 | * Verify that, |
| 20 | * 1. Creat a directory and open it, then delete the directory, ENOENT would |
| 21 | * return. |
| 22 | * 2. File descriptor does not refer to a directory, ENOTDIR would return. |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 23 | * 3. Invalid file descriptor fd, EBADF would return. |
| 24 | * 4. Argument points outside the calling process's address space, EFAULT |
| 25 | * would return. |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 26 | * |
| 27 | * PS: |
| 28 | * This file is for readdir(2) and the other files(readdir01.c and |
| 29 | * readdir02.c) are for readdir(3). |
| 30 | */ |
| 31 | |
| 32 | #define _GNU_SOURCE |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 40 | #include <sys/mman.h> |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 41 | #include "test.h" |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 42 | #include "safe_macros.h" |
| 43 | #include "linux_syscall_numbers.h" |
| 44 | #include "lapi/readdir.h" |
| 45 | |
| 46 | char *TCID = "readdir21"; |
| 47 | |
| 48 | #define TEST_DIR "test_dir" |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 49 | #define TEST_DIR4 "test_dir4" |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 50 | #define TEST_FILE "test_file" |
| 51 | #define DIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \ |
| 52 | S_IXGRP|S_IROTH|S_IXOTH) |
| 53 | |
| 54 | static unsigned int del_dir_fd, file_fd; |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 55 | static unsigned int invalid_fd = 999; |
| 56 | static unsigned int dir_fd; |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 57 | static struct old_linux_dirent dirp; |
| 58 | static void setup(void); |
| 59 | static void cleanup(void); |
| 60 | |
| 61 | static struct test_case_t { |
| 62 | unsigned int *fd; |
| 63 | struct old_linux_dirent *dirp; |
| 64 | unsigned int count; |
| 65 | int exp_errno; |
| 66 | } test_cases[] = { |
| 67 | {&del_dir_fd, &dirp, sizeof(struct old_linux_dirent), ENOENT}, |
| 68 | {&file_fd, &dirp, sizeof(struct old_linux_dirent), ENOTDIR}, |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 69 | {&invalid_fd, &dirp, sizeof(struct old_linux_dirent), EBADF}, |
| 70 | #if !defined(UCLINUX) |
| 71 | {&dir_fd, (struct old_linux_dirent *)-1, |
| 72 | sizeof(struct old_linux_dirent), EFAULT}, |
| 73 | #endif |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | int TST_TOTAL = ARRAY_SIZE(test_cases); |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 77 | static void readdir_verify(const struct test_case_t *); |
| 78 | |
| 79 | int main(int argc, char **argv) |
| 80 | { |
| 81 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 82 | const char *msg; |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 83 | |
| 84 | msg = parse_opts(argc, argv, NULL, NULL); |
| 85 | if (msg != NULL) |
| 86 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 87 | |
| 88 | setup(); |
| 89 | |
| 90 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 91 | tst_count = 0; |
| 92 | for (i = 0; i < TST_TOTAL; i++) |
| 93 | readdir_verify(&test_cases[i]); |
| 94 | } |
| 95 | |
| 96 | cleanup(); |
| 97 | tst_exit(); |
| 98 | } |
| 99 | |
| 100 | static void setup(void) |
| 101 | { |
| 102 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 103 | |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 104 | TEST_PAUSE; |
| 105 | |
| 106 | tst_tmpdir(); |
| 107 | |
| 108 | SAFE_MKDIR(cleanup, TEST_DIR, DIR_MODE); |
| 109 | del_dir_fd = SAFE_OPEN(cleanup, TEST_DIR, O_RDONLY | O_DIRECTORY); |
Cyril Hrubis | 97499c2 | 2014-05-07 14:54:22 +0200 | [diff] [blame] | 110 | SAFE_RMDIR(cleanup, TEST_DIR); |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 111 | |
| 112 | file_fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT, 0777); |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 113 | |
| 114 | SAFE_MKDIR(cleanup, TEST_DIR4, DIR_MODE); |
| 115 | dir_fd = SAFE_OPEN(cleanup, TEST_DIR4, O_RDONLY | O_DIRECTORY); |
| 116 | |
| 117 | #if !defined(UCLINUX) |
| 118 | test_cases[3].dirp = SAFE_MMAP(cleanup, 0, 1, PROT_NONE, |
| 119 | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); |
| 120 | #endif |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void readdir_verify(const struct test_case_t *test) |
| 124 | { |
| 125 | TEST(ltp_syscall(__NR_readdir, *test->fd, test->dirp, test->count)); |
| 126 | |
| 127 | if (TEST_RETURN != -1) { |
| 128 | tst_resm(TFAIL, "readdir() succeeded unexpectedly"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if (TEST_ERRNO == test->exp_errno) { |
| 133 | tst_resm(TPASS | TTERRNO, "readdir() failed as expected"); |
| 134 | } else { |
| 135 | tst_resm(TFAIL | TTERRNO, |
| 136 | "readdir() failed unexpectedly; expected: %d - %s", |
| 137 | test->exp_errno, strerror(test->exp_errno)); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | static void cleanup(void) |
| 142 | { |
Zeng Linggang | 0d99d6f | 2014-05-14 16:54:54 +0800 | [diff] [blame] | 143 | if (dir_fd > 0) |
| 144 | close(dir_fd); |
| 145 | |
Zeng Linggang | c7fb4b7 | 2014-05-07 18:51:41 +0800 | [diff] [blame] | 146 | if (del_dir_fd > 0) |
| 147 | close(del_dir_fd); |
| 148 | |
| 149 | if (file_fd > 0) |
| 150 | close(file_fd); |
| 151 | |
| 152 | tst_rmdir(); |
| 153 | } |