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
26 #define __STDC_FORMAT_MACROS
39 #include <arpa/inet.h>
41 #include <netinet/in.h>
42 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/utsname.h>
50 #include "confile_utils.h"
52 #include "lxcseccomp.h"
61 #include <../include/ifaddrs.h>
64 #if HAVE_SYS_PERSONALITY_H
65 #include <sys/personality.h>
69 #include "include/strlcpy.h"
73 #include "include/strlcat.h"
76 lxc_log_define(confile
, lxc
);
78 #define lxc_config_define(name) \
79 static int set_config_##name(const char *, const char *, \
80 struct lxc_conf *, void *); \
81 static int get_config_##name(const char *, char *, int, \
82 struct lxc_conf *, void *); \
83 static int clr_config_##name(const char *, struct lxc_conf *, void *);
85 lxc_config_define(autodev
);
86 lxc_config_define(apparmor_allow_incomplete
);
87 lxc_config_define(apparmor_allow_nesting
);
88 lxc_config_define(apparmor_profile
);
89 lxc_config_define(apparmor_raw
);
90 lxc_config_define(cap_drop
);
91 lxc_config_define(cap_keep
);
92 lxc_config_define(cgroup_controller
);
93 lxc_config_define(cgroup2_controller
);
94 lxc_config_define(cgroup_dir
);
95 lxc_config_define(cgroup_relative
);
96 lxc_config_define(console_buffer_size
);
97 lxc_config_define(console_logfile
);
98 lxc_config_define(console_path
);
99 lxc_config_define(console_rotate
);
100 lxc_config_define(console_size
);
101 lxc_config_define(environment
);
102 lxc_config_define(ephemeral
);
103 lxc_config_define(execute_cmd
);
104 lxc_config_define(group
);
105 lxc_config_define(hooks
);
106 lxc_config_define(hooks_version
);
107 lxc_config_define(idmaps
);
108 lxc_config_define(includefiles
);
109 lxc_config_define(init_cmd
);
110 lxc_config_define(init_cwd
);
111 lxc_config_define(init_gid
);
112 lxc_config_define(init_uid
);
113 lxc_config_define(log_file
);
114 lxc_config_define(log_level
);
115 lxc_config_define(log_syslog
);
116 lxc_config_define(monitor
);
117 lxc_config_define(monitor_signal_pdeath
);
118 lxc_config_define(mount
);
119 lxc_config_define(mount_auto
);
120 lxc_config_define(mount_fstab
);
121 lxc_config_define(namespace_clone
);
122 lxc_config_define(namespace_keep
);
123 lxc_config_define(namespace_share
);
124 lxc_config_define(net
);
125 lxc_config_define(net_flags
);
126 lxc_config_define(net_hwaddr
);
127 lxc_config_define(net_ipv4_address
);
128 lxc_config_define(net_ipv4_gateway
);
129 lxc_config_define(net_ipv6_address
);
130 lxc_config_define(net_ipv6_gateway
);
131 lxc_config_define(net_link
);
132 lxc_config_define(net_macvlan_mode
);
133 lxc_config_define(net_mtu
);
134 lxc_config_define(net_name
);
135 lxc_config_define(net_nic
);
136 lxc_config_define(net_script_down
);
137 lxc_config_define(net_script_up
);
138 lxc_config_define(net_type
);
139 lxc_config_define(net_veth_pair
);
140 lxc_config_define(net_vlan_id
);
141 lxc_config_define(no_new_privs
);
142 lxc_config_define(personality
);
143 lxc_config_define(prlimit
);
144 lxc_config_define(pty_max
);
145 lxc_config_define(rootfs_managed
);
146 lxc_config_define(rootfs_mount
);
147 lxc_config_define(rootfs_options
);
148 lxc_config_define(rootfs_path
);
149 lxc_config_define(seccomp_profile
);
150 lxc_config_define(selinux_context
);
151 lxc_config_define(signal_halt
);
152 lxc_config_define(signal_reboot
);
153 lxc_config_define(signal_stop
);
154 lxc_config_define(start
);
155 lxc_config_define(tty_max
);
156 lxc_config_define(tty_dir
);
157 lxc_config_define(uts_name
);
158 lxc_config_define(sysctl
);
159 lxc_config_define(proc
);
161 static struct lxc_config_t config
[] = {
162 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
163 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
164 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
165 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
166 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
167 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
168 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
169 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
170 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
171 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
172 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
173 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
174 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
175 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
176 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
177 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
178 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
179 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
180 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
181 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
182 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
183 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
184 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
185 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
188 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
190 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
192 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
193 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
194 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
195 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
196 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
197 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
198 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
199 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
200 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
201 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
202 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
203 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
204 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
205 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
206 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
207 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
208 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
209 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
210 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
211 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
212 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
213 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
214 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
215 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
216 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
217 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
218 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
219 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
220 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
221 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
222 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
223 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
224 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
225 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
226 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
227 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
228 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
229 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
230 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
231 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
232 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
233 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
234 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
235 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
236 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
237 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
238 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
239 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
240 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
241 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
242 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
243 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
244 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
245 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
246 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
247 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
248 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
251 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
253 struct lxc_config_t
*lxc_get_config(const char *key
)
257 for (i
= 0; i
< config_size
; i
++)
258 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
264 static int set_config_net(const char *key
, const char *value
,
265 struct lxc_conf
*lxc_conf
, void *data
)
267 if (!lxc_config_value_empty(value
)) {
268 ERROR("lxc.net must not have a value");
272 return clr_config_net(key
, lxc_conf
, data
);
275 static int set_config_net_type(const char *key
, const char *value
,
276 struct lxc_conf
*lxc_conf
, void *data
)
278 struct lxc_netdev
*netdev
= data
;
280 if (lxc_config_value_empty(value
))
281 return clr_config_net_type(key
, lxc_conf
, data
);
286 if (!strcmp(value
, "veth")) {
287 netdev
->type
= LXC_NET_VETH
;
288 } else if (!strcmp(value
, "macvlan")) {
289 netdev
->type
= LXC_NET_MACVLAN
;
290 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
292 } else if (!strcmp(value
, "vlan")) {
293 netdev
->type
= LXC_NET_VLAN
;
294 } else if (!strcmp(value
, "phys")) {
295 netdev
->type
= LXC_NET_PHYS
;
296 } else if (!strcmp(value
, "empty")) {
297 netdev
->type
= LXC_NET_EMPTY
;
298 } else if (!strcmp(value
, "none")) {
299 netdev
->type
= LXC_NET_NONE
;
301 ERROR("invalid network type %s", value
);
308 static int set_config_net_flags(const char *key
, const char *value
,
309 struct lxc_conf
*lxc_conf
, void *data
)
311 struct lxc_netdev
*netdev
= data
;
313 if (lxc_config_value_empty(value
))
314 return clr_config_net_flags(key
, lxc_conf
, data
);
319 netdev
->flags
|= IFF_UP
;
324 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
325 struct lxc_netdev
*netdev
)
327 struct ifaddrs
*ifaddr
, *ifa
;
330 const char *type_key
= "lxc.net.type";
331 const char *link_key
= "lxc.net.link";
332 const char *tmpvalue
= "phys";
334 if (getifaddrs(&ifaddr
) == -1) {
335 SYSERROR("Get network interfaces failed");
339 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
342 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
345 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
346 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
349 ret
= set_config_net_link(
350 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
352 ERROR("failed to create matched ifnames");
356 ERROR("failed to create matched ifnames");
368 static int set_config_net_link(const char *key
, const char *value
,
369 struct lxc_conf
*lxc_conf
, void *data
)
371 struct lxc_netdev
*netdev
= data
;
374 if (lxc_config_value_empty(value
))
375 return clr_config_net_link(key
, lxc_conf
, data
);
380 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
381 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
383 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
388 static int set_config_net_name(const char *key
, const char *value
,
389 struct lxc_conf
*lxc_conf
, void *data
)
391 struct lxc_netdev
*netdev
= data
;
393 if (lxc_config_value_empty(value
))
394 return clr_config_net_name(key
, lxc_conf
, data
);
399 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
402 static int set_config_net_veth_pair(const char *key
, const char *value
,
403 struct lxc_conf
*lxc_conf
, void *data
)
405 struct lxc_netdev
*netdev
= data
;
407 if (lxc_config_value_empty(value
))
408 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
413 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
416 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
417 struct lxc_conf
*lxc_conf
, void *data
)
419 struct lxc_netdev
*netdev
= data
;
421 if (lxc_config_value_empty(value
))
422 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
427 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
430 static int set_config_net_hwaddr(const char *key
, const char *value
,
431 struct lxc_conf
*lxc_conf
, void *data
)
433 struct lxc_netdev
*netdev
= data
;
436 if (lxc_config_value_empty(value
))
437 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
442 new_value
= strdup(value
);
446 rand_complete_hwaddr(new_value
);
448 if (lxc_config_value_empty(new_value
)) {
450 netdev
->hwaddr
= NULL
;
454 netdev
->hwaddr
= new_value
;
458 static int set_config_net_vlan_id(const char *key
, const char *value
,
459 struct lxc_conf
*lxc_conf
, void *data
)
462 struct lxc_netdev
*netdev
= data
;
464 if (lxc_config_value_empty(value
))
465 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
470 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
477 static int set_config_net_mtu(const char *key
, const char *value
,
478 struct lxc_conf
*lxc_conf
, void *data
)
480 struct lxc_netdev
*netdev
= data
;
482 if (lxc_config_value_empty(value
))
483 return clr_config_net_mtu(key
, lxc_conf
, data
);
488 return set_config_string_item(&netdev
->mtu
, value
);
491 static int set_config_net_ipv4_address(const char *key
, const char *value
,
492 struct lxc_conf
*lxc_conf
, void *data
)
495 struct lxc_netdev
*netdev
= data
;
496 struct lxc_inetdev
*inetdev
;
497 struct lxc_list
*list
;
498 char *cursor
, *slash
;
499 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
501 if (lxc_config_value_empty(value
))
502 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
507 inetdev
= malloc(sizeof(*inetdev
));
511 memset(inetdev
, 0, sizeof(*inetdev
));
513 list
= malloc(sizeof(*list
));
520 list
->elem
= inetdev
;
522 addr
= strdup(value
);
529 cursor
= strstr(addr
, " ");
535 slash
= strstr(addr
, "/");
541 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
542 if (!ret
|| ret
< 0) {
543 SYSERROR("Invalid ipv4 address \"%s\"", value
);
551 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
552 if (!ret
|| ret
< 0) {
553 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
562 /* No prefix specified, determine it from the network class. */
564 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
572 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
575 /* If no broadcast address, let compute one from the
576 * prefix and address.
579 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
580 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
583 lxc_list_add_tail(&netdev
->ipv4
, list
);
589 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
590 struct lxc_conf
*lxc_conf
, void *data
)
592 struct lxc_netdev
*netdev
= data
;
594 if (lxc_config_value_empty(value
))
595 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
600 free(netdev
->ipv4_gateway
);
602 if (!strcmp(value
, "auto")) {
603 netdev
->ipv4_gateway
= NULL
;
604 netdev
->ipv4_gateway_auto
= true;
609 gw
= malloc(sizeof(*gw
));
613 ret
= inet_pton(AF_INET
, value
, gw
);
614 if (!ret
|| ret
< 0) {
615 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
620 netdev
->ipv4_gateway
= gw
;
621 netdev
->ipv4_gateway_auto
= false;
627 static int set_config_net_ipv6_address(const char *key
, const char *value
,
628 struct lxc_conf
*lxc_conf
, void *data
)
631 struct lxc_netdev
*netdev
= data
;
632 struct lxc_inet6dev
*inet6dev
;
633 struct lxc_list
*list
;
634 char *slash
, *valdup
, *netmask
;
636 if (lxc_config_value_empty(value
))
637 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
642 inet6dev
= malloc(sizeof(*inet6dev
));
646 memset(inet6dev
, 0, sizeof(*inet6dev
));
648 list
= malloc(sizeof(*list
));
655 list
->elem
= inet6dev
;
657 valdup
= strdup(value
);
664 inet6dev
->prefix
= 64;
665 slash
= strstr(valdup
, "/");
669 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
678 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
679 if (!ret
|| ret
< 0) {
680 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
687 lxc_list_add_tail(&netdev
->ipv6
, list
);
693 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
694 struct lxc_conf
*lxc_conf
, void *data
)
696 struct lxc_netdev
*netdev
= data
;
698 if (lxc_config_value_empty(value
))
699 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
704 free(netdev
->ipv6_gateway
);
706 if (!strcmp(value
, "auto")) {
707 netdev
->ipv6_gateway
= NULL
;
708 netdev
->ipv6_gateway_auto
= true;
713 gw
= malloc(sizeof(*gw
));
717 ret
= inet_pton(AF_INET6
, value
, gw
);
718 if (!ret
|| ret
< 0) {
719 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
724 netdev
->ipv6_gateway
= gw
;
725 netdev
->ipv6_gateway_auto
= false;
731 static int set_config_net_script_up(const char *key
, const char *value
,
732 struct lxc_conf
*lxc_conf
, void *data
)
734 struct lxc_netdev
*netdev
= data
;
736 if (lxc_config_value_empty(value
))
737 return clr_config_net_script_up(key
, lxc_conf
, data
);
742 return set_config_string_item(&netdev
->upscript
, value
);
745 static int set_config_net_script_down(const char *key
, const char *value
,
746 struct lxc_conf
*lxc_conf
, void *data
)
748 struct lxc_netdev
*netdev
= data
;
750 if (lxc_config_value_empty(value
))
751 return clr_config_net_script_down(key
, lxc_conf
, data
);
756 return set_config_string_item(&netdev
->downscript
, value
);
759 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
761 struct lxc_list
*hooklist
;
763 hooklist
= malloc(sizeof(*hooklist
));
769 hooklist
->elem
= hook
;
770 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
774 static int set_config_seccomp_profile(const char *key
, const char *value
,
775 struct lxc_conf
*lxc_conf
, void *data
)
777 return set_config_path_item(&lxc_conf
->seccomp
, value
);
780 static int set_config_execute_cmd(const char *key
, const char *value
,
781 struct lxc_conf
*lxc_conf
, void *data
)
783 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
786 static int set_config_init_cmd(const char *key
, const char *value
,
787 struct lxc_conf
*lxc_conf
, void *data
)
789 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
792 static int set_config_init_cwd(const char *key
, const char *value
,
793 struct lxc_conf
*lxc_conf
, void *data
)
795 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
798 static int set_config_init_uid(const char *key
, const char *value
,
799 struct lxc_conf
*lxc_conf
, void *data
)
801 unsigned int init_uid
;
803 if (lxc_config_value_empty(value
)) {
804 lxc_conf
->init_uid
= 0;
808 if (lxc_safe_uint(value
, &init_uid
) < 0)
811 lxc_conf
->init_uid
= init_uid
;
816 static int set_config_init_gid(const char *key
, const char *value
,
817 struct lxc_conf
*lxc_conf
, void *data
)
819 unsigned int init_gid
;
821 if (lxc_config_value_empty(value
)) {
822 lxc_conf
->init_gid
= 0;
826 if (lxc_safe_uint(value
, &init_gid
) < 0)
829 lxc_conf
->init_gid
= init_gid
;
834 static int set_config_hooks(const char *key
, const char *value
,
835 struct lxc_conf
*lxc_conf
, void *data
)
839 if (lxc_config_value_empty(value
))
840 return lxc_clear_hooks(lxc_conf
, key
);
842 if (strcmp(key
+ 4, "hook") == 0) {
843 ERROR("lxc.hook must not have a value");
847 copy
= strdup(value
);
851 if (strcmp(key
+ 9, "pre-start") == 0)
852 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
853 else if (strcmp(key
+ 9, "start-host") == 0)
854 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
855 else if (strcmp(key
+ 9, "pre-mount") == 0)
856 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
857 else if (strcmp(key
+ 9, "autodev") == 0)
858 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
859 else if (strcmp(key
+ 9, "mount") == 0)
860 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
861 else if (strcmp(key
+ 9, "start") == 0)
862 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
863 else if (strcmp(key
+ 9, "stop") == 0)
864 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
865 else if (strcmp(key
+ 9, "post-stop") == 0)
866 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
867 else if (strcmp(key
+ 9, "clone") == 0)
868 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
869 else if (strcmp(key
+ 9, "destroy") == 0)
870 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
876 static int set_config_hooks_version(const char *key
, const char *value
,
877 struct lxc_conf
*lxc_conf
, void *data
)
882 if (lxc_config_value_empty(value
))
883 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
885 ret
= lxc_safe_uint(value
, &tmp
);
890 ERROR("Invalid hook version specified. Currently only 0 "
891 "(legacy) and 1 are supported");
895 lxc_conf
->hooks_version
= tmp
;
899 static int set_config_personality(const char *key
, const char *value
,
900 struct lxc_conf
*lxc_conf
, void *data
)
902 signed long personality
= lxc_config_parse_arch(value
);
904 if (personality
>= 0)
905 lxc_conf
->personality
= personality
;
907 WARN("Unsupported personality \"%s\"", value
);
912 static int set_config_pty_max(const char *key
, const char *value
,
913 struct lxc_conf
*lxc_conf
, void *data
)
916 unsigned int max
= 0;
918 if (lxc_config_value_empty(value
)) {
919 lxc_conf
->pty_max
= 0;
923 ret
= lxc_safe_uint(value
, &max
);
927 lxc_conf
->pty_max
= max
;
931 /* We only need to check whether the first byte of the key after the lxc.start.
932 * prefix matches our expectations since they fortunately all start with a
933 * different letter. If anything was wrong with the key we would have already
934 * noticed when the callback was called.
936 static int set_config_start(const char *key
, const char *value
,
937 struct lxc_conf
*lxc_conf
, void *data
)
941 is_empty
= lxc_config_value_empty(value
);
943 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
945 lxc_conf
->start_auto
= 0;
949 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
952 if (lxc_conf
->start_auto
> 1)
956 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
958 lxc_conf
->start_delay
= 0;
962 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
963 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
965 lxc_conf
->start_order
= 0;
969 return lxc_safe_int(value
, &lxc_conf
->start_order
);
975 static int set_config_monitor(const char *key
, const char *value
,
976 struct lxc_conf
*lxc_conf
, void *data
)
978 if (lxc_config_value_empty(value
)) {
979 lxc_conf
->monitor_unshare
= 0;
983 if (strcmp(key
+ 12, "unshare") == 0)
984 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
989 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
990 struct lxc_conf
*lxc_conf
, void *data
)
992 if (lxc_config_value_empty(value
)) {
993 lxc_conf
->monitor_signal_pdeath
= 0;
997 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1000 sig_n
= sig_parse(value
);
1004 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1011 static int set_config_group(const char *key
, const char *value
,
1012 struct lxc_conf
*lxc_conf
, void *data
)
1014 char *groups
, *token
;
1015 struct lxc_list
*grouplist
;
1018 if (lxc_config_value_empty(value
))
1019 return lxc_clear_groups(lxc_conf
);
1021 groups
= strdup(value
);
1025 /* In case several groups are specified in a single line split these
1026 * groups in a single element for the list.
1028 lxc_iterate_parts(token
, groups
, " \t") {
1029 grouplist
= malloc(sizeof(*grouplist
));
1035 grouplist
->elem
= strdup(token
);
1036 if (!grouplist
->elem
) {
1042 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1049 static int set_config_environment(const char *key
, const char *value
,
1050 struct lxc_conf
*lxc_conf
, void *data
)
1052 struct lxc_list
*list_item
= NULL
;
1054 if (lxc_config_value_empty(value
))
1055 return lxc_clear_environment(lxc_conf
);
1057 list_item
= malloc(sizeof(*list_item
));
1061 if (!strchr(value
, '=')) {
1062 const char *env_val
;
1063 const char *env_key
= value
;
1064 const char *env_var
[3] = {0};
1066 env_val
= getenv(env_key
);
1070 env_var
[0] = env_key
;
1071 env_var
[1] = env_val
;
1072 list_item
->elem
= lxc_string_join("=", env_var
, false);
1074 list_item
->elem
= strdup(value
);
1077 if (!list_item
->elem
)
1080 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1089 static int set_config_tty_max(const char *key
, const char *value
,
1090 struct lxc_conf
*lxc_conf
, void *data
)
1093 unsigned int nbtty
= 0;
1095 if (lxc_config_value_empty(value
)) {
1096 lxc_conf
->ttys
.max
= 0;
1100 ret
= lxc_safe_uint(value
, &nbtty
);
1104 lxc_conf
->ttys
.max
= nbtty
;
1108 static int set_config_tty_dir(const char *key
, const char *value
,
1109 struct lxc_conf
*lxc_conf
, void *data
)
1111 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1115 static int set_config_apparmor_profile(const char *key
, const char *value
,
1116 struct lxc_conf
*lxc_conf
, void *data
)
1118 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1121 static int set_config_apparmor_allow_incomplete(const char *key
,
1123 struct lxc_conf
*lxc_conf
,
1126 if (lxc_config_value_empty(value
)) {
1127 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1131 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1134 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1140 static int set_config_apparmor_allow_nesting(const char *key
,
1142 struct lxc_conf
*lxc_conf
,
1145 if (lxc_config_value_empty(value
))
1146 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1148 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1151 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1157 static int set_config_apparmor_raw(const char *key
,
1159 struct lxc_conf
*lxc_conf
,
1163 struct lxc_list
*list
;
1165 if (lxc_config_value_empty(value
))
1166 return lxc_clear_apparmor_raw(lxc_conf
);
1168 list
= malloc(sizeof(*list
));
1174 elem
= strdup(value
);
1181 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1186 static int set_config_selinux_context(const char *key
, const char *value
,
1187 struct lxc_conf
*lxc_conf
, void *data
)
1189 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1192 static int set_config_log_file(const char *key
, const char *value
,
1193 struct lxc_conf
*c
, void *data
)
1197 if (lxc_config_value_empty(value
)) {
1203 /* Store these values in the lxc_conf, and then try to set for actual
1206 ret
= set_config_path_item(&c
->logfile
, value
);
1208 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1213 static int set_config_log_level(const char *key
, const char *value
,
1214 struct lxc_conf
*lxc_conf
, void *data
)
1218 if (lxc_config_value_empty(value
)) {
1219 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1223 if (value
[0] >= '0' && value
[0] <= '9') {
1224 if (lxc_safe_int(value
, &newlevel
) < 0)
1227 newlevel
= lxc_log_priority_to_int(value
);
1230 /* Store these values in the lxc_conf, and then try to set for actual
1233 lxc_conf
->loglevel
= newlevel
;
1234 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1237 static int set_config_autodev(const char *key
, const char *value
,
1238 struct lxc_conf
*lxc_conf
, void *data
)
1240 if (lxc_config_value_empty(value
)) {
1241 lxc_conf
->autodev
= 0;
1245 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1248 if (lxc_conf
->autodev
> 1)
1254 static int set_config_signal_halt(const char *key
, const char *value
,
1255 struct lxc_conf
*lxc_conf
, void *data
)
1259 if (lxc_config_value_empty(value
)) {
1260 lxc_conf
->haltsignal
= 0;
1264 sig_n
= sig_parse(value
);
1268 lxc_conf
->haltsignal
= sig_n
;
1273 static int set_config_signal_reboot(const char *key
, const char *value
,
1274 struct lxc_conf
*lxc_conf
, void *data
)
1278 if (lxc_config_value_empty(value
)) {
1279 lxc_conf
->rebootsignal
= 0;
1283 sig_n
= sig_parse(value
);
1287 lxc_conf
->rebootsignal
= sig_n
;
1292 static int set_config_signal_stop(const char *key
, const char *value
,
1293 struct lxc_conf
*lxc_conf
, void *data
)
1297 if (lxc_config_value_empty(value
)) {
1298 lxc_conf
->stopsignal
= 0;
1302 sig_n
= sig_parse(value
);
1306 lxc_conf
->stopsignal
= sig_n
;
1311 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1312 struct lxc_conf
*lxc_conf
, int version
)
1314 const char *subkey
, *token
;
1316 struct lxc_list
*cglist
= NULL
;
1317 struct lxc_cgroup
*cgelem
= NULL
;
1319 if (lxc_config_value_empty(value
))
1320 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1322 if (version
== CGROUP2_SUPER_MAGIC
) {
1323 token
= "lxc.cgroup2.";
1325 } else if (version
== CGROUP_SUPER_MAGIC
) {
1326 token
= "lxc.cgroup.";
1332 if (strncmp(key
, token
, token_len
) != 0)
1335 subkey
= key
+ token_len
;
1336 if (*subkey
== '\0')
1339 cglist
= malloc(sizeof(*cglist
));
1343 cgelem
= malloc(sizeof(*cgelem
));
1346 memset(cgelem
, 0, sizeof(*cgelem
));
1348 cgelem
->subsystem
= strdup(subkey
);
1349 if (!cgelem
->subsystem
)
1352 cgelem
->value
= strdup(value
);
1356 cgelem
->version
= version
;
1358 lxc_list_add_elem(cglist
, cgelem
);
1360 if (version
== CGROUP2_SUPER_MAGIC
)
1361 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1363 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1370 free(cgelem
->subsystem
);
1371 free(cgelem
->value
);
1378 static int set_config_cgroup_controller(const char *key
, const char *value
,
1379 struct lxc_conf
*lxc_conf
, void *data
)
1381 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1382 CGROUP_SUPER_MAGIC
);
1385 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1386 struct lxc_conf
*lxc_conf
, void *data
)
1388 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1389 CGROUP2_SUPER_MAGIC
);
1393 static int set_config_cgroup_dir(const char *key
, const char *value
,
1394 struct lxc_conf
*lxc_conf
, void *data
)
1396 if (lxc_config_value_empty(value
))
1397 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1399 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1402 static int set_config_cgroup_relative(const char *key
, const char *value
,
1403 struct lxc_conf
*lxc_conf
, void *data
)
1405 unsigned int converted
;
1408 if (lxc_config_value_empty(value
))
1409 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1411 ret
= lxc_safe_uint(value
, &converted
);
1415 if (converted
== 1) {
1416 lxc_conf
->cgroup_meta
.relative
= true;
1420 if (converted
== 0) {
1421 lxc_conf
->cgroup_meta
.relative
= false;
1428 static int set_config_prlimit(const char *key
, const char *value
,
1429 struct lxc_conf
*lxc_conf
, void *data
)
1431 struct lxc_list
*iter
;
1432 struct rlimit limit
;
1434 struct lxc_list
*limlist
= NULL
;
1435 struct lxc_limit
*limelem
= NULL
;
1437 if (lxc_config_value_empty(value
))
1438 return lxc_clear_limits(lxc_conf
, key
);
1440 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1443 key
+= sizeof("lxc.prlimit.") - 1;
1445 /* soft limit comes first in the value */
1446 if (!parse_limit_value(&value
, &limit_value
))
1448 limit
.rlim_cur
= limit_value
;
1450 /* skip spaces and a colon */
1451 while (isspace(*value
))
1456 else if (*value
) /* any other character is an error here */
1459 while (isspace(*value
))
1462 /* optional hard limit */
1464 if (!parse_limit_value(&value
, &limit_value
))
1466 limit
.rlim_max
= limit_value
;
1468 /* check for trailing garbage */
1469 while (isspace(*value
))
1475 /* a single value sets both hard and soft limit */
1476 limit
.rlim_max
= limit
.rlim_cur
;
1479 /* find existing list element */
1480 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1481 limelem
= iter
->elem
;
1482 if (!strcmp(key
, limelem
->resource
)) {
1483 limelem
->limit
= limit
;
1488 /* allocate list element */
1489 limlist
= malloc(sizeof(*limlist
));
1493 limelem
= malloc(sizeof(*limelem
));
1496 memset(limelem
, 0, sizeof(*limelem
));
1498 limelem
->resource
= strdup(key
);
1499 if (!limelem
->resource
)
1501 limelem
->limit
= limit
;
1503 lxc_list_add_elem(limlist
, limelem
);;
1505 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1512 free(limelem
->resource
);
1518 static int set_config_sysctl(const char *key
, const char *value
,
1519 struct lxc_conf
*lxc_conf
, void *data
)
1521 struct lxc_list
*iter
;
1522 char *replace_value
= NULL
;
1523 struct lxc_list
*sysctl_list
= NULL
;
1524 struct lxc_sysctl
*sysctl_elem
= NULL
;
1526 if (lxc_config_value_empty(value
))
1527 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1529 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1532 key
+= sizeof("lxc.sysctl.") - 1;
1534 /* find existing list element */
1535 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1536 sysctl_elem
= iter
->elem
;
1538 if (strcmp(key
, sysctl_elem
->key
) != 0)
1541 replace_value
= strdup(value
);
1545 free(sysctl_elem
->value
);
1546 sysctl_elem
->value
= replace_value
;
1550 /* allocate list element */
1551 sysctl_list
= malloc(sizeof(*sysctl_list
));
1555 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1558 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1560 sysctl_elem
->key
= strdup(key
);
1561 if (!sysctl_elem
->key
)
1564 sysctl_elem
->value
= strdup(value
);
1565 if (!sysctl_elem
->value
)
1568 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1570 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1577 free(sysctl_elem
->key
);
1578 free(sysctl_elem
->value
);
1584 static int set_config_proc(const char *key
, const char *value
,
1585 struct lxc_conf
*lxc_conf
, void *data
)
1588 struct lxc_list
*proclist
= NULL
;
1589 struct lxc_proc
*procelem
= NULL
;
1591 if (lxc_config_value_empty(value
))
1592 return clr_config_proc(key
, lxc_conf
, NULL
);
1594 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1597 subkey
= key
+ sizeof("lxc.proc.") - 1;
1598 if (*subkey
== '\0')
1601 proclist
= malloc(sizeof(*proclist
));
1605 procelem
= malloc(sizeof(*procelem
));
1608 memset(procelem
, 0, sizeof(*procelem
));
1610 procelem
->filename
= strdup(subkey
);
1611 procelem
->value
= strdup(value
);
1613 if (!procelem
->filename
|| !procelem
->value
)
1616 proclist
->elem
= procelem
;
1618 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1625 free(procelem
->filename
);
1626 free(procelem
->value
);
1633 static int set_config_idmaps(const char *key
, const char *value
,
1634 struct lxc_conf
*lxc_conf
, void *data
)
1636 unsigned long hostid
, nsid
, range
;
1639 struct lxc_list
*idmaplist
= NULL
;
1640 struct id_map
*idmap
= NULL
;
1642 if (lxc_config_value_empty(value
))
1643 return lxc_clear_idmaps(lxc_conf
);
1645 idmaplist
= malloc(sizeof(*idmaplist
));
1649 idmap
= malloc(sizeof(*idmap
));
1652 memset(idmap
, 0, sizeof(*idmap
));
1654 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1656 ERROR("Failed to parse id mappings");
1660 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1662 idmap
->idtype
= ID_TYPE_UID
;
1663 else if (type
== 'g')
1664 idmap
->idtype
= ID_TYPE_GID
;
1668 idmap
->hostid
= hostid
;
1670 idmap
->range
= range
;
1671 idmaplist
->elem
= idmap
;
1672 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1674 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1675 if (idmap
->nsid
== 0)
1676 lxc_conf
->root_nsuid_map
= idmap
;
1679 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1680 if (idmap
->nsid
== 0)
1681 lxc_conf
->root_nsgid_map
= idmap
;
1694 static int set_config_mount_fstab(const char *key
, const char *value
,
1695 struct lxc_conf
*lxc_conf
, void *data
)
1697 if (lxc_config_value_empty(value
)) {
1698 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1702 return set_config_path_item(&lxc_conf
->fstab
, value
);
1705 static int set_config_mount_auto(const char *key
, const char *value
,
1706 struct lxc_conf
*lxc_conf
, void *data
)
1708 char *autos
, *token
;
1715 } allowed_auto_mounts
[] = {
1716 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1717 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1718 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1719 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1720 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1721 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1722 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1723 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1724 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1725 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1726 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1727 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1728 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1729 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1730 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1731 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1732 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1733 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1734 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1735 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1736 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1737 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
1738 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
1739 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
1740 /* For adding anything that is just a single on/off, but has no
1741 * options: keep mask and flag identical and just define the enum
1742 * value as an unused bit so far
1747 if (lxc_config_value_empty(value
)) {
1748 lxc_conf
->auto_mounts
= 0;
1752 autos
= strdup(value
);
1756 lxc_iterate_parts(token
, autos
, " \t") {
1757 bool is_shmounts
= false;
1759 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1760 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1763 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
1764 strncmp("shmounts:", token
, sizeof("shmounts:") - 1) == 0) {
1770 if (!allowed_auto_mounts
[i
].token
) {
1771 ERROR("Invalid filesystem to automount \"%s\"", token
);
1775 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1776 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1778 lxc_conf
->shmount
.path_host
= strdup(token
+ (sizeof("shmounts:") - 1));
1779 if (!lxc_conf
->shmount
.path_host
) {
1780 SYSERROR("Failed to copy shmounts host path");
1784 if (strcmp(lxc_conf
->shmount
.path_host
, "") == 0) {
1785 ERROR("Invalid shmounts path: empty");
1789 lxc_conf
->shmount
.path_cont
= strdup("/dev/.lxc-mounts");
1790 if(!lxc_conf
->shmount
.path_cont
) {
1791 SYSERROR("Failed to copy shmounts container path");
1805 static int set_config_mount(const char *key
, const char *value
,
1806 struct lxc_conf
*lxc_conf
, void *data
)
1809 struct lxc_list
*mntlist
;
1811 if (lxc_config_value_empty(value
))
1812 return lxc_clear_mount_entries(lxc_conf
);
1814 mntlist
= malloc(sizeof(*mntlist
));
1818 mntelem
= strdup(value
);
1823 mntlist
->elem
= mntelem
;
1825 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1830 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
1831 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
1834 static int set_config_cap_keep(const char *key
, const char *value
,
1835 struct lxc_conf
*lxc_conf
, void *data
)
1837 char *keepcaps
, *token
;
1838 struct lxc_list
*keeplist
;
1841 if (lxc_config_value_empty(value
))
1842 return lxc_clear_config_keepcaps(lxc_conf
);
1844 keepcaps
= strdup(value
);
1848 /* In case several capability keep is specified in a single line
1849 * split these caps in a single element for the list.
1851 lxc_iterate_parts(token
, keepcaps
, " \t") {
1852 if (!strcmp(token
, "none"))
1853 lxc_clear_config_keepcaps(lxc_conf
);
1855 keeplist
= malloc(sizeof(*keeplist
));
1859 keeplist
->elem
= strdup(token
);
1860 if (!keeplist
->elem
) {
1865 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1876 static int set_config_cap_drop(const char *key
, const char *value
,
1877 struct lxc_conf
*lxc_conf
, void *data
)
1879 char *dropcaps
, *token
;
1880 struct lxc_list
*droplist
;
1883 if (lxc_config_value_empty(value
))
1884 return lxc_clear_config_caps(lxc_conf
);
1886 dropcaps
= strdup(value
);
1890 /* In case several capability drop is specified in a single line
1891 * split these caps in a single element for the list.
1893 lxc_iterate_parts(token
, dropcaps
, " \t") {
1894 droplist
= malloc(sizeof(*droplist
));
1898 droplist
->elem
= strdup(token
);
1899 if (!droplist
->elem
) {
1904 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1915 static int set_config_console_path(const char *key
, const char *value
,
1916 struct lxc_conf
*lxc_conf
, void *data
)
1918 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1921 static int set_config_console_rotate(const char *key
, const char *value
,
1922 struct lxc_conf
*lxc_conf
, void *data
)
1924 if (lxc_config_value_empty(value
)) {
1925 lxc_conf
->console
.log_rotate
= 0;
1929 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1932 if (lxc_conf
->console
.log_rotate
> 1) {
1933 ERROR("The \"lxc.console.rotate\" config key can only be set "
1941 static int set_config_console_logfile(const char *key
, const char *value
,
1942 struct lxc_conf
*lxc_conf
, void *data
)
1944 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1947 static int set_config_console_buffer_size(const char *key
, const char *value
,
1948 struct lxc_conf
*lxc_conf
, void *data
)
1952 uint64_t buffer_size
, pgsz
;
1954 if (lxc_config_value_empty(value
)) {
1955 lxc_conf
->console
.buffer_size
= 0;
1959 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1960 if (!strcmp(value
, "auto")) {
1961 lxc_conf
->console
.buffer_size
= 1 << 17;
1965 ret
= parse_byte_size_string(value
, &size
);
1972 /* must be at least a page size */
1973 pgsz
= lxc_getpagesize();
1974 if ((uint64_t)size
< pgsz
) {
1975 NOTICE("Requested ringbuffer size for the console is %" PRId64
1976 " but must be at least %" PRId64
1977 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1982 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1983 if (buffer_size
== 0)
1986 if (buffer_size
!= size
)
1987 NOTICE("Passed size was not a power of 2. Rounding log size to "
1988 "next power of two: %" PRIu64
" bytes", buffer_size
);
1990 lxc_conf
->console
.buffer_size
= buffer_size
;
1994 static int set_config_console_size(const char *key
, const char *value
,
1995 struct lxc_conf
*lxc_conf
, void *data
)
1999 uint64_t log_size
, pgsz
;
2001 if (lxc_config_value_empty(value
)) {
2002 lxc_conf
->console
.log_size
= 0;
2006 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2007 if (!strcmp(value
, "auto")) {
2008 lxc_conf
->console
.log_size
= 1 << 17;
2012 ret
= parse_byte_size_string(value
, &size
);
2019 /* must be at least a page size */
2020 pgsz
= lxc_getpagesize();
2021 if ((uint64_t)size
< pgsz
) {
2022 NOTICE("Requested ringbuffer size for the console is %" PRId64
2023 " but must be at least %" PRId64
2024 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2029 log_size
= lxc_find_next_power2((uint64_t)size
);
2033 if (log_size
!= size
)
2034 NOTICE("Passed size was not a power of 2. Rounding log size to "
2035 "next power of two: %" PRIu64
" bytes", log_size
);
2037 lxc_conf
->console
.log_size
= log_size
;
2041 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2043 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
2045 update_hwaddr(line
);
2047 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2048 char *tmp
= realloc(conf
->unexpanded_config
,
2049 conf
->unexpanded_alloced
+ 1024);
2053 if (!conf
->unexpanded_config
)
2055 conf
->unexpanded_config
= tmp
;
2056 conf
->unexpanded_alloced
+= 1024;
2059 (void)strlcat(conf
->unexpanded_config
, line
, conf
->unexpanded_alloced
);
2060 conf
->unexpanded_len
+= linelen
;
2061 if (line
[linelen
- 1] != '\n') {
2062 (void)strlcat(conf
->unexpanded_config
, "\n", conf
->unexpanded_alloced
);
2063 conf
->unexpanded_len
++;
2069 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2071 struct dirent
*direntp
;
2073 char path
[MAXPATHLEN
];
2077 dir
= opendir(dirp
);
2081 while ((direntp
= readdir(dir
))) {
2084 fnam
= direntp
->d_name
;
2085 if (!strcmp(fnam
, "."))
2088 if (!strcmp(fnam
, ".."))
2092 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2095 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2096 if (len
< 0 || len
>= MAXPATHLEN
) {
2101 ret
= lxc_config_read(path
, lxc_conf
, true);
2113 static int set_config_includefiles(const char *key
, const char *value
,
2114 struct lxc_conf
*lxc_conf
, void *data
)
2116 if (lxc_config_value_empty(value
)) {
2117 clr_config_includefiles(key
, lxc_conf
, NULL
);
2122 return do_includedir(value
, lxc_conf
);
2124 return lxc_config_read(value
, lxc_conf
, true);
2127 static int set_config_rootfs_path(const char *key
, const char *value
,
2128 struct lxc_conf
*lxc_conf
, void *data
)
2132 const char *container_path
;
2134 if (lxc_config_value_empty(value
)) {
2135 free(lxc_conf
->rootfs
.path
);
2136 lxc_conf
->rootfs
.path
= NULL
;
2140 dup
= strdup(value
);
2144 /* Split <storage type>:<container path> into <storage type> and
2145 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2146 * "rootfs.path" to <container path>.
2148 tmp
= strchr(dup
, ':');
2151 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2157 container_path
= tmp
;
2159 container_path
= value
;
2162 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2167 static int set_config_rootfs_managed(const char *key
, const char *value
,
2168 struct lxc_conf
*lxc_conf
, void *data
)
2170 unsigned int val
= 0;
2172 if (lxc_config_value_empty(value
)) {
2173 lxc_conf
->rootfs
.managed
= true;
2177 if (lxc_safe_uint(value
, &val
) < 0)
2182 lxc_conf
->rootfs
.managed
= false;
2185 lxc_conf
->rootfs
.managed
= true;
2192 static int set_config_rootfs_mount(const char *key
, const char *value
,
2193 struct lxc_conf
*lxc_conf
, void *data
)
2195 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2198 static int set_config_rootfs_options(const char *key
, const char *value
,
2199 struct lxc_conf
*lxc_conf
, void *data
)
2202 unsigned long mflags
= 0, pflags
= 0;
2203 char *mdata
= NULL
, *opts
= NULL
;
2204 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2206 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2210 ret
= parse_propagationopts(value
, &pflags
);
2216 ret
= set_config_string_item(&opts
, value
);
2222 rootfs
->mountflags
= mflags
| pflags
;
2223 rootfs
->options
= opts
;
2224 rootfs
->data
= mdata
;
2229 static int set_config_uts_name(const char *key
, const char *value
,
2230 struct lxc_conf
*lxc_conf
, void *data
)
2232 struct utsname
*utsname
;
2234 if (lxc_config_value_empty(value
)) {
2235 clr_config_uts_name(key
, lxc_conf
, NULL
);
2239 utsname
= malloc(sizeof(*utsname
));
2243 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2248 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2249 free(lxc_conf
->utsname
);
2250 lxc_conf
->utsname
= utsname
;
2255 static int set_config_namespace_clone(const char *key
, const char *value
,
2256 struct lxc_conf
*lxc_conf
, void *data
)
2261 if (lxc_config_value_empty(value
))
2262 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2264 if (lxc_conf
->ns_keep
!= 0) {
2266 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2267 "\"lxc.namespace.keep\"");
2275 lxc_iterate_parts(token
, ns
, " \t") {
2276 token
+= lxc_char_left_gc(token
, strlen(token
));
2277 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2278 cloneflag
= lxc_namespace_2_cloneflag(token
);
2279 if (cloneflag
< 0) {
2283 lxc_conf
->ns_clone
|= cloneflag
;
2290 static int set_config_namespace_keep(const char *key
, const char *value
,
2291 struct lxc_conf
*lxc_conf
, void *data
)
2296 if (lxc_config_value_empty(value
))
2297 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2299 if (lxc_conf
->ns_clone
!= 0) {
2301 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2302 "\"lxc.namespace.keep\"");
2310 lxc_iterate_parts(token
, ns
, " \t") {
2311 token
+= lxc_char_left_gc(token
, strlen(token
));
2312 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2313 cloneflag
= lxc_namespace_2_cloneflag(token
);
2314 if (cloneflag
< 0) {
2318 lxc_conf
->ns_keep
|= cloneflag
;
2325 static int set_config_namespace_share(const char *key
, const char *value
,
2326 struct lxc_conf
*lxc_conf
, void *data
)
2329 const char *namespace;
2331 if (lxc_config_value_empty(value
))
2332 return clr_config_namespace_share(key
, lxc_conf
, data
);
2334 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
2335 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2339 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2342 struct parse_line_conf
{
2343 struct lxc_conf
*conf
;
2347 static int parse_line(char *buffer
, void *data
)
2349 char *dot
, *key
, *line
, *linep
, *value
;
2351 struct lxc_config_t
*config
;
2354 struct parse_line_conf
*plc
= data
;
2356 /* If there are newlines in the config file we should keep them. */
2357 empty_line
= lxc_is_line_empty(dup
);
2361 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2362 * modified the original string on the stack by replacing '=' by '\0'
2365 linep
= line
= strdup(dup
);
2369 if (!plc
->from_include
) {
2370 ret
= append_unexp_config_line(line
, plc
->conf
);
2378 line
+= lxc_char_left_gc(line
, strlen(line
));
2380 /* ignore comments */
2384 /* martian option - don't add it to the config itself */
2385 if (strncmp(line
, "lxc.", 4))
2390 dot
= strchr(line
, '=');
2392 ERROR("Invalid configuration line: %s", line
);
2400 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2402 value
+= lxc_char_left_gc(value
, strlen(value
));
2403 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2405 if (*value
== '\'' || *value
== '\"') {
2408 len
= strlen(value
);
2409 if (len
> 1 && value
[len
- 1] == *value
) {
2410 value
[len
- 1] = '\0';
2415 config
= lxc_get_config(key
);
2417 ERROR("Unknown configuration key \"%s\"", key
);
2421 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2428 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2430 char *dot
, *key
, *line
, *linep
, *value
;
2433 struct new_config_item
*new = NULL
;
2435 linep
= line
= strdup(dup
);
2439 line
+= lxc_char_left_gc(line
, strlen(line
));
2441 /* martian option - don't add it to the config itself */
2442 if (strncmp(line
, "lxc.", 4))
2446 dot
= strchr(line
, '=');
2448 ERROR("Invalid configuration item: %s", line
);
2456 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2458 value
+= lxc_char_left_gc(value
, strlen(value
));
2459 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2461 if (*value
== '\'' || *value
== '\"') {
2464 len
= strlen(value
);
2465 if (len
> 1 && value
[len
- 1] == *value
) {
2466 value
[len
- 1] = '\0';
2472 new = malloc(sizeof(struct new_config_item
));
2476 new->key
= strdup(key
);
2477 new->val
= strdup(value
);
2478 if (!new->val
|| !new->key
)
2486 if (ret
< 0 && new) {
2496 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2499 struct parse_line_conf c
;
2502 c
.from_include
= from_include
;
2504 ret
= access(file
, R_OK
);
2508 /* Catch only the top level config file name in the structure. */
2510 conf
->rcfile
= strdup(file
);
2512 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2515 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2517 struct lxc_list
*dent
;
2519 dent
= malloc(sizeof(struct lxc_list
));
2523 dent
->elem
= parse_new_conf_line(arg
);
2529 lxc_list_add_tail(defines
, dent
);
2533 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2535 struct lxc_list
*it
;
2538 lxc_list_for_each(it
, defines
) {
2539 struct new_config_item
*new_item
= it
->elem
;
2540 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2545 lxc_config_define_free(defines
);
2549 void lxc_config_define_free(struct lxc_list
*defines
)
2551 struct lxc_list
*it
, *next
;
2553 lxc_list_for_each_safe(it
, defines
, next
) {
2554 struct new_config_item
*new_item
= it
->elem
;
2555 free(new_item
->key
);
2556 free(new_item
->val
);
2562 signed long lxc_config_parse_arch(const char *arch
)
2564 #if HAVE_SYS_PERSONALITY_H
2570 { "arm", PER_LINUX32
},
2571 { "armel", PER_LINUX32
},
2572 { "armhf", PER_LINUX32
},
2573 { "armv7l", PER_LINUX32
},
2574 { "athlon", PER_LINUX32
},
2575 { "i386", PER_LINUX32
},
2576 { "i486", PER_LINUX32
},
2577 { "i586", PER_LINUX32
},
2578 { "i686", PER_LINUX32
},
2579 { "linux32", PER_LINUX32
},
2580 { "mips", PER_LINUX32
},
2581 { "mipsel", PER_LINUX32
},
2582 { "ppc", PER_LINUX32
},
2583 { "powerpc", PER_LINUX32
},
2584 { "x86", PER_LINUX32
},
2585 { "amd64", PER_LINUX
},
2586 { "arm64", PER_LINUX
},
2587 { "linux64", PER_LINUX
},
2588 { "mips64", PER_LINUX
},
2589 { "mips64el", PER_LINUX
},
2590 { "ppc64", PER_LINUX
},
2591 { "ppc64el", PER_LINUX
},
2592 { "ppc64le", PER_LINUX
},
2593 { "powerpc64", PER_LINUX
},
2594 { "s390x", PER_LINUX
},
2595 { "x86_64", PER_LINUX
},
2597 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2599 for (i
= 0; i
< len
; i
++)
2600 if (!strcmp(pername
[i
].name
, arch
))
2601 return pername
[i
].per
;
2607 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2615 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2616 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2617 { "LSM", LXC_ATTACH_LSM_EXEC
},
2622 /* For the sake of backward compatibility, drop all privileges
2623 * if none is specified.
2625 for (i
= 0; all_privs
[i
].token
; i
++)
2626 *flags
|= all_privs
[i
].flag
;
2631 lxc_iterate_parts(token
, flaglist
, "|") {
2634 for (i
= 0; all_privs
[i
].token
; i
++)
2635 if (!strcmp(all_privs
[i
].token
, token
))
2636 aflag
= all_privs
[i
].flag
;
2647 /* Write out a configuration file. */
2648 int write_config(int fd
, const struct lxc_conf
*conf
)
2651 size_t len
= conf
->unexpanded_len
;
2656 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
2658 SYSERROR("Failed to write configuration file");
2665 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2672 len
= strlen(key
) + strlen(v
) + 4;
2675 if (lxc_config_value_empty(v
))
2676 ret
= snprintf(tmp
, len
, "%s =", key
);
2678 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2679 if (ret
< 0 || ret
>= len
)
2682 /* Save the line verbatim into unexpanded_conf */
2683 if (append_unexp_config_line(tmp
, conf
))
2689 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2693 char *lstart
= conf
->unexpanded_config
;
2695 if (!conf
->unexpanded_config
)
2699 lend
= strchr(lstart
, '\n');
2702 lend
= lstart
+ strlen(lstart
);
2705 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2710 v
= lstart
[strlen(key
)];
2711 if (!isspace(v
) && v
!= '=') {
2716 conf
->unexpanded_len
-= (lend
- lstart
);
2717 if (*lend
== '\0') {
2721 memmove(lstart
, lend
, strlen(lend
) + 1);
2725 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2726 const char *newpath
, const char *oldname
,
2727 const char *newname
, const char *ovldir
)
2730 char *lend
, *newdir
, *olddir
, *p
, *q
;
2731 size_t newdirlen
, olddirlen
;
2732 char *lstart
= conf
->unexpanded_config
;
2733 const char *key
= "lxc.mount.entry";
2735 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2736 olddir
= alloca(olddirlen
+ 1);
2737 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2739 if (ret
< 0 || ret
>= olddirlen
+ 1)
2742 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2743 newdir
= alloca(newdirlen
+ 1);
2744 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2746 if (ret
< 0 || ret
>= newdirlen
+ 1)
2749 if (!conf
->unexpanded_config
)
2753 lend
= strchr(lstart
, '\n');
2755 lend
= lstart
+ strlen(lstart
);
2759 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2762 p
= strchr(lstart
+ strlen(key
), '=');
2773 /* Whenever a lxc.mount.entry entry is found in a line we check
2774 * if the substring "overlay" is present before doing any
2775 * further work. We check for "overlay" because substrings need
2776 * to have at least one space before them in a valid overlay
2777 * lxc.mount.entry (/A B overlay). When the space before is
2778 * missing it is very likely that these substrings are part of a
2779 * path or something else. (Checking q >= lend ensures that we
2780 * only count matches in the current line.) */
2781 q
= strstr(p
, " overlay");
2782 if (!q
|| q
>= lend
)
2785 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2788 /* replace the olddir with newdir */
2789 if (olddirlen
>= newdirlen
) {
2790 size_t diff
= olddirlen
- newdirlen
;
2791 memcpy(q
, newdir
, newdirlen
);
2792 if (olddirlen
!= newdirlen
) {
2793 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2794 strlen(q
) - newdirlen
- diff
+ 1);
2796 conf
->unexpanded_len
-= diff
;
2800 size_t diff
= newdirlen
- olddirlen
;
2801 size_t oldlen
= conf
->unexpanded_len
;
2802 size_t newlen
= oldlen
+ diff
;
2803 size_t poffset
= q
- conf
->unexpanded_config
;
2805 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2809 conf
->unexpanded_len
= newlen
;
2810 conf
->unexpanded_alloced
= newlen
+ 1;
2811 new[newlen
- 1] = '\0';
2812 lend
= new + (lend
- conf
->unexpanded_config
);
2813 /* Move over the remainder to make room for the newdir.
2815 memmove(new + poffset
+ newdirlen
,
2816 new + poffset
+ olddirlen
,
2817 oldlen
- poffset
- olddirlen
+ 1);
2818 conf
->unexpanded_config
= new;
2819 memcpy(new + poffset
, newdir
, newdirlen
);
2829 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2830 const char *newpath
, const char *oldname
,
2831 const char *newname
)
2834 char *lend
, *newdir
, *olddir
, *p
;
2835 char *lstart
= conf
->unexpanded_config
;
2836 size_t newdirlen
, olddirlen
;
2837 const char *key
= "lxc.hook";
2839 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2840 olddir
= alloca(olddirlen
+ 1);
2841 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2842 if (ret
< 0 || ret
>= olddirlen
+ 1)
2845 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2846 newdir
= alloca(newdirlen
+ 1);
2847 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2848 if (ret
< 0 || ret
>= newdirlen
+ 1)
2851 if (!conf
->unexpanded_config
)
2855 lend
= strchr(lstart
, '\n');
2857 lend
= lstart
+ strlen(lstart
);
2861 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2864 p
= strchr(lstart
+ strlen(key
), '=');
2875 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2878 /* replace the olddir with newdir */
2879 if (olddirlen
>= newdirlen
) {
2880 size_t diff
= olddirlen
- newdirlen
;
2881 memcpy(p
, newdir
, newdirlen
);
2882 if (olddirlen
!= newdirlen
) {
2883 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2884 strlen(p
) - newdirlen
- diff
+ 1);
2886 conf
->unexpanded_len
-= diff
;
2890 size_t diff
= newdirlen
- olddirlen
;
2891 size_t oldlen
= conf
->unexpanded_len
;
2892 size_t newlen
= oldlen
+ diff
;
2893 size_t poffset
= p
- conf
->unexpanded_config
;
2895 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2899 conf
->unexpanded_len
= newlen
;
2900 conf
->unexpanded_alloced
= newlen
+ 1;
2901 new[newlen
- 1] = '\0';
2902 lend
= new + (lend
- conf
->unexpanded_config
);
2903 /* Move over the remainder to make room for the newdir.
2905 memmove(new + poffset
+ newdirlen
,
2906 new + poffset
+ olddirlen
,
2907 oldlen
- poffset
- olddirlen
+ 1);
2908 conf
->unexpanded_config
= new;
2909 memcpy(new + poffset
, newdir
, newdirlen
);
2922 ERROR("Error writing to new config"); \
2927 /* This is called only from clone. We wish to update all hwaddrs in the
2928 * unexpanded config file. We can't/don't want to update any which come from
2929 * lxc.includes (there shouldn't be any).
2930 * We can't just walk the c->lxc-conf->network list because that includes netifs
2931 * from the include files. So we update the ones which we find in the unexp
2932 * config file, then find the original macaddr in the conf->network, and update
2933 * that to the same value.
2935 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2937 char *lend
, *p
, *p2
;
2938 struct lxc_list
*it
;
2939 char *lstart
= conf
->unexpanded_config
;
2941 if (!conf
->unexpanded_config
)
2945 char newhwaddr
[18], oldhwaddr
[17];
2947 lend
= strchr(lstart
, '\n');
2949 lend
= lstart
+ strlen(lstart
);
2953 if (!lxc_config_net_hwaddr(lstart
)) {
2958 p
= strchr(lstart
, '=');
2971 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2974 if ((p2
- p
) != 17) {
2975 WARN("Bad hwaddr entry");
2980 memcpy(oldhwaddr
, p
, 17);
2982 if (!new_hwaddr(newhwaddr
))
2985 memcpy(p
, newhwaddr
, 17);
2986 lxc_list_for_each(it
, &conf
->network
) {
2987 struct lxc_netdev
*n
= it
->elem
;
2989 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2990 memcpy(n
->hwaddr
, newhwaddr
, 17);
2999 static int set_config_ephemeral(const char *key
, const char *value
,
3000 struct lxc_conf
*lxc_conf
, void *data
)
3002 if (lxc_config_value_empty(value
)) {
3003 lxc_conf
->ephemeral
= 0;
3007 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3010 if (lxc_conf
->ephemeral
> 1)
3016 static int set_config_log_syslog(const char *key
, const char *value
,
3017 struct lxc_conf
*lxc_conf
, void *data
)
3021 if (lxc_conf
->syslog
) {
3022 free(lxc_conf
->syslog
);
3023 lxc_conf
->syslog
= NULL
;
3026 if (lxc_config_value_empty(value
))
3029 facility
= lxc_syslog_priority_to_int(value
);
3030 if (facility
== -EINVAL
)
3033 lxc_log_syslog(facility
);
3034 return set_config_string_item(&lxc_conf
->syslog
, value
);
3037 static int set_config_no_new_privs(const char *key
, const char *value
,
3038 struct lxc_conf
*lxc_conf
, void *data
)
3042 if (lxc_config_value_empty(value
)) {
3043 lxc_conf
->no_new_privs
= false;
3047 if (lxc_safe_uint(value
, &v
) < 0)
3053 lxc_conf
->no_new_privs
= v
? true : false;
3058 /* Callbacks to get configuration items. */
3059 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3060 struct lxc_conf
*c
, void *data
)
3067 memset(retv
, 0, inlen
);
3069 #if HAVE_SYS_PERSONALITY_H
3072 switch (c
->personality
) {
3074 strprint(retv
, inlen
, "i686");
3077 strprint(retv
, inlen
, "x86_64");
3087 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3088 struct lxc_conf
*c
, void *data
)
3090 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3093 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3094 struct lxc_conf
*c
, void *data
)
3096 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3099 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3100 struct lxc_conf
*c
, void *data
)
3102 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3105 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3106 struct lxc_conf
*c
, void *data
)
3108 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3111 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3112 int inlen
, struct lxc_conf
*c
,
3115 return lxc_get_conf_int(c
, retv
, inlen
,
3116 c
->lsm_aa_allow_incomplete
);
3119 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3120 int inlen
, struct lxc_conf
*c
,
3123 return lxc_get_conf_int(c
, retv
, inlen
,
3124 c
->lsm_aa_allow_nesting
);
3127 static int get_config_apparmor_raw(const char *key
, char *retv
,
3128 int inlen
, struct lxc_conf
*c
,
3132 struct lxc_list
*it
;
3138 memset(retv
, 0, inlen
);
3140 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3141 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3147 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3148 struct lxc_conf
*c
, void *data
)
3150 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3153 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3154 * just the value(s) will be printed. Since there still could be more than one,
3155 * it is newline-separated.
3156 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
3158 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3159 * 'lxc.cgroup.subsystem.key = value' format.
3161 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3162 int inlen
, struct lxc_conf
*c
,
3166 size_t namespaced_token_len
;
3167 char *global_token
, *namespaced_token
;
3168 struct lxc_list
*it
;
3170 bool get_all
= false;
3175 memset(retv
, 0, inlen
);
3177 if (version
== CGROUP2_SUPER_MAGIC
) {
3178 global_token
= "lxc.cgroup2";
3179 namespaced_token
= "lxc.cgroup2.";
3180 namespaced_token_len
= sizeof("lxc.cgroup2.") - 1;;
3181 } else if (version
== CGROUP_SUPER_MAGIC
) {
3182 global_token
= "lxc.cgroup";
3183 namespaced_token
= "lxc.cgroup.";
3184 namespaced_token_len
= sizeof("lxc.cgroup.") - 1;;
3189 if (strcmp(key
, global_token
) == 0)
3191 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3192 key
+= namespaced_token_len
;
3196 lxc_list_for_each(it
, &c
->cgroup
) {
3197 struct lxc_cgroup
*cg
= it
->elem
;
3200 if (version
!= cg
->version
)
3203 strprint(retv
, inlen
, "%s.%s = %s\n",
3204 global_token
, cg
->subsystem
, cg
->value
);
3205 } else if (!strcmp(cg
->subsystem
, key
)) {
3206 strprint(retv
, inlen
, "%s\n", cg
->value
);
3213 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3214 struct lxc_conf
*c
, void *data
)
3216 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3217 CGROUP_SUPER_MAGIC
);
3220 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3221 struct lxc_conf
*c
, void *data
)
3223 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3224 CGROUP2_SUPER_MAGIC
);
3227 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3228 struct lxc_conf
*lxc_conf
, void *data
)
3236 memset(retv
, 0, inlen
);
3238 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3243 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3244 int inlen
, struct lxc_conf
*lxc_conf
,
3247 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3248 lxc_conf
->cgroup_meta
.relative
);
3251 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3252 struct lxc_conf
*c
, void *data
)
3254 struct lxc_list
*it
;
3255 int len
, listlen
, ret
;
3257 /* "u 1000 1000000 65536"
3259 * let's render this as
3277 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3278 char buf
[__LXC_IDMAP_STR_BUF
];
3283 memset(retv
, 0, inlen
);
3285 listlen
= lxc_list_len(&c
->id_map
);
3286 lxc_list_for_each(it
, &c
->id_map
) {
3287 struct id_map
*map
= it
->elem
;
3288 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3289 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3290 map
->nsid
, map
->hostid
, map
->range
);
3291 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3294 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3299 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3300 struct lxc_conf
*c
, void *data
)
3303 v
= lxc_log_priority_to_string(c
->loglevel
);
3304 return lxc_get_conf_str(retv
, inlen
, v
);
3307 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3308 struct lxc_conf
*c
, void *data
)
3310 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3313 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3314 struct lxc_conf
*c
, void *data
)
3316 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3319 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3320 struct lxc_conf
*c
, void *data
)
3322 int len
, fulllen
= 0;
3323 const char *sep
= "";
3328 memset(retv
, 0, inlen
);
3330 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3333 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3334 case LXC_AUTO_PROC_MIXED
:
3335 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3338 case LXC_AUTO_PROC_RW
:
3339 strprint(retv
, inlen
, "%sproc:rw", sep
);
3346 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3347 case LXC_AUTO_SYS_RO
:
3348 strprint(retv
, inlen
, "%ssys:ro", sep
);
3351 case LXC_AUTO_SYS_RW
:
3352 strprint(retv
, inlen
, "%ssys:rw", sep
);
3355 case LXC_AUTO_SYS_MIXED
:
3356 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3363 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3364 case LXC_AUTO_CGROUP_NOSPEC
:
3365 strprint(retv
, inlen
, "%scgroup", sep
);
3367 case LXC_AUTO_CGROUP_MIXED
:
3368 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3370 case LXC_AUTO_CGROUP_RO
:
3371 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3373 case LXC_AUTO_CGROUP_RW
:
3374 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3376 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3377 strprint(retv
, inlen
, "%scgroup-full", sep
);
3379 case LXC_AUTO_CGROUP_FULL_MIXED
:
3380 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3382 case LXC_AUTO_CGROUP_FULL_RO
:
3383 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3385 case LXC_AUTO_CGROUP_FULL_RW
:
3386 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3395 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3396 struct lxc_conf
*c
, void *data
)
3398 int len
, fulllen
= 0;
3399 struct lxc_list
*it
;
3404 memset(retv
, 0, inlen
);
3406 lxc_list_for_each(it
, &c
->mount_list
) {
3407 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3413 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3414 struct lxc_conf
*c
, void *data
)
3416 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3419 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3420 struct lxc_conf
*c
, void *data
)
3422 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3425 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3426 struct lxc_conf
*c
, void *data
)
3428 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3431 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3432 struct lxc_conf
*c
, void *data
)
3434 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3437 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3438 struct lxc_conf
*c
, void *data
)
3440 return lxc_get_conf_str(
3442 c
->utsname
? c
->utsname
->nodename
: NULL
);
3445 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3446 struct lxc_conf
*c
, void *data
)
3449 int len
, fulllen
= 0, found
= -1;
3450 struct lxc_list
*it
;
3453 subkey
= strchr(key
, '.');
3455 subkey
= strchr(subkey
+ 1, '.');
3461 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3462 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3473 memset(retv
, 0, inlen
);
3475 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3476 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3481 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3482 struct lxc_conf
*c
, void *data
)
3484 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3487 static int get_config_net(const char *key
, char *retv
, int inlen
,
3488 struct lxc_conf
*c
, void *data
)
3490 int len
, fulllen
= 0;
3491 struct lxc_list
*it
;
3496 memset(retv
, 0, inlen
);
3498 lxc_list_for_each(it
, &c
->network
) {
3499 struct lxc_netdev
*n
= it
->elem
;
3500 const char *t
= lxc_net_type_to_str(n
->type
);
3501 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3507 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3508 struct lxc_conf
*c
, void *data
)
3510 int len
, fulllen
= 0;
3511 struct lxc_list
*it
;
3516 memset(retv
, 0, inlen
);
3518 lxc_list_for_each(it
, &c
->caps
) {
3519 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3525 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3526 struct lxc_conf
*c
, void *data
)
3528 int len
, fulllen
= 0;
3529 struct lxc_list
*it
;
3534 memset(retv
, 0, inlen
);
3536 lxc_list_for_each(it
, &c
->keepcaps
) {
3537 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3543 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3544 struct lxc_conf
*c
, void *data
)
3546 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3549 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3550 struct lxc_conf
*c
, void *data
)
3552 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3555 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3556 struct lxc_conf
*c
, void *data
)
3558 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3562 static int get_config_console_buffer_size(const char *key
, char *retv
,
3563 int inlen
, struct lxc_conf
*c
,
3566 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3569 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3570 struct lxc_conf
*c
, void *data
)
3572 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3576 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3577 struct lxc_conf
*c
, void *data
)
3579 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3582 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3583 struct lxc_conf
*c
, void *data
)
3585 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3588 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3589 struct lxc_conf
*c
, void *data
)
3591 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3594 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3595 struct lxc_conf
*c
, void *data
)
3597 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3600 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3601 struct lxc_conf
*c
, void *data
)
3603 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3606 static int get_config_start(const char *key
, char *retv
, int inlen
,
3607 struct lxc_conf
*c
, void *data
)
3609 if (strcmp(key
+ 10, "auto") == 0)
3610 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3611 else if (strcmp(key
+ 10, "delay") == 0)
3612 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3613 else if (strcmp(key
+ 10, "order") == 0)
3614 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3619 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3620 struct lxc_conf
*c
, void *data
)
3622 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3625 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3626 struct lxc_conf
*c
, void *data
)
3628 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3631 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
3632 int inlen
, struct lxc_conf
*c
,
3635 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
3638 static int get_config_group(const char *key
, char *retv
, int inlen
,
3639 struct lxc_conf
*c
, void *data
)
3641 int len
, fulllen
= 0;
3642 struct lxc_list
*it
;
3647 memset(retv
, 0, inlen
);
3649 lxc_list_for_each(it
, &c
->groups
) {
3650 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3656 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3657 struct lxc_conf
*c
, void *data
)
3659 int len
, fulllen
= 0;
3660 struct lxc_list
*it
;
3665 memset(retv
, 0, inlen
);
3667 lxc_list_for_each(it
, &c
->environment
) {
3668 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3674 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3675 struct lxc_conf
*c
, void *data
)
3677 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3680 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3681 struct lxc_conf
*c
, void *data
)
3683 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3686 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3687 struct lxc_conf
*c
, void *data
)
3689 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3692 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3693 struct lxc_conf
*c
, void *data
)
3695 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3698 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3699 struct lxc_conf
*c
, void *data
)
3701 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3704 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3705 struct lxc_conf
*c
, void *data
)
3707 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3710 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3711 struct lxc_conf
*c
, void *data
)
3713 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3716 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3717 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3718 * printed, in 'lxc.prlimit.resource = value' format.
3720 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3721 struct lxc_conf
*c
, void *data
)
3723 int fulllen
= 0, len
;
3724 bool get_all
= false;
3725 struct lxc_list
*it
;
3730 memset(retv
, 0, inlen
);
3732 if (!strcmp(key
, "lxc.prlimit"))
3734 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3739 lxc_list_for_each(it
, &c
->limits
) {
3740 /* 2 colon separated 64 bit integers or the word 'unlimited' */
3741 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
3743 struct lxc_limit
*lim
= it
->elem
;
3745 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3746 memcpy(buf
, "unlimited", sizeof("unlimited"));
3747 partlen
= sizeof("unlimited") - 1;
3749 partlen
= sprintf(buf
, "%" PRIu64
,
3750 (uint64_t)lim
->limit
.rlim_cur
);
3752 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3753 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3754 memcpy(buf
+ partlen
, ":unlimited",
3755 sizeof(":unlimited"));
3757 sprintf(buf
+ partlen
, ":%" PRIu64
,
3758 (uint64_t)lim
->limit
.rlim_max
);
3762 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3763 lim
->resource
, buf
);
3764 } else if (!strcmp(lim
->resource
, key
)) {
3765 strprint(retv
, inlen
, "%s", buf
);
3772 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3773 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3774 * entries will be printed, in 'lxc.sysctl.key = value' format.
3776 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3777 struct lxc_conf
*c
, void *data
)
3780 struct lxc_list
*it
;
3782 bool get_all
= false;
3787 memset(retv
, 0, inlen
);
3789 if (strcmp(key
, "lxc.sysctl") == 0)
3791 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3792 key
+= sizeof("lxc.sysctl.") - 1;
3796 lxc_list_for_each(it
, &c
->sysctls
) {
3797 struct lxc_sysctl
*elem
= it
->elem
;
3799 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3800 elem
->key
, elem
->value
);
3801 } else if (strcmp(elem
->key
, key
) == 0) {
3802 strprint(retv
, inlen
, "%s", elem
->value
);
3809 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3810 struct lxc_conf
*c
, void *data
)
3812 struct lxc_list
*it
;
3815 bool get_all
= false;
3820 memset(retv
, 0, inlen
);
3822 if (strcmp(key
, "lxc.proc") == 0)
3824 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3825 key
+= sizeof("lxc.proc.") - 1;
3829 lxc_list_for_each(it
, &c
->procs
) {
3830 struct lxc_proc
*proc
= it
->elem
;
3833 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3834 proc
->filename
, proc
->value
);
3835 } else if (strcmp(proc
->filename
, key
) == 0) {
3836 strprint(retv
, inlen
, "%s", proc
->value
);
3843 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3844 struct lxc_conf
*c
, void *data
)
3852 memset(retv
, 0, inlen
);
3854 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3855 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3856 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3862 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3863 struct lxc_conf
*c
, void *data
)
3871 memset(retv
, 0, inlen
);
3873 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3874 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3875 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3881 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3882 struct lxc_conf
*c
, void *data
)
3885 const char *namespace;
3891 memset(retv
, 0, inlen
);
3893 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
3894 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3898 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3903 /* Callbacks to clear config items. */
3904 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3907 c
->personality
= -1;
3911 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3918 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3925 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3933 static inline int clr_config_apparmor_profile(const char *key
,
3934 struct lxc_conf
*c
, void *data
)
3936 free(c
->lsm_aa_profile
);
3937 c
->lsm_aa_profile
= NULL
;
3941 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3945 c
->lsm_aa_allow_incomplete
= 0;
3949 static inline int clr_config_apparmor_allow_nesting(const char *key
,
3953 c
->lsm_aa_allow_nesting
= 0;
3957 static inline int clr_config_apparmor_raw(const char *key
,
3961 return lxc_clear_apparmor_raw(c
);
3964 static inline int clr_config_selinux_context(const char *key
,
3965 struct lxc_conf
*c
, void *data
)
3967 free(c
->lsm_se_context
);
3968 c
->lsm_se_context
= NULL
;
3972 static inline int clr_config_cgroup_controller(const char *key
,
3973 struct lxc_conf
*c
, void *data
)
3975 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3978 static inline int clr_config_cgroup2_controller(const char *key
,
3979 struct lxc_conf
*c
, void *data
)
3981 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3984 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3987 if (lxc_conf
->cgroup_meta
.dir
) {
3988 free(lxc_conf
->cgroup_meta
.dir
);
3989 lxc_conf
->cgroup_meta
.dir
= NULL
;
3995 static inline int clr_config_cgroup_relative(const char *key
,
3996 struct lxc_conf
*lxc_conf
,
3999 lxc_conf
->cgroup_meta
.relative
= false;
4003 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4006 return lxc_clear_idmaps(c
);
4009 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4012 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4016 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4024 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4027 return lxc_clear_mount_entries(c
);
4030 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4033 return lxc_clear_automounts(c
);
4036 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4044 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4047 free(c
->rootfs
.path
);
4048 c
->rootfs
.path
= NULL
;
4052 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4055 c
->rootfs
.managed
= true;
4059 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4062 free(c
->rootfs
.mount
);
4063 c
->rootfs
.mount
= NULL
;
4067 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4070 free(c
->rootfs
.options
);
4071 c
->rootfs
.options
= NULL
;
4073 free(c
->rootfs
.data
);
4074 c
->rootfs
.data
= NULL
;
4079 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4087 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4090 return lxc_clear_hooks(c
, key
);
4093 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4096 /* default to legacy hooks version */
4097 c
->hooks_version
= 0;
4101 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4104 lxc_free_networks(&c
->network
);
4109 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4112 return lxc_clear_config_caps(c
);
4115 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4118 return lxc_clear_config_keepcaps(c
);
4121 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4124 free(c
->console
.path
);
4125 c
->console
.path
= NULL
;
4129 static inline int clr_config_console_logfile(const char *key
,
4130 struct lxc_conf
*c
, void *data
)
4132 free(c
->console
.log_path
);
4133 c
->console
.log_path
= NULL
;
4137 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4140 c
->console
.log_rotate
= 0;
4144 static inline int clr_config_console_buffer_size(const char *key
,
4145 struct lxc_conf
*c
, void *data
)
4147 c
->console
.buffer_size
= 0;
4151 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4154 c
->console
.log_size
= 0;
4158 static inline int clr_config_seccomp_profile(const char *key
,
4159 struct lxc_conf
*c
, void *data
)
4166 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4173 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4180 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4183 c
->rebootsignal
= 0;
4187 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4194 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4197 if (strcmp(key
+ 10, "auto") == 0)
4199 else if (strcmp(key
+ 10, "delay") == 0)
4201 else if (strcmp(key
+ 10, "order") == 0)
4207 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4215 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4218 c
->monitor_unshare
= 0;
4222 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4223 struct lxc_conf
*c
, void *data
)
4225 c
->monitor_signal_pdeath
= 0;
4229 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4232 return lxc_clear_groups(c
);
4235 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4238 return lxc_clear_environment(c
);
4241 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4244 free(c
->execute_cmd
);
4245 c
->execute_cmd
= NULL
;
4249 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4257 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4265 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4272 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4279 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4286 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4289 c
->no_new_privs
= false;
4293 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4296 return lxc_clear_limits(c
, key
);
4299 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4302 return lxc_clear_sysctls(c
, key
);
4305 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4308 return lxc_clear_procs(c
, key
);
4311 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4314 lxc_clear_includes(c
);
4318 static int clr_config_namespace_clone(const char *key
,
4319 struct lxc_conf
*lxc_conf
, void *data
)
4321 lxc_conf
->ns_clone
= 0;
4325 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4328 lxc_conf
->ns_keep
= 0;
4332 static int clr_config_namespace_share(const char *key
,
4333 struct lxc_conf
*lxc_conf
, void *data
)
4336 const char *namespace;
4338 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
4339 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4343 free(lxc_conf
->ns_share
[ns_idx
]);
4344 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4349 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4350 struct lxc_conf
*c
, void *data
)
4355 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4356 struct lxc_conf
*lxc_conf
,
4358 char **deindexed_key
)
4361 unsigned int tmpidx
;
4363 char *copy
, *idx_start
, *idx_end
;
4364 struct lxc_config_t
*config
= NULL
;
4366 /* check that this is a sensible network key */
4367 if (strncmp("lxc.net.", key
, 8)) {
4368 ERROR("Invalid network configuration key \"%s\"", key
);
4374 ERROR("Failed to duplicate string \"%s\"", key
);
4379 if (!isdigit(*(key
+ 8))) {
4380 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4384 /* beginning of index string */
4385 idx_start
= (copy
+ 7);
4388 /* end of index string */
4389 idx_end
= strchr((copy
+ 8), '.');
4393 /* parse current index */
4394 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4397 SYSERROR("Failed to parse usigned integer from string \"%s\"",
4403 /* This, of course is utterly nonsensical on so many levels, but
4404 * better safe than sorry.
4405 * (Checking for INT_MAX here is intentional.)
4407 if (tmpidx
== INT_MAX
) {
4408 SYSERROR("Number of configured networks would overflow the "
4414 numstrlen
= strlen((idx_start
+ 1));
4416 /* repair configuration key */
4419 /* lxc.net.<idx>.<subkey> */
4422 if (strlen(idx_end
+ 1) == 0) {
4423 ERROR("No subkey in network configuration key \"%s\"", key
);
4427 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4428 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4430 config
= lxc_get_config(copy
);
4432 ERROR("Unknown network configuration key \"%s\"", key
);
4438 *deindexed_key
= copy
;
4447 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4448 * found. So we make sure next comes an integer, find the right callback (by
4449 * rewriting the key), and call it.
4451 static int set_config_net_nic(const char *key
, const char *value
,
4452 struct lxc_conf
*lxc_conf
, void *data
)
4455 const char *idxstring
;
4456 struct lxc_config_t
*config
;
4457 struct lxc_netdev
*netdev
;
4459 char *deindexed_key
= NULL
;
4461 idxstring
= key
+ 8;
4462 if (!isdigit(*idxstring
))
4465 if (lxc_config_value_empty(value
))
4466 return clr_config_net_nic(key
, lxc_conf
, data
);
4468 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4469 if (!config
|| idx
< 0)
4472 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4474 free(deindexed_key
);
4478 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4479 free(deindexed_key
);
4483 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4487 const char *idxstring
;
4488 struct lxc_config_t
*config
;
4489 struct lxc_netdev
*netdev
;
4491 char *deindexed_key
= NULL
;
4493 idxstring
= key
+ 8;
4494 if (!isdigit(*idxstring
))
4497 /* The left conjunct is pretty self-explanatory. The right conjunct
4498 * checks whether the two pointers are equal. If they are we know that
4499 * this is not a key that is namespaced any further and so we are
4500 * supposed to clear the whole network.
4502 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4503 unsigned int rmnetdevidx
;
4505 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4508 /* Remove network from network list. */
4509 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4513 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4514 if (!config
|| idx
< 0)
4517 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4519 free(deindexed_key
);
4523 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4524 free(deindexed_key
);
4528 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4531 struct lxc_netdev
*netdev
= data
;
4541 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4544 struct lxc_netdev
*netdev
= data
;
4549 netdev
->name
[0] = '\0';
4554 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4557 struct lxc_netdev
*netdev
= data
;
4567 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4570 struct lxc_netdev
*netdev
= data
;
4575 netdev
->link
[0] = '\0';
4580 static int clr_config_net_macvlan_mode(const char *key
,
4581 struct lxc_conf
*lxc_conf
, void *data
)
4583 struct lxc_netdev
*netdev
= data
;
4588 if (netdev
->type
!= LXC_NET_MACVLAN
)
4591 netdev
->priv
.macvlan_attr
.mode
= -1;
4596 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4599 struct lxc_netdev
*netdev
= data
;
4604 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4609 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4612 struct lxc_netdev
*netdev
= data
;
4617 free(netdev
->upscript
);
4618 netdev
->upscript
= NULL
;
4623 static int clr_config_net_script_down(const char *key
,
4624 struct lxc_conf
*lxc_conf
, void *data
)
4626 struct lxc_netdev
*netdev
= data
;
4631 free(netdev
->downscript
);
4632 netdev
->downscript
= NULL
;
4637 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4640 struct lxc_netdev
*netdev
= data
;
4645 free(netdev
->hwaddr
);
4646 netdev
->hwaddr
= NULL
;
4651 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4654 struct lxc_netdev
*netdev
= data
;
4665 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4668 struct lxc_netdev
*netdev
= data
;
4673 netdev
->priv
.vlan_attr
.vid
= 0;
4678 static int clr_config_net_ipv4_gateway(const char *key
,
4679 struct lxc_conf
*lxc_conf
, void *data
)
4681 struct lxc_netdev
*netdev
= data
;
4686 free(netdev
->ipv4_gateway
);
4687 netdev
->ipv4_gateway
= NULL
;
4692 static int clr_config_net_ipv4_address(const char *key
,
4693 struct lxc_conf
*lxc_conf
, void *data
)
4695 struct lxc_netdev
*netdev
= data
;
4696 struct lxc_list
*cur
, *next
;
4701 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4710 static int clr_config_net_ipv6_gateway(const char *key
,
4711 struct lxc_conf
*lxc_conf
, void *data
)
4713 struct lxc_netdev
*netdev
= data
;
4718 free(netdev
->ipv6_gateway
);
4719 netdev
->ipv6_gateway
= NULL
;
4724 static int clr_config_net_ipv6_address(const char *key
,
4725 struct lxc_conf
*lxc_conf
, void *data
)
4727 struct lxc_netdev
*netdev
= data
;
4728 struct lxc_list
*cur
, *next
;
4733 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4742 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4743 struct lxc_conf
*c
, void *data
)
4746 const char *idxstring
;
4747 struct lxc_config_t
*config
;
4748 struct lxc_netdev
*netdev
;
4750 char *deindexed_key
= NULL
;
4752 idxstring
= key
+ 8;
4753 if (!isdigit(*idxstring
))
4756 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4757 if (!config
|| idx
< 0)
4760 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4762 free(deindexed_key
);
4766 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4767 free(deindexed_key
);
4771 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4772 struct lxc_conf
*c
, void *data
)
4776 struct lxc_netdev
*netdev
= data
;
4781 memset(retv
, 0, inlen
);
4786 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4791 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4792 struct lxc_conf
*c
, void *data
)
4796 struct lxc_netdev
*netdev
= data
;
4801 memset(retv
, 0, inlen
);
4806 if (netdev
->flags
& IFF_UP
)
4807 strprint(retv
, inlen
, "up");
4812 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4813 struct lxc_conf
*c
, void *data
)
4817 struct lxc_netdev
*netdev
= data
;
4822 memset(retv
, 0, inlen
);
4827 if (netdev
->link
[0] != '\0')
4828 strprint(retv
, inlen
, "%s", netdev
->link
);
4833 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4834 struct lxc_conf
*c
, void *data
)
4838 struct lxc_netdev
*netdev
= data
;
4843 memset(retv
, 0, inlen
);
4848 if (netdev
->name
[0] != '\0')
4849 strprint(retv
, inlen
, "%s", netdev
->name
);
4854 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4855 struct lxc_conf
*c
, void *data
)
4860 struct lxc_netdev
*netdev
= data
;
4865 memset(retv
, 0, inlen
);
4870 if (netdev
->type
!= LXC_NET_MACVLAN
)
4873 switch (netdev
->priv
.macvlan_attr
.mode
) {
4874 case MACVLAN_MODE_PRIVATE
:
4877 case MACVLAN_MODE_VEPA
:
4880 case MACVLAN_MODE_BRIDGE
:
4883 case MACVLAN_MODE_PASSTHRU
:
4891 strprint(retv
, inlen
, "%s", mode
);
4896 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4897 struct lxc_conf
*c
, void *data
)
4901 struct lxc_netdev
*netdev
= data
;
4906 memset(retv
, 0, inlen
);
4911 if (netdev
->type
!= LXC_NET_VETH
)
4914 strprint(retv
, inlen
, "%s",
4915 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4916 ? netdev
->priv
.veth_attr
.pair
4917 : netdev
->priv
.veth_attr
.veth1
);
4922 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4923 struct lxc_conf
*c
, void *data
)
4927 struct lxc_netdev
*netdev
= data
;
4932 memset(retv
, 0, inlen
);
4937 if (netdev
->upscript
)
4938 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4943 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4944 struct lxc_conf
*c
, void *data
)
4948 struct lxc_netdev
*netdev
= data
;
4953 memset(retv
, 0, inlen
);
4958 if (netdev
->downscript
)
4959 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4964 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4965 struct lxc_conf
*c
, void *data
)
4969 struct lxc_netdev
*netdev
= data
;
4974 memset(retv
, 0, inlen
);
4980 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4985 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4986 struct lxc_conf
*c
, void *data
)
4990 struct lxc_netdev
*netdev
= data
;
4995 memset(retv
, 0, inlen
);
5001 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5006 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5007 struct lxc_conf
*c
, void *data
)
5011 struct lxc_netdev
*netdev
= data
;
5016 memset(retv
, 0, inlen
);
5021 if (netdev
->type
!= LXC_NET_VLAN
)
5024 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5029 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5030 struct lxc_conf
*c
, void *data
)
5033 char buf
[INET_ADDRSTRLEN
];
5035 struct lxc_netdev
*netdev
= data
;
5040 memset(retv
, 0, inlen
);
5045 if (netdev
->ipv4_gateway_auto
) {
5046 strprint(retv
, inlen
, "auto");
5047 } else if (netdev
->ipv4_gateway
) {
5048 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5049 strprint(retv
, inlen
, "%s", buf
);
5055 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5056 struct lxc_conf
*c
, void *data
)
5060 char buf
[INET_ADDRSTRLEN
];
5061 struct lxc_list
*it
;
5063 struct lxc_netdev
*netdev
= data
;
5068 memset(retv
, 0, inlen
);
5073 listlen
= lxc_list_len(&netdev
->ipv4
);
5074 lxc_list_for_each(it
, &netdev
->ipv4
) {
5075 struct lxc_inetdev
*i
= it
->elem
;
5076 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5077 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5078 (listlen
-- > 1) ? "\n" : "");
5084 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5085 struct lxc_conf
*c
, void *data
)
5088 char buf
[INET6_ADDRSTRLEN
];
5090 struct lxc_netdev
*netdev
= data
;
5095 memset(retv
, 0, inlen
);
5100 if (netdev
->ipv6_gateway_auto
) {
5101 strprint(retv
, inlen
, "auto");
5102 } else if (netdev
->ipv6_gateway
) {
5103 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5104 strprint(retv
, inlen
, "%s", buf
);
5110 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5111 struct lxc_conf
*c
, void *data
)
5115 char buf
[INET6_ADDRSTRLEN
];
5116 struct lxc_list
*it
;
5118 struct lxc_netdev
*netdev
= data
;
5123 memset(retv
, 0, inlen
);
5128 listlen
= lxc_list_len(&netdev
->ipv6
);
5129 lxc_list_for_each(it
, &netdev
->ipv6
) {
5130 struct lxc_inet6dev
*i
= it
->elem
;
5131 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5132 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5133 (listlen
-- > 1) ? "\n" : "");
5139 int lxc_list_config_items(char *retv
, int inlen
)
5148 memset(retv
, 0, inlen
);
5150 for (i
= 0; i
< config_size
; i
++) {
5151 char *s
= config
[i
].name
;
5153 if (s
[strlen(s
) - 1] == '.')
5156 strprint(retv
, inlen
, "%s\n", s
);
5162 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5171 memset(retv
, 0, inlen
);
5173 if (!strcmp(key
, "lxc.apparmor")) {
5174 strprint(retv
, inlen
, "allow_incomplete\n");
5175 strprint(retv
, inlen
, "allow_nesting\n");
5176 strprint(retv
, inlen
, "profile\n");
5177 strprint(retv
, inlen
, "raw\n");
5178 } else if (!strcmp(key
, "lxc.cgroup")) {
5179 strprint(retv
, inlen
, "dir\n");
5180 } else if (!strcmp(key
, "lxc.selinux")) {
5181 strprint(retv
, inlen
, "context\n");
5182 } else if (!strcmp(key
, "lxc.mount")) {
5183 strprint(retv
, inlen
, "auto\n");
5184 strprint(retv
, inlen
, "entry\n");
5185 strprint(retv
, inlen
, "fstab\n");
5186 } else if (!strcmp(key
, "lxc.rootfs")) {
5187 strprint(retv
, inlen
, "mount\n");
5188 strprint(retv
, inlen
, "options\n");
5189 strprint(retv
, inlen
, "path\n");
5190 } else if (!strcmp(key
, "lxc.uts")) {
5191 strprint(retv
, inlen
, "name\n");
5192 } else if (!strcmp(key
, "lxc.hook")) {
5193 strprint(retv
, inlen
, "autodev\n");
5194 strprint(retv
, inlen
, "clone\n");
5195 strprint(retv
, inlen
, "destroy\n");
5196 strprint(retv
, inlen
, "mount\n");
5197 strprint(retv
, inlen
, "post-stop\n");
5198 strprint(retv
, inlen
, "pre-mount\n");
5199 strprint(retv
, inlen
, "pre-start\n");
5200 strprint(retv
, inlen
, "start-host\n");
5201 strprint(retv
, inlen
, "start\n");
5202 strprint(retv
, inlen
, "stop\n");
5203 } else if (!strcmp(key
, "lxc.cap")) {
5204 strprint(retv
, inlen
, "drop\n");
5205 strprint(retv
, inlen
, "keep\n");
5206 } else if (!strcmp(key
, "lxc.console")) {
5207 strprint(retv
, inlen
, "logfile\n");
5208 strprint(retv
, inlen
, "path\n");
5209 } else if (!strcmp(key
, "lxc.seccomp")) {
5210 strprint(retv
, inlen
, "profile\n");
5211 } else if (!strcmp(key
, "lxc.signal")) {
5212 strprint(retv
, inlen
, "halt\n");
5213 strprint(retv
, inlen
, "reboot\n");
5214 strprint(retv
, inlen
, "stop\n");
5215 } else if (!strcmp(key
, "lxc.start")) {
5216 strprint(retv
, inlen
, "auto\n");
5217 strprint(retv
, inlen
, "delay\n");
5218 strprint(retv
, inlen
, "order\n");
5219 } else if (!strcmp(key
, "lxc.monitor")) {
5220 strprint(retv
, inlen
, "unshare\n");
5228 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5231 const char *idxstring
;
5232 struct lxc_netdev
*netdev
;
5236 idxstring
= key
+ 8;
5237 if (!isdigit(*idxstring
))
5240 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5244 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5251 memset(retv
, 0, inlen
);
5253 strprint(retv
, inlen
, "type\n");
5254 strprint(retv
, inlen
, "script.up\n");
5255 strprint(retv
, inlen
, "script.down\n");
5256 if (netdev
->type
!= LXC_NET_EMPTY
) {
5257 strprint(retv
, inlen
, "flags\n");
5258 strprint(retv
, inlen
, "link\n");
5259 strprint(retv
, inlen
, "name\n");
5260 strprint(retv
, inlen
, "hwaddr\n");
5261 strprint(retv
, inlen
, "mtu\n");
5262 strprint(retv
, inlen
, "ipv6.address\n");
5263 strprint(retv
, inlen
, "ipv6.gateway\n");
5264 strprint(retv
, inlen
, "ipv4.address\n");
5265 strprint(retv
, inlen
, "ipv4.gateway\n");
5268 switch (netdev
->type
) {
5270 strprint(retv
, inlen
, "veth.pair\n");
5272 case LXC_NET_MACVLAN
:
5273 strprint(retv
, inlen
, "macvlan.mode\n");
5276 strprint(retv
, inlen
, "vlan.id\n");