chiark / gitweb /
xattr-util: use crtime/btime if statx() is available for implementation of fd_setcrti...
[elogind.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 #if 0 /// UNNEEDED by elogind
27 #include <sys/types.h>
28
29 #if !HAVE_PIVOT_ROOT
30 static inline int pivot_root(const char *new_root, const char *put_old) {
31         return syscall(SYS_pivot_root, new_root, put_old);
32 }
33 #endif
34 #endif // 0
35
36 /* ======================================================================= */
37
38 #if !HAVE_MEMFD_CREATE
39 #  ifndef __NR_memfd_create
40 #    if defined __x86_64__
41 #      define __NR_memfd_create 319
42 #    elif defined __arm__
43 #      define __NR_memfd_create 385
44 #    elif defined __aarch64__
45 #      define __NR_memfd_create 279
46 #    elif defined __s390__
47 #      define __NR_memfd_create 350
48 #    elif defined _MIPS_SIM
49 #      if _MIPS_SIM == _MIPS_SIM_ABI32
50 #        define __NR_memfd_create 4354
51 #      endif
52 #      if _MIPS_SIM == _MIPS_SIM_NABI32
53 #        define __NR_memfd_create 6318
54 #      endif
55 #      if _MIPS_SIM == _MIPS_SIM_ABI64
56 #        define __NR_memfd_create 5314
57 #      endif
58 #    elif defined __i386__
59 #      define __NR_memfd_create 356
60 #    elif defined __arc__
61 #      define __NR_memfd_create 279
62 #    else
63 #      warning "__NR_memfd_create unknown for your architecture"
64 #    endif
65 #  endif
66
67 static inline int memfd_create(const char *name, unsigned int flags) {
68 #  ifdef __NR_memfd_create
69         return syscall(__NR_memfd_create, name, flags);
70 #  else
71         errno = ENOSYS;
72         return -1;
73 #  endif
74 }
75 #endif
76
77 /* ======================================================================= */
78
79 #if !HAVE_GETRANDOM
80 #  ifndef __NR_getrandom
81 #    if defined __x86_64__
82 #      define __NR_getrandom 318
83 #    elif defined(__i386__)
84 #      define __NR_getrandom 355
85 #    elif defined(__arm__)
86 #      define __NR_getrandom 384
87 #   elif defined(__aarch64__)
88 #      define __NR_getrandom 278
89 #    elif defined(__ia64__)
90 #      define __NR_getrandom 1339
91 #    elif defined(__m68k__)
92 #      define __NR_getrandom 352
93 #    elif defined(__s390x__)
94 #      define __NR_getrandom 349
95 #    elif defined(__powerpc__)
96 #      define __NR_getrandom 359
97 #    elif defined _MIPS_SIM
98 #      if _MIPS_SIM == _MIPS_SIM_ABI32
99 #        define __NR_getrandom 4353
100 #      endif
101 #      if _MIPS_SIM == _MIPS_SIM_NABI32
102 #        define __NR_getrandom 6317
103 #      endif
104 #      if _MIPS_SIM == _MIPS_SIM_ABI64
105 #        define __NR_getrandom 5313
106 #      endif
107 #    elif defined(__arc__)
108 #      define __NR_getrandom 278
109 #    else
110 #      warning "__NR_getrandom unknown for your architecture"
111 #    endif
112 #  endif
113
114 static inline int getrandom(void *buffer, size_t count, unsigned flags) {
115 #  ifdef __NR_getrandom
116         return syscall(__NR_getrandom, buffer, count, flags);
117 #  else
118         errno = ENOSYS;
119         return -1;
120 #  endif
121 }
122 #endif
123
124 /* ======================================================================= */
125
126 #if !HAVE_GETTID
127 static inline pid_t gettid(void) {
128         return (pid_t) syscall(SYS_gettid);
129 }
130 #endif
131
132 /* ======================================================================= */
133
134 #if !HAVE_NAME_TO_HANDLE_AT
135 #  ifndef __NR_name_to_handle_at
136 #    if defined(__x86_64__)
137 #      define __NR_name_to_handle_at 303
138 #    elif defined(__i386__)
139 #      define __NR_name_to_handle_at 341
140 #    elif defined(__arm__)
141 #      define __NR_name_to_handle_at 370
142 #    elif defined(__powerpc__)
143 #      define __NR_name_to_handle_at 345
144 #    elif defined(__arc__)
145 #      define __NR_name_to_handle_at 264
146 #    else
147 #      error "__NR_name_to_handle_at is not defined"
148 #    endif
149 #  endif
150
151 struct file_handle {
152         unsigned int handle_bytes;
153         int handle_type;
154         unsigned char f_handle[0];
155 };
156
157 static inline int name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
158 #  ifdef __NR_name_to_handle_at
159         return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
160 #  else
161         errno = ENOSYS;
162         return -1;
163 #  endif
164 }
165 #endif
166
167 /* ======================================================================= */
168
169 #if !HAVE_SETNS
170 #  ifndef __NR_setns
171 #    if defined(__x86_64__)
172 #      define __NR_setns 308
173 #    elif defined(__i386__)
174 #      define __NR_setns 346
175 #    elif defined(__arc__)
176 #      define __NR_setns 268
177 #    else
178 #      error "__NR_setns is not defined"
179 #    endif
180 #  endif
181
182 static inline int setns(int fd, int nstype) {
183 #  ifdef __NR_setns
184         return syscall(__NR_setns, fd, nstype);
185 #  else
186         errno = ENOSYS;
187         return -1;
188 #  endif
189 }
190 #endif
191
192 /* ======================================================================= */
193
194 static inline pid_t raw_getpid(void) {
195 #if defined(__alpha__)
196         return (pid_t) syscall(__NR_getxpid);
197 #else
198         return (pid_t) syscall(__NR_getpid);
199 #endif
200 }
201
202 /* ======================================================================= */
203
204 #if !HAVE_RENAMEAT2
205 #  ifndef __NR_renameat2
206 #    if defined __x86_64__
207 #      define __NR_renameat2 316
208 #    elif defined __arm__
209 #      define __NR_renameat2 382
210 #    elif defined __aarch64__
211 #      define __NR_renameat2 276
212 #    elif defined _MIPS_SIM
213 #      if _MIPS_SIM == _MIPS_SIM_ABI32
214 #        define __NR_renameat2 4351
215 #      endif
216 #      if _MIPS_SIM == _MIPS_SIM_NABI32
217 #        define __NR_renameat2 6315
218 #      endif
219 #      if _MIPS_SIM == _MIPS_SIM_ABI64
220 #        define __NR_renameat2 5311
221 #      endif
222 #    elif defined __i386__
223 #      define __NR_renameat2 353
224 #    elif defined __powerpc64__
225 #      define __NR_renameat2 357
226 #    elif defined __s390__ || defined __s390x__
227 #      define __NR_renameat2 347
228 #    elif defined __arc__
229 #      define __NR_renameat2 276
230 #    else
231 #      warning "__NR_renameat2 unknown for your architecture"
232 #    endif
233 #  endif
234
235 static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
236 #  ifdef __NR_renameat2
237         return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
238 #  else
239         errno = ENOSYS;
240         return -1;
241 #  endif
242 }
243 #endif
244
245 /* ======================================================================= */
246
247 #if !HAVE_KCMP
248 static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
249 #  ifdef __NR_kcmp
250         return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
251 #  else
252         errno = ENOSYS;
253         return -1;
254 #  endif
255 }
256 #endif
257
258 /* ======================================================================= */
259
260 #if !HAVE_KEYCTL
261 static inline long keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4,unsigned long arg5) {
262 #  ifdef __NR_keyctl
263         return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
264 #  else
265         errno = ENOSYS;
266         return -1;
267 #  endif
268 }
269
270 static inline key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
271 #  ifdef __NR_add_key
272         return syscall(__NR_add_key, type, description, payload, plen, ringid);
273 #  else
274         errno = ENOSYS;
275         return -1;
276 #  endif
277 }
278
279 static inline key_serial_t request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
280 #  ifdef __NR_request_key
281         return syscall(__NR_request_key, type, description, callout_info, destringid);
282 #  else
283         errno = ENOSYS;
284         return -1;
285 #  endif
286 }
287 #endif
288
289 /* ======================================================================= */
290
291 #if !HAVE_COPY_FILE_RANGE
292 #  ifndef __NR_copy_file_range
293 #    if defined(__x86_64__)
294 #      define __NR_copy_file_range 326
295 #    elif defined(__i386__)
296 #      define __NR_copy_file_range 377
297 #    elif defined __s390__
298 #      define __NR_copy_file_range 375
299 #    elif defined __arm__
300 #      define __NR_copy_file_range 391
301 #    elif defined __aarch64__
302 #      define __NR_copy_file_range 285
303 #    elif defined __powerpc__
304 #      define __NR_copy_file_range 379
305 #    elif defined __arc__
306 #      define __NR_copy_file_range 285
307 #    else
308 #      warning "__NR_copy_file_range not defined for your architecture"
309 #    endif
310 #  endif
311
312 static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
313                                       int fd_out, loff_t *off_out,
314                                       size_t len,
315                                       unsigned int flags) {
316 #  ifdef __NR_copy_file_range
317         return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
318 #  else
319         errno = ENOSYS;
320         return -1;
321 #  endif
322 }
323 #endif
324
325 /* ======================================================================= */
326
327 #if !HAVE_BPF
328 #  ifndef __NR_bpf
329 #    if defined __i386__
330 #      define __NR_bpf 357
331 #    elif defined __x86_64__
332 #      define __NR_bpf 321
333 #    elif defined __aarch64__
334 #      define __NR_bpf 280
335 #    elif defined __arm__
336 #      define __NR_bpf 386
337 #    elif defined __sparc__
338 #      define __NR_bpf 349
339 #    elif defined __s390__
340 #      define __NR_bpf 351
341 #    elif defined __tilegx__
342 #      define __NR_bpf 280
343 #    else
344 #      warning "__NR_bpf not defined for your architecture"
345 #    endif
346 #  endif
347
348 union bpf_attr;
349
350 static inline int bpf(int cmd, union bpf_attr *attr, size_t size) {
351 #ifdef __NR_bpf
352         return (int) syscall(__NR_bpf, cmd, attr, size);
353 #else
354         errno = ENOSYS;
355         return -1;
356 #endif
357 }
358
359 #endif
360
361 /* ======================================================================= */
362
363 #ifndef __IGNORE_pkey_mprotect
364 #  ifndef __NR_pkey_mprotect
365 #    if defined __i386__
366 #      define __NR_pkey_mprotect 380
367 #    elif defined __x86_64__
368 #      define __NR_pkey_mprotect 329
369 #    elif defined __arm__
370 #      define __NR_pkey_mprotect 394
371 #    elif defined __aarch64__
372 #      define __NR_pkey_mprotect 394
373 #    elif defined _MIPS_SIM
374 #      if _MIPS_SIM == _MIPS_SIM_ABI32
375 #        define __NR_pkey_mprotect 4363
376 #      endif
377 #      if _MIPS_SIM == _MIPS_SIM_NABI32
378 #        define __NR_pkey_mprotect 6327
379 #      endif
380 #      if _MIPS_SIM == _MIPS_SIM_ABI64
381 #        define __NR_pkey_mprotect 5323
382 #      endif
383 #    else
384 #      warning "__NR_pkey_mprotect not defined for your architecture"
385 #    endif
386 #  endif
387 #endif
388
389 #if !HAVE_STATX
390 #  ifndef __NR_statx
391 #    if defined __i386__
392 #      define __NR_bpf 383
393 #    elif defined __x86_64__
394 #      define __NR_bpf 332
395 #    else
396 #      warning "__NR_statx not defined for your architecture"
397 #    endif
398 #  endif
399
400 struct statx;
401
402 static inline ssize_t statx(int dfd, const char *filename, unsigned flags, unsigned int mask, struct statx *buffer) {
403 #  ifdef __NR_statx
404         return syscall(__NR_statx, dfd, filename, flags, mask, buffer);
405 #  else
406         errno = ENOSYS;
407         return -1;
408 #  endif
409 }
410 #endif