]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/macro.h
utils: use SYSTRACE() when logging stdio permission fixup failures
[mirror_lxc.git] / src / lxc / macro.h
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
279c45ee
CB
2
3#ifndef __LXC_MACRO_H
4#define __LXC_MACRO_H
5
900b6606
CB
6#ifndef _GNU_SOURCE
7#define _GNU_SOURCE 1
8#endif
9#define __STDC_FORMAT_MACROS
85de58d6 10#include <asm/types.h>
b3509169 11#include <limits.h>
85de58d6
CB
12#include <linux/if_link.h>
13#include <linux/loop.h>
14#include <linux/netlink.h>
15#include <linux/rtnetlink.h>
938980ba 16#include <linux/types.h>
245532a2 17#include <stdint.h>
2259663c 18#include <string.h>
85de58d6
CB
19#include <sys/mount.h>
20#include <sys/socket.h>
b1234129 21#include <sys/un.h>
2955a58a 22#include <unistd.h>
85de58d6 23
b3509169
CB
24#ifndef PATH_MAX
25#define PATH_MAX 4096
26#endif
27
052535c8 28#ifndef MAX_GRBUF_SIZE
abd833eb 29#define MAX_GRBUF_SIZE 2097152
052535c8
CB
30#endif
31
900b6606
CB
32#define INT64_FMT "%" PRId64
33
279c45ee
CB
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
ba2b6354 39/* capabilities */
fa934e3e
CB
40#ifndef CAP_CHOWN
41#define CAP_CHOWN 0
42#endif
43
44#ifndef CAP_DAC_OVERRIDE
45#define CAP_DAC_OVERRIDE 1
46#endif
47
48#ifndef CAP_DAC_READ_SEARCH
49#define CAP_DAC_READ_SEARCH 2
50#endif
51
52#ifndef CAP_FOWNER
53#define CAP_FOWNER 3
54#endif
55
56#ifndef CAP_FSETID
57#define CAP_FSETID 4
58#endif
59
60#ifndef CAP_KILL
61#define CAP_KILL 5
62#endif
63
24b77f47 64#ifndef CAP_SETGID
fa934e3e 65#define CAP_SETGID 6
24b77f47
CB
66#endif
67
68#ifndef CAP_SETUID
fa934e3e
CB
69#define CAP_SETUID 7
70#endif
71
72#ifndef CAP_SETPCAP
73#define CAP_SETPCAP 8
74#endif
75
76#ifndef CAP_LINUX_IMMUTABLE
77#define CAP_LINUX_IMMUTABLE 9
78#endif
79
80#ifndef CAP_NET_BIND_SERVICE
81#define CAP_NET_BIND_SERVICE 10
82#endif
83
84#ifndef CAP_NET_BROADCAST
85#define CAP_NET_BROADCAST 11
86#endif
87
88#ifndef CAP_NET_ADMIN
89#define CAP_NET_ADMIN 12
90#endif
91
92#ifndef CAP_NET_RAW
93#define CAP_NET_RAW 13
94#endif
95
96#ifndef CAP_IPC_LOCK
97#define CAP_IPC_LOCK 14
98#endif
99
100#ifndef CAP_IPC_OWNER
101#define CAP_IPC_OWNER 15
102#endif
103
104#ifndef CAP_SYS_MODULE
105#define CAP_SYS_MODULE 16
106#endif
107
108#ifndef CAP_SYS_RAWIO
109#define CAP_SYS_RAWIO 17
110#endif
111
112#ifndef CAP_SYS_CHROOT
113#define CAP_SYS_CHROOT 18
114#endif
115
116#ifndef CAP_SYS_PTRACE
117#define CAP_SYS_PTRACE 19
118#endif
119
120#ifndef CAP_SYS_PACCT
121#define CAP_SYS_PACCT 20
24b77f47
CB
122#endif
123
ba2b6354 124#ifndef CAP_SYS_ADMIN
fa934e3e
CB
125#define CAP_SYS_ADMIN 21
126#endif
127
128#ifndef CAP_SYS_BOOT
129#define CAP_SYS_BOOT 22
130#endif
131
132#ifndef CAP_SYS_NICE
133#define CAP_SYS_NICE 23
134#endif
135
136#ifndef CAP_SYS_RESOURCE
137#define CAP_SYS_RESOURCE 24
138#endif
139
140#ifndef CAP_SYS_TIME
141#define CAP_SYS_TIME 25
142#endif
143
144#ifndef CAP_SYS_TTY_CONFIG
145#define CAP_SYS_TTY_CONFIG 26
146#endif
147
148#ifndef CAP_MKNOD
149#define CAP_MKNOD 27
150#endif
151
152#ifndef CAP_LEASE
153#define CAP_LEASE 28
ba2b6354
CB
154#endif
155
f2da98c0 156#ifndef CAP_AUDIT_WRITE
fa934e3e 157#define CAP_AUDIT_WRITE 29
f2da98c0
CB
158#endif
159
160#ifndef CAP_AUDIT_CONTROL
fa934e3e 161#define CAP_AUDIT_CONTROL 30
f2da98c0
CB
162#endif
163
279c45ee 164#ifndef CAP_SETFCAP
fa934e3e 165#define CAP_SETFCAP 31
279c45ee
CB
166#endif
167
168#ifndef CAP_MAC_OVERRIDE
fa934e3e 169#define CAP_MAC_OVERRIDE 32
279c45ee
CB
170#endif
171
172#ifndef CAP_MAC_ADMIN
fa934e3e 173#define CAP_MAC_ADMIN 33
279c45ee 174#endif
279c45ee 175
f2da98c0 176#ifndef CAP_SYSLOG
fa934e3e 177#define CAP_SYSLOG 34
f2da98c0
CB
178#endif
179
180#ifndef CAP_WAKE_ALARM
fa934e3e 181#define CAP_WAKE_ALARM 35
f2da98c0
CB
182#endif
183
184#ifndef CAP_BLOCK_SUSPEND
fa934e3e 185#define CAP_BLOCK_SUSPEND 36
f2da98c0
CB
186#endif
187
188#ifndef CAP_AUDIT_READ
fa934e3e 189#define CAP_AUDIT_READ 37
f2da98c0
CB
190#endif
191
7b4cd468 192#ifndef CAP_PERFMON
fa934e3e 193#define CAP_PERFMON 38
7b4cd468
CB
194#endif
195
196#ifndef CAP_BPF
fa934e3e 197#define CAP_BPF 39
7b4cd468
CB
198#endif
199
200#ifndef CAP_CHECKPOINT_RESTORE
fa934e3e 201#define CAP_CHECKPOINT_RESTORE 40
7b4cd468
CB
202#endif
203
ba2b6354 204/* prctl */
1f207a5c
CB
205#ifndef PR_CAPBSET_READ
206#define PR_CAPBSET_READ 23
207#endif
208
ba2b6354 209#ifndef PR_CAPBSET_DROP
604ca1c0
CB
210#define PR_CAPBSET_DROP 24
211#endif
212
1f207a5c
CB
213/* Control the ambient capability set */
214#ifndef PR_CAP_AMBIENT
215#define PR_CAP_AMBIENT 47
216#endif
217
218#ifndef PR_CAP_AMBIENT_IS_SET
219#define PR_CAP_AMBIENT_IS_SET 1
220#endif
221
222#ifndef PR_CAP_AMBIENT_RAISE
223#define PR_CAP_AMBIENT_RAISE 2
224#endif
225
226#ifndef PR_CAP_AMBIENT_LOWER
227#define PR_CAP_AMBIENT_LOWER 3
228#endif
229
230#ifndef PR_CAP_AMBIENT_CLEAR_ALL
231#define PR_CAP_AMBIENT_CLEAR_ALL 4
232#endif
233
ba2b6354 234#ifndef PR_SET_NO_NEW_PRIVS
604ca1c0
CB
235#define PR_SET_NO_NEW_PRIVS 38
236#endif
237
ba2b6354 238#ifndef PR_GET_NO_NEW_PRIVS
604ca1c0
CB
239#define PR_GET_NO_NEW_PRIVS 39
240#endif
241
ba2b6354 242/* filesystem magic values */
279c45ee
CB
243#ifndef CGROUP_SUPER_MAGIC
244#define CGROUP_SUPER_MAGIC 0x27e0eb
245#endif
246
247#ifndef CGROUP2_SUPER_MAGIC
248#define CGROUP2_SUPER_MAGIC 0x63677270
249#endif
250
f26dc127
CB
251#ifndef NSFS_MAGIC
252#define NSFS_MAGIC 0x6e736673
253#endif
254
ba2b6354 255/* current overlayfs */
37ef15bb
CB
256#ifndef OVERLAY_SUPER_MAGIC
257#define OVERLAY_SUPER_MAGIC 0x794c7630
258#endif
259
ba2b6354
CB
260/* legacy overlayfs */
261#ifndef OVERLAYFS_SUPER_MAGIC
262#define OVERLAYFS_SUPER_MAGIC 0x794c764f
263#endif
264
f246d9b8
CB
265/* Calculate the number of chars needed to represent a given integer as a C
266 * string. Include room for '-' to indicate negative numbers and the \0 byte.
267 * This is based on systemd.
268 */
269#define INTTYPE_TO_STRLEN(type) \
270 (2 + (sizeof(type) <= 1 \
271 ? 3 \
272 : sizeof(type) <= 2 \
273 ? 5 \
274 : sizeof(type) <= 4 \
275 ? 10 \
276 : sizeof(type) <= 8 \
277 ? 20 \
278 : sizeof(int[-2 * (sizeof(type) > 8)])))
279
279c45ee 280/* Useful macros */
279c45ee
CB
281#define LXC_LINELEN 4096
282#define LXC_IDMAPLEN 4096
283#define LXC_MAX_BUFFER 4096
b280bc38 284#define LXC_NAMESPACE_NAME_MAX 256
0c5ea884 285
c538837d
CB
286/* /proc/ = 6
287 * +
288 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
289 * +
290 * /fd/ = 4
291 * +
292 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
293 * +
294 * \0 = 1
295 */
296#define LXC_PROC_PID_LEN \
297 (6 + INTTYPE_TO_STRLEN(pid_t) + 1)
298
279c45ee
CB
299/* /proc/ = 6
300 * +
0c5ea884 301 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
279c45ee
CB
302 * +
303 * /fd/ = 4
304 * +
0c5ea884 305 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
279c45ee
CB
306 * +
307 * \0 = 1
308 */
0c9b1f82
CB
309#define LXC_PROC_PID_FD_LEN \
310 (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
311
312/* /proc/ = 6
313 * +
314 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
315 * +
316 * /status = 7
317 * +
318 * \0 = 1
319 */
320#define LXC_PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
321
322/* /proc/ = 6
323 * +
324 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
325 * +
326 * /attr/ = 6
327 * +
328 * /current = 8
329 * +
330 * \0 = 1
331 */
332#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
0c5ea884 333
3c736187 334#define LXC_CMD_DATA_MAX (PATH_MAX * 2)
279c45ee
CB
335
336/* loop devices */
337#ifndef LO_FLAGS_AUTOCLEAR
338#define LO_FLAGS_AUTOCLEAR 4
339#endif
340
341#ifndef LOOP_CTL_GET_FREE
342#define LOOP_CTL_GET_FREE 0x4C82
343#endif
344
345/* memfd_create() */
346#ifndef MFD_CLOEXEC
347#define MFD_CLOEXEC 0x0001U
348#endif
349
350#ifndef MFD_ALLOW_SEALING
351#define MFD_ALLOW_SEALING 0x0002U
352#endif
353
354/**
355 * BUILD_BUG_ON - break compile if a condition is true.
356 * @condition: the condition which the compiler should know is false.
357 *
358 * If you have some code which relies on certain constants being equal, or
359 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
360 * detect if someone changes it.
361 *
362 * The implementation uses gcc's reluctance to create a negative array, but
363 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
364 * to inline functions). So as a fallback we use the optimizer; if it can't
365 * prove the condition is false, it will cause a link error on the undefined
366 * "__build_bug_on_failed". This error message can be harder to track down
367 * though, hence the two different methods.
368 */
369#ifndef __OPTIMIZE__
ba2b6354 370#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
279c45ee
CB
371#else
372extern int __build_bug_on_failed;
ba2b6354
CB
373#define BUILD_BUG_ON(condition) \
374 do { \
375 ((void)sizeof(char[1 - 2 * !!(condition)])); \
376 if (condition) \
377 __build_bug_on_failed = 1; \
378 } while (0)
279c45ee
CB
379#endif
380
381#define lxc_iterate_parts(__iterator, __splitme, __separators) \
382 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
383 (__iterator = __it); \
384 __iterator = __it = strtok_r(NULL, __separators, &__p))
385
b81689a1
CB
386#define prctl_arg(x) ((unsigned long)x)
387
4fb34c04 388/* networking */
c8ca5a43
CB
389#ifndef NETLINK_GET_STRICT_CHK
390#define NETLINK_GET_STRICT_CHK 12
8df6fa99
CB
391#endif
392
d38f5b17
CB
393#ifndef SOL_NETLINK
394#define SOL_NETLINK 270
395#endif
396
4fb34c04
CB
397#ifndef IFLA_LINKMODE
398#define IFLA_LINKMODE 17
399#endif
400
401#ifndef IFLA_LINKINFO
402#define IFLA_LINKINFO 18
403#endif
404
405#ifndef IFLA_NET_NS_PID
406#define IFLA_NET_NS_PID 19
407#endif
408
0037ab49
TP
409#ifndef IFLA_NET_NS_FD
410#define IFLA_NET_NS_FD 28
411#endif
412
4fb34c04
CB
413#ifndef IFLA_INFO_KIND
414#define IFLA_INFO_KIND 1
415#endif
416
417#ifndef IFLA_VLAN_ID
418#define IFLA_VLAN_ID 1
419#endif
420
421#ifndef IFLA_INFO_DATA
422#define IFLA_INFO_DATA 2
423#endif
424
425#ifndef VETH_INFO_PEER
426#define VETH_INFO_PEER 1
427#endif
428
3f0ed090
TP
429#ifndef VETH_MODE_BRIDGE
430#define VETH_MODE_BRIDGE 1
431#endif
432
433#ifndef VETH_MODE_ROUTER
434#define VETH_MODE_ROUTER 2
435#endif
436
4fb34c04
CB
437#ifndef IFLA_MACVLAN_MODE
438#define IFLA_MACVLAN_MODE 1
439#endif
440
c9f52382 441#ifndef IFLA_IPVLAN_MODE
442#define IFLA_IPVLAN_MODE 1
443#endif
444
445#ifndef IFLA_IPVLAN_ISOLATION
446#define IFLA_IPVLAN_ISOLATION 2
447#endif
448
4fb34c04
CB
449#ifndef IFLA_NEW_NETNSID
450#define IFLA_NEW_NETNSID 45
451#endif
452
cc6119a0
CB
453#ifdef IFLA_IF_NETNSID
454#ifndef IFLA_TARGET_NETNSID
455#define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
456#endif
457#else
4fb34c04 458#define IFLA_IF_NETNSID 46
cc6119a0
CB
459#define IFLA_TARGET_NETNSID 46
460#endif
461
462#ifndef IFA_TARGET_NETNSID
463#define IFA_TARGET_NETNSID 10
4fb34c04
CB
464#endif
465
da5efb6f
CB
466#ifndef IFLA_STATS
467#define IFLA_STATS 7
468#endif
469
470#ifndef IFLA_STATS64
471#define IFLA_STATS64 23
472#endif
cc6119a0 473
873c6e87
CB
474#ifndef RTM_NEWNSID
475#define RTM_NEWNSID 88
476#endif
477
938980ba
CB
478#ifndef RTM_GETNSID
479#define RTM_GETNSID 90
480#endif
481
4e3ed0d1
CB
482#ifndef NLMSG_ERROR
483#define NLMSG_ERROR 0x2
484#endif
485
7b15813c
CB
486#ifndef MACVLAN_MODE_PRIVATE
487#define MACVLAN_MODE_PRIVATE 1
488#endif
489
490#ifndef MACVLAN_MODE_VEPA
491#define MACVLAN_MODE_VEPA 2
492#endif
493
494#ifndef MACVLAN_MODE_BRIDGE
495#define MACVLAN_MODE_BRIDGE 4
496#endif
497
498#ifndef MACVLAN_MODE_PASSTHRU
499#define MACVLAN_MODE_PASSTHRU 8
500#endif
501
c9f52382 502#ifndef IPVLAN_MODE_L2
503#define IPVLAN_MODE_L2 0
504#endif
505
506#ifndef IPVLAN_MODE_L3
507#define IPVLAN_MODE_L3 1
508#endif
509
510#ifndef IPVLAN_MODE_L3S
511#define IPVLAN_MODE_L3S 2
512#endif
513
514#ifndef IPVLAN_ISOLATION_BRIDGE
515#define IPVLAN_ISOLATION_BRIDGE 0
516#endif
517
518#ifndef IPVLAN_ISOLATION_PRIVATE
519#define IPVLAN_ISOLATION_PRIVATE 1
520#endif
521
522#ifndef IPVLAN_ISOLATION_VEPA
523#define IPVLAN_ISOLATION_VEPA 2
524#endif
525
53a9ba7f
TP
526#ifndef BRIDGE_VLAN_NONE
527#define BRIDGE_VLAN_NONE -1 /* Bridge VLAN option set to "none". */
528#endif
529
2abd5206
TP
530#ifndef BRIDGE_VLAN_ID_MAX
531#define BRIDGE_VLAN_ID_MAX 4094 /* Bridge VLAN MAX VLAN ID. */
532#endif
533
59315a06 534#ifndef BRIDGE_FLAGS_MASTER
0dc9a142 535#define BRIDGE_FLAGS_MASTER 1 /* Bridge command to/from parent */
59315a06
TP
536#endif
537
538#ifndef BRIDGE_VLAN_INFO_PVID
539#define BRIDGE_VLAN_INFO_PVID (1<<1) /* VLAN is PVID, ingress untagged */
540#endif
541
542#ifndef BRIDGE_VLAN_INFO_UNTAGGED
543#define BRIDGE_VLAN_INFO_UNTAGGED (1<<2) /* VLAN egresses untagged */
544#endif
545
546#ifndef IFLA_BRIDGE_FLAGS
547#define IFLA_BRIDGE_FLAGS 0
548#endif
549
550#ifndef IFLA_BRIDGE_VLAN_INFO
551#define IFLA_BRIDGE_VLAN_INFO 2
552#endif
553
cc6119a0
CB
554/* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
555enum {
556 __LXC_NETNSA_NONE,
557#define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
558 __LXC_NETNSA_NSID,
559 __LXC_NETNSA_PID,
560 __LXC_NETNSA_FD,
561 __LXC_NETNSA_MAX,
562};
563
b1234129
CB
564/* Length of abstract unix domain socket socket address. */
565#define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
566
c881c810 567/* mount */
6e5655e0
CB
568#ifndef MS_PRIVATE
569#define MS_PRIVATE (1<<18)
c881c810
CB
570#endif
571
572#ifndef MS_SLAVE
573#define MS_SLAVE (1 << 19)
574#endif
575
6e5655e0
CB
576#ifndef MS_LAZYTIME
577#define MS_LAZYTIME (1<<25)
578#endif
579
580#ifndef MS_REC
581#define MS_REC 16384
582#endif
583
37ef15bb
CB
584/* open */
585#ifndef O_PATH
586#define O_PATH 010000000
587#endif
588
589#ifndef O_NOFOLLOW
590#define O_NOFOLLOW 00400000
591#endif
592
604ca1c0
CB
593/* sockets */
594#ifndef SOCK_CLOEXEC
595#define SOCK_CLOEXEC 02000000
596#endif
597
245532a2
CB
598/* pointer conversion macros */
599#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
600#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
601
565eb353
CB
602#define PTR_TO_PID(p) ((pid_t)((intptr_t)(p)))
603#define PID_TO_PTR(u) ((void *)((intptr_t)(u)))
9234406b 604
7d2f7ae1
CB
605#define PTR_TO_UINT64(p) ((uint64_t)((uintptr_t)(p)))
606#define PTR_TO_U64(p) ((__u64)((uintptr_t)(p)))
bf651989 607
372adece
TP
608#define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
609#define PTR_TO_USHORT(p) ((unsigned short)((uintptr_t)(p)))
610
b962868f
CB
611#define LXC_INVALID_UID ((uid_t)-1)
612#define LXC_INVALID_GID ((gid_t)-1)
613
36dee4a2
CB
614#define STRLITERALLEN(x) (sizeof(""x"") - 1)
615#define STRARRAYLEN(x) (sizeof(x) - 1)
616
7c4d9466
CB
617/* Maximum number of bytes sendfile() is able to send in one go. */
618#define LXC_SENDFILE_MAX 0x7ffff000
619
e4edf5d7
CB
620#define move_ptr(ptr) \
621 ({ \
622 typeof(ptr) __internal_ptr__ = (ptr); \
623 (ptr) = NULL; \
624 __internal_ptr__; \
c74da4ab
CB
625 })
626
240fecd0
CB
627#define move_fd(fd) \
628 ({ \
629 int __internal_fd__ = (fd); \
630 (fd) = -EBADF; \
631 __internal_fd__; \
4101805b
CB
632 })
633
d7d1e27a
CB
634#define ret_set_errno(__ret__, __errno__) \
635 ({ \
636 typeof(__ret__) __internal_ret__ = (__ret__); \
637 errno = (__errno__); \
638 __internal_ret__; \
c581d2a6
CB
639 })
640
d7d1e27a
CB
641#define ret_errno(__errno__) \
642 ({ \
643 errno = (__errno__); \
644 -(__errno__); \
9958e6fe
CB
645 })
646
d7d1e27a
CB
647#define free_move_ptr(a, b) \
648 ({ \
649 free(a); \
650 (a) = move_ptr((b)); \
46383a85
CB
651 })
652
eff0e7bb
RK
653/* Container's specific file/directory names */
654#define LXC_CONFIG_FNAME "config"
655#define LXC_PARTIAL_FNAME "partial"
656#define LXC_ROOTFS_DNAME "rootfs"
657#define LXC_TIMESTAMP_FNAME "ts"
658#define LXC_COMMENT_FNAME "comment"
659
bf651989
CB
660#define ARRAY_SIZE(x) \
661 (__builtin_choose_expr(!__builtin_types_compatible_p(typeof(x), \
662 typeof(&*(x))), \
663 sizeof(x) / sizeof((x)[0]), ((void)0)))
664
07002a08
CB
665#ifndef TIOCGPTPEER
666 #if defined __sparc__
667 #define TIOCGPTPEER _IO('t', 137)
668 #else
669 #define TIOCGPTPEER _IO('T', 0x41)
670 #endif
671#endif
672
279c45ee 673#endif /* __LXC_MACRO_H */