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_legacy.h"
51 #include "confile_utils.h"
53 #include "lxcseccomp.h"
62 #include <../include/ifaddrs.h>
65 #if HAVE_SYS_PERSONALITY_H
66 #include <sys/personality.h>
69 lxc_log_define(lxc_confile
, lxc
);
71 #define lxc_config_define(name) \
72 static int set_config_##name(const char *, const char *, \
73 struct lxc_conf *, void *); \
74 static int get_config_##name(const char *, char *, int, \
75 struct lxc_conf *, void *); \
76 static int clr_config_##name(const char *, struct lxc_conf *, \
80 lxc_config_define(personality
);
81 lxc_config_define(pty_max
);
82 lxc_config_define(tty_max
);
83 lxc_config_define(tty_dir
);
84 lxc_config_define(apparmor_profile
);
85 lxc_config_define(apparmor_allow_incomplete
);
86 lxc_config_define(selinux_context
);
87 lxc_config_define(cgroup
);
88 lxc_config_define(idmaps
);
89 lxc_config_define(log_level
);
90 lxc_config_define(log_file
);
91 lxc_config_define(mount
);
92 lxc_config_define(mount_auto
);
93 lxc_config_define(mount_fstab
);
94 lxc_config_define(rootfs_mount
);
95 lxc_config_define(rootfs_options
);
96 lxc_config_define(rootfs_backend
);
97 lxc_config_define(rootfs_path
);
98 lxc_config_define(uts_name
);
99 lxc_config_define(hooks
);
100 lxc_config_define(net_type
);
101 lxc_config_define(net_flags
);
102 lxc_config_define(net_link
);
103 lxc_config_define(net_name
);
104 lxc_config_define(net_veth_pair
);
105 lxc_config_define(net_macvlan_mode
);
106 lxc_config_define(net_hwaddr
);
107 lxc_config_define(net_vlan_id
);
108 lxc_config_define(net_mtu
);
109 lxc_config_define(net_ipv4_address
);
110 lxc_config_define(net_ipv4_gateway
);
111 lxc_config_define(net_script_up
);
112 lxc_config_define(net_script_down
);
113 lxc_config_define(net_ipv6_address
);
114 lxc_config_define(net_ipv6_gateway
);
115 lxc_config_define(net_nic
);
116 lxc_config_define(net
);
117 lxc_config_define(cap_drop
);
118 lxc_config_define(cap_keep
);
119 lxc_config_define(console_logfile
);
120 lxc_config_define(console_path
);
121 lxc_config_define(seccomp_profile
);
122 lxc_config_define(includefiles
);
123 lxc_config_define(autodev
);
124 lxc_config_define(signal_halt
);
125 lxc_config_define(signal_reboot
);
126 lxc_config_define(signal_stop
);
127 lxc_config_define(start
);
128 lxc_config_define(monitor
);
129 lxc_config_define(group
);
130 lxc_config_define(environment
);
131 lxc_config_define(init_cmd
);
132 lxc_config_define(init_uid
);
133 lxc_config_define(init_gid
);
134 lxc_config_define(ephemeral
);
135 lxc_config_define(log_syslog
);
136 lxc_config_define(no_new_privs
);
137 lxc_config_define(prlimit
);
139 static struct lxc_config_t config
[] = {
140 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
141 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
142 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
143 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
145 /* [START]: REMOVE IN LXC 3.0 */
146 { "lxc.pts", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
147 { "lxc.devttydir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
148 { "lxc.tty", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
149 /* [END]: REMOVE IN LXC 3.0 */
151 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
152 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
153 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
155 /* [START]: REMOVE IN LXC 3.0 */
156 { "lxc.aa_profile", set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
157 { "lxc.aa_allow_incomplete", set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
158 { "lxc.se_context", set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
159 /* [END]: REMOVE IN LXC 3.0 */
161 { "lxc.cgroup", set_config_cgroup
, get_config_cgroup
, clr_config_cgroup
, },
162 { "lxc.id_map", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
163 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
164 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
165 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
166 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
167 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
168 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
170 /* [START]: REMOVE IN LXC 3.0 */
171 { "lxc.mount", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
172 { "lxc.rootfs.backend", set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
173 { "lxc.rootfs", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
174 { "lxc.utsname", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
175 /* [END]: REMOVE IN LXC 3.0 */
177 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
178 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
179 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
180 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
181 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
182 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
183 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
184 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
185 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 /* [START]: REMOVE IN LXC 3.0 */
190 { "lxc.network.type", set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
191 { "lxc.network.flags", set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
192 { "lxc.network.link", set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
193 { "lxc.network.name", set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
194 { "lxc.network.macvlan.mode", set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
195 { "lxc.network.veth.pair", set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
196 { "lxc.network.script.up", set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
197 { "lxc.network.script.down", set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
198 { "lxc.network.hwaddr", set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
199 { "lxc.network.mtu", set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
200 { "lxc.network.vlan.id", set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
201 { "lxc.network.ipv4.gateway", set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
202 { "lxc.network.ipv4", set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
203 { "lxc.network.ipv6.gateway", set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
204 { "lxc.network.ipv6", set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
205 { "lxc.network.", set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
206 { "lxc.network", set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
207 /* [END]: REMOVE IN LXC 3.0 */
209 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
210 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
211 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
212 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
213 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
214 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
215 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
216 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
217 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
218 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
219 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
220 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
221 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
222 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
223 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
224 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
225 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
226 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
227 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
228 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
229 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
230 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
231 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
232 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
234 /* [START]: REMOVE IN LXC 3.0 */
235 { "lxc.seccomp", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
236 { "lxc.console", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
237 { "lxc.haltsignal", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
238 { "lxc.rebootsignal", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
239 { "lxc.stopsignal", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
240 /* [END]: REMOVE IN LXC 3.0 */
242 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
243 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
244 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
245 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
246 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
247 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
248 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
249 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
250 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
251 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
252 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
254 /* [START]: REMOVE IN LXC 3.0 */
255 { "lxc.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
256 { "lxc.loglevel", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
257 { "lxc.logfile", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
258 { "lxc.init_cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
259 { "lxc.init_uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
260 { "lxc.init_gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
261 { "lxc.limit", set_config_limit
, get_config_limit
, clr_config_limit
, },
262 /* [END]: REMOVE IN LXC 3.0 */
264 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
265 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
266 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
267 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
268 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
269 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
270 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
278 static const struct signame signames
[] = {
311 { SIGSTKFLT
, "STKFLT" },
326 { SIGVTALRM
, "VTALRM" },
332 { SIGWINCH
, "WINCH" },
350 { SIGUNUSED
, "UNUSED" },
357 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
359 extern struct lxc_config_t
*lxc_getconfig(const char *key
)
363 for (i
= 0; i
< config_size
; i
++)
364 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
369 static int set_config_net(const char *key
, const char *value
,
370 struct lxc_conf
*lxc_conf
, void *data
)
372 if (!lxc_config_value_empty(value
)) {
373 ERROR("lxc.net must not have a value");
377 return clr_config_net(key
, lxc_conf
, data
);
380 static int set_config_net_type(const char *key
, const char *value
,
381 struct lxc_conf
*lxc_conf
, void *data
)
383 struct lxc_netdev
*netdev
;
385 if (lxc_config_value_empty(value
))
386 return clr_config_net_type(key
, lxc_conf
, data
);
395 if (!strcmp(value
, "veth")) {
396 netdev
->type
= LXC_NET_VETH
;
397 } else if (!strcmp(value
, "macvlan")) {
398 netdev
->type
= LXC_NET_MACVLAN
;
399 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
401 } else if (!strcmp(value
, "vlan")) {
402 netdev
->type
= LXC_NET_VLAN
;
403 } else if (!strcmp(value
, "phys")) {
404 netdev
->type
= LXC_NET_PHYS
;
405 } else if (!strcmp(value
, "empty")) {
406 netdev
->type
= LXC_NET_EMPTY
;
407 } else if (!strcmp(value
, "none")) {
408 netdev
->type
= LXC_NET_NONE
;
410 ERROR("invalid network type %s", value
);
417 static int set_config_net_flags(const char *key
, const char *value
,
418 struct lxc_conf
*lxc_conf
, void *data
)
420 struct lxc_netdev
*netdev
;
422 if (lxc_config_value_empty(value
))
423 return clr_config_net_flags(key
, lxc_conf
, data
);
432 netdev
->flags
|= IFF_UP
;
437 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
438 struct lxc_netdev
*netdev
)
440 struct ifaddrs
*ifaddr
, *ifa
;
443 const char *type_key
= "lxc.net.type";
444 const char *link_key
= "lxc.net.link";
445 const char *tmpvalue
= "phys";
447 if (getifaddrs(&ifaddr
) == -1) {
448 SYSERROR("Get network interfaces failed");
452 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
455 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
458 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
459 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
462 ret
= set_config_net_link(
463 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
465 ERROR("failed to create matched ifnames");
469 ERROR("failed to create matched ifnames");
481 static int set_config_net_link(const char *key
, const char *value
,
482 struct lxc_conf
*lxc_conf
, void *data
)
484 struct lxc_netdev
*netdev
;
487 if (lxc_config_value_empty(value
))
488 return clr_config_net_link(key
, lxc_conf
, data
);
497 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
498 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
500 ret
= network_ifname(&netdev
->link
, value
);
505 static int set_config_net_name(const char *key
, const char *value
,
506 struct lxc_conf
*lxc_conf
, void *data
)
508 struct lxc_netdev
*netdev
;
510 if (lxc_config_value_empty(value
))
511 return clr_config_net_name(key
, lxc_conf
, data
);
520 return network_ifname(&netdev
->name
, value
);
523 static int set_config_net_veth_pair(const char *key
, const char *value
,
524 struct lxc_conf
*lxc_conf
, void *data
)
526 struct lxc_netdev
*netdev
;
528 if (lxc_config_value_empty(value
))
529 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
538 return network_ifname(&netdev
->priv
.veth_attr
.pair
, value
);
541 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
542 struct lxc_conf
*lxc_conf
, void *data
)
544 struct lxc_netdev
*netdev
;
546 if (lxc_config_value_empty(value
))
547 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
556 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
559 static int set_config_net_hwaddr(const char *key
, const char *value
,
560 struct lxc_conf
*lxc_conf
, void *data
)
562 struct lxc_netdev
*netdev
;
565 if (lxc_config_value_empty(value
))
566 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
575 new_value
= strdup(value
);
577 SYSERROR("failed to strdup \"%s\"", value
);
580 rand_complete_hwaddr(new_value
);
582 if (lxc_config_value_empty(new_value
)) {
584 netdev
->hwaddr
= NULL
;
588 netdev
->hwaddr
= new_value
;
592 static int set_config_net_vlan_id(const char *key
, const char *value
,
593 struct lxc_conf
*lxc_conf
, void *data
)
595 struct lxc_netdev
*netdev
;
597 if (lxc_config_value_empty(value
))
598 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
607 if (get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0))
613 static int set_config_net_mtu(const char *key
, const char *value
,
614 struct lxc_conf
*lxc_conf
, void *data
)
616 struct lxc_netdev
*netdev
;
618 if (lxc_config_value_empty(value
))
619 return clr_config_net_mtu(key
, lxc_conf
, data
);
628 return set_config_string_item(&netdev
->mtu
, value
);
631 static int set_config_net_ipv4_address(const char *key
, const char *value
,
632 struct lxc_conf
*lxc_conf
, void *data
)
634 struct lxc_netdev
*netdev
;
635 struct lxc_inetdev
*inetdev
;
636 struct lxc_list
*list
;
637 char *cursor
, *slash
;
638 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
640 if (lxc_config_value_empty(value
))
641 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
650 inetdev
= malloc(sizeof(*inetdev
));
652 SYSERROR("failed to allocate ipv4 address");
655 memset(inetdev
, 0, sizeof(*inetdev
));
657 list
= malloc(sizeof(*list
));
659 SYSERROR("failed to allocate memory");
665 list
->elem
= inetdev
;
667 addr
= strdup(value
);
669 ERROR("no address specified");
675 cursor
= strstr(addr
, " ");
681 slash
= strstr(addr
, "/");
687 if (!inet_pton(AF_INET
, addr
, &inetdev
->addr
)) {
688 SYSERROR("invalid ipv4 address: %s", value
);
695 if (bcast
&& !inet_pton(AF_INET
, bcast
, &inetdev
->bcast
)) {
696 SYSERROR("invalid ipv4 broadcast address: %s", value
);
703 /* No prefix specified, determine it from the network class. */
705 if (lxc_safe_uint(prefix
, &inetdev
->prefix
) < 0)
708 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
711 /* If no broadcast address, let compute one from the
712 * prefix and address.
715 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
716 inetdev
->bcast
.s_addr
|=
717 htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
720 lxc_list_add_tail(&netdev
->ipv4
, list
);
726 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
727 struct lxc_conf
*lxc_conf
, void *data
)
729 struct lxc_netdev
*netdev
;
731 if (lxc_config_value_empty(value
))
732 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
741 free(netdev
->ipv4_gateway
);
743 if (!strcmp(value
, "auto")) {
744 netdev
->ipv4_gateway
= NULL
;
745 netdev
->ipv4_gateway_auto
= true;
749 gw
= malloc(sizeof(*gw
));
751 SYSERROR("failed to allocate ipv4 gateway address");
755 if (!inet_pton(AF_INET
, value
, gw
)) {
756 SYSERROR("invalid ipv4 gateway address: %s", value
);
761 netdev
->ipv4_gateway
= gw
;
762 netdev
->ipv4_gateway_auto
= false;
768 static int set_config_net_ipv6_address(const char *key
, const char *value
,
769 struct lxc_conf
*lxc_conf
, void *data
)
771 struct lxc_netdev
*netdev
;
772 struct lxc_inet6dev
*inet6dev
;
773 struct lxc_list
*list
;
774 char *slash
, *valdup
, *netmask
;
776 if (lxc_config_value_empty(value
))
777 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
786 inet6dev
= malloc(sizeof(*inet6dev
));
788 SYSERROR("failed to allocate ipv6 address");
791 memset(inet6dev
, 0, sizeof(*inet6dev
));
793 list
= malloc(sizeof(*list
));
795 SYSERROR("failed to allocate memory");
801 list
->elem
= inet6dev
;
803 valdup
= strdup(value
);
805 ERROR("no address specified");
811 inet6dev
->prefix
= 64;
812 slash
= strstr(valdup
, "/");
816 if (lxc_safe_uint(netmask
, &inet6dev
->prefix
) < 0)
820 if (!inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
)) {
821 SYSERROR("invalid ipv6 address: %s", valdup
);
828 lxc_list_add_tail(&netdev
->ipv6
, list
);
834 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
835 struct lxc_conf
*lxc_conf
, void *data
)
837 struct lxc_netdev
*netdev
;
839 if (lxc_config_value_empty(value
))
840 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
849 free(netdev
->ipv6_gateway
);
851 if (!strcmp(value
, "auto")) {
852 netdev
->ipv6_gateway
= NULL
;
853 netdev
->ipv6_gateway_auto
= true;
857 gw
= malloc(sizeof(*gw
));
859 SYSERROR("failed to allocate ipv6 gateway address");
863 if (!inet_pton(AF_INET6
, value
, gw
)) {
864 SYSERROR("invalid ipv6 gateway address: %s", value
);
869 netdev
->ipv6_gateway
= gw
;
870 netdev
->ipv6_gateway_auto
= false;
876 static int set_config_net_script_up(const char *key
, const char *value
,
877 struct lxc_conf
*lxc_conf
, void *data
)
879 struct lxc_netdev
*netdev
;
881 if (lxc_config_value_empty(value
))
882 return clr_config_net_script_up(key
, lxc_conf
, data
);
891 return set_config_string_item(&netdev
->upscript
, value
);
894 static int set_config_net_script_down(const char *key
, const char *value
,
895 struct lxc_conf
*lxc_conf
, void *data
)
897 struct lxc_netdev
*netdev
;
899 if (lxc_config_value_empty(value
))
900 return clr_config_net_script_down(key
, lxc_conf
, data
);
909 return set_config_string_item(&netdev
->downscript
, value
);
912 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
914 struct lxc_list
*hooklist
;
916 hooklist
= malloc(sizeof(*hooklist
));
922 hooklist
->elem
= hook
;
923 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
927 static int set_config_seccomp_profile(const char *key
, const char *value
,
928 struct lxc_conf
*lxc_conf
, void *data
)
930 return set_config_path_item(&lxc_conf
->seccomp
, value
);
933 static int set_config_init_cmd(const char *key
, const char *value
,
934 struct lxc_conf
*lxc_conf
, void *data
)
936 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
939 static int set_config_init_uid(const char *key
, const char *value
,
940 struct lxc_conf
*lxc_conf
, void *data
)
942 unsigned int init_uid
;
944 /* Set config value to default. */
945 if (lxc_config_value_empty(value
)) {
946 lxc_conf
->init_uid
= 0;
950 /* Parse new config value. */
951 if (lxc_safe_uint(value
, &init_uid
) < 0)
953 lxc_conf
->init_uid
= init_uid
;
958 static int set_config_init_gid(const char *key
, const char *value
,
959 struct lxc_conf
*lxc_conf
, void *data
)
961 unsigned int init_gid
;
963 /* Set config value to default. */
964 if (lxc_config_value_empty(value
)) {
965 lxc_conf
->init_gid
= 0;
969 /* Parse new config value. */
970 if (lxc_safe_uint(value
, &init_gid
) < 0)
972 lxc_conf
->init_gid
= init_gid
;
977 static int set_config_hooks(const char *key
, const char *value
,
978 struct lxc_conf
*lxc_conf
, void *data
)
982 if (lxc_config_value_empty(value
))
983 return lxc_clear_hooks(lxc_conf
, key
);
985 if (strcmp(key
+ 4, "hook") == 0) {
986 ERROR("lxc.hook cannot take a value");
989 copy
= strdup(value
);
991 SYSERROR("failed to dup string '%s'", value
);
995 if (strcmp(key
+ 9, "pre-start") == 0)
996 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
997 else if (strcmp(key
+ 9, "pre-mount") == 0)
998 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
999 else if (strcmp(key
+ 9, "autodev") == 0)
1000 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
1001 else if (strcmp(key
+ 9, "mount") == 0)
1002 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
1003 else if (strcmp(key
+ 9, "start") == 0)
1004 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
1005 else if (strcmp(key
+ 9, "stop") == 0)
1006 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
1007 else if (strcmp(key
+ 9, "post-stop") == 0)
1008 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
1009 else if (strcmp(key
+ 9, "clone") == 0)
1010 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
1011 else if (strcmp(key
+ 9, "destroy") == 0)
1012 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
1014 SYSERROR("Unknown key: %s", key
);
1019 static int set_config_personality(const char *key
, const char *value
,
1020 struct lxc_conf
*lxc_conf
, void *data
)
1022 signed long personality
= lxc_config_parse_arch(value
);
1024 if (personality
>= 0)
1025 lxc_conf
->personality
= personality
;
1027 WARN("unsupported personality '%s'", value
);
1032 static int set_config_pty_max(const char *key
, const char *value
,
1033 struct lxc_conf
*lxc_conf
, void *data
)
1035 /* Set config value to default. */
1036 if (lxc_config_value_empty(value
)) {
1041 /* Parse new config value. */
1042 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1048 /* We only need to check whether the first byte of the key after the lxc.start.
1049 * prefix matches our expectations since they fortunately all start with a
1050 * different letter. If anything was wrong with the key we would have already
1051 * noticed when the callback was called.
1053 static int set_config_start(const char *key
, const char *value
,
1054 struct lxc_conf
*lxc_conf
, void *data
)
1058 is_empty
= lxc_config_value_empty(value
);
1060 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1061 /* Set config value to default. */
1063 lxc_conf
->start_auto
= 0;
1067 /* Parse new config value. */
1068 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1071 if (lxc_conf
->start_auto
> 1)
1075 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1076 /* Set config value to default. */
1078 lxc_conf
->start_delay
= 0;
1082 /* Parse new config value. */
1083 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1084 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1085 /* Set config value to default. */
1087 lxc_conf
->start_order
= 0;
1091 /* Parse new config value. */
1092 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1095 SYSERROR("Unknown key: %s", key
);
1099 static int set_config_monitor(const char *key
, const char *value
,
1100 struct lxc_conf
*lxc_conf
, void *data
)
1102 /* Set config value to default. */
1103 if (lxc_config_value_empty(value
)) {
1104 lxc_conf
->monitor_unshare
= 0;
1108 /* Parse new config value. */
1109 if (strcmp(key
+ 12, "unshare") == 0)
1110 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1112 SYSERROR("Unknown key: %s", key
);
1116 static int set_config_group(const char *key
, const char *value
,
1117 struct lxc_conf
*lxc_conf
, void *data
)
1119 char *groups
, *groupptr
, *sptr
, *token
;
1120 struct lxc_list
*grouplist
;
1123 if (lxc_config_value_empty(value
))
1124 return lxc_clear_groups(lxc_conf
);
1126 groups
= strdup(value
);
1128 SYSERROR("failed to dup '%s'", value
);
1132 /* In case several groups are specified in a single line
1133 * split these groups in a single element for the list.
1135 for (groupptr
= groups
;; groupptr
= NULL
) {
1136 token
= strtok_r(groupptr
, " \t", &sptr
);
1142 grouplist
= malloc(sizeof(*grouplist
));
1144 SYSERROR("failed to allocate groups list");
1148 grouplist
->elem
= strdup(token
);
1149 if (!grouplist
->elem
) {
1150 SYSERROR("failed to dup '%s'", token
);
1155 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1162 static int set_config_environment(const char *key
, const char *value
,
1163 struct lxc_conf
*lxc_conf
, void *data
)
1165 struct lxc_list
*list_item
= NULL
;
1167 if (lxc_config_value_empty(value
))
1168 return lxc_clear_environment(lxc_conf
);
1170 list_item
= malloc(sizeof(*list_item
));
1174 list_item
->elem
= strdup(value
);
1176 if (!list_item
->elem
)
1179 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1188 static int set_config_tty_max(const char *key
, const char *value
,
1189 struct lxc_conf
*lxc_conf
, void *data
)
1191 /* Set config value to default. */
1192 if (lxc_config_value_empty(value
)) {
1197 /* Parse new config value. */
1198 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1201 static int set_config_tty_dir(const char *key
, const char *value
,
1202 struct lxc_conf
*lxc_conf
, void *data
)
1204 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1208 static int set_config_apparmor_profile(const char *key
, const char *value
,
1209 struct lxc_conf
*lxc_conf
, void *data
)
1211 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1214 static int set_config_apparmor_allow_incomplete(const char *key
,
1216 struct lxc_conf
*lxc_conf
,
1219 /* Set config value to default. */
1220 if (lxc_config_value_empty(value
)) {
1221 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1225 /* Parse new config value. */
1226 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1229 if (lxc_conf
->lsm_aa_allow_incomplete
> 1) {
1230 ERROR("Wrong value for lxc.apparmor.allow_incomplete. Can only "
1231 "be set to 0 or 1");
1238 static int set_config_selinux_context(const char *key
, const char *value
,
1239 struct lxc_conf
*lxc_conf
, void *data
)
1241 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1244 static int set_config_log_file(const char *key
, const char *value
,
1245 struct lxc_conf
*c
, void *data
)
1249 if (lxc_config_value_empty(value
)) {
1255 /* Store these values in the lxc_conf, and then try to set for actual
1258 ret
= set_config_path_item(&c
->logfile
, value
);
1260 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1264 static int set_config_log_level(const char *key
, const char *value
,
1265 struct lxc_conf
*lxc_conf
, void *data
)
1269 /* Set config value to default. */
1270 if (lxc_config_value_empty(value
)) {
1271 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1275 /* Parse new config value. */
1276 if (value
[0] >= '0' && value
[0] <= '9') {
1277 if (lxc_safe_int(value
, &newlevel
) < 0)
1280 newlevel
= lxc_log_priority_to_int(value
);
1283 /* Store these values in the lxc_conf, and then try to set for actual
1286 lxc_conf
->loglevel
= newlevel
;
1287 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1290 static int set_config_autodev(const char *key
, const char *value
,
1291 struct lxc_conf
*lxc_conf
, void *data
)
1293 /* Set config value to default. */
1294 if (lxc_config_value_empty(value
)) {
1295 lxc_conf
->autodev
= 0;
1299 /* Parse new config value. */
1300 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1303 if (lxc_conf
->autodev
> 1) {
1304 ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1");
1311 static int sig_num(const char *sig
)
1313 unsigned int signum
;
1315 if (lxc_safe_uint(sig
, &signum
) < 0)
1321 static int rt_sig_num(const char *signame
)
1323 int rtmax
= 0, sig_n
= 0;
1325 if (strncasecmp(signame
, "max-", 4) == 0) {
1330 if (!isdigit(*signame
))
1333 sig_n
= sig_num(signame
);
1334 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1335 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1341 static int sig_parse(const char *signame
)
1345 if (isdigit(*signame
)) {
1346 return sig_num(signame
);
1347 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1349 if (strncasecmp(signame
, "rt", 2) == 0)
1350 return rt_sig_num(signame
+ 2);
1351 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1352 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1353 return signames
[n
].num
;
1360 static int set_config_signal_halt(const char *key
, const char *value
,
1361 struct lxc_conf
*lxc_conf
, void *data
)
1365 /* Set config value to default. */
1366 if (lxc_config_value_empty(value
)) {
1367 lxc_conf
->haltsignal
= 0;
1371 /* Parse new config value. */
1372 sig_n
= sig_parse(value
);
1376 lxc_conf
->haltsignal
= sig_n
;
1381 static int set_config_signal_reboot(const char *key
, const char *value
,
1382 struct lxc_conf
*lxc_conf
, void *data
)
1386 /* Set config value to default. */
1387 if (lxc_config_value_empty(value
)) {
1388 lxc_conf
->rebootsignal
= 0;
1392 /* Parse new config value. */
1393 sig_n
= sig_parse(value
);
1396 lxc_conf
->rebootsignal
= sig_n
;
1401 static int set_config_signal_stop(const char *key
, const char *value
,
1402 struct lxc_conf
*lxc_conf
, void *data
)
1406 /* Set config value to default. */
1407 if (lxc_config_value_empty(value
)) {
1408 lxc_conf
->stopsignal
= 0;
1412 /* Parse new config value. */
1413 sig_n
= sig_parse(value
);
1416 lxc_conf
->stopsignal
= sig_n
;
1421 static int set_config_cgroup(const char *key
, const char *value
,
1422 struct lxc_conf
*lxc_conf
, void *data
)
1425 char *token
= "lxc.cgroup.";
1426 struct lxc_list
*cglist
= NULL
;
1427 struct lxc_cgroup
*cgelem
= NULL
;
1429 if (lxc_config_value_empty(value
))
1430 return lxc_clear_cgroups(lxc_conf
, key
);
1432 subkey
= strstr(key
, token
);
1436 if (!strlen(subkey
))
1439 if (strlen(subkey
) == strlen(token
))
1442 subkey
+= strlen(token
);
1444 cglist
= malloc(sizeof(*cglist
));
1448 cgelem
= malloc(sizeof(*cgelem
));
1451 memset(cgelem
, 0, sizeof(*cgelem
));
1453 cgelem
->subsystem
= strdup(subkey
);
1454 cgelem
->value
= strdup(value
);
1456 if (!cgelem
->subsystem
|| !cgelem
->value
)
1459 cglist
->elem
= cgelem
;
1461 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1469 free(cgelem
->subsystem
);
1471 free(cgelem
->value
);
1479 static int set_config_prlimit(const char *key
, const char *value
,
1480 struct lxc_conf
*lxc_conf
, void *data
)
1482 struct lxc_list
*iter
;
1483 struct rlimit limit
;
1484 unsigned long limit_value
;
1485 struct lxc_list
*limlist
= NULL
;
1486 struct lxc_limit
*limelem
= NULL
;
1488 if (lxc_config_value_empty(value
))
1489 return lxc_clear_limits(lxc_conf
, key
);
1491 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1494 key
+= sizeof("lxc.prlimit.") - 1;
1496 /* soft limit comes first in the value */
1497 if (!parse_limit_value(&value
, &limit_value
))
1499 limit
.rlim_cur
= limit_value
;
1501 /* skip spaces and a colon */
1502 while (isspace(*value
))
1507 else if (*value
) /* any other character is an error here */
1510 while (isspace(*value
))
1513 /* optional hard limit */
1515 if (!parse_limit_value(&value
, &limit_value
))
1517 limit
.rlim_max
= limit_value
;
1519 /* check for trailing garbage */
1520 while (isspace(*value
))
1526 /* a single value sets both hard and soft limit */
1527 limit
.rlim_max
= limit
.rlim_cur
;
1530 /* find existing list element */
1531 lxc_list_for_each(iter
, &lxc_conf
->limits
)
1533 limelem
= iter
->elem
;
1534 if (!strcmp(key
, limelem
->resource
)) {
1535 limelem
->limit
= limit
;
1540 /* allocate list element */
1541 limlist
= malloc(sizeof(*limlist
));
1545 limelem
= malloc(sizeof(*limelem
));
1548 memset(limelem
, 0, sizeof(*limelem
));
1550 limelem
->resource
= strdup(key
);
1551 if (!limelem
->resource
)
1553 limelem
->limit
= limit
;
1555 limlist
->elem
= limelem
;
1557 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1564 free(limelem
->resource
);
1570 static int set_config_idmaps(const char *key
, const char *value
,
1571 struct lxc_conf
*lxc_conf
, void *data
)
1573 unsigned long hostid
, nsid
, range
;
1576 struct lxc_list
*idmaplist
= NULL
;
1577 struct id_map
*idmap
= NULL
;
1579 if (lxc_config_value_empty(value
))
1580 return lxc_clear_idmaps(lxc_conf
);
1582 idmaplist
= malloc(sizeof(*idmaplist
));
1586 idmap
= malloc(sizeof(*idmap
));
1589 memset(idmap
, 0, sizeof(*idmap
));
1591 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1595 INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1597 idmap
->idtype
= ID_TYPE_UID
;
1598 else if (type
== 'g')
1599 idmap
->idtype
= ID_TYPE_GID
;
1603 idmap
->hostid
= hostid
;
1605 idmap
->range
= range
;
1606 idmaplist
->elem
= idmap
;
1607 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1619 static int set_config_mount_fstab(const char *key
, const char *value
,
1620 struct lxc_conf
*lxc_conf
, void *data
)
1622 if (lxc_config_value_empty(value
)) {
1623 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1627 return set_config_path_item(&lxc_conf
->fstab
, value
);
1630 static int set_config_mount_auto(const char *key
, const char *value
,
1631 struct lxc_conf
*lxc_conf
, void *data
)
1633 char *autos
, *autoptr
, *sptr
, *token
;
1640 } allowed_auto_mounts
[] = {
1641 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1642 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1643 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1644 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1645 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1646 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1647 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1648 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1649 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1650 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1651 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1652 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1653 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1654 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1655 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1656 /* NB: For adding anything that is just a single on/off, but has
1657 * no options: keep mask and flag identical and just define the
1658 * enum value as an unused bit so far
1663 if (lxc_config_value_empty(value
)) {
1664 lxc_conf
->auto_mounts
= 0;
1668 autos
= strdup(value
);
1670 SYSERROR("failed to dup '%s'", value
);
1674 for (autoptr
= autos
;; autoptr
= NULL
) {
1675 token
= strtok_r(autoptr
, " \t", &sptr
);
1681 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1682 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1686 if (!allowed_auto_mounts
[i
].token
) {
1687 ERROR("Invalid filesystem to automount: %s", token
);
1691 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1692 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1699 static int set_config_mount(const char *key
, const char *value
,
1700 struct lxc_conf
*lxc_conf
, void *data
)
1703 struct lxc_list
*mntlist
;
1705 if (lxc_config_value_empty(value
))
1706 return lxc_clear_mount_entries(lxc_conf
);
1708 mntlist
= malloc(sizeof(*mntlist
));
1712 mntelem
= strdup(value
);
1717 mntlist
->elem
= mntelem
;
1719 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1724 static int set_config_cap_keep(const char *key
, const char *value
,
1725 struct lxc_conf
*lxc_conf
, void *data
)
1727 char *keepcaps
, *keepptr
, *sptr
, *token
;
1728 struct lxc_list
*keeplist
;
1731 if (lxc_config_value_empty(value
))
1732 return lxc_clear_config_keepcaps(lxc_conf
);
1734 keepcaps
= strdup(value
);
1736 SYSERROR("failed to dup '%s'", value
);
1740 /* In case several capability keep is specified in a single line
1741 * split these caps in a single element for the list.
1743 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1744 token
= strtok_r(keepptr
, " \t", &sptr
);
1750 if (!strcmp(token
, "none"))
1751 lxc_clear_config_keepcaps(lxc_conf
);
1753 keeplist
= malloc(sizeof(*keeplist
));
1755 SYSERROR("failed to allocate keepcap list");
1759 keeplist
->elem
= strdup(token
);
1760 if (!keeplist
->elem
) {
1761 SYSERROR("failed to dup '%s'", token
);
1766 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1774 static int set_config_cap_drop(const char *key
, const char *value
,
1775 struct lxc_conf
*lxc_conf
, void *data
)
1777 char *dropcaps
, *dropptr
, *sptr
, *token
;
1778 struct lxc_list
*droplist
;
1781 if (lxc_config_value_empty(value
))
1782 return lxc_clear_config_caps(lxc_conf
);
1784 dropcaps
= strdup(value
);
1786 SYSERROR("failed to dup '%s'", value
);
1790 /* In case several capability drop is specified in a single line
1791 * split these caps in a single element for the list.
1793 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1794 token
= strtok_r(dropptr
, " \t", &sptr
);
1800 droplist
= malloc(sizeof(*droplist
));
1802 SYSERROR("failed to allocate drop list");
1806 droplist
->elem
= strdup(token
);
1807 if (!droplist
->elem
) {
1808 SYSERROR("failed to dup '%s'", token
);
1813 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1821 static int set_config_console_path(const char *key
, const char *value
,
1822 struct lxc_conf
*lxc_conf
, void *data
)
1824 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1827 static int set_config_console_logfile(const char *key
, const char *value
,
1828 struct lxc_conf
*lxc_conf
, void *data
)
1830 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1833 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1835 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1837 update_hwaddr(line
);
1839 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1840 char *tmp
= realloc(conf
->unexpanded_config
,
1841 conf
->unexpanded_alloced
+ 1024);
1845 if (!conf
->unexpanded_config
)
1847 conf
->unexpanded_config
= tmp
;
1848 conf
->unexpanded_alloced
+= 1024;
1850 strcat(conf
->unexpanded_config
, line
);
1851 conf
->unexpanded_len
+= linelen
;
1852 if (line
[linelen
- 1] != '\n') {
1853 strcat(conf
->unexpanded_config
, "\n");
1854 conf
->unexpanded_len
++;
1859 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1861 struct dirent
*direntp
;
1863 char path
[MAXPATHLEN
];
1867 dir
= opendir(dirp
);
1869 SYSERROR("failed to open '%s'", dirp
);
1873 while ((direntp
= readdir(dir
))) {
1878 fnam
= direntp
->d_name
;
1879 if (!strcmp(fnam
, "."))
1882 if (!strcmp(fnam
, ".."))
1886 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
1888 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
1889 if (len
< 0 || len
>= MAXPATHLEN
) {
1890 ERROR("lxc.include filename too long under '%s'", dirp
);
1895 ret
= lxc_config_read(path
, lxc_conf
, true);
1903 WARN("lxc.include dir: failed to close directory");
1908 static int set_config_includefiles(const char *key
, const char *value
,
1909 struct lxc_conf
*lxc_conf
, void *data
)
1911 /* Set config value to default. */
1912 if (lxc_config_value_empty(value
)) {
1913 clr_config_includefiles(key
, lxc_conf
, NULL
);
1917 /* Parse new config value. */
1919 return do_includedir(value
, lxc_conf
);
1921 return lxc_config_read(value
, lxc_conf
, true);
1924 static int set_config_rootfs_path(const char *key
, const char *value
,
1925 struct lxc_conf
*lxc_conf
, void *data
)
1927 return set_config_path_item(&lxc_conf
->rootfs
.path
, value
);
1930 static int set_config_rootfs_mount(const char *key
, const char *value
,
1931 struct lxc_conf
*lxc_conf
, void *data
)
1933 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
1936 static int set_config_rootfs_options(const char *key
, const char *value
,
1937 struct lxc_conf
*lxc_conf
, void *data
)
1939 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
1942 static int set_config_rootfs_backend(const char *key
, const char *value
,
1943 struct lxc_conf
*lxc_conf
, void *data
)
1948 static int set_config_uts_name(const char *key
, const char *value
,
1949 struct lxc_conf
*lxc_conf
, void *data
)
1951 struct utsname
*utsname
;
1953 if (lxc_config_value_empty(value
)) {
1954 clr_config_uts_name(key
, lxc_conf
, NULL
);
1958 utsname
= malloc(sizeof(*utsname
));
1960 SYSERROR("failed to allocate memory");
1964 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
1965 ERROR("node name '%s' is too long", value
);
1970 strcpy(utsname
->nodename
, value
);
1971 free(lxc_conf
->utsname
);
1972 lxc_conf
->utsname
= utsname
;
1977 struct parse_line_conf
{
1978 struct lxc_conf
*conf
;
1982 static int parse_line(char *buffer
, void *data
)
1984 struct lxc_config_t
*config
;
1985 char *dot
, *key
, *line
, *linep
, *value
;
1986 struct parse_line_conf
*plc
= data
;
1989 if (lxc_is_line_empty(buffer
))
1992 /* we have to dup the buffer otherwise, at the re-exec for
1993 * reboot we modified the original string on the stack by
1994 * replacing '=' by '\0' below
1996 linep
= line
= strdup(buffer
);
1998 SYSERROR("failed to allocate memory for '%s'", buffer
);
2002 if (!plc
->from_include
)
2003 if ((ret
= append_unexp_config_line(line
, plc
->conf
)))
2006 line
+= lxc_char_left_gc(line
, strlen(line
));
2008 /* ignore comments */
2012 /* martian option - don't add it to the config itself */
2013 if (strncmp(line
, "lxc.", 4))
2018 dot
= strstr(line
, "=");
2020 ERROR("invalid configuration line: %s", line
);
2028 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2030 value
+= lxc_char_left_gc(value
, strlen(value
));
2031 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2033 if (*value
== '\'' || *value
== '\"') {
2034 size_t len
= strlen(value
);
2035 if (len
> 1 && value
[len
- 1] == *value
) {
2036 value
[len
- 1] = '\0';
2041 config
= lxc_getconfig(key
);
2043 ERROR("unknown key %s", key
);
2047 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2054 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2056 struct parse_line_conf c
;
2059 c
.from_include
= false;
2061 return parse_line(buffer
, &c
);
2064 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2066 struct parse_line_conf c
;
2069 c
.from_include
= from_include
;
2071 if (access(file
, R_OK
) == -1) {
2075 /* Catch only the top level config file name in the structure */
2077 conf
->rcfile
= strdup(file
);
2079 return lxc_file_for_each_line(file
, parse_line
, &c
);
2082 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2084 struct lxc_list
*dent
;
2086 dent
= malloc(sizeof(struct lxc_list
));
2091 lxc_list_add_tail(defines
, dent
);
2095 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2097 struct lxc_list
*it
, *next
;
2100 lxc_list_for_each(it
, defines
) {
2101 ret
= lxc_config_readline(it
->elem
, conf
);
2106 lxc_list_for_each_safe(it
, defines
, next
) {
2114 signed long lxc_config_parse_arch(const char *arch
)
2116 #if HAVE_SYS_PERSONALITY_H
2122 { "x86", PER_LINUX32
},
2123 { "linux32", PER_LINUX32
},
2124 { "i386", PER_LINUX32
},
2125 { "i486", PER_LINUX32
},
2126 { "i586", PER_LINUX32
},
2127 { "i686", PER_LINUX32
},
2128 { "athlon", PER_LINUX32
},
2129 { "mips", PER_LINUX32
},
2130 { "mipsel", PER_LINUX32
},
2131 { "ppc", PER_LINUX32
},
2132 { "arm", PER_LINUX32
},
2133 { "armv7l", PER_LINUX32
},
2134 { "armhf", PER_LINUX32
},
2135 { "armel", PER_LINUX32
},
2136 { "powerpc", PER_LINUX32
},
2137 { "linux64", PER_LINUX
},
2138 { "x86_64", PER_LINUX
},
2139 { "amd64", PER_LINUX
},
2140 { "mips64", PER_LINUX
},
2141 { "mips64el", PER_LINUX
},
2142 { "ppc64", PER_LINUX
},
2143 { "ppc64le", PER_LINUX
},
2144 { "ppc64el", PER_LINUX
},
2145 { "powerpc64", PER_LINUX
},
2146 { "s390x", PER_LINUX
},
2147 { "aarch64", PER_LINUX
},
2148 { "arm64", PER_LINUX
},
2150 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2152 for (i
= 0; i
< len
; i
++) {
2153 if (!strcmp(pername
[i
].name
, arch
))
2154 return pername
[i
].per
;
2161 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2163 char *token
, *saveptr
= NULL
;
2169 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2170 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2171 { "LSM", LXC_ATTACH_LSM_EXEC
},
2176 /* For the sake of backward compatibility, drop all privileges
2177 * if none is specified.
2179 for (i
= 0; all_privs
[i
].token
; i
++) {
2180 *flags
|= all_privs
[i
].flag
;
2185 token
= strtok_r(flaglist
, "|", &saveptr
);
2188 for (i
= 0; all_privs
[i
].token
; i
++) {
2189 if (!strcmp(all_privs
[i
].token
, token
))
2190 aflag
= all_privs
[i
].flag
;
2197 token
= strtok_r(NULL
, "|", &saveptr
);
2203 /* Write out a configuration file. */
2204 void write_config(FILE *fout
, struct lxc_conf
*c
)
2207 size_t len
= c
->unexpanded_len
;
2212 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2214 SYSERROR("Error writing configuration file");
2217 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2224 len
= strlen(key
) + strlen(v
) + 4;
2227 if (lxc_config_value_empty(v
))
2228 ret
= snprintf(tmp
, len
, "%s =", key
);
2230 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2231 if (ret
< 0 || ret
>= len
)
2234 /* Save the line verbatim into unexpanded_conf */
2235 if (append_unexp_config_line(tmp
, conf
))
2241 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2245 char *lstart
= conf
->unexpanded_config
;
2247 if (!conf
->unexpanded_config
)
2251 lend
= strchr(lstart
, '\n');
2254 lend
= lstart
+ strlen(lstart
);
2257 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2262 v
= lstart
[strlen(key
)];
2263 if (!isspace(v
) && v
!= '=') {
2268 conf
->unexpanded_len
-= (lend
- lstart
);
2269 if (*lend
== '\0') {
2273 memmove(lstart
, lend
, strlen(lend
) + 1);
2277 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2278 const char *newpath
, const char *oldname
,
2279 const char *newname
, const char *ovldir
)
2282 char *lend
, *newdir
, *olddir
, *p
, *q
;
2283 size_t newdirlen
, olddirlen
;
2284 char *lstart
= conf
->unexpanded_config
;
2285 const char *key
= "lxc.mount.entry";
2287 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2288 olddir
= alloca(olddirlen
+ 1);
2289 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2291 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2292 ERROR("failed to create string");
2296 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2297 newdir
= alloca(newdirlen
+ 1);
2298 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2300 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2301 ERROR("failed to create string");
2305 if (!conf
->unexpanded_config
)
2309 lend
= strchr(lstart
, '\n');
2311 lend
= lstart
+ strlen(lstart
);
2315 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2318 p
= strchr(lstart
+ strlen(key
), '=');
2329 /* Whenever an lxc.mount.entry entry is found in a line we check
2330 * if the substring " overlay" or the substring " aufs" is
2331 * present before doing any further work. We check for "
2332 * overlay" and " aufs" since both substrings need to have at
2333 * least one space before them in a valid overlay
2334 * lxc.mount.entry (/A B overlay). When the space before is
2335 * missing it is very likely that these substrings are part of a
2336 * path or something else. (Checking q >= lend ensures that we
2337 * only count matches in the current line.) */
2338 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2339 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2342 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2345 /* replace the olddir with newdir */
2346 if (olddirlen
>= newdirlen
) {
2347 size_t diff
= olddirlen
- newdirlen
;
2348 memcpy(q
, newdir
, newdirlen
);
2349 if (olddirlen
!= newdirlen
) {
2350 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2351 strlen(q
) - newdirlen
- diff
+ 1);
2353 conf
->unexpanded_len
-= diff
;
2357 size_t diff
= newdirlen
- olddirlen
;
2358 size_t oldlen
= conf
->unexpanded_len
;
2359 size_t newlen
= oldlen
+ diff
;
2360 size_t poffset
= q
- conf
->unexpanded_config
;
2362 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2364 ERROR("Out of memory");
2367 conf
->unexpanded_len
= newlen
;
2368 conf
->unexpanded_alloced
= newlen
+ 1;
2369 new[newlen
- 1] = '\0';
2370 lend
= new + (lend
- conf
->unexpanded_config
);
2371 /* move over the remainder to make room for the newdir
2373 memmove(new + poffset
+ newdirlen
,
2374 new + poffset
+ olddirlen
,
2375 oldlen
- poffset
- olddirlen
+ 1);
2376 conf
->unexpanded_config
= new;
2377 memcpy(new + poffset
, newdir
, newdirlen
);
2387 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2388 const char *newpath
, const char *oldname
,
2389 const char *newname
)
2392 char *lend
, *newdir
, *olddir
, *p
;
2393 char *lstart
= conf
->unexpanded_config
;
2394 size_t newdirlen
, olddirlen
;
2395 const char *key
= "lxc.hook";
2397 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2398 olddir
= alloca(olddirlen
+ 1);
2399 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2400 if (ret
< 0 || ret
>= olddirlen
+ 1) {
2401 ERROR("failed to create string");
2405 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2406 newdir
= alloca(newdirlen
+ 1);
2407 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2408 if (ret
< 0 || ret
>= newdirlen
+ 1) {
2409 ERROR("failed to create string");
2412 if (!conf
->unexpanded_config
)
2415 lend
= strchr(lstart
, '\n');
2417 lend
= lstart
+ strlen(lstart
);
2421 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2424 p
= strchr(lstart
+ strlen(key
), '=');
2435 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2438 /* replace the olddir with newdir */
2439 if (olddirlen
>= newdirlen
) {
2440 size_t diff
= olddirlen
- newdirlen
;
2441 memcpy(p
, newdir
, newdirlen
);
2442 if (olddirlen
!= newdirlen
) {
2443 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2444 strlen(p
) - newdirlen
- diff
+ 1);
2446 conf
->unexpanded_len
-= diff
;
2450 size_t diff
= newdirlen
- olddirlen
;
2451 size_t oldlen
= conf
->unexpanded_len
;
2452 size_t newlen
= oldlen
+ diff
;
2453 size_t poffset
= p
- conf
->unexpanded_config
;
2455 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2457 ERROR("failed to allocate memory");
2460 conf
->unexpanded_len
= newlen
;
2461 conf
->unexpanded_alloced
= newlen
+ 1;
2462 new[newlen
- 1] = '\0';
2463 lend
= new + (lend
- conf
->unexpanded_config
);
2464 /* move over the remainder to make room for the newdir
2466 memmove(new + poffset
+ newdirlen
,
2467 new + poffset
+ olddirlen
,
2468 oldlen
- poffset
- olddirlen
+ 1);
2469 conf
->unexpanded_config
= new;
2470 memcpy(new + poffset
, newdir
, newdirlen
);
2483 ERROR("Error writing to new config"); \
2489 * This is called only from clone. We wish to update all hwaddrs in the
2490 * unexpanded config file. We can't/don't want to update any which come from
2491 * lxc.includes (there shouldn't be any).
2492 * We can't just walk the c->lxc-conf->network list because that includes netifs
2493 * from the include files. So we update the ones which we find in the unexp
2494 * config file, then find the original macaddr in the conf->network, and update
2495 * that to the same value.
2497 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2499 char *lend
, *p
, *p2
;
2500 struct lxc_list
*it
;
2501 char *lstart
= conf
->unexpanded_config
;
2503 if (!conf
->unexpanded_config
)
2507 char newhwaddr
[18], oldhwaddr
[17];
2509 lend
= strchr(lstart
, '\n');
2511 lend
= lstart
+ strlen(lstart
);
2515 if (!lxc_config_net_hwaddr(lstart
)) {
2520 p
= strchr(lstart
, '=');
2533 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2536 if ((p2
- p
) != 17) {
2537 WARN("Bad hwaddr entry");
2542 memcpy(oldhwaddr
, p
, 17);
2544 if (!new_hwaddr(newhwaddr
))
2547 memcpy(p
, newhwaddr
, 17);
2548 lxc_list_for_each(it
, &conf
->network
)
2550 struct lxc_netdev
*n
= it
->elem
;
2551 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2552 memcpy(n
->hwaddr
, newhwaddr
, 17);
2561 static int set_config_ephemeral(const char *key
, const char *value
,
2562 struct lxc_conf
*lxc_conf
, void *data
)
2564 /* Set config value to default. */
2565 if (lxc_config_value_empty(value
)) {
2566 lxc_conf
->ephemeral
= 0;
2570 /* Parse new config value. */
2571 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2574 if (lxc_conf
->ephemeral
> 1) {
2576 "Wrong value for lxc.ephemeral. Can only be set to 0 or 1");
2583 static int set_config_log_syslog(const char *key
, const char *value
,
2584 struct lxc_conf
*lxc_conf
, void *data
)
2588 /* Clear any previously set value. */
2589 if (lxc_conf
->syslog
) {
2590 free(lxc_conf
->syslog
);
2591 lxc_conf
->syslog
= NULL
;
2594 /* Check if value is empty. */
2595 if (lxc_config_value_empty(value
))
2599 facility
= lxc_syslog_priority_to_int(value
);
2600 if (facility
== -EINVAL
) {
2601 ERROR("Wrong value for lxc.log.syslog.");
2605 lxc_log_syslog(facility
);
2606 return set_config_string_item(&lxc_conf
->syslog
, value
);
2609 static int set_config_no_new_privs(const char *key
, const char *value
,
2610 struct lxc_conf
*lxc_conf
, void *data
)
2614 /* Set config value to default. */
2615 if (lxc_config_value_empty(value
)) {
2616 lxc_conf
->no_new_privs
= false;
2620 /* Parse new config value. */
2621 if (lxc_safe_uint(value
, &v
) < 0)
2625 ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 "
2630 lxc_conf
->no_new_privs
= v
? true : false;
2635 /* Callbacks to get configuration items. */
2636 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2637 struct lxc_conf
*c
, void *data
)
2644 memset(retv
, 0, inlen
);
2646 #if HAVE_SYS_PERSONALITY_H
2649 switch (c
->personality
) {
2651 strprint(retv
, inlen
, "i686");
2654 strprint(retv
, inlen
, "x86_64");
2664 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2665 struct lxc_conf
*c
, void *data
)
2667 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2670 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2671 struct lxc_conf
*c
, void *data
)
2673 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2676 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2677 struct lxc_conf
*c
, void *data
)
2679 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2682 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2683 struct lxc_conf
*c
, void *data
)
2685 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2688 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2689 int inlen
, struct lxc_conf
*c
,
2692 return lxc_get_conf_int(c
, retv
, inlen
,
2693 c
->lsm_aa_allow_incomplete
);
2696 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2697 struct lxc_conf
*c
, void *data
)
2699 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2703 * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list,
2704 * then just the value(s) will be printed. Since there still could be
2705 * more than one, it is newline-separated.
2706 * (Maybe that's ambigous, since some values, i.e. devices.list, will
2707 * already have newlines?)
2708 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed,
2709 * in 'lxc.cgroup.subsystem.key = value' format.
2711 static int get_config_cgroup(const char *key
, char *retv
, int inlen
,
2712 struct lxc_conf
*c
, void *data
)
2714 struct lxc_list
*it
;
2717 bool get_all
= false;
2722 memset(retv
, 0, inlen
);
2724 if (!strcmp(key
, "lxc.cgroup"))
2726 else if (!strncmp(key
, "lxc.cgroup.", 11))
2731 lxc_list_for_each(it
, &c
->cgroup
) {
2732 struct lxc_cgroup
*cg
= it
->elem
;
2734 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n", cg
->subsystem
, cg
->value
);
2735 } else if (!strcmp(cg
->subsystem
, key
)) {
2736 strprint(retv
, inlen
, "%s\n", cg
->value
);
2743 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2744 struct lxc_conf
*c
, void *data
)
2746 struct lxc_list
*it
;
2747 int len
, listlen
, ret
;
2749 /* "u 1000 1000000 65536"
2751 * let's render this as
2769 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2770 char buf
[__LXC_IDMAP_STR_BUF
];
2775 memset(retv
, 0, inlen
);
2777 listlen
= lxc_list_len(&c
->id_map
);
2778 lxc_list_for_each(it
, &c
->id_map
)
2780 struct id_map
*map
= it
->elem
;
2781 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
2782 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
2783 map
->nsid
, map
->hostid
, map
->range
);
2784 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
2787 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
2792 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
2793 struct lxc_conf
*c
, void *data
)
2796 v
= lxc_log_priority_to_string(c
->loglevel
);
2797 return lxc_get_conf_str(retv
, inlen
, v
);
2800 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
2801 struct lxc_conf
*c
, void *data
)
2803 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
2806 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
2807 struct lxc_conf
*c
, void *data
)
2809 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
2812 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
2813 struct lxc_conf
*c
, void *data
)
2815 int len
, fulllen
= 0;
2816 const char *sep
= "";
2821 memset(retv
, 0, inlen
);
2823 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
2826 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
2827 case LXC_AUTO_PROC_MIXED
:
2828 strprint(retv
, inlen
, "%sproc:mixed", sep
);
2831 case LXC_AUTO_PROC_RW
:
2832 strprint(retv
, inlen
, "%sproc:rw", sep
);
2839 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
2840 case LXC_AUTO_SYS_RO
:
2841 strprint(retv
, inlen
, "%ssys:ro", sep
);
2844 case LXC_AUTO_SYS_RW
:
2845 strprint(retv
, inlen
, "%ssys:rw", sep
);
2848 case LXC_AUTO_SYS_MIXED
:
2849 strprint(retv
, inlen
, "%ssys:mixed", sep
);
2856 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
2857 case LXC_AUTO_CGROUP_NOSPEC
:
2858 strprint(retv
, inlen
, "%scgroup", sep
);
2861 case LXC_AUTO_CGROUP_MIXED
:
2862 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
2865 case LXC_AUTO_CGROUP_RO
:
2866 strprint(retv
, inlen
, "%scgroup:ro", sep
);
2869 case LXC_AUTO_CGROUP_RW
:
2870 strprint(retv
, inlen
, "%scgroup:rw", sep
);
2873 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
2874 strprint(retv
, inlen
, "%scgroup-full", sep
);
2877 case LXC_AUTO_CGROUP_FULL_MIXED
:
2878 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
2881 case LXC_AUTO_CGROUP_FULL_RO
:
2882 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
2885 case LXC_AUTO_CGROUP_FULL_RW
:
2886 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
2896 static int get_config_mount(const char *key
, char *retv
, int inlen
,
2897 struct lxc_conf
*c
, void *data
)
2899 int len
, fulllen
= 0;
2900 struct lxc_list
*it
;
2905 memset(retv
, 0, inlen
);
2907 lxc_list_for_each(it
, &c
->mount_list
)
2909 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
2915 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
2916 struct lxc_conf
*c
, void *data
)
2918 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
2921 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
2922 struct lxc_conf
*c
, void *data
)
2924 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
2927 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
2928 struct lxc_conf
*c
, void *data
)
2930 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
2933 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
2934 struct lxc_conf
*c
, void *data
)
2939 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
2940 struct lxc_conf
*c
, void *data
)
2942 return lxc_get_conf_str(
2944 c
->utsname
? c
->utsname
->nodename
: NULL
);
2947 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
2948 struct lxc_conf
*c
, void *data
)
2951 int len
, fulllen
= 0, found
= -1;
2952 struct lxc_list
*it
;
2955 /* "lxc.hook.mount" */
2956 subkey
= strchr(key
, '.');
2958 subkey
= strchr(subkey
+ 1, '.');
2964 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
2965 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
2976 memset(retv
, 0, inlen
);
2978 lxc_list_for_each(it
, &c
->hooks
[found
]) {
2979 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
2984 static int get_config_net(const char *key
, char *retv
, int inlen
,
2985 struct lxc_conf
*c
, void *data
)
2987 int len
, fulllen
= 0;
2988 struct lxc_list
*it
;
2993 memset(retv
, 0, inlen
);
2995 lxc_list_for_each(it
, &c
->network
) {
2996 struct lxc_netdev
*n
= it
->elem
;
2997 const char *t
= lxc_net_type_to_str(n
->type
);
2998 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3004 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3005 struct lxc_conf
*c
, void *data
)
3007 int len
, fulllen
= 0;
3008 struct lxc_list
*it
;
3013 memset(retv
, 0, inlen
);
3015 lxc_list_for_each(it
, &c
->caps
) {
3016 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3021 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3022 struct lxc_conf
*c
, void *data
)
3024 int len
, fulllen
= 0;
3025 struct lxc_list
*it
;
3030 memset(retv
, 0, inlen
);
3032 lxc_list_for_each(it
, &c
->keepcaps
) {
3033 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3038 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3039 struct lxc_conf
*c
, void *data
)
3041 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3044 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3045 struct lxc_conf
*c
, void *data
)
3047 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3050 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3051 struct lxc_conf
*c
, void *data
)
3053 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3056 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3057 struct lxc_conf
*c
, void *data
)
3059 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3062 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3063 struct lxc_conf
*c
, void *data
)
3065 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3068 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3069 struct lxc_conf
*c
, void *data
)
3071 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3074 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3075 struct lxc_conf
*c
, void *data
)
3077 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3080 static int get_config_start(const char *key
, char *retv
, int inlen
,
3081 struct lxc_conf
*c
, void *data
)
3083 if (strcmp(key
+ 10, "auto") == 0)
3084 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3085 else if (strcmp(key
+ 10, "delay") == 0)
3086 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3087 else if (strcmp(key
+ 10, "order") == 0)
3088 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3093 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3094 struct lxc_conf
*c
, void *data
)
3096 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3099 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3100 struct lxc_conf
*c
, void *data
)
3102 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3105 static int get_config_group(const char *key
, char *retv
, int inlen
,
3106 struct lxc_conf
*c
, void *data
)
3108 int len
, fulllen
= 0;
3109 struct lxc_list
*it
;
3114 memset(retv
, 0, inlen
);
3116 lxc_list_for_each(it
, &c
->groups
) {
3117 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3122 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3123 struct lxc_conf
*c
, void *data
)
3125 int len
, fulllen
= 0;
3126 struct lxc_list
*it
;
3131 memset(retv
, 0, inlen
);
3133 lxc_list_for_each(it
, &c
->environment
) {
3134 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3139 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3140 struct lxc_conf
*c
, void *data
)
3142 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3145 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3146 struct lxc_conf
*c
, void *data
)
3148 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3151 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3152 struct lxc_conf
*c
, void *data
)
3154 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3157 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3158 struct lxc_conf
*c
, void *data
)
3160 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3163 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3164 struct lxc_conf
*c
, void *data
)
3166 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3170 * If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3171 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3172 * printed, in 'lxc.prlimit.resource = value' format.
3174 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3175 struct lxc_conf
*c
, void *data
)
3177 int fulllen
= 0, len
;
3178 bool get_all
= false;
3179 struct lxc_list
*it
;
3184 memset(retv
, 0, inlen
);
3186 if (!strcmp(key
, "lxc.prlimit"))
3188 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3193 lxc_list_for_each(it
, &c
->limits
) {
3194 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3195 integers or the word
3198 struct lxc_limit
*lim
= it
->elem
;
3200 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3201 memcpy(buf
, "unlimited", sizeof("unlimited"));
3202 partlen
= sizeof("unlimited") - 1;
3204 partlen
= sprintf(buf
, "%" PRIu64
,
3205 (uint64_t)lim
->limit
.rlim_cur
);
3207 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3208 if (lim
->limit
.rlim_max
== RLIM_INFINITY
) {
3209 memcpy(buf
+ partlen
, ":unlimited",
3210 sizeof(":unlimited"));
3212 sprintf(buf
+ partlen
, ":%" PRIu64
,
3213 (uint64_t)lim
->limit
.rlim_max
);
3218 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3219 lim
->resource
, buf
);
3220 } else if (strcmp(lim
->resource
, key
) == 0) {
3221 strprint(retv
, inlen
, "%s", buf
);
3228 /* Callbacks to clear config items. */
3229 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3232 c
->personality
= -1;
3236 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3243 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3250 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3258 static inline int clr_config_apparmor_profile(const char *key
,
3259 struct lxc_conf
*c
, void *data
)
3261 free(c
->lsm_aa_profile
);
3262 c
->lsm_aa_profile
= NULL
;
3266 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3270 c
->lsm_aa_allow_incomplete
= 0;
3274 static inline int clr_config_selinux_context(const char *key
,
3275 struct lxc_conf
*c
, void *data
)
3277 free(c
->lsm_se_context
);
3278 c
->lsm_se_context
= NULL
;
3282 static inline int clr_config_cgroup(const char *key
, struct lxc_conf
*c
,
3285 return lxc_clear_cgroups(c
, key
);
3288 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3291 return lxc_clear_idmaps(c
);
3294 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3297 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3301 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3309 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3312 return lxc_clear_mount_entries(c
);
3315 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3318 return lxc_clear_automounts(c
);
3321 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3329 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3332 free(c
->rootfs
.path
);
3333 c
->rootfs
.path
= NULL
;
3337 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3340 free(c
->rootfs
.mount
);
3341 c
->rootfs
.mount
= NULL
;
3345 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3348 free(c
->rootfs
.options
);
3349 c
->rootfs
.options
= NULL
;
3353 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3359 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3367 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3370 return lxc_clear_hooks(c
, key
);
3373 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3376 lxc_free_networks(&c
->network
);
3381 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3384 return lxc_clear_config_caps(c
);
3387 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3390 return lxc_clear_config_keepcaps(c
);
3393 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3396 free(c
->console
.path
);
3397 c
->console
.path
= NULL
;
3401 static inline int clr_config_console_logfile(const char *key
,
3402 struct lxc_conf
*c
, void *data
)
3404 free(c
->console
.log_path
);
3405 c
->console
.log_path
= NULL
;
3409 static inline int clr_config_seccomp_profile(const char *key
,
3410 struct lxc_conf
*c
, void *data
)
3417 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3424 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3431 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3434 c
->rebootsignal
= 0;
3438 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3445 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3448 if (strcmp(key
+ 10, "auto") == 0)
3450 else if (strcmp(key
+ 10, "delay") == 0)
3452 else if (strcmp(key
+ 10, "order") == 0)
3458 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3466 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3469 c
->monitor_unshare
= 0;
3473 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3476 return lxc_clear_groups(c
);
3479 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3482 return lxc_clear_environment(c
);
3485 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3493 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3500 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3507 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3514 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3517 c
->no_new_privs
= false;
3521 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3524 return lxc_clear_limits(c
, key
);
3527 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3530 lxc_clear_includes(c
);
3534 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3535 struct lxc_conf
*c
, void *data
)
3540 static struct lxc_config_t
*get_network_config_ops(const char *key
,
3541 struct lxc_conf
*lxc_conf
,
3543 char **deindexed_key
)
3546 unsigned int tmpidx
;
3548 char *copy
, *idx_start
, *idx_end
;
3549 struct lxc_config_t
*config
= NULL
;
3551 /* check that this is a sensible network key */
3552 if (strncmp("lxc.net.", key
, 8)) {
3553 ERROR("Invalid network configuration key \"%s\"", key
);
3559 ERROR("Failed to duplicate string \"%s\"", key
);
3564 if (!isdigit(*(key
+ 8))) {
3565 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
3569 /* beginning of index string */
3570 idx_start
= (copy
+ 7);
3573 /* end of index string */
3574 idx_end
= strchr((copy
+ 8), '.');
3576 ERROR("Failed to detect \".\" in string \"%s\"", (copy
+ 8));
3581 /* parse current index */
3582 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
3584 ERROR("Failed to parse usigned integer from string \"%s\": %s",
3585 idx_start
+ 1, strerror(-ret
));
3590 /* This, of course is utterly nonsensical on so many levels, but
3591 * better safe than sorry.
3592 * (Checking for INT_MAX here is intentional.)
3594 if (tmpidx
== INT_MAX
) {
3595 SYSERROR("number of configured networks would overflow the "
3596 "counter... what are you doing?");
3601 numstrlen
= strlen((idx_start
+ 1));
3603 /* repair configuration key */
3607 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
3608 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
3610 config
= lxc_getconfig(copy
);
3612 ERROR("unknown network configuration key %s", key
);
3616 *deindexed_key
= copy
;
3625 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3626 * was found. So we make sure next comes an integer, find the right callback
3627 * (by rewriting the key), and call it.
3629 static int set_config_net_nic(const char *key
, const char *value
,
3630 struct lxc_conf
*lxc_conf
, void *data
)
3633 const char *idxstring
;
3634 struct lxc_config_t
*config
;
3635 struct lxc_netdev
*netdev
;
3637 char *deindexed_key
= NULL
;
3639 idxstring
= key
+ 8;
3640 if (!isdigit(*idxstring
))
3643 if (lxc_config_value_empty(value
))
3644 return clr_config_net_nic(key
, lxc_conf
, data
);
3646 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
3647 if (!config
|| idx
< 0)
3650 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
3652 free(deindexed_key
);
3656 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
3657 free(deindexed_key
);
3662 * Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.'
3663 * was found. So we make sure next comes an integer, find the right callback
3664 * (by rewriting the key), and call it.
3666 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
3670 const char *idxstring
;
3671 struct lxc_config_t
*config
;
3672 struct lxc_netdev
*netdev
;
3674 char *deindexed_key
= NULL
;
3676 idxstring
= key
+ 8;
3677 if (!isdigit(*idxstring
))
3680 /* The left conjunct is pretty self-explanatory. The right conjunct
3681 * checks whether the two pointers are equal. If they are we know that
3682 * this is not a key that is namespaced any further and so we are
3683 * supposed to clear the whole network.
3685 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
3686 unsigned int rmnetdevidx
;
3688 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
3691 /* Remove network from network list. */
3692 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
3696 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
3697 if (!config
|| idx
< 0)
3700 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
3702 free(deindexed_key
);
3706 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
3707 free(deindexed_key
);
3711 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
3714 struct lxc_netdev
*netdev
;
3728 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
3731 struct lxc_netdev
*netdev
;
3741 netdev
->name
= NULL
;
3746 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
3749 struct lxc_netdev
*netdev
;
3763 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
3766 struct lxc_netdev
*netdev
;
3776 netdev
->link
= NULL
;
3781 static int clr_config_net_macvlan_mode(const char *key
,
3782 struct lxc_conf
*lxc_conf
, void *data
)
3784 struct lxc_netdev
*netdev
;
3793 if (netdev
->type
!= LXC_NET_MACVLAN
)
3796 netdev
->priv
.macvlan_attr
.mode
= -1;
3801 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
3804 struct lxc_netdev
*netdev
;
3813 free(netdev
->priv
.veth_attr
.pair
);
3814 netdev
->priv
.veth_attr
.pair
= NULL
;
3819 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
3822 struct lxc_netdev
*netdev
;
3831 free(netdev
->upscript
);
3832 netdev
->upscript
= NULL
;
3837 static int clr_config_net_script_down(const char *key
,
3838 struct lxc_conf
*lxc_conf
, void *data
)
3840 struct lxc_netdev
*netdev
;
3849 free(netdev
->downscript
);
3850 netdev
->downscript
= NULL
;
3855 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
3858 struct lxc_netdev
*netdev
;
3867 free(netdev
->hwaddr
);
3868 netdev
->hwaddr
= NULL
;
3873 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
3876 struct lxc_netdev
*netdev
;
3891 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
3894 struct lxc_netdev
*netdev
;
3903 netdev
->priv
.vlan_attr
.vid
= 0;
3908 static int clr_config_net_ipv4_gateway(const char *key
,
3909 struct lxc_conf
*lxc_conf
, void *data
)
3911 struct lxc_netdev
*netdev
;
3920 free(netdev
->ipv4_gateway
);
3921 netdev
->ipv4_gateway
= NULL
;
3926 static int clr_config_net_ipv4_address(const char *key
,
3927 struct lxc_conf
*lxc_conf
, void *data
)
3929 struct lxc_netdev
*netdev
;
3930 struct lxc_list
*cur
, *next
;
3939 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3948 static int clr_config_net_ipv6_gateway(const char *key
,
3949 struct lxc_conf
*lxc_conf
, void *data
)
3951 struct lxc_netdev
*netdev
;
3960 free(netdev
->ipv6_gateway
);
3961 netdev
->ipv6_gateway
= NULL
;
3966 static int clr_config_net_ipv6_address(const char *key
,
3967 struct lxc_conf
*lxc_conf
, void *data
)
3969 struct lxc_netdev
*netdev
;
3970 struct lxc_list
*cur
, *next
;
3979 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3988 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
3989 struct lxc_conf
*c
, void *data
)
3992 const char *idxstring
;
3993 struct lxc_config_t
*config
;
3994 struct lxc_netdev
*netdev
;
3996 char *deindexed_key
= NULL
;
3998 idxstring
= key
+ 8;
3999 if (!isdigit(*idxstring
))
4002 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4003 if (!config
|| idx
< 0)
4006 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4008 free(deindexed_key
);
4012 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4013 free(deindexed_key
);
4017 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4018 struct lxc_conf
*c
, void *data
)
4020 int len
, fulllen
= 0;
4021 struct lxc_netdev
*netdev
;
4026 memset(retv
, 0, inlen
);
4035 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4040 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4041 struct lxc_conf
*c
, void *data
)
4043 int len
, fulllen
= 0;
4044 struct lxc_netdev
*netdev
;
4049 memset(retv
, 0, inlen
);
4058 if (netdev
->flags
& IFF_UP
)
4059 strprint(retv
, inlen
, "up");
4064 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4065 struct lxc_conf
*c
, void *data
)
4067 int len
, fulllen
= 0;
4068 struct lxc_netdev
*netdev
;
4073 memset(retv
, 0, inlen
);
4083 strprint(retv
, inlen
, "%s", netdev
->link
);
4088 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4089 struct lxc_conf
*c
, void *data
)
4091 int len
, fulllen
= 0;
4092 struct lxc_netdev
*netdev
;
4097 memset(retv
, 0, inlen
);
4107 strprint(retv
, inlen
, "%s", netdev
->name
);
4112 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4113 struct lxc_conf
*c
, void *data
)
4116 int len
, fulllen
= 0;
4117 struct lxc_netdev
*netdev
;
4122 memset(retv
, 0, inlen
);
4131 if (netdev
->type
!= LXC_NET_MACVLAN
)
4134 switch (netdev
->priv
.macvlan_attr
.mode
) {
4135 case MACVLAN_MODE_PRIVATE
:
4138 case MACVLAN_MODE_VEPA
:
4141 case MACVLAN_MODE_BRIDGE
:
4144 case MACVLAN_MODE_PASSTHRU
:
4152 strprint(retv
, inlen
, "%s", mode
);
4157 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4158 struct lxc_conf
*c
, void *data
)
4160 int len
, fulllen
= 0;
4161 struct lxc_netdev
*netdev
;
4166 memset(retv
, 0, inlen
);
4175 if (netdev
->type
!= LXC_NET_VETH
)
4178 strprint(retv
, inlen
, "%s",
4179 netdev
->priv
.veth_attr
.pair
? netdev
->priv
.veth_attr
.pair
4180 : netdev
->priv
.veth_attr
.veth1
);
4185 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4186 struct lxc_conf
*c
, void *data
)
4188 int len
, fulllen
= 0;
4189 struct lxc_netdev
*netdev
;
4194 memset(retv
, 0, inlen
);
4203 if (netdev
->upscript
)
4204 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4209 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4210 struct lxc_conf
*c
, void *data
)
4212 int len
, fulllen
= 0;
4213 struct lxc_netdev
*netdev
;
4218 memset(retv
, 0, inlen
);
4227 if (netdev
->downscript
)
4228 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4233 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4234 struct lxc_conf
*c
, void *data
)
4236 int len
, fulllen
= 0;
4237 struct lxc_netdev
*netdev
;
4242 memset(retv
, 0, inlen
);
4252 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4257 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4258 struct lxc_conf
*c
, void *data
)
4260 int len
, fulllen
= 0;
4261 struct lxc_netdev
*netdev
;
4266 memset(retv
, 0, inlen
);
4276 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4281 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4282 struct lxc_conf
*c
, void *data
)
4284 int len
, fulllen
= 0;
4285 struct lxc_netdev
*netdev
;
4290 memset(retv
, 0, inlen
);
4299 if (netdev
->type
!= LXC_NET_VLAN
)
4302 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4307 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4308 struct lxc_conf
*c
, void *data
)
4310 int len
, fulllen
= 0;
4311 char buf
[INET_ADDRSTRLEN
];
4312 struct lxc_netdev
*netdev
;
4317 memset(retv
, 0, inlen
);
4326 if (netdev
->ipv4_gateway_auto
) {
4327 strprint(retv
, inlen
, "auto");
4328 } else if (netdev
->ipv4_gateway
) {
4329 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4330 strprint(retv
, inlen
, "%s", buf
);
4336 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4337 struct lxc_conf
*c
, void *data
)
4339 int len
, fulllen
= 0;
4341 char buf
[INET_ADDRSTRLEN
];
4342 struct lxc_netdev
*netdev
;
4343 struct lxc_list
*it
;
4348 memset(retv
, 0, inlen
);
4357 listlen
= lxc_list_len(&netdev
->ipv4
);
4358 lxc_list_for_each(it
, &netdev
->ipv4
) {
4359 struct lxc_inetdev
*i
= it
->elem
;
4360 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4361 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4362 (listlen
-- > 1) ? "\n" : "");
4368 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4369 struct lxc_conf
*c
, void *data
)
4371 int len
, fulllen
= 0;
4372 char buf
[INET6_ADDRSTRLEN
];
4373 struct lxc_netdev
*netdev
;
4378 memset(retv
, 0, inlen
);
4387 if (netdev
->ipv6_gateway_auto
) {
4388 strprint(retv
, inlen
, "auto");
4389 } else if (netdev
->ipv6_gateway
) {
4390 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4391 strprint(retv
, inlen
, "%s", buf
);
4397 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4398 struct lxc_conf
*c
, void *data
)
4400 int len
, fulllen
= 0;
4402 char buf
[INET6_ADDRSTRLEN
];
4403 struct lxc_netdev
*netdev
;
4404 struct lxc_list
*it
;
4409 memset(retv
, 0, inlen
);
4418 listlen
= lxc_list_len(&netdev
->ipv6
);
4419 lxc_list_for_each(it
, &netdev
->ipv6
) {
4420 struct lxc_inet6dev
*i
= it
->elem
;
4421 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4422 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4423 (listlen
-- > 1) ? "\n" : "");
4429 int lxc_list_config_items(char *retv
, int inlen
)
4438 memset(retv
, 0, inlen
);
4440 for (i
= 0; i
< config_size
; i
++) {
4441 char *s
= config
[i
].name
;
4442 if (s
[strlen(s
) - 1] == '.')
4444 strprint(retv
, inlen
, "%s\n", s
);
4450 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4453 const char *idxstring
;
4454 struct lxc_config_t
*config
;
4455 struct lxc_netdev
*netdev
;
4458 char *deindexed_key
= NULL
;
4460 idxstring
= key
+ 8;
4461 if (!isdigit(*idxstring
))
4464 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4465 if (!config
|| idx
< 0)
4468 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4469 free(deindexed_key
);
4476 memset(retv
, 0, inlen
);
4478 strprint(retv
, inlen
, "type\n");
4479 strprint(retv
, inlen
, "script.up\n");
4480 strprint(retv
, inlen
, "script.down\n");
4481 if (netdev
->type
!= LXC_NET_EMPTY
) {
4482 strprint(retv
, inlen
, "flags\n");
4483 strprint(retv
, inlen
, "link\n");
4484 strprint(retv
, inlen
, "name\n");
4485 strprint(retv
, inlen
, "hwaddr\n");
4486 strprint(retv
, inlen
, "mtu\n");
4487 strprint(retv
, inlen
, "ipv6.address\n");
4488 strprint(retv
, inlen
, "ipv6.gateway\n");
4489 strprint(retv
, inlen
, "ipv4.address\n");
4490 strprint(retv
, inlen
, "ipv4.gateway\n");
4493 switch (netdev
->type
) {
4495 strprint(retv
, inlen
, "veth.pair\n");
4497 case LXC_NET_MACVLAN
:
4498 strprint(retv
, inlen
, "macvlan.mode\n");
4501 strprint(retv
, inlen
, "vlan.id\n");