1 /*
2 * Check decoding of open_tree syscall.
3 *
4 * Copyright (c) 2019-2021 Dmitry V. Levin <ldv@strace.io>
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "tests.h"
11 #include "scno.h"
12
13 #include <fcntl.h>
14 #include <limits.h>
15 #include <stdio.h>
16 #include <stdint.h>
17 #include <unistd.h>
18 #include "kernel_fcntl.h"
19
20 static const char *errstr;
21
22 static long
23 k_open_tree(const unsigned int dfd,
24 const void *fname,
25 const unsigned int flags)
26 {
27 const kernel_ulong_t fill = (kernel_ulong_t) 0xdefaced00000000ULL;
28 const kernel_ulong_t bad = (kernel_ulong_t) 0xbadc0dedbadc0dedULL;
29 const kernel_ulong_t arg1 = fill | dfd;
30 const kernel_ulong_t arg2 = (uintptr_t) fname;
31 const kernel_ulong_t arg3 = fill | flags;
32 const long rc = syscall(__NR_open_tree, arg1, arg2, arg3, bad, bad, bad);
33 errstr = sprintrc(rc);
34 return rc;
35 }
36
37 int
38 main(void)
39 {
40 skip_if_unavailable("/proc/self/fd/");
41
42 #ifndef PATH_TRACING
43 char *cwd = get_fd_path(get_dir_fd("."));
44 #endif
45 static const char path_full[] = "/dev/full";
46 const char *const path = tail_memdup(path_full, sizeof(path_full));
47 char *const fname = tail_alloc(PATH_MAX);
48 const void *const efault = fname + PATH_MAX;
49 const char *const empty = efault - 1;
50 fill_memory_ex(fname, PATH_MAX, '0', 10);
51
52 int dfd = open(path, O_WRONLY);
53 if (dfd < 0)
54 perror_msg_and_fail("open: %s", path);
55
56 k_open_tree(-1, 0, 1);
57 #ifndef PATH_TRACING
58 printf("open_tree(-1, NULL, %s) = %s\n", "OPEN_TREE_CLONE", errstr);
59 #endif
60
61 k_open_tree(-100, fname, 0);
62 #ifndef PATH_TRACING
63 printf("open_tree(AT_FDCWD<%s>, \"%.*s\"..., 0) = %s\n",
64 cwd, (int) PATH_MAX - 1, fname, errstr);
65 #endif
66
67 fname[PATH_MAX - 1] = '\0';
68 k_open_tree(dfd, fname, 0x8000);
69 printf("open_tree(%d<%s>, \"%s\", %s) = %s\n",
70 dfd, path, fname, "AT_RECURSIVE", errstr);
71
72 k_open_tree(-1, efault, O_CLOEXEC | 1);
73 #ifndef PATH_TRACING
74 printf("open_tree(-1, %p, %s) = %s\n",
75 efault, "OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC", errstr);
76 #endif
77
78 k_open_tree(-1, empty, -1);
79 #ifndef PATH_TRACING
80 printf("open_tree(-1, \"\", %s|%#x) = %s\n",
81 "OPEN_TREE_CLONE|OPEN_TREE_CLOEXEC"
82 "|AT_SYMLINK_NOFOLLOW|AT_REMOVEDIR|AT_SYMLINK_FOLLOW"
83 "|AT_NO_AUTOMOUNT|AT_EMPTY_PATH|AT_RECURSIVE",
84 -1U & ~0x9f01 & ~O_CLOEXEC,
85 errstr);
86 #endif
87
88 if (k_open_tree(-1, path, 0) < 0)
89 printf("open_tree(-1, \"%s\", 0) = %s\n",
90 path, errstr);
91 else
92 printf("open_tree(-1, \"%s\", 0) = %s<%s>\n",
93 path, errstr, path);
94
95 if (k_open_tree(dfd, empty, 0x1000) < 0)
96 printf("open_tree(%d<%s>, \"\", %s) = %s\n",
97 dfd, path, "AT_EMPTY_PATH", errstr);
98 else
99 printf("open_tree(%d<%s>, \"\", %s) = %s<%s>\n",
100 dfd, path, "AT_EMPTY_PATH", errstr, path);
101
102 puts("+++ exited with 0 +++");
103 return 0;
104 }