blob: 16541df6fe76d4f22f692d85adf6f6a6ed41361b [file] [log] [blame]
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +00001/*
Dmitry V. Levin74769e92016-01-06 12:02:55 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +00003 * 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
Dmitry V. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +000029#include <sys/syscall.h>
30
31#ifdef __NR_getdents
32
Dmitry V. Levin74769e92016-01-06 12:02:55 +000033# 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# include "kernel_types.h"
Dmitry V. Levinbdb07e32015-11-27 01:51:22 +000041
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +000042static 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
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +000061static 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
Dmitry V. Levine67c8e42015-12-16 00:07:16 +000082 printf("{d_ino=%llu, d_off=%llu, d_reclen=%u, d_name=",
Dmitry V. Levinbdb07e32015-11-27 01:51:22 +000083 (unsigned long long) d->d_ino,
84 (unsigned long long) d->d_off, d->d_reclen);
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +000085
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(int ac, const char **av)
97{
98 char *dname;
99 int rc;
100
101 assert(ac == 1);
102 assert(asprintf(&dname, "%s.test.tmp.dir", av[0]) > 0);
103 assert(!mkdir(dname, 0700));
104 assert(!chdir(dname));
105 (void) close(0);
106 assert(!creat(fname, 0600));
107 assert(!close(0));
108 assert(!open(".", O_RDONLY | O_DIRECTORY));
109 while ((rc = syscall(__NR_getdents, 0, buf, sizeof(buf)))) {
110 kernel_dirent *d;
111 int i;
112
113 if (rc < 0)
Dmitry V. Levin74769e92016-01-06 12:02:55 +0000114 perror_msg_and_skip("getdents");
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +0000115 printf("getdents(0, [");
116 for (i = 0; i < rc; i += d->d_reclen) {
117 d = (kernel_dirent *) &buf[i];
118 if (i)
119 printf(", ");
120 print_dirent(d);
121 }
122 printf("], %zu) = %d\n", sizeof(buf), rc);
123 }
124 printf("getdents(0, [], %zu) = 0\n", sizeof(buf));
125 puts("+++ exited with 0 +++");
126 assert(!unlink(fname));
127 assert(!chdir(".."));
128 assert(!rmdir(dname));
129
130 return 0;
131}
132
133#else
134
Dmitry V. Levin74769e92016-01-06 12:02:55 +0000135SKIP_MAIN_UNDEFINED("__NR_getdents")
Dmitry V. Levin7528a0b2015-11-19 16:39:32 +0000136
137#endif