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