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