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