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_clone
);
111 lxc_config_define(namespace_keep
);
112 lxc_config_define(namespace_share
);
113 lxc_config_define(net
);
114 lxc_config_define(net_flags
);
115 lxc_config_define(net_hwaddr
);
116 lxc_config_define(net_ipv4_address
);
117 lxc_config_define(net_ipv4_gateway
);
118 lxc_config_define(net_ipv6_address
);
119 lxc_config_define(net_ipv6_gateway
);
120 lxc_config_define(net_link
);
121 lxc_config_define(net_macvlan_mode
);
122 lxc_config_define(net_mtu
);
123 lxc_config_define(net_name
);
124 lxc_config_define(net_nic
);
125 lxc_config_define(net_script_down
);
126 lxc_config_define(net_script_up
);
127 lxc_config_define(net_type
);
128 lxc_config_define(net_veth_pair
);
129 lxc_config_define(net_vlan_id
);
130 lxc_config_define(no_new_privs
);
131 lxc_config_define(noop
);
132 lxc_config_define(personality
);
133 lxc_config_define(prlimit
);
134 lxc_config_define(pty_max
);
135 lxc_config_define(rootfs_backend
);
136 lxc_config_define(rootfs_mount
);
137 lxc_config_define(rootfs_options
);
138 lxc_config_define(rootfs_path
);
139 lxc_config_define(seccomp_profile
);
140 lxc_config_define(selinux_context
);
141 lxc_config_define(signal_halt
);
142 lxc_config_define(signal_reboot
);
143 lxc_config_define(signal_stop
);
144 lxc_config_define(start
);
145 lxc_config_define(tty_max
);
146 lxc_config_define(tty_dir
);
147 lxc_config_define(uts_name
);
148 lxc_config_define(sysctl
);
149 lxc_config_define(proc
);
151 static struct lxc_config_t config
[] = {
152 /* REMOVE in LXC 3.0 */
153 { "lxc.arch", false, set_config_personality
, get_config_personality
, clr_config_personality
, },
154 { "lxc.apparmor.profile", false, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
155 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
156 { "lxc.autodev", false, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
157 { "lxc.cap.drop", false, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
158 { "lxc.cap.keep", false, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
159 { "lxc.cgroup2", false, set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
160 { "lxc.cgroup.dir", false, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
161 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
162 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile
, get_config_console_buffer_logfile
, clr_config_console_buffer_logfile
, },
163 { "lxc.console.buffer.size", false, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
164 { "lxc.console.logfile", false, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
165 { "lxc.console.path", false, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
166 { "lxc.console.rotate", false, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
167 { "lxc.environment", false, set_config_environment
, get_config_environment
, clr_config_environment
, },
168 { "lxc.ephemeral", false, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
169 { "lxc.execute.cmd", false, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
170 { "lxc.group", false, set_config_group
, get_config_group
, clr_config_group
, },
171 { "lxc.hook.autodev", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
172 { "lxc.hook.clone", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
173 { "lxc.hook.destroy", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
174 { "lxc.hook.mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
175 { "lxc.hook.post-stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
176 { "lxc.hook.pre-mount", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
177 { "lxc.hook.pre-start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
178 { "lxc.hook.start", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
179 { "lxc.hook.start-host", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
180 { "lxc.hook.stop", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
181 { "lxc.hook.version", false, set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
182 { "lxc.hook", false, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
183 { "lxc.idmap", false, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
184 { "lxc.include", false, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
185 { "lxc.init.cmd", false, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
186 { "lxc.init.gid", false, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
187 { "lxc.init.uid", false, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
188 { "lxc.init.cwd", false, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
189 { "lxc.log.file", false, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
190 { "lxc.log.level", false, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
191 { "lxc.log.syslog", false, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
192 { "lxc.monitor.unshare", false, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
193 { "lxc.mount.auto", false, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
194 { "lxc.mount.entry", false, set_config_mount
, get_config_mount
, clr_config_mount
, },
195 { "lxc.mount.fstab", false, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
196 { "lxc.namespace.clone", false, set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
197 { "lxc.namespace.keep", false, set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
198 { "lxc.namespace.share", false, set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
200 /* [START]: REMOVE IN LXC 3.0 */
201 { "lxc.network.type", true, set_config_network_legacy_type
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
202 { "lxc.network.flags", true, set_config_network_legacy_flags
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
203 { "lxc.network.link", true, set_config_network_legacy_link
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
204 { "lxc.network.name", true, set_config_network_legacy_name
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
205 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
206 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
207 { "lxc.network.script.up", true, set_config_network_legacy_script_up
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
208 { "lxc.network.script.down", true, set_config_network_legacy_script_down
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
209 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
210 { "lxc.network.mtu", true, set_config_network_legacy_mtu
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
211 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
212 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
213 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
214 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
215 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
216 { "lxc.network.", true, set_config_network_legacy_nic
, get_config_network_legacy_item
, clr_config_network_legacy_item
, },
217 { "lxc.network", true, set_config_network_legacy
, get_config_network_legacy
, clr_config_network_legacy
, },
218 /* [END]: REMOVE IN LXC 3.0 */
220 { "lxc.net.flags", false, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
221 { "lxc.net.hwaddr", false, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
222 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
223 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
224 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
225 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
226 { "lxc.net.link", false, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
227 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
228 { "lxc.net.mtu", false, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
229 { "lxc.net.name", false, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
230 { "lxc.net.script.down", false, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
231 { "lxc.net.script.up", false, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
232 { "lxc.net.type", false, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
233 { "lxc.net.vlan.id", false, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
234 { "lxc.net.veth.pair", false, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
235 { "lxc.net.", false, set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
236 { "lxc.net", false, set_config_net
, get_config_net
, clr_config_net
, },
237 { "lxc.no_new_privs", false, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
238 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
239 { "lxc.pty.max", false, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
240 { "lxc.rootfs.mount", false, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
241 { "lxc.rootfs.options", false, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
242 { "lxc.rootfs.path", false, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
243 { "lxc.seccomp.profile", false, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
244 { "lxc.selinux.context", false, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
245 { "lxc.signal.halt", false, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
246 { "lxc.signal.reboot", false, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
247 { "lxc.signal.stop", false, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
248 { "lxc.start.auto", false, set_config_start
, get_config_start
, clr_config_start
, },
249 { "lxc.start.delay", false, set_config_start
, get_config_start
, clr_config_start
, },
250 { "lxc.start.order", false, set_config_start
, get_config_start
, clr_config_start
, },
251 { "lxc.tty.dir", false, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
252 { "lxc.tty.max", false, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
253 { "lxc.uts.name", false, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
254 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
255 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
257 /* [START]: REMOVE IN LXC 3.0 */
258 { "lxc.pts", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
259 { "lxc.devttydir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
260 { "lxc.tty", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
261 { "lxc.aa_profile", true, set_config_lsm_aa_profile
, get_config_lsm_aa_profile
, clr_config_lsm_aa_profile
, },
262 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete
, get_config_lsm_aa_incomplete
, clr_config_lsm_aa_incomplete
, },
263 { "lxc.se_context", true, set_config_lsm_se_context
, get_config_lsm_se_context
, clr_config_lsm_se_context
, },
264 { "lxc.id_map", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
265 { "lxc.mount", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
266 { "lxc.rootfs.backend", true, set_config_rootfs_backend
, get_config_rootfs_backend
, clr_config_rootfs_backend
, },
267 { "lxc.rootfs", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
268 { "lxc.utsname", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
269 { "lxc.seccomp", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
270 { "lxc.console", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
271 { "lxc.haltsignal", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
272 { "lxc.rebootsignal", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
273 { "lxc.stopsignal", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
274 { "lxc.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
275 { "lxc.kmsg", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
276 { "lxc.loglevel", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
277 { "lxc.logfile", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
278 { "lxc.init_cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
279 { "lxc.init_uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
280 { "lxc.init_gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
281 { "lxc.limit", true, set_config_limit
, get_config_limit
, clr_config_limit
, },
282 { "lxc.pivotdir", true, set_config_noop
, get_config_noop
, clr_config_noop
, },
283 /* [END]: REMOVE IN LXC 3.0 */
291 static const struct signame signames
[] = {
324 { SIGSTKFLT
, "STKFLT" },
339 { SIGVTALRM
, "VTALRM" },
345 { SIGWINCH
, "WINCH" },
363 { SIGUNUSED
, "UNUSED" },
370 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
372 struct lxc_config_t
*lxc_get_config(const char *key
)
376 for (i
= 0; i
< config_size
; i
++)
377 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
383 static int set_config_net(const char *key
, const char *value
,
384 struct lxc_conf
*lxc_conf
, void *data
)
386 if (!lxc_config_value_empty(value
)) {
387 ERROR("lxc.net must not have a value");
391 return clr_config_net(key
, lxc_conf
, data
);
394 static int set_config_net_type(const char *key
, const char *value
,
395 struct lxc_conf
*lxc_conf
, void *data
)
397 struct lxc_netdev
*netdev
= data
;
399 if (lxc_config_value_empty(value
))
400 return clr_config_net_type(key
, lxc_conf
, data
);
405 if (!strcmp(value
, "veth")) {
406 netdev
->type
= LXC_NET_VETH
;
407 } else if (!strcmp(value
, "macvlan")) {
408 netdev
->type
= LXC_NET_MACVLAN
;
409 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
411 } else if (!strcmp(value
, "vlan")) {
412 netdev
->type
= LXC_NET_VLAN
;
413 } else if (!strcmp(value
, "phys")) {
414 netdev
->type
= LXC_NET_PHYS
;
415 } else if (!strcmp(value
, "empty")) {
416 netdev
->type
= LXC_NET_EMPTY
;
417 } else if (!strcmp(value
, "none")) {
418 netdev
->type
= LXC_NET_NONE
;
420 ERROR("invalid network type %s", value
);
427 static int set_config_net_flags(const char *key
, const char *value
,
428 struct lxc_conf
*lxc_conf
, void *data
)
430 struct lxc_netdev
*netdev
= data
;
432 if (lxc_config_value_empty(value
))
433 return clr_config_net_flags(key
, lxc_conf
, data
);
438 netdev
->flags
|= IFF_UP
;
443 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
444 struct lxc_netdev
*netdev
)
446 struct ifaddrs
*ifaddr
, *ifa
;
449 const char *type_key
= "lxc.net.type";
450 const char *link_key
= "lxc.net.link";
451 const char *tmpvalue
= "phys";
453 if (getifaddrs(&ifaddr
) == -1) {
454 SYSERROR("Get network interfaces failed");
458 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
461 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
464 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
465 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
468 ret
= set_config_net_link(
469 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
471 ERROR("failed to create matched ifnames");
475 ERROR("failed to create matched ifnames");
487 static int set_config_net_link(const char *key
, const char *value
,
488 struct lxc_conf
*lxc_conf
, void *data
)
490 struct lxc_netdev
*netdev
= data
;
493 if (lxc_config_value_empty(value
))
494 return clr_config_net_link(key
, lxc_conf
, data
);
499 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
500 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
502 ret
= network_ifname(netdev
->link
, value
);
507 static int set_config_net_name(const char *key
, const char *value
,
508 struct lxc_conf
*lxc_conf
, void *data
)
510 struct lxc_netdev
*netdev
= data
;
512 if (lxc_config_value_empty(value
))
513 return clr_config_net_name(key
, lxc_conf
, data
);
518 return network_ifname(netdev
->name
, value
);
521 static int set_config_net_veth_pair(const char *key
, const char *value
,
522 struct lxc_conf
*lxc_conf
, void *data
)
524 struct lxc_netdev
*netdev
= data
;
526 if (lxc_config_value_empty(value
))
527 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
532 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
);
535 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
536 struct lxc_conf
*lxc_conf
, void *data
)
538 struct lxc_netdev
*netdev
= data
;
540 if (lxc_config_value_empty(value
))
541 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
546 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
549 static int set_config_net_hwaddr(const char *key
, const char *value
,
550 struct lxc_conf
*lxc_conf
, void *data
)
552 struct lxc_netdev
*netdev
= data
;
555 if (lxc_config_value_empty(value
))
556 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
561 new_value
= strdup(value
);
565 rand_complete_hwaddr(new_value
);
567 if (lxc_config_value_empty(new_value
)) {
569 netdev
->hwaddr
= NULL
;
573 netdev
->hwaddr
= new_value
;
577 static int set_config_net_vlan_id(const char *key
, const char *value
,
578 struct lxc_conf
*lxc_conf
, void *data
)
581 struct lxc_netdev
*netdev
= data
;
583 if (lxc_config_value_empty(value
))
584 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
589 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
596 static int set_config_net_mtu(const char *key
, const char *value
,
597 struct lxc_conf
*lxc_conf
, void *data
)
599 struct lxc_netdev
*netdev
= data
;
601 if (lxc_config_value_empty(value
))
602 return clr_config_net_mtu(key
, lxc_conf
, data
);
607 return set_config_string_item(&netdev
->mtu
, value
);
610 static int set_config_net_ipv4_address(const char *key
, const char *value
,
611 struct lxc_conf
*lxc_conf
, void *data
)
614 struct lxc_netdev
*netdev
= data
;
615 struct lxc_inetdev
*inetdev
;
616 struct lxc_list
*list
;
617 char *cursor
, *slash
;
618 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
620 if (lxc_config_value_empty(value
))
621 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
626 inetdev
= malloc(sizeof(*inetdev
));
630 memset(inetdev
, 0, sizeof(*inetdev
));
632 list
= malloc(sizeof(*list
));
639 list
->elem
= inetdev
;
641 addr
= strdup(value
);
648 cursor
= strstr(addr
, " ");
654 slash
= strstr(addr
, "/");
660 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
661 if (!ret
|| ret
< 0) {
662 SYSERROR("Invalid ipv4 address \"%s\"", value
);
670 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
671 if (!ret
|| ret
< 0) {
672 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
681 /* No prefix specified, determine it from the network class. */
683 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
687 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
690 /* If no broadcast address, let compute one from the
691 * prefix and address.
694 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
695 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
698 lxc_list_add_tail(&netdev
->ipv4
, list
);
704 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
705 struct lxc_conf
*lxc_conf
, void *data
)
707 struct lxc_netdev
*netdev
= data
;
709 if (lxc_config_value_empty(value
))
710 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
715 free(netdev
->ipv4_gateway
);
717 if (!strcmp(value
, "auto")) {
718 netdev
->ipv4_gateway
= NULL
;
719 netdev
->ipv4_gateway_auto
= true;
724 gw
= malloc(sizeof(*gw
));
728 ret
= inet_pton(AF_INET
, value
, gw
);
729 if (!ret
|| ret
< 0) {
730 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
735 netdev
->ipv4_gateway
= gw
;
736 netdev
->ipv4_gateway_auto
= false;
742 static int set_config_net_ipv6_address(const char *key
, const char *value
,
743 struct lxc_conf
*lxc_conf
, void *data
)
746 struct lxc_netdev
*netdev
= data
;
747 struct lxc_inet6dev
*inet6dev
;
748 struct lxc_list
*list
;
749 char *slash
, *valdup
, *netmask
;
751 if (lxc_config_value_empty(value
))
752 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
757 inet6dev
= malloc(sizeof(*inet6dev
));
761 memset(inet6dev
, 0, sizeof(*inet6dev
));
763 list
= malloc(sizeof(*list
));
770 list
->elem
= inet6dev
;
772 valdup
= strdup(value
);
779 inet6dev
->prefix
= 64;
780 slash
= strstr(valdup
, "/");
784 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
793 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
794 if (!ret
|| ret
< 0) {
795 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
802 lxc_list_add_tail(&netdev
->ipv6
, list
);
808 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
809 struct lxc_conf
*lxc_conf
, void *data
)
811 struct lxc_netdev
*netdev
= data
;
813 if (lxc_config_value_empty(value
))
814 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
819 free(netdev
->ipv6_gateway
);
821 if (!strcmp(value
, "auto")) {
822 netdev
->ipv6_gateway
= NULL
;
823 netdev
->ipv6_gateway_auto
= true;
828 gw
= malloc(sizeof(*gw
));
832 ret
= inet_pton(AF_INET6
, value
, gw
);
833 if (!ret
|| ret
< 0) {
834 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
839 netdev
->ipv6_gateway
= gw
;
840 netdev
->ipv6_gateway_auto
= false;
846 static int set_config_net_script_up(const char *key
, const char *value
,
847 struct lxc_conf
*lxc_conf
, void *data
)
849 struct lxc_netdev
*netdev
= data
;
851 if (lxc_config_value_empty(value
))
852 return clr_config_net_script_up(key
, lxc_conf
, data
);
857 return set_config_string_item(&netdev
->upscript
, value
);
860 static int set_config_net_script_down(const char *key
, const char *value
,
861 struct lxc_conf
*lxc_conf
, void *data
)
863 struct lxc_netdev
*netdev
= data
;
865 if (lxc_config_value_empty(value
))
866 return clr_config_net_script_down(key
, lxc_conf
, data
);
871 return set_config_string_item(&netdev
->downscript
, value
);
874 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
876 struct lxc_list
*hooklist
;
878 hooklist
= malloc(sizeof(*hooklist
));
884 hooklist
->elem
= hook
;
885 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
889 static int set_config_seccomp_profile(const char *key
, const char *value
,
890 struct lxc_conf
*lxc_conf
, void *data
)
892 return set_config_path_item(&lxc_conf
->seccomp
, value
);
895 static int set_config_execute_cmd(const char *key
, const char *value
,
896 struct lxc_conf
*lxc_conf
, void *data
)
898 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
901 static int set_config_init_cmd(const char *key
, const char *value
,
902 struct lxc_conf
*lxc_conf
, void *data
)
904 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
907 static int set_config_init_cwd(const char *key
, const char *value
,
908 struct lxc_conf
*lxc_conf
, void *data
)
910 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
913 static int set_config_init_uid(const char *key
, const char *value
,
914 struct lxc_conf
*lxc_conf
, void *data
)
916 unsigned int init_uid
;
918 if (lxc_config_value_empty(value
)) {
919 lxc_conf
->init_uid
= 0;
923 if (lxc_safe_uint(value
, &init_uid
) < 0)
926 lxc_conf
->init_uid
= init_uid
;
931 static int set_config_init_gid(const char *key
, const char *value
,
932 struct lxc_conf
*lxc_conf
, void *data
)
934 unsigned int init_gid
;
936 if (lxc_config_value_empty(value
)) {
937 lxc_conf
->init_gid
= 0;
941 if (lxc_safe_uint(value
, &init_gid
) < 0)
944 lxc_conf
->init_gid
= init_gid
;
949 static int set_config_hooks(const char *key
, const char *value
,
950 struct lxc_conf
*lxc_conf
, void *data
)
954 if (lxc_config_value_empty(value
))
955 return lxc_clear_hooks(lxc_conf
, key
);
957 if (strcmp(key
+ 4, "hook") == 0) {
958 ERROR("lxc.hook must not have a value");
962 copy
= strdup(value
);
966 if (strcmp(key
+ 9, "pre-start") == 0)
967 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
968 else if (strcmp(key
+ 9, "start-host") == 0)
969 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
970 else if (strcmp(key
+ 9, "pre-mount") == 0)
971 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
972 else if (strcmp(key
+ 9, "autodev") == 0)
973 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
974 else if (strcmp(key
+ 9, "mount") == 0)
975 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
976 else if (strcmp(key
+ 9, "start") == 0)
977 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
978 else if (strcmp(key
+ 9, "stop") == 0)
979 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
980 else if (strcmp(key
+ 9, "post-stop") == 0)
981 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
982 else if (strcmp(key
+ 9, "clone") == 0)
983 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
984 else if (strcmp(key
+ 9, "destroy") == 0)
985 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
991 static int set_config_hooks_version(const char *key
, const char *value
,
992 struct lxc_conf
*lxc_conf
, void *data
)
997 if (lxc_config_value_empty(value
))
998 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1000 ret
= lxc_safe_uint(value
, &tmp
);
1005 ERROR("Invalid hook version specified. Currently only 0 "
1006 "(legacy) and 1 are supported");
1010 lxc_conf
->hooks_version
= tmp
;
1014 static int set_config_personality(const char *key
, const char *value
,
1015 struct lxc_conf
*lxc_conf
, void *data
)
1017 signed long personality
= lxc_config_parse_arch(value
);
1019 if (personality
>= 0)
1020 lxc_conf
->personality
= personality
;
1022 WARN("Unsupported personality \"%s\"", value
);
1027 static int set_config_pty_max(const char *key
, const char *value
,
1028 struct lxc_conf
*lxc_conf
, void *data
)
1030 if (lxc_config_value_empty(value
)) {
1035 if (lxc_safe_uint(value
, &lxc_conf
->pts
) < 0)
1041 /* We only need to check whether the first byte of the key after the lxc.start.
1042 * prefix matches our expectations since they fortunately all start with a
1043 * different letter. If anything was wrong with the key we would have already
1044 * noticed when the callback was called.
1046 static int set_config_start(const char *key
, const char *value
,
1047 struct lxc_conf
*lxc_conf
, void *data
)
1051 is_empty
= lxc_config_value_empty(value
);
1053 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1055 lxc_conf
->start_auto
= 0;
1059 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1062 if (lxc_conf
->start_auto
> 1)
1066 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1068 lxc_conf
->start_delay
= 0;
1072 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1073 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1075 lxc_conf
->start_order
= 0;
1079 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1085 static int set_config_monitor(const char *key
, const char *value
,
1086 struct lxc_conf
*lxc_conf
, void *data
)
1088 if (lxc_config_value_empty(value
)) {
1089 lxc_conf
->monitor_unshare
= 0;
1093 if (strcmp(key
+ 12, "unshare") == 0)
1094 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1099 static int set_config_group(const char *key
, const char *value
,
1100 struct lxc_conf
*lxc_conf
, void *data
)
1102 char *groups
, *groupptr
, *sptr
, *token
;
1103 struct lxc_list
*grouplist
;
1106 if (lxc_config_value_empty(value
))
1107 return lxc_clear_groups(lxc_conf
);
1109 groups
= strdup(value
);
1113 /* In case several groups are specified in a single line split these
1114 * groups in a single element for the list.
1116 for (groupptr
= groups
;; groupptr
= NULL
) {
1117 token
= strtok_r(groupptr
, " \t", &sptr
);
1123 grouplist
= malloc(sizeof(*grouplist
));
1127 grouplist
->elem
= strdup(token
);
1128 if (!grouplist
->elem
) {
1133 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1140 static int set_config_environment(const char *key
, const char *value
,
1141 struct lxc_conf
*lxc_conf
, void *data
)
1143 struct lxc_list
*list_item
= NULL
;
1145 if (lxc_config_value_empty(value
))
1146 return lxc_clear_environment(lxc_conf
);
1148 list_item
= malloc(sizeof(*list_item
));
1152 list_item
->elem
= strdup(value
);
1154 if (!list_item
->elem
)
1157 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1166 static int set_config_tty_max(const char *key
, const char *value
,
1167 struct lxc_conf
*lxc_conf
, void *data
)
1169 if (lxc_config_value_empty(value
)) {
1174 return lxc_safe_uint(value
, &lxc_conf
->tty
);
1177 static int set_config_tty_dir(const char *key
, const char *value
,
1178 struct lxc_conf
*lxc_conf
, void *data
)
1180 return set_config_string_item_max(&lxc_conf
->ttydir
, value
,
1184 static int set_config_apparmor_profile(const char *key
, const char *value
,
1185 struct lxc_conf
*lxc_conf
, void *data
)
1187 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1190 static int set_config_apparmor_allow_incomplete(const char *key
,
1192 struct lxc_conf
*lxc_conf
,
1195 if (lxc_config_value_empty(value
)) {
1196 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1200 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1203 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1209 static int set_config_selinux_context(const char *key
, const char *value
,
1210 struct lxc_conf
*lxc_conf
, void *data
)
1212 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1215 static int set_config_log_file(const char *key
, const char *value
,
1216 struct lxc_conf
*c
, void *data
)
1220 if (lxc_config_value_empty(value
)) {
1226 /* Store these values in the lxc_conf, and then try to set for actual
1229 ret
= set_config_path_item(&c
->logfile
, value
);
1231 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1236 static int set_config_log_level(const char *key
, const char *value
,
1237 struct lxc_conf
*lxc_conf
, void *data
)
1241 if (lxc_config_value_empty(value
)) {
1242 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1246 if (value
[0] >= '0' && value
[0] <= '9') {
1247 if (lxc_safe_int(value
, &newlevel
) < 0)
1250 newlevel
= lxc_log_priority_to_int(value
);
1253 /* Store these values in the lxc_conf, and then try to set for actual
1256 lxc_conf
->loglevel
= newlevel
;
1257 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1260 static int set_config_autodev(const char *key
, const char *value
,
1261 struct lxc_conf
*lxc_conf
, void *data
)
1263 if (lxc_config_value_empty(value
)) {
1264 lxc_conf
->autodev
= 0;
1268 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1271 if (lxc_conf
->autodev
> 1)
1277 static int sig_num(const char *sig
)
1279 unsigned int signum
;
1281 if (lxc_safe_uint(sig
, &signum
) < 0)
1287 static int rt_sig_num(const char *signame
)
1289 int rtmax
= 0, sig_n
= 0;
1291 if (strncasecmp(signame
, "max-", 4) == 0) {
1296 if (!isdigit(*signame
))
1299 sig_n
= sig_num(signame
);
1300 sig_n
= rtmax
? SIGRTMAX
- sig_n
: SIGRTMIN
+ sig_n
;
1301 if (sig_n
> SIGRTMAX
|| sig_n
< SIGRTMIN
)
1307 static int sig_parse(const char *signame
)
1311 if (isdigit(*signame
)) {
1312 return sig_num(signame
);
1313 } else if (strncasecmp(signame
, "sig", 3) == 0) {
1315 if (strncasecmp(signame
, "rt", 2) == 0)
1316 return rt_sig_num(signame
+ 2);
1317 for (n
= 0; n
< sizeof(signames
) / sizeof((signames
)[0]); n
++) {
1318 if (strcasecmp(signames
[n
].name
, signame
) == 0)
1319 return signames
[n
].num
;
1326 static int set_config_signal_halt(const char *key
, const char *value
,
1327 struct lxc_conf
*lxc_conf
, void *data
)
1331 if (lxc_config_value_empty(value
)) {
1332 lxc_conf
->haltsignal
= 0;
1336 sig_n
= sig_parse(value
);
1340 lxc_conf
->haltsignal
= sig_n
;
1345 static int set_config_signal_reboot(const char *key
, const char *value
,
1346 struct lxc_conf
*lxc_conf
, void *data
)
1350 if (lxc_config_value_empty(value
)) {
1351 lxc_conf
->rebootsignal
= 0;
1355 sig_n
= sig_parse(value
);
1359 lxc_conf
->rebootsignal
= sig_n
;
1364 static int set_config_signal_stop(const char *key
, const char *value
,
1365 struct lxc_conf
*lxc_conf
, void *data
)
1369 if (lxc_config_value_empty(value
)) {
1370 lxc_conf
->stopsignal
= 0;
1374 sig_n
= sig_parse(value
);
1378 lxc_conf
->stopsignal
= sig_n
;
1383 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1384 struct lxc_conf
*lxc_conf
, int version
)
1386 const char *subkey
, *token
;
1388 struct lxc_list
*cglist
= NULL
;
1389 struct lxc_cgroup
*cgelem
= NULL
;
1391 if (lxc_config_value_empty(value
))
1392 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1394 if (version
== CGROUP2_SUPER_MAGIC
) {
1395 token
= "lxc.cgroup2.";
1397 } else if (version
== CGROUP_SUPER_MAGIC
) {
1398 token
= "lxc.cgroup.";
1404 if (strncmp(key
, token
, token_len
) != 0)
1407 subkey
= key
+ token_len
;
1408 if (*subkey
== '\0')
1411 cglist
= malloc(sizeof(*cglist
));
1415 cgelem
= malloc(sizeof(*cgelem
));
1418 memset(cgelem
, 0, sizeof(*cgelem
));
1420 cgelem
->subsystem
= strdup(subkey
);
1421 if (!cgelem
->subsystem
)
1424 cgelem
->value
= strdup(value
);
1428 cgelem
->version
= version
;
1430 lxc_list_add_elem(cglist
, cgelem
);
1432 if (version
== CGROUP2_SUPER_MAGIC
)
1433 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1435 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1442 free(cgelem
->subsystem
);
1443 free(cgelem
->value
);
1450 static int set_config_cgroup_controller(const char *key
, const char *value
,
1451 struct lxc_conf
*lxc_conf
, void *data
)
1453 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1454 CGROUP_SUPER_MAGIC
);
1457 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1458 struct lxc_conf
*lxc_conf
, void *data
)
1460 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1461 CGROUP2_SUPER_MAGIC
);
1465 static int set_config_cgroup_dir(const char *key
, const char *value
,
1466 struct lxc_conf
*lxc_conf
, void *data
)
1468 if (lxc_config_value_empty(value
))
1469 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1471 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1474 static int set_config_prlimit(const char *key
, const char *value
,
1475 struct lxc_conf
*lxc_conf
, void *data
)
1477 struct lxc_list
*iter
;
1478 struct rlimit limit
;
1480 struct lxc_list
*limlist
= NULL
;
1481 struct lxc_limit
*limelem
= NULL
;
1483 if (lxc_config_value_empty(value
))
1484 return lxc_clear_limits(lxc_conf
, key
);
1486 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1489 key
+= sizeof("lxc.prlimit.") - 1;
1491 /* soft limit comes first in the value */
1492 if (!parse_limit_value(&value
, &limit_value
))
1494 limit
.rlim_cur
= limit_value
;
1496 /* skip spaces and a colon */
1497 while (isspace(*value
))
1502 else if (*value
) /* any other character is an error here */
1505 while (isspace(*value
))
1508 /* optional hard limit */
1510 if (!parse_limit_value(&value
, &limit_value
))
1512 limit
.rlim_max
= limit_value
;
1514 /* check for trailing garbage */
1515 while (isspace(*value
))
1521 /* a single value sets both hard and soft limit */
1522 limit
.rlim_max
= limit
.rlim_cur
;
1525 /* find existing list element */
1526 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1527 limelem
= iter
->elem
;
1528 if (!strcmp(key
, limelem
->resource
)) {
1529 limelem
->limit
= limit
;
1534 /* allocate list element */
1535 limlist
= malloc(sizeof(*limlist
));
1539 limelem
= malloc(sizeof(*limelem
));
1542 memset(limelem
, 0, sizeof(*limelem
));
1544 limelem
->resource
= strdup(key
);
1545 if (!limelem
->resource
)
1547 limelem
->limit
= limit
;
1549 lxc_list_add_elem(limlist
, limelem
);;
1551 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1558 free(limelem
->resource
);
1564 static int set_config_sysctl(const char *key
, const char *value
,
1565 struct lxc_conf
*lxc_conf
, void *data
)
1567 struct lxc_list
*iter
;
1568 char *replace_value
= NULL
;
1569 struct lxc_list
*sysctl_list
= NULL
;
1570 struct lxc_sysctl
*sysctl_elem
= NULL
;
1572 if (lxc_config_value_empty(value
))
1573 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1575 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1578 key
+= sizeof("lxc.sysctl.") - 1;
1580 /* find existing list element */
1581 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1582 sysctl_elem
= iter
->elem
;
1584 if (strcmp(key
, sysctl_elem
->key
) != 0)
1587 replace_value
= strdup(value
);
1591 free(sysctl_elem
->value
);
1592 sysctl_elem
->value
= replace_value
;
1596 /* allocate list element */
1597 sysctl_list
= malloc(sizeof(*sysctl_list
));
1601 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1604 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1606 sysctl_elem
->key
= strdup(key
);
1607 if (!sysctl_elem
->key
)
1610 sysctl_elem
->value
= strdup(value
);
1611 if (!sysctl_elem
->value
)
1614 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1616 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1623 free(sysctl_elem
->key
);
1624 free(sysctl_elem
->value
);
1630 static int set_config_proc(const char *key
, const char *value
,
1631 struct lxc_conf
*lxc_conf
, void *data
)
1634 struct lxc_list
*proclist
= NULL
;
1635 struct lxc_proc
*procelem
= NULL
;
1637 if (lxc_config_value_empty(value
))
1638 return clr_config_proc(key
, lxc_conf
, NULL
);
1640 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1643 subkey
= key
+ sizeof("lxc.proc.") - 1;
1644 if (*subkey
== '\0')
1647 proclist
= malloc(sizeof(*proclist
));
1651 procelem
= malloc(sizeof(*procelem
));
1654 memset(procelem
, 0, sizeof(*procelem
));
1656 procelem
->filename
= strdup(subkey
);
1657 procelem
->value
= strdup(value
);
1659 if (!procelem
->filename
|| !procelem
->value
)
1662 proclist
->elem
= procelem
;
1664 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1671 free(procelem
->filename
);
1672 free(procelem
->value
);
1679 static int set_config_idmaps(const char *key
, const char *value
,
1680 struct lxc_conf
*lxc_conf
, void *data
)
1682 unsigned long hostid
, nsid
, range
;
1685 struct lxc_list
*idmaplist
= NULL
;
1686 struct id_map
*idmap
= NULL
;
1688 if (lxc_config_value_empty(value
))
1689 return lxc_clear_idmaps(lxc_conf
);
1691 idmaplist
= malloc(sizeof(*idmaplist
));
1695 idmap
= malloc(sizeof(*idmap
));
1698 memset(idmap
, 0, sizeof(*idmap
));
1700 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1702 ERROR("Failed to parse id mappings");
1706 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1708 idmap
->idtype
= ID_TYPE_UID
;
1709 else if (type
== 'g')
1710 idmap
->idtype
= ID_TYPE_GID
;
1714 idmap
->hostid
= hostid
;
1716 idmap
->range
= range
;
1717 idmaplist
->elem
= idmap
;
1718 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1720 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1721 if (idmap
->nsid
== 0)
1722 lxc_conf
->root_nsuid_map
= idmap
;
1725 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1726 if (idmap
->nsid
== 0)
1727 lxc_conf
->root_nsgid_map
= idmap
;
1740 static int set_config_mount_fstab(const char *key
, const char *value
,
1741 struct lxc_conf
*lxc_conf
, void *data
)
1743 if (lxc_config_value_empty(value
)) {
1744 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1748 return set_config_path_item(&lxc_conf
->fstab
, value
);
1751 static int set_config_mount_auto(const char *key
, const char *value
,
1752 struct lxc_conf
*lxc_conf
, void *data
)
1754 char *autos
, *autoptr
, *sptr
, *token
;
1761 } allowed_auto_mounts
[] = {
1762 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1763 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1764 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1765 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1766 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1767 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1768 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1769 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1770 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1771 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1772 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1773 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1774 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1775 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1776 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1777 /* For adding anything that is just a single on/off, but has no
1778 * options: keep mask and flag identical and just define the enum
1779 * value as an unused bit so far
1784 if (lxc_config_value_empty(value
)) {
1785 lxc_conf
->auto_mounts
= 0;
1789 autos
= strdup(value
);
1793 for (autoptr
= autos
;; autoptr
= NULL
) {
1794 token
= strtok_r(autoptr
, " \t", &sptr
);
1800 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1801 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1805 if (!allowed_auto_mounts
[i
].token
) {
1806 ERROR("Invalid filesystem to automount \"%s\"", token
);
1810 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1811 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1818 static int set_config_mount(const char *key
, const char *value
,
1819 struct lxc_conf
*lxc_conf
, void *data
)
1822 struct lxc_list
*mntlist
;
1824 if (lxc_config_value_empty(value
))
1825 return lxc_clear_mount_entries(lxc_conf
);
1827 mntlist
= malloc(sizeof(*mntlist
));
1831 mntelem
= strdup(value
);
1836 mntlist
->elem
= mntelem
;
1838 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1843 static int set_config_cap_keep(const char *key
, const char *value
,
1844 struct lxc_conf
*lxc_conf
, void *data
)
1846 char *keepcaps
, *keepptr
, *sptr
, *token
;
1847 struct lxc_list
*keeplist
;
1850 if (lxc_config_value_empty(value
))
1851 return lxc_clear_config_keepcaps(lxc_conf
);
1853 keepcaps
= strdup(value
);
1857 /* In case several capability keep is specified in a single line
1858 * split these caps in a single element for the list.
1860 for (keepptr
= keepcaps
;; keepptr
= NULL
) {
1861 token
= strtok_r(keepptr
, " \t", &sptr
);
1867 if (!strcmp(token
, "none"))
1868 lxc_clear_config_keepcaps(lxc_conf
);
1870 keeplist
= malloc(sizeof(*keeplist
));
1874 keeplist
->elem
= strdup(token
);
1875 if (!keeplist
->elem
) {
1880 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1888 static int set_config_cap_drop(const char *key
, const char *value
,
1889 struct lxc_conf
*lxc_conf
, void *data
)
1891 char *dropcaps
, *dropptr
, *sptr
, *token
;
1892 struct lxc_list
*droplist
;
1895 if (lxc_config_value_empty(value
))
1896 return lxc_clear_config_caps(lxc_conf
);
1898 dropcaps
= strdup(value
);
1902 /* In case several capability drop is specified in a single line
1903 * split these caps in a single element for the list.
1905 for (dropptr
= dropcaps
;; dropptr
= NULL
) {
1906 token
= strtok_r(dropptr
, " \t", &sptr
);
1912 droplist
= malloc(sizeof(*droplist
));
1916 droplist
->elem
= strdup(token
);
1917 if (!droplist
->elem
) {
1922 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1930 static int set_config_console_path(const char *key
, const char *value
,
1931 struct lxc_conf
*lxc_conf
, void *data
)
1933 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1936 static int set_config_console_rotate(const char *key
, const char *value
,
1937 struct lxc_conf
*lxc_conf
, void *data
)
1939 if (lxc_config_value_empty(value
)) {
1940 lxc_conf
->console
.log_rotate
= 0;
1944 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1947 if (lxc_conf
->console
.log_rotate
> 1) {
1948 ERROR("The \"lxc.console.rotate\" config key can only be set "
1956 static int set_config_console_logfile(const char *key
, const char *value
,
1957 struct lxc_conf
*lxc_conf
, void *data
)
1959 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1962 static int set_config_console_buffer_size(const char *key
, const char *value
,
1963 struct lxc_conf
*lxc_conf
, void *data
)
1967 uint64_t buffer_size
, pgsz
;
1969 if (lxc_config_value_empty(value
)) {
1970 lxc_conf
->console
.buffer_size
= 0;
1974 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1975 if (!strcmp(value
, "auto")) {
1976 lxc_conf
->console
.buffer_size
= 1 << 17;
1980 ret
= parse_byte_size_string(value
, &size
);
1987 /* must be at least a page size */
1988 pgsz
= lxc_getpagesize();
1989 if ((uint64_t)size
< pgsz
) {
1990 NOTICE("Requested ringbuffer size for the console is %" PRId64
1991 " but must be at least %" PRId64
1992 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1997 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1998 if (buffer_size
== 0)
2001 if (buffer_size
!= size
)
2002 NOTICE("Passed size was not a power of 2. Rounding log size to "
2003 "next power of two: %" PRIu64
" bytes", buffer_size
);
2005 lxc_conf
->console
.buffer_size
= buffer_size
;
2009 static int set_config_console_buffer_logfile(const char *key
, const char *value
,
2010 struct lxc_conf
*lxc_conf
,
2013 return set_config_path_item(&lxc_conf
->console
.buffer_log_file
, value
);
2016 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2018 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
2020 update_hwaddr(line
);
2022 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2023 char *tmp
= realloc(conf
->unexpanded_config
,
2024 conf
->unexpanded_alloced
+ 1024);
2028 if (!conf
->unexpanded_config
)
2030 conf
->unexpanded_config
= tmp
;
2031 conf
->unexpanded_alloced
+= 1024;
2033 strcat(conf
->unexpanded_config
, line
);
2034 conf
->unexpanded_len
+= linelen
;
2035 if (line
[linelen
- 1] != '\n') {
2036 strcat(conf
->unexpanded_config
, "\n");
2037 conf
->unexpanded_len
++;
2043 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2045 struct dirent
*direntp
;
2047 char path
[MAXPATHLEN
];
2051 dir
= opendir(dirp
);
2055 while ((direntp
= readdir(dir
))) {
2060 fnam
= direntp
->d_name
;
2061 if (!strcmp(fnam
, "."))
2064 if (!strcmp(fnam
, ".."))
2068 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2071 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2072 if (len
< 0 || len
>= MAXPATHLEN
) {
2077 ret
= lxc_config_read(path
, lxc_conf
, true);
2089 static int set_config_includefiles(const char *key
, const char *value
,
2090 struct lxc_conf
*lxc_conf
, void *data
)
2092 if (lxc_config_value_empty(value
)) {
2093 clr_config_includefiles(key
, lxc_conf
, NULL
);
2098 return do_includedir(value
, lxc_conf
);
2100 return lxc_config_read(value
, lxc_conf
, true);
2103 static int set_config_rootfs_path(const char *key
, const char *value
,
2104 struct lxc_conf
*lxc_conf
, void *data
)
2108 const char *container_path
;
2110 if (lxc_config_value_empty(value
)) {
2111 free(lxc_conf
->rootfs
.path
);
2112 lxc_conf
->rootfs
.path
= NULL
;
2116 dup
= strdup(value
);
2120 /* Split <storage type>:<container path> into <storage type> and
2121 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2122 * "rootfs.path" to <container path>.
2124 tmp
= strchr(dup
, ':');
2127 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2133 container_path
= tmp
;
2135 container_path
= value
;
2138 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2143 static int set_config_rootfs_mount(const char *key
, const char *value
,
2144 struct lxc_conf
*lxc_conf
, void *data
)
2146 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2149 static int set_config_rootfs_options(const char *key
, const char *value
,
2150 struct lxc_conf
*lxc_conf
, void *data
)
2152 return set_config_string_item(&lxc_conf
->rootfs
.options
, value
);
2155 static int set_config_rootfs_backend(const char *key
, const char *value
,
2156 struct lxc_conf
*lxc_conf
, void *data
)
2161 static int set_config_uts_name(const char *key
, const char *value
,
2162 struct lxc_conf
*lxc_conf
, void *data
)
2164 struct utsname
*utsname
;
2166 if (lxc_config_value_empty(value
)) {
2167 clr_config_uts_name(key
, lxc_conf
, NULL
);
2171 utsname
= malloc(sizeof(*utsname
));
2175 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2180 strcpy(utsname
->nodename
, value
);
2181 free(lxc_conf
->utsname
);
2182 lxc_conf
->utsname
= utsname
;
2187 static int set_config_namespace_clone(const char *key
, const char *value
,
2188 struct lxc_conf
*lxc_conf
, void *data
)
2190 char *ns
, *nsptr
, *token
;
2192 char *saveptr
= NULL
;
2194 if (lxc_config_value_empty(value
))
2195 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2197 if (lxc_conf
->ns_keep
!= 0) {
2198 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2199 "\"lxc.namespace.keep\"", strerror(EINVAL
));
2208 for (; (token
= strtok_r(nsptr
, " \t", &saveptr
)); nsptr
= NULL
) {
2209 token
+= lxc_char_left_gc(token
, strlen(token
));
2210 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2211 cloneflag
= lxc_namespace_2_cloneflag(token
);
2212 if (cloneflag
< 0) {
2216 lxc_conf
->ns_clone
|= cloneflag
;
2223 static int set_config_namespace_keep(const char *key
, const char *value
,
2224 struct lxc_conf
*lxc_conf
, void *data
)
2226 char *ns
, *nsptr
, *token
;
2228 char *saveptr
= NULL
;
2230 if (lxc_config_value_empty(value
))
2231 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2233 if (lxc_conf
->ns_clone
!= 0) {
2234 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2235 "\"lxc.namespace.keep\"", strerror(EINVAL
));
2244 for (; (token
= strtok_r(nsptr
, " \t", &saveptr
)); nsptr
= NULL
) {
2245 token
+= lxc_char_left_gc(token
, strlen(token
));
2246 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2247 cloneflag
= lxc_namespace_2_cloneflag(token
);
2248 if (cloneflag
< 0) {
2252 lxc_conf
->ns_keep
|= cloneflag
;
2259 static int set_config_namespace_share(const char *key
, const char *value
,
2260 struct lxc_conf
*lxc_conf
, void *data
)
2263 const char *namespace;
2265 if (lxc_config_value_empty(value
))
2266 return clr_config_namespace_share(key
, lxc_conf
, data
);
2268 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
2269 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2273 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2276 struct parse_line_conf
{
2277 struct lxc_conf
*conf
;
2281 static int parse_line(char *buffer
, void *data
)
2283 char *dot
, *key
, *line
, *linep
, *value
;
2285 struct lxc_config_t
*config
;
2288 struct parse_line_conf
*plc
= data
;
2290 /* If there are newlines in the config file we should keep them. */
2291 empty_line
= lxc_is_line_empty(dup
);
2295 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2296 * modified the original string on the stack by replacing '=' by '\0'
2299 linep
= line
= strdup(dup
);
2303 if (!plc
->from_include
) {
2304 ret
= append_unexp_config_line(line
, plc
->conf
);
2312 line
+= lxc_char_left_gc(line
, strlen(line
));
2314 /* ignore comments */
2318 /* martian option - don't add it to the config itself */
2319 if (strncmp(line
, "lxc.", 4))
2324 dot
= strchr(line
, '=');
2326 ERROR("Invalid configuration line: %s", line
);
2334 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2336 value
+= lxc_char_left_gc(value
, strlen(value
));
2337 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2339 if (*value
== '\'' || *value
== '\"') {
2342 len
= strlen(value
);
2343 if (len
> 1 && value
[len
- 1] == *value
) {
2344 value
[len
- 1] = '\0';
2349 config
= lxc_get_config(key
);
2351 ERROR("Unknown configuration key \"%s\"", key
);
2355 /* [START]: REMOVE IN LXC 3.0 */
2356 if (config
->is_legacy_key
&& !plc
->conf
->contains_legacy_key
) {
2357 plc
->conf
->contains_legacy_key
= true;
2358 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2359 /* Warn the user once loud and clear that there is at
2360 * least one legacy configuration item in the
2361 * configuration file and then an update is required.
2363 fprintf(stderr
, "The configuration file contains "
2364 "legacy configuration keys.\nPlease "
2365 "update your configuration file!\nThe "
2366 "update script lxc-update-config -c "
2367 "<path-to-config> can be used for "
2371 /* [END]: REMOVE IN LXC 3.0 */
2373 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2380 static int lxc_config_readline(char *buffer
, struct lxc_conf
*conf
)
2382 struct parse_line_conf c
;
2385 c
.from_include
= false;
2387 return parse_line(buffer
, &c
);
2390 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2393 struct parse_line_conf c
;
2396 c
.from_include
= from_include
;
2398 ret
= access(file
, R_OK
);
2402 /* Catch only the top level config file name in the structure. */
2404 conf
->rcfile
= strdup(file
);
2406 return lxc_file_for_each_line(file
, parse_line
, &c
);
2409 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2411 struct lxc_list
*dent
;
2413 dent
= malloc(sizeof(struct lxc_list
));
2418 lxc_list_add_tail(defines
, dent
);
2422 int lxc_config_define_load(struct lxc_list
*defines
, struct lxc_conf
*conf
)
2424 struct lxc_list
*it
, *next
;
2427 lxc_list_for_each(it
, defines
) {
2428 ret
= lxc_config_readline(it
->elem
, conf
);
2433 lxc_list_for_each_safe(it
, defines
, next
) {
2441 signed long lxc_config_parse_arch(const char *arch
)
2443 #if HAVE_SYS_PERSONALITY_H
2449 { "x86", PER_LINUX32
},
2450 { "linux32", PER_LINUX32
},
2451 { "i386", PER_LINUX32
},
2452 { "i486", PER_LINUX32
},
2453 { "i586", PER_LINUX32
},
2454 { "i686", PER_LINUX32
},
2455 { "athlon", PER_LINUX32
},
2456 { "mips", PER_LINUX32
},
2457 { "mipsel", PER_LINUX32
},
2458 { "ppc", PER_LINUX32
},
2459 { "arm", PER_LINUX32
},
2460 { "armv7l", PER_LINUX32
},
2461 { "armhf", PER_LINUX32
},
2462 { "armel", PER_LINUX32
},
2463 { "powerpc", PER_LINUX32
},
2464 { "linux64", PER_LINUX
},
2465 { "x86_64", PER_LINUX
},
2466 { "amd64", PER_LINUX
},
2467 { "mips64", PER_LINUX
},
2468 { "mips64el", PER_LINUX
},
2469 { "ppc64", PER_LINUX
},
2470 { "ppc64le", PER_LINUX
},
2471 { "ppc64el", PER_LINUX
},
2472 { "powerpc64", PER_LINUX
},
2473 { "s390x", PER_LINUX
},
2474 { "aarch64", PER_LINUX
},
2475 { "arm64", PER_LINUX
},
2477 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2479 for (i
= 0; i
< len
; i
++) {
2480 if (!strcmp(pername
[i
].name
, arch
))
2481 return pername
[i
].per
;
2488 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2490 char *token
, *saveptr
= NULL
;
2496 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2497 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2498 { "LSM", LXC_ATTACH_LSM_EXEC
},
2503 /* For the sake of backward compatibility, drop all privileges
2504 * if none is specified.
2506 for (i
= 0; all_privs
[i
].token
; i
++)
2507 *flags
|= all_privs
[i
].flag
;
2512 token
= strtok_r(flaglist
, "|", &saveptr
);
2515 for (i
= 0; all_privs
[i
].token
; i
++)
2516 if (!strcmp(all_privs
[i
].token
, token
))
2517 aflag
= all_privs
[i
].flag
;
2523 token
= strtok_r(NULL
, "|", &saveptr
);
2529 /* Write out a configuration file. */
2530 void write_config(FILE *fout
, struct lxc_conf
*c
)
2533 size_t len
= c
->unexpanded_len
;
2538 ret
= fwrite(c
->unexpanded_config
, 1, len
, fout
);
2540 SYSERROR("Failed to write configuration file");
2543 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2550 len
= strlen(key
) + strlen(v
) + 4;
2553 if (lxc_config_value_empty(v
))
2554 ret
= snprintf(tmp
, len
, "%s =", key
);
2556 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2557 if (ret
< 0 || ret
>= len
)
2560 /* Save the line verbatim into unexpanded_conf */
2561 if (append_unexp_config_line(tmp
, conf
))
2567 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2571 char *lstart
= conf
->unexpanded_config
;
2573 if (!conf
->unexpanded_config
)
2577 lend
= strchr(lstart
, '\n');
2580 lend
= lstart
+ strlen(lstart
);
2583 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2588 v
= lstart
[strlen(key
)];
2589 if (!isspace(v
) && v
!= '=') {
2594 conf
->unexpanded_len
-= (lend
- lstart
);
2595 if (*lend
== '\0') {
2599 memmove(lstart
, lend
, strlen(lend
) + 1);
2603 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2604 const char *newpath
, const char *oldname
,
2605 const char *newname
, const char *ovldir
)
2608 char *lend
, *newdir
, *olddir
, *p
, *q
;
2609 size_t newdirlen
, olddirlen
;
2610 char *lstart
= conf
->unexpanded_config
;
2611 const char *key
= "lxc.mount.entry";
2613 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2614 olddir
= alloca(olddirlen
+ 1);
2615 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2617 if (ret
< 0 || ret
>= olddirlen
+ 1)
2620 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2621 newdir
= alloca(newdirlen
+ 1);
2622 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2624 if (ret
< 0 || ret
>= newdirlen
+ 1)
2627 if (!conf
->unexpanded_config
)
2631 lend
= strchr(lstart
, '\n');
2633 lend
= lstart
+ strlen(lstart
);
2637 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2640 p
= strchr(lstart
+ strlen(key
), '=');
2651 /* Whenever an lxc.mount.entry entry is found in a line we check
2652 * if the substring " overlay" or the substring " aufs" is
2653 * present before doing any further work. We check for "
2654 * overlay" and " aufs" since both substrings need to have at
2655 * least one space before them in a valid overlay
2656 * lxc.mount.entry (/A B overlay). When the space before is
2657 * missing it is very likely that these substrings are part of a
2658 * path or something else. (Checking q >= lend ensures that we
2659 * only count matches in the current line.) */
2660 if ((!(q
= strstr(p
, " overlay")) || q
>= lend
) &&
2661 (!(q
= strstr(p
, " aufs")) || q
>= lend
))
2664 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2667 /* replace the olddir with newdir */
2668 if (olddirlen
>= newdirlen
) {
2669 size_t diff
= olddirlen
- newdirlen
;
2670 memcpy(q
, newdir
, newdirlen
);
2671 if (olddirlen
!= newdirlen
) {
2672 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2673 strlen(q
) - newdirlen
- diff
+ 1);
2675 conf
->unexpanded_len
-= diff
;
2679 size_t diff
= newdirlen
- olddirlen
;
2680 size_t oldlen
= conf
->unexpanded_len
;
2681 size_t newlen
= oldlen
+ diff
;
2682 size_t poffset
= q
- conf
->unexpanded_config
;
2684 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2688 conf
->unexpanded_len
= newlen
;
2689 conf
->unexpanded_alloced
= newlen
+ 1;
2690 new[newlen
- 1] = '\0';
2691 lend
= new + (lend
- conf
->unexpanded_config
);
2692 /* Move over the remainder to make room for the newdir.
2694 memmove(new + poffset
+ newdirlen
,
2695 new + poffset
+ olddirlen
,
2696 oldlen
- poffset
- olddirlen
+ 1);
2697 conf
->unexpanded_config
= new;
2698 memcpy(new + poffset
, newdir
, newdirlen
);
2708 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2709 const char *newpath
, const char *oldname
,
2710 const char *newname
)
2713 char *lend
, *newdir
, *olddir
, *p
;
2714 char *lstart
= conf
->unexpanded_config
;
2715 size_t newdirlen
, olddirlen
;
2716 const char *key
= "lxc.hook";
2718 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2719 olddir
= alloca(olddirlen
+ 1);
2720 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2721 if (ret
< 0 || ret
>= olddirlen
+ 1)
2724 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2725 newdir
= alloca(newdirlen
+ 1);
2726 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2727 if (ret
< 0 || ret
>= newdirlen
+ 1)
2730 if (!conf
->unexpanded_config
)
2734 lend
= strchr(lstart
, '\n');
2736 lend
= lstart
+ strlen(lstart
);
2740 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2743 p
= strchr(lstart
+ strlen(key
), '=');
2754 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2757 /* replace the olddir with newdir */
2758 if (olddirlen
>= newdirlen
) {
2759 size_t diff
= olddirlen
- newdirlen
;
2760 memcpy(p
, newdir
, newdirlen
);
2761 if (olddirlen
!= newdirlen
) {
2762 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2763 strlen(p
) - newdirlen
- diff
+ 1);
2765 conf
->unexpanded_len
-= diff
;
2769 size_t diff
= newdirlen
- olddirlen
;
2770 size_t oldlen
= conf
->unexpanded_len
;
2771 size_t newlen
= oldlen
+ diff
;
2772 size_t poffset
= p
- conf
->unexpanded_config
;
2774 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2778 conf
->unexpanded_len
= newlen
;
2779 conf
->unexpanded_alloced
= newlen
+ 1;
2780 new[newlen
- 1] = '\0';
2781 lend
= new + (lend
- conf
->unexpanded_config
);
2782 /* Move over the remainder to make room for the newdir.
2784 memmove(new + poffset
+ newdirlen
,
2785 new + poffset
+ olddirlen
,
2786 oldlen
- poffset
- olddirlen
+ 1);
2787 conf
->unexpanded_config
= new;
2788 memcpy(new + poffset
, newdir
, newdirlen
);
2801 ERROR("Error writing to new config"); \
2806 /* This is called only from clone. We wish to update all hwaddrs in the
2807 * unexpanded config file. We can't/don't want to update any which come from
2808 * lxc.includes (there shouldn't be any).
2809 * We can't just walk the c->lxc-conf->network list because that includes netifs
2810 * from the include files. So we update the ones which we find in the unexp
2811 * config file, then find the original macaddr in the conf->network, and update
2812 * that to the same value.
2814 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2816 char *lend
, *p
, *p2
;
2817 struct lxc_list
*it
;
2818 char *lstart
= conf
->unexpanded_config
;
2820 if (!conf
->unexpanded_config
)
2824 char newhwaddr
[18], oldhwaddr
[17];
2826 lend
= strchr(lstart
, '\n');
2828 lend
= lstart
+ strlen(lstart
);
2832 if (!lxc_config_net_hwaddr(lstart
)) {
2837 p
= strchr(lstart
, '=');
2850 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2853 if ((p2
- p
) != 17) {
2854 WARN("Bad hwaddr entry");
2859 memcpy(oldhwaddr
, p
, 17);
2861 if (!new_hwaddr(newhwaddr
))
2864 memcpy(p
, newhwaddr
, 17);
2865 lxc_list_for_each(it
, &conf
->network
) {
2866 struct lxc_netdev
*n
= it
->elem
;
2868 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2869 memcpy(n
->hwaddr
, newhwaddr
, 17);
2878 static int set_config_ephemeral(const char *key
, const char *value
,
2879 struct lxc_conf
*lxc_conf
, void *data
)
2881 if (lxc_config_value_empty(value
)) {
2882 lxc_conf
->ephemeral
= 0;
2886 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2889 if (lxc_conf
->ephemeral
> 1)
2895 static int set_config_log_syslog(const char *key
, const char *value
,
2896 struct lxc_conf
*lxc_conf
, void *data
)
2900 if (lxc_conf
->syslog
) {
2901 free(lxc_conf
->syslog
);
2902 lxc_conf
->syslog
= NULL
;
2905 if (lxc_config_value_empty(value
))
2908 facility
= lxc_syslog_priority_to_int(value
);
2909 if (facility
== -EINVAL
)
2912 lxc_log_syslog(facility
);
2913 return set_config_string_item(&lxc_conf
->syslog
, value
);
2916 static int set_config_no_new_privs(const char *key
, const char *value
,
2917 struct lxc_conf
*lxc_conf
, void *data
)
2921 if (lxc_config_value_empty(value
)) {
2922 lxc_conf
->no_new_privs
= false;
2926 if (lxc_safe_uint(value
, &v
) < 0)
2932 lxc_conf
->no_new_privs
= v
? true : false;
2937 static int set_config_noop(const char *key
, const char *value
,
2938 struct lxc_conf
*lxc_conf
, void *data
)
2943 /* Callbacks to get configuration items. */
2944 static int get_config_personality(const char *key
, char *retv
, int inlen
,
2945 struct lxc_conf
*c
, void *data
)
2952 memset(retv
, 0, inlen
);
2954 #if HAVE_SYS_PERSONALITY_H
2957 switch (c
->personality
) {
2959 strprint(retv
, inlen
, "i686");
2962 strprint(retv
, inlen
, "x86_64");
2972 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
2973 struct lxc_conf
*c
, void *data
)
2975 return lxc_get_conf_int(c
, retv
, inlen
, c
->pts
);
2978 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
2979 struct lxc_conf
*c
, void *data
)
2981 return lxc_get_conf_int(c
, retv
, inlen
, c
->tty
);
2984 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
2985 struct lxc_conf
*c
, void *data
)
2987 return lxc_get_conf_str(retv
, inlen
, c
->ttydir
);
2990 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
2991 struct lxc_conf
*c
, void *data
)
2993 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
2996 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
2997 int inlen
, struct lxc_conf
*c
,
3000 return lxc_get_conf_int(c
, retv
, inlen
,
3001 c
->lsm_aa_allow_incomplete
);
3004 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3005 struct lxc_conf
*c
, void *data
)
3007 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3010 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3011 * just the value(s) will be printed. Since there still could be more than one,
3012 * it is newline-separated.
3013 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
3015 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3016 * 'lxc.cgroup.subsystem.key = value' format.
3018 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3019 int inlen
, struct lxc_conf
*c
,
3023 size_t namespaced_token_len
;
3024 char *global_token
, *namespaced_token
;
3025 struct lxc_list
*it
;
3027 bool get_all
= false;
3032 memset(retv
, 0, inlen
);
3034 if (version
== CGROUP2_SUPER_MAGIC
) {
3035 global_token
= "lxc.cgroup2";
3036 namespaced_token
= "lxc.cgroup2.";
3037 namespaced_token_len
= sizeof("lxc.cgroup2.") - 1;;
3038 } else if (version
== CGROUP_SUPER_MAGIC
) {
3039 global_token
= "lxc.cgroup";
3040 namespaced_token
= "lxc.cgroup.";
3041 namespaced_token_len
= sizeof("lxc.cgroup.") - 1;;
3046 if (strcmp(key
, global_token
) == 0)
3048 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3049 key
+= namespaced_token_len
;
3053 lxc_list_for_each(it
, &c
->cgroup
) {
3054 struct lxc_cgroup
*cg
= it
->elem
;
3057 if (version
!= cg
->version
)
3060 strprint(retv
, inlen
, "%s.%s = %s\n",
3061 global_token
, cg
->subsystem
, cg
->value
);
3062 } else if (!strcmp(cg
->subsystem
, key
)) {
3063 strprint(retv
, inlen
, "%s\n", cg
->value
);
3070 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3071 struct lxc_conf
*c
, void *data
)
3073 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3074 CGROUP_SUPER_MAGIC
);
3077 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3078 struct lxc_conf
*c
, void *data
)
3080 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3081 CGROUP2_SUPER_MAGIC
);
3084 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3085 struct lxc_conf
*lxc_conf
, void *data
)
3093 memset(retv
, 0, inlen
);
3095 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3100 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3101 struct lxc_conf
*c
, void *data
)
3103 struct lxc_list
*it
;
3104 int len
, listlen
, ret
;
3106 /* "u 1000 1000000 65536"
3108 * let's render this as
3126 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3127 char buf
[__LXC_IDMAP_STR_BUF
];
3132 memset(retv
, 0, inlen
);
3134 listlen
= lxc_list_len(&c
->id_map
);
3135 lxc_list_for_each(it
, &c
->id_map
)
3137 struct id_map
*map
= it
->elem
;
3138 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3139 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3140 map
->nsid
, map
->hostid
, map
->range
);
3141 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3144 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3149 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3150 struct lxc_conf
*c
, void *data
)
3153 v
= lxc_log_priority_to_string(c
->loglevel
);
3154 return lxc_get_conf_str(retv
, inlen
, v
);
3157 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3158 struct lxc_conf
*c
, void *data
)
3160 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3163 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3164 struct lxc_conf
*c
, void *data
)
3166 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3169 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3170 struct lxc_conf
*c
, void *data
)
3172 int len
, fulllen
= 0;
3173 const char *sep
= "";
3178 memset(retv
, 0, inlen
);
3180 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3183 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3184 case LXC_AUTO_PROC_MIXED
:
3185 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3188 case LXC_AUTO_PROC_RW
:
3189 strprint(retv
, inlen
, "%sproc:rw", sep
);
3196 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3197 case LXC_AUTO_SYS_RO
:
3198 strprint(retv
, inlen
, "%ssys:ro", sep
);
3201 case LXC_AUTO_SYS_RW
:
3202 strprint(retv
, inlen
, "%ssys:rw", sep
);
3205 case LXC_AUTO_SYS_MIXED
:
3206 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3213 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3214 case LXC_AUTO_CGROUP_NOSPEC
:
3215 strprint(retv
, inlen
, "%scgroup", sep
);
3217 case LXC_AUTO_CGROUP_MIXED
:
3218 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3220 case LXC_AUTO_CGROUP_RO
:
3221 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3223 case LXC_AUTO_CGROUP_RW
:
3224 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3226 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3227 strprint(retv
, inlen
, "%scgroup-full", sep
);
3229 case LXC_AUTO_CGROUP_FULL_MIXED
:
3230 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3232 case LXC_AUTO_CGROUP_FULL_RO
:
3233 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3235 case LXC_AUTO_CGROUP_FULL_RW
:
3236 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3245 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3246 struct lxc_conf
*c
, void *data
)
3248 int len
, fulllen
= 0;
3249 struct lxc_list
*it
;
3254 memset(retv
, 0, inlen
);
3256 lxc_list_for_each(it
, &c
->mount_list
) {
3257 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3263 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3264 struct lxc_conf
*c
, void *data
)
3266 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3269 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3270 struct lxc_conf
*c
, void *data
)
3272 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3275 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3276 struct lxc_conf
*c
, void *data
)
3278 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3281 static int get_config_rootfs_backend(const char *key
, char *retv
, int inlen
,
3282 struct lxc_conf
*c
, void *data
)
3287 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3288 struct lxc_conf
*c
, void *data
)
3290 return lxc_get_conf_str(
3292 c
->utsname
? c
->utsname
->nodename
: NULL
);
3295 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3296 struct lxc_conf
*c
, void *data
)
3299 int len
, fulllen
= 0, found
= -1;
3300 struct lxc_list
*it
;
3303 subkey
= strchr(key
, '.');
3305 subkey
= strchr(subkey
+ 1, '.');
3311 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3312 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3323 memset(retv
, 0, inlen
);
3325 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3326 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3331 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3332 struct lxc_conf
*c
, void *data
)
3334 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3337 static int get_config_net(const char *key
, char *retv
, int inlen
,
3338 struct lxc_conf
*c
, void *data
)
3340 int len
, fulllen
= 0;
3341 struct lxc_list
*it
;
3346 memset(retv
, 0, inlen
);
3348 lxc_list_for_each(it
, &c
->network
) {
3349 struct lxc_netdev
*n
= it
->elem
;
3350 const char *t
= lxc_net_type_to_str(n
->type
);
3351 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3357 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3358 struct lxc_conf
*c
, void *data
)
3360 int len
, fulllen
= 0;
3361 struct lxc_list
*it
;
3366 memset(retv
, 0, inlen
);
3368 lxc_list_for_each(it
, &c
->caps
) {
3369 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3375 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3376 struct lxc_conf
*c
, void *data
)
3378 int len
, fulllen
= 0;
3379 struct lxc_list
*it
;
3384 memset(retv
, 0, inlen
);
3386 lxc_list_for_each(it
, &c
->keepcaps
) {
3387 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3393 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3394 struct lxc_conf
*c
, void *data
)
3396 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3399 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3400 struct lxc_conf
*c
, void *data
)
3402 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3405 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3406 struct lxc_conf
*c
, void *data
)
3408 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3412 static int get_config_console_buffer_size(const char *key
, char *retv
,
3413 int inlen
, struct lxc_conf
*c
,
3416 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3419 static int get_config_console_buffer_logfile(const char *key
, char *retv
,
3420 int inlen
, struct lxc_conf
*c
,
3423 return lxc_get_conf_str(retv
, inlen
, c
->console
.buffer_log_file
);
3426 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3427 struct lxc_conf
*c
, void *data
)
3429 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3432 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3433 struct lxc_conf
*c
, void *data
)
3435 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3438 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3439 struct lxc_conf
*c
, void *data
)
3441 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3444 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3445 struct lxc_conf
*c
, void *data
)
3447 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3450 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3451 struct lxc_conf
*c
, void *data
)
3453 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3456 static int get_config_start(const char *key
, char *retv
, int inlen
,
3457 struct lxc_conf
*c
, void *data
)
3459 if (strcmp(key
+ 10, "auto") == 0)
3460 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3461 else if (strcmp(key
+ 10, "delay") == 0)
3462 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3463 else if (strcmp(key
+ 10, "order") == 0)
3464 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3469 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3470 struct lxc_conf
*c
, void *data
)
3472 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3475 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3476 struct lxc_conf
*c
, void *data
)
3478 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3481 static int get_config_group(const char *key
, char *retv
, int inlen
,
3482 struct lxc_conf
*c
, void *data
)
3484 int len
, fulllen
= 0;
3485 struct lxc_list
*it
;
3490 memset(retv
, 0, inlen
);
3492 lxc_list_for_each(it
, &c
->groups
) {
3493 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3499 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3500 struct lxc_conf
*c
, void *data
)
3502 int len
, fulllen
= 0;
3503 struct lxc_list
*it
;
3508 memset(retv
, 0, inlen
);
3510 lxc_list_for_each(it
, &c
->environment
) {
3511 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3517 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3518 struct lxc_conf
*c
, void *data
)
3520 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3523 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3524 struct lxc_conf
*c
, void *data
)
3526 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3529 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3530 struct lxc_conf
*c
, void *data
)
3532 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3535 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3536 struct lxc_conf
*c
, void *data
)
3538 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3541 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3542 struct lxc_conf
*c
, void *data
)
3544 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3547 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3548 struct lxc_conf
*c
, void *data
)
3550 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3553 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3554 struct lxc_conf
*c
, void *data
)
3556 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3559 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3560 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3561 * printed, in 'lxc.prlimit.resource = value' format.
3563 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3564 struct lxc_conf
*c
, void *data
)
3566 int fulllen
= 0, len
;
3567 bool get_all
= false;
3568 struct lxc_list
*it
;
3573 memset(retv
, 0, inlen
);
3575 if (!strcmp(key
, "lxc.prlimit"))
3577 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3582 lxc_list_for_each(it
, &c
->limits
) {
3583 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3584 integers or the word
3587 struct lxc_limit
*lim
= it
->elem
;
3589 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3590 memcpy(buf
, "unlimited", sizeof("unlimited"));
3591 partlen
= sizeof("unlimited") - 1;
3593 partlen
= sprintf(buf
, "%" PRIu64
,
3594 (uint64_t)lim
->limit
.rlim_cur
);
3596 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3597 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3598 memcpy(buf
+ partlen
, ":unlimited",
3599 sizeof(":unlimited"));
3601 sprintf(buf
+ partlen
, ":%" PRIu64
,
3602 (uint64_t)lim
->limit
.rlim_max
);
3606 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3607 lim
->resource
, buf
);
3608 } else if (!strcmp(lim
->resource
, key
)) {
3609 strprint(retv
, inlen
, "%s", buf
);
3616 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3617 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3618 * entries will be printed, in 'lxc.sysctl.key = value' format.
3620 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3621 struct lxc_conf
*c
, void *data
)
3624 struct lxc_list
*it
;
3626 bool get_all
= false;
3631 memset(retv
, 0, inlen
);
3633 if (strcmp(key
, "lxc.sysctl") == 0)
3635 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3636 key
+= sizeof("lxc.sysctl.") - 1;
3640 lxc_list_for_each(it
, &c
->sysctls
) {
3641 struct lxc_sysctl
*elem
= it
->elem
;
3643 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3644 elem
->key
, elem
->value
);
3645 } else if (strcmp(elem
->key
, key
) == 0) {
3646 strprint(retv
, inlen
, "%s", elem
->value
);
3653 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3654 struct lxc_conf
*c
, void *data
)
3656 struct lxc_list
*it
;
3659 bool get_all
= false;
3664 memset(retv
, 0, inlen
);
3666 if (strcmp(key
, "lxc.proc") == 0)
3668 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3669 key
+= sizeof("lxc.proc.") - 1;
3673 lxc_list_for_each(it
, &c
->procs
) {
3674 struct lxc_proc
*proc
= it
->elem
;
3677 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3678 proc
->filename
, proc
->value
);
3679 } else if (strcmp(proc
->filename
, key
) == 0) {
3680 strprint(retv
, inlen
, "%s", proc
->value
);
3687 static int get_config_noop(const char *key
, char *retv
, int inlen
,
3688 struct lxc_conf
*c
, void *data
)
3693 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3694 struct lxc_conf
*c
, void *data
)
3702 memset(retv
, 0, inlen
);
3704 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3705 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3706 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3712 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3713 struct lxc_conf
*c
, void *data
)
3721 memset(retv
, 0, inlen
);
3723 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3724 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3725 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3731 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3732 struct lxc_conf
*c
, void *data
)
3735 const char *namespace;
3741 memset(retv
, 0, inlen
);
3743 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
3744 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3748 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3753 /* Callbacks to clear config items. */
3754 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3757 c
->personality
= -1;
3761 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3768 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3775 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3783 static inline int clr_config_apparmor_profile(const char *key
,
3784 struct lxc_conf
*c
, void *data
)
3786 free(c
->lsm_aa_profile
);
3787 c
->lsm_aa_profile
= NULL
;
3791 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3795 c
->lsm_aa_allow_incomplete
= 0;
3799 static inline int clr_config_selinux_context(const char *key
,
3800 struct lxc_conf
*c
, void *data
)
3802 free(c
->lsm_se_context
);
3803 c
->lsm_se_context
= NULL
;
3807 static inline int clr_config_cgroup_controller(const char *key
,
3808 struct lxc_conf
*c
, void *data
)
3810 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3813 static inline int clr_config_cgroup2_controller(const char *key
,
3814 struct lxc_conf
*c
, void *data
)
3816 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3819 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3822 if (lxc_conf
->cgroup_meta
.dir
) {
3823 free(lxc_conf
->cgroup_meta
.dir
);
3824 lxc_conf
->cgroup_meta
.dir
= NULL
;
3830 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3833 return lxc_clear_idmaps(c
);
3836 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3839 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3843 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3851 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3854 return lxc_clear_mount_entries(c
);
3857 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3860 return lxc_clear_automounts(c
);
3863 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
3871 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
3874 free(c
->rootfs
.path
);
3875 c
->rootfs
.path
= NULL
;
3879 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
3882 free(c
->rootfs
.mount
);
3883 c
->rootfs
.mount
= NULL
;
3887 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
3890 free(c
->rootfs
.options
);
3891 c
->rootfs
.options
= NULL
;
3895 static inline int clr_config_rootfs_backend(const char *key
, struct lxc_conf
*c
,
3901 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
3909 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
3912 return lxc_clear_hooks(c
, key
);
3915 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
3918 /* default to legacy hooks version */
3919 c
->hooks_version
= 0;
3923 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
3926 lxc_free_networks(&c
->network
);
3931 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
3934 return lxc_clear_config_caps(c
);
3937 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
3940 return lxc_clear_config_keepcaps(c
);
3943 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
3946 free(c
->console
.path
);
3947 c
->console
.path
= NULL
;
3951 static inline int clr_config_console_logfile(const char *key
,
3952 struct lxc_conf
*c
, void *data
)
3954 free(c
->console
.log_path
);
3955 c
->console
.log_path
= NULL
;
3959 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
3962 c
->console
.log_rotate
= 0;
3966 static inline int clr_config_console_buffer_size(const char *key
,
3967 struct lxc_conf
*c
, void *data
)
3969 c
->console
.buffer_size
= 0;
3973 static inline int clr_config_console_buffer_logfile(const char *key
,
3977 free(c
->console
.buffer_log_file
);
3978 c
->console
.buffer_log_file
= NULL
;
3982 static inline int clr_config_seccomp_profile(const char *key
,
3983 struct lxc_conf
*c
, void *data
)
3990 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
3997 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4004 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4007 c
->rebootsignal
= 0;
4011 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4018 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4021 if (strcmp(key
+ 10, "auto") == 0)
4023 else if (strcmp(key
+ 10, "delay") == 0)
4025 else if (strcmp(key
+ 10, "order") == 0)
4031 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4039 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4042 c
->monitor_unshare
= 0;
4046 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4049 return lxc_clear_groups(c
);
4052 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4055 return lxc_clear_environment(c
);
4058 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4061 free(c
->execute_cmd
);
4062 c
->execute_cmd
= NULL
;
4066 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4074 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4082 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4089 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4096 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4103 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4106 c
->no_new_privs
= false;
4110 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4113 return lxc_clear_limits(c
, key
);
4116 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4119 return lxc_clear_sysctls(c
, key
);
4122 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4125 return lxc_clear_procs(c
, key
);
4128 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4131 lxc_clear_includes(c
);
4135 static inline int clr_config_noop(const char *key
, struct lxc_conf
*c
,
4141 static int clr_config_namespace_clone(const char *key
,
4142 struct lxc_conf
*lxc_conf
, void *data
)
4144 lxc_conf
->ns_clone
= 0;
4148 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4151 lxc_conf
->ns_keep
= 0;
4155 static int clr_config_namespace_share(const char *key
,
4156 struct lxc_conf
*lxc_conf
, void *data
)
4159 const char *namespace;
4161 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
4162 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4166 free(lxc_conf
->ns_share
[ns_idx
]);
4167 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4172 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4173 struct lxc_conf
*c
, void *data
)
4178 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4179 struct lxc_conf
*lxc_conf
,
4181 char **deindexed_key
)
4184 unsigned int tmpidx
;
4186 char *copy
, *idx_start
, *idx_end
;
4187 struct lxc_config_t
*config
= NULL
;
4189 /* check that this is a sensible network key */
4190 if (strncmp("lxc.net.", key
, 8)) {
4191 ERROR("Invalid network configuration key \"%s\"", key
);
4197 ERROR("Failed to duplicate string \"%s\"", key
);
4202 if (!isdigit(*(key
+ 8))) {
4203 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4207 /* beginning of index string */
4208 idx_start
= (copy
+ 7);
4211 /* end of index string */
4212 idx_end
= strchr((copy
+ 8), '.');
4216 /* parse current index */
4217 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4219 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4220 idx_start
+ 1, strerror(-ret
));
4225 /* This, of course is utterly nonsensical on so many levels, but
4226 * better safe than sorry.
4227 * (Checking for INT_MAX here is intentional.)
4229 if (tmpidx
== INT_MAX
) {
4230 SYSERROR("Number of configured networks would overflow the "
4236 numstrlen
= strlen((idx_start
+ 1));
4238 /* repair configuration key */
4241 /* lxc.net.<idx>.<subkey> */
4244 if (strlen(idx_end
+ 1) == 0) {
4245 ERROR("No subkey in network configuration key \"%s\"", key
);
4249 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4250 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4252 config
= lxc_get_config(copy
);
4254 ERROR("Unknown network configuration key \"%s\"", key
);
4260 *deindexed_key
= copy
;
4269 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4270 * found. So we make sure next comes an integer, find the right callback (by
4271 * rewriting the key), and call it.
4273 static int set_config_net_nic(const char *key
, const char *value
,
4274 struct lxc_conf
*lxc_conf
, void *data
)
4277 const char *idxstring
;
4278 struct lxc_config_t
*config
;
4279 struct lxc_netdev
*netdev
;
4281 char *deindexed_key
= NULL
;
4283 idxstring
= key
+ 8;
4284 if (!isdigit(*idxstring
))
4287 if (lxc_config_value_empty(value
))
4288 return clr_config_net_nic(key
, lxc_conf
, data
);
4290 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4291 if (!config
|| idx
< 0)
4294 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4296 free(deindexed_key
);
4300 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4301 free(deindexed_key
);
4305 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4309 const char *idxstring
;
4310 struct lxc_config_t
*config
;
4311 struct lxc_netdev
*netdev
;
4313 char *deindexed_key
= NULL
;
4315 idxstring
= key
+ 8;
4316 if (!isdigit(*idxstring
))
4319 /* The left conjunct is pretty self-explanatory. The right conjunct
4320 * checks whether the two pointers are equal. If they are we know that
4321 * this is not a key that is namespaced any further and so we are
4322 * supposed to clear the whole network.
4324 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4325 unsigned int rmnetdevidx
;
4327 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4330 /* Remove network from network list. */
4331 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4335 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4336 if (!config
|| idx
< 0)
4339 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4341 free(deindexed_key
);
4345 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4346 free(deindexed_key
);
4350 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4353 struct lxc_netdev
*netdev
= data
;
4363 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4366 struct lxc_netdev
*netdev
= data
;
4371 netdev
->name
[0] = '\0';
4376 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4379 struct lxc_netdev
*netdev
= data
;
4389 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4392 struct lxc_netdev
*netdev
= data
;
4397 netdev
->link
[0] = '\0';
4402 static int clr_config_net_macvlan_mode(const char *key
,
4403 struct lxc_conf
*lxc_conf
, void *data
)
4405 struct lxc_netdev
*netdev
= data
;
4410 if (netdev
->type
!= LXC_NET_MACVLAN
)
4413 netdev
->priv
.macvlan_attr
.mode
= -1;
4418 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4421 struct lxc_netdev
*netdev
= data
;
4426 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4431 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4434 struct lxc_netdev
*netdev
= data
;
4439 free(netdev
->upscript
);
4440 netdev
->upscript
= NULL
;
4445 static int clr_config_net_script_down(const char *key
,
4446 struct lxc_conf
*lxc_conf
, void *data
)
4448 struct lxc_netdev
*netdev
= data
;
4453 free(netdev
->downscript
);
4454 netdev
->downscript
= NULL
;
4459 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4462 struct lxc_netdev
*netdev
= data
;
4467 free(netdev
->hwaddr
);
4468 netdev
->hwaddr
= NULL
;
4473 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4476 struct lxc_netdev
*netdev
= data
;
4487 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4490 struct lxc_netdev
*netdev
= data
;
4495 netdev
->priv
.vlan_attr
.vid
= 0;
4500 static int clr_config_net_ipv4_gateway(const char *key
,
4501 struct lxc_conf
*lxc_conf
, void *data
)
4503 struct lxc_netdev
*netdev
= data
;
4508 free(netdev
->ipv4_gateway
);
4509 netdev
->ipv4_gateway
= NULL
;
4514 static int clr_config_net_ipv4_address(const char *key
,
4515 struct lxc_conf
*lxc_conf
, void *data
)
4517 struct lxc_netdev
*netdev
= data
;
4518 struct lxc_list
*cur
, *next
;
4523 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4532 static int clr_config_net_ipv6_gateway(const char *key
,
4533 struct lxc_conf
*lxc_conf
, void *data
)
4535 struct lxc_netdev
*netdev
= data
;
4540 free(netdev
->ipv6_gateway
);
4541 netdev
->ipv6_gateway
= NULL
;
4546 static int clr_config_net_ipv6_address(const char *key
,
4547 struct lxc_conf
*lxc_conf
, void *data
)
4549 struct lxc_netdev
*netdev
= data
;
4550 struct lxc_list
*cur
, *next
;
4555 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4564 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4565 struct lxc_conf
*c
, void *data
)
4568 const char *idxstring
;
4569 struct lxc_config_t
*config
;
4570 struct lxc_netdev
*netdev
;
4572 char *deindexed_key
= NULL
;
4574 idxstring
= key
+ 8;
4575 if (!isdigit(*idxstring
))
4578 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4579 if (!config
|| idx
< 0)
4582 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4584 free(deindexed_key
);
4588 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4589 free(deindexed_key
);
4593 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4594 struct lxc_conf
*c
, void *data
)
4598 struct lxc_netdev
*netdev
= data
;
4603 memset(retv
, 0, inlen
);
4608 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4613 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4614 struct lxc_conf
*c
, void *data
)
4618 struct lxc_netdev
*netdev
= data
;
4623 memset(retv
, 0, inlen
);
4628 if (netdev
->flags
& IFF_UP
)
4629 strprint(retv
, inlen
, "up");
4634 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4635 struct lxc_conf
*c
, void *data
)
4639 struct lxc_netdev
*netdev
= data
;
4644 memset(retv
, 0, inlen
);
4649 if (netdev
->link
[0] != '\0')
4650 strprint(retv
, inlen
, "%s", netdev
->link
);
4655 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4656 struct lxc_conf
*c
, void *data
)
4660 struct lxc_netdev
*netdev
= data
;
4665 memset(retv
, 0, inlen
);
4670 if (netdev
->name
[0] != '\0')
4671 strprint(retv
, inlen
, "%s", netdev
->name
);
4676 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4677 struct lxc_conf
*c
, void *data
)
4682 struct lxc_netdev
*netdev
= data
;
4687 memset(retv
, 0, inlen
);
4692 if (netdev
->type
!= LXC_NET_MACVLAN
)
4695 switch (netdev
->priv
.macvlan_attr
.mode
) {
4696 case MACVLAN_MODE_PRIVATE
:
4699 case MACVLAN_MODE_VEPA
:
4702 case MACVLAN_MODE_BRIDGE
:
4705 case MACVLAN_MODE_PASSTHRU
:
4713 strprint(retv
, inlen
, "%s", mode
);
4718 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4719 struct lxc_conf
*c
, void *data
)
4723 struct lxc_netdev
*netdev
= data
;
4728 memset(retv
, 0, inlen
);
4733 if (netdev
->type
!= LXC_NET_VETH
)
4736 strprint(retv
, inlen
, "%s",
4737 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4738 ? netdev
->priv
.veth_attr
.pair
4739 : netdev
->priv
.veth_attr
.veth1
);
4744 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4745 struct lxc_conf
*c
, void *data
)
4749 struct lxc_netdev
*netdev
= data
;
4754 memset(retv
, 0, inlen
);
4759 if (netdev
->upscript
)
4760 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4765 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4766 struct lxc_conf
*c
, void *data
)
4770 struct lxc_netdev
*netdev
= data
;
4775 memset(retv
, 0, inlen
);
4780 if (netdev
->downscript
)
4781 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4786 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4787 struct lxc_conf
*c
, void *data
)
4791 struct lxc_netdev
*netdev
= data
;
4796 memset(retv
, 0, inlen
);
4802 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4807 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4808 struct lxc_conf
*c
, void *data
)
4812 struct lxc_netdev
*netdev
= data
;
4817 memset(retv
, 0, inlen
);
4823 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4828 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4829 struct lxc_conf
*c
, void *data
)
4833 struct lxc_netdev
*netdev
= data
;
4838 memset(retv
, 0, inlen
);
4843 if (netdev
->type
!= LXC_NET_VLAN
)
4846 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4851 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4852 struct lxc_conf
*c
, void *data
)
4855 char buf
[INET_ADDRSTRLEN
];
4857 struct lxc_netdev
*netdev
= data
;
4862 memset(retv
, 0, inlen
);
4867 if (netdev
->ipv4_gateway_auto
) {
4868 strprint(retv
, inlen
, "auto");
4869 } else if (netdev
->ipv4_gateway
) {
4870 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
4871 strprint(retv
, inlen
, "%s", buf
);
4877 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
4878 struct lxc_conf
*c
, void *data
)
4882 char buf
[INET_ADDRSTRLEN
];
4883 struct lxc_list
*it
;
4885 struct lxc_netdev
*netdev
= data
;
4890 memset(retv
, 0, inlen
);
4895 listlen
= lxc_list_len(&netdev
->ipv4
);
4896 lxc_list_for_each(it
, &netdev
->ipv4
) {
4897 struct lxc_inetdev
*i
= it
->elem
;
4898 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
4899 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4900 (listlen
-- > 1) ? "\n" : "");
4906 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
4907 struct lxc_conf
*c
, void *data
)
4910 char buf
[INET6_ADDRSTRLEN
];
4912 struct lxc_netdev
*netdev
= data
;
4917 memset(retv
, 0, inlen
);
4922 if (netdev
->ipv6_gateway_auto
) {
4923 strprint(retv
, inlen
, "auto");
4924 } else if (netdev
->ipv6_gateway
) {
4925 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
4926 strprint(retv
, inlen
, "%s", buf
);
4932 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
4933 struct lxc_conf
*c
, void *data
)
4937 char buf
[INET6_ADDRSTRLEN
];
4938 struct lxc_list
*it
;
4940 struct lxc_netdev
*netdev
= data
;
4945 memset(retv
, 0, inlen
);
4950 listlen
= lxc_list_len(&netdev
->ipv6
);
4951 lxc_list_for_each(it
, &netdev
->ipv6
) {
4952 struct lxc_inet6dev
*i
= it
->elem
;
4953 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
4954 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
4955 (listlen
-- > 1) ? "\n" : "");
4961 int lxc_list_config_items(char *retv
, int inlen
)
4970 memset(retv
, 0, inlen
);
4972 for (i
= 0; i
< config_size
; i
++) {
4973 char *s
= config
[i
].name
;
4975 if (s
[strlen(s
) - 1] == '.')
4978 strprint(retv
, inlen
, "%s\n", s
);
4984 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
4993 memset(retv
, 0, inlen
);
4995 if (!strcmp(key
, "lxc.apparmor")) {
4996 strprint(retv
, inlen
, "allow_incomplete\n");
4997 strprint(retv
, inlen
, "profile\n");
4998 } else if (!strcmp(key
, "lxc.cgroup")) {
4999 strprint(retv
, inlen
, "dir\n");
5000 } else if (!strcmp(key
, "lxc.selinux")) {
5001 strprint(retv
, inlen
, "context\n");
5002 } else if (!strcmp(key
, "lxc.mount")) {
5003 strprint(retv
, inlen
, "auto\n");
5004 strprint(retv
, inlen
, "entry\n");
5005 strprint(retv
, inlen
, "fstab\n");
5006 } else if (!strcmp(key
, "lxc.rootfs")) {
5007 strprint(retv
, inlen
, "mount\n");
5008 strprint(retv
, inlen
, "options\n");
5009 strprint(retv
, inlen
, "path\n");
5010 } else if (!strcmp(key
, "lxc.uts")) {
5011 strprint(retv
, inlen
, "name\n");
5012 } else if (!strcmp(key
, "lxc.hook")) {
5013 strprint(retv
, inlen
, "autodev\n");
5014 strprint(retv
, inlen
, "clone\n");
5015 strprint(retv
, inlen
, "destroy\n");
5016 strprint(retv
, inlen
, "mount\n");
5017 strprint(retv
, inlen
, "post-stop\n");
5018 strprint(retv
, inlen
, "pre-mount\n");
5019 strprint(retv
, inlen
, "pre-start\n");
5020 strprint(retv
, inlen
, "start-host\n");
5021 strprint(retv
, inlen
, "start\n");
5022 strprint(retv
, inlen
, "stop\n");
5023 } else if (!strcmp(key
, "lxc.cap")) {
5024 strprint(retv
, inlen
, "drop\n");
5025 strprint(retv
, inlen
, "keep\n");
5026 } else if (!strcmp(key
, "lxc.console")) {
5027 strprint(retv
, inlen
, "logfile\n");
5028 strprint(retv
, inlen
, "path\n");
5029 } else if (!strcmp(key
, "lxc.seccomp")) {
5030 strprint(retv
, inlen
, "profile\n");
5031 } else if (!strcmp(key
, "lxc.signal")) {
5032 strprint(retv
, inlen
, "halt\n");
5033 strprint(retv
, inlen
, "reboot\n");
5034 strprint(retv
, inlen
, "stop\n");
5035 } else if (!strcmp(key
, "lxc.start")) {
5036 strprint(retv
, inlen
, "auto\n");
5037 strprint(retv
, inlen
, "delay\n");
5038 strprint(retv
, inlen
, "order\n");
5039 } else if (!strcmp(key
, "lxc.monitor")) {
5040 strprint(retv
, inlen
, "unshare\n");
5048 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5051 const char *idxstring
;
5052 struct lxc_netdev
*netdev
;
5056 idxstring
= key
+ 8;
5057 if (!isdigit(*idxstring
))
5060 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5064 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5071 memset(retv
, 0, inlen
);
5073 strprint(retv
, inlen
, "type\n");
5074 strprint(retv
, inlen
, "script.up\n");
5075 strprint(retv
, inlen
, "script.down\n");
5076 if (netdev
->type
!= LXC_NET_EMPTY
) {
5077 strprint(retv
, inlen
, "flags\n");
5078 strprint(retv
, inlen
, "link\n");
5079 strprint(retv
, inlen
, "name\n");
5080 strprint(retv
, inlen
, "hwaddr\n");
5081 strprint(retv
, inlen
, "mtu\n");
5082 strprint(retv
, inlen
, "ipv6.address\n");
5083 strprint(retv
, inlen
, "ipv6.gateway\n");
5084 strprint(retv
, inlen
, "ipv4.address\n");
5085 strprint(retv
, inlen
, "ipv4.gateway\n");
5088 switch (netdev
->type
) {
5090 strprint(retv
, inlen
, "veth.pair\n");
5092 case LXC_NET_MACVLAN
:
5093 strprint(retv
, inlen
, "macvlan.mode\n");
5096 strprint(retv
, inlen
, "vlan.id\n");