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