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