Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 1 | /* |
Dmitry V. Levin | 2e00fe9 | 2016-01-06 12:03:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 28 | #include "tests.h" |
Dmitry V. Levin | 6a2f43c | 2016-08-09 14:38:29 +0000 | [diff] [blame] | 29 | #include <asm/unistd.h> |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 30 | |
| 31 | #ifdef __NR_getdents64 |
| 32 | |
Dmitry V. Levin | 2e00fe9 | 2016-01-06 12:03:37 +0000 | [diff] [blame] | 33 | # include <assert.h> |
| 34 | # include <dirent.h> |
| 35 | # include <fcntl.h> |
| 36 | # include <inttypes.h> |
| 37 | # include <stddef.h> |
| 38 | # include <stdio.h> |
| 39 | # include <sys/stat.h> |
| 40 | # include <unistd.h> |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 41 | |
| 42 | static const char fname[] = |
| 43 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 44 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 45 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 46 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 47 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 48 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 49 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 50 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ"; |
| 51 | static const char qname[] = |
| 52 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 53 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 54 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 55 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 56 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 57 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 58 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 59 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ"; |
| 60 | |
| 61 | typedef struct { |
| 62 | uint64_t d_ino; |
| 63 | uint64_t d_off; |
| 64 | unsigned short d_reclen; |
| 65 | unsigned char d_type; |
| 66 | char d_name[256]; |
| 67 | } kernel_dirent64; |
| 68 | |
| 69 | static char buf[8192]; |
| 70 | |
| 71 | static const char * |
| 72 | str_d_type(const unsigned char d_type) |
| 73 | { |
| 74 | switch (d_type) { |
| 75 | case DT_DIR: |
| 76 | return "DT_DIR"; |
| 77 | case DT_REG: |
| 78 | return "DT_REG"; |
| 79 | default: |
| 80 | return "DT_UNKNOWN"; |
| 81 | } |
| 82 | } |
| 83 | static void |
| 84 | print_dirent(const kernel_dirent64 *d) |
| 85 | { |
| 86 | const unsigned int d_name_offset = offsetof(kernel_dirent64, d_name); |
| 87 | int d_name_len = d->d_reclen - d_name_offset; |
| 88 | assert(d_name_len > 0); |
| 89 | |
| 90 | printf("{d_ino=%" PRIu64 ", d_off=%" PRId64 |
| 91 | ", d_reclen=%u, d_type=%s, d_name=", |
| 92 | d->d_ino, d->d_off, d->d_reclen, str_d_type(d->d_type)); |
| 93 | |
| 94 | if (d->d_name[0] == '.') |
| 95 | printf("\"%.*s\"}", d_name_len, d->d_name); |
| 96 | else |
| 97 | printf("\"%s\"}", qname); |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | main(int ac, const char **av) |
| 102 | { |
| 103 | char *dname; |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 104 | |
| 105 | assert(ac == 1); |
| 106 | assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0); |
| 107 | assert(!mkdir(dname, 0700)); |
| 108 | assert(!chdir(dname)); |
| 109 | (void) close(0); |
| 110 | assert(!creat(fname, 0600)); |
| 111 | assert(!close(0)); |
| 112 | assert(!open(".", O_RDONLY | O_DIRECTORY)); |
Dmitry V. Levin | f443fd4 | 2016-04-26 22:38:10 +0000 | [diff] [blame] | 113 | |
| 114 | unsigned long count = (unsigned long) 0xfacefeeddeadbeef; |
| 115 | long rc = syscall(__NR_getdents64, (long) 0xdefacedffffffff, NULL, count); |
| 116 | printf("getdents64(-1, NULL, %u) = %ld %s (%m)\n", |
| 117 | (unsigned) count, rc, errno2name()); |
| 118 | |
| 119 | count = (unsigned long) 0xfacefeed00000000 | sizeof(buf); |
| 120 | while ((rc = syscall(__NR_getdents64, 0, buf, count))) { |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 121 | kernel_dirent64 *d; |
Dmitry V. Levin | f443fd4 | 2016-04-26 22:38:10 +0000 | [diff] [blame] | 122 | long i; |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 123 | |
| 124 | if (rc < 0) |
Dmitry V. Levin | 2e00fe9 | 2016-01-06 12:03:37 +0000 | [diff] [blame] | 125 | perror_msg_and_skip("getdents64"); |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 126 | printf("getdents64(0, ["); |
| 127 | for (i = 0; i < rc; i += d->d_reclen) { |
| 128 | d = (kernel_dirent64 *) &buf[i]; |
| 129 | if (i) |
| 130 | printf(", "); |
| 131 | print_dirent(d); |
| 132 | } |
Dmitry V. Levin | f443fd4 | 2016-04-26 22:38:10 +0000 | [diff] [blame] | 133 | printf("], %u) = %ld\n", (unsigned) count, rc); |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 134 | } |
Dmitry V. Levin | f443fd4 | 2016-04-26 22:38:10 +0000 | [diff] [blame] | 135 | printf("getdents64(0, [], %u) = 0\n", (unsigned) count); |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 136 | puts("+++ exited with 0 +++"); |
| 137 | assert(!unlink(fname)); |
| 138 | assert(!chdir("..")); |
| 139 | assert(!rmdir(dname)); |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | #else |
| 145 | |
Dmitry V. Levin | 2e00fe9 | 2016-01-06 12:03:37 +0000 | [diff] [blame] | 146 | SKIP_MAIN_UNDEFINED("__NR_getdents64") |
Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame] | 147 | |
| 148 | #endif |