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