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