]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/macro.h
macro: use ascending order for capabilities
[mirror_lxc.git] / src / lxc / macro.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef __LXC_MACRO_H
4 #define __LXC_MACRO_H
5
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE 1
8 #endif
9 #define __STDC_FORMAT_MACROS
10 #include <asm/types.h>
11 #include <limits.h>
12 #include <linux/if_link.h>
13 #include <linux/loop.h>
14 #include <linux/netlink.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/types.h>
17 #include <stdint.h>
18 #include <string.h>
19 #include <sys/mount.h>
20 #include <sys/socket.h>
21 #include <sys/un.h>
22 #include <unistd.h>
23
24 #ifndef PATH_MAX
25 #define PATH_MAX 4096
26 #endif
27
28 #ifndef MAX_GRBUF_SIZE
29 #define MAX_GRBUF_SIZE 2097152
30 #endif
31
32 #define INT64_FMT "%" PRId64
33
34 /* Define __S_ISTYPE if missing from the C library. */
35 #ifndef __S_ISTYPE
36 #define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask))
37 #endif
38
39 /* capabilities */
40 #ifndef CAP_SETGID
41 #define CAP_SETGID 6
42 #endif
43
44 #ifndef CAP_SETUID
45 #define CAP_SETUID 7
46 #endif
47
48 #ifndef CAP_SYS_ADMIN
49 #define CAP_SYS_ADMIN 21
50 #endif
51
52 #ifndef CAP_SETFCAP
53 #define CAP_SETFCAP 31
54 #endif
55
56 #ifndef CAP_MAC_OVERRIDE
57 #define CAP_MAC_OVERRIDE 32
58 #endif
59
60 #ifndef CAP_MAC_ADMIN
61 #define CAP_MAC_ADMIN 33
62 #endif
63
64 /* prctl */
65 #ifndef PR_CAPBSET_READ
66 #define PR_CAPBSET_READ 23
67 #endif
68
69 #ifndef PR_CAPBSET_DROP
70 #define PR_CAPBSET_DROP 24
71 #endif
72
73 /* Control the ambient capability set */
74 #ifndef PR_CAP_AMBIENT
75 #define PR_CAP_AMBIENT 47
76 #endif
77
78 #ifndef PR_CAP_AMBIENT_IS_SET
79 #define PR_CAP_AMBIENT_IS_SET 1
80 #endif
81
82 #ifndef PR_CAP_AMBIENT_RAISE
83 #define PR_CAP_AMBIENT_RAISE 2
84 #endif
85
86 #ifndef PR_CAP_AMBIENT_LOWER
87 #define PR_CAP_AMBIENT_LOWER 3
88 #endif
89
90 #ifndef PR_CAP_AMBIENT_CLEAR_ALL
91 #define PR_CAP_AMBIENT_CLEAR_ALL 4
92 #endif
93
94 #ifndef PR_SET_NO_NEW_PRIVS
95 #define PR_SET_NO_NEW_PRIVS 38
96 #endif
97
98 #ifndef PR_GET_NO_NEW_PRIVS
99 #define PR_GET_NO_NEW_PRIVS 39
100 #endif
101
102 /* filesystem magic values */
103 #ifndef CGROUP_SUPER_MAGIC
104 #define CGROUP_SUPER_MAGIC 0x27e0eb
105 #endif
106
107 #ifndef CGROUP2_SUPER_MAGIC
108 #define CGROUP2_SUPER_MAGIC 0x63677270
109 #endif
110
111 #ifndef NSFS_MAGIC
112 #define NSFS_MAGIC 0x6e736673
113 #endif
114
115 /* current overlayfs */
116 #ifndef OVERLAY_SUPER_MAGIC
117 #define OVERLAY_SUPER_MAGIC 0x794c7630
118 #endif
119
120 /* legacy overlayfs */
121 #ifndef OVERLAYFS_SUPER_MAGIC
122 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
123 #endif
124
125 /* Calculate the number of chars needed to represent a given integer as a C
126 * string. Include room for '-' to indicate negative numbers and the \0 byte.
127 * This is based on systemd.
128 */
129 #define INTTYPE_TO_STRLEN(type) \
130 (2 + (sizeof(type) <= 1 \
131 ? 3 \
132 : sizeof(type) <= 2 \
133 ? 5 \
134 : sizeof(type) <= 4 \
135 ? 10 \
136 : sizeof(type) <= 8 \
137 ? 20 \
138 : sizeof(int[-2 * (sizeof(type) > 8)])))
139
140 /* Useful macros */
141 #define LXC_LINELEN 4096
142 #define LXC_IDMAPLEN 4096
143 #define LXC_MAX_BUFFER 4096
144 #define LXC_NAMESPACE_NAME_MAX 256
145
146 /* /proc/ = 6
147 * +
148 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
149 * +
150 * /fd/ = 4
151 * +
152 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
153 * +
154 * \0 = 1
155 */
156 #define LXC_PROC_PID_FD_LEN \
157 (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
158
159 /* /proc/ = 6
160 * +
161 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
162 * +
163 * /status = 7
164 * +
165 * \0 = 1
166 */
167 #define LXC_PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
168
169 /* /proc/ = 6
170 * +
171 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
172 * +
173 * /attr/ = 6
174 * +
175 * /current = 8
176 * +
177 * \0 = 1
178 */
179 #define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
180
181 #define LXC_CMD_DATA_MAX (PATH_MAX * 2)
182
183 /* loop devices */
184 #ifndef LO_FLAGS_AUTOCLEAR
185 #define LO_FLAGS_AUTOCLEAR 4
186 #endif
187
188 #ifndef LOOP_CTL_GET_FREE
189 #define LOOP_CTL_GET_FREE 0x4C82
190 #endif
191
192 /* memfd_create() */
193 #ifndef MFD_CLOEXEC
194 #define MFD_CLOEXEC 0x0001U
195 #endif
196
197 #ifndef MFD_ALLOW_SEALING
198 #define MFD_ALLOW_SEALING 0x0002U
199 #endif
200
201 /**
202 * BUILD_BUG_ON - break compile if a condition is true.
203 * @condition: the condition which the compiler should know is false.
204 *
205 * If you have some code which relies on certain constants being equal, or
206 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
207 * detect if someone changes it.
208 *
209 * The implementation uses gcc's reluctance to create a negative array, but
210 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
211 * to inline functions). So as a fallback we use the optimizer; if it can't
212 * prove the condition is false, it will cause a link error on the undefined
213 * "__build_bug_on_failed". This error message can be harder to track down
214 * though, hence the two different methods.
215 */
216 #ifndef __OPTIMIZE__
217 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
218 #else
219 extern int __build_bug_on_failed;
220 #define BUILD_BUG_ON(condition) \
221 do { \
222 ((void)sizeof(char[1 - 2 * !!(condition)])); \
223 if (condition) \
224 __build_bug_on_failed = 1; \
225 } while (0)
226 #endif
227
228 #define lxc_iterate_parts(__iterator, __splitme, __separators) \
229 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
230 (__iterator = __it); \
231 __iterator = __it = strtok_r(NULL, __separators, &__p))
232
233 #define prctl_arg(x) ((unsigned long)x)
234
235 /* networking */
236 #ifndef NETLINK_GET_STRICT_CHK
237 #define NETLINK_GET_STRICT_CHK 12
238 #endif
239
240 #ifndef SOL_NETLINK
241 #define SOL_NETLINK 270
242 #endif
243
244 #ifndef IFLA_LINKMODE
245 #define IFLA_LINKMODE 17
246 #endif
247
248 #ifndef IFLA_LINKINFO
249 #define IFLA_LINKINFO 18
250 #endif
251
252 #ifndef IFLA_NET_NS_PID
253 #define IFLA_NET_NS_PID 19
254 #endif
255
256 #ifndef IFLA_NET_NS_FD
257 #define IFLA_NET_NS_FD 28
258 #endif
259
260 #ifndef IFLA_INFO_KIND
261 #define IFLA_INFO_KIND 1
262 #endif
263
264 #ifndef IFLA_VLAN_ID
265 #define IFLA_VLAN_ID 1
266 #endif
267
268 #ifndef IFLA_INFO_DATA
269 #define IFLA_INFO_DATA 2
270 #endif
271
272 #ifndef VETH_INFO_PEER
273 #define VETH_INFO_PEER 1
274 #endif
275
276 #ifndef VETH_MODE_BRIDGE
277 #define VETH_MODE_BRIDGE 1
278 #endif
279
280 #ifndef VETH_MODE_ROUTER
281 #define VETH_MODE_ROUTER 2
282 #endif
283
284 #ifndef IFLA_MACVLAN_MODE
285 #define IFLA_MACVLAN_MODE 1
286 #endif
287
288 #ifndef IFLA_IPVLAN_MODE
289 #define IFLA_IPVLAN_MODE 1
290 #endif
291
292 #ifndef IFLA_IPVLAN_ISOLATION
293 #define IFLA_IPVLAN_ISOLATION 2
294 #endif
295
296 #ifndef IFLA_NEW_NETNSID
297 #define IFLA_NEW_NETNSID 45
298 #endif
299
300 #ifdef IFLA_IF_NETNSID
301 #ifndef IFLA_TARGET_NETNSID
302 #define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
303 #endif
304 #else
305 #define IFLA_IF_NETNSID 46
306 #define IFLA_TARGET_NETNSID 46
307 #endif
308
309 #ifndef IFA_TARGET_NETNSID
310 #define IFA_TARGET_NETNSID 10
311 #endif
312
313 #ifndef IFLA_STATS
314 #define IFLA_STATS 7
315 #endif
316
317 #ifndef IFLA_STATS64
318 #define IFLA_STATS64 23
319 #endif
320
321 #ifndef RTM_NEWNSID
322 #define RTM_NEWNSID 88
323 #endif
324
325 #ifndef RTM_GETNSID
326 #define RTM_GETNSID 90
327 #endif
328
329 #ifndef NLMSG_ERROR
330 #define NLMSG_ERROR 0x2
331 #endif
332
333 #ifndef MACVLAN_MODE_PRIVATE
334 #define MACVLAN_MODE_PRIVATE 1
335 #endif
336
337 #ifndef MACVLAN_MODE_VEPA
338 #define MACVLAN_MODE_VEPA 2
339 #endif
340
341 #ifndef MACVLAN_MODE_BRIDGE
342 #define MACVLAN_MODE_BRIDGE 4
343 #endif
344
345 #ifndef MACVLAN_MODE_PASSTHRU
346 #define MACVLAN_MODE_PASSTHRU 8
347 #endif
348
349 #ifndef IPVLAN_MODE_L2
350 #define IPVLAN_MODE_L2 0
351 #endif
352
353 #ifndef IPVLAN_MODE_L3
354 #define IPVLAN_MODE_L3 1
355 #endif
356
357 #ifndef IPVLAN_MODE_L3S
358 #define IPVLAN_MODE_L3S 2
359 #endif
360
361 #ifndef IPVLAN_ISOLATION_BRIDGE
362 #define IPVLAN_ISOLATION_BRIDGE 0
363 #endif
364
365 #ifndef IPVLAN_ISOLATION_PRIVATE
366 #define IPVLAN_ISOLATION_PRIVATE 1
367 #endif
368
369 #ifndef IPVLAN_ISOLATION_VEPA
370 #define IPVLAN_ISOLATION_VEPA 2
371 #endif
372
373 /* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
374 enum {
375 __LXC_NETNSA_NONE,
376 #define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
377 __LXC_NETNSA_NSID,
378 __LXC_NETNSA_PID,
379 __LXC_NETNSA_FD,
380 __LXC_NETNSA_MAX,
381 };
382
383 /* Length of abstract unix domain socket socket address. */
384 #define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
385
386 /* mount */
387 #ifndef MS_PRIVATE
388 #define MS_PRIVATE (1<<18)
389 #endif
390
391 #ifndef MS_SLAVE
392 #define MS_SLAVE (1 << 19)
393 #endif
394
395 #ifndef MS_LAZYTIME
396 #define MS_LAZYTIME (1<<25)
397 #endif
398
399 #ifndef MS_REC
400 #define MS_REC 16384
401 #endif
402
403 /* open */
404 #ifndef O_PATH
405 #define O_PATH 010000000
406 #endif
407
408 #ifndef O_NOFOLLOW
409 #define O_NOFOLLOW 00400000
410 #endif
411
412 /* sockets */
413 #ifndef SOCK_CLOEXEC
414 #define SOCK_CLOEXEC 02000000
415 #endif
416
417 /* pointer conversion macros */
418 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
419 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
420
421 #define PTR_TO_PID(p) ((pid_t)((intptr_t)(p)))
422 #define PID_TO_PTR(u) ((void *)((intptr_t)(u)))
423
424 #define PTR_TO_UINT64(p) ((uint64_t)((intptr_t)(p)))
425
426 #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
427 #define PTR_TO_USHORT(p) ((unsigned short)((uintptr_t)(p)))
428
429 #define LXC_INVALID_UID ((uid_t)-1)
430 #define LXC_INVALID_GID ((gid_t)-1)
431
432 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
433 #define STRARRAYLEN(x) (sizeof(x) - 1)
434
435 /* Maximum number of bytes sendfile() is able to send in one go. */
436 #define LXC_SENDFILE_MAX 0x7ffff000
437
438 #define move_ptr(ptr) \
439 ({ \
440 typeof(ptr) __internal_ptr__ = (ptr); \
441 (ptr) = NULL; \
442 __internal_ptr__; \
443 })
444
445 #define move_fd(fd) \
446 ({ \
447 int __internal_fd__ = (fd); \
448 (fd) = -EBADF; \
449 __internal_fd__; \
450 })
451
452 #define ret_set_errno(__ret__, __errno__) \
453 ({ \
454 typeof(__ret__) __internal_ret__ = (__ret__); \
455 errno = (__errno__); \
456 __internal_ret__; \
457 })
458
459 #define ret_errno(__errno__) \
460 ({ \
461 errno = (__errno__); \
462 -(__errno__); \
463 })
464
465 #define free_move_ptr(a, b) \
466 ({ \
467 free(a); \
468 (a) = move_ptr((b)); \
469 })
470
471 /* Container's specific file/directory names */
472 #define LXC_CONFIG_FNAME "config"
473 #define LXC_PARTIAL_FNAME "partial"
474 #define LXC_ROOTFS_DNAME "rootfs"
475 #define LXC_TIMESTAMP_FNAME "ts"
476 #define LXC_COMMENT_FNAME "comment"
477
478 #define ARRAY_SIZE(x) \
479 (__builtin_choose_expr(!__builtin_types_compatible_p(typeof(x), \
480 typeof(&*(x))), \
481 sizeof(x) / sizeof((x)[0]), ((void)0)))
482
483 #ifndef TIOCGPTPEER
484 #if defined __sparc__
485 #define TIOCGPTPEER _IO('t', 137)
486 #else
487 #define TIOCGPTPEER _IO('T', 0x41)
488 #endif
489 #endif
490
491 #endif /* __LXC_MACRO_H */