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 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
785 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
786 if (!ret
|| ret
< 0) {
787 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
794 lxc_list_add_tail(&netdev
->ipv6
, list
);
800 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
801 struct lxc_conf
*lxc_conf
, void *data
)
803 struct lxc_netdev
*netdev
= data
;
805 if (lxc_config_value_empty(value
))
806 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
811 free(netdev
->ipv6_gateway
);
813 if (!strcmp(value
, "auto")) {
814 netdev
->ipv6_gateway
= NULL
;
815 netdev
->ipv6_gateway_auto
= true;
820 gw
= malloc(sizeof(*gw
));
824 ret
= inet_pton(AF_INET6
, value
, gw
);
825 if (!ret
|| ret
< 0) {
826 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
831 netdev
->ipv6_gateway
= gw
;
832 netdev
->ipv6_gateway_auto
= false;
838 static int set_config_net_script_up(const char *key
, const char *value
,
839 struct lxc_conf
*lxc_conf
, void *data
)
841 struct lxc_netdev
*netdev
= data
;
843 if (lxc_config_value_empty(value
))
844 return clr_config_net_script_up(key
, lxc_conf
, data
);
849 return set_config_string_item(&netdev
->upscript
, value
);
852 static int set_config_net_script_down(const char *key
, const char *value
,
853 struct lxc_conf
*lxc_conf
, void *data
)
855 struct lxc_netdev
*netdev
= data
;
857 if (lxc_config_value_empty(value
))
858 return clr_config_net_script_down(key
, lxc_conf
, data
);
863 return set_config_string_item(&netdev
->downscript
, value
);
866 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
868 struct lxc_list
*hooklist
;
870 hooklist
= malloc(sizeof(*hooklist
));
876 hooklist
->elem
= hook
;
877 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
881 static int set_config_seccomp_profile(const char *key
, const char *value
,
882 struct lxc_conf
*lxc_conf
, void *data
)
884 return set_config_path_item(&lxc_conf
->seccomp
, value
);
887 static int set_config_execute_cmd(const char *key
, const char *value
,
888 struct lxc_conf
*lxc_conf
, void *data
)
890 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
893 static int set_config_init_cmd(const char *key
, const char *value
,
894 struct lxc_conf
*lxc_conf
, void *data
)
896 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
899 static int set_config_init_cwd(const char *key
, const char *value
,
900 struct lxc_conf
*lxc_conf
, void *data
)
902 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
905 static int set_config_init_uid(const char *key
, const char *value
,
906 struct lxc_conf
*lxc_conf
, void *data
)
908 unsigned int init_uid
;
910 if (lxc_config_value_empty(value
)) {
911 lxc_conf
->init_uid
= 0;
915 if (lxc_safe_uint(value
, &init_uid
) < 0)
918 lxc_conf
->init_uid
= init_uid
;
923 static int set_config_init_gid(const char *key
, const char *value
,
924 struct lxc_conf
*lxc_conf
, void *data
)
926 unsigned int init_gid
;
928 if (lxc_config_value_empty(value
)) {
929 lxc_conf
->init_gid
= 0;
933 if (lxc_safe_uint(value
, &init_gid
) < 0)
936 lxc_conf
->init_gid
= init_gid
;
941 static int set_config_hooks(const char *key
, const char *value
,
942 struct lxc_conf
*lxc_conf
, void *data
)
946 if (lxc_config_value_empty(value
))
947 return lxc_clear_hooks(lxc_conf
, key
);
949 if (strcmp(key
+ 4, "hook") == 0) {
950 ERROR("lxc.hook must not have a value");
954 copy
= strdup(value
);
958 if (strcmp(key
+ 9, "pre-start") == 0)
959 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
960 else if (strcmp(key
+ 9, "start-host") == 0)
961 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
962 else if (strcmp(key
+ 9, "pre-mount") == 0)
963 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
964 else if (strcmp(key
+ 9, "autodev") == 0)
965 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
966 else if (strcmp(key
+ 9, "mount") == 0)
967 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
968 else if (strcmp(key
+ 9, "start") == 0)
969 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
970 else if (strcmp(key
+ 9, "stop") == 0)
971 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
972 else if (strcmp(key
+ 9, "post-stop") == 0)
973 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
974 else if (strcmp(key
+ 9, "clone") == 0)
975 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
976 else if (strcmp(key
+ 9, "destroy") == 0)
977 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
983 static int set_config_personality(const char *key
, const char *value
,
984 struct lxc_conf
*lxc_conf
, void *data
)
986 signed long personality
= lxc_config_parse_arch(value
);
988 if (personality
>= 0)
989 lxc_conf
->personality
= personality
;
991 WARN("Unsupported personality \"%s\"", value
);
996 static int set_config_pty_max(const char *key
, const char *value
,
997 struct lxc_conf
*lxc_conf
, void *data
)
999 if (lxc_config_value_empty(value
)) {
1004 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1010 /* We only need to check whether the first byte of the key after the lxc.start.
1011 * prefix matches our expectations since they fortunately all start with a
1012 * different letter. If anything was wrong with the key we would have already
1013 * noticed when the callback was called.
1015 static int set_config_start(const char *key
, const char *value
,
1016 struct lxc_conf
*lxc_conf
, void *data
)
1020 is_empty
= lxc_config_value_empty(value
);
1022 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1024 lxc_conf
->start_auto
= 0;
1028 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1031 if (lxc_conf
->start_auto
> 1)
1035 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1037 lxc_conf
->start_delay
= 0;
1041 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1042 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1044 lxc_conf
->start_order
= 0;
1048 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1054 static int set_config_monitor(const char *key
, const char *value
,
1055 struct lxc_conf
*lxc_conf
, void *data
)
1057 if (lxc_config_value_empty(value
)) {
1058 lxc_conf
->monitor_unshare
= 0;
1062 if (strcmp(key
+ 12, "unshare") == 0)
1063 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1068 static int set_config_group(const char *key
, const char *value
,
1069 struct lxc_conf
*lxc_conf
, void *data
)
1071 char *groups
, *groupptr
, *sptr
, *token
;
1072 struct lxc_list
*grouplist
;
1075 if (lxc_config_value_empty(value
))
1076 return lxc_clear_groups(lxc_conf
);
1078 groups
= strdup(value
);
1082 /* In case several groups are specified in a single line split these
1083 * groups in a single element for the list.
1085 for (groupptr
= groups
;; groupptr
= NULL
) {
1086 token
= strtok_r(groupptr
, " \t", &sptr
);
1092 grouplist
= malloc(sizeof(*grouplist
));
1096 grouplist
->elem
= strdup(token
);
1097 if (!grouplist
->elem
) {
1102 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1109 static int set_config_environment(const char *key
, const char *value
,
1110 struct lxc_conf
*lxc_conf
, void *data
)
1112 struct lxc_list
*list_item
= NULL
;
1114 if (lxc_config_value_empty(value
))
1115 return lxc_clear_environment(lxc_conf
);
1117 list_item
= malloc(sizeof(*list_item
));
1121 list_item
->elem
= strdup(value
);
1123 if (!list_item
->elem
)
1126 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1135 static int set_config_tty_max(const char *key
, const char *value
,
1136 struct lxc_conf
*lxc_conf
, void *data
)
1138 if (lxc_config_value_empty(value
)) {
1143 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1146 static int set_config_tty_dir(const char *key
, const char *value
,
1147 struct lxc_conf
*lxc_conf
, void *data
)
1149 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1153 static int set_config_apparmor_profile(const char *key
, const char *value
,
1154 struct lxc_conf
*lxc_conf
, void *data
)
1156 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1159 static int set_config_apparmor_allow_incomplete(const char *key
,
1161 struct lxc_conf
*lxc_conf
,
1164 if (lxc_config_value_empty(value
)) {
1165 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1169 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1172 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1178 static int set_config_selinux_context(const char *key
, const char *value
,
1179 struct lxc_conf
*lxc_conf
, void *data
)
1181 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1184 static int set_config_log_file(const char *key
, const char *value
,
1185 struct lxc_conf
*c
, void *data
)
1189 if (lxc_config_value_empty(value
)) {
1195 /* Store these values in the lxc_conf, and then try to set for actual
1198 ret
= set_config_path_item(&c
->logfile
, value
);
1200 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1205 static int set_config_log_level(const char *key
, const char *value
,
1206 struct lxc_conf
*lxc_conf
, void *data
)
1210 if (lxc_config_value_empty(value
)) {
1211 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1215 if (value
[0] >= '0' && value
[0] <= '9') {
1216 if (lxc_safe_int(value
, &newlevel
) < 0)
1219 newlevel
= lxc_log_priority_to_int(value
);
1222 /* Store these values in the lxc_conf, and then try to set for actual
1225 lxc_conf
->loglevel
= newlevel
;
1226 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1229 static int set_config_autodev(const char *key
, const char *value
,
1230 struct lxc_conf
*lxc_conf
, void *data
)
1232 if (lxc_config_value_empty(value
)) {
1233 lxc_conf
->autodev
= 0;
1237 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1240 if (lxc_conf
->autodev
> 1)
1246 static int sig_num(const char *sig
)
1248 unsigned int signum
;
1250 if (lxc_safe_uint(sig
, &signum
) < 0)
1256 static int rt_sig_num(const char *signame
)
1258 int rtmax
= 0, sig_n
= 0;
1260 if (strncasecmp(signame
, "max-", 4) == 0) {
1265 if (!isdigit(*signame
))
1268 sig_n
= sig_num(signame
);
1269 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1270 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1276 static int sig_parse(const char *signame
)
1280 if (isdigit(*signame
)) {
1281 return sig_num(signame
);
1282 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1284 if (strncasecmp(signame
, "rt", 2) == 0)
1285 return rt_sig_num(signame
+ 2);
1286 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1287 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1288 return signames
[n
].num
;
1295 static int set_config_signal_halt(const char *key
, const char *value
,
1296 struct lxc_conf
*lxc_conf
, void *data
)
1300 if (lxc_config_value_empty(value
)) {
1301 lxc_conf
->haltsignal
= 0;
1305 sig_n
= sig_parse(value
);
1309 lxc_conf
->haltsignal
= sig_n
;
1314 static int set_config_signal_reboot(const char *key
, const char *value
,
1315 struct lxc_conf
*lxc_conf
, void *data
)
1319 if (lxc_config_value_empty(value
)) {
1320 lxc_conf
->rebootsignal
= 0;
1324 sig_n
= sig_parse(value
);
1328 lxc_conf
->rebootsignal
= sig_n
;
1333 static int set_config_signal_stop(const char *key
, const char *value
,
1334 struct lxc_conf
*lxc_conf
, void *data
)
1338 if (lxc_config_value_empty(value
)) {
1339 lxc_conf
->stopsignal
= 0;
1343 sig_n
= sig_parse(value
);
1347 lxc_conf
->stopsignal
= sig_n
;
1352 static int set_config_cgroup_controller(const char *key
, const char *value
,
1353 struct lxc_conf
*lxc_conf
, void *data
)
1356 char *token
= "lxc.cgroup.";
1357 struct lxc_list
*cglist
= NULL
;
1358 struct lxc_cgroup
*cgelem
= NULL
;
1360 if (lxc_config_value_empty(value
))
1361 return lxc_clear_cgroups(lxc_conf
, key
);
1363 subkey
= strstr(key
, token
);
1367 if (!strlen(subkey
))
1370 if (strlen(subkey
) == strlen(token
))
1373 subkey
+= strlen(token
);
1375 cglist
= malloc(sizeof(*cglist
));
1379 cgelem
= malloc(sizeof(*cgelem
));
1382 memset(cgelem
, 0, sizeof(*cgelem
));
1384 cgelem
->subsystem
= strdup(subkey
);
1385 cgelem
->value
= strdup(value
);
1387 if (!cgelem
->subsystem
|| !cgelem
->value
)
1390 cglist
->elem
= cgelem
;
1392 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1399 free(cgelem
->subsystem
);
1400 free(cgelem
->value
);
1407 static int set_config_cgroup_dir(const char *key
, const char *value
,
1408 struct lxc_conf
*lxc_conf
, void *data
)
1410 if (lxc_config_value_empty(value
))
1411 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1413 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1416 static int set_config_prlimit(const char *key
, const char *value
,
1417 struct lxc_conf
*lxc_conf
, void *data
)
1419 struct lxc_list
*iter
;
1420 struct rlimit limit
;
1422 struct lxc_list
*limlist
= NULL
;
1423 struct lxc_limit
*limelem
= NULL
;
1425 if (lxc_config_value_empty(value
))
1426 return lxc_clear_limits(lxc_conf
, key
);
1428 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1431 key
+= sizeof("lxc.prlimit.") - 1;
1433 /* soft limit comes first in the value */
1434 if (!parse_limit_value(&value
, &limit_value
))
1436 limit
.rlim_cur
= limit_value
;
1438 /* skip spaces and a colon */
1439 while (isspace(*value
))
1444 else if (*value
) /* any other character is an error here */
1447 while (isspace(*value
))
1450 /* optional hard limit */
1452 if (!parse_limit_value(&value
, &limit_value
))
1454 limit
.rlim_max
= limit_value
;
1456 /* check for trailing garbage */
1457 while (isspace(*value
))
1463 /* a single value sets both hard and soft limit */
1464 limit
.rlim_max
= limit
.rlim_cur
;
1467 /* find existing list element */
1468 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1469 limelem
= iter
->elem
;
1470 if (!strcmp(key
, limelem
->resource
)) {
1471 limelem
->limit
= limit
;
1476 /* allocate list element */
1477 limlist
= malloc(sizeof(*limlist
));
1481 limelem
= malloc(sizeof(*limelem
));
1484 memset(limelem
, 0, sizeof(*limelem
));
1486 limelem
->resource
= strdup(key
);
1487 if (!limelem
->resource
)
1489 limelem
->limit
= limit
;
1491 lxc_list_add_elem(limlist
, limelem
);;
1493 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1500 free(limelem
->resource
);
1506 static int set_config_sysctl(const char *key
, const char *value
,
1507 struct lxc_conf
*lxc_conf
, void *data
)
1509 struct lxc_list
*iter
;
1510 char *replace_value
= NULL
;
1511 struct lxc_list
*sysctl_list
= NULL
;
1512 struct lxc_sysctl
*sysctl_elem
= NULL
;
1514 if (lxc_config_value_empty(value
))
1515 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1517 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1520 key
+= sizeof("lxc.sysctl.") - 1;
1522 /* find existing list element */
1523 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1524 sysctl_elem
= iter
->elem
;
1526 if (strcmp(key
, sysctl_elem
->key
) != 0)
1529 replace_value
= strdup(value
);
1533 free(sysctl_elem
->value
);
1534 sysctl_elem
->value
= replace_value
;
1538 /* allocate list element */
1539 sysctl_list
= malloc(sizeof(*sysctl_list
));
1543 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1546 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1548 sysctl_elem
->key
= strdup(key
);
1549 if (!sysctl_elem
->key
)
1552 sysctl_elem
->value
= strdup(value
);
1553 if (!sysctl_elem
->value
)
1556 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1558 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1565 free(sysctl_elem
->key
);
1566 free(sysctl_elem
->value
);
1572 static int set_config_proc(const char *key
, const char *value
,
1573 struct lxc_conf
*lxc_conf
, void *data
)
1576 struct lxc_list
*proclist
= NULL
;
1577 struct lxc_proc
*procelem
= NULL
;
1579 if (lxc_config_value_empty(value
))
1580 return clr_config_proc(key
, lxc_conf
, NULL
);
1582 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1585 subkey
= key
+ sizeof("lxc.proc.") - 1;
1586 if (*subkey
== '\0')
1589 proclist
= malloc(sizeof(*proclist
));
1593 procelem
= malloc(sizeof(*procelem
));
1596 memset(procelem
, 0, sizeof(*procelem
));
1598 procelem
->filename
= strdup(subkey
);
1599 procelem
->value
= strdup(value
);
1601 if (!procelem
->filename
|| !procelem
->value
)
1604 proclist
->elem
= procelem
;
1606 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1613 free(procelem
->filename
);
1614 free(procelem
->value
);
1621 static int set_config_idmaps(const char *key
, const char *value
,
1622 struct lxc_conf
*lxc_conf
, void *data
)
1624 unsigned long hostid
, nsid
, range
;
1627 struct lxc_list
*idmaplist
= NULL
;
1628 struct id_map
*idmap
= NULL
;
1630 if (lxc_config_value_empty(value
))
1631 return lxc_clear_idmaps(lxc_conf
);
1633 idmaplist
= malloc(sizeof(*idmaplist
));
1637 idmap
= malloc(sizeof(*idmap
));
1640 memset(idmap
, 0, sizeof(*idmap
));
1642 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1646 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1648 idmap
->idtype
= ID_TYPE_UID
;
1649 else if (type
== 'g')
1650 idmap
->idtype
= ID_TYPE_GID
;
1654 idmap
->hostid
= hostid
;
1656 idmap
->range
= range
;
1657 idmaplist
->elem
= idmap
;
1658 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1670 static int set_config_mount_fstab(const char *key
, const char *value
,
1671 struct lxc_conf
*lxc_conf
, void *data
)
1673 if (lxc_config_value_empty(value
)) {
1674 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1678 return set_config_path_item(&lxc_conf
->fstab
, value
);
1681 static int set_config_mount_auto(const char *key
, const char *value
,
1682 struct lxc_conf
*lxc_conf
, void *data
)
1684 char *autos
, *autoptr
, *sptr
, *token
;
1691 } allowed_auto_mounts
[] = {
1692 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1693 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1694 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1695 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1696 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1697 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1698 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1699 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1700 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1701 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1702 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1703 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1704 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1705 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1706 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1707 /* For adding anything that is just a single on/off, but has no
1708 * options: keep mask and flag identical and just define the enum
1709 * value as an unused bit so far
1714 if (lxc_config_value_empty(value
)) {
1715 lxc_conf
->auto_mounts
= 0;
1719 autos
= strdup(value
);
1723 for (autoptr
= autos
;; autoptr
= NULL
) {
1724 token
= strtok_r(autoptr
, " \t", &sptr
);
1730 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1731 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1735 if (!allowed_auto_mounts
[i
].token
) {
1736 ERROR("Invalid filesystem to automount \"%s\"", token
);
1740 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1741 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1748 static int set_config_mount(const char *key
, const char *value
,
1749 struct lxc_conf
*lxc_conf
, void *data
)
1752 struct lxc_list
*mntlist
;
1754 if (lxc_config_value_empty(value
))
1755 return lxc_clear_mount_entries(lxc_conf
);
1757 mntlist
= malloc(sizeof(*mntlist
));
1761 mntelem
= strdup(value
);
1766 mntlist
->elem
= mntelem
;
1768 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1773 static int set_config_cap_keep(const char *key
, const char *value
,
1774 struct lxc_conf
*lxc_conf
, void *data
)
1776 char *keepcaps
, *keepptr
, *sptr
, *token
;
1777 struct lxc_list
*keeplist
;
1780 if (lxc_config_value_empty(value
))
1781 return lxc_clear_config_keepcaps(lxc_conf
);
1783 keepcaps
= strdup(value
);
1787 /* In case several capability keep is specified in a single line
1788 * split these caps in a single element for the list.
1790 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1791 token
= strtok_r(keepptr
, " \t", &sptr
);
1797 if (!strcmp(token
, "none"))
1798 lxc_clear_config_keepcaps(lxc_conf
);
1800 keeplist
= malloc(sizeof(*keeplist
));
1804 keeplist
->elem
= strdup(token
);
1805 if (!keeplist
->elem
) {
1810 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1818 static int set_config_cap_drop(const char *key
, const char *value
,
1819 struct lxc_conf
*lxc_conf
, void *data
)
1821 char *dropcaps
, *dropptr
, *sptr
, *token
;
1822 struct lxc_list
*droplist
;
1825 if (lxc_config_value_empty(value
))
1826 return lxc_clear_config_caps(lxc_conf
);
1828 dropcaps
= strdup(value
);
1832 /* In case several capability drop is specified in a single line
1833 * split these caps in a single element for the list.
1835 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1836 token
= strtok_r(dropptr
, " \t", &sptr
);
1842 droplist
= malloc(sizeof(*droplist
));
1846 droplist
->elem
= strdup(token
);
1847 if (!droplist
->elem
) {
1852 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1860 static int set_config_console_path(const char *key
, const char *value
,
1861 struct lxc_conf
*lxc_conf
, void *data
)
1863 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1866 static int set_config_console_rotate(const char *key
, const char *value
,
1867 struct lxc_conf
*lxc_conf
, void *data
)
1869 if (lxc_config_value_empty(value
)) {
1870 lxc_conf
->console
.log_rotate
= 0;
1874 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1877 if (lxc_conf
->console
.log_rotate
> 1) {
1878 ERROR("The \"lxc.console.rotate\" config key can only be set "
1886 static int set_config_console_logfile(const char *key
, const char *value
,
1887 struct lxc_conf
*lxc_conf
, void *data
)
1889 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1892 static int set_config_console_buffer_size(const char *key
, const char *value
,
1893 struct lxc_conf
*lxc_conf
, void *data
)
1897 uint64_t buffer_size
, pgsz
;
1899 if (lxc_config_value_empty(value
)) {
1900 lxc_conf
->console
.buffer_size
= 0;
1904 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1905 if (!strcmp(value
, "auto")) {
1906 lxc_conf
->console
.buffer_size
= 1 << 17;
1910 ret
= parse_byte_size_string(value
, &size
);
1917 /* must be at least a page size */
1918 pgsz
= lxc_getpagesize();
1919 if ((uint64_t)size
< pgsz
) {
1920 NOTICE("Requested ringbuffer size for the console is %" PRId64
1921 " but must be at least %" PRId64
1922 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1927 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1928 if (buffer_size
== 0)
1931 if (buffer_size
!= size
)
1932 NOTICE("Passed size was not a power of 2. Rounding log size to "
1933 "next power of two: %" PRIu64
" bytes", buffer_size
);
1935 lxc_conf
->console
.buffer_size
= buffer_size
;
1939 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
1940 struct lxc_conf
*lxc_conf
,
1943 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
1946 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1948 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1950 update_hwaddr(line
);
1952 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1953 char *tmp
= realloc(conf
->unexpanded_config
,
1954 conf
->unexpanded_alloced
+ 1024);
1958 if (!conf
->unexpanded_config
)
1960 conf
->unexpanded_config
= tmp
;
1961 conf
->unexpanded_alloced
+= 1024;
1963 strcat(conf
->unexpanded_config
, line
);
1964 conf
->unexpanded_len
+= linelen
;
1965 if (line
[linelen
- 1] != '\n') {
1966 strcat(conf
->unexpanded_config
, "\n");
1967 conf
->unexpanded_len
++;
1973 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
1975 struct dirent
*direntp
;
1977 char path
[MAXPATHLEN
];
1981 dir
= opendir(dirp
);
1985 while ((direntp
= readdir(dir
))) {
1990 fnam
= direntp
->d_name
;
1991 if (!strcmp(fnam
, "."))
1994 if (!strcmp(fnam
, ".."))
1998 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2001 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2002 if (len
< 0 || len
>= MAXPATHLEN
) {
2007 ret
= lxc_config_read(path
, lxc_conf
, true);
2019 static int set_config_includefiles(const char *key
, const char *value
,
2020 struct lxc_conf
*lxc_conf
, void *data
)
2022 if (lxc_config_value_empty(value
)) {
2023 clr_config_includefiles(key
, lxc_conf
, NULL
);
2028 return do_includedir(value
, lxc_conf
);
2030 return lxc_config_read(value
, lxc_conf
, true);
2033 static int set_config_rootfs_path(const char *key
, const char *value
,
2034 struct lxc_conf
*lxc_conf
, void *data
)
2038 const char *container_path
;
2040 if (lxc_config_value_empty(value
)) {
2041 free(lxc_conf
->rootfs
.path
);
2042 lxc_conf
->rootfs
.path
= NULL
;
2046 dup
= strdup(value
);
2050 /* Split <storage type>:<container path> into <storage type> and
2051 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2052 * "rootfs.path" to <container path>.
2054 tmp
= strchr(dup
, ':');
2057 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2063 container_path
= tmp
;
2065 container_path
= value
;
2068 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2073 static int set_config_rootfs_mount(const char *key
, const char *value
,
2074 struct lxc_conf
*lxc_conf
, void *data
)
2076 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2079 static int set_config_rootfs_options(const char *key
, const char *value
,
2080 struct lxc_conf
*lxc_conf
, void *data
)
2082 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2085 static int set_config_rootfs_backend(const char *key
, const char *value
,
2086 struct lxc_conf
*lxc_conf
, void *data
)
2091 static int set_config_uts_name(const char *key
, const char *value
,
2092 struct lxc_conf
*lxc_conf
, void *data
)
2094 struct utsname
*utsname
;
2096 if (lxc_config_value_empty(value
)) {
2097 clr_config_uts_name(key
, lxc_conf
, NULL
);
2101 utsname
= malloc(sizeof(*utsname
));
2105 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2110 strcpy(utsname
->nodename
, value
);
2111 free(lxc_conf
->utsname
);
2112 lxc_conf
->utsname
= utsname
;
2117 static int set_config_namespace(const char *key
, const char *value
,
2118 struct lxc_conf
*lxc_conf
, void *data
)
2121 const char *namespace;
2123 if (lxc_config_value_empty(value
))
2124 return clr_config_namespace(key
, lxc_conf
, data
);
2126 namespace = key
+ sizeof("lxc.namespace.") - 1;
2127 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2131 return set_config_string_item(&lxc_conf
->inherit_ns
[ns_idx
], value
);
2134 struct parse_line_conf
{
2135 struct lxc_conf
*conf
;
2139 static int parse_line(char *buffer
, void *data
)
2141 char *dot
, *key
, *line
, *linep
, *value
;
2143 struct lxc_config_t
*config
;
2146 struct parse_line_conf
*plc
= data
;
2148 /* If there are newlines in the config file we should keep them. */
2149 empty_line
= lxc_is_line_empty(dup
);
2153 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2154 * modified the original string on the stack by replacing '=' by '\0'
2157 linep
= line
= strdup(dup
);
2161 if (!plc
->from_include
) {
2162 ret
= append_unexp_config_line(line
, plc
->conf
);
2170 line
+= lxc_char_left_gc(line
, strlen(line
));
2172 /* ignore comments */
2176 /* martian option - don't add it to the config itself */
2177 if (strncmp(line
, "lxc.", 4))
2182 dot
= strchr(line
, '=');
2184 ERROR("Invalid configuration line: %s", line
);
2192 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2194 value
+= lxc_char_left_gc(value
, strlen(value
));
2195 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2197 if (*value
== '\'' || *value
== '\"') {
2200 len
= strlen(value
);
2201 if (len
> 1 && value
[len
- 1] == *value
) {
2202 value
[len
- 1] = '\0';
2207 config
= lxc_get_config(key
);
2209 ERROR("Unknown configuration key \"%s\"", key
);
2213 /* [START]: REMOVE IN LXC 3.0 */
2214 if (config
->is_legacy_key
&& !plc
->conf
->contains_legacy_key
) {
2215 plc
->conf
->contains_legacy_key
= true;
2216 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2217 /* Warn the user once loud and clear that there is at
2218 * least one legacy configuration item in the
2219 * configuration file and then an update is required.
2221 fprintf(stderr
, "The configuration file contains "
2222 "legacy configuration keys.\nPlease "
2223 "update your configuration file!\nThe "
2224 "update script lxc-update-config -c "
2225 "<path-to-config> can be used for "
2229 /* [END]: REMOVE IN LXC 3.0 */
2231 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2238 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2240 struct parse_line_conf c
;
2243 c
.from_include
= false;
2245 return parse_line(buffer
, &c
);
2248 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2251 struct parse_line_conf c
;
2254 c
.from_include
= from_include
;
2256 ret
= access(file
, R_OK
);
2260 /* Catch only the top level config file name in the structure. */
2262 conf
->rcfile
= strdup(file
);
2264 return lxc_file_for_each_line(file
, parse_line
, &c
);
2267 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2269 struct lxc_list
*dent
;
2271 dent
= malloc(sizeof(struct lxc_list
));
2276 lxc_list_add_tail(defines
, dent
);
2280 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2282 struct lxc_list
*it
, *next
;
2285 lxc_list_for_each(it
, defines
) {
2286 ret
= lxc_config_readline(it
->elem
, conf
);
2291 lxc_list_for_each_safe(it
, defines
, next
) {
2299 signed long lxc_config_parse_arch(const char *arch
)
2301 #if HAVE_SYS_PERSONALITY_H
2307 { "x86", PER_LINUX32
},
2308 { "linux32", PER_LINUX32
},
2309 { "i386", PER_LINUX32
},
2310 { "i486", PER_LINUX32
},
2311 { "i586", PER_LINUX32
},
2312 { "i686", PER_LINUX32
},
2313 { "athlon", PER_LINUX32
},
2314 { "mips", PER_LINUX32
},
2315 { "mipsel", PER_LINUX32
},
2316 { "ppc", PER_LINUX32
},
2317 { "arm", PER_LINUX32
},
2318 { "armv7l", PER_LINUX32
},
2319 { "armhf", PER_LINUX32
},
2320 { "armel", PER_LINUX32
},
2321 { "powerpc", PER_LINUX32
},
2322 { "linux64", PER_LINUX
},
2323 { "x86_64", PER_LINUX
},
2324 { "amd64", PER_LINUX
},
2325 { "mips64", PER_LINUX
},
2326 { "mips64el", PER_LINUX
},
2327 { "ppc64", PER_LINUX
},
2328 { "ppc64le", PER_LINUX
},
2329 { "ppc64el", PER_LINUX
},
2330 { "powerpc64", PER_LINUX
},
2331 { "s390x", PER_LINUX
},
2332 { "aarch64", PER_LINUX
},
2333 { "arm64", PER_LINUX
},
2335 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2337 for (i
= 0; i
< len
; i
++) {
2338 if (!strcmp(pername
[i
].name
, arch
))
2339 return pername
[i
].per
;
2346 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2348 char *token
, *saveptr
= NULL
;
2354 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2355 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2356 { "LSM", LXC_ATTACH_LSM_EXEC
},
2361 /* For the sake of backward compatibility, drop all privileges
2362 * if none is specified.
2364 for (i
= 0; all_privs
[i
].token
; i
++)
2365 *flags
|= all_privs
[i
].flag
;
2370 token
= strtok_r(flaglist
, "|", &saveptr
);
2373 for (i
= 0; all_privs
[i
].token
; i
++)
2374 if (!strcmp(all_privs
[i
].token
, token
))
2375 aflag
= all_privs
[i
].flag
;
2381 token
= strtok_r(NULL
, "|", &saveptr
);
2387 /* Write out a configuration file. */
2388 void write_config(FILE *fout
, struct lxc_conf
*c
)
2391 size_t len
= c
->unexpanded_len
;
2396 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2398 SYSERROR("Failed to write configuration file");
2401 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2408 len
= strlen(key
) + strlen(v
) + 4;
2411 if (lxc_config_value_empty(v
))
2412 ret
= snprintf(tmp
, len
, "%s =", key
);
2414 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2415 if (ret
< 0 || ret
>= len
)
2418 /* Save the line verbatim into unexpanded_conf */
2419 if (append_unexp_config_line(tmp
, conf
))
2425 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2429 char *lstart
= conf
->unexpanded_config
;
2431 if (!conf
->unexpanded_config
)
2435 lend
= strchr(lstart
, '\n');
2438 lend
= lstart
+ strlen(lstart
);
2441 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2446 v
= lstart
[strlen(key
)];
2447 if (!isspace(v
) && v
!= '=') {
2452 conf
->unexpanded_len
-= (lend
- lstart
);
2453 if (*lend
== '\0') {
2457 memmove(lstart
, lend
, strlen(lend
) + 1);
2461 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2462 const char *newpath
, const char *oldname
,
2463 const char *newname
, const char *ovldir
)
2466 char *lend
, *newdir
, *olddir
, *p
, *q
;
2467 size_t newdirlen
, olddirlen
;
2468 char *lstart
= conf
->unexpanded_config
;
2469 const char *key
= "lxc.mount.entry";
2471 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2472 olddir
= alloca(olddirlen
+ 1);
2473 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2475 if (ret
< 0 || ret
>= olddirlen
+ 1)
2478 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2479 newdir
= alloca(newdirlen
+ 1);
2480 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2482 if (ret
< 0 || ret
>= newdirlen
+ 1)
2485 if (!conf
->unexpanded_config
)
2489 lend
= strchr(lstart
, '\n');
2491 lend
= lstart
+ strlen(lstart
);
2495 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2498 p
= strchr(lstart
+ strlen(key
), '=');
2509 /* Whenever an lxc.mount.entry entry is found in a line we check
2510 * if the substring " overlay" or the substring " aufs" is
2511 * present before doing any further work. We check for "
2512 * overlay" and " aufs" since both substrings need to have at
2513 * least one space before them in a valid overlay
2514 * lxc.mount.entry (/A B overlay). When the space before is
2515 * missing it is very likely that these substrings are part of a
2516 * path or something else. (Checking q >= lend ensures that we
2517 * only count matches in the current line.) */
2518 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2519 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2522 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2525 /* replace the olddir with newdir */
2526 if (olddirlen
>= newdirlen
) {
2527 size_t diff
= olddirlen
- newdirlen
;
2528 memcpy(q
, newdir
, newdirlen
);
2529 if (olddirlen
!= newdirlen
) {
2530 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2531 strlen(q
) - newdirlen
- diff
+ 1);
2533 conf
->unexpanded_len
-= diff
;
2537 size_t diff
= newdirlen
- olddirlen
;
2538 size_t oldlen
= conf
->unexpanded_len
;
2539 size_t newlen
= oldlen
+ diff
;
2540 size_t poffset
= q
- conf
->unexpanded_config
;
2542 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2546 conf
->unexpanded_len
= newlen
;
2547 conf
->unexpanded_alloced
= newlen
+ 1;
2548 new[newlen
- 1] = '\0';
2549 lend
= new + (lend
- conf
->unexpanded_config
);
2550 /* Move over the remainder to make room for the newdir.
2552 memmove(new + poffset
+ newdirlen
,
2553 new + poffset
+ olddirlen
,
2554 oldlen
- poffset
- olddirlen
+ 1);
2555 conf
->unexpanded_config
= new;
2556 memcpy(new + poffset
, newdir
, newdirlen
);
2566 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2567 const char *newpath
, const char *oldname
,
2568 const char *newname
)
2571 char *lend
, *newdir
, *olddir
, *p
;
2572 char *lstart
= conf
->unexpanded_config
;
2573 size_t newdirlen
, olddirlen
;
2574 const char *key
= "lxc.hook";
2576 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2577 olddir
= alloca(olddirlen
+ 1);
2578 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2579 if (ret
< 0 || ret
>= olddirlen
+ 1)
2582 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2583 newdir
= alloca(newdirlen
+ 1);
2584 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2585 if (ret
< 0 || ret
>= newdirlen
+ 1)
2588 if (!conf
->unexpanded_config
)
2592 lend
= strchr(lstart
, '\n');
2594 lend
= lstart
+ strlen(lstart
);
2598 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2601 p
= strchr(lstart
+ strlen(key
), '=');
2612 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2615 /* replace the olddir with newdir */
2616 if (olddirlen
>= newdirlen
) {
2617 size_t diff
= olddirlen
- newdirlen
;
2618 memcpy(p
, newdir
, newdirlen
);
2619 if (olddirlen
!= newdirlen
) {
2620 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2621 strlen(p
) - newdirlen
- diff
+ 1);
2623 conf
->unexpanded_len
-= diff
;
2627 size_t diff
= newdirlen
- olddirlen
;
2628 size_t oldlen
= conf
->unexpanded_len
;
2629 size_t newlen
= oldlen
+ diff
;
2630 size_t poffset
= p
- conf
->unexpanded_config
;
2632 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2636 conf
->unexpanded_len
= newlen
;
2637 conf
->unexpanded_alloced
= newlen
+ 1;
2638 new[newlen
- 1] = '\0';
2639 lend
= new + (lend
- conf
->unexpanded_config
);
2640 /* Move over the remainder to make room for the newdir.
2642 memmove(new + poffset
+ newdirlen
,
2643 new + poffset
+ olddirlen
,
2644 oldlen
- poffset
- olddirlen
+ 1);
2645 conf
->unexpanded_config
= new;
2646 memcpy(new + poffset
, newdir
, newdirlen
);
2659 ERROR("Error writing to new config"); \
2664 /* This is called only from clone. We wish to update all hwaddrs in the
2665 * unexpanded config file. We can't/don't want to update any which come from
2666 * lxc.includes (there shouldn't be any).
2667 * We can't just walk the c->lxc-conf->network list because that includes netifs
2668 * from the include files. So we update the ones which we find in the unexp
2669 * config file, then find the original macaddr in the conf->network, and update
2670 * that to the same value.
2672 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2674 char *lend
, *p
, *p2
;
2675 struct lxc_list
*it
;
2676 char *lstart
= conf
->unexpanded_config
;
2678 if (!conf
->unexpanded_config
)
2682 char newhwaddr
[18], oldhwaddr
[17];
2684 lend
= strchr(lstart
, '\n');
2686 lend
= lstart
+ strlen(lstart
);
2690 if (!lxc_config_net_hwaddr(lstart
)) {
2695 p
= strchr(lstart
, '=');
2708 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2711 if ((p2
- p
) != 17) {
2712 WARN("Bad hwaddr entry");
2717 memcpy(oldhwaddr
, p
, 17);
2719 if (!new_hwaddr(newhwaddr
))
2722 memcpy(p
, newhwaddr
, 17);
2723 lxc_list_for_each(it
, &conf
->network
) {
2724 struct lxc_netdev
*n
= it
->elem
;
2726 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2727 memcpy(n
->hwaddr
, newhwaddr
, 17);
2736 static int set_config_ephemeral(const char *key
, const char *value
,
2737 struct lxc_conf
*lxc_conf
, void *data
)
2739 if (lxc_config_value_empty(value
)) {
2740 lxc_conf
->ephemeral
= 0;
2744 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2747 if (lxc_conf
->ephemeral
> 1)
2753 static int set_config_log_syslog(const char *key
, const char *value
,
2754 struct lxc_conf
*lxc_conf
, void *data
)
2758 if (lxc_conf
->syslog
) {
2759 free(lxc_conf
->syslog
);
2760 lxc_conf
->syslog
= NULL
;
2763 if (lxc_config_value_empty(value
))
2766 facility
= lxc_syslog_priority_to_int(value
);
2767 if (facility
== -EINVAL
)
2770 lxc_log_syslog(facility
);
2771 return set_config_string_item(&lxc_conf
->syslog
, value
);
2774 static int set_config_no_new_privs(const char *key
, const char *value
,
2775 struct lxc_conf
*lxc_conf
, void *data
)
2779 if (lxc_config_value_empty(value
)) {
2780 lxc_conf
->no_new_privs
= false;
2784 if (lxc_safe_uint(value
, &v
) < 0)
2790 lxc_conf
->no_new_privs
= v
? true : false;
2795 static int set_config_noop(const char *key
, const char *value
,
2796 struct lxc_conf
*lxc_conf
, void *data
)
2801 /* Callbacks to get configuration items. */
2802 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2803 struct lxc_conf
*c
, void *data
)
2810 memset(retv
, 0, inlen
);
2812 #if HAVE_SYS_PERSONALITY_H
2815 switch (c
->personality
) {
2817 strprint(retv
, inlen
, "i686");
2820 strprint(retv
, inlen
, "x86_64");
2830 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2831 struct lxc_conf
*c
, void *data
)
2833 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2836 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2837 struct lxc_conf
*c
, void *data
)
2839 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2842 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2843 struct lxc_conf
*c
, void *data
)
2845 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2848 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2849 struct lxc_conf
*c
, void *data
)
2851 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2854 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2855 int inlen
, struct lxc_conf
*c
,
2858 return lxc_get_conf_int(c
, retv
, inlen
,
2859 c
->lsm_aa_allow_incomplete
);
2862 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2863 struct lxc_conf
*c
, void *data
)
2865 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2868 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2869 * just the value(s) will be printed. Since there still could be more than one,
2870 * it is newline-separated.
2871 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2873 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2874 * 'lxc.cgroup.subsystem.key = value' format.
2876 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
2877 struct lxc_conf
*c
, void *data
)
2879 struct lxc_list
*it
;
2882 bool get_all
= false;
2887 memset(retv
, 0, inlen
);
2889 if (!strcmp(key
, "lxc.cgroup"))
2891 else if (!strncmp(key
, "lxc.cgroup.", 11))
2896 lxc_list_for_each(it
, &c
->cgroup
) {
2897 struct lxc_cgroup
*cg
= it
->elem
;
2900 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n",
2901 cg
->subsystem
, cg
->value
);
2902 } else if (!strcmp(cg
->subsystem
, key
)) {
2903 strprint(retv
, inlen
, "%s\n", cg
->value
);
2910 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
2911 struct lxc_conf
*lxc_conf
, void *data
)
2919 memset(retv
, 0, inlen
);
2921 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
2926 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2927 struct lxc_conf
*c
, void *data
)
2929 struct lxc_list
*it
;
2930 int len
, listlen
, ret
;
2932 /* "u 1000 1000000 65536"
2934 * let's render this as
2952 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2953 char buf
[__LXC_IDMAP_STR_BUF
];
2958 memset(retv
, 0, inlen
);
2960 listlen
= lxc_list_len(&c
->id_map
);
2961 lxc_list_for_each(it
, &c
->id_map
)
2963 struct id_map
*map
= it
->elem
;
2964 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
2965 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
2966 map
->nsid
, map
->hostid
, map
->range
);
2967 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
2970 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
2975 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
2976 struct lxc_conf
*c
, void *data
)
2979 v
= lxc_log_priority_to_string(c
->loglevel
);
2980 return lxc_get_conf_str(retv
, inlen
, v
);
2983 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
2984 struct lxc_conf
*c
, void *data
)
2986 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
2989 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
2990 struct lxc_conf
*c
, void *data
)
2992 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
2995 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
2996 struct lxc_conf
*c
, void *data
)
2998 int len
, fulllen
= 0;
2999 const char *sep
= "";
3004 memset(retv
, 0, inlen
);
3006 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3009 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3010 case LXC_AUTO_PROC_MIXED
:
3011 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3014 case LXC_AUTO_PROC_RW
:
3015 strprint(retv
, inlen
, "%sproc:rw", sep
);
3022 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3023 case LXC_AUTO_SYS_RO
:
3024 strprint(retv
, inlen
, "%ssys:ro", sep
);
3027 case LXC_AUTO_SYS_RW
:
3028 strprint(retv
, inlen
, "%ssys:rw", sep
);
3031 case LXC_AUTO_SYS_MIXED
:
3032 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3039 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3040 case LXC_AUTO_CGROUP_NOSPEC
:
3041 strprint(retv
, inlen
, "%scgroup", sep
);
3043 case LXC_AUTO_CGROUP_MIXED
:
3044 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3046 case LXC_AUTO_CGROUP_RO
:
3047 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3049 case LXC_AUTO_CGROUP_RW
:
3050 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3052 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3053 strprint(retv
, inlen
, "%scgroup-full", sep
);
3055 case LXC_AUTO_CGROUP_FULL_MIXED
:
3056 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3058 case LXC_AUTO_CGROUP_FULL_RO
:
3059 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3061 case LXC_AUTO_CGROUP_FULL_RW
:
3062 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3071 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3072 struct lxc_conf
*c
, void *data
)
3074 int len
, fulllen
= 0;
3075 struct lxc_list
*it
;
3080 memset(retv
, 0, inlen
);
3082 lxc_list_for_each(it
, &c
->mount_list
) {
3083 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3089 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3090 struct lxc_conf
*c
, void *data
)
3092 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3095 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3096 struct lxc_conf
*c
, void *data
)
3098 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3101 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3102 struct lxc_conf
*c
, void *data
)
3104 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3107 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3108 struct lxc_conf
*c
, void *data
)
3113 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3114 struct lxc_conf
*c
, void *data
)
3116 return lxc_get_conf_str(
3118 c
->utsname
? c
->utsname
->nodename
: NULL
);
3121 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3122 struct lxc_conf
*c
, void *data
)
3125 int len
, fulllen
= 0, found
= -1;
3126 struct lxc_list
*it
;
3129 subkey
= strchr(key
, '.');
3131 subkey
= strchr(subkey
+ 1, '.');
3137 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3138 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3149 memset(retv
, 0, inlen
);
3151 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3152 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3157 static int get_config_net(const char *key
, char *retv
, int inlen
,
3158 struct lxc_conf
*c
, void *data
)
3160 int len
, fulllen
= 0;
3161 struct lxc_list
*it
;
3166 memset(retv
, 0, inlen
);
3168 lxc_list_for_each(it
, &c
->network
) {
3169 struct lxc_netdev
*n
= it
->elem
;
3170 const char *t
= lxc_net_type_to_str(n
->type
);
3171 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3177 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3178 struct lxc_conf
*c
, void *data
)
3180 int len
, fulllen
= 0;
3181 struct lxc_list
*it
;
3186 memset(retv
, 0, inlen
);
3188 lxc_list_for_each(it
, &c
->caps
) {
3189 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3195 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3196 struct lxc_conf
*c
, void *data
)
3198 int len
, fulllen
= 0;
3199 struct lxc_list
*it
;
3204 memset(retv
, 0, inlen
);
3206 lxc_list_for_each(it
, &c
->keepcaps
) {
3207 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3213 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3214 struct lxc_conf
*c
, void *data
)
3216 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3219 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3220 struct lxc_conf
*c
, void *data
)
3222 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3225 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3226 struct lxc_conf
*c
, void *data
)
3228 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3232 static int get_config_console_buffer_size(const char *key
, char *retv
,
3233 int inlen
, struct lxc_conf
*c
,
3236 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->autodev
);
3239 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3240 int inlen
, struct lxc_conf
*c
,
3243 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3246 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3247 struct lxc_conf
*c
, void *data
)
3249 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3252 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3253 struct lxc_conf
*c
, void *data
)
3255 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3258 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3259 struct lxc_conf
*c
, void *data
)
3261 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3264 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3265 struct lxc_conf
*c
, void *data
)
3267 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3270 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3271 struct lxc_conf
*c
, void *data
)
3273 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3276 static int get_config_start(const char *key
, char *retv
, int inlen
,
3277 struct lxc_conf
*c
, void *data
)
3279 if (strcmp(key
+ 10, "auto") == 0)
3280 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3281 else if (strcmp(key
+ 10, "delay") == 0)
3282 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3283 else if (strcmp(key
+ 10, "order") == 0)
3284 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3289 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3290 struct lxc_conf
*c
, void *data
)
3292 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3295 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3296 struct lxc_conf
*c
, void *data
)
3298 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3301 static int get_config_group(const char *key
, char *retv
, int inlen
,
3302 struct lxc_conf
*c
, void *data
)
3304 int len
, fulllen
= 0;
3305 struct lxc_list
*it
;
3310 memset(retv
, 0, inlen
);
3312 lxc_list_for_each(it
, &c
->groups
) {
3313 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3319 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3320 struct lxc_conf
*c
, void *data
)
3322 int len
, fulllen
= 0;
3323 struct lxc_list
*it
;
3328 memset(retv
, 0, inlen
);
3330 lxc_list_for_each(it
, &c
->environment
) {
3331 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3337 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3338 struct lxc_conf
*c
, void *data
)
3340 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3343 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3344 struct lxc_conf
*c
, void *data
)
3346 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3349 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3350 struct lxc_conf
*c
, void *data
)
3352 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3355 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3356 struct lxc_conf
*c
, void *data
)
3358 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3361 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3362 struct lxc_conf
*c
, void *data
)
3364 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3367 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3368 struct lxc_conf
*c
, void *data
)
3370 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3373 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3374 struct lxc_conf
*c
, void *data
)
3376 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3379 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3380 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3381 * printed, in 'lxc.prlimit.resource = value' format.
3383 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3384 struct lxc_conf
*c
, void *data
)
3386 int fulllen
= 0, len
;
3387 bool get_all
= false;
3388 struct lxc_list
*it
;
3393 memset(retv
, 0, inlen
);
3395 if (!strcmp(key
, "lxc.prlimit"))
3397 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3402 lxc_list_for_each(it
, &c
->limits
) {
3403 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3404 integers or the word
3407 struct lxc_limit
*lim
= it
->elem
;
3409 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3410 memcpy(buf
, "unlimited", sizeof("unlimited"));
3411 partlen
= sizeof("unlimited") - 1;
3413 partlen
= sprintf(buf
, "%" PRIu64
,
3414 (uint64_t)lim
->limit
.rlim_cur
);
3416 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3417 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3418 memcpy(buf
+ partlen
, ":unlimited",
3419 sizeof(":unlimited"));
3421 sprintf(buf
+ partlen
, ":%" PRIu64
,
3422 (uint64_t)lim
->limit
.rlim_max
);
3426 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3427 lim
->resource
, buf
);
3428 } else if (!strcmp(lim
->resource
, key
)) {
3429 strprint(retv
, inlen
, "%s", buf
);
3436 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3437 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3438 * entries will be printed, in 'lxc.sysctl.key = value' format.
3440 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3441 struct lxc_conf
*c
, void *data
)
3444 struct lxc_list
*it
;
3446 bool get_all
= false;
3451 memset(retv
, 0, inlen
);
3453 if (strcmp(key
, "lxc.sysctl") == 0)
3455 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3456 key
+= sizeof("lxc.sysctl.") - 1;
3460 lxc_list_for_each(it
, &c
->sysctls
) {
3461 struct lxc_sysctl
*elem
= it
->elem
;
3463 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3464 elem
->key
, elem
->value
);
3465 } else if (strcmp(elem
->key
, key
) == 0) {
3466 strprint(retv
, inlen
, "%s", elem
->value
);
3473 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3474 struct lxc_conf
*c
, void *data
)
3476 struct lxc_list
*it
;
3479 bool get_all
= false;
3484 memset(retv
, 0, inlen
);
3486 if (strcmp(key
, "lxc.proc") == 0)
3488 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3489 key
+= sizeof("lxc.proc.") - 1;
3493 lxc_list_for_each(it
, &c
->procs
) {
3494 struct lxc_proc
*proc
= it
->elem
;
3497 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3498 proc
->filename
, proc
->value
);
3499 } else if (strcmp(proc
->filename
, key
) == 0) {
3500 strprint(retv
, inlen
, "%s", proc
->value
);
3507 static int get_config_noop(const char *key
, char *retv
, int inlen
,
3508 struct lxc_conf
*c
, void *data
)
3513 static int get_config_namespace(const char *key
, char *retv
, int inlen
,
3514 struct lxc_conf
*c
, void *data
)
3517 const char *namespace;
3523 memset(retv
, 0, inlen
);
3525 namespace = key
+ sizeof("lxc.namespace.") - 1;
3526 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3530 strprint(retv
, inlen
, "%s", c
->inherit_ns
[ns_idx
]);
3535 /* Callbacks to clear config items. */
3536 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3539 c
->personality
= -1;
3543 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3550 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3557 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3565 static inline int clr_config_apparmor_profile(const char *key
,
3566 struct lxc_conf
*c
, void *data
)
3568 free(c
->lsm_aa_profile
);
3569 c
->lsm_aa_profile
= NULL
;
3573 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3577 c
->lsm_aa_allow_incomplete
= 0;
3581 static inline int clr_config_selinux_context(const char *key
,
3582 struct lxc_conf
*c
, void *data
)
3584 free(c
->lsm_se_context
);
3585 c
->lsm_se_context
= NULL
;
3589 static inline int clr_config_cgroup_controller(const char *key
,
3590 struct lxc_conf
*c
, void *data
)
3592 return lxc_clear_cgroups(c
, key
);
3595 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3598 if (lxc_conf
->cgroup_meta
.dir
) {
3599 free(lxc_conf
->cgroup_meta
.dir
);
3600 lxc_conf
->cgroup_meta
.dir
= NULL
;
3606 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3609 return lxc_clear_idmaps(c
);
3612 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3615 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3619 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3627 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3630 return lxc_clear_mount_entries(c
);
3633 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3636 return lxc_clear_automounts(c
);
3639 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3647 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3650 free(c
->rootfs
.path
);
3651 c
->rootfs
.path
= NULL
;
3655 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3658 free(c
->rootfs
.mount
);
3659 c
->rootfs
.mount
= NULL
;
3663 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3666 free(c
->rootfs
.options
);
3667 c
->rootfs
.options
= NULL
;
3671 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3677 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3685 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3688 return lxc_clear_hooks(c
, key
);
3691 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3694 lxc_free_networks(&c
->network
);
3699 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3702 return lxc_clear_config_caps(c
);
3705 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3708 return lxc_clear_config_keepcaps(c
);
3711 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3714 free(c
->console
.path
);
3715 c
->console
.path
= NULL
;
3719 static inline int clr_config_console_logfile(const char *key
,
3720 struct lxc_conf
*c
, void *data
)
3722 free(c
->console
.log_path
);
3723 c
->console
.log_path
= NULL
;
3727 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3730 c
->console
.log_rotate
= 0;
3734 static inline int clr_config_console_buffer_size(const char *key
,
3735 struct lxc_conf
*c
, void *data
)
3737 c
->console
.buffer_size
= 0;
3741 static inline int clr_config_console_buffer_logfile(const char *key
,
3745 free(c
->console
.buffer_log_file
);
3746 c
->console
.buffer_log_file
= NULL
;
3750 static inline int clr_config_seccomp_profile(const char *key
,
3751 struct lxc_conf
*c
, void *data
)
3758 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3765 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3772 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3775 c
->rebootsignal
= 0;
3779 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3786 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3789 if (strcmp(key
+ 10, "auto") == 0)
3791 else if (strcmp(key
+ 10, "delay") == 0)
3793 else if (strcmp(key
+ 10, "order") == 0)
3799 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3807 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3810 c
->monitor_unshare
= 0;
3814 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3817 return lxc_clear_groups(c
);
3820 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3823 return lxc_clear_environment(c
);
3826 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
3829 free(c
->execute_cmd
);
3830 c
->execute_cmd
= NULL
;
3834 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3842 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
3850 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3857 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3864 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3871 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3874 c
->no_new_privs
= false;
3878 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3881 return lxc_clear_limits(c
, key
);
3884 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
3887 return lxc_clear_sysctls(c
, key
);
3890 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
3893 return lxc_clear_procs(c
, key
);
3896 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3899 lxc_clear_includes(c
);
3903 static inline int clr_config_noop(const char *key
, struct lxc_conf
*c
,
3909 static int clr_config_namespace(const char *key
, struct lxc_conf
*lxc_conf
,
3913 const char *namespace;
3915 namespace = key
+ sizeof("lxc.namespace.") - 1;
3916 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3920 free(lxc_conf
->inherit_ns
[ns_idx
]);
3921 lxc_conf
->inherit_ns
[ns_idx
] = NULL
;
3926 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3927 struct lxc_conf
*c
, void *data
)
3932 static struct lxc_config_t
*get_network_config_ops(const char *key
,
3933 struct lxc_conf
*lxc_conf
,
3935 char **deindexed_key
)
3938 unsigned int tmpidx
;
3940 char *copy
, *idx_start
, *idx_end
;
3941 struct lxc_config_t
*config
= NULL
;
3943 /* check that this is a sensible network key */
3944 if (strncmp("lxc.net.", key
, 8)) {
3945 ERROR("Invalid network configuration key \"%s\"", key
);
3951 ERROR("Failed to duplicate string \"%s\"", key
);
3956 if (!isdigit(*(key
+ 8))) {
3957 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
3961 /* beginning of index string */
3962 idx_start
= (copy
+ 7);
3965 /* end of index string */
3966 idx_end
= strchr((copy
+ 8), '.');
3970 /* parse current index */
3971 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
3973 ERROR("Failed to parse usigned integer from string \"%s\": %s",
3974 idx_start
+ 1, strerror(-ret
));
3979 /* This, of course is utterly nonsensical on so many levels, but
3980 * better safe than sorry.
3981 * (Checking for INT_MAX here is intentional.)
3983 if (tmpidx
== INT_MAX
) {
3984 SYSERROR("Number of configured networks would overflow the "
3990 numstrlen
= strlen((idx_start
+ 1));
3992 /* repair configuration key */
3995 /* lxc.net.<idx>.<subkey> */
3998 if (strlen(idx_end
+ 1) == 0) {
3999 ERROR("No subkey in network configuration key \"%s\"", key
);
4003 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4004 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4006 config
= lxc_get_config(copy
);
4008 ERROR("Unknown network configuration key \"%s\"", key
);
4014 *deindexed_key
= copy
;
4023 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4024 * found. So we make sure next comes an integer, find the right callback (by
4025 * rewriting the key), and call it.
4027 static int set_config_net_nic(const char *key
, const char *value
,
4028 struct lxc_conf
*lxc_conf
, void *data
)
4031 const char *idxstring
;
4032 struct lxc_config_t
*config
;
4033 struct lxc_netdev
*netdev
;
4035 char *deindexed_key
= NULL
;
4037 idxstring
= key
+ 8;
4038 if (!isdigit(*idxstring
))
4041 if (lxc_config_value_empty(value
))
4042 return clr_config_net_nic(key
, lxc_conf
, data
);
4044 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4045 if (!config
|| idx
< 0)
4048 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4050 free(deindexed_key
);
4054 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4055 free(deindexed_key
);
4059 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4063 const char *idxstring
;
4064 struct lxc_config_t
*config
;
4065 struct lxc_netdev
*netdev
;
4067 char *deindexed_key
= NULL
;
4069 idxstring
= key
+ 8;
4070 if (!isdigit(*idxstring
))
4073 /* The left conjunct is pretty self-explanatory. The right conjunct
4074 * checks whether the two pointers are equal. If they are we know that
4075 * this is not a key that is namespaced any further and so we are
4076 * supposed to clear the whole network.
4078 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4079 unsigned int rmnetdevidx
;
4081 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4084 /* Remove network from network list. */
4085 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4089 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4090 if (!config
|| idx
< 0)
4093 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4095 free(deindexed_key
);
4099 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4100 free(deindexed_key
);
4104 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4107 struct lxc_netdev
*netdev
= data
;
4117 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4120 struct lxc_netdev
*netdev
= data
;
4125 netdev
->name
[0] = '\0';
4130 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4133 struct lxc_netdev
*netdev
= data
;
4143 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4146 struct lxc_netdev
*netdev
= data
;
4151 netdev
->link
[0] = '\0';
4156 static int clr_config_net_macvlan_mode(const char *key
,
4157 struct lxc_conf
*lxc_conf
, void *data
)
4159 struct lxc_netdev
*netdev
= data
;
4164 if (netdev
->type
!= LXC_NET_MACVLAN
)
4167 netdev
->priv
.macvlan_attr
.mode
= -1;
4172 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4175 struct lxc_netdev
*netdev
= data
;
4180 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4185 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4188 struct lxc_netdev
*netdev
= data
;
4193 free(netdev
->upscript
);
4194 netdev
->upscript
= NULL
;
4199 static int clr_config_net_script_down(const char *key
,
4200 struct lxc_conf
*lxc_conf
, void *data
)
4202 struct lxc_netdev
*netdev
= data
;
4207 free(netdev
->downscript
);
4208 netdev
->downscript
= NULL
;
4213 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4216 struct lxc_netdev
*netdev
= data
;
4221 free(netdev
->hwaddr
);
4222 netdev
->hwaddr
= NULL
;
4227 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4230 struct lxc_netdev
*netdev
= data
;
4241 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4244 struct lxc_netdev
*netdev
= data
;
4249 netdev
->priv
.vlan_attr
.vid
= 0;
4254 static int clr_config_net_ipv4_gateway(const char *key
,
4255 struct lxc_conf
*lxc_conf
, void *data
)
4257 struct lxc_netdev
*netdev
= data
;
4262 free(netdev
->ipv4_gateway
);
4263 netdev
->ipv4_gateway
= NULL
;
4268 static int clr_config_net_ipv4_address(const char *key
,
4269 struct lxc_conf
*lxc_conf
, void *data
)
4271 struct lxc_netdev
*netdev
= data
;
4272 struct lxc_list
*cur
, *next
;
4277 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4286 static int clr_config_net_ipv6_gateway(const char *key
,
4287 struct lxc_conf
*lxc_conf
, void *data
)
4289 struct lxc_netdev
*netdev
= data
;
4294 free(netdev
->ipv6_gateway
);
4295 netdev
->ipv6_gateway
= NULL
;
4300 static int clr_config_net_ipv6_address(const char *key
,
4301 struct lxc_conf
*lxc_conf
, void *data
)
4303 struct lxc_netdev
*netdev
= data
;
4304 struct lxc_list
*cur
, *next
;
4309 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4318 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4319 struct lxc_conf
*c
, void *data
)
4322 const char *idxstring
;
4323 struct lxc_config_t
*config
;
4324 struct lxc_netdev
*netdev
;
4326 char *deindexed_key
= NULL
;
4328 idxstring
= key
+ 8;
4329 if (!isdigit(*idxstring
))
4332 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4333 if (!config
|| idx
< 0)
4336 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4338 free(deindexed_key
);
4342 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4343 free(deindexed_key
);
4347 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4348 struct lxc_conf
*c
, void *data
)
4352 struct lxc_netdev
*netdev
= data
;
4357 memset(retv
, 0, inlen
);
4362 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4367 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4368 struct lxc_conf
*c
, void *data
)
4372 struct lxc_netdev
*netdev
= data
;
4377 memset(retv
, 0, inlen
);
4382 if (netdev
->flags
& IFF_UP
)
4383 strprint(retv
, inlen
, "up");
4388 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4389 struct lxc_conf
*c
, void *data
)
4393 struct lxc_netdev
*netdev
= data
;
4398 memset(retv
, 0, inlen
);
4403 if (netdev
->link
[0] != '\0')
4404 strprint(retv
, inlen
, "%s", netdev
->link
);
4409 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4410 struct lxc_conf
*c
, void *data
)
4414 struct lxc_netdev
*netdev
= data
;
4419 memset(retv
, 0, inlen
);
4424 if (netdev
->name
[0] != '\0')
4425 strprint(retv
, inlen
, "%s", netdev
->name
);
4430 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4431 struct lxc_conf
*c
, void *data
)
4436 struct lxc_netdev
*netdev
= data
;
4441 memset(retv
, 0, inlen
);
4446 if (netdev
->type
!= LXC_NET_MACVLAN
)
4449 switch (netdev
->priv
.macvlan_attr
.mode
) {
4450 case MACVLAN_MODE_PRIVATE
:
4453 case MACVLAN_MODE_VEPA
:
4456 case MACVLAN_MODE_BRIDGE
:
4459 case MACVLAN_MODE_PASSTHRU
:
4467 strprint(retv
, inlen
, "%s", mode
);
4472 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4473 struct lxc_conf
*c
, void *data
)
4477 struct lxc_netdev
*netdev
= data
;
4482 memset(retv
, 0, inlen
);
4487 if (netdev
->type
!= LXC_NET_VETH
)
4490 strprint(retv
, inlen
, "%s",
4491 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4492 ? netdev
->priv
.veth_attr
.pair
4493 : netdev
->priv
.veth_attr
.veth1
);
4498 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4499 struct lxc_conf
*c
, void *data
)
4503 struct lxc_netdev
*netdev
= data
;
4508 memset(retv
, 0, inlen
);
4513 if (netdev
->upscript
)
4514 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4519 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4520 struct lxc_conf
*c
, void *data
)
4524 struct lxc_netdev
*netdev
= data
;
4529 memset(retv
, 0, inlen
);
4534 if (netdev
->downscript
)
4535 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4540 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4541 struct lxc_conf
*c
, void *data
)
4545 struct lxc_netdev
*netdev
= data
;
4550 memset(retv
, 0, inlen
);
4556 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4561 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4562 struct lxc_conf
*c
, void *data
)
4566 struct lxc_netdev
*netdev
= data
;
4571 memset(retv
, 0, inlen
);
4577 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4582 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4583 struct lxc_conf
*c
, void *data
)
4587 struct lxc_netdev
*netdev
= data
;
4592 memset(retv
, 0, inlen
);
4597 if (netdev
->type
!= LXC_NET_VLAN
)
4600 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4605 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4606 struct lxc_conf
*c
, void *data
)
4609 char buf
[INET_ADDRSTRLEN
];
4611 struct lxc_netdev
*netdev
= data
;
4616 memset(retv
, 0, inlen
);
4621 if (netdev
->ipv4_gateway_auto
) {
4622 strprint(retv
, inlen
, "auto");
4623 } else if (netdev
->ipv4_gateway
) {
4624 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4625 strprint(retv
, inlen
, "%s", buf
);
4631 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4632 struct lxc_conf
*c
, void *data
)
4636 char buf
[INET_ADDRSTRLEN
];
4637 struct lxc_list
*it
;
4639 struct lxc_netdev
*netdev
= data
;
4644 memset(retv
, 0, inlen
);
4649 listlen
= lxc_list_len(&netdev
->ipv4
);
4650 lxc_list_for_each(it
, &netdev
->ipv4
) {
4651 struct lxc_inetdev
*i
= it
->elem
;
4652 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4653 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4654 (listlen
-- > 1) ? "\n" : "");
4660 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4661 struct lxc_conf
*c
, void *data
)
4664 char buf
[INET6_ADDRSTRLEN
];
4666 struct lxc_netdev
*netdev
= data
;
4671 memset(retv
, 0, inlen
);
4676 if (netdev
->ipv6_gateway_auto
) {
4677 strprint(retv
, inlen
, "auto");
4678 } else if (netdev
->ipv6_gateway
) {
4679 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4680 strprint(retv
, inlen
, "%s", buf
);
4686 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4687 struct lxc_conf
*c
, void *data
)
4691 char buf
[INET6_ADDRSTRLEN
];
4692 struct lxc_list
*it
;
4694 struct lxc_netdev
*netdev
= data
;
4699 memset(retv
, 0, inlen
);
4704 listlen
= lxc_list_len(&netdev
->ipv6
);
4705 lxc_list_for_each(it
, &netdev
->ipv6
) {
4706 struct lxc_inet6dev
*i
= it
->elem
;
4707 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4708 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4709 (listlen
-- > 1) ? "\n" : "");
4715 int lxc_list_config_items(char *retv
, int inlen
)
4724 memset(retv
, 0, inlen
);
4726 for (i
= 0; i
< config_size
; i
++) {
4727 char *s
= config
[i
].name
;
4729 if (s
[strlen(s
) - 1] == '.')
4732 strprint(retv
, inlen
, "%s\n", s
);
4738 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4747 memset(retv
, 0, inlen
);
4749 if (!strcmp(key
, "lxc.apparmor")) {
4750 strprint(retv
, inlen
, "allow_incomplete\n");
4751 strprint(retv
, inlen
, "profile\n");
4752 } else if (!strcmp(key
, "lxc.cgroup")) {
4753 strprint(retv
, inlen
, "dir\n");
4754 } else if (!strcmp(key
, "lxc.selinux")) {
4755 strprint(retv
, inlen
, "context\n");
4756 } else if (!strcmp(key
, "lxc.mount")) {
4757 strprint(retv
, inlen
, "auto\n");
4758 strprint(retv
, inlen
, "entry\n");
4759 strprint(retv
, inlen
, "fstab\n");
4760 } else if (!strcmp(key
, "lxc.rootfs")) {
4761 strprint(retv
, inlen
, "mount\n");
4762 strprint(retv
, inlen
, "options\n");
4763 strprint(retv
, inlen
, "path\n");
4764 } else if (!strcmp(key
, "lxc.uts")) {
4765 strprint(retv
, inlen
, "name\n");
4766 } else if (!strcmp(key
, "lxc.hook")) {
4767 strprint(retv
, inlen
, "autodev\n");
4768 strprint(retv
, inlen
, "clone\n");
4769 strprint(retv
, inlen
, "destroy\n");
4770 strprint(retv
, inlen
, "mount\n");
4771 strprint(retv
, inlen
, "post-stop\n");
4772 strprint(retv
, inlen
, "pre-mount\n");
4773 strprint(retv
, inlen
, "pre-start\n");
4774 strprint(retv
, inlen
, "start-host\n");
4775 strprint(retv
, inlen
, "start\n");
4776 strprint(retv
, inlen
, "stop\n");
4777 } else if (!strcmp(key
, "lxc.cap")) {
4778 strprint(retv
, inlen
, "drop\n");
4779 strprint(retv
, inlen
, "keep\n");
4780 } else if (!strcmp(key
, "lxc.console")) {
4781 strprint(retv
, inlen
, "logfile\n");
4782 strprint(retv
, inlen
, "path\n");
4783 } else if (!strcmp(key
, "lxc.seccomp")) {
4784 strprint(retv
, inlen
, "profile\n");
4785 } else if (!strcmp(key
, "lxc.signal")) {
4786 strprint(retv
, inlen
, "halt\n");
4787 strprint(retv
, inlen
, "reboot\n");
4788 strprint(retv
, inlen
, "stop\n");
4789 } else if (!strcmp(key
, "lxc.start")) {
4790 strprint(retv
, inlen
, "auto\n");
4791 strprint(retv
, inlen
, "delay\n");
4792 strprint(retv
, inlen
, "order\n");
4793 } else if (!strcmp(key
, "lxc.monitor")) {
4794 strprint(retv
, inlen
, "unshare\n");
4802 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4805 const char *idxstring
;
4806 struct lxc_netdev
*netdev
;
4810 idxstring
= key
+ 8;
4811 if (!isdigit(*idxstring
))
4814 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
4818 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4825 memset(retv
, 0, inlen
);
4827 strprint(retv
, inlen
, "type\n");
4828 strprint(retv
, inlen
, "script.up\n");
4829 strprint(retv
, inlen
, "script.down\n");
4830 if (netdev
->type
!= LXC_NET_EMPTY
) {
4831 strprint(retv
, inlen
, "flags\n");
4832 strprint(retv
, inlen
, "link\n");
4833 strprint(retv
, inlen
, "name\n");
4834 strprint(retv
, inlen
, "hwaddr\n");
4835 strprint(retv
, inlen
, "mtu\n");
4836 strprint(retv
, inlen
, "ipv6.address\n");
4837 strprint(retv
, inlen
, "ipv6.gateway\n");
4838 strprint(retv
, inlen
, "ipv4.address\n");
4839 strprint(retv
, inlen
, "ipv4.gateway\n");
4842 switch (netdev
->type
) {
4844 strprint(retv
, inlen
, "veth.pair\n");
4846 case LXC_NET_MACVLAN
:
4847 strprint(retv
, inlen
, "macvlan.mode\n");
4850 strprint(retv
, inlen
, "vlan.id\n");