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>
68 lxc_log_define(lxc_confile
, lxc
);
70 #define lxc_config_define(name) \
71 static int set_config_##name(const char *, const char *, \
72 struct lxc_conf *, void *); \
73 static int get_config_##name(const char *, char *, int, \
74 struct lxc_conf *, void *); \
75 static int clr_config_##name(const char *, struct lxc_conf *, void *);
77 lxc_config_define(autodev
);
78 lxc_config_define(apparmor_allow_incomplete
);
79 lxc_config_define(apparmor_profile
);
80 lxc_config_define(cap_drop
);
81 lxc_config_define(cap_keep
);
82 lxc_config_define(cgroup_controller
);
83 lxc_config_define(cgroup2_controller
);
84 lxc_config_define(cgroup_dir
);
85 lxc_config_define(console_logfile
);
86 lxc_config_define(console_rotate
);
87 lxc_config_define(console_buffer_logfile
);
88 lxc_config_define(console_buffer_size
);
89 lxc_config_define(console_path
);
90 lxc_config_define(environment
);
91 lxc_config_define(ephemeral
);
92 lxc_config_define(execute_cmd
);
93 lxc_config_define(group
);
94 lxc_config_define(hooks
);
95 lxc_config_define(hooks_version
);
96 lxc_config_define(idmaps
);
97 lxc_config_define(includefiles
);
98 lxc_config_define(init_cmd
);
99 lxc_config_define(init_cwd
);
100 lxc_config_define(init_gid
);
101 lxc_config_define(init_uid
);
102 lxc_config_define(log_file
);
103 lxc_config_define(log_level
);
104 lxc_config_define(log_syslog
);
105 lxc_config_define(monitor
);
106 lxc_config_define(mount
);
107 lxc_config_define(mount_auto
);
108 lxc_config_define(mount_fstab
);
109 lxc_config_define(namespace_clone
);
110 lxc_config_define(namespace_keep
);
111 lxc_config_define(namespace_share
);
112 lxc_config_define(net
);
113 lxc_config_define(net_flags
);
114 lxc_config_define(net_hwaddr
);
115 lxc_config_define(net_ipv4_address
);
116 lxc_config_define(net_ipv4_gateway
);
117 lxc_config_define(net_ipv6_address
);
118 lxc_config_define(net_ipv6_gateway
);
119 lxc_config_define(net_link
);
120 lxc_config_define(net_macvlan_mode
);
121 lxc_config_define(net_mtu
);
122 lxc_config_define(net_name
);
123 lxc_config_define(net_nic
);
124 lxc_config_define(net_script_down
);
125 lxc_config_define(net_script_up
);
126 lxc_config_define(net_type
);
127 lxc_config_define(net_veth_pair
);
128 lxc_config_define(net_vlan_id
);
129 lxc_config_define(no_new_privs
);
130 lxc_config_define(personality
);
131 lxc_config_define(prlimit
);
132 lxc_config_define(pty_max
);
133 lxc_config_define(rootfs_mount
);
134 lxc_config_define(rootfs_options
);
135 lxc_config_define(rootfs_path
);
136 lxc_config_define(seccomp_profile
);
137 lxc_config_define(selinux_context
);
138 lxc_config_define(signal_halt
);
139 lxc_config_define(signal_reboot
);
140 lxc_config_define(signal_stop
);
141 lxc_config_define(start
);
142 lxc_config_define(tty_max
);
143 lxc_config_define(tty_dir
);
144 lxc_config_define(uts_name
);
145 lxc_config_define(sysctl
);
146 lxc_config_define(proc
);
148 static struct lxc_config_t config
[] = {
149 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
150 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
151 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
152 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
153 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
154 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
155 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
156 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
157 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
158 { "lxc.console.buffer.logfile", set_config_console_buffer_logfile
, get_config_console_buffer_logfile
, clr_config_console_buffer_logfile
, },
159 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
160 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
161 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
162 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
163 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
164 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
165 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
166 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
167 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
168 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
169 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
170 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
171 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
174 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
175 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
176 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
177 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
178 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
179 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
180 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
181 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
182 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
183 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
184 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
185 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
186 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
187 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
188 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
189 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
190 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
191 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
192 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
193 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
194 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
195 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
196 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
197 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
198 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
199 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
200 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
201 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
202 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
203 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
204 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
205 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
206 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
207 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
208 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
209 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
210 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
211 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
212 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
213 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
214 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
215 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
216 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
217 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
218 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
219 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
220 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
221 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
222 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
223 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
224 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
225 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
226 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
227 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
228 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
229 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
230 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
238 static const struct signame signames
[] = {
271 { SIGSTKFLT
, "STKFLT" },
286 { SIGVTALRM
, "VTALRM" },
292 { SIGWINCH
, "WINCH" },
310 { SIGUNUSED
, "UNUSED" },
317 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
319 struct lxc_config_t
*lxc_get_config(const char *key
)
323 for (i
= 0; i
< config_size
; i
++)
324 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
330 static int set_config_net(const char *key
, const char *value
,
331 struct lxc_conf
*lxc_conf
, void *data
)
333 if (!lxc_config_value_empty(value
)) {
334 ERROR("lxc.net must not have a value");
338 return clr_config_net(key
, lxc_conf
, data
);
341 static int set_config_net_type(const char *key
, const char *value
,
342 struct lxc_conf
*lxc_conf
, void *data
)
344 struct lxc_netdev
*netdev
= data
;
346 if (lxc_config_value_empty(value
))
347 return clr_config_net_type(key
, lxc_conf
, data
);
352 if (!strcmp(value
, "veth")) {
353 netdev
->type
= LXC_NET_VETH
;
354 } else if (!strcmp(value
, "macvlan")) {
355 netdev
->type
= LXC_NET_MACVLAN
;
356 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
358 } else if (!strcmp(value
, "vlan")) {
359 netdev
->type
= LXC_NET_VLAN
;
360 } else if (!strcmp(value
, "phys")) {
361 netdev
->type
= LXC_NET_PHYS
;
362 } else if (!strcmp(value
, "empty")) {
363 netdev
->type
= LXC_NET_EMPTY
;
364 } else if (!strcmp(value
, "none")) {
365 netdev
->type
= LXC_NET_NONE
;
367 ERROR("invalid network type %s", value
);
374 static int set_config_net_flags(const char *key
, const char *value
,
375 struct lxc_conf
*lxc_conf
, void *data
)
377 struct lxc_netdev
*netdev
= data
;
379 if (lxc_config_value_empty(value
))
380 return clr_config_net_flags(key
, lxc_conf
, data
);
385 netdev
->flags
|= IFF_UP
;
390 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
391 struct lxc_netdev
*netdev
)
393 struct ifaddrs
*ifaddr
, *ifa
;
396 const char *type_key
= "lxc.net.type";
397 const char *link_key
= "lxc.net.link";
398 const char *tmpvalue
= "phys";
400 if (getifaddrs(&ifaddr
) == -1) {
401 SYSERROR("Get network interfaces failed");
405 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
408 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
411 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
412 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
415 ret
= set_config_net_link(
416 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
418 ERROR("failed to create matched ifnames");
422 ERROR("failed to create matched ifnames");
434 static int set_config_net_link(const char *key
, const char *value
,
435 struct lxc_conf
*lxc_conf
, void *data
)
437 struct lxc_netdev
*netdev
= data
;
440 if (lxc_config_value_empty(value
))
441 return clr_config_net_link(key
, lxc_conf
, data
);
446 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
447 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
449 ret
= network_ifname(netdev
->link
, value
);
454 static int set_config_net_name(const char *key
, const char *value
,
455 struct lxc_conf
*lxc_conf
, void *data
)
457 struct lxc_netdev
*netdev
= data
;
459 if (lxc_config_value_empty(value
))
460 return clr_config_net_name(key
, lxc_conf
, data
);
465 return network_ifname(netdev
->name
, value
);
468 static int set_config_net_veth_pair(const char *key
, const char *value
,
469 struct lxc_conf
*lxc_conf
, void *data
)
471 struct lxc_netdev
*netdev
= data
;
473 if (lxc_config_value_empty(value
))
474 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
479 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
);
482 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
483 struct lxc_conf
*lxc_conf
, void *data
)
485 struct lxc_netdev
*netdev
= data
;
487 if (lxc_config_value_empty(value
))
488 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
493 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
496 static int set_config_net_hwaddr(const char *key
, const char *value
,
497 struct lxc_conf
*lxc_conf
, void *data
)
499 struct lxc_netdev
*netdev
= data
;
502 if (lxc_config_value_empty(value
))
503 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
508 new_value
= strdup(value
);
512 rand_complete_hwaddr(new_value
);
514 if (lxc_config_value_empty(new_value
)) {
516 netdev
->hwaddr
= NULL
;
520 netdev
->hwaddr
= new_value
;
524 static int set_config_net_vlan_id(const char *key
, const char *value
,
525 struct lxc_conf
*lxc_conf
, void *data
)
528 struct lxc_netdev
*netdev
= data
;
530 if (lxc_config_value_empty(value
))
531 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
536 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
543 static int set_config_net_mtu(const char *key
, const char *value
,
544 struct lxc_conf
*lxc_conf
, void *data
)
546 struct lxc_netdev
*netdev
= data
;
548 if (lxc_config_value_empty(value
))
549 return clr_config_net_mtu(key
, lxc_conf
, data
);
554 return set_config_string_item(&netdev
->mtu
, value
);
557 static int set_config_net_ipv4_address(const char *key
, const char *value
,
558 struct lxc_conf
*lxc_conf
, void *data
)
561 struct lxc_netdev
*netdev
= data
;
562 struct lxc_inetdev
*inetdev
;
563 struct lxc_list
*list
;
564 char *cursor
, *slash
;
565 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
567 if (lxc_config_value_empty(value
))
568 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
573 inetdev
= malloc(sizeof(*inetdev
));
577 memset(inetdev
, 0, sizeof(*inetdev
));
579 list
= malloc(sizeof(*list
));
586 list
->elem
= inetdev
;
588 addr
= strdup(value
);
595 cursor
= strstr(addr
, " ");
601 slash
= strstr(addr
, "/");
607 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
608 if (!ret
|| ret
< 0) {
609 SYSERROR("Invalid ipv4 address \"%s\"", value
);
617 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
618 if (!ret
|| ret
< 0) {
619 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
628 /* No prefix specified, determine it from the network class. */
630 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
634 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
637 /* If no broadcast address, let compute one from the
638 * prefix and address.
641 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
642 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
645 lxc_list_add_tail(&netdev
->ipv4
, list
);
651 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
652 struct lxc_conf
*lxc_conf
, void *data
)
654 struct lxc_netdev
*netdev
= data
;
656 if (lxc_config_value_empty(value
))
657 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
662 free(netdev
->ipv4_gateway
);
664 if (!strcmp(value
, "auto")) {
665 netdev
->ipv4_gateway
= NULL
;
666 netdev
->ipv4_gateway_auto
= true;
671 gw
= malloc(sizeof(*gw
));
675 ret
= inet_pton(AF_INET
, value
, gw
);
676 if (!ret
|| ret
< 0) {
677 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
682 netdev
->ipv4_gateway
= gw
;
683 netdev
->ipv4_gateway_auto
= false;
689 static int set_config_net_ipv6_address(const char *key
, const char *value
,
690 struct lxc_conf
*lxc_conf
, void *data
)
693 struct lxc_netdev
*netdev
= data
;
694 struct lxc_inet6dev
*inet6dev
;
695 struct lxc_list
*list
;
696 char *slash
, *valdup
, *netmask
;
698 if (lxc_config_value_empty(value
))
699 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
704 inet6dev
= malloc(sizeof(*inet6dev
));
708 memset(inet6dev
, 0, sizeof(*inet6dev
));
710 list
= malloc(sizeof(*list
));
717 list
->elem
= inet6dev
;
719 valdup
= strdup(value
);
726 inet6dev
->prefix
= 64;
727 slash
= strstr(valdup
, "/");
731 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
740 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
741 if (!ret
|| ret
< 0) {
742 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
749 lxc_list_add_tail(&netdev
->ipv6
, list
);
755 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
756 struct lxc_conf
*lxc_conf
, void *data
)
758 struct lxc_netdev
*netdev
= data
;
760 if (lxc_config_value_empty(value
))
761 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
766 free(netdev
->ipv6_gateway
);
768 if (!strcmp(value
, "auto")) {
769 netdev
->ipv6_gateway
= NULL
;
770 netdev
->ipv6_gateway_auto
= true;
775 gw
= malloc(sizeof(*gw
));
779 ret
= inet_pton(AF_INET6
, value
, gw
);
780 if (!ret
|| ret
< 0) {
781 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
786 netdev
->ipv6_gateway
= gw
;
787 netdev
->ipv6_gateway_auto
= false;
793 static int set_config_net_script_up(const char *key
, const char *value
,
794 struct lxc_conf
*lxc_conf
, void *data
)
796 struct lxc_netdev
*netdev
= data
;
798 if (lxc_config_value_empty(value
))
799 return clr_config_net_script_up(key
, lxc_conf
, data
);
804 return set_config_string_item(&netdev
->upscript
, value
);
807 static int set_config_net_script_down(const char *key
, const char *value
,
808 struct lxc_conf
*lxc_conf
, void *data
)
810 struct lxc_netdev
*netdev
= data
;
812 if (lxc_config_value_empty(value
))
813 return clr_config_net_script_down(key
, lxc_conf
, data
);
818 return set_config_string_item(&netdev
->downscript
, value
);
821 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
823 struct lxc_list
*hooklist
;
825 hooklist
= malloc(sizeof(*hooklist
));
831 hooklist
->elem
= hook
;
832 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
836 static int set_config_seccomp_profile(const char *key
, const char *value
,
837 struct lxc_conf
*lxc_conf
, void *data
)
839 return set_config_path_item(&lxc_conf
->seccomp
, value
);
842 static int set_config_execute_cmd(const char *key
, const char *value
,
843 struct lxc_conf
*lxc_conf
, void *data
)
845 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
848 static int set_config_init_cmd(const char *key
, const char *value
,
849 struct lxc_conf
*lxc_conf
, void *data
)
851 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
854 static int set_config_init_cwd(const char *key
, const char *value
,
855 struct lxc_conf
*lxc_conf
, void *data
)
857 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
860 static int set_config_init_uid(const char *key
, const char *value
,
861 struct lxc_conf
*lxc_conf
, void *data
)
863 unsigned int init_uid
;
865 if (lxc_config_value_empty(value
)) {
866 lxc_conf
->init_uid
= 0;
870 if (lxc_safe_uint(value
, &init_uid
) < 0)
873 lxc_conf
->init_uid
= init_uid
;
878 static int set_config_init_gid(const char *key
, const char *value
,
879 struct lxc_conf
*lxc_conf
, void *data
)
881 unsigned int init_gid
;
883 if (lxc_config_value_empty(value
)) {
884 lxc_conf
->init_gid
= 0;
888 if (lxc_safe_uint(value
, &init_gid
) < 0)
891 lxc_conf
->init_gid
= init_gid
;
896 static int set_config_hooks(const char *key
, const char *value
,
897 struct lxc_conf
*lxc_conf
, void *data
)
901 if (lxc_config_value_empty(value
))
902 return lxc_clear_hooks(lxc_conf
, key
);
904 if (strcmp(key
+ 4, "hook") == 0) {
905 ERROR("lxc.hook must not have a value");
909 copy
= strdup(value
);
913 if (strcmp(key
+ 9, "pre-start") == 0)
914 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
915 else if (strcmp(key
+ 9, "start-host") == 0)
916 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
917 else if (strcmp(key
+ 9, "pre-mount") == 0)
918 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
919 else if (strcmp(key
+ 9, "autodev") == 0)
920 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
921 else if (strcmp(key
+ 9, "mount") == 0)
922 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
923 else if (strcmp(key
+ 9, "start") == 0)
924 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
925 else if (strcmp(key
+ 9, "stop") == 0)
926 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
927 else if (strcmp(key
+ 9, "post-stop") == 0)
928 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
929 else if (strcmp(key
+ 9, "clone") == 0)
930 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
931 else if (strcmp(key
+ 9, "destroy") == 0)
932 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
938 static int set_config_hooks_version(const char *key
, const char *value
,
939 struct lxc_conf
*lxc_conf
, void *data
)
944 if (lxc_config_value_empty(value
))
945 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
947 ret
= lxc_safe_uint(value
, &tmp
);
952 ERROR("Invalid hook version specified. Currently only 0 "
953 "(legacy) and 1 are supported");
957 lxc_conf
->hooks_version
= tmp
;
961 static int set_config_personality(const char *key
, const char *value
,
962 struct lxc_conf
*lxc_conf
, void *data
)
964 signed long personality
= lxc_config_parse_arch(value
);
966 if (personality
>= 0)
967 lxc_conf
->personality
= personality
;
969 WARN("Unsupported personality \"%s\"", value
);
974 static int set_config_pty_max(const char *key
, const char *value
,
975 struct lxc_conf
*lxc_conf
, void *data
)
977 if (lxc_config_value_empty(value
)) {
982 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
988 /* We only need to check whether the first byte of the key after the lxc.start.
989 * prefix matches our expectations since they fortunately all start with a
990 * different letter. If anything was wrong with the key we would have already
991 * noticed when the callback was called.
993 static int set_config_start(const char *key
, const char *value
,
994 struct lxc_conf
*lxc_conf
, void *data
)
998 is_empty
= lxc_config_value_empty(value
);
1000 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1002 lxc_conf
->start_auto
= 0;
1006 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1009 if (lxc_conf
->start_auto
> 1)
1013 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1015 lxc_conf
->start_delay
= 0;
1019 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1020 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1022 lxc_conf
->start_order
= 0;
1026 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1032 static int set_config_monitor(const char *key
, const char *value
,
1033 struct lxc_conf
*lxc_conf
, void *data
)
1035 if (lxc_config_value_empty(value
)) {
1036 lxc_conf
->monitor_unshare
= 0;
1040 if (strcmp(key
+ 12, "unshare") == 0)
1041 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1046 static int set_config_group(const char *key
, const char *value
,
1047 struct lxc_conf
*lxc_conf
, void *data
)
1049 char *groups
, *groupptr
, *sptr
, *token
;
1050 struct lxc_list
*grouplist
;
1053 if (lxc_config_value_empty(value
))
1054 return lxc_clear_groups(lxc_conf
);
1056 groups
= strdup(value
);
1060 /* In case several groups are specified in a single line split these
1061 * groups in a single element for the list.
1063 for (groupptr
= groups
;; groupptr
= NULL
) {
1064 token
= strtok_r(groupptr
, " \t", &sptr
);
1070 grouplist
= malloc(sizeof(*grouplist
));
1074 grouplist
->elem
= strdup(token
);
1075 if (!grouplist
->elem
) {
1080 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1087 static int set_config_environment(const char *key
, const char *value
,
1088 struct lxc_conf
*lxc_conf
, void *data
)
1090 struct lxc_list
*list_item
= NULL
;
1092 if (lxc_config_value_empty(value
))
1093 return lxc_clear_environment(lxc_conf
);
1095 list_item
= malloc(sizeof(*list_item
));
1099 list_item
->elem
= strdup(value
);
1101 if (!list_item
->elem
)
1104 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1113 static int set_config_tty_max(const char *key
, const char *value
,
1114 struct lxc_conf
*lxc_conf
, void *data
)
1116 if (lxc_config_value_empty(value
)) {
1121 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1124 static int set_config_tty_dir(const char *key
, const char *value
,
1125 struct lxc_conf
*lxc_conf
, void *data
)
1127 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1131 static int set_config_apparmor_profile(const char *key
, const char *value
,
1132 struct lxc_conf
*lxc_conf
, void *data
)
1134 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1137 static int set_config_apparmor_allow_incomplete(const char *key
,
1139 struct lxc_conf
*lxc_conf
,
1142 if (lxc_config_value_empty(value
)) {
1143 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1147 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1150 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1156 static int set_config_selinux_context(const char *key
, const char *value
,
1157 struct lxc_conf
*lxc_conf
, void *data
)
1159 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1162 static int set_config_log_file(const char *key
, const char *value
,
1163 struct lxc_conf
*c
, void *data
)
1167 if (lxc_config_value_empty(value
)) {
1173 /* Store these values in the lxc_conf, and then try to set for actual
1176 ret
= set_config_path_item(&c
->logfile
, value
);
1178 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1183 static int set_config_log_level(const char *key
, const char *value
,
1184 struct lxc_conf
*lxc_conf
, void *data
)
1188 if (lxc_config_value_empty(value
)) {
1189 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1193 if (value
[0] >= '0' && value
[0] <= '9') {
1194 if (lxc_safe_int(value
, &newlevel
) < 0)
1197 newlevel
= lxc_log_priority_to_int(value
);
1200 /* Store these values in the lxc_conf, and then try to set for actual
1203 lxc_conf
->loglevel
= newlevel
;
1204 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1207 static int set_config_autodev(const char *key
, const char *value
,
1208 struct lxc_conf
*lxc_conf
, void *data
)
1210 if (lxc_config_value_empty(value
)) {
1211 lxc_conf
->autodev
= 0;
1215 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1218 if (lxc_conf
->autodev
> 1)
1224 static int sig_num(const char *sig
)
1226 unsigned int signum
;
1228 if (lxc_safe_uint(sig
, &signum
) < 0)
1234 static int rt_sig_num(const char *signame
)
1236 int rtmax
= 0, sig_n
= 0;
1238 if (strncasecmp(signame
, "max-", 4) == 0) {
1243 if (!isdigit(*signame
))
1246 sig_n
= sig_num(signame
);
1247 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1248 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1254 static int sig_parse(const char *signame
)
1258 if (isdigit(*signame
)) {
1259 return sig_num(signame
);
1260 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1262 if (strncasecmp(signame
, "rt", 2) == 0)
1263 return rt_sig_num(signame
+ 2);
1264 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1265 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1266 return signames
[n
].num
;
1273 static int set_config_signal_halt(const char *key
, const char *value
,
1274 struct lxc_conf
*lxc_conf
, void *data
)
1278 if (lxc_config_value_empty(value
)) {
1279 lxc_conf
->haltsignal
= 0;
1283 sig_n
= sig_parse(value
);
1287 lxc_conf
->haltsignal
= sig_n
;
1292 static int set_config_signal_reboot(const char *key
, const char *value
,
1293 struct lxc_conf
*lxc_conf
, void *data
)
1297 if (lxc_config_value_empty(value
)) {
1298 lxc_conf
->rebootsignal
= 0;
1302 sig_n
= sig_parse(value
);
1306 lxc_conf
->rebootsignal
= sig_n
;
1311 static int set_config_signal_stop(const char *key
, const char *value
,
1312 struct lxc_conf
*lxc_conf
, void *data
)
1316 if (lxc_config_value_empty(value
)) {
1317 lxc_conf
->stopsignal
= 0;
1321 sig_n
= sig_parse(value
);
1325 lxc_conf
->stopsignal
= sig_n
;
1330 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1331 struct lxc_conf
*lxc_conf
, int version
)
1333 const char *subkey
, *token
;
1335 struct lxc_list
*cglist
= NULL
;
1336 struct lxc_cgroup
*cgelem
= NULL
;
1338 if (lxc_config_value_empty(value
))
1339 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1341 if (version
== CGROUP2_SUPER_MAGIC
) {
1342 token
= "lxc.cgroup2.";
1344 } else if (version
== CGROUP_SUPER_MAGIC
) {
1345 token
= "lxc.cgroup.";
1351 if (strncmp(key
, token
, token_len
) != 0)
1354 subkey
= key
+ token_len
;
1355 if (*subkey
== '\0')
1358 cglist
= malloc(sizeof(*cglist
));
1362 cgelem
= malloc(sizeof(*cgelem
));
1365 memset(cgelem
, 0, sizeof(*cgelem
));
1367 cgelem
->subsystem
= strdup(subkey
);
1368 if (!cgelem
->subsystem
)
1371 cgelem
->value
= strdup(value
);
1375 cgelem
->version
= version
;
1377 lxc_list_add_elem(cglist
, cgelem
);
1379 if (version
== CGROUP2_SUPER_MAGIC
)
1380 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1382 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1389 free(cgelem
->subsystem
);
1390 free(cgelem
->value
);
1397 static int set_config_cgroup_controller(const char *key
, const char *value
,
1398 struct lxc_conf
*lxc_conf
, void *data
)
1400 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1401 CGROUP_SUPER_MAGIC
);
1404 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1405 struct lxc_conf
*lxc_conf
, void *data
)
1407 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1408 CGROUP2_SUPER_MAGIC
);
1412 static int set_config_cgroup_dir(const char *key
, const char *value
,
1413 struct lxc_conf
*lxc_conf
, void *data
)
1415 if (lxc_config_value_empty(value
))
1416 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1418 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1421 static int set_config_prlimit(const char *key
, const char *value
,
1422 struct lxc_conf
*lxc_conf
, void *data
)
1424 struct lxc_list
*iter
;
1425 struct rlimit limit
;
1427 struct lxc_list
*limlist
= NULL
;
1428 struct lxc_limit
*limelem
= NULL
;
1430 if (lxc_config_value_empty(value
))
1431 return lxc_clear_limits(lxc_conf
, key
);
1433 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1436 key
+= sizeof("lxc.prlimit.") - 1;
1438 /* soft limit comes first in the value */
1439 if (!parse_limit_value(&value
, &limit_value
))
1441 limit
.rlim_cur
= limit_value
;
1443 /* skip spaces and a colon */
1444 while (isspace(*value
))
1449 else if (*value
) /* any other character is an error here */
1452 while (isspace(*value
))
1455 /* optional hard limit */
1457 if (!parse_limit_value(&value
, &limit_value
))
1459 limit
.rlim_max
= limit_value
;
1461 /* check for trailing garbage */
1462 while (isspace(*value
))
1468 /* a single value sets both hard and soft limit */
1469 limit
.rlim_max
= limit
.rlim_cur
;
1472 /* find existing list element */
1473 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1474 limelem
= iter
->elem
;
1475 if (!strcmp(key
, limelem
->resource
)) {
1476 limelem
->limit
= limit
;
1481 /* allocate list element */
1482 limlist
= malloc(sizeof(*limlist
));
1486 limelem
= malloc(sizeof(*limelem
));
1489 memset(limelem
, 0, sizeof(*limelem
));
1491 limelem
->resource
= strdup(key
);
1492 if (!limelem
->resource
)
1494 limelem
->limit
= limit
;
1496 lxc_list_add_elem(limlist
, limelem
);;
1498 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1505 free(limelem
->resource
);
1511 static int set_config_sysctl(const char *key
, const char *value
,
1512 struct lxc_conf
*lxc_conf
, void *data
)
1514 struct lxc_list
*iter
;
1515 char *replace_value
= NULL
;
1516 struct lxc_list
*sysctl_list
= NULL
;
1517 struct lxc_sysctl
*sysctl_elem
= NULL
;
1519 if (lxc_config_value_empty(value
))
1520 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1522 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1525 key
+= sizeof("lxc.sysctl.") - 1;
1527 /* find existing list element */
1528 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1529 sysctl_elem
= iter
->elem
;
1531 if (strcmp(key
, sysctl_elem
->key
) != 0)
1534 replace_value
= strdup(value
);
1538 free(sysctl_elem
->value
);
1539 sysctl_elem
->value
= replace_value
;
1543 /* allocate list element */
1544 sysctl_list
= malloc(sizeof(*sysctl_list
));
1548 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1551 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1553 sysctl_elem
->key
= strdup(key
);
1554 if (!sysctl_elem
->key
)
1557 sysctl_elem
->value
= strdup(value
);
1558 if (!sysctl_elem
->value
)
1561 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1563 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1570 free(sysctl_elem
->key
);
1571 free(sysctl_elem
->value
);
1577 static int set_config_proc(const char *key
, const char *value
,
1578 struct lxc_conf
*lxc_conf
, void *data
)
1581 struct lxc_list
*proclist
= NULL
;
1582 struct lxc_proc
*procelem
= NULL
;
1584 if (lxc_config_value_empty(value
))
1585 return clr_config_proc(key
, lxc_conf
, NULL
);
1587 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1590 subkey
= key
+ sizeof("lxc.proc.") - 1;
1591 if (*subkey
== '\0')
1594 proclist
= malloc(sizeof(*proclist
));
1598 procelem
= malloc(sizeof(*procelem
));
1601 memset(procelem
, 0, sizeof(*procelem
));
1603 procelem
->filename
= strdup(subkey
);
1604 procelem
->value
= strdup(value
);
1606 if (!procelem
->filename
|| !procelem
->value
)
1609 proclist
->elem
= procelem
;
1611 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1618 free(procelem
->filename
);
1619 free(procelem
->value
);
1626 static int set_config_idmaps(const char *key
, const char *value
,
1627 struct lxc_conf
*lxc_conf
, void *data
)
1629 unsigned long hostid
, nsid
, range
;
1632 struct lxc_list
*idmaplist
= NULL
;
1633 struct id_map
*idmap
= NULL
;
1635 if (lxc_config_value_empty(value
))
1636 return lxc_clear_idmaps(lxc_conf
);
1638 idmaplist
= malloc(sizeof(*idmaplist
));
1642 idmap
= malloc(sizeof(*idmap
));
1645 memset(idmap
, 0, sizeof(*idmap
));
1647 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1649 ERROR("Failed to parse id mappings");
1653 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1655 idmap
->idtype
= ID_TYPE_UID
;
1656 else if (type
== 'g')
1657 idmap
->idtype
= ID_TYPE_GID
;
1661 idmap
->hostid
= hostid
;
1663 idmap
->range
= range
;
1664 idmaplist
->elem
= idmap
;
1665 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1667 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1668 if (idmap
->nsid
== 0)
1669 lxc_conf
->root_nsuid_map
= idmap
;
1672 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1673 if (idmap
->nsid
== 0)
1674 lxc_conf
->root_nsgid_map
= idmap
;
1687 static int set_config_mount_fstab(const char *key
, const char *value
,
1688 struct lxc_conf
*lxc_conf
, void *data
)
1690 if (lxc_config_value_empty(value
)) {
1691 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1695 return set_config_path_item(&lxc_conf
->fstab
, value
);
1698 static int set_config_mount_auto(const char *key
, const char *value
,
1699 struct lxc_conf
*lxc_conf
, void *data
)
1701 char *autos
, *autoptr
, *sptr
, *token
;
1708 } allowed_auto_mounts
[] = {
1709 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1710 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1711 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1712 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1713 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1714 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1715 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1716 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1717 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1718 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1719 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1720 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1721 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1722 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1723 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1724 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1725 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1726 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1727 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1728 /* For adding anything that is just a single on/off, but has no
1729 * options: keep mask and flag identical and just define the enum
1730 * value as an unused bit so far
1735 if (lxc_config_value_empty(value
)) {
1736 lxc_conf
->auto_mounts
= 0;
1740 autos
= strdup(value
);
1744 for (autoptr
= autos
;; autoptr
= NULL
) {
1745 token
= strtok_r(autoptr
, " \t", &sptr
);
1751 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1752 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1756 if (!allowed_auto_mounts
[i
].token
) {
1757 ERROR("Invalid filesystem to automount \"%s\"", token
);
1761 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1762 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1769 static int set_config_mount(const char *key
, const char *value
,
1770 struct lxc_conf
*lxc_conf
, void *data
)
1773 struct lxc_list
*mntlist
;
1775 if (lxc_config_value_empty(value
))
1776 return lxc_clear_mount_entries(lxc_conf
);
1778 mntlist
= malloc(sizeof(*mntlist
));
1782 mntelem
= strdup(value
);
1787 mntlist
->elem
= mntelem
;
1789 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1794 static int set_config_cap_keep(const char *key
, const char *value
,
1795 struct lxc_conf
*lxc_conf
, void *data
)
1797 char *keepcaps
, *keepptr
, *sptr
, *token
;
1798 struct lxc_list
*keeplist
;
1801 if (lxc_config_value_empty(value
))
1802 return lxc_clear_config_keepcaps(lxc_conf
);
1804 keepcaps
= strdup(value
);
1808 /* In case several capability keep is specified in a single line
1809 * split these caps in a single element for the list.
1811 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1812 token
= strtok_r(keepptr
, " \t", &sptr
);
1818 if (!strcmp(token
, "none"))
1819 lxc_clear_config_keepcaps(lxc_conf
);
1821 keeplist
= malloc(sizeof(*keeplist
));
1825 keeplist
->elem
= strdup(token
);
1826 if (!keeplist
->elem
) {
1831 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1839 static int set_config_cap_drop(const char *key
, const char *value
,
1840 struct lxc_conf
*lxc_conf
, void *data
)
1842 char *dropcaps
, *dropptr
, *sptr
, *token
;
1843 struct lxc_list
*droplist
;
1846 if (lxc_config_value_empty(value
))
1847 return lxc_clear_config_caps(lxc_conf
);
1849 dropcaps
= strdup(value
);
1853 /* In case several capability drop is specified in a single line
1854 * split these caps in a single element for the list.
1856 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1857 token
= strtok_r(dropptr
, " \t", &sptr
);
1863 droplist
= malloc(sizeof(*droplist
));
1867 droplist
->elem
= strdup(token
);
1868 if (!droplist
->elem
) {
1873 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1881 static int set_config_console_path(const char *key
, const char *value
,
1882 struct lxc_conf
*lxc_conf
, void *data
)
1884 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1887 static int set_config_console_rotate(const char *key
, const char *value
,
1888 struct lxc_conf
*lxc_conf
, void *data
)
1890 if (lxc_config_value_empty(value
)) {
1891 lxc_conf
->console
.log_rotate
= 0;
1895 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1898 if (lxc_conf
->console
.log_rotate
> 1) {
1899 ERROR("The \"lxc.console.rotate\" config key can only be set "
1907 static int set_config_console_logfile(const char *key
, const char *value
,
1908 struct lxc_conf
*lxc_conf
, void *data
)
1910 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1913 static int set_config_console_buffer_size(const char *key
, const char *value
,
1914 struct lxc_conf
*lxc_conf
, void *data
)
1918 uint64_t buffer_size
, pgsz
;
1920 if (lxc_config_value_empty(value
)) {
1921 lxc_conf
->console
.buffer_size
= 0;
1925 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1926 if (!strcmp(value
, "auto")) {
1927 lxc_conf
->console
.buffer_size
= 1 << 17;
1931 ret
= parse_byte_size_string(value
, &size
);
1938 /* must be at least a page size */
1939 pgsz
= lxc_getpagesize();
1940 if ((uint64_t)size
< pgsz
) {
1941 NOTICE("Requested ringbuffer size for the console is %" PRId64
1942 " but must be at least %" PRId64
1943 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1948 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1949 if (buffer_size
== 0)
1952 if (buffer_size
!= size
)
1953 NOTICE("Passed size was not a power of 2. Rounding log size to "
1954 "next power of two: %" PRIu64
" bytes", buffer_size
);
1956 lxc_conf
->console
.buffer_size
= buffer_size
;
1960 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
1961 struct lxc_conf
*lxc_conf
,
1964 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
1967 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1969 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1971 update_hwaddr(line
);
1973 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1974 char *tmp
= realloc(conf
->unexpanded_config
,
1975 conf
->unexpanded_alloced
+ 1024);
1979 if (!conf
->unexpanded_config
)
1981 conf
->unexpanded_config
= tmp
;
1982 conf
->unexpanded_alloced
+= 1024;
1984 strcat(conf
->unexpanded_config
, line
);
1985 conf
->unexpanded_len
+= linelen
;
1986 if (line
[linelen
- 1] != '\n') {
1987 strcat(conf
->unexpanded_config
, "\n");
1988 conf
->unexpanded_len
++;
1994 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1996 struct dirent
*direntp
;
1998 char path
[MAXPATHLEN
];
2002 dir
= opendir(dirp
);
2006 while ((direntp
= readdir(dir
))) {
2011 fnam
= direntp
->d_name
;
2012 if (!strcmp(fnam
, "."))
2015 if (!strcmp(fnam
, ".."))
2019 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2022 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2023 if (len
< 0 || len
>= MAXPATHLEN
) {
2028 ret
= lxc_config_read(path
, lxc_conf
, true);
2040 static int set_config_includefiles(const char *key
, const char *value
,
2041 struct lxc_conf
*lxc_conf
, void *data
)
2043 if (lxc_config_value_empty(value
)) {
2044 clr_config_includefiles(key
, lxc_conf
, NULL
);
2049 return do_includedir(value
, lxc_conf
);
2051 return lxc_config_read(value
, lxc_conf
, true);
2054 static int set_config_rootfs_path(const char *key
, const char *value
,
2055 struct lxc_conf
*lxc_conf
, void *data
)
2059 const char *container_path
;
2061 if (lxc_config_value_empty(value
)) {
2062 free(lxc_conf
->rootfs
.path
);
2063 lxc_conf
->rootfs
.path
= NULL
;
2067 dup
= strdup(value
);
2071 /* Split <storage type>:<container path> into <storage type> and
2072 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2073 * "rootfs.path" to <container path>.
2075 tmp
= strchr(dup
, ':');
2078 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2084 container_path
= tmp
;
2086 container_path
= value
;
2089 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2094 static int set_config_rootfs_mount(const char *key
, const char *value
,
2095 struct lxc_conf
*lxc_conf
, void *data
)
2097 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2100 static int set_config_rootfs_options(const char *key
, const char *value
,
2101 struct lxc_conf
*lxc_conf
, void *data
)
2103 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2106 static int set_config_uts_name(const char *key
, const char *value
,
2107 struct lxc_conf
*lxc_conf
, void *data
)
2109 struct utsname
*utsname
;
2111 if (lxc_config_value_empty(value
)) {
2112 clr_config_uts_name(key
, lxc_conf
, NULL
);
2116 utsname
= malloc(sizeof(*utsname
));
2120 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2125 strcpy(utsname
->nodename
, value
);
2126 free(lxc_conf
->utsname
);
2127 lxc_conf
->utsname
= utsname
;
2132 static int set_config_namespace_clone(const char *key
, const char *value
,
2133 struct lxc_conf
*lxc_conf
, void *data
)
2135 char *ns
, *nsptr
, *token
;
2137 char *saveptr
= NULL
;
2139 if (lxc_config_value_empty(value
))
2140 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2142 if (lxc_conf
->ns_keep
!= 0) {
2143 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2144 "\"lxc.namespace.keep\"", strerror(EINVAL
));
2153 for (; (token
= strtok_r(nsptr
, " \t", &saveptr
)); nsptr
= NULL
) {
2154 token
+= lxc_char_left_gc(token
, strlen(token
));
2155 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2156 cloneflag
= lxc_namespace_2_cloneflag(token
);
2157 if (cloneflag
< 0) {
2161 lxc_conf
->ns_clone
|= cloneflag
;
2168 static int set_config_namespace_keep(const char *key
, const char *value
,
2169 struct lxc_conf
*lxc_conf
, void *data
)
2171 char *ns
, *nsptr
, *token
;
2173 char *saveptr
= NULL
;
2175 if (lxc_config_value_empty(value
))
2176 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2178 if (lxc_conf
->ns_clone
!= 0) {
2179 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2180 "\"lxc.namespace.keep\"", strerror(EINVAL
));
2189 for (; (token
= strtok_r(nsptr
, " \t", &saveptr
)); nsptr
= NULL
) {
2190 token
+= lxc_char_left_gc(token
, strlen(token
));
2191 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2192 cloneflag
= lxc_namespace_2_cloneflag(token
);
2193 if (cloneflag
< 0) {
2197 lxc_conf
->ns_keep
|= cloneflag
;
2204 static int set_config_namespace_share(const char *key
, const char *value
,
2205 struct lxc_conf
*lxc_conf
, void *data
)
2208 const char *namespace;
2210 if (lxc_config_value_empty(value
))
2211 return clr_config_namespace_share(key
, lxc_conf
, data
);
2213 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
2214 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2218 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2221 struct parse_line_conf
{
2222 struct lxc_conf
*conf
;
2226 static int parse_line(char *buffer
, void *data
)
2228 char *dot
, *key
, *line
, *linep
, *value
;
2230 struct lxc_config_t
*config
;
2233 struct parse_line_conf
*plc
= data
;
2235 /* If there are newlines in the config file we should keep them. */
2236 empty_line
= lxc_is_line_empty(dup
);
2240 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2241 * modified the original string on the stack by replacing '=' by '\0'
2244 linep
= line
= strdup(dup
);
2248 if (!plc
->from_include
) {
2249 ret
= append_unexp_config_line(line
, plc
->conf
);
2257 line
+= lxc_char_left_gc(line
, strlen(line
));
2259 /* ignore comments */
2263 /* martian option - don't add it to the config itself */
2264 if (strncmp(line
, "lxc.", 4))
2269 dot
= strchr(line
, '=');
2271 ERROR("Invalid configuration line: %s", line
);
2279 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2281 value
+= lxc_char_left_gc(value
, strlen(value
));
2282 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2284 if (*value
== '\'' || *value
== '\"') {
2287 len
= strlen(value
);
2288 if (len
> 1 && value
[len
- 1] == *value
) {
2289 value
[len
- 1] = '\0';
2294 config
= lxc_get_config(key
);
2296 ERROR("Unknown configuration key \"%s\"", key
);
2300 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2307 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2309 struct parse_line_conf c
;
2312 c
.from_include
= false;
2314 return parse_line(buffer
, &c
);
2317 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2320 struct parse_line_conf c
;
2323 c
.from_include
= from_include
;
2325 ret
= access(file
, R_OK
);
2329 /* Catch only the top level config file name in the structure. */
2331 conf
->rcfile
= strdup(file
);
2333 return lxc_file_for_each_line(file
, parse_line
, &c
);
2336 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2338 struct lxc_list
*dent
;
2340 dent
= malloc(sizeof(struct lxc_list
));
2345 lxc_list_add_tail(defines
, dent
);
2349 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2351 struct lxc_list
*it
, *next
;
2354 lxc_list_for_each(it
, defines
) {
2355 ret
= lxc_config_readline(it
->elem
, conf
);
2360 lxc_list_for_each_safe(it
, defines
, next
) {
2368 signed long lxc_config_parse_arch(const char *arch
)
2370 #if HAVE_SYS_PERSONALITY_H
2376 { "x86", PER_LINUX32
},
2377 { "linux32", PER_LINUX32
},
2378 { "i386", PER_LINUX32
},
2379 { "i486", PER_LINUX32
},
2380 { "i586", PER_LINUX32
},
2381 { "i686", PER_LINUX32
},
2382 { "athlon", PER_LINUX32
},
2383 { "mips", PER_LINUX32
},
2384 { "mipsel", PER_LINUX32
},
2385 { "ppc", PER_LINUX32
},
2386 { "arm", PER_LINUX32
},
2387 { "armv7l", PER_LINUX32
},
2388 { "armhf", PER_LINUX32
},
2389 { "armel", PER_LINUX32
},
2390 { "powerpc", PER_LINUX32
},
2391 { "linux64", PER_LINUX
},
2392 { "x86_64", PER_LINUX
},
2393 { "amd64", PER_LINUX
},
2394 { "mips64", PER_LINUX
},
2395 { "mips64el", PER_LINUX
},
2396 { "ppc64", PER_LINUX
},
2397 { "ppc64le", PER_LINUX
},
2398 { "ppc64el", PER_LINUX
},
2399 { "powerpc64", PER_LINUX
},
2400 { "s390x", PER_LINUX
},
2401 { "aarch64", PER_LINUX
},
2402 { "arm64", PER_LINUX
},
2404 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2406 for (i
= 0; i
< len
; i
++) {
2407 if (!strcmp(pername
[i
].name
, arch
))
2408 return pername
[i
].per
;
2415 /* Write out a configuration file. */
2416 void write_config(FILE *fout
, struct lxc_conf
*c
)
2419 size_t len
= c
->unexpanded_len
;
2424 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2426 SYSERROR("Failed to write configuration file");
2429 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2436 len
= strlen(key
) + strlen(v
) + 4;
2439 if (lxc_config_value_empty(v
))
2440 ret
= snprintf(tmp
, len
, "%s =", key
);
2442 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2443 if (ret
< 0 || ret
>= len
)
2446 /* Save the line verbatim into unexpanded_conf */
2447 if (append_unexp_config_line(tmp
, conf
))
2453 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2457 char *lstart
= conf
->unexpanded_config
;
2459 if (!conf
->unexpanded_config
)
2463 lend
= strchr(lstart
, '\n');
2466 lend
= lstart
+ strlen(lstart
);
2469 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2474 v
= lstart
[strlen(key
)];
2475 if (!isspace(v
) && v
!= '=') {
2480 conf
->unexpanded_len
-= (lend
- lstart
);
2481 if (*lend
== '\0') {
2485 memmove(lstart
, lend
, strlen(lend
) + 1);
2489 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2490 const char *newpath
, const char *oldname
,
2491 const char *newname
, const char *ovldir
)
2494 char *lend
, *newdir
, *olddir
, *p
, *q
;
2495 size_t newdirlen
, olddirlen
;
2496 char *lstart
= conf
->unexpanded_config
;
2497 const char *key
= "lxc.mount.entry";
2499 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2500 olddir
= alloca(olddirlen
+ 1);
2501 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2503 if (ret
< 0 || ret
>= olddirlen
+ 1)
2506 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2507 newdir
= alloca(newdirlen
+ 1);
2508 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2510 if (ret
< 0 || ret
>= newdirlen
+ 1)
2513 if (!conf
->unexpanded_config
)
2517 lend
= strchr(lstart
, '\n');
2519 lend
= lstart
+ strlen(lstart
);
2523 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2526 p
= strchr(lstart
+ strlen(key
), '=');
2537 /* Whenever an lxc.mount.entry entry is found in a line we check
2538 * if the substring " overlay" or the substring " aufs" is
2539 * present before doing any further work. We check for "
2540 * overlay" and " aufs" since both substrings need to have at
2541 * least one space before them in a valid overlay
2542 * lxc.mount.entry (/A B overlay). When the space before is
2543 * missing it is very likely that these substrings are part of a
2544 * path or something else. (Checking q >= lend ensures that we
2545 * only count matches in the current line.) */
2546 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2547 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2550 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2553 /* replace the olddir with newdir */
2554 if (olddirlen
>= newdirlen
) {
2555 size_t diff
= olddirlen
- newdirlen
;
2556 memcpy(q
, newdir
, newdirlen
);
2557 if (olddirlen
!= newdirlen
) {
2558 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2559 strlen(q
) - newdirlen
- diff
+ 1);
2561 conf
->unexpanded_len
-= diff
;
2565 size_t diff
= newdirlen
- olddirlen
;
2566 size_t oldlen
= conf
->unexpanded_len
;
2567 size_t newlen
= oldlen
+ diff
;
2568 size_t poffset
= q
- conf
->unexpanded_config
;
2570 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2574 conf
->unexpanded_len
= newlen
;
2575 conf
->unexpanded_alloced
= newlen
+ 1;
2576 new[newlen
- 1] = '\0';
2577 lend
= new + (lend
- conf
->unexpanded_config
);
2578 /* Move over the remainder to make room for the newdir.
2580 memmove(new + poffset
+ newdirlen
,
2581 new + poffset
+ olddirlen
,
2582 oldlen
- poffset
- olddirlen
+ 1);
2583 conf
->unexpanded_config
= new;
2584 memcpy(new + poffset
, newdir
, newdirlen
);
2594 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2595 const char *newpath
, const char *oldname
,
2596 const char *newname
)
2599 char *lend
, *newdir
, *olddir
, *p
;
2600 char *lstart
= conf
->unexpanded_config
;
2601 size_t newdirlen
, olddirlen
;
2602 const char *key
= "lxc.hook";
2604 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2605 olddir
= alloca(olddirlen
+ 1);
2606 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2607 if (ret
< 0 || ret
>= olddirlen
+ 1)
2610 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2611 newdir
= alloca(newdirlen
+ 1);
2612 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2613 if (ret
< 0 || ret
>= newdirlen
+ 1)
2616 if (!conf
->unexpanded_config
)
2620 lend
= strchr(lstart
, '\n');
2622 lend
= lstart
+ strlen(lstart
);
2626 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2629 p
= strchr(lstart
+ strlen(key
), '=');
2640 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2643 /* replace the olddir with newdir */
2644 if (olddirlen
>= newdirlen
) {
2645 size_t diff
= olddirlen
- newdirlen
;
2646 memcpy(p
, newdir
, newdirlen
);
2647 if (olddirlen
!= newdirlen
) {
2648 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2649 strlen(p
) - newdirlen
- diff
+ 1);
2651 conf
->unexpanded_len
-= diff
;
2655 size_t diff
= newdirlen
- olddirlen
;
2656 size_t oldlen
= conf
->unexpanded_len
;
2657 size_t newlen
= oldlen
+ diff
;
2658 size_t poffset
= p
- conf
->unexpanded_config
;
2660 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2664 conf
->unexpanded_len
= newlen
;
2665 conf
->unexpanded_alloced
= newlen
+ 1;
2666 new[newlen
- 1] = '\0';
2667 lend
= new + (lend
- conf
->unexpanded_config
);
2668 /* Move over the remainder to make room for the newdir.
2670 memmove(new + poffset
+ newdirlen
,
2671 new + poffset
+ olddirlen
,
2672 oldlen
- poffset
- olddirlen
+ 1);
2673 conf
->unexpanded_config
= new;
2674 memcpy(new + poffset
, newdir
, newdirlen
);
2687 ERROR("Error writing to new config"); \
2692 /* This is called only from clone. We wish to update all hwaddrs in the
2693 * unexpanded config file. We can't/don't want to update any which come from
2694 * lxc.includes (there shouldn't be any).
2695 * We can't just walk the c->lxc-conf->network list because that includes netifs
2696 * from the include files. So we update the ones which we find in the unexp
2697 * config file, then find the original macaddr in the conf->network, and update
2698 * that to the same value.
2700 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2702 char *lend
, *p
, *p2
;
2703 struct lxc_list
*it
;
2704 char *lstart
= conf
->unexpanded_config
;
2706 if (!conf
->unexpanded_config
)
2710 char newhwaddr
[18], oldhwaddr
[17];
2712 lend
= strchr(lstart
, '\n');
2714 lend
= lstart
+ strlen(lstart
);
2718 if (!lxc_config_net_hwaddr(lstart
)) {
2723 p
= strchr(lstart
, '=');
2736 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2739 if ((p2
- p
) != 17) {
2740 WARN("Bad hwaddr entry");
2745 memcpy(oldhwaddr
, p
, 17);
2747 if (!new_hwaddr(newhwaddr
))
2750 memcpy(p
, newhwaddr
, 17);
2751 lxc_list_for_each(it
, &conf
->network
) {
2752 struct lxc_netdev
*n
= it
->elem
;
2754 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2755 memcpy(n
->hwaddr
, newhwaddr
, 17);
2764 static int set_config_ephemeral(const char *key
, const char *value
,
2765 struct lxc_conf
*lxc_conf
, void *data
)
2767 if (lxc_config_value_empty(value
)) {
2768 lxc_conf
->ephemeral
= 0;
2772 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2775 if (lxc_conf
->ephemeral
> 1)
2781 static int set_config_log_syslog(const char *key
, const char *value
,
2782 struct lxc_conf
*lxc_conf
, void *data
)
2786 if (lxc_conf
->syslog
) {
2787 free(lxc_conf
->syslog
);
2788 lxc_conf
->syslog
= NULL
;
2791 if (lxc_config_value_empty(value
))
2794 facility
= lxc_syslog_priority_to_int(value
);
2795 if (facility
== -EINVAL
)
2798 lxc_log_syslog(facility
);
2799 return set_config_string_item(&lxc_conf
->syslog
, value
);
2802 static int set_config_no_new_privs(const char *key
, const char *value
,
2803 struct lxc_conf
*lxc_conf
, void *data
)
2807 if (lxc_config_value_empty(value
)) {
2808 lxc_conf
->no_new_privs
= false;
2812 if (lxc_safe_uint(value
, &v
) < 0)
2818 lxc_conf
->no_new_privs
= v
? true : false;
2823 /* Callbacks to get configuration items. */
2824 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2825 struct lxc_conf
*c
, void *data
)
2832 memset(retv
, 0, inlen
);
2834 #if HAVE_SYS_PERSONALITY_H
2837 switch (c
->personality
) {
2839 strprint(retv
, inlen
, "i686");
2842 strprint(retv
, inlen
, "x86_64");
2852 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2853 struct lxc_conf
*c
, void *data
)
2855 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2858 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2859 struct lxc_conf
*c
, void *data
)
2861 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2864 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2865 struct lxc_conf
*c
, void *data
)
2867 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2870 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2871 struct lxc_conf
*c
, void *data
)
2873 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2876 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2877 int inlen
, struct lxc_conf
*c
,
2880 return lxc_get_conf_int(c
, retv
, inlen
,
2881 c
->lsm_aa_allow_incomplete
);
2884 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2885 struct lxc_conf
*c
, void *data
)
2887 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2890 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2891 * just the value(s) will be printed. Since there still could be more than one,
2892 * it is newline-separated.
2893 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2895 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2896 * 'lxc.cgroup.subsystem.key = value' format.
2898 static int __get_config_cgroup_controller(const char *key
, char *retv
,
2899 int inlen
, struct lxc_conf
*c
,
2903 size_t namespaced_token_len
;
2904 char *global_token
, *namespaced_token
;
2905 struct lxc_list
*it
;
2907 bool get_all
= false;
2912 memset(retv
, 0, inlen
);
2914 if (version
== CGROUP2_SUPER_MAGIC
) {
2915 global_token
= "lxc.cgroup2";
2916 namespaced_token
= "lxc.cgroup2.";
2917 namespaced_token_len
= sizeof("lxc.cgroup2.") - 1;;
2918 } else if (version
== CGROUP_SUPER_MAGIC
) {
2919 global_token
= "lxc.cgroup";
2920 namespaced_token
= "lxc.cgroup.";
2921 namespaced_token_len
= sizeof("lxc.cgroup.") - 1;;
2926 if (strcmp(key
, global_token
) == 0)
2928 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
2929 key
+= namespaced_token_len
;
2933 lxc_list_for_each(it
, &c
->cgroup
) {
2934 struct lxc_cgroup
*cg
= it
->elem
;
2937 if (version
!= cg
->version
)
2940 strprint(retv
, inlen
, "%s.%s = %s\n",
2941 global_token
, cg
->subsystem
, cg
->value
);
2942 } else if (!strcmp(cg
->subsystem
, key
)) {
2943 strprint(retv
, inlen
, "%s\n", cg
->value
);
2950 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
2951 struct lxc_conf
*c
, void *data
)
2953 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
2954 CGROUP_SUPER_MAGIC
);
2957 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
2958 struct lxc_conf
*c
, void *data
)
2960 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
2961 CGROUP2_SUPER_MAGIC
);
2964 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
2965 struct lxc_conf
*lxc_conf
, void *data
)
2973 memset(retv
, 0, inlen
);
2975 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
2980 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2981 struct lxc_conf
*c
, void *data
)
2983 struct lxc_list
*it
;
2984 int len
, listlen
, ret
;
2986 /* "u 1000 1000000 65536"
2988 * let's render this as
3006 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3007 char buf
[__LXC_IDMAP_STR_BUF
];
3012 memset(retv
, 0, inlen
);
3014 listlen
= lxc_list_len(&c
->id_map
);
3015 lxc_list_for_each(it
, &c
->id_map
)
3017 struct id_map
*map
= it
->elem
;
3018 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3019 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3020 map
->nsid
, map
->hostid
, map
->range
);
3021 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3024 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3029 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3030 struct lxc_conf
*c
, void *data
)
3033 v
= lxc_log_priority_to_string(c
->loglevel
);
3034 return lxc_get_conf_str(retv
, inlen
, v
);
3037 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3038 struct lxc_conf
*c
, void *data
)
3040 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3043 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3044 struct lxc_conf
*c
, void *data
)
3046 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3049 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3050 struct lxc_conf
*c
, void *data
)
3052 int len
, fulllen
= 0;
3053 const char *sep
= "";
3058 memset(retv
, 0, inlen
);
3060 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3063 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3064 case LXC_AUTO_PROC_MIXED
:
3065 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3068 case LXC_AUTO_PROC_RW
:
3069 strprint(retv
, inlen
, "%sproc:rw", sep
);
3076 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3077 case LXC_AUTO_SYS_RO
:
3078 strprint(retv
, inlen
, "%ssys:ro", sep
);
3081 case LXC_AUTO_SYS_RW
:
3082 strprint(retv
, inlen
, "%ssys:rw", sep
);
3085 case LXC_AUTO_SYS_MIXED
:
3086 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3093 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3094 case LXC_AUTO_CGROUP_NOSPEC
:
3095 strprint(retv
, inlen
, "%scgroup", sep
);
3097 case LXC_AUTO_CGROUP_MIXED
:
3098 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3100 case LXC_AUTO_CGROUP_RO
:
3101 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3103 case LXC_AUTO_CGROUP_RW
:
3104 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3106 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3107 strprint(retv
, inlen
, "%scgroup-full", sep
);
3109 case LXC_AUTO_CGROUP_FULL_MIXED
:
3110 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3112 case LXC_AUTO_CGROUP_FULL_RO
:
3113 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3115 case LXC_AUTO_CGROUP_FULL_RW
:
3116 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3125 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3126 struct lxc_conf
*c
, void *data
)
3128 int len
, fulllen
= 0;
3129 struct lxc_list
*it
;
3134 memset(retv
, 0, inlen
);
3136 lxc_list_for_each(it
, &c
->mount_list
) {
3137 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3143 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3144 struct lxc_conf
*c
, void *data
)
3146 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3149 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3150 struct lxc_conf
*c
, void *data
)
3152 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3155 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3156 struct lxc_conf
*c
, void *data
)
3158 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3161 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3162 struct lxc_conf
*c
, void *data
)
3164 return lxc_get_conf_str(
3166 c
->utsname
? c
->utsname
->nodename
: NULL
);
3169 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3170 struct lxc_conf
*c
, void *data
)
3173 int len
, fulllen
= 0, found
= -1;
3174 struct lxc_list
*it
;
3177 subkey
= strchr(key
, '.');
3179 subkey
= strchr(subkey
+ 1, '.');
3185 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3186 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3197 memset(retv
, 0, inlen
);
3199 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3200 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3205 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3206 struct lxc_conf
*c
, void *data
)
3208 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3211 static int get_config_net(const char *key
, char *retv
, int inlen
,
3212 struct lxc_conf
*c
, void *data
)
3214 int len
, fulllen
= 0;
3215 struct lxc_list
*it
;
3220 memset(retv
, 0, inlen
);
3222 lxc_list_for_each(it
, &c
->network
) {
3223 struct lxc_netdev
*n
= it
->elem
;
3224 const char *t
= lxc_net_type_to_str(n
->type
);
3225 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3231 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3232 struct lxc_conf
*c
, void *data
)
3234 int len
, fulllen
= 0;
3235 struct lxc_list
*it
;
3240 memset(retv
, 0, inlen
);
3242 lxc_list_for_each(it
, &c
->caps
) {
3243 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3249 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3250 struct lxc_conf
*c
, void *data
)
3252 int len
, fulllen
= 0;
3253 struct lxc_list
*it
;
3258 memset(retv
, 0, inlen
);
3260 lxc_list_for_each(it
, &c
->keepcaps
) {
3261 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3267 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3268 struct lxc_conf
*c
, void *data
)
3270 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3273 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3274 struct lxc_conf
*c
, void *data
)
3276 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3279 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3280 struct lxc_conf
*c
, void *data
)
3282 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3286 static int get_config_console_buffer_size(const char *key
, char *retv
,
3287 int inlen
, struct lxc_conf
*c
,
3290 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3293 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3294 int inlen
, struct lxc_conf
*c
,
3297 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3300 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3301 struct lxc_conf
*c
, void *data
)
3303 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3306 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3307 struct lxc_conf
*c
, void *data
)
3309 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3312 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3313 struct lxc_conf
*c
, void *data
)
3315 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3318 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3319 struct lxc_conf
*c
, void *data
)
3321 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3324 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3325 struct lxc_conf
*c
, void *data
)
3327 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3330 static int get_config_start(const char *key
, char *retv
, int inlen
,
3331 struct lxc_conf
*c
, void *data
)
3333 if (strcmp(key
+ 10, "auto") == 0)
3334 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3335 else if (strcmp(key
+ 10, "delay") == 0)
3336 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3337 else if (strcmp(key
+ 10, "order") == 0)
3338 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3343 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3344 struct lxc_conf
*c
, void *data
)
3346 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3349 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3350 struct lxc_conf
*c
, void *data
)
3352 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3355 static int get_config_group(const char *key
, char *retv
, int inlen
,
3356 struct lxc_conf
*c
, void *data
)
3358 int len
, fulllen
= 0;
3359 struct lxc_list
*it
;
3364 memset(retv
, 0, inlen
);
3366 lxc_list_for_each(it
, &c
->groups
) {
3367 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3373 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3374 struct lxc_conf
*c
, void *data
)
3376 int len
, fulllen
= 0;
3377 struct lxc_list
*it
;
3382 memset(retv
, 0, inlen
);
3384 lxc_list_for_each(it
, &c
->environment
) {
3385 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3391 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3392 struct lxc_conf
*c
, void *data
)
3394 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3397 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3398 struct lxc_conf
*c
, void *data
)
3400 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3403 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3404 struct lxc_conf
*c
, void *data
)
3406 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3409 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3410 struct lxc_conf
*c
, void *data
)
3412 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3415 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3416 struct lxc_conf
*c
, void *data
)
3418 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3421 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3422 struct lxc_conf
*c
, void *data
)
3424 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3427 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3428 struct lxc_conf
*c
, void *data
)
3430 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3433 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3434 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3435 * printed, in 'lxc.prlimit.resource = value' format.
3437 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3438 struct lxc_conf
*c
, void *data
)
3440 int fulllen
= 0, len
;
3441 bool get_all
= false;
3442 struct lxc_list
*it
;
3447 memset(retv
, 0, inlen
);
3449 if (!strcmp(key
, "lxc.prlimit"))
3451 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3456 lxc_list_for_each(it
, &c
->limits
) {
3457 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3458 integers or the word
3461 struct lxc_limit
*lim
= it
->elem
;
3463 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3464 memcpy(buf
, "unlimited", sizeof("unlimited"));
3465 partlen
= sizeof("unlimited") - 1;
3467 partlen
= sprintf(buf
, "%" PRIu64
,
3468 (uint64_t)lim
->limit
.rlim_cur
);
3470 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3471 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3472 memcpy(buf
+ partlen
, ":unlimited",
3473 sizeof(":unlimited"));
3475 sprintf(buf
+ partlen
, ":%" PRIu64
,
3476 (uint64_t)lim
->limit
.rlim_max
);
3480 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3481 lim
->resource
, buf
);
3482 } else if (!strcmp(lim
->resource
, key
)) {
3483 strprint(retv
, inlen
, "%s", buf
);
3490 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3491 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3492 * entries will be printed, in 'lxc.sysctl.key = value' format.
3494 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3495 struct lxc_conf
*c
, void *data
)
3498 struct lxc_list
*it
;
3500 bool get_all
= false;
3505 memset(retv
, 0, inlen
);
3507 if (strcmp(key
, "lxc.sysctl") == 0)
3509 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3510 key
+= sizeof("lxc.sysctl.") - 1;
3514 lxc_list_for_each(it
, &c
->sysctls
) {
3515 struct lxc_sysctl
*elem
= it
->elem
;
3517 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3518 elem
->key
, elem
->value
);
3519 } else if (strcmp(elem
->key
, key
) == 0) {
3520 strprint(retv
, inlen
, "%s", elem
->value
);
3527 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3528 struct lxc_conf
*c
, void *data
)
3530 struct lxc_list
*it
;
3533 bool get_all
= false;
3538 memset(retv
, 0, inlen
);
3540 if (strcmp(key
, "lxc.proc") == 0)
3542 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3543 key
+= sizeof("lxc.proc.") - 1;
3547 lxc_list_for_each(it
, &c
->procs
) {
3548 struct lxc_proc
*proc
= it
->elem
;
3551 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3552 proc
->filename
, proc
->value
);
3553 } else if (strcmp(proc
->filename
, key
) == 0) {
3554 strprint(retv
, inlen
, "%s", proc
->value
);
3561 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3562 struct lxc_conf
*c
, void *data
)
3570 memset(retv
, 0, inlen
);
3572 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3573 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3574 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3580 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3581 struct lxc_conf
*c
, void *data
)
3589 memset(retv
, 0, inlen
);
3591 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3592 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3593 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3599 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3600 struct lxc_conf
*c
, void *data
)
3603 const char *namespace;
3609 memset(retv
, 0, inlen
);
3611 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
3612 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3616 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3621 /* Callbacks to clear config items. */
3622 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3625 c
->personality
= -1;
3629 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3636 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3643 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3651 static inline int clr_config_apparmor_profile(const char *key
,
3652 struct lxc_conf
*c
, void *data
)
3654 free(c
->lsm_aa_profile
);
3655 c
->lsm_aa_profile
= NULL
;
3659 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3663 c
->lsm_aa_allow_incomplete
= 0;
3667 static inline int clr_config_selinux_context(const char *key
,
3668 struct lxc_conf
*c
, void *data
)
3670 free(c
->lsm_se_context
);
3671 c
->lsm_se_context
= NULL
;
3675 static inline int clr_config_cgroup_controller(const char *key
,
3676 struct lxc_conf
*c
, void *data
)
3678 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3681 static inline int clr_config_cgroup2_controller(const char *key
,
3682 struct lxc_conf
*c
, void *data
)
3684 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3687 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3690 if (lxc_conf
->cgroup_meta
.dir
) {
3691 free(lxc_conf
->cgroup_meta
.dir
);
3692 lxc_conf
->cgroup_meta
.dir
= NULL
;
3698 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3701 return lxc_clear_idmaps(c
);
3704 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3707 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3711 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3719 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3722 return lxc_clear_mount_entries(c
);
3725 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3728 return lxc_clear_automounts(c
);
3731 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3739 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3742 free(c
->rootfs
.path
);
3743 c
->rootfs
.path
= NULL
;
3747 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3750 free(c
->rootfs
.mount
);
3751 c
->rootfs
.mount
= NULL
;
3755 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3758 free(c
->rootfs
.options
);
3759 c
->rootfs
.options
= NULL
;
3763 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3771 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3774 return lxc_clear_hooks(c
, key
);
3777 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
3780 /* default to legacy hooks version */
3781 c
->hooks_version
= 0;
3785 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3788 lxc_free_networks(&c
->network
);
3793 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3796 return lxc_clear_config_caps(c
);
3799 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3802 return lxc_clear_config_keepcaps(c
);
3805 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3808 free(c
->console
.path
);
3809 c
->console
.path
= NULL
;
3813 static inline int clr_config_console_logfile(const char *key
,
3814 struct lxc_conf
*c
, void *data
)
3816 free(c
->console
.log_path
);
3817 c
->console
.log_path
= NULL
;
3821 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3824 c
->console
.log_rotate
= 0;
3828 static inline int clr_config_console_buffer_size(const char *key
,
3829 struct lxc_conf
*c
, void *data
)
3831 c
->console
.buffer_size
= 0;
3835 static inline int clr_config_console_buffer_logfile(const char *key
,
3839 free(c
->console
.buffer_log_file
);
3840 c
->console
.buffer_log_file
= NULL
;
3844 static inline int clr_config_seccomp_profile(const char *key
,
3845 struct lxc_conf
*c
, void *data
)
3852 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3859 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3866 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3869 c
->rebootsignal
= 0;
3873 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3880 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3883 if (strcmp(key
+ 10, "auto") == 0)
3885 else if (strcmp(key
+ 10, "delay") == 0)
3887 else if (strcmp(key
+ 10, "order") == 0)
3893 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3901 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3904 c
->monitor_unshare
= 0;
3908 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3911 return lxc_clear_groups(c
);
3914 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3917 return lxc_clear_environment(c
);
3920 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
3923 free(c
->execute_cmd
);
3924 c
->execute_cmd
= NULL
;
3928 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3936 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
3944 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3951 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3958 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3965 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3968 c
->no_new_privs
= false;
3972 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3975 return lxc_clear_limits(c
, key
);
3978 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
3981 return lxc_clear_sysctls(c
, key
);
3984 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
3987 return lxc_clear_procs(c
, key
);
3990 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3993 lxc_clear_includes(c
);
3997 static int clr_config_namespace_clone(const char *key
,
3998 struct lxc_conf
*lxc_conf
, void *data
)
4000 lxc_conf
->ns_clone
= 0;
4004 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4007 lxc_conf
->ns_keep
= 0;
4011 static int clr_config_namespace_share(const char *key
,
4012 struct lxc_conf
*lxc_conf
, void *data
)
4015 const char *namespace;
4017 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
4018 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4022 free(lxc_conf
->ns_share
[ns_idx
]);
4023 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4028 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4029 struct lxc_conf
*c
, void *data
)
4034 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4035 struct lxc_conf
*lxc_conf
,
4037 char **deindexed_key
)
4040 unsigned int tmpidx
;
4042 char *copy
, *idx_start
, *idx_end
;
4043 struct lxc_config_t
*config
= NULL
;
4045 /* check that this is a sensible network key */
4046 if (strncmp("lxc.net.", key
, 8)) {
4047 ERROR("Invalid network configuration key \"%s\"", key
);
4053 ERROR("Failed to duplicate string \"%s\"", key
);
4058 if (!isdigit(*(key
+ 8))) {
4059 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4063 /* beginning of index string */
4064 idx_start
= (copy
+ 7);
4067 /* end of index string */
4068 idx_end
= strchr((copy
+ 8), '.');
4072 /* parse current index */
4073 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4075 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4076 idx_start
+ 1, strerror(-ret
));
4081 /* This, of course is utterly nonsensical on so many levels, but
4082 * better safe than sorry.
4083 * (Checking for INT_MAX here is intentional.)
4085 if (tmpidx
== INT_MAX
) {
4086 SYSERROR("Number of configured networks would overflow the "
4092 numstrlen
= strlen((idx_start
+ 1));
4094 /* repair configuration key */
4097 /* lxc.net.<idx>.<subkey> */
4100 if (strlen(idx_end
+ 1) == 0) {
4101 ERROR("No subkey in network configuration key \"%s\"", key
);
4105 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4106 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4108 config
= lxc_get_config(copy
);
4110 ERROR("Unknown network configuration key \"%s\"", key
);
4116 *deindexed_key
= copy
;
4125 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4126 * found. So we make sure next comes an integer, find the right callback (by
4127 * rewriting the key), and call it.
4129 static int set_config_net_nic(const char *key
, const char *value
,
4130 struct lxc_conf
*lxc_conf
, void *data
)
4133 const char *idxstring
;
4134 struct lxc_config_t
*config
;
4135 struct lxc_netdev
*netdev
;
4137 char *deindexed_key
= NULL
;
4139 idxstring
= key
+ 8;
4140 if (!isdigit(*idxstring
))
4143 if (lxc_config_value_empty(value
))
4144 return clr_config_net_nic(key
, lxc_conf
, data
);
4146 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4147 if (!config
|| idx
< 0)
4150 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4152 free(deindexed_key
);
4156 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4157 free(deindexed_key
);
4161 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4165 const char *idxstring
;
4166 struct lxc_config_t
*config
;
4167 struct lxc_netdev
*netdev
;
4169 char *deindexed_key
= NULL
;
4171 idxstring
= key
+ 8;
4172 if (!isdigit(*idxstring
))
4175 /* The left conjunct is pretty self-explanatory. The right conjunct
4176 * checks whether the two pointers are equal. If they are we know that
4177 * this is not a key that is namespaced any further and so we are
4178 * supposed to clear the whole network.
4180 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4181 unsigned int rmnetdevidx
;
4183 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4186 /* Remove network from network list. */
4187 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4191 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4192 if (!config
|| idx
< 0)
4195 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4197 free(deindexed_key
);
4201 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4202 free(deindexed_key
);
4206 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4209 struct lxc_netdev
*netdev
= data
;
4219 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4222 struct lxc_netdev
*netdev
= data
;
4227 netdev
->name
[0] = '\0';
4232 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4235 struct lxc_netdev
*netdev
= data
;
4245 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4248 struct lxc_netdev
*netdev
= data
;
4253 netdev
->link
[0] = '\0';
4258 static int clr_config_net_macvlan_mode(const char *key
,
4259 struct lxc_conf
*lxc_conf
, void *data
)
4261 struct lxc_netdev
*netdev
= data
;
4266 if (netdev
->type
!= LXC_NET_MACVLAN
)
4269 netdev
->priv
.macvlan_attr
.mode
= -1;
4274 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4277 struct lxc_netdev
*netdev
= data
;
4282 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4287 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4290 struct lxc_netdev
*netdev
= data
;
4295 free(netdev
->upscript
);
4296 netdev
->upscript
= NULL
;
4301 static int clr_config_net_script_down(const char *key
,
4302 struct lxc_conf
*lxc_conf
, void *data
)
4304 struct lxc_netdev
*netdev
= data
;
4309 free(netdev
->downscript
);
4310 netdev
->downscript
= NULL
;
4315 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4318 struct lxc_netdev
*netdev
= data
;
4323 free(netdev
->hwaddr
);
4324 netdev
->hwaddr
= NULL
;
4329 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4332 struct lxc_netdev
*netdev
= data
;
4343 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4346 struct lxc_netdev
*netdev
= data
;
4351 netdev
->priv
.vlan_attr
.vid
= 0;
4356 static int clr_config_net_ipv4_gateway(const char *key
,
4357 struct lxc_conf
*lxc_conf
, void *data
)
4359 struct lxc_netdev
*netdev
= data
;
4364 free(netdev
->ipv4_gateway
);
4365 netdev
->ipv4_gateway
= NULL
;
4370 static int clr_config_net_ipv4_address(const char *key
,
4371 struct lxc_conf
*lxc_conf
, void *data
)
4373 struct lxc_netdev
*netdev
= data
;
4374 struct lxc_list
*cur
, *next
;
4379 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4388 static int clr_config_net_ipv6_gateway(const char *key
,
4389 struct lxc_conf
*lxc_conf
, void *data
)
4391 struct lxc_netdev
*netdev
= data
;
4396 free(netdev
->ipv6_gateway
);
4397 netdev
->ipv6_gateway
= NULL
;
4402 static int clr_config_net_ipv6_address(const char *key
,
4403 struct lxc_conf
*lxc_conf
, void *data
)
4405 struct lxc_netdev
*netdev
= data
;
4406 struct lxc_list
*cur
, *next
;
4411 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4420 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4421 struct lxc_conf
*c
, void *data
)
4424 const char *idxstring
;
4425 struct lxc_config_t
*config
;
4426 struct lxc_netdev
*netdev
;
4428 char *deindexed_key
= NULL
;
4430 idxstring
= key
+ 8;
4431 if (!isdigit(*idxstring
))
4434 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4435 if (!config
|| idx
< 0)
4438 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4440 free(deindexed_key
);
4444 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4445 free(deindexed_key
);
4449 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4450 struct lxc_conf
*c
, void *data
)
4454 struct lxc_netdev
*netdev
= data
;
4459 memset(retv
, 0, inlen
);
4464 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4469 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4470 struct lxc_conf
*c
, void *data
)
4474 struct lxc_netdev
*netdev
= data
;
4479 memset(retv
, 0, inlen
);
4484 if (netdev
->flags
& IFF_UP
)
4485 strprint(retv
, inlen
, "up");
4490 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4491 struct lxc_conf
*c
, void *data
)
4495 struct lxc_netdev
*netdev
= data
;
4500 memset(retv
, 0, inlen
);
4505 if (netdev
->link
[0] != '\0')
4506 strprint(retv
, inlen
, "%s", netdev
->link
);
4511 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4512 struct lxc_conf
*c
, void *data
)
4516 struct lxc_netdev
*netdev
= data
;
4521 memset(retv
, 0, inlen
);
4526 if (netdev
->name
[0] != '\0')
4527 strprint(retv
, inlen
, "%s", netdev
->name
);
4532 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4533 struct lxc_conf
*c
, void *data
)
4538 struct lxc_netdev
*netdev
= data
;
4543 memset(retv
, 0, inlen
);
4548 if (netdev
->type
!= LXC_NET_MACVLAN
)
4551 switch (netdev
->priv
.macvlan_attr
.mode
) {
4552 case MACVLAN_MODE_PRIVATE
:
4555 case MACVLAN_MODE_VEPA
:
4558 case MACVLAN_MODE_BRIDGE
:
4561 case MACVLAN_MODE_PASSTHRU
:
4569 strprint(retv
, inlen
, "%s", mode
);
4574 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4575 struct lxc_conf
*c
, void *data
)
4579 struct lxc_netdev
*netdev
= data
;
4584 memset(retv
, 0, inlen
);
4589 if (netdev
->type
!= LXC_NET_VETH
)
4592 strprint(retv
, inlen
, "%s",
4593 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4594 ? netdev
->priv
.veth_attr
.pair
4595 : netdev
->priv
.veth_attr
.veth1
);
4600 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4601 struct lxc_conf
*c
, void *data
)
4605 struct lxc_netdev
*netdev
= data
;
4610 memset(retv
, 0, inlen
);
4615 if (netdev
->upscript
)
4616 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4621 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4622 struct lxc_conf
*c
, void *data
)
4626 struct lxc_netdev
*netdev
= data
;
4631 memset(retv
, 0, inlen
);
4636 if (netdev
->downscript
)
4637 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4642 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4643 struct lxc_conf
*c
, void *data
)
4647 struct lxc_netdev
*netdev
= data
;
4652 memset(retv
, 0, inlen
);
4658 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4663 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4664 struct lxc_conf
*c
, void *data
)
4668 struct lxc_netdev
*netdev
= data
;
4673 memset(retv
, 0, inlen
);
4679 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4684 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4685 struct lxc_conf
*c
, void *data
)
4689 struct lxc_netdev
*netdev
= data
;
4694 memset(retv
, 0, inlen
);
4699 if (netdev
->type
!= LXC_NET_VLAN
)
4702 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4707 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4708 struct lxc_conf
*c
, void *data
)
4711 char buf
[INET_ADDRSTRLEN
];
4713 struct lxc_netdev
*netdev
= data
;
4718 memset(retv
, 0, inlen
);
4723 if (netdev
->ipv4_gateway_auto
) {
4724 strprint(retv
, inlen
, "auto");
4725 } else if (netdev
->ipv4_gateway
) {
4726 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4727 strprint(retv
, inlen
, "%s", buf
);
4733 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4734 struct lxc_conf
*c
, void *data
)
4738 char buf
[INET_ADDRSTRLEN
];
4739 struct lxc_list
*it
;
4741 struct lxc_netdev
*netdev
= data
;
4746 memset(retv
, 0, inlen
);
4751 listlen
= lxc_list_len(&netdev
->ipv4
);
4752 lxc_list_for_each(it
, &netdev
->ipv4
) {
4753 struct lxc_inetdev
*i
= it
->elem
;
4754 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4755 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4756 (listlen
-- > 1) ? "\n" : "");
4762 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4763 struct lxc_conf
*c
, void *data
)
4766 char buf
[INET6_ADDRSTRLEN
];
4768 struct lxc_netdev
*netdev
= data
;
4773 memset(retv
, 0, inlen
);
4778 if (netdev
->ipv6_gateway_auto
) {
4779 strprint(retv
, inlen
, "auto");
4780 } else if (netdev
->ipv6_gateway
) {
4781 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4782 strprint(retv
, inlen
, "%s", buf
);
4788 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4789 struct lxc_conf
*c
, void *data
)
4793 char buf
[INET6_ADDRSTRLEN
];
4794 struct lxc_list
*it
;
4796 struct lxc_netdev
*netdev
= data
;
4801 memset(retv
, 0, inlen
);
4806 listlen
= lxc_list_len(&netdev
->ipv6
);
4807 lxc_list_for_each(it
, &netdev
->ipv6
) {
4808 struct lxc_inet6dev
*i
= it
->elem
;
4809 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4810 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4811 (listlen
-- > 1) ? "\n" : "");
4817 int lxc_list_config_items(char *retv
, int inlen
)
4826 memset(retv
, 0, inlen
);
4828 for (i
= 0; i
< config_size
; i
++) {
4829 char *s
= config
[i
].name
;
4831 if (s
[strlen(s
) - 1] == '.')
4834 strprint(retv
, inlen
, "%s\n", s
);
4840 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4849 memset(retv
, 0, inlen
);
4851 if (!strcmp(key
, "lxc.apparmor")) {
4852 strprint(retv
, inlen
, "allow_incomplete\n");
4853 strprint(retv
, inlen
, "profile\n");
4854 } else if (!strcmp(key
, "lxc.cgroup")) {
4855 strprint(retv
, inlen
, "dir\n");
4856 } else if (!strcmp(key
, "lxc.selinux")) {
4857 strprint(retv
, inlen
, "context\n");
4858 } else if (!strcmp(key
, "lxc.mount")) {
4859 strprint(retv
, inlen
, "auto\n");
4860 strprint(retv
, inlen
, "entry\n");
4861 strprint(retv
, inlen
, "fstab\n");
4862 } else if (!strcmp(key
, "lxc.rootfs")) {
4863 strprint(retv
, inlen
, "mount\n");
4864 strprint(retv
, inlen
, "options\n");
4865 strprint(retv
, inlen
, "path\n");
4866 } else if (!strcmp(key
, "lxc.uts")) {
4867 strprint(retv
, inlen
, "name\n");
4868 } else if (!strcmp(key
, "lxc.hook")) {
4869 strprint(retv
, inlen
, "autodev\n");
4870 strprint(retv
, inlen
, "clone\n");
4871 strprint(retv
, inlen
, "destroy\n");
4872 strprint(retv
, inlen
, "mount\n");
4873 strprint(retv
, inlen
, "post-stop\n");
4874 strprint(retv
, inlen
, "pre-mount\n");
4875 strprint(retv
, inlen
, "pre-start\n");
4876 strprint(retv
, inlen
, "start-host\n");
4877 strprint(retv
, inlen
, "start\n");
4878 strprint(retv
, inlen
, "stop\n");
4879 } else if (!strcmp(key
, "lxc.cap")) {
4880 strprint(retv
, inlen
, "drop\n");
4881 strprint(retv
, inlen
, "keep\n");
4882 } else if (!strcmp(key
, "lxc.console")) {
4883 strprint(retv
, inlen
, "logfile\n");
4884 strprint(retv
, inlen
, "path\n");
4885 } else if (!strcmp(key
, "lxc.seccomp")) {
4886 strprint(retv
, inlen
, "profile\n");
4887 } else if (!strcmp(key
, "lxc.signal")) {
4888 strprint(retv
, inlen
, "halt\n");
4889 strprint(retv
, inlen
, "reboot\n");
4890 strprint(retv
, inlen
, "stop\n");
4891 } else if (!strcmp(key
, "lxc.start")) {
4892 strprint(retv
, inlen
, "auto\n");
4893 strprint(retv
, inlen
, "delay\n");
4894 strprint(retv
, inlen
, "order\n");
4895 } else if (!strcmp(key
, "lxc.monitor")) {
4896 strprint(retv
, inlen
, "unshare\n");
4904 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4907 const char *idxstring
;
4908 struct lxc_netdev
*netdev
;
4912 idxstring
= key
+ 8;
4913 if (!isdigit(*idxstring
))
4916 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
4920 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4927 memset(retv
, 0, inlen
);
4929 strprint(retv
, inlen
, "type\n");
4930 strprint(retv
, inlen
, "script.up\n");
4931 strprint(retv
, inlen
, "script.down\n");
4932 if (netdev
->type
!= LXC_NET_EMPTY
) {
4933 strprint(retv
, inlen
, "flags\n");
4934 strprint(retv
, inlen
, "link\n");
4935 strprint(retv
, inlen
, "name\n");
4936 strprint(retv
, inlen
, "hwaddr\n");
4937 strprint(retv
, inlen
, "mtu\n");
4938 strprint(retv
, inlen
, "ipv6.address\n");
4939 strprint(retv
, inlen
, "ipv6.gateway\n");
4940 strprint(retv
, inlen
, "ipv4.address\n");
4941 strprint(retv
, inlen
, "ipv4.gateway\n");
4944 switch (netdev
->type
) {
4946 strprint(retv
, inlen
, "veth.pair\n");
4948 case LXC_NET_MACVLAN
:
4949 strprint(retv
, inlen
, "macvlan.mode\n");
4952 strprint(retv
, inlen
, "vlan.id\n");