1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Linaro Limited. All rights reserved.
4 * Author: Viresh Kumar <viresh.kumar@linaro.org>
5 */
6
7 #ifndef OPENAT2_H
8 #define OPENAT2_H
9
10 #include <sys/syscall.h>
11 #include <linux/types.h>
12
13 #include "lapi/syscalls.h"
14
15 #include "config.h"
16
17 #ifndef HAVE_OPENAT2
18 /*
19 * Arguments for how openat2(2) should open the target path. If only @flags and
20 * @mode are non-zero, then openat2(2) operates very similarly to openat(2).
21 *
22 * However, unlike openat(2), unknown or invalid bits in @flags result in
23 * -EINVAL rather than being silently ignored. @mode must be zero unless one of
24 * {O_CREAT, O_TMPFILE} are set.
25 *
26 * @flags: O_* flags.
27 * @mode: O_CREAT/O_TMPFILE file mode.
28 * @resolve: RESOLVE_* flags.
29 */
30 struct open_how {
31 uint64_t flags;
32 uint64_t mode;
33 uint64_t resolve;
34 };
35
36 /* how->resolve flags for openat2(2). */
37 #define RESOLVE_NO_XDEV 0x01 /* Block mount-point crossings
38 (includes bind-mounts). */
39 #define RESOLVE_NO_MAGICLINKS 0x02 /* Block traversal through procfs-style
40 "magic-links". */
41 #define RESOLVE_NO_SYMLINKS 0x04 /* Block traversal through all symlinks
42 (implies OEXT_NO_MAGICLINKS) */
43 #define RESOLVE_BENEATH 0x08 /* Block "lexical" trickery like
44 "..", symlinks, and absolute
45 paths which escape the dirfd. */
46 #define RESOLVE_IN_ROOT 0x10 /* Make all jumps to "/" and ".."
47 be scoped inside the dirfd
48 (similar to chroot(2)). */
49
openat2(int dfd,const char * pathname,struct open_how * how,size_t size)50 int openat2(int dfd, const char *pathname, struct open_how *how, size_t size)
51 {
52 return tst_syscall(__NR_openat2, dfd, pathname, how, size);
53 }
54 #endif
55
56 struct open_how_pad {
57 /* how should be kept as the first entry here */
58 struct open_how how;
59 uint64_t pad;
60 };
61
openat2_supported_by_kernel(void)62 void openat2_supported_by_kernel(void)
63 {
64 if ((tst_kvercmp(5, 6, 0)) < 0) {
65 /* Check if the syscall is backported on an older kernel */
66 TEST(syscall(__NR_openat2, -1, NULL, NULL, 0));
67 if (TST_RET == -1 && TST_ERR == ENOSYS)
68 tst_brk(TCONF, "Test not supported on kernel version < v5.6");
69 }
70 }
71
72 #endif /* OPENAT2_H */
73