4 This file is part of elogind.
6 Copyright 2010 Lennart Poettering
7 Copyright 2016 Zbigniew Jędrzejewski-Szmek
9 elogind 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.
14 elogind 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.
19 You should have received a copy of the GNU Lesser General Public License
20 along with elogind; If not, see <http://www.gnu.org/licenses/>.
23 /* Missing glibc definitions to access certain kernel APIs */
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);
31 /* ======================================================================= */
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
47 # if _MIPS_SIM == _MIPS_SIM_NABI32
48 # define __NR_memfd_create 6318
50 # if _MIPS_SIM == _MIPS_SIM_ABI64
51 # define __NR_memfd_create 5314
53 # elif defined __i386__
54 # define __NR_memfd_create 356
56 # warning "__NR_memfd_create unknown for your architecture"
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);
70 /* ======================================================================= */
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
94 # if _MIPS_SIM == _MIPS_SIM_NABI32
95 # define __NR_getrandom 6317
97 # if _MIPS_SIM == _MIPS_SIM_ABI64
98 # define __NR_getrandom 5313
101 # warning "__NR_getrandom unknown for your architecture"
105 static inline int getrandom(void *buffer, size_t count, unsigned flags) {
106 # ifdef __NR_getrandom
107 return syscall(__NR_getrandom, buffer, count, flags);
115 /* ======================================================================= */
117 #if !HAVE_DECL_GETTID
118 static inline pid_t gettid(void) {
119 return (pid_t) syscall(SYS_gettid);
123 /* ======================================================================= */
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
136 # error "__NR_name_to_handle_at is not defined"
141 unsigned int handle_bytes;
143 unsigned char f_handle[0];
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);
156 /* ======================================================================= */
160 # if defined(__x86_64__)
161 # define __NR_setns 308
162 # elif defined(__i386__)
163 # define __NR_setns 346
165 # error "__NR_setns is not defined"
169 static inline int setns(int fd, int nstype) {
171 return syscall(__NR_setns, fd, nstype);
179 /* ======================================================================= */
181 static inline int raw_clone(unsigned long flags, void *child_stack) {
182 #if defined(__s390__) || defined(__CRIS__)
183 /* On s390 and cris the order of the first and second arguments
184 * of the raw clone() system call is reversed. */
185 return (int) syscall(__NR_clone, child_stack, flags);
187 return (int) syscall(__NR_clone, flags, child_stack);
191 /* ======================================================================= */
193 static inline pid_t raw_getpid(void) {
194 #if defined(__alpha__)
195 return (pid_t) syscall(__NR_getxpid);
197 return (pid_t) syscall(__NR_getpid);
201 /* ======================================================================= */
203 #if !HAVE_DECL_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 _MIPS_SIM
210 # if _MIPS_SIM == _MIPS_SIM_ABI32
211 # define __NR_renameat2 4351
213 # if _MIPS_SIM == _MIPS_SIM_NABI32
214 # define __NR_renameat2 6315
216 # if _MIPS_SIM == _MIPS_SIM_ABI64
217 # define __NR_renameat2 5311
219 # elif defined __i386__
220 # define __NR_renameat2 353
222 # warning "__NR_renameat2 unknown for your architecture"
226 static inline int renameat2(int oldfd, const char *oldname, int newfd, const char *newname, unsigned flags) {
227 # ifdef __NR_renameat2
228 return syscall(__NR_renameat2, oldfd, oldname, newfd, newname, flags);
236 /* ======================================================================= */
239 static inline int kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1, unsigned long idx2) {
241 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
249 /* ======================================================================= */
251 #if !HAVE_DECL_KEYCTL
252 static inline long keyctl(int cmd, unsigned long arg2, unsigned long arg3, unsigned long arg4,unsigned long arg5) {
254 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
261 static inline key_serial_t add_key(const char *type, const char *description, const void *payload, size_t plen, key_serial_t ringid) {
263 return syscall(__NR_add_key, type, description, payload, plen, ringid);
270 static inline key_serial_t request_key(const char *type, const char *description, const char * callout_info, key_serial_t destringid) {
271 # ifdef __NR_request_key
272 return syscall(__NR_request_key, type, description, callout_info, destringid);
280 /* ======================================================================= */
282 #if !HAVE_DECL_COPY_FILE_RANGE
283 # ifndef __NR_copy_file_range
284 # if defined(__x86_64__)
285 # define __NR_copy_file_range 326
286 # elif defined(__i386__)
287 # define __NR_copy_file_range 377
288 # elif defined __s390__
289 # define __NR_copy_file_range 375
290 # elif defined __arm__
291 # define __NR_copy_file_range 391
292 # elif defined __aarch64__
293 # define __NR_copy_file_range 285
295 # warning "__NR_copy_file_range not defined for your architecture"
299 static inline ssize_t copy_file_range(int fd_in, loff_t *off_in,
300 int fd_out, loff_t *off_out,
302 unsigned int flags) {
303 # ifdef __NR_copy_file_range
304 return syscall(__NR_copy_file_range, fd_in, off_in, fd_out, off_out, len, flags);