1 /* 2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> 3 * Copyright (c) 2015-2017 The strace developers. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "tests.h" 30 #include <asm/unistd.h> 31 32 #ifdef __NR_getdents64 33 34 # include <assert.h> 35 # include <dirent.h> 36 # include <fcntl.h> 37 # include <inttypes.h> 38 # include <stddef.h> 39 # include <stdio.h> 40 # include <sys/stat.h> 41 # include <unistd.h> 42 43 static const char fname[] = 44 "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\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_dirent64; 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_dirent64 *d) 86 { 87 const unsigned int d_name_offset = offsetof(kernel_dirent64, d_name); 88 int d_name_len = d->d_reclen - d_name_offset; 89 assert(d_name_len > 0); 90 91 printf("{d_ino=%" PRIu64 ", d_off=%" PRId64 92 ", d_reclen=%u, d_type=%s, d_name=", 93 d->d_ino, d->d_off, d->d_reclen, str_d_type(d->d_type)); 94 95 if (d->d_name[0] == '.') 96 printf("\"%.*s\"}", d_name_len, d->d_name); 97 else 98 printf("\"%s\"}", qname); 99 } 100 101 int 102 main(void) 103 { 104 static const char dname[] = "getdents64.test.tmp.dir"; 105 106 assert(!mkdir(dname, 0700)); 107 assert(!chdir(dname)); 108 (void) close(0); 109 assert(!creat(fname, 0600)); 110 assert(!close(0)); 111 assert(!open(".", O_RDONLY | O_DIRECTORY)); 112 113 unsigned long count = (unsigned long) 0xfacefeeddeadbeefULL; 114 long rc = syscall(__NR_getdents64, (long) 0xdefacedffffffffULL, NULL, 115 count); 116 printf("getdents64(-1, NULL, %u) = %ld %s (%m)\n", 117 (unsigned) count, rc, errno2name()); 118 119 count = (unsigned long) 0xfacefeed00000000ULL | sizeof(buf); 120 while ((rc = syscall(__NR_getdents64, 0, buf, count))) { 121 kernel_dirent64 *d; 122 long i; 123 124 if (rc < 0) 125 perror_msg_and_skip("getdents64"); 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 } 133 printf("], %u) = %ld\n", (unsigned) count, rc); 134 } 135 printf("getdents64(0, [], %u) = 0\n", (unsigned) count); 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 146 SKIP_MAIN_UNDEFINED("__NR_getdents64") 147 148 #endif 149