]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/missing_syscall.h
Merge pull request #7582 from pfl/dhcp6_prefix_delegation
[thirdparty/systemd.git] / src / basic / missing_syscall.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8 Copyright 2016 Zbigniew Jędrzejewski-Szmek
9
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 /* Missing glibc definitions to access certain kernel APIs */
25
26 #include <sys/types.h>
27
28 #if !HAVE_PIVOT_ROOT
29 static inline int pivot_root(const char *new_root, const char *put_old) {
30 return syscall(SYS_pivot_root, new_root, put_old);
31 }
32 #endif
33
34 /* ======================================================================= */
35
36 #if !HAVE_MEMFD_CREATE
37 # ifndef __NR_memfd_create
38 # if defined __x86_64__
39 # define __NR_memfd_create 319
40 # elif defined __arm__
41 # define __NR_memfd_create 385
42 # elif defined __aarch64__
43 # define __NR_memfd_create 279
44 # elif defined __s390__
45 # define __NR_memfd_create 350
46 # elif defined _MIPS_SIM
47 # if _MIPS_SIM == _MIPS_SIM_ABI32
48 # define __NR_memfd_create 4354
49 # endif
50 # if _MIPS_SIM == _MIPS_SIM_NABI32
51 # define __NR_memfd_create 6318
52 # endif
53 # if _MIPS_SIM == _MIPS_SIM_ABI64
54 # define __NR_memfd_create 5314
55 # endif
56 # elif defined __i386__
57 # define __NR_memfd_create 356
58 # elif defined __arc__
59 # define __NR_memfd_create 279
60 # else
61 # warning "__NR_memfd_create unknown for your architecture"
62 # endif
63 # endif
64
65 static inline int memfd_create(const char *name, unsigned int flags) {
66 # ifdef __NR_memfd_create
67 return syscall(__NR_memfd_create, name, flags);
68 # else
69 errno = ENOSYS;
70 return -1;
71 # endif
72 }
73 #endif
74
75 /* ======================================================================= */
76
77 #if !HAVE_GETRANDOM
78 # ifndef __NR_getrandom
79 # if defined __x86_64__
80 # define __NR_getrandom 318
81 # elif defined(__i386__)
82 # define __NR_getrandom 355
83 # elif defined(__arm__)
84 # define __NR_getrandom 384
85 # elif defined(__aarch64__)
86 # define __NR_getrandom 278
87 # elif defined(__ia64__)
88 # define __NR_getrandom 1339
89 # elif defined(__m68k__)
90 # define __NR_getrandom 352
91 # elif defined(__s390x__)
92 # define __NR_getrandom 349
93 # elif defined(__powerpc__)
94 # define __NR_getrandom 359
95 # elif defined _MIPS_SIM
96 # if _MIPS_SIM == _MIPS_SIM_ABI32
97 # define __NR_getrandom 4353
98 # endif
99 # if _MIPS_SIM == _MIPS_SIM_NABI32
100 # define __NR_getrandom 6317
101 # endif
102 # if _MIPS_SIM == _MIPS_SIM_ABI64
103 # define __NR_getrandom 5313
104 # endif
105 # elif defined(__arc__)
106 # define __NR_getrandom 278
107 # else
108 # warning "__NR_getrandom unknown for your architecture"
109 # endif
110 # endif
111
112 static inline int getrandom(void *buffer, size_t count, unsigned flags) {
113 # ifdef __NR_getrandom
114 return syscall(__NR_getrandom, buffer, count, flags);
115 # else
116 errno = ENOSYS;
117 return -1;
118 # endif
119 }
120 #endif
121
122 /* ======================================================================= */
123
124 #if !HAVE_GETTID
125 static inline pid_t gettid(void) {
126 return (pid_t) syscall(SYS_gettid);
127 }
128 #endif
129
130 /* ======================================================================= */
131
132 #if !HAVE_NAME_TO_HANDLE_AT
133 # ifndef __NR_name_to_handle_at
134 # if defined(__x86_64__)
135 # define __NR_name_to_handle_at 303
136 # elif defined(__i386__)
137 # define __NR_name_to_handle_at 341
138 # elif defined(__arm__)
139 # define __NR_name_to_handle_at 370
140 # elif defined(__powerpc__)
141 # define __NR_name_to_handle_at 345
142 # elif defined(__arc__)
143 # define __NR_name_to_handle_at 264
144 # else
145 # error "__NR_name_to_handle_at is not defined"
146 # endif
147 # endif
148
149 struct file_handle {
150 unsigned int handle_bytes;
151 int handle_type;
152 unsigned char f_handle[0];
153 };
154
155 static inline int name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
156 # ifdef __NR_name_to_handle_at
157 return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
158 # else
159 errno = ENOSYS;
160 return -1;
161 # endif
162 }
163 #endif
164
165 /* ======================================================================= */
166
167 #if !HAVE_SETNS
168 # ifndef __NR_setns
169 # if defined(__x86_64__)
170 # define __NR_setns 308
171 # elif defined(__i386__)
172 # define __NR_setns 346
173 # elif defined(__arc__)
174 # define __NR_setns 268
175 # else
176 # error "__NR_setns is not defined"
177 # endif
178 # endif
179
180 static inline int setns(int fd, int nstype) {
181 # ifdef __NR_setns
182 return syscall(__NR_setns, fd, nstype);
183 # else
184 errno = ENOSYS;
185 return -1;
186 # endif
187 }
188 #endif
189
190 /* ======================================================================= */
191
192 static inline pid_t raw_getpid(void) {
193 #if defined(__alpha__)
194 return (pid_t) syscall(__NR_getxpid);
195 #else
196 return (pid_t) syscall(__NR_getpid);
197 #endif
198 }
199
200 /* ======================================================================= */
201
202 #if !HAVE_RENAMEAT2
203 # ifndef __NR_renameat2
204 # if defined __x86_64__
205 # define __NR_renameat2 316
206 # elif defined __arm__
207 # define __NR_renameat2 382
208 # elif defined __aarch64__
209 # define __NR_renameat2 276
210 # elif defined _MIPS_SIM
211 # if _MIPS_SIM == _MIPS_SIM_ABI32
212 # define __NR_renameat2 4351
213 # endif
214 # if _MIPS_SIM == _MIPS_SIM_NABI32
215 # define __NR_renameat2 6315
216 # endif
217 # if _MIPS_SIM == _MIPS_SIM_ABI64
218 # define __NR_renameat2 5311
219 # endif
220 # elif defined __i386__
221 # define __NR_renameat2 353
222 # elif defined __powerpc64__
223 # define __NR_renameat2 357
224 # elif defined __s390__ || defined __s390x__
225 # define __NR_renameat2 347
226 # elif defined __arc__
227 # define __NR_renameat2 276
228 # else
229 # warning "__NR_renameat2 unknown for your architecture"
230 # endif
231 # endif
232
233 static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
234 # ifdef __NR_renameat2
235 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
236 # else
237 errno = ENOSYS;
238 return -1;
239 # endif
240 }
241 #endif
242
243 /* ======================================================================= */
244
245 #if !HAVE_KCMP
246 static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
247 # ifdef __NR_kcmp
248 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
249 # else
250 errno = ENOSYS;
251 return -1;
252 # endif
253 }
254 #endif
255
256 /* ======================================================================= */
257
258 #if !HAVE_KEYCTL
259 static inline long keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4,unsigned long arg5) {
260 # ifdef __NR_keyctl
261 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
262 # else
263 errno = ENOSYS;
264 return -1;
265 # endif
266 }
267
268 static inline key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
269 # ifdef __NR_add_key
270 return syscall(__NR_add_key, type, description, payload, plen, ringid);
271 # else
272 errno = ENOSYS;
273 return -1;
274 # endif
275 }
276
277 static inline key_serial_t request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
278 # ifdef __NR_request_key
279 return syscall(__NR_request_key, type, description, callout_info, destringid);
280 # else
281 errno = ENOSYS;
282 return -1;
283 # endif
284 }
285 #endif
286
287 /* ======================================================================= */
288
289 #if !HAVE_COPY_FILE_RANGE
290 # ifndef __NR_copy_file_range
291 # if defined(__x86_64__)
292 # define __NR_copy_file_range 326
293 # elif defined(__i386__)
294 # define __NR_copy_file_range 377
295 # elif defined __s390__
296 # define __NR_copy_file_range 375
297 # elif defined __arm__
298 # define __NR_copy_file_range 391
299 # elif defined __aarch64__
300 # define __NR_copy_file_range 285
301 # elif defined __powerpc__
302 # define __NR_copy_file_range 379
303 # elif defined __arc__
304 # define __NR_copy_file_range 285
305 # else
306 # warning "__NR_copy_file_range not defined for your architecture"
307 # endif
308 # endif
309
310 static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
311 int fd_out, loff_t *off_out,
312 size_t len,
313 unsigned int flags) {
314 # ifdef __NR_copy_file_range
315 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
316 # else
317 errno = ENOSYS;
318 return -1;
319 # endif
320 }
321 #endif
322
323 /* ======================================================================= */
324
325 #if !HAVE_BPF
326 # ifndef __NR_bpf
327 # if defined __i386__
328 # define __NR_bpf 357
329 # elif defined __x86_64__
330 # define __NR_bpf 321
331 # elif defined __aarch64__
332 # define __NR_bpf 280
333 # elif defined __arm__
334 # define __NR_bpf 386
335 # elif defined __sparc__
336 # define __NR_bpf 349
337 # elif defined __s390__
338 # define __NR_bpf 351
339 # elif defined __tilegx__
340 # define __NR_bpf 280
341 # else
342 # warning "__NR_bpf not defined for your architecture"
343 # endif
344 # endif
345
346 union bpf_attr;
347
348 static inline int bpf(int cmd, union bpf_attr *attr, size_t size) {
349 #ifdef __NR_bpf
350 return (int) syscall(__NR_bpf, cmd, attr, size);
351 #else
352 errno = ENOSYS;
353 return -1;
354 #endif
355 }
356
357 #endif
358
359 /* ======================================================================= */
360
361 #ifndef __IGNORE_pkey_mprotect
362 # ifndef __NR_pkey_mprotect
363 # if defined __i386__
364 # define __NR_pkey_mprotect 380
365 # elif defined __x86_64__
366 # define __NR_pkey_mprotect 329
367 # elif defined __arm__
368 # define __NR_pkey_mprotect 394
369 # elif defined __aarch64__
370 # define __NR_pkey_mprotect 394
371 # elif defined _MIPS_SIM
372 # if _MIPS_SIM == _MIPS_SIM_ABI32
373 # define __NR_pkey_mprotect 4363
374 # endif
375 # if _MIPS_SIM == _MIPS_SIM_NABI32
376 # define __NR_pkey_mprotect 6327
377 # endif
378 # if _MIPS_SIM == _MIPS_SIM_ABI64
379 # define __NR_pkey_mprotect 5323
380 # endif
381 # else
382 # warning "__NR_pkey_mprotect not defined for your architecture"
383 # endif
384 # endif
385 #endif