]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/missing_syscall.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / missing_syscall.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2f368e4a
ZJS
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
71e5200f
DM
26#include <sys/types.h>
27
4b9545f1 28#if !HAVE_PIVOT_ROOT
2f368e4a
ZJS
29static 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
4b9545f1 36#if !HAVE_MEMFD_CREATE
2f368e4a
ZJS
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
27b09f1f
AB
58# elif defined __arc__
59# define __NR_memfd_create 279
2f368e4a
ZJS
60# else
61# warning "__NR_memfd_create unknown for your architecture"
62# endif
63# endif
64
65static 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
4b9545f1 77#if !HAVE_GETRANDOM
2f368e4a
ZJS
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
27b09f1f
AB
105# elif defined(__arc__)
106# define __NR_getrandom 278
2f368e4a
ZJS
107# else
108# warning "__NR_getrandom unknown for your architecture"
109# endif
110# endif
111
112static 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
4b9545f1 124#if !HAVE_GETTID
2f368e4a
ZJS
125static inline pid_t gettid(void) {
126 return (pid_t) syscall(SYS_gettid);
127}
128#endif
129
130/* ======================================================================= */
131
4b9545f1 132#if !HAVE_NAME_TO_HANDLE_AT
2f368e4a
ZJS
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
27b09f1f
AB
142# elif defined(__arc__)
143# define __NR_name_to_handle_at 264
2f368e4a
ZJS
144# else
145# error "__NR_name_to_handle_at is not defined"
146# endif
147# endif
148
149struct file_handle {
150 unsigned int handle_bytes;
151 int handle_type;
152 unsigned char f_handle[0];
153};
154
155static 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
4b9545f1 167#if !HAVE_SETNS
2f368e4a
ZJS
168# ifndef __NR_setns
169# if defined(__x86_64__)
170# define __NR_setns 308
171# elif defined(__i386__)
172# define __NR_setns 346
27b09f1f
AB
173# elif defined(__arc__)
174# define __NR_setns 268
2f368e4a
ZJS
175# else
176# error "__NR_setns is not defined"
177# endif
178# endif
179
180static 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
2f368e4a
ZJS
192static 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
4b9545f1 202#if !HAVE_RENAMEAT2
2f368e4a
ZJS
203# ifndef __NR_renameat2
204# if defined __x86_64__
205# define __NR_renameat2 316
206# elif defined __arm__
207# define __NR_renameat2 382
925c81cd
LP
208# elif defined __aarch64__
209# define __NR_renameat2 276
2f368e4a
ZJS
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
92369d5e
ZJS
222# elif defined __powerpc64__
223# define __NR_renameat2 357
224# elif defined __s390__ || defined __s390x__
225# define __NR_renameat2 347
27b09f1f
AB
226# elif defined __arc__
227# define __NR_renameat2 276
2f368e4a
ZJS
228# else
229# warning "__NR_renameat2 unknown for your architecture"
230# endif
231# endif
232
233static 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
4b9545f1 245#if !HAVE_KCMP
2f368e4a
ZJS
246static 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
4b9545f1 258#if !HAVE_KEYCTL
2f368e4a
ZJS
259static 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
268static 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
277static 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
4b9545f1 289#if !HAVE_COPY_FILE_RANGE
2f368e4a
ZJS
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
4d07c8d3
AIB
301# elif defined __powerpc__
302# define __NR_copy_file_range 379
27b09f1f
AB
303# elif defined __arc__
304# define __NR_copy_file_range 285
2f368e4a
ZJS
305# else
306# warning "__NR_copy_file_range not defined for your architecture"
307# endif
308# endif
309
310static 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
71e5200f 322
213f2883
ZJS
323/* ======================================================================= */
324
4b9545f1 325#if !HAVE_BPF
71e5200f
DM
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 __sparc__
334# define __NR_bpf 349
335# elif defined __s390__
336# define __NR_bpf 351
337# else
338# warning "__NR_bpf not defined for your architecture"
339# endif
340# endif
341
342union bpf_attr;
343
344static inline int bpf(int cmd, union bpf_attr *attr, size_t size) {
345#ifdef __NR_bpf
346 return (int) syscall(__NR_bpf, cmd, attr, size);
347#else
348 errno = ENOSYS;
349 return -1;
350#endif
351}
352
353#endif
213f2883
ZJS
354
355/* ======================================================================= */
356
357#ifndef __IGNORE_pkey_mprotect
358# ifndef __NR_pkey_mprotect
359# if defined __i386__
360# define __NR_pkey_mprotect 380
361# elif defined __x86_64__
362# define __NR_pkey_mprotect 329
363# elif defined __arm__
364# define __NR_pkey_mprotect 394
365# elif defined __aarch64__
366# define __NR_pkey_mprotect 394
367# elif defined _MIPS_SIM
368# if _MIPS_SIM == _MIPS_SIM_ABI32
369# define __NR_pkey_mprotect 4363
370# endif
371# if _MIPS_SIM == _MIPS_SIM_NABI32
372# define __NR_pkey_mprotect 6327
373# endif
374# if _MIPS_SIM == _MIPS_SIM_ABI64
375# define __NR_pkey_mprotect 5323
376# endif
9c6888ac 377# else
213f2883
ZJS
378# warning "__NR_pkey_mprotect not defined for your architecture"
379# endif
380# endif
381#endif