]> granicus.if.org Git - strace/blob - tests/getdents.c
tests: add prctl-seccomp-strict.test
[strace] / tests / getdents.c
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 <sys/syscall.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 # include "kernel_types.h"
41
42 static 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";
51 static 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
61 static char buf[8192];
62
63 static const char *
64 str_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 }
75 static void
76 print_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
95 int
96 main(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)
114                         perror_msg_and_skip("getdents");
115                 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
135 SKIP_MAIN_UNDEFINED("__NR_getdents")
136
137 #endif