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 #include "kernel_types.h"
45 
46 static const char fname[] =
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\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\nZ";
55 static const char qname[] =
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\\nA\\n"
62 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
63 	"A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
64 
65 static char buf[8192];
66 
67 static const char *
str_d_type(const unsigned char d_type)68 str_d_type(const unsigned char d_type)
69 {
70 	switch (d_type) {
71 		case DT_DIR:
72 			return "DT_DIR";
73 		case DT_REG:
74 			return "DT_REG";
75 		default:
76 			return "DT_UNKNOWN";
77 	}
78 }
79 static void
print_dirent(const kernel_dirent * d)80 print_dirent(const kernel_dirent *d)
81 {
82 	const unsigned int d_name_offset = offsetof(kernel_dirent, d_name);
83 	int d_name_len = d->d_reclen - d_name_offset - 1;
84 	assert(d_name_len > 0);
85 
86 	printf("{d_ino=%llu, d_off=%llu, d_reclen=%u, d_name=",
87 	       (unsigned long long) d->d_ino,
88 	       (unsigned long long) d->d_off, d->d_reclen);
89 
90 	if (d->d_name[0] == '.')
91 		printf("\"%.*s\"", d_name_len, d->d_name);
92 	else
93 		printf("\"%s\"", qname);
94 
95 	printf(", d_type=%s}",
96 	       str_d_type(*((const char *) d + d->d_reclen - 1)));
97 }
98 
99 int
main(int ac,const char ** av)100 main(int ac, const char **av)
101 {
102 	char *dname;
103 	int rc;
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));
113 	while ((rc = syscall(__NR_getdents, 0, buf, sizeof(buf)))) {
114 		kernel_dirent *d;
115 		int i;
116 
117 		if (rc < 0)
118 			return 77;
119 		printf("getdents(0, [");
120 		for (i = 0; i < rc; i += d->d_reclen) {
121 			d = (kernel_dirent *) &buf[i];
122 			if (i)
123 				printf(", ");
124 			print_dirent(d);
125 		}
126 		printf("], %zu) = %d\n", sizeof(buf), rc);
127 	}
128 	printf("getdents(0, [], %zu) = 0\n", sizeof(buf));
129 	puts("+++ exited with 0 +++");
130 	assert(!unlink(fname));
131 	assert(!chdir(".."));
132 	assert(!rmdir(dname));
133 
134 	return 0;
135 }
136 
137 #else
138 
139 int
main(void)140 main(void)
141 {
142 	return 77;
143 }
144 
145 #endif
146