]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/syscall_wrappers.h
syscall_wrappers: move unshare()
[mirror_lxc.git] / src / lxc / syscall_wrappers.h
CommitLineData
b25291da
CB
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 <linux/keyctl.h>
59524108 28#include <sched.h>
b25291da
CB
29#include <stdint.h>
30#include <sys/syscall.h>
31#include <sys/types.h>
32#include <unistd.h>
33
34#include "config.h"
35
6a886ddf
CB
36#ifdef HAVE_LINUX_MEMFD_H
37#include <linux/memfd.h>
38#endif
39
b25291da
CB
40typedef int32_t key_serial_t;
41
42#if !HAVE_KEYCTL
43static inline long __keyctl(int cmd, unsigned long arg2, unsigned long arg3,
44 unsigned long arg4, unsigned long arg5)
45{
46#ifdef __NR_keyctl
47 return syscall(__NR_keyctl, cmd, arg2, arg3, arg4, arg5);
48#else
49 errno = ENOSYS;
50 return -1;
51#endif
52}
53#define keyctl __keyctl
54#endif
55
6a886ddf
CB
56#ifndef HAVE_MEMFD_CREATE
57static inline int memfd_create(const char *name, unsigned int flags) {
58 #ifndef __NR_memfd_create
59 #if defined __i386__
60 #define __NR_memfd_create 356
61 #elif defined __x86_64__
62 #define __NR_memfd_create 319
63 #elif defined __arm__
64 #define __NR_memfd_create 385
65 #elif defined __aarch64__
66 #define __NR_memfd_create 279
67 #elif defined __s390__
68 #define __NR_memfd_create 350
69 #elif defined __powerpc__
70 #define __NR_memfd_create 360
71 #elif defined __sparc__
72 #define __NR_memfd_create 348
73 #elif defined __blackfin__
74 #define __NR_memfd_create 390
75 #elif defined __ia64__
76 #define __NR_memfd_create 1340
77 #elif defined _MIPS_SIM
78 #if _MIPS_SIM == _MIPS_SIM_ABI32
79 #define __NR_memfd_create 4354
80 #endif
81 #if _MIPS_SIM == _MIPS_SIM_NABI32
82 #define __NR_memfd_create 6318
83 #endif
84 #if _MIPS_SIM == _MIPS_SIM_ABI64
85 #define __NR_memfd_create 5314
86 #endif
87 #endif
88 #endif
89 #ifdef __NR_memfd_create
90 return syscall(__NR_memfd_create, name, flags);
91 #else
92 errno = ENOSYS;
93 return -1;
94 #endif
95}
96#else
97extern int memfd_create(const char *name, unsigned int flags);
98#endif
99
6b3d24d7
CB
100#if !HAVE_PIVOT_ROOT
101static int pivot_root(const char *new_root, const char *put_old)
102{
103#ifdef __NR_pivot_root
104 return syscall(__NR_pivot_root, new_root, put_old);
105#else
106 errno = ENOSYS;
107 return -1;
108#endif
109}
110#else
111extern int pivot_root(const char *new_root, const char *put_old);
112#endif
113
59524108
CB
114#if !defined(__NR_setns) && !defined(__NR_set_ns)
115 #if defined(__x86_64__)
116 #define __NR_setns 308
117 #elif defined(__i386__)
118 #define __NR_setns 346
119 #elif defined(__arm__)
120 #define __NR_setns 375
121 #elif defined(__aarch64__)
122 #define __NR_setns 375
123 #elif defined(__powerpc__)
124 #define __NR_setns 350
125 #elif defined(__s390__)
126 #define __NR_setns 339
127 #endif
128#endif
129
364932cf
CB
130/* Define sethostname() if missing from the C library */
131#ifndef HAVE_SETHOSTNAME
132static inline int sethostname(const char *name, size_t len)
133{
134#ifdef __NR_sethostname
135 return syscall(__NR_sethostname, name, len);
136#else
137 errno = ENOSYS;
138 return -1;
139#endif
140}
141#endif
142
59524108
CB
143/* Define setns() if missing from the C library */
144#ifndef HAVE_SETNS
145static inline int setns(int fd, int nstype)
146{
147#ifdef __NR_setns
148 return syscall(__NR_setns, fd, nstype);
149#elif defined(__NR_set_ns)
150 return syscall(__NR_set_ns, fd, nstype);
151#else
152 errno = ENOSYS;
153 return -1;
154#endif
155}
156#endif
157
e8f764b6
CB
158/* Define unshare() if missing from the C library */
159#ifndef HAVE_UNSHARE
160static inline int unshare(int flags)
161{
162#ifdef __NR_unshare
163 return syscall(__NR_unshare, flags);
164#else
165 errno = ENOSYS;
166 return -1;
167#endif
168}
169#else
170extern int unshare(int);
171#endif
172
b25291da 173#endif /* __LXC_SYSCALL_WRAPPER_H */