]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/macro.h
Revert "Revert "cgfsng: avoid tiny race window""
[mirror_lxc.git] / src / lxc / macro.h
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
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>
28 #include <linux/types.h>
29 #include <stdint.h>
30 #include <string.h>
31 #include <sys/mount.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
34 #include <unistd.h>
35
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
41 /* capabilities */
42 #ifndef CAP_SYS_ADMIN
43 #define CAP_SYS_ADMIN 21
44 #endif
45
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
57
58 #ifndef CAP_SETUID
59 #define CAP_SETUID 7
60 #endif
61
62 #ifndef CAP_SETGID
63 #define CAP_SETGID 6
64 #endif
65
66 /* prctl */
67 #ifndef PR_CAPBSET_READ
68 #define PR_CAPBSET_READ 23
69 #endif
70
71 #ifndef PR_CAPBSET_DROP
72 #define PR_CAPBSET_DROP 24
73 #endif
74
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
96 #ifndef PR_SET_NO_NEW_PRIVS
97 #define PR_SET_NO_NEW_PRIVS 38
98 #endif
99
100 #ifndef PR_GET_NO_NEW_PRIVS
101 #define PR_GET_NO_NEW_PRIVS 39
102 #endif
103
104 /* filesystem magic values */
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
113 #ifndef NSFS_MAGIC
114 #define NSFS_MAGIC 0x6e736673
115 #endif
116
117 /* current overlayfs */
118 #ifndef OVERLAY_SUPER_MAGIC
119 #define OVERLAY_SUPER_MAGIC 0x794c7630
120 #endif
121
122 /* legacy overlayfs */
123 #ifndef OVERLAYFS_SUPER_MAGIC
124 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
125 #endif
126
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
142 /* Useful macros */
143 #define LXC_LINELEN 4096
144 #define LXC_IDMAPLEN 4096
145 #define LXC_MAX_BUFFER 4096
146
147 /* /proc/ = 6
148 * +
149 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
150 * +
151 * /fd/ = 4
152 * +
153 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
154 * +
155 * \0 = 1
156 */
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)
181
182 #define LXC_CMD_DATA_MAX (MAXPATHLEN * 2)
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__
218 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
219 #else
220 extern int __build_bug_on_failed;
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)
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
234 #define prctl_arg(x) ((unsigned long)x)
235
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
273 #ifdef IFLA_IF_NETNSID
274 #ifndef IFLA_TARGET_NETNSID
275 #define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
276 #endif
277 #else
278 #define IFLA_IF_NETNSID 46
279 #define IFLA_TARGET_NETNSID 46
280 #endif
281
282 #ifndef IFA_TARGET_NETNSID
283 #define IFA_TARGET_NETNSID 10
284 #endif
285
286 #ifndef IFLA_STATS
287 #define IFLA_STATS 7
288 #endif
289
290 #ifndef IFLA_STATS64
291 #define IFLA_STATS64 23
292 #endif
293
294 #ifndef RTM_NEWNSID
295 #define RTM_NEWNSID 88
296 #endif
297
298 #ifndef RTM_GETNSID
299 #define RTM_GETNSID 90
300 #endif
301
302 #ifndef NLMSG_ERROR
303 #define NLMSG_ERROR 0x2
304 #endif
305
306 #ifndef MACVLAN_MODE_PRIVATE
307 #define MACVLAN_MODE_PRIVATE 1
308 #endif
309
310 #ifndef MACVLAN_MODE_VEPA
311 #define MACVLAN_MODE_VEPA 2
312 #endif
313
314 #ifndef MACVLAN_MODE_BRIDGE
315 #define MACVLAN_MODE_BRIDGE 4
316 #endif
317
318 #ifndef MACVLAN_MODE_PASSTHRU
319 #define MACVLAN_MODE_PASSTHRU 8
320 #endif
321
322 /* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
323 enum {
324 __LXC_NETNSA_NONE,
325 #define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
326 __LXC_NETNSA_NSID,
327 __LXC_NETNSA_PID,
328 __LXC_NETNSA_FD,
329 __LXC_NETNSA_MAX,
330 };
331
332 /* Length of abstract unix domain socket socket address. */
333 #define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
334
335 /* mount */
336 #ifndef MS_PRIVATE
337 #define MS_PRIVATE (1<<18)
338 #endif
339
340 #ifndef MS_SLAVE
341 #define MS_SLAVE (1 << 19)
342 #endif
343
344 #ifndef MS_LAZYTIME
345 #define MS_LAZYTIME (1<<25)
346 #endif
347
348 #ifndef MS_REC
349 #define MS_REC 16384
350 #endif
351
352 /* open */
353 #ifndef O_PATH
354 #define O_PATH 010000000
355 #endif
356
357 #ifndef O_NOFOLLOW
358 #define O_NOFOLLOW 00400000
359 #endif
360
361 /* sockets */
362 #ifndef SOCK_CLOEXEC
363 #define SOCK_CLOEXEC 02000000
364 #endif
365
366 /* pointer conversion macros */
367 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
368 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
369
370 #define PTR_TO_INTMAX(p) ((intmax_t)((intptr_t)(p)))
371 #define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u)))
372
373 #define LXC_INVALID_UID ((uid_t)-1)
374 #define LXC_INVALID_GID ((gid_t)-1)
375
376 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
377 #define STRARRAYLEN(x) (sizeof(x) - 1)
378
379 #endif /* __LXC_MACRO_H */