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