]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/macro.h
utils: improve lxc_switch_uid_gid()
[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 */
0c9b1f82
CB
158#define LXC_PROC_PID_FD_LEN \
159 (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
160
161/* /proc/ = 6
162 * +
163 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
164 * +
165 * /status = 7
166 * +
167 * \0 = 1
168 */
169#define LXC_PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
170
171/* /proc/ = 6
172 * +
173 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
174 * +
175 * /attr/ = 6
176 * +
177 * /current = 8
178 * +
179 * \0 = 1
180 */
181#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
0c5ea884 182
9b8d4c58 183#define LXC_CMD_DATA_MAX (MAXPATHLEN * 2)
279c45ee
CB
184
185/* loop devices */
186#ifndef LO_FLAGS_AUTOCLEAR
187#define LO_FLAGS_AUTOCLEAR 4
188#endif
189
190#ifndef LOOP_CTL_GET_FREE
191#define LOOP_CTL_GET_FREE 0x4C82
192#endif
193
194/* memfd_create() */
195#ifndef MFD_CLOEXEC
196#define MFD_CLOEXEC 0x0001U
197#endif
198
199#ifndef MFD_ALLOW_SEALING
200#define MFD_ALLOW_SEALING 0x0002U
201#endif
202
203/**
204 * BUILD_BUG_ON - break compile if a condition is true.
205 * @condition: the condition which the compiler should know is false.
206 *
207 * If you have some code which relies on certain constants being equal, or
208 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
209 * detect if someone changes it.
210 *
211 * The implementation uses gcc's reluctance to create a negative array, but
212 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
213 * to inline functions). So as a fallback we use the optimizer; if it can't
214 * prove the condition is false, it will cause a link error on the undefined
215 * "__build_bug_on_failed". This error message can be harder to track down
216 * though, hence the two different methods.
217 */
218#ifndef __OPTIMIZE__
ba2b6354 219#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
279c45ee
CB
220#else
221extern int __build_bug_on_failed;
ba2b6354
CB
222#define BUILD_BUG_ON(condition) \
223 do { \
224 ((void)sizeof(char[1 - 2 * !!(condition)])); \
225 if (condition) \
226 __build_bug_on_failed = 1; \
227 } while (0)
279c45ee
CB
228#endif
229
230#define lxc_iterate_parts(__iterator, __splitme, __separators) \
231 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
232 (__iterator = __it); \
233 __iterator = __it = strtok_r(NULL, __separators, &__p))
234
b81689a1
CB
235#define prctl_arg(x) ((unsigned long)x)
236
4fb34c04
CB
237/* networking */
238#ifndef IFLA_LINKMODE
239#define IFLA_LINKMODE 17
240#endif
241
242#ifndef IFLA_LINKINFO
243#define IFLA_LINKINFO 18
244#endif
245
246#ifndef IFLA_NET_NS_PID
247#define IFLA_NET_NS_PID 19
248#endif
249
250#ifndef IFLA_INFO_KIND
251#define IFLA_INFO_KIND 1
252#endif
253
254#ifndef IFLA_VLAN_ID
255#define IFLA_VLAN_ID 1
256#endif
257
258#ifndef IFLA_INFO_DATA
259#define IFLA_INFO_DATA 2
260#endif
261
262#ifndef VETH_INFO_PEER
263#define VETH_INFO_PEER 1
264#endif
265
266#ifndef IFLA_MACVLAN_MODE
267#define IFLA_MACVLAN_MODE 1
268#endif
269
270#ifndef IFLA_NEW_NETNSID
271#define IFLA_NEW_NETNSID 45
272#endif
273
274#ifndef IFLA_IF_NETNSID
275#define IFLA_IF_NETNSID 46
276#endif
277
873c6e87
CB
278#ifndef RTM_NEWNSID
279#define RTM_NEWNSID 88
280#endif
281
4e3ed0d1
CB
282#ifndef NLMSG_ERROR
283#define NLMSG_ERROR 0x2
284#endif
285
7b15813c
CB
286#ifndef MACVLAN_MODE_PRIVATE
287#define MACVLAN_MODE_PRIVATE 1
288#endif
289
290#ifndef MACVLAN_MODE_VEPA
291#define MACVLAN_MODE_VEPA 2
292#endif
293
294#ifndef MACVLAN_MODE_BRIDGE
295#define MACVLAN_MODE_BRIDGE 4
296#endif
297
298#ifndef MACVLAN_MODE_PASSTHRU
299#define MACVLAN_MODE_PASSTHRU 8
300#endif
301
b1234129
CB
302/* Length of abstract unix domain socket socket address. */
303#define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
304
c881c810 305/* mount */
6e5655e0
CB
306#ifndef MS_PRIVATE
307#define MS_PRIVATE (1<<18)
c881c810
CB
308#endif
309
310#ifndef MS_SLAVE
311#define MS_SLAVE (1 << 19)
312#endif
313
6e5655e0
CB
314#ifndef MS_LAZYTIME
315#define MS_LAZYTIME (1<<25)
316#endif
317
318#ifndef MS_REC
319#define MS_REC 16384
320#endif
321
37ef15bb
CB
322/* open */
323#ifndef O_PATH
324#define O_PATH 010000000
325#endif
326
327#ifndef O_NOFOLLOW
328#define O_NOFOLLOW 00400000
329#endif
330
604ca1c0
CB
331/* sockets */
332#ifndef SOCK_CLOEXEC
333#define SOCK_CLOEXEC 02000000
334#endif
335
245532a2
CB
336/* pointer conversion macros */
337#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
338#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
339
9234406b
CB
340#define PTR_TO_INTMAX(p) ((intmax_t)((intptr_t)(p)))
341#define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u)))
342
b962868f
CB
343#define LXC_INVALID_UID ((uid_t)-1)
344#define LXC_INVALID_GID ((gid_t)-1)
345
279c45ee 346#endif /* __LXC_MACRO_H */