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