]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/missing_syscall.h
Copy defines for renameat2 from casync (#6181)
[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 # elif defined __arc__
56 # define __NR_memfd_create 279
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 # elif defined(__arc__)
103 # define __NR_getrandom 278
104 # else
105 # warning "__NR_getrandom unknown for your architecture"
106 # endif
107 # endif
108
109 static inline int getrandom(void *buffer, size_t count, unsigned flags) {
110 # ifdef __NR_getrandom
111 return syscall(__NR_getrandom, buffer, count, flags);
112 # else
113 errno = ENOSYS;
114 return -1;
115 # endif
116 }
117 #endif
118
119 /* ======================================================================= */
120
121 #if !HAVE_DECL_GETTID
122 static inline pid_t gettid(void) {
123 return (pid_t) syscall(SYS_gettid);
124 }
125 #endif
126
127 /* ======================================================================= */
128
129 #if !HAVE_DECL_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 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 #endif
161
162 /* ======================================================================= */
163
164 #if !HAVE_DECL_SETNS
165 # ifndef __NR_setns
166 # if defined(__x86_64__)
167 # define __NR_setns 308
168 # elif defined(__i386__)
169 # define __NR_setns 346
170 # elif defined(__arc__)
171 # define __NR_setns 268
172 # else
173 # error "__NR_setns is not defined"
174 # endif
175 # endif
176
177 static inline int setns(int fd, int nstype) {
178 # ifdef __NR_setns
179 return syscall(__NR_setns, fd, nstype);
180 # else
181 errno = ENOSYS;
182 return -1;
183 # endif
184 }
185 #endif
186
187 /* ======================================================================= */
188
189 static inline pid_t raw_getpid(void) {
190 #if defined(__alpha__)
191 return (pid_t) syscall(__NR_getxpid);
192 #else
193 return (pid_t) syscall(__NR_getpid);
194 #endif
195 }
196
197 /* ======================================================================= */
198
199 #if !HAVE_DECL_RENAMEAT2
200 # ifndef __NR_renameat2
201 # if defined __x86_64__
202 # define __NR_renameat2 316
203 # elif defined __arm__
204 # define __NR_renameat2 382
205 # elif defined __aarch64__
206 # define __NR_renameat2 276
207 # elif defined _MIPS_SIM
208 # if _MIPS_SIM == _MIPS_SIM_ABI32
209 # define __NR_renameat2 4351
210 # endif
211 # if _MIPS_SIM == _MIPS_SIM_NABI32
212 # define __NR_renameat2 6315
213 # endif
214 # if _MIPS_SIM == _MIPS_SIM_ABI64
215 # define __NR_renameat2 5311
216 # endif
217 # elif defined __i386__
218 # define __NR_renameat2 353
219 # elif defined __powerpc64__
220 # define __NR_renameat2 357
221 # elif defined __s390__ || defined __s390x__
222 # define __NR_renameat2 347
223 # elif defined __arc__
224 # define __NR_renameat2 276
225 # else
226 # warning "__NR_renameat2 unknown for your architecture"
227 # endif
228 # endif
229
230 static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
231 # ifdef __NR_renameat2
232 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
233 # else
234 errno = ENOSYS;
235 return -1;
236 # endif
237 }
238 #endif
239
240 /* ======================================================================= */
241
242 #if !HAVE_DECL_KCMP
243 static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
244 # ifdef __NR_kcmp
245 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
246 # else
247 errno = ENOSYS;
248 return -1;
249 # endif
250 }
251 #endif
252
253 /* ======================================================================= */
254
255 #if !HAVE_DECL_KEYCTL
256 static inline long keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4,unsigned long arg5) {
257 # ifdef __NR_keyctl
258 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
259 # else
260 errno = ENOSYS;
261 return -1;
262 # endif
263 }
264
265 static inline key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
266 # ifdef __NR_add_key
267 return syscall(__NR_add_key, type, description, payload, plen, ringid);
268 # else
269 errno = ENOSYS;
270 return -1;
271 # endif
272 }
273
274 static inline key_serial_t request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
275 # ifdef __NR_request_key
276 return syscall(__NR_request_key, type, description, callout_info, destringid);
277 # else
278 errno = ENOSYS;
279 return -1;
280 # endif
281 }
282 #endif
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 # elif defined __powerpc__
299 # define __NR_copy_file_range 379
300 # elif defined __arc__
301 # define __NR_copy_file_range 285
302 # else
303 # warning "__NR_copy_file_range not defined for your architecture"
304 # endif
305 # endif
306
307 static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
308 int fd_out, loff_t *off_out,
309 size_t len,
310 unsigned int flags) {
311 # ifdef __NR_copy_file_range
312 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);
313 # else
314 errno = ENOSYS;
315 return -1;
316 # endif
317 }
318 #endif