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