]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/syscall_wrappers.h
f50875cc12fb9d03e43b9d8ab144402f45072153
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #ifndef __LXC_SYSCALL_WRAPPER_H
4 #define __LXC_SYSCALL_WRAPPER_H
9 #include <asm/unistd.h>
11 #include <linux/keyctl.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
20 #include "syscall_numbers.h"
22 #ifdef HAVE_LINUX_MEMFD_H
23 #include <linux/memfd.h>
26 #ifdef HAVE_SYS_SIGNALFD_H
27 #include <sys/signalfd.h>
30 #ifdef HAVE_STRUCT_OPEN_HOW
31 #include <linux/openat2.h>
34 #if HAVE_SYS_PERSONALITY_H
35 #include <sys/personality.h>
38 typedef int32_t key_serial_t
;
41 static inline long __keyctl(int cmd
, unsigned long arg2
, unsigned long arg3
,
42 unsigned long arg4
, unsigned long arg5
)
44 return syscall(__NR_keyctl
, cmd
, arg2
, arg3
, arg4
, arg5
);
46 #define keyctl __keyctl
49 #ifndef F_LINUX_SPECIFIC_BASE
50 #define F_LINUX_SPECIFIC_BASE 1024
53 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
54 #define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
57 #define F_SEAL_SEAL 0x0001
58 #define F_SEAL_SHRINK 0x0002
59 #define F_SEAL_GROW 0x0004
60 #define F_SEAL_WRITE 0x0008
63 #ifndef HAVE_MEMFD_CREATE
64 static inline int memfd_create_lxc(const char *name
, unsigned int flags
)
66 return syscall(__NR_memfd_create
, name
, flags
);
68 #define memfd_create memfd_create_lxc
70 extern int memfd_create(const char *name
, unsigned int flags
);
73 #ifndef HAVE_PIVOT_ROOT
74 static int pivot_root(const char *new_root
, const char *put_old
)
76 return syscall(__NR_pivot_root
, new_root
, put_old
);
79 extern int pivot_root(const char *new_root
, const char *put_old
);
82 /* Define sethostname() if missing from the C library */
83 #ifndef HAVE_SETHOSTNAME
84 static inline int sethostname(const char *name
, size_t len
)
86 return syscall(__NR_sethostname
, name
, len
);
90 /* Define setns() if missing from the C library */
92 static inline int setns(int fd
, int nstype
)
94 return syscall(__NR_setns
, fd
, nstype
);
98 #ifndef HAVE_SYS_SIGNALFD_H
99 struct signalfd_siginfo
{
108 uint32_t ssi_overrun
;
119 static inline int signalfd(int fd
, const sigset_t
*mask
, int flags
)
123 retval
= syscall(__NR_signalfd4
, fd
, mask
, _NSIG
/ 8, flags
);
125 if (errno
== ENOSYS
&& flags
== 0)
126 retval
= syscall(__NR_signalfd
, fd
, mask
, _NSIG
/ 8);
133 /* Define unshare() if missing from the C library */
135 static inline int unshare(int flags
)
137 return syscall(__NR_unshare
, flags
);
140 extern int unshare(int);
143 /* Define faccessat() if missing from the C library */
144 #ifndef HAVE_FACCESSAT
145 static int faccessat(int __fd
, const char *__file
, int __type
, int __flag
)
147 return syscall(__NR_faccessat
, __fd
, __file
, __type
, __flag
);
151 #ifndef HAVE_MOVE_MOUNT
152 static inline int move_mount_lxc(int from_dfd
, const char *from_pathname
,
153 int to_dfd
, const char *to_pathname
,
156 return syscall(__NR_move_mount
, from_dfd
, from_pathname
, to_dfd
,
159 #define move_mount move_mount_lxc
161 extern int move_mount(int from_dfd
, const char *from_pathname
, int to_dfd
,
162 const char *to_pathname
, unsigned int flags
);
165 #ifndef HAVE_OPEN_TREE
166 static inline int open_tree_lxc(int dfd
, const char *filename
, unsigned int flags
)
168 return syscall(__NR_open_tree
, dfd
, filename
, flags
);
170 #define open_tree open_tree_lxc
172 extern int open_tree(int dfd
, const char *filename
, unsigned int flags
);
176 static inline int fsopen_lxc(const char *fs_name
, unsigned int flags
)
178 return syscall(__NR_fsopen
, fs_name
, flags
);
180 #define fsopen fsopen_lxc
182 extern int fsopen(const char *fs_name
, unsigned int flags
);
186 static inline int fspick_lxc(int dfd
, const char *path
, unsigned int flags
)
188 return syscall(__NR_fspick
, dfd
, path
, flags
);
190 #define fspick fspick_lxc
192 extern int fspick(int dfd
, const char *path
, unsigned int flags
);
195 #ifndef HAVE_FSCONFIG
196 static inline int fsconfig_lxc(int fd
, unsigned int cmd
, const char *key
, const void *value
, int aux
)
198 return syscall(__NR_fsconfig
, fd
, cmd
, key
, value
, aux
);
200 #define fsconfig fsconfig_lxc
202 extern int fsconfig(int fd
, unsigned int cmd
, const char *key
, const void *value
, int aux
);
206 static inline int fsmount_lxc(int fs_fd
, unsigned int flags
, unsigned int attr_flags
)
208 return syscall(__NR_fsmount
, fs_fd
, flags
, attr_flags
);
210 #define fsmount fsmount_lxc
212 extern int fsmount(int fs_fd
, unsigned int flags
, unsigned int attr_flags
);
218 struct lxc_mount_attr
{
225 #ifndef HAVE_MOUNT_SETATTR
226 static inline int mount_setattr(int dfd
, const char *path
, unsigned int flags
,
227 struct lxc_mount_attr
*attr
, size_t size
)
229 return syscall(__NR_mount_setattr
, dfd
, path
, flags
, attr
, size
);
234 * Arguments for how openat2(2) should open the target path. If only @flags and
235 * @mode are non-zero, then openat2(2) operates very similarly to openat(2).
237 * However, unlike openat(2), unknown or invalid bits in @flags result in
238 * -EINVAL rather than being silently ignored. @mode must be zero unless one of
239 * {O_CREAT, O_TMPFILE} are set.
242 * @mode: O_CREAT/O_TMPFILE file mode.
243 * @resolve: RESOLVE_* flags.
245 struct lxc_open_how
{
251 /* how->resolve flags for openat2(2). */
252 #ifndef RESOLVE_NO_XDEV
253 #define RESOLVE_NO_XDEV 0x01 /* Block mount-point crossings
254 (includes bind-mounts). */
257 #ifndef RESOLVE_NO_MAGICLINKS
258 #define RESOLVE_NO_MAGICLINKS 0x02 /* Block traversal through procfs-style
262 #ifndef RESOLVE_NO_SYMLINKS
263 #define RESOLVE_NO_SYMLINKS 0x04 /* Block traversal through all symlinks
264 (implies OEXT_NO_MAGICLINKS) */
267 #ifndef RESOLVE_BENEATH
268 #define RESOLVE_BENEATH 0x08 /* Block "lexical" trickery like
269 "..", symlinks, and absolute
270 paths which escape the dirfd. */
273 #ifndef RESOLVE_IN_ROOT
274 #define RESOLVE_IN_ROOT 0x10 /* Make all jumps to "/" and ".."
275 be scoped inside the dirfd
276 (similar to chroot(2)). */
279 #define PROTECT_LOOKUP_BENEATH (RESOLVE_BENEATH | RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS)
280 #define PROTECT_LOOKUP_BENEATH_WITH_SYMLINKS (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_SYMLINKS)
281 #define PROTECT_LOOKUP_BENEATH_WITH_MAGICLINKS (PROTECT_LOOKUP_BENEATH & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
282 #define PROTECT_LOOKUP_BENEATH_XDEV (PROTECT_LOOKUP_BENEATH & ~RESOLVE_NO_XDEV)
284 #define PROTECT_LOOKUP_ABSOLUTE (PROTECT_LOOKUP_BENEATH & ~RESOLVE_BENEATH)
285 #define PROTECT_LOOKUP_ABSOLUTE_WITH_SYMLINKS (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_SYMLINKS)
286 #define PROTECT_LOOKUP_ABSOLUTE_WITH_MAGICLINKS (PROTECT_LOOKUP_ABSOLUTE & ~(RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS))
287 #define PROTECT_LOOKUP_ABSOLUTE_XDEV (PROTECT_LOOKUP_ABSOLUTE & ~RESOLVE_NO_XDEV)
289 #define PROTECT_OPATH_FILE (O_NOFOLLOW | O_PATH | O_CLOEXEC)
290 #define PROTECT_OPATH_DIRECTORY (PROTECT_OPATH_FILE | O_DIRECTORY)
292 #define PROTECT_OPEN_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_RDONLY)
293 #define PROTECT_OPEN (PROTECT_OPEN_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
295 #define PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS (O_CLOEXEC | O_NOCTTY | O_WRONLY)
296 #define PROTECT_OPEN_W (PROTECT_OPEN_W_WITH_TRAILING_SYMLINKS | O_NOFOLLOW)
297 #define PROTECT_OPEN_RW (O_CLOEXEC | O_NOCTTY | O_RDWR | O_NOFOLLOW)
300 static inline int openat2(int dfd
, const char *filename
, struct lxc_open_how
*how
, size_t size
)
302 /* When struct open_how is updated we should update lxc as well. */
303 #ifdef HAVE_STRUCT_OPEN_HOW
304 BUILD_BUG_ON(sizeof(struct lxc_open_how
) != sizeof(struct open_how
));
306 return syscall(__NR_openat2
, dfd
, filename
, (struct open_how
*)how
, size
);
308 #endif /* HAVE_OPENAT2 */
310 #ifndef CLOSE_RANGE_UNSHARE
311 #define CLOSE_RANGE_UNSHARE (1U << 1)
314 #ifndef CLOSE_RANGE_CLOEXEC
315 #define CLOSE_RANGE_CLOEXEC (1U << 2)
318 #ifndef HAVE_CLOSE_RANGE
319 static inline int close_range(unsigned int fd
, unsigned int max_fd
, unsigned int flags
)
321 return syscall(__NR_close_range
, fd
, max_fd
, flags
);
325 #ifndef HAVE_SYS_PERSONALITY_H
326 static inline int personality(unsigned long persona
)
328 return syscall(__NR_personality
, persona
);
332 #endif /* __LXC_SYSCALL_WRAPPER_H */