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