2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
6 * Daniel Lezcano <daniel.lezcano at free.fr>
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #define __STDC_FORMAT_MACROS
29 #include <arpa/inet.h>
36 #include <netinet/in.h>
41 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/utsname.h>
52 #include "confile_utils.h"
53 #include "../include/netns_ifaddrs.h"
55 #include "lxcseccomp.h"
61 #if HAVE_SYS_PERSONALITY_H
62 #include <sys/personality.h>
66 #include "include/strlcpy.h"
70 #include "include/strlcat.h"
73 lxc_log_define(confile
, lxc
);
75 #define lxc_config_define(name) \
76 __hot static int set_config_##name(const char *, const char *, \
77 struct lxc_conf *, void *); \
78 __hot static int get_config_##name(const char *, char *, int, \
79 struct lxc_conf *, void *); \
80 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
83 lxc_config_define(autodev
);
84 lxc_config_define(apparmor_allow_incomplete
);
85 lxc_config_define(apparmor_allow_nesting
);
86 lxc_config_define(apparmor_profile
);
87 lxc_config_define(apparmor_raw
);
88 lxc_config_define(cap_drop
);
89 lxc_config_define(cap_keep
);
90 lxc_config_define(cgroup_controller
);
91 lxc_config_define(cgroup2_controller
);
92 lxc_config_define(cgroup_dir
);
93 lxc_config_define(cgroup_relative
);
94 lxc_config_define(console_buffer_size
);
95 lxc_config_define(console_logfile
);
96 lxc_config_define(console_path
);
97 lxc_config_define(console_rotate
);
98 lxc_config_define(console_size
);
99 lxc_config_define(environment
);
100 lxc_config_define(ephemeral
);
101 lxc_config_define(execute_cmd
);
102 lxc_config_define(group
);
103 lxc_config_define(hooks
);
104 lxc_config_define(hooks_version
);
105 lxc_config_define(idmaps
);
106 lxc_config_define(includefiles
);
107 lxc_config_define(init_cmd
);
108 lxc_config_define(init_cwd
);
109 lxc_config_define(init_gid
);
110 lxc_config_define(init_uid
);
111 lxc_config_define(log_file
);
112 lxc_config_define(log_level
);
113 lxc_config_define(log_syslog
);
114 lxc_config_define(monitor
);
115 lxc_config_define(monitor_signal_pdeath
);
116 lxc_config_define(mount
);
117 lxc_config_define(mount_auto
);
118 lxc_config_define(mount_fstab
);
119 lxc_config_define(namespace_clone
);
120 lxc_config_define(namespace_keep
);
121 lxc_config_define(namespace_share
);
122 lxc_config_define(net
);
123 lxc_config_define(net_flags
);
124 lxc_config_define(net_hwaddr
);
125 lxc_config_define(net_ipv4_address
);
126 lxc_config_define(net_ipv4_gateway
);
127 lxc_config_define(net_ipv6_address
);
128 lxc_config_define(net_ipv6_gateway
);
129 lxc_config_define(net_link
);
130 lxc_config_define(net_macvlan_mode
);
131 lxc_config_define(net_mtu
);
132 lxc_config_define(net_name
);
133 lxc_config_define(net_nic
);
134 lxc_config_define(net_script_down
);
135 lxc_config_define(net_script_up
);
136 lxc_config_define(net_type
);
137 lxc_config_define(net_veth_pair
);
138 lxc_config_define(net_vlan_id
);
139 lxc_config_define(no_new_privs
);
140 lxc_config_define(personality
);
141 lxc_config_define(prlimit
);
142 lxc_config_define(pty_max
);
143 lxc_config_define(rootfs_managed
);
144 lxc_config_define(rootfs_mount
);
145 lxc_config_define(rootfs_options
);
146 lxc_config_define(rootfs_path
);
147 lxc_config_define(seccomp_profile
);
148 lxc_config_define(selinux_context
);
149 lxc_config_define(signal_halt
);
150 lxc_config_define(signal_reboot
);
151 lxc_config_define(signal_stop
);
152 lxc_config_define(start
);
153 lxc_config_define(tty_max
);
154 lxc_config_define(tty_dir
);
155 lxc_config_define(uts_name
);
156 lxc_config_define(sysctl
);
157 lxc_config_define(proc
);
159 static struct lxc_config_t config
[] = {
160 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
161 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
162 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
163 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
164 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
165 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
166 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
167 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
168 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
169 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
170 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
171 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
172 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
173 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
174 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
175 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
176 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
177 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
178 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
179 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
180 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
181 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
182 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
183 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
184 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
185 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
188 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
190 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
192 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
193 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
194 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
195 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
196 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
197 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
198 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
199 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
200 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
201 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
202 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
203 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
204 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
205 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
206 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
207 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
208 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
209 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
210 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
211 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
212 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
213 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
214 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
215 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
216 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
217 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
218 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
219 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
220 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
221 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
222 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
223 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
224 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
225 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
226 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
227 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
228 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
229 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
230 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
231 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
232 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
233 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
234 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
235 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
236 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
237 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
238 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
239 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
240 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
241 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
242 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
243 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
244 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
245 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
246 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
249 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
251 struct lxc_config_t
*lxc_get_config(const char *key
)
255 for (i
= 0; i
< config_size
; i
++)
256 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
262 static int set_config_net(const char *key
, const char *value
,
263 struct lxc_conf
*lxc_conf
, void *data
)
265 if (!lxc_config_value_empty(value
)) {
266 ERROR("lxc.net must not have a value");
270 return clr_config_net(key
, lxc_conf
, data
);
273 static int set_config_net_type(const char *key
, const char *value
,
274 struct lxc_conf
*lxc_conf
, void *data
)
276 struct lxc_netdev
*netdev
= data
;
278 if (lxc_config_value_empty(value
))
279 return clr_config_net_type(key
, lxc_conf
, data
);
284 if (!strcmp(value
, "veth")) {
285 netdev
->type
= LXC_NET_VETH
;
286 } else if (!strcmp(value
, "macvlan")) {
287 netdev
->type
= LXC_NET_MACVLAN
;
288 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
290 } else if (!strcmp(value
, "vlan")) {
291 netdev
->type
= LXC_NET_VLAN
;
292 } else if (!strcmp(value
, "phys")) {
293 netdev
->type
= LXC_NET_PHYS
;
294 } else if (!strcmp(value
, "empty")) {
295 netdev
->type
= LXC_NET_EMPTY
;
296 } else if (!strcmp(value
, "none")) {
297 netdev
->type
= LXC_NET_NONE
;
299 ERROR("invalid network type %s", value
);
306 static int set_config_net_flags(const char *key
, const char *value
,
307 struct lxc_conf
*lxc_conf
, void *data
)
309 struct lxc_netdev
*netdev
= data
;
311 if (lxc_config_value_empty(value
))
312 return clr_config_net_flags(key
, lxc_conf
, data
);
317 netdev
->flags
|= IFF_UP
;
322 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
323 struct lxc_netdev
*netdev
)
325 struct netns_ifaddrs
*ifaddr
, *ifa
;
328 const char *type_key
= "lxc.net.type";
329 const char *link_key
= "lxc.net.link";
330 const char *tmpvalue
= "phys";
332 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0) {
333 SYSERROR("Get network interfaces failed");
337 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
340 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
343 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
344 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
347 ret
= set_config_net_link(
348 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
350 ERROR("failed to create matched ifnames");
354 ERROR("failed to create matched ifnames");
360 netns_freeifaddrs(ifaddr
);
366 static int set_config_net_link(const char *key
, const char *value
,
367 struct lxc_conf
*lxc_conf
, void *data
)
369 struct lxc_netdev
*netdev
= data
;
372 if (lxc_config_value_empty(value
))
373 return clr_config_net_link(key
, lxc_conf
, data
);
378 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
379 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
381 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
386 static int set_config_net_name(const char *key
, const char *value
,
387 struct lxc_conf
*lxc_conf
, void *data
)
389 struct lxc_netdev
*netdev
= data
;
391 if (lxc_config_value_empty(value
))
392 return clr_config_net_name(key
, lxc_conf
, data
);
397 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
400 static int set_config_net_veth_pair(const char *key
, const char *value
,
401 struct lxc_conf
*lxc_conf
, void *data
)
403 struct lxc_netdev
*netdev
= data
;
405 if (lxc_config_value_empty(value
))
406 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
411 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
414 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
415 struct lxc_conf
*lxc_conf
, void *data
)
417 struct lxc_netdev
*netdev
= data
;
419 if (lxc_config_value_empty(value
))
420 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
425 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
428 static int set_config_net_hwaddr(const char *key
, const char *value
,
429 struct lxc_conf
*lxc_conf
, void *data
)
431 struct lxc_netdev
*netdev
= data
;
434 if (lxc_config_value_empty(value
))
435 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
440 new_value
= strdup(value
);
444 rand_complete_hwaddr(new_value
);
446 if (lxc_config_value_empty(new_value
)) {
448 netdev
->hwaddr
= NULL
;
452 netdev
->hwaddr
= new_value
;
456 static int set_config_net_vlan_id(const char *key
, const char *value
,
457 struct lxc_conf
*lxc_conf
, void *data
)
460 struct lxc_netdev
*netdev
= data
;
462 if (lxc_config_value_empty(value
))
463 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
468 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
475 static int set_config_net_mtu(const char *key
, const char *value
,
476 struct lxc_conf
*lxc_conf
, void *data
)
478 struct lxc_netdev
*netdev
= data
;
480 if (lxc_config_value_empty(value
))
481 return clr_config_net_mtu(key
, lxc_conf
, data
);
486 return set_config_string_item(&netdev
->mtu
, value
);
489 static int set_config_net_ipv4_address(const char *key
, const char *value
,
490 struct lxc_conf
*lxc_conf
, void *data
)
493 struct lxc_netdev
*netdev
= data
;
494 struct lxc_inetdev
*inetdev
;
495 struct lxc_list
*list
;
496 char *cursor
, *slash
;
497 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
499 if (lxc_config_value_empty(value
))
500 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
505 inetdev
= malloc(sizeof(*inetdev
));
509 memset(inetdev
, 0, sizeof(*inetdev
));
511 list
= malloc(sizeof(*list
));
518 list
->elem
= inetdev
;
520 addr
= strdup(value
);
527 cursor
= strstr(addr
, " ");
533 slash
= strstr(addr
, "/");
539 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
540 if (!ret
|| ret
< 0) {
541 SYSERROR("Invalid ipv4 address \"%s\"", value
);
549 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
550 if (!ret
|| ret
< 0) {
551 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
560 /* No prefix specified, determine it from the network class. */
562 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
570 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
573 /* If no broadcast address, let compute one from the
574 * prefix and address.
577 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
578 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
581 lxc_list_add_tail(&netdev
->ipv4
, list
);
587 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
588 struct lxc_conf
*lxc_conf
, void *data
)
590 struct lxc_netdev
*netdev
= data
;
592 if (lxc_config_value_empty(value
))
593 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
598 free(netdev
->ipv4_gateway
);
600 if (!strcmp(value
, "auto")) {
601 netdev
->ipv4_gateway
= NULL
;
602 netdev
->ipv4_gateway_auto
= true;
607 gw
= malloc(sizeof(*gw
));
611 ret
= inet_pton(AF_INET
, value
, gw
);
612 if (!ret
|| ret
< 0) {
613 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
618 netdev
->ipv4_gateway
= gw
;
619 netdev
->ipv4_gateway_auto
= false;
625 static int set_config_net_ipv6_address(const char *key
, const char *value
,
626 struct lxc_conf
*lxc_conf
, void *data
)
629 struct lxc_netdev
*netdev
= data
;
630 struct lxc_inet6dev
*inet6dev
;
631 struct lxc_list
*list
;
632 char *slash
, *valdup
, *netmask
;
634 if (lxc_config_value_empty(value
))
635 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
640 inet6dev
= malloc(sizeof(*inet6dev
));
644 memset(inet6dev
, 0, sizeof(*inet6dev
));
646 list
= malloc(sizeof(*list
));
653 list
->elem
= inet6dev
;
655 valdup
= strdup(value
);
662 inet6dev
->prefix
= 64;
663 slash
= strstr(valdup
, "/");
667 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
676 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
677 if (!ret
|| ret
< 0) {
678 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
685 lxc_list_add_tail(&netdev
->ipv6
, list
);
691 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
692 struct lxc_conf
*lxc_conf
, void *data
)
694 struct lxc_netdev
*netdev
= data
;
696 if (lxc_config_value_empty(value
))
697 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
702 free(netdev
->ipv6_gateway
);
704 if (!strcmp(value
, "auto")) {
705 netdev
->ipv6_gateway
= NULL
;
706 netdev
->ipv6_gateway_auto
= true;
711 gw
= malloc(sizeof(*gw
));
715 ret
= inet_pton(AF_INET6
, value
, gw
);
716 if (!ret
|| ret
< 0) {
717 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
722 netdev
->ipv6_gateway
= gw
;
723 netdev
->ipv6_gateway_auto
= false;
729 static int set_config_net_script_up(const char *key
, const char *value
,
730 struct lxc_conf
*lxc_conf
, void *data
)
732 struct lxc_netdev
*netdev
= data
;
734 if (lxc_config_value_empty(value
))
735 return clr_config_net_script_up(key
, lxc_conf
, data
);
740 return set_config_string_item(&netdev
->upscript
, value
);
743 static int set_config_net_script_down(const char *key
, const char *value
,
744 struct lxc_conf
*lxc_conf
, void *data
)
746 struct lxc_netdev
*netdev
= data
;
748 if (lxc_config_value_empty(value
))
749 return clr_config_net_script_down(key
, lxc_conf
, data
);
754 return set_config_string_item(&netdev
->downscript
, value
);
757 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
759 struct lxc_list
*hooklist
;
761 hooklist
= malloc(sizeof(*hooklist
));
767 hooklist
->elem
= hook
;
768 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
772 static int set_config_seccomp_profile(const char *key
, const char *value
,
773 struct lxc_conf
*lxc_conf
, void *data
)
775 return set_config_path_item(&lxc_conf
->seccomp
, value
);
778 static int set_config_execute_cmd(const char *key
, const char *value
,
779 struct lxc_conf
*lxc_conf
, void *data
)
781 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
784 static int set_config_init_cmd(const char *key
, const char *value
,
785 struct lxc_conf
*lxc_conf
, void *data
)
787 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
790 static int set_config_init_cwd(const char *key
, const char *value
,
791 struct lxc_conf
*lxc_conf
, void *data
)
793 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
796 static int set_config_init_uid(const char *key
, const char *value
,
797 struct lxc_conf
*lxc_conf
, void *data
)
799 unsigned int init_uid
;
801 if (lxc_config_value_empty(value
)) {
802 lxc_conf
->init_uid
= 0;
806 if (lxc_safe_uint(value
, &init_uid
) < 0)
809 lxc_conf
->init_uid
= init_uid
;
814 static int set_config_init_gid(const char *key
, const char *value
,
815 struct lxc_conf
*lxc_conf
, void *data
)
817 unsigned int init_gid
;
819 if (lxc_config_value_empty(value
)) {
820 lxc_conf
->init_gid
= 0;
824 if (lxc_safe_uint(value
, &init_gid
) < 0)
827 lxc_conf
->init_gid
= init_gid
;
832 static int set_config_hooks(const char *key
, const char *value
,
833 struct lxc_conf
*lxc_conf
, void *data
)
837 if (lxc_config_value_empty(value
))
838 return lxc_clear_hooks(lxc_conf
, key
);
840 if (strcmp(key
+ 4, "hook") == 0) {
841 ERROR("lxc.hook must not have a value");
845 copy
= strdup(value
);
849 if (strcmp(key
+ 9, "pre-start") == 0)
850 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
851 else if (strcmp(key
+ 9, "start-host") == 0)
852 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
853 else if (strcmp(key
+ 9, "pre-mount") == 0)
854 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
855 else if (strcmp(key
+ 9, "autodev") == 0)
856 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
857 else if (strcmp(key
+ 9, "mount") == 0)
858 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
859 else if (strcmp(key
+ 9, "start") == 0)
860 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
861 else if (strcmp(key
+ 9, "stop") == 0)
862 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
863 else if (strcmp(key
+ 9, "post-stop") == 0)
864 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
865 else if (strcmp(key
+ 9, "clone") == 0)
866 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
867 else if (strcmp(key
+ 9, "destroy") == 0)
868 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
874 static int set_config_hooks_version(const char *key
, const char *value
,
875 struct lxc_conf
*lxc_conf
, void *data
)
880 if (lxc_config_value_empty(value
))
881 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
883 ret
= lxc_safe_uint(value
, &tmp
);
888 ERROR("Invalid hook version specified. Currently only 0 "
889 "(legacy) and 1 are supported");
893 lxc_conf
->hooks_version
= tmp
;
897 static int set_config_personality(const char *key
, const char *value
,
898 struct lxc_conf
*lxc_conf
, void *data
)
900 signed long personality
= lxc_config_parse_arch(value
);
902 if (personality
>= 0)
903 lxc_conf
->personality
= personality
;
905 WARN("Unsupported personality \"%s\"", value
);
910 static int set_config_pty_max(const char *key
, const char *value
,
911 struct lxc_conf
*lxc_conf
, void *data
)
914 unsigned int max
= 0;
916 if (lxc_config_value_empty(value
)) {
917 lxc_conf
->pty_max
= 0;
921 ret
= lxc_safe_uint(value
, &max
);
925 lxc_conf
->pty_max
= max
;
929 /* We only need to check whether the first byte of the key after the lxc.start.
930 * prefix matches our expectations since they fortunately all start with a
931 * different letter. If anything was wrong with the key we would have already
932 * noticed when the callback was called.
934 static int set_config_start(const char *key
, const char *value
,
935 struct lxc_conf
*lxc_conf
, void *data
)
939 is_empty
= lxc_config_value_empty(value
);
941 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
943 lxc_conf
->start_auto
= 0;
947 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
950 if (lxc_conf
->start_auto
> 1)
954 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
956 lxc_conf
->start_delay
= 0;
960 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
961 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
963 lxc_conf
->start_order
= 0;
967 return lxc_safe_int(value
, &lxc_conf
->start_order
);
973 static int set_config_monitor(const char *key
, const char *value
,
974 struct lxc_conf
*lxc_conf
, void *data
)
976 if (lxc_config_value_empty(value
)) {
977 lxc_conf
->monitor_unshare
= 0;
981 if (strcmp(key
+ 12, "unshare") == 0)
982 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
987 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
988 struct lxc_conf
*lxc_conf
, void *data
)
990 if (lxc_config_value_empty(value
)) {
991 lxc_conf
->monitor_signal_pdeath
= 0;
995 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
998 sig_n
= sig_parse(value
);
1002 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1009 static int set_config_group(const char *key
, const char *value
,
1010 struct lxc_conf
*lxc_conf
, void *data
)
1012 char *groups
, *token
;
1013 struct lxc_list
*grouplist
;
1016 if (lxc_config_value_empty(value
))
1017 return lxc_clear_groups(lxc_conf
);
1019 groups
= strdup(value
);
1023 /* In case several groups are specified in a single line split these
1024 * groups in a single element for the list.
1026 lxc_iterate_parts(token
, groups
, " \t") {
1027 grouplist
= malloc(sizeof(*grouplist
));
1033 grouplist
->elem
= strdup(token
);
1034 if (!grouplist
->elem
) {
1040 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1047 static int set_config_environment(const char *key
, const char *value
,
1048 struct lxc_conf
*lxc_conf
, void *data
)
1050 struct lxc_list
*list_item
= NULL
;
1052 if (lxc_config_value_empty(value
))
1053 return lxc_clear_environment(lxc_conf
);
1055 list_item
= malloc(sizeof(*list_item
));
1059 if (!strchr(value
, '=')) {
1060 const char *env_val
;
1061 const char *env_key
= value
;
1062 const char *env_var
[3] = {0};
1064 env_val
= getenv(env_key
);
1068 env_var
[0] = env_key
;
1069 env_var
[1] = env_val
;
1070 list_item
->elem
= lxc_string_join("=", env_var
, false);
1072 list_item
->elem
= strdup(value
);
1075 if (!list_item
->elem
)
1078 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1087 static int set_config_tty_max(const char *key
, const char *value
,
1088 struct lxc_conf
*lxc_conf
, void *data
)
1091 unsigned int nbtty
= 0;
1093 if (lxc_config_value_empty(value
)) {
1094 lxc_conf
->ttys
.max
= 0;
1098 ret
= lxc_safe_uint(value
, &nbtty
);
1102 lxc_conf
->ttys
.max
= nbtty
;
1106 static int set_config_tty_dir(const char *key
, const char *value
,
1107 struct lxc_conf
*lxc_conf
, void *data
)
1109 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1113 static int set_config_apparmor_profile(const char *key
, const char *value
,
1114 struct lxc_conf
*lxc_conf
, void *data
)
1116 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1119 static int set_config_apparmor_allow_incomplete(const char *key
,
1121 struct lxc_conf
*lxc_conf
,
1124 if (lxc_config_value_empty(value
)) {
1125 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1129 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1132 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1138 static int set_config_apparmor_allow_nesting(const char *key
,
1140 struct lxc_conf
*lxc_conf
,
1143 if (lxc_config_value_empty(value
))
1144 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1146 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1149 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1155 static int set_config_apparmor_raw(const char *key
,
1157 struct lxc_conf
*lxc_conf
,
1161 struct lxc_list
*list
;
1163 if (lxc_config_value_empty(value
))
1164 return lxc_clear_apparmor_raw(lxc_conf
);
1166 list
= malloc(sizeof(*list
));
1172 elem
= strdup(value
);
1179 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1184 static int set_config_selinux_context(const char *key
, const char *value
,
1185 struct lxc_conf
*lxc_conf
, void *data
)
1187 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1190 static int set_config_log_file(const char *key
, const char *value
,
1191 struct lxc_conf
*c
, void *data
)
1195 if (lxc_config_value_empty(value
)) {
1201 /* Store these values in the lxc_conf, and then try to set for actual
1204 ret
= set_config_path_item(&c
->logfile
, value
);
1206 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1211 static int set_config_log_level(const char *key
, const char *value
,
1212 struct lxc_conf
*lxc_conf
, void *data
)
1216 if (lxc_config_value_empty(value
)) {
1217 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1221 if (value
[0] >= '0' && value
[0] <= '9') {
1222 if (lxc_safe_int(value
, &newlevel
) < 0)
1225 newlevel
= lxc_log_priority_to_int(value
);
1228 /* Store these values in the lxc_conf, and then try to set for actual
1231 lxc_conf
->loglevel
= newlevel
;
1232 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1235 static int set_config_autodev(const char *key
, const char *value
,
1236 struct lxc_conf
*lxc_conf
, void *data
)
1238 if (lxc_config_value_empty(value
)) {
1239 lxc_conf
->autodev
= 0;
1243 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1246 if (lxc_conf
->autodev
> 1)
1252 static int set_config_signal_halt(const char *key
, const char *value
,
1253 struct lxc_conf
*lxc_conf
, void *data
)
1257 if (lxc_config_value_empty(value
)) {
1258 lxc_conf
->haltsignal
= 0;
1262 sig_n
= sig_parse(value
);
1266 lxc_conf
->haltsignal
= sig_n
;
1271 static int set_config_signal_reboot(const char *key
, const char *value
,
1272 struct lxc_conf
*lxc_conf
, void *data
)
1276 if (lxc_config_value_empty(value
)) {
1277 lxc_conf
->rebootsignal
= 0;
1281 sig_n
= sig_parse(value
);
1285 lxc_conf
->rebootsignal
= sig_n
;
1290 static int set_config_signal_stop(const char *key
, const char *value
,
1291 struct lxc_conf
*lxc_conf
, void *data
)
1295 if (lxc_config_value_empty(value
)) {
1296 lxc_conf
->stopsignal
= 0;
1300 sig_n
= sig_parse(value
);
1304 lxc_conf
->stopsignal
= sig_n
;
1309 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1310 struct lxc_conf
*lxc_conf
, int version
)
1312 const char *subkey
, *token
;
1314 struct lxc_list
*cglist
= NULL
;
1315 struct lxc_cgroup
*cgelem
= NULL
;
1317 if (lxc_config_value_empty(value
))
1318 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1320 if (version
== CGROUP2_SUPER_MAGIC
) {
1321 token
= "lxc.cgroup2.";
1323 } else if (version
== CGROUP_SUPER_MAGIC
) {
1324 token
= "lxc.cgroup.";
1330 if (strncmp(key
, token
, token_len
) != 0)
1333 subkey
= key
+ token_len
;
1334 if (*subkey
== '\0')
1337 cglist
= malloc(sizeof(*cglist
));
1341 cgelem
= malloc(sizeof(*cgelem
));
1344 memset(cgelem
, 0, sizeof(*cgelem
));
1346 cgelem
->subsystem
= strdup(subkey
);
1347 if (!cgelem
->subsystem
)
1350 cgelem
->value
= strdup(value
);
1354 cgelem
->version
= version
;
1356 lxc_list_add_elem(cglist
, cgelem
);
1358 if (version
== CGROUP2_SUPER_MAGIC
)
1359 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1361 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1368 free(cgelem
->subsystem
);
1369 free(cgelem
->value
);
1376 static int set_config_cgroup_controller(const char *key
, const char *value
,
1377 struct lxc_conf
*lxc_conf
, void *data
)
1379 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1380 CGROUP_SUPER_MAGIC
);
1383 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1384 struct lxc_conf
*lxc_conf
, void *data
)
1386 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1387 CGROUP2_SUPER_MAGIC
);
1391 static int set_config_cgroup_dir(const char *key
, const char *value
,
1392 struct lxc_conf
*lxc_conf
, void *data
)
1394 if (lxc_config_value_empty(value
))
1395 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1397 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1400 static int set_config_cgroup_relative(const char *key
, const char *value
,
1401 struct lxc_conf
*lxc_conf
, void *data
)
1403 unsigned int converted
;
1406 if (lxc_config_value_empty(value
))
1407 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1409 ret
= lxc_safe_uint(value
, &converted
);
1413 if (converted
== 1) {
1414 lxc_conf
->cgroup_meta
.relative
= true;
1418 if (converted
== 0) {
1419 lxc_conf
->cgroup_meta
.relative
= false;
1426 static int set_config_prlimit(const char *key
, const char *value
,
1427 struct lxc_conf
*lxc_conf
, void *data
)
1429 struct lxc_list
*iter
;
1430 struct rlimit limit
;
1432 struct lxc_list
*limlist
= NULL
;
1433 struct lxc_limit
*limelem
= NULL
;
1435 if (lxc_config_value_empty(value
))
1436 return lxc_clear_limits(lxc_conf
, key
);
1438 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1441 key
+= STRLITERALLEN("lxc.prlimit.");
1443 /* soft limit comes first in the value */
1444 if (!parse_limit_value(&value
, &limit_value
))
1446 limit
.rlim_cur
= limit_value
;
1448 /* skip spaces and a colon */
1449 while (isspace(*value
))
1454 else if (*value
) /* any other character is an error here */
1457 while (isspace(*value
))
1460 /* optional hard limit */
1462 if (!parse_limit_value(&value
, &limit_value
))
1464 limit
.rlim_max
= limit_value
;
1466 /* check for trailing garbage */
1467 while (isspace(*value
))
1473 /* a single value sets both hard and soft limit */
1474 limit
.rlim_max
= limit
.rlim_cur
;
1477 /* find existing list element */
1478 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1479 limelem
= iter
->elem
;
1480 if (!strcmp(key
, limelem
->resource
)) {
1481 limelem
->limit
= limit
;
1486 /* allocate list element */
1487 limlist
= malloc(sizeof(*limlist
));
1491 limelem
= malloc(sizeof(*limelem
));
1494 memset(limelem
, 0, sizeof(*limelem
));
1496 limelem
->resource
= strdup(key
);
1497 if (!limelem
->resource
)
1499 limelem
->limit
= limit
;
1501 lxc_list_add_elem(limlist
, limelem
);;
1503 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1510 free(limelem
->resource
);
1516 static int set_config_sysctl(const char *key
, const char *value
,
1517 struct lxc_conf
*lxc_conf
, void *data
)
1519 struct lxc_list
*iter
;
1520 char *replace_value
= NULL
;
1521 struct lxc_list
*sysctl_list
= NULL
;
1522 struct lxc_sysctl
*sysctl_elem
= NULL
;
1524 if (lxc_config_value_empty(value
))
1525 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1527 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1530 key
+= STRLITERALLEN("lxc.sysctl.");
1532 /* find existing list element */
1533 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1534 sysctl_elem
= iter
->elem
;
1536 if (strcmp(key
, sysctl_elem
->key
) != 0)
1539 replace_value
= strdup(value
);
1543 free(sysctl_elem
->value
);
1544 sysctl_elem
->value
= replace_value
;
1548 /* allocate list element */
1549 sysctl_list
= malloc(sizeof(*sysctl_list
));
1553 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1556 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1558 sysctl_elem
->key
= strdup(key
);
1559 if (!sysctl_elem
->key
)
1562 sysctl_elem
->value
= strdup(value
);
1563 if (!sysctl_elem
->value
)
1566 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1568 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1575 free(sysctl_elem
->key
);
1576 free(sysctl_elem
->value
);
1582 static int set_config_proc(const char *key
, const char *value
,
1583 struct lxc_conf
*lxc_conf
, void *data
)
1586 struct lxc_list
*proclist
= NULL
;
1587 struct lxc_proc
*procelem
= NULL
;
1589 if (lxc_config_value_empty(value
))
1590 return clr_config_proc(key
, lxc_conf
, NULL
);
1592 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
1595 subkey
= key
+ STRLITERALLEN("lxc.proc.");
1596 if (*subkey
== '\0')
1599 proclist
= malloc(sizeof(*proclist
));
1603 procelem
= malloc(sizeof(*procelem
));
1606 memset(procelem
, 0, sizeof(*procelem
));
1608 procelem
->filename
= strdup(subkey
);
1609 procelem
->value
= strdup(value
);
1611 if (!procelem
->filename
|| !procelem
->value
)
1614 proclist
->elem
= procelem
;
1616 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1623 free(procelem
->filename
);
1624 free(procelem
->value
);
1631 static int set_config_idmaps(const char *key
, const char *value
,
1632 struct lxc_conf
*lxc_conf
, void *data
)
1634 unsigned long hostid
, nsid
, range
;
1637 struct lxc_list
*idmaplist
= NULL
;
1638 struct id_map
*idmap
= NULL
;
1640 if (lxc_config_value_empty(value
))
1641 return lxc_clear_idmaps(lxc_conf
);
1643 idmaplist
= malloc(sizeof(*idmaplist
));
1647 idmap
= malloc(sizeof(*idmap
));
1650 memset(idmap
, 0, sizeof(*idmap
));
1652 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1654 ERROR("Failed to parse id mappings");
1658 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1660 idmap
->idtype
= ID_TYPE_UID
;
1661 else if (type
== 'g')
1662 idmap
->idtype
= ID_TYPE_GID
;
1666 idmap
->hostid
= hostid
;
1668 idmap
->range
= range
;
1669 idmaplist
->elem
= idmap
;
1670 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1672 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1673 if (idmap
->nsid
== 0)
1674 lxc_conf
->root_nsuid_map
= idmap
;
1677 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1678 if (idmap
->nsid
== 0)
1679 lxc_conf
->root_nsgid_map
= idmap
;
1692 static int set_config_mount_fstab(const char *key
, const char *value
,
1693 struct lxc_conf
*lxc_conf
, void *data
)
1695 if (lxc_config_value_empty(value
)) {
1696 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1700 return set_config_path_item(&lxc_conf
->fstab
, value
);
1703 static int set_config_mount_auto(const char *key
, const char *value
,
1704 struct lxc_conf
*lxc_conf
, void *data
)
1706 char *autos
, *token
;
1713 } allowed_auto_mounts
[] = {
1714 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1715 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1716 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1717 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1718 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1719 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1720 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1721 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1722 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1723 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1724 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1725 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1726 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1727 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1728 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1729 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1730 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1731 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1732 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1733 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1734 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1735 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
1736 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
1737 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
1738 /* For adding anything that is just a single on/off, but has no
1739 * options: keep mask and flag identical and just define the enum
1740 * value as an unused bit so far
1745 if (lxc_config_value_empty(value
)) {
1746 lxc_conf
->auto_mounts
= 0;
1750 autos
= strdup(value
);
1754 lxc_iterate_parts(token
, autos
, " \t") {
1755 bool is_shmounts
= false;
1757 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1758 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1761 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
1762 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
1768 if (!allowed_auto_mounts
[i
].token
) {
1769 ERROR("Invalid filesystem to automount \"%s\"", token
);
1773 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1774 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1776 lxc_conf
->shmount
.path_host
= strdup(token
+ STRLITERALLEN("shmounts:"));
1777 if (!lxc_conf
->shmount
.path_host
) {
1778 SYSERROR("Failed to copy shmounts host path");
1782 if (strcmp(lxc_conf
->shmount
.path_host
, "") == 0) {
1783 ERROR("Invalid shmounts path: empty");
1787 lxc_conf
->shmount
.path_cont
= strdup("/dev/.lxc-mounts");
1788 if(!lxc_conf
->shmount
.path_cont
) {
1789 SYSERROR("Failed to copy shmounts container path");
1803 static int set_config_mount(const char *key
, const char *value
,
1804 struct lxc_conf
*lxc_conf
, void *data
)
1807 struct lxc_list
*mntlist
;
1809 if (lxc_config_value_empty(value
))
1810 return lxc_clear_mount_entries(lxc_conf
);
1812 mntlist
= malloc(sizeof(*mntlist
));
1816 mntelem
= strdup(value
);
1821 mntlist
->elem
= mntelem
;
1823 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1828 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
1829 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
1832 static int set_config_cap_keep(const char *key
, const char *value
,
1833 struct lxc_conf
*lxc_conf
, void *data
)
1835 char *keepcaps
, *token
;
1836 struct lxc_list
*keeplist
;
1839 if (lxc_config_value_empty(value
))
1840 return lxc_clear_config_keepcaps(lxc_conf
);
1842 keepcaps
= strdup(value
);
1846 /* In case several capability keep is specified in a single line
1847 * split these caps in a single element for the list.
1849 lxc_iterate_parts(token
, keepcaps
, " \t") {
1850 if (!strcmp(token
, "none"))
1851 lxc_clear_config_keepcaps(lxc_conf
);
1853 keeplist
= malloc(sizeof(*keeplist
));
1857 keeplist
->elem
= strdup(token
);
1858 if (!keeplist
->elem
) {
1863 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1874 static int set_config_cap_drop(const char *key
, const char *value
,
1875 struct lxc_conf
*lxc_conf
, void *data
)
1877 char *dropcaps
, *token
;
1878 struct lxc_list
*droplist
;
1881 if (lxc_config_value_empty(value
))
1882 return lxc_clear_config_caps(lxc_conf
);
1884 dropcaps
= strdup(value
);
1888 /* In case several capability drop is specified in a single line
1889 * split these caps in a single element for the list.
1891 lxc_iterate_parts(token
, dropcaps
, " \t") {
1892 droplist
= malloc(sizeof(*droplist
));
1896 droplist
->elem
= strdup(token
);
1897 if (!droplist
->elem
) {
1902 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1913 static int set_config_console_path(const char *key
, const char *value
,
1914 struct lxc_conf
*lxc_conf
, void *data
)
1916 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1919 static int set_config_console_rotate(const char *key
, const char *value
,
1920 struct lxc_conf
*lxc_conf
, void *data
)
1922 if (lxc_config_value_empty(value
)) {
1923 lxc_conf
->console
.log_rotate
= 0;
1927 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1930 if (lxc_conf
->console
.log_rotate
> 1) {
1931 ERROR("The \"lxc.console.rotate\" config key can only be set "
1939 static int set_config_console_logfile(const char *key
, const char *value
,
1940 struct lxc_conf
*lxc_conf
, void *data
)
1942 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1945 static int set_config_console_buffer_size(const char *key
, const char *value
,
1946 struct lxc_conf
*lxc_conf
, void *data
)
1950 uint64_t buffer_size
, pgsz
;
1952 if (lxc_config_value_empty(value
)) {
1953 lxc_conf
->console
.buffer_size
= 0;
1957 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1958 if (!strcmp(value
, "auto")) {
1959 lxc_conf
->console
.buffer_size
= 1 << 17;
1963 ret
= parse_byte_size_string(value
, &size
);
1970 /* must be at least a page size */
1971 pgsz
= lxc_getpagesize();
1972 if ((uint64_t)size
< pgsz
) {
1973 NOTICE("Requested ringbuffer size for the console is %" PRId64
1974 " but must be at least %" PRId64
1975 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1980 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1981 if (buffer_size
== 0)
1984 if (buffer_size
!= size
)
1985 NOTICE("Passed size was not a power of 2. Rounding log size to "
1986 "next power of two: %" PRIu64
" bytes", buffer_size
);
1988 lxc_conf
->console
.buffer_size
= buffer_size
;
1992 static int set_config_console_size(const char *key
, const char *value
,
1993 struct lxc_conf
*lxc_conf
, void *data
)
1997 uint64_t log_size
, pgsz
;
1999 if (lxc_config_value_empty(value
)) {
2000 lxc_conf
->console
.log_size
= 0;
2004 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2005 if (!strcmp(value
, "auto")) {
2006 lxc_conf
->console
.log_size
= 1 << 17;
2010 ret
= parse_byte_size_string(value
, &size
);
2017 /* must be at least a page size */
2018 pgsz
= lxc_getpagesize();
2019 if ((uint64_t)size
< pgsz
) {
2020 NOTICE("Requested ringbuffer size for the console is %" PRId64
2021 " but must be at least %" PRId64
2022 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2027 log_size
= lxc_find_next_power2((uint64_t)size
);
2031 if (log_size
!= size
)
2032 NOTICE("Passed size was not a power of 2. Rounding log size to "
2033 "next power of two: %" PRIu64
" bytes", log_size
);
2035 lxc_conf
->console
.log_size
= log_size
;
2039 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2041 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
2043 update_hwaddr(line
);
2045 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2046 char *tmp
= realloc(conf
->unexpanded_config
,
2047 conf
->unexpanded_alloced
+ 1024);
2051 if (!conf
->unexpanded_config
)
2053 conf
->unexpanded_config
= tmp
;
2054 conf
->unexpanded_alloced
+= 1024;
2057 (void)strlcat(conf
->unexpanded_config
, line
, conf
->unexpanded_alloced
);
2058 conf
->unexpanded_len
+= linelen
;
2059 if (line
[linelen
- 1] != '\n') {
2060 (void)strlcat(conf
->unexpanded_config
, "\n", conf
->unexpanded_alloced
);
2061 conf
->unexpanded_len
++;
2067 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2069 struct dirent
*direntp
;
2071 char path
[PATH_MAX
];
2075 dir
= opendir(dirp
);
2079 while ((direntp
= readdir(dir
))) {
2082 fnam
= direntp
->d_name
;
2083 if (!strcmp(fnam
, "."))
2086 if (!strcmp(fnam
, ".."))
2090 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2093 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2094 if (len
< 0 || len
>= PATH_MAX
) {
2099 ret
= lxc_config_read(path
, lxc_conf
, true);
2111 static int set_config_includefiles(const char *key
, const char *value
,
2112 struct lxc_conf
*lxc_conf
, void *data
)
2114 if (lxc_config_value_empty(value
)) {
2115 clr_config_includefiles(key
, lxc_conf
, NULL
);
2120 return do_includedir(value
, lxc_conf
);
2122 return lxc_config_read(value
, lxc_conf
, true);
2125 static int set_config_rootfs_path(const char *key
, const char *value
,
2126 struct lxc_conf
*lxc_conf
, void *data
)
2130 const char *container_path
;
2132 if (lxc_config_value_empty(value
)) {
2133 free(lxc_conf
->rootfs
.path
);
2134 lxc_conf
->rootfs
.path
= NULL
;
2138 dup
= strdup(value
);
2142 /* Split <storage type>:<container path> into <storage type> and
2143 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2144 * "rootfs.path" to <container path>.
2146 tmp
= strchr(dup
, ':');
2149 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2155 container_path
= tmp
;
2157 container_path
= value
;
2160 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2165 static int set_config_rootfs_managed(const char *key
, const char *value
,
2166 struct lxc_conf
*lxc_conf
, void *data
)
2168 unsigned int val
= 0;
2170 if (lxc_config_value_empty(value
)) {
2171 lxc_conf
->rootfs
.managed
= true;
2175 if (lxc_safe_uint(value
, &val
) < 0)
2180 lxc_conf
->rootfs
.managed
= false;
2183 lxc_conf
->rootfs
.managed
= true;
2190 static int set_config_rootfs_mount(const char *key
, const char *value
,
2191 struct lxc_conf
*lxc_conf
, void *data
)
2193 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2196 static int set_config_rootfs_options(const char *key
, const char *value
,
2197 struct lxc_conf
*lxc_conf
, void *data
)
2200 unsigned long mflags
= 0, pflags
= 0;
2201 char *mdata
= NULL
, *opts
= NULL
;
2202 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2204 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2208 ret
= parse_propagationopts(value
, &pflags
);
2214 ret
= set_config_string_item(&opts
, value
);
2220 rootfs
->mountflags
= mflags
| pflags
;
2221 rootfs
->options
= opts
;
2222 rootfs
->data
= mdata
;
2227 static int set_config_uts_name(const char *key
, const char *value
,
2228 struct lxc_conf
*lxc_conf
, void *data
)
2230 struct utsname
*utsname
;
2232 if (lxc_config_value_empty(value
)) {
2233 clr_config_uts_name(key
, lxc_conf
, NULL
);
2237 utsname
= malloc(sizeof(*utsname
));
2241 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2246 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2247 free(lxc_conf
->utsname
);
2248 lxc_conf
->utsname
= utsname
;
2253 static int set_config_namespace_clone(const char *key
, const char *value
,
2254 struct lxc_conf
*lxc_conf
, void *data
)
2259 if (lxc_config_value_empty(value
))
2260 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2262 if (lxc_conf
->ns_keep
!= 0) {
2264 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2265 "\"lxc.namespace.keep\"");
2273 lxc_iterate_parts(token
, ns
, " \t") {
2274 token
+= lxc_char_left_gc(token
, strlen(token
));
2275 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2276 cloneflag
= lxc_namespace_2_cloneflag(token
);
2277 if (cloneflag
< 0) {
2281 lxc_conf
->ns_clone
|= cloneflag
;
2288 static int set_config_namespace_keep(const char *key
, const char *value
,
2289 struct lxc_conf
*lxc_conf
, void *data
)
2294 if (lxc_config_value_empty(value
))
2295 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2297 if (lxc_conf
->ns_clone
!= 0) {
2299 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2300 "\"lxc.namespace.keep\"");
2308 lxc_iterate_parts(token
, ns
, " \t") {
2309 token
+= lxc_char_left_gc(token
, strlen(token
));
2310 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2311 cloneflag
= lxc_namespace_2_cloneflag(token
);
2312 if (cloneflag
< 0) {
2316 lxc_conf
->ns_keep
|= cloneflag
;
2323 static int set_config_namespace_share(const char *key
, const char *value
,
2324 struct lxc_conf
*lxc_conf
, void *data
)
2327 const char *namespace;
2329 if (lxc_config_value_empty(value
))
2330 return clr_config_namespace_share(key
, lxc_conf
, data
);
2332 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2333 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2337 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2340 struct parse_line_conf
{
2341 struct lxc_conf
*conf
;
2345 static int parse_line(char *buffer
, void *data
)
2347 char *dot
, *key
, *line
, *linep
, *value
;
2349 struct lxc_config_t
*config
;
2352 struct parse_line_conf
*plc
= data
;
2354 /* If there are newlines in the config file we should keep them. */
2355 empty_line
= lxc_is_line_empty(dup
);
2359 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2360 * modified the original string on the stack by replacing '=' by '\0'
2363 linep
= line
= strdup(dup
);
2367 if (!plc
->from_include
) {
2368 ret
= append_unexp_config_line(line
, plc
->conf
);
2376 line
+= lxc_char_left_gc(line
, strlen(line
));
2378 /* ignore comments */
2382 /* martian option - don't add it to the config itself */
2383 if (strncmp(line
, "lxc.", 4))
2388 dot
= strchr(line
, '=');
2390 ERROR("Invalid configuration line: %s", line
);
2398 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2400 value
+= lxc_char_left_gc(value
, strlen(value
));
2401 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2403 if (*value
== '\'' || *value
== '\"') {
2406 len
= strlen(value
);
2407 if (len
> 1 && value
[len
- 1] == *value
) {
2408 value
[len
- 1] = '\0';
2413 config
= lxc_get_config(key
);
2415 ERROR("Unknown configuration key \"%s\"", key
);
2419 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2426 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2428 char *dot
, *key
, *line
, *linep
, *value
;
2431 struct new_config_item
*new = NULL
;
2433 linep
= line
= strdup(dup
);
2437 line
+= lxc_char_left_gc(line
, strlen(line
));
2439 /* martian option - don't add it to the config itself */
2440 if (strncmp(line
, "lxc.", 4))
2444 dot
= strchr(line
, '=');
2446 ERROR("Invalid configuration item: %s", line
);
2454 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2456 value
+= lxc_char_left_gc(value
, strlen(value
));
2457 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2459 if (*value
== '\'' || *value
== '\"') {
2462 len
= strlen(value
);
2463 if (len
> 1 && value
[len
- 1] == *value
) {
2464 value
[len
- 1] = '\0';
2470 new = malloc(sizeof(struct new_config_item
));
2474 new->key
= strdup(key
);
2475 new->val
= strdup(value
);
2476 if (!new->val
|| !new->key
)
2484 if (ret
< 0 && new) {
2494 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2497 struct parse_line_conf c
;
2500 c
.from_include
= from_include
;
2502 ret
= access(file
, R_OK
);
2506 /* Catch only the top level config file name in the structure. */
2508 conf
->rcfile
= strdup(file
);
2510 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2513 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2515 struct lxc_list
*dent
;
2517 dent
= malloc(sizeof(struct lxc_list
));
2521 dent
->elem
= parse_new_conf_line(arg
);
2527 lxc_list_add_tail(defines
, dent
);
2531 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2533 struct lxc_list
*it
;
2536 lxc_list_for_each(it
, defines
) {
2537 struct new_config_item
*new_item
= it
->elem
;
2538 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2543 lxc_config_define_free(defines
);
2547 void lxc_config_define_free(struct lxc_list
*defines
)
2549 struct lxc_list
*it
, *next
;
2551 lxc_list_for_each_safe(it
, defines
, next
) {
2552 struct new_config_item
*new_item
= it
->elem
;
2553 free(new_item
->key
);
2554 free(new_item
->val
);
2560 signed long lxc_config_parse_arch(const char *arch
)
2562 #if HAVE_SYS_PERSONALITY_H
2568 { "arm", PER_LINUX32
},
2569 { "armel", PER_LINUX32
},
2570 { "armhf", PER_LINUX32
},
2571 { "armv7l", PER_LINUX32
},
2572 { "athlon", PER_LINUX32
},
2573 { "i386", PER_LINUX32
},
2574 { "i486", PER_LINUX32
},
2575 { "i586", PER_LINUX32
},
2576 { "i686", PER_LINUX32
},
2577 { "linux32", PER_LINUX32
},
2578 { "mips", PER_LINUX32
},
2579 { "mipsel", PER_LINUX32
},
2580 { "ppc", PER_LINUX32
},
2581 { "powerpc", PER_LINUX32
},
2582 { "x86", PER_LINUX32
},
2583 { "amd64", PER_LINUX
},
2584 { "arm64", PER_LINUX
},
2585 { "linux64", PER_LINUX
},
2586 { "mips64", PER_LINUX
},
2587 { "mips64el", PER_LINUX
},
2588 { "ppc64", PER_LINUX
},
2589 { "ppc64el", PER_LINUX
},
2590 { "ppc64le", PER_LINUX
},
2591 { "powerpc64", PER_LINUX
},
2592 { "s390x", PER_LINUX
},
2593 { "x86_64", PER_LINUX
},
2595 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2597 for (i
= 0; i
< len
; i
++)
2598 if (!strcmp(pername
[i
].name
, arch
))
2599 return pername
[i
].per
;
2605 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2613 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2614 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2615 { "LSM", LXC_ATTACH_LSM_EXEC
},
2620 /* For the sake of backward compatibility, drop all privileges
2621 * if none is specified.
2623 for (i
= 0; all_privs
[i
].token
; i
++)
2624 *flags
|= all_privs
[i
].flag
;
2629 lxc_iterate_parts(token
, flaglist
, "|") {
2632 for (i
= 0; all_privs
[i
].token
; i
++)
2633 if (!strcmp(all_privs
[i
].token
, token
))
2634 aflag
= all_privs
[i
].flag
;
2645 /* Write out a configuration file. */
2646 int write_config(int fd
, const struct lxc_conf
*conf
)
2649 size_t len
= conf
->unexpanded_len
;
2654 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
2656 SYSERROR("Failed to write configuration file");
2663 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2670 len
= strlen(key
) + strlen(v
) + 4;
2673 if (lxc_config_value_empty(v
))
2674 ret
= snprintf(tmp
, len
, "%s =", key
);
2676 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2677 if (ret
< 0 || ret
>= len
)
2680 /* Save the line verbatim into unexpanded_conf */
2681 if (append_unexp_config_line(tmp
, conf
))
2687 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2691 char *lstart
= conf
->unexpanded_config
;
2693 if (!conf
->unexpanded_config
)
2697 lend
= strchr(lstart
, '\n');
2700 lend
= lstart
+ strlen(lstart
);
2703 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2708 v
= lstart
[strlen(key
)];
2709 if (!isspace(v
) && v
!= '=') {
2714 conf
->unexpanded_len
-= (lend
- lstart
);
2715 if (*lend
== '\0') {
2719 memmove(lstart
, lend
, strlen(lend
) + 1);
2723 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2724 const char *newpath
, const char *oldname
,
2725 const char *newname
, const char *ovldir
)
2728 char *lend
, *newdir
, *olddir
, *p
, *q
;
2729 size_t newdirlen
, olddirlen
;
2730 char *lstart
= conf
->unexpanded_config
;
2731 const char *key
= "lxc.mount.entry";
2733 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2734 olddir
= alloca(olddirlen
+ 1);
2735 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2737 if (ret
< 0 || ret
>= olddirlen
+ 1)
2740 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2741 newdir
= alloca(newdirlen
+ 1);
2742 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2744 if (ret
< 0 || ret
>= newdirlen
+ 1)
2747 if (!conf
->unexpanded_config
)
2751 lend
= strchr(lstart
, '\n');
2753 lend
= lstart
+ strlen(lstart
);
2757 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2760 p
= strchr(lstart
+ strlen(key
), '=');
2771 /* Whenever a lxc.mount.entry entry is found in a line we check
2772 * if the substring "overlay" is present before doing any
2773 * further work. We check for "overlay" because substrings need
2774 * to have at least one space before them in a valid overlay
2775 * lxc.mount.entry (/A B overlay). When the space before is
2776 * missing it is very likely that these substrings are part of a
2777 * path or something else. (Checking q >= lend ensures that we
2778 * only count matches in the current line.) */
2779 q
= strstr(p
, " overlay");
2780 if (!q
|| q
>= lend
)
2783 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2786 /* replace the olddir with newdir */
2787 if (olddirlen
>= newdirlen
) {
2788 size_t diff
= olddirlen
- newdirlen
;
2789 memcpy(q
, newdir
, newdirlen
);
2790 if (olddirlen
!= newdirlen
) {
2791 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2792 strlen(q
) - newdirlen
- diff
+ 1);
2794 conf
->unexpanded_len
-= diff
;
2798 size_t diff
= newdirlen
- olddirlen
;
2799 size_t oldlen
= conf
->unexpanded_len
;
2800 size_t newlen
= oldlen
+ diff
;
2801 size_t poffset
= q
- conf
->unexpanded_config
;
2803 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2807 conf
->unexpanded_len
= newlen
;
2808 conf
->unexpanded_alloced
= newlen
+ 1;
2809 new[newlen
- 1] = '\0';
2810 lend
= new + (lend
- conf
->unexpanded_config
);
2811 /* Move over the remainder to make room for the newdir.
2813 memmove(new + poffset
+ newdirlen
,
2814 new + poffset
+ olddirlen
,
2815 oldlen
- poffset
- olddirlen
+ 1);
2816 conf
->unexpanded_config
= new;
2817 memcpy(new + poffset
, newdir
, newdirlen
);
2827 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2828 const char *newpath
, const char *oldname
,
2829 const char *newname
)
2832 char *lend
, *newdir
, *olddir
, *p
;
2833 char *lstart
= conf
->unexpanded_config
;
2834 size_t newdirlen
, olddirlen
;
2835 const char *key
= "lxc.hook";
2837 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2838 olddir
= alloca(olddirlen
+ 1);
2839 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2840 if (ret
< 0 || ret
>= olddirlen
+ 1)
2843 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2844 newdir
= alloca(newdirlen
+ 1);
2845 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2846 if (ret
< 0 || ret
>= newdirlen
+ 1)
2849 if (!conf
->unexpanded_config
)
2853 lend
= strchr(lstart
, '\n');
2855 lend
= lstart
+ strlen(lstart
);
2859 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2862 p
= strchr(lstart
+ strlen(key
), '=');
2873 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2876 /* replace the olddir with newdir */
2877 if (olddirlen
>= newdirlen
) {
2878 size_t diff
= olddirlen
- newdirlen
;
2879 memcpy(p
, newdir
, newdirlen
);
2880 if (olddirlen
!= newdirlen
) {
2881 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2882 strlen(p
) - newdirlen
- diff
+ 1);
2884 conf
->unexpanded_len
-= diff
;
2888 size_t diff
= newdirlen
- olddirlen
;
2889 size_t oldlen
= conf
->unexpanded_len
;
2890 size_t newlen
= oldlen
+ diff
;
2891 size_t poffset
= p
- conf
->unexpanded_config
;
2893 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2897 conf
->unexpanded_len
= newlen
;
2898 conf
->unexpanded_alloced
= newlen
+ 1;
2899 new[newlen
- 1] = '\0';
2900 lend
= new + (lend
- conf
->unexpanded_config
);
2901 /* Move over the remainder to make room for the newdir.
2903 memmove(new + poffset
+ newdirlen
,
2904 new + poffset
+ olddirlen
,
2905 oldlen
- poffset
- olddirlen
+ 1);
2906 conf
->unexpanded_config
= new;
2907 memcpy(new + poffset
, newdir
, newdirlen
);
2920 ERROR("Error writing to new config"); \
2925 /* This is called only from clone. We wish to update all hwaddrs in the
2926 * unexpanded config file. We can't/don't want to update any which come from
2927 * lxc.includes (there shouldn't be any).
2928 * We can't just walk the c->lxc-conf->network list because that includes netifs
2929 * from the include files. So we update the ones which we find in the unexp
2930 * config file, then find the original macaddr in the conf->network, and update
2931 * that to the same value.
2933 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2935 char *lend
, *p
, *p2
;
2936 struct lxc_list
*it
;
2937 char *lstart
= conf
->unexpanded_config
;
2939 if (!conf
->unexpanded_config
)
2943 char newhwaddr
[18], oldhwaddr
[17];
2945 lend
= strchr(lstart
, '\n');
2947 lend
= lstart
+ strlen(lstart
);
2951 if (!lxc_config_net_hwaddr(lstart
)) {
2956 p
= strchr(lstart
, '=');
2969 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2972 if ((p2
- p
) != 17) {
2973 WARN("Bad hwaddr entry");
2978 memcpy(oldhwaddr
, p
, 17);
2980 if (!new_hwaddr(newhwaddr
))
2983 memcpy(p
, newhwaddr
, 17);
2984 lxc_list_for_each(it
, &conf
->network
) {
2985 struct lxc_netdev
*n
= it
->elem
;
2987 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2988 memcpy(n
->hwaddr
, newhwaddr
, 17);
2997 static int set_config_ephemeral(const char *key
, const char *value
,
2998 struct lxc_conf
*lxc_conf
, void *data
)
3000 if (lxc_config_value_empty(value
)) {
3001 lxc_conf
->ephemeral
= 0;
3005 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3008 if (lxc_conf
->ephemeral
> 1)
3014 static int set_config_log_syslog(const char *key
, const char *value
,
3015 struct lxc_conf
*lxc_conf
, void *data
)
3019 if (lxc_conf
->syslog
) {
3020 free(lxc_conf
->syslog
);
3021 lxc_conf
->syslog
= NULL
;
3024 if (lxc_config_value_empty(value
))
3027 facility
= lxc_syslog_priority_to_int(value
);
3028 if (facility
== -EINVAL
)
3031 lxc_log_syslog(facility
);
3032 return set_config_string_item(&lxc_conf
->syslog
, value
);
3035 static int set_config_no_new_privs(const char *key
, const char *value
,
3036 struct lxc_conf
*lxc_conf
, void *data
)
3040 if (lxc_config_value_empty(value
)) {
3041 lxc_conf
->no_new_privs
= false;
3045 if (lxc_safe_uint(value
, &v
) < 0)
3051 lxc_conf
->no_new_privs
= v
? true : false;
3056 /* Callbacks to get configuration items. */
3057 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3058 struct lxc_conf
*c
, void *data
)
3065 memset(retv
, 0, inlen
);
3067 #if HAVE_SYS_PERSONALITY_H
3070 switch (c
->personality
) {
3072 strprint(retv
, inlen
, "i686");
3075 strprint(retv
, inlen
, "x86_64");
3085 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3086 struct lxc_conf
*c
, void *data
)
3088 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3091 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3092 struct lxc_conf
*c
, void *data
)
3094 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3097 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3098 struct lxc_conf
*c
, void *data
)
3100 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3103 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3104 struct lxc_conf
*c
, void *data
)
3106 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3109 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3110 int inlen
, struct lxc_conf
*c
,
3113 return lxc_get_conf_int(c
, retv
, inlen
,
3114 c
->lsm_aa_allow_incomplete
);
3117 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3118 int inlen
, struct lxc_conf
*c
,
3121 return lxc_get_conf_int(c
, retv
, inlen
,
3122 c
->lsm_aa_allow_nesting
);
3125 static int get_config_apparmor_raw(const char *key
, char *retv
,
3126 int inlen
, struct lxc_conf
*c
,
3130 struct lxc_list
*it
;
3136 memset(retv
, 0, inlen
);
3138 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3139 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3145 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3146 struct lxc_conf
*c
, void *data
)
3148 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3151 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3152 * just the value(s) will be printed. Since there still could be more than one,
3153 * it is newline-separated.
3154 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
3156 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3157 * 'lxc.cgroup.subsystem.key = value' format.
3159 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3160 int inlen
, struct lxc_conf
*c
,
3164 size_t namespaced_token_len
;
3165 char *global_token
, *namespaced_token
;
3166 struct lxc_list
*it
;
3168 bool get_all
= false;
3173 memset(retv
, 0, inlen
);
3175 if (version
== CGROUP2_SUPER_MAGIC
) {
3176 global_token
= "lxc.cgroup2";
3177 namespaced_token
= "lxc.cgroup2.";
3178 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3179 } else if (version
== CGROUP_SUPER_MAGIC
) {
3180 global_token
= "lxc.cgroup";
3181 namespaced_token
= "lxc.cgroup.";
3182 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3187 if (strcmp(key
, global_token
) == 0)
3189 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3190 key
+= namespaced_token_len
;
3194 lxc_list_for_each(it
, &c
->cgroup
) {
3195 struct lxc_cgroup
*cg
= it
->elem
;
3198 if (version
!= cg
->version
)
3201 strprint(retv
, inlen
, "%s.%s = %s\n",
3202 global_token
, cg
->subsystem
, cg
->value
);
3203 } else if (!strcmp(cg
->subsystem
, key
)) {
3204 strprint(retv
, inlen
, "%s\n", cg
->value
);
3211 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3212 struct lxc_conf
*c
, void *data
)
3214 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3215 CGROUP_SUPER_MAGIC
);
3218 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3219 struct lxc_conf
*c
, void *data
)
3221 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3222 CGROUP2_SUPER_MAGIC
);
3225 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3226 struct lxc_conf
*lxc_conf
, void *data
)
3234 memset(retv
, 0, inlen
);
3236 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3241 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3242 int inlen
, struct lxc_conf
*lxc_conf
,
3245 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3246 lxc_conf
->cgroup_meta
.relative
);
3249 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3250 struct lxc_conf
*c
, void *data
)
3252 struct lxc_list
*it
;
3253 int len
, listlen
, ret
;
3255 /* "u 1000 1000000 65536"
3257 * let's render this as
3275 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3276 char buf
[__LXC_IDMAP_STR_BUF
];
3281 memset(retv
, 0, inlen
);
3283 listlen
= lxc_list_len(&c
->id_map
);
3284 lxc_list_for_each(it
, &c
->id_map
) {
3285 struct id_map
*map
= it
->elem
;
3286 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3287 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3288 map
->nsid
, map
->hostid
, map
->range
);
3289 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3292 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3297 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3298 struct lxc_conf
*c
, void *data
)
3301 v
= lxc_log_priority_to_string(c
->loglevel
);
3302 return lxc_get_conf_str(retv
, inlen
, v
);
3305 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3306 struct lxc_conf
*c
, void *data
)
3308 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3311 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3312 struct lxc_conf
*c
, void *data
)
3314 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3317 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3318 struct lxc_conf
*c
, void *data
)
3320 int len
, fulllen
= 0;
3321 const char *sep
= "";
3326 memset(retv
, 0, inlen
);
3328 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3331 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3332 case LXC_AUTO_PROC_MIXED
:
3333 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3336 case LXC_AUTO_PROC_RW
:
3337 strprint(retv
, inlen
, "%sproc:rw", sep
);
3344 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3345 case LXC_AUTO_SYS_RO
:
3346 strprint(retv
, inlen
, "%ssys:ro", sep
);
3349 case LXC_AUTO_SYS_RW
:
3350 strprint(retv
, inlen
, "%ssys:rw", sep
);
3353 case LXC_AUTO_SYS_MIXED
:
3354 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3361 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3362 case LXC_AUTO_CGROUP_NOSPEC
:
3363 strprint(retv
, inlen
, "%scgroup", sep
);
3365 case LXC_AUTO_CGROUP_MIXED
:
3366 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3368 case LXC_AUTO_CGROUP_RO
:
3369 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3371 case LXC_AUTO_CGROUP_RW
:
3372 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3374 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3375 strprint(retv
, inlen
, "%scgroup-full", sep
);
3377 case LXC_AUTO_CGROUP_FULL_MIXED
:
3378 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3380 case LXC_AUTO_CGROUP_FULL_RO
:
3381 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3383 case LXC_AUTO_CGROUP_FULL_RW
:
3384 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3393 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3394 struct lxc_conf
*c
, void *data
)
3396 int len
, fulllen
= 0;
3397 struct lxc_list
*it
;
3402 memset(retv
, 0, inlen
);
3404 lxc_list_for_each(it
, &c
->mount_list
) {
3405 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3411 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3412 struct lxc_conf
*c
, void *data
)
3414 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3417 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3418 struct lxc_conf
*c
, void *data
)
3420 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3423 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3424 struct lxc_conf
*c
, void *data
)
3426 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3429 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3430 struct lxc_conf
*c
, void *data
)
3432 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3435 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3436 struct lxc_conf
*c
, void *data
)
3438 return lxc_get_conf_str(
3440 c
->utsname
? c
->utsname
->nodename
: NULL
);
3443 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3444 struct lxc_conf
*c
, void *data
)
3447 int len
, fulllen
= 0, found
= -1;
3448 struct lxc_list
*it
;
3451 subkey
= strchr(key
, '.');
3453 subkey
= strchr(subkey
+ 1, '.');
3459 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3460 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3471 memset(retv
, 0, inlen
);
3473 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3474 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3479 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3480 struct lxc_conf
*c
, void *data
)
3482 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3485 static int get_config_net(const char *key
, char *retv
, int inlen
,
3486 struct lxc_conf
*c
, void *data
)
3488 int len
, fulllen
= 0;
3489 struct lxc_list
*it
;
3494 memset(retv
, 0, inlen
);
3496 lxc_list_for_each(it
, &c
->network
) {
3497 struct lxc_netdev
*n
= it
->elem
;
3498 const char *t
= lxc_net_type_to_str(n
->type
);
3499 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3505 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3506 struct lxc_conf
*c
, void *data
)
3508 int len
, fulllen
= 0;
3509 struct lxc_list
*it
;
3514 memset(retv
, 0, inlen
);
3516 lxc_list_for_each(it
, &c
->caps
) {
3517 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3523 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3524 struct lxc_conf
*c
, void *data
)
3526 int len
, fulllen
= 0;
3527 struct lxc_list
*it
;
3532 memset(retv
, 0, inlen
);
3534 lxc_list_for_each(it
, &c
->keepcaps
) {
3535 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3541 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3542 struct lxc_conf
*c
, void *data
)
3544 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3547 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3548 struct lxc_conf
*c
, void *data
)
3550 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3553 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3554 struct lxc_conf
*c
, void *data
)
3556 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3560 static int get_config_console_buffer_size(const char *key
, char *retv
,
3561 int inlen
, struct lxc_conf
*c
,
3564 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3567 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3568 struct lxc_conf
*c
, void *data
)
3570 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3574 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3575 struct lxc_conf
*c
, void *data
)
3577 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3580 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3581 struct lxc_conf
*c
, void *data
)
3583 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3586 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3587 struct lxc_conf
*c
, void *data
)
3589 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3592 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3593 struct lxc_conf
*c
, void *data
)
3595 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3598 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3599 struct lxc_conf
*c
, void *data
)
3601 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3604 static int get_config_start(const char *key
, char *retv
, int inlen
,
3605 struct lxc_conf
*c
, void *data
)
3607 if (strcmp(key
+ 10, "auto") == 0)
3608 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3609 else if (strcmp(key
+ 10, "delay") == 0)
3610 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3611 else if (strcmp(key
+ 10, "order") == 0)
3612 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3617 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3618 struct lxc_conf
*c
, void *data
)
3620 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3623 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3624 struct lxc_conf
*c
, void *data
)
3626 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3629 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
3630 int inlen
, struct lxc_conf
*c
,
3633 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
3636 static int get_config_group(const char *key
, char *retv
, int inlen
,
3637 struct lxc_conf
*c
, void *data
)
3639 int len
, fulllen
= 0;
3640 struct lxc_list
*it
;
3645 memset(retv
, 0, inlen
);
3647 lxc_list_for_each(it
, &c
->groups
) {
3648 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3654 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3655 struct lxc_conf
*c
, void *data
)
3657 int len
, fulllen
= 0;
3658 struct lxc_list
*it
;
3663 memset(retv
, 0, inlen
);
3665 lxc_list_for_each(it
, &c
->environment
) {
3666 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3672 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3673 struct lxc_conf
*c
, void *data
)
3675 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3678 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3679 struct lxc_conf
*c
, void *data
)
3681 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3684 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3685 struct lxc_conf
*c
, void *data
)
3687 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3690 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3691 struct lxc_conf
*c
, void *data
)
3693 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3696 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3697 struct lxc_conf
*c
, void *data
)
3699 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3702 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3703 struct lxc_conf
*c
, void *data
)
3705 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3708 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3709 struct lxc_conf
*c
, void *data
)
3711 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3714 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3715 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3716 * printed, in 'lxc.prlimit.resource = value' format.
3718 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3719 struct lxc_conf
*c
, void *data
)
3721 int fulllen
= 0, len
;
3722 bool get_all
= false;
3723 struct lxc_list
*it
;
3728 memset(retv
, 0, inlen
);
3730 if (!strcmp(key
, "lxc.prlimit"))
3732 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3737 lxc_list_for_each(it
, &c
->limits
) {
3738 /* 2 colon separated 64 bit integers or the word 'unlimited' */
3739 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
3741 struct lxc_limit
*lim
= it
->elem
;
3743 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3744 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
3745 partlen
= STRLITERALLEN("unlimited");
3747 partlen
= sprintf(buf
, "%" PRIu64
,
3748 (uint64_t)lim
->limit
.rlim_cur
);
3750 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3751 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3752 memcpy(buf
+ partlen
, ":unlimited",
3753 STRLITERALLEN(":unlimited") + 1);
3755 sprintf(buf
+ partlen
, ":%" PRIu64
,
3756 (uint64_t)lim
->limit
.rlim_max
);
3760 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3761 lim
->resource
, buf
);
3762 } else if (!strcmp(lim
->resource
, key
)) {
3763 strprint(retv
, inlen
, "%s", buf
);
3770 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3771 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3772 * entries will be printed, in 'lxc.sysctl.key = value' format.
3774 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3775 struct lxc_conf
*c
, void *data
)
3778 struct lxc_list
*it
;
3780 bool get_all
= false;
3785 memset(retv
, 0, inlen
);
3787 if (strcmp(key
, "lxc.sysctl") == 0)
3789 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
3790 key
+= STRLITERALLEN("lxc.sysctl.");
3794 lxc_list_for_each(it
, &c
->sysctls
) {
3795 struct lxc_sysctl
*elem
= it
->elem
;
3797 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3798 elem
->key
, elem
->value
);
3799 } else if (strcmp(elem
->key
, key
) == 0) {
3800 strprint(retv
, inlen
, "%s", elem
->value
);
3807 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3808 struct lxc_conf
*c
, void *data
)
3810 struct lxc_list
*it
;
3813 bool get_all
= false;
3818 memset(retv
, 0, inlen
);
3820 if (strcmp(key
, "lxc.proc") == 0)
3822 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
3823 key
+= STRLITERALLEN("lxc.proc.");
3827 lxc_list_for_each(it
, &c
->procs
) {
3828 struct lxc_proc
*proc
= it
->elem
;
3831 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3832 proc
->filename
, proc
->value
);
3833 } else if (strcmp(proc
->filename
, key
) == 0) {
3834 strprint(retv
, inlen
, "%s", proc
->value
);
3841 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3842 struct lxc_conf
*c
, void *data
)
3850 memset(retv
, 0, inlen
);
3852 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3853 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3854 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3860 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3861 struct lxc_conf
*c
, void *data
)
3869 memset(retv
, 0, inlen
);
3871 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3872 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3873 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3879 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3880 struct lxc_conf
*c
, void *data
)
3883 const char *namespace;
3889 memset(retv
, 0, inlen
);
3891 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
3892 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3896 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3901 /* Callbacks to clear config items. */
3902 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3905 c
->personality
= -1;
3909 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3916 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3923 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3931 static inline int clr_config_apparmor_profile(const char *key
,
3932 struct lxc_conf
*c
, void *data
)
3934 free(c
->lsm_aa_profile
);
3935 c
->lsm_aa_profile
= NULL
;
3939 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3943 c
->lsm_aa_allow_incomplete
= 0;
3947 static inline int clr_config_apparmor_allow_nesting(const char *key
,
3951 c
->lsm_aa_allow_nesting
= 0;
3955 static inline int clr_config_apparmor_raw(const char *key
,
3959 return lxc_clear_apparmor_raw(c
);
3962 static inline int clr_config_selinux_context(const char *key
,
3963 struct lxc_conf
*c
, void *data
)
3965 free(c
->lsm_se_context
);
3966 c
->lsm_se_context
= NULL
;
3970 static inline int clr_config_cgroup_controller(const char *key
,
3971 struct lxc_conf
*c
, void *data
)
3973 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3976 static inline int clr_config_cgroup2_controller(const char *key
,
3977 struct lxc_conf
*c
, void *data
)
3979 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3982 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3985 if (lxc_conf
->cgroup_meta
.dir
) {
3986 free(lxc_conf
->cgroup_meta
.dir
);
3987 lxc_conf
->cgroup_meta
.dir
= NULL
;
3993 static inline int clr_config_cgroup_relative(const char *key
,
3994 struct lxc_conf
*lxc_conf
,
3997 lxc_conf
->cgroup_meta
.relative
= false;
4001 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4004 return lxc_clear_idmaps(c
);
4007 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4010 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4014 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4022 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4025 return lxc_clear_mount_entries(c
);
4028 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4031 return lxc_clear_automounts(c
);
4034 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4042 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4045 free(c
->rootfs
.path
);
4046 c
->rootfs
.path
= NULL
;
4050 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4053 c
->rootfs
.managed
= true;
4057 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4060 free(c
->rootfs
.mount
);
4061 c
->rootfs
.mount
= NULL
;
4065 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4068 free(c
->rootfs
.options
);
4069 c
->rootfs
.options
= NULL
;
4071 free(c
->rootfs
.data
);
4072 c
->rootfs
.data
= NULL
;
4077 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4085 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4088 return lxc_clear_hooks(c
, key
);
4091 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4094 /* default to legacy hooks version */
4095 c
->hooks_version
= 0;
4099 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4102 lxc_free_networks(&c
->network
);
4107 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4110 return lxc_clear_config_caps(c
);
4113 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4116 return lxc_clear_config_keepcaps(c
);
4119 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4122 free(c
->console
.path
);
4123 c
->console
.path
= NULL
;
4127 static inline int clr_config_console_logfile(const char *key
,
4128 struct lxc_conf
*c
, void *data
)
4130 free(c
->console
.log_path
);
4131 c
->console
.log_path
= NULL
;
4135 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4138 c
->console
.log_rotate
= 0;
4142 static inline int clr_config_console_buffer_size(const char *key
,
4143 struct lxc_conf
*c
, void *data
)
4145 c
->console
.buffer_size
= 0;
4149 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4152 c
->console
.log_size
= 0;
4156 static inline int clr_config_seccomp_profile(const char *key
,
4157 struct lxc_conf
*c
, void *data
)
4164 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4171 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4178 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4181 c
->rebootsignal
= 0;
4185 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4192 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4195 if (strcmp(key
+ 10, "auto") == 0)
4197 else if (strcmp(key
+ 10, "delay") == 0)
4199 else if (strcmp(key
+ 10, "order") == 0)
4205 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4213 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4216 c
->monitor_unshare
= 0;
4220 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4221 struct lxc_conf
*c
, void *data
)
4223 c
->monitor_signal_pdeath
= 0;
4227 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4230 return lxc_clear_groups(c
);
4233 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4236 return lxc_clear_environment(c
);
4239 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4242 free(c
->execute_cmd
);
4243 c
->execute_cmd
= NULL
;
4247 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4255 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4263 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4270 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4277 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4284 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4287 c
->no_new_privs
= false;
4291 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4294 return lxc_clear_limits(c
, key
);
4297 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4300 return lxc_clear_sysctls(c
, key
);
4303 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4306 return lxc_clear_procs(c
, key
);
4309 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4312 lxc_clear_includes(c
);
4316 static int clr_config_namespace_clone(const char *key
,
4317 struct lxc_conf
*lxc_conf
, void *data
)
4319 lxc_conf
->ns_clone
= 0;
4323 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4326 lxc_conf
->ns_keep
= 0;
4330 static int clr_config_namespace_share(const char *key
,
4331 struct lxc_conf
*lxc_conf
, void *data
)
4334 const char *namespace;
4336 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4337 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4341 free(lxc_conf
->ns_share
[ns_idx
]);
4342 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4347 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4348 struct lxc_conf
*c
, void *data
)
4353 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4354 struct lxc_conf
*lxc_conf
,
4356 char **deindexed_key
)
4359 unsigned int tmpidx
;
4361 char *copy
, *idx_start
, *idx_end
;
4362 struct lxc_config_t
*config
= NULL
;
4364 /* check that this is a sensible network key */
4365 if (strncmp("lxc.net.", key
, 8)) {
4366 ERROR("Invalid network configuration key \"%s\"", key
);
4372 ERROR("Failed to duplicate string \"%s\"", key
);
4377 if (!isdigit(*(key
+ 8))) {
4378 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4382 /* beginning of index string */
4383 idx_start
= (copy
+ 7);
4386 /* end of index string */
4387 idx_end
= strchr((copy
+ 8), '.');
4391 /* parse current index */
4392 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4395 SYSERROR("Failed to parse usigned integer from string \"%s\"",
4401 /* This, of course is utterly nonsensical on so many levels, but
4402 * better safe than sorry.
4403 * (Checking for INT_MAX here is intentional.)
4405 if (tmpidx
== INT_MAX
) {
4406 SYSERROR("Number of configured networks would overflow the "
4412 numstrlen
= strlen((idx_start
+ 1));
4414 /* repair configuration key */
4417 /* lxc.net.<idx>.<subkey> */
4420 if (strlen(idx_end
+ 1) == 0) {
4421 ERROR("No subkey in network configuration key \"%s\"", key
);
4425 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4426 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4428 config
= lxc_get_config(copy
);
4430 ERROR("Unknown network configuration key \"%s\"", key
);
4436 *deindexed_key
= copy
;
4445 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4446 * found. So we make sure next comes an integer, find the right callback (by
4447 * rewriting the key), and call it.
4449 static int set_config_net_nic(const char *key
, const char *value
,
4450 struct lxc_conf
*lxc_conf
, void *data
)
4453 const char *idxstring
;
4454 struct lxc_config_t
*config
;
4455 struct lxc_netdev
*netdev
;
4457 char *deindexed_key
= NULL
;
4459 idxstring
= key
+ 8;
4460 if (!isdigit(*idxstring
))
4463 if (lxc_config_value_empty(value
))
4464 return clr_config_net_nic(key
, lxc_conf
, data
);
4466 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4467 if (!config
|| idx
< 0)
4470 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4472 free(deindexed_key
);
4476 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4477 free(deindexed_key
);
4481 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4485 const char *idxstring
;
4486 struct lxc_config_t
*config
;
4487 struct lxc_netdev
*netdev
;
4489 char *deindexed_key
= NULL
;
4491 idxstring
= key
+ 8;
4492 if (!isdigit(*idxstring
))
4495 /* The left conjunct is pretty self-explanatory. The right conjunct
4496 * checks whether the two pointers are equal. If they are we know that
4497 * this is not a key that is namespaced any further and so we are
4498 * supposed to clear the whole network.
4500 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4501 unsigned int rmnetdevidx
;
4503 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4506 /* Remove network from network list. */
4507 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4511 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4512 if (!config
|| idx
< 0)
4515 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4517 free(deindexed_key
);
4521 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4522 free(deindexed_key
);
4526 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4529 struct lxc_netdev
*netdev
= data
;
4539 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4542 struct lxc_netdev
*netdev
= data
;
4547 netdev
->name
[0] = '\0';
4552 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4555 struct lxc_netdev
*netdev
= data
;
4565 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4568 struct lxc_netdev
*netdev
= data
;
4573 netdev
->link
[0] = '\0';
4578 static int clr_config_net_macvlan_mode(const char *key
,
4579 struct lxc_conf
*lxc_conf
, void *data
)
4581 struct lxc_netdev
*netdev
= data
;
4586 if (netdev
->type
!= LXC_NET_MACVLAN
)
4589 netdev
->priv
.macvlan_attr
.mode
= -1;
4594 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4597 struct lxc_netdev
*netdev
= data
;
4602 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4607 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4610 struct lxc_netdev
*netdev
= data
;
4615 free(netdev
->upscript
);
4616 netdev
->upscript
= NULL
;
4621 static int clr_config_net_script_down(const char *key
,
4622 struct lxc_conf
*lxc_conf
, void *data
)
4624 struct lxc_netdev
*netdev
= data
;
4629 free(netdev
->downscript
);
4630 netdev
->downscript
= NULL
;
4635 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4638 struct lxc_netdev
*netdev
= data
;
4643 free(netdev
->hwaddr
);
4644 netdev
->hwaddr
= NULL
;
4649 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4652 struct lxc_netdev
*netdev
= data
;
4663 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4666 struct lxc_netdev
*netdev
= data
;
4671 netdev
->priv
.vlan_attr
.vid
= 0;
4676 static int clr_config_net_ipv4_gateway(const char *key
,
4677 struct lxc_conf
*lxc_conf
, void *data
)
4679 struct lxc_netdev
*netdev
= data
;
4684 free(netdev
->ipv4_gateway
);
4685 netdev
->ipv4_gateway
= NULL
;
4690 static int clr_config_net_ipv4_address(const char *key
,
4691 struct lxc_conf
*lxc_conf
, void *data
)
4693 struct lxc_netdev
*netdev
= data
;
4694 struct lxc_list
*cur
, *next
;
4699 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4708 static int clr_config_net_ipv6_gateway(const char *key
,
4709 struct lxc_conf
*lxc_conf
, void *data
)
4711 struct lxc_netdev
*netdev
= data
;
4716 free(netdev
->ipv6_gateway
);
4717 netdev
->ipv6_gateway
= NULL
;
4722 static int clr_config_net_ipv6_address(const char *key
,
4723 struct lxc_conf
*lxc_conf
, void *data
)
4725 struct lxc_netdev
*netdev
= data
;
4726 struct lxc_list
*cur
, *next
;
4731 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4740 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4741 struct lxc_conf
*c
, void *data
)
4744 const char *idxstring
;
4745 struct lxc_config_t
*config
;
4746 struct lxc_netdev
*netdev
;
4748 char *deindexed_key
= NULL
;
4750 idxstring
= key
+ 8;
4751 if (!isdigit(*idxstring
))
4754 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4755 if (!config
|| idx
< 0)
4758 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4760 free(deindexed_key
);
4764 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4765 free(deindexed_key
);
4769 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4770 struct lxc_conf
*c
, void *data
)
4774 struct lxc_netdev
*netdev
= data
;
4779 memset(retv
, 0, inlen
);
4784 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4789 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4790 struct lxc_conf
*c
, void *data
)
4794 struct lxc_netdev
*netdev
= data
;
4799 memset(retv
, 0, inlen
);
4804 if (netdev
->flags
& IFF_UP
)
4805 strprint(retv
, inlen
, "up");
4810 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4811 struct lxc_conf
*c
, void *data
)
4815 struct lxc_netdev
*netdev
= data
;
4820 memset(retv
, 0, inlen
);
4825 if (netdev
->link
[0] != '\0')
4826 strprint(retv
, inlen
, "%s", netdev
->link
);
4831 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4832 struct lxc_conf
*c
, void *data
)
4836 struct lxc_netdev
*netdev
= data
;
4841 memset(retv
, 0, inlen
);
4846 if (netdev
->name
[0] != '\0')
4847 strprint(retv
, inlen
, "%s", netdev
->name
);
4852 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4853 struct lxc_conf
*c
, void *data
)
4858 struct lxc_netdev
*netdev
= data
;
4863 memset(retv
, 0, inlen
);
4868 if (netdev
->type
!= LXC_NET_MACVLAN
)
4871 switch (netdev
->priv
.macvlan_attr
.mode
) {
4872 case MACVLAN_MODE_PRIVATE
:
4875 case MACVLAN_MODE_VEPA
:
4878 case MACVLAN_MODE_BRIDGE
:
4881 case MACVLAN_MODE_PASSTHRU
:
4889 strprint(retv
, inlen
, "%s", mode
);
4894 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4895 struct lxc_conf
*c
, void *data
)
4899 struct lxc_netdev
*netdev
= data
;
4904 memset(retv
, 0, inlen
);
4909 if (netdev
->type
!= LXC_NET_VETH
)
4912 strprint(retv
, inlen
, "%s",
4913 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4914 ? netdev
->priv
.veth_attr
.pair
4915 : netdev
->priv
.veth_attr
.veth1
);
4920 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4921 struct lxc_conf
*c
, void *data
)
4925 struct lxc_netdev
*netdev
= data
;
4930 memset(retv
, 0, inlen
);
4935 if (netdev
->upscript
)
4936 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4941 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4942 struct lxc_conf
*c
, void *data
)
4946 struct lxc_netdev
*netdev
= data
;
4951 memset(retv
, 0, inlen
);
4956 if (netdev
->downscript
)
4957 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4962 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4963 struct lxc_conf
*c
, void *data
)
4967 struct lxc_netdev
*netdev
= data
;
4972 memset(retv
, 0, inlen
);
4978 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4983 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4984 struct lxc_conf
*c
, void *data
)
4988 struct lxc_netdev
*netdev
= data
;
4993 memset(retv
, 0, inlen
);
4999 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5004 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5005 struct lxc_conf
*c
, void *data
)
5009 struct lxc_netdev
*netdev
= data
;
5014 memset(retv
, 0, inlen
);
5019 if (netdev
->type
!= LXC_NET_VLAN
)
5022 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5027 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5028 struct lxc_conf
*c
, void *data
)
5031 char buf
[INET_ADDRSTRLEN
];
5033 struct lxc_netdev
*netdev
= data
;
5038 memset(retv
, 0, inlen
);
5043 if (netdev
->ipv4_gateway_auto
) {
5044 strprint(retv
, inlen
, "auto");
5045 } else if (netdev
->ipv4_gateway
) {
5046 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5047 strprint(retv
, inlen
, "%s", buf
);
5053 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5054 struct lxc_conf
*c
, void *data
)
5058 char buf
[INET_ADDRSTRLEN
];
5059 struct lxc_list
*it
;
5061 struct lxc_netdev
*netdev
= data
;
5066 memset(retv
, 0, inlen
);
5071 listlen
= lxc_list_len(&netdev
->ipv4
);
5072 lxc_list_for_each(it
, &netdev
->ipv4
) {
5073 struct lxc_inetdev
*i
= it
->elem
;
5074 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5075 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5076 (listlen
-- > 1) ? "\n" : "");
5082 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5083 struct lxc_conf
*c
, void *data
)
5086 char buf
[INET6_ADDRSTRLEN
];
5088 struct lxc_netdev
*netdev
= data
;
5093 memset(retv
, 0, inlen
);
5098 if (netdev
->ipv6_gateway_auto
) {
5099 strprint(retv
, inlen
, "auto");
5100 } else if (netdev
->ipv6_gateway
) {
5101 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5102 strprint(retv
, inlen
, "%s", buf
);
5108 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5109 struct lxc_conf
*c
, void *data
)
5113 char buf
[INET6_ADDRSTRLEN
];
5114 struct lxc_list
*it
;
5116 struct lxc_netdev
*netdev
= data
;
5121 memset(retv
, 0, inlen
);
5126 listlen
= lxc_list_len(&netdev
->ipv6
);
5127 lxc_list_for_each(it
, &netdev
->ipv6
) {
5128 struct lxc_inet6dev
*i
= it
->elem
;
5129 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5130 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5131 (listlen
-- > 1) ? "\n" : "");
5137 int lxc_list_config_items(char *retv
, int inlen
)
5146 memset(retv
, 0, inlen
);
5148 for (i
= 0; i
< config_size
; i
++) {
5149 char *s
= config
[i
].name
;
5151 if (s
[strlen(s
) - 1] == '.')
5154 strprint(retv
, inlen
, "%s\n", s
);
5160 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5169 memset(retv
, 0, inlen
);
5171 if (!strcmp(key
, "lxc.apparmor")) {
5172 strprint(retv
, inlen
, "allow_incomplete\n");
5173 strprint(retv
, inlen
, "allow_nesting\n");
5174 strprint(retv
, inlen
, "profile\n");
5175 strprint(retv
, inlen
, "raw\n");
5176 } else if (!strcmp(key
, "lxc.cgroup")) {
5177 strprint(retv
, inlen
, "dir\n");
5178 } else if (!strcmp(key
, "lxc.selinux")) {
5179 strprint(retv
, inlen
, "context\n");
5180 } else if (!strcmp(key
, "lxc.mount")) {
5181 strprint(retv
, inlen
, "auto\n");
5182 strprint(retv
, inlen
, "entry\n");
5183 strprint(retv
, inlen
, "fstab\n");
5184 } else if (!strcmp(key
, "lxc.rootfs")) {
5185 strprint(retv
, inlen
, "mount\n");
5186 strprint(retv
, inlen
, "options\n");
5187 strprint(retv
, inlen
, "path\n");
5188 } else if (!strcmp(key
, "lxc.uts")) {
5189 strprint(retv
, inlen
, "name\n");
5190 } else if (!strcmp(key
, "lxc.hook")) {
5191 strprint(retv
, inlen
, "autodev\n");
5192 strprint(retv
, inlen
, "clone\n");
5193 strprint(retv
, inlen
, "destroy\n");
5194 strprint(retv
, inlen
, "mount\n");
5195 strprint(retv
, inlen
, "post-stop\n");
5196 strprint(retv
, inlen
, "pre-mount\n");
5197 strprint(retv
, inlen
, "pre-start\n");
5198 strprint(retv
, inlen
, "start-host\n");
5199 strprint(retv
, inlen
, "start\n");
5200 strprint(retv
, inlen
, "stop\n");
5201 } else if (!strcmp(key
, "lxc.cap")) {
5202 strprint(retv
, inlen
, "drop\n");
5203 strprint(retv
, inlen
, "keep\n");
5204 } else if (!strcmp(key
, "lxc.console")) {
5205 strprint(retv
, inlen
, "logfile\n");
5206 strprint(retv
, inlen
, "path\n");
5207 } else if (!strcmp(key
, "lxc.seccomp")) {
5208 strprint(retv
, inlen
, "profile\n");
5209 } else if (!strcmp(key
, "lxc.signal")) {
5210 strprint(retv
, inlen
, "halt\n");
5211 strprint(retv
, inlen
, "reboot\n");
5212 strprint(retv
, inlen
, "stop\n");
5213 } else if (!strcmp(key
, "lxc.start")) {
5214 strprint(retv
, inlen
, "auto\n");
5215 strprint(retv
, inlen
, "delay\n");
5216 strprint(retv
, inlen
, "order\n");
5217 } else if (!strcmp(key
, "lxc.monitor")) {
5218 strprint(retv
, inlen
, "unshare\n");
5226 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5229 const char *idxstring
;
5230 struct lxc_netdev
*netdev
;
5234 idxstring
= key
+ 8;
5235 if (!isdigit(*idxstring
))
5238 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5242 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5249 memset(retv
, 0, inlen
);
5251 strprint(retv
, inlen
, "type\n");
5252 strprint(retv
, inlen
, "script.up\n");
5253 strprint(retv
, inlen
, "script.down\n");
5254 if (netdev
->type
!= LXC_NET_EMPTY
) {
5255 strprint(retv
, inlen
, "flags\n");
5256 strprint(retv
, inlen
, "link\n");
5257 strprint(retv
, inlen
, "name\n");
5258 strprint(retv
, inlen
, "hwaddr\n");
5259 strprint(retv
, inlen
, "mtu\n");
5260 strprint(retv
, inlen
, "ipv6.address\n");
5261 strprint(retv
, inlen
, "ipv6.gateway\n");
5262 strprint(retv
, inlen
, "ipv4.address\n");
5263 strprint(retv
, inlen
, "ipv4.gateway\n");
5266 switch (netdev
->type
) {
5268 strprint(retv
, inlen
, "veth.pair\n");
5270 case LXC_NET_MACVLAN
:
5271 strprint(retv
, inlen
, "macvlan.mode\n");
5274 strprint(retv
, inlen
, "vlan.id\n");