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