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
28 #define __STDC_FORMAT_MACROS
29 #include <arpa/inet.h>
36 #include <netinet/in.h>
41 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/utsname.h>
53 #include "confile_utils.h"
54 #include "../include/netns_ifaddrs.h"
56 #include "lxcseccomp.h"
57 #include "memory_utils.h"
63 #if HAVE_SYS_PERSONALITY_H
64 #include <sys/personality.h>
68 #include "include/strlcpy.h"
72 #include "include/strlcat.h"
75 lxc_log_define(confile
, lxc
);
77 #define lxc_config_define(name) \
78 __hot static int set_config_##name(const char *, const char *, \
79 struct lxc_conf *, void *); \
80 __hot static int get_config_##name(const char *, char *, int, \
81 struct lxc_conf *, void *); \
82 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
85 lxc_config_define(autodev
);
86 lxc_config_define(apparmor_allow_incomplete
);
87 lxc_config_define(apparmor_allow_nesting
);
88 lxc_config_define(apparmor_profile
);
89 lxc_config_define(apparmor_raw
);
90 lxc_config_define(cap_drop
);
91 lxc_config_define(cap_keep
);
92 lxc_config_define(cgroup_controller
);
93 lxc_config_define(cgroup2_controller
);
94 lxc_config_define(cgroup_dir
);
95 lxc_config_define(cgroup_relative
);
96 lxc_config_define(console_buffer_size
);
97 lxc_config_define(console_logfile
);
98 lxc_config_define(console_path
);
99 lxc_config_define(console_rotate
);
100 lxc_config_define(console_size
);
101 lxc_config_define(environment
);
102 lxc_config_define(ephemeral
);
103 lxc_config_define(execute_cmd
);
104 lxc_config_define(group
);
105 lxc_config_define(hooks
);
106 lxc_config_define(hooks_version
);
107 lxc_config_define(idmaps
);
108 lxc_config_define(includefiles
);
109 lxc_config_define(init_cmd
);
110 lxc_config_define(init_cwd
);
111 lxc_config_define(init_gid
);
112 lxc_config_define(init_uid
);
113 lxc_config_define(log_file
);
114 lxc_config_define(log_level
);
115 lxc_config_define(log_syslog
);
116 lxc_config_define(monitor
);
117 lxc_config_define(monitor_signal_pdeath
);
118 lxc_config_define(mount
);
119 lxc_config_define(mount_auto
);
120 lxc_config_define(mount_fstab
);
121 lxc_config_define(namespace_clone
);
122 lxc_config_define(namespace_keep
);
123 lxc_config_define(namespace_share
);
124 lxc_config_define(net
);
125 lxc_config_define(net_flags
);
126 lxc_config_define(net_hwaddr
);
127 lxc_config_define(net_ipv4_address
);
128 lxc_config_define(net_ipv4_gateway
);
129 lxc_config_define(net_ipv6_address
);
130 lxc_config_define(net_ipv6_gateway
);
131 lxc_config_define(net_link
);
132 lxc_config_define(net_l2proxy
);
133 lxc_config_define(net_macvlan_mode
);
134 lxc_config_define(net_ipvlan_mode
);
135 lxc_config_define(net_ipvlan_isolation
);
136 lxc_config_define(net_mtu
);
137 lxc_config_define(net_name
);
138 lxc_config_define(net_nic
);
139 lxc_config_define(net_script_down
);
140 lxc_config_define(net_script_up
);
141 lxc_config_define(net_type
);
142 lxc_config_define(net_veth_pair
);
143 lxc_config_define(net_veth_ipv4_route
);
144 lxc_config_define(net_veth_ipv6_route
);
145 lxc_config_define(net_vlan_id
);
146 lxc_config_define(no_new_privs
);
147 lxc_config_define(personality
);
148 lxc_config_define(prlimit
);
149 lxc_config_define(pty_max
);
150 lxc_config_define(rootfs_managed
);
151 lxc_config_define(rootfs_mount
);
152 lxc_config_define(rootfs_options
);
153 lxc_config_define(rootfs_path
);
154 lxc_config_define(seccomp_profile
);
155 lxc_config_define(seccomp_allow_nesting
);
156 lxc_config_define(seccomp_notify_proxy
);
157 lxc_config_define(selinux_context
);
158 lxc_config_define(signal_halt
);
159 lxc_config_define(signal_reboot
);
160 lxc_config_define(signal_stop
);
161 lxc_config_define(start
);
162 lxc_config_define(tty_max
);
163 lxc_config_define(tty_dir
);
164 lxc_config_define(uts_name
);
165 lxc_config_define(sysctl
);
166 lxc_config_define(proc
);
168 static struct lxc_config_t config_jump_table
[] = {
169 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
170 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
171 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
172 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
173 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
174 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
175 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
176 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
177 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
178 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
179 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
180 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
181 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
182 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
183 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
184 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
185 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
186 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
187 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
188 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
189 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
190 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
192 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
193 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
194 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
195 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
196 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
197 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
198 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
199 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
200 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
201 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
202 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
203 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
204 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
205 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
206 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
207 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
208 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
209 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
210 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
211 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
212 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
213 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
214 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
215 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
216 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
217 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
218 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
219 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
220 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
221 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
222 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
223 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
224 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
225 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
226 { "lxc.net.l2proxy", set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
227 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
228 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
229 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
230 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
231 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
232 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
233 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
234 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
235 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
236 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
237 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
238 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
239 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
240 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
241 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
242 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
243 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
244 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
245 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
246 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
247 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
248 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
249 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
250 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
251 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
252 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
253 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
254 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
255 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
256 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
257 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
258 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
259 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
260 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
261 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
262 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
265 static const size_t config_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
267 struct lxc_config_t
*lxc_get_config(const char *key
)
271 for (i
= 0; i
< config_jump_table_size
; i
++)
272 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
273 return &config_jump_table
[i
];
278 static int set_config_net(const char *key
, const char *value
,
279 struct lxc_conf
*lxc_conf
, void *data
)
281 if (!lxc_config_value_empty(value
)) {
282 ERROR("lxc.net must not have a value");
286 return clr_config_net(key
, lxc_conf
, data
);
289 static int set_config_net_type(const char *key
, const char *value
,
290 struct lxc_conf
*lxc_conf
, void *data
)
292 struct lxc_netdev
*netdev
= data
;
294 if (lxc_config_value_empty(value
))
295 return clr_config_net_type(key
, lxc_conf
, data
);
300 if (strcmp(value
, "veth") == 0) {
301 netdev
->type
= LXC_NET_VETH
;
302 lxc_list_init(&netdev
->priv
.veth_attr
.ipv4_routes
);
303 lxc_list_init(&netdev
->priv
.veth_attr
.ipv6_routes
);
304 } else if (strcmp(value
, "macvlan") == 0) {
305 netdev
->type
= LXC_NET_MACVLAN
;
306 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
307 } else if (strcmp(value
, "ipvlan") == 0) {
308 netdev
->type
= LXC_NET_IPVLAN
;
309 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
310 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
311 } else if (strcmp(value
, "vlan") == 0) {
312 netdev
->type
= LXC_NET_VLAN
;
313 } else if (strcmp(value
, "phys") == 0) {
314 netdev
->type
= LXC_NET_PHYS
;
315 } else if (strcmp(value
, "empty") == 0) {
316 netdev
->type
= LXC_NET_EMPTY
;
317 } else if (strcmp(value
, "none") == 0) {
318 netdev
->type
= LXC_NET_NONE
;
320 ERROR("Invalid network type %s", value
);
327 static int set_config_net_flags(const char *key
, const char *value
,
328 struct lxc_conf
*lxc_conf
, void *data
)
330 struct lxc_netdev
*netdev
= data
;
332 if (lxc_config_value_empty(value
))
333 return clr_config_net_flags(key
, lxc_conf
, data
);
338 netdev
->flags
|= IFF_UP
;
343 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
344 struct lxc_netdev
*netdev
)
346 struct netns_ifaddrs
*ifaddr
, *ifa
;
349 const char *type_key
= "lxc.net.type";
350 const char *link_key
= "lxc.net.link";
351 const char *tmpvalue
= "phys";
353 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0) {
354 SYSERROR("Failed to get network interfaces");
358 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
362 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
365 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
366 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
369 ret
= set_config_net_link(
370 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
372 ERROR("Failed to create matched ifnames");
376 ERROR("Failed to create matched ifnames");
382 netns_freeifaddrs(ifaddr
);
388 static int set_config_net_link(const char *key
, const char *value
,
389 struct lxc_conf
*lxc_conf
, void *data
)
391 struct lxc_netdev
*netdev
= data
;
394 if (lxc_config_value_empty(value
))
395 return clr_config_net_link(key
, lxc_conf
, data
);
400 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
401 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
403 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
408 static int set_config_net_l2proxy(const char *key
, const char *value
,
409 struct lxc_conf
*lxc_conf
, void *data
)
411 struct lxc_netdev
*netdev
= data
;
412 unsigned int val
= 0;
415 if (lxc_config_value_empty(value
))
416 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
419 return minus_one_set_errno(EINVAL
);
421 ret
= lxc_safe_uint(value
, &val
);
423 return minus_one_set_errno(-ret
);
427 netdev
->l2proxy
= false;
430 netdev
->l2proxy
= true;
434 return minus_one_set_errno(EINVAL
);
437 static int set_config_net_name(const char *key
, const char *value
,
438 struct lxc_conf
*lxc_conf
, void *data
)
440 struct lxc_netdev
*netdev
= data
;
442 if (lxc_config_value_empty(value
))
443 return clr_config_net_name(key
, lxc_conf
, data
);
448 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
451 static int set_config_net_veth_pair(const char *key
, const char *value
,
452 struct lxc_conf
*lxc_conf
, void *data
)
454 struct lxc_netdev
*netdev
= data
;
456 if (lxc_config_value_empty(value
))
457 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
462 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
465 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
466 struct lxc_conf
*lxc_conf
, void *data
)
468 struct lxc_netdev
*netdev
= data
;
470 if (lxc_config_value_empty(value
))
471 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
476 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
479 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
480 struct lxc_conf
*lxc_conf
, void *data
)
482 struct lxc_netdev
*netdev
= data
;
484 if (lxc_config_value_empty(value
))
485 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
488 return minus_one_set_errno(EINVAL
);
490 if (netdev
->type
!= LXC_NET_IPVLAN
) {
491 SYSERROR("Invalid ipvlan mode \"%s\", can only be used with ipvlan network", value
);
492 return minus_one_set_errno(EINVAL
);
495 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
498 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
499 struct lxc_conf
*lxc_conf
, void *data
)
501 struct lxc_netdev
*netdev
= data
;
503 if (lxc_config_value_empty(value
))
504 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
507 return minus_one_set_errno(EINVAL
);
509 if (netdev
->type
!= LXC_NET_IPVLAN
) {
510 SYSERROR("Invalid ipvlan isolation \"%s\", can only be used with ipvlan network", value
);
511 return minus_one_set_errno(EINVAL
);
514 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
517 static int set_config_net_hwaddr(const char *key
, const char *value
,
518 struct lxc_conf
*lxc_conf
, void *data
)
520 struct lxc_netdev
*netdev
= data
;
523 if (lxc_config_value_empty(value
))
524 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
529 new_value
= strdup(value
);
533 rand_complete_hwaddr(new_value
);
535 if (lxc_config_value_empty(new_value
)) {
537 netdev
->hwaddr
= NULL
;
541 netdev
->hwaddr
= new_value
;
546 static int set_config_net_vlan_id(const char *key
, const char *value
,
547 struct lxc_conf
*lxc_conf
, void *data
)
550 struct lxc_netdev
*netdev
= data
;
552 if (lxc_config_value_empty(value
))
553 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
558 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
565 static int set_config_net_mtu(const char *key
, const char *value
,
566 struct lxc_conf
*lxc_conf
, void *data
)
568 struct lxc_netdev
*netdev
= data
;
570 if (lxc_config_value_empty(value
))
571 return clr_config_net_mtu(key
, lxc_conf
, data
);
576 return set_config_string_item(&netdev
->mtu
, value
);
579 static int set_config_net_ipv4_address(const char *key
, const char *value
,
580 struct lxc_conf
*lxc_conf
, void *data
)
583 struct lxc_netdev
*netdev
= data
;
584 struct lxc_inetdev
*inetdev
;
585 struct lxc_list
*list
;
586 char *cursor
, *slash
;
587 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
589 if (lxc_config_value_empty(value
))
590 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
595 inetdev
= malloc(sizeof(*inetdev
));
598 memset(inetdev
, 0, sizeof(*inetdev
));
600 list
= malloc(sizeof(*list
));
607 list
->elem
= inetdev
;
609 addr
= strdup(value
);
616 cursor
= strstr(addr
, " ");
622 slash
= strstr(addr
, "/");
628 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
629 if (!ret
|| ret
< 0) {
630 SYSERROR("Invalid ipv4 address \"%s\"", value
);
638 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
639 if (!ret
|| ret
< 0) {
640 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
649 /* No prefix specified, determine it from the network class. */
651 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
659 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
662 /* If no broadcast address, let compute one from the
663 * prefix and address.
666 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
667 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
670 lxc_list_add_tail(&netdev
->ipv4
, list
);
676 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
677 struct lxc_conf
*lxc_conf
, void *data
)
679 struct lxc_netdev
*netdev
= data
;
681 if (lxc_config_value_empty(value
))
682 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
687 free(netdev
->ipv4_gateway
);
689 if (strcmp(value
, "auto") == 0) {
690 netdev
->ipv4_gateway
= NULL
;
691 netdev
->ipv4_gateway_auto
= true;
692 } else if (strcmp(value
, "dev") == 0) {
693 netdev
->ipv4_gateway
= NULL
;
694 netdev
->ipv4_gateway_auto
= false;
695 netdev
->ipv4_gateway_dev
= true;
700 gw
= malloc(sizeof(*gw
));
704 ret
= inet_pton(AF_INET
, value
, gw
);
705 if (!ret
|| ret
< 0) {
706 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
711 netdev
->ipv4_gateway
= gw
;
712 netdev
->ipv4_gateway_auto
= false;
718 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
719 struct lxc_conf
*lxc_conf
, void *data
)
721 __do_free
char *valdup
= NULL
;
722 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
723 __do_free
struct lxc_list
*list
= NULL
;
725 char *netmask
, *slash
;
726 struct lxc_netdev
*netdev
= data
;
728 if (lxc_config_value_empty(value
))
729 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
732 return minus_one_set_errno(EINVAL
);
734 if (netdev
->type
!= LXC_NET_VETH
) {
735 SYSERROR("Invalid ipv4 route \"%s\", can only be used with veth network", value
);
736 return minus_one_set_errno(EINVAL
);
739 inetdev
= malloc(sizeof(*inetdev
));
742 memset(inetdev
, 0, sizeof(*inetdev
));
744 list
= malloc(sizeof(*list
));
749 list
->elem
= inetdev
;
751 valdup
= strdup(value
);
755 slash
= strchr(valdup
, '/');
757 return minus_one_set_errno(EINVAL
);
762 return minus_one_set_errno(EINVAL
);
766 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
767 if (ret
< 0 || inetdev
->prefix
> 32)
768 return minus_one_set_errno(EINVAL
);
770 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
772 return minus_one_set_errno(EINVAL
);
774 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv4_routes
, list
);
781 static int set_config_net_ipv6_address(const char *key
, const char *value
,
782 struct lxc_conf
*lxc_conf
, void *data
)
785 struct lxc_netdev
*netdev
= data
;
786 struct lxc_inet6dev
*inet6dev
;
787 struct lxc_list
*list
;
788 char *slash
, *valdup
, *netmask
;
790 if (lxc_config_value_empty(value
))
791 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
796 inet6dev
= malloc(sizeof(*inet6dev
));
799 memset(inet6dev
, 0, sizeof(*inet6dev
));
801 list
= malloc(sizeof(*list
));
808 list
->elem
= inet6dev
;
810 valdup
= strdup(value
);
817 inet6dev
->prefix
= 64;
818 slash
= strstr(valdup
, "/");
823 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
832 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
833 if (!ret
|| ret
< 0) {
834 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
841 lxc_list_add_tail(&netdev
->ipv6
, list
);
847 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
848 struct lxc_conf
*lxc_conf
, void *data
)
850 struct lxc_netdev
*netdev
= data
;
852 if (lxc_config_value_empty(value
))
853 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
858 free(netdev
->ipv6_gateway
);
860 if (strcmp(value
, "auto") == 0) {
861 netdev
->ipv6_gateway
= NULL
;
862 netdev
->ipv6_gateway_auto
= true;
863 } else if (strcmp(value
, "dev") == 0) {
864 netdev
->ipv6_gateway
= NULL
;
865 netdev
->ipv6_gateway_auto
= false;
866 netdev
->ipv6_gateway_dev
= true;
871 gw
= malloc(sizeof(*gw
));
875 ret
= inet_pton(AF_INET6
, value
, gw
);
876 if (!ret
|| ret
< 0) {
877 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
882 netdev
->ipv6_gateway
= gw
;
883 netdev
->ipv6_gateway_auto
= false;
889 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
890 struct lxc_conf
*lxc_conf
, void *data
)
892 __do_free
char *valdup
;
893 __do_free
struct lxc_inet6dev
*inet6dev
;
894 __do_free
struct lxc_list
*list
;
896 char *netmask
, *slash
;
897 struct lxc_netdev
*netdev
= data
;
899 if (lxc_config_value_empty(value
))
900 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
903 return minus_one_set_errno(EINVAL
);
905 if (netdev
->type
!= LXC_NET_VETH
) {
906 SYSERROR("Invalid ipv6 route \"%s\", can only be used with veth network", value
);
907 return minus_one_set_errno(EINVAL
);
910 inet6dev
= malloc(sizeof(*inet6dev
));
913 memset(inet6dev
, 0, sizeof(*inet6dev
));
915 list
= malloc(sizeof(*list
));
920 list
->elem
= inet6dev
;
922 valdup
= strdup(value
);
926 slash
= strchr(valdup
, '/');
928 return minus_one_set_errno(EINVAL
);
933 return minus_one_set_errno(EINVAL
);
937 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
938 if (ret
< 0 || inet6dev
->prefix
> 128)
939 return minus_one_set_errno(EINVAL
);
941 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
943 return minus_one_set_errno(EINVAL
);
945 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv6_routes
, list
);
952 static int set_config_net_script_up(const char *key
, const char *value
,
953 struct lxc_conf
*lxc_conf
, void *data
)
955 struct lxc_netdev
*netdev
= data
;
957 if (lxc_config_value_empty(value
))
958 return clr_config_net_script_up(key
, lxc_conf
, data
);
963 return set_config_string_item(&netdev
->upscript
, value
);
966 static int set_config_net_script_down(const char *key
, const char *value
,
967 struct lxc_conf
*lxc_conf
, void *data
)
969 struct lxc_netdev
*netdev
= data
;
971 if (lxc_config_value_empty(value
))
972 return clr_config_net_script_down(key
, lxc_conf
, data
);
977 return set_config_string_item(&netdev
->downscript
, value
);
980 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
982 struct lxc_list
*hooklist
;
984 hooklist
= malloc(sizeof(*hooklist
));
990 hooklist
->elem
= hook
;
991 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
996 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
997 struct lxc_conf
*lxc_conf
, void *data
)
1000 if (lxc_config_value_empty(value
))
1001 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1003 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1006 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1007 return minus_one_set_errno(EINVAL
);
1016 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1017 struct lxc_conf
*lxc_conf
, void *data
)
1019 #ifdef HAVE_SECCOMP_NOTIFY
1022 if (lxc_config_value_empty(value
))
1023 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1025 if (strncmp(value
, "unix:", 5) != 0)
1026 return minus_one_set_errno(EINVAL
);
1029 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1034 return minus_one_set_errno(ENOSYS
);
1038 static int set_config_seccomp_profile(const char *key
, const char *value
,
1039 struct lxc_conf
*lxc_conf
, void *data
)
1041 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1044 static int set_config_execute_cmd(const char *key
, const char *value
,
1045 struct lxc_conf
*lxc_conf
, void *data
)
1047 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1050 static int set_config_init_cmd(const char *key
, const char *value
,
1051 struct lxc_conf
*lxc_conf
, void *data
)
1053 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1056 static int set_config_init_cwd(const char *key
, const char *value
,
1057 struct lxc_conf
*lxc_conf
, void *data
)
1059 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1062 static int set_config_init_uid(const char *key
, const char *value
,
1063 struct lxc_conf
*lxc_conf
, void *data
)
1065 unsigned int init_uid
;
1067 if (lxc_config_value_empty(value
)) {
1068 lxc_conf
->init_uid
= 0;
1072 if (lxc_safe_uint(value
, &init_uid
) < 0)
1075 lxc_conf
->init_uid
= init_uid
;
1080 static int set_config_init_gid(const char *key
, const char *value
,
1081 struct lxc_conf
*lxc_conf
, void *data
)
1083 unsigned int init_gid
;
1085 if (lxc_config_value_empty(value
)) {
1086 lxc_conf
->init_gid
= 0;
1090 if (lxc_safe_uint(value
, &init_gid
) < 0)
1093 lxc_conf
->init_gid
= init_gid
;
1098 static int set_config_hooks(const char *key
, const char *value
,
1099 struct lxc_conf
*lxc_conf
, void *data
)
1103 if (lxc_config_value_empty(value
))
1104 return lxc_clear_hooks(lxc_conf
, key
);
1106 if (strcmp(key
+ 4, "hook") == 0) {
1107 ERROR("lxc.hook must not have a value");
1111 copy
= strdup(value
);
1115 if (strcmp(key
+ 9, "pre-start") == 0)
1116 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
1117 else if (strcmp(key
+ 9, "start-host") == 0)
1118 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
1119 else if (strcmp(key
+ 9, "pre-mount") == 0)
1120 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
1121 else if (strcmp(key
+ 9, "autodev") == 0)
1122 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
1123 else if (strcmp(key
+ 9, "mount") == 0)
1124 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
1125 else if (strcmp(key
+ 9, "start") == 0)
1126 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
1127 else if (strcmp(key
+ 9, "stop") == 0)
1128 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
1129 else if (strcmp(key
+ 9, "post-stop") == 0)
1130 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
1131 else if (strcmp(key
+ 9, "clone") == 0)
1132 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
1133 else if (strcmp(key
+ 9, "destroy") == 0)
1134 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
1141 static int set_config_hooks_version(const char *key
, const char *value
,
1142 struct lxc_conf
*lxc_conf
, void *data
)
1147 if (lxc_config_value_empty(value
))
1148 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1150 ret
= lxc_safe_uint(value
, &tmp
);
1155 ERROR("Invalid hook version specified. Currently only 0 "
1156 "(legacy) and 1 are supported");
1160 lxc_conf
->hooks_version
= tmp
;
1165 static int set_config_personality(const char *key
, const char *value
,
1166 struct lxc_conf
*lxc_conf
, void *data
)
1168 signed long personality
= lxc_config_parse_arch(value
);
1170 if (personality
>= 0)
1171 lxc_conf
->personality
= personality
;
1173 WARN("Unsupported personality \"%s\"", value
);
1178 static int set_config_pty_max(const char *key
, const char *value
,
1179 struct lxc_conf
*lxc_conf
, void *data
)
1182 unsigned int max
= 0;
1184 if (lxc_config_value_empty(value
)) {
1185 lxc_conf
->pty_max
= 0;
1189 ret
= lxc_safe_uint(value
, &max
);
1193 lxc_conf
->pty_max
= max
;
1198 /* We only need to check whether the first byte of the key after the lxc.start.
1199 * prefix matches our expectations since they fortunately all start with a
1200 * different letter. If anything was wrong with the key we would have already
1201 * noticed when the callback was called.
1203 static int set_config_start(const char *key
, const char *value
,
1204 struct lxc_conf
*lxc_conf
, void *data
)
1208 is_empty
= lxc_config_value_empty(value
);
1210 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1212 lxc_conf
->start_auto
= 0;
1216 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1219 if (lxc_conf
->start_auto
> 1)
1223 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1225 lxc_conf
->start_delay
= 0;
1229 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1230 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1232 lxc_conf
->start_order
= 0;
1236 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1242 static int set_config_monitor(const char *key
, const char *value
,
1243 struct lxc_conf
*lxc_conf
, void *data
)
1245 if (lxc_config_value_empty(value
)) {
1246 lxc_conf
->monitor_unshare
= 0;
1250 if (strcmp(key
+ 12, "unshare") == 0)
1251 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1256 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1257 struct lxc_conf
*lxc_conf
, void *data
)
1259 if (lxc_config_value_empty(value
)) {
1260 lxc_conf
->monitor_signal_pdeath
= 0;
1264 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1267 sig_n
= sig_parse(value
);
1271 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1278 static int set_config_group(const char *key
, const char *value
,
1279 struct lxc_conf
*lxc_conf
, void *data
)
1281 char *groups
, *token
;
1282 struct lxc_list
*grouplist
;
1285 if (lxc_config_value_empty(value
))
1286 return lxc_clear_groups(lxc_conf
);
1288 groups
= strdup(value
);
1292 /* In case several groups are specified in a single line split these
1293 * groups in a single element for the list.
1295 lxc_iterate_parts(token
, groups
, " \t") {
1296 grouplist
= malloc(sizeof(*grouplist
));
1302 grouplist
->elem
= strdup(token
);
1303 if (!grouplist
->elem
) {
1309 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1317 static int set_config_environment(const char *key
, const char *value
,
1318 struct lxc_conf
*lxc_conf
, void *data
)
1320 struct lxc_list
*list_item
= NULL
;
1322 if (lxc_config_value_empty(value
))
1323 return lxc_clear_environment(lxc_conf
);
1325 list_item
= malloc(sizeof(*list_item
));
1329 if (!strchr(value
, '=')) {
1330 const char *env_val
;
1331 const char *env_key
= value
;
1332 const char *env_var
[3] = {0};
1334 env_val
= getenv(env_key
);
1338 env_var
[0] = env_key
;
1339 env_var
[1] = env_val
;
1340 list_item
->elem
= lxc_string_join("=", env_var
, false);
1342 list_item
->elem
= strdup(value
);
1345 if (!list_item
->elem
)
1348 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1358 static int set_config_tty_max(const char *key
, const char *value
,
1359 struct lxc_conf
*lxc_conf
, void *data
)
1362 unsigned int nbtty
= 0;
1364 if (lxc_config_value_empty(value
)) {
1365 lxc_conf
->ttys
.max
= 0;
1369 ret
= lxc_safe_uint(value
, &nbtty
);
1373 lxc_conf
->ttys
.max
= nbtty
;
1378 static int set_config_tty_dir(const char *key
, const char *value
,
1379 struct lxc_conf
*lxc_conf
, void *data
)
1381 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1385 static int set_config_apparmor_profile(const char *key
, const char *value
,
1386 struct lxc_conf
*lxc_conf
, void *data
)
1388 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1391 static int set_config_apparmor_allow_incomplete(const char *key
,
1393 struct lxc_conf
*lxc_conf
,
1396 if (lxc_config_value_empty(value
)) {
1397 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1401 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1404 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1410 static int set_config_apparmor_allow_nesting(const char *key
,
1412 struct lxc_conf
*lxc_conf
,
1415 if (lxc_config_value_empty(value
))
1416 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1418 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1421 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1427 static int set_config_apparmor_raw(const char *key
,
1429 struct lxc_conf
*lxc_conf
,
1433 struct lxc_list
*list
;
1435 if (lxc_config_value_empty(value
))
1436 return lxc_clear_apparmor_raw(lxc_conf
);
1438 list
= malloc(sizeof(*list
));
1444 elem
= strdup(value
);
1451 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1456 static int set_config_selinux_context(const char *key
, const char *value
,
1457 struct lxc_conf
*lxc_conf
, void *data
)
1459 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1462 static int set_config_log_file(const char *key
, const char *value
,
1463 struct lxc_conf
*c
, void *data
)
1467 if (lxc_config_value_empty(value
)) {
1473 /* Store these values in the lxc_conf, and then try to set for actual
1476 ret
= set_config_path_item(&c
->logfile
, value
);
1478 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1483 static int set_config_log_level(const char *key
, const char *value
,
1484 struct lxc_conf
*lxc_conf
, void *data
)
1488 if (lxc_config_value_empty(value
)) {
1489 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1493 if (value
[0] >= '0' && value
[0] <= '9') {
1494 if (lxc_safe_int(value
, &newlevel
) < 0)
1497 newlevel
= lxc_log_priority_to_int(value
);
1500 /* Store these values in the lxc_conf, and then try to set for actual
1503 lxc_conf
->loglevel
= newlevel
;
1505 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1508 static int set_config_autodev(const char *key
, const char *value
,
1509 struct lxc_conf
*lxc_conf
, void *data
)
1511 if (lxc_config_value_empty(value
)) {
1512 lxc_conf
->autodev
= 0;
1516 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1519 if (lxc_conf
->autodev
> 1)
1525 static int set_config_signal_halt(const char *key
, const char *value
,
1526 struct lxc_conf
*lxc_conf
, void *data
)
1530 if (lxc_config_value_empty(value
)) {
1531 lxc_conf
->haltsignal
= 0;
1535 sig_n
= sig_parse(value
);
1539 lxc_conf
->haltsignal
= sig_n
;
1544 static int set_config_signal_reboot(const char *key
, const char *value
,
1545 struct lxc_conf
*lxc_conf
, void *data
)
1549 if (lxc_config_value_empty(value
)) {
1550 lxc_conf
->rebootsignal
= 0;
1554 sig_n
= sig_parse(value
);
1558 lxc_conf
->rebootsignal
= sig_n
;
1563 static int set_config_signal_stop(const char *key
, const char *value
,
1564 struct lxc_conf
*lxc_conf
, void *data
)
1568 if (lxc_config_value_empty(value
)) {
1569 lxc_conf
->stopsignal
= 0;
1573 sig_n
= sig_parse(value
);
1577 lxc_conf
->stopsignal
= sig_n
;
1582 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1583 struct lxc_conf
*lxc_conf
, int version
)
1585 const char *subkey
, *token
;
1587 struct lxc_list
*cglist
= NULL
;
1588 struct lxc_cgroup
*cgelem
= NULL
;
1590 if (lxc_config_value_empty(value
))
1591 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1593 if (version
== CGROUP2_SUPER_MAGIC
) {
1594 token
= "lxc.cgroup2.";
1596 } else if (version
== CGROUP_SUPER_MAGIC
) {
1597 token
= "lxc.cgroup.";
1603 if (strncmp(key
, token
, token_len
) != 0)
1606 subkey
= key
+ token_len
;
1607 if (*subkey
== '\0')
1610 cglist
= malloc(sizeof(*cglist
));
1614 cgelem
= malloc(sizeof(*cgelem
));
1617 memset(cgelem
, 0, sizeof(*cgelem
));
1619 cgelem
->subsystem
= strdup(subkey
);
1620 if (!cgelem
->subsystem
)
1623 cgelem
->value
= strdup(value
);
1627 cgelem
->version
= version
;
1629 lxc_list_add_elem(cglist
, cgelem
);
1631 if (version
== CGROUP2_SUPER_MAGIC
)
1632 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1634 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1641 free(cgelem
->subsystem
);
1642 free(cgelem
->value
);
1649 static int set_config_cgroup_controller(const char *key
, const char *value
,
1650 struct lxc_conf
*lxc_conf
, void *data
)
1652 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1653 CGROUP_SUPER_MAGIC
);
1656 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1657 struct lxc_conf
*lxc_conf
, void *data
)
1659 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1660 CGROUP2_SUPER_MAGIC
);
1664 static int set_config_cgroup_dir(const char *key
, const char *value
,
1665 struct lxc_conf
*lxc_conf
, void *data
)
1667 if (lxc_config_value_empty(value
))
1668 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1670 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1673 static int set_config_cgroup_relative(const char *key
, const char *value
,
1674 struct lxc_conf
*lxc_conf
, void *data
)
1676 unsigned int converted
;
1679 if (lxc_config_value_empty(value
))
1680 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1682 ret
= lxc_safe_uint(value
, &converted
);
1686 if (converted
== 1) {
1687 lxc_conf
->cgroup_meta
.relative
= true;
1691 if (converted
== 0) {
1692 lxc_conf
->cgroup_meta
.relative
= false;
1699 static bool parse_limit_value(const char **value
, rlim_t
*res
)
1701 char *endptr
= NULL
;
1703 if (strncmp(*value
, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1704 *res
= RLIM_INFINITY
;
1705 *value
+= STRLITERALLEN("unlimited");
1710 *res
= strtoull(*value
, &endptr
, 10);
1711 if (errno
|| !endptr
)
1719 static int set_config_prlimit(const char *key
, const char *value
,
1720 struct lxc_conf
*lxc_conf
, void *data
)
1722 struct lxc_list
*iter
;
1723 struct rlimit limit
;
1725 struct lxc_list
*limlist
= NULL
;
1726 struct lxc_limit
*limelem
= NULL
;
1728 if (lxc_config_value_empty(value
))
1729 return lxc_clear_limits(lxc_conf
, key
);
1731 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1734 key
+= STRLITERALLEN("lxc.prlimit.");
1736 /* soft limit comes first in the value */
1737 if (!parse_limit_value(&value
, &limit_value
))
1740 limit
.rlim_cur
= limit_value
;
1742 /* skip spaces and a colon */
1743 while (isspace(*value
))
1748 else if (*value
) /* any other character is an error here */
1751 while (isspace(*value
))
1754 /* optional hard limit */
1756 if (!parse_limit_value(&value
, &limit_value
))
1759 limit
.rlim_max
= limit_value
;
1761 /* check for trailing garbage */
1762 while (isspace(*value
))
1768 /* a single value sets both hard and soft limit */
1769 limit
.rlim_max
= limit
.rlim_cur
;
1772 /* find existing list element */
1773 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1774 limelem
= iter
->elem
;
1775 if (!strcmp(key
, limelem
->resource
)) {
1776 limelem
->limit
= limit
;
1781 /* allocate list element */
1782 limlist
= malloc(sizeof(*limlist
));
1786 limelem
= malloc(sizeof(*limelem
));
1789 memset(limelem
, 0, sizeof(*limelem
));
1791 limelem
->resource
= strdup(key
);
1792 if (!limelem
->resource
)
1795 limelem
->limit
= limit
;
1796 lxc_list_add_elem(limlist
, limelem
);;
1797 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1805 free(limelem
->resource
);
1812 static int set_config_sysctl(const char *key
, const char *value
,
1813 struct lxc_conf
*lxc_conf
, void *data
)
1815 struct lxc_list
*iter
;
1816 char *replace_value
= NULL
;
1817 struct lxc_list
*sysctl_list
= NULL
;
1818 struct lxc_sysctl
*sysctl_elem
= NULL
;
1820 if (lxc_config_value_empty(value
))
1821 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1823 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1826 key
+= STRLITERALLEN("lxc.sysctl.");
1828 /* find existing list element */
1829 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1830 sysctl_elem
= iter
->elem
;
1832 if (strcmp(key
, sysctl_elem
->key
) != 0)
1835 replace_value
= strdup(value
);
1839 free(sysctl_elem
->value
);
1840 sysctl_elem
->value
= replace_value
;
1845 /* allocate list element */
1846 sysctl_list
= malloc(sizeof(*sysctl_list
));
1850 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1853 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1855 sysctl_elem
->key
= strdup(key
);
1856 if (!sysctl_elem
->key
)
1859 sysctl_elem
->value
= strdup(value
);
1860 if (!sysctl_elem
->value
)
1863 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1864 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1872 free(sysctl_elem
->key
);
1873 free(sysctl_elem
->value
);
1880 static int set_config_proc(const char *key
, const char *value
,
1881 struct lxc_conf
*lxc_conf
, void *data
)
1884 struct lxc_list
*proclist
= NULL
;
1885 struct lxc_proc
*procelem
= NULL
;
1887 if (lxc_config_value_empty(value
))
1888 return clr_config_proc(key
, lxc_conf
, NULL
);
1890 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
1893 subkey
= key
+ STRLITERALLEN("lxc.proc.");
1894 if (*subkey
== '\0')
1897 proclist
= malloc(sizeof(*proclist
));
1901 procelem
= malloc(sizeof(*procelem
));
1904 memset(procelem
, 0, sizeof(*procelem
));
1906 procelem
->filename
= strdup(subkey
);
1907 procelem
->value
= strdup(value
);
1909 if (!procelem
->filename
|| !procelem
->value
)
1912 proclist
->elem
= procelem
;
1914 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1922 free(procelem
->filename
);
1923 free(procelem
->value
);
1930 static int set_config_idmaps(const char *key
, const char *value
,
1931 struct lxc_conf
*lxc_conf
, void *data
)
1933 unsigned long hostid
, nsid
, range
;
1936 struct lxc_list
*idmaplist
= NULL
;
1937 struct id_map
*idmap
= NULL
;
1939 if (lxc_config_value_empty(value
))
1940 return lxc_clear_idmaps(lxc_conf
);
1942 idmaplist
= malloc(sizeof(*idmaplist
));
1946 idmap
= malloc(sizeof(*idmap
));
1949 memset(idmap
, 0, sizeof(*idmap
));
1951 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1953 ERROR("Failed to parse id mappings");
1957 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1959 idmap
->idtype
= ID_TYPE_UID
;
1960 else if (type
== 'g')
1961 idmap
->idtype
= ID_TYPE_GID
;
1965 idmap
->hostid
= hostid
;
1967 idmap
->range
= range
;
1968 idmaplist
->elem
= idmap
;
1969 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1971 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1972 if (idmap
->nsid
== 0)
1973 lxc_conf
->root_nsuid_map
= idmap
;
1975 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1976 if (idmap
->nsid
== 0)
1977 lxc_conf
->root_nsgid_map
= idmap
;
1990 static int set_config_mount_fstab(const char *key
, const char *value
,
1991 struct lxc_conf
*lxc_conf
, void *data
)
1993 if (lxc_config_value_empty(value
)) {
1994 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1998 return set_config_path_item(&lxc_conf
->fstab
, value
);
2001 static int set_config_mount_auto(const char *key
, const char *value
,
2002 struct lxc_conf
*lxc_conf
, void *data
)
2004 char *autos
, *token
;
2011 } allowed_auto_mounts
[] = {
2012 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2013 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2014 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2015 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2016 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2017 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2018 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2019 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2020 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2021 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2022 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2023 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2024 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2025 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2026 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2027 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2028 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2029 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2030 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2031 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2032 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2033 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2034 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2035 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2036 /* For adding anything that is just a single on/off, but has no
2037 * options: keep mask and flag identical and just define the enum
2038 * value as an unused bit so far
2043 if (lxc_config_value_empty(value
)) {
2044 lxc_conf
->auto_mounts
= 0;
2048 autos
= strdup(value
);
2052 lxc_iterate_parts(token
, autos
, " \t") {
2053 bool is_shmounts
= false;
2055 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2056 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
2059 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
2060 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
2066 if (!allowed_auto_mounts
[i
].token
) {
2067 ERROR("Invalid filesystem to automount \"%s\"", token
);
2071 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2072 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2075 char *container_path
;
2078 host_path
= token
+ STRLITERALLEN("shmounts:");
2079 if (*host_path
== '\0') {
2080 SYSERROR("Failed to copy shmounts host path");
2084 container_path
= strchr(host_path
, ':');
2085 if (!container_path
|| *(container_path
+ 1) == '\0')
2086 container_path
= "/dev/.lxc-mounts";
2088 *container_path
++ = '\0';
2090 lxc_conf
->shmount
.path_host
= strdup(host_path
);
2091 if (!lxc_conf
->shmount
.path_host
) {
2092 SYSERROR("Failed to copy shmounts host path");
2096 lxc_conf
->shmount
.path_cont
= strdup(container_path
);
2097 if(!lxc_conf
->shmount
.path_cont
) {
2098 SYSERROR("Failed to copy shmounts container path");
2112 static int set_config_mount(const char *key
, const char *value
,
2113 struct lxc_conf
*lxc_conf
, void *data
)
2116 struct lxc_list
*mntlist
;
2118 if (lxc_config_value_empty(value
))
2119 return lxc_clear_mount_entries(lxc_conf
);
2121 mntlist
= malloc(sizeof(*mntlist
));
2125 mntelem
= strdup(value
);
2130 mntlist
->elem
= mntelem
;
2132 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
2137 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2138 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2141 static int set_config_cap_keep(const char *key
, const char *value
,
2142 struct lxc_conf
*lxc_conf
, void *data
)
2144 char *keepcaps
, *token
;
2145 struct lxc_list
*keeplist
;
2148 if (lxc_config_value_empty(value
))
2149 return lxc_clear_config_keepcaps(lxc_conf
);
2151 keepcaps
= strdup(value
);
2155 /* In case several capability keep is specified in a single line
2156 * split these caps in a single element for the list.
2158 lxc_iterate_parts(token
, keepcaps
, " \t") {
2159 if (!strcmp(token
, "none"))
2160 lxc_clear_config_keepcaps(lxc_conf
);
2162 keeplist
= malloc(sizeof(*keeplist
));
2166 keeplist
->elem
= strdup(token
);
2167 if (!keeplist
->elem
) {
2172 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
2183 static int set_config_cap_drop(const char *key
, const char *value
,
2184 struct lxc_conf
*lxc_conf
, void *data
)
2186 char *dropcaps
, *token
;
2187 struct lxc_list
*droplist
;
2190 if (lxc_config_value_empty(value
))
2191 return lxc_clear_config_caps(lxc_conf
);
2193 dropcaps
= strdup(value
);
2197 /* In case several capability drop is specified in a single line
2198 * split these caps in a single element for the list.
2200 lxc_iterate_parts(token
, dropcaps
, " \t") {
2201 droplist
= malloc(sizeof(*droplist
));
2205 droplist
->elem
= strdup(token
);
2206 if (!droplist
->elem
) {
2211 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
2222 static int set_config_console_path(const char *key
, const char *value
,
2223 struct lxc_conf
*lxc_conf
, void *data
)
2225 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2228 static int set_config_console_rotate(const char *key
, const char *value
,
2229 struct lxc_conf
*lxc_conf
, void *data
)
2231 if (lxc_config_value_empty(value
)) {
2232 lxc_conf
->console
.log_rotate
= 0;
2236 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
2239 if (lxc_conf
->console
.log_rotate
> 1) {
2240 ERROR("The \"lxc.console.rotate\" config key can only be set "
2248 static int set_config_console_logfile(const char *key
, const char *value
,
2249 struct lxc_conf
*lxc_conf
, void *data
)
2251 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2254 static int set_config_console_buffer_size(const char *key
, const char *value
,
2255 struct lxc_conf
*lxc_conf
, void *data
)
2259 uint64_t buffer_size
, pgsz
;
2261 if (lxc_config_value_empty(value
)) {
2262 lxc_conf
->console
.buffer_size
= 0;
2266 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2267 if (!strcmp(value
, "auto")) {
2268 lxc_conf
->console
.buffer_size
= 1 << 17;
2272 ret
= parse_byte_size_string(value
, &size
);
2279 /* must be at least a page size */
2280 pgsz
= lxc_getpagesize();
2281 if ((uint64_t)size
< pgsz
) {
2282 NOTICE("Requested ringbuffer size for the console is %" PRId64
2283 " but must be at least %" PRId64
2284 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2289 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2290 if (buffer_size
== 0)
2293 if (buffer_size
!= size
)
2294 NOTICE("Passed size was not a power of 2. Rounding log size to "
2295 "next power of two: %" PRIu64
" bytes", buffer_size
);
2297 lxc_conf
->console
.buffer_size
= buffer_size
;
2302 static int set_config_console_size(const char *key
, const char *value
,
2303 struct lxc_conf
*lxc_conf
, void *data
)
2307 uint64_t log_size
, pgsz
;
2309 if (lxc_config_value_empty(value
)) {
2310 lxc_conf
->console
.log_size
= 0;
2314 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2315 if (!strcmp(value
, "auto")) {
2316 lxc_conf
->console
.log_size
= 1 << 17;
2320 ret
= parse_byte_size_string(value
, &size
);
2327 /* must be at least a page size */
2328 pgsz
= lxc_getpagesize();
2329 if ((uint64_t)size
< pgsz
) {
2330 NOTICE("Requested ringbuffer size for the console is %" PRId64
2331 " but must be at least %" PRId64
2332 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2337 log_size
= lxc_find_next_power2((uint64_t)size
);
2341 if (log_size
!= size
)
2342 NOTICE("Passed size was not a power of 2. Rounding log size to "
2343 "next power of two: %" PRIu64
" bytes", log_size
);
2345 lxc_conf
->console
.log_size
= log_size
;
2351 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2352 * file, we expand it in the unexpanded_config, so that after a save_config we
2353 * store the hwaddr for re-use.
2354 * This is only called when reading the config file, not when executing a
2356 * 'x' and 'X' are substituted in-place.
2358 static void update_hwaddr(const char *line
)
2362 line
+= lxc_char_left_gc(line
, strlen(line
));
2366 if (!lxc_config_net_is_hwaddr(line
))
2369 /* Let config_net_hwaddr raise the error. */
2370 p
= strchr(line
, '=');
2381 rand_complete_hwaddr(p
);
2384 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2387 size_t len
= conf
->unexpanded_len
;
2389 update_hwaddr(line
);
2391 linelen
= strlen(line
);
2392 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2393 char *tmp
= realloc(conf
->unexpanded_config
,
2394 conf
->unexpanded_alloced
+ 1024);
2398 if (!conf
->unexpanded_config
)
2401 conf
->unexpanded_config
= tmp
;
2402 conf
->unexpanded_alloced
+= 1024;
2405 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2406 conf
->unexpanded_len
+= linelen
;
2407 if (line
[linelen
- 1] != '\n')
2408 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2409 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2414 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2416 struct dirent
*direntp
;
2418 char path
[PATH_MAX
];
2422 dir
= opendir(dirp
);
2426 while ((direntp
= readdir(dir
))) {
2429 fnam
= direntp
->d_name
;
2430 if (!strcmp(fnam
, "."))
2433 if (!strcmp(fnam
, ".."))
2437 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2440 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2441 if (len
< 0 || len
>= PATH_MAX
) {
2446 ret
= lxc_config_read(path
, lxc_conf
, true);
2458 static int set_config_includefiles(const char *key
, const char *value
,
2459 struct lxc_conf
*lxc_conf
, void *data
)
2461 if (lxc_config_value_empty(value
)) {
2462 clr_config_includefiles(key
, lxc_conf
, NULL
);
2467 return do_includedir(value
, lxc_conf
);
2469 return lxc_config_read(value
, lxc_conf
, true);
2472 static int set_config_rootfs_path(const char *key
, const char *value
,
2473 struct lxc_conf
*lxc_conf
, void *data
)
2477 const char *container_path
;
2479 if (lxc_config_value_empty(value
)) {
2480 free(lxc_conf
->rootfs
.path
);
2481 lxc_conf
->rootfs
.path
= NULL
;
2485 dup
= strdup(value
);
2489 /* Split <storage type>:<container path> into <storage type> and
2490 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2491 * "rootfs.path" to <container path>.
2493 tmp
= strchr(dup
, ':');
2497 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2504 container_path
= tmp
;
2506 container_path
= value
;
2509 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2515 static int set_config_rootfs_managed(const char *key
, const char *value
,
2516 struct lxc_conf
*lxc_conf
, void *data
)
2518 unsigned int val
= 0;
2520 if (lxc_config_value_empty(value
)) {
2521 lxc_conf
->rootfs
.managed
= true;
2525 if (lxc_safe_uint(value
, &val
) < 0)
2530 lxc_conf
->rootfs
.managed
= false;
2533 lxc_conf
->rootfs
.managed
= true;
2540 static int set_config_rootfs_mount(const char *key
, const char *value
,
2541 struct lxc_conf
*lxc_conf
, void *data
)
2543 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2546 static int set_config_rootfs_options(const char *key
, const char *value
,
2547 struct lxc_conf
*lxc_conf
, void *data
)
2550 unsigned long mflags
= 0, pflags
= 0;
2551 char *mdata
= NULL
, *opts
= NULL
;
2552 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2554 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2558 ret
= parse_propagationopts(value
, &pflags
);
2564 ret
= set_config_string_item(&opts
, value
);
2570 rootfs
->mountflags
= mflags
| pflags
;
2571 rootfs
->options
= opts
;
2572 rootfs
->data
= mdata
;
2577 static int set_config_uts_name(const char *key
, const char *value
,
2578 struct lxc_conf
*lxc_conf
, void *data
)
2580 struct utsname
*utsname
;
2582 if (lxc_config_value_empty(value
)) {
2583 clr_config_uts_name(key
, lxc_conf
, NULL
);
2587 utsname
= malloc(sizeof(*utsname
));
2591 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2596 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2597 free(lxc_conf
->utsname
);
2598 lxc_conf
->utsname
= utsname
;
2603 static int set_config_namespace_clone(const char *key
, const char *value
,
2604 struct lxc_conf
*lxc_conf
, void *data
)
2609 if (lxc_config_value_empty(value
))
2610 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2612 if (lxc_conf
->ns_keep
!= 0) {
2614 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2615 "\"lxc.namespace.keep\"");
2623 lxc_iterate_parts(token
, ns
, " \t") {
2624 token
+= lxc_char_left_gc(token
, strlen(token
));
2625 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2626 cloneflag
= lxc_namespace_2_cloneflag(token
);
2627 if (cloneflag
< 0) {
2631 lxc_conf
->ns_clone
|= cloneflag
;
2638 static int set_config_namespace_keep(const char *key
, const char *value
,
2639 struct lxc_conf
*lxc_conf
, void *data
)
2644 if (lxc_config_value_empty(value
))
2645 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2647 if (lxc_conf
->ns_clone
!= 0) {
2649 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2650 "\"lxc.namespace.keep\"");
2658 lxc_iterate_parts(token
, ns
, " \t") {
2659 token
+= lxc_char_left_gc(token
, strlen(token
));
2660 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2661 cloneflag
= lxc_namespace_2_cloneflag(token
);
2662 if (cloneflag
< 0) {
2666 lxc_conf
->ns_keep
|= cloneflag
;
2673 static int set_config_namespace_share(const char *key
, const char *value
,
2674 struct lxc_conf
*lxc_conf
, void *data
)
2677 const char *namespace;
2679 if (lxc_config_value_empty(value
))
2680 return clr_config_namespace_share(key
, lxc_conf
, data
);
2682 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2683 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2687 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2690 struct parse_line_conf
{
2691 struct lxc_conf
*conf
;
2695 static int parse_line(char *buffer
, void *data
)
2697 char *dot
, *key
, *line
, *linep
, *value
;
2699 struct lxc_config_t
*config
;
2702 struct parse_line_conf
*plc
= data
;
2704 /* If there are newlines in the config file we should keep them. */
2705 empty_line
= lxc_is_line_empty(dup
);
2709 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2710 * modified the original string on the stack by replacing '=' by '\0'
2713 linep
= line
= strdup(dup
);
2717 if (!plc
->from_include
) {
2718 ret
= append_unexp_config_line(line
, plc
->conf
);
2726 line
+= lxc_char_left_gc(line
, strlen(line
));
2728 /* ignore comments */
2732 /* martian option - don't add it to the config itself */
2733 if (strncmp(line
, "lxc.", 4))
2738 dot
= strchr(line
, '=');
2740 ERROR("Invalid configuration line: %s", line
);
2748 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2750 value
+= lxc_char_left_gc(value
, strlen(value
));
2751 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2753 if (*value
== '\'' || *value
== '\"') {
2756 len
= strlen(value
);
2757 if (len
> 1 && value
[len
- 1] == *value
) {
2758 value
[len
- 1] = '\0';
2763 config
= lxc_get_config(key
);
2765 ERROR("Unknown configuration key \"%s\"", key
);
2769 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2777 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2779 char *dot
, *key
, *line
, *linep
, *value
;
2782 struct new_config_item
*new = NULL
;
2784 linep
= line
= strdup(dup
);
2788 line
+= lxc_char_left_gc(line
, strlen(line
));
2790 /* martian option - don't add it to the config itself */
2791 if (strncmp(line
, "lxc.", 4))
2795 dot
= strchr(line
, '=');
2797 ERROR("Invalid configuration item: %s", line
);
2805 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2807 value
+= lxc_char_left_gc(value
, strlen(value
));
2808 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2810 if (*value
== '\'' || *value
== '\"') {
2813 len
= strlen(value
);
2814 if (len
> 1 && value
[len
- 1] == *value
) {
2815 value
[len
- 1] = '\0';
2821 new = malloc(sizeof(struct new_config_item
));
2825 new->key
= strdup(key
);
2826 new->val
= strdup(value
);
2827 if (!new->val
|| !new->key
)
2835 if (ret
< 0 && new) {
2845 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2847 struct parse_line_conf c
;
2850 c
.from_include
= from_include
;
2852 /* Catch only the top level config file name in the structure. */
2854 conf
->rcfile
= strdup(file
);
2856 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2859 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2861 struct lxc_list
*dent
;
2863 dent
= malloc(sizeof(struct lxc_list
));
2867 dent
->elem
= parse_new_conf_line(arg
);
2873 lxc_list_add_tail(defines
, dent
);
2878 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2880 struct lxc_list
*it
;
2883 lxc_list_for_each(it
, defines
) {
2884 struct new_config_item
*new_item
= it
->elem
;
2885 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2890 lxc_config_define_free(defines
);
2895 void lxc_config_define_free(struct lxc_list
*defines
)
2897 struct lxc_list
*it
, *next
;
2899 lxc_list_for_each_safe(it
, defines
, next
) {
2900 struct new_config_item
*new_item
= it
->elem
;
2901 free(new_item
->key
);
2902 free(new_item
->val
);
2908 signed long lxc_config_parse_arch(const char *arch
)
2910 #if HAVE_SYS_PERSONALITY_H
2916 { "arm", PER_LINUX32
},
2917 { "armel", PER_LINUX32
},
2918 { "armhf", PER_LINUX32
},
2919 { "armv7l", PER_LINUX32
},
2920 { "athlon", PER_LINUX32
},
2921 { "i386", PER_LINUX32
},
2922 { "i486", PER_LINUX32
},
2923 { "i586", PER_LINUX32
},
2924 { "i686", PER_LINUX32
},
2925 { "linux32", PER_LINUX32
},
2926 { "mips", PER_LINUX32
},
2927 { "mipsel", PER_LINUX32
},
2928 { "ppc", PER_LINUX32
},
2929 { "powerpc", PER_LINUX32
},
2930 { "x86", PER_LINUX32
},
2931 { "amd64", PER_LINUX
},
2932 { "arm64", PER_LINUX
},
2933 { "linux64", PER_LINUX
},
2934 { "mips64", PER_LINUX
},
2935 { "mips64el", PER_LINUX
},
2936 { "ppc64", PER_LINUX
},
2937 { "ppc64el", PER_LINUX
},
2938 { "ppc64le", PER_LINUX
},
2939 { "powerpc64", PER_LINUX
},
2940 { "s390x", PER_LINUX
},
2941 { "x86_64", PER_LINUX
},
2943 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2945 for (i
= 0; i
< len
; i
++)
2946 if (!strcmp(pername
[i
].name
, arch
))
2947 return pername
[i
].per
;
2953 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2961 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2962 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2963 { "LSM", LXC_ATTACH_LSM_EXEC
},
2968 /* For the sake of backward compatibility, drop all privileges
2969 * if none is specified.
2971 for (i
= 0; all_privs
[i
].token
; i
++)
2972 *flags
|= all_privs
[i
].flag
;
2977 lxc_iterate_parts(token
, flaglist
, "|") {
2980 for (i
= 0; all_privs
[i
].token
; i
++)
2981 if (!strcmp(all_privs
[i
].token
, token
))
2982 aflag
= all_privs
[i
].flag
;
2993 /* Write out a configuration file. */
2994 int write_config(int fd
, const struct lxc_conf
*conf
)
2997 size_t len
= conf
->unexpanded_len
;
3002 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3004 SYSERROR("Failed to write configuration file");
3011 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3014 __do_free
char *tmp
= NULL
;
3018 len
= strlen(key
) + strlen(v
) + 4;
3019 tmp
= must_realloc(NULL
, len
);
3021 if (lxc_config_value_empty(v
))
3022 ret
= snprintf(tmp
, len
, "%s =", key
);
3024 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
3025 if (ret
< 0 || ret
>= len
)
3028 /* Save the line verbatim into unexpanded_conf */
3029 if (append_unexp_config_line(tmp
, conf
))
3035 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3039 char *lstart
= conf
->unexpanded_config
;
3041 if (!conf
->unexpanded_config
)
3045 lend
= strchr(lstart
, '\n');
3049 lend
= lstart
+ strlen(lstart
);
3053 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
3059 v
= lstart
[strlen(key
)];
3060 if (!isspace(v
) && v
!= '=') {
3066 conf
->unexpanded_len
-= (lend
- lstart
);
3068 if (*lend
== '\0') {
3073 memmove(lstart
, lend
, strlen(lend
) + 1);
3077 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3078 const char *newpath
, const char *oldname
,
3079 const char *newname
, const char *ovldir
)
3081 __do_free
char *newdir
= NULL
,
3085 size_t newdirlen
, olddirlen
;
3086 char *lstart
= conf
->unexpanded_config
;
3087 const char *key
= "lxc.mount.entry";
3089 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3090 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3091 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
3093 if (ret
< 0 || ret
>= olddirlen
+ 1)
3096 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3097 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3098 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
3100 if (ret
< 0 || ret
>= newdirlen
+ 1)
3103 if (!conf
->unexpanded_config
)
3107 lend
= strchr(lstart
, '\n');
3109 lend
= lstart
+ strlen(lstart
);
3113 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3116 p
= strchr(lstart
+ strlen(key
), '=');
3127 /* Whenever a lxc.mount.entry entry is found in a line we check
3128 * if the substring "overlay" is present before doing any
3129 * further work. We check for "overlay" because substrings need
3130 * to have at least one space before them in a valid overlay
3131 * lxc.mount.entry (/A B overlay). When the space before is
3132 * missing it is very likely that these substrings are part of a
3133 * path or something else. (Checking q >= lend ensures that we
3134 * only count matches in the current line.) */
3135 q
= strstr(p
, " overlay");
3136 if (!q
|| q
>= lend
)
3139 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3142 /* replace the olddir with newdir */
3143 if (olddirlen
>= newdirlen
) {
3144 size_t diff
= olddirlen
- newdirlen
;
3145 memcpy(q
, newdir
, newdirlen
);
3147 if (olddirlen
!= newdirlen
) {
3148 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3149 strlen(q
) - newdirlen
- diff
+ 1);
3151 conf
->unexpanded_len
-= diff
;
3155 size_t diff
= newdirlen
- olddirlen
;
3156 size_t oldlen
= conf
->unexpanded_len
;
3157 size_t newlen
= oldlen
+ diff
;
3158 size_t poffset
= q
- conf
->unexpanded_config
;
3160 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3164 conf
->unexpanded_len
= newlen
;
3165 conf
->unexpanded_alloced
= newlen
+ 1;
3166 new[newlen
- 1] = '\0';
3167 lend
= new + (lend
- conf
->unexpanded_config
);
3169 /* Move over the remainder to make room for the newdir.
3171 memmove(new + poffset
+ newdirlen
,
3172 new + poffset
+ olddirlen
,
3173 oldlen
- poffset
- olddirlen
+ 1);
3174 conf
->unexpanded_config
= new;
3176 memcpy(new + poffset
, newdir
, newdirlen
);
3187 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3188 const char *newpath
, const char *oldname
,
3189 const char *newname
)
3191 __do_free
char *newdir
= NULL
,
3195 char *lstart
= conf
->unexpanded_config
;
3196 size_t newdirlen
, olddirlen
;
3197 const char *key
= "lxc.hook";
3199 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3200 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3201 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3202 if (ret
< 0 || ret
>= olddirlen
+ 1)
3205 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3206 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3207 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3208 if (ret
< 0 || ret
>= newdirlen
+ 1)
3211 if (!conf
->unexpanded_config
)
3215 lend
= strchr(lstart
, '\n');
3217 lend
= lstart
+ strlen(lstart
);
3221 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3224 p
= strchr(lstart
+ strlen(key
), '=');
3235 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
3238 /* replace the olddir with newdir */
3239 if (olddirlen
>= newdirlen
) {
3240 size_t diff
= olddirlen
- newdirlen
;
3241 memcpy(p
, newdir
, newdirlen
);
3243 if (olddirlen
!= newdirlen
) {
3244 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3245 strlen(p
) - newdirlen
- diff
+ 1);
3247 conf
->unexpanded_len
-= diff
;
3251 size_t diff
= newdirlen
- olddirlen
;
3252 size_t oldlen
= conf
->unexpanded_len
;
3253 size_t newlen
= oldlen
+ diff
;
3254 size_t poffset
= p
- conf
->unexpanded_config
;
3256 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3260 conf
->unexpanded_len
= newlen
;
3261 conf
->unexpanded_alloced
= newlen
+ 1;
3262 new[newlen
- 1] = '\0';
3263 lend
= new + (lend
- conf
->unexpanded_config
);
3265 /* Move over the remainder to make room for the newdir.
3267 memmove(new + poffset
+ newdirlen
,
3268 new + poffset
+ olddirlen
,
3269 oldlen
- poffset
- olddirlen
+ 1);
3270 conf
->unexpanded_config
= new;
3272 memcpy(new + poffset
, newdir
, newdirlen
);
3286 ERROR("Error writing to new config"); \
3291 /* This is called only from clone. We wish to update all hwaddrs in the
3292 * unexpanded config file. We can't/don't want to update any which come from
3293 * lxc.includes (there shouldn't be any).
3294 * We can't just walk the c->lxc-conf->network list because that includes netifs
3295 * from the include files. So we update the ones which we find in the unexp
3296 * config file, then find the original macaddr in the conf->network, and update
3297 * that to the same value.
3299 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3301 char *lend
, *p
, *p2
;
3302 struct lxc_list
*it
;
3303 char *lstart
= conf
->unexpanded_config
;
3305 if (!conf
->unexpanded_config
)
3309 char newhwaddr
[18], oldhwaddr
[17];
3311 lend
= strchr(lstart
, '\n');
3313 lend
= lstart
+ strlen(lstart
);
3317 if (!lxc_config_net_is_hwaddr(lstart
)) {
3322 p
= strchr(lstart
, '=');
3335 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3338 if ((p2
- p
) != 17) {
3339 WARN("Bad hwaddr entry");
3344 memcpy(oldhwaddr
, p
, 17);
3346 if (!new_hwaddr(newhwaddr
))
3349 memcpy(p
, newhwaddr
, 17);
3350 lxc_list_for_each(it
, &conf
->network
) {
3351 struct lxc_netdev
*n
= it
->elem
;
3353 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3354 memcpy(n
->hwaddr
, newhwaddr
, 17);
3363 static int set_config_ephemeral(const char *key
, const char *value
,
3364 struct lxc_conf
*lxc_conf
, void *data
)
3366 if (lxc_config_value_empty(value
)) {
3367 lxc_conf
->ephemeral
= 0;
3371 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3374 if (lxc_conf
->ephemeral
> 1)
3380 static int set_config_log_syslog(const char *key
, const char *value
,
3381 struct lxc_conf
*lxc_conf
, void *data
)
3385 if (lxc_conf
->syslog
) {
3386 free(lxc_conf
->syslog
);
3387 lxc_conf
->syslog
= NULL
;
3390 if (lxc_config_value_empty(value
))
3393 facility
= lxc_syslog_priority_to_int(value
);
3394 if (facility
== -EINVAL
)
3397 lxc_log_syslog(facility
);
3399 return set_config_string_item(&lxc_conf
->syslog
, value
);
3402 static int set_config_no_new_privs(const char *key
, const char *value
,
3403 struct lxc_conf
*lxc_conf
, void *data
)
3407 if (lxc_config_value_empty(value
)) {
3408 lxc_conf
->no_new_privs
= false;
3412 if (lxc_safe_uint(value
, &v
) < 0)
3418 lxc_conf
->no_new_privs
= v
? true : false;
3423 /* Callbacks to get configuration items. */
3424 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3425 struct lxc_conf
*c
, void *data
)
3432 memset(retv
, 0, inlen
);
3434 #if HAVE_SYS_PERSONALITY_H
3437 switch (c
->personality
) {
3439 strprint(retv
, inlen
, "i686");
3442 strprint(retv
, inlen
, "x86_64");
3452 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3453 struct lxc_conf
*c
, void *data
)
3455 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3458 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3459 struct lxc_conf
*c
, void *data
)
3461 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3464 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3465 struct lxc_conf
*c
, void *data
)
3467 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3470 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3471 struct lxc_conf
*c
, void *data
)
3473 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3476 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3477 int inlen
, struct lxc_conf
*c
,
3480 return lxc_get_conf_int(c
, retv
, inlen
,
3481 c
->lsm_aa_allow_incomplete
);
3484 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3485 int inlen
, struct lxc_conf
*c
,
3488 return lxc_get_conf_int(c
, retv
, inlen
,
3489 c
->lsm_aa_allow_nesting
);
3492 static int get_config_apparmor_raw(const char *key
, char *retv
,
3493 int inlen
, struct lxc_conf
*c
,
3497 struct lxc_list
*it
;
3503 memset(retv
, 0, inlen
);
3505 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3506 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3512 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3513 struct lxc_conf
*c
, void *data
)
3515 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3518 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3519 * just the value(s) will be printed. Since there still could be more than one,
3520 * it is newline-separated.
3521 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3523 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3524 * 'lxc.cgroup.subsystem.key = value' format.
3526 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3527 int inlen
, struct lxc_conf
*c
,
3531 size_t namespaced_token_len
;
3532 char *global_token
, *namespaced_token
;
3533 struct lxc_list
*it
;
3535 bool get_all
= false;
3540 memset(retv
, 0, inlen
);
3542 if (version
== CGROUP2_SUPER_MAGIC
) {
3543 global_token
= "lxc.cgroup2";
3544 namespaced_token
= "lxc.cgroup2.";
3545 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3546 } else if (version
== CGROUP_SUPER_MAGIC
) {
3547 global_token
= "lxc.cgroup";
3548 namespaced_token
= "lxc.cgroup.";
3549 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3554 if (strcmp(key
, global_token
) == 0)
3556 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3557 key
+= namespaced_token_len
;
3561 lxc_list_for_each(it
, &c
->cgroup
) {
3562 struct lxc_cgroup
*cg
= it
->elem
;
3565 if (version
!= cg
->version
)
3568 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3569 cg
->subsystem
, cg
->value
);
3570 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3571 strprint(retv
, inlen
, "%s\n", cg
->value
);
3578 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3579 struct lxc_conf
*c
, void *data
)
3581 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3582 CGROUP_SUPER_MAGIC
);
3585 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3586 struct lxc_conf
*c
, void *data
)
3588 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3589 CGROUP2_SUPER_MAGIC
);
3592 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3593 struct lxc_conf
*lxc_conf
, void *data
)
3601 memset(retv
, 0, inlen
);
3603 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3608 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3609 int inlen
, struct lxc_conf
*lxc_conf
,
3612 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3613 lxc_conf
->cgroup_meta
.relative
);
3616 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3617 struct lxc_conf
*c
, void *data
)
3619 struct lxc_list
*it
;
3620 int len
, listlen
, ret
;
3622 /* "u 1000 1000000 65536"
3624 * let's render this as
3642 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3643 char buf
[__LXC_IDMAP_STR_BUF
];
3648 memset(retv
, 0, inlen
);
3650 listlen
= lxc_list_len(&c
->id_map
);
3651 lxc_list_for_each(it
, &c
->id_map
) {
3652 struct id_map
*map
= it
->elem
;
3653 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3654 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3655 map
->nsid
, map
->hostid
, map
->range
);
3656 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3659 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3665 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3666 struct lxc_conf
*c
, void *data
)
3669 v
= lxc_log_priority_to_string(c
->loglevel
);
3670 return lxc_get_conf_str(retv
, inlen
, v
);
3673 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3674 struct lxc_conf
*c
, void *data
)
3676 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3679 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3680 struct lxc_conf
*c
, void *data
)
3682 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3685 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3686 struct lxc_conf
*c
, void *data
)
3688 int len
, fulllen
= 0;
3689 const char *sep
= "";
3694 memset(retv
, 0, inlen
);
3696 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3699 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3700 case LXC_AUTO_PROC_MIXED
:
3701 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3704 case LXC_AUTO_PROC_RW
:
3705 strprint(retv
, inlen
, "%sproc:rw", sep
);
3712 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3713 case LXC_AUTO_SYS_RO
:
3714 strprint(retv
, inlen
, "%ssys:ro", sep
);
3717 case LXC_AUTO_SYS_RW
:
3718 strprint(retv
, inlen
, "%ssys:rw", sep
);
3721 case LXC_AUTO_SYS_MIXED
:
3722 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3729 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3730 case LXC_AUTO_CGROUP_NOSPEC
:
3731 strprint(retv
, inlen
, "%scgroup", sep
);
3733 case LXC_AUTO_CGROUP_MIXED
:
3734 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3736 case LXC_AUTO_CGROUP_RO
:
3737 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3739 case LXC_AUTO_CGROUP_RW
:
3740 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3742 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3743 strprint(retv
, inlen
, "%scgroup-full", sep
);
3745 case LXC_AUTO_CGROUP_FULL_MIXED
:
3746 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3748 case LXC_AUTO_CGROUP_FULL_RO
:
3749 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3751 case LXC_AUTO_CGROUP_FULL_RW
:
3752 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3761 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3762 struct lxc_conf
*c
, void *data
)
3764 int len
, fulllen
= 0;
3765 struct lxc_list
*it
;
3770 memset(retv
, 0, inlen
);
3772 lxc_list_for_each(it
, &c
->mount_list
) {
3773 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3779 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3780 struct lxc_conf
*c
, void *data
)
3782 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3785 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3786 struct lxc_conf
*c
, void *data
)
3788 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3791 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3792 struct lxc_conf
*c
, void *data
)
3794 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3797 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3798 struct lxc_conf
*c
, void *data
)
3800 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3803 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3804 struct lxc_conf
*c
, void *data
)
3806 return lxc_get_conf_str(
3808 c
->utsname
? c
->utsname
->nodename
: NULL
);
3811 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3812 struct lxc_conf
*c
, void *data
)
3815 int len
, fulllen
= 0, found
= -1;
3816 struct lxc_list
*it
;
3819 subkey
= strchr(key
, '.');
3823 subkey
= strchr(subkey
+ 1, '.');
3827 if (*subkey
== '\0')
3830 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3831 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3843 memset(retv
, 0, inlen
);
3845 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3846 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3852 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3853 struct lxc_conf
*c
, void *data
)
3855 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3858 static int get_config_net(const char *key
, char *retv
, int inlen
,
3859 struct lxc_conf
*c
, void *data
)
3861 int len
, fulllen
= 0;
3862 struct lxc_list
*it
;
3867 memset(retv
, 0, inlen
);
3869 lxc_list_for_each(it
, &c
->network
) {
3870 struct lxc_netdev
*n
= it
->elem
;
3871 const char *t
= lxc_net_type_to_str(n
->type
);
3872 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3878 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3879 struct lxc_conf
*c
, void *data
)
3881 int len
, fulllen
= 0;
3882 struct lxc_list
*it
;
3887 memset(retv
, 0, inlen
);
3889 lxc_list_for_each(it
, &c
->caps
) {
3890 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3896 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3897 struct lxc_conf
*c
, void *data
)
3899 int len
, fulllen
= 0;
3900 struct lxc_list
*it
;
3905 memset(retv
, 0, inlen
);
3907 lxc_list_for_each(it
, &c
->keepcaps
) {
3908 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3914 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3915 struct lxc_conf
*c
, void *data
)
3917 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3920 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3921 struct lxc_conf
*c
, void *data
)
3923 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3926 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3927 struct lxc_conf
*c
, void *data
)
3929 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3933 static int get_config_console_buffer_size(const char *key
, char *retv
,
3934 int inlen
, struct lxc_conf
*c
,
3937 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3940 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3941 struct lxc_conf
*c
, void *data
)
3943 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3946 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
3947 int inlen
, struct lxc_conf
*c
,
3951 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
3958 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
3959 struct lxc_conf
*c
, void *data
)
3961 #ifdef HAVE_SECCOMP_NOTIFY
3962 return lxc_get_conf_str(retv
, inlen
,
3963 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
3964 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
3965 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
3967 return minus_one_set_errno(ENOSYS
);
3971 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3972 struct lxc_conf
*c
, void *data
)
3974 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
3977 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3978 struct lxc_conf
*c
, void *data
)
3980 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3983 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3984 struct lxc_conf
*c
, void *data
)
3986 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3989 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3990 struct lxc_conf
*c
, void *data
)
3992 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3995 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3996 struct lxc_conf
*c
, void *data
)
3998 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4001 static int get_config_start(const char *key
, char *retv
, int inlen
,
4002 struct lxc_conf
*c
, void *data
)
4004 if (strcmp(key
+ 10, "auto") == 0)
4005 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4006 else if (strcmp(key
+ 10, "delay") == 0)
4007 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4008 else if (strcmp(key
+ 10, "order") == 0)
4009 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4014 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4015 struct lxc_conf
*c
, void *data
)
4017 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4020 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4021 struct lxc_conf
*c
, void *data
)
4023 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4026 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4027 int inlen
, struct lxc_conf
*c
,
4030 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4033 static int get_config_group(const char *key
, char *retv
, int inlen
,
4034 struct lxc_conf
*c
, void *data
)
4036 int len
, fulllen
= 0;
4037 struct lxc_list
*it
;
4042 memset(retv
, 0, inlen
);
4044 lxc_list_for_each(it
, &c
->groups
) {
4045 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4051 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4052 struct lxc_conf
*c
, void *data
)
4054 int len
, fulllen
= 0;
4055 struct lxc_list
*it
;
4060 memset(retv
, 0, inlen
);
4062 lxc_list_for_each(it
, &c
->environment
) {
4063 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4069 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4070 struct lxc_conf
*c
, void *data
)
4072 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4075 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4076 struct lxc_conf
*c
, void *data
)
4078 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4081 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4082 struct lxc_conf
*c
, void *data
)
4084 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4087 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4088 struct lxc_conf
*c
, void *data
)
4090 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4093 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4094 struct lxc_conf
*c
, void *data
)
4096 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4099 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4100 struct lxc_conf
*c
, void *data
)
4102 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4105 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4106 struct lxc_conf
*c
, void *data
)
4108 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4111 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4112 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4113 * printed, in 'lxc.prlimit.resource = value' format.
4115 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4116 struct lxc_conf
*c
, void *data
)
4118 int fulllen
= 0, len
;
4119 bool get_all
= false;
4120 struct lxc_list
*it
;
4125 memset(retv
, 0, inlen
);
4127 if (!strcmp(key
, "lxc.prlimit"))
4129 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
4134 lxc_list_for_each(it
, &c
->limits
) {
4135 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4136 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4138 struct lxc_limit
*lim
= it
->elem
;
4140 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4141 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4142 partlen
= STRLITERALLEN("unlimited");
4144 partlen
= sprintf(buf
, "%" PRIu64
,
4145 (uint64_t)lim
->limit
.rlim_cur
);
4148 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4149 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4150 memcpy(buf
+ partlen
, ":unlimited",
4151 STRLITERALLEN(":unlimited") + 1);
4153 sprintf(buf
+ partlen
, ":%" PRIu64
,
4154 (uint64_t)lim
->limit
.rlim_max
);
4158 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4159 lim
->resource
, buf
);
4160 } else if (strcmp(lim
->resource
, key
) == 0) {
4161 strprint(retv
, inlen
, "%s", buf
);
4168 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4169 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4170 * entries will be printed, in 'lxc.sysctl.key = value' format.
4172 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4173 struct lxc_conf
*c
, void *data
)
4176 struct lxc_list
*it
;
4178 bool get_all
= false;
4183 memset(retv
, 0, inlen
);
4185 if (strcmp(key
, "lxc.sysctl") == 0)
4187 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4188 key
+= STRLITERALLEN("lxc.sysctl.");
4192 lxc_list_for_each(it
, &c
->sysctls
) {
4193 struct lxc_sysctl
*elem
= it
->elem
;
4195 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
4197 } else if (strcmp(elem
->key
, key
) == 0) {
4198 strprint(retv
, inlen
, "%s", elem
->value
);
4205 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4206 struct lxc_conf
*c
, void *data
)
4208 struct lxc_list
*it
;
4211 bool get_all
= false;
4216 memset(retv
, 0, inlen
);
4218 if (strcmp(key
, "lxc.proc") == 0)
4220 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4221 key
+= STRLITERALLEN("lxc.proc.");
4225 lxc_list_for_each(it
, &c
->procs
) {
4226 struct lxc_proc
*proc
= it
->elem
;
4229 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4230 proc
->filename
, proc
->value
);
4231 } else if (strcmp(proc
->filename
, key
) == 0) {
4232 strprint(retv
, inlen
, "%s", proc
->value
);
4239 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4240 struct lxc_conf
*c
, void *data
)
4248 memset(retv
, 0, inlen
);
4250 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4251 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4252 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4258 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4259 struct lxc_conf
*c
, void *data
)
4267 memset(retv
, 0, inlen
);
4269 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4270 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4271 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4277 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4278 struct lxc_conf
*c
, void *data
)
4281 const char *namespace;
4287 memset(retv
, 0, inlen
);
4289 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4290 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4294 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4299 /* Callbacks to clear config items. */
4300 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4303 c
->personality
= -1;
4307 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4314 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4321 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4329 static inline int clr_config_apparmor_profile(const char *key
,
4330 struct lxc_conf
*c
, void *data
)
4332 free(c
->lsm_aa_profile
);
4333 c
->lsm_aa_profile
= NULL
;
4337 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4341 c
->lsm_aa_allow_incomplete
= 0;
4345 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4349 c
->lsm_aa_allow_nesting
= 0;
4353 static inline int clr_config_apparmor_raw(const char *key
,
4357 return lxc_clear_apparmor_raw(c
);
4360 static inline int clr_config_selinux_context(const char *key
,
4361 struct lxc_conf
*c
, void *data
)
4363 free(c
->lsm_se_context
);
4364 c
->lsm_se_context
= NULL
;
4368 static inline int clr_config_cgroup_controller(const char *key
,
4369 struct lxc_conf
*c
, void *data
)
4371 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4374 static inline int clr_config_cgroup2_controller(const char *key
,
4375 struct lxc_conf
*c
, void *data
)
4377 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4380 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4383 if (lxc_conf
->cgroup_meta
.dir
) {
4384 free(lxc_conf
->cgroup_meta
.dir
);
4385 lxc_conf
->cgroup_meta
.dir
= NULL
;
4391 static inline int clr_config_cgroup_relative(const char *key
,
4392 struct lxc_conf
*lxc_conf
,
4395 lxc_conf
->cgroup_meta
.relative
= false;
4399 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4402 return lxc_clear_idmaps(c
);
4405 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4408 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4412 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4420 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4423 return lxc_clear_mount_entries(c
);
4426 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4429 return lxc_clear_automounts(c
);
4432 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4440 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4443 free(c
->rootfs
.path
);
4444 c
->rootfs
.path
= NULL
;
4448 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4451 c
->rootfs
.managed
= true;
4455 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4458 free(c
->rootfs
.mount
);
4459 c
->rootfs
.mount
= NULL
;
4463 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4466 free(c
->rootfs
.options
);
4467 c
->rootfs
.options
= NULL
;
4469 free(c
->rootfs
.data
);
4470 c
->rootfs
.data
= NULL
;
4475 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4483 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4486 return lxc_clear_hooks(c
, key
);
4489 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4492 /* default to legacy hooks version */
4493 c
->hooks_version
= 0;
4497 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4500 lxc_free_networks(&c
->network
);
4505 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4508 return lxc_clear_config_caps(c
);
4511 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4514 return lxc_clear_config_keepcaps(c
);
4517 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4520 free(c
->console
.path
);
4521 c
->console
.path
= NULL
;
4525 static inline int clr_config_console_logfile(const char *key
,
4526 struct lxc_conf
*c
, void *data
)
4528 free(c
->console
.log_path
);
4529 c
->console
.log_path
= NULL
;
4533 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4536 c
->console
.log_rotate
= 0;
4540 static inline int clr_config_console_buffer_size(const char *key
,
4541 struct lxc_conf
*c
, void *data
)
4543 c
->console
.buffer_size
= 0;
4547 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4550 c
->console
.log_size
= 0;
4554 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4555 struct lxc_conf
*c
, void *data
)
4558 c
->seccomp
.allow_nesting
= 0;
4566 static inline int clr_config_seccomp_notify_proxy(const char *key
,
4567 struct lxc_conf
*c
, void *data
)
4569 #ifdef HAVE_SECCOMP_NOTIFY
4570 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
4571 sizeof(c
->seccomp
.notifier
.proxy_addr
));
4574 return minus_one_set_errno(ENOSYS
);
4578 static inline int clr_config_seccomp_profile(const char *key
,
4579 struct lxc_conf
*c
, void *data
)
4581 free(c
->seccomp
.seccomp
);
4582 c
->seccomp
.seccomp
= NULL
;
4586 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4593 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4600 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4603 c
->rebootsignal
= 0;
4607 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4614 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4617 if (strcmp(key
+ 10, "auto") == 0)
4619 else if (strcmp(key
+ 10, "delay") == 0)
4621 else if (strcmp(key
+ 10, "order") == 0)
4627 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4635 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4638 c
->monitor_unshare
= 0;
4642 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4643 struct lxc_conf
*c
, void *data
)
4645 c
->monitor_signal_pdeath
= 0;
4649 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4652 return lxc_clear_groups(c
);
4655 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4658 return lxc_clear_environment(c
);
4661 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4664 free(c
->execute_cmd
);
4665 c
->execute_cmd
= NULL
;
4669 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4677 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4685 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4692 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4699 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4706 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4709 c
->no_new_privs
= false;
4713 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4716 return lxc_clear_limits(c
, key
);
4719 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4722 return lxc_clear_sysctls(c
, key
);
4725 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4728 return lxc_clear_procs(c
, key
);
4731 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4734 lxc_clear_includes(c
);
4738 static int clr_config_namespace_clone(const char *key
,
4739 struct lxc_conf
*lxc_conf
, void *data
)
4741 lxc_conf
->ns_clone
= 0;
4745 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4748 lxc_conf
->ns_keep
= 0;
4752 static int clr_config_namespace_share(const char *key
,
4753 struct lxc_conf
*lxc_conf
, void *data
)
4756 const char *namespace;
4758 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4759 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4763 free(lxc_conf
->ns_share
[ns_idx
]);
4764 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4769 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4770 struct lxc_conf
*c
, void *data
)
4775 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4776 struct lxc_conf
*lxc_conf
,
4778 char **deindexed_key
)
4781 unsigned int tmpidx
;
4783 char *copy
, *idx_start
, *idx_end
;
4784 struct lxc_config_t
*config
= NULL
;
4786 /* check that this is a sensible network key */
4787 if (strncmp("lxc.net.", key
, 8)) {
4788 ERROR("Invalid network configuration key \"%s\"", key
);
4794 ERROR("Failed to duplicate string \"%s\"", key
);
4799 if (!isdigit(*(key
+ 8))) {
4800 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4804 /* beginning of index string */
4805 idx_start
= (copy
+ 7);
4808 /* end of index string */
4809 idx_end
= strchr((copy
+ 8), '.');
4813 /* parse current index */
4814 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4817 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
4823 /* This, of course is utterly nonsensical on so many levels, but
4824 * better safe than sorry.
4825 * (Checking for INT_MAX here is intentional.)
4827 if (tmpidx
== INT_MAX
) {
4828 SYSERROR("Number of configured networks would overflow the "
4834 numstrlen
= strlen((idx_start
+ 1));
4836 /* repair configuration key */
4839 /* lxc.net.<idx>.<subkey> */
4842 if (strlen(idx_end
+ 1) == 0) {
4843 ERROR("No subkey in network configuration key \"%s\"", key
);
4847 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4848 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4850 config
= lxc_get_config(copy
);
4852 ERROR("Unknown network configuration key \"%s\"", key
);
4858 *deindexed_key
= copy
;
4867 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4868 * found. So we make sure next comes an integer, find the right callback (by
4869 * rewriting the key), and call it.
4871 static int set_config_net_nic(const char *key
, const char *value
,
4872 struct lxc_conf
*lxc_conf
, void *data
)
4875 const char *idxstring
;
4876 struct lxc_config_t
*config
;
4877 struct lxc_netdev
*netdev
;
4879 char *deindexed_key
= NULL
;
4881 idxstring
= key
+ 8;
4882 if (!isdigit(*idxstring
))
4885 if (lxc_config_value_empty(value
))
4886 return clr_config_net_nic(key
, lxc_conf
, data
);
4888 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4889 if (!config
|| idx
< 0)
4892 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4894 free(deindexed_key
);
4898 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4899 free(deindexed_key
);
4904 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4908 const char *idxstring
;
4909 struct lxc_config_t
*config
;
4910 struct lxc_netdev
*netdev
;
4912 char *deindexed_key
= NULL
;
4914 idxstring
= key
+ 8;
4915 if (!isdigit(*idxstring
))
4918 /* The left conjunct is pretty self-explanatory. The right conjunct
4919 * checks whether the two pointers are equal. If they are we know that
4920 * this is not a key that is namespaced any further and so we are
4921 * supposed to clear the whole network.
4923 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4924 unsigned int rmnetdevidx
;
4926 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4929 /* Remove network from network list. */
4930 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4934 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4935 if (!config
|| idx
< 0)
4938 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4940 free(deindexed_key
);
4944 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4945 free(deindexed_key
);
4950 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4953 struct lxc_netdev
*netdev
= data
;
4963 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4966 struct lxc_netdev
*netdev
= data
;
4971 netdev
->name
[0] = '\0';
4976 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4979 struct lxc_netdev
*netdev
= data
;
4989 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4992 struct lxc_netdev
*netdev
= data
;
4997 netdev
->link
[0] = '\0';
5002 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5005 struct lxc_netdev
*netdev
= data
;
5008 return minus_one_set_errno(EINVAL
);
5010 netdev
->l2proxy
= false;
5015 static int clr_config_net_macvlan_mode(const char *key
,
5016 struct lxc_conf
*lxc_conf
, void *data
)
5018 struct lxc_netdev
*netdev
= data
;
5023 if (netdev
->type
!= LXC_NET_MACVLAN
)
5026 netdev
->priv
.macvlan_attr
.mode
= -1;
5031 static int clr_config_net_ipvlan_mode(const char *key
,
5032 struct lxc_conf
*lxc_conf
, void *data
)
5034 struct lxc_netdev
*netdev
= data
;
5037 return minus_one_set_errno(EINVAL
);
5039 if (netdev
->type
!= LXC_NET_IPVLAN
)
5042 netdev
->priv
.ipvlan_attr
.mode
= -1;
5047 static int clr_config_net_ipvlan_isolation(const char *key
,
5048 struct lxc_conf
*lxc_conf
, void *data
)
5050 struct lxc_netdev
*netdev
= data
;
5053 return minus_one_set_errno(EINVAL
);
5055 if (netdev
->type
!= LXC_NET_IPVLAN
)
5058 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5063 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5066 struct lxc_netdev
*netdev
= data
;
5071 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5076 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5079 struct lxc_netdev
*netdev
= data
;
5084 free(netdev
->upscript
);
5085 netdev
->upscript
= NULL
;
5090 static int clr_config_net_script_down(const char *key
,
5091 struct lxc_conf
*lxc_conf
, void *data
)
5093 struct lxc_netdev
*netdev
= data
;
5098 free(netdev
->downscript
);
5099 netdev
->downscript
= NULL
;
5104 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5107 struct lxc_netdev
*netdev
= data
;
5112 free(netdev
->hwaddr
);
5113 netdev
->hwaddr
= NULL
;
5118 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5121 struct lxc_netdev
*netdev
= data
;
5132 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5135 struct lxc_netdev
*netdev
= data
;
5140 netdev
->priv
.vlan_attr
.vid
= 0;
5145 static int clr_config_net_ipv4_gateway(const char *key
,
5146 struct lxc_conf
*lxc_conf
, void *data
)
5148 struct lxc_netdev
*netdev
= data
;
5153 free(netdev
->ipv4_gateway
);
5154 netdev
->ipv4_gateway
= NULL
;
5159 static int clr_config_net_ipv4_address(const char *key
,
5160 struct lxc_conf
*lxc_conf
, void *data
)
5162 struct lxc_netdev
*netdev
= data
;
5163 struct lxc_list
*cur
, *next
;
5168 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
5177 static int clr_config_net_veth_ipv4_route(const char *key
,
5178 struct lxc_conf
*lxc_conf
, void *data
)
5180 struct lxc_netdev
*netdev
= data
;
5181 struct lxc_list
*cur
, *next
;
5186 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv4_routes
, next
) {
5195 static int clr_config_net_ipv6_gateway(const char *key
,
5196 struct lxc_conf
*lxc_conf
, void *data
)
5198 struct lxc_netdev
*netdev
= data
;
5203 free(netdev
->ipv6_gateway
);
5204 netdev
->ipv6_gateway
= NULL
;
5209 static int clr_config_net_ipv6_address(const char *key
,
5210 struct lxc_conf
*lxc_conf
, void *data
)
5212 struct lxc_netdev
*netdev
= data
;
5213 struct lxc_list
*cur
, *next
;
5218 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
5227 static int clr_config_net_veth_ipv6_route(const char *key
,
5228 struct lxc_conf
*lxc_conf
, void *data
)
5230 struct lxc_netdev
*netdev
= data
;
5231 struct lxc_list
*cur
, *next
;
5236 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv6_routes
, next
) {
5245 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
5246 struct lxc_conf
*c
, void *data
)
5249 const char *idxstring
;
5250 struct lxc_config_t
*config
;
5251 struct lxc_netdev
*netdev
;
5253 char *deindexed_key
= NULL
;
5255 idxstring
= key
+ 8;
5256 if (!isdigit(*idxstring
))
5259 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
5260 if (!config
|| idx
< 0)
5263 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5265 free(deindexed_key
);
5269 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
5270 free(deindexed_key
);
5275 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5276 struct lxc_conf
*c
, void *data
)
5280 struct lxc_netdev
*netdev
= data
;
5285 memset(retv
, 0, inlen
);
5290 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5295 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5296 struct lxc_conf
*c
, void *data
)
5300 struct lxc_netdev
*netdev
= data
;
5305 memset(retv
, 0, inlen
);
5310 if (netdev
->flags
& IFF_UP
)
5311 strprint(retv
, inlen
, "up");
5316 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5317 struct lxc_conf
*c
, void *data
)
5321 struct lxc_netdev
*netdev
= data
;
5326 memset(retv
, 0, inlen
);
5331 if (netdev
->link
[0] != '\0')
5332 strprint(retv
, inlen
, "%s", netdev
->link
);
5337 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5338 struct lxc_conf
*c
, void *data
)
5340 struct lxc_netdev
*netdev
= data
;
5341 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5344 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5345 struct lxc_conf
*c
, void *data
)
5349 struct lxc_netdev
*netdev
= data
;
5354 memset(retv
, 0, inlen
);
5359 if (netdev
->name
[0] != '\0')
5360 strprint(retv
, inlen
, "%s", netdev
->name
);
5365 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5366 struct lxc_conf
*c
, void *data
)
5371 struct lxc_netdev
*netdev
= data
;
5376 memset(retv
, 0, inlen
);
5381 if (netdev
->type
!= LXC_NET_MACVLAN
)
5384 switch (netdev
->priv
.macvlan_attr
.mode
) {
5385 case MACVLAN_MODE_PRIVATE
:
5388 case MACVLAN_MODE_VEPA
:
5391 case MACVLAN_MODE_BRIDGE
:
5394 case MACVLAN_MODE_PASSTHRU
:
5402 strprint(retv
, inlen
, "%s", mode
);
5407 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5408 struct lxc_conf
*c
, void *data
)
5413 struct lxc_netdev
*netdev
= data
;
5418 memset(retv
, 0, inlen
);
5421 return minus_one_set_errno(EINVAL
);
5423 if (netdev
->type
!= LXC_NET_IPVLAN
)
5426 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5427 case IPVLAN_MODE_L3
:
5430 case IPVLAN_MODE_L3S
:
5433 case IPVLAN_MODE_L2
:
5441 strprint(retv
, inlen
, "%s", mode
);
5446 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5447 struct lxc_conf
*c
, void *data
)
5452 struct lxc_netdev
*netdev
= data
;
5457 memset(retv
, 0, inlen
);
5460 return minus_one_set_errno(EINVAL
);
5462 if (netdev
->type
!= LXC_NET_IPVLAN
)
5465 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
5466 case IPVLAN_ISOLATION_BRIDGE
:
5469 case IPVLAN_ISOLATION_PRIVATE
:
5472 case IPVLAN_ISOLATION_VEPA
:
5480 strprint(retv
, inlen
, "%s", mode
);
5485 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
5486 struct lxc_conf
*c
, void *data
)
5490 struct lxc_netdev
*netdev
= data
;
5495 memset(retv
, 0, inlen
);
5500 if (netdev
->type
!= LXC_NET_VETH
)
5503 strprint(retv
, inlen
, "%s",
5504 netdev
->priv
.veth_attr
.pair
[0] != '\0'
5505 ? netdev
->priv
.veth_attr
.pair
5506 : netdev
->priv
.veth_attr
.veth1
);
5511 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
5512 struct lxc_conf
*c
, void *data
)
5516 struct lxc_netdev
*netdev
= data
;
5521 memset(retv
, 0, inlen
);
5526 if (netdev
->upscript
)
5527 strprint(retv
, inlen
, "%s", netdev
->upscript
);
5532 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
5533 struct lxc_conf
*c
, void *data
)
5537 struct lxc_netdev
*netdev
= data
;
5542 memset(retv
, 0, inlen
);
5547 if (netdev
->downscript
)
5548 strprint(retv
, inlen
, "%s", netdev
->downscript
);
5553 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
5554 struct lxc_conf
*c
, void *data
)
5558 struct lxc_netdev
*netdev
= data
;
5563 memset(retv
, 0, inlen
);
5569 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
5574 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
5575 struct lxc_conf
*c
, void *data
)
5579 struct lxc_netdev
*netdev
= data
;
5584 memset(retv
, 0, inlen
);
5590 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5595 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5596 struct lxc_conf
*c
, void *data
)
5600 struct lxc_netdev
*netdev
= data
;
5605 memset(retv
, 0, inlen
);
5610 if (netdev
->type
!= LXC_NET_VLAN
)
5613 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5618 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5619 struct lxc_conf
*c
, void *data
)
5622 char buf
[INET_ADDRSTRLEN
];
5624 struct lxc_netdev
*netdev
= data
;
5629 memset(retv
, 0, inlen
);
5634 if (netdev
->ipv4_gateway_auto
) {
5635 strprint(retv
, inlen
, "auto");
5636 } else if (netdev
->ipv4_gateway_dev
) {
5637 strprint(retv
, inlen
, "dev");
5638 } else if (netdev
->ipv4_gateway
) {
5639 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5640 strprint(retv
, inlen
, "%s", buf
);
5646 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5647 struct lxc_conf
*c
, void *data
)
5651 char buf
[INET_ADDRSTRLEN
];
5652 struct lxc_list
*it
;
5654 struct lxc_netdev
*netdev
= data
;
5659 memset(retv
, 0, inlen
);
5664 listlen
= lxc_list_len(&netdev
->ipv4
);
5666 lxc_list_for_each(it
, &netdev
->ipv4
) {
5667 struct lxc_inetdev
*i
= it
->elem
;
5668 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5669 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5670 (listlen
-- > 1) ? "\n" : "");
5676 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
5677 struct lxc_conf
*c
, void *data
)
5681 char buf
[INET_ADDRSTRLEN
];
5682 struct lxc_list
*it
;
5684 struct lxc_netdev
*netdev
= data
;
5689 memset(retv
, 0, inlen
);
5692 return minus_one_set_errno(EINVAL
);
5694 if (netdev
->type
!= LXC_NET_VETH
)
5697 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
5699 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv4_routes
) {
5700 struct lxc_inetdev
*i
= it
->elem
;
5701 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5702 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5703 (listlen
-- > 1) ? "\n" : "");
5709 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5710 struct lxc_conf
*c
, void *data
)
5713 char buf
[INET6_ADDRSTRLEN
];
5715 struct lxc_netdev
*netdev
= data
;
5720 memset(retv
, 0, inlen
);
5725 if (netdev
->ipv6_gateway_auto
) {
5726 strprint(retv
, inlen
, "auto");
5727 } else if (netdev
->ipv6_gateway_dev
) {
5728 strprint(retv
, inlen
, "dev");
5729 } else if (netdev
->ipv6_gateway
) {
5730 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5731 strprint(retv
, inlen
, "%s", buf
);
5737 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5738 struct lxc_conf
*c
, void *data
)
5742 char buf
[INET6_ADDRSTRLEN
];
5743 struct lxc_list
*it
;
5745 struct lxc_netdev
*netdev
= data
;
5750 memset(retv
, 0, inlen
);
5755 listlen
= lxc_list_len(&netdev
->ipv6
);
5757 lxc_list_for_each(it
, &netdev
->ipv6
) {
5758 struct lxc_inet6dev
*i
= it
->elem
;
5759 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5760 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5761 (listlen
-- > 1) ? "\n" : "");
5767 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
5768 struct lxc_conf
*c
, void *data
)
5772 char buf
[INET6_ADDRSTRLEN
];
5773 struct lxc_list
*it
;
5775 struct lxc_netdev
*netdev
= data
;
5780 memset(retv
, 0, inlen
);
5783 return minus_one_set_errno(EINVAL
);
5785 if (netdev
->type
!= LXC_NET_VETH
)
5788 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
5790 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv6_routes
) {
5791 struct lxc_inet6dev
*i
= it
->elem
;
5792 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5793 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5794 (listlen
-- > 1) ? "\n" : "");
5800 int lxc_list_config_items(char *retv
, int inlen
)
5809 memset(retv
, 0, inlen
);
5811 for (i
= 0; i
< config_jump_table_size
; i
++) {
5812 char *s
= config_jump_table
[i
].name
;
5814 if (s
[strlen(s
) - 1] == '.')
5817 strprint(retv
, inlen
, "%s\n", s
);
5823 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5832 memset(retv
, 0, inlen
);
5834 if (!strcmp(key
, "lxc.apparmor")) {
5835 strprint(retv
, inlen
, "allow_incomplete\n");
5836 strprint(retv
, inlen
, "allow_nesting\n");
5837 strprint(retv
, inlen
, "profile\n");
5838 strprint(retv
, inlen
, "raw\n");
5839 } else if (!strcmp(key
, "lxc.cgroup")) {
5840 strprint(retv
, inlen
, "dir\n");
5841 } else if (!strcmp(key
, "lxc.selinux")) {
5842 strprint(retv
, inlen
, "context\n");
5843 } else if (!strcmp(key
, "lxc.mount")) {
5844 strprint(retv
, inlen
, "auto\n");
5845 strprint(retv
, inlen
, "entry\n");
5846 strprint(retv
, inlen
, "fstab\n");
5847 } else if (!strcmp(key
, "lxc.rootfs")) {
5848 strprint(retv
, inlen
, "mount\n");
5849 strprint(retv
, inlen
, "options\n");
5850 strprint(retv
, inlen
, "path\n");
5851 } else if (!strcmp(key
, "lxc.uts")) {
5852 strprint(retv
, inlen
, "name\n");
5853 } else if (!strcmp(key
, "lxc.hook")) {
5854 strprint(retv
, inlen
, "autodev\n");
5855 strprint(retv
, inlen
, "clone\n");
5856 strprint(retv
, inlen
, "destroy\n");
5857 strprint(retv
, inlen
, "mount\n");
5858 strprint(retv
, inlen
, "post-stop\n");
5859 strprint(retv
, inlen
, "pre-mount\n");
5860 strprint(retv
, inlen
, "pre-start\n");
5861 strprint(retv
, inlen
, "start-host\n");
5862 strprint(retv
, inlen
, "start\n");
5863 strprint(retv
, inlen
, "stop\n");
5864 } else if (!strcmp(key
, "lxc.cap")) {
5865 strprint(retv
, inlen
, "drop\n");
5866 strprint(retv
, inlen
, "keep\n");
5867 } else if (!strcmp(key
, "lxc.console")) {
5868 strprint(retv
, inlen
, "logfile\n");
5869 strprint(retv
, inlen
, "path\n");
5870 } else if (!strcmp(key
, "lxc.seccomp")) {
5871 strprint(retv
, inlen
, "profile\n");
5872 } else if (!strcmp(key
, "lxc.signal")) {
5873 strprint(retv
, inlen
, "halt\n");
5874 strprint(retv
, inlen
, "reboot\n");
5875 strprint(retv
, inlen
, "stop\n");
5876 } else if (!strcmp(key
, "lxc.start")) {
5877 strprint(retv
, inlen
, "auto\n");
5878 strprint(retv
, inlen
, "delay\n");
5879 strprint(retv
, inlen
, "order\n");
5880 } else if (!strcmp(key
, "lxc.monitor")) {
5881 strprint(retv
, inlen
, "unshare\n");
5889 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5892 const char *idxstring
;
5893 struct lxc_netdev
*netdev
;
5897 idxstring
= key
+ 8;
5898 if (!isdigit(*idxstring
))
5901 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5905 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5912 memset(retv
, 0, inlen
);
5914 strprint(retv
, inlen
, "type\n");
5915 strprint(retv
, inlen
, "script.up\n");
5916 strprint(retv
, inlen
, "script.down\n");
5918 if (netdev
->type
!= LXC_NET_EMPTY
) {
5919 strprint(retv
, inlen
, "flags\n");
5920 strprint(retv
, inlen
, "link\n");
5921 strprint(retv
, inlen
, "name\n");
5922 strprint(retv
, inlen
, "hwaddr\n");
5923 strprint(retv
, inlen
, "mtu\n");
5924 strprint(retv
, inlen
, "ipv6.address\n");
5925 strprint(retv
, inlen
, "ipv6.gateway\n");
5926 strprint(retv
, inlen
, "ipv4.address\n");
5927 strprint(retv
, inlen
, "ipv4.gateway\n");
5930 switch (netdev
->type
) {
5932 strprint(retv
, inlen
, "veth.pair\n");
5933 strprint(retv
, inlen
, "veth.ipv4.route\n");
5934 strprint(retv
, inlen
, "veth.ipv6.route\n");
5936 case LXC_NET_MACVLAN
:
5937 strprint(retv
, inlen
, "macvlan.mode\n");
5939 case LXC_NET_IPVLAN
:
5940 strprint(retv
, inlen
, "ipvlan.mode\n");
5941 strprint(retv
, inlen
, "ipvlan.isolation\n");
5944 strprint(retv
, inlen
, "vlan.id\n");