Dmitry V. Levin | 7528a0b | 2015-11-19 16:39:32 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org> |
| 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 | |
| 28 | #ifdef HAVE_CONFIG_H |
| 29 | # include "config.h" |
| 30 | #endif |
| 31 | |
| 32 | #include <sys/syscall.h> |
| 33 | |
| 34 | #ifdef __NR_getdents |
| 35 | |
| 36 | #include <assert.h> |
| 37 | #include <dirent.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <stddef.h> |
| 40 | #include <stdio.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <unistd.h> |
| 43 | |
| 44 | static const char fname[] = |
| 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\nA\n" |
| 51 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n" |
| 52 | "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ"; |
| 53 | static const char qname[] = |
| 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\\nA\\n" |
| 60 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n" |
| 61 | "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ"; |
| 62 | |
| 63 | typedef struct { |
| 64 | unsigned long d_ino; |
| 65 | unsigned long d_off; |
| 66 | unsigned short d_reclen; |
| 67 | char d_name[256]; |
| 68 | } kernel_dirent; |
| 69 | |
| 70 | static char buf[8192]; |
| 71 | |
| 72 | static const char * |
| 73 | str_d_type(const unsigned char d_type) |
| 74 | { |
| 75 | switch (d_type) { |
| 76 | case DT_DIR: |
| 77 | return "DT_DIR"; |
| 78 | case DT_REG: |
| 79 | return "DT_REG"; |
| 80 | default: |
| 81 | return "DT_UNKNOWN"; |
| 82 | } |
| 83 | } |
| 84 | static void |
| 85 | print_dirent(const kernel_dirent *d) |
| 86 | { |
| 87 | const unsigned int d_name_offset = offsetof(kernel_dirent, d_name); |
| 88 | int d_name_len = d->d_reclen - d_name_offset - 1; |
| 89 | assert(d_name_len > 0); |
| 90 | |
| 91 | printf("{d_ino=%lu, d_off=%lu, d_reclen=%u, d_name=", |
| 92 | d->d_ino, d->d_off, d->d_reclen); |
| 93 | |
| 94 | if (d->d_name[0] == '.') |
| 95 | printf("\"%.*s\"", d_name_len, d->d_name); |
| 96 | else |
| 97 | printf("\"%s\"", qname); |
| 98 | |
| 99 | printf(", d_type=%s}", |
| 100 | str_d_type(*((const char *) d + d->d_reclen - 1))); |
| 101 | } |
| 102 | |
| 103 | int |
| 104 | main(int ac, const char **av) |
| 105 | { |
| 106 | char *dname; |
| 107 | int rc; |
| 108 | |
| 109 | assert(ac == 1); |
| 110 | assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0); |
| 111 | assert(!mkdir(dname, 0700)); |
| 112 | assert(!chdir(dname)); |
| 113 | (void) close(0); |
| 114 | assert(!creat(fname, 0600)); |
| 115 | assert(!close(0)); |
| 116 | assert(!open(".", O_RDONLY | O_DIRECTORY)); |
| 117 | while ((rc = syscall(__NR_getdents, 0, buf, sizeof(buf)))) { |
| 118 | kernel_dirent *d; |
| 119 | int i; |
| 120 | |
| 121 | if (rc < 0) |
| 122 | return 77; |
| 123 | printf("getdents(0, ["); |
| 124 | for (i = 0; i < rc; i += d->d_reclen) { |
| 125 | d = (kernel_dirent *) &buf[i]; |
| 126 | if (i) |
| 127 | printf(", "); |
| 128 | print_dirent(d); |
| 129 | } |
| 130 | printf("], %zu) = %d\n", sizeof(buf), rc); |
| 131 | } |
| 132 | printf("getdents(0, [], %zu) = 0\n", sizeof(buf)); |
| 133 | puts("+++ exited with 0 +++"); |
| 134 | assert(!unlink(fname)); |
| 135 | assert(!chdir("..")); |
| 136 | assert(!rmdir(dname)); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | #else |
| 142 | |
| 143 | int |
| 144 | main(void) |
| 145 | { |
| 146 | return 77; |
| 147 | } |
| 148 | |
| 149 | #endif |