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>
52 #include "confile_utils.h"
53 #include "../include/netns_ifaddrs.h"
55 #include "lxcseccomp.h"
61 #if HAVE_SYS_PERSONALITY_H
62 #include <sys/personality.h>
66 #include "include/strlcpy.h"
70 #include "include/strlcat.h"
73 lxc_log_define(confile
, lxc
);
75 #define lxc_config_define(name) \
76 __hot static int set_config_##name(const char *, const char *, \
77 struct lxc_conf *, void *); \
78 __hot static int get_config_##name(const char *, char *, int, \
79 struct lxc_conf *, void *); \
80 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
83 lxc_config_define(autodev
);
84 lxc_config_define(apparmor_allow_incomplete
);
85 lxc_config_define(apparmor_allow_nesting
);
86 lxc_config_define(apparmor_profile
);
87 lxc_config_define(apparmor_raw
);
88 lxc_config_define(cap_drop
);
89 lxc_config_define(cap_keep
);
90 lxc_config_define(cgroup_controller
);
91 lxc_config_define(cgroup2_controller
);
92 lxc_config_define(cgroup_dir
);
93 lxc_config_define(cgroup_relative
);
94 lxc_config_define(console_buffer_size
);
95 lxc_config_define(console_logfile
);
96 lxc_config_define(console_path
);
97 lxc_config_define(console_rotate
);
98 lxc_config_define(console_size
);
99 lxc_config_define(environment
);
100 lxc_config_define(ephemeral
);
101 lxc_config_define(execute_cmd
);
102 lxc_config_define(group
);
103 lxc_config_define(hooks
);
104 lxc_config_define(hooks_version
);
105 lxc_config_define(idmaps
);
106 lxc_config_define(includefiles
);
107 lxc_config_define(init_cmd
);
108 lxc_config_define(init_cwd
);
109 lxc_config_define(init_gid
);
110 lxc_config_define(init_uid
);
111 lxc_config_define(log_file
);
112 lxc_config_define(log_level
);
113 lxc_config_define(log_syslog
);
114 lxc_config_define(monitor
);
115 lxc_config_define(monitor_signal_pdeath
);
116 lxc_config_define(mount
);
117 lxc_config_define(mount_auto
);
118 lxc_config_define(mount_fstab
);
119 lxc_config_define(namespace_clone
);
120 lxc_config_define(namespace_keep
);
121 lxc_config_define(namespace_share
);
122 lxc_config_define(net
);
123 lxc_config_define(net_flags
);
124 lxc_config_define(net_hwaddr
);
125 lxc_config_define(net_ipv4_address
);
126 lxc_config_define(net_ipv4_gateway
);
127 lxc_config_define(net_ipv6_address
);
128 lxc_config_define(net_ipv6_gateway
);
129 lxc_config_define(net_link
);
130 lxc_config_define(net_macvlan_mode
);
131 lxc_config_define(net_mtu
);
132 lxc_config_define(net_name
);
133 lxc_config_define(net_nic
);
134 lxc_config_define(net_script_down
);
135 lxc_config_define(net_script_up
);
136 lxc_config_define(net_type
);
137 lxc_config_define(net_veth_pair
);
138 lxc_config_define(net_vlan_id
);
139 lxc_config_define(no_new_privs
);
140 lxc_config_define(personality
);
141 lxc_config_define(prlimit
);
142 lxc_config_define(pty_max
);
143 lxc_config_define(rootfs_managed
);
144 lxc_config_define(rootfs_mount
);
145 lxc_config_define(rootfs_options
);
146 lxc_config_define(rootfs_path
);
147 lxc_config_define(seccomp_profile
);
148 lxc_config_define(seccomp_allow_nesting
);
149 lxc_config_define(selinux_context
);
150 lxc_config_define(signal_halt
);
151 lxc_config_define(signal_reboot
);
152 lxc_config_define(signal_stop
);
153 lxc_config_define(start
);
154 lxc_config_define(tty_max
);
155 lxc_config_define(tty_dir
);
156 lxc_config_define(uts_name
);
157 lxc_config_define(sysctl
);
158 lxc_config_define(proc
);
160 static struct lxc_config_t config_jump_table
[] = {
161 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
162 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
163 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
164 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
165 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
166 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
167 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
168 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
169 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
170 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
171 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
172 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
173 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
174 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
175 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
176 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
177 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
178 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
179 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
180 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
181 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
182 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
183 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
184 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
185 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
188 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
190 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
192 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
193 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
194 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
195 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
196 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
197 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
198 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
199 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
200 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
201 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
202 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
203 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
204 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
205 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
206 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
207 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
208 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
209 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
210 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
211 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
212 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
213 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
214 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
215 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
216 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
217 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
218 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
219 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
220 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
221 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
222 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
223 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
224 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
225 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
226 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
227 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
228 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
229 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
230 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
231 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
232 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
233 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
234 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
235 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
236 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
237 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
238 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
239 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
240 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
241 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
242 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
243 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
244 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
245 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
246 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
247 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
248 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
251 static const size_t config_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
253 struct lxc_config_t
*lxc_get_config(const char *key
)
257 for (i
= 0; i
< config_jump_table_size
; i
++)
258 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
259 return &config_jump_table
[i
];
264 static int set_config_net(const char *key
, const char *value
,
265 struct lxc_conf
*lxc_conf
, void *data
)
267 if (!lxc_config_value_empty(value
)) {
268 ERROR("lxc.net must not have a value");
272 return clr_config_net(key
, lxc_conf
, data
);
275 static int set_config_net_type(const char *key
, const char *value
,
276 struct lxc_conf
*lxc_conf
, void *data
)
278 struct lxc_netdev
*netdev
= data
;
280 if (lxc_config_value_empty(value
))
281 return clr_config_net_type(key
, lxc_conf
, data
);
286 if (!strcmp(value
, "veth")) {
287 netdev
->type
= LXC_NET_VETH
;
288 } else if (!strcmp(value
, "macvlan")) {
289 netdev
->type
= LXC_NET_MACVLAN
;
290 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
292 } else if (!strcmp(value
, "vlan")) {
293 netdev
->type
= LXC_NET_VLAN
;
294 } else if (!strcmp(value
, "phys")) {
295 netdev
->type
= LXC_NET_PHYS
;
296 } else if (!strcmp(value
, "empty")) {
297 netdev
->type
= LXC_NET_EMPTY
;
298 } else if (!strcmp(value
, "none")) {
299 netdev
->type
= LXC_NET_NONE
;
301 ERROR("Invalid network type %s", value
);
308 static int set_config_net_flags(const char *key
, const char *value
,
309 struct lxc_conf
*lxc_conf
, void *data
)
311 struct lxc_netdev
*netdev
= data
;
313 if (lxc_config_value_empty(value
))
314 return clr_config_net_flags(key
, lxc_conf
, data
);
319 netdev
->flags
|= IFF_UP
;
324 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
325 struct lxc_netdev
*netdev
)
327 struct netns_ifaddrs
*ifaddr
, *ifa
;
330 const char *type_key
= "lxc.net.type";
331 const char *link_key
= "lxc.net.link";
332 const char *tmpvalue
= "phys";
334 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0) {
335 SYSERROR("Failed to get network interfaces");
339 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
343 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
346 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
347 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
350 ret
= set_config_net_link(
351 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
353 ERROR("Failed to create matched ifnames");
357 ERROR("Failed to create matched ifnames");
363 netns_freeifaddrs(ifaddr
);
369 static int set_config_net_link(const char *key
, const char *value
,
370 struct lxc_conf
*lxc_conf
, void *data
)
372 struct lxc_netdev
*netdev
= data
;
375 if (lxc_config_value_empty(value
))
376 return clr_config_net_link(key
, lxc_conf
, data
);
381 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
382 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
384 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
389 static int set_config_net_name(const char *key
, const char *value
,
390 struct lxc_conf
*lxc_conf
, void *data
)
392 struct lxc_netdev
*netdev
= data
;
394 if (lxc_config_value_empty(value
))
395 return clr_config_net_name(key
, lxc_conf
, data
);
400 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
403 static int set_config_net_veth_pair(const char *key
, const char *value
,
404 struct lxc_conf
*lxc_conf
, void *data
)
406 struct lxc_netdev
*netdev
= data
;
408 if (lxc_config_value_empty(value
))
409 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
414 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
417 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
418 struct lxc_conf
*lxc_conf
, void *data
)
420 struct lxc_netdev
*netdev
= data
;
422 if (lxc_config_value_empty(value
))
423 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
428 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
431 static int set_config_net_hwaddr(const char *key
, const char *value
,
432 struct lxc_conf
*lxc_conf
, void *data
)
434 struct lxc_netdev
*netdev
= data
;
437 if (lxc_config_value_empty(value
))
438 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
443 new_value
= strdup(value
);
447 rand_complete_hwaddr(new_value
);
449 if (lxc_config_value_empty(new_value
)) {
451 netdev
->hwaddr
= NULL
;
455 netdev
->hwaddr
= new_value
;
460 static int set_config_net_vlan_id(const char *key
, const char *value
,
461 struct lxc_conf
*lxc_conf
, void *data
)
464 struct lxc_netdev
*netdev
= data
;
466 if (lxc_config_value_empty(value
))
467 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
472 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
479 static int set_config_net_mtu(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_mtu(key
, lxc_conf
, data
);
490 return set_config_string_item(&netdev
->mtu
, value
);
493 static int set_config_net_ipv4_address(const char *key
, const char *value
,
494 struct lxc_conf
*lxc_conf
, void *data
)
497 struct lxc_netdev
*netdev
= data
;
498 struct lxc_inetdev
*inetdev
;
499 struct lxc_list
*list
;
500 char *cursor
, *slash
;
501 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
503 if (lxc_config_value_empty(value
))
504 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
509 inetdev
= malloc(sizeof(*inetdev
));
512 memset(inetdev
, 0, sizeof(*inetdev
));
514 list
= malloc(sizeof(*list
));
521 list
->elem
= inetdev
;
523 addr
= strdup(value
);
530 cursor
= strstr(addr
, " ");
536 slash
= strstr(addr
, "/");
542 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
543 if (!ret
|| ret
< 0) {
544 SYSERROR("Invalid ipv4 address \"%s\"", value
);
552 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
553 if (!ret
|| ret
< 0) {
554 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
563 /* No prefix specified, determine it from the network class. */
565 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
573 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
576 /* If no broadcast address, let compute one from the
577 * prefix and address.
580 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
581 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
584 lxc_list_add_tail(&netdev
->ipv4
, list
);
590 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
591 struct lxc_conf
*lxc_conf
, void *data
)
593 struct lxc_netdev
*netdev
= data
;
595 if (lxc_config_value_empty(value
))
596 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
601 free(netdev
->ipv4_gateway
);
603 if (!strcmp(value
, "auto")) {
604 netdev
->ipv4_gateway
= NULL
;
605 netdev
->ipv4_gateway_auto
= true;
610 gw
= malloc(sizeof(*gw
));
614 ret
= inet_pton(AF_INET
, value
, gw
);
615 if (!ret
|| ret
< 0) {
616 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
621 netdev
->ipv4_gateway
= gw
;
622 netdev
->ipv4_gateway_auto
= false;
628 static int set_config_net_ipv6_address(const char *key
, const char *value
,
629 struct lxc_conf
*lxc_conf
, void *data
)
632 struct lxc_netdev
*netdev
= data
;
633 struct lxc_inet6dev
*inet6dev
;
634 struct lxc_list
*list
;
635 char *slash
, *valdup
, *netmask
;
637 if (lxc_config_value_empty(value
))
638 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
643 inet6dev
= malloc(sizeof(*inet6dev
));
646 memset(inet6dev
, 0, sizeof(*inet6dev
));
648 list
= malloc(sizeof(*list
));
655 list
->elem
= inet6dev
;
657 valdup
= strdup(value
);
664 inet6dev
->prefix
= 64;
665 slash
= strstr(valdup
, "/");
670 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
679 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
680 if (!ret
|| ret
< 0) {
681 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
688 lxc_list_add_tail(&netdev
->ipv6
, list
);
694 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
695 struct lxc_conf
*lxc_conf
, void *data
)
697 struct lxc_netdev
*netdev
= data
;
699 if (lxc_config_value_empty(value
))
700 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
705 free(netdev
->ipv6_gateway
);
707 if (!strcmp(value
, "auto")) {
708 netdev
->ipv6_gateway
= NULL
;
709 netdev
->ipv6_gateway_auto
= true;
714 gw
= malloc(sizeof(*gw
));
718 ret
= inet_pton(AF_INET6
, value
, gw
);
719 if (!ret
|| ret
< 0) {
720 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
725 netdev
->ipv6_gateway
= gw
;
726 netdev
->ipv6_gateway_auto
= false;
732 static int set_config_net_script_up(const char *key
, const char *value
,
733 struct lxc_conf
*lxc_conf
, void *data
)
735 struct lxc_netdev
*netdev
= data
;
737 if (lxc_config_value_empty(value
))
738 return clr_config_net_script_up(key
, lxc_conf
, data
);
743 return set_config_string_item(&netdev
->upscript
, value
);
746 static int set_config_net_script_down(const char *key
, const char *value
,
747 struct lxc_conf
*lxc_conf
, void *data
)
749 struct lxc_netdev
*netdev
= data
;
751 if (lxc_config_value_empty(value
))
752 return clr_config_net_script_down(key
, lxc_conf
, data
);
757 return set_config_string_item(&netdev
->downscript
, value
);
760 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
762 struct lxc_list
*hooklist
;
764 hooklist
= malloc(sizeof(*hooklist
));
770 hooklist
->elem
= hook
;
771 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
776 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
777 struct lxc_conf
*lxc_conf
, void *data
)
779 if (lxc_config_value_empty(value
))
780 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
782 if (lxc_safe_uint(value
, &lxc_conf
->seccomp_allow_nesting
) < 0)
785 if (lxc_conf
->seccomp_allow_nesting
> 1)
791 static int set_config_seccomp_profile(const char *key
, const char *value
,
792 struct lxc_conf
*lxc_conf
, void *data
)
794 return set_config_path_item(&lxc_conf
->seccomp
, value
);
797 static int set_config_execute_cmd(const char *key
, const char *value
,
798 struct lxc_conf
*lxc_conf
, void *data
)
800 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
803 static int set_config_init_cmd(const char *key
, const char *value
,
804 struct lxc_conf
*lxc_conf
, void *data
)
806 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
809 static int set_config_init_cwd(const char *key
, const char *value
,
810 struct lxc_conf
*lxc_conf
, void *data
)
812 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
815 static int set_config_init_uid(const char *key
, const char *value
,
816 struct lxc_conf
*lxc_conf
, void *data
)
818 unsigned int init_uid
;
820 if (lxc_config_value_empty(value
)) {
821 lxc_conf
->init_uid
= 0;
825 if (lxc_safe_uint(value
, &init_uid
) < 0)
828 lxc_conf
->init_uid
= init_uid
;
833 static int set_config_init_gid(const char *key
, const char *value
,
834 struct lxc_conf
*lxc_conf
, void *data
)
836 unsigned int init_gid
;
838 if (lxc_config_value_empty(value
)) {
839 lxc_conf
->init_gid
= 0;
843 if (lxc_safe_uint(value
, &init_gid
) < 0)
846 lxc_conf
->init_gid
= init_gid
;
851 static int set_config_hooks(const char *key
, const char *value
,
852 struct lxc_conf
*lxc_conf
, void *data
)
856 if (lxc_config_value_empty(value
))
857 return lxc_clear_hooks(lxc_conf
, key
);
859 if (strcmp(key
+ 4, "hook") == 0) {
860 ERROR("lxc.hook must not have a value");
864 copy
= strdup(value
);
868 if (strcmp(key
+ 9, "pre-start") == 0)
869 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
870 else if (strcmp(key
+ 9, "start-host") == 0)
871 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
872 else if (strcmp(key
+ 9, "pre-mount") == 0)
873 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
874 else if (strcmp(key
+ 9, "autodev") == 0)
875 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
876 else if (strcmp(key
+ 9, "mount") == 0)
877 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
878 else if (strcmp(key
+ 9, "start") == 0)
879 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
880 else if (strcmp(key
+ 9, "stop") == 0)
881 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
882 else if (strcmp(key
+ 9, "post-stop") == 0)
883 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
884 else if (strcmp(key
+ 9, "clone") == 0)
885 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
886 else if (strcmp(key
+ 9, "destroy") == 0)
887 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
894 static int set_config_hooks_version(const char *key
, const char *value
,
895 struct lxc_conf
*lxc_conf
, void *data
)
900 if (lxc_config_value_empty(value
))
901 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
903 ret
= lxc_safe_uint(value
, &tmp
);
908 ERROR("Invalid hook version specified. Currently only 0 "
909 "(legacy) and 1 are supported");
913 lxc_conf
->hooks_version
= tmp
;
918 static int set_config_personality(const char *key
, const char *value
,
919 struct lxc_conf
*lxc_conf
, void *data
)
921 signed long personality
= lxc_config_parse_arch(value
);
923 if (personality
>= 0)
924 lxc_conf
->personality
= personality
;
926 WARN("Unsupported personality \"%s\"", value
);
931 static int set_config_pty_max(const char *key
, const char *value
,
932 struct lxc_conf
*lxc_conf
, void *data
)
935 unsigned int max
= 0;
937 if (lxc_config_value_empty(value
)) {
938 lxc_conf
->pty_max
= 0;
942 ret
= lxc_safe_uint(value
, &max
);
946 lxc_conf
->pty_max
= max
;
951 /* We only need to check whether the first byte of the key after the lxc.start.
952 * prefix matches our expectations since they fortunately all start with a
953 * different letter. If anything was wrong with the key we would have already
954 * noticed when the callback was called.
956 static int set_config_start(const char *key
, const char *value
,
957 struct lxc_conf
*lxc_conf
, void *data
)
961 is_empty
= lxc_config_value_empty(value
);
963 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
965 lxc_conf
->start_auto
= 0;
969 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
972 if (lxc_conf
->start_auto
> 1)
976 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
978 lxc_conf
->start_delay
= 0;
982 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
983 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
985 lxc_conf
->start_order
= 0;
989 return lxc_safe_int(value
, &lxc_conf
->start_order
);
995 static int set_config_monitor(const char *key
, const char *value
,
996 struct lxc_conf
*lxc_conf
, void *data
)
998 if (lxc_config_value_empty(value
)) {
999 lxc_conf
->monitor_unshare
= 0;
1003 if (strcmp(key
+ 12, "unshare") == 0)
1004 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1009 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1010 struct lxc_conf
*lxc_conf
, void *data
)
1012 if (lxc_config_value_empty(value
)) {
1013 lxc_conf
->monitor_signal_pdeath
= 0;
1017 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1020 sig_n
= sig_parse(value
);
1024 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1031 static int set_config_group(const char *key
, const char *value
,
1032 struct lxc_conf
*lxc_conf
, void *data
)
1034 char *groups
, *token
;
1035 struct lxc_list
*grouplist
;
1038 if (lxc_config_value_empty(value
))
1039 return lxc_clear_groups(lxc_conf
);
1041 groups
= strdup(value
);
1045 /* In case several groups are specified in a single line split these
1046 * groups in a single element for the list.
1048 lxc_iterate_parts(token
, groups
, " \t") {
1049 grouplist
= malloc(sizeof(*grouplist
));
1055 grouplist
->elem
= strdup(token
);
1056 if (!grouplist
->elem
) {
1062 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1070 static int set_config_environment(const char *key
, const char *value
,
1071 struct lxc_conf
*lxc_conf
, void *data
)
1073 struct lxc_list
*list_item
= NULL
;
1075 if (lxc_config_value_empty(value
))
1076 return lxc_clear_environment(lxc_conf
);
1078 list_item
= malloc(sizeof(*list_item
));
1082 if (!strchr(value
, '=')) {
1083 const char *env_val
;
1084 const char *env_key
= value
;
1085 const char *env_var
[3] = {0};
1087 env_val
= getenv(env_key
);
1091 env_var
[0] = env_key
;
1092 env_var
[1] = env_val
;
1093 list_item
->elem
= lxc_string_join("=", env_var
, false);
1095 list_item
->elem
= strdup(value
);
1098 if (!list_item
->elem
)
1101 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1111 static int set_config_tty_max(const char *key
, const char *value
,
1112 struct lxc_conf
*lxc_conf
, void *data
)
1115 unsigned int nbtty
= 0;
1117 if (lxc_config_value_empty(value
)) {
1118 lxc_conf
->ttys
.max
= 0;
1122 ret
= lxc_safe_uint(value
, &nbtty
);
1126 lxc_conf
->ttys
.max
= nbtty
;
1131 static int set_config_tty_dir(const char *key
, const char *value
,
1132 struct lxc_conf
*lxc_conf
, void *data
)
1134 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1138 static int set_config_apparmor_profile(const char *key
, const char *value
,
1139 struct lxc_conf
*lxc_conf
, void *data
)
1141 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1144 static int set_config_apparmor_allow_incomplete(const char *key
,
1146 struct lxc_conf
*lxc_conf
,
1149 if (lxc_config_value_empty(value
)) {
1150 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1154 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1157 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1163 static int set_config_apparmor_allow_nesting(const char *key
,
1165 struct lxc_conf
*lxc_conf
,
1168 if (lxc_config_value_empty(value
))
1169 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1171 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1174 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1180 static int set_config_apparmor_raw(const char *key
,
1182 struct lxc_conf
*lxc_conf
,
1186 struct lxc_list
*list
;
1188 if (lxc_config_value_empty(value
))
1189 return lxc_clear_apparmor_raw(lxc_conf
);
1191 list
= malloc(sizeof(*list
));
1197 elem
= strdup(value
);
1204 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1209 static int set_config_selinux_context(const char *key
, const char *value
,
1210 struct lxc_conf
*lxc_conf
, void *data
)
1212 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1215 static int set_config_log_file(const char *key
, const char *value
,
1216 struct lxc_conf
*c
, void *data
)
1220 if (lxc_config_value_empty(value
)) {
1226 /* Store these values in the lxc_conf, and then try to set for actual
1229 ret
= set_config_path_item(&c
->logfile
, value
);
1231 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1236 static int set_config_log_level(const char *key
, const char *value
,
1237 struct lxc_conf
*lxc_conf
, void *data
)
1241 if (lxc_config_value_empty(value
)) {
1242 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1246 if (value
[0] >= '0' && value
[0] <= '9') {
1247 if (lxc_safe_int(value
, &newlevel
) < 0)
1250 newlevel
= lxc_log_priority_to_int(value
);
1253 /* Store these values in the lxc_conf, and then try to set for actual
1256 lxc_conf
->loglevel
= newlevel
;
1258 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1261 static int set_config_autodev(const char *key
, const char *value
,
1262 struct lxc_conf
*lxc_conf
, void *data
)
1264 if (lxc_config_value_empty(value
)) {
1265 lxc_conf
->autodev
= 0;
1269 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1272 if (lxc_conf
->autodev
> 1)
1278 static int set_config_signal_halt(const char *key
, const char *value
,
1279 struct lxc_conf
*lxc_conf
, void *data
)
1283 if (lxc_config_value_empty(value
)) {
1284 lxc_conf
->haltsignal
= 0;
1288 sig_n
= sig_parse(value
);
1292 lxc_conf
->haltsignal
= sig_n
;
1297 static int set_config_signal_reboot(const char *key
, const char *value
,
1298 struct lxc_conf
*lxc_conf
, void *data
)
1302 if (lxc_config_value_empty(value
)) {
1303 lxc_conf
->rebootsignal
= 0;
1307 sig_n
= sig_parse(value
);
1311 lxc_conf
->rebootsignal
= sig_n
;
1316 static int set_config_signal_stop(const char *key
, const char *value
,
1317 struct lxc_conf
*lxc_conf
, void *data
)
1321 if (lxc_config_value_empty(value
)) {
1322 lxc_conf
->stopsignal
= 0;
1326 sig_n
= sig_parse(value
);
1330 lxc_conf
->stopsignal
= sig_n
;
1335 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1336 struct lxc_conf
*lxc_conf
, int version
)
1338 const char *subkey
, *token
;
1340 struct lxc_list
*cglist
= NULL
;
1341 struct lxc_cgroup
*cgelem
= NULL
;
1343 if (lxc_config_value_empty(value
))
1344 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1346 if (version
== CGROUP2_SUPER_MAGIC
) {
1347 token
= "lxc.cgroup2.";
1349 } else if (version
== CGROUP_SUPER_MAGIC
) {
1350 token
= "lxc.cgroup.";
1356 if (strncmp(key
, token
, token_len
) != 0)
1359 subkey
= key
+ token_len
;
1360 if (*subkey
== '\0')
1363 cglist
= malloc(sizeof(*cglist
));
1367 cgelem
= malloc(sizeof(*cgelem
));
1370 memset(cgelem
, 0, sizeof(*cgelem
));
1372 cgelem
->subsystem
= strdup(subkey
);
1373 if (!cgelem
->subsystem
)
1376 cgelem
->value
= strdup(value
);
1380 cgelem
->version
= version
;
1382 lxc_list_add_elem(cglist
, cgelem
);
1384 if (version
== CGROUP2_SUPER_MAGIC
)
1385 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1387 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1394 free(cgelem
->subsystem
);
1395 free(cgelem
->value
);
1402 static int set_config_cgroup_controller(const char *key
, const char *value
,
1403 struct lxc_conf
*lxc_conf
, void *data
)
1405 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1406 CGROUP_SUPER_MAGIC
);
1409 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1410 struct lxc_conf
*lxc_conf
, void *data
)
1412 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1413 CGROUP2_SUPER_MAGIC
);
1417 static int set_config_cgroup_dir(const char *key
, const char *value
,
1418 struct lxc_conf
*lxc_conf
, void *data
)
1420 if (lxc_config_value_empty(value
))
1421 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1423 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1426 static int set_config_cgroup_relative(const char *key
, const char *value
,
1427 struct lxc_conf
*lxc_conf
, void *data
)
1429 unsigned int converted
;
1432 if (lxc_config_value_empty(value
))
1433 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1435 ret
= lxc_safe_uint(value
, &converted
);
1439 if (converted
== 1) {
1440 lxc_conf
->cgroup_meta
.relative
= true;
1444 if (converted
== 0) {
1445 lxc_conf
->cgroup_meta
.relative
= false;
1452 static int set_config_prlimit(const char *key
, const char *value
,
1453 struct lxc_conf
*lxc_conf
, void *data
)
1455 struct lxc_list
*iter
;
1456 struct rlimit limit
;
1458 struct lxc_list
*limlist
= NULL
;
1459 struct lxc_limit
*limelem
= NULL
;
1461 if (lxc_config_value_empty(value
))
1462 return lxc_clear_limits(lxc_conf
, key
);
1464 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1467 key
+= STRLITERALLEN("lxc.prlimit.");
1469 /* soft limit comes first in the value */
1470 if (!parse_limit_value(&value
, &limit_value
))
1473 limit
.rlim_cur
= limit_value
;
1475 /* skip spaces and a colon */
1476 while (isspace(*value
))
1481 else if (*value
) /* any other character is an error here */
1484 while (isspace(*value
))
1487 /* optional hard limit */
1489 if (!parse_limit_value(&value
, &limit_value
))
1492 limit
.rlim_max
= limit_value
;
1494 /* check for trailing garbage */
1495 while (isspace(*value
))
1501 /* a single value sets both hard and soft limit */
1502 limit
.rlim_max
= limit
.rlim_cur
;
1505 /* find existing list element */
1506 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1507 limelem
= iter
->elem
;
1508 if (!strcmp(key
, limelem
->resource
)) {
1509 limelem
->limit
= limit
;
1514 /* allocate list element */
1515 limlist
= malloc(sizeof(*limlist
));
1519 limelem
= malloc(sizeof(*limelem
));
1522 memset(limelem
, 0, sizeof(*limelem
));
1524 limelem
->resource
= strdup(key
);
1525 if (!limelem
->resource
)
1528 limelem
->limit
= limit
;
1529 lxc_list_add_elem(limlist
, limelem
);;
1530 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1538 free(limelem
->resource
);
1545 static int set_config_sysctl(const char *key
, const char *value
,
1546 struct lxc_conf
*lxc_conf
, void *data
)
1548 struct lxc_list
*iter
;
1549 char *replace_value
= NULL
;
1550 struct lxc_list
*sysctl_list
= NULL
;
1551 struct lxc_sysctl
*sysctl_elem
= NULL
;
1553 if (lxc_config_value_empty(value
))
1554 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1556 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1559 key
+= STRLITERALLEN("lxc.sysctl.");
1561 /* find existing list element */
1562 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1563 sysctl_elem
= iter
->elem
;
1565 if (strcmp(key
, sysctl_elem
->key
) != 0)
1568 replace_value
= strdup(value
);
1572 free(sysctl_elem
->value
);
1573 sysctl_elem
->value
= replace_value
;
1578 /* allocate list element */
1579 sysctl_list
= malloc(sizeof(*sysctl_list
));
1583 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1586 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1588 sysctl_elem
->key
= strdup(key
);
1589 if (!sysctl_elem
->key
)
1592 sysctl_elem
->value
= strdup(value
);
1593 if (!sysctl_elem
->value
)
1596 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1597 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1605 free(sysctl_elem
->key
);
1606 free(sysctl_elem
->value
);
1613 static int set_config_proc(const char *key
, const char *value
,
1614 struct lxc_conf
*lxc_conf
, void *data
)
1617 struct lxc_list
*proclist
= NULL
;
1618 struct lxc_proc
*procelem
= NULL
;
1620 if (lxc_config_value_empty(value
))
1621 return clr_config_proc(key
, lxc_conf
, NULL
);
1623 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
1626 subkey
= key
+ STRLITERALLEN("lxc.proc.");
1627 if (*subkey
== '\0')
1630 proclist
= malloc(sizeof(*proclist
));
1634 procelem
= malloc(sizeof(*procelem
));
1637 memset(procelem
, 0, sizeof(*procelem
));
1639 procelem
->filename
= strdup(subkey
);
1640 procelem
->value
= strdup(value
);
1642 if (!procelem
->filename
|| !procelem
->value
)
1645 proclist
->elem
= procelem
;
1647 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1655 free(procelem
->filename
);
1656 free(procelem
->value
);
1663 static int set_config_idmaps(const char *key
, const char *value
,
1664 struct lxc_conf
*lxc_conf
, void *data
)
1666 unsigned long hostid
, nsid
, range
;
1669 struct lxc_list
*idmaplist
= NULL
;
1670 struct id_map
*idmap
= NULL
;
1672 if (lxc_config_value_empty(value
))
1673 return lxc_clear_idmaps(lxc_conf
);
1675 idmaplist
= malloc(sizeof(*idmaplist
));
1679 idmap
= malloc(sizeof(*idmap
));
1682 memset(idmap
, 0, sizeof(*idmap
));
1684 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1686 ERROR("Failed to parse id mappings");
1690 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1692 idmap
->idtype
= ID_TYPE_UID
;
1693 else if (type
== 'g')
1694 idmap
->idtype
= ID_TYPE_GID
;
1698 idmap
->hostid
= hostid
;
1700 idmap
->range
= range
;
1701 idmaplist
->elem
= idmap
;
1702 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1704 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1705 if (idmap
->nsid
== 0)
1706 lxc_conf
->root_nsuid_map
= idmap
;
1708 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1709 if (idmap
->nsid
== 0)
1710 lxc_conf
->root_nsgid_map
= idmap
;
1723 static int set_config_mount_fstab(const char *key
, const char *value
,
1724 struct lxc_conf
*lxc_conf
, void *data
)
1726 if (lxc_config_value_empty(value
)) {
1727 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1731 return set_config_path_item(&lxc_conf
->fstab
, value
);
1734 static int set_config_mount_auto(const char *key
, const char *value
,
1735 struct lxc_conf
*lxc_conf
, void *data
)
1737 char *autos
, *token
;
1744 } allowed_auto_mounts
[] = {
1745 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1746 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1747 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1748 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1749 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1750 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1751 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1752 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1753 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1754 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1755 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1756 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1757 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1758 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1759 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1760 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1761 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1762 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1763 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1764 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1765 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1766 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
1767 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
1768 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
1769 /* For adding anything that is just a single on/off, but has no
1770 * options: keep mask and flag identical and just define the enum
1771 * value as an unused bit so far
1776 if (lxc_config_value_empty(value
)) {
1777 lxc_conf
->auto_mounts
= 0;
1781 autos
= strdup(value
);
1785 lxc_iterate_parts(token
, autos
, " \t") {
1786 bool is_shmounts
= false;
1788 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1789 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1792 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
1793 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
1799 if (!allowed_auto_mounts
[i
].token
) {
1800 ERROR("Invalid filesystem to automount \"%s\"", token
);
1804 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1805 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1808 char *container_path
;
1811 host_path
= token
+ STRLITERALLEN("shmounts:");
1812 if (*host_path
== '\0') {
1813 SYSERROR("Failed to copy shmounts host path");
1817 container_path
= strchr(host_path
, ':');
1818 if (!container_path
|| *(container_path
+ 1) == '\0')
1819 container_path
= "/dev/.lxc-mounts";
1821 *container_path
++ = '\0';
1823 lxc_conf
->shmount
.path_host
= strdup(host_path
);
1824 if (!lxc_conf
->shmount
.path_host
) {
1825 SYSERROR("Failed to copy shmounts host path");
1829 lxc_conf
->shmount
.path_cont
= strdup(container_path
);
1830 if(!lxc_conf
->shmount
.path_cont
) {
1831 SYSERROR("Failed to copy shmounts container path");
1845 static int set_config_mount(const char *key
, const char *value
,
1846 struct lxc_conf
*lxc_conf
, void *data
)
1849 struct lxc_list
*mntlist
;
1851 if (lxc_config_value_empty(value
))
1852 return lxc_clear_mount_entries(lxc_conf
);
1854 mntlist
= malloc(sizeof(*mntlist
));
1858 mntelem
= strdup(value
);
1863 mntlist
->elem
= mntelem
;
1865 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1870 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
1871 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
1874 static int set_config_cap_keep(const char *key
, const char *value
,
1875 struct lxc_conf
*lxc_conf
, void *data
)
1877 char *keepcaps
, *token
;
1878 struct lxc_list
*keeplist
;
1881 if (lxc_config_value_empty(value
))
1882 return lxc_clear_config_keepcaps(lxc_conf
);
1884 keepcaps
= strdup(value
);
1888 /* In case several capability keep is specified in a single line
1889 * split these caps in a single element for the list.
1891 lxc_iterate_parts(token
, keepcaps
, " \t") {
1892 if (!strcmp(token
, "none"))
1893 lxc_clear_config_keepcaps(lxc_conf
);
1895 keeplist
= malloc(sizeof(*keeplist
));
1899 keeplist
->elem
= strdup(token
);
1900 if (!keeplist
->elem
) {
1905 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1916 static int set_config_cap_drop(const char *key
, const char *value
,
1917 struct lxc_conf
*lxc_conf
, void *data
)
1919 char *dropcaps
, *token
;
1920 struct lxc_list
*droplist
;
1923 if (lxc_config_value_empty(value
))
1924 return lxc_clear_config_caps(lxc_conf
);
1926 dropcaps
= strdup(value
);
1930 /* In case several capability drop is specified in a single line
1931 * split these caps in a single element for the list.
1933 lxc_iterate_parts(token
, dropcaps
, " \t") {
1934 droplist
= malloc(sizeof(*droplist
));
1938 droplist
->elem
= strdup(token
);
1939 if (!droplist
->elem
) {
1944 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1955 static int set_config_console_path(const char *key
, const char *value
,
1956 struct lxc_conf
*lxc_conf
, void *data
)
1958 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1961 static int set_config_console_rotate(const char *key
, const char *value
,
1962 struct lxc_conf
*lxc_conf
, void *data
)
1964 if (lxc_config_value_empty(value
)) {
1965 lxc_conf
->console
.log_rotate
= 0;
1969 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1972 if (lxc_conf
->console
.log_rotate
> 1) {
1973 ERROR("The \"lxc.console.rotate\" config key can only be set "
1981 static int set_config_console_logfile(const char *key
, const char *value
,
1982 struct lxc_conf
*lxc_conf
, void *data
)
1984 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1987 static int set_config_console_buffer_size(const char *key
, const char *value
,
1988 struct lxc_conf
*lxc_conf
, void *data
)
1992 uint64_t buffer_size
, pgsz
;
1994 if (lxc_config_value_empty(value
)) {
1995 lxc_conf
->console
.buffer_size
= 0;
1999 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2000 if (!strcmp(value
, "auto")) {
2001 lxc_conf
->console
.buffer_size
= 1 << 17;
2005 ret
= parse_byte_size_string(value
, &size
);
2012 /* must be at least a page size */
2013 pgsz
= lxc_getpagesize();
2014 if ((uint64_t)size
< pgsz
) {
2015 NOTICE("Requested ringbuffer size for the console is %" PRId64
2016 " but must be at least %" PRId64
2017 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2022 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2023 if (buffer_size
== 0)
2026 if (buffer_size
!= size
)
2027 NOTICE("Passed size was not a power of 2. Rounding log size to "
2028 "next power of two: %" PRIu64
" bytes", buffer_size
);
2030 lxc_conf
->console
.buffer_size
= buffer_size
;
2035 static int set_config_console_size(const char *key
, const char *value
,
2036 struct lxc_conf
*lxc_conf
, void *data
)
2040 uint64_t log_size
, pgsz
;
2042 if (lxc_config_value_empty(value
)) {
2043 lxc_conf
->console
.log_size
= 0;
2047 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2048 if (!strcmp(value
, "auto")) {
2049 lxc_conf
->console
.log_size
= 1 << 17;
2053 ret
= parse_byte_size_string(value
, &size
);
2060 /* must be at least a page size */
2061 pgsz
= lxc_getpagesize();
2062 if ((uint64_t)size
< pgsz
) {
2063 NOTICE("Requested ringbuffer size for the console is %" PRId64
2064 " but must be at least %" PRId64
2065 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2070 log_size
= lxc_find_next_power2((uint64_t)size
);
2074 if (log_size
!= size
)
2075 NOTICE("Passed size was not a power of 2. Rounding log size to "
2076 "next power of two: %" PRIu64
" bytes", log_size
);
2078 lxc_conf
->console
.log_size
= log_size
;
2083 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2086 size_t len
= conf
->unexpanded_len
;
2088 update_hwaddr(line
);
2090 linelen
= strlen(line
);
2091 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2092 char *tmp
= realloc(conf
->unexpanded_config
,
2093 conf
->unexpanded_alloced
+ 1024);
2097 if (!conf
->unexpanded_config
)
2100 conf
->unexpanded_config
= tmp
;
2101 conf
->unexpanded_alloced
+= 1024;
2104 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2105 conf
->unexpanded_len
+= linelen
;
2106 if (line
[linelen
- 1] != '\n')
2107 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2108 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2113 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2115 struct dirent
*direntp
;
2117 char path
[PATH_MAX
];
2121 dir
= opendir(dirp
);
2125 while ((direntp
= readdir(dir
))) {
2128 fnam
= direntp
->d_name
;
2129 if (!strcmp(fnam
, "."))
2132 if (!strcmp(fnam
, ".."))
2136 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2139 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2140 if (len
< 0 || len
>= PATH_MAX
) {
2145 ret
= lxc_config_read(path
, lxc_conf
, true);
2157 static int set_config_includefiles(const char *key
, const char *value
,
2158 struct lxc_conf
*lxc_conf
, void *data
)
2160 if (lxc_config_value_empty(value
)) {
2161 clr_config_includefiles(key
, lxc_conf
, NULL
);
2166 return do_includedir(value
, lxc_conf
);
2168 return lxc_config_read(value
, lxc_conf
, true);
2171 static int set_config_rootfs_path(const char *key
, const char *value
,
2172 struct lxc_conf
*lxc_conf
, void *data
)
2176 const char *container_path
;
2178 if (lxc_config_value_empty(value
)) {
2179 free(lxc_conf
->rootfs
.path
);
2180 lxc_conf
->rootfs
.path
= NULL
;
2184 dup
= strdup(value
);
2188 /* Split <storage type>:<container path> into <storage type> and
2189 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2190 * "rootfs.path" to <container path>.
2192 tmp
= strchr(dup
, ':');
2196 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2203 container_path
= tmp
;
2205 container_path
= value
;
2208 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2214 static int set_config_rootfs_managed(const char *key
, const char *value
,
2215 struct lxc_conf
*lxc_conf
, void *data
)
2217 unsigned int val
= 0;
2219 if (lxc_config_value_empty(value
)) {
2220 lxc_conf
->rootfs
.managed
= true;
2224 if (lxc_safe_uint(value
, &val
) < 0)
2229 lxc_conf
->rootfs
.managed
= false;
2232 lxc_conf
->rootfs
.managed
= true;
2239 static int set_config_rootfs_mount(const char *key
, const char *value
,
2240 struct lxc_conf
*lxc_conf
, void *data
)
2242 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2245 static int set_config_rootfs_options(const char *key
, const char *value
,
2246 struct lxc_conf
*lxc_conf
, void *data
)
2249 unsigned long mflags
= 0, pflags
= 0;
2250 char *mdata
= NULL
, *opts
= NULL
;
2251 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2253 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2257 ret
= parse_propagationopts(value
, &pflags
);
2263 ret
= set_config_string_item(&opts
, value
);
2269 rootfs
->mountflags
= mflags
| pflags
;
2270 rootfs
->options
= opts
;
2271 rootfs
->data
= mdata
;
2276 static int set_config_uts_name(const char *key
, const char *value
,
2277 struct lxc_conf
*lxc_conf
, void *data
)
2279 struct utsname
*utsname
;
2281 if (lxc_config_value_empty(value
)) {
2282 clr_config_uts_name(key
, lxc_conf
, NULL
);
2286 utsname
= malloc(sizeof(*utsname
));
2290 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2295 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2296 free(lxc_conf
->utsname
);
2297 lxc_conf
->utsname
= utsname
;
2302 static int set_config_namespace_clone(const char *key
, const char *value
,
2303 struct lxc_conf
*lxc_conf
, void *data
)
2308 if (lxc_config_value_empty(value
))
2309 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2311 if (lxc_conf
->ns_keep
!= 0) {
2313 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2314 "\"lxc.namespace.keep\"");
2322 lxc_iterate_parts(token
, ns
, " \t") {
2323 token
+= lxc_char_left_gc(token
, strlen(token
));
2324 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2325 cloneflag
= lxc_namespace_2_cloneflag(token
);
2326 if (cloneflag
< 0) {
2330 lxc_conf
->ns_clone
|= cloneflag
;
2337 static int set_config_namespace_keep(const char *key
, const char *value
,
2338 struct lxc_conf
*lxc_conf
, void *data
)
2343 if (lxc_config_value_empty(value
))
2344 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2346 if (lxc_conf
->ns_clone
!= 0) {
2348 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2349 "\"lxc.namespace.keep\"");
2357 lxc_iterate_parts(token
, ns
, " \t") {
2358 token
+= lxc_char_left_gc(token
, strlen(token
));
2359 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2360 cloneflag
= lxc_namespace_2_cloneflag(token
);
2361 if (cloneflag
< 0) {
2365 lxc_conf
->ns_keep
|= cloneflag
;
2372 static int set_config_namespace_share(const char *key
, const char *value
,
2373 struct lxc_conf
*lxc_conf
, void *data
)
2376 const char *namespace;
2378 if (lxc_config_value_empty(value
))
2379 return clr_config_namespace_share(key
, lxc_conf
, data
);
2381 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2382 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2386 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2389 struct parse_line_conf
{
2390 struct lxc_conf
*conf
;
2394 static int parse_line(char *buffer
, void *data
)
2396 char *dot
, *key
, *line
, *linep
, *value
;
2398 struct lxc_config_t
*config
;
2401 struct parse_line_conf
*plc
= data
;
2403 /* If there are newlines in the config file we should keep them. */
2404 empty_line
= lxc_is_line_empty(dup
);
2408 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2409 * modified the original string on the stack by replacing '=' by '\0'
2412 linep
= line
= strdup(dup
);
2416 if (!plc
->from_include
) {
2417 ret
= append_unexp_config_line(line
, plc
->conf
);
2425 line
+= lxc_char_left_gc(line
, strlen(line
));
2427 /* ignore comments */
2431 /* martian option - don't add it to the config itself */
2432 if (strncmp(line
, "lxc.", 4))
2437 dot
= strchr(line
, '=');
2439 ERROR("Invalid configuration line: %s", line
);
2447 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2449 value
+= lxc_char_left_gc(value
, strlen(value
));
2450 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2452 if (*value
== '\'' || *value
== '\"') {
2455 len
= strlen(value
);
2456 if (len
> 1 && value
[len
- 1] == *value
) {
2457 value
[len
- 1] = '\0';
2462 config
= lxc_get_config(key
);
2464 ERROR("Unknown configuration key \"%s\"", key
);
2468 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2476 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2478 char *dot
, *key
, *line
, *linep
, *value
;
2481 struct new_config_item
*new = NULL
;
2483 linep
= line
= strdup(dup
);
2487 line
+= lxc_char_left_gc(line
, strlen(line
));
2489 /* martian option - don't add it to the config itself */
2490 if (strncmp(line
, "lxc.", 4))
2494 dot
= strchr(line
, '=');
2496 ERROR("Invalid configuration item: %s", line
);
2504 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2506 value
+= lxc_char_left_gc(value
, strlen(value
));
2507 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2509 if (*value
== '\'' || *value
== '\"') {
2512 len
= strlen(value
);
2513 if (len
> 1 && value
[len
- 1] == *value
) {
2514 value
[len
- 1] = '\0';
2520 new = malloc(sizeof(struct new_config_item
));
2524 new->key
= strdup(key
);
2525 new->val
= strdup(value
);
2526 if (!new->val
|| !new->key
)
2534 if (ret
< 0 && new) {
2544 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2546 struct parse_line_conf c
;
2549 c
.from_include
= from_include
;
2551 /* Catch only the top level config file name in the structure. */
2553 conf
->rcfile
= strdup(file
);
2555 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2558 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2560 struct lxc_list
*dent
;
2562 dent
= malloc(sizeof(struct lxc_list
));
2566 dent
->elem
= parse_new_conf_line(arg
);
2572 lxc_list_add_tail(defines
, dent
);
2577 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2579 struct lxc_list
*it
;
2582 lxc_list_for_each(it
, defines
) {
2583 struct new_config_item
*new_item
= it
->elem
;
2584 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2589 lxc_config_define_free(defines
);
2594 void lxc_config_define_free(struct lxc_list
*defines
)
2596 struct lxc_list
*it
, *next
;
2598 lxc_list_for_each_safe(it
, defines
, next
) {
2599 struct new_config_item
*new_item
= it
->elem
;
2600 free(new_item
->key
);
2601 free(new_item
->val
);
2607 signed long lxc_config_parse_arch(const char *arch
)
2609 #if HAVE_SYS_PERSONALITY_H
2615 { "arm", PER_LINUX32
},
2616 { "armel", PER_LINUX32
},
2617 { "armhf", PER_LINUX32
},
2618 { "armv7l", PER_LINUX32
},
2619 { "athlon", PER_LINUX32
},
2620 { "i386", PER_LINUX32
},
2621 { "i486", PER_LINUX32
},
2622 { "i586", PER_LINUX32
},
2623 { "i686", PER_LINUX32
},
2624 { "linux32", PER_LINUX32
},
2625 { "mips", PER_LINUX32
},
2626 { "mipsel", PER_LINUX32
},
2627 { "ppc", PER_LINUX32
},
2628 { "powerpc", PER_LINUX32
},
2629 { "x86", PER_LINUX32
},
2630 { "amd64", PER_LINUX
},
2631 { "arm64", PER_LINUX
},
2632 { "linux64", PER_LINUX
},
2633 { "mips64", PER_LINUX
},
2634 { "mips64el", PER_LINUX
},
2635 { "ppc64", PER_LINUX
},
2636 { "ppc64el", PER_LINUX
},
2637 { "ppc64le", PER_LINUX
},
2638 { "powerpc64", PER_LINUX
},
2639 { "s390x", PER_LINUX
},
2640 { "x86_64", PER_LINUX
},
2642 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2644 for (i
= 0; i
< len
; i
++)
2645 if (!strcmp(pername
[i
].name
, arch
))
2646 return pername
[i
].per
;
2652 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2660 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2661 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2662 { "LSM", LXC_ATTACH_LSM_EXEC
},
2667 /* For the sake of backward compatibility, drop all privileges
2668 * if none is specified.
2670 for (i
= 0; all_privs
[i
].token
; i
++)
2671 *flags
|= all_privs
[i
].flag
;
2676 lxc_iterate_parts(token
, flaglist
, "|") {
2679 for (i
= 0; all_privs
[i
].token
; i
++)
2680 if (!strcmp(all_privs
[i
].token
, token
))
2681 aflag
= all_privs
[i
].flag
;
2692 /* Write out a configuration file. */
2693 int write_config(int fd
, const struct lxc_conf
*conf
)
2696 size_t len
= conf
->unexpanded_len
;
2701 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
2703 SYSERROR("Failed to write configuration file");
2710 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2717 len
= strlen(key
) + strlen(v
) + 4;
2720 if (lxc_config_value_empty(v
))
2721 ret
= snprintf(tmp
, len
, "%s =", key
);
2723 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2724 if (ret
< 0 || ret
>= len
)
2727 /* Save the line verbatim into unexpanded_conf */
2728 if (append_unexp_config_line(tmp
, conf
))
2734 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2738 char *lstart
= conf
->unexpanded_config
;
2740 if (!conf
->unexpanded_config
)
2744 lend
= strchr(lstart
, '\n');
2748 lend
= lstart
+ strlen(lstart
);
2752 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2758 v
= lstart
[strlen(key
)];
2759 if (!isspace(v
) && v
!= '=') {
2765 conf
->unexpanded_len
-= (lend
- lstart
);
2767 if (*lend
== '\0') {
2772 memmove(lstart
, lend
, strlen(lend
) + 1);
2776 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2777 const char *newpath
, const char *oldname
,
2778 const char *newname
, const char *ovldir
)
2781 char *lend
, *newdir
, *olddir
, *p
, *q
;
2782 size_t newdirlen
, olddirlen
;
2783 char *lstart
= conf
->unexpanded_config
;
2784 const char *key
= "lxc.mount.entry";
2786 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2787 olddir
= alloca(olddirlen
+ 1);
2788 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2790 if (ret
< 0 || ret
>= olddirlen
+ 1)
2793 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2794 newdir
= alloca(newdirlen
+ 1);
2795 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2797 if (ret
< 0 || ret
>= newdirlen
+ 1)
2800 if (!conf
->unexpanded_config
)
2804 lend
= strchr(lstart
, '\n');
2806 lend
= lstart
+ strlen(lstart
);
2810 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2813 p
= strchr(lstart
+ strlen(key
), '=');
2824 /* Whenever a lxc.mount.entry entry is found in a line we check
2825 * if the substring "overlay" is present before doing any
2826 * further work. We check for "overlay" because substrings need
2827 * to have at least one space before them in a valid overlay
2828 * lxc.mount.entry (/A B overlay). When the space before is
2829 * missing it is very likely that these substrings are part of a
2830 * path or something else. (Checking q >= lend ensures that we
2831 * only count matches in the current line.) */
2832 q
= strstr(p
, " overlay");
2833 if (!q
|| q
>= lend
)
2836 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2839 /* replace the olddir with newdir */
2840 if (olddirlen
>= newdirlen
) {
2841 size_t diff
= olddirlen
- newdirlen
;
2842 memcpy(q
, newdir
, newdirlen
);
2844 if (olddirlen
!= newdirlen
) {
2845 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2846 strlen(q
) - newdirlen
- diff
+ 1);
2848 conf
->unexpanded_len
-= diff
;
2852 size_t diff
= newdirlen
- olddirlen
;
2853 size_t oldlen
= conf
->unexpanded_len
;
2854 size_t newlen
= oldlen
+ diff
;
2855 size_t poffset
= q
- conf
->unexpanded_config
;
2857 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2861 conf
->unexpanded_len
= newlen
;
2862 conf
->unexpanded_alloced
= newlen
+ 1;
2863 new[newlen
- 1] = '\0';
2864 lend
= new + (lend
- conf
->unexpanded_config
);
2866 /* Move over the remainder to make room for the newdir.
2868 memmove(new + poffset
+ newdirlen
,
2869 new + poffset
+ olddirlen
,
2870 oldlen
- poffset
- olddirlen
+ 1);
2871 conf
->unexpanded_config
= new;
2873 memcpy(new + poffset
, newdir
, newdirlen
);
2884 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2885 const char *newpath
, const char *oldname
,
2886 const char *newname
)
2889 char *lend
, *newdir
, *olddir
, *p
;
2890 char *lstart
= conf
->unexpanded_config
;
2891 size_t newdirlen
, olddirlen
;
2892 const char *key
= "lxc.hook";
2894 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2895 olddir
= alloca(olddirlen
+ 1);
2896 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2897 if (ret
< 0 || ret
>= olddirlen
+ 1)
2900 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2901 newdir
= alloca(newdirlen
+ 1);
2902 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2903 if (ret
< 0 || ret
>= newdirlen
+ 1)
2906 if (!conf
->unexpanded_config
)
2910 lend
= strchr(lstart
, '\n');
2912 lend
= lstart
+ strlen(lstart
);
2916 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2919 p
= strchr(lstart
+ strlen(key
), '=');
2930 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2933 /* replace the olddir with newdir */
2934 if (olddirlen
>= newdirlen
) {
2935 size_t diff
= olddirlen
- newdirlen
;
2936 memcpy(p
, newdir
, newdirlen
);
2938 if (olddirlen
!= newdirlen
) {
2939 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2940 strlen(p
) - newdirlen
- diff
+ 1);
2942 conf
->unexpanded_len
-= diff
;
2946 size_t diff
= newdirlen
- olddirlen
;
2947 size_t oldlen
= conf
->unexpanded_len
;
2948 size_t newlen
= oldlen
+ diff
;
2949 size_t poffset
= p
- conf
->unexpanded_config
;
2951 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2955 conf
->unexpanded_len
= newlen
;
2956 conf
->unexpanded_alloced
= newlen
+ 1;
2957 new[newlen
- 1] = '\0';
2958 lend
= new + (lend
- conf
->unexpanded_config
);
2960 /* Move over the remainder to make room for the newdir.
2962 memmove(new + poffset
+ newdirlen
,
2963 new + poffset
+ olddirlen
,
2964 oldlen
- poffset
- olddirlen
+ 1);
2965 conf
->unexpanded_config
= new;
2967 memcpy(new + poffset
, newdir
, newdirlen
);
2981 ERROR("Error writing to new config"); \
2986 /* This is called only from clone. We wish to update all hwaddrs in the
2987 * unexpanded config file. We can't/don't want to update any which come from
2988 * lxc.includes (there shouldn't be any).
2989 * We can't just walk the c->lxc-conf->network list because that includes netifs
2990 * from the include files. So we update the ones which we find in the unexp
2991 * config file, then find the original macaddr in the conf->network, and update
2992 * that to the same value.
2994 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2996 char *lend
, *p
, *p2
;
2997 struct lxc_list
*it
;
2998 char *lstart
= conf
->unexpanded_config
;
3000 if (!conf
->unexpanded_config
)
3004 char newhwaddr
[18], oldhwaddr
[17];
3006 lend
= strchr(lstart
, '\n');
3008 lend
= lstart
+ strlen(lstart
);
3012 if (!lxc_config_net_hwaddr(lstart
)) {
3017 p
= strchr(lstart
, '=');
3030 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3033 if ((p2
- p
) != 17) {
3034 WARN("Bad hwaddr entry");
3039 memcpy(oldhwaddr
, p
, 17);
3041 if (!new_hwaddr(newhwaddr
))
3044 memcpy(p
, newhwaddr
, 17);
3045 lxc_list_for_each(it
, &conf
->network
) {
3046 struct lxc_netdev
*n
= it
->elem
;
3048 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3049 memcpy(n
->hwaddr
, newhwaddr
, 17);
3058 static int set_config_ephemeral(const char *key
, const char *value
,
3059 struct lxc_conf
*lxc_conf
, void *data
)
3061 if (lxc_config_value_empty(value
)) {
3062 lxc_conf
->ephemeral
= 0;
3066 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3069 if (lxc_conf
->ephemeral
> 1)
3075 static int set_config_log_syslog(const char *key
, const char *value
,
3076 struct lxc_conf
*lxc_conf
, void *data
)
3080 if (lxc_conf
->syslog
) {
3081 free(lxc_conf
->syslog
);
3082 lxc_conf
->syslog
= NULL
;
3085 if (lxc_config_value_empty(value
))
3088 facility
= lxc_syslog_priority_to_int(value
);
3089 if (facility
== -EINVAL
)
3092 lxc_log_syslog(facility
);
3094 return set_config_string_item(&lxc_conf
->syslog
, value
);
3097 static int set_config_no_new_privs(const char *key
, const char *value
,
3098 struct lxc_conf
*lxc_conf
, void *data
)
3102 if (lxc_config_value_empty(value
)) {
3103 lxc_conf
->no_new_privs
= false;
3107 if (lxc_safe_uint(value
, &v
) < 0)
3113 lxc_conf
->no_new_privs
= v
? true : false;
3118 /* Callbacks to get configuration items. */
3119 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3120 struct lxc_conf
*c
, void *data
)
3127 memset(retv
, 0, inlen
);
3129 #if HAVE_SYS_PERSONALITY_H
3132 switch (c
->personality
) {
3134 strprint(retv
, inlen
, "i686");
3137 strprint(retv
, inlen
, "x86_64");
3147 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3148 struct lxc_conf
*c
, void *data
)
3150 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3153 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3154 struct lxc_conf
*c
, void *data
)
3156 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3159 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3160 struct lxc_conf
*c
, void *data
)
3162 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3165 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3166 struct lxc_conf
*c
, void *data
)
3168 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3171 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3172 int inlen
, struct lxc_conf
*c
,
3175 return lxc_get_conf_int(c
, retv
, inlen
,
3176 c
->lsm_aa_allow_incomplete
);
3179 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3180 int inlen
, struct lxc_conf
*c
,
3183 return lxc_get_conf_int(c
, retv
, inlen
,
3184 c
->lsm_aa_allow_nesting
);
3187 static int get_config_apparmor_raw(const char *key
, char *retv
,
3188 int inlen
, struct lxc_conf
*c
,
3192 struct lxc_list
*it
;
3198 memset(retv
, 0, inlen
);
3200 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3201 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3207 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3208 struct lxc_conf
*c
, void *data
)
3210 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3213 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3214 * just the value(s) will be printed. Since there still could be more than one,
3215 * it is newline-separated.
3216 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3218 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3219 * 'lxc.cgroup.subsystem.key = value' format.
3221 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3222 int inlen
, struct lxc_conf
*c
,
3226 size_t namespaced_token_len
;
3227 char *global_token
, *namespaced_token
;
3228 struct lxc_list
*it
;
3230 bool get_all
= false;
3235 memset(retv
, 0, inlen
);
3237 if (version
== CGROUP2_SUPER_MAGIC
) {
3238 global_token
= "lxc.cgroup2";
3239 namespaced_token
= "lxc.cgroup2.";
3240 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3241 } else if (version
== CGROUP_SUPER_MAGIC
) {
3242 global_token
= "lxc.cgroup";
3243 namespaced_token
= "lxc.cgroup.";
3244 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3249 if (strcmp(key
, global_token
) == 0)
3251 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3252 key
+= namespaced_token_len
;
3256 lxc_list_for_each(it
, &c
->cgroup
) {
3257 struct lxc_cgroup
*cg
= it
->elem
;
3260 if (version
!= cg
->version
)
3263 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3264 cg
->subsystem
, cg
->value
);
3265 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3266 strprint(retv
, inlen
, "%s\n", cg
->value
);
3273 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3274 struct lxc_conf
*c
, void *data
)
3276 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3277 CGROUP_SUPER_MAGIC
);
3280 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3281 struct lxc_conf
*c
, void *data
)
3283 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3284 CGROUP2_SUPER_MAGIC
);
3287 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3288 struct lxc_conf
*lxc_conf
, void *data
)
3296 memset(retv
, 0, inlen
);
3298 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3303 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3304 int inlen
, struct lxc_conf
*lxc_conf
,
3307 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3308 lxc_conf
->cgroup_meta
.relative
);
3311 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3312 struct lxc_conf
*c
, void *data
)
3314 struct lxc_list
*it
;
3315 int len
, listlen
, ret
;
3317 /* "u 1000 1000000 65536"
3319 * let's render this as
3337 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3338 char buf
[__LXC_IDMAP_STR_BUF
];
3343 memset(retv
, 0, inlen
);
3345 listlen
= lxc_list_len(&c
->id_map
);
3346 lxc_list_for_each(it
, &c
->id_map
) {
3347 struct id_map
*map
= it
->elem
;
3348 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3349 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3350 map
->nsid
, map
->hostid
, map
->range
);
3351 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3354 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3360 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3361 struct lxc_conf
*c
, void *data
)
3364 v
= lxc_log_priority_to_string(c
->loglevel
);
3365 return lxc_get_conf_str(retv
, inlen
, v
);
3368 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3369 struct lxc_conf
*c
, void *data
)
3371 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3374 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3375 struct lxc_conf
*c
, void *data
)
3377 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3380 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3381 struct lxc_conf
*c
, void *data
)
3383 int len
, fulllen
= 0;
3384 const char *sep
= "";
3389 memset(retv
, 0, inlen
);
3391 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3394 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3395 case LXC_AUTO_PROC_MIXED
:
3396 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3399 case LXC_AUTO_PROC_RW
:
3400 strprint(retv
, inlen
, "%sproc:rw", sep
);
3407 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3408 case LXC_AUTO_SYS_RO
:
3409 strprint(retv
, inlen
, "%ssys:ro", sep
);
3412 case LXC_AUTO_SYS_RW
:
3413 strprint(retv
, inlen
, "%ssys:rw", sep
);
3416 case LXC_AUTO_SYS_MIXED
:
3417 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3424 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3425 case LXC_AUTO_CGROUP_NOSPEC
:
3426 strprint(retv
, inlen
, "%scgroup", sep
);
3428 case LXC_AUTO_CGROUP_MIXED
:
3429 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3431 case LXC_AUTO_CGROUP_RO
:
3432 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3434 case LXC_AUTO_CGROUP_RW
:
3435 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3437 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3438 strprint(retv
, inlen
, "%scgroup-full", sep
);
3440 case LXC_AUTO_CGROUP_FULL_MIXED
:
3441 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3443 case LXC_AUTO_CGROUP_FULL_RO
:
3444 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3446 case LXC_AUTO_CGROUP_FULL_RW
:
3447 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3456 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3457 struct lxc_conf
*c
, void *data
)
3459 int len
, fulllen
= 0;
3460 struct lxc_list
*it
;
3465 memset(retv
, 0, inlen
);
3467 lxc_list_for_each(it
, &c
->mount_list
) {
3468 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3474 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3475 struct lxc_conf
*c
, void *data
)
3477 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3480 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3481 struct lxc_conf
*c
, void *data
)
3483 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3486 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3487 struct lxc_conf
*c
, void *data
)
3489 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3492 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3493 struct lxc_conf
*c
, void *data
)
3495 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3498 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3499 struct lxc_conf
*c
, void *data
)
3501 return lxc_get_conf_str(
3503 c
->utsname
? c
->utsname
->nodename
: NULL
);
3506 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3507 struct lxc_conf
*c
, void *data
)
3510 int len
, fulllen
= 0, found
= -1;
3511 struct lxc_list
*it
;
3514 subkey
= strchr(key
, '.');
3518 subkey
= strchr(subkey
+ 1, '.');
3522 if (*subkey
== '\0')
3525 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3526 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3538 memset(retv
, 0, inlen
);
3540 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3541 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3547 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3548 struct lxc_conf
*c
, void *data
)
3550 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3553 static int get_config_net(const char *key
, char *retv
, int inlen
,
3554 struct lxc_conf
*c
, void *data
)
3556 int len
, fulllen
= 0;
3557 struct lxc_list
*it
;
3562 memset(retv
, 0, inlen
);
3564 lxc_list_for_each(it
, &c
->network
) {
3565 struct lxc_netdev
*n
= it
->elem
;
3566 const char *t
= lxc_net_type_to_str(n
->type
);
3567 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3573 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3574 struct lxc_conf
*c
, void *data
)
3576 int len
, fulllen
= 0;
3577 struct lxc_list
*it
;
3582 memset(retv
, 0, inlen
);
3584 lxc_list_for_each(it
, &c
->caps
) {
3585 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3591 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3592 struct lxc_conf
*c
, void *data
)
3594 int len
, fulllen
= 0;
3595 struct lxc_list
*it
;
3600 memset(retv
, 0, inlen
);
3602 lxc_list_for_each(it
, &c
->keepcaps
) {
3603 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3609 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3610 struct lxc_conf
*c
, void *data
)
3612 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3615 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3616 struct lxc_conf
*c
, void *data
)
3618 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3621 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3622 struct lxc_conf
*c
, void *data
)
3624 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3628 static int get_config_console_buffer_size(const char *key
, char *retv
,
3629 int inlen
, struct lxc_conf
*c
,
3632 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3635 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3636 struct lxc_conf
*c
, void *data
)
3638 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3641 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
3642 int inlen
, struct lxc_conf
*c
,
3645 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp_allow_nesting
);
3648 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3649 struct lxc_conf
*c
, void *data
)
3651 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3654 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3655 struct lxc_conf
*c
, void *data
)
3657 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3660 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3661 struct lxc_conf
*c
, void *data
)
3663 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3666 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3667 struct lxc_conf
*c
, void *data
)
3669 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3672 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3673 struct lxc_conf
*c
, void *data
)
3675 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3678 static int get_config_start(const char *key
, char *retv
, int inlen
,
3679 struct lxc_conf
*c
, void *data
)
3681 if (strcmp(key
+ 10, "auto") == 0)
3682 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3683 else if (strcmp(key
+ 10, "delay") == 0)
3684 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3685 else if (strcmp(key
+ 10, "order") == 0)
3686 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3691 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3692 struct lxc_conf
*c
, void *data
)
3694 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3697 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3698 struct lxc_conf
*c
, void *data
)
3700 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3703 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
3704 int inlen
, struct lxc_conf
*c
,
3707 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
3710 static int get_config_group(const char *key
, char *retv
, int inlen
,
3711 struct lxc_conf
*c
, void *data
)
3713 int len
, fulllen
= 0;
3714 struct lxc_list
*it
;
3719 memset(retv
, 0, inlen
);
3721 lxc_list_for_each(it
, &c
->groups
) {
3722 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3728 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3729 struct lxc_conf
*c
, void *data
)
3731 int len
, fulllen
= 0;
3732 struct lxc_list
*it
;
3737 memset(retv
, 0, inlen
);
3739 lxc_list_for_each(it
, &c
->environment
) {
3740 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3746 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3747 struct lxc_conf
*c
, void *data
)
3749 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3752 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3753 struct lxc_conf
*c
, void *data
)
3755 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3758 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3759 struct lxc_conf
*c
, void *data
)
3761 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3764 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3765 struct lxc_conf
*c
, void *data
)
3767 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3770 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3771 struct lxc_conf
*c
, void *data
)
3773 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3776 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3777 struct lxc_conf
*c
, void *data
)
3779 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3782 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3783 struct lxc_conf
*c
, void *data
)
3785 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3788 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3789 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3790 * printed, in 'lxc.prlimit.resource = value' format.
3792 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3793 struct lxc_conf
*c
, void *data
)
3795 int fulllen
= 0, len
;
3796 bool get_all
= false;
3797 struct lxc_list
*it
;
3802 memset(retv
, 0, inlen
);
3804 if (!strcmp(key
, "lxc.prlimit"))
3806 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3811 lxc_list_for_each(it
, &c
->limits
) {
3812 /* 2 colon separated 64 bit integers or the word 'unlimited' */
3813 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
3815 struct lxc_limit
*lim
= it
->elem
;
3817 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3818 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
3819 partlen
= STRLITERALLEN("unlimited");
3821 partlen
= sprintf(buf
, "%" PRIu64
,
3822 (uint64_t)lim
->limit
.rlim_cur
);
3825 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3826 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3827 memcpy(buf
+ partlen
, ":unlimited",
3828 STRLITERALLEN(":unlimited") + 1);
3830 sprintf(buf
+ partlen
, ":%" PRIu64
,
3831 (uint64_t)lim
->limit
.rlim_max
);
3835 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3836 lim
->resource
, buf
);
3837 } else if (strcmp(lim
->resource
, key
) == 0) {
3838 strprint(retv
, inlen
, "%s", buf
);
3845 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3846 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3847 * entries will be printed, in 'lxc.sysctl.key = value' format.
3849 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3850 struct lxc_conf
*c
, void *data
)
3853 struct lxc_list
*it
;
3855 bool get_all
= false;
3860 memset(retv
, 0, inlen
);
3862 if (strcmp(key
, "lxc.sysctl") == 0)
3864 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
3865 key
+= STRLITERALLEN("lxc.sysctl.");
3869 lxc_list_for_each(it
, &c
->sysctls
) {
3870 struct lxc_sysctl
*elem
= it
->elem
;
3872 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
3874 } else if (strcmp(elem
->key
, key
) == 0) {
3875 strprint(retv
, inlen
, "%s", elem
->value
);
3882 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3883 struct lxc_conf
*c
, void *data
)
3885 struct lxc_list
*it
;
3888 bool get_all
= false;
3893 memset(retv
, 0, inlen
);
3895 if (strcmp(key
, "lxc.proc") == 0)
3897 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
3898 key
+= STRLITERALLEN("lxc.proc.");
3902 lxc_list_for_each(it
, &c
->procs
) {
3903 struct lxc_proc
*proc
= it
->elem
;
3906 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3907 proc
->filename
, proc
->value
);
3908 } else if (strcmp(proc
->filename
, key
) == 0) {
3909 strprint(retv
, inlen
, "%s", proc
->value
);
3916 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3917 struct lxc_conf
*c
, void *data
)
3925 memset(retv
, 0, inlen
);
3927 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3928 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3929 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3935 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3936 struct lxc_conf
*c
, void *data
)
3944 memset(retv
, 0, inlen
);
3946 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3947 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3948 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3954 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3955 struct lxc_conf
*c
, void *data
)
3958 const char *namespace;
3964 memset(retv
, 0, inlen
);
3966 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
3967 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3971 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3976 /* Callbacks to clear config items. */
3977 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3980 c
->personality
= -1;
3984 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3991 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3998 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4006 static inline int clr_config_apparmor_profile(const char *key
,
4007 struct lxc_conf
*c
, void *data
)
4009 free(c
->lsm_aa_profile
);
4010 c
->lsm_aa_profile
= NULL
;
4014 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4018 c
->lsm_aa_allow_incomplete
= 0;
4022 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4026 c
->lsm_aa_allow_nesting
= 0;
4030 static inline int clr_config_apparmor_raw(const char *key
,
4034 return lxc_clear_apparmor_raw(c
);
4037 static inline int clr_config_selinux_context(const char *key
,
4038 struct lxc_conf
*c
, void *data
)
4040 free(c
->lsm_se_context
);
4041 c
->lsm_se_context
= NULL
;
4045 static inline int clr_config_cgroup_controller(const char *key
,
4046 struct lxc_conf
*c
, void *data
)
4048 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4051 static inline int clr_config_cgroup2_controller(const char *key
,
4052 struct lxc_conf
*c
, void *data
)
4054 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4057 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4060 if (lxc_conf
->cgroup_meta
.dir
) {
4061 free(lxc_conf
->cgroup_meta
.dir
);
4062 lxc_conf
->cgroup_meta
.dir
= NULL
;
4068 static inline int clr_config_cgroup_relative(const char *key
,
4069 struct lxc_conf
*lxc_conf
,
4072 lxc_conf
->cgroup_meta
.relative
= false;
4076 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4079 return lxc_clear_idmaps(c
);
4082 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4085 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4089 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4097 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4100 return lxc_clear_mount_entries(c
);
4103 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4106 return lxc_clear_automounts(c
);
4109 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4117 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4120 free(c
->rootfs
.path
);
4121 c
->rootfs
.path
= NULL
;
4125 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4128 c
->rootfs
.managed
= true;
4132 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4135 free(c
->rootfs
.mount
);
4136 c
->rootfs
.mount
= NULL
;
4140 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4143 free(c
->rootfs
.options
);
4144 c
->rootfs
.options
= NULL
;
4146 free(c
->rootfs
.data
);
4147 c
->rootfs
.data
= NULL
;
4152 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4160 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4163 return lxc_clear_hooks(c
, key
);
4166 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4169 /* default to legacy hooks version */
4170 c
->hooks_version
= 0;
4174 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4177 lxc_free_networks(&c
->network
);
4182 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4185 return lxc_clear_config_caps(c
);
4188 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4191 return lxc_clear_config_keepcaps(c
);
4194 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4197 free(c
->console
.path
);
4198 c
->console
.path
= NULL
;
4202 static inline int clr_config_console_logfile(const char *key
,
4203 struct lxc_conf
*c
, void *data
)
4205 free(c
->console
.log_path
);
4206 c
->console
.log_path
= NULL
;
4210 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4213 c
->console
.log_rotate
= 0;
4217 static inline int clr_config_console_buffer_size(const char *key
,
4218 struct lxc_conf
*c
, void *data
)
4220 c
->console
.buffer_size
= 0;
4224 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4227 c
->console
.log_size
= 0;
4231 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4232 struct lxc_conf
*c
, void *data
)
4234 c
->seccomp_allow_nesting
= 0;
4238 static inline int clr_config_seccomp_profile(const char *key
,
4239 struct lxc_conf
*c
, void *data
)
4246 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4253 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4260 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4263 c
->rebootsignal
= 0;
4267 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4274 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4277 if (strcmp(key
+ 10, "auto") == 0)
4279 else if (strcmp(key
+ 10, "delay") == 0)
4281 else if (strcmp(key
+ 10, "order") == 0)
4287 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4295 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4298 c
->monitor_unshare
= 0;
4302 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4303 struct lxc_conf
*c
, void *data
)
4305 c
->monitor_signal_pdeath
= 0;
4309 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4312 return lxc_clear_groups(c
);
4315 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4318 return lxc_clear_environment(c
);
4321 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4324 free(c
->execute_cmd
);
4325 c
->execute_cmd
= NULL
;
4329 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4337 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4345 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4352 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4359 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4366 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4369 c
->no_new_privs
= false;
4373 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4376 return lxc_clear_limits(c
, key
);
4379 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4382 return lxc_clear_sysctls(c
, key
);
4385 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4388 return lxc_clear_procs(c
, key
);
4391 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4394 lxc_clear_includes(c
);
4398 static int clr_config_namespace_clone(const char *key
,
4399 struct lxc_conf
*lxc_conf
, void *data
)
4401 lxc_conf
->ns_clone
= 0;
4405 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4408 lxc_conf
->ns_keep
= 0;
4412 static int clr_config_namespace_share(const char *key
,
4413 struct lxc_conf
*lxc_conf
, void *data
)
4416 const char *namespace;
4418 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4419 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4423 free(lxc_conf
->ns_share
[ns_idx
]);
4424 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4429 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4430 struct lxc_conf
*c
, void *data
)
4435 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4436 struct lxc_conf
*lxc_conf
,
4438 char **deindexed_key
)
4441 unsigned int tmpidx
;
4443 char *copy
, *idx_start
, *idx_end
;
4444 struct lxc_config_t
*config
= NULL
;
4446 /* check that this is a sensible network key */
4447 if (strncmp("lxc.net.", key
, 8)) {
4448 ERROR("Invalid network configuration key \"%s\"", key
);
4454 ERROR("Failed to duplicate string \"%s\"", key
);
4459 if (!isdigit(*(key
+ 8))) {
4460 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4464 /* beginning of index string */
4465 idx_start
= (copy
+ 7);
4468 /* end of index string */
4469 idx_end
= strchr((copy
+ 8), '.');
4473 /* parse current index */
4474 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4477 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
4483 /* This, of course is utterly nonsensical on so many levels, but
4484 * better safe than sorry.
4485 * (Checking for INT_MAX here is intentional.)
4487 if (tmpidx
== INT_MAX
) {
4488 SYSERROR("Number of configured networks would overflow the "
4494 numstrlen
= strlen((idx_start
+ 1));
4496 /* repair configuration key */
4499 /* lxc.net.<idx>.<subkey> */
4502 if (strlen(idx_end
+ 1) == 0) {
4503 ERROR("No subkey in network configuration key \"%s\"", key
);
4507 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4508 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4510 config
= lxc_get_config(copy
);
4512 ERROR("Unknown network configuration key \"%s\"", key
);
4518 *deindexed_key
= copy
;
4527 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4528 * found. So we make sure next comes an integer, find the right callback (by
4529 * rewriting the key), and call it.
4531 static int set_config_net_nic(const char *key
, const char *value
,
4532 struct lxc_conf
*lxc_conf
, void *data
)
4535 const char *idxstring
;
4536 struct lxc_config_t
*config
;
4537 struct lxc_netdev
*netdev
;
4539 char *deindexed_key
= NULL
;
4541 idxstring
= key
+ 8;
4542 if (!isdigit(*idxstring
))
4545 if (lxc_config_value_empty(value
))
4546 return clr_config_net_nic(key
, lxc_conf
, data
);
4548 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4549 if (!config
|| idx
< 0)
4552 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4554 free(deindexed_key
);
4558 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4559 free(deindexed_key
);
4564 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4568 const char *idxstring
;
4569 struct lxc_config_t
*config
;
4570 struct lxc_netdev
*netdev
;
4572 char *deindexed_key
= NULL
;
4574 idxstring
= key
+ 8;
4575 if (!isdigit(*idxstring
))
4578 /* The left conjunct is pretty self-explanatory. The right conjunct
4579 * checks whether the two pointers are equal. If they are we know that
4580 * this is not a key that is namespaced any further and so we are
4581 * supposed to clear the whole network.
4583 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4584 unsigned int rmnetdevidx
;
4586 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4589 /* Remove network from network list. */
4590 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4594 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4595 if (!config
|| idx
< 0)
4598 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4600 free(deindexed_key
);
4604 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4605 free(deindexed_key
);
4610 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4613 struct lxc_netdev
*netdev
= data
;
4623 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4626 struct lxc_netdev
*netdev
= data
;
4631 netdev
->name
[0] = '\0';
4636 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4639 struct lxc_netdev
*netdev
= data
;
4649 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4652 struct lxc_netdev
*netdev
= data
;
4657 netdev
->link
[0] = '\0';
4662 static int clr_config_net_macvlan_mode(const char *key
,
4663 struct lxc_conf
*lxc_conf
, void *data
)
4665 struct lxc_netdev
*netdev
= data
;
4670 if (netdev
->type
!= LXC_NET_MACVLAN
)
4673 netdev
->priv
.macvlan_attr
.mode
= -1;
4678 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4681 struct lxc_netdev
*netdev
= data
;
4686 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4691 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4694 struct lxc_netdev
*netdev
= data
;
4699 free(netdev
->upscript
);
4700 netdev
->upscript
= NULL
;
4705 static int clr_config_net_script_down(const char *key
,
4706 struct lxc_conf
*lxc_conf
, void *data
)
4708 struct lxc_netdev
*netdev
= data
;
4713 free(netdev
->downscript
);
4714 netdev
->downscript
= NULL
;
4719 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4722 struct lxc_netdev
*netdev
= data
;
4727 free(netdev
->hwaddr
);
4728 netdev
->hwaddr
= NULL
;
4733 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4736 struct lxc_netdev
*netdev
= data
;
4747 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4750 struct lxc_netdev
*netdev
= data
;
4755 netdev
->priv
.vlan_attr
.vid
= 0;
4760 static int clr_config_net_ipv4_gateway(const char *key
,
4761 struct lxc_conf
*lxc_conf
, void *data
)
4763 struct lxc_netdev
*netdev
= data
;
4768 free(netdev
->ipv4_gateway
);
4769 netdev
->ipv4_gateway
= NULL
;
4774 static int clr_config_net_ipv4_address(const char *key
,
4775 struct lxc_conf
*lxc_conf
, void *data
)
4777 struct lxc_netdev
*netdev
= data
;
4778 struct lxc_list
*cur
, *next
;
4783 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4792 static int clr_config_net_ipv6_gateway(const char *key
,
4793 struct lxc_conf
*lxc_conf
, void *data
)
4795 struct lxc_netdev
*netdev
= data
;
4800 free(netdev
->ipv6_gateway
);
4801 netdev
->ipv6_gateway
= NULL
;
4806 static int clr_config_net_ipv6_address(const char *key
,
4807 struct lxc_conf
*lxc_conf
, void *data
)
4809 struct lxc_netdev
*netdev
= data
;
4810 struct lxc_list
*cur
, *next
;
4815 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4824 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4825 struct lxc_conf
*c
, void *data
)
4828 const char *idxstring
;
4829 struct lxc_config_t
*config
;
4830 struct lxc_netdev
*netdev
;
4832 char *deindexed_key
= NULL
;
4834 idxstring
= key
+ 8;
4835 if (!isdigit(*idxstring
))
4838 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4839 if (!config
|| idx
< 0)
4842 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4844 free(deindexed_key
);
4848 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4849 free(deindexed_key
);
4854 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4855 struct lxc_conf
*c
, void *data
)
4859 struct lxc_netdev
*netdev
= data
;
4864 memset(retv
, 0, inlen
);
4869 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4874 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4875 struct lxc_conf
*c
, void *data
)
4879 struct lxc_netdev
*netdev
= data
;
4884 memset(retv
, 0, inlen
);
4889 if (netdev
->flags
& IFF_UP
)
4890 strprint(retv
, inlen
, "up");
4895 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4896 struct lxc_conf
*c
, void *data
)
4900 struct lxc_netdev
*netdev
= data
;
4905 memset(retv
, 0, inlen
);
4910 if (netdev
->link
[0] != '\0')
4911 strprint(retv
, inlen
, "%s", netdev
->link
);
4916 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4917 struct lxc_conf
*c
, void *data
)
4921 struct lxc_netdev
*netdev
= data
;
4926 memset(retv
, 0, inlen
);
4931 if (netdev
->name
[0] != '\0')
4932 strprint(retv
, inlen
, "%s", netdev
->name
);
4937 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4938 struct lxc_conf
*c
, void *data
)
4943 struct lxc_netdev
*netdev
= data
;
4948 memset(retv
, 0, inlen
);
4953 if (netdev
->type
!= LXC_NET_MACVLAN
)
4956 switch (netdev
->priv
.macvlan_attr
.mode
) {
4957 case MACVLAN_MODE_PRIVATE
:
4960 case MACVLAN_MODE_VEPA
:
4963 case MACVLAN_MODE_BRIDGE
:
4966 case MACVLAN_MODE_PASSTHRU
:
4974 strprint(retv
, inlen
, "%s", mode
);
4979 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4980 struct lxc_conf
*c
, void *data
)
4984 struct lxc_netdev
*netdev
= data
;
4989 memset(retv
, 0, inlen
);
4994 if (netdev
->type
!= LXC_NET_VETH
)
4997 strprint(retv
, inlen
, "%s",
4998 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4999 ? netdev
->priv
.veth_attr
.pair
5000 : netdev
->priv
.veth_attr
.veth1
);
5005 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
5006 struct lxc_conf
*c
, void *data
)
5010 struct lxc_netdev
*netdev
= data
;
5015 memset(retv
, 0, inlen
);
5020 if (netdev
->upscript
)
5021 strprint(retv
, inlen
, "%s", netdev
->upscript
);
5026 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
5027 struct lxc_conf
*c
, void *data
)
5031 struct lxc_netdev
*netdev
= data
;
5036 memset(retv
, 0, inlen
);
5041 if (netdev
->downscript
)
5042 strprint(retv
, inlen
, "%s", netdev
->downscript
);
5047 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
5048 struct lxc_conf
*c
, void *data
)
5052 struct lxc_netdev
*netdev
= data
;
5057 memset(retv
, 0, inlen
);
5063 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
5068 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
5069 struct lxc_conf
*c
, void *data
)
5073 struct lxc_netdev
*netdev
= data
;
5078 memset(retv
, 0, inlen
);
5084 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5089 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5090 struct lxc_conf
*c
, void *data
)
5094 struct lxc_netdev
*netdev
= data
;
5099 memset(retv
, 0, inlen
);
5104 if (netdev
->type
!= LXC_NET_VLAN
)
5107 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5112 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5113 struct lxc_conf
*c
, void *data
)
5116 char buf
[INET_ADDRSTRLEN
];
5118 struct lxc_netdev
*netdev
= data
;
5123 memset(retv
, 0, inlen
);
5128 if (netdev
->ipv4_gateway_auto
) {
5129 strprint(retv
, inlen
, "auto");
5130 } else if (netdev
->ipv4_gateway
) {
5131 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5132 strprint(retv
, inlen
, "%s", buf
);
5138 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5139 struct lxc_conf
*c
, void *data
)
5143 char buf
[INET_ADDRSTRLEN
];
5144 struct lxc_list
*it
;
5146 struct lxc_netdev
*netdev
= data
;
5151 memset(retv
, 0, inlen
);
5156 listlen
= lxc_list_len(&netdev
->ipv4
);
5158 lxc_list_for_each(it
, &netdev
->ipv4
) {
5159 struct lxc_inetdev
*i
= it
->elem
;
5160 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5161 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5162 (listlen
-- > 1) ? "\n" : "");
5168 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5169 struct lxc_conf
*c
, void *data
)
5172 char buf
[INET6_ADDRSTRLEN
];
5174 struct lxc_netdev
*netdev
= data
;
5179 memset(retv
, 0, inlen
);
5184 if (netdev
->ipv6_gateway_auto
) {
5185 strprint(retv
, inlen
, "auto");
5186 } else if (netdev
->ipv6_gateway
) {
5187 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5188 strprint(retv
, inlen
, "%s", buf
);
5194 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5195 struct lxc_conf
*c
, void *data
)
5199 char buf
[INET6_ADDRSTRLEN
];
5200 struct lxc_list
*it
;
5202 struct lxc_netdev
*netdev
= data
;
5207 memset(retv
, 0, inlen
);
5212 listlen
= lxc_list_len(&netdev
->ipv6
);
5214 lxc_list_for_each(it
, &netdev
->ipv6
) {
5215 struct lxc_inet6dev
*i
= it
->elem
;
5216 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5217 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5218 (listlen
-- > 1) ? "\n" : "");
5224 int lxc_list_config_items(char *retv
, int inlen
)
5233 memset(retv
, 0, inlen
);
5235 for (i
= 0; i
< config_jump_table_size
; i
++) {
5236 char *s
= config_jump_table
[i
].name
;
5238 if (s
[strlen(s
) - 1] == '.')
5241 strprint(retv
, inlen
, "%s\n", s
);
5247 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5256 memset(retv
, 0, inlen
);
5258 if (!strcmp(key
, "lxc.apparmor")) {
5259 strprint(retv
, inlen
, "allow_incomplete\n");
5260 strprint(retv
, inlen
, "allow_nesting\n");
5261 strprint(retv
, inlen
, "profile\n");
5262 strprint(retv
, inlen
, "raw\n");
5263 } else if (!strcmp(key
, "lxc.cgroup")) {
5264 strprint(retv
, inlen
, "dir\n");
5265 } else if (!strcmp(key
, "lxc.selinux")) {
5266 strprint(retv
, inlen
, "context\n");
5267 } else if (!strcmp(key
, "lxc.mount")) {
5268 strprint(retv
, inlen
, "auto\n");
5269 strprint(retv
, inlen
, "entry\n");
5270 strprint(retv
, inlen
, "fstab\n");
5271 } else if (!strcmp(key
, "lxc.rootfs")) {
5272 strprint(retv
, inlen
, "mount\n");
5273 strprint(retv
, inlen
, "options\n");
5274 strprint(retv
, inlen
, "path\n");
5275 } else if (!strcmp(key
, "lxc.uts")) {
5276 strprint(retv
, inlen
, "name\n");
5277 } else if (!strcmp(key
, "lxc.hook")) {
5278 strprint(retv
, inlen
, "autodev\n");
5279 strprint(retv
, inlen
, "clone\n");
5280 strprint(retv
, inlen
, "destroy\n");
5281 strprint(retv
, inlen
, "mount\n");
5282 strprint(retv
, inlen
, "post-stop\n");
5283 strprint(retv
, inlen
, "pre-mount\n");
5284 strprint(retv
, inlen
, "pre-start\n");
5285 strprint(retv
, inlen
, "start-host\n");
5286 strprint(retv
, inlen
, "start\n");
5287 strprint(retv
, inlen
, "stop\n");
5288 } else if (!strcmp(key
, "lxc.cap")) {
5289 strprint(retv
, inlen
, "drop\n");
5290 strprint(retv
, inlen
, "keep\n");
5291 } else if (!strcmp(key
, "lxc.console")) {
5292 strprint(retv
, inlen
, "logfile\n");
5293 strprint(retv
, inlen
, "path\n");
5294 } else if (!strcmp(key
, "lxc.seccomp")) {
5295 strprint(retv
, inlen
, "profile\n");
5296 } else if (!strcmp(key
, "lxc.signal")) {
5297 strprint(retv
, inlen
, "halt\n");
5298 strprint(retv
, inlen
, "reboot\n");
5299 strprint(retv
, inlen
, "stop\n");
5300 } else if (!strcmp(key
, "lxc.start")) {
5301 strprint(retv
, inlen
, "auto\n");
5302 strprint(retv
, inlen
, "delay\n");
5303 strprint(retv
, inlen
, "order\n");
5304 } else if (!strcmp(key
, "lxc.monitor")) {
5305 strprint(retv
, inlen
, "unshare\n");
5313 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5316 const char *idxstring
;
5317 struct lxc_netdev
*netdev
;
5321 idxstring
= key
+ 8;
5322 if (!isdigit(*idxstring
))
5325 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5329 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5336 memset(retv
, 0, inlen
);
5338 strprint(retv
, inlen
, "type\n");
5339 strprint(retv
, inlen
, "script.up\n");
5340 strprint(retv
, inlen
, "script.down\n");
5342 if (netdev
->type
!= LXC_NET_EMPTY
) {
5343 strprint(retv
, inlen
, "flags\n");
5344 strprint(retv
, inlen
, "link\n");
5345 strprint(retv
, inlen
, "name\n");
5346 strprint(retv
, inlen
, "hwaddr\n");
5347 strprint(retv
, inlen
, "mtu\n");
5348 strprint(retv
, inlen
, "ipv6.address\n");
5349 strprint(retv
, inlen
, "ipv6.gateway\n");
5350 strprint(retv
, inlen
, "ipv4.address\n");
5351 strprint(retv
, inlen
, "ipv4.gateway\n");
5354 switch (netdev
->type
) {
5356 strprint(retv
, inlen
, "veth.pair\n");
5358 case LXC_NET_MACVLAN
:
5359 strprint(retv
, inlen
, "macvlan.mode\n");
5362 strprint(retv
, inlen
, "vlan.id\n");