]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/syscall_wrappers.h
Merge pull request #3204 from brauner/switch_to_spdx
[mirror_lxc.git] / src / lxc / syscall_wrappers.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_SYSCALL_WRAPPER_H
4 #define __LXC_SYSCALL_WRAPPER_H
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE 1
8 #endif
9 #include <asm/unistd.h>
10 #include <errno.h>
11 #include <linux/keyctl.h>
12 #include <sched.h>
13 #include <stdint.h>
14 #include <sys/syscall.h>
15 #include <sys/types.h>
16 #include <unistd.h>
17
18 #include "config.h"
19
20 #ifdef HAVE_LINUX_MEMFD_H
21 #include <linux/memfd.h>
22 #endif
23
24 #ifdef HAVE_SYS_SIGNALFD_H
25 #include <sys/signalfd.h>
26 #endif
27
28 typedef int32_t key_serial_t;
29
30 #if !HAVE_KEYCTL
31 static inline long __keyctl(int cmd, unsigned long arg2, unsigned long arg3,
32 unsigned long arg4, unsigned long arg5)
33 {
34 #ifdef __NR_keyctl
35 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
36 #else
37 errno = ENOSYS;
38 return -1;
39 #endif
40 }
41 #define keyctl __keyctl
42 #endif
43
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
58 #ifndef HAVE_MEMFD_CREATE
59 static inline int memfd_create_lxc(const char *name, unsigned int flags) {
60 #ifndef __NR_memfd_create
61 #if defined __i386__
62 #define __NR_memfd_create 356
63 #elif defined __x86_64__
64 #define __NR_memfd_create 319
65 #elif defined __arm__
66 #define __NR_memfd_create 385
67 #elif defined __aarch64__
68 #define __NR_memfd_create 279
69 #elif defined __s390__
70 #define __NR_memfd_create 350
71 #elif defined __powerpc__
72 #define __NR_memfd_create 360
73 #elif defined __sparc__
74 #define __NR_memfd_create 348
75 #elif defined __blackfin__
76 #define __NR_memfd_create 390
77 #elif defined __ia64__
78 #define __NR_memfd_create 1340
79 #elif defined _MIPS_SIM
80 #if _MIPS_SIM == _MIPS_SIM_ABI32
81 #define __NR_memfd_create 4354
82 #endif
83 #if _MIPS_SIM == _MIPS_SIM_NABI32
84 #define __NR_memfd_create 6318
85 #endif
86 #if _MIPS_SIM == _MIPS_SIM_ABI64
87 #define __NR_memfd_create 5314
88 #endif
89 #endif
90 #endif
91 #ifdef __NR_memfd_create
92 return syscall(__NR_memfd_create, name, flags);
93 #else
94 errno = ENOSYS;
95 return -1;
96 #endif
97 }
98 #define memfd_create memfd_create_lxc
99 #else
100 extern int memfd_create(const char *name, unsigned int flags);
101 #endif
102
103 #if !HAVE_PIVOT_ROOT
104 static int pivot_root(const char *new_root, const char *put_old)
105 {
106 #ifdef __NR_pivot_root
107 return syscall(__NR_pivot_root, new_root, put_old);
108 #else
109 errno = ENOSYS;
110 return -1;
111 #endif
112 }
113 #else
114 extern int pivot_root(const char *new_root, const char *put_old);
115 #endif
116
117 #if !defined(__NR_setns) && !defined(__NR_set_ns)
118 #if defined(__x86_64__)
119 #define __NR_setns 308
120 #elif defined(__i386__)
121 #define __NR_setns 346
122 #elif defined(__arm__)
123 #define __NR_setns 375
124 #elif defined(__aarch64__)
125 #define __NR_setns 375
126 #elif defined(__powerpc__)
127 #define __NR_setns 350
128 #elif defined(__s390__)
129 #define __NR_setns 339
130 #endif
131 #endif
132
133 /* Define sethostname() if missing from the C library */
134 #ifndef HAVE_SETHOSTNAME
135 static inline int sethostname(const char *name, size_t len)
136 {
137 #ifdef __NR_sethostname
138 return syscall(__NR_sethostname, name, len);
139 #else
140 errno = ENOSYS;
141 return -1;
142 #endif
143 }
144 #endif
145
146 /* Define setns() if missing from the C library */
147 #ifndef HAVE_SETNS
148 static inline int setns(int fd, int nstype)
149 {
150 #ifdef __NR_setns
151 return syscall(__NR_setns, fd, nstype);
152 #elif defined(__NR_set_ns)
153 return syscall(__NR_set_ns, fd, nstype);
154 #else
155 errno = ENOSYS;
156 return -1;
157 #endif
158 }
159 #endif
160
161 #ifndef HAVE_SYS_SIGNALFD_H
162 struct signalfd_siginfo {
163 uint32_t ssi_signo;
164 int32_t ssi_errno;
165 int32_t ssi_code;
166 uint32_t ssi_pid;
167 uint32_t ssi_uid;
168 int32_t ssi_fd;
169 uint32_t ssi_tid;
170 uint32_t ssi_band;
171 uint32_t ssi_overrun;
172 uint32_t ssi_trapno;
173 int32_t ssi_status;
174 int32_t ssi_int;
175 uint64_t ssi_ptr;
176 uint64_t ssi_utime;
177 uint64_t ssi_stime;
178 uint64_t ssi_addr;
179 uint8_t __pad[48];
180 };
181
182 #ifndef __NR_signalfd4
183 /* assume kernel headers are too old */
184 #if __i386__
185 #define __NR_signalfd4 327
186 #elif __x86_64__
187 #define __NR_signalfd4 289
188 #elif __powerpc__
189 #define __NR_signalfd4 313
190 #elif __s390x__
191 #define __NR_signalfd4 322
192 #elif __arm__
193 #define __NR_signalfd4 355
194 #elif __mips__ && _MIPS_SIM == _ABIO32
195 #define __NR_signalfd4 4324
196 #elif __mips__ && _MIPS_SIM == _ABI64
197 #define __NR_signalfd4 5283
198 #elif __mips__ && _MIPS_SIM == _ABIN32
199 #define __NR_signalfd4 6287
200 #endif
201 #endif
202
203 #ifndef __NR_signalfd
204 /* assume kernel headers are too old */
205 #if __i386__
206 #define __NR_signalfd 321
207 #elif __x86_64__
208 #define __NR_signalfd 282
209 #elif __powerpc__
210 #define __NR_signalfd 305
211 #elif __s390x__
212 #define __NR_signalfd 316
213 #elif __arm__
214 #define __NR_signalfd 349
215 #elif __mips__ && _MIPS_SIM == _ABIO32
216 #define __NR_signalfd 4317
217 #elif __mips__ && _MIPS_SIM == _ABI64
218 #define __NR_signalfd 5276
219 #elif __mips__ && _MIPS_SIM == _ABIN32
220 #define __NR_signalfd 6280
221 #endif
222 #endif
223
224 static inline int signalfd(int fd, const sigset_t *mask, int flags)
225 {
226 int retval;
227
228 retval = syscall(__NR_signalfd4, fd, mask, _NSIG / 8, flags);
229 if (errno == ENOSYS && flags == 0)
230 retval = syscall(__NR_signalfd, fd, mask, _NSIG / 8);
231
232 return retval;
233 }
234 #endif
235
236 /* Define unshare() if missing from the C library */
237 #ifndef HAVE_UNSHARE
238 static inline int unshare(int flags)
239 {
240 #ifdef __NR_unshare
241 return syscall(__NR_unshare, flags);
242 #else
243 errno = ENOSYS;
244 return -1;
245 #endif
246 }
247 #else
248 extern int unshare(int);
249 #endif
250
251 #endif /* __LXC_SYSCALL_WRAPPER_H */