chiark / gitweb /
abfe62a5eeab70ce5e79a95b3c728d1e0d4d07ea
[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 #if !HAVE_DECL_PIVOT_ROOT
27 static inline int pivot_root(const char *new_root, const char *put_old) {
28         return syscall(SYS_pivot_root, new_root, put_old);
29 }
30 #endif
31 #endif // 0
32
33 /* ======================================================================= */
34
35 #if !HAVE_DECL_MEMFD_CREATE
36 #  ifndef __NR_memfd_create
37 #    if defined __x86_64__
38 #      define __NR_memfd_create 319
39 #    elif defined __arm__
40 #      define __NR_memfd_create 385
41 #    elif defined __aarch64__
42 #      define __NR_memfd_create 279
43 #    elif defined __s390__
44 #      define __NR_memfd_create 350
45 #    elif defined _MIPS_SIM
46 #      if _MIPS_SIM == _MIPS_SIM_ABI32
47 #        define __NR_memfd_create 4354
48 #      endif
49 #      if _MIPS_SIM == _MIPS_SIM_NABI32
50 #        define __NR_memfd_create 6318
51 #      endif
52 #      if _MIPS_SIM == _MIPS_SIM_ABI64
53 #        define __NR_memfd_create 5314
54 #      endif
55 #    elif defined __i386__
56 #      define __NR_memfd_create 356
57 #    else
58 #      warning "__NR_memfd_create unknown for your architecture"
59 #    endif
60 #  endif
61
62 static inline int memfd_create(const char *name, unsigned int flags) {
63 #  ifdef __NR_memfd_create
64         return syscall(__NR_memfd_create, name, flags);
65 #  else
66         errno = ENOSYS;
67         return -1;
68 #  endif
69 }
70 #endif
71
72 /* ======================================================================= */
73
74 #if !HAVE_DECL_GETRANDOM
75 #  ifndef __NR_getrandom
76 #    if defined __x86_64__
77 #      define __NR_getrandom 318
78 #    elif defined(__i386__)
79 #      define __NR_getrandom 355
80 #    elif defined(__arm__)
81 #      define __NR_getrandom 384
82 #   elif defined(__aarch64__)
83 #      define __NR_getrandom 278
84 #    elif defined(__ia64__)
85 #      define __NR_getrandom 1339
86 #    elif defined(__m68k__)
87 #      define __NR_getrandom 352
88 #    elif defined(__s390x__)
89 #      define __NR_getrandom 349
90 #    elif defined(__powerpc__)
91 #      define __NR_getrandom 359
92 #    elif defined _MIPS_SIM
93 #      if _MIPS_SIM == _MIPS_SIM_ABI32
94 #        define __NR_getrandom 4353
95 #      endif
96 #      if _MIPS_SIM == _MIPS_SIM_NABI32
97 #        define __NR_getrandom 6317
98 #      endif
99 #      if _MIPS_SIM == _MIPS_SIM_ABI64
100 #        define __NR_getrandom 5313
101 #      endif
102 #    else
103 #      warning "__NR_getrandom unknown for your architecture"
104 #    endif
105 #  endif
106
107 static inline int getrandom(void *buffer, size_t count, unsigned flags) {
108 #  ifdef __NR_getrandom
109         return syscall(__NR_getrandom, buffer, count, flags);
110 #  else
111         errno = ENOSYS;
112         return -1;
113 #  endif
114 }
115 #endif
116
117 /* ======================================================================= */
118
119 #if !HAVE_DECL_GETTID
120 static inline pid_t gettid(void) {
121         return (pid_t) syscall(SYS_gettid);
122 }
123 #endif
124
125 /* ======================================================================= */
126
127 #if !HAVE_DECL_NAME_TO_HANDLE_AT
128 #  ifndef __NR_name_to_handle_at
129 #    if defined(__x86_64__)
130 #      define __NR_name_to_handle_at 303
131 #    elif defined(__i386__)
132 #      define __NR_name_to_handle_at 341
133 #    elif defined(__arm__)
134 #      define __NR_name_to_handle_at 370
135 #    elif defined(__powerpc__)
136 #      define __NR_name_to_handle_at 345
137 #    else
138 #      error "__NR_name_to_handle_at is not defined"
139 #    endif
140 #  endif
141
142 struct file_handle {
143         unsigned int handle_bytes;
144         int handle_type;
145         unsigned char f_handle[0];
146 };
147
148 static inline int name_to_handle_at(int fd, const char *name, struct file_handle *handle, int *mnt_id, int flags) {
149 #  ifdef __NR_name_to_handle_at
150         return syscall(__NR_name_to_handle_at, fd, name, handle, mnt_id, flags);
151 #  else
152         errno = ENOSYS;
153         return -1;
154 #  endif
155 }
156 #endif
157
158 /* ======================================================================= */
159
160 #if !HAVE_DECL_SETNS
161 #  ifndef __NR_setns
162 #    if defined(__x86_64__)
163 #      define __NR_setns 308
164 #    elif defined(__i386__)
165 #      define __NR_setns 346
166 #    else
167 #      error "__NR_setns is not defined"
168 #    endif
169 #  endif
170
171 static inline int setns(int fd, int nstype) {
172 #  ifdef __NR_setns
173         return syscall(__NR_setns, fd, nstype);
174 #  else
175         errno = ENOSYS;
176         return -1;
177 #  endif
178 }
179 #endif
180
181 /* ======================================================================= */
182
183 #if 0 /// UNNEEDED by elogind
184 static inline int raw_clone(unsigned long flags, void *child_stack) {
185 #if defined(__s390__) || defined(__CRIS__)
186         /* On s390 and cris the order of the first and second arguments
187          * of the raw clone() system call is reversed. */
188         return (int) syscall(__NR_clone, child_stack, flags);
189 #else
190         return (int) syscall(__NR_clone, flags, child_stack);
191 #endif
192 }
193
194 /* ======================================================================= */
195
196 static inline pid_t raw_getpid(void) {
197 #if defined(__alpha__)
198         return (pid_t) syscall(__NR_getxpid);
199 #else
200         return (pid_t) syscall(__NR_getpid);
201 #endif
202 }
203
204 /* ======================================================================= */
205
206 #if !HAVE_DECL_RENAMEAT2
207 #  ifndef __NR_renameat2
208 #    if defined __x86_64__
209 #      define __NR_renameat2 316
210 #    elif defined __arm__
211 #      define __NR_renameat2 382
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 #    else
225 #      warning "__NR_renameat2 unknown for your architecture"
226 #    endif
227 #  endif
228
229 static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
230 #  ifdef __NR_renameat2
231         return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
232 #  else
233         errno = ENOSYS;
234         return -1;
235 #  endif
236 }
237 #endif
238
239 /* ======================================================================= */
240
241 #if !HAVE_DECL_KCMP
242 static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
243 #  ifdef __NR_kcmp
244         return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
245 #  else
246         errno = ENOSYS;
247         return -1;
248 #  endif
249 }
250 #endif
251
252 /* ======================================================================= */
253
254 #if !HAVE_DECL_KEYCTL
255 static inline long keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4,unsigned long arg5) {
256 #  ifdef __NR_keyctl
257         return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
258 #  else
259         errno = ENOSYS;
260         return -1;
261 #  endif
262 }
263
264 static inline key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
265 #  ifdef __NR_add_key
266         return syscall(__NR_add_key, type, description, payload, plen, ringid);
267 #  else
268         errno = ENOSYS;
269         return -1;
270 #  endif
271 }
272
273 static inline key_serial_t request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
274 #  ifdef __NR_request_key
275         return syscall(__NR_request_key, type, description, callout_info, destringid);
276 #  else
277         errno = ENOSYS;
278         return -1;
279 #  endif
280 }
281 #endif
282 #endif // 0
283
284 /* ======================================================================= */
285
286 #if !HAVE_DECL_COPY_FILE_RANGE
287 #  ifndef __NR_copy_file_range
288 #    if defined(__x86_64__)
289 #      define __NR_copy_file_range 326
290 #    elif defined(__i386__)
291 #      define __NR_copy_file_range 377
292 #    elif defined __s390__
293 #      define __NR_copy_file_range 375
294 #    elif defined __arm__
295 #      define __NR_copy_file_range 391
296 #    elif defined __aarch64__
297 #      define __NR_copy_file_range 285
298 #    else
299 #      warning "__NR_copy_file_range not defined for your architecture"
300 #    endif
301 #  endif
302
303 static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
304                                       int fd_out, loff_t *off_out,
305                                       size_t len,
306                                       unsigned int flags) {
307 #  ifdef __NR_copy_file_range
308         return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
309 #  else
310         errno = ENOSYS;
311         return -1;
312 #  endif
313 }
314 #endif