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(hooks_version
);
96 lxc_config_define(idmaps
);
97 lxc_config_define(includefiles
);
98 lxc_config_define(init_cmd
);
99 lxc_config_define(init_cwd
);
100 lxc_config_define(init_gid
);
101 lxc_config_define(init_uid
);
102 lxc_config_define(log_file
);
103 lxc_config_define(log_level
);
104 lxc_config_define(log_syslog
);
105 lxc_config_define(monitor
);
106 lxc_config_define(mount
);
107 lxc_config_define(mount_auto
);
108 lxc_config_define(mount_fstab
);
109 lxc_config_define(namespace);
110 lxc_config_define(net
);
111 lxc_config_define(net_flags
);
112 lxc_config_define(net_hwaddr
);
113 lxc_config_define(net_ipv4_address
);
114 lxc_config_define(net_ipv4_gateway
);
115 lxc_config_define(net_ipv6_address
);
116 lxc_config_define(net_ipv6_gateway
);
117 lxc_config_define(net_link
);
118 lxc_config_define(net_macvlan_mode
);
119 lxc_config_define(net_mtu
);
120 lxc_config_define(net_name
);
121 lxc_config_define(net_nic
);
122 lxc_config_define(net_script_down
);
123 lxc_config_define(net_script_up
);
124 lxc_config_define(net_type
);
125 lxc_config_define(net_veth_pair
);
126 lxc_config_define(net_vlan_id
);
127 lxc_config_define(no_new_privs
);
128 lxc_config_define(noop
);
129 lxc_config_define(personality
);
130 lxc_config_define(prlimit
);
131 lxc_config_define(pty_max
);
132 lxc_config_define(rootfs_backend
);
133 lxc_config_define(rootfs_mount
);
134 lxc_config_define(rootfs_options
);
135 lxc_config_define(rootfs_path
);
136 lxc_config_define(seccomp_profile
);
137 lxc_config_define(selinux_context
);
138 lxc_config_define(signal_halt
);
139 lxc_config_define(signal_reboot
);
140 lxc_config_define(signal_stop
);
141 lxc_config_define(start
);
142 lxc_config_define(tty_max
);
143 lxc_config_define(tty_dir
);
144 lxc_config_define(uts_name
);
145 lxc_config_define(sysctl
);
146 lxc_config_define(proc
);
148 static struct lxc_config_t config
[] = {
149 /* REMOVE in LXC 3.0 */
150 { "lxc.arch", false, set_config_personality
, get_config_personality
, clr_config_personality
, },
151 { "lxc.apparmor.profile", false, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
152 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
153 { "lxc.autodev", false, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
154 { "lxc.cap.drop", false, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
155 { "lxc.cap.keep", false, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
156 { "lxc.cgroup.dir", false, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
157 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
158 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile
, get_config_console_buffer_logfile
, clr_config_console_buffer_logfile
, },
159 { "lxc.console.buffer.size", false, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
160 { "lxc.console.logfile", false, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
161 { "lxc.console.path", false, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
162 { "lxc.console.rotate", false, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
163 { "lxc.environment", false, set_config_environment
, get_config_environment
, clr_config_environment
, },
164 { "lxc.ephemeral", false, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
165 { "lxc.execute.cmd", false, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
166 { "lxc.group", false, set_config_group
, get_config_group
, clr_config_group
, },
167 { "lxc.hook.autodev", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
168 { "lxc.hook.clone", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
169 { "lxc.hook.destroy", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
170 { "lxc.hook.mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
171 { "lxc.hook.post-stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.pre-mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook.pre-start", 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.start-host", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
176 { "lxc.hook.stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
177 { "lxc.hook.version", false, set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
178 { "lxc.hook", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
179 { "lxc.idmap", false, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
180 { "lxc.include", false, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
181 { "lxc.init.cmd", false, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
182 { "lxc.init.gid", false, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
183 { "lxc.init.uid", false, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
184 { "lxc.init.cwd", false, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
185 { "lxc.log.file", false, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
186 { "lxc.log.level", false, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
187 { "lxc.log.syslog", false, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
188 { "lxc.monitor.unshare", false, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
189 { "lxc.mount.auto", false, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
190 { "lxc.mount.entry", false, set_config_mount
, get_config_mount
, clr_config_mount
, },
191 { "lxc.mount.fstab", false, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
192 { "lxc.namespace", false, set_config_namespace
, get_config_namespace
, clr_config_namespace
, },
194 /* [START]: REMOVE IN LXC 3.0 */
195 { "lxc.network.type", true, set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
196 { "lxc.network.flags", true, set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
197 { "lxc.network.link", true, set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
198 { "lxc.network.name", true, set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
199 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
200 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
201 { "lxc.network.script.up", true, set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
202 { "lxc.network.script.down", true, set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
203 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
204 { "lxc.network.mtu", true, set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
205 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
206 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
207 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
208 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
209 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
210 { "lxc.network.", true, set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
211 { "lxc.network", true, set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
212 /* [END]: REMOVE IN LXC 3.0 */
214 { "lxc.net.flags", false, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
215 { "lxc.net.hwaddr", false, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
216 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
217 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
218 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
219 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
220 { "lxc.net.link", false, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
221 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
222 { "lxc.net.mtu", false, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
223 { "lxc.net.name", false, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
224 { "lxc.net.script.down", false, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
225 { "lxc.net.script.up", false, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
226 { "lxc.net.type", false, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
227 { "lxc.net.vlan.id", false, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
228 { "lxc.net.veth.pair", false, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
229 { "lxc.net.", false, set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
230 { "lxc.net", false, set_config_net
, get_config_net
, clr_config_net
, },
231 { "lxc.no_new_privs", false, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
232 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
233 { "lxc.pty.max", false, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
234 { "lxc.rootfs.mount", false, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
235 { "lxc.rootfs.options", false, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
236 { "lxc.rootfs.path", false, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
237 { "lxc.seccomp.profile", false, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
238 { "lxc.selinux.context", false, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
239 { "lxc.signal.halt", false, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
240 { "lxc.signal.reboot", false, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
241 { "lxc.signal.stop", false, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
242 { "lxc.start.auto", false, set_config_start
, get_config_start
, clr_config_start
, },
243 { "lxc.start.delay", false, set_config_start
, get_config_start
, clr_config_start
, },
244 { "lxc.start.order", false, set_config_start
, get_config_start
, clr_config_start
, },
245 { "lxc.tty.dir", false, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
246 { "lxc.tty.max", false, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
247 { "lxc.uts.name", false, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
248 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
249 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
251 /* [START]: REMOVE IN LXC 3.0 */
252 { "lxc.pts", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
253 { "lxc.devttydir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
254 { "lxc.tty", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
255 { "lxc.aa_profile", true, set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
256 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
257 { "lxc.se_context", true, set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
258 { "lxc.id_map", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
259 { "lxc.mount", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
260 { "lxc.rootfs.backend", true, set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
261 { "lxc.rootfs", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
262 { "lxc.utsname", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
263 { "lxc.seccomp", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
264 { "lxc.console", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
265 { "lxc.haltsignal", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
266 { "lxc.rebootsignal", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
267 { "lxc.stopsignal", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
268 { "lxc.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
269 { "lxc.kmsg", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
270 { "lxc.loglevel", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
271 { "lxc.logfile", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
272 { "lxc.init_cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
273 { "lxc.init_uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
274 { "lxc.init_gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
275 { "lxc.limit", true, set_config_limit
, get_config_limit
, clr_config_limit
, },
276 { "lxc.pivotdir", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
277 /* [END]: REMOVE IN LXC 3.0 */
285 static const struct signame signames
[] = {
318 { SIGSTKFLT
, "STKFLT" },
333 { SIGVTALRM
, "VTALRM" },
339 { SIGWINCH
, "WINCH" },
357 { SIGUNUSED
, "UNUSED" },
364 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
366 struct lxc_config_t
*lxc_get_config(const char *key
)
370 for (i
= 0; i
< config_size
; i
++)
371 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
377 static int set_config_net(const char *key
, const char *value
,
378 struct lxc_conf
*lxc_conf
, void *data
)
380 if (!lxc_config_value_empty(value
)) {
381 ERROR("lxc.net must not have a value");
385 return clr_config_net(key
, lxc_conf
, data
);
388 static int set_config_net_type(const char *key
, const char *value
,
389 struct lxc_conf
*lxc_conf
, void *data
)
391 struct lxc_netdev
*netdev
= data
;
393 if (lxc_config_value_empty(value
))
394 return clr_config_net_type(key
, lxc_conf
, data
);
399 if (!strcmp(value
, "veth")) {
400 netdev
->type
= LXC_NET_VETH
;
401 } else if (!strcmp(value
, "macvlan")) {
402 netdev
->type
= LXC_NET_MACVLAN
;
403 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
405 } else if (!strcmp(value
, "vlan")) {
406 netdev
->type
= LXC_NET_VLAN
;
407 } else if (!strcmp(value
, "phys")) {
408 netdev
->type
= LXC_NET_PHYS
;
409 } else if (!strcmp(value
, "empty")) {
410 netdev
->type
= LXC_NET_EMPTY
;
411 } else if (!strcmp(value
, "none")) {
412 netdev
->type
= LXC_NET_NONE
;
414 ERROR("invalid network type %s", value
);
421 static int set_config_net_flags(const char *key
, const char *value
,
422 struct lxc_conf
*lxc_conf
, void *data
)
424 struct lxc_netdev
*netdev
= data
;
426 if (lxc_config_value_empty(value
))
427 return clr_config_net_flags(key
, lxc_conf
, data
);
432 netdev
->flags
|= IFF_UP
;
437 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
438 struct lxc_netdev
*netdev
)
440 struct ifaddrs
*ifaddr
, *ifa
;
443 const char *type_key
= "lxc.net.type";
444 const char *link_key
= "lxc.net.link";
445 const char *tmpvalue
= "phys";
447 if (getifaddrs(&ifaddr
) == -1) {
448 SYSERROR("Get network interfaces failed");
452 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
455 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
458 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
459 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
462 ret
= set_config_net_link(
463 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
465 ERROR("failed to create matched ifnames");
469 ERROR("failed to create matched ifnames");
481 static int set_config_net_link(const char *key
, const char *value
,
482 struct lxc_conf
*lxc_conf
, void *data
)
484 struct lxc_netdev
*netdev
= data
;
487 if (lxc_config_value_empty(value
))
488 return clr_config_net_link(key
, lxc_conf
, data
);
493 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
494 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
496 ret
= network_ifname(netdev
->link
, value
);
501 static int set_config_net_name(const char *key
, const char *value
,
502 struct lxc_conf
*lxc_conf
, void *data
)
504 struct lxc_netdev
*netdev
= data
;
506 if (lxc_config_value_empty(value
))
507 return clr_config_net_name(key
, lxc_conf
, data
);
512 return network_ifname(netdev
->name
, value
);
515 static int set_config_net_veth_pair(const char *key
, const char *value
,
516 struct lxc_conf
*lxc_conf
, void *data
)
518 struct lxc_netdev
*netdev
= data
;
520 if (lxc_config_value_empty(value
))
521 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
526 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
);
529 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
530 struct lxc_conf
*lxc_conf
, void *data
)
532 struct lxc_netdev
*netdev
= data
;
534 if (lxc_config_value_empty(value
))
535 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
540 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
543 static int set_config_net_hwaddr(const char *key
, const char *value
,
544 struct lxc_conf
*lxc_conf
, void *data
)
546 struct lxc_netdev
*netdev
= data
;
549 if (lxc_config_value_empty(value
))
550 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
555 new_value
= strdup(value
);
559 rand_complete_hwaddr(new_value
);
561 if (lxc_config_value_empty(new_value
)) {
563 netdev
->hwaddr
= NULL
;
567 netdev
->hwaddr
= new_value
;
571 static int set_config_net_vlan_id(const char *key
, const char *value
,
572 struct lxc_conf
*lxc_conf
, void *data
)
575 struct lxc_netdev
*netdev
= data
;
577 if (lxc_config_value_empty(value
))
578 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
583 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
590 static int set_config_net_mtu(const char *key
, const char *value
,
591 struct lxc_conf
*lxc_conf
, void *data
)
593 struct lxc_netdev
*netdev
= data
;
595 if (lxc_config_value_empty(value
))
596 return clr_config_net_mtu(key
, lxc_conf
, data
);
601 return set_config_string_item(&netdev
->mtu
, value
);
604 static int set_config_net_ipv4_address(const char *key
, const char *value
,
605 struct lxc_conf
*lxc_conf
, void *data
)
608 struct lxc_netdev
*netdev
= data
;
609 struct lxc_inetdev
*inetdev
;
610 struct lxc_list
*list
;
611 char *cursor
, *slash
;
612 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
614 if (lxc_config_value_empty(value
))
615 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
620 inetdev
= malloc(sizeof(*inetdev
));
624 memset(inetdev
, 0, sizeof(*inetdev
));
626 list
= malloc(sizeof(*list
));
633 list
->elem
= inetdev
;
635 addr
= strdup(value
);
642 cursor
= strstr(addr
, " ");
648 slash
= strstr(addr
, "/");
654 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
655 if (!ret
|| ret
< 0) {
656 SYSERROR("Invalid ipv4 address \"%s\"", value
);
664 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
665 if (!ret
|| ret
< 0) {
666 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
675 /* No prefix specified, determine it from the network class. */
677 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
681 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
684 /* If no broadcast address, let compute one from the
685 * prefix and address.
688 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
689 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
692 lxc_list_add_tail(&netdev
->ipv4
, list
);
698 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
699 struct lxc_conf
*lxc_conf
, void *data
)
701 struct lxc_netdev
*netdev
= data
;
703 if (lxc_config_value_empty(value
))
704 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
709 free(netdev
->ipv4_gateway
);
711 if (!strcmp(value
, "auto")) {
712 netdev
->ipv4_gateway
= NULL
;
713 netdev
->ipv4_gateway_auto
= true;
718 gw
= malloc(sizeof(*gw
));
722 ret
= inet_pton(AF_INET
, value
, gw
);
723 if (!ret
|| ret
< 0) {
724 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
729 netdev
->ipv4_gateway
= gw
;
730 netdev
->ipv4_gateway_auto
= false;
736 static int set_config_net_ipv6_address(const char *key
, const char *value
,
737 struct lxc_conf
*lxc_conf
, void *data
)
740 struct lxc_netdev
*netdev
= data
;
741 struct lxc_inet6dev
*inet6dev
;
742 struct lxc_list
*list
;
743 char *slash
, *valdup
, *netmask
;
745 if (lxc_config_value_empty(value
))
746 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
751 inet6dev
= malloc(sizeof(*inet6dev
));
755 memset(inet6dev
, 0, sizeof(*inet6dev
));
757 list
= malloc(sizeof(*list
));
764 list
->elem
= inet6dev
;
766 valdup
= strdup(value
);
773 inet6dev
->prefix
= 64;
774 slash
= strstr(valdup
, "/");
778 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
787 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
788 if (!ret
|| ret
< 0) {
789 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
796 lxc_list_add_tail(&netdev
->ipv6
, list
);
802 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
803 struct lxc_conf
*lxc_conf
, void *data
)
805 struct lxc_netdev
*netdev
= data
;
807 if (lxc_config_value_empty(value
))
808 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
813 free(netdev
->ipv6_gateway
);
815 if (!strcmp(value
, "auto")) {
816 netdev
->ipv6_gateway
= NULL
;
817 netdev
->ipv6_gateway_auto
= true;
822 gw
= malloc(sizeof(*gw
));
826 ret
= inet_pton(AF_INET6
, value
, gw
);
827 if (!ret
|| ret
< 0) {
828 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
833 netdev
->ipv6_gateway
= gw
;
834 netdev
->ipv6_gateway_auto
= false;
840 static int set_config_net_script_up(const char *key
, const char *value
,
841 struct lxc_conf
*lxc_conf
, void *data
)
843 struct lxc_netdev
*netdev
= data
;
845 if (lxc_config_value_empty(value
))
846 return clr_config_net_script_up(key
, lxc_conf
, data
);
851 return set_config_string_item(&netdev
->upscript
, value
);
854 static int set_config_net_script_down(const char *key
, const char *value
,
855 struct lxc_conf
*lxc_conf
, void *data
)
857 struct lxc_netdev
*netdev
= data
;
859 if (lxc_config_value_empty(value
))
860 return clr_config_net_script_down(key
, lxc_conf
, data
);
865 return set_config_string_item(&netdev
->downscript
, value
);
868 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
870 struct lxc_list
*hooklist
;
872 hooklist
= malloc(sizeof(*hooklist
));
878 hooklist
->elem
= hook
;
879 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
883 static int set_config_seccomp_profile(const char *key
, const char *value
,
884 struct lxc_conf
*lxc_conf
, void *data
)
886 return set_config_path_item(&lxc_conf
->seccomp
, value
);
889 static int set_config_execute_cmd(const char *key
, const char *value
,
890 struct lxc_conf
*lxc_conf
, void *data
)
892 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
895 static int set_config_init_cmd(const char *key
, const char *value
,
896 struct lxc_conf
*lxc_conf
, void *data
)
898 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
901 static int set_config_init_cwd(const char *key
, const char *value
,
902 struct lxc_conf
*lxc_conf
, void *data
)
904 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
907 static int set_config_init_uid(const char *key
, const char *value
,
908 struct lxc_conf
*lxc_conf
, void *data
)
910 unsigned int init_uid
;
912 if (lxc_config_value_empty(value
)) {
913 lxc_conf
->init_uid
= 0;
917 if (lxc_safe_uint(value
, &init_uid
) < 0)
920 lxc_conf
->init_uid
= init_uid
;
925 static int set_config_init_gid(const char *key
, const char *value
,
926 struct lxc_conf
*lxc_conf
, void *data
)
928 unsigned int init_gid
;
930 if (lxc_config_value_empty(value
)) {
931 lxc_conf
->init_gid
= 0;
935 if (lxc_safe_uint(value
, &init_gid
) < 0)
938 lxc_conf
->init_gid
= init_gid
;
943 static int set_config_hooks(const char *key
, const char *value
,
944 struct lxc_conf
*lxc_conf
, void *data
)
948 if (lxc_config_value_empty(value
))
949 return lxc_clear_hooks(lxc_conf
, key
);
951 if (strcmp(key
+ 4, "hook") == 0) {
952 ERROR("lxc.hook must not have a value");
956 copy
= strdup(value
);
960 if (strcmp(key
+ 9, "pre-start") == 0)
961 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
962 else if (strcmp(key
+ 9, "start-host") == 0)
963 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
964 else if (strcmp(key
+ 9, "pre-mount") == 0)
965 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
966 else if (strcmp(key
+ 9, "autodev") == 0)
967 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
968 else if (strcmp(key
+ 9, "mount") == 0)
969 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
970 else if (strcmp(key
+ 9, "start") == 0)
971 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
972 else if (strcmp(key
+ 9, "stop") == 0)
973 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
974 else if (strcmp(key
+ 9, "post-stop") == 0)
975 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
976 else if (strcmp(key
+ 9, "clone") == 0)
977 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
978 else if (strcmp(key
+ 9, "destroy") == 0)
979 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
985 static int set_config_hooks_version(const char *key
, const char *value
,
986 struct lxc_conf
*lxc_conf
, void *data
)
991 if (lxc_config_value_empty(value
))
992 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
994 ret
= lxc_safe_uint(value
, &tmp
);
999 ERROR("Invalid hook version specified. Currently only 0 "
1000 "(legacy) and 1 are supported");
1004 lxc_conf
->hooks_version
= tmp
;
1008 static int set_config_personality(const char *key
, const char *value
,
1009 struct lxc_conf
*lxc_conf
, void *data
)
1011 signed long personality
= lxc_config_parse_arch(value
);
1013 if (personality
>= 0)
1014 lxc_conf
->personality
= personality
;
1016 WARN("Unsupported personality \"%s\"", value
);
1021 static int set_config_pty_max(const char *key
, const char *value
,
1022 struct lxc_conf
*lxc_conf
, void *data
)
1024 if (lxc_config_value_empty(value
)) {
1029 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1035 /* We only need to check whether the first byte of the key after the lxc.start.
1036 * prefix matches our expectations since they fortunately all start with a
1037 * different letter. If anything was wrong with the key we would have already
1038 * noticed when the callback was called.
1040 static int set_config_start(const char *key
, const char *value
,
1041 struct lxc_conf
*lxc_conf
, void *data
)
1045 is_empty
= lxc_config_value_empty(value
);
1047 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1049 lxc_conf
->start_auto
= 0;
1053 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1056 if (lxc_conf
->start_auto
> 1)
1060 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1062 lxc_conf
->start_delay
= 0;
1066 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1067 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1069 lxc_conf
->start_order
= 0;
1073 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1079 static int set_config_monitor(const char *key
, const char *value
,
1080 struct lxc_conf
*lxc_conf
, void *data
)
1082 if (lxc_config_value_empty(value
)) {
1083 lxc_conf
->monitor_unshare
= 0;
1087 if (strcmp(key
+ 12, "unshare") == 0)
1088 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1093 static int set_config_group(const char *key
, const char *value
,
1094 struct lxc_conf
*lxc_conf
, void *data
)
1096 char *groups
, *groupptr
, *sptr
, *token
;
1097 struct lxc_list
*grouplist
;
1100 if (lxc_config_value_empty(value
))
1101 return lxc_clear_groups(lxc_conf
);
1103 groups
= strdup(value
);
1107 /* In case several groups are specified in a single line split these
1108 * groups in a single element for the list.
1110 for (groupptr
= groups
;; groupptr
= NULL
) {
1111 token
= strtok_r(groupptr
, " \t", &sptr
);
1117 grouplist
= malloc(sizeof(*grouplist
));
1121 grouplist
->elem
= strdup(token
);
1122 if (!grouplist
->elem
) {
1127 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1134 static int set_config_environment(const char *key
, const char *value
,
1135 struct lxc_conf
*lxc_conf
, void *data
)
1137 struct lxc_list
*list_item
= NULL
;
1139 if (lxc_config_value_empty(value
))
1140 return lxc_clear_environment(lxc_conf
);
1142 list_item
= malloc(sizeof(*list_item
));
1146 list_item
->elem
= strdup(value
);
1148 if (!list_item
->elem
)
1151 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1160 static int set_config_tty_max(const char *key
, const char *value
,
1161 struct lxc_conf
*lxc_conf
, void *data
)
1163 if (lxc_config_value_empty(value
)) {
1168 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1171 static int set_config_tty_dir(const char *key
, const char *value
,
1172 struct lxc_conf
*lxc_conf
, void *data
)
1174 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1178 static int set_config_apparmor_profile(const char *key
, const char *value
,
1179 struct lxc_conf
*lxc_conf
, void *data
)
1181 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1184 static int set_config_apparmor_allow_incomplete(const char *key
,
1186 struct lxc_conf
*lxc_conf
,
1189 if (lxc_config_value_empty(value
)) {
1190 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1194 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1197 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1203 static int set_config_selinux_context(const char *key
, const char *value
,
1204 struct lxc_conf
*lxc_conf
, void *data
)
1206 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1209 static int set_config_log_file(const char *key
, const char *value
,
1210 struct lxc_conf
*c
, void *data
)
1214 if (lxc_config_value_empty(value
)) {
1220 /* Store these values in the lxc_conf, and then try to set for actual
1223 ret
= set_config_path_item(&c
->logfile
, value
);
1225 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1230 static int set_config_log_level(const char *key
, const char *value
,
1231 struct lxc_conf
*lxc_conf
, void *data
)
1235 if (lxc_config_value_empty(value
)) {
1236 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1240 if (value
[0] >= '0' && value
[0] <= '9') {
1241 if (lxc_safe_int(value
, &newlevel
) < 0)
1244 newlevel
= lxc_log_priority_to_int(value
);
1247 /* Store these values in the lxc_conf, and then try to set for actual
1250 lxc_conf
->loglevel
= newlevel
;
1251 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1254 static int set_config_autodev(const char *key
, const char *value
,
1255 struct lxc_conf
*lxc_conf
, void *data
)
1257 if (lxc_config_value_empty(value
)) {
1258 lxc_conf
->autodev
= 0;
1262 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1265 if (lxc_conf
->autodev
> 1)
1271 static int sig_num(const char *sig
)
1273 unsigned int signum
;
1275 if (lxc_safe_uint(sig
, &signum
) < 0)
1281 static int rt_sig_num(const char *signame
)
1283 int rtmax
= 0, sig_n
= 0;
1285 if (strncasecmp(signame
, "max-", 4) == 0) {
1290 if (!isdigit(*signame
))
1293 sig_n
= sig_num(signame
);
1294 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1295 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1301 static int sig_parse(const char *signame
)
1305 if (isdigit(*signame
)) {
1306 return sig_num(signame
);
1307 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1309 if (strncasecmp(signame
, "rt", 2) == 0)
1310 return rt_sig_num(signame
+ 2);
1311 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1312 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1313 return signames
[n
].num
;
1320 static int set_config_signal_halt(const char *key
, const char *value
,
1321 struct lxc_conf
*lxc_conf
, void *data
)
1325 if (lxc_config_value_empty(value
)) {
1326 lxc_conf
->haltsignal
= 0;
1330 sig_n
= sig_parse(value
);
1334 lxc_conf
->haltsignal
= sig_n
;
1339 static int set_config_signal_reboot(const char *key
, const char *value
,
1340 struct lxc_conf
*lxc_conf
, void *data
)
1344 if (lxc_config_value_empty(value
)) {
1345 lxc_conf
->rebootsignal
= 0;
1349 sig_n
= sig_parse(value
);
1353 lxc_conf
->rebootsignal
= sig_n
;
1358 static int set_config_signal_stop(const char *key
, const char *value
,
1359 struct lxc_conf
*lxc_conf
, void *data
)
1363 if (lxc_config_value_empty(value
)) {
1364 lxc_conf
->stopsignal
= 0;
1368 sig_n
= sig_parse(value
);
1372 lxc_conf
->stopsignal
= sig_n
;
1377 static int set_config_cgroup_controller(const char *key
, const char *value
,
1378 struct lxc_conf
*lxc_conf
, void *data
)
1381 char *token
= "lxc.cgroup.";
1382 struct lxc_list
*cglist
= NULL
;
1383 struct lxc_cgroup
*cgelem
= NULL
;
1385 if (lxc_config_value_empty(value
))
1386 return lxc_clear_cgroups(lxc_conf
, key
);
1388 subkey
= strstr(key
, token
);
1392 if (!strlen(subkey
))
1395 if (strlen(subkey
) == strlen(token
))
1398 subkey
+= strlen(token
);
1400 cglist
= malloc(sizeof(*cglist
));
1404 cgelem
= malloc(sizeof(*cgelem
));
1407 memset(cgelem
, 0, sizeof(*cgelem
));
1409 cgelem
->subsystem
= strdup(subkey
);
1410 cgelem
->value
= strdup(value
);
1412 if (!cgelem
->subsystem
|| !cgelem
->value
)
1415 cglist
->elem
= cgelem
;
1417 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1424 free(cgelem
->subsystem
);
1425 free(cgelem
->value
);
1432 static int set_config_cgroup_dir(const char *key
, const char *value
,
1433 struct lxc_conf
*lxc_conf
, void *data
)
1435 if (lxc_config_value_empty(value
))
1436 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1438 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1441 static int set_config_prlimit(const char *key
, const char *value
,
1442 struct lxc_conf
*lxc_conf
, void *data
)
1444 struct lxc_list
*iter
;
1445 struct rlimit limit
;
1447 struct lxc_list
*limlist
= NULL
;
1448 struct lxc_limit
*limelem
= NULL
;
1450 if (lxc_config_value_empty(value
))
1451 return lxc_clear_limits(lxc_conf
, key
);
1453 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1456 key
+= sizeof("lxc.prlimit.") - 1;
1458 /* soft limit comes first in the value */
1459 if (!parse_limit_value(&value
, &limit_value
))
1461 limit
.rlim_cur
= limit_value
;
1463 /* skip spaces and a colon */
1464 while (isspace(*value
))
1469 else if (*value
) /* any other character is an error here */
1472 while (isspace(*value
))
1475 /* optional hard limit */
1477 if (!parse_limit_value(&value
, &limit_value
))
1479 limit
.rlim_max
= limit_value
;
1481 /* check for trailing garbage */
1482 while (isspace(*value
))
1488 /* a single value sets both hard and soft limit */
1489 limit
.rlim_max
= limit
.rlim_cur
;
1492 /* find existing list element */
1493 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1494 limelem
= iter
->elem
;
1495 if (!strcmp(key
, limelem
->resource
)) {
1496 limelem
->limit
= limit
;
1501 /* allocate list element */
1502 limlist
= malloc(sizeof(*limlist
));
1506 limelem
= malloc(sizeof(*limelem
));
1509 memset(limelem
, 0, sizeof(*limelem
));
1511 limelem
->resource
= strdup(key
);
1512 if (!limelem
->resource
)
1514 limelem
->limit
= limit
;
1516 lxc_list_add_elem(limlist
, limelem
);;
1518 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1525 free(limelem
->resource
);
1531 static int set_config_sysctl(const char *key
, const char *value
,
1532 struct lxc_conf
*lxc_conf
, void *data
)
1534 struct lxc_list
*iter
;
1535 char *replace_value
= NULL
;
1536 struct lxc_list
*sysctl_list
= NULL
;
1537 struct lxc_sysctl
*sysctl_elem
= NULL
;
1539 if (lxc_config_value_empty(value
))
1540 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1542 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1545 key
+= sizeof("lxc.sysctl.") - 1;
1547 /* find existing list element */
1548 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1549 sysctl_elem
= iter
->elem
;
1551 if (strcmp(key
, sysctl_elem
->key
) != 0)
1554 replace_value
= strdup(value
);
1558 free(sysctl_elem
->value
);
1559 sysctl_elem
->value
= replace_value
;
1563 /* allocate list element */
1564 sysctl_list
= malloc(sizeof(*sysctl_list
));
1568 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1571 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1573 sysctl_elem
->key
= strdup(key
);
1574 if (!sysctl_elem
->key
)
1577 sysctl_elem
->value
= strdup(value
);
1578 if (!sysctl_elem
->value
)
1581 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1583 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1590 free(sysctl_elem
->key
);
1591 free(sysctl_elem
->value
);
1597 static int set_config_proc(const char *key
, const char *value
,
1598 struct lxc_conf
*lxc_conf
, void *data
)
1601 struct lxc_list
*proclist
= NULL
;
1602 struct lxc_proc
*procelem
= NULL
;
1604 if (lxc_config_value_empty(value
))
1605 return clr_config_proc(key
, lxc_conf
, NULL
);
1607 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1610 subkey
= key
+ sizeof("lxc.proc.") - 1;
1611 if (*subkey
== '\0')
1614 proclist
= malloc(sizeof(*proclist
));
1618 procelem
= malloc(sizeof(*procelem
));
1621 memset(procelem
, 0, sizeof(*procelem
));
1623 procelem
->filename
= strdup(subkey
);
1624 procelem
->value
= strdup(value
);
1626 if (!procelem
->filename
|| !procelem
->value
)
1629 proclist
->elem
= procelem
;
1631 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1638 free(procelem
->filename
);
1639 free(procelem
->value
);
1646 static int set_config_idmaps(const char *key
, const char *value
,
1647 struct lxc_conf
*lxc_conf
, void *data
)
1649 unsigned long hostid
, nsid
, range
;
1652 struct lxc_list
*idmaplist
= NULL
;
1653 struct id_map
*idmap
= NULL
;
1655 if (lxc_config_value_empty(value
))
1656 return lxc_clear_idmaps(lxc_conf
);
1658 idmaplist
= malloc(sizeof(*idmaplist
));
1662 idmap
= malloc(sizeof(*idmap
));
1665 memset(idmap
, 0, sizeof(*idmap
));
1667 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1669 ERROR("Failed to parse id mappings");
1673 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1675 idmap
->idtype
= ID_TYPE_UID
;
1676 else if (type
== 'g')
1677 idmap
->idtype
= ID_TYPE_GID
;
1681 idmap
->hostid
= hostid
;
1683 idmap
->range
= range
;
1684 idmaplist
->elem
= idmap
;
1685 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1687 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1688 if (idmap
->nsid
== 0)
1689 lxc_conf
->root_nsuid_map
= idmap
;
1692 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1693 if (idmap
->nsid
== 0)
1694 lxc_conf
->root_nsgid_map
= idmap
;
1707 static int set_config_mount_fstab(const char *key
, const char *value
,
1708 struct lxc_conf
*lxc_conf
, void *data
)
1710 if (lxc_config_value_empty(value
)) {
1711 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1715 return set_config_path_item(&lxc_conf
->fstab
, value
);
1718 static int set_config_mount_auto(const char *key
, const char *value
,
1719 struct lxc_conf
*lxc_conf
, void *data
)
1721 char *autos
, *autoptr
, *sptr
, *token
;
1728 } allowed_auto_mounts
[] = {
1729 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1730 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1731 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1732 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1733 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1734 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1735 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1736 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1737 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1738 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1739 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1740 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1741 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1742 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1743 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1744 /* For adding anything that is just a single on/off, but has no
1745 * options: keep mask and flag identical and just define the enum
1746 * value as an unused bit so far
1751 if (lxc_config_value_empty(value
)) {
1752 lxc_conf
->auto_mounts
= 0;
1756 autos
= strdup(value
);
1760 for (autoptr
= autos
;; autoptr
= NULL
) {
1761 token
= strtok_r(autoptr
, " \t", &sptr
);
1767 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1768 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1772 if (!allowed_auto_mounts
[i
].token
) {
1773 ERROR("Invalid filesystem to automount \"%s\"", token
);
1777 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1778 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1785 static int set_config_mount(const char *key
, const char *value
,
1786 struct lxc_conf
*lxc_conf
, void *data
)
1789 struct lxc_list
*mntlist
;
1791 if (lxc_config_value_empty(value
))
1792 return lxc_clear_mount_entries(lxc_conf
);
1794 mntlist
= malloc(sizeof(*mntlist
));
1798 mntelem
= strdup(value
);
1803 mntlist
->elem
= mntelem
;
1805 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1810 static int set_config_cap_keep(const char *key
, const char *value
,
1811 struct lxc_conf
*lxc_conf
, void *data
)
1813 char *keepcaps
, *keepptr
, *sptr
, *token
;
1814 struct lxc_list
*keeplist
;
1817 if (lxc_config_value_empty(value
))
1818 return lxc_clear_config_keepcaps(lxc_conf
);
1820 keepcaps
= strdup(value
);
1824 /* In case several capability keep is specified in a single line
1825 * split these caps in a single element for the list.
1827 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1828 token
= strtok_r(keepptr
, " \t", &sptr
);
1834 if (!strcmp(token
, "none"))
1835 lxc_clear_config_keepcaps(lxc_conf
);
1837 keeplist
= malloc(sizeof(*keeplist
));
1841 keeplist
->elem
= strdup(token
);
1842 if (!keeplist
->elem
) {
1847 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1855 static int set_config_cap_drop(const char *key
, const char *value
,
1856 struct lxc_conf
*lxc_conf
, void *data
)
1858 char *dropcaps
, *dropptr
, *sptr
, *token
;
1859 struct lxc_list
*droplist
;
1862 if (lxc_config_value_empty(value
))
1863 return lxc_clear_config_caps(lxc_conf
);
1865 dropcaps
= strdup(value
);
1869 /* In case several capability drop is specified in a single line
1870 * split these caps in a single element for the list.
1872 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1873 token
= strtok_r(dropptr
, " \t", &sptr
);
1879 droplist
= malloc(sizeof(*droplist
));
1883 droplist
->elem
= strdup(token
);
1884 if (!droplist
->elem
) {
1889 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1897 static int set_config_console_path(const char *key
, const char *value
,
1898 struct lxc_conf
*lxc_conf
, void *data
)
1900 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1903 static int set_config_console_rotate(const char *key
, const char *value
,
1904 struct lxc_conf
*lxc_conf
, void *data
)
1906 if (lxc_config_value_empty(value
)) {
1907 lxc_conf
->console
.log_rotate
= 0;
1911 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1914 if (lxc_conf
->console
.log_rotate
> 1) {
1915 ERROR("The \"lxc.console.rotate\" config key can only be set "
1923 static int set_config_console_logfile(const char *key
, const char *value
,
1924 struct lxc_conf
*lxc_conf
, void *data
)
1926 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1929 static int set_config_console_buffer_size(const char *key
, const char *value
,
1930 struct lxc_conf
*lxc_conf
, void *data
)
1934 uint64_t buffer_size
, pgsz
;
1936 if (lxc_config_value_empty(value
)) {
1937 lxc_conf
->console
.buffer_size
= 0;
1941 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1942 if (!strcmp(value
, "auto")) {
1943 lxc_conf
->console
.buffer_size
= 1 << 17;
1947 ret
= parse_byte_size_string(value
, &size
);
1954 /* must be at least a page size */
1955 pgsz
= lxc_getpagesize();
1956 if ((uint64_t)size
< pgsz
) {
1957 NOTICE("Requested ringbuffer size for the console is %" PRId64
1958 " but must be at least %" PRId64
1959 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1964 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1965 if (buffer_size
== 0)
1968 if (buffer_size
!= size
)
1969 NOTICE("Passed size was not a power of 2. Rounding log size to "
1970 "next power of two: %" PRIu64
" bytes", buffer_size
);
1972 lxc_conf
->console
.buffer_size
= buffer_size
;
1976 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
1977 struct lxc_conf
*lxc_conf
,
1980 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
1983 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
1985 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
1987 update_hwaddr(line
);
1989 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
1990 char *tmp
= realloc(conf
->unexpanded_config
,
1991 conf
->unexpanded_alloced
+ 1024);
1995 if (!conf
->unexpanded_config
)
1997 conf
->unexpanded_config
= tmp
;
1998 conf
->unexpanded_alloced
+= 1024;
2000 strcat(conf
->unexpanded_config
, line
);
2001 conf
->unexpanded_len
+= linelen
;
2002 if (line
[linelen
- 1] != '\n') {
2003 strcat(conf
->unexpanded_config
, "\n");
2004 conf
->unexpanded_len
++;
2010 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2012 struct dirent
*direntp
;
2014 char path
[MAXPATHLEN
];
2018 dir
= opendir(dirp
);
2022 while ((direntp
= readdir(dir
))) {
2027 fnam
= direntp
->d_name
;
2028 if (!strcmp(fnam
, "."))
2031 if (!strcmp(fnam
, ".."))
2035 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2038 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2039 if (len
< 0 || len
>= MAXPATHLEN
) {
2044 ret
= lxc_config_read(path
, lxc_conf
, true);
2056 static int set_config_includefiles(const char *key
, const char *value
,
2057 struct lxc_conf
*lxc_conf
, void *data
)
2059 if (lxc_config_value_empty(value
)) {
2060 clr_config_includefiles(key
, lxc_conf
, NULL
);
2065 return do_includedir(value
, lxc_conf
);
2067 return lxc_config_read(value
, lxc_conf
, true);
2070 static int set_config_rootfs_path(const char *key
, const char *value
,
2071 struct lxc_conf
*lxc_conf
, void *data
)
2075 const char *container_path
;
2077 if (lxc_config_value_empty(value
)) {
2078 free(lxc_conf
->rootfs
.path
);
2079 lxc_conf
->rootfs
.path
= NULL
;
2083 dup
= strdup(value
);
2087 /* Split <storage type>:<container path> into <storage type> and
2088 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2089 * "rootfs.path" to <container path>.
2091 tmp
= strchr(dup
, ':');
2094 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2100 container_path
= tmp
;
2102 container_path
= value
;
2105 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2110 static int set_config_rootfs_mount(const char *key
, const char *value
,
2111 struct lxc_conf
*lxc_conf
, void *data
)
2113 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2116 static int set_config_rootfs_options(const char *key
, const char *value
,
2117 struct lxc_conf
*lxc_conf
, void *data
)
2119 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2122 static int set_config_rootfs_backend(const char *key
, const char *value
,
2123 struct lxc_conf
*lxc_conf
, void *data
)
2128 static int set_config_uts_name(const char *key
, const char *value
,
2129 struct lxc_conf
*lxc_conf
, void *data
)
2131 struct utsname
*utsname
;
2133 if (lxc_config_value_empty(value
)) {
2134 clr_config_uts_name(key
, lxc_conf
, NULL
);
2138 utsname
= malloc(sizeof(*utsname
));
2142 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2147 strcpy(utsname
->nodename
, value
);
2148 free(lxc_conf
->utsname
);
2149 lxc_conf
->utsname
= utsname
;
2154 static int set_config_namespace(const char *key
, const char *value
,
2155 struct lxc_conf
*lxc_conf
, void *data
)
2158 const char *namespace;
2160 if (lxc_config_value_empty(value
))
2161 return clr_config_namespace(key
, lxc_conf
, data
);
2163 namespace = key
+ sizeof("lxc.namespace.") - 1;
2164 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2168 return set_config_string_item(&lxc_conf
->inherit_ns
[ns_idx
], value
);
2171 struct parse_line_conf
{
2172 struct lxc_conf
*conf
;
2176 static int parse_line(char *buffer
, void *data
)
2178 char *dot
, *key
, *line
, *linep
, *value
;
2180 struct lxc_config_t
*config
;
2183 struct parse_line_conf
*plc
= data
;
2185 /* If there are newlines in the config file we should keep them. */
2186 empty_line
= lxc_is_line_empty(dup
);
2190 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2191 * modified the original string on the stack by replacing '=' by '\0'
2194 linep
= line
= strdup(dup
);
2198 if (!plc
->from_include
) {
2199 ret
= append_unexp_config_line(line
, plc
->conf
);
2207 line
+= lxc_char_left_gc(line
, strlen(line
));
2209 /* ignore comments */
2213 /* martian option - don't add it to the config itself */
2214 if (strncmp(line
, "lxc.", 4))
2219 dot
= strchr(line
, '=');
2221 ERROR("Invalid configuration line: %s", line
);
2229 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2231 value
+= lxc_char_left_gc(value
, strlen(value
));
2232 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2234 if (*value
== '\'' || *value
== '\"') {
2237 len
= strlen(value
);
2238 if (len
> 1 && value
[len
- 1] == *value
) {
2239 value
[len
- 1] = '\0';
2244 config
= lxc_get_config(key
);
2246 ERROR("Unknown configuration key \"%s\"", key
);
2250 /* [START]: REMOVE IN LXC 3.0 */
2251 if (config
->is_legacy_key
&& !plc
->conf
->contains_legacy_key
) {
2252 plc
->conf
->contains_legacy_key
= true;
2253 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2254 /* Warn the user once loud and clear that there is at
2255 * least one legacy configuration item in the
2256 * configuration file and then an update is required.
2258 fprintf(stderr
, "The configuration file contains "
2259 "legacy configuration keys.\nPlease "
2260 "update your configuration file!\nThe "
2261 "update script lxc-update-config -c "
2262 "<path-to-config> can be used for "
2266 /* [END]: REMOVE IN LXC 3.0 */
2268 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2275 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2277 struct parse_line_conf c
;
2280 c
.from_include
= false;
2282 return parse_line(buffer
, &c
);
2285 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2288 struct parse_line_conf c
;
2291 c
.from_include
= from_include
;
2293 ret
= access(file
, R_OK
);
2297 /* Catch only the top level config file name in the structure. */
2299 conf
->rcfile
= strdup(file
);
2301 return lxc_file_for_each_line(file
, parse_line
, &c
);
2304 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2306 struct lxc_list
*dent
;
2308 dent
= malloc(sizeof(struct lxc_list
));
2313 lxc_list_add_tail(defines
, dent
);
2317 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2319 struct lxc_list
*it
, *next
;
2322 lxc_list_for_each(it
, defines
) {
2323 ret
= lxc_config_readline(it
->elem
, conf
);
2328 lxc_list_for_each_safe(it
, defines
, next
) {
2336 signed long lxc_config_parse_arch(const char *arch
)
2338 #if HAVE_SYS_PERSONALITY_H
2344 { "x86", PER_LINUX32
},
2345 { "linux32", PER_LINUX32
},
2346 { "i386", PER_LINUX32
},
2347 { "i486", PER_LINUX32
},
2348 { "i586", PER_LINUX32
},
2349 { "i686", PER_LINUX32
},
2350 { "athlon", PER_LINUX32
},
2351 { "mips", PER_LINUX32
},
2352 { "mipsel", PER_LINUX32
},
2353 { "ppc", PER_LINUX32
},
2354 { "arm", PER_LINUX32
},
2355 { "armv7l", PER_LINUX32
},
2356 { "armhf", PER_LINUX32
},
2357 { "armel", PER_LINUX32
},
2358 { "powerpc", PER_LINUX32
},
2359 { "linux64", PER_LINUX
},
2360 { "x86_64", PER_LINUX
},
2361 { "amd64", PER_LINUX
},
2362 { "mips64", PER_LINUX
},
2363 { "mips64el", PER_LINUX
},
2364 { "ppc64", PER_LINUX
},
2365 { "ppc64le", PER_LINUX
},
2366 { "ppc64el", PER_LINUX
},
2367 { "powerpc64", PER_LINUX
},
2368 { "s390x", PER_LINUX
},
2369 { "aarch64", PER_LINUX
},
2370 { "arm64", PER_LINUX
},
2372 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2374 for (i
= 0; i
< len
; i
++) {
2375 if (!strcmp(pername
[i
].name
, arch
))
2376 return pername
[i
].per
;
2383 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2385 char *token
, *saveptr
= NULL
;
2391 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2392 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2393 { "LSM", LXC_ATTACH_LSM_EXEC
},
2398 /* For the sake of backward compatibility, drop all privileges
2399 * if none is specified.
2401 for (i
= 0; all_privs
[i
].token
; i
++)
2402 *flags
|= all_privs
[i
].flag
;
2407 token
= strtok_r(flaglist
, "|", &saveptr
);
2410 for (i
= 0; all_privs
[i
].token
; i
++)
2411 if (!strcmp(all_privs
[i
].token
, token
))
2412 aflag
= all_privs
[i
].flag
;
2418 token
= strtok_r(NULL
, "|", &saveptr
);
2424 /* Write out a configuration file. */
2425 void write_config(FILE *fout
, struct lxc_conf
*c
)
2428 size_t len
= c
->unexpanded_len
;
2433 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2435 SYSERROR("Failed to write configuration file");
2438 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2445 len
= strlen(key
) + strlen(v
) + 4;
2448 if (lxc_config_value_empty(v
))
2449 ret
= snprintf(tmp
, len
, "%s =", key
);
2451 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2452 if (ret
< 0 || ret
>= len
)
2455 /* Save the line verbatim into unexpanded_conf */
2456 if (append_unexp_config_line(tmp
, conf
))
2462 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2466 char *lstart
= conf
->unexpanded_config
;
2468 if (!conf
->unexpanded_config
)
2472 lend
= strchr(lstart
, '\n');
2475 lend
= lstart
+ strlen(lstart
);
2478 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2483 v
= lstart
[strlen(key
)];
2484 if (!isspace(v
) && v
!= '=') {
2489 conf
->unexpanded_len
-= (lend
- lstart
);
2490 if (*lend
== '\0') {
2494 memmove(lstart
, lend
, strlen(lend
) + 1);
2498 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2499 const char *newpath
, const char *oldname
,
2500 const char *newname
, const char *ovldir
)
2503 char *lend
, *newdir
, *olddir
, *p
, *q
;
2504 size_t newdirlen
, olddirlen
;
2505 char *lstart
= conf
->unexpanded_config
;
2506 const char *key
= "lxc.mount.entry";
2508 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2509 olddir
= alloca(olddirlen
+ 1);
2510 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2512 if (ret
< 0 || ret
>= olddirlen
+ 1)
2515 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2516 newdir
= alloca(newdirlen
+ 1);
2517 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2519 if (ret
< 0 || ret
>= newdirlen
+ 1)
2522 if (!conf
->unexpanded_config
)
2526 lend
= strchr(lstart
, '\n');
2528 lend
= lstart
+ strlen(lstart
);
2532 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2535 p
= strchr(lstart
+ strlen(key
), '=');
2546 /* Whenever an lxc.mount.entry entry is found in a line we check
2547 * if the substring " overlay" or the substring " aufs" is
2548 * present before doing any further work. We check for "
2549 * overlay" and " aufs" since both substrings need to have at
2550 * least one space before them in a valid overlay
2551 * lxc.mount.entry (/A B overlay). When the space before is
2552 * missing it is very likely that these substrings are part of a
2553 * path or something else. (Checking q >= lend ensures that we
2554 * only count matches in the current line.) */
2555 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2556 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2559 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2562 /* replace the olddir with newdir */
2563 if (olddirlen
>= newdirlen
) {
2564 size_t diff
= olddirlen
- newdirlen
;
2565 memcpy(q
, newdir
, newdirlen
);
2566 if (olddirlen
!= newdirlen
) {
2567 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2568 strlen(q
) - newdirlen
- diff
+ 1);
2570 conf
->unexpanded_len
-= diff
;
2574 size_t diff
= newdirlen
- olddirlen
;
2575 size_t oldlen
= conf
->unexpanded_len
;
2576 size_t newlen
= oldlen
+ diff
;
2577 size_t poffset
= q
- conf
->unexpanded_config
;
2579 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2583 conf
->unexpanded_len
= newlen
;
2584 conf
->unexpanded_alloced
= newlen
+ 1;
2585 new[newlen
- 1] = '\0';
2586 lend
= new + (lend
- conf
->unexpanded_config
);
2587 /* Move over the remainder to make room for the newdir.
2589 memmove(new + poffset
+ newdirlen
,
2590 new + poffset
+ olddirlen
,
2591 oldlen
- poffset
- olddirlen
+ 1);
2592 conf
->unexpanded_config
= new;
2593 memcpy(new + poffset
, newdir
, newdirlen
);
2603 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2604 const char *newpath
, const char *oldname
,
2605 const char *newname
)
2608 char *lend
, *newdir
, *olddir
, *p
;
2609 char *lstart
= conf
->unexpanded_config
;
2610 size_t newdirlen
, olddirlen
;
2611 const char *key
= "lxc.hook";
2613 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2614 olddir
= alloca(olddirlen
+ 1);
2615 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2616 if (ret
< 0 || ret
>= olddirlen
+ 1)
2619 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2620 newdir
= alloca(newdirlen
+ 1);
2621 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2622 if (ret
< 0 || ret
>= newdirlen
+ 1)
2625 if (!conf
->unexpanded_config
)
2629 lend
= strchr(lstart
, '\n');
2631 lend
= lstart
+ strlen(lstart
);
2635 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2638 p
= strchr(lstart
+ strlen(key
), '=');
2649 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2652 /* replace the olddir with newdir */
2653 if (olddirlen
>= newdirlen
) {
2654 size_t diff
= olddirlen
- newdirlen
;
2655 memcpy(p
, newdir
, newdirlen
);
2656 if (olddirlen
!= newdirlen
) {
2657 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2658 strlen(p
) - newdirlen
- diff
+ 1);
2660 conf
->unexpanded_len
-= diff
;
2664 size_t diff
= newdirlen
- olddirlen
;
2665 size_t oldlen
= conf
->unexpanded_len
;
2666 size_t newlen
= oldlen
+ diff
;
2667 size_t poffset
= p
- conf
->unexpanded_config
;
2669 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2673 conf
->unexpanded_len
= newlen
;
2674 conf
->unexpanded_alloced
= newlen
+ 1;
2675 new[newlen
- 1] = '\0';
2676 lend
= new + (lend
- conf
->unexpanded_config
);
2677 /* Move over the remainder to make room for the newdir.
2679 memmove(new + poffset
+ newdirlen
,
2680 new + poffset
+ olddirlen
,
2681 oldlen
- poffset
- olddirlen
+ 1);
2682 conf
->unexpanded_config
= new;
2683 memcpy(new + poffset
, newdir
, newdirlen
);
2696 ERROR("Error writing to new config"); \
2701 /* This is called only from clone. We wish to update all hwaddrs in the
2702 * unexpanded config file. We can't/don't want to update any which come from
2703 * lxc.includes (there shouldn't be any).
2704 * We can't just walk the c->lxc-conf->network list because that includes netifs
2705 * from the include files. So we update the ones which we find in the unexp
2706 * config file, then find the original macaddr in the conf->network, and update
2707 * that to the same value.
2709 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2711 char *lend
, *p
, *p2
;
2712 struct lxc_list
*it
;
2713 char *lstart
= conf
->unexpanded_config
;
2715 if (!conf
->unexpanded_config
)
2719 char newhwaddr
[18], oldhwaddr
[17];
2721 lend
= strchr(lstart
, '\n');
2723 lend
= lstart
+ strlen(lstart
);
2727 if (!lxc_config_net_hwaddr(lstart
)) {
2732 p
= strchr(lstart
, '=');
2745 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2748 if ((p2
- p
) != 17) {
2749 WARN("Bad hwaddr entry");
2754 memcpy(oldhwaddr
, p
, 17);
2756 if (!new_hwaddr(newhwaddr
))
2759 memcpy(p
, newhwaddr
, 17);
2760 lxc_list_for_each(it
, &conf
->network
) {
2761 struct lxc_netdev
*n
= it
->elem
;
2763 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2764 memcpy(n
->hwaddr
, newhwaddr
, 17);
2773 static int set_config_ephemeral(const char *key
, const char *value
,
2774 struct lxc_conf
*lxc_conf
, void *data
)
2776 if (lxc_config_value_empty(value
)) {
2777 lxc_conf
->ephemeral
= 0;
2781 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2784 if (lxc_conf
->ephemeral
> 1)
2790 static int set_config_log_syslog(const char *key
, const char *value
,
2791 struct lxc_conf
*lxc_conf
, void *data
)
2795 if (lxc_conf
->syslog
) {
2796 free(lxc_conf
->syslog
);
2797 lxc_conf
->syslog
= NULL
;
2800 if (lxc_config_value_empty(value
))
2803 facility
= lxc_syslog_priority_to_int(value
);
2804 if (facility
== -EINVAL
)
2807 lxc_log_syslog(facility
);
2808 return set_config_string_item(&lxc_conf
->syslog
, value
);
2811 static int set_config_no_new_privs(const char *key
, const char *value
,
2812 struct lxc_conf
*lxc_conf
, void *data
)
2816 if (lxc_config_value_empty(value
)) {
2817 lxc_conf
->no_new_privs
= false;
2821 if (lxc_safe_uint(value
, &v
) < 0)
2827 lxc_conf
->no_new_privs
= v
? true : false;
2832 static int set_config_noop(const char *key
, const char *value
,
2833 struct lxc_conf
*lxc_conf
, void *data
)
2838 /* Callbacks to get configuration items. */
2839 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2840 struct lxc_conf
*c
, void *data
)
2847 memset(retv
, 0, inlen
);
2849 #if HAVE_SYS_PERSONALITY_H
2852 switch (c
->personality
) {
2854 strprint(retv
, inlen
, "i686");
2857 strprint(retv
, inlen
, "x86_64");
2867 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2868 struct lxc_conf
*c
, void *data
)
2870 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2873 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2874 struct lxc_conf
*c
, void *data
)
2876 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2879 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2880 struct lxc_conf
*c
, void *data
)
2882 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2885 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2886 struct lxc_conf
*c
, void *data
)
2888 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2891 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2892 int inlen
, struct lxc_conf
*c
,
2895 return lxc_get_conf_int(c
, retv
, inlen
,
2896 c
->lsm_aa_allow_incomplete
);
2899 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2900 struct lxc_conf
*c
, void *data
)
2902 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2905 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2906 * just the value(s) will be printed. Since there still could be more than one,
2907 * it is newline-separated.
2908 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2910 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2911 * 'lxc.cgroup.subsystem.key = value' format.
2913 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
2914 struct lxc_conf
*c
, void *data
)
2916 struct lxc_list
*it
;
2919 bool get_all
= false;
2924 memset(retv
, 0, inlen
);
2926 if (!strcmp(key
, "lxc.cgroup"))
2928 else if (!strncmp(key
, "lxc.cgroup.", 11))
2933 lxc_list_for_each(it
, &c
->cgroup
) {
2934 struct lxc_cgroup
*cg
= it
->elem
;
2937 strprint(retv
, inlen
, "lxc.cgroup.%s = %s\n",
2938 cg
->subsystem
, cg
->value
);
2939 } else if (!strcmp(cg
->subsystem
, key
)) {
2940 strprint(retv
, inlen
, "%s\n", cg
->value
);
2947 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
2948 struct lxc_conf
*lxc_conf
, void *data
)
2956 memset(retv
, 0, inlen
);
2958 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
2963 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
2964 struct lxc_conf
*c
, void *data
)
2966 struct lxc_list
*it
;
2967 int len
, listlen
, ret
;
2969 /* "u 1000 1000000 65536"
2971 * let's render this as
2989 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
2990 char buf
[__LXC_IDMAP_STR_BUF
];
2995 memset(retv
, 0, inlen
);
2997 listlen
= lxc_list_len(&c
->id_map
);
2998 lxc_list_for_each(it
, &c
->id_map
)
3000 struct id_map
*map
= it
->elem
;
3001 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3002 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3003 map
->nsid
, map
->hostid
, map
->range
);
3004 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3007 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3012 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3013 struct lxc_conf
*c
, void *data
)
3016 v
= lxc_log_priority_to_string(c
->loglevel
);
3017 return lxc_get_conf_str(retv
, inlen
, v
);
3020 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3021 struct lxc_conf
*c
, void *data
)
3023 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3026 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3027 struct lxc_conf
*c
, void *data
)
3029 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3032 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3033 struct lxc_conf
*c
, void *data
)
3035 int len
, fulllen
= 0;
3036 const char *sep
= "";
3041 memset(retv
, 0, inlen
);
3043 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3046 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3047 case LXC_AUTO_PROC_MIXED
:
3048 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3051 case LXC_AUTO_PROC_RW
:
3052 strprint(retv
, inlen
, "%sproc:rw", sep
);
3059 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3060 case LXC_AUTO_SYS_RO
:
3061 strprint(retv
, inlen
, "%ssys:ro", sep
);
3064 case LXC_AUTO_SYS_RW
:
3065 strprint(retv
, inlen
, "%ssys:rw", sep
);
3068 case LXC_AUTO_SYS_MIXED
:
3069 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3076 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3077 case LXC_AUTO_CGROUP_NOSPEC
:
3078 strprint(retv
, inlen
, "%scgroup", sep
);
3080 case LXC_AUTO_CGROUP_MIXED
:
3081 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3083 case LXC_AUTO_CGROUP_RO
:
3084 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3086 case LXC_AUTO_CGROUP_RW
:
3087 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3089 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3090 strprint(retv
, inlen
, "%scgroup-full", sep
);
3092 case LXC_AUTO_CGROUP_FULL_MIXED
:
3093 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3095 case LXC_AUTO_CGROUP_FULL_RO
:
3096 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3098 case LXC_AUTO_CGROUP_FULL_RW
:
3099 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3108 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3109 struct lxc_conf
*c
, void *data
)
3111 int len
, fulllen
= 0;
3112 struct lxc_list
*it
;
3117 memset(retv
, 0, inlen
);
3119 lxc_list_for_each(it
, &c
->mount_list
) {
3120 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3126 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3127 struct lxc_conf
*c
, void *data
)
3129 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3132 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3133 struct lxc_conf
*c
, void *data
)
3135 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3138 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3139 struct lxc_conf
*c
, void *data
)
3141 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3144 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3145 struct lxc_conf
*c
, void *data
)
3150 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3151 struct lxc_conf
*c
, void *data
)
3153 return lxc_get_conf_str(
3155 c
->utsname
? c
->utsname
->nodename
: NULL
);
3158 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3159 struct lxc_conf
*c
, void *data
)
3162 int len
, fulllen
= 0, found
= -1;
3163 struct lxc_list
*it
;
3166 subkey
= strchr(key
, '.');
3168 subkey
= strchr(subkey
+ 1, '.');
3174 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3175 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3186 memset(retv
, 0, inlen
);
3188 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3189 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3194 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3195 struct lxc_conf
*c
, void *data
)
3197 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3200 static int get_config_net(const char *key
, char *retv
, int inlen
,
3201 struct lxc_conf
*c
, void *data
)
3203 int len
, fulllen
= 0;
3204 struct lxc_list
*it
;
3209 memset(retv
, 0, inlen
);
3211 lxc_list_for_each(it
, &c
->network
) {
3212 struct lxc_netdev
*n
= it
->elem
;
3213 const char *t
= lxc_net_type_to_str(n
->type
);
3214 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3220 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3221 struct lxc_conf
*c
, void *data
)
3223 int len
, fulllen
= 0;
3224 struct lxc_list
*it
;
3229 memset(retv
, 0, inlen
);
3231 lxc_list_for_each(it
, &c
->caps
) {
3232 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3238 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3239 struct lxc_conf
*c
, void *data
)
3241 int len
, fulllen
= 0;
3242 struct lxc_list
*it
;
3247 memset(retv
, 0, inlen
);
3249 lxc_list_for_each(it
, &c
->keepcaps
) {
3250 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3256 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3257 struct lxc_conf
*c
, void *data
)
3259 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3262 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3263 struct lxc_conf
*c
, void *data
)
3265 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3268 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3269 struct lxc_conf
*c
, void *data
)
3271 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3275 static int get_config_console_buffer_size(const char *key
, char *retv
,
3276 int inlen
, struct lxc_conf
*c
,
3279 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3282 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3283 int inlen
, struct lxc_conf
*c
,
3286 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3289 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3290 struct lxc_conf
*c
, void *data
)
3292 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3295 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3296 struct lxc_conf
*c
, void *data
)
3298 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3301 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3302 struct lxc_conf
*c
, void *data
)
3304 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3307 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3308 struct lxc_conf
*c
, void *data
)
3310 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3313 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3314 struct lxc_conf
*c
, void *data
)
3316 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3319 static int get_config_start(const char *key
, char *retv
, int inlen
,
3320 struct lxc_conf
*c
, void *data
)
3322 if (strcmp(key
+ 10, "auto") == 0)
3323 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3324 else if (strcmp(key
+ 10, "delay") == 0)
3325 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3326 else if (strcmp(key
+ 10, "order") == 0)
3327 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3332 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3333 struct lxc_conf
*c
, void *data
)
3335 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3338 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3339 struct lxc_conf
*c
, void *data
)
3341 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3344 static int get_config_group(const char *key
, char *retv
, int inlen
,
3345 struct lxc_conf
*c
, void *data
)
3347 int len
, fulllen
= 0;
3348 struct lxc_list
*it
;
3353 memset(retv
, 0, inlen
);
3355 lxc_list_for_each(it
, &c
->groups
) {
3356 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3362 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3363 struct lxc_conf
*c
, void *data
)
3365 int len
, fulllen
= 0;
3366 struct lxc_list
*it
;
3371 memset(retv
, 0, inlen
);
3373 lxc_list_for_each(it
, &c
->environment
) {
3374 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3380 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3381 struct lxc_conf
*c
, void *data
)
3383 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3386 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3387 struct lxc_conf
*c
, void *data
)
3389 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3392 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3393 struct lxc_conf
*c
, void *data
)
3395 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3398 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3399 struct lxc_conf
*c
, void *data
)
3401 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3404 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3405 struct lxc_conf
*c
, void *data
)
3407 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3410 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3411 struct lxc_conf
*c
, void *data
)
3413 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3416 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3417 struct lxc_conf
*c
, void *data
)
3419 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3422 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3423 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3424 * printed, in 'lxc.prlimit.resource = value' format.
3426 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3427 struct lxc_conf
*c
, void *data
)
3429 int fulllen
= 0, len
;
3430 bool get_all
= false;
3431 struct lxc_list
*it
;
3436 memset(retv
, 0, inlen
);
3438 if (!strcmp(key
, "lxc.prlimit"))
3440 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3445 lxc_list_for_each(it
, &c
->limits
) {
3446 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3447 integers or the word
3450 struct lxc_limit
*lim
= it
->elem
;
3452 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3453 memcpy(buf
, "unlimited", sizeof("unlimited"));
3454 partlen
= sizeof("unlimited") - 1;
3456 partlen
= sprintf(buf
, "%" PRIu64
,
3457 (uint64_t)lim
->limit
.rlim_cur
);
3459 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3460 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3461 memcpy(buf
+ partlen
, ":unlimited",
3462 sizeof(":unlimited"));
3464 sprintf(buf
+ partlen
, ":%" PRIu64
,
3465 (uint64_t)lim
->limit
.rlim_max
);
3469 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3470 lim
->resource
, buf
);
3471 } else if (!strcmp(lim
->resource
, key
)) {
3472 strprint(retv
, inlen
, "%s", buf
);
3479 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3480 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3481 * entries will be printed, in 'lxc.sysctl.key = value' format.
3483 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3484 struct lxc_conf
*c
, void *data
)
3487 struct lxc_list
*it
;
3489 bool get_all
= false;
3494 memset(retv
, 0, inlen
);
3496 if (strcmp(key
, "lxc.sysctl") == 0)
3498 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3499 key
+= sizeof("lxc.sysctl.") - 1;
3503 lxc_list_for_each(it
, &c
->sysctls
) {
3504 struct lxc_sysctl
*elem
= it
->elem
;
3506 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3507 elem
->key
, elem
->value
);
3508 } else if (strcmp(elem
->key
, key
) == 0) {
3509 strprint(retv
, inlen
, "%s", elem
->value
);
3516 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3517 struct lxc_conf
*c
, void *data
)
3519 struct lxc_list
*it
;
3522 bool get_all
= false;
3527 memset(retv
, 0, inlen
);
3529 if (strcmp(key
, "lxc.proc") == 0)
3531 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3532 key
+= sizeof("lxc.proc.") - 1;
3536 lxc_list_for_each(it
, &c
->procs
) {
3537 struct lxc_proc
*proc
= it
->elem
;
3540 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3541 proc
->filename
, proc
->value
);
3542 } else if (strcmp(proc
->filename
, key
) == 0) {
3543 strprint(retv
, inlen
, "%s", proc
->value
);
3550 static int get_config_noop(const char *key
, char *retv
, int inlen
,
3551 struct lxc_conf
*c
, void *data
)
3556 static int get_config_namespace(const char *key
, char *retv
, int inlen
,
3557 struct lxc_conf
*c
, void *data
)
3560 const char *namespace;
3566 memset(retv
, 0, inlen
);
3568 namespace = key
+ sizeof("lxc.namespace.") - 1;
3569 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3573 strprint(retv
, inlen
, "%s", c
->inherit_ns
[ns_idx
]);
3578 /* Callbacks to clear config items. */
3579 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3582 c
->personality
= -1;
3586 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3593 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3600 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3608 static inline int clr_config_apparmor_profile(const char *key
,
3609 struct lxc_conf
*c
, void *data
)
3611 free(c
->lsm_aa_profile
);
3612 c
->lsm_aa_profile
= NULL
;
3616 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3620 c
->lsm_aa_allow_incomplete
= 0;
3624 static inline int clr_config_selinux_context(const char *key
,
3625 struct lxc_conf
*c
, void *data
)
3627 free(c
->lsm_se_context
);
3628 c
->lsm_se_context
= NULL
;
3632 static inline int clr_config_cgroup_controller(const char *key
,
3633 struct lxc_conf
*c
, void *data
)
3635 return lxc_clear_cgroups(c
, key
);
3638 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3641 if (lxc_conf
->cgroup_meta
.dir
) {
3642 free(lxc_conf
->cgroup_meta
.dir
);
3643 lxc_conf
->cgroup_meta
.dir
= NULL
;
3649 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3652 return lxc_clear_idmaps(c
);
3655 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3658 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3662 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3670 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3673 return lxc_clear_mount_entries(c
);
3676 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3679 return lxc_clear_automounts(c
);
3682 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3690 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3693 free(c
->rootfs
.path
);
3694 c
->rootfs
.path
= NULL
;
3698 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3701 free(c
->rootfs
.mount
);
3702 c
->rootfs
.mount
= NULL
;
3706 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3709 free(c
->rootfs
.options
);
3710 c
->rootfs
.options
= NULL
;
3714 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3720 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3728 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3731 return lxc_clear_hooks(c
, key
);
3734 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
3737 /* default to legacy hooks version */
3738 c
->hooks_version
= 0;
3742 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3745 lxc_free_networks(&c
->network
);
3750 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3753 return lxc_clear_config_caps(c
);
3756 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3759 return lxc_clear_config_keepcaps(c
);
3762 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3765 free(c
->console
.path
);
3766 c
->console
.path
= NULL
;
3770 static inline int clr_config_console_logfile(const char *key
,
3771 struct lxc_conf
*c
, void *data
)
3773 free(c
->console
.log_path
);
3774 c
->console
.log_path
= NULL
;
3778 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3781 c
->console
.log_rotate
= 0;
3785 static inline int clr_config_console_buffer_size(const char *key
,
3786 struct lxc_conf
*c
, void *data
)
3788 c
->console
.buffer_size
= 0;
3792 static inline int clr_config_console_buffer_logfile(const char *key
,
3796 free(c
->console
.buffer_log_file
);
3797 c
->console
.buffer_log_file
= NULL
;
3801 static inline int clr_config_seccomp_profile(const char *key
,
3802 struct lxc_conf
*c
, void *data
)
3809 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3816 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3823 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3826 c
->rebootsignal
= 0;
3830 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3837 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3840 if (strcmp(key
+ 10, "auto") == 0)
3842 else if (strcmp(key
+ 10, "delay") == 0)
3844 else if (strcmp(key
+ 10, "order") == 0)
3850 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3858 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3861 c
->monitor_unshare
= 0;
3865 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3868 return lxc_clear_groups(c
);
3871 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3874 return lxc_clear_environment(c
);
3877 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
3880 free(c
->execute_cmd
);
3881 c
->execute_cmd
= NULL
;
3885 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3893 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
3901 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3908 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3915 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3922 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3925 c
->no_new_privs
= false;
3929 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3932 return lxc_clear_limits(c
, key
);
3935 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
3938 return lxc_clear_sysctls(c
, key
);
3941 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
3944 return lxc_clear_procs(c
, key
);
3947 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
3950 lxc_clear_includes(c
);
3954 static inline int clr_config_noop(const char *key
, struct lxc_conf
*c
,
3960 static int clr_config_namespace(const char *key
, struct lxc_conf
*lxc_conf
,
3964 const char *namespace;
3966 namespace = key
+ sizeof("lxc.namespace.") - 1;
3967 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3971 free(lxc_conf
->inherit_ns
[ns_idx
]);
3972 lxc_conf
->inherit_ns
[ns_idx
] = NULL
;
3977 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
3978 struct lxc_conf
*c
, void *data
)
3983 static struct lxc_config_t
*get_network_config_ops(const char *key
,
3984 struct lxc_conf
*lxc_conf
,
3986 char **deindexed_key
)
3989 unsigned int tmpidx
;
3991 char *copy
, *idx_start
, *idx_end
;
3992 struct lxc_config_t
*config
= NULL
;
3994 /* check that this is a sensible network key */
3995 if (strncmp("lxc.net.", key
, 8)) {
3996 ERROR("Invalid network configuration key \"%s\"", key
);
4002 ERROR("Failed to duplicate string \"%s\"", key
);
4007 if (!isdigit(*(key
+ 8))) {
4008 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4012 /* beginning of index string */
4013 idx_start
= (copy
+ 7);
4016 /* end of index string */
4017 idx_end
= strchr((copy
+ 8), '.');
4021 /* parse current index */
4022 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4024 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4025 idx_start
+ 1, strerror(-ret
));
4030 /* This, of course is utterly nonsensical on so many levels, but
4031 * better safe than sorry.
4032 * (Checking for INT_MAX here is intentional.)
4034 if (tmpidx
== INT_MAX
) {
4035 SYSERROR("Number of configured networks would overflow the "
4041 numstrlen
= strlen((idx_start
+ 1));
4043 /* repair configuration key */
4046 /* lxc.net.<idx>.<subkey> */
4049 if (strlen(idx_end
+ 1) == 0) {
4050 ERROR("No subkey in network configuration key \"%s\"", key
);
4054 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4055 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4057 config
= lxc_get_config(copy
);
4059 ERROR("Unknown network configuration key \"%s\"", key
);
4065 *deindexed_key
= copy
;
4074 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4075 * found. So we make sure next comes an integer, find the right callback (by
4076 * rewriting the key), and call it.
4078 static int set_config_net_nic(const char *key
, const char *value
,
4079 struct lxc_conf
*lxc_conf
, void *data
)
4082 const char *idxstring
;
4083 struct lxc_config_t
*config
;
4084 struct lxc_netdev
*netdev
;
4086 char *deindexed_key
= NULL
;
4088 idxstring
= key
+ 8;
4089 if (!isdigit(*idxstring
))
4092 if (lxc_config_value_empty(value
))
4093 return clr_config_net_nic(key
, lxc_conf
, data
);
4095 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4096 if (!config
|| idx
< 0)
4099 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4101 free(deindexed_key
);
4105 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4106 free(deindexed_key
);
4110 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4114 const char *idxstring
;
4115 struct lxc_config_t
*config
;
4116 struct lxc_netdev
*netdev
;
4118 char *deindexed_key
= NULL
;
4120 idxstring
= key
+ 8;
4121 if (!isdigit(*idxstring
))
4124 /* The left conjunct is pretty self-explanatory. The right conjunct
4125 * checks whether the two pointers are equal. If they are we know that
4126 * this is not a key that is namespaced any further and so we are
4127 * supposed to clear the whole network.
4129 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4130 unsigned int rmnetdevidx
;
4132 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4135 /* Remove network from network list. */
4136 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4140 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4141 if (!config
|| idx
< 0)
4144 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4146 free(deindexed_key
);
4150 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4151 free(deindexed_key
);
4155 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4158 struct lxc_netdev
*netdev
= data
;
4168 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4171 struct lxc_netdev
*netdev
= data
;
4176 netdev
->name
[0] = '\0';
4181 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4184 struct lxc_netdev
*netdev
= data
;
4194 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4197 struct lxc_netdev
*netdev
= data
;
4202 netdev
->link
[0] = '\0';
4207 static int clr_config_net_macvlan_mode(const char *key
,
4208 struct lxc_conf
*lxc_conf
, void *data
)
4210 struct lxc_netdev
*netdev
= data
;
4215 if (netdev
->type
!= LXC_NET_MACVLAN
)
4218 netdev
->priv
.macvlan_attr
.mode
= -1;
4223 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4226 struct lxc_netdev
*netdev
= data
;
4231 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4236 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4239 struct lxc_netdev
*netdev
= data
;
4244 free(netdev
->upscript
);
4245 netdev
->upscript
= NULL
;
4250 static int clr_config_net_script_down(const char *key
,
4251 struct lxc_conf
*lxc_conf
, void *data
)
4253 struct lxc_netdev
*netdev
= data
;
4258 free(netdev
->downscript
);
4259 netdev
->downscript
= NULL
;
4264 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4267 struct lxc_netdev
*netdev
= data
;
4272 free(netdev
->hwaddr
);
4273 netdev
->hwaddr
= NULL
;
4278 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4281 struct lxc_netdev
*netdev
= data
;
4292 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4295 struct lxc_netdev
*netdev
= data
;
4300 netdev
->priv
.vlan_attr
.vid
= 0;
4305 static int clr_config_net_ipv4_gateway(const char *key
,
4306 struct lxc_conf
*lxc_conf
, void *data
)
4308 struct lxc_netdev
*netdev
= data
;
4313 free(netdev
->ipv4_gateway
);
4314 netdev
->ipv4_gateway
= NULL
;
4319 static int clr_config_net_ipv4_address(const char *key
,
4320 struct lxc_conf
*lxc_conf
, void *data
)
4322 struct lxc_netdev
*netdev
= data
;
4323 struct lxc_list
*cur
, *next
;
4328 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4337 static int clr_config_net_ipv6_gateway(const char *key
,
4338 struct lxc_conf
*lxc_conf
, void *data
)
4340 struct lxc_netdev
*netdev
= data
;
4345 free(netdev
->ipv6_gateway
);
4346 netdev
->ipv6_gateway
= NULL
;
4351 static int clr_config_net_ipv6_address(const char *key
,
4352 struct lxc_conf
*lxc_conf
, void *data
)
4354 struct lxc_netdev
*netdev
= data
;
4355 struct lxc_list
*cur
, *next
;
4360 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4369 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4370 struct lxc_conf
*c
, void *data
)
4373 const char *idxstring
;
4374 struct lxc_config_t
*config
;
4375 struct lxc_netdev
*netdev
;
4377 char *deindexed_key
= NULL
;
4379 idxstring
= key
+ 8;
4380 if (!isdigit(*idxstring
))
4383 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4384 if (!config
|| idx
< 0)
4387 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4389 free(deindexed_key
);
4393 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4394 free(deindexed_key
);
4398 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4399 struct lxc_conf
*c
, void *data
)
4403 struct lxc_netdev
*netdev
= data
;
4408 memset(retv
, 0, inlen
);
4413 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4418 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4419 struct lxc_conf
*c
, void *data
)
4423 struct lxc_netdev
*netdev
= data
;
4428 memset(retv
, 0, inlen
);
4433 if (netdev
->flags
& IFF_UP
)
4434 strprint(retv
, inlen
, "up");
4439 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4440 struct lxc_conf
*c
, void *data
)
4444 struct lxc_netdev
*netdev
= data
;
4449 memset(retv
, 0, inlen
);
4454 if (netdev
->link
[0] != '\0')
4455 strprint(retv
, inlen
, "%s", netdev
->link
);
4460 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4461 struct lxc_conf
*c
, void *data
)
4465 struct lxc_netdev
*netdev
= data
;
4470 memset(retv
, 0, inlen
);
4475 if (netdev
->name
[0] != '\0')
4476 strprint(retv
, inlen
, "%s", netdev
->name
);
4481 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4482 struct lxc_conf
*c
, void *data
)
4487 struct lxc_netdev
*netdev
= data
;
4492 memset(retv
, 0, inlen
);
4497 if (netdev
->type
!= LXC_NET_MACVLAN
)
4500 switch (netdev
->priv
.macvlan_attr
.mode
) {
4501 case MACVLAN_MODE_PRIVATE
:
4504 case MACVLAN_MODE_VEPA
:
4507 case MACVLAN_MODE_BRIDGE
:
4510 case MACVLAN_MODE_PASSTHRU
:
4518 strprint(retv
, inlen
, "%s", mode
);
4523 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4524 struct lxc_conf
*c
, void *data
)
4528 struct lxc_netdev
*netdev
= data
;
4533 memset(retv
, 0, inlen
);
4538 if (netdev
->type
!= LXC_NET_VETH
)
4541 strprint(retv
, inlen
, "%s",
4542 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4543 ? netdev
->priv
.veth_attr
.pair
4544 : netdev
->priv
.veth_attr
.veth1
);
4549 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4550 struct lxc_conf
*c
, void *data
)
4554 struct lxc_netdev
*netdev
= data
;
4559 memset(retv
, 0, inlen
);
4564 if (netdev
->upscript
)
4565 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4570 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4571 struct lxc_conf
*c
, void *data
)
4575 struct lxc_netdev
*netdev
= data
;
4580 memset(retv
, 0, inlen
);
4585 if (netdev
->downscript
)
4586 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4591 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4592 struct lxc_conf
*c
, void *data
)
4596 struct lxc_netdev
*netdev
= data
;
4601 memset(retv
, 0, inlen
);
4607 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4612 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4613 struct lxc_conf
*c
, void *data
)
4617 struct lxc_netdev
*netdev
= data
;
4622 memset(retv
, 0, inlen
);
4628 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4633 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4634 struct lxc_conf
*c
, void *data
)
4638 struct lxc_netdev
*netdev
= data
;
4643 memset(retv
, 0, inlen
);
4648 if (netdev
->type
!= LXC_NET_VLAN
)
4651 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4656 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4657 struct lxc_conf
*c
, void *data
)
4660 char buf
[INET_ADDRSTRLEN
];
4662 struct lxc_netdev
*netdev
= data
;
4667 memset(retv
, 0, inlen
);
4672 if (netdev
->ipv4_gateway_auto
) {
4673 strprint(retv
, inlen
, "auto");
4674 } else if (netdev
->ipv4_gateway
) {
4675 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4676 strprint(retv
, inlen
, "%s", buf
);
4682 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4683 struct lxc_conf
*c
, void *data
)
4687 char buf
[INET_ADDRSTRLEN
];
4688 struct lxc_list
*it
;
4690 struct lxc_netdev
*netdev
= data
;
4695 memset(retv
, 0, inlen
);
4700 listlen
= lxc_list_len(&netdev
->ipv4
);
4701 lxc_list_for_each(it
, &netdev
->ipv4
) {
4702 struct lxc_inetdev
*i
= it
->elem
;
4703 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4704 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4705 (listlen
-- > 1) ? "\n" : "");
4711 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4712 struct lxc_conf
*c
, void *data
)
4715 char buf
[INET6_ADDRSTRLEN
];
4717 struct lxc_netdev
*netdev
= data
;
4722 memset(retv
, 0, inlen
);
4727 if (netdev
->ipv6_gateway_auto
) {
4728 strprint(retv
, inlen
, "auto");
4729 } else if (netdev
->ipv6_gateway
) {
4730 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4731 strprint(retv
, inlen
, "%s", buf
);
4737 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4738 struct lxc_conf
*c
, void *data
)
4742 char buf
[INET6_ADDRSTRLEN
];
4743 struct lxc_list
*it
;
4745 struct lxc_netdev
*netdev
= data
;
4750 memset(retv
, 0, inlen
);
4755 listlen
= lxc_list_len(&netdev
->ipv6
);
4756 lxc_list_for_each(it
, &netdev
->ipv6
) {
4757 struct lxc_inet6dev
*i
= it
->elem
;
4758 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4759 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4760 (listlen
-- > 1) ? "\n" : "");
4766 int lxc_list_config_items(char *retv
, int inlen
)
4775 memset(retv
, 0, inlen
);
4777 for (i
= 0; i
< config_size
; i
++) {
4778 char *s
= config
[i
].name
;
4780 if (s
[strlen(s
) - 1] == '.')
4783 strprint(retv
, inlen
, "%s\n", s
);
4789 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4798 memset(retv
, 0, inlen
);
4800 if (!strcmp(key
, "lxc.apparmor")) {
4801 strprint(retv
, inlen
, "allow_incomplete\n");
4802 strprint(retv
, inlen
, "profile\n");
4803 } else if (!strcmp(key
, "lxc.cgroup")) {
4804 strprint(retv
, inlen
, "dir\n");
4805 } else if (!strcmp(key
, "lxc.selinux")) {
4806 strprint(retv
, inlen
, "context\n");
4807 } else if (!strcmp(key
, "lxc.mount")) {
4808 strprint(retv
, inlen
, "auto\n");
4809 strprint(retv
, inlen
, "entry\n");
4810 strprint(retv
, inlen
, "fstab\n");
4811 } else if (!strcmp(key
, "lxc.rootfs")) {
4812 strprint(retv
, inlen
, "mount\n");
4813 strprint(retv
, inlen
, "options\n");
4814 strprint(retv
, inlen
, "path\n");
4815 } else if (!strcmp(key
, "lxc.uts")) {
4816 strprint(retv
, inlen
, "name\n");
4817 } else if (!strcmp(key
, "lxc.hook")) {
4818 strprint(retv
, inlen
, "autodev\n");
4819 strprint(retv
, inlen
, "clone\n");
4820 strprint(retv
, inlen
, "destroy\n");
4821 strprint(retv
, inlen
, "mount\n");
4822 strprint(retv
, inlen
, "post-stop\n");
4823 strprint(retv
, inlen
, "pre-mount\n");
4824 strprint(retv
, inlen
, "pre-start\n");
4825 strprint(retv
, inlen
, "start-host\n");
4826 strprint(retv
, inlen
, "start\n");
4827 strprint(retv
, inlen
, "stop\n");
4828 } else if (!strcmp(key
, "lxc.cap")) {
4829 strprint(retv
, inlen
, "drop\n");
4830 strprint(retv
, inlen
, "keep\n");
4831 } else if (!strcmp(key
, "lxc.console")) {
4832 strprint(retv
, inlen
, "logfile\n");
4833 strprint(retv
, inlen
, "path\n");
4834 } else if (!strcmp(key
, "lxc.seccomp")) {
4835 strprint(retv
, inlen
, "profile\n");
4836 } else if (!strcmp(key
, "lxc.signal")) {
4837 strprint(retv
, inlen
, "halt\n");
4838 strprint(retv
, inlen
, "reboot\n");
4839 strprint(retv
, inlen
, "stop\n");
4840 } else if (!strcmp(key
, "lxc.start")) {
4841 strprint(retv
, inlen
, "auto\n");
4842 strprint(retv
, inlen
, "delay\n");
4843 strprint(retv
, inlen
, "order\n");
4844 } else if (!strcmp(key
, "lxc.monitor")) {
4845 strprint(retv
, inlen
, "unshare\n");
4853 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4856 const char *idxstring
;
4857 struct lxc_netdev
*netdev
;
4861 idxstring
= key
+ 8;
4862 if (!isdigit(*idxstring
))
4865 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
4869 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4876 memset(retv
, 0, inlen
);
4878 strprint(retv
, inlen
, "type\n");
4879 strprint(retv
, inlen
, "script.up\n");
4880 strprint(retv
, inlen
, "script.down\n");
4881 if (netdev
->type
!= LXC_NET_EMPTY
) {
4882 strprint(retv
, inlen
, "flags\n");
4883 strprint(retv
, inlen
, "link\n");
4884 strprint(retv
, inlen
, "name\n");
4885 strprint(retv
, inlen
, "hwaddr\n");
4886 strprint(retv
, inlen
, "mtu\n");
4887 strprint(retv
, inlen
, "ipv6.address\n");
4888 strprint(retv
, inlen
, "ipv6.gateway\n");
4889 strprint(retv
, inlen
, "ipv4.address\n");
4890 strprint(retv
, inlen
, "ipv4.gateway\n");
4893 switch (netdev
->type
) {
4895 strprint(retv
, inlen
, "veth.pair\n");
4897 case LXC_NET_MACVLAN
:
4898 strprint(retv
, inlen
, "macvlan.mode\n");
4901 strprint(retv
, inlen
, "vlan.id\n");