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 LAPI_OPENAT2_H__
8 #define LAPI_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 static inline int openat2(int dfd, const char *pathname,
51 struct open_how *how, size_t size)
52 {
53 return tst_syscall(__NR_openat2, dfd, pathname, how, size);
54 }
55 #endif
56
57 struct open_how_pad {
58 /* how should be kept as the first entry here */
59 struct open_how how;
60 uint64_t pad;
61 };
62
openat2_supported_by_kernel(void)63 static inline void openat2_supported_by_kernel(void)
64 {
65 long ret;
66
67 if ((tst_kvercmp(5, 6, 0)) < 0) {
68 /* Check if the syscall is backported on an older kernel */
69 ret = syscall(__NR_openat2, -1, NULL, NULL, 0);
70 if (ret == -1 && errno == ENOSYS)
71 tst_brk(TCONF, "Test not supported on kernel version < v5.6");
72 }
73 }
74
75 #endif /* LAPI_OPENAT2_H__ */
76