]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/macro.h
Merge pull request #2435 from brauner/2018-06-27/storage_managed
[mirror_lxc.git] / src / lxc / macro.h
CommitLineData
279c45ee
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_MACRO_H
21#define __LXC_MACRO_H
22
85de58d6
CB
23#include "config.h"
24
25#include <asm/types.h>
26#include <linux/if_link.h>
27#include <linux/loop.h>
28#include <linux/netlink.h>
29#include <linux/rtnetlink.h>
245532a2 30#include <stdint.h>
2259663c 31#include <string.h>
85de58d6
CB
32#include <sys/mount.h>
33#include <sys/socket.h>
b1234129 34#include <sys/un.h>
2955a58a 35#include <unistd.h>
85de58d6 36
279c45ee
CB
37/* Define __S_ISTYPE if missing from the C library. */
38#ifndef __S_ISTYPE
39#define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask))
40#endif
41
ba2b6354
CB
42/* capabilities */
43#ifndef CAP_SYS_ADMIN
44#define CAP_SYS_ADMIN 21
45#endif
46
279c45ee
CB
47#ifndef CAP_SETFCAP
48#define CAP_SETFCAP 31
49#endif
50
51#ifndef CAP_MAC_OVERRIDE
52#define CAP_MAC_OVERRIDE 32
53#endif
54
55#ifndef CAP_MAC_ADMIN
56#define CAP_MAC_ADMIN 33
57#endif
279c45ee 58
279c45ee
CB
59#ifndef CAP_SETUID
60#define CAP_SETUID 7
61#endif
62
63#ifndef CAP_SETGID
64#define CAP_SETGID 6
65#endif
66
ba2b6354 67/* prctl */
1f207a5c
CB
68#ifndef PR_CAPBSET_READ
69#define PR_CAPBSET_READ 23
70#endif
71
ba2b6354 72#ifndef PR_CAPBSET_DROP
604ca1c0
CB
73#define PR_CAPBSET_DROP 24
74#endif
75
1f207a5c
CB
76/* Control the ambient capability set */
77#ifndef PR_CAP_AMBIENT
78#define PR_CAP_AMBIENT 47
79#endif
80
81#ifndef PR_CAP_AMBIENT_IS_SET
82#define PR_CAP_AMBIENT_IS_SET 1
83#endif
84
85#ifndef PR_CAP_AMBIENT_RAISE
86#define PR_CAP_AMBIENT_RAISE 2
87#endif
88
89#ifndef PR_CAP_AMBIENT_LOWER
90#define PR_CAP_AMBIENT_LOWER 3
91#endif
92
93#ifndef PR_CAP_AMBIENT_CLEAR_ALL
94#define PR_CAP_AMBIENT_CLEAR_ALL 4
95#endif
96
ba2b6354 97#ifndef PR_SET_NO_NEW_PRIVS
604ca1c0
CB
98#define PR_SET_NO_NEW_PRIVS 38
99#endif
100
ba2b6354 101#ifndef PR_GET_NO_NEW_PRIVS
604ca1c0
CB
102#define PR_GET_NO_NEW_PRIVS 39
103#endif
104
ba2b6354 105/* filesystem magic values */
279c45ee
CB
106#ifndef CGROUP_SUPER_MAGIC
107#define CGROUP_SUPER_MAGIC 0x27e0eb
108#endif
109
110#ifndef CGROUP2_SUPER_MAGIC
111#define CGROUP2_SUPER_MAGIC 0x63677270
112#endif
113
f26dc127
CB
114#ifndef NSFS_MAGIC
115#define NSFS_MAGIC 0x6e736673
116#endif
117
ba2b6354 118/* current overlayfs */
37ef15bb
CB
119#ifndef OVERLAY_SUPER_MAGIC
120#define OVERLAY_SUPER_MAGIC 0x794c7630
121#endif
122
ba2b6354
CB
123/* legacy overlayfs */
124#ifndef OVERLAYFS_SUPER_MAGIC
125#define OVERLAYFS_SUPER_MAGIC 0x794c764f
126#endif
127
f246d9b8
CB
128/* Calculate the number of chars needed to represent a given integer as a C
129 * string. Include room for '-' to indicate negative numbers and the \0 byte.
130 * This is based on systemd.
131 */
132#define INTTYPE_TO_STRLEN(type) \
133 (2 + (sizeof(type) <= 1 \
134 ? 3 \
135 : sizeof(type) <= 2 \
136 ? 5 \
137 : sizeof(type) <= 4 \
138 ? 10 \
139 : sizeof(type) <= 8 \
140 ? 20 \
141 : sizeof(int[-2 * (sizeof(type) > 8)])))
142
279c45ee 143/* Useful macros */
279c45ee
CB
144#define LXC_LINELEN 4096
145#define LXC_IDMAPLEN 4096
146#define LXC_MAX_BUFFER 4096
0c5ea884 147
279c45ee
CB
148/* /proc/ = 6
149 * +
0c5ea884 150 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
279c45ee
CB
151 * +
152 * /fd/ = 4
153 * +
0c5ea884 154 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
279c45ee
CB
155 * +
156 * \0 = 1
157 */
2955a58a 158#define LXC_PROC_PID_FD_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
0c5ea884
CB
159
160/* /proc/pid-to-str/status\0 = (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1) */
161#define LXC_PROC_STATUS_LEN (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
162
163/* /proc/pid-to-str/attr/current = (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1) */
164#define LXC_LSMATTRLEN (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
165
9b8d4c58 166#define LXC_CMD_DATA_MAX (MAXPATHLEN * 2)
279c45ee
CB
167
168/* loop devices */
169#ifndef LO_FLAGS_AUTOCLEAR
170#define LO_FLAGS_AUTOCLEAR 4
171#endif
172
173#ifndef LOOP_CTL_GET_FREE
174#define LOOP_CTL_GET_FREE 0x4C82
175#endif
176
177/* memfd_create() */
178#ifndef MFD_CLOEXEC
179#define MFD_CLOEXEC 0x0001U
180#endif
181
182#ifndef MFD_ALLOW_SEALING
183#define MFD_ALLOW_SEALING 0x0002U
184#endif
185
186/**
187 * BUILD_BUG_ON - break compile if a condition is true.
188 * @condition: the condition which the compiler should know is false.
189 *
190 * If you have some code which relies on certain constants being equal, or
191 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
192 * detect if someone changes it.
193 *
194 * The implementation uses gcc's reluctance to create a negative array, but
195 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
196 * to inline functions). So as a fallback we use the optimizer; if it can't
197 * prove the condition is false, it will cause a link error on the undefined
198 * "__build_bug_on_failed". This error message can be harder to track down
199 * though, hence the two different methods.
200 */
201#ifndef __OPTIMIZE__
ba2b6354 202#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
279c45ee
CB
203#else
204extern int __build_bug_on_failed;
ba2b6354
CB
205#define BUILD_BUG_ON(condition) \
206 do { \
207 ((void)sizeof(char[1 - 2 * !!(condition)])); \
208 if (condition) \
209 __build_bug_on_failed = 1; \
210 } while (0)
279c45ee
CB
211#endif
212
213#define lxc_iterate_parts(__iterator, __splitme, __separators) \
214 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
215 (__iterator = __it); \
216 __iterator = __it = strtok_r(NULL, __separators, &__p))
217
b81689a1
CB
218#define prctl_arg(x) ((unsigned long)x)
219
4fb34c04
CB
220/* networking */
221#ifndef IFLA_LINKMODE
222#define IFLA_LINKMODE 17
223#endif
224
225#ifndef IFLA_LINKINFO
226#define IFLA_LINKINFO 18
227#endif
228
229#ifndef IFLA_NET_NS_PID
230#define IFLA_NET_NS_PID 19
231#endif
232
233#ifndef IFLA_INFO_KIND
234#define IFLA_INFO_KIND 1
235#endif
236
237#ifndef IFLA_VLAN_ID
238#define IFLA_VLAN_ID 1
239#endif
240
241#ifndef IFLA_INFO_DATA
242#define IFLA_INFO_DATA 2
243#endif
244
245#ifndef VETH_INFO_PEER
246#define VETH_INFO_PEER 1
247#endif
248
249#ifndef IFLA_MACVLAN_MODE
250#define IFLA_MACVLAN_MODE 1
251#endif
252
253#ifndef IFLA_NEW_NETNSID
254#define IFLA_NEW_NETNSID 45
255#endif
256
257#ifndef IFLA_IF_NETNSID
258#define IFLA_IF_NETNSID 46
259#endif
260
873c6e87
CB
261#ifndef RTM_NEWNSID
262#define RTM_NEWNSID 88
263#endif
264
4e3ed0d1
CB
265#ifndef NLMSG_ERROR
266#define NLMSG_ERROR 0x2
267#endif
268
7b15813c
CB
269#ifndef MACVLAN_MODE_PRIVATE
270#define MACVLAN_MODE_PRIVATE 1
271#endif
272
273#ifndef MACVLAN_MODE_VEPA
274#define MACVLAN_MODE_VEPA 2
275#endif
276
277#ifndef MACVLAN_MODE_BRIDGE
278#define MACVLAN_MODE_BRIDGE 4
279#endif
280
281#ifndef MACVLAN_MODE_PASSTHRU
282#define MACVLAN_MODE_PASSTHRU 8
283#endif
284
b1234129
CB
285/* Length of abstract unix domain socket socket address. */
286#define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
287
c881c810
CB
288/* mount */
289#ifndef MS_REC
290#define MS_REC 16384
291#endif
292
293#ifndef MS_SLAVE
294#define MS_SLAVE (1 << 19)
295#endif
296
37ef15bb
CB
297/* open */
298#ifndef O_PATH
299#define O_PATH 010000000
300#endif
301
302#ifndef O_NOFOLLOW
303#define O_NOFOLLOW 00400000
304#endif
305
604ca1c0
CB
306/* sockets */
307#ifndef SOCK_CLOEXEC
308#define SOCK_CLOEXEC 02000000
309#endif
310
245532a2
CB
311/* pointer conversion macros */
312#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
313#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
314
279c45ee 315#endif /* __LXC_MACRO_H */