]>
Commit | Line | Data |
---|---|---|
279c45ee CB |
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 | ||
85de58d6 | 23 | #include <asm/types.h> |
b3509169 | 24 | #include <limits.h> |
85de58d6 CB |
25 | #include <linux/if_link.h> |
26 | #include <linux/loop.h> | |
27 | #include <linux/netlink.h> | |
28 | #include <linux/rtnetlink.h> | |
938980ba | 29 | #include <linux/types.h> |
245532a2 | 30 | #include <stdint.h> |
2259663c | 31 | #include <string.h> |
85de58d6 CB |
32 | #include <sys/mount.h> |
33 | #include <sys/socket.h> | |
b1234129 | 34 | #include <sys/un.h> |
2955a58a | 35 | #include <unistd.h> |
85de58d6 | 36 | |
b3509169 CB |
37 | #ifndef PATH_MAX |
38 | #define PATH_MAX 4096 | |
39 | #endif | |
40 | ||
279c45ee CB |
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 | ||
ba2b6354 CB |
46 | /* capabilities */ |
47 | #ifndef CAP_SYS_ADMIN | |
48 | #define CAP_SYS_ADMIN 21 | |
49 | #endif | |
50 | ||
279c45ee CB |
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 | |
279c45ee | 62 | |
279c45ee CB |
63 | #ifndef CAP_SETUID |
64 | #define CAP_SETUID 7 | |
65 | #endif | |
66 | ||
67 | #ifndef CAP_SETGID | |
68 | #define CAP_SETGID 6 | |
69 | #endif | |
70 | ||
ba2b6354 | 71 | /* prctl */ |
1f207a5c CB |
72 | #ifndef PR_CAPBSET_READ |
73 | #define PR_CAPBSET_READ 23 | |
74 | #endif | |
75 | ||
ba2b6354 | 76 | #ifndef PR_CAPBSET_DROP |
604ca1c0 CB |
77 | #define PR_CAPBSET_DROP 24 |
78 | #endif | |
79 | ||
1f207a5c CB |
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 | ||
ba2b6354 | 101 | #ifndef PR_SET_NO_NEW_PRIVS |
604ca1c0 CB |
102 | #define PR_SET_NO_NEW_PRIVS 38 |
103 | #endif | |
104 | ||
ba2b6354 | 105 | #ifndef PR_GET_NO_NEW_PRIVS |
604ca1c0 CB |
106 | #define PR_GET_NO_NEW_PRIVS 39 |
107 | #endif | |
108 | ||
ba2b6354 | 109 | /* filesystem magic values */ |
279c45ee CB |
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 | ||
f26dc127 CB |
118 | #ifndef NSFS_MAGIC |
119 | #define NSFS_MAGIC 0x6e736673 | |
120 | #endif | |
121 | ||
ba2b6354 | 122 | /* current overlayfs */ |
37ef15bb CB |
123 | #ifndef OVERLAY_SUPER_MAGIC |
124 | #define OVERLAY_SUPER_MAGIC 0x794c7630 | |
125 | #endif | |
126 | ||
ba2b6354 CB |
127 | /* legacy overlayfs */ |
128 | #ifndef OVERLAYFS_SUPER_MAGIC | |
129 | #define OVERLAYFS_SUPER_MAGIC 0x794c764f | |
130 | #endif | |
131 | ||
f246d9b8 CB |
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 | ||
279c45ee | 147 | /* Useful macros */ |
279c45ee CB |
148 | #define LXC_LINELEN 4096 |
149 | #define LXC_IDMAPLEN 4096 | |
150 | #define LXC_MAX_BUFFER 4096 | |
0c5ea884 | 151 | |
279c45ee CB |
152 | /* /proc/ = 6 |
153 | * + | |
0c5ea884 | 154 | * <pid-as-str> = INTTYPE_TO_STRLEN(pid_t) |
279c45ee CB |
155 | * + |
156 | * /fd/ = 4 | |
157 | * + | |
0c5ea884 | 158 | * <fd-as-str> = INTTYPE_TO_STRLEN(int) |
279c45ee CB |
159 | * + |
160 | * \0 = 1 | |
161 | */ | |
0c9b1f82 CB |
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) | |
0c5ea884 | 186 | |
3c736187 | 187 | #define LXC_CMD_DATA_MAX (PATH_MAX * 2) |
279c45ee CB |
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__ | |
ba2b6354 | 223 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2 * !!(condition)])) |
279c45ee CB |
224 | #else |
225 | extern int __build_bug_on_failed; | |
ba2b6354 CB |
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) | |
279c45ee CB |
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 | ||
b81689a1 CB |
239 | #define prctl_arg(x) ((unsigned long)x) |
240 | ||
4fb34c04 | 241 | /* networking */ |
8df6fa99 CB |
242 | #ifndef NETLINK_DUMP_STRICT_CHK |
243 | #define NETLINK_DUMP_STRICT_CHK 12 | |
244 | #endif | |
245 | ||
d38f5b17 CB |
246 | #ifndef SOL_NETLINK |
247 | #define SOL_NETLINK 270 | |
248 | #endif | |
249 | ||
4fb34c04 CB |
250 | #ifndef IFLA_LINKMODE |
251 | #define IFLA_LINKMODE 17 | |
252 | #endif | |
253 | ||
254 | #ifndef IFLA_LINKINFO | |
255 | #define IFLA_LINKINFO 18 | |
256 | #endif | |
257 | ||
258 | #ifndef IFLA_NET_NS_PID | |
259 | #define IFLA_NET_NS_PID 19 | |
260 | #endif | |
261 | ||
262 | #ifndef IFLA_INFO_KIND | |
263 | #define IFLA_INFO_KIND 1 | |
264 | #endif | |
265 | ||
266 | #ifndef IFLA_VLAN_ID | |
267 | #define IFLA_VLAN_ID 1 | |
268 | #endif | |
269 | ||
270 | #ifndef IFLA_INFO_DATA | |
271 | #define IFLA_INFO_DATA 2 | |
272 | #endif | |
273 | ||
274 | #ifndef VETH_INFO_PEER | |
275 | #define VETH_INFO_PEER 1 | |
276 | #endif | |
277 | ||
278 | #ifndef IFLA_MACVLAN_MODE | |
279 | #define IFLA_MACVLAN_MODE 1 | |
280 | #endif | |
281 | ||
282 | #ifndef IFLA_NEW_NETNSID | |
283 | #define IFLA_NEW_NETNSID 45 | |
284 | #endif | |
285 | ||
cc6119a0 CB |
286 | #ifdef IFLA_IF_NETNSID |
287 | #ifndef IFLA_TARGET_NETNSID | |
288 | #define IFLA_TARGET_NETNSID = IFLA_IF_NETNSID | |
289 | #endif | |
290 | #else | |
4fb34c04 | 291 | #define IFLA_IF_NETNSID 46 |
cc6119a0 CB |
292 | #define IFLA_TARGET_NETNSID 46 |
293 | #endif | |
294 | ||
295 | #ifndef IFA_TARGET_NETNSID | |
296 | #define IFA_TARGET_NETNSID 10 | |
4fb34c04 CB |
297 | #endif |
298 | ||
da5efb6f CB |
299 | #ifndef IFLA_STATS |
300 | #define IFLA_STATS 7 | |
301 | #endif | |
302 | ||
303 | #ifndef IFLA_STATS64 | |
304 | #define IFLA_STATS64 23 | |
305 | #endif | |
cc6119a0 | 306 | |
873c6e87 CB |
307 | #ifndef RTM_NEWNSID |
308 | #define RTM_NEWNSID 88 | |
309 | #endif | |
310 | ||
938980ba CB |
311 | #ifndef RTM_GETNSID |
312 | #define RTM_GETNSID 90 | |
313 | #endif | |
314 | ||
4e3ed0d1 CB |
315 | #ifndef NLMSG_ERROR |
316 | #define NLMSG_ERROR 0x2 | |
317 | #endif | |
318 | ||
7b15813c CB |
319 | #ifndef MACVLAN_MODE_PRIVATE |
320 | #define MACVLAN_MODE_PRIVATE 1 | |
321 | #endif | |
322 | ||
323 | #ifndef MACVLAN_MODE_VEPA | |
324 | #define MACVLAN_MODE_VEPA 2 | |
325 | #endif | |
326 | ||
327 | #ifndef MACVLAN_MODE_BRIDGE | |
328 | #define MACVLAN_MODE_BRIDGE 4 | |
329 | #endif | |
330 | ||
331 | #ifndef MACVLAN_MODE_PASSTHRU | |
332 | #define MACVLAN_MODE_PASSTHRU 8 | |
333 | #endif | |
334 | ||
cc6119a0 CB |
335 | /* Attributes of RTM_NEWNSID/RTM_GETNSID messages */ |
336 | enum { | |
337 | __LXC_NETNSA_NONE, | |
338 | #define __LXC_NETNSA_NSID_NOT_ASSIGNED -1 | |
339 | __LXC_NETNSA_NSID, | |
340 | __LXC_NETNSA_PID, | |
341 | __LXC_NETNSA_FD, | |
342 | __LXC_NETNSA_MAX, | |
343 | }; | |
344 | ||
b1234129 CB |
345 | /* Length of abstract unix domain socket socket address. */ |
346 | #define LXC_AUDS_ADDR_LEN sizeof(((struct sockaddr_un *)0)->sun_path) | |
347 | ||
c881c810 | 348 | /* mount */ |
6e5655e0 CB |
349 | #ifndef MS_PRIVATE |
350 | #define MS_PRIVATE (1<<18) | |
c881c810 CB |
351 | #endif |
352 | ||
353 | #ifndef MS_SLAVE | |
354 | #define MS_SLAVE (1 << 19) | |
355 | #endif | |
356 | ||
6e5655e0 CB |
357 | #ifndef MS_LAZYTIME |
358 | #define MS_LAZYTIME (1<<25) | |
359 | #endif | |
360 | ||
361 | #ifndef MS_REC | |
362 | #define MS_REC 16384 | |
363 | #endif | |
364 | ||
37ef15bb CB |
365 | /* open */ |
366 | #ifndef O_PATH | |
367 | #define O_PATH 010000000 | |
368 | #endif | |
369 | ||
370 | #ifndef O_NOFOLLOW | |
371 | #define O_NOFOLLOW 00400000 | |
372 | #endif | |
373 | ||
604ca1c0 CB |
374 | /* sockets */ |
375 | #ifndef SOCK_CLOEXEC | |
376 | #define SOCK_CLOEXEC 02000000 | |
377 | #endif | |
378 | ||
245532a2 CB |
379 | /* pointer conversion macros */ |
380 | #define PTR_TO_INT(p) ((int)((intptr_t)(p))) | |
381 | #define INT_TO_PTR(u) ((void *)((intptr_t)(u))) | |
382 | ||
9234406b CB |
383 | #define PTR_TO_INTMAX(p) ((intmax_t)((intptr_t)(p))) |
384 | #define INTMAX_TO_PTR(u) ((void *)((intptr_t)(u))) | |
385 | ||
b962868f CB |
386 | #define LXC_INVALID_UID ((uid_t)-1) |
387 | #define LXC_INVALID_GID ((gid_t)-1) | |
388 | ||
36dee4a2 CB |
389 | #define STRLITERALLEN(x) (sizeof(""x"") - 1) |
390 | #define STRARRAYLEN(x) (sizeof(x) - 1) | |
391 | ||
7c4d9466 CB |
392 | /* Maximum number of bytes sendfile() is able to send in one go. */ |
393 | #define LXC_SENDFILE_MAX 0x7ffff000 | |
394 | ||
279c45ee | 395 | #endif /* __LXC_MACRO_H */ |