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(cgroup2_controller
);
85 lxc_config_define(cgroup_dir
);
86 lxc_config_define(console_logfile
);
87 lxc_config_define(console_rotate
);
88 lxc_config_define(console_buffer_logfile
);
89 lxc_config_define(console_buffer_size
);
90 lxc_config_define(console_path
);
91 lxc_config_define(environment
);
92 lxc_config_define(ephemeral
);
93 lxc_config_define(execute_cmd
);
94 lxc_config_define(group
);
95 lxc_config_define(hooks
);
96 lxc_config_define(hooks_version
);
97 lxc_config_define(idmaps
);
98 lxc_config_define(includefiles
);
99 lxc_config_define(init_cmd
);
100 lxc_config_define(init_cwd
);
101 lxc_config_define(init_gid
);
102 lxc_config_define(init_uid
);
103 lxc_config_define(log_file
);
104 lxc_config_define(log_level
);
105 lxc_config_define(log_syslog
);
106 lxc_config_define(monitor
);
107 lxc_config_define(mount
);
108 lxc_config_define(mount_auto
);
109 lxc_config_define(mount_fstab
);
110 lxc_config_define(namespace_share
);
111 lxc_config_define(net
);
112 lxc_config_define(net_flags
);
113 lxc_config_define(net_hwaddr
);
114 lxc_config_define(net_ipv4_address
);
115 lxc_config_define(net_ipv4_gateway
);
116 lxc_config_define(net_ipv6_address
);
117 lxc_config_define(net_ipv6_gateway
);
118 lxc_config_define(net_link
);
119 lxc_config_define(net_macvlan_mode
);
120 lxc_config_define(net_mtu
);
121 lxc_config_define(net_name
);
122 lxc_config_define(net_nic
);
123 lxc_config_define(net_script_down
);
124 lxc_config_define(net_script_up
);
125 lxc_config_define(net_type
);
126 lxc_config_define(net_veth_pair
);
127 lxc_config_define(net_vlan_id
);
128 lxc_config_define(no_new_privs
);
129 lxc_config_define(noop
);
130 lxc_config_define(personality
);
131 lxc_config_define(prlimit
);
132 lxc_config_define(pty_max
);
133 lxc_config_define(rootfs_backend
);
134 lxc_config_define(rootfs_mount
);
135 lxc_config_define(rootfs_options
);
136 lxc_config_define(rootfs_path
);
137 lxc_config_define(seccomp_profile
);
138 lxc_config_define(selinux_context
);
139 lxc_config_define(signal_halt
);
140 lxc_config_define(signal_reboot
);
141 lxc_config_define(signal_stop
);
142 lxc_config_define(start
);
143 lxc_config_define(tty_max
);
144 lxc_config_define(tty_dir
);
145 lxc_config_define(uts_name
);
146 lxc_config_define(sysctl
);
147 lxc_config_define(proc
);
149 static struct lxc_config_t config
[] = {
150 /* REMOVE in LXC 3.0 */
151 { "lxc.arch", false, set_config_personality
, get_config_personality
, clr_config_personality
, },
152 { "lxc.apparmor.profile", false, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
153 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
154 { "lxc.autodev", false, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
155 { "lxc.cap.drop", false, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
156 { "lxc.cap.keep", false, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
157 { "lxc.cgroup2", false, set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
158 { "lxc.cgroup.dir", false, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
159 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
160 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile
, get_config_console_buffer_logfile
, clr_config_console_buffer_logfile
, },
161 { "lxc.console.buffer.size", false, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
162 { "lxc.console.logfile", false, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
163 { "lxc.console.path", false, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
164 { "lxc.console.rotate", false, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
165 { "lxc.environment", false, set_config_environment
, get_config_environment
, clr_config_environment
, },
166 { "lxc.ephemeral", false, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
167 { "lxc.execute.cmd", false, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
168 { "lxc.group", false, set_config_group
, get_config_group
, clr_config_group
, },
169 { "lxc.hook.autodev", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
170 { "lxc.hook.clone", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
171 { "lxc.hook.destroy", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook.post-stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
174 { "lxc.hook.pre-mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
175 { "lxc.hook.pre-start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
176 { "lxc.hook.start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
177 { "lxc.hook.start-host", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
178 { "lxc.hook.stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
179 { "lxc.hook.version", false, set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
180 { "lxc.hook", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
181 { "lxc.idmap", false, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
182 { "lxc.include", false, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
183 { "lxc.init.cmd", false, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
184 { "lxc.init.gid", false, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
185 { "lxc.init.uid", false, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
186 { "lxc.init.cwd", false, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
187 { "lxc.log.file", false, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
188 { "lxc.log.level", false, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
189 { "lxc.log.syslog", false, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
190 { "lxc.monitor.unshare", false, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
191 { "lxc.mount.auto", false, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
192 { "lxc.mount.entry", false, set_config_mount
, get_config_mount
, clr_config_mount
, },
193 { "lxc.mount.fstab", false, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
194 { "lxc.namespace.share", false, set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
196 /* [START]: REMOVE IN LXC 3.0 */
197 { "lxc.network.type", true, set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
198 { "lxc.network.flags", true, set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
199 { "lxc.network.link", true, set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
200 { "lxc.network.name", true, set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
201 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
202 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
203 { "lxc.network.script.up", true, set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
204 { "lxc.network.script.down", true, set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
205 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
206 { "lxc.network.mtu", true, set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
207 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
208 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
209 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
210 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
211 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
212 { "lxc.network.", true, set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
213 { "lxc.network", true, set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
214 /* [END]: REMOVE IN LXC 3.0 */
216 { "lxc.net.flags", false, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
217 { "lxc.net.hwaddr", false, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
218 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
219 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
220 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
221 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
222 { "lxc.net.link", false, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
223 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
224 { "lxc.net.mtu", false, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
225 { "lxc.net.name", false, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
226 { "lxc.net.script.down", false, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
227 { "lxc.net.script.up", false, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
228 { "lxc.net.type", false, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
229 { "lxc.net.vlan.id", false, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
230 { "lxc.net.veth.pair", false, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
231 { "lxc.net.", false, set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
232 { "lxc.net", false, set_config_net
, get_config_net
, clr_config_net
, },
233 { "lxc.no_new_privs", false, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
234 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
235 { "lxc.pty.max", false, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
236 { "lxc.rootfs.mount", false, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
237 { "lxc.rootfs.options", false, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
238 { "lxc.rootfs.path", false, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
239 { "lxc.seccomp.profile", false, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
240 { "lxc.selinux.context", false, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
241 { "lxc.signal.halt", false, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
242 { "lxc.signal.reboot", false, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
243 { "lxc.signal.stop", false, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
244 { "lxc.start.auto", false, set_config_start
, get_config_start
, clr_config_start
, },
245 { "lxc.start.delay", false, set_config_start
, get_config_start
, clr_config_start
, },
246 { "lxc.start.order", false, set_config_start
, get_config_start
, clr_config_start
, },
247 { "lxc.tty.dir", false, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
248 { "lxc.tty.max", false, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
249 { "lxc.uts.name", false, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
250 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
251 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
253 /* [START]: REMOVE IN LXC 3.0 */
254 { "lxc.pts", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
255 { "lxc.devttydir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
256 { "lxc.tty", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
257 { "lxc.aa_profile", true, set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
258 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
259 { "lxc.se_context", true, set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
260 { "lxc.id_map", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
261 { "lxc.mount", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
262 { "lxc.rootfs.backend", true, set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
263 { "lxc.rootfs", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
264 { "lxc.utsname", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
265 { "lxc.seccomp", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
266 { "lxc.console", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
267 { "lxc.haltsignal", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
268 { "lxc.rebootsignal", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
269 { "lxc.stopsignal", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
270 { "lxc.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
271 { "lxc.kmsg", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
272 { "lxc.loglevel", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
273 { "lxc.logfile", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
274 { "lxc.init_cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
275 { "lxc.init_uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
276 { "lxc.init_gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
277 { "lxc.limit", true, set_config_limit
, get_config_limit
, clr_config_limit
, },
278 { "lxc.pivotdir", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
279 /* [END]: REMOVE IN LXC 3.0 */
287 static const struct signame signames
[] = {
320 { SIGSTKFLT
, "STKFLT" },
335 { SIGVTALRM
, "VTALRM" },
341 { SIGWINCH
, "WINCH" },
359 { SIGUNUSED
, "UNUSED" },
366 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
368 struct lxc_config_t
*lxc_get_config(const char *key
)
372 for (i
= 0; i
< config_size
; i
++)
373 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
379 static int set_config_net(const char *key
, const char *value
,
380 struct lxc_conf
*lxc_conf
, void *data
)
382 if (!lxc_config_value_empty(value
)) {
383 ERROR("lxc.net must not have a value");
387 return clr_config_net(key
, lxc_conf
, data
);
390 static int set_config_net_type(const char *key
, const char *value
,
391 struct lxc_conf
*lxc_conf
, void *data
)
393 struct lxc_netdev
*netdev
= data
;
395 if (lxc_config_value_empty(value
))
396 return clr_config_net_type(key
, lxc_conf
, data
);
401 if (!strcmp(value
, "veth")) {
402 netdev
->type
= LXC_NET_VETH
;
403 } else if (!strcmp(value
, "macvlan")) {
404 netdev
->type
= LXC_NET_MACVLAN
;
405 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
407 } else if (!strcmp(value
, "vlan")) {
408 netdev
->type
= LXC_NET_VLAN
;
409 } else if (!strcmp(value
, "phys")) {
410 netdev
->type
= LXC_NET_PHYS
;
411 } else if (!strcmp(value
, "empty")) {
412 netdev
->type
= LXC_NET_EMPTY
;
413 } else if (!strcmp(value
, "none")) {
414 netdev
->type
= LXC_NET_NONE
;
416 ERROR("invalid network type %s", value
);
423 static int set_config_net_flags(const char *key
, const char *value
,
424 struct lxc_conf
*lxc_conf
, void *data
)
426 struct lxc_netdev
*netdev
= data
;
428 if (lxc_config_value_empty(value
))
429 return clr_config_net_flags(key
, lxc_conf
, data
);
434 netdev
->flags
|= IFF_UP
;
439 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
440 struct lxc_netdev
*netdev
)
442 struct ifaddrs
*ifaddr
, *ifa
;
445 const char *type_key
= "lxc.net.type";
446 const char *link_key
= "lxc.net.link";
447 const char *tmpvalue
= "phys";
449 if (getifaddrs(&ifaddr
) == -1) {
450 SYSERROR("Get network interfaces failed");
454 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
457 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
460 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
461 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
464 ret
= set_config_net_link(
465 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
467 ERROR("failed to create matched ifnames");
471 ERROR("failed to create matched ifnames");
483 static int set_config_net_link(const char *key
, const char *value
,
484 struct lxc_conf
*lxc_conf
, void *data
)
486 struct lxc_netdev
*netdev
= data
;
489 if (lxc_config_value_empty(value
))
490 return clr_config_net_link(key
, lxc_conf
, data
);
495 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
496 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
498 ret
= network_ifname(netdev
->link
, value
);
503 static int set_config_net_name(const char *key
, const char *value
,
504 struct lxc_conf
*lxc_conf
, void *data
)
506 struct lxc_netdev
*netdev
= data
;
508 if (lxc_config_value_empty(value
))
509 return clr_config_net_name(key
, lxc_conf
, data
);
514 return network_ifname(netdev
->name
, value
);
517 static int set_config_net_veth_pair(const char *key
, const char *value
,
518 struct lxc_conf
*lxc_conf
, void *data
)
520 struct lxc_netdev
*netdev
= data
;
522 if (lxc_config_value_empty(value
))
523 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
528 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
);
531 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
532 struct lxc_conf
*lxc_conf
, void *data
)
534 struct lxc_netdev
*netdev
= data
;
536 if (lxc_config_value_empty(value
))
537 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
542 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
545 static int set_config_net_hwaddr(const char *key
, const char *value
,
546 struct lxc_conf
*lxc_conf
, void *data
)
548 struct lxc_netdev
*netdev
= data
;
551 if (lxc_config_value_empty(value
))
552 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
557 new_value
= strdup(value
);
561 rand_complete_hwaddr(new_value
);
563 if (lxc_config_value_empty(new_value
)) {
565 netdev
->hwaddr
= NULL
;
569 netdev
->hwaddr
= new_value
;
573 static int set_config_net_vlan_id(const char *key
, const char *value
,
574 struct lxc_conf
*lxc_conf
, void *data
)
577 struct lxc_netdev
*netdev
= data
;
579 if (lxc_config_value_empty(value
))
580 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
585 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
592 static int set_config_net_mtu(const char *key
, const char *value
,
593 struct lxc_conf
*lxc_conf
, void *data
)
595 struct lxc_netdev
*netdev
= data
;
597 if (lxc_config_value_empty(value
))
598 return clr_config_net_mtu(key
, lxc_conf
, data
);
603 return set_config_string_item(&netdev
->mtu
, value
);
606 static int set_config_net_ipv4_address(const char *key
, const char *value
,
607 struct lxc_conf
*lxc_conf
, void *data
)
610 struct lxc_netdev
*netdev
= data
;
611 struct lxc_inetdev
*inetdev
;
612 struct lxc_list
*list
;
613 char *cursor
, *slash
;
614 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
616 if (lxc_config_value_empty(value
))
617 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
622 inetdev
= malloc(sizeof(*inetdev
));
626 memset(inetdev
, 0, sizeof(*inetdev
));
628 list
= malloc(sizeof(*list
));
635 list
->elem
= inetdev
;
637 addr
= strdup(value
);
644 cursor
= strstr(addr
, " ");
650 slash
= strstr(addr
, "/");
656 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
657 if (!ret
|| ret
< 0) {
658 SYSERROR("Invalid ipv4 address \"%s\"", value
);
666 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
667 if (!ret
|| ret
< 0) {
668 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
677 /* No prefix specified, determine it from the network class. */
679 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
683 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
686 /* If no broadcast address, let compute one from the
687 * prefix and address.
690 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
691 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
694 lxc_list_add_tail(&netdev
->ipv4
, list
);
700 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
701 struct lxc_conf
*lxc_conf
, void *data
)
703 struct lxc_netdev
*netdev
= data
;
705 if (lxc_config_value_empty(value
))
706 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
711 free(netdev
->ipv4_gateway
);
713 if (!strcmp(value
, "auto")) {
714 netdev
->ipv4_gateway
= NULL
;
715 netdev
->ipv4_gateway_auto
= true;
720 gw
= malloc(sizeof(*gw
));
724 ret
= inet_pton(AF_INET
, value
, gw
);
725 if (!ret
|| ret
< 0) {
726 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
731 netdev
->ipv4_gateway
= gw
;
732 netdev
->ipv4_gateway_auto
= false;
738 static int set_config_net_ipv6_address(const char *key
, const char *value
,
739 struct lxc_conf
*lxc_conf
, void *data
)
742 struct lxc_netdev
*netdev
= data
;
743 struct lxc_inet6dev
*inet6dev
;
744 struct lxc_list
*list
;
745 char *slash
, *valdup
, *netmask
;
747 if (lxc_config_value_empty(value
))
748 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
753 inet6dev
= malloc(sizeof(*inet6dev
));
757 memset(inet6dev
, 0, sizeof(*inet6dev
));
759 list
= malloc(sizeof(*list
));
766 list
->elem
= inet6dev
;
768 valdup
= strdup(value
);
775 inet6dev
->prefix
= 64;
776 slash
= strstr(valdup
, "/");
780 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
789 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
790 if (!ret
|| ret
< 0) {
791 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
798 lxc_list_add_tail(&netdev
->ipv6
, list
);
804 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
805 struct lxc_conf
*lxc_conf
, void *data
)
807 struct lxc_netdev
*netdev
= data
;
809 if (lxc_config_value_empty(value
))
810 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
815 free(netdev
->ipv6_gateway
);
817 if (!strcmp(value
, "auto")) {
818 netdev
->ipv6_gateway
= NULL
;
819 netdev
->ipv6_gateway_auto
= true;
824 gw
= malloc(sizeof(*gw
));
828 ret
= inet_pton(AF_INET6
, value
, gw
);
829 if (!ret
|| ret
< 0) {
830 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
835 netdev
->ipv6_gateway
= gw
;
836 netdev
->ipv6_gateway_auto
= false;
842 static int set_config_net_script_up(const char *key
, const char *value
,
843 struct lxc_conf
*lxc_conf
, void *data
)
845 struct lxc_netdev
*netdev
= data
;
847 if (lxc_config_value_empty(value
))
848 return clr_config_net_script_up(key
, lxc_conf
, data
);
853 return set_config_string_item(&netdev
->upscript
, value
);
856 static int set_config_net_script_down(const char *key
, const char *value
,
857 struct lxc_conf
*lxc_conf
, void *data
)
859 struct lxc_netdev
*netdev
= data
;
861 if (lxc_config_value_empty(value
))
862 return clr_config_net_script_down(key
, lxc_conf
, data
);
867 return set_config_string_item(&netdev
->downscript
, value
);
870 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
872 struct lxc_list
*hooklist
;
874 hooklist
= malloc(sizeof(*hooklist
));
880 hooklist
->elem
= hook
;
881 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
885 static int set_config_seccomp_profile(const char *key
, const char *value
,
886 struct lxc_conf
*lxc_conf
, void *data
)
888 return set_config_path_item(&lxc_conf
->seccomp
, value
);
891 static int set_config_execute_cmd(const char *key
, const char *value
,
892 struct lxc_conf
*lxc_conf
, void *data
)
894 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
897 static int set_config_init_cmd(const char *key
, const char *value
,
898 struct lxc_conf
*lxc_conf
, void *data
)
900 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
903 static int set_config_init_cwd(const char *key
, const char *value
,
904 struct lxc_conf
*lxc_conf
, void *data
)
906 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
909 static int set_config_init_uid(const char *key
, const char *value
,
910 struct lxc_conf
*lxc_conf
, void *data
)
912 unsigned int init_uid
;
914 if (lxc_config_value_empty(value
)) {
915 lxc_conf
->init_uid
= 0;
919 if (lxc_safe_uint(value
, &init_uid
) < 0)
922 lxc_conf
->init_uid
= init_uid
;
927 static int set_config_init_gid(const char *key
, const char *value
,
928 struct lxc_conf
*lxc_conf
, void *data
)
930 unsigned int init_gid
;
932 if (lxc_config_value_empty(value
)) {
933 lxc_conf
->init_gid
= 0;
937 if (lxc_safe_uint(value
, &init_gid
) < 0)
940 lxc_conf
->init_gid
= init_gid
;
945 static int set_config_hooks(const char *key
, const char *value
,
946 struct lxc_conf
*lxc_conf
, void *data
)
950 if (lxc_config_value_empty(value
))
951 return lxc_clear_hooks(lxc_conf
, key
);
953 if (strcmp(key
+ 4, "hook") == 0) {
954 ERROR("lxc.hook must not have a value");
958 copy
= strdup(value
);
962 if (strcmp(key
+ 9, "pre-start") == 0)
963 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
964 else if (strcmp(key
+ 9, "start-host") == 0)
965 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
966 else if (strcmp(key
+ 9, "pre-mount") == 0)
967 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
968 else if (strcmp(key
+ 9, "autodev") == 0)
969 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
970 else if (strcmp(key
+ 9, "mount") == 0)
971 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
972 else if (strcmp(key
+ 9, "start") == 0)
973 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
974 else if (strcmp(key
+ 9, "stop") == 0)
975 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
976 else if (strcmp(key
+ 9, "post-stop") == 0)
977 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
978 else if (strcmp(key
+ 9, "clone") == 0)
979 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
980 else if (strcmp(key
+ 9, "destroy") == 0)
981 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
987 static int set_config_hooks_version(const char *key
, const char *value
,
988 struct lxc_conf
*lxc_conf
, void *data
)
993 if (lxc_config_value_empty(value
))
994 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
996 ret
= lxc_safe_uint(value
, &tmp
);
1001 ERROR("Invalid hook version specified. Currently only 0 "
1002 "(legacy) and 1 are supported");
1006 lxc_conf
->hooks_version
= tmp
;
1010 static int set_config_personality(const char *key
, const char *value
,
1011 struct lxc_conf
*lxc_conf
, void *data
)
1013 signed long personality
= lxc_config_parse_arch(value
);
1015 if (personality
>= 0)
1016 lxc_conf
->personality
= personality
;
1018 WARN("Unsupported personality \"%s\"", value
);
1023 static int set_config_pty_max(const char *key
, const char *value
,
1024 struct lxc_conf
*lxc_conf
, void *data
)
1026 if (lxc_config_value_empty(value
)) {
1031 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1037 /* We only need to check whether the first byte of the key after the lxc.start.
1038 * prefix matches our expectations since they fortunately all start with a
1039 * different letter. If anything was wrong with the key we would have already
1040 * noticed when the callback was called.
1042 static int set_config_start(const char *key
, const char *value
,
1043 struct lxc_conf
*lxc_conf
, void *data
)
1047 is_empty
= lxc_config_value_empty(value
);
1049 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1051 lxc_conf
->start_auto
= 0;
1055 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1058 if (lxc_conf
->start_auto
> 1)
1062 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1064 lxc_conf
->start_delay
= 0;
1068 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1069 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1071 lxc_conf
->start_order
= 0;
1075 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1081 static int set_config_monitor(const char *key
, const char *value
,
1082 struct lxc_conf
*lxc_conf
, void *data
)
1084 if (lxc_config_value_empty(value
)) {
1085 lxc_conf
->monitor_unshare
= 0;
1089 if (strcmp(key
+ 12, "unshare") == 0)
1090 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1095 static int set_config_group(const char *key
, const char *value
,
1096 struct lxc_conf
*lxc_conf
, void *data
)
1098 char *groups
, *groupptr
, *sptr
, *token
;
1099 struct lxc_list
*grouplist
;
1102 if (lxc_config_value_empty(value
))
1103 return lxc_clear_groups(lxc_conf
);
1105 groups
= strdup(value
);
1109 /* In case several groups are specified in a single line split these
1110 * groups in a single element for the list.
1112 for (groupptr
= groups
;; groupptr
= NULL
) {
1113 token
= strtok_r(groupptr
, " \t", &sptr
);
1119 grouplist
= malloc(sizeof(*grouplist
));
1123 grouplist
->elem
= strdup(token
);
1124 if (!grouplist
->elem
) {
1129 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1136 static int set_config_environment(const char *key
, const char *value
,
1137 struct lxc_conf
*lxc_conf
, void *data
)
1139 struct lxc_list
*list_item
= NULL
;
1141 if (lxc_config_value_empty(value
))
1142 return lxc_clear_environment(lxc_conf
);
1144 list_item
= malloc(sizeof(*list_item
));
1148 list_item
->elem
= strdup(value
);
1150 if (!list_item
->elem
)
1153 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1162 static int set_config_tty_max(const char *key
, const char *value
,
1163 struct lxc_conf
*lxc_conf
, void *data
)
1165 if (lxc_config_value_empty(value
)) {
1170 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1173 static int set_config_tty_dir(const char *key
, const char *value
,
1174 struct lxc_conf
*lxc_conf
, void *data
)
1176 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1180 static int set_config_apparmor_profile(const char *key
, const char *value
,
1181 struct lxc_conf
*lxc_conf
, void *data
)
1183 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1186 static int set_config_apparmor_allow_incomplete(const char *key
,
1188 struct lxc_conf
*lxc_conf
,
1191 if (lxc_config_value_empty(value
)) {
1192 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1196 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1199 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1205 static int set_config_selinux_context(const char *key
, const char *value
,
1206 struct lxc_conf
*lxc_conf
, void *data
)
1208 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1211 static int set_config_log_file(const char *key
, const char *value
,
1212 struct lxc_conf
*c
, void *data
)
1216 if (lxc_config_value_empty(value
)) {
1222 /* Store these values in the lxc_conf, and then try to set for actual
1225 ret
= set_config_path_item(&c
->logfile
, value
);
1227 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1232 static int set_config_log_level(const char *key
, const char *value
,
1233 struct lxc_conf
*lxc_conf
, void *data
)
1237 if (lxc_config_value_empty(value
)) {
1238 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1242 if (value
[0] >= '0' && value
[0] <= '9') {
1243 if (lxc_safe_int(value
, &newlevel
) < 0)
1246 newlevel
= lxc_log_priority_to_int(value
);
1249 /* Store these values in the lxc_conf, and then try to set for actual
1252 lxc_conf
->loglevel
= newlevel
;
1253 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1256 static int set_config_autodev(const char *key
, const char *value
,
1257 struct lxc_conf
*lxc_conf
, void *data
)
1259 if (lxc_config_value_empty(value
)) {
1260 lxc_conf
->autodev
= 0;
1264 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1267 if (lxc_conf
->autodev
> 1)
1273 static int sig_num(const char *sig
)
1275 unsigned int signum
;
1277 if (lxc_safe_uint(sig
, &signum
) < 0)
1283 static int rt_sig_num(const char *signame
)
1285 int rtmax
= 0, sig_n
= 0;
1287 if (strncasecmp(signame
, "max-", 4) == 0) {
1292 if (!isdigit(*signame
))
1295 sig_n
= sig_num(signame
);
1296 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1297 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1303 static int sig_parse(const char *signame
)
1307 if (isdigit(*signame
)) {
1308 return sig_num(signame
);
1309 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1311 if (strncasecmp(signame
, "rt", 2) == 0)
1312 return rt_sig_num(signame
+ 2);
1313 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1314 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1315 return signames
[n
].num
;
1322 static int set_config_signal_halt(const char *key
, const char *value
,
1323 struct lxc_conf
*lxc_conf
, void *data
)
1327 if (lxc_config_value_empty(value
)) {
1328 lxc_conf
->haltsignal
= 0;
1332 sig_n
= sig_parse(value
);
1336 lxc_conf
->haltsignal
= sig_n
;
1341 static int set_config_signal_reboot(const char *key
, const char *value
,
1342 struct lxc_conf
*lxc_conf
, void *data
)
1346 if (lxc_config_value_empty(value
)) {
1347 lxc_conf
->rebootsignal
= 0;
1351 sig_n
= sig_parse(value
);
1355 lxc_conf
->rebootsignal
= sig_n
;
1360 static int set_config_signal_stop(const char *key
, const char *value
,
1361 struct lxc_conf
*lxc_conf
, void *data
)
1365 if (lxc_config_value_empty(value
)) {
1366 lxc_conf
->stopsignal
= 0;
1370 sig_n
= sig_parse(value
);
1374 lxc_conf
->stopsignal
= sig_n
;
1379 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1380 struct lxc_conf
*lxc_conf
, int version
)
1382 const char *subkey
, *token
;
1384 struct lxc_list
*cglist
= NULL
;
1385 struct lxc_cgroup
*cgelem
= NULL
;
1387 if (lxc_config_value_empty(value
))
1388 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1390 if (version
== CGROUP2_SUPER_MAGIC
) {
1391 token
= "lxc.cgroup2.";
1393 } else if (version
== CGROUP_SUPER_MAGIC
) {
1394 token
= "lxc.cgroup.";
1400 if (strncmp(key
, token
, token_len
) != 0)
1403 subkey
= key
+ token_len
;
1404 if (*subkey
== '\0')
1407 cglist
= malloc(sizeof(*cglist
));
1411 cgelem
= malloc(sizeof(*cgelem
));
1414 memset(cgelem
, 0, sizeof(*cgelem
));
1416 cgelem
->subsystem
= strdup(subkey
);
1417 if (!cgelem
->subsystem
)
1420 cgelem
->value
= strdup(value
);
1424 cgelem
->version
= version
;
1426 lxc_list_add_elem(cglist
, cgelem
);
1428 if (version
== CGROUP2_SUPER_MAGIC
)
1429 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1431 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1438 free(cgelem
->subsystem
);
1439 free(cgelem
->value
);
1446 static int set_config_cgroup_controller(const char *key
, const char *value
,
1447 struct lxc_conf
*lxc_conf
, void *data
)
1449 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1450 CGROUP_SUPER_MAGIC
);
1453 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1454 struct lxc_conf
*lxc_conf
, void *data
)
1456 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1457 CGROUP2_SUPER_MAGIC
);
1461 static int set_config_cgroup_dir(const char *key
, const char *value
,
1462 struct lxc_conf
*lxc_conf
, void *data
)
1464 if (lxc_config_value_empty(value
))
1465 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1467 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1470 static int set_config_prlimit(const char *key
, const char *value
,
1471 struct lxc_conf
*lxc_conf
, void *data
)
1473 struct lxc_list
*iter
;
1474 struct rlimit limit
;
1476 struct lxc_list
*limlist
= NULL
;
1477 struct lxc_limit
*limelem
= NULL
;
1479 if (lxc_config_value_empty(value
))
1480 return lxc_clear_limits(lxc_conf
, key
);
1482 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1485 key
+= sizeof("lxc.prlimit.") - 1;
1487 /* soft limit comes first in the value */
1488 if (!parse_limit_value(&value
, &limit_value
))
1490 limit
.rlim_cur
= limit_value
;
1492 /* skip spaces and a colon */
1493 while (isspace(*value
))
1498 else if (*value
) /* any other character is an error here */
1501 while (isspace(*value
))
1504 /* optional hard limit */
1506 if (!parse_limit_value(&value
, &limit_value
))
1508 limit
.rlim_max
= limit_value
;
1510 /* check for trailing garbage */
1511 while (isspace(*value
))
1517 /* a single value sets both hard and soft limit */
1518 limit
.rlim_max
= limit
.rlim_cur
;
1521 /* find existing list element */
1522 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1523 limelem
= iter
->elem
;
1524 if (!strcmp(key
, limelem
->resource
)) {
1525 limelem
->limit
= limit
;
1530 /* allocate list element */
1531 limlist
= malloc(sizeof(*limlist
));
1535 limelem
= malloc(sizeof(*limelem
));
1538 memset(limelem
, 0, sizeof(*limelem
));
1540 limelem
->resource
= strdup(key
);
1541 if (!limelem
->resource
)
1543 limelem
->limit
= limit
;
1545 lxc_list_add_elem(limlist
, limelem
);;
1547 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1554 free(limelem
->resource
);
1560 static int set_config_sysctl(const char *key
, const char *value
,
1561 struct lxc_conf
*lxc_conf
, void *data
)
1563 struct lxc_list
*iter
;
1564 char *replace_value
= NULL
;
1565 struct lxc_list
*sysctl_list
= NULL
;
1566 struct lxc_sysctl
*sysctl_elem
= NULL
;
1568 if (lxc_config_value_empty(value
))
1569 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1571 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1574 key
+= sizeof("lxc.sysctl.") - 1;
1576 /* find existing list element */
1577 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1578 sysctl_elem
= iter
->elem
;
1580 if (strcmp(key
, sysctl_elem
->key
) != 0)
1583 replace_value
= strdup(value
);
1587 free(sysctl_elem
->value
);
1588 sysctl_elem
->value
= replace_value
;
1592 /* allocate list element */
1593 sysctl_list
= malloc(sizeof(*sysctl_list
));
1597 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1600 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1602 sysctl_elem
->key
= strdup(key
);
1603 if (!sysctl_elem
->key
)
1606 sysctl_elem
->value
= strdup(value
);
1607 if (!sysctl_elem
->value
)
1610 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1612 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1619 free(sysctl_elem
->key
);
1620 free(sysctl_elem
->value
);
1626 static int set_config_proc(const char *key
, const char *value
,
1627 struct lxc_conf
*lxc_conf
, void *data
)
1630 struct lxc_list
*proclist
= NULL
;
1631 struct lxc_proc
*procelem
= NULL
;
1633 if (lxc_config_value_empty(value
))
1634 return clr_config_proc(key
, lxc_conf
, NULL
);
1636 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1639 subkey
= key
+ sizeof("lxc.proc.") - 1;
1640 if (*subkey
== '\0')
1643 proclist
= malloc(sizeof(*proclist
));
1647 procelem
= malloc(sizeof(*procelem
));
1650 memset(procelem
, 0, sizeof(*procelem
));
1652 procelem
->filename
= strdup(subkey
);
1653 procelem
->value
= strdup(value
);
1655 if (!procelem
->filename
|| !procelem
->value
)
1658 proclist
->elem
= procelem
;
1660 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1667 free(procelem
->filename
);
1668 free(procelem
->value
);
1675 static int set_config_idmaps(const char *key
, const char *value
,
1676 struct lxc_conf
*lxc_conf
, void *data
)
1678 unsigned long hostid
, nsid
, range
;
1681 struct lxc_list
*idmaplist
= NULL
;
1682 struct id_map
*idmap
= NULL
;
1684 if (lxc_config_value_empty(value
))
1685 return lxc_clear_idmaps(lxc_conf
);
1687 idmaplist
= malloc(sizeof(*idmaplist
));
1691 idmap
= malloc(sizeof(*idmap
));
1694 memset(idmap
, 0, sizeof(*idmap
));
1696 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1698 ERROR("Failed to parse id mappings");
1702 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1704 idmap
->idtype
= ID_TYPE_UID
;
1705 else if (type
== 'g')
1706 idmap
->idtype
= ID_TYPE_GID
;
1710 idmap
->hostid
= hostid
;
1712 idmap
->range
= range
;
1713 idmaplist
->elem
= idmap
;
1714 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1716 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1717 if (idmap
->nsid
== 0)
1718 lxc_conf
->root_nsuid_map
= idmap
;
1721 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1722 if (idmap
->nsid
== 0)
1723 lxc_conf
->root_nsgid_map
= idmap
;
1736 static int set_config_mount_fstab(const char *key
, const char *value
,
1737 struct lxc_conf
*lxc_conf
, void *data
)
1739 if (lxc_config_value_empty(value
)) {
1740 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1744 return set_config_path_item(&lxc_conf
->fstab
, value
);
1747 static int set_config_mount_auto(const char *key
, const char *value
,
1748 struct lxc_conf
*lxc_conf
, void *data
)
1750 char *autos
, *autoptr
, *sptr
, *token
;
1757 } allowed_auto_mounts
[] = {
1758 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1759 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1760 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1761 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1762 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1763 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1764 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1765 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1766 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1767 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1768 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1769 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1770 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1771 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1772 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1773 /* For adding anything that is just a single on/off, but has no
1774 * options: keep mask and flag identical and just define the enum
1775 * value as an unused bit so far
1780 if (lxc_config_value_empty(value
)) {
1781 lxc_conf
->auto_mounts
= 0;
1785 autos
= strdup(value
);
1789 for (autoptr
= autos
;; autoptr
= NULL
) {
1790 token
= strtok_r(autoptr
, " \t", &sptr
);
1796 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1797 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1801 if (!allowed_auto_mounts
[i
].token
) {
1802 ERROR("Invalid filesystem to automount \"%s\"", token
);
1806 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1807 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1814 static int set_config_mount(const char *key
, const char *value
,
1815 struct lxc_conf
*lxc_conf
, void *data
)
1818 struct lxc_list
*mntlist
;
1820 if (lxc_config_value_empty(value
))
1821 return lxc_clear_mount_entries(lxc_conf
);
1823 mntlist
= malloc(sizeof(*mntlist
));
1827 mntelem
= strdup(value
);
1832 mntlist
->elem
= mntelem
;
1834 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1839 static int set_config_cap_keep(const char *key
, const char *value
,
1840 struct lxc_conf
*lxc_conf
, void *data
)
1842 char *keepcaps
, *keepptr
, *sptr
, *token
;
1843 struct lxc_list
*keeplist
;
1846 if (lxc_config_value_empty(value
))
1847 return lxc_clear_config_keepcaps(lxc_conf
);
1849 keepcaps
= strdup(value
);
1853 /* In case several capability keep is specified in a single line
1854 * split these caps in a single element for the list.
1856 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1857 token
= strtok_r(keepptr
, " \t", &sptr
);
1863 if (!strcmp(token
, "none"))
1864 lxc_clear_config_keepcaps(lxc_conf
);
1866 keeplist
= malloc(sizeof(*keeplist
));
1870 keeplist
->elem
= strdup(token
);
1871 if (!keeplist
->elem
) {
1876 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1884 static int set_config_cap_drop(const char *key
, const char *value
,
1885 struct lxc_conf
*lxc_conf
, void *data
)
1887 char *dropcaps
, *dropptr
, *sptr
, *token
;
1888 struct lxc_list
*droplist
;
1891 if (lxc_config_value_empty(value
))
1892 return lxc_clear_config_caps(lxc_conf
);
1894 dropcaps
= strdup(value
);
1898 /* In case several capability drop is specified in a single line
1899 * split these caps in a single element for the list.
1901 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1902 token
= strtok_r(dropptr
, " \t", &sptr
);
1908 droplist
= malloc(sizeof(*droplist
));
1912 droplist
->elem
= strdup(token
);
1913 if (!droplist
->elem
) {
1918 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1926 static int set_config_console_path(const char *key
, const char *value
,
1927 struct lxc_conf
*lxc_conf
, void *data
)
1929 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1932 static int set_config_console_rotate(const char *key
, const char *value
,
1933 struct lxc_conf
*lxc_conf
, void *data
)
1935 if (lxc_config_value_empty(value
)) {
1936 lxc_conf
->console
.log_rotate
= 0;
1940 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1943 if (lxc_conf
->console
.log_rotate
> 1) {
1944 ERROR("The \"lxc.console.rotate\" config key can only be set "
1952 static int set_config_console_logfile(const char *key
, const char *value
,
1953 struct lxc_conf
*lxc_conf
, void *data
)
1955 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1958 static int set_config_console_buffer_size(const char *key
, const char *value
,
1959 struct lxc_conf
*lxc_conf
, void *data
)
1963 uint64_t buffer_size
, pgsz
;
1965 if (lxc_config_value_empty(value
)) {
1966 lxc_conf
->console
.buffer_size
= 0;
1970 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1971 if (!strcmp(value
, "auto")) {
1972 lxc_conf
->console
.buffer_size
= 1 << 17;
1976 ret
= parse_byte_size_string(value
, &size
);
1983 /* must be at least a page size */
1984 pgsz
= lxc_getpagesize();
1985 if ((uint64_t)size
< pgsz
) {
1986 NOTICE("Requested ringbuffer size for the console is %" PRId64
1987 " but must be at least %" PRId64
1988 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1993 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1994 if (buffer_size
== 0)
1997 if (buffer_size
!= size
)
1998 NOTICE("Passed size was not a power of 2. Rounding log size to "
1999 "next power of two: %" PRIu64
" bytes", buffer_size
);
2001 lxc_conf
->console
.buffer_size
= buffer_size
;
2005 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
2006 struct lxc_conf
*lxc_conf
,
2009 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
2012 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2014 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
2016 update_hwaddr(line
);
2018 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2019 char *tmp
= realloc(conf
->unexpanded_config
,
2020 conf
->unexpanded_alloced
+ 1024);
2024 if (!conf
->unexpanded_config
)
2026 conf
->unexpanded_config
= tmp
;
2027 conf
->unexpanded_alloced
+= 1024;
2029 strcat(conf
->unexpanded_config
, line
);
2030 conf
->unexpanded_len
+= linelen
;
2031 if (line
[linelen
- 1] != '\n') {
2032 strcat(conf
->unexpanded_config
, "\n");
2033 conf
->unexpanded_len
++;
2039 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2041 struct dirent
*direntp
;
2043 char path
[MAXPATHLEN
];
2047 dir
= opendir(dirp
);
2051 while ((direntp
= readdir(dir
))) {
2056 fnam
= direntp
->d_name
;
2057 if (!strcmp(fnam
, "."))
2060 if (!strcmp(fnam
, ".."))
2064 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2067 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2068 if (len
< 0 || len
>= MAXPATHLEN
) {
2073 ret
= lxc_config_read(path
, lxc_conf
, true);
2085 static int set_config_includefiles(const char *key
, const char *value
,
2086 struct lxc_conf
*lxc_conf
, void *data
)
2088 if (lxc_config_value_empty(value
)) {
2089 clr_config_includefiles(key
, lxc_conf
, NULL
);
2094 return do_includedir(value
, lxc_conf
);
2096 return lxc_config_read(value
, lxc_conf
, true);
2099 static int set_config_rootfs_path(const char *key
, const char *value
,
2100 struct lxc_conf
*lxc_conf
, void *data
)
2104 const char *container_path
;
2106 if (lxc_config_value_empty(value
)) {
2107 free(lxc_conf
->rootfs
.path
);
2108 lxc_conf
->rootfs
.path
= NULL
;
2112 dup
= strdup(value
);
2116 /* Split <storage type>:<container path> into <storage type> and
2117 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2118 * "rootfs.path" to <container path>.
2120 tmp
= strchr(dup
, ':');
2123 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2129 container_path
= tmp
;
2131 container_path
= value
;
2134 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2139 static int set_config_rootfs_mount(const char *key
, const char *value
,
2140 struct lxc_conf
*lxc_conf
, void *data
)
2142 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2145 static int set_config_rootfs_options(const char *key
, const char *value
,
2146 struct lxc_conf
*lxc_conf
, void *data
)
2148 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2151 static int set_config_rootfs_backend(const char *key
, const char *value
,
2152 struct lxc_conf
*lxc_conf
, void *data
)
2157 static int set_config_uts_name(const char *key
, const char *value
,
2158 struct lxc_conf
*lxc_conf
, void *data
)
2160 struct utsname
*utsname
;
2162 if (lxc_config_value_empty(value
)) {
2163 clr_config_uts_name(key
, lxc_conf
, NULL
);
2167 utsname
= malloc(sizeof(*utsname
));
2171 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2176 strcpy(utsname
->nodename
, value
);
2177 free(lxc_conf
->utsname
);
2178 lxc_conf
->utsname
= utsname
;
2183 static int set_config_namespace_share(const char *key
, const char *value
,
2184 struct lxc_conf
*lxc_conf
, void *data
)
2187 const char *namespace;
2189 if (lxc_config_value_empty(value
))
2190 return clr_config_namespace_share(key
, lxc_conf
, data
);
2192 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
2193 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2197 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2200 struct parse_line_conf
{
2201 struct lxc_conf
*conf
;
2205 static int parse_line(char *buffer
, void *data
)
2207 char *dot
, *key
, *line
, *linep
, *value
;
2209 struct lxc_config_t
*config
;
2212 struct parse_line_conf
*plc
= data
;
2214 /* If there are newlines in the config file we should keep them. */
2215 empty_line
= lxc_is_line_empty(dup
);
2219 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2220 * modified the original string on the stack by replacing '=' by '\0'
2223 linep
= line
= strdup(dup
);
2227 if (!plc
->from_include
) {
2228 ret
= append_unexp_config_line(line
, plc
->conf
);
2236 line
+= lxc_char_left_gc(line
, strlen(line
));
2238 /* ignore comments */
2242 /* martian option - don't add it to the config itself */
2243 if (strncmp(line
, "lxc.", 4))
2248 dot
= strchr(line
, '=');
2250 ERROR("Invalid configuration line: %s", line
);
2258 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2260 value
+= lxc_char_left_gc(value
, strlen(value
));
2261 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2263 if (*value
== '\'' || *value
== '\"') {
2266 len
= strlen(value
);
2267 if (len
> 1 && value
[len
- 1] == *value
) {
2268 value
[len
- 1] = '\0';
2273 config
= lxc_get_config(key
);
2275 ERROR("Unknown configuration key \"%s\"", key
);
2279 /* [START]: REMOVE IN LXC 3.0 */
2280 if (config
->is_legacy_key
&& !plc
->conf
->contains_legacy_key
) {
2281 plc
->conf
->contains_legacy_key
= true;
2282 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2283 /* Warn the user once loud and clear that there is at
2284 * least one legacy configuration item in the
2285 * configuration file and then an update is required.
2287 fprintf(stderr
, "The configuration file contains "
2288 "legacy configuration keys.\nPlease "
2289 "update your configuration file!\nThe "
2290 "update script lxc-update-config -c "
2291 "<path-to-config> can be used for "
2295 /* [END]: REMOVE IN LXC 3.0 */
2297 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2304 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2306 struct parse_line_conf c
;
2309 c
.from_include
= false;
2311 return parse_line(buffer
, &c
);
2314 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2317 struct parse_line_conf c
;
2320 c
.from_include
= from_include
;
2322 ret
= access(file
, R_OK
);
2326 /* Catch only the top level config file name in the structure. */
2328 conf
->rcfile
= strdup(file
);
2330 return lxc_file_for_each_line(file
, parse_line
, &c
);
2333 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2335 struct lxc_list
*dent
;
2337 dent
= malloc(sizeof(struct lxc_list
));
2342 lxc_list_add_tail(defines
, dent
);
2346 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2348 struct lxc_list
*it
, *next
;
2351 lxc_list_for_each(it
, defines
) {
2352 ret
= lxc_config_readline(it
->elem
, conf
);
2357 lxc_list_for_each_safe(it
, defines
, next
) {
2365 signed long lxc_config_parse_arch(const char *arch
)
2367 #if HAVE_SYS_PERSONALITY_H
2373 { "x86", PER_LINUX32
},
2374 { "linux32", PER_LINUX32
},
2375 { "i386", PER_LINUX32
},
2376 { "i486", PER_LINUX32
},
2377 { "i586", PER_LINUX32
},
2378 { "i686", PER_LINUX32
},
2379 { "athlon", PER_LINUX32
},
2380 { "mips", PER_LINUX32
},
2381 { "mipsel", PER_LINUX32
},
2382 { "ppc", PER_LINUX32
},
2383 { "arm", PER_LINUX32
},
2384 { "armv7l", PER_LINUX32
},
2385 { "armhf", PER_LINUX32
},
2386 { "armel", PER_LINUX32
},
2387 { "powerpc", PER_LINUX32
},
2388 { "linux64", PER_LINUX
},
2389 { "x86_64", PER_LINUX
},
2390 { "amd64", PER_LINUX
},
2391 { "mips64", PER_LINUX
},
2392 { "mips64el", PER_LINUX
},
2393 { "ppc64", PER_LINUX
},
2394 { "ppc64le", PER_LINUX
},
2395 { "ppc64el", PER_LINUX
},
2396 { "powerpc64", PER_LINUX
},
2397 { "s390x", PER_LINUX
},
2398 { "aarch64", PER_LINUX
},
2399 { "arm64", PER_LINUX
},
2401 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2403 for (i
= 0; i
< len
; i
++) {
2404 if (!strcmp(pername
[i
].name
, arch
))
2405 return pername
[i
].per
;
2412 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2414 char *token
, *saveptr
= NULL
;
2420 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2421 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2422 { "LSM", LXC_ATTACH_LSM_EXEC
},
2427 /* For the sake of backward compatibility, drop all privileges
2428 * if none is specified.
2430 for (i
= 0; all_privs
[i
].token
; i
++)
2431 *flags
|= all_privs
[i
].flag
;
2436 token
= strtok_r(flaglist
, "|", &saveptr
);
2439 for (i
= 0; all_privs
[i
].token
; i
++)
2440 if (!strcmp(all_privs
[i
].token
, token
))
2441 aflag
= all_privs
[i
].flag
;
2447 token
= strtok_r(NULL
, "|", &saveptr
);
2453 /* Write out a configuration file. */
2454 void write_config(FILE *fout
, struct lxc_conf
*c
)
2457 size_t len
= c
->unexpanded_len
;
2462 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2464 SYSERROR("Failed to write configuration file");
2467 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2474 len
= strlen(key
) + strlen(v
) + 4;
2477 if (lxc_config_value_empty(v
))
2478 ret
= snprintf(tmp
, len
, "%s =", key
);
2480 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2481 if (ret
< 0 || ret
>= len
)
2484 /* Save the line verbatim into unexpanded_conf */
2485 if (append_unexp_config_line(tmp
, conf
))
2491 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2495 char *lstart
= conf
->unexpanded_config
;
2497 if (!conf
->unexpanded_config
)
2501 lend
= strchr(lstart
, '\n');
2504 lend
= lstart
+ strlen(lstart
);
2507 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2512 v
= lstart
[strlen(key
)];
2513 if (!isspace(v
) && v
!= '=') {
2518 conf
->unexpanded_len
-= (lend
- lstart
);
2519 if (*lend
== '\0') {
2523 memmove(lstart
, lend
, strlen(lend
) + 1);
2527 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2528 const char *newpath
, const char *oldname
,
2529 const char *newname
, const char *ovldir
)
2532 char *lend
, *newdir
, *olddir
, *p
, *q
;
2533 size_t newdirlen
, olddirlen
;
2534 char *lstart
= conf
->unexpanded_config
;
2535 const char *key
= "lxc.mount.entry";
2537 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2538 olddir
= alloca(olddirlen
+ 1);
2539 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2541 if (ret
< 0 || ret
>= olddirlen
+ 1)
2544 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2545 newdir
= alloca(newdirlen
+ 1);
2546 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2548 if (ret
< 0 || ret
>= newdirlen
+ 1)
2551 if (!conf
->unexpanded_config
)
2555 lend
= strchr(lstart
, '\n');
2557 lend
= lstart
+ strlen(lstart
);
2561 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2564 p
= strchr(lstart
+ strlen(key
), '=');
2575 /* Whenever an lxc.mount.entry entry is found in a line we check
2576 * if the substring " overlay" or the substring " aufs" is
2577 * present before doing any further work. We check for "
2578 * overlay" and " aufs" since both substrings need to have at
2579 * least one space before them in a valid overlay
2580 * lxc.mount.entry (/A B overlay). When the space before is
2581 * missing it is very likely that these substrings are part of a
2582 * path or something else. (Checking q >= lend ensures that we
2583 * only count matches in the current line.) */
2584 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2585 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2588 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2591 /* replace the olddir with newdir */
2592 if (olddirlen
>= newdirlen
) {
2593 size_t diff
= olddirlen
- newdirlen
;
2594 memcpy(q
, newdir
, newdirlen
);
2595 if (olddirlen
!= newdirlen
) {
2596 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2597 strlen(q
) - newdirlen
- diff
+ 1);
2599 conf
->unexpanded_len
-= diff
;
2603 size_t diff
= newdirlen
- olddirlen
;
2604 size_t oldlen
= conf
->unexpanded_len
;
2605 size_t newlen
= oldlen
+ diff
;
2606 size_t poffset
= q
- conf
->unexpanded_config
;
2608 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2612 conf
->unexpanded_len
= newlen
;
2613 conf
->unexpanded_alloced
= newlen
+ 1;
2614 new[newlen
- 1] = '\0';
2615 lend
= new + (lend
- conf
->unexpanded_config
);
2616 /* Move over the remainder to make room for the newdir.
2618 memmove(new + poffset
+ newdirlen
,
2619 new + poffset
+ olddirlen
,
2620 oldlen
- poffset
- olddirlen
+ 1);
2621 conf
->unexpanded_config
= new;
2622 memcpy(new + poffset
, newdir
, newdirlen
);
2632 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2633 const char *newpath
, const char *oldname
,
2634 const char *newname
)
2637 char *lend
, *newdir
, *olddir
, *p
;
2638 char *lstart
= conf
->unexpanded_config
;
2639 size_t newdirlen
, olddirlen
;
2640 const char *key
= "lxc.hook";
2642 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2643 olddir
= alloca(olddirlen
+ 1);
2644 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2645 if (ret
< 0 || ret
>= olddirlen
+ 1)
2648 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2649 newdir
= alloca(newdirlen
+ 1);
2650 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2651 if (ret
< 0 || ret
>= newdirlen
+ 1)
2654 if (!conf
->unexpanded_config
)
2658 lend
= strchr(lstart
, '\n');
2660 lend
= lstart
+ strlen(lstart
);
2664 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2667 p
= strchr(lstart
+ strlen(key
), '=');
2678 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2681 /* replace the olddir with newdir */
2682 if (olddirlen
>= newdirlen
) {
2683 size_t diff
= olddirlen
- newdirlen
;
2684 memcpy(p
, newdir
, newdirlen
);
2685 if (olddirlen
!= newdirlen
) {
2686 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2687 strlen(p
) - newdirlen
- diff
+ 1);
2689 conf
->unexpanded_len
-= diff
;
2693 size_t diff
= newdirlen
- olddirlen
;
2694 size_t oldlen
= conf
->unexpanded_len
;
2695 size_t newlen
= oldlen
+ diff
;
2696 size_t poffset
= p
- conf
->unexpanded_config
;
2698 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2702 conf
->unexpanded_len
= newlen
;
2703 conf
->unexpanded_alloced
= newlen
+ 1;
2704 new[newlen
- 1] = '\0';
2705 lend
= new + (lend
- conf
->unexpanded_config
);
2706 /* Move over the remainder to make room for the newdir.
2708 memmove(new + poffset
+ newdirlen
,
2709 new + poffset
+ olddirlen
,
2710 oldlen
- poffset
- olddirlen
+ 1);
2711 conf
->unexpanded_config
= new;
2712 memcpy(new + poffset
, newdir
, newdirlen
);
2725 ERROR("Error writing to new config"); \
2730 /* This is called only from clone. We wish to update all hwaddrs in the
2731 * unexpanded config file. We can't/don't want to update any which come from
2732 * lxc.includes (there shouldn't be any).
2733 * We can't just walk the c->lxc-conf->network list because that includes netifs
2734 * from the include files. So we update the ones which we find in the unexp
2735 * config file, then find the original macaddr in the conf->network, and update
2736 * that to the same value.
2738 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2740 char *lend
, *p
, *p2
;
2741 struct lxc_list
*it
;
2742 char *lstart
= conf
->unexpanded_config
;
2744 if (!conf
->unexpanded_config
)
2748 char newhwaddr
[18], oldhwaddr
[17];
2750 lend
= strchr(lstart
, '\n');
2752 lend
= lstart
+ strlen(lstart
);
2756 if (!lxc_config_net_hwaddr(lstart
)) {
2761 p
= strchr(lstart
, '=');
2774 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2777 if ((p2
- p
) != 17) {
2778 WARN("Bad hwaddr entry");
2783 memcpy(oldhwaddr
, p
, 17);
2785 if (!new_hwaddr(newhwaddr
))
2788 memcpy(p
, newhwaddr
, 17);
2789 lxc_list_for_each(it
, &conf
->network
) {
2790 struct lxc_netdev
*n
= it
->elem
;
2792 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2793 memcpy(n
->hwaddr
, newhwaddr
, 17);
2802 static int set_config_ephemeral(const char *key
, const char *value
,
2803 struct lxc_conf
*lxc_conf
, void *data
)
2805 if (lxc_config_value_empty(value
)) {
2806 lxc_conf
->ephemeral
= 0;
2810 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2813 if (lxc_conf
->ephemeral
> 1)
2819 static int set_config_log_syslog(const char *key
, const char *value
,
2820 struct lxc_conf
*lxc_conf
, void *data
)
2824 if (lxc_conf
->syslog
) {
2825 free(lxc_conf
->syslog
);
2826 lxc_conf
->syslog
= NULL
;
2829 if (lxc_config_value_empty(value
))
2832 facility
= lxc_syslog_priority_to_int(value
);
2833 if (facility
== -EINVAL
)
2836 lxc_log_syslog(facility
);
2837 return set_config_string_item(&lxc_conf
->syslog
, value
);
2840 static int set_config_no_new_privs(const char *key
, const char *value
,
2841 struct lxc_conf
*lxc_conf
, void *data
)
2845 if (lxc_config_value_empty(value
)) {
2846 lxc_conf
->no_new_privs
= false;
2850 if (lxc_safe_uint(value
, &v
) < 0)
2856 lxc_conf
->no_new_privs
= v
? true : false;
2861 static int set_config_noop(const char *key
, const char *value
,
2862 struct lxc_conf
*lxc_conf
, void *data
)
2867 /* Callbacks to get configuration items. */
2868 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2869 struct lxc_conf
*c
, void *data
)
2876 memset(retv
, 0, inlen
);
2878 #if HAVE_SYS_PERSONALITY_H
2881 switch (c
->personality
) {
2883 strprint(retv
, inlen
, "i686");
2886 strprint(retv
, inlen
, "x86_64");
2896 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2897 struct lxc_conf
*c
, void *data
)
2899 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2902 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2903 struct lxc_conf
*c
, void *data
)
2905 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2908 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2909 struct lxc_conf
*c
, void *data
)
2911 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2914 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2915 struct lxc_conf
*c
, void *data
)
2917 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2920 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2921 int inlen
, struct lxc_conf
*c
,
2924 return lxc_get_conf_int(c
, retv
, inlen
,
2925 c
->lsm_aa_allow_incomplete
);
2928 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
2929 struct lxc_conf
*c
, void *data
)
2931 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
2934 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2935 * just the value(s) will be printed. Since there still could be more than one,
2936 * it is newline-separated.
2937 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2939 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2940 * 'lxc.cgroup.subsystem.key = value' format.
2942 static int __get_config_cgroup_controller(const char *key
, char *retv
,
2943 int inlen
, struct lxc_conf
*c
,
2947 size_t namespaced_token_len
;
2948 char *global_token
, *namespaced_token
;
2949 struct lxc_list
*it
;
2951 bool get_all
= false;
2956 memset(retv
, 0, inlen
);
2958 if (version
== CGROUP2_SUPER_MAGIC
) {
2959 global_token
= "lxc.cgroup2";
2960 namespaced_token
= "lxc.cgroup2.";
2961 namespaced_token_len
= sizeof("lxc.cgroup2.") - 1;;
2962 } else if (version
== CGROUP_SUPER_MAGIC
) {
2963 global_token
= "lxc.cgroup";
2964 namespaced_token
= "lxc.cgroup.";
2965 namespaced_token_len
= sizeof("lxc.cgroup.") - 1;;
2970 if (strcmp(key
, global_token
) == 0)
2972 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
2973 key
+= namespaced_token_len
;
2977 lxc_list_for_each(it
, &c
->cgroup
) {
2978 struct lxc_cgroup
*cg
= it
->elem
;
2981 if (version
!= cg
->version
)
2984 strprint(retv
, inlen
, "%s.%s = %s\n",
2985 global_token
, cg
->subsystem
, cg
->value
);
2986 } else if (!strcmp(cg
->subsystem
, key
)) {
2987 strprint(retv
, inlen
, "%s\n", cg
->value
);
2994 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
2995 struct lxc_conf
*c
, void *data
)
2997 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
2998 CGROUP_SUPER_MAGIC
);
3001 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3002 struct lxc_conf
*c
, void *data
)
3004 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3005 CGROUP2_SUPER_MAGIC
);
3008 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3009 struct lxc_conf
*lxc_conf
, void *data
)
3017 memset(retv
, 0, inlen
);
3019 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3024 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3025 struct lxc_conf
*c
, void *data
)
3027 struct lxc_list
*it
;
3028 int len
, listlen
, ret
;
3030 /* "u 1000 1000000 65536"
3032 * let's render this as
3050 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3051 char buf
[__LXC_IDMAP_STR_BUF
];
3056 memset(retv
, 0, inlen
);
3058 listlen
= lxc_list_len(&c
->id_map
);
3059 lxc_list_for_each(it
, &c
->id_map
)
3061 struct id_map
*map
= it
->elem
;
3062 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3063 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3064 map
->nsid
, map
->hostid
, map
->range
);
3065 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3068 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3073 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3074 struct lxc_conf
*c
, void *data
)
3077 v
= lxc_log_priority_to_string(c
->loglevel
);
3078 return lxc_get_conf_str(retv
, inlen
, v
);
3081 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3082 struct lxc_conf
*c
, void *data
)
3084 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3087 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3088 struct lxc_conf
*c
, void *data
)
3090 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3093 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3094 struct lxc_conf
*c
, void *data
)
3096 int len
, fulllen
= 0;
3097 const char *sep
= "";
3102 memset(retv
, 0, inlen
);
3104 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3107 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3108 case LXC_AUTO_PROC_MIXED
:
3109 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3112 case LXC_AUTO_PROC_RW
:
3113 strprint(retv
, inlen
, "%sproc:rw", sep
);
3120 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3121 case LXC_AUTO_SYS_RO
:
3122 strprint(retv
, inlen
, "%ssys:ro", sep
);
3125 case LXC_AUTO_SYS_RW
:
3126 strprint(retv
, inlen
, "%ssys:rw", sep
);
3129 case LXC_AUTO_SYS_MIXED
:
3130 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3137 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3138 case LXC_AUTO_CGROUP_NOSPEC
:
3139 strprint(retv
, inlen
, "%scgroup", sep
);
3141 case LXC_AUTO_CGROUP_MIXED
:
3142 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3144 case LXC_AUTO_CGROUP_RO
:
3145 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3147 case LXC_AUTO_CGROUP_RW
:
3148 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3150 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3151 strprint(retv
, inlen
, "%scgroup-full", sep
);
3153 case LXC_AUTO_CGROUP_FULL_MIXED
:
3154 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3156 case LXC_AUTO_CGROUP_FULL_RO
:
3157 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3159 case LXC_AUTO_CGROUP_FULL_RW
:
3160 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3169 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3170 struct lxc_conf
*c
, void *data
)
3172 int len
, fulllen
= 0;
3173 struct lxc_list
*it
;
3178 memset(retv
, 0, inlen
);
3180 lxc_list_for_each(it
, &c
->mount_list
) {
3181 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3187 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3188 struct lxc_conf
*c
, void *data
)
3190 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3193 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3194 struct lxc_conf
*c
, void *data
)
3196 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3199 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3200 struct lxc_conf
*c
, void *data
)
3202 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3205 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3206 struct lxc_conf
*c
, void *data
)
3211 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3212 struct lxc_conf
*c
, void *data
)
3214 return lxc_get_conf_str(
3216 c
->utsname
? c
->utsname
->nodename
: NULL
);
3219 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3220 struct lxc_conf
*c
, void *data
)
3223 int len
, fulllen
= 0, found
= -1;
3224 struct lxc_list
*it
;
3227 subkey
= strchr(key
, '.');
3229 subkey
= strchr(subkey
+ 1, '.');
3235 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3236 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3247 memset(retv
, 0, inlen
);
3249 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3250 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3255 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3256 struct lxc_conf
*c
, void *data
)
3258 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3261 static int get_config_net(const char *key
, char *retv
, int inlen
,
3262 struct lxc_conf
*c
, void *data
)
3264 int len
, fulllen
= 0;
3265 struct lxc_list
*it
;
3270 memset(retv
, 0, inlen
);
3272 lxc_list_for_each(it
, &c
->network
) {
3273 struct lxc_netdev
*n
= it
->elem
;
3274 const char *t
= lxc_net_type_to_str(n
->type
);
3275 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3281 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3282 struct lxc_conf
*c
, void *data
)
3284 int len
, fulllen
= 0;
3285 struct lxc_list
*it
;
3290 memset(retv
, 0, inlen
);
3292 lxc_list_for_each(it
, &c
->caps
) {
3293 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3299 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3300 struct lxc_conf
*c
, void *data
)
3302 int len
, fulllen
= 0;
3303 struct lxc_list
*it
;
3308 memset(retv
, 0, inlen
);
3310 lxc_list_for_each(it
, &c
->keepcaps
) {
3311 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3317 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3318 struct lxc_conf
*c
, void *data
)
3320 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3323 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3324 struct lxc_conf
*c
, void *data
)
3326 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3329 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3330 struct lxc_conf
*c
, void *data
)
3332 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3336 static int get_config_console_buffer_size(const char *key
, char *retv
,
3337 int inlen
, struct lxc_conf
*c
,
3340 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3343 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3344 int inlen
, struct lxc_conf
*c
,
3347 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3350 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3351 struct lxc_conf
*c
, void *data
)
3353 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3356 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3357 struct lxc_conf
*c
, void *data
)
3359 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3362 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3363 struct lxc_conf
*c
, void *data
)
3365 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3368 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3369 struct lxc_conf
*c
, void *data
)
3371 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3374 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3375 struct lxc_conf
*c
, void *data
)
3377 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3380 static int get_config_start(const char *key
, char *retv
, int inlen
,
3381 struct lxc_conf
*c
, void *data
)
3383 if (strcmp(key
+ 10, "auto") == 0)
3384 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3385 else if (strcmp(key
+ 10, "delay") == 0)
3386 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3387 else if (strcmp(key
+ 10, "order") == 0)
3388 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3393 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3394 struct lxc_conf
*c
, void *data
)
3396 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3399 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3400 struct lxc_conf
*c
, void *data
)
3402 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3405 static int get_config_group(const char *key
, char *retv
, int inlen
,
3406 struct lxc_conf
*c
, void *data
)
3408 int len
, fulllen
= 0;
3409 struct lxc_list
*it
;
3414 memset(retv
, 0, inlen
);
3416 lxc_list_for_each(it
, &c
->groups
) {
3417 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3423 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3424 struct lxc_conf
*c
, void *data
)
3426 int len
, fulllen
= 0;
3427 struct lxc_list
*it
;
3432 memset(retv
, 0, inlen
);
3434 lxc_list_for_each(it
, &c
->environment
) {
3435 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3441 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3442 struct lxc_conf
*c
, void *data
)
3444 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3447 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3448 struct lxc_conf
*c
, void *data
)
3450 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3453 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3454 struct lxc_conf
*c
, void *data
)
3456 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3459 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3460 struct lxc_conf
*c
, void *data
)
3462 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3465 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3466 struct lxc_conf
*c
, void *data
)
3468 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3471 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3472 struct lxc_conf
*c
, void *data
)
3474 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3477 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3478 struct lxc_conf
*c
, void *data
)
3480 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3483 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3484 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3485 * printed, in 'lxc.prlimit.resource = value' format.
3487 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3488 struct lxc_conf
*c
, void *data
)
3490 int fulllen
= 0, len
;
3491 bool get_all
= false;
3492 struct lxc_list
*it
;
3497 memset(retv
, 0, inlen
);
3499 if (!strcmp(key
, "lxc.prlimit"))
3501 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3506 lxc_list_for_each(it
, &c
->limits
) {
3507 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3508 integers or the word
3511 struct lxc_limit
*lim
= it
->elem
;
3513 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3514 memcpy(buf
, "unlimited", sizeof("unlimited"));
3515 partlen
= sizeof("unlimited") - 1;
3517 partlen
= sprintf(buf
, "%" PRIu64
,
3518 (uint64_t)lim
->limit
.rlim_cur
);
3520 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3521 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3522 memcpy(buf
+ partlen
, ":unlimited",
3523 sizeof(":unlimited"));
3525 sprintf(buf
+ partlen
, ":%" PRIu64
,
3526 (uint64_t)lim
->limit
.rlim_max
);
3530 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3531 lim
->resource
, buf
);
3532 } else if (!strcmp(lim
->resource
, key
)) {
3533 strprint(retv
, inlen
, "%s", buf
);
3540 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3541 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3542 * entries will be printed, in 'lxc.sysctl.key = value' format.
3544 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3545 struct lxc_conf
*c
, void *data
)
3548 struct lxc_list
*it
;
3550 bool get_all
= false;
3555 memset(retv
, 0, inlen
);
3557 if (strcmp(key
, "lxc.sysctl") == 0)
3559 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3560 key
+= sizeof("lxc.sysctl.") - 1;
3564 lxc_list_for_each(it
, &c
->sysctls
) {
3565 struct lxc_sysctl
*elem
= it
->elem
;
3567 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3568 elem
->key
, elem
->value
);
3569 } else if (strcmp(elem
->key
, key
) == 0) {
3570 strprint(retv
, inlen
, "%s", elem
->value
);
3577 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3578 struct lxc_conf
*c
, void *data
)
3580 struct lxc_list
*it
;
3583 bool get_all
= false;
3588 memset(retv
, 0, inlen
);
3590 if (strcmp(key
, "lxc.proc") == 0)
3592 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3593 key
+= sizeof("lxc.proc.") - 1;
3597 lxc_list_for_each(it
, &c
->procs
) {
3598 struct lxc_proc
*proc
= it
->elem
;
3601 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3602 proc
->filename
, proc
->value
);
3603 } else if (strcmp(proc
->filename
, key
) == 0) {
3604 strprint(retv
, inlen
, "%s", proc
->value
);
3611 static int get_config_noop(const char *key
, char *retv
, int inlen
,
3612 struct lxc_conf
*c
, void *data
)
3617 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3618 struct lxc_conf
*c
, void *data
)
3621 const char *namespace;
3627 memset(retv
, 0, inlen
);
3629 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
3630 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3634 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3639 /* Callbacks to clear config items. */
3640 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3643 c
->personality
= -1;
3647 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3654 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3661 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3669 static inline int clr_config_apparmor_profile(const char *key
,
3670 struct lxc_conf
*c
, void *data
)
3672 free(c
->lsm_aa_profile
);
3673 c
->lsm_aa_profile
= NULL
;
3677 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3681 c
->lsm_aa_allow_incomplete
= 0;
3685 static inline int clr_config_selinux_context(const char *key
,
3686 struct lxc_conf
*c
, void *data
)
3688 free(c
->lsm_se_context
);
3689 c
->lsm_se_context
= NULL
;
3693 static inline int clr_config_cgroup_controller(const char *key
,
3694 struct lxc_conf
*c
, void *data
)
3696 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3699 static inline int clr_config_cgroup2_controller(const char *key
,
3700 struct lxc_conf
*c
, void *data
)
3702 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3705 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3708 if (lxc_conf
->cgroup_meta
.dir
) {
3709 free(lxc_conf
->cgroup_meta
.dir
);
3710 lxc_conf
->cgroup_meta
.dir
= NULL
;
3716 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3719 return lxc_clear_idmaps(c
);
3722 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3725 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3729 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3737 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3740 return lxc_clear_mount_entries(c
);
3743 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3746 return lxc_clear_automounts(c
);
3749 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3757 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3760 free(c
->rootfs
.path
);
3761 c
->rootfs
.path
= NULL
;
3765 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3768 free(c
->rootfs
.mount
);
3769 c
->rootfs
.mount
= NULL
;
3773 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3776 free(c
->rootfs
.options
);
3777 c
->rootfs
.options
= NULL
;
3781 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3787 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3795 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3798 return lxc_clear_hooks(c
, key
);
3801 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
3804 /* default to legacy hooks version */
3805 c
->hooks_version
= 0;
3809 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3812 lxc_free_networks(&c
->network
);
3817 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3820 return lxc_clear_config_caps(c
);
3823 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3826 return lxc_clear_config_keepcaps(c
);
3829 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3832 free(c
->console
.path
);
3833 c
->console
.path
= NULL
;
3837 static inline int clr_config_console_logfile(const char *key
,
3838 struct lxc_conf
*c
, void *data
)
3840 free(c
->console
.log_path
);
3841 c
->console
.log_path
= NULL
;
3845 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3848 c
->console
.log_rotate
= 0;
3852 static inline int clr_config_console_buffer_size(const char *key
,
3853 struct lxc_conf
*c
, void *data
)
3855 c
->console
.buffer_size
= 0;
3859 static inline int clr_config_console_buffer_logfile(const char *key
,
3863 free(c
->console
.buffer_log_file
);
3864 c
->console
.buffer_log_file
= NULL
;
3868 static inline int clr_config_seccomp_profile(const char *key
,
3869 struct lxc_conf
*c
, void *data
)
3876 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3883 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
3890 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
3893 c
->rebootsignal
= 0;
3897 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
3904 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
3907 if (strcmp(key
+ 10, "auto") == 0)
3909 else if (strcmp(key
+ 10, "delay") == 0)
3911 else if (strcmp(key
+ 10, "order") == 0)
3917 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
3925 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
3928 c
->monitor_unshare
= 0;
3932 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
3935 return lxc_clear_groups(c
);
3938 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
3941 return lxc_clear_environment(c
);
3944 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
3947 free(c
->execute_cmd
);
3948 c
->execute_cmd
= NULL
;
3952 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
3960 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
3968 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
3975 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
3982 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
3989 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
3992 c
->no_new_privs
= false;
3996 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
3999 return lxc_clear_limits(c
, key
);
4002 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4005 return lxc_clear_sysctls(c
, key
);
4008 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4011 return lxc_clear_procs(c
, key
);
4014 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4017 lxc_clear_includes(c
);
4021 static inline int clr_config_noop(const char *key
, struct lxc_conf
*c
,
4027 static int clr_config_namespace_share(const char *key
,
4028 struct lxc_conf
*lxc_conf
, void *data
)
4031 const char *namespace;
4033 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
4034 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4038 free(lxc_conf
->ns_share
[ns_idx
]);
4039 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4044 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4045 struct lxc_conf
*c
, void *data
)
4050 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4051 struct lxc_conf
*lxc_conf
,
4053 char **deindexed_key
)
4056 unsigned int tmpidx
;
4058 char *copy
, *idx_start
, *idx_end
;
4059 struct lxc_config_t
*config
= NULL
;
4061 /* check that this is a sensible network key */
4062 if (strncmp("lxc.net.", key
, 8)) {
4063 ERROR("Invalid network configuration key \"%s\"", key
);
4069 ERROR("Failed to duplicate string \"%s\"", key
);
4074 if (!isdigit(*(key
+ 8))) {
4075 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4079 /* beginning of index string */
4080 idx_start
= (copy
+ 7);
4083 /* end of index string */
4084 idx_end
= strchr((copy
+ 8), '.');
4088 /* parse current index */
4089 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4091 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4092 idx_start
+ 1, strerror(-ret
));
4097 /* This, of course is utterly nonsensical on so many levels, but
4098 * better safe than sorry.
4099 * (Checking for INT_MAX here is intentional.)
4101 if (tmpidx
== INT_MAX
) {
4102 SYSERROR("Number of configured networks would overflow the "
4108 numstrlen
= strlen((idx_start
+ 1));
4110 /* repair configuration key */
4113 /* lxc.net.<idx>.<subkey> */
4116 if (strlen(idx_end
+ 1) == 0) {
4117 ERROR("No subkey in network configuration key \"%s\"", key
);
4121 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4122 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4124 config
= lxc_get_config(copy
);
4126 ERROR("Unknown network configuration key \"%s\"", key
);
4132 *deindexed_key
= copy
;
4141 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4142 * found. So we make sure next comes an integer, find the right callback (by
4143 * rewriting the key), and call it.
4145 static int set_config_net_nic(const char *key
, const char *value
,
4146 struct lxc_conf
*lxc_conf
, void *data
)
4149 const char *idxstring
;
4150 struct lxc_config_t
*config
;
4151 struct lxc_netdev
*netdev
;
4153 char *deindexed_key
= NULL
;
4155 idxstring
= key
+ 8;
4156 if (!isdigit(*idxstring
))
4159 if (lxc_config_value_empty(value
))
4160 return clr_config_net_nic(key
, lxc_conf
, data
);
4162 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4163 if (!config
|| idx
< 0)
4166 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4168 free(deindexed_key
);
4172 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4173 free(deindexed_key
);
4177 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4181 const char *idxstring
;
4182 struct lxc_config_t
*config
;
4183 struct lxc_netdev
*netdev
;
4185 char *deindexed_key
= NULL
;
4187 idxstring
= key
+ 8;
4188 if (!isdigit(*idxstring
))
4191 /* The left conjunct is pretty self-explanatory. The right conjunct
4192 * checks whether the two pointers are equal. If they are we know that
4193 * this is not a key that is namespaced any further and so we are
4194 * supposed to clear the whole network.
4196 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4197 unsigned int rmnetdevidx
;
4199 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4202 /* Remove network from network list. */
4203 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4207 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4208 if (!config
|| idx
< 0)
4211 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4213 free(deindexed_key
);
4217 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4218 free(deindexed_key
);
4222 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4225 struct lxc_netdev
*netdev
= data
;
4235 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4238 struct lxc_netdev
*netdev
= data
;
4243 netdev
->name
[0] = '\0';
4248 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4251 struct lxc_netdev
*netdev
= data
;
4261 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4264 struct lxc_netdev
*netdev
= data
;
4269 netdev
->link
[0] = '\0';
4274 static int clr_config_net_macvlan_mode(const char *key
,
4275 struct lxc_conf
*lxc_conf
, void *data
)
4277 struct lxc_netdev
*netdev
= data
;
4282 if (netdev
->type
!= LXC_NET_MACVLAN
)
4285 netdev
->priv
.macvlan_attr
.mode
= -1;
4290 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4293 struct lxc_netdev
*netdev
= data
;
4298 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4303 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4306 struct lxc_netdev
*netdev
= data
;
4311 free(netdev
->upscript
);
4312 netdev
->upscript
= NULL
;
4317 static int clr_config_net_script_down(const char *key
,
4318 struct lxc_conf
*lxc_conf
, void *data
)
4320 struct lxc_netdev
*netdev
= data
;
4325 free(netdev
->downscript
);
4326 netdev
->downscript
= NULL
;
4331 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4334 struct lxc_netdev
*netdev
= data
;
4339 free(netdev
->hwaddr
);
4340 netdev
->hwaddr
= NULL
;
4345 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4348 struct lxc_netdev
*netdev
= data
;
4359 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4362 struct lxc_netdev
*netdev
= data
;
4367 netdev
->priv
.vlan_attr
.vid
= 0;
4372 static int clr_config_net_ipv4_gateway(const char *key
,
4373 struct lxc_conf
*lxc_conf
, void *data
)
4375 struct lxc_netdev
*netdev
= data
;
4380 free(netdev
->ipv4_gateway
);
4381 netdev
->ipv4_gateway
= NULL
;
4386 static int clr_config_net_ipv4_address(const char *key
,
4387 struct lxc_conf
*lxc_conf
, void *data
)
4389 struct lxc_netdev
*netdev
= data
;
4390 struct lxc_list
*cur
, *next
;
4395 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4404 static int clr_config_net_ipv6_gateway(const char *key
,
4405 struct lxc_conf
*lxc_conf
, void *data
)
4407 struct lxc_netdev
*netdev
= data
;
4412 free(netdev
->ipv6_gateway
);
4413 netdev
->ipv6_gateway
= NULL
;
4418 static int clr_config_net_ipv6_address(const char *key
,
4419 struct lxc_conf
*lxc_conf
, void *data
)
4421 struct lxc_netdev
*netdev
= data
;
4422 struct lxc_list
*cur
, *next
;
4427 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4436 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4437 struct lxc_conf
*c
, void *data
)
4440 const char *idxstring
;
4441 struct lxc_config_t
*config
;
4442 struct lxc_netdev
*netdev
;
4444 char *deindexed_key
= NULL
;
4446 idxstring
= key
+ 8;
4447 if (!isdigit(*idxstring
))
4450 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4451 if (!config
|| idx
< 0)
4454 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4456 free(deindexed_key
);
4460 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4461 free(deindexed_key
);
4465 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4466 struct lxc_conf
*c
, void *data
)
4470 struct lxc_netdev
*netdev
= data
;
4475 memset(retv
, 0, inlen
);
4480 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4485 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4486 struct lxc_conf
*c
, void *data
)
4490 struct lxc_netdev
*netdev
= data
;
4495 memset(retv
, 0, inlen
);
4500 if (netdev
->flags
& IFF_UP
)
4501 strprint(retv
, inlen
, "up");
4506 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4507 struct lxc_conf
*c
, void *data
)
4511 struct lxc_netdev
*netdev
= data
;
4516 memset(retv
, 0, inlen
);
4521 if (netdev
->link
[0] != '\0')
4522 strprint(retv
, inlen
, "%s", netdev
->link
);
4527 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4528 struct lxc_conf
*c
, void *data
)
4532 struct lxc_netdev
*netdev
= data
;
4537 memset(retv
, 0, inlen
);
4542 if (netdev
->name
[0] != '\0')
4543 strprint(retv
, inlen
, "%s", netdev
->name
);
4548 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4549 struct lxc_conf
*c
, void *data
)
4554 struct lxc_netdev
*netdev
= data
;
4559 memset(retv
, 0, inlen
);
4564 if (netdev
->type
!= LXC_NET_MACVLAN
)
4567 switch (netdev
->priv
.macvlan_attr
.mode
) {
4568 case MACVLAN_MODE_PRIVATE
:
4571 case MACVLAN_MODE_VEPA
:
4574 case MACVLAN_MODE_BRIDGE
:
4577 case MACVLAN_MODE_PASSTHRU
:
4585 strprint(retv
, inlen
, "%s", mode
);
4590 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4591 struct lxc_conf
*c
, void *data
)
4595 struct lxc_netdev
*netdev
= data
;
4600 memset(retv
, 0, inlen
);
4605 if (netdev
->type
!= LXC_NET_VETH
)
4608 strprint(retv
, inlen
, "%s",
4609 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4610 ? netdev
->priv
.veth_attr
.pair
4611 : netdev
->priv
.veth_attr
.veth1
);
4616 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4617 struct lxc_conf
*c
, void *data
)
4621 struct lxc_netdev
*netdev
= data
;
4626 memset(retv
, 0, inlen
);
4631 if (netdev
->upscript
)
4632 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4637 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4638 struct lxc_conf
*c
, void *data
)
4642 struct lxc_netdev
*netdev
= data
;
4647 memset(retv
, 0, inlen
);
4652 if (netdev
->downscript
)
4653 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4658 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4659 struct lxc_conf
*c
, void *data
)
4663 struct lxc_netdev
*netdev
= data
;
4668 memset(retv
, 0, inlen
);
4674 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4679 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4680 struct lxc_conf
*c
, void *data
)
4684 struct lxc_netdev
*netdev
= data
;
4689 memset(retv
, 0, inlen
);
4695 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4700 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4701 struct lxc_conf
*c
, void *data
)
4705 struct lxc_netdev
*netdev
= data
;
4710 memset(retv
, 0, inlen
);
4715 if (netdev
->type
!= LXC_NET_VLAN
)
4718 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4723 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4724 struct lxc_conf
*c
, void *data
)
4727 char buf
[INET_ADDRSTRLEN
];
4729 struct lxc_netdev
*netdev
= data
;
4734 memset(retv
, 0, inlen
);
4739 if (netdev
->ipv4_gateway_auto
) {
4740 strprint(retv
, inlen
, "auto");
4741 } else if (netdev
->ipv4_gateway
) {
4742 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4743 strprint(retv
, inlen
, "%s", buf
);
4749 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4750 struct lxc_conf
*c
, void *data
)
4754 char buf
[INET_ADDRSTRLEN
];
4755 struct lxc_list
*it
;
4757 struct lxc_netdev
*netdev
= data
;
4762 memset(retv
, 0, inlen
);
4767 listlen
= lxc_list_len(&netdev
->ipv4
);
4768 lxc_list_for_each(it
, &netdev
->ipv4
) {
4769 struct lxc_inetdev
*i
= it
->elem
;
4770 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4771 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4772 (listlen
-- > 1) ? "\n" : "");
4778 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4779 struct lxc_conf
*c
, void *data
)
4782 char buf
[INET6_ADDRSTRLEN
];
4784 struct lxc_netdev
*netdev
= data
;
4789 memset(retv
, 0, inlen
);
4794 if (netdev
->ipv6_gateway_auto
) {
4795 strprint(retv
, inlen
, "auto");
4796 } else if (netdev
->ipv6_gateway
) {
4797 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4798 strprint(retv
, inlen
, "%s", buf
);
4804 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4805 struct lxc_conf
*c
, void *data
)
4809 char buf
[INET6_ADDRSTRLEN
];
4810 struct lxc_list
*it
;
4812 struct lxc_netdev
*netdev
= data
;
4817 memset(retv
, 0, inlen
);
4822 listlen
= lxc_list_len(&netdev
->ipv6
);
4823 lxc_list_for_each(it
, &netdev
->ipv6
) {
4824 struct lxc_inet6dev
*i
= it
->elem
;
4825 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4826 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4827 (listlen
-- > 1) ? "\n" : "");
4833 int lxc_list_config_items(char *retv
, int inlen
)
4842 memset(retv
, 0, inlen
);
4844 for (i
= 0; i
< config_size
; i
++) {
4845 char *s
= config
[i
].name
;
4847 if (s
[strlen(s
) - 1] == '.')
4850 strprint(retv
, inlen
, "%s\n", s
);
4856 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4865 memset(retv
, 0, inlen
);
4867 if (!strcmp(key
, "lxc.apparmor")) {
4868 strprint(retv
, inlen
, "allow_incomplete\n");
4869 strprint(retv
, inlen
, "profile\n");
4870 } else if (!strcmp(key
, "lxc.cgroup")) {
4871 strprint(retv
, inlen
, "dir\n");
4872 } else if (!strcmp(key
, "lxc.selinux")) {
4873 strprint(retv
, inlen
, "context\n");
4874 } else if (!strcmp(key
, "lxc.mount")) {
4875 strprint(retv
, inlen
, "auto\n");
4876 strprint(retv
, inlen
, "entry\n");
4877 strprint(retv
, inlen
, "fstab\n");
4878 } else if (!strcmp(key
, "lxc.rootfs")) {
4879 strprint(retv
, inlen
, "mount\n");
4880 strprint(retv
, inlen
, "options\n");
4881 strprint(retv
, inlen
, "path\n");
4882 } else if (!strcmp(key
, "lxc.uts")) {
4883 strprint(retv
, inlen
, "name\n");
4884 } else if (!strcmp(key
, "lxc.hook")) {
4885 strprint(retv
, inlen
, "autodev\n");
4886 strprint(retv
, inlen
, "clone\n");
4887 strprint(retv
, inlen
, "destroy\n");
4888 strprint(retv
, inlen
, "mount\n");
4889 strprint(retv
, inlen
, "post-stop\n");
4890 strprint(retv
, inlen
, "pre-mount\n");
4891 strprint(retv
, inlen
, "pre-start\n");
4892 strprint(retv
, inlen
, "start-host\n");
4893 strprint(retv
, inlen
, "start\n");
4894 strprint(retv
, inlen
, "stop\n");
4895 } else if (!strcmp(key
, "lxc.cap")) {
4896 strprint(retv
, inlen
, "drop\n");
4897 strprint(retv
, inlen
, "keep\n");
4898 } else if (!strcmp(key
, "lxc.console")) {
4899 strprint(retv
, inlen
, "logfile\n");
4900 strprint(retv
, inlen
, "path\n");
4901 } else if (!strcmp(key
, "lxc.seccomp")) {
4902 strprint(retv
, inlen
, "profile\n");
4903 } else if (!strcmp(key
, "lxc.signal")) {
4904 strprint(retv
, inlen
, "halt\n");
4905 strprint(retv
, inlen
, "reboot\n");
4906 strprint(retv
, inlen
, "stop\n");
4907 } else if (!strcmp(key
, "lxc.start")) {
4908 strprint(retv
, inlen
, "auto\n");
4909 strprint(retv
, inlen
, "delay\n");
4910 strprint(retv
, inlen
, "order\n");
4911 } else if (!strcmp(key
, "lxc.monitor")) {
4912 strprint(retv
, inlen
, "unshare\n");
4920 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
4923 const char *idxstring
;
4924 struct lxc_netdev
*netdev
;
4928 idxstring
= key
+ 8;
4929 if (!isdigit(*idxstring
))
4932 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
4936 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4943 memset(retv
, 0, inlen
);
4945 strprint(retv
, inlen
, "type\n");
4946 strprint(retv
, inlen
, "script.up\n");
4947 strprint(retv
, inlen
, "script.down\n");
4948 if (netdev
->type
!= LXC_NET_EMPTY
) {
4949 strprint(retv
, inlen
, "flags\n");
4950 strprint(retv
, inlen
, "link\n");
4951 strprint(retv
, inlen
, "name\n");
4952 strprint(retv
, inlen
, "hwaddr\n");
4953 strprint(retv
, inlen
, "mtu\n");
4954 strprint(retv
, inlen
, "ipv6.address\n");
4955 strprint(retv
, inlen
, "ipv6.gateway\n");
4956 strprint(retv
, inlen
, "ipv4.address\n");
4957 strprint(retv
, inlen
, "ipv4.gateway\n");
4960 switch (netdev
->type
) {
4962 strprint(retv
, inlen
, "veth.pair\n");
4964 case LXC_NET_MACVLAN
:
4965 strprint(retv
, inlen
, "macvlan.mode\n");
4968 strprint(retv
, inlen
, "vlan.id\n");