]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/syscall_wrappers.h
Merge pull request #2693 from stgraber/master
[mirror_lxc.git] / src / lxc / syscall_wrappers.h
1 /* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef __LXC_SYSCALL_WRAPPER_H
21 #define __LXC_SYSCALL_WRAPPER_H
22
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE 1
25 #endif
26 #include <asm/unistd.h>
27 #include <errno.h>
28 #include <linux/keyctl.h>
29 #include <sched.h>
30 #include <stdint.h>
31 #include <sys/syscall.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34
35 #include "config.h"
36
37 #ifdef HAVE_LINUX_MEMFD_H
38 #include <linux/memfd.h>
39 #endif
40
41 #ifdef HAVE_SYS_SIGNALFD_H
42 #include <sys/signalfd.h>
43 #endif
44
45 typedef int32_t key_serial_t;
46
47 #if !HAVE_KEYCTL
48 static inline long __keyctl(int cmd, unsigned long arg2, unsigned long arg3,
49 unsigned long arg4, unsigned long arg5)
50 {
51 #ifdef __NR_keyctl
52 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
53 #else
54 errno = ENOSYS;
55 return -1;
56 #endif
57 }
58 #define keyctl __keyctl
59 #endif
60
61 #ifndef HAVE_MEMFD_CREATE
62 static inline int memfd_create(const char *name, unsigned int flags) {
63 #ifndef __NR_memfd_create
64 #if defined __i386__
65 #define __NR_memfd_create 356
66 #elif defined __x86_64__
67 #define __NR_memfd_create 319
68 #elif defined __arm__
69 #define __NR_memfd_create 385
70 #elif defined __aarch64__
71 #define __NR_memfd_create 279
72 #elif defined __s390__
73 #define __NR_memfd_create 350
74 #elif defined __powerpc__
75 #define __NR_memfd_create 360
76 #elif defined __sparc__
77 #define __NR_memfd_create 348
78 #elif defined __blackfin__
79 #define __NR_memfd_create 390
80 #elif defined __ia64__
81 #define __NR_memfd_create 1340
82 #elif defined _MIPS_SIM
83 #if _MIPS_SIM == _MIPS_SIM_ABI32
84 #define __NR_memfd_create 4354
85 #endif
86 #if _MIPS_SIM == _MIPS_SIM_NABI32
87 #define __NR_memfd_create 6318
88 #endif
89 #if _MIPS_SIM == _MIPS_SIM_ABI64
90 #define __NR_memfd_create 5314
91 #endif
92 #endif
93 #endif
94 #ifdef __NR_memfd_create
95 return syscall(__NR_memfd_create, name, flags);
96 #else
97 errno = ENOSYS;
98 return -1;
99 #endif
100 }
101 #else
102 extern int memfd_create(const char *name, unsigned int flags);
103 #endif
104
105 #if !HAVE_PIVOT_ROOT
106 static int pivot_root(const char *new_root, const char *put_old)
107 {
108 #ifdef __NR_pivot_root
109 return syscall(__NR_pivot_root, new_root, put_old);
110 #else
111 errno = ENOSYS;
112 return -1;
113 #endif
114 }
115 #else
116 extern int pivot_root(const char *new_root, const char *put_old);
117 #endif
118
119 #if !defined(__NR_setns) && !defined(__NR_set_ns)
120 #if defined(__x86_64__)
121 #define __NR_setns 308
122 #elif defined(__i386__)
123 #define __NR_setns 346
124 #elif defined(__arm__)
125 #define __NR_setns 375
126 #elif defined(__aarch64__)
127 #define __NR_setns 375
128 #elif defined(__powerpc__)
129 #define __NR_setns 350
130 #elif defined(__s390__)
131 #define __NR_setns 339
132 #endif
133 #endif
134
135 /* Define sethostname() if missing from the C library */
136 #ifndef HAVE_SETHOSTNAME
137 static inline int sethostname(const char *name, size_t len)
138 {
139 #ifdef __NR_sethostname
140 return syscall(__NR_sethostname, name, len);
141 #else
142 errno = ENOSYS;
143 return -1;
144 #endif
145 }
146 #endif
147
148 /* Define setns() if missing from the C library */
149 #ifndef HAVE_SETNS
150 static inline int setns(int fd, int nstype)
151 {
152 #ifdef __NR_setns
153 return syscall(__NR_setns, fd, nstype);
154 #elif defined(__NR_set_ns)
155 return syscall(__NR_set_ns, fd, nstype);
156 #else
157 errno = ENOSYS;
158 return -1;
159 #endif
160 }
161 #endif
162
163 #ifndef HAVE_SYS_SIGNALFD_H
164 struct signalfd_siginfo {
165 uint32_t ssi_signo;
166 int32_t ssi_errno;
167 int32_t ssi_code;
168 uint32_t ssi_pid;
169 uint32_t ssi_uid;
170 int32_t ssi_fd;
171 uint32_t ssi_tid;
172 uint32_t ssi_band;
173 uint32_t ssi_overrun;
174 uint32_t ssi_trapno;
175 int32_t ssi_status;
176 int32_t ssi_int;
177 uint64_t ssi_ptr;
178 uint64_t ssi_utime;
179 uint64_t ssi_stime;
180 uint64_t ssi_addr;
181 uint8_t __pad[48];
182 };
183
184 #ifndef __NR_signalfd4
185 /* assume kernel headers are too old */
186 #if __i386__
187 #define __NR_signalfd4 327
188 #elif __x86_64__
189 #define __NR_signalfd4 289
190 #elif __powerpc__
191 #define __NR_signalfd4 313
192 #elif __s390x__
193 #define __NR_signalfd4 322
194 #elif __arm__
195 #define __NR_signalfd4 355
196 #elif __mips__ && _MIPS_SIM == _ABIO32
197 #define __NR_signalfd4 4324
198 #elif __mips__ && _MIPS_SIM == _ABI64
199 #define __NR_signalfd4 5283
200 #elif __mips__ && _MIPS_SIM == _ABIN32
201 #define __NR_signalfd4 6287
202 #endif
203 #endif
204
205 #ifndef __NR_signalfd
206 /* assume kernel headers are too old */
207 #if __i386__
208 #define __NR_signalfd 321
209 #elif __x86_64__
210 #define __NR_signalfd 282
211 #elif __powerpc__
212 #define __NR_signalfd 305
213 #elif __s390x__
214 #define __NR_signalfd 316
215 #elif __arm__
216 #define __NR_signalfd 349
217 #elif __mips__ && _MIPS_SIM == _ABIO32
218 #define __NR_signalfd 4317
219 #elif __mips__ && _MIPS_SIM == _ABI64
220 #define __NR_signalfd 5276
221 #elif __mips__ && _MIPS_SIM == _ABIN32
222 #define __NR_signalfd 6280
223 #endif
224 #endif
225
226 static inline int signalfd(int fd, const sigset_t *mask, int flags)
227 {
228 int retval;
229
230 retval = syscall(__NR_signalfd4, fd, mask, _NSIG / 8, flags);
231 if (errno == ENOSYS && flags == 0)
232 retval = syscall(__NR_signalfd, fd, mask, _NSIG / 8);
233
234 return retval;
235 }
236 #endif
237
238 /* Define unshare() if missing from the C library */
239 #ifndef HAVE_UNSHARE
240 static inline int unshare(int flags)
241 {
242 #ifdef __NR_unshare
243 return syscall(__NR_unshare, flags);
244 #else
245 errno = ENOSYS;
246 return -1;
247 #endif
248 }
249 #else
250 extern int unshare(int);
251 #endif
252
253 #endif /* __LXC_SYSCALL_WRAPPER_H */