]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/macro.h
pam: s/MAXPATHLEN/PATH_MAX/g
[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 <limits.h>
25 #include <linux/if_link.h>
26 #include <linux/loop.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/types.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <sys/mount.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <unistd.h>
36
37 #ifndef PATH_MAX
38 #define PATH_MAX 4096
39 #endif
40
41 /* Define __S_ISTYPE if missing from the C library. */
42 #ifndef __S_ISTYPE
43 #define __S_ISTYPE(mode, mask) (((mode)&S_IFMT) == (mask))
44 #endif
45
46 /* capabilities */
47 #ifndef CAP_SYS_ADMIN
48 #define CAP_SYS_ADMIN 21
49 #endif
50
51 #ifndef CAP_SETFCAP
52 #define CAP_SETFCAP 31
53 #endif
54
55 #ifndef CAP_MAC_OVERRIDE
56 #define CAP_MAC_OVERRIDE 32
57 #endif
58
59 #ifndef CAP_MAC_ADMIN
60 #define CAP_MAC_ADMIN 33
61 #endif
62
63 #ifndef CAP_SETUID
64 #define CAP_SETUID 7
65 #endif
66
67 #ifndef CAP_SETGID
68 #define CAP_SETGID 6
69 #endif
70
71 /* prctl */
72 #ifndef PR_CAPBSET_READ
73 #define PR_CAPBSET_READ 23
74 #endif
75
76 #ifndef PR_CAPBSET_DROP
77 #define PR_CAPBSET_DROP 24
78 #endif
79
80 /* Control the ambient capability set */
81 #ifndef PR_CAP_AMBIENT
82 #define PR_CAP_AMBIENT 47
83 #endif
84
85 #ifndef PR_CAP_AMBIENT_IS_SET
86 #define PR_CAP_AMBIENT_IS_SET 1
87 #endif
88
89 #ifndef PR_CAP_AMBIENT_RAISE
90 #define PR_CAP_AMBIENT_RAISE 2
91 #endif
92
93 #ifndef PR_CAP_AMBIENT_LOWER
94 #define PR_CAP_AMBIENT_LOWER 3
95 #endif
96
97 #ifndef PR_CAP_AMBIENT_CLEAR_ALL
98 #define PR_CAP_AMBIENT_CLEAR_ALL 4
99 #endif
100
101 #ifndef PR_SET_NO_NEW_PRIVS
102 #define PR_SET_NO_NEW_PRIVS 38
103 #endif
104
105 #ifndef PR_GET_NO_NEW_PRIVS
106 #define PR_GET_NO_NEW_PRIVS 39
107 #endif
108
109 /* filesystem magic values */
110 #ifndef CGROUP_SUPER_MAGIC
111 #define CGROUP_SUPER_MAGIC 0x27e0eb
112 #endif
113
114 #ifndef CGROUP2_SUPER_MAGIC
115 #define CGROUP2_SUPER_MAGIC 0x63677270
116 #endif
117
118 #ifndef NSFS_MAGIC
119 #define NSFS_MAGIC 0x6e736673
120 #endif
121
122 /* current overlayfs */
123 #ifndef OVERLAY_SUPER_MAGIC
124 #define OVERLAY_SUPER_MAGIC 0x794c7630
125 #endif
126
127 /* legacy overlayfs */
128 #ifndef OVERLAYFS_SUPER_MAGIC
129 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
130 #endif
131
132 /* Calculate the number of chars needed to represent a given integer as a C
133 * string. Include room for '-' to indicate negative numbers and the \0 byte.
134 * This is based on systemd.
135 */
136 #define INTTYPE_TO_STRLEN(type) \
137 (2 + (sizeof(type) <= 1 \
138 ? 3 \
139 : sizeof(type) <= 2 \
140 ? 5 \
141 : sizeof(type) <= 4 \
142 ? 10 \
143 : sizeof(type) <= 8 \
144 ? 20 \
145 : sizeof(int[-2 * (sizeof(type) > 8)])))
146
147 /* Useful macros */
148 #define LXC_LINELEN 4096
149 #define LXC_IDMAPLEN 4096
150 #define LXC_MAX_BUFFER 4096
151
152 /* /proc/ = 6
153 * +
154 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
155 * +
156 * /fd/ = 4
157 * +
158 * <fd-as-str> = INTTYPE_TO_STRLEN(int)
159 * +
160 * \0 = 1
161 */
162 #define LXC_PROC_PID_FD_LEN \
163 (6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1)
164
165 /* /proc/ = 6
166 * +
167 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
168 * +
169 * /status = 7
170 * +
171 * \0 = 1
172 */
173 #define LXC_PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
174
175 /* /proc/ = 6
176 * +
177 * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t)
178 * +
179 * /attr/ = 6
180 * +
181 * /current = 8
182 * +
183 * \0 = 1
184 */
185 #define LXC_LSMATTRLEN (6 + INTTYPE_TO_STRLEN(pid_t) + 6 + 8 + 1)
186
187 #define LXC_CMD_DATA_MAX (PATH_MAX * 2)
188
189 /* loop devices */
190 #ifndef LO_FLAGS_AUTOCLEAR
191 #define LO_FLAGS_AUTOCLEAR 4
192 #endif
193
194 #ifndef LOOP_CTL_GET_FREE
195 #define LOOP_CTL_GET_FREE 0x4C82
196 #endif
197
198 /* memfd_create() */
199 #ifndef MFD_CLOEXEC
200 #define MFD_CLOEXEC 0x0001U
201 #endif
202
203 #ifndef MFD_ALLOW_SEALING
204 #define MFD_ALLOW_SEALING 0x0002U
205 #endif
206
207 /**
208 * BUILD_BUG_ON - break compile if a condition is true.
209 * @condition: the condition which the compiler should know is false.
210 *
211 * If you have some code which relies on certain constants being equal, or
212 * other compile-time-evaluated condition, you should use BUILD_BUG_ON to
213 * detect if someone changes it.
214 *
215 * The implementation uses gcc's reluctance to create a negative array, but
216 * gcc (as of 4.4) only emits that error for obvious cases (eg. not arguments
217 * to inline functions). So as a fallback we use the optimizer; if it can't
218 * prove the condition is false, it will cause a link error on the undefined
219 * "__build_bug_on_failed". This error message can be harder to track down
220 * though, hence the two different methods.
221 */
222 #ifndef __OPTIMIZE__
223 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)]))
224 #else
225 extern int __build_bug_on_failed;
226 #define BUILD_BUG_ON(condition) \
227 do { \
228 ((void)sizeof(char[1 - 2 * !!(condition)])); \
229 if (condition) \
230 __build_bug_on_failed = 1; \
231 } while (0)
232 #endif
233
234 #define lxc_iterate_parts(__iterator, __splitme, __separators) \
235 for (char *__p = NULL, *__it = strtok_r(__splitme, __separators, &__p); \
236 (__iterator = __it); \
237 __iterator = __it = strtok_r(NULL, __separators, &__p))
238
239 #define prctl_arg(x) ((unsigned long)x)
240
241 /* networking */
242 #ifndef IFLA_LINKMODE
243 #define IFLA_LINKMODE 17
244 #endif
245
246 #ifndef IFLA_LINKINFO
247 #define IFLA_LINKINFO 18
248 #endif
249
250 #ifndef IFLA_NET_NS_PID
251 #define IFLA_NET_NS_PID 19
252 #endif
253
254 #ifndef IFLA_INFO_KIND
255 #define IFLA_INFO_KIND 1
256 #endif
257
258 #ifndef IFLA_VLAN_ID
259 #define IFLA_VLAN_ID 1
260 #endif
261
262 #ifndef IFLA_INFO_DATA
263 #define IFLA_INFO_DATA 2
264 #endif
265
266 #ifndef VETH_INFO_PEER
267 #define VETH_INFO_PEER 1
268 #endif
269
270 #ifndef IFLA_MACVLAN_MODE
271 #define IFLA_MACVLAN_MODE 1
272 #endif
273
274 #ifndef IFLA_NEW_NETNSID
275 #define IFLA_NEW_NETNSID 45
276 #endif
277
278 #ifdef IFLA_IF_NETNSID
279 #ifndef IFLA_TARGET_NETNSID
280 #define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID
281 #endif
282 #else
283 #define IFLA_IF_NETNSID 46
284 #define IFLA_TARGET_NETNSID 46
285 #endif
286
287 #ifndef IFA_TARGET_NETNSID
288 #define IFA_TARGET_NETNSID 10
289 #endif
290
291 #ifndef IFLA_STATS
292 #define IFLA_STATS 7
293 #endif
294
295 #ifndef IFLA_STATS64
296 #define IFLA_STATS64 23
297 #endif
298
299 #ifndef RTM_NEWNSID
300 #define RTM_NEWNSID 88
301 #endif
302
303 #ifndef RTM_GETNSID
304 #define RTM_GETNSID 90
305 #endif
306
307 #ifndef NLMSG_ERROR
308 #define NLMSG_ERROR 0x2
309 #endif
310
311 #ifndef MACVLAN_MODE_PRIVATE
312 #define MACVLAN_MODE_PRIVATE 1
313 #endif
314
315 #ifndef MACVLAN_MODE_VEPA
316 #define MACVLAN_MODE_VEPA 2
317 #endif
318
319 #ifndef MACVLAN_MODE_BRIDGE
320 #define MACVLAN_MODE_BRIDGE 4
321 #endif
322
323 #ifndef MACVLAN_MODE_PASSTHRU
324 #define MACVLAN_MODE_PASSTHRU 8
325 #endif
326
327 /* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
328 enum {
329 __LXC_NETNSA_NONE,
330 #define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
331 __LXC_NETNSA_NSID,
332 __LXC_NETNSA_PID,
333 __LXC_NETNSA_FD,
334 __LXC_NETNSA_MAX,
335 };
336
337 /* Length of abstract unix domain socket socket address. */
338 #define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path)
339
340 /* mount */
341 #ifndef MS_PRIVATE
342 #define MS_PRIVATE (1<<18)
343 #endif
344
345 #ifndef MS_SLAVE
346 #define MS_SLAVE (1 << 19)
347 #endif
348
349 #ifndef MS_LAZYTIME
350 #define MS_LAZYTIME (1<<25)
351 #endif
352
353 #ifndef MS_REC
354 #define MS_REC 16384
355 #endif
356
357 /* open */
358 #ifndef O_PATH
359 #define O_PATH 010000000
360 #endif
361
362 #ifndef O_NOFOLLOW
363 #define O_NOFOLLOW 00400000
364 #endif
365
366 /* sockets */
367 #ifndef SOCK_CLOEXEC
368 #define SOCK_CLOEXEC 02000000
369 #endif
370
371 /* pointer conversion macros */
372 #define PTR_TO_INT(p) ((int)((intptr_t)(p)))
373 #define INT_TO_PTR(u) ((void *)((intptr_t)(u)))
374
375 #define PTR_TO_INTMAX(p) ((intmax_t)((intptr_t)(p)))
376 #define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u)))
377
378 #define LXC_INVALID_UID ((uid_t)-1)
379 #define LXC_INVALID_GID ((gid_t)-1)
380
381 #define STRLITERALLEN(x) (sizeof(""x"") - 1)
382 #define STRARRAYLEN(x) (sizeof(x) - 1)
383
384 #endif /* __LXC_MACRO_H */