]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/syscall_wrappers.h
github: Update for main branch
[mirror_lxc.git] / src / lxc / syscall_wrappers.h
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
b25291da
CB
2
3#ifndef __LXC_SYSCALL_WRAPPER_H
4#define __LXC_SYSCALL_WRAPPER_H
5
1160ce89
CB
6#include "config.h"
7
b25291da 8#include <asm/unistd.h>
303037d2 9#include <errno.h>
b25291da 10#include <linux/keyctl.h>
59524108 11#include <sched.h>
b25291da 12#include <stdint.h>
09996a48 13#include <sys/prctl.h>
b25291da
CB
14#include <sys/syscall.h>
15#include <sys/types.h>
16#include <unistd.h>
17
2b0c8106 18#include "macro.h"
bed09c9c 19#include "syscall_numbers.h"
b25291da 20
6a886ddf
CB
21#ifdef HAVE_LINUX_MEMFD_H
22#include <linux/memfd.h>
23#endif
24
303037d2
CB
25#ifdef HAVE_SYS_SIGNALFD_H
26#include <sys/signalfd.h>
27#endif
28
38608992
CB
29#if HAVE_SYS_PERSONALITY_H
30#include <sys/personality.h>
31#endif
32
b25291da
CB
33typedef int32_t key_serial_t;
34
35#if !HAVE_KEYCTL
36static inline long __keyctl(int cmd, unsigned long arg2, unsigned long arg3,
37 unsigned long arg4, unsigned long arg5)
38{
b25291da 39 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
b25291da
CB
40}
41#define keyctl __keyctl
42#endif
43
6400238d
CB
44#ifndef F_LINUX_SPECIFIC_BASE
45#define F_LINUX_SPECIFIC_BASE 1024
46#endif
47#ifndef F_ADD_SEALS
48#define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
49#define F_GET_SEALS (F_LINUX_SPECIFIC_BASE + 10)
50#endif
51#ifndef F_SEAL_SEAL
52#define F_SEAL_SEAL 0x0001
53#define F_SEAL_SHRINK 0x0002
54#define F_SEAL_GROW 0x0004
55#define F_SEAL_WRITE 0x0008
56#endif
57
db4af8c5 58#if !HAVE_MEMFD_CREATE
bed09c9c
CB
59static inline int memfd_create_lxc(const char *name, unsigned int flags)
60{
6a886ddf 61 return syscall(__NR_memfd_create, name, flags);
6a886ddf 62}
40b06c78 63#define memfd_create memfd_create_lxc
6a886ddf
CB
64#else
65extern int memfd_create(const char *name, unsigned int flags);
66#endif
67
db4af8c5 68#if !HAVE_PIVOT_ROOT
4ee86fda 69static inline int pivot_root(const char *new_root, const char *put_old)
6b3d24d7 70{
6b3d24d7 71 return syscall(__NR_pivot_root, new_root, put_old);
6b3d24d7
CB
72}
73#else
74extern int pivot_root(const char *new_root, const char *put_old);
75#endif
76
364932cf 77/* Define sethostname() if missing from the C library */
db4af8c5 78#if !HAVE_SETHOSTNAME
364932cf
CB
79static inline int sethostname(const char *name, size_t len)
80{
364932cf 81 return syscall(__NR_sethostname, name, len);
364932cf
CB
82}
83#endif
84
59524108 85/* Define setns() if missing from the C library */
db4af8c5 86#if !HAVE_SETNS
59524108
CB
87static inline int setns(int fd, int nstype)
88{
59524108 89 return syscall(__NR_setns, fd, nstype);
59524108
CB
90}
91#endif
92
db4af8c5 93#if !HAVE_SYS_SIGNALFD_H
303037d2
CB
94struct signalfd_siginfo {
95 uint32_t ssi_signo;
96 int32_t ssi_errno;
97 int32_t ssi_code;
98 uint32_t ssi_pid;
99 uint32_t ssi_uid;
100 int32_t ssi_fd;
101 uint32_t ssi_tid;
102 uint32_t ssi_band;
103 uint32_t ssi_overrun;
104 uint32_t ssi_trapno;
105 int32_t ssi_status;
106 int32_t ssi_int;
107 uint64_t ssi_ptr;
108 uint64_t ssi_utime;
109 uint64_t ssi_stime;
110 uint64_t ssi_addr;
111 uint8_t __pad[48];
112};
113
303037d2
CB
114static inline int signalfd(int fd, const sigset_t *mask, int flags)
115{
116 int retval;
117
118 retval = syscall(__NR_signalfd4, fd, mask, _NSIG / 8, flags);
3341e204 119#ifdef __NR_signalfd
303037d2
CB
120 if (errno == ENOSYS && flags == 0)
121 retval = syscall(__NR_signalfd, fd, mask, _NSIG / 8);
3341e204 122#endif
303037d2
CB
123
124 return retval;
125}
126#endif
127
e8f764b6 128/* Define unshare() if missing from the C library */
db4af8c5 129#if !HAVE_UNSHARE
e8f764b6
CB
130static inline int unshare(int flags)
131{
e8f764b6 132 return syscall(__NR_unshare, flags);
e8f764b6
CB
133}
134#else
135extern int unshare(int);
136#endif
137
bed09c9c 138/* Define faccessat() if missing from the C library */
db4af8c5 139#if !HAVE_FACCESSAT
bed09c9c
CB
140static int faccessat(int __fd, const char *__file, int __type, int __flag)
141{
142 return syscall(__NR_faccessat, __fd, __file, __type, __flag);
143}
144#endif
145
e8aaef81
CB
146#ifndef CLOSE_RANGE_UNSHARE
147#define CLOSE_RANGE_UNSHARE (1U << 1)
148#endif
149
150#ifndef CLOSE_RANGE_CLOEXEC
151#define CLOSE_RANGE_CLOEXEC (1U << 2)
152#endif
153
db4af8c5 154#if !HAVE_CLOSE_RANGE
e8aaef81
CB
155static inline int close_range(unsigned int fd, unsigned int max_fd, unsigned int flags)
156{
157 return syscall(__NR_close_range, fd, max_fd, flags);
158}
159#endif
160
db4af8c5 161#if !HAVE_SYS_PERSONALITY_H
3857c4eb
CB
162static inline int personality(unsigned long persona)
163{
164 return syscall(__NR_personality, persona);
165}
166#endif
167
09996a48
CB
168/* arg1 of prctl() */
169#ifndef PR_SCHED_CORE
170#define PR_SCHED_CORE 62
171#endif
172
173/* arg2 of prctl() */
174#ifndef PR_SCHED_CORE_GET
175#define PR_SCHED_CORE_GET 0
176#endif
177
178#ifndef PR_SCHED_CORE_CREATE
179#define PR_SCHED_CORE_CREATE 1 /* create unique core_sched cookie */
180#endif
181
182#ifndef PR_SCHED_CORE_SHARE_TO
183#define PR_SCHED_CORE_SHARE_TO 2 /* push core_sched cookie to pid */
184#endif
185
186#ifndef PR_SCHED_CORE_SHARE_FROM
187#define PR_SCHED_CORE_SHARE_FROM 3 /* pull core_sched cookie to pid */
188#endif
189
190#ifndef PR_SCHED_CORE_MAX
191#define PR_SCHED_CORE_MAX 4
192#endif
193
194/* arg3 of prctl() */
195#ifndef PR_SCHED_CORE_SCOPE_THREAD
196#define PR_SCHED_CORE_SCOPE_THREAD 0
197#endif
198
199#ifndef PR_SCHED_CORE_SCOPE_THREAD_GROUP
200#define PR_SCHED_CORE_SCOPE_THREAD_GROUP 1
201#endif
202
203#ifndef PR_SCHED_CORE_SCOPE_PROCESS_GROUP
204#define PR_SCHED_CORE_SCOPE_PROCESS_GROUP 2
205#endif
206
207#define INVALID_SCHED_CORE_COOKIE ((__u64)-1)
208
c958a332
CB
209static inline bool core_scheduling_cookie_valid(__u64 cookie)
210{
211 return (cookie > 0) && (cookie != INVALID_SCHED_CORE_COOKIE);
212}
213
f3d90fca 214static inline int core_scheduling_cookie_get(pid_t pid, __u64 *cookie)
09996a48 215{
09996a48
CB
216 int ret;
217
f3d90fca
CB
218 if (!cookie)
219 return ret_errno(EINVAL);
220
09996a48 221 ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, pid,
f3d90fca
CB
222 PR_SCHED_CORE_SCOPE_THREAD, (unsigned long)cookie);
223 if (ret) {
224 *cookie = INVALID_SCHED_CORE_COOKIE;
225 return -errno;
226 }
09996a48 227
f3d90fca 228 return 0;
09996a48
CB
229}
230
47bfecf5 231static inline int core_scheduling_cookie_create_threadgroup(pid_t pid)
09996a48
CB
232{
233 int ret;
234
235 ret = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, pid,
236 PR_SCHED_CORE_SCOPE_THREAD_GROUP, 0);
237 if (ret)
238 return -errno;
239
240 return 0;
241}
242
c958a332
CB
243static inline int core_scheduling_cookie_share_with(pid_t pid)
244{
245 return prctl(PR_SCHED_CORE, PR_SCHED_CORE_SHARE_FROM, pid,
246 PR_SCHED_CORE_SCOPE_THREAD, 0);
247}
248
b25291da 249#endif /* __LXC_SYSCALL_WRAPPER_H */