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 *, void *);
78 lxc_config_define(autodev
);
79 lxc_config_define(apparmor_allow_incomplete
);
80 lxc_config_define(apparmor_profile
);
81 lxc_config_define(cap_drop
);
82 lxc_config_define(cap_keep
);
83 lxc_config_define(cgroup_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(idmaps
);
96 lxc_config_define(includefiles
);
97 lxc_config_define(init_cmd
);
98 lxc_config_define(init_cwd
);
99 lxc_config_define(init_gid
);
100 lxc_config_define(init_uid
);
101 lxc_config_define(log_file
);
102 lxc_config_define(log_level
);
103 lxc_config_define(log_syslog
);
104 lxc_config_define(monitor
);
105 lxc_config_define(mount
);
106 lxc_config_define(mount_auto
);
107 lxc_config_define(mount_fstab
);
108 lxc_config_define(namespace);
109 lxc_config_define(net
);
110 lxc_config_define(net_flags
);
111 lxc_config_define(net_hwaddr
);
112 lxc_config_define(net_ipv4_address
);
113 lxc_config_define(net_ipv4_gateway
);
114 lxc_config_define(net_ipv6_address
);
115 lxc_config_define(net_ipv6_gateway
);
116 lxc_config_define(net_link
);
117 lxc_config_define(net_macvlan_mode
);
118 lxc_config_define(net_mtu
);
119 lxc_config_define(net_name
);
120 lxc_config_define(net_nic
);
121 lxc_config_define(net_script_down
);
122 lxc_config_define(net_script_up
);
123 lxc_config_define(net_type
);
124 lxc_config_define(net_veth_pair
);
125 lxc_config_define(net_vlan_id
);
126 lxc_config_define(no_new_privs
);
127 lxc_config_define(noop
);
128 lxc_config_define(personality
);
129 lxc_config_define(prlimit
);
130 lxc_config_define(pty_max
);
131 lxc_config_define(rootfs_backend
);
132 lxc_config_define(rootfs_mount
);
133 lxc_config_define(rootfs_options
);
134 lxc_config_define(rootfs_path
);
135 lxc_config_define(seccomp_profile
);
136 lxc_config_define(selinux_context
);
137 lxc_config_define(signal_halt
);
138 lxc_config_define(signal_reboot
);
139 lxc_config_define(signal_stop
);
140 lxc_config_define(start
);
141 lxc_config_define(tty_max
);
142 lxc_config_define(tty_dir
);
143 lxc_config_define(uts_name
);
144 lxc_config_define(sysctl
);
145 lxc_config_define(proc
);
147 static struct lxc_config_t config
[] = {
148 /* REMOVE in LXC 3.0 */
149 { "lxc.arch", false, set_config_personality
, get_config_personality
, clr_config_personality
, },
150 { "lxc.apparmor.profile", false, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
151 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
152 { "lxc.autodev", false, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
153 { "lxc.cap.drop", false, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
154 { "lxc.cap.keep", false, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
155 { "lxc.cgroup.dir", false, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
156 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
157 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile
, get_config_console_buffer_logfile
, clr_config_console_buffer_logfile
, },
158 { "lxc.console.buffer.size", false, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
159 { "lxc.console.logfile", false, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
160 { "lxc.console.path", false, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
161 { "lxc.console.rotate", false, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
162 { "lxc.environment", false, set_config_environment
, get_config_environment
, clr_config_environment
, },
163 { "lxc.ephemeral", false, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
164 { "lxc.execute.cmd", false, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
165 { "lxc.group", false, set_config_group
, get_config_group
, clr_config_group
, },
166 { "lxc.hook.autodev", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
167 { "lxc.hook.clone", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
168 { "lxc.hook.destroy", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
169 { "lxc.hook.mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
170 { "lxc.hook.post-stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
171 { "lxc.hook.start-host", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.pre-start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook.pre-mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
174 { "lxc.hook.start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
175 { "lxc.hook.stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
176 { "lxc.hook", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
177 { "lxc.idmap", false, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
178 { "lxc.include", false, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
179 { "lxc.init.cmd", false, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
180 { "lxc.init.gid", false, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
181 { "lxc.init.uid", false, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
182 { "lxc.init.cwd", false, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
183 { "lxc.log.file", false, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
184 { "lxc.log.level", false, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
185 { "lxc.log.syslog", false, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
186 { "lxc.monitor.unshare", false, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
187 { "lxc.mount.auto", false, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
188 { "lxc.mount.entry", false, set_config_mount
, get_config_mount
, clr_config_mount
, },
189 { "lxc.mount.fstab", false, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
190 { "lxc.namespace", false, set_config_namespace
, get_config_namespace
, clr_config_namespace
, },
192 /* [START]: REMOVE IN LXC 3.0 */
193 { "lxc.network.type", true, set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
194 { "lxc.network.flags", true, set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
195 { "lxc.network.link", true, set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
196 { "lxc.network.name", true, set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
197 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
198 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
199 { "lxc.network.script.up", true, set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
200 { "lxc.network.script.down", true, set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
201 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
202 { "lxc.network.mtu", true, set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
203 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
204 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
205 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
206 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
207 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
208 { "lxc.network.", true, set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
209 { "lxc.network", true, set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
210 /* [END]: REMOVE IN LXC 3.0 */
212 { "lxc.net.flags", false, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
213 { "lxc.net.hwaddr", false, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
214 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
215 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
216 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
217 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
218 { "lxc.net.link", false, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
219 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
220 { "lxc.net.mtu", false, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
221 { "lxc.net.name", false, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
222 { "lxc.net.script.down", false, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
223 { "lxc.net.script.up", false, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
224 { "lxc.net.type", false, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
225 { "lxc.net.vlan.id", false, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
226 { "lxc.net.veth.pair", false, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
227 { "lxc.net.", false, set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
228 { "lxc.net", false, set_config_net
, get_config_net
, clr_config_net
, },
229 { "lxc.no_new_privs", false, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
230 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
231 { "lxc.pty.max", false, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
232 { "lxc.rootfs.mount", false, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
233 { "lxc.rootfs.options", false, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
234 { "lxc.rootfs.path", false, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
235 { "lxc.seccomp.profile", false, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
236 { "lxc.selinux.context", false, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
237 { "lxc.signal.halt", false, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
238 { "lxc.signal.reboot", false, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
239 { "lxc.signal.stop", false, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
240 { "lxc.start.auto", false, set_config_start
, get_config_start
, clr_config_start
, },
241 { "lxc.start.delay", false, set_config_start
, get_config_start
, clr_config_start
, },
242 { "lxc.start.order", false, set_config_start
, get_config_start
, clr_config_start
, },
243 { "lxc.tty.dir", false, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
244 { "lxc.tty.max", false, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
245 { "lxc.uts.name", false, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
246 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
247 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
249 /* [START]: REMOVE IN LXC 3.0 */
250 { "lxc.pts", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
251 { "lxc.devttydir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
252 { "lxc.tty", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
253 { "lxc.aa_profile", true, set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
254 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
255 { "lxc.se_context", true, set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
256 { "lxc.id_map", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
257 { "lxc.mount", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
258 { "lxc.rootfs.backend", true, set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
259 { "lxc.rootfs", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
260 { "lxc.utsname", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
261 { "lxc.seccomp", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
262 { "lxc.console", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
263 { "lxc.haltsignal", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
264 { "lxc.rebootsignal", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
265 { "lxc.stopsignal", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
266 { "lxc.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
267 { "lxc.kmsg", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
268 { "lxc.loglevel", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
269 { "lxc.logfile", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
270 { "lxc.init_cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
271 { "lxc.init_uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
272 { "lxc.init_gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
273 { "lxc.limit", true, set_config_limit
, get_config_limit
, clr_config_limit
, },
274 { "lxc.pivotdir", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
275 /* [END]: REMOVE IN LXC 3.0 */
283 static const struct signame signames
[] = {
316 { SIGSTKFLT
, "STKFLT" },
331 { SIGVTALRM
, "VTALRM" },
337 { SIGWINCH
, "WINCH" },
355 { SIGUNUSED
, "UNUSED" },
362 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
364 struct lxc_config_t
*lxc_get_config(const char *key
)
368 for (i
= 0; i
< config_size
; i
++)
369 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
375 static int set_config_net(const char *key
, const char *value
,
376 struct lxc_conf
*lxc_conf
, void *data
)
378 if (!lxc_config_value_empty(value
)) {
379 ERROR("lxc.net must not have a value");
383 return clr_config_net(key
, lxc_conf
, data
);
386 static int set_config_net_type(const char *key
, const char *value
,
387 struct lxc_conf
*lxc_conf
, void *data
)
389 struct lxc_netdev
*netdev
= data
;
391 if (lxc_config_value_empty(value
))
392 return clr_config_net_type(key
, lxc_conf
, data
);
397 if (!strcmp(value
, "veth")) {
398 netdev
->type
= LXC_NET_VETH
;
399 } else if (!strcmp(value
, "macvlan")) {
400 netdev
->type
= LXC_NET_MACVLAN
;
401 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
403 } else if (!strcmp(value
, "vlan")) {
404 netdev
->type
= LXC_NET_VLAN
;
405 } else if (!strcmp(value
, "phys")) {
406 netdev
->type
= LXC_NET_PHYS
;
407 } else if (!strcmp(value
, "empty")) {
408 netdev
->type
= LXC_NET_EMPTY
;
409 } else if (!strcmp(value
, "none")) {
410 netdev
->type
= LXC_NET_NONE
;
412 ERROR("invalid network type %s", value
);
419 static int set_config_net_flags(const char *key
, const char *value
,
420 struct lxc_conf
*lxc_conf
, void *data
)
422 struct lxc_netdev
*netdev
= data
;
424 if (lxc_config_value_empty(value
))
425 return clr_config_net_flags(key
, lxc_conf
, data
);
430 netdev
->flags
|= IFF_UP
;
435 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
436 struct lxc_netdev
*netdev
)
438 struct ifaddrs
*ifaddr
, *ifa
;
441 const char *type_key
= "lxc.net.type";
442 const char *link_key
= "lxc.net.link";
443 const char *tmpvalue
= "phys";
445 if (getifaddrs(&ifaddr
) == -1) {
446 SYSERROR("Get network interfaces failed");
450 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
453 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
456 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
457 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
460 ret
= set_config_net_link(
461 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
463 ERROR("failed to create matched ifnames");
467 ERROR("failed to create matched ifnames");
479 static int set_config_net_link(const char *key
, const char *value
,
480 struct lxc_conf
*lxc_conf
, void *data
)
482 struct lxc_netdev
*netdev
= data
;
485 if (lxc_config_value_empty(value
))
486 return clr_config_net_link(key
, lxc_conf
, data
);
491 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
492 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
494 ret
= network_ifname(netdev
->link
, value
);
499 static int set_config_net_name(const char *key
, const char *value
,
500 struct lxc_conf
*lxc_conf
, void *data
)
502 struct lxc_netdev
*netdev
= data
;
504 if (lxc_config_value_empty(value
))
505 return clr_config_net_name(key
, lxc_conf
, data
);
510 return network_ifname(netdev
->name
, value
);
513 static int set_config_net_veth_pair(const char *key
, const char *value
,
514 struct lxc_conf
*lxc_conf
, void *data
)
516 struct lxc_netdev
*netdev
= data
;
518 if (lxc_config_value_empty(value
))
519 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
524 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
);
527 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
528 struct lxc_conf
*lxc_conf
, void *data
)
530 struct lxc_netdev
*netdev
= data
;
532 if (lxc_config_value_empty(value
))
533 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
538 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
541 static int set_config_net_hwaddr(const char *key
, const char *value
,
542 struct lxc_conf
*lxc_conf
, void *data
)
544 struct lxc_netdev
*netdev
= data
;
547 if (lxc_config_value_empty(value
))
548 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
553 new_value
= strdup(value
);
557 rand_complete_hwaddr(new_value
);
559 if (lxc_config_value_empty(new_value
)) {
561 netdev
->hwaddr
= NULL
;
565 netdev
->hwaddr
= new_value
;
569 static int set_config_net_vlan_id(const char *key
, const char *value
,
570 struct lxc_conf
*lxc_conf
, void *data
)
573 struct lxc_netdev
*netdev
= data
;
575 if (lxc_config_value_empty(value
))
576 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
581 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
588 static int set_config_net_mtu(const char *key
, const char *value
,
589 struct lxc_conf
*lxc_conf
, void *data
)
591 struct lxc_netdev
*netdev
= data
;
593 if (lxc_config_value_empty(value
))
594 return clr_config_net_mtu(key
, lxc_conf
, data
);
599 return set_config_string_item(&netdev
->mtu
, value
);
602 static int set_config_net_ipv4_address(const char *key
, const char *value
,
603 struct lxc_conf
*lxc_conf
, void *data
)
606 struct lxc_netdev
*netdev
= data
;
607 struct lxc_inetdev
*inetdev
;
608 struct lxc_list
*list
;
609 char *cursor
, *slash
;
610 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
612 if (lxc_config_value_empty(value
))
613 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
618 inetdev
= malloc(sizeof(*inetdev
));
622 memset(inetdev
, 0, sizeof(*inetdev
));
624 list
= malloc(sizeof(*list
));
631 list
->elem
= inetdev
;
633 addr
= strdup(value
);
640 cursor
= strstr(addr
, " ");
646 slash
= strstr(addr
, "/");
652 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
653 if (!ret
|| ret
< 0) {
654 SYSERROR("Invalid ipv4 address \"%s\"", value
);
662 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
663 if (!ret
|| ret
< 0) {
664 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
673 /* No prefix specified, determine it from the network class. */
675 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
679 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
682 /* If no broadcast address, let compute one from the
683 * prefix and address.
686 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
687 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
690 lxc_list_add_tail(&netdev
->ipv4
, list
);
696 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
697 struct lxc_conf
*lxc_conf
, void *data
)
699 struct lxc_netdev
*netdev
= data
;
701 if (lxc_config_value_empty(value
))
702 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
707 free(netdev
->ipv4_gateway
);
709 if (!strcmp(value
, "auto")) {
710 netdev
->ipv4_gateway
= NULL
;
711 netdev
->ipv4_gateway_auto
= true;
716 gw
= malloc(sizeof(*gw
));
720 ret
= inet_pton(AF_INET
, value
, gw
);
721 if (!ret
|| ret
< 0) {
722 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
727 netdev
->ipv4_gateway
= gw
;
728 netdev
->ipv4_gateway_auto
= false;
734 static int set_config_net_ipv6_address(const char *key
, const char *value
,
735 struct lxc_conf
*lxc_conf
, void *data
)
738 struct lxc_netdev
*netdev
= data
;
739 struct lxc_inet6dev
*inet6dev
;
740 struct lxc_list
*list
;
741 char *slash
, *valdup
, *netmask
;
743 if (lxc_config_value_empty(value
))
744 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
749 inet6dev
= malloc(sizeof(*inet6dev
));
753 memset(inet6dev
, 0, sizeof(*inet6dev
));
755 list
= malloc(sizeof(*list
));
762 list
->elem
= inet6dev
;
764 valdup
= strdup(value
);
771 inet6dev
->prefix
= 64;
772 slash
= strstr(valdup
, "/");
776 if (lxc_safe_uint(netmask
, &inet6dev
->prefix
) < 0)
780 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
781 if (!ret
|| ret
< 0) {
782 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
789 lxc_list_add_tail(&netdev
->ipv6
, list
);
795 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
796 struct lxc_conf
*lxc_conf
, void *data
)
798 struct lxc_netdev
*netdev
= data
;
800 if (lxc_config_value_empty(value
))
801 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
806 free(netdev
->ipv6_gateway
);
808 if (!strcmp(value
, "auto")) {
809 netdev
->ipv6_gateway
= NULL
;
810 netdev
->ipv6_gateway_auto
= true;
815 gw
= malloc(sizeof(*gw
));
819 ret
= inet_pton(AF_INET6
, value
, gw
);
820 if (!ret
|| ret
< 0) {
821 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
826 netdev
->ipv6_gateway
= gw
;
827 netdev
->ipv6_gateway_auto
= false;
833 static int set_config_net_script_up(const char *key
, const char *value
,
834 struct lxc_conf
*lxc_conf
, void *data
)
836 struct lxc_netdev
*netdev
= data
;
838 if (lxc_config_value_empty(value
))
839 return clr_config_net_script_up(key
, lxc_conf
, data
);
844 return set_config_string_item(&netdev
->upscript
, value
);
847 static int set_config_net_script_down(const char *key
, const char *value
,
848 struct lxc_conf
*lxc_conf
, void *data
)
850 struct lxc_netdev
*netdev
= data
;
852 if (lxc_config_value_empty(value
))
853 return clr_config_net_script_down(key
, lxc_conf
, data
);
858 return set_config_string_item(&netdev
->downscript
, value
);
861 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
863 struct lxc_list
*hooklist
;
865 hooklist
= malloc(sizeof(*hooklist
));
871 hooklist
->elem
= hook
;
872 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
876 static int set_config_seccomp_profile(const char *key
, const char *value
,
877 struct lxc_conf
*lxc_conf
, void *data
)
879 return set_config_path_item(&lxc_conf
->seccomp
, value
);
882 static int set_config_execute_cmd(const char *key
, const char *value
,
883 struct lxc_conf
*lxc_conf
, void *data
)
885 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
888 static int set_config_init_cmd(const char *key
, const char *value
,
889 struct lxc_conf
*lxc_conf
, void *data
)
891 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
894 static int set_config_init_cwd(const char *key
, const char *value
,
895 struct lxc_conf
*lxc_conf
, void *data
)
897 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
900 static int set_config_init_uid(const char *key
, const char *value
,
901 struct lxc_conf
*lxc_conf
, void *data
)
903 unsigned int init_uid
;
905 if (lxc_config_value_empty(value
)) {
906 lxc_conf
->init_uid
= 0;
910 if (lxc_safe_uint(value
, &init_uid
) < 0)
913 lxc_conf
->init_uid
= init_uid
;
918 static int set_config_init_gid(const char *key
, const char *value
,
919 struct lxc_conf
*lxc_conf
, void *data
)
921 unsigned int init_gid
;
923 if (lxc_config_value_empty(value
)) {
924 lxc_conf
->init_gid
= 0;
928 if (lxc_safe_uint(value
, &init_gid
) < 0)
931 lxc_conf
->init_gid
= init_gid
;
936 static int set_config_hooks(const char *key
, const char *value
,
937 struct lxc_conf
*lxc_conf
, void *data
)
941 if (lxc_config_value_empty(value
))
942 return lxc_clear_hooks(lxc_conf
, key
);
944 if (strcmp(key
+ 4, "hook") == 0) {
945 ERROR("lxc.hook must not have a value");
949 copy
= strdup(value
);
953 if (strcmp(key
+ 9, "pre-start") == 0)
954 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
955 else if (strcmp(key
+ 9, "start-host") == 0)
956 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
957 else if (strcmp(key
+ 9, "pre-mount") == 0)
958 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
959 else if (strcmp(key
+ 9, "autodev") == 0)
960 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
961 else if (strcmp(key
+ 9, "mount") == 0)
962 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
963 else if (strcmp(key
+ 9, "start") == 0)
964 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
965 else if (strcmp(key
+ 9, "stop") == 0)
966 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
967 else if (strcmp(key
+ 9, "post-stop") == 0)
968 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
969 else if (strcmp(key
+ 9, "clone") == 0)
970 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
971 else if (strcmp(key
+ 9, "destroy") == 0)
972 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
978 static int set_config_personality(const char *key
, const char *value
,
979 struct lxc_conf
*lxc_conf
, void *data
)
981 signed long personality
= lxc_config_parse_arch(value
);
983 if (personality
>= 0)
984 lxc_conf
->personality
= personality
;
986 WARN("Unsupported personality \"%s\"", value
);
991 static int set_config_pty_max(const char *key
, const char *value
,
992 struct lxc_conf
*lxc_conf
, void *data
)
994 if (lxc_config_value_empty(value
)) {
999 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1005 /* We only need to check whether the first byte of the key after the lxc.start.
1006 * prefix matches our expectations since they fortunately all start with a
1007 * different letter. If anything was wrong with the key we would have already
1008 * noticed when the callback was called.
1010 static int set_config_start(const char *key
, const char *value
,
1011 struct lxc_conf
*lxc_conf
, void *data
)
1015 is_empty
= lxc_config_value_empty(value
);
1017 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1019 lxc_conf
->start_auto
= 0;
1023 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1026 if (lxc_conf
->start_auto
> 1)
1030 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1032 lxc_conf
->start_delay
= 0;
1036 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1037 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1039 lxc_conf
->start_order
= 0;
1043 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1049 static int set_config_monitor(const char *key
, const char *value
,
1050 struct lxc_conf
*lxc_conf
, void *data
)
1052 if (lxc_config_value_empty(value
)) {
1053 lxc_conf
->monitor_unshare
= 0;
1057 if (strcmp(key
+ 12, "unshare") == 0)
1058 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1063 static int set_config_group(const char *key
, const char *value
,
1064 struct lxc_conf
*lxc_conf
, void *data
)
1066 char *groups
, *groupptr
, *sptr
, *token
;
1067 struct lxc_list
*grouplist
;
1070 if (lxc_config_value_empty(value
))
1071 return lxc_clear_groups(lxc_conf
);
1073 groups
= strdup(value
);
1077 /* In case several groups are specified in a single line split these
1078 * groups in a single element for the list.
1080 for (groupptr
= groups
;; groupptr
= NULL
) {
1081 token
= strtok_r(groupptr
, " \t", &sptr
);
1087 grouplist
= malloc(sizeof(*grouplist
));
1091 grouplist
->elem
= strdup(token
);
1092 if (!grouplist
->elem
) {
1097 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1104 static int set_config_environment(const char *key
, const char *value
,
1105 struct lxc_conf
*lxc_conf
, void *data
)
1107 struct lxc_list
*list_item
= NULL
;
1109 if (lxc_config_value_empty(value
))
1110 return lxc_clear_environment(lxc_conf
);
1112 list_item
= malloc(sizeof(*list_item
));
1116 list_item
->elem
= strdup(value
);
1118 if (!list_item
->elem
)
1121 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1130 static int set_config_tty_max(const char *key
, const char *value
,
1131 struct lxc_conf
*lxc_conf
, void *data
)
1133 if (lxc_config_value_empty(value
)) {
1138 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1141 static int set_config_tty_dir(const char *key
, const char *value
,
1142 struct lxc_conf
*lxc_conf
, void *data
)
1144 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1148 static int set_config_apparmor_profile(const char *key
, const char *value
,
1149 struct lxc_conf
*lxc_conf
, void *data
)
1151 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1154 static int set_config_apparmor_allow_incomplete(const char *key
,
1156 struct lxc_conf
*lxc_conf
,
1159 if (lxc_config_value_empty(value
)) {
1160 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1164 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1167 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1173 static int set_config_selinux_context(const char *key
, const char *value
,
1174 struct lxc_conf
*lxc_conf
, void *data
)
1176 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1179 static int set_config_log_file(const char *key
, const char *value
,
1180 struct lxc_conf
*c
, void *data
)
1184 if (lxc_config_value_empty(value
)) {
1190 /* Store these values in the lxc_conf, and then try to set for actual
1193 ret
= set_config_path_item(&c
->logfile
, value
);
1195 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1200 static int set_config_log_level(const char *key
, const char *value
,
1201 struct lxc_conf
*lxc_conf
, void *data
)
1205 if (lxc_config_value_empty(value
)) {
1206 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1210 if (value
[0] >= '0' && value
[0] <= '9') {
1211 if (lxc_safe_int(value
, &newlevel
) < 0)
1214 newlevel
= lxc_log_priority_to_int(value
);
1217 /* Store these values in the lxc_conf, and then try to set for actual
1220 lxc_conf
->loglevel
= newlevel
;
1221 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1224 static int set_config_autodev(const char *key
, const char *value
,
1225 struct lxc_conf
*lxc_conf
, void *data
)
1227 if (lxc_config_value_empty(value
)) {
1228 lxc_conf
->autodev
= 0;
1232 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1235 if (lxc_conf
->autodev
> 1)
1241 static int sig_num(const char *sig
)
1243 unsigned int signum
;
1245 if (lxc_safe_uint(sig
, &signum
) < 0)
1251 static int rt_sig_num(const char *signame
)
1253 int rtmax
= 0, sig_n
= 0;
1255 if (strncasecmp(signame
, "max-", 4) == 0) {
1260 if (!isdigit(*signame
))
1263 sig_n
= sig_num(signame
);
1264 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1265 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1271 static int sig_parse(const char *signame
)
1275 if (isdigit(*signame
)) {
1276 return sig_num(signame
);
1277 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1279 if (strncasecmp(signame
, "rt", 2) == 0)
1280 return rt_sig_num(signame
+ 2);
1281 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1282 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1283 return signames
[n
].num
;
1290 static int set_config_signal_halt(const char *key
, const char *value
,
1291 struct lxc_conf
*lxc_conf
, void *data
)
1295 if (lxc_config_value_empty(value
)) {
1296 lxc_conf
->haltsignal
= 0;
1300 sig_n
= sig_parse(value
);
1304 lxc_conf
->haltsignal
= sig_n
;
1309 static int set_config_signal_reboot(const char *key
, const char *value
,
1310 struct lxc_conf
*lxc_conf
, void *data
)
1314 if (lxc_config_value_empty(value
)) {
1315 lxc_conf
->rebootsignal
= 0;
1319 sig_n
= sig_parse(value
);
1323 lxc_conf
->rebootsignal
= sig_n
;
1328 static int set_config_signal_stop(const char *key
, const char *value
,
1329 struct lxc_conf
*lxc_conf
, void *data
)
1333 if (lxc_config_value_empty(value
)) {
1334 lxc_conf
->stopsignal
= 0;
1338 sig_n
= sig_parse(value
);
1342 lxc_conf
->stopsignal
= sig_n
;
1347 static int set_config_cgroup_controller(const char *key
, const char *value
,
1348 struct lxc_conf
*lxc_conf
, void *data
)
1351 char *token
= "lxc.cgroup.";
1352 struct lxc_list
*cglist
= NULL
;
1353 struct lxc_cgroup
*cgelem
= NULL
;
1355 if (lxc_config_value_empty(value
))
1356 return lxc_clear_cgroups(lxc_conf
, key
);
1358 subkey
= strstr(key
, token
);
1362 if (!strlen(subkey
))
1365 if (strlen(subkey
) == strlen(token
))
1368 subkey
+= strlen(token
);
1370 cglist
= malloc(sizeof(*cglist
));
1374 cgelem
= malloc(sizeof(*cgelem
));
1377 memset(cgelem
, 0, sizeof(*cgelem
));
1379 cgelem
->subsystem
= strdup(subkey
);
1380 cgelem
->value
= strdup(value
);
1382 if (!cgelem
->subsystem
|| !cgelem
->value
)
1385 cglist
->elem
= cgelem
;
1387 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1394 free(cgelem
->subsystem
);
1395 free(cgelem
->value
);
1402 static int set_config_cgroup_dir(const char *key
, const char *value
,
1403 struct lxc_conf
*lxc_conf
, void *data
)
1405 if (lxc_config_value_empty(value
))
1406 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1408 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1411 static int set_config_prlimit(const char *key
, const char *value
,
1412 struct lxc_conf
*lxc_conf
, void *data
)
1414 struct lxc_list
*iter
;
1415 struct rlimit limit
;
1417 struct lxc_list
*limlist
= NULL
;
1418 struct lxc_limit
*limelem
= NULL
;
1420 if (lxc_config_value_empty(value
))
1421 return lxc_clear_limits(lxc_conf
, key
);
1423 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1426 key
+= sizeof("lxc.prlimit.") - 1;
1428 /* soft limit comes first in the value */
1429 if (!parse_limit_value(&value
, &limit_value
))
1431 limit
.rlim_cur
= limit_value
;
1433 /* skip spaces and a colon */
1434 while (isspace(*value
))
1439 else if (*value
) /* any other character is an error here */
1442 while (isspace(*value
))
1445 /* optional hard limit */
1447 if (!parse_limit_value(&value
, &limit_value
))
1449 limit
.rlim_max
= limit_value
;
1451 /* check for trailing garbage */
1452 while (isspace(*value
))
1458 /* a single value sets both hard and soft limit */
1459 limit
.rlim_max
= limit
.rlim_cur
;
1462 /* find existing list element */
1463 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1464 limelem
= iter
->elem
;
1465 if (!strcmp(key
, limelem
->resource
)) {
1466 limelem
->limit
= limit
;
1471 /* allocate list element */
1472 limlist
= malloc(sizeof(*limlist
));
1476 limelem
= malloc(sizeof(*limelem
));
1479 memset(limelem
, 0, sizeof(*limelem
));
1481 limelem
->resource
= strdup(key
);
1482 if (!limelem
->resource
)
1484 limelem
->limit
= limit
;
1486 lxc_list_add_elem(limlist
, limelem
);;
1488 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1495 free(limelem
->resource
);
1501 static int set_config_sysctl(const char *key
, const char *value
,
1502 struct lxc_conf
*lxc_conf
, void *data
)
1504 struct lxc_list
*iter
;
1505 char *replace_value
= NULL
;
1506 struct lxc_list
*sysctl_list
= NULL
;
1507 struct lxc_sysctl
*sysctl_elem
= NULL
;
1509 if (lxc_config_value_empty(value
))
1510 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1512 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1515 key
+= sizeof("lxc.sysctl.") - 1;
1517 /* find existing list element */
1518 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1519 sysctl_elem
= iter
->elem
;
1521 if (strcmp(key
, sysctl_elem
->key
) != 0)
1524 replace_value
= strdup(value
);
1528 free(sysctl_elem
->value
);
1529 sysctl_elem
->value
= replace_value
;
1533 /* allocate list element */
1534 sysctl_list
= malloc(sizeof(*sysctl_list
));
1538 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1541 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1543 sysctl_elem
->key
= strdup(key
);
1544 if (!sysctl_elem
->key
)
1547 sysctl_elem
->value
= strdup(value
);
1548 if (!sysctl_elem
->value
)
1551 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1553 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1560 free(sysctl_elem
->key
);
1561 free(sysctl_elem
->value
);
1567 static int set_config_proc(const char *key
, const char *value
,
1568 struct lxc_conf
*lxc_conf
, void *data
)
1571 struct lxc_list
*proclist
= NULL
;
1572 struct lxc_proc
*procelem
= NULL
;
1574 if (lxc_config_value_empty(value
))
1575 return clr_config_proc(key
, lxc_conf
, NULL
);
1577 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1580 subkey
= key
+ sizeof("lxc.proc.") - 1;
1581 if (*subkey
== '\0')
1584 proclist
= malloc(sizeof(*proclist
));
1588 procelem
= malloc(sizeof(*procelem
));
1591 memset(procelem
, 0, sizeof(*procelem
));
1593 procelem
->filename
= strdup(subkey
);
1594 procelem
->value
= strdup(value
);
1596 if (!procelem
->filename
|| !procelem
->value
)
1599 proclist
->elem
= procelem
;
1601 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1608 free(procelem
->filename
);
1609 free(procelem
->value
);
1616 static int set_config_idmaps(const char *key
, const char *value
,
1617 struct lxc_conf
*lxc_conf
, void *data
)
1619 unsigned long hostid
, nsid
, range
;
1622 struct lxc_list
*idmaplist
= NULL
;
1623 struct id_map
*idmap
= NULL
;
1625 if (lxc_config_value_empty(value
))
1626 return lxc_clear_idmaps(lxc_conf
);
1628 idmaplist
= malloc(sizeof(*idmaplist
));
1632 idmap
= malloc(sizeof(*idmap
));
1635 memset(idmap
, 0, sizeof(*idmap
));
1637 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1641 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1643 idmap
->idtype
= ID_TYPE_UID
;
1644 else if (type
== 'g')
1645 idmap
->idtype
= ID_TYPE_GID
;
1649 idmap
->hostid
= hostid
;
1651 idmap
->range
= range
;
1652 idmaplist
->elem
= idmap
;
1653 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1665 static int set_config_mount_fstab(const char *key
, const char *value
,
1666 struct lxc_conf
*lxc_conf
, void *data
)
1668 if (lxc_config_value_empty(value
)) {
1669 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1673 return set_config_path_item(&lxc_conf
->fstab
, value
);
1676 static int set_config_mount_auto(const char *key
, const char *value
,
1677 struct lxc_conf
*lxc_conf
, void *data
)
1679 char *autos
, *autoptr
, *sptr
, *token
;
1686 } allowed_auto_mounts
[] = {
1687 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1688 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1689 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1690 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1691 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1692 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1693 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1694 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1695 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1696 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1697 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1698 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1699 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1700 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1701 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1702 /* For adding anything that is just a single on/off, but has no
1703 * options: keep mask and flag identical and just define the enum
1704 * value as an unused bit so far
1709 if (lxc_config_value_empty(value
)) {
1710 lxc_conf
->auto_mounts
= 0;
1714 autos
= strdup(value
);
1718 for (autoptr
= autos
;; autoptr
= NULL
) {
1719 token
= strtok_r(autoptr
, " \t", &sptr
);
1725 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1726 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1730 if (!allowed_auto_mounts
[i
].token
) {
1731 ERROR("Invalid filesystem to automount \"%s\"", token
);
1735 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1736 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1743 static int set_config_mount(const char *key
, const char *value
,
1744 struct lxc_conf
*lxc_conf
, void *data
)
1747 struct lxc_list
*mntlist
;
1749 if (lxc_config_value_empty(value
))
1750 return lxc_clear_mount_entries(lxc_conf
);
1752 mntlist
= malloc(sizeof(*mntlist
));
1756 mntelem
= strdup(value
);
1761 mntlist
->elem
= mntelem
;
1763 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1768 static int set_config_cap_keep(const char *key
, const char *value
,
1769 struct lxc_conf
*lxc_conf
, void *data
)
1771 char *keepcaps
, *keepptr
, *sptr
, *token
;
1772 struct lxc_list
*keeplist
;
1775 if (lxc_config_value_empty(value
))
1776 return lxc_clear_config_keepcaps(lxc_conf
);
1778 keepcaps
= strdup(value
);
1782 /* In case several capability keep is specified in a single line
1783 * split these caps in a single element for the list.
1785 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1786 token
= strtok_r(keepptr
, " \t", &sptr
);
1792 if (!strcmp(token
, "none"))
1793 lxc_clear_config_keepcaps(lxc_conf
);
1795 keeplist
= malloc(sizeof(*keeplist
));
1799 keeplist
->elem
= strdup(token
);
1800 if (!keeplist
->elem
) {
1805 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1813 static int set_config_cap_drop(const char *key
, const char *value
,
1814 struct lxc_conf
*lxc_conf
, void *data
)
1816 char *dropcaps
, *dropptr
, *sptr
, *token
;
1817 struct lxc_list
*droplist
;
1820 if (lxc_config_value_empty(value
))
1821 return lxc_clear_config_caps(lxc_conf
);
1823 dropcaps
= strdup(value
);
1827 /* In case several capability drop is specified in a single line
1828 * split these caps in a single element for the list.
1830 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1831 token
= strtok_r(dropptr
, " \t", &sptr
);
1837 droplist
= malloc(sizeof(*droplist
));
1841 droplist
->elem
= strdup(token
);
1842 if (!droplist
->elem
) {
1847 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1855 static int set_config_console_path(const char *key
, const char *value
,
1856 struct lxc_conf
*lxc_conf
, void *data
)
1858 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1861 static int set_config_console_rotate(const char *key
, const char *value
,
1862 struct lxc_conf
*lxc_conf
, void *data
)
1864 if (lxc_config_value_empty(value
)) {
1865 lxc_conf
->console
.log_rotate
= 0;
1869 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1872 if (lxc_conf
->console
.log_rotate
> 1) {
1873 ERROR("The \"lxc.console.rotate\" config key can only be set "
1881 static int set_config_console_logfile(const char *key
, const char *value
,
1882 struct lxc_conf
*lxc_conf
, void *data
)
1884 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1887 static int set_config_console_buffer_size(const char *key
, const char *value
,
1888 struct lxc_conf
*lxc_conf
, void *data
)
1892 uint64_t buffer_size
, pgsz
;
1894 if (lxc_config_value_empty(value
)) {
1895 lxc_conf
->console
.buffer_size
= 0;
1899 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1900 if (!strcmp(value
, "auto")) {
1901 lxc_conf
->console
.buffer_size
= 1 << 17;
1905 ret
= parse_byte_size_string(value
, &size
);
1912 /* must be at least a page size */
1913 pgsz
= lxc_getpagesize();
1914 if ((uint64_t)size
< pgsz
) {
1915 NOTICE("Requested ringbuffer size for the console is %" PRId64
1916 " but must be at least %" PRId64
1917 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1922 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1923 if (buffer_size
== 0)
1926 if (buffer_size
!= size
)
1927 NOTICE("Passed size was not a power of 2. Rounding log size to "
1928 "next power of two: %" PRIu64
" bytes", buffer_size
);
1930 lxc_conf
->console
.buffer_size
= buffer_size
;
1934 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
1935 struct lxc_conf
*lxc_conf
,
1938 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
1941 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1943 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1945 update_hwaddr(line
);
1947 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1948 char *tmp
= realloc(conf
->unexpanded_config
,
1949 conf
->unexpanded_alloced
+ 1024);
1953 if (!conf
->unexpanded_config
)
1955 conf
->unexpanded_config
= tmp
;
1956 conf
->unexpanded_alloced
+= 1024;
1958 strcat(conf
->unexpanded_config
, line
);
1959 conf
->unexpanded_len
+= linelen
;
1960 if (line
[linelen
- 1] != '\n') {
1961 strcat(conf
->unexpanded_config
, "\n");
1962 conf
->unexpanded_len
++;
1968 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1970 struct dirent
*direntp
;
1972 char path
[MAXPATHLEN
];
1976 dir
= opendir(dirp
);
1980 while ((direntp
= readdir(dir
))) {
1985 fnam
= direntp
->d_name
;
1986 if (!strcmp(fnam
, "."))
1989 if (!strcmp(fnam
, ".."))
1993 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
1996 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
1997 if (len
< 0 || len
>= MAXPATHLEN
) {
2002 ret
= lxc_config_read(path
, lxc_conf
, true);
2014 static int set_config_includefiles(const char *key
, const char *value
,
2015 struct lxc_conf
*lxc_conf
, void *data
)
2017 if (lxc_config_value_empty(value
)) {
2018 clr_config_includefiles(key
, lxc_conf
, NULL
);
2023 return do_includedir(value
, lxc_conf
);
2025 return lxc_config_read(value
, lxc_conf
, true);
2028 static int set_config_rootfs_path(const char *key
, const char *value
,
2029 struct lxc_conf
*lxc_conf
, void *data
)
2033 const char *container_path
;
2035 if (lxc_config_value_empty(value
)) {
2036 free(lxc_conf
->rootfs
.path
);
2037 lxc_conf
->rootfs
.path
= NULL
;
2041 dup
= strdup(value
);
2045 /* Split <storage type>:<container path> into <storage type> and
2046 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2047 * "rootfs.path" to <container path>.
2049 tmp
= strchr(dup
, ':');
2052 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2058 container_path
= tmp
;
2060 container_path
= value
;
2063 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2068 static int set_config_rootfs_mount(const char *key
, const char *value
,
2069 struct lxc_conf
*lxc_conf
, void *data
)
2071 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2074 static int set_config_rootfs_options(const char *key
, const char *value
,
2075 struct lxc_conf
*lxc_conf
, void *data
)
2077 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2080 static int set_config_rootfs_backend(const char *key
, const char *value
,
2081 struct lxc_conf
*lxc_conf
, void *data
)
2086 static int set_config_uts_name(const char *key
, const char *value
,
2087 struct lxc_conf
*lxc_conf
, void *data
)
2089 struct utsname
*utsname
;
2091 if (lxc_config_value_empty(value
)) {
2092 clr_config_uts_name(key
, lxc_conf
, NULL
);
2096 utsname
= malloc(sizeof(*utsname
));
2100 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2105 strcpy(utsname
->nodename
, value
);
2106 free(lxc_conf
->utsname
);
2107 lxc_conf
->utsname
= utsname
;
2112 static int set_config_namespace(const char *key
, const char *value
,
2113 struct lxc_conf
*lxc_conf
, void *data
)
2116 const char *namespace;
2118 if (lxc_config_value_empty(value
))
2119 return clr_config_namespace(key
, lxc_conf
, data
);
2121 namespace = key
+ sizeof("lxc.namespace.") - 1;
2122 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2126 return set_config_string_item(&lxc_conf
->inherit_ns
[ns_idx
], value
);
2129 struct parse_line_conf
{
2130 struct lxc_conf
*conf
;
2134 static int parse_line(char *buffer
, void *data
)
2136 char *dot
, *key
, *line
, *linep
, *value
;
2138 struct lxc_config_t
*config
;
2141 struct parse_line_conf
*plc
= data
;
2143 /* If there are newlines in the config file we should keep them. */
2144 empty_line
= lxc_is_line_empty(dup
);
2148 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2149 * modified the original string on the stack by replacing '=' by '\0'
2152 linep
= line
= strdup(dup
);
2156 if (!plc
->from_include
) {
2157 ret
= append_unexp_config_line(line
, plc
->conf
);
2165 line
+= lxc_char_left_gc(line
, strlen(line
));
2167 /* ignore comments */
2171 /* martian option - don't add it to the config itself */
2172 if (strncmp(line
, "lxc.", 4))
2177 dot
= strchr(line
, '=');
2179 ERROR("Invalid configuration line: %s", line
);
2187 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2189 value
+= lxc_char_left_gc(value
, strlen(value
));
2190 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2192 if (*value
== '\'' || *value
== '\"') {
2195 len
= strlen(value
);
2196 if (len
> 1 && value
[len
- 1] == *value
) {
2197 value
[len
- 1] = '\0';
2202 config
= lxc_get_config(key
);
2204 ERROR("Unknown configuration key \"%s\"", key
);
2208 /* [START]: REMOVE IN LXC 3.0 */
2209 if (config
->is_legacy_key
&& !plc
->conf
->contains_legacy_key
) {
2210 plc
->conf
->contains_legacy_key
= true;
2211 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2212 /* Warn the user once loud and clear that there is at
2213 * least one legacy configuration item in the
2214 * configuration file and then an update is required.
2216 fprintf(stderr
, "The configuration file contains "
2217 "legacy configuration keys.\nPlease "
2218 "update your configuration file!\nThe "
2219 "update script lxc-update-config -c "
2220 "<path-to-config> can be used for "
2224 /* [END]: REMOVE IN LXC 3.0 */
2226 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2233 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2235 struct parse_line_conf c
;
2238 c
.from_include
= false;
2240 return parse_line(buffer
, &c
);
2243 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2246 struct parse_line_conf c
;
2249 c
.from_include
= from_include
;
2251 ret
= access(file
, R_OK
);
2255 /* Catch only the top level config file name in the structure. */
2257 conf
->rcfile
= strdup(file
);
2259 return lxc_file_for_each_line(file
, parse_line
, &c
);
2262 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2264 struct lxc_list
*dent
;
2266 dent
= malloc(sizeof(struct lxc_list
));
2271 lxc_list_add_tail(defines
, dent
);
2275 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2277 struct lxc_list
*it
, *next
;
2280 lxc_list_for_each(it
, defines
) {
2281 ret
= lxc_config_readline(it
->elem
, conf
);
2286 lxc_list_for_each_safe(it
, defines
, next
) {
2294 signed long lxc_config_parse_arch(const char *arch
)
2296 #if HAVE_SYS_PERSONALITY_H
2302 { "x86", PER_LINUX32
},
2303 { "linux32", PER_LINUX32
},
2304 { "i386", PER_LINUX32
},
2305 { "i486", PER_LINUX32
},
2306 { "i586", PER_LINUX32
},
2307 { "i686", PER_LINUX32
},
2308 { "athlon", PER_LINUX32
},
2309 { "mips", PER_LINUX32
},
2310 { "mipsel", PER_LINUX32
},
2311 { "ppc", PER_LINUX32
},
2312 { "arm", PER_LINUX32
},
2313 { "armv7l", PER_LINUX32
},
2314 { "armhf", PER_LINUX32
},
2315 { "armel", PER_LINUX32
},
2316 { "powerpc", PER_LINUX32
},
2317 { "linux64", PER_LINUX
},
2318 { "x86_64", PER_LINUX
},
2319 { "amd64", PER_LINUX
},
2320 { "mips64", PER_LINUX
},
2321 { "mips64el", PER_LINUX
},
2322 { "ppc64", PER_LINUX
},
2323 { "ppc64le", PER_LINUX
},
2324 { "ppc64el", PER_LINUX
},
2325 { "powerpc64", PER_LINUX
},
2326 { "s390x", PER_LINUX
},
2327 { "aarch64", PER_LINUX
},
2328 { "arm64", PER_LINUX
},
2330 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2332 for (i
= 0; i
< len
; i
++) {
2333 if (!strcmp(pername
[i
].name
, arch
))
2334 return pername
[i
].per
;
2341 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2343 char *token
, *saveptr
= NULL
;
2349 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2350 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2351 { "LSM", LXC_ATTACH_LSM_EXEC
},
2356 /* For the sake of backward compatibility, drop all privileges
2357 * if none is specified.
2359 for (i
= 0; all_privs
[i
].token
; i
++)
2360 *flags
|= all_privs
[i
].flag
;
2365 token
= strtok_r(flaglist
, "|", &saveptr
);
2368 for (i
= 0; all_privs
[i
].token
; i
++)
2369 if (!strcmp(all_privs
[i
].token
, token
))
2370 aflag
= all_privs
[i
].flag
;
2376 token
= strtok_r(NULL
, "|", &saveptr
);
2382 /* Write out a configuration file. */
2383 void write_config(FILE *fout
, struct lxc_conf
*c
)
2386 size_t len
= c
->unexpanded_len
;
2391 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2393 SYSERROR("Failed to write configuration file");
2396 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2403 len
= strlen(key
) + strlen(v
) + 4;
2406 if (lxc_config_value_empty(v
))
2407 ret
= snprintf(tmp
, len
, "%s =", key
);
2409 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2410 if (ret
< 0 || ret
>= len
)
2413 /* Save the line verbatim into unexpanded_conf */
2414 if (append_unexp_config_line(tmp
, conf
))
2420 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2424 char *lstart
= conf
->unexpanded_config
;
2426 if (!conf
->unexpanded_config
)
2430 lend
= strchr(lstart
, '\n');
2433 lend
= lstart
+ strlen(lstart
);
2436 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2441 v
= lstart
[strlen(key
)];
2442 if (!isspace(v
) && v
!= '=') {
2447 conf
->unexpanded_len
-= (lend
- lstart
);
2448 if (*lend
== '\0') {
2452 memmove(lstart
, lend
, strlen(lend
) + 1);
2456 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2457 const char *newpath
, const char *oldname
,
2458 const char *newname
, const char *ovldir
)
2461 char *lend
, *newdir
, *olddir
, *p
, *q
;
2462 size_t newdirlen
, olddirlen
;
2463 char *lstart
= conf
->unexpanded_config
;
2464 const char *key
= "lxc.mount.entry";
2466 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2467 olddir
= alloca(olddirlen
+ 1);
2468 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2470 if (ret
< 0 || ret
>= olddirlen
+ 1)
2473 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2474 newdir
= alloca(newdirlen
+ 1);
2475 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2477 if (ret
< 0 || ret
>= newdirlen
+ 1)
2480 if (!conf
->unexpanded_config
)
2484 lend
= strchr(lstart
, '\n');
2486 lend
= lstart
+ strlen(lstart
);
2490 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2493 p
= strchr(lstart
+ strlen(key
), '=');
2504 /* Whenever an lxc.mount.entry entry is found in a line we check
2505 * if the substring " overlay" or the substring " aufs" is
2506 * present before doing any further work. We check for "
2507 * overlay" and " aufs" since both substrings need to have at
2508 * least one space before them in a valid overlay
2509 * lxc.mount.entry (/A B overlay). When the space before is
2510 * missing it is very likely that these substrings are part of a
2511 * path or something else. (Checking q >= lend ensures that we
2512 * only count matches in the current line.) */
2513 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2514 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2517 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2520 /* replace the olddir with newdir */
2521 if (olddirlen
>= newdirlen
) {
2522 size_t diff
= olddirlen
- newdirlen
;
2523 memcpy(q
, newdir
, newdirlen
);
2524 if (olddirlen
!= newdirlen
) {
2525 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2526 strlen(q
) - newdirlen
- diff
+ 1);
2528 conf
->unexpanded_len
-= diff
;
2532 size_t diff
= newdirlen
- olddirlen
;
2533 size_t oldlen
= conf
->unexpanded_len
;
2534 size_t newlen
= oldlen
+ diff
;
2535 size_t poffset
= q
- conf
->unexpanded_config
;
2537 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2541 conf
->unexpanded_len
= newlen
;
2542 conf
->unexpanded_alloced
= newlen
+ 1;
2543 new[newlen
- 1] = '\0';
2544 lend
= new + (lend
- conf
->unexpanded_config
);
2545 /* Move over the remainder to make room for the newdir.
2547 memmove(new + poffset
+ newdirlen
,
2548 new + poffset
+ olddirlen
,
2549 oldlen
- poffset
- olddirlen
+ 1);
2550 conf
->unexpanded_config
= new;
2551 memcpy(new + poffset
, newdir
, newdirlen
);
2561 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2562 const char *newpath
, const char *oldname
,
2563 const char *newname
)
2566 char *lend
, *newdir
, *olddir
, *p
;
2567 char *lstart
= conf
->unexpanded_config
;
2568 size_t newdirlen
, olddirlen
;
2569 const char *key
= "lxc.hook";
2571 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2572 olddir
= alloca(olddirlen
+ 1);
2573 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2574 if (ret
< 0 || ret
>= olddirlen
+ 1)
2577 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2578 newdir
= alloca(newdirlen
+ 1);
2579 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2580 if (ret
< 0 || ret
>= newdirlen
+ 1)
2583 if (!conf
->unexpanded_config
)
2587 lend
= strchr(lstart
, '\n');
2589 lend
= lstart
+ strlen(lstart
);
2593 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2596 p
= strchr(lstart
+ strlen(key
), '=');
2607 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2610 /* replace the olddir with newdir */
2611 if (olddirlen
>= newdirlen
) {
2612 size_t diff
= olddirlen
- newdirlen
;
2613 memcpy(p
, newdir
, newdirlen
);
2614 if (olddirlen
!= newdirlen
) {
2615 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2616 strlen(p
) - newdirlen
- diff
+ 1);
2618 conf
->unexpanded_len
-= diff
;
2622 size_t diff
= newdirlen
- olddirlen
;
2623 size_t oldlen
= conf
->unexpanded_len
;
2624 size_t newlen
= oldlen
+ diff
;
2625 size_t poffset
= p
- conf
->unexpanded_config
;
2627 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2631 conf
->unexpanded_len
= newlen
;
2632 conf
->unexpanded_alloced
= newlen
+ 1;
2633 new[newlen
- 1] = '\0';
2634 lend
= new + (lend
- conf
->unexpanded_config
);
2635 /* Move over the remainder to make room for the newdir.
2637 memmove(new + poffset
+ newdirlen
,
2638 new + poffset
+ olddirlen
,
2639 oldlen
- poffset
- olddirlen
+ 1);
2640 conf
->unexpanded_config
= new;
2641 memcpy(new + poffset
, newdir
, newdirlen
);
2654 ERROR("Error writing to new config"); \
2659 /* This is called only from clone. We wish to update all hwaddrs in the
2660 * unexpanded config file. We can't/don't want to update any which come from
2661 * lxc.includes (there shouldn't be any).
2662 * We can't just walk the c->lxc-conf->network list because that includes netifs
2663 * from the include files. So we update the ones which we find in the unexp
2664 * config file, then find the original macaddr in the conf->network, and update
2665 * that to the same value.
2667 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2669 char *lend
, *p
, *p2
;
2670 struct lxc_list
*it
;
2671 char *lstart
= conf
->unexpanded_config
;
2673 if (!conf
->unexpanded_config
)
2677 char newhwaddr
[18], oldhwaddr
[17];
2679 lend
= strchr(lstart
, '\n');
2681 lend
= lstart
+ strlen(lstart
);
2685 if (!lxc_config_net_hwaddr(lstart
)) {
2690 p
= strchr(lstart
, '=');
2703 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2706 if ((p2
- p
) != 17) {
2707 WARN("Bad hwaddr entry");
2712 memcpy(oldhwaddr
, p
, 17);
2714 if (!new_hwaddr(newhwaddr
))
2717 memcpy(p
, newhwaddr
, 17);
2718 lxc_list_for_each(it
, &conf
->network
) {
2719 struct lxc_netdev
*n
= it
->elem
;
2721 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2722 memcpy(n
->hwaddr
, newhwaddr
, 17);
2731 static int set_config_ephemeral(const char *key
, const char *value
,
2732 struct lxc_conf
*lxc_conf
, void *data
)
2734 if (lxc_config_value_empty(value
)) {
2735 lxc_conf
->ephemeral
= 0;
2739 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2742 if (lxc_conf
->ephemeral
> 1)
2748 static int set_config_log_syslog(const char *key
, const char *value
,
2749 struct lxc_conf
*lxc_conf
, void *data
)
2753 if (lxc_conf
->syslog
) {
2754 free(lxc_conf
->syslog
);
2755 lxc_conf
->syslog
= NULL
;
2758 if (lxc_config_value_empty(value
))
2761 facility
= lxc_syslog_priority_to_int(value
);
2762 if (facility
== -EINVAL
)
2765 lxc_log_syslog(facility
);
2766 return set_config_string_item(&lxc_conf
->syslog
, value
);
2769 static int set_config_no_new_privs(const char *key
, const char *value
,
2770 struct lxc_conf
*lxc_conf
, void *data
)
2774 if (lxc_config_value_empty(value
)) {
2775 lxc_conf
->no_new_privs
= false;
2779 if (lxc_safe_uint(value
, &v
) < 0)
2785 lxc_conf
->no_new_privs
= v
? true : false;
2790 static int set_config_noop(const char *key
, const char *value
,
2791 struct lxc_conf
*lxc_conf
, void *data
)
2796 /* Callbacks to get configuration items. */
2797 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2798 struct lxc_conf
*c
, void *data
)
2805 memset(retv
, 0, inlen
);
2807 #if HAVE_SYS_PERSONALITY_H
2810 switch (c
->personality
) {
2812 strprint(retv
, inlen
, "i686");
2815 strprint(retv
, inlen
, "x86_64");
2825 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2826 struct lxc_conf
*c
, void *data
)
2828 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2831 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2832 struct lxc_conf
*c
, void *data
)
2834 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2837 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2838 struct lxc_conf
*c
, void *data
)
2840 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2843 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2844 struct lxc_conf
*c
, void *data
)
2846 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2849 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2850 int inlen
, struct lxc_conf
*c
,
2853 return lxc_get_conf_int(c
, retv
, inlen
,
2854 c
->lsm_aa_allow_incomplete
);
2857 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2858 struct lxc_conf
*c
, void *data
)
2860 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2863 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2864 * just the value(s) will be printed. Since there still could be more than one,
2865 * it is newline-separated.
2866 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2868 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2869 * 'lxc.cgroup.subsystem.key = value' format.
2871 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
2872 struct lxc_conf
*c
, void *data
)
2874 struct lxc_list
*it
;
2877 bool get_all
= false;
2882 memset(retv
, 0, inlen
);
2884 if (!strcmp(key
, "lxc.cgroup"))
2886 else if (!strncmp(key
, "lxc.cgroup.", 11))
2891 lxc_list_for_each(it
, &c
->cgroup
) {
2892 struct lxc_cgroup
*cg
= it
->elem
;
2895 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n",
2896 cg
->subsystem
, cg
->value
);
2897 } else if (!strcmp(cg
->subsystem
, key
)) {
2898 strprint(retv
, inlen
, "%s\n", cg
->value
);
2905 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
2906 struct lxc_conf
*lxc_conf
, void *data
)
2914 memset(retv
, 0, inlen
);
2916 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
2921 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2922 struct lxc_conf
*c
, void *data
)
2924 struct lxc_list
*it
;
2925 int len
, listlen
, ret
;
2927 /* "u 1000 1000000 65536"
2929 * let's render this as
2947 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2948 char buf
[__LXC_IDMAP_STR_BUF
];
2953 memset(retv
, 0, inlen
);
2955 listlen
= lxc_list_len(&c
->id_map
);
2956 lxc_list_for_each(it
, &c
->id_map
)
2958 struct id_map
*map
= it
->elem
;
2959 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
2960 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
2961 map
->nsid
, map
->hostid
, map
->range
);
2962 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
2965 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
2970 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
2971 struct lxc_conf
*c
, void *data
)
2974 v
= lxc_log_priority_to_string(c
->loglevel
);
2975 return lxc_get_conf_str(retv
, inlen
, v
);
2978 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
2979 struct lxc_conf
*c
, void *data
)
2981 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
2984 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
2985 struct lxc_conf
*c
, void *data
)
2987 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
2990 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
2991 struct lxc_conf
*c
, void *data
)
2993 int len
, fulllen
= 0;
2994 const char *sep
= "";
2999 memset(retv
, 0, inlen
);
3001 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3004 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3005 case LXC_AUTO_PROC_MIXED
:
3006 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3009 case LXC_AUTO_PROC_RW
:
3010 strprint(retv
, inlen
, "%sproc:rw", sep
);
3017 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3018 case LXC_AUTO_SYS_RO
:
3019 strprint(retv
, inlen
, "%ssys:ro", sep
);
3022 case LXC_AUTO_SYS_RW
:
3023 strprint(retv
, inlen
, "%ssys:rw", sep
);
3026 case LXC_AUTO_SYS_MIXED
:
3027 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3034 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3035 case LXC_AUTO_CGROUP_NOSPEC
:
3036 strprint(retv
, inlen
, "%scgroup", sep
);
3038 case LXC_AUTO_CGROUP_MIXED
:
3039 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3041 case LXC_AUTO_CGROUP_RO
:
3042 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3044 case LXC_AUTO_CGROUP_RW
:
3045 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3047 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3048 strprint(retv
, inlen
, "%scgroup-full", sep
);
3050 case LXC_AUTO_CGROUP_FULL_MIXED
:
3051 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3053 case LXC_AUTO_CGROUP_FULL_RO
:
3054 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3056 case LXC_AUTO_CGROUP_FULL_RW
:
3057 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3066 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3067 struct lxc_conf
*c
, void *data
)
3069 int len
, fulllen
= 0;
3070 struct lxc_list
*it
;
3075 memset(retv
, 0, inlen
);
3077 lxc_list_for_each(it
, &c
->mount_list
) {
3078 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3084 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3085 struct lxc_conf
*c
, void *data
)
3087 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3090 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3091 struct lxc_conf
*c
, void *data
)
3093 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3096 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3097 struct lxc_conf
*c
, void *data
)
3099 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3102 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3103 struct lxc_conf
*c
, void *data
)
3108 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3109 struct lxc_conf
*c
, void *data
)
3111 return lxc_get_conf_str(
3113 c
->utsname
? c
->utsname
->nodename
: NULL
);
3116 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3117 struct lxc_conf
*c
, void *data
)
3120 int len
, fulllen
= 0, found
= -1;
3121 struct lxc_list
*it
;
3124 subkey
= strchr(key
, '.');
3126 subkey
= strchr(subkey
+ 1, '.');
3132 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3133 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3144 memset(retv
, 0, inlen
);
3146 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3147 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3152 static int get_config_net(const char *key
, char *retv
, int inlen
,
3153 struct lxc_conf
*c
, void *data
)
3155 int len
, fulllen
= 0;
3156 struct lxc_list
*it
;
3161 memset(retv
, 0, inlen
);
3163 lxc_list_for_each(it
, &c
->network
) {
3164 struct lxc_netdev
*n
= it
->elem
;
3165 const char *t
= lxc_net_type_to_str(n
->type
);
3166 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3172 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3173 struct lxc_conf
*c
, void *data
)
3175 int len
, fulllen
= 0;
3176 struct lxc_list
*it
;
3181 memset(retv
, 0, inlen
);
3183 lxc_list_for_each(it
, &c
->caps
) {
3184 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3190 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3191 struct lxc_conf
*c
, void *data
)
3193 int len
, fulllen
= 0;
3194 struct lxc_list
*it
;
3199 memset(retv
, 0, inlen
);
3201 lxc_list_for_each(it
, &c
->keepcaps
) {
3202 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3208 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3209 struct lxc_conf
*c
, void *data
)
3211 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3214 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3215 struct lxc_conf
*c
, void *data
)
3217 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3220 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3221 struct lxc_conf
*c
, void *data
)
3223 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3227 static int get_config_console_buffer_size(const char *key
, char *retv
,
3228 int inlen
, struct lxc_conf
*c
,
3231 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->autodev
);
3234 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3235 int inlen
, struct lxc_conf
*c
,
3238 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3241 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3242 struct lxc_conf
*c
, void *data
)
3244 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3247 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3248 struct lxc_conf
*c
, void *data
)
3250 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3253 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3254 struct lxc_conf
*c
, void *data
)
3256 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3259 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3260 struct lxc_conf
*c
, void *data
)
3262 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3265 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3266 struct lxc_conf
*c
, void *data
)
3268 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3271 static int get_config_start(const char *key
, char *retv
, int inlen
,
3272 struct lxc_conf
*c
, void *data
)
3274 if (strcmp(key
+ 10, "auto") == 0)
3275 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3276 else if (strcmp(key
+ 10, "delay") == 0)
3277 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3278 else if (strcmp(key
+ 10, "order") == 0)
3279 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3284 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3285 struct lxc_conf
*c
, void *data
)
3287 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3290 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3291 struct lxc_conf
*c
, void *data
)
3293 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3296 static int get_config_group(const char *key
, char *retv
, int inlen
,
3297 struct lxc_conf
*c
, void *data
)
3299 int len
, fulllen
= 0;
3300 struct lxc_list
*it
;
3305 memset(retv
, 0, inlen
);
3307 lxc_list_for_each(it
, &c
->groups
) {
3308 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3314 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3315 struct lxc_conf
*c
, void *data
)
3317 int len
, fulllen
= 0;
3318 struct lxc_list
*it
;
3323 memset(retv
, 0, inlen
);
3325 lxc_list_for_each(it
, &c
->environment
) {
3326 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3332 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3333 struct lxc_conf
*c
, void *data
)
3335 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3338 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3339 struct lxc_conf
*c
, void *data
)
3341 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3344 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3345 struct lxc_conf
*c
, void *data
)
3347 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3350 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3351 struct lxc_conf
*c
, void *data
)
3353 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3356 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3357 struct lxc_conf
*c
, void *data
)
3359 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3362 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3363 struct lxc_conf
*c
, void *data
)
3365 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3368 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3369 struct lxc_conf
*c
, void *data
)
3371 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3374 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3375 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3376 * printed, in 'lxc.prlimit.resource = value' format.
3378 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3379 struct lxc_conf
*c
, void *data
)
3381 int fulllen
= 0, len
;
3382 bool get_all
= false;
3383 struct lxc_list
*it
;
3388 memset(retv
, 0, inlen
);
3390 if (!strcmp(key
, "lxc.prlimit"))
3392 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3397 lxc_list_for_each(it
, &c
->limits
) {
3398 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3399 integers or the word
3402 struct lxc_limit
*lim
= it
->elem
;
3404 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3405 memcpy(buf
, "unlimited", sizeof("unlimited"));
3406 partlen
= sizeof("unlimited") - 1;
3408 partlen
= sprintf(buf
, "%" PRIu64
,
3409 (uint64_t)lim
->limit
.rlim_cur
);
3411 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3412 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3413 memcpy(buf
+ partlen
, ":unlimited",
3414 sizeof(":unlimited"));
3416 sprintf(buf
+ partlen
, ":%" PRIu64
,
3417 (uint64_t)lim
->limit
.rlim_max
);
3421 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3422 lim
->resource
, buf
);
3423 } else if (!strcmp(lim
->resource
, key
)) {
3424 strprint(retv
, inlen
, "%s", buf
);
3431 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3432 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3433 * entries will be printed, in 'lxc.sysctl.key = value' format.
3435 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3436 struct lxc_conf
*c
, void *data
)
3439 struct lxc_list
*it
;
3441 bool get_all
= false;
3446 memset(retv
, 0, inlen
);
3448 if (strcmp(key
, "lxc.sysctl") == 0)
3450 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3451 key
+= sizeof("lxc.sysctl.") - 1;
3455 lxc_list_for_each(it
, &c
->sysctls
) {
3456 struct lxc_sysctl
*elem
= it
->elem
;
3458 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3459 elem
->key
, elem
->value
);
3460 } else if (strcmp(elem
->key
, key
) == 0) {
3461 strprint(retv
, inlen
, "%s", elem
->value
);
3468 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3469 struct lxc_conf
*c
, void *data
)
3471 struct lxc_list
*it
;
3474 bool get_all
= false;
3479 memset(retv
, 0, inlen
);
3481 if (strcmp(key
, "lxc.proc") == 0)
3483 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3484 key
+= sizeof("lxc.proc.") - 1;
3488 lxc_list_for_each(it
, &c
->procs
) {
3489 struct lxc_proc
*proc
= it
->elem
;
3492 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3493 proc
->filename
, proc
->value
);
3494 } else if (strcmp(proc
->filename
, key
) == 0) {
3495 strprint(retv
, inlen
, "%s", proc
->value
);
3502 static int get_config_noop(const char *key
, char *retv
, int inlen
,
3503 struct lxc_conf
*c
, void *data
)
3508 static int get_config_namespace(const char *key
, char *retv
, int inlen
,
3509 struct lxc_conf
*c
, void *data
)
3512 const char *namespace;
3518 memset(retv
, 0, inlen
);
3520 namespace = key
+ sizeof("lxc.namespace.") - 1;
3521 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3525 strprint(retv
, inlen
, "%s", c
->inherit_ns
[ns_idx
]);
3530 /* Callbacks to clear config items. */
3531 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3534 c
->personality
= -1;
3538 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3545 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3552 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3560 static inline int clr_config_apparmor_profile(const char *key
,
3561 struct lxc_conf
*c
, void *data
)
3563 free(c
->lsm_aa_profile
);
3564 c
->lsm_aa_profile
= NULL
;
3568 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3572 c
->lsm_aa_allow_incomplete
= 0;
3576 static inline int clr_config_selinux_context(const char *key
,
3577 struct lxc_conf
*c
, void *data
)
3579 free(c
->lsm_se_context
);
3580 c
->lsm_se_context
= NULL
;
3584 static inline int clr_config_cgroup_controller(const char *key
,
3585 struct lxc_conf
*c
, void *data
)
3587 return lxc_clear_cgroups(c
, key
);
3590 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3593 if (lxc_conf
->cgroup_meta
.dir
) {
3594 free(lxc_conf
->cgroup_meta
.dir
);
3595 lxc_conf
->cgroup_meta
.dir
= NULL
;
3601 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3604 return lxc_clear_idmaps(c
);
3607 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3610 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3614 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3622 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3625 return lxc_clear_mount_entries(c
);
3628 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3631 return lxc_clear_automounts(c
);
3634 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3642 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3645 free(c
->rootfs
.path
);
3646 c
->rootfs
.path
= NULL
;
3650 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3653 free(c
->rootfs
.mount
);
3654 c
->rootfs
.mount
= NULL
;
3658 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3661 free(c
->rootfs
.options
);
3662 c
->rootfs
.options
= NULL
;
3666 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3672 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3680 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3683 return lxc_clear_hooks(c
, key
);
3686 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3689 lxc_free_networks(&c
->network
);
3694 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3697 return lxc_clear_config_caps(c
);
3700 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3703 return lxc_clear_config_keepcaps(c
);
3706 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3709 free(c
->console
.path
);
3710 c
->console
.path
= NULL
;
3714 static inline int clr_config_console_logfile(const char *key
,
3715 struct lxc_conf
*c
, void *data
)
3717 free(c
->console
.log_path
);
3718 c
->console
.log_path
= NULL
;
3722 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3725 c
->console
.log_rotate
= 0;
3729 static inline int clr_config_console_buffer_size(const char *key
,
3730 struct lxc_conf
*c
, void *data
)
3732 c
->console
.buffer_size
= 0;
3736 static inline int clr_config_console_buffer_logfile(const char *key
,
3740 free(c
->console
.buffer_log_file
);
3741 c
->console
.buffer_log_file
= NULL
;
3745 static inline int clr_config_seccomp_profile(const char *key
,
3746 struct lxc_conf
*c
, void *data
)
3753 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3760 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3767 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3770 c
->rebootsignal
= 0;
3774 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3781 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3784 if (strcmp(key
+ 10, "auto") == 0)
3786 else if (strcmp(key
+ 10, "delay") == 0)
3788 else if (strcmp(key
+ 10, "order") == 0)
3794 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3802 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3805 c
->monitor_unshare
= 0;
3809 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3812 return lxc_clear_groups(c
);
3815 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3818 return lxc_clear_environment(c
);
3821 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
3824 free(c
->execute_cmd
);
3825 c
->execute_cmd
= NULL
;
3829 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3837 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
3845 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3852 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3859 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3866 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3869 c
->no_new_privs
= false;
3873 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3876 return lxc_clear_limits(c
, key
);
3879 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
3882 return lxc_clear_sysctls(c
, key
);
3885 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
3888 return lxc_clear_procs(c
, key
);
3891 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3894 lxc_clear_includes(c
);
3898 static inline int clr_config_noop(const char *key
, struct lxc_conf
*c
,
3904 static int clr_config_namespace(const char *key
, struct lxc_conf
*lxc_conf
,
3908 const char *namespace;
3910 namespace = key
+ sizeof("lxc.namespace.") - 1;
3911 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3915 free(lxc_conf
->inherit_ns
[ns_idx
]);
3916 lxc_conf
->inherit_ns
[ns_idx
] = NULL
;
3921 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3922 struct lxc_conf
*c
, void *data
)
3927 static struct lxc_config_t
*get_network_config_ops(const char *key
,
3928 struct lxc_conf
*lxc_conf
,
3930 char **deindexed_key
)
3933 unsigned int tmpidx
;
3935 char *copy
, *idx_start
, *idx_end
;
3936 struct lxc_config_t
*config
= NULL
;
3938 /* check that this is a sensible network key */
3939 if (strncmp("lxc.net.", key
, 8)) {
3940 ERROR("Invalid network configuration key \"%s\"", key
);
3946 ERROR("Failed to duplicate string \"%s\"", key
);
3951 if (!isdigit(*(key
+ 8))) {
3952 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
3956 /* beginning of index string */
3957 idx_start
= (copy
+ 7);
3960 /* end of index string */
3961 idx_end
= strchr((copy
+ 8), '.');
3965 /* parse current index */
3966 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
3968 ERROR("Failed to parse usigned integer from string \"%s\": %s",
3969 idx_start
+ 1, strerror(-ret
));
3974 /* This, of course is utterly nonsensical on so many levels, but
3975 * better safe than sorry.
3976 * (Checking for INT_MAX here is intentional.)
3978 if (tmpidx
== INT_MAX
) {
3979 SYSERROR("Number of configured networks would overflow the "
3985 numstrlen
= strlen((idx_start
+ 1));
3987 /* repair configuration key */
3990 /* lxc.net.<idx>.<subkey> */
3993 if (strlen(idx_end
+ 1) == 0) {
3994 ERROR("No subkey in network configuration key \"%s\"", key
);
3998 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
3999 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4001 config
= lxc_get_config(copy
);
4003 ERROR("Unknown network configuration key \"%s\"", key
);
4009 *deindexed_key
= copy
;
4018 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4019 * found. So we make sure next comes an integer, find the right callback (by
4020 * rewriting the key), and call it.
4022 static int set_config_net_nic(const char *key
, const char *value
,
4023 struct lxc_conf
*lxc_conf
, void *data
)
4026 const char *idxstring
;
4027 struct lxc_config_t
*config
;
4028 struct lxc_netdev
*netdev
;
4030 char *deindexed_key
= NULL
;
4032 idxstring
= key
+ 8;
4033 if (!isdigit(*idxstring
))
4036 if (lxc_config_value_empty(value
))
4037 return clr_config_net_nic(key
, lxc_conf
, data
);
4039 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4040 if (!config
|| idx
< 0)
4043 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4045 free(deindexed_key
);
4049 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4050 free(deindexed_key
);
4054 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4058 const char *idxstring
;
4059 struct lxc_config_t
*config
;
4060 struct lxc_netdev
*netdev
;
4062 char *deindexed_key
= NULL
;
4064 idxstring
= key
+ 8;
4065 if (!isdigit(*idxstring
))
4068 /* The left conjunct is pretty self-explanatory. The right conjunct
4069 * checks whether the two pointers are equal. If they are we know that
4070 * this is not a key that is namespaced any further and so we are
4071 * supposed to clear the whole network.
4073 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4074 unsigned int rmnetdevidx
;
4076 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4079 /* Remove network from network list. */
4080 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4084 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4085 if (!config
|| idx
< 0)
4088 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4090 free(deindexed_key
);
4094 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4095 free(deindexed_key
);
4099 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4102 struct lxc_netdev
*netdev
= data
;
4112 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4115 struct lxc_netdev
*netdev
= data
;
4120 netdev
->name
[0] = '\0';
4125 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4128 struct lxc_netdev
*netdev
= data
;
4138 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4141 struct lxc_netdev
*netdev
= data
;
4146 netdev
->link
[0] = '\0';
4151 static int clr_config_net_macvlan_mode(const char *key
,
4152 struct lxc_conf
*lxc_conf
, void *data
)
4154 struct lxc_netdev
*netdev
= data
;
4159 if (netdev
->type
!= LXC_NET_MACVLAN
)
4162 netdev
->priv
.macvlan_attr
.mode
= -1;
4167 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4170 struct lxc_netdev
*netdev
= data
;
4175 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4180 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4183 struct lxc_netdev
*netdev
= data
;
4188 free(netdev
->upscript
);
4189 netdev
->upscript
= NULL
;
4194 static int clr_config_net_script_down(const char *key
,
4195 struct lxc_conf
*lxc_conf
, void *data
)
4197 struct lxc_netdev
*netdev
= data
;
4202 free(netdev
->downscript
);
4203 netdev
->downscript
= NULL
;
4208 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4211 struct lxc_netdev
*netdev
= data
;
4216 free(netdev
->hwaddr
);
4217 netdev
->hwaddr
= NULL
;
4222 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4225 struct lxc_netdev
*netdev
= data
;
4236 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4239 struct lxc_netdev
*netdev
= data
;
4244 netdev
->priv
.vlan_attr
.vid
= 0;
4249 static int clr_config_net_ipv4_gateway(const char *key
,
4250 struct lxc_conf
*lxc_conf
, void *data
)
4252 struct lxc_netdev
*netdev
= data
;
4257 free(netdev
->ipv4_gateway
);
4258 netdev
->ipv4_gateway
= NULL
;
4263 static int clr_config_net_ipv4_address(const char *key
,
4264 struct lxc_conf
*lxc_conf
, void *data
)
4266 struct lxc_netdev
*netdev
= data
;
4267 struct lxc_list
*cur
, *next
;
4272 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4281 static int clr_config_net_ipv6_gateway(const char *key
,
4282 struct lxc_conf
*lxc_conf
, void *data
)
4284 struct lxc_netdev
*netdev
= data
;
4289 free(netdev
->ipv6_gateway
);
4290 netdev
->ipv6_gateway
= NULL
;
4295 static int clr_config_net_ipv6_address(const char *key
,
4296 struct lxc_conf
*lxc_conf
, void *data
)
4298 struct lxc_netdev
*netdev
= data
;
4299 struct lxc_list
*cur
, *next
;
4304 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4313 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4314 struct lxc_conf
*c
, void *data
)
4317 const char *idxstring
;
4318 struct lxc_config_t
*config
;
4319 struct lxc_netdev
*netdev
;
4321 char *deindexed_key
= NULL
;
4323 idxstring
= key
+ 8;
4324 if (!isdigit(*idxstring
))
4327 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4328 if (!config
|| idx
< 0)
4331 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4333 free(deindexed_key
);
4337 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4338 free(deindexed_key
);
4342 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4343 struct lxc_conf
*c
, void *data
)
4347 struct lxc_netdev
*netdev
= data
;
4352 memset(retv
, 0, inlen
);
4357 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4362 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4363 struct lxc_conf
*c
, void *data
)
4367 struct lxc_netdev
*netdev
= data
;
4372 memset(retv
, 0, inlen
);
4377 if (netdev
->flags
& IFF_UP
)
4378 strprint(retv
, inlen
, "up");
4383 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4384 struct lxc_conf
*c
, void *data
)
4388 struct lxc_netdev
*netdev
= data
;
4393 memset(retv
, 0, inlen
);
4398 if (netdev
->link
[0] != '\0')
4399 strprint(retv
, inlen
, "%s", netdev
->link
);
4404 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4405 struct lxc_conf
*c
, void *data
)
4409 struct lxc_netdev
*netdev
= data
;
4414 memset(retv
, 0, inlen
);
4419 if (netdev
->name
[0] != '\0')
4420 strprint(retv
, inlen
, "%s", netdev
->name
);
4425 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4426 struct lxc_conf
*c
, void *data
)
4431 struct lxc_netdev
*netdev
= data
;
4436 memset(retv
, 0, inlen
);
4441 if (netdev
->type
!= LXC_NET_MACVLAN
)
4444 switch (netdev
->priv
.macvlan_attr
.mode
) {
4445 case MACVLAN_MODE_PRIVATE
:
4448 case MACVLAN_MODE_VEPA
:
4451 case MACVLAN_MODE_BRIDGE
:
4454 case MACVLAN_MODE_PASSTHRU
:
4462 strprint(retv
, inlen
, "%s", mode
);
4467 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4468 struct lxc_conf
*c
, void *data
)
4472 struct lxc_netdev
*netdev
= data
;
4477 memset(retv
, 0, inlen
);
4482 if (netdev
->type
!= LXC_NET_VETH
)
4485 strprint(retv
, inlen
, "%s",
4486 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4487 ? netdev
->priv
.veth_attr
.pair
4488 : netdev
->priv
.veth_attr
.veth1
);
4493 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4494 struct lxc_conf
*c
, void *data
)
4498 struct lxc_netdev
*netdev
= data
;
4503 memset(retv
, 0, inlen
);
4508 if (netdev
->upscript
)
4509 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4514 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4515 struct lxc_conf
*c
, void *data
)
4519 struct lxc_netdev
*netdev
= data
;
4524 memset(retv
, 0, inlen
);
4529 if (netdev
->downscript
)
4530 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4535 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4536 struct lxc_conf
*c
, void *data
)
4540 struct lxc_netdev
*netdev
= data
;
4545 memset(retv
, 0, inlen
);
4551 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4556 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4557 struct lxc_conf
*c
, void *data
)
4561 struct lxc_netdev
*netdev
= data
;
4566 memset(retv
, 0, inlen
);
4572 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4577 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4578 struct lxc_conf
*c
, void *data
)
4582 struct lxc_netdev
*netdev
= data
;
4587 memset(retv
, 0, inlen
);
4592 if (netdev
->type
!= LXC_NET_VLAN
)
4595 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4600 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4601 struct lxc_conf
*c
, void *data
)
4604 char buf
[INET_ADDRSTRLEN
];
4606 struct lxc_netdev
*netdev
= data
;
4611 memset(retv
, 0, inlen
);
4616 if (netdev
->ipv4_gateway_auto
) {
4617 strprint(retv
, inlen
, "auto");
4618 } else if (netdev
->ipv4_gateway
) {
4619 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4620 strprint(retv
, inlen
, "%s", buf
);
4626 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4627 struct lxc_conf
*c
, void *data
)
4631 char buf
[INET_ADDRSTRLEN
];
4632 struct lxc_list
*it
;
4634 struct lxc_netdev
*netdev
= data
;
4639 memset(retv
, 0, inlen
);
4644 listlen
= lxc_list_len(&netdev
->ipv4
);
4645 lxc_list_for_each(it
, &netdev
->ipv4
) {
4646 struct lxc_inetdev
*i
= it
->elem
;
4647 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4648 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4649 (listlen
-- > 1) ? "\n" : "");
4655 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4656 struct lxc_conf
*c
, void *data
)
4659 char buf
[INET6_ADDRSTRLEN
];
4661 struct lxc_netdev
*netdev
= data
;
4666 memset(retv
, 0, inlen
);
4671 if (netdev
->ipv6_gateway_auto
) {
4672 strprint(retv
, inlen
, "auto");
4673 } else if (netdev
->ipv6_gateway
) {
4674 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4675 strprint(retv
, inlen
, "%s", buf
);
4681 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4682 struct lxc_conf
*c
, void *data
)
4686 char buf
[INET6_ADDRSTRLEN
];
4687 struct lxc_list
*it
;
4689 struct lxc_netdev
*netdev
= data
;
4694 memset(retv
, 0, inlen
);
4699 listlen
= lxc_list_len(&netdev
->ipv6
);
4700 lxc_list_for_each(it
, &netdev
->ipv6
) {
4701 struct lxc_inet6dev
*i
= it
->elem
;
4702 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4703 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4704 (listlen
-- > 1) ? "\n" : "");
4710 int lxc_list_config_items(char *retv
, int inlen
)
4719 memset(retv
, 0, inlen
);
4721 for (i
= 0; i
< config_size
; i
++) {
4722 char *s
= config
[i
].name
;
4724 if (s
[strlen(s
) - 1] == '.')
4727 strprint(retv
, inlen
, "%s\n", s
);
4733 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4742 memset(retv
, 0, inlen
);
4744 if (!strcmp(key
, "lxc.apparmor")) {
4745 strprint(retv
, inlen
, "allow_incomplete\n");
4746 strprint(retv
, inlen
, "profile\n");
4747 } else if (!strcmp(key
, "lxc.cgroup")) {
4748 strprint(retv
, inlen
, "dir\n");
4749 } else if (!strcmp(key
, "lxc.selinux")) {
4750 strprint(retv
, inlen
, "context\n");
4751 } else if (!strcmp(key
, "lxc.mount")) {
4752 strprint(retv
, inlen
, "auto\n");
4753 strprint(retv
, inlen
, "entry\n");
4754 strprint(retv
, inlen
, "fstab\n");
4755 } else if (!strcmp(key
, "lxc.rootfs")) {
4756 strprint(retv
, inlen
, "mount\n");
4757 strprint(retv
, inlen
, "options\n");
4758 strprint(retv
, inlen
, "path\n");
4759 } else if (!strcmp(key
, "lxc.uts")) {
4760 strprint(retv
, inlen
, "name\n");
4761 } else if (!strcmp(key
, "lxc.hook")) {
4762 strprint(retv
, inlen
, "autodev\n");
4763 strprint(retv
, inlen
, "clone\n");
4764 strprint(retv
, inlen
, "destroy\n");
4765 strprint(retv
, inlen
, "mount\n");
4766 strprint(retv
, inlen
, "post-stop\n");
4767 strprint(retv
, inlen
, "pre-mount\n");
4768 strprint(retv
, inlen
, "pre-start\n");
4769 strprint(retv
, inlen
, "start-host\n");
4770 strprint(retv
, inlen
, "start\n");
4771 strprint(retv
, inlen
, "stop\n");
4772 } else if (!strcmp(key
, "lxc.cap")) {
4773 strprint(retv
, inlen
, "drop\n");
4774 strprint(retv
, inlen
, "keep\n");
4775 } else if (!strcmp(key
, "lxc.console")) {
4776 strprint(retv
, inlen
, "logfile\n");
4777 strprint(retv
, inlen
, "path\n");
4778 } else if (!strcmp(key
, "lxc.seccomp")) {
4779 strprint(retv
, inlen
, "profile\n");
4780 } else if (!strcmp(key
, "lxc.signal")) {
4781 strprint(retv
, inlen
, "halt\n");
4782 strprint(retv
, inlen
, "reboot\n");
4783 strprint(retv
, inlen
, "stop\n");
4784 } else if (!strcmp(key
, "lxc.start")) {
4785 strprint(retv
, inlen
, "auto\n");
4786 strprint(retv
, inlen
, "delay\n");
4787 strprint(retv
, inlen
, "order\n");
4788 } else if (!strcmp(key
, "lxc.monitor")) {
4789 strprint(retv
, inlen
, "unshare\n");
4797 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4800 const char *idxstring
;
4801 struct lxc_netdev
*netdev
;
4805 idxstring
= key
+ 8;
4806 if (!isdigit(*idxstring
))
4809 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
4813 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4820 memset(retv
, 0, inlen
);
4822 strprint(retv
, inlen
, "type\n");
4823 strprint(retv
, inlen
, "script.up\n");
4824 strprint(retv
, inlen
, "script.down\n");
4825 if (netdev
->type
!= LXC_NET_EMPTY
) {
4826 strprint(retv
, inlen
, "flags\n");
4827 strprint(retv
, inlen
, "link\n");
4828 strprint(retv
, inlen
, "name\n");
4829 strprint(retv
, inlen
, "hwaddr\n");
4830 strprint(retv
, inlen
, "mtu\n");
4831 strprint(retv
, inlen
, "ipv6.address\n");
4832 strprint(retv
, inlen
, "ipv6.gateway\n");
4833 strprint(retv
, inlen
, "ipv4.address\n");
4834 strprint(retv
, inlen
, "ipv4.gateway\n");
4837 switch (netdev
->type
) {
4839 strprint(retv
, inlen
, "veth.pair\n");
4841 case LXC_NET_MACVLAN
:
4842 strprint(retv
, inlen
, "macvlan.mode\n");
4845 strprint(retv
, inlen
, "vlan.id\n");