]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/macro.h
github: Update for main branch
[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
1160ce89
CB
6#include "config.h"
7
85de58d6 8#include <asm/types.h>
b3509169 9#include <limits.h>
85de58d6 10#include <linux/if_link.h>
4771699f 11#include <linux/ioctl.h>
85de58d6
CB
12#include <linux/loop.h>
13#include <linux/netlink.h>
14#include <linux/rtnetlink.h>
938980ba 15#include <linux/types.h>
245532a2 16#include <stdint.h>
2259663c 17#include <string.h>
85de58d6
CB
18#include <sys/mount.h>
19#include <sys/socket.h>
b1234129 20#include <sys/un.h>
2955a58a 21#include <unistd.h>
85de58d6 22
844d929d
CB
23#include "compiler.h"
24
b3509169
CB
25#ifndef PATH_MAX
26#define PATH_MAX 4096
27#endif
28
052535c8 29#ifndef MAX_GRBUF_SIZE
abd833eb 30#define MAX_GRBUF_SIZE 2097152
052535c8
CB
31#endif
32
900b6606
CB
33#define INT64_FMT "%" PRId64
34
279c45ee
CB
35/* Define __S_ISTYPE if missing from the C library. */
36#ifndef __S_ISTYPE
37#define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask))
38#endif
39
54e21c7e
CB
40/*
41 * POLL_ADD flags. Note that since sqe->poll_events is the flag space, the
42 * command flags for POLL_ADD are stored in sqe->len.
43 *
44 * IORING_POLL_ADD_MULTI Multishot poll. Sets IORING_CQE_F_MORE if
45 * the poll handler will continue to report
46 * CQEs on behalf of the same SQE.
47 */
48#ifndef IORING_POLL_ADD_MULTI
49#define IORING_POLL_ADD_MULTI (1U << 0)
50#endif
51
52/*
53 * cqe->flags
54 *
55 * IORING_CQE_F_MORE If set, parent SQE will generate more CQE entries
56 */
57#ifndef IORING_CQE_F_MORE
58#define IORING_CQE_F_MORE (1U << 1)
59#endif
60
ba2b6354 61/* capabilities */
fa934e3e
CB
62#ifndef CAP_CHOWN
63#define CAP_CHOWN 0
64#endif
65
66#ifndef CAP_DAC_OVERRIDE
67#define CAP_DAC_OVERRIDE 1
68#endif
69
70#ifndef CAP_DAC_READ_SEARCH
71#define CAP_DAC_READ_SEARCH 2
72#endif
73
74#ifndef CAP_FOWNER
75#define CAP_FOWNER 3
76#endif
77
78#ifndef CAP_FSETID
79#define CAP_FSETID 4
80#endif
81
82#ifndef CAP_KILL
83#define CAP_KILL 5
84#endif
85
24b77f47 86#ifndef CAP_SETGID
fa934e3e 87#define CAP_SETGID 6
24b77f47
CB
88#endif
89
90#ifndef CAP_SETUID
fa934e3e
CB
91#define CAP_SETUID 7
92#endif
93
94#ifndef CAP_SETPCAP
95#define CAP_SETPCAP 8
96#endif
97
98#ifndef CAP_LINUX_IMMUTABLE
99#define CAP_LINUX_IMMUTABLE 9
100#endif
101
102#ifndef CAP_NET_BIND_SERVICE
103#define CAP_NET_BIND_SERVICE 10
104#endif
105
106#ifndef CAP_NET_BROADCAST
107#define CAP_NET_BROADCAST 11
108#endif
109
110#ifndef CAP_NET_ADMIN
111#define CAP_NET_ADMIN 12
112#endif
113
114#ifndef CAP_NET_RAW
115#define CAP_NET_RAW 13
116#endif
117
118#ifndef CAP_IPC_LOCK
119#define CAP_IPC_LOCK 14
120#endif
121
122#ifndef CAP_IPC_OWNER
123#define CAP_IPC_OWNER 15
124#endif
125
126#ifndef CAP_SYS_MODULE
127#define CAP_SYS_MODULE 16
128#endif
129
130#ifndef CAP_SYS_RAWIO
131#define CAP_SYS_RAWIO 17
132#endif
133
134#ifndef CAP_SYS_CHROOT
135#define CAP_SYS_CHROOT 18
136#endif
137
138#ifndef CAP_SYS_PTRACE
139#define CAP_SYS_PTRACE 19
140#endif
141
142#ifndef CAP_SYS_PACCT
143#define CAP_SYS_PACCT 20
24b77f47
CB
144#endif
145
ba2b6354 146#ifndef CAP_SYS_ADMIN
fa934e3e
CB
147#define CAP_SYS_ADMIN 21
148#endif
149
150#ifndef CAP_SYS_BOOT
151#define CAP_SYS_BOOT 22
152#endif
153
154#ifndef CAP_SYS_NICE
155#define CAP_SYS_NICE 23
156#endif
157
158#ifndef CAP_SYS_RESOURCE
159#define CAP_SYS_RESOURCE 24
160#endif
161
162#ifndef CAP_SYS_TIME
163#define CAP_SYS_TIME 25
164#endif
165
166#ifndef CAP_SYS_TTY_CONFIG
167#define CAP_SYS_TTY_CONFIG 26
168#endif
169
170#ifndef CAP_MKNOD
171#define CAP_MKNOD 27
172#endif
173
174#ifndef CAP_LEASE
175#define CAP_LEASE 28
ba2b6354
CB
176#endif
177
f2da98c0 178#ifndef CAP_AUDIT_WRITE
fa934e3e 179#define CAP_AUDIT_WRITE 29
f2da98c0
CB
180#endif
181
182#ifndef CAP_AUDIT_CONTROL
fa934e3e 183#define CAP_AUDIT_CONTROL 30
f2da98c0
CB
184#endif
185
279c45ee 186#ifndef CAP_SETFCAP
fa934e3e 187#define CAP_SETFCAP 31
279c45ee
CB
188#endif
189
190#ifndef CAP_MAC_OVERRIDE
fa934e3e 191#define CAP_MAC_OVERRIDE 32
279c45ee
CB
192#endif
193
194#ifndef CAP_MAC_ADMIN
fa934e3e 195#define CAP_MAC_ADMIN 33
279c45ee 196#endif
279c45ee 197
f2da98c0 198#ifndef CAP_SYSLOG
fa934e3e 199#define CAP_SYSLOG 34
f2da98c0
CB
200#endif
201
202#ifndef CAP_WAKE_ALARM
fa934e3e 203#define CAP_WAKE_ALARM 35
f2da98c0
CB
204#endif
205
206#ifndef CAP_BLOCK_SUSPEND
fa934e3e 207#define CAP_BLOCK_SUSPEND 36
f2da98c0
CB
208#endif
209
210#ifndef CAP_AUDIT_READ
fa934e3e 211#define CAP_AUDIT_READ 37
f2da98c0
CB
212#endif
213
7b4cd468 214#ifndef CAP_PERFMON
fa934e3e 215#define CAP_PERFMON 38
7b4cd468
CB
216#endif
217
218#ifndef CAP_BPF
fa934e3e 219#define CAP_BPF 39
7b4cd468
CB
220#endif
221
222#ifndef CAP_CHECKPOINT_RESTORE
fa934e3e 223#define CAP_CHECKPOINT_RESTORE 40
7b4cd468
CB
224#endif
225
ba2b6354 226/* prctl */
1f207a5c
CB
227#ifndef PR_CAPBSET_READ
228#define PR_CAPBSET_READ 23
229#endif
230
ba2b6354 231#ifndef PR_CAPBSET_DROP
604ca1c0
CB
232#define PR_CAPBSET_DROP 24
233#endif
234
1f207a5c
CB
235/* Control the ambient capability set */
236#ifndef PR_CAP_AMBIENT
237#define PR_CAP_AMBIENT 47
238#endif
239
240#ifndef PR_CAP_AMBIENT_IS_SET
241#define PR_CAP_AMBIENT_IS_SET 1
242#endif
243
244#ifndef PR_CAP_AMBIENT_RAISE
245#define PR_CAP_AMBIENT_RAISE 2
246#endif
247
248#ifndef PR_CAP_AMBIENT_LOWER
249#define PR_CAP_AMBIENT_LOWER 3
250#endif
251
252#ifndef PR_CAP_AMBIENT_CLEAR_ALL
253#define PR_CAP_AMBIENT_CLEAR_ALL 4
254#endif
255
ba2b6354 256#ifndef PR_SET_NO_NEW_PRIVS
604ca1c0
CB
257#define PR_SET_NO_NEW_PRIVS 38
258#endif
259
ba2b6354 260#ifndef PR_GET_NO_NEW_PRIVS
604ca1c0
CB
261#define PR_GET_NO_NEW_PRIVS 39
262#endif
263
ba2b6354 264/* filesystem magic values */
279c45ee
CB
265#ifndef CGROUP_SUPER_MAGIC
266#define CGROUP_SUPER_MAGIC 0x27e0eb
267#endif
268
269#ifndef CGROUP2_SUPER_MAGIC
270#define CGROUP2_SUPER_MAGIC 0x63677270
271#endif
272
f26dc127
CB
273#ifndef NSFS_MAGIC
274#define NSFS_MAGIC 0x6e736673
275#endif
276
ba2b6354 277/* current overlayfs */
37ef15bb
CB
278#ifndef OVERLAY_SUPER_MAGIC
279#define OVERLAY_SUPER_MAGIC 0x794c7630
280#endif
281
ba2b6354
CB
282/* legacy overlayfs */
283#ifndef OVERLAYFS_SUPER_MAGIC
284#define OVERLAYFS_SUPER_MAGIC 0x794c764f
285#endif
286
f246d9b8
CB
287/* Calculate the number of chars needed to represent a given integer as a C
288 * string. Include room for '-' to indicate negative numbers and the \0 byte.
289 * This is based on systemd.
290 */
291#define INTTYPE_TO_STRLEN(type) \
292 (2 + (sizeof(type) <= 1 \
293 ? 3 \
294 : sizeof(type) <= 2 \
295 ? 5 \
296 : sizeof(type) <= 4 \
297 ? 10 \
298 : sizeof(type) <= 8 \
299 ? 20 \
300 : sizeof(int[-2 * (sizeof(type) > 8)])))
301
279c45ee 302/* Useful macros */
279c45ee
CB
303#define LXC_LINELEN 4096
304#define LXC_IDMAPLEN 4096
305#define LXC_MAX_BUFFER 4096
b280bc38 306#define LXC_NAMESPACE_NAME_MAX 256
0c5ea884 307
c538837d
CB
308/* /proc/ = 6
309 * +
310 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
311 * +
312 * /fd/ = 4
313 * +
314 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
315 * +
316 * \0 = 1
317 */
318#define LXC_PROC_PID_LEN \
319 (6 + INTTYPE_TO_STRLEN(pid_t) + 1)
320
279c45ee
CB
321/* /proc/ = 6
322 * +
0c5ea884 323 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
279c45ee
CB
324 * +
325 * /fd/ = 4
326 * +
0c5ea884 327 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
279c45ee
CB
328 * +
329 * \0 = 1
330 */
0c9b1f82
CB
331#define LXC_PROC_PID_FD_LEN \
332 (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
333
4a233eab
CB
334/* /proc/self/fd/ = 14
335 * +
336 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
337 * +
338 * \0 = 1
339 */
340#define LXC_PROC_SELF_FD_LEN (14 + INTTYPE_TO_STRLEN(int) + 1)
341
0c9b1f82
CB
342/* /proc/ = 6
343 * +
344 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
345 * +
346 * /status = 7
347 * +
348 * \0 = 1
349 */
350#define LXC_PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
351
352/* /proc/ = 6
353 * +
354 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
355 * +
356 * /attr/ = 6
357 * +
47f4914d
AS
358 * /apparmor/ = 10
359 * +
0c9b1f82
CB
360 * /current = 8
361 * +
362 * \0 = 1
363 */
47f4914d 364#define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 10 + 8 + 1)
0c5ea884 365
8db6be1b
CB
366/* MAX_NS_PROC_NAME = MAX_NS_PROC_NAME
367 * +
368 * : = 1
369 * +
370 * /proc/ = 6
371 * +
372 * <pid-as_str> = INTTYPE_TO_STRLEN(pid_t)
373 * +
374 * /fd/ = 4
375 * +
376 * <int-as-str> = INTTYPE_TO_STRLEN(int)
377 * +
378 * \0 = 1
379 */
380#define LXC_EXPOSE_NAMESPACE_LEN \
381 (MAX_NS_PROC_NAME + 1 + 6 + INTTYPE_TO_STRLEN(pid_t) + 4 + \
382 INTTYPE_TO_STRLEN(int) + 1)
383
3c736187 384#define LXC_CMD_DATA_MAX (PATH_MAX * 2)
279c45ee
CB
385
386/* loop devices */
387#ifndef LO_FLAGS_AUTOCLEAR
388#define LO_FLAGS_AUTOCLEAR 4
389#endif
390
391#ifndef LOOP_CTL_GET_FREE
392#define LOOP_CTL_GET_FREE 0x4C82
393#endif
394
395/* memfd_create() */
396#ifndef MFD_CLOEXEC
397#define MFD_CLOEXEC 0x0001U
398#endif
399
400#ifndef MFD_ALLOW_SEALING
401#define MFD_ALLOW_SEALING 0x0002U
402#endif
403
404/**
405 * BUILD_BUG_ON - break compile if a condition is true.
406 * @condition: the condition which the compiler should know is false.
407 *
408 * If you have some code which relies on certain constants being equal, or
409 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
410 * detect if someone changes it.
411 *
412 * The implementation uses gcc's reluctance to create a negative array, but
413 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
414 * to inline functions). So as a fallback we use the optimizer; if it can't
415 * prove the condition is false, it will cause a link error on the undefined
416 * "__build_bug_on_failed". This error message can be harder to track down
417 * though, hence the two different methods.
418 */
419#ifndef __OPTIMIZE__
ba2b6354 420#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
279c45ee
CB
421#else
422extern int __build_bug_on_failed;
ba2b6354
CB
423#define BUILD_BUG_ON(condition) \
424 do { \
425 ((void)sizeof(char[1 - 2 * !!(condition)])); \
426 if (condition) \
427 __build_bug_on_failed = 1; \
428 } while (0)
279c45ee
CB
429#endif
430
b81689a1
CB
431#define prctl_arg(x) ((unsigned long)x)
432
4fb34c04 433/* networking */
c8ca5a43
CB
434#ifndef NETLINK_GET_STRICT_CHK
435#define NETLINK_GET_STRICT_CHK 12
8df6fa99
CB
436#endif
437
d38f5b17
CB
438#ifndef SOL_NETLINK
439#define SOL_NETLINK 270
440#endif
441
4fb34c04
CB
442#ifndef IFLA_LINKMODE
443#define IFLA_LINKMODE 17
444#endif
445
446#ifndef IFLA_LINKINFO
447#define IFLA_LINKINFO 18
448#endif
449
450#ifndef IFLA_NET_NS_PID
451#define IFLA_NET_NS_PID 19
452#endif
453
0037ab49
TP
454#ifndef IFLA_NET_NS_FD
455#define IFLA_NET_NS_FD 28
456#endif
457
4fb34c04
CB
458#ifndef IFLA_INFO_KIND
459#define IFLA_INFO_KIND 1
460#endif
461
462#ifndef IFLA_VLAN_ID
463#define IFLA_VLAN_ID 1
464#endif
465
466#ifndef IFLA_INFO_DATA
467#define IFLA_INFO_DATA 2
468#endif
469
470#ifndef VETH_INFO_PEER
471#define VETH_INFO_PEER 1
472#endif
473
3f0ed090
TP
474#ifndef VETH_MODE_BRIDGE
475#define VETH_MODE_BRIDGE 1
476#endif
477
478#ifndef VETH_MODE_ROUTER
479#define VETH_MODE_ROUTER 2
480#endif
481
4fb34c04
CB
482#ifndef IFLA_MACVLAN_MODE
483#define IFLA_MACVLAN_MODE 1
484#endif
485
c9f52382 486#ifndef IFLA_IPVLAN_MODE
487#define IFLA_IPVLAN_MODE 1
488#endif
489
490#ifndef IFLA_IPVLAN_ISOLATION
491#define IFLA_IPVLAN_ISOLATION 2
492#endif
493
4fb34c04
CB
494#ifndef IFLA_NEW_NETNSID
495#define IFLA_NEW_NETNSID 45
496#endif
497
cc6119a0
CB
498#ifdef IFLA_IF_NETNSID
499#ifndef IFLA_TARGET_NETNSID
500#define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
501#endif
502#else
4fb34c04 503#define IFLA_IF_NETNSID 46
cc6119a0
CB
504#define IFLA_TARGET_NETNSID 46
505#endif
506
507#ifndef IFA_TARGET_NETNSID
508#define IFA_TARGET_NETNSID 10
4fb34c04
CB
509#endif
510
da5efb6f
CB
511#ifndef IFLA_STATS
512#define IFLA_STATS 7
513#endif
514
515#ifndef IFLA_STATS64
516#define IFLA_STATS64 23
517#endif
cc6119a0 518
873c6e87
CB
519#ifndef RTM_NEWNSID
520#define RTM_NEWNSID 88
521#endif
522
938980ba
CB
523#ifndef RTM_GETNSID
524#define RTM_GETNSID 90
525#endif
526
4e3ed0d1
CB
527#ifndef NLMSG_ERROR
528#define NLMSG_ERROR 0x2
529#endif
530
7b15813c
CB
531#ifndef MACVLAN_MODE_PRIVATE
532#define MACVLAN_MODE_PRIVATE 1
533#endif
534
535#ifndef MACVLAN_MODE_VEPA
536#define MACVLAN_MODE_VEPA 2
537#endif
538
539#ifndef MACVLAN_MODE_BRIDGE
540#define MACVLAN_MODE_BRIDGE 4
541#endif
542
543#ifndef MACVLAN_MODE_PASSTHRU
544#define MACVLAN_MODE_PASSTHRU 8
545#endif
546
c9f52382 547#ifndef IPVLAN_MODE_L2
548#define IPVLAN_MODE_L2 0
549#endif
550
551#ifndef IPVLAN_MODE_L3
552#define IPVLAN_MODE_L3 1
553#endif
554
555#ifndef IPVLAN_MODE_L3S
556#define IPVLAN_MODE_L3S 2
557#endif
558
559#ifndef IPVLAN_ISOLATION_BRIDGE
560#define IPVLAN_ISOLATION_BRIDGE 0
561#endif
562
563#ifndef IPVLAN_ISOLATION_PRIVATE
564#define IPVLAN_ISOLATION_PRIVATE 1
565#endif
566
567#ifndef IPVLAN_ISOLATION_VEPA
568#define IPVLAN_ISOLATION_VEPA 2
569#endif
570
53a9ba7f
TP
571#ifndef BRIDGE_VLAN_NONE
572#define BRIDGE_VLAN_NONE -1 /* Bridge VLAN option set to "none". */
573#endif
574
2abd5206
TP
575#ifndef BRIDGE_VLAN_ID_MAX
576#define BRIDGE_VLAN_ID_MAX 4094 /* Bridge VLAN MAX VLAN ID. */
577#endif
578
59315a06 579#ifndef BRIDGE_FLAGS_MASTER
0dc9a142 580#define BRIDGE_FLAGS_MASTER 1 /* Bridge command to/from parent */
59315a06
TP
581#endif
582
583#ifndef BRIDGE_VLAN_INFO_PVID
584#define BRIDGE_VLAN_INFO_PVID (1<<1) /* VLAN is PVID, ingress untagged */
585#endif
586
587#ifndef BRIDGE_VLAN_INFO_UNTAGGED
588#define BRIDGE_VLAN_INFO_UNTAGGED (1<<2) /* VLAN egresses untagged */
589#endif
590
591#ifndef IFLA_BRIDGE_FLAGS
592#define IFLA_BRIDGE_FLAGS 0
593#endif
594
595#ifndef IFLA_BRIDGE_VLAN_INFO
596#define IFLA_BRIDGE_VLAN_INFO 2
597#endif
598
cc6119a0
CB
599/* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
600enum {
601 __LXC_NETNSA_NONE,
602#define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
603 __LXC_NETNSA_NSID,
604 __LXC_NETNSA_PID,
605 __LXC_NETNSA_FD,
606 __LXC_NETNSA_MAX,
607};
608
b1234129
CB
609/* Length of abstract unix domain socket socket address. */
610#define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
611
c881c810 612/* mount */
6e5655e0
CB
613#ifndef MS_PRIVATE
614#define MS_PRIVATE (1<<18)
c881c810
CB
615#endif
616
617#ifndef MS_SLAVE
618#define MS_SLAVE (1 << 19)
619#endif
620
6e5655e0
CB
621#ifndef MS_LAZYTIME
622#define MS_LAZYTIME (1<<25)
623#endif
624
625#ifndef MS_REC
626#define MS_REC 16384
627#endif
628
37ef15bb
CB
629/* open */
630#ifndef O_PATH
631#define O_PATH 010000000
632#endif
633
634#ifndef O_NOFOLLOW
635#define O_NOFOLLOW 00400000
636#endif
637
604ca1c0
CB
638/* sockets */
639#ifndef SOCK_CLOEXEC
640#define SOCK_CLOEXEC 02000000
641#endif
642
245532a2
CB
643/* pointer conversion macros */
644#define PTR_TO_INT(p) ((int)((intptr_t)(p)))
645#define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
646
565eb353
CB
647#define PTR_TO_PID(p) ((pid_t)((intptr_t)(p)))
648#define PID_TO_PTR(u) ((void *)((intptr_t)(u)))
9234406b 649
7d2f7ae1
CB
650#define PTR_TO_UINT64(p) ((uint64_t)((uintptr_t)(p)))
651#define PTR_TO_U64(p) ((__u64)((uintptr_t)(p)))
bf651989 652
372adece
TP
653#define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
654#define PTR_TO_USHORT(p) ((unsigned short)((uintptr_t)(p)))
655
b962868f
CB
656#define LXC_INVALID_UID ((uid_t)-1)
657#define LXC_INVALID_GID ((gid_t)-1)
658
36dee4a2
CB
659#define STRLITERALLEN(x) (sizeof(""x"") - 1)
660#define STRARRAYLEN(x) (sizeof(x) - 1)
661
7c4d9466
CB
662/* Maximum number of bytes sendfile() is able to send in one go. */
663#define LXC_SENDFILE_MAX 0x7ffff000
664
e4edf5d7
CB
665#define move_ptr(ptr) \
666 ({ \
667 typeof(ptr) __internal_ptr__ = (ptr); \
668 (ptr) = NULL; \
669 __internal_ptr__; \
c74da4ab
CB
670 })
671
240fecd0
CB
672#define move_fd(fd) \
673 ({ \
674 int __internal_fd__ = (fd); \
675 (fd) = -EBADF; \
676 __internal_fd__; \
4101805b
CB
677 })
678
d7d1e27a
CB
679#define ret_set_errno(__ret__, __errno__) \
680 ({ \
681 typeof(__ret__) __internal_ret__ = (__ret__); \
682 errno = (__errno__); \
683 __internal_ret__; \
c581d2a6
CB
684 })
685
0a3b86f9
CB
686#define ret_errno(__errno__) \
687 ({ \
688 errno = labs(__errno__); \
689 -errno; \
9958e6fe
CB
690 })
691
eff0e7bb
RK
692/* Container's specific file/directory names */
693#define LXC_CONFIG_FNAME "config"
694#define LXC_PARTIAL_FNAME "partial"
695#define LXC_ROOTFS_DNAME "rootfs"
696#define LXC_TIMESTAMP_FNAME "ts"
697#define LXC_COMMENT_FNAME "comment"
698
bf651989
CB
699#define ARRAY_SIZE(x) \
700 (__builtin_choose_expr(!__builtin_types_compatible_p(typeof(x), \
701 typeof(&*(x))), \
702 sizeof(x) / sizeof((x)[0]), ((void)0)))
703
07002a08
CB
704#ifndef TIOCGPTPEER
705 #if defined __sparc__
706 #define TIOCGPTPEER _IO('t', 137)
707 #else
708 #define TIOCGPTPEER _IO('T', 0x41)
709 #endif
710#endif
711
6de35cd9
CB
712#define ENOCGROUP2 ENOMEDIUM
713
531d36ad
CB
714#define MAX_FILENO ~0U
715
a38a34f8
CB
716#define swap(a, b) \
717 do { \
718 typeof(a) __tmp = (a); \
719 (a) = (b); \
720 (b) = __tmp; \
721 } while (0)
722
a1feaf29
CB
723#define min(x, y) \
724 ({ \
725 typeof(x) _min1 = (x); \
726 typeof(y) _min2 = (y); \
727 (void)(&_min1 == &_min2); \
728 _min1 < _min2 ? _min1 : _min2; \
729 })
730
265dc73a
CB
731#define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); })))
732
733/*
734 * Compile time versions of __arch_hweightN()
735 */
736#define __const_hweight8(w) \
737 ((unsigned int) \
738 ((!!((w) & (1ULL << 0))) + \
739 (!!((w) & (1ULL << 1))) + \
740 (!!((w) & (1ULL << 2))) + \
741 (!!((w) & (1ULL << 3))) + \
742 (!!((w) & (1ULL << 4))) + \
743 (!!((w) & (1ULL << 5))) + \
744 (!!((w) & (1ULL << 6))) + \
745 (!!((w) & (1ULL << 7)))))
746
747#define __const_hweight16(w) (__const_hweight8(w) + __const_hweight8((w) >> 8 ))
748#define __const_hweight32(w) (__const_hweight16(w) + __const_hweight16((w) >> 16))
749#define __const_hweight64(w) (__const_hweight32(w) + __const_hweight32((w) >> 32))
750
751#define hweight8(w) __const_hweight8(w)
752#define hweight16(w) __const_hweight16(w)
753#define hweight32(w) __const_hweight32(w)
754#define hweight64(w) __const_hweight64(w)
755
db4af8c5 756#if !HAVE___ALIGNED_U64
b1abf15d
CB
757#define __aligned_u64 __u64 __attribute__((aligned(8)))
758#endif
759
36513635
CB
760#define BITS_PER_BYTE 8
761#define BITS_PER_TYPE(type) (sizeof(type) * 8)
762#define LAST_BIT_PER_TYPE(type) (BITS_PER_TYPE(type) - 1)
763
db4af8c5 764#if !HAVE_SYS_PERSONALITY_H
3857c4eb
CB
765#define PER_LINUX 0x0000
766#define PER_LINUX32 0x0008
767#endif
768
543d2f83
CB
769static inline bool has_exact_flags(__u32 flags, __u32 mask)
770{
771 return (flags & mask) == mask;
772}
773
4780b5e7
CB
774/**
775 * container_of - cast a member of a structure out to the containing structure
776 * @ptr: the pointer to the member.
777 * @type: the type of the container struct this is embedded in.
778 * @member: the name of the member within the struct.
779 *
780 */
781#define container_of(ptr, type, member) ({ \
782 void *__mptr = (void *)(ptr); \
783 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
784 !__same_type(*(ptr), void), \
785 "pointer type mismatch in container_of()"); \
786 ((type *)(__mptr - offsetof(type, member))); })
787
2e9aa146
CB
788typedef long long unsigned int llu;
789
cafcd2a7
CB
790/* Taken over modified from the kernel sources. */
791#define NBITS 32 /* bits in uint32_t */
792#define DIV_ROUND_UP(n, d) (((n) + (d)-1) / (d))
793#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, NBITS)
794
623f47b0 795static inline void set_bit(__u32 bit, __u32 *bitarr)
cafcd2a7 796{
623f47b0 797 bitarr[bit / NBITS] |= ((__u32)1 << (bit % NBITS));
cafcd2a7
CB
798}
799
623f47b0 800static inline void clear_bit(__u32 bit, __u32 *bitarr)
cafcd2a7 801{
623f47b0 802 bitarr[bit / NBITS] &= ~((__u32)1 << (bit % NBITS));
cafcd2a7
CB
803}
804
623f47b0 805static inline bool is_set(__u32 bit, __u32 *bitarr)
cafcd2a7 806{
623f47b0 807 return (bitarr[bit / NBITS] & ((__u32)1 << (bit % NBITS))) != 0;
cafcd2a7
CB
808}
809
62054cf3
CB
810#define BIT(nr) (1UL << (nr))
811
4771699f
CB
812#ifndef FS_IOC_GETFLAGS
813#define FS_IOC_GETFLAGS _IOR('f', 1, long)
814#endif
815
816#ifndef FS_IOC_SETFLAGS
817#define FS_IOC_SETFLAGS _IOW('f', 2, long)
818#endif
819
820#ifndef FS_IMMUTABLE_FL
821#define FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
822#endif
823
279c45ee 824#endif /* __LXC_MACRO_H */