2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
6 * Daniel Lezcano <daniel.lezcano at free.fr>
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #define __STDC_FORMAT_MACROS
39 #include <arpa/inet.h>
41 #include <netinet/in.h>
42 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/utsname.h>
50 #include "confile_utils.h"
52 #include "lxcseccomp.h"
61 #include <../include/ifaddrs.h>
64 #if HAVE_SYS_PERSONALITY_H
65 #include <sys/personality.h>
69 #include "include/strlcpy.h"
73 #include "include/strlcat.h"
76 lxc_log_define(confile
, lxc
);
78 #define lxc_config_define(name) \
79 static int set_config_##name(const char *, const char *, \
80 struct lxc_conf *, void *); \
81 static int get_config_##name(const char *, char *, int, \
82 struct lxc_conf *, void *); \
83 static int clr_config_##name(const char *, struct lxc_conf *, void *);
85 lxc_config_define(autodev
);
86 lxc_config_define(apparmor_allow_incomplete
);
87 lxc_config_define(apparmor_allow_nesting
);
88 lxc_config_define(apparmor_profile
);
89 lxc_config_define(apparmor_raw
);
90 lxc_config_define(cap_drop
);
91 lxc_config_define(cap_keep
);
92 lxc_config_define(cgroup_controller
);
93 lxc_config_define(cgroup2_controller
);
94 lxc_config_define(cgroup_dir
);
95 lxc_config_define(console_buffer_size
);
96 lxc_config_define(console_logfile
);
97 lxc_config_define(console_path
);
98 lxc_config_define(console_rotate
);
99 lxc_config_define(console_size
);
100 lxc_config_define(environment
);
101 lxc_config_define(ephemeral
);
102 lxc_config_define(execute_cmd
);
103 lxc_config_define(group
);
104 lxc_config_define(hooks
);
105 lxc_config_define(hooks_version
);
106 lxc_config_define(idmaps
);
107 lxc_config_define(includefiles
);
108 lxc_config_define(init_cmd
);
109 lxc_config_define(init_cwd
);
110 lxc_config_define(init_gid
);
111 lxc_config_define(init_uid
);
112 lxc_config_define(log_file
);
113 lxc_config_define(log_level
);
114 lxc_config_define(log_syslog
);
115 lxc_config_define(monitor
);
116 lxc_config_define(monitor_signal_pdeath
);
117 lxc_config_define(mount
);
118 lxc_config_define(mount_auto
);
119 lxc_config_define(mount_fstab
);
120 lxc_config_define(namespace_clone
);
121 lxc_config_define(namespace_keep
);
122 lxc_config_define(namespace_share
);
123 lxc_config_define(net
);
124 lxc_config_define(net_flags
);
125 lxc_config_define(net_hwaddr
);
126 lxc_config_define(net_ipv4_address
);
127 lxc_config_define(net_ipv4_gateway
);
128 lxc_config_define(net_ipv6_address
);
129 lxc_config_define(net_ipv6_gateway
);
130 lxc_config_define(net_link
);
131 lxc_config_define(net_macvlan_mode
);
132 lxc_config_define(net_mtu
);
133 lxc_config_define(net_name
);
134 lxc_config_define(net_nic
);
135 lxc_config_define(net_script_down
);
136 lxc_config_define(net_script_up
);
137 lxc_config_define(net_type
);
138 lxc_config_define(net_veth_pair
);
139 lxc_config_define(net_vlan_id
);
140 lxc_config_define(no_new_privs
);
141 lxc_config_define(personality
);
142 lxc_config_define(prlimit
);
143 lxc_config_define(pty_max
);
144 lxc_config_define(rootfs_managed
);
145 lxc_config_define(rootfs_mount
);
146 lxc_config_define(rootfs_options
);
147 lxc_config_define(rootfs_path
);
148 lxc_config_define(seccomp_profile
);
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
[] = {
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", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
172 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
173 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
174 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
175 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
176 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
177 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
178 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
179 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
180 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
181 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
182 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
183 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
184 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
185 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
188 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
190 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
192 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
193 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
194 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
195 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
196 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
197 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
198 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
199 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
200 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
201 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
202 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
203 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
204 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
205 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
206 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
207 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
208 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
209 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
210 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
211 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
212 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
213 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
214 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
215 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
216 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
217 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
218 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
219 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
220 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
221 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
222 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
223 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
224 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
225 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
226 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
227 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
228 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
229 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
230 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
231 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
232 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
233 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
234 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
235 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
236 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
237 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
238 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
239 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
240 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
241 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
242 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
243 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
244 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
245 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
246 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
249 static const size_t config_size
= sizeof(config
) / sizeof(struct lxc_config_t
);
251 struct lxc_config_t
*lxc_get_config(const char *key
)
255 for (i
= 0; i
< config_size
; i
++)
256 if (!strncmp(config
[i
].name
, key
, strlen(config
[i
].name
)))
262 static int set_config_net(const char *key
, const char *value
,
263 struct lxc_conf
*lxc_conf
, void *data
)
265 if (!lxc_config_value_empty(value
)) {
266 ERROR("lxc.net must not have a value");
270 return clr_config_net(key
, lxc_conf
, data
);
273 static int set_config_net_type(const char *key
, const char *value
,
274 struct lxc_conf
*lxc_conf
, void *data
)
276 struct lxc_netdev
*netdev
= data
;
278 if (lxc_config_value_empty(value
))
279 return clr_config_net_type(key
, lxc_conf
, data
);
284 if (!strcmp(value
, "veth")) {
285 netdev
->type
= LXC_NET_VETH
;
286 } else if (!strcmp(value
, "macvlan")) {
287 netdev
->type
= LXC_NET_MACVLAN
;
288 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
,
290 } else if (!strcmp(value
, "vlan")) {
291 netdev
->type
= LXC_NET_VLAN
;
292 } else if (!strcmp(value
, "phys")) {
293 netdev
->type
= LXC_NET_PHYS
;
294 } else if (!strcmp(value
, "empty")) {
295 netdev
->type
= LXC_NET_EMPTY
;
296 } else if (!strcmp(value
, "none")) {
297 netdev
->type
= LXC_NET_NONE
;
299 ERROR("invalid network type %s", value
);
306 static int set_config_net_flags(const char *key
, const char *value
,
307 struct lxc_conf
*lxc_conf
, void *data
)
309 struct lxc_netdev
*netdev
= data
;
311 if (lxc_config_value_empty(value
))
312 return clr_config_net_flags(key
, lxc_conf
, data
);
317 netdev
->flags
|= IFF_UP
;
322 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
323 struct lxc_netdev
*netdev
)
325 struct ifaddrs
*ifaddr
, *ifa
;
328 const char *type_key
= "lxc.net.type";
329 const char *link_key
= "lxc.net.link";
330 const char *tmpvalue
= "phys";
332 if (getifaddrs(&ifaddr
) == -1) {
333 SYSERROR("Get network interfaces failed");
337 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
340 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
343 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
344 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
347 ret
= set_config_net_link(
348 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
350 ERROR("failed to create matched ifnames");
354 ERROR("failed to create matched ifnames");
366 static int set_config_net_link(const char *key
, const char *value
,
367 struct lxc_conf
*lxc_conf
, void *data
)
369 struct lxc_netdev
*netdev
= data
;
372 if (lxc_config_value_empty(value
))
373 return clr_config_net_link(key
, lxc_conf
, data
);
378 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
379 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
381 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
386 static int set_config_net_name(const char *key
, const char *value
,
387 struct lxc_conf
*lxc_conf
, void *data
)
389 struct lxc_netdev
*netdev
= data
;
391 if (lxc_config_value_empty(value
))
392 return clr_config_net_name(key
, lxc_conf
, data
);
397 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
400 static int set_config_net_veth_pair(const char *key
, const char *value
,
401 struct lxc_conf
*lxc_conf
, void *data
)
403 struct lxc_netdev
*netdev
= data
;
405 if (lxc_config_value_empty(value
))
406 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
411 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
414 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
415 struct lxc_conf
*lxc_conf
, void *data
)
417 struct lxc_netdev
*netdev
= data
;
419 if (lxc_config_value_empty(value
))
420 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
425 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
428 static int set_config_net_hwaddr(const char *key
, const char *value
,
429 struct lxc_conf
*lxc_conf
, void *data
)
431 struct lxc_netdev
*netdev
= data
;
434 if (lxc_config_value_empty(value
))
435 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
440 new_value
= strdup(value
);
444 rand_complete_hwaddr(new_value
);
446 if (lxc_config_value_empty(new_value
)) {
448 netdev
->hwaddr
= NULL
;
452 netdev
->hwaddr
= new_value
;
456 static int set_config_net_vlan_id(const char *key
, const char *value
,
457 struct lxc_conf
*lxc_conf
, void *data
)
460 struct lxc_netdev
*netdev
= data
;
462 if (lxc_config_value_empty(value
))
463 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
468 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
475 static int set_config_net_mtu(const char *key
, const char *value
,
476 struct lxc_conf
*lxc_conf
, void *data
)
478 struct lxc_netdev
*netdev
= data
;
480 if (lxc_config_value_empty(value
))
481 return clr_config_net_mtu(key
, lxc_conf
, data
);
486 return set_config_string_item(&netdev
->mtu
, value
);
489 static int set_config_net_ipv4_address(const char *key
, const char *value
,
490 struct lxc_conf
*lxc_conf
, void *data
)
493 struct lxc_netdev
*netdev
= data
;
494 struct lxc_inetdev
*inetdev
;
495 struct lxc_list
*list
;
496 char *cursor
, *slash
;
497 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
499 if (lxc_config_value_empty(value
))
500 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
505 inetdev
= malloc(sizeof(*inetdev
));
509 memset(inetdev
, 0, sizeof(*inetdev
));
511 list
= malloc(sizeof(*list
));
518 list
->elem
= inetdev
;
520 addr
= strdup(value
);
527 cursor
= strstr(addr
, " ");
533 slash
= strstr(addr
, "/");
539 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
540 if (!ret
|| ret
< 0) {
541 SYSERROR("Invalid ipv4 address \"%s\"", value
);
549 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
550 if (!ret
|| ret
< 0) {
551 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
560 /* No prefix specified, determine it from the network class. */
562 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
570 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
573 /* If no broadcast address, let compute one from the
574 * prefix and address.
577 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
578 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
581 lxc_list_add_tail(&netdev
->ipv4
, list
);
587 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
588 struct lxc_conf
*lxc_conf
, void *data
)
590 struct lxc_netdev
*netdev
= data
;
592 if (lxc_config_value_empty(value
))
593 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
598 free(netdev
->ipv4_gateway
);
600 if (!strcmp(value
, "auto")) {
601 netdev
->ipv4_gateway
= NULL
;
602 netdev
->ipv4_gateway_auto
= true;
607 gw
= malloc(sizeof(*gw
));
611 ret
= inet_pton(AF_INET
, value
, gw
);
612 if (!ret
|| ret
< 0) {
613 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
618 netdev
->ipv4_gateway
= gw
;
619 netdev
->ipv4_gateway_auto
= false;
625 static int set_config_net_ipv6_address(const char *key
, const char *value
,
626 struct lxc_conf
*lxc_conf
, void *data
)
629 struct lxc_netdev
*netdev
= data
;
630 struct lxc_inet6dev
*inet6dev
;
631 struct lxc_list
*list
;
632 char *slash
, *valdup
, *netmask
;
634 if (lxc_config_value_empty(value
))
635 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
640 inet6dev
= malloc(sizeof(*inet6dev
));
644 memset(inet6dev
, 0, sizeof(*inet6dev
));
646 list
= malloc(sizeof(*list
));
653 list
->elem
= inet6dev
;
655 valdup
= strdup(value
);
662 inet6dev
->prefix
= 64;
663 slash
= strstr(valdup
, "/");
667 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
676 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
677 if (!ret
|| ret
< 0) {
678 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
685 lxc_list_add_tail(&netdev
->ipv6
, list
);
691 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
692 struct lxc_conf
*lxc_conf
, void *data
)
694 struct lxc_netdev
*netdev
= data
;
696 if (lxc_config_value_empty(value
))
697 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
702 free(netdev
->ipv6_gateway
);
704 if (!strcmp(value
, "auto")) {
705 netdev
->ipv6_gateway
= NULL
;
706 netdev
->ipv6_gateway_auto
= true;
711 gw
= malloc(sizeof(*gw
));
715 ret
= inet_pton(AF_INET6
, value
, gw
);
716 if (!ret
|| ret
< 0) {
717 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
722 netdev
->ipv6_gateway
= gw
;
723 netdev
->ipv6_gateway_auto
= false;
729 static int set_config_net_script_up(const char *key
, const char *value
,
730 struct lxc_conf
*lxc_conf
, void *data
)
732 struct lxc_netdev
*netdev
= data
;
734 if (lxc_config_value_empty(value
))
735 return clr_config_net_script_up(key
, lxc_conf
, data
);
740 return set_config_string_item(&netdev
->upscript
, value
);
743 static int set_config_net_script_down(const char *key
, const char *value
,
744 struct lxc_conf
*lxc_conf
, void *data
)
746 struct lxc_netdev
*netdev
= data
;
748 if (lxc_config_value_empty(value
))
749 return clr_config_net_script_down(key
, lxc_conf
, data
);
754 return set_config_string_item(&netdev
->downscript
, value
);
757 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
759 struct lxc_list
*hooklist
;
761 hooklist
= malloc(sizeof(*hooklist
));
767 hooklist
->elem
= hook
;
768 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
772 static int set_config_seccomp_profile(const char *key
, const char *value
,
773 struct lxc_conf
*lxc_conf
, void *data
)
775 return set_config_path_item(&lxc_conf
->seccomp
, value
);
778 static int set_config_execute_cmd(const char *key
, const char *value
,
779 struct lxc_conf
*lxc_conf
, void *data
)
781 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
784 static int set_config_init_cmd(const char *key
, const char *value
,
785 struct lxc_conf
*lxc_conf
, void *data
)
787 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
790 static int set_config_init_cwd(const char *key
, const char *value
,
791 struct lxc_conf
*lxc_conf
, void *data
)
793 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
796 static int set_config_init_uid(const char *key
, const char *value
,
797 struct lxc_conf
*lxc_conf
, void *data
)
799 unsigned int init_uid
;
801 if (lxc_config_value_empty(value
)) {
802 lxc_conf
->init_uid
= 0;
806 if (lxc_safe_uint(value
, &init_uid
) < 0)
809 lxc_conf
->init_uid
= init_uid
;
814 static int set_config_init_gid(const char *key
, const char *value
,
815 struct lxc_conf
*lxc_conf
, void *data
)
817 unsigned int init_gid
;
819 if (lxc_config_value_empty(value
)) {
820 lxc_conf
->init_gid
= 0;
824 if (lxc_safe_uint(value
, &init_gid
) < 0)
827 lxc_conf
->init_gid
= init_gid
;
832 static int set_config_hooks(const char *key
, const char *value
,
833 struct lxc_conf
*lxc_conf
, void *data
)
837 if (lxc_config_value_empty(value
))
838 return lxc_clear_hooks(lxc_conf
, key
);
840 if (strcmp(key
+ 4, "hook") == 0) {
841 ERROR("lxc.hook must not have a value");
845 copy
= strdup(value
);
849 if (strcmp(key
+ 9, "pre-start") == 0)
850 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
851 else if (strcmp(key
+ 9, "start-host") == 0)
852 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
853 else if (strcmp(key
+ 9, "pre-mount") == 0)
854 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
855 else if (strcmp(key
+ 9, "autodev") == 0)
856 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
857 else if (strcmp(key
+ 9, "mount") == 0)
858 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
859 else if (strcmp(key
+ 9, "start") == 0)
860 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
861 else if (strcmp(key
+ 9, "stop") == 0)
862 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
863 else if (strcmp(key
+ 9, "post-stop") == 0)
864 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
865 else if (strcmp(key
+ 9, "clone") == 0)
866 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
867 else if (strcmp(key
+ 9, "destroy") == 0)
868 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
874 static int set_config_hooks_version(const char *key
, const char *value
,
875 struct lxc_conf
*lxc_conf
, void *data
)
880 if (lxc_config_value_empty(value
))
881 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
883 ret
= lxc_safe_uint(value
, &tmp
);
888 ERROR("Invalid hook version specified. Currently only 0 "
889 "(legacy) and 1 are supported");
893 lxc_conf
->hooks_version
= tmp
;
897 static int set_config_personality(const char *key
, const char *value
,
898 struct lxc_conf
*lxc_conf
, void *data
)
900 signed long personality
= lxc_config_parse_arch(value
);
902 if (personality
>= 0)
903 lxc_conf
->personality
= personality
;
905 WARN("Unsupported personality \"%s\"", value
);
910 static int set_config_pty_max(const char *key
, const char *value
,
911 struct lxc_conf
*lxc_conf
, void *data
)
914 unsigned int max
= 0;
916 if (lxc_config_value_empty(value
)) {
917 lxc_conf
->pty_max
= 0;
921 ret
= lxc_safe_uint(value
, &max
);
925 lxc_conf
->pty_max
= max
;
929 /* We only need to check whether the first byte of the key after the lxc.start.
930 * prefix matches our expectations since they fortunately all start with a
931 * different letter. If anything was wrong with the key we would have already
932 * noticed when the callback was called.
934 static int set_config_start(const char *key
, const char *value
,
935 struct lxc_conf
*lxc_conf
, void *data
)
939 is_empty
= lxc_config_value_empty(value
);
941 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
943 lxc_conf
->start_auto
= 0;
947 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
950 if (lxc_conf
->start_auto
> 1)
954 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
956 lxc_conf
->start_delay
= 0;
960 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
961 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
963 lxc_conf
->start_order
= 0;
967 return lxc_safe_int(value
, &lxc_conf
->start_order
);
973 static int set_config_monitor(const char *key
, const char *value
,
974 struct lxc_conf
*lxc_conf
, void *data
)
976 if (lxc_config_value_empty(value
)) {
977 lxc_conf
->monitor_unshare
= 0;
981 if (strcmp(key
+ 12, "unshare") == 0)
982 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
987 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
988 struct lxc_conf
*lxc_conf
, void *data
)
990 if (lxc_config_value_empty(value
)) {
991 lxc_conf
->monitor_signal_pdeath
= 0;
995 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
998 sig_n
= sig_parse(value
);
1002 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1009 static int set_config_group(const char *key
, const char *value
,
1010 struct lxc_conf
*lxc_conf
, void *data
)
1012 char *groups
, *token
;
1013 struct lxc_list
*grouplist
;
1016 if (lxc_config_value_empty(value
))
1017 return lxc_clear_groups(lxc_conf
);
1019 groups
= strdup(value
);
1023 /* In case several groups are specified in a single line split these
1024 * groups in a single element for the list.
1026 lxc_iterate_parts(token
, groups
, " \t") {
1027 grouplist
= malloc(sizeof(*grouplist
));
1033 grouplist
->elem
= strdup(token
);
1034 if (!grouplist
->elem
) {
1040 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1047 static int set_config_environment(const char *key
, const char *value
,
1048 struct lxc_conf
*lxc_conf
, void *data
)
1050 struct lxc_list
*list_item
= NULL
;
1052 if (lxc_config_value_empty(value
))
1053 return lxc_clear_environment(lxc_conf
);
1055 list_item
= malloc(sizeof(*list_item
));
1059 if (!strchr(value
, '=')) {
1060 const char *env_val
;
1061 const char *env_key
= value
;
1062 const char *env_var
[3] = {0};
1064 env_val
= getenv(env_key
);
1068 env_var
[0] = env_key
;
1069 env_var
[1] = env_val
;
1070 list_item
->elem
= lxc_string_join("=", env_var
, false);
1072 list_item
->elem
= strdup(value
);
1075 if (!list_item
->elem
)
1078 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1087 static int set_config_tty_max(const char *key
, const char *value
,
1088 struct lxc_conf
*lxc_conf
, void *data
)
1091 unsigned int nbtty
= 0;
1093 if (lxc_config_value_empty(value
)) {
1094 lxc_conf
->ttys
.max
= 0;
1098 ret
= lxc_safe_uint(value
, &nbtty
);
1102 lxc_conf
->ttys
.max
= nbtty
;
1106 static int set_config_tty_dir(const char *key
, const char *value
,
1107 struct lxc_conf
*lxc_conf
, void *data
)
1109 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1113 static int set_config_apparmor_profile(const char *key
, const char *value
,
1114 struct lxc_conf
*lxc_conf
, void *data
)
1116 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1119 static int set_config_apparmor_allow_incomplete(const char *key
,
1121 struct lxc_conf
*lxc_conf
,
1124 if (lxc_config_value_empty(value
)) {
1125 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1129 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1132 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1138 static int set_config_apparmor_allow_nesting(const char *key
,
1140 struct lxc_conf
*lxc_conf
,
1143 if (lxc_config_value_empty(value
))
1144 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1146 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1149 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1155 static int set_config_apparmor_raw(const char *key
,
1157 struct lxc_conf
*lxc_conf
,
1161 struct lxc_list
*list
;
1163 if (lxc_config_value_empty(value
))
1164 return lxc_clear_apparmor_raw(lxc_conf
);
1166 list
= malloc(sizeof(*list
));
1172 elem
= strdup(value
);
1179 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1184 static int set_config_selinux_context(const char *key
, const char *value
,
1185 struct lxc_conf
*lxc_conf
, void *data
)
1187 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1190 static int set_config_log_file(const char *key
, const char *value
,
1191 struct lxc_conf
*c
, void *data
)
1195 if (lxc_config_value_empty(value
)) {
1201 /* Store these values in the lxc_conf, and then try to set for actual
1204 ret
= set_config_path_item(&c
->logfile
, value
);
1206 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1211 static int set_config_log_level(const char *key
, const char *value
,
1212 struct lxc_conf
*lxc_conf
, void *data
)
1216 if (lxc_config_value_empty(value
)) {
1217 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1221 if (value
[0] >= '0' && value
[0] <= '9') {
1222 if (lxc_safe_int(value
, &newlevel
) < 0)
1225 newlevel
= lxc_log_priority_to_int(value
);
1228 /* Store these values in the lxc_conf, and then try to set for actual
1231 lxc_conf
->loglevel
= newlevel
;
1232 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1235 static int set_config_autodev(const char *key
, const char *value
,
1236 struct lxc_conf
*lxc_conf
, void *data
)
1238 if (lxc_config_value_empty(value
)) {
1239 lxc_conf
->autodev
= 0;
1243 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1246 if (lxc_conf
->autodev
> 1)
1252 static int set_config_signal_halt(const char *key
, const char *value
,
1253 struct lxc_conf
*lxc_conf
, void *data
)
1257 if (lxc_config_value_empty(value
)) {
1258 lxc_conf
->haltsignal
= 0;
1262 sig_n
= sig_parse(value
);
1266 lxc_conf
->haltsignal
= sig_n
;
1271 static int set_config_signal_reboot(const char *key
, const char *value
,
1272 struct lxc_conf
*lxc_conf
, void *data
)
1276 if (lxc_config_value_empty(value
)) {
1277 lxc_conf
->rebootsignal
= 0;
1281 sig_n
= sig_parse(value
);
1285 lxc_conf
->rebootsignal
= sig_n
;
1290 static int set_config_signal_stop(const char *key
, const char *value
,
1291 struct lxc_conf
*lxc_conf
, void *data
)
1295 if (lxc_config_value_empty(value
)) {
1296 lxc_conf
->stopsignal
= 0;
1300 sig_n
= sig_parse(value
);
1304 lxc_conf
->stopsignal
= sig_n
;
1309 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1310 struct lxc_conf
*lxc_conf
, int version
)
1312 const char *subkey
, *token
;
1314 struct lxc_list
*cglist
= NULL
;
1315 struct lxc_cgroup
*cgelem
= NULL
;
1317 if (lxc_config_value_empty(value
))
1318 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1320 if (version
== CGROUP2_SUPER_MAGIC
) {
1321 token
= "lxc.cgroup2.";
1323 } else if (version
== CGROUP_SUPER_MAGIC
) {
1324 token
= "lxc.cgroup.";
1330 if (strncmp(key
, token
, token_len
) != 0)
1333 subkey
= key
+ token_len
;
1334 if (*subkey
== '\0')
1337 cglist
= malloc(sizeof(*cglist
));
1341 cgelem
= malloc(sizeof(*cgelem
));
1344 memset(cgelem
, 0, sizeof(*cgelem
));
1346 cgelem
->subsystem
= strdup(subkey
);
1347 if (!cgelem
->subsystem
)
1350 cgelem
->value
= strdup(value
);
1354 cgelem
->version
= version
;
1356 lxc_list_add_elem(cglist
, cgelem
);
1358 if (version
== CGROUP2_SUPER_MAGIC
)
1359 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1361 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1368 free(cgelem
->subsystem
);
1369 free(cgelem
->value
);
1376 static int set_config_cgroup_controller(const char *key
, const char *value
,
1377 struct lxc_conf
*lxc_conf
, void *data
)
1379 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1380 CGROUP_SUPER_MAGIC
);
1383 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1384 struct lxc_conf
*lxc_conf
, void *data
)
1386 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1387 CGROUP2_SUPER_MAGIC
);
1391 static int set_config_cgroup_dir(const char *key
, const char *value
,
1392 struct lxc_conf
*lxc_conf
, void *data
)
1394 if (lxc_config_value_empty(value
))
1395 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1397 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1400 static int set_config_prlimit(const char *key
, const char *value
,
1401 struct lxc_conf
*lxc_conf
, void *data
)
1403 struct lxc_list
*iter
;
1404 struct rlimit limit
;
1406 struct lxc_list
*limlist
= NULL
;
1407 struct lxc_limit
*limelem
= NULL
;
1409 if (lxc_config_value_empty(value
))
1410 return lxc_clear_limits(lxc_conf
, key
);
1412 if (strncmp(key
, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1415 key
+= sizeof("lxc.prlimit.") - 1;
1417 /* soft limit comes first in the value */
1418 if (!parse_limit_value(&value
, &limit_value
))
1420 limit
.rlim_cur
= limit_value
;
1422 /* skip spaces and a colon */
1423 while (isspace(*value
))
1428 else if (*value
) /* any other character is an error here */
1431 while (isspace(*value
))
1434 /* optional hard limit */
1436 if (!parse_limit_value(&value
, &limit_value
))
1438 limit
.rlim_max
= limit_value
;
1440 /* check for trailing garbage */
1441 while (isspace(*value
))
1447 /* a single value sets both hard and soft limit */
1448 limit
.rlim_max
= limit
.rlim_cur
;
1451 /* find existing list element */
1452 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1453 limelem
= iter
->elem
;
1454 if (!strcmp(key
, limelem
->resource
)) {
1455 limelem
->limit
= limit
;
1460 /* allocate list element */
1461 limlist
= malloc(sizeof(*limlist
));
1465 limelem
= malloc(sizeof(*limelem
));
1468 memset(limelem
, 0, sizeof(*limelem
));
1470 limelem
->resource
= strdup(key
);
1471 if (!limelem
->resource
)
1473 limelem
->limit
= limit
;
1475 lxc_list_add_elem(limlist
, limelem
);;
1477 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1484 free(limelem
->resource
);
1490 static int set_config_sysctl(const char *key
, const char *value
,
1491 struct lxc_conf
*lxc_conf
, void *data
)
1493 struct lxc_list
*iter
;
1494 char *replace_value
= NULL
;
1495 struct lxc_list
*sysctl_list
= NULL
;
1496 struct lxc_sysctl
*sysctl_elem
= NULL
;
1498 if (lxc_config_value_empty(value
))
1499 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1501 if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1504 key
+= sizeof("lxc.sysctl.") - 1;
1506 /* find existing list element */
1507 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1508 sysctl_elem
= iter
->elem
;
1510 if (strcmp(key
, sysctl_elem
->key
) != 0)
1513 replace_value
= strdup(value
);
1517 free(sysctl_elem
->value
);
1518 sysctl_elem
->value
= replace_value
;
1522 /* allocate list element */
1523 sysctl_list
= malloc(sizeof(*sysctl_list
));
1527 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1530 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1532 sysctl_elem
->key
= strdup(key
);
1533 if (!sysctl_elem
->key
)
1536 sysctl_elem
->value
= strdup(value
);
1537 if (!sysctl_elem
->value
)
1540 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1542 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1549 free(sysctl_elem
->key
);
1550 free(sysctl_elem
->value
);
1556 static int set_config_proc(const char *key
, const char *value
,
1557 struct lxc_conf
*lxc_conf
, void *data
)
1560 struct lxc_list
*proclist
= NULL
;
1561 struct lxc_proc
*procelem
= NULL
;
1563 if (lxc_config_value_empty(value
))
1564 return clr_config_proc(key
, lxc_conf
, NULL
);
1566 if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1569 subkey
= key
+ sizeof("lxc.proc.") - 1;
1570 if (*subkey
== '\0')
1573 proclist
= malloc(sizeof(*proclist
));
1577 procelem
= malloc(sizeof(*procelem
));
1580 memset(procelem
, 0, sizeof(*procelem
));
1582 procelem
->filename
= strdup(subkey
);
1583 procelem
->value
= strdup(value
);
1585 if (!procelem
->filename
|| !procelem
->value
)
1588 proclist
->elem
= procelem
;
1590 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1597 free(procelem
->filename
);
1598 free(procelem
->value
);
1605 static int set_config_idmaps(const char *key
, const char *value
,
1606 struct lxc_conf
*lxc_conf
, void *data
)
1608 unsigned long hostid
, nsid
, range
;
1611 struct lxc_list
*idmaplist
= NULL
;
1612 struct id_map
*idmap
= NULL
;
1614 if (lxc_config_value_empty(value
))
1615 return lxc_clear_idmaps(lxc_conf
);
1617 idmaplist
= malloc(sizeof(*idmaplist
));
1621 idmap
= malloc(sizeof(*idmap
));
1624 memset(idmap
, 0, sizeof(*idmap
));
1626 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1628 ERROR("Failed to parse id mappings");
1632 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1634 idmap
->idtype
= ID_TYPE_UID
;
1635 else if (type
== 'g')
1636 idmap
->idtype
= ID_TYPE_GID
;
1640 idmap
->hostid
= hostid
;
1642 idmap
->range
= range
;
1643 idmaplist
->elem
= idmap
;
1644 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1646 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1647 if (idmap
->nsid
== 0)
1648 lxc_conf
->root_nsuid_map
= idmap
;
1651 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1652 if (idmap
->nsid
== 0)
1653 lxc_conf
->root_nsgid_map
= idmap
;
1666 static int set_config_mount_fstab(const char *key
, const char *value
,
1667 struct lxc_conf
*lxc_conf
, void *data
)
1669 if (lxc_config_value_empty(value
)) {
1670 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1674 return set_config_path_item(&lxc_conf
->fstab
, value
);
1677 static int set_config_mount_auto(const char *key
, const char *value
,
1678 struct lxc_conf
*lxc_conf
, void *data
)
1680 char *autos
, *token
;
1687 } allowed_auto_mounts
[] = {
1688 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1689 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1690 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1691 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1692 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1693 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1694 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1695 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1696 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1697 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1698 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1699 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1700 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1701 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1702 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1703 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1704 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1705 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1706 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1707 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1708 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1709 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
1710 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
1711 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
1712 /* For adding anything that is just a single on/off, but has no
1713 * options: keep mask and flag identical and just define the enum
1714 * value as an unused bit so far
1719 if (lxc_config_value_empty(value
)) {
1720 lxc_conf
->auto_mounts
= 0;
1724 autos
= strdup(value
);
1728 lxc_iterate_parts(token
, autos
, " \t") {
1729 bool is_shmounts
= false;
1731 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1732 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1735 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
1736 strncmp("shmounts:", token
, sizeof("shmounts:") - 1) == 0) {
1742 if (!allowed_auto_mounts
[i
].token
) {
1743 ERROR("Invalid filesystem to automount \"%s\"", token
);
1747 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1748 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1750 lxc_conf
->shmount
.path_host
= strdup(token
+ (sizeof("shmounts:") - 1));
1751 if (!lxc_conf
->shmount
.path_host
) {
1752 SYSERROR("Failed to copy shmounts host path");
1756 if (strcmp(lxc_conf
->shmount
.path_host
, "") == 0) {
1757 ERROR("Invalid shmounts path: empty");
1761 lxc_conf
->shmount
.path_cont
= strdup("/dev/.lxc-mounts");
1762 if(!lxc_conf
->shmount
.path_cont
) {
1763 SYSERROR("Failed to copy shmounts container path");
1777 static int set_config_mount(const char *key
, const char *value
,
1778 struct lxc_conf
*lxc_conf
, void *data
)
1781 struct lxc_list
*mntlist
;
1783 if (lxc_config_value_empty(value
))
1784 return lxc_clear_mount_entries(lxc_conf
);
1786 mntlist
= malloc(sizeof(*mntlist
));
1790 mntelem
= strdup(value
);
1795 mntlist
->elem
= mntelem
;
1797 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1802 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
1803 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
1806 static int set_config_cap_keep(const char *key
, const char *value
,
1807 struct lxc_conf
*lxc_conf
, void *data
)
1809 char *keepcaps
, *token
;
1810 struct lxc_list
*keeplist
;
1813 if (lxc_config_value_empty(value
))
1814 return lxc_clear_config_keepcaps(lxc_conf
);
1816 keepcaps
= strdup(value
);
1820 /* In case several capability keep is specified in a single line
1821 * split these caps in a single element for the list.
1823 lxc_iterate_parts(token
, keepcaps
, " \t") {
1824 if (!strcmp(token
, "none"))
1825 lxc_clear_config_keepcaps(lxc_conf
);
1827 keeplist
= malloc(sizeof(*keeplist
));
1831 keeplist
->elem
= strdup(token
);
1832 if (!keeplist
->elem
) {
1837 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1848 static int set_config_cap_drop(const char *key
, const char *value
,
1849 struct lxc_conf
*lxc_conf
, void *data
)
1851 char *dropcaps
, *token
;
1852 struct lxc_list
*droplist
;
1855 if (lxc_config_value_empty(value
))
1856 return lxc_clear_config_caps(lxc_conf
);
1858 dropcaps
= strdup(value
);
1862 /* In case several capability drop is specified in a single line
1863 * split these caps in a single element for the list.
1865 lxc_iterate_parts(token
, dropcaps
, " \t") {
1866 droplist
= malloc(sizeof(*droplist
));
1870 droplist
->elem
= strdup(token
);
1871 if (!droplist
->elem
) {
1876 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1887 static int set_config_console_path(const char *key
, const char *value
,
1888 struct lxc_conf
*lxc_conf
, void *data
)
1890 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1893 static int set_config_console_rotate(const char *key
, const char *value
,
1894 struct lxc_conf
*lxc_conf
, void *data
)
1896 if (lxc_config_value_empty(value
)) {
1897 lxc_conf
->console
.log_rotate
= 0;
1901 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1904 if (lxc_conf
->console
.log_rotate
> 1) {
1905 ERROR("The \"lxc.console.rotate\" config key can only be set "
1913 static int set_config_console_logfile(const char *key
, const char *value
,
1914 struct lxc_conf
*lxc_conf
, void *data
)
1916 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1919 static int set_config_console_buffer_size(const char *key
, const char *value
,
1920 struct lxc_conf
*lxc_conf
, void *data
)
1924 uint64_t buffer_size
, pgsz
;
1926 if (lxc_config_value_empty(value
)) {
1927 lxc_conf
->console
.buffer_size
= 0;
1931 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1932 if (!strcmp(value
, "auto")) {
1933 lxc_conf
->console
.buffer_size
= 1 << 17;
1937 ret
= parse_byte_size_string(value
, &size
);
1944 /* must be at least a page size */
1945 pgsz
= lxc_getpagesize();
1946 if ((uint64_t)size
< pgsz
) {
1947 NOTICE("Requested ringbuffer size for the console is %" PRId64
1948 " but must be at least %" PRId64
1949 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1954 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1955 if (buffer_size
== 0)
1958 if (buffer_size
!= size
)
1959 NOTICE("Passed size was not a power of 2. Rounding log size to "
1960 "next power of two: %" PRIu64
" bytes", buffer_size
);
1962 lxc_conf
->console
.buffer_size
= buffer_size
;
1966 static int set_config_console_size(const char *key
, const char *value
,
1967 struct lxc_conf
*lxc_conf
, void *data
)
1971 uint64_t log_size
, pgsz
;
1973 if (lxc_config_value_empty(value
)) {
1974 lxc_conf
->console
.log_size
= 0;
1978 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1979 if (!strcmp(value
, "auto")) {
1980 lxc_conf
->console
.log_size
= 1 << 17;
1984 ret
= parse_byte_size_string(value
, &size
);
1991 /* must be at least a page size */
1992 pgsz
= lxc_getpagesize();
1993 if ((uint64_t)size
< pgsz
) {
1994 NOTICE("Requested ringbuffer size for the console is %" PRId64
1995 " but must be at least %" PRId64
1996 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2001 log_size
= lxc_find_next_power2((uint64_t)size
);
2005 if (log_size
!= size
)
2006 NOTICE("Passed size was not a power of 2. Rounding log size to "
2007 "next power of two: %" PRIu64
" bytes", log_size
);
2009 lxc_conf
->console
.log_size
= log_size
;
2013 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2015 size_t len
= conf
->unexpanded_len
, linelen
= strlen(line
);
2017 update_hwaddr(line
);
2019 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2020 char *tmp
= realloc(conf
->unexpanded_config
,
2021 conf
->unexpanded_alloced
+ 1024);
2025 if (!conf
->unexpanded_config
)
2027 conf
->unexpanded_config
= tmp
;
2028 conf
->unexpanded_alloced
+= 1024;
2031 (void)strlcat(conf
->unexpanded_config
, line
, conf
->unexpanded_alloced
);
2032 conf
->unexpanded_len
+= linelen
;
2033 if (line
[linelen
- 1] != '\n') {
2034 (void)strlcat(conf
->unexpanded_config
, "\n", conf
->unexpanded_alloced
);
2035 conf
->unexpanded_len
++;
2041 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2043 struct dirent
*direntp
;
2045 char path
[MAXPATHLEN
];
2049 dir
= opendir(dirp
);
2053 while ((direntp
= readdir(dir
))) {
2056 fnam
= direntp
->d_name
;
2057 if (!strcmp(fnam
, "."))
2060 if (!strcmp(fnam
, ".."))
2064 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2067 len
= snprintf(path
, MAXPATHLEN
, "%s/%s", dirp
, fnam
);
2068 if (len
< 0 || len
>= MAXPATHLEN
) {
2073 ret
= lxc_config_read(path
, lxc_conf
, true);
2085 static int set_config_includefiles(const char *key
, const char *value
,
2086 struct lxc_conf
*lxc_conf
, void *data
)
2088 if (lxc_config_value_empty(value
)) {
2089 clr_config_includefiles(key
, lxc_conf
, NULL
);
2094 return do_includedir(value
, lxc_conf
);
2096 return lxc_config_read(value
, lxc_conf
, true);
2099 static int set_config_rootfs_path(const char *key
, const char *value
,
2100 struct lxc_conf
*lxc_conf
, void *data
)
2104 const char *container_path
;
2106 if (lxc_config_value_empty(value
)) {
2107 free(lxc_conf
->rootfs
.path
);
2108 lxc_conf
->rootfs
.path
= NULL
;
2112 dup
= strdup(value
);
2116 /* Split <storage type>:<container path> into <storage type> and
2117 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2118 * "rootfs.path" to <container path>.
2120 tmp
= strchr(dup
, ':');
2123 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2129 container_path
= tmp
;
2131 container_path
= value
;
2134 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2139 static int set_config_rootfs_managed(const char *key
, const char *value
,
2140 struct lxc_conf
*lxc_conf
, void *data
)
2142 unsigned int val
= 0;
2144 if (lxc_config_value_empty(value
)) {
2145 lxc_conf
->rootfs
.managed
= true;
2149 if (lxc_safe_uint(value
, &val
) < 0)
2154 lxc_conf
->rootfs
.managed
= false;
2157 lxc_conf
->rootfs
.managed
= true;
2164 static int set_config_rootfs_mount(const char *key
, const char *value
,
2165 struct lxc_conf
*lxc_conf
, void *data
)
2167 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2170 static int set_config_rootfs_options(const char *key
, const char *value
,
2171 struct lxc_conf
*lxc_conf
, void *data
)
2174 unsigned long mflags
= 0, pflags
= 0;
2175 char *mdata
= NULL
, *opts
= NULL
;
2176 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2178 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2182 ret
= parse_propagationopts(value
, &pflags
);
2188 ret
= set_config_string_item(&opts
, value
);
2194 rootfs
->mountflags
= mflags
| pflags
;
2195 rootfs
->options
= opts
;
2196 rootfs
->data
= mdata
;
2201 static int set_config_uts_name(const char *key
, const char *value
,
2202 struct lxc_conf
*lxc_conf
, void *data
)
2204 struct utsname
*utsname
;
2206 if (lxc_config_value_empty(value
)) {
2207 clr_config_uts_name(key
, lxc_conf
, NULL
);
2211 utsname
= malloc(sizeof(*utsname
));
2215 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2220 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2221 free(lxc_conf
->utsname
);
2222 lxc_conf
->utsname
= utsname
;
2227 static int set_config_namespace_clone(const char *key
, const char *value
,
2228 struct lxc_conf
*lxc_conf
, void *data
)
2232 char *saveptr
= NULL
;
2234 if (lxc_config_value_empty(value
))
2235 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2237 if (lxc_conf
->ns_keep
!= 0) {
2239 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2240 "\"lxc.namespace.keep\"");
2248 lxc_iterate_parts(token
, ns
, " \t") {
2249 token
+= lxc_char_left_gc(token
, strlen(token
));
2250 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2251 cloneflag
= lxc_namespace_2_cloneflag(token
);
2252 if (cloneflag
< 0) {
2256 lxc_conf
->ns_clone
|= cloneflag
;
2263 static int set_config_namespace_keep(const char *key
, const char *value
,
2264 struct lxc_conf
*lxc_conf
, void *data
)
2268 char *saveptr
= NULL
;
2270 if (lxc_config_value_empty(value
))
2271 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2273 if (lxc_conf
->ns_clone
!= 0) {
2275 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2276 "\"lxc.namespace.keep\"");
2284 lxc_iterate_parts(token
, ns
, " \t") {
2285 token
+= lxc_char_left_gc(token
, strlen(token
));
2286 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2287 cloneflag
= lxc_namespace_2_cloneflag(token
);
2288 if (cloneflag
< 0) {
2292 lxc_conf
->ns_keep
|= cloneflag
;
2299 static int set_config_namespace_share(const char *key
, const char *value
,
2300 struct lxc_conf
*lxc_conf
, void *data
)
2303 const char *namespace;
2305 if (lxc_config_value_empty(value
))
2306 return clr_config_namespace_share(key
, lxc_conf
, data
);
2308 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
2309 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2313 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2316 struct parse_line_conf
{
2317 struct lxc_conf
*conf
;
2321 static int parse_line(char *buffer
, void *data
)
2323 char *dot
, *key
, *line
, *linep
, *value
;
2325 struct lxc_config_t
*config
;
2328 struct parse_line_conf
*plc
= data
;
2330 /* If there are newlines in the config file we should keep them. */
2331 empty_line
= lxc_is_line_empty(dup
);
2335 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2336 * modified the original string on the stack by replacing '=' by '\0'
2339 linep
= line
= strdup(dup
);
2343 if (!plc
->from_include
) {
2344 ret
= append_unexp_config_line(line
, plc
->conf
);
2352 line
+= lxc_char_left_gc(line
, strlen(line
));
2354 /* ignore comments */
2358 /* martian option - don't add it to the config itself */
2359 if (strncmp(line
, "lxc.", 4))
2364 dot
= strchr(line
, '=');
2366 ERROR("Invalid configuration line: %s", line
);
2374 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2376 value
+= lxc_char_left_gc(value
, strlen(value
));
2377 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2379 if (*value
== '\'' || *value
== '\"') {
2382 len
= strlen(value
);
2383 if (len
> 1 && value
[len
- 1] == *value
) {
2384 value
[len
- 1] = '\0';
2389 config
= lxc_get_config(key
);
2391 ERROR("Unknown configuration key \"%s\"", key
);
2395 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2402 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2404 char *dot
, *key
, *line
, *linep
, *value
;
2407 struct new_config_item
*new = NULL
;
2409 linep
= line
= strdup(dup
);
2413 line
+= lxc_char_left_gc(line
, strlen(line
));
2415 /* martian option - don't add it to the config itself */
2416 if (strncmp(line
, "lxc.", 4))
2420 dot
= strchr(line
, '=');
2422 ERROR("Invalid configuration item: %s", line
);
2430 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2432 value
+= lxc_char_left_gc(value
, strlen(value
));
2433 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2435 if (*value
== '\'' || *value
== '\"') {
2438 len
= strlen(value
);
2439 if (len
> 1 && value
[len
- 1] == *value
) {
2440 value
[len
- 1] = '\0';
2446 new = malloc(sizeof(struct new_config_item
));
2450 new->key
= strdup(key
);
2451 new->val
= strdup(value
);
2452 if (!new->val
|| !new->key
)
2460 if (ret
< 0 && new) {
2470 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2473 struct parse_line_conf c
;
2476 c
.from_include
= from_include
;
2478 ret
= access(file
, R_OK
);
2482 /* Catch only the top level config file name in the structure. */
2484 conf
->rcfile
= strdup(file
);
2486 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2489 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2491 struct lxc_list
*dent
;
2493 dent
= malloc(sizeof(struct lxc_list
));
2497 dent
->elem
= parse_new_conf_line(arg
);
2503 lxc_list_add_tail(defines
, dent
);
2507 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2509 struct lxc_list
*it
;
2512 lxc_list_for_each(it
, defines
) {
2513 struct new_config_item
*new_item
= it
->elem
;
2514 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2519 lxc_config_define_free(defines
);
2523 void lxc_config_define_free(struct lxc_list
*defines
)
2525 struct lxc_list
*it
, *next
;
2527 lxc_list_for_each_safe(it
, defines
, next
) {
2528 struct new_config_item
*new_item
= it
->elem
;
2529 free(new_item
->key
);
2530 free(new_item
->val
);
2536 signed long lxc_config_parse_arch(const char *arch
)
2538 #if HAVE_SYS_PERSONALITY_H
2544 { "arm", PER_LINUX32
},
2545 { "armel", PER_LINUX32
},
2546 { "armhf", PER_LINUX32
},
2547 { "armv7l", PER_LINUX32
},
2548 { "athlon", PER_LINUX32
},
2549 { "i386", PER_LINUX32
},
2550 { "i486", PER_LINUX32
},
2551 { "i586", PER_LINUX32
},
2552 { "i686", PER_LINUX32
},
2553 { "linux32", PER_LINUX32
},
2554 { "mips", PER_LINUX32
},
2555 { "mipsel", PER_LINUX32
},
2556 { "ppc", PER_LINUX32
},
2557 { "powerpc", PER_LINUX32
},
2558 { "x86", PER_LINUX32
},
2559 { "amd64", PER_LINUX
},
2560 { "arm64", PER_LINUX
},
2561 { "linux64", PER_LINUX
},
2562 { "mips64", PER_LINUX
},
2563 { "mips64el", PER_LINUX
},
2564 { "ppc64", PER_LINUX
},
2565 { "ppc64el", PER_LINUX
},
2566 { "ppc64le", PER_LINUX
},
2567 { "powerpc64", PER_LINUX
},
2568 { "s390x", PER_LINUX
},
2569 { "x86_64", PER_LINUX
},
2571 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2573 for (i
= 0; i
< len
; i
++)
2574 if (!strcmp(pername
[i
].name
, arch
))
2575 return pername
[i
].per
;
2581 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2589 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2590 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2591 { "LSM", LXC_ATTACH_LSM_EXEC
},
2596 /* For the sake of backward compatibility, drop all privileges
2597 * if none is specified.
2599 for (i
= 0; all_privs
[i
].token
; i
++)
2600 *flags
|= all_privs
[i
].flag
;
2605 lxc_iterate_parts(token
, flaglist
, "|") {
2608 for (i
= 0; all_privs
[i
].token
; i
++)
2609 if (!strcmp(all_privs
[i
].token
, token
))
2610 aflag
= all_privs
[i
].flag
;
2621 /* Write out a configuration file. */
2622 int write_config(int fd
, const struct lxc_conf
*conf
)
2625 size_t len
= conf
->unexpanded_len
;
2630 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
2632 SYSERROR("Failed to write configuration file");
2639 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2646 len
= strlen(key
) + strlen(v
) + 4;
2649 if (lxc_config_value_empty(v
))
2650 ret
= snprintf(tmp
, len
, "%s =", key
);
2652 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2653 if (ret
< 0 || ret
>= len
)
2656 /* Save the line verbatim into unexpanded_conf */
2657 if (append_unexp_config_line(tmp
, conf
))
2663 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2667 char *lstart
= conf
->unexpanded_config
;
2669 if (!conf
->unexpanded_config
)
2673 lend
= strchr(lstart
, '\n');
2676 lend
= lstart
+ strlen(lstart
);
2679 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2684 v
= lstart
[strlen(key
)];
2685 if (!isspace(v
) && v
!= '=') {
2690 conf
->unexpanded_len
-= (lend
- lstart
);
2691 if (*lend
== '\0') {
2695 memmove(lstart
, lend
, strlen(lend
) + 1);
2699 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2700 const char *newpath
, const char *oldname
,
2701 const char *newname
, const char *ovldir
)
2704 char *lend
, *newdir
, *olddir
, *p
, *q
;
2705 size_t newdirlen
, olddirlen
;
2706 char *lstart
= conf
->unexpanded_config
;
2707 const char *key
= "lxc.mount.entry";
2709 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2710 olddir
= alloca(olddirlen
+ 1);
2711 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2713 if (ret
< 0 || ret
>= olddirlen
+ 1)
2716 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2717 newdir
= alloca(newdirlen
+ 1);
2718 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2720 if (ret
< 0 || ret
>= newdirlen
+ 1)
2723 if (!conf
->unexpanded_config
)
2727 lend
= strchr(lstart
, '\n');
2729 lend
= lstart
+ strlen(lstart
);
2733 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2736 p
= strchr(lstart
+ strlen(key
), '=');
2747 /* Whenever a lxc.mount.entry entry is found in a line we check
2748 * if the substring "overlay" is present before doing any
2749 * further work. We check for "overlay" because substrings need
2750 * to have at least one space before them in a valid overlay
2751 * lxc.mount.entry (/A B overlay). When the space before is
2752 * missing it is very likely that these substrings are part of a
2753 * path or something else. (Checking q >= lend ensures that we
2754 * only count matches in the current line.) */
2755 q
= strstr(p
, " overlay");
2756 if (!q
|| q
>= lend
)
2759 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2762 /* replace the olddir with newdir */
2763 if (olddirlen
>= newdirlen
) {
2764 size_t diff
= olddirlen
- newdirlen
;
2765 memcpy(q
, newdir
, newdirlen
);
2766 if (olddirlen
!= newdirlen
) {
2767 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2768 strlen(q
) - newdirlen
- diff
+ 1);
2770 conf
->unexpanded_len
-= diff
;
2774 size_t diff
= newdirlen
- olddirlen
;
2775 size_t oldlen
= conf
->unexpanded_len
;
2776 size_t newlen
= oldlen
+ diff
;
2777 size_t poffset
= q
- conf
->unexpanded_config
;
2779 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2783 conf
->unexpanded_len
= newlen
;
2784 conf
->unexpanded_alloced
= newlen
+ 1;
2785 new[newlen
- 1] = '\0';
2786 lend
= new + (lend
- conf
->unexpanded_config
);
2787 /* Move over the remainder to make room for the newdir.
2789 memmove(new + poffset
+ newdirlen
,
2790 new + poffset
+ olddirlen
,
2791 oldlen
- poffset
- olddirlen
+ 1);
2792 conf
->unexpanded_config
= new;
2793 memcpy(new + poffset
, newdir
, newdirlen
);
2803 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2804 const char *newpath
, const char *oldname
,
2805 const char *newname
)
2808 char *lend
, *newdir
, *olddir
, *p
;
2809 char *lstart
= conf
->unexpanded_config
;
2810 size_t newdirlen
, olddirlen
;
2811 const char *key
= "lxc.hook";
2813 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2814 olddir
= alloca(olddirlen
+ 1);
2815 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2816 if (ret
< 0 || ret
>= olddirlen
+ 1)
2819 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2820 newdir
= alloca(newdirlen
+ 1);
2821 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2822 if (ret
< 0 || ret
>= newdirlen
+ 1)
2825 if (!conf
->unexpanded_config
)
2829 lend
= strchr(lstart
, '\n');
2831 lend
= lstart
+ strlen(lstart
);
2835 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2838 p
= strchr(lstart
+ strlen(key
), '=');
2849 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2852 /* replace the olddir with newdir */
2853 if (olddirlen
>= newdirlen
) {
2854 size_t diff
= olddirlen
- newdirlen
;
2855 memcpy(p
, newdir
, newdirlen
);
2856 if (olddirlen
!= newdirlen
) {
2857 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2858 strlen(p
) - newdirlen
- diff
+ 1);
2860 conf
->unexpanded_len
-= diff
;
2864 size_t diff
= newdirlen
- olddirlen
;
2865 size_t oldlen
= conf
->unexpanded_len
;
2866 size_t newlen
= oldlen
+ diff
;
2867 size_t poffset
= p
- conf
->unexpanded_config
;
2869 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2873 conf
->unexpanded_len
= newlen
;
2874 conf
->unexpanded_alloced
= newlen
+ 1;
2875 new[newlen
- 1] = '\0';
2876 lend
= new + (lend
- conf
->unexpanded_config
);
2877 /* Move over the remainder to make room for the newdir.
2879 memmove(new + poffset
+ newdirlen
,
2880 new + poffset
+ olddirlen
,
2881 oldlen
- poffset
- olddirlen
+ 1);
2882 conf
->unexpanded_config
= new;
2883 memcpy(new + poffset
, newdir
, newdirlen
);
2896 ERROR("Error writing to new config"); \
2901 /* This is called only from clone. We wish to update all hwaddrs in the
2902 * unexpanded config file. We can't/don't want to update any which come from
2903 * lxc.includes (there shouldn't be any).
2904 * We can't just walk the c->lxc-conf->network list because that includes netifs
2905 * from the include files. So we update the ones which we find in the unexp
2906 * config file, then find the original macaddr in the conf->network, and update
2907 * that to the same value.
2909 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2911 char *lend
, *p
, *p2
;
2912 struct lxc_list
*it
;
2913 char *lstart
= conf
->unexpanded_config
;
2915 if (!conf
->unexpanded_config
)
2919 char newhwaddr
[18], oldhwaddr
[17];
2921 lend
= strchr(lstart
, '\n');
2923 lend
= lstart
+ strlen(lstart
);
2927 if (!lxc_config_net_hwaddr(lstart
)) {
2932 p
= strchr(lstart
, '=');
2945 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
2948 if ((p2
- p
) != 17) {
2949 WARN("Bad hwaddr entry");
2954 memcpy(oldhwaddr
, p
, 17);
2956 if (!new_hwaddr(newhwaddr
))
2959 memcpy(p
, newhwaddr
, 17);
2960 lxc_list_for_each(it
, &conf
->network
) {
2961 struct lxc_netdev
*n
= it
->elem
;
2963 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
2964 memcpy(n
->hwaddr
, newhwaddr
, 17);
2973 static int set_config_ephemeral(const char *key
, const char *value
,
2974 struct lxc_conf
*lxc_conf
, void *data
)
2976 if (lxc_config_value_empty(value
)) {
2977 lxc_conf
->ephemeral
= 0;
2981 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
2984 if (lxc_conf
->ephemeral
> 1)
2990 static int set_config_log_syslog(const char *key
, const char *value
,
2991 struct lxc_conf
*lxc_conf
, void *data
)
2995 if (lxc_conf
->syslog
) {
2996 free(lxc_conf
->syslog
);
2997 lxc_conf
->syslog
= NULL
;
3000 if (lxc_config_value_empty(value
))
3003 facility
= lxc_syslog_priority_to_int(value
);
3004 if (facility
== -EINVAL
)
3007 lxc_log_syslog(facility
);
3008 return set_config_string_item(&lxc_conf
->syslog
, value
);
3011 static int set_config_no_new_privs(const char *key
, const char *value
,
3012 struct lxc_conf
*lxc_conf
, void *data
)
3016 if (lxc_config_value_empty(value
)) {
3017 lxc_conf
->no_new_privs
= false;
3021 if (lxc_safe_uint(value
, &v
) < 0)
3027 lxc_conf
->no_new_privs
= v
? true : false;
3032 /* Callbacks to get configuration items. */
3033 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3034 struct lxc_conf
*c
, void *data
)
3041 memset(retv
, 0, inlen
);
3043 #if HAVE_SYS_PERSONALITY_H
3046 switch (c
->personality
) {
3048 strprint(retv
, inlen
, "i686");
3051 strprint(retv
, inlen
, "x86_64");
3061 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3062 struct lxc_conf
*c
, void *data
)
3064 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3067 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3068 struct lxc_conf
*c
, void *data
)
3070 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3073 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3074 struct lxc_conf
*c
, void *data
)
3076 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3079 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3080 struct lxc_conf
*c
, void *data
)
3082 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3085 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3086 int inlen
, struct lxc_conf
*c
,
3089 return lxc_get_conf_int(c
, retv
, inlen
,
3090 c
->lsm_aa_allow_incomplete
);
3093 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3094 int inlen
, struct lxc_conf
*c
,
3097 return lxc_get_conf_int(c
, retv
, inlen
,
3098 c
->lsm_aa_allow_nesting
);
3101 static int get_config_apparmor_raw(const char *key
, char *retv
,
3102 int inlen
, struct lxc_conf
*c
,
3106 struct lxc_list
*it
;
3112 memset(retv
, 0, inlen
);
3114 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3115 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3121 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3122 struct lxc_conf
*c
, void *data
)
3124 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3127 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3128 * just the value(s) will be printed. Since there still could be more than one,
3129 * it is newline-separated.
3130 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
3132 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3133 * 'lxc.cgroup.subsystem.key = value' format.
3135 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3136 int inlen
, struct lxc_conf
*c
,
3140 size_t namespaced_token_len
;
3141 char *global_token
, *namespaced_token
;
3142 struct lxc_list
*it
;
3144 bool get_all
= false;
3149 memset(retv
, 0, inlen
);
3151 if (version
== CGROUP2_SUPER_MAGIC
) {
3152 global_token
= "lxc.cgroup2";
3153 namespaced_token
= "lxc.cgroup2.";
3154 namespaced_token_len
= sizeof("lxc.cgroup2.") - 1;;
3155 } else if (version
== CGROUP_SUPER_MAGIC
) {
3156 global_token
= "lxc.cgroup";
3157 namespaced_token
= "lxc.cgroup.";
3158 namespaced_token_len
= sizeof("lxc.cgroup.") - 1;;
3163 if (strcmp(key
, global_token
) == 0)
3165 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3166 key
+= namespaced_token_len
;
3170 lxc_list_for_each(it
, &c
->cgroup
) {
3171 struct lxc_cgroup
*cg
= it
->elem
;
3174 if (version
!= cg
->version
)
3177 strprint(retv
, inlen
, "%s.%s = %s\n",
3178 global_token
, cg
->subsystem
, cg
->value
);
3179 } else if (!strcmp(cg
->subsystem
, key
)) {
3180 strprint(retv
, inlen
, "%s\n", cg
->value
);
3187 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3188 struct lxc_conf
*c
, void *data
)
3190 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3191 CGROUP_SUPER_MAGIC
);
3194 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3195 struct lxc_conf
*c
, void *data
)
3197 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3198 CGROUP2_SUPER_MAGIC
);
3201 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3202 struct lxc_conf
*lxc_conf
, void *data
)
3210 memset(retv
, 0, inlen
);
3212 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3217 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3218 struct lxc_conf
*c
, void *data
)
3220 struct lxc_list
*it
;
3221 int len
, listlen
, ret
;
3223 /* "u 1000 1000000 65536"
3225 * let's render this as
3243 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3244 char buf
[__LXC_IDMAP_STR_BUF
];
3249 memset(retv
, 0, inlen
);
3251 listlen
= lxc_list_len(&c
->id_map
);
3252 lxc_list_for_each(it
, &c
->id_map
)
3254 struct id_map
*map
= it
->elem
;
3255 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3256 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3257 map
->nsid
, map
->hostid
, map
->range
);
3258 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3261 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3266 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3267 struct lxc_conf
*c
, void *data
)
3270 v
= lxc_log_priority_to_string(c
->loglevel
);
3271 return lxc_get_conf_str(retv
, inlen
, v
);
3274 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3275 struct lxc_conf
*c
, void *data
)
3277 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3280 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3281 struct lxc_conf
*c
, void *data
)
3283 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3286 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3287 struct lxc_conf
*c
, void *data
)
3289 int len
, fulllen
= 0;
3290 const char *sep
= "";
3295 memset(retv
, 0, inlen
);
3297 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3300 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3301 case LXC_AUTO_PROC_MIXED
:
3302 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3305 case LXC_AUTO_PROC_RW
:
3306 strprint(retv
, inlen
, "%sproc:rw", sep
);
3313 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3314 case LXC_AUTO_SYS_RO
:
3315 strprint(retv
, inlen
, "%ssys:ro", sep
);
3318 case LXC_AUTO_SYS_RW
:
3319 strprint(retv
, inlen
, "%ssys:rw", sep
);
3322 case LXC_AUTO_SYS_MIXED
:
3323 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3330 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3331 case LXC_AUTO_CGROUP_NOSPEC
:
3332 strprint(retv
, inlen
, "%scgroup", sep
);
3334 case LXC_AUTO_CGROUP_MIXED
:
3335 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3337 case LXC_AUTO_CGROUP_RO
:
3338 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3340 case LXC_AUTO_CGROUP_RW
:
3341 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3343 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3344 strprint(retv
, inlen
, "%scgroup-full", sep
);
3346 case LXC_AUTO_CGROUP_FULL_MIXED
:
3347 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3349 case LXC_AUTO_CGROUP_FULL_RO
:
3350 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3352 case LXC_AUTO_CGROUP_FULL_RW
:
3353 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3362 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3363 struct lxc_conf
*c
, void *data
)
3365 int len
, fulllen
= 0;
3366 struct lxc_list
*it
;
3371 memset(retv
, 0, inlen
);
3373 lxc_list_for_each(it
, &c
->mount_list
) {
3374 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3380 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3381 struct lxc_conf
*c
, void *data
)
3383 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3386 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3387 struct lxc_conf
*c
, void *data
)
3389 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3392 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3393 struct lxc_conf
*c
, void *data
)
3395 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3398 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3399 struct lxc_conf
*c
, void *data
)
3401 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3404 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3405 struct lxc_conf
*c
, void *data
)
3407 return lxc_get_conf_str(
3409 c
->utsname
? c
->utsname
->nodename
: NULL
);
3412 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3413 struct lxc_conf
*c
, void *data
)
3416 int len
, fulllen
= 0, found
= -1;
3417 struct lxc_list
*it
;
3420 subkey
= strchr(key
, '.');
3422 subkey
= strchr(subkey
+ 1, '.');
3428 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3429 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3440 memset(retv
, 0, inlen
);
3442 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3443 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3448 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3449 struct lxc_conf
*c
, void *data
)
3451 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3454 static int get_config_net(const char *key
, char *retv
, int inlen
,
3455 struct lxc_conf
*c
, void *data
)
3457 int len
, fulllen
= 0;
3458 struct lxc_list
*it
;
3463 memset(retv
, 0, inlen
);
3465 lxc_list_for_each(it
, &c
->network
) {
3466 struct lxc_netdev
*n
= it
->elem
;
3467 const char *t
= lxc_net_type_to_str(n
->type
);
3468 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3474 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3475 struct lxc_conf
*c
, void *data
)
3477 int len
, fulllen
= 0;
3478 struct lxc_list
*it
;
3483 memset(retv
, 0, inlen
);
3485 lxc_list_for_each(it
, &c
->caps
) {
3486 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3492 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3493 struct lxc_conf
*c
, void *data
)
3495 int len
, fulllen
= 0;
3496 struct lxc_list
*it
;
3501 memset(retv
, 0, inlen
);
3503 lxc_list_for_each(it
, &c
->keepcaps
) {
3504 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3510 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3511 struct lxc_conf
*c
, void *data
)
3513 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3516 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3517 struct lxc_conf
*c
, void *data
)
3519 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3522 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3523 struct lxc_conf
*c
, void *data
)
3525 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3529 static int get_config_console_buffer_size(const char *key
, char *retv
,
3530 int inlen
, struct lxc_conf
*c
,
3533 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3536 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3537 struct lxc_conf
*c
, void *data
)
3539 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3543 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3544 struct lxc_conf
*c
, void *data
)
3546 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3549 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3550 struct lxc_conf
*c
, void *data
)
3552 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3555 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3556 struct lxc_conf
*c
, void *data
)
3558 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3561 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3562 struct lxc_conf
*c
, void *data
)
3564 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3567 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3568 struct lxc_conf
*c
, void *data
)
3570 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3573 static int get_config_start(const char *key
, char *retv
, int inlen
,
3574 struct lxc_conf
*c
, void *data
)
3576 if (strcmp(key
+ 10, "auto") == 0)
3577 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3578 else if (strcmp(key
+ 10, "delay") == 0)
3579 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3580 else if (strcmp(key
+ 10, "order") == 0)
3581 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3586 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3587 struct lxc_conf
*c
, void *data
)
3589 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3592 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3593 struct lxc_conf
*c
, void *data
)
3595 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3598 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
3599 int inlen
, struct lxc_conf
*c
,
3602 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
3605 static int get_config_group(const char *key
, char *retv
, int inlen
,
3606 struct lxc_conf
*c
, void *data
)
3608 int len
, fulllen
= 0;
3609 struct lxc_list
*it
;
3614 memset(retv
, 0, inlen
);
3616 lxc_list_for_each(it
, &c
->groups
) {
3617 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3623 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3624 struct lxc_conf
*c
, void *data
)
3626 int len
, fulllen
= 0;
3627 struct lxc_list
*it
;
3632 memset(retv
, 0, inlen
);
3634 lxc_list_for_each(it
, &c
->environment
) {
3635 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3641 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3642 struct lxc_conf
*c
, void *data
)
3644 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3647 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3648 struct lxc_conf
*c
, void *data
)
3650 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3653 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3654 struct lxc_conf
*c
, void *data
)
3656 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3659 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3660 struct lxc_conf
*c
, void *data
)
3662 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3665 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3666 struct lxc_conf
*c
, void *data
)
3668 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3671 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3672 struct lxc_conf
*c
, void *data
)
3674 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3677 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3678 struct lxc_conf
*c
, void *data
)
3680 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3683 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3684 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3685 * printed, in 'lxc.prlimit.resource = value' format.
3687 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3688 struct lxc_conf
*c
, void *data
)
3690 int fulllen
= 0, len
;
3691 bool get_all
= false;
3692 struct lxc_list
*it
;
3697 memset(retv
, 0, inlen
);
3699 if (!strcmp(key
, "lxc.prlimit"))
3701 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3706 lxc_list_for_each(it
, &c
->limits
) {
3707 char buf
[LXC_NUMSTRLEN64
* 2 + 2]; /* 2 colon separated 64 bit
3708 integers or the word
3711 struct lxc_limit
*lim
= it
->elem
;
3713 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3714 memcpy(buf
, "unlimited", sizeof("unlimited"));
3715 partlen
= sizeof("unlimited") - 1;
3717 partlen
= sprintf(buf
, "%" PRIu64
,
3718 (uint64_t)lim
->limit
.rlim_cur
);
3720 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3721 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3722 memcpy(buf
+ partlen
, ":unlimited",
3723 sizeof(":unlimited"));
3725 sprintf(buf
+ partlen
, ":%" PRIu64
,
3726 (uint64_t)lim
->limit
.rlim_max
);
3730 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3731 lim
->resource
, buf
);
3732 } else if (!strcmp(lim
->resource
, key
)) {
3733 strprint(retv
, inlen
, "%s", buf
);
3740 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3741 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3742 * entries will be printed, in 'lxc.sysctl.key = value' format.
3744 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3745 struct lxc_conf
*c
, void *data
)
3748 struct lxc_list
*it
;
3750 bool get_all
= false;
3755 memset(retv
, 0, inlen
);
3757 if (strcmp(key
, "lxc.sysctl") == 0)
3759 else if (strncmp(key
, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3760 key
+= sizeof("lxc.sysctl.") - 1;
3764 lxc_list_for_each(it
, &c
->sysctls
) {
3765 struct lxc_sysctl
*elem
= it
->elem
;
3767 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n",
3768 elem
->key
, elem
->value
);
3769 } else if (strcmp(elem
->key
, key
) == 0) {
3770 strprint(retv
, inlen
, "%s", elem
->value
);
3777 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3778 struct lxc_conf
*c
, void *data
)
3780 struct lxc_list
*it
;
3783 bool get_all
= false;
3788 memset(retv
, 0, inlen
);
3790 if (strcmp(key
, "lxc.proc") == 0)
3792 else if (strncmp(key
, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3793 key
+= sizeof("lxc.proc.") - 1;
3797 lxc_list_for_each(it
, &c
->procs
) {
3798 struct lxc_proc
*proc
= it
->elem
;
3801 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3802 proc
->filename
, proc
->value
);
3803 } else if (strcmp(proc
->filename
, key
) == 0) {
3804 strprint(retv
, inlen
, "%s", proc
->value
);
3811 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3812 struct lxc_conf
*c
, void *data
)
3820 memset(retv
, 0, inlen
);
3822 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3823 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3824 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3830 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3831 struct lxc_conf
*c
, void *data
)
3839 memset(retv
, 0, inlen
);
3841 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3842 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3843 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3849 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3850 struct lxc_conf
*c
, void *data
)
3853 const char *namespace;
3859 memset(retv
, 0, inlen
);
3861 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
3862 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3866 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3871 /* Callbacks to clear config items. */
3872 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3875 c
->personality
= -1;
3879 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3886 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3893 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3901 static inline int clr_config_apparmor_profile(const char *key
,
3902 struct lxc_conf
*c
, void *data
)
3904 free(c
->lsm_aa_profile
);
3905 c
->lsm_aa_profile
= NULL
;
3909 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3913 c
->lsm_aa_allow_incomplete
= 0;
3917 static inline int clr_config_apparmor_allow_nesting(const char *key
,
3921 c
->lsm_aa_allow_nesting
= 0;
3925 static inline int clr_config_apparmor_raw(const char *key
,
3929 return lxc_clear_apparmor_raw(c
);
3932 static inline int clr_config_selinux_context(const char *key
,
3933 struct lxc_conf
*c
, void *data
)
3935 free(c
->lsm_se_context
);
3936 c
->lsm_se_context
= NULL
;
3940 static inline int clr_config_cgroup_controller(const char *key
,
3941 struct lxc_conf
*c
, void *data
)
3943 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
3946 static inline int clr_config_cgroup2_controller(const char *key
,
3947 struct lxc_conf
*c
, void *data
)
3949 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
3952 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
3955 if (lxc_conf
->cgroup_meta
.dir
) {
3956 free(lxc_conf
->cgroup_meta
.dir
);
3957 lxc_conf
->cgroup_meta
.dir
= NULL
;
3963 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
3966 return lxc_clear_idmaps(c
);
3969 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
3972 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
3976 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
3984 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
3987 return lxc_clear_mount_entries(c
);
3990 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
3993 return lxc_clear_automounts(c
);
3996 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4004 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4007 free(c
->rootfs
.path
);
4008 c
->rootfs
.path
= NULL
;
4012 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4015 c
->rootfs
.managed
= true;
4019 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4022 free(c
->rootfs
.mount
);
4023 c
->rootfs
.mount
= NULL
;
4027 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4030 free(c
->rootfs
.options
);
4031 c
->rootfs
.options
= NULL
;
4033 free(c
->rootfs
.data
);
4034 c
->rootfs
.data
= NULL
;
4039 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4047 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4050 return lxc_clear_hooks(c
, key
);
4053 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4056 /* default to legacy hooks version */
4057 c
->hooks_version
= 0;
4061 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4064 lxc_free_networks(&c
->network
);
4069 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4072 return lxc_clear_config_caps(c
);
4075 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4078 return lxc_clear_config_keepcaps(c
);
4081 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4084 free(c
->console
.path
);
4085 c
->console
.path
= NULL
;
4089 static inline int clr_config_console_logfile(const char *key
,
4090 struct lxc_conf
*c
, void *data
)
4092 free(c
->console
.log_path
);
4093 c
->console
.log_path
= NULL
;
4097 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4100 c
->console
.log_rotate
= 0;
4104 static inline int clr_config_console_buffer_size(const char *key
,
4105 struct lxc_conf
*c
, void *data
)
4107 c
->console
.buffer_size
= 0;
4111 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4114 c
->console
.log_size
= 0;
4118 static inline int clr_config_seccomp_profile(const char *key
,
4119 struct lxc_conf
*c
, void *data
)
4126 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4133 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4140 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4143 c
->rebootsignal
= 0;
4147 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4154 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4157 if (strcmp(key
+ 10, "auto") == 0)
4159 else if (strcmp(key
+ 10, "delay") == 0)
4161 else if (strcmp(key
+ 10, "order") == 0)
4167 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4175 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4178 c
->monitor_unshare
= 0;
4182 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4183 struct lxc_conf
*c
, void *data
)
4185 c
->monitor_signal_pdeath
= 0;
4189 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4192 return lxc_clear_groups(c
);
4195 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4198 return lxc_clear_environment(c
);
4201 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4204 free(c
->execute_cmd
);
4205 c
->execute_cmd
= NULL
;
4209 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4217 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4225 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4232 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4239 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4246 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4249 c
->no_new_privs
= false;
4253 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4256 return lxc_clear_limits(c
, key
);
4259 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4262 return lxc_clear_sysctls(c
, key
);
4265 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4268 return lxc_clear_procs(c
, key
);
4271 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4274 lxc_clear_includes(c
);
4278 static int clr_config_namespace_clone(const char *key
,
4279 struct lxc_conf
*lxc_conf
, void *data
)
4281 lxc_conf
->ns_clone
= 0;
4285 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4288 lxc_conf
->ns_keep
= 0;
4292 static int clr_config_namespace_share(const char *key
,
4293 struct lxc_conf
*lxc_conf
, void *data
)
4296 const char *namespace;
4298 namespace = key
+ sizeof("lxc.namespace.share.") - 1;
4299 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4303 free(lxc_conf
->ns_share
[ns_idx
]);
4304 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4309 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4310 struct lxc_conf
*c
, void *data
)
4315 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4316 struct lxc_conf
*lxc_conf
,
4318 char **deindexed_key
)
4321 unsigned int tmpidx
;
4323 char *copy
, *idx_start
, *idx_end
;
4324 struct lxc_config_t
*config
= NULL
;
4326 /* check that this is a sensible network key */
4327 if (strncmp("lxc.net.", key
, 8)) {
4328 ERROR("Invalid network configuration key \"%s\"", key
);
4334 ERROR("Failed to duplicate string \"%s\"", key
);
4339 if (!isdigit(*(key
+ 8))) {
4340 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4344 /* beginning of index string */
4345 idx_start
= (copy
+ 7);
4348 /* end of index string */
4349 idx_end
= strchr((copy
+ 8), '.');
4353 /* parse current index */
4354 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4357 SYSERROR("Failed to parse usigned integer from string \"%s\"",
4363 /* This, of course is utterly nonsensical on so many levels, but
4364 * better safe than sorry.
4365 * (Checking for INT_MAX here is intentional.)
4367 if (tmpidx
== INT_MAX
) {
4368 SYSERROR("Number of configured networks would overflow the "
4374 numstrlen
= strlen((idx_start
+ 1));
4376 /* repair configuration key */
4379 /* lxc.net.<idx>.<subkey> */
4382 if (strlen(idx_end
+ 1) == 0) {
4383 ERROR("No subkey in network configuration key \"%s\"", key
);
4387 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4388 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4390 config
= lxc_get_config(copy
);
4392 ERROR("Unknown network configuration key \"%s\"", key
);
4398 *deindexed_key
= copy
;
4407 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4408 * found. So we make sure next comes an integer, find the right callback (by
4409 * rewriting the key), and call it.
4411 static int set_config_net_nic(const char *key
, const char *value
,
4412 struct lxc_conf
*lxc_conf
, void *data
)
4415 const char *idxstring
;
4416 struct lxc_config_t
*config
;
4417 struct lxc_netdev
*netdev
;
4419 char *deindexed_key
= NULL
;
4421 idxstring
= key
+ 8;
4422 if (!isdigit(*idxstring
))
4425 if (lxc_config_value_empty(value
))
4426 return clr_config_net_nic(key
, lxc_conf
, data
);
4428 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4429 if (!config
|| idx
< 0)
4432 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4434 free(deindexed_key
);
4438 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4439 free(deindexed_key
);
4443 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4447 const char *idxstring
;
4448 struct lxc_config_t
*config
;
4449 struct lxc_netdev
*netdev
;
4451 char *deindexed_key
= NULL
;
4453 idxstring
= key
+ 8;
4454 if (!isdigit(*idxstring
))
4457 /* The left conjunct is pretty self-explanatory. The right conjunct
4458 * checks whether the two pointers are equal. If they are we know that
4459 * this is not a key that is namespaced any further and so we are
4460 * supposed to clear the whole network.
4462 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4463 unsigned int rmnetdevidx
;
4465 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4468 /* Remove network from network list. */
4469 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4473 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4474 if (!config
|| idx
< 0)
4477 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4479 free(deindexed_key
);
4483 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4484 free(deindexed_key
);
4488 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4491 struct lxc_netdev
*netdev
= data
;
4501 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4504 struct lxc_netdev
*netdev
= data
;
4509 netdev
->name
[0] = '\0';
4514 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4517 struct lxc_netdev
*netdev
= data
;
4527 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4530 struct lxc_netdev
*netdev
= data
;
4535 netdev
->link
[0] = '\0';
4540 static int clr_config_net_macvlan_mode(const char *key
,
4541 struct lxc_conf
*lxc_conf
, void *data
)
4543 struct lxc_netdev
*netdev
= data
;
4548 if (netdev
->type
!= LXC_NET_MACVLAN
)
4551 netdev
->priv
.macvlan_attr
.mode
= -1;
4556 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4559 struct lxc_netdev
*netdev
= data
;
4564 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4569 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4572 struct lxc_netdev
*netdev
= data
;
4577 free(netdev
->upscript
);
4578 netdev
->upscript
= NULL
;
4583 static int clr_config_net_script_down(const char *key
,
4584 struct lxc_conf
*lxc_conf
, void *data
)
4586 struct lxc_netdev
*netdev
= data
;
4591 free(netdev
->downscript
);
4592 netdev
->downscript
= NULL
;
4597 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4600 struct lxc_netdev
*netdev
= data
;
4605 free(netdev
->hwaddr
);
4606 netdev
->hwaddr
= NULL
;
4611 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4614 struct lxc_netdev
*netdev
= data
;
4625 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4628 struct lxc_netdev
*netdev
= data
;
4633 netdev
->priv
.vlan_attr
.vid
= 0;
4638 static int clr_config_net_ipv4_gateway(const char *key
,
4639 struct lxc_conf
*lxc_conf
, void *data
)
4641 struct lxc_netdev
*netdev
= data
;
4646 free(netdev
->ipv4_gateway
);
4647 netdev
->ipv4_gateway
= NULL
;
4652 static int clr_config_net_ipv4_address(const char *key
,
4653 struct lxc_conf
*lxc_conf
, void *data
)
4655 struct lxc_netdev
*netdev
= data
;
4656 struct lxc_list
*cur
, *next
;
4661 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4670 static int clr_config_net_ipv6_gateway(const char *key
,
4671 struct lxc_conf
*lxc_conf
, void *data
)
4673 struct lxc_netdev
*netdev
= data
;
4678 free(netdev
->ipv6_gateway
);
4679 netdev
->ipv6_gateway
= NULL
;
4684 static int clr_config_net_ipv6_address(const char *key
,
4685 struct lxc_conf
*lxc_conf
, void *data
)
4687 struct lxc_netdev
*netdev
= data
;
4688 struct lxc_list
*cur
, *next
;
4693 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4702 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4703 struct lxc_conf
*c
, void *data
)
4706 const char *idxstring
;
4707 struct lxc_config_t
*config
;
4708 struct lxc_netdev
*netdev
;
4710 char *deindexed_key
= NULL
;
4712 idxstring
= key
+ 8;
4713 if (!isdigit(*idxstring
))
4716 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4717 if (!config
|| idx
< 0)
4720 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4722 free(deindexed_key
);
4726 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4727 free(deindexed_key
);
4731 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4732 struct lxc_conf
*c
, void *data
)
4736 struct lxc_netdev
*netdev
= data
;
4741 memset(retv
, 0, inlen
);
4746 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4751 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4752 struct lxc_conf
*c
, void *data
)
4756 struct lxc_netdev
*netdev
= data
;
4761 memset(retv
, 0, inlen
);
4766 if (netdev
->flags
& IFF_UP
)
4767 strprint(retv
, inlen
, "up");
4772 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4773 struct lxc_conf
*c
, void *data
)
4777 struct lxc_netdev
*netdev
= data
;
4782 memset(retv
, 0, inlen
);
4787 if (netdev
->link
[0] != '\0')
4788 strprint(retv
, inlen
, "%s", netdev
->link
);
4793 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4794 struct lxc_conf
*c
, void *data
)
4798 struct lxc_netdev
*netdev
= data
;
4803 memset(retv
, 0, inlen
);
4808 if (netdev
->name
[0] != '\0')
4809 strprint(retv
, inlen
, "%s", netdev
->name
);
4814 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4815 struct lxc_conf
*c
, void *data
)
4820 struct lxc_netdev
*netdev
= data
;
4825 memset(retv
, 0, inlen
);
4830 if (netdev
->type
!= LXC_NET_MACVLAN
)
4833 switch (netdev
->priv
.macvlan_attr
.mode
) {
4834 case MACVLAN_MODE_PRIVATE
:
4837 case MACVLAN_MODE_VEPA
:
4840 case MACVLAN_MODE_BRIDGE
:
4843 case MACVLAN_MODE_PASSTHRU
:
4851 strprint(retv
, inlen
, "%s", mode
);
4856 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4857 struct lxc_conf
*c
, void *data
)
4861 struct lxc_netdev
*netdev
= data
;
4866 memset(retv
, 0, inlen
);
4871 if (netdev
->type
!= LXC_NET_VETH
)
4874 strprint(retv
, inlen
, "%s",
4875 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4876 ? netdev
->priv
.veth_attr
.pair
4877 : netdev
->priv
.veth_attr
.veth1
);
4882 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4883 struct lxc_conf
*c
, void *data
)
4887 struct lxc_netdev
*netdev
= data
;
4892 memset(retv
, 0, inlen
);
4897 if (netdev
->upscript
)
4898 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4903 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4904 struct lxc_conf
*c
, void *data
)
4908 struct lxc_netdev
*netdev
= data
;
4913 memset(retv
, 0, inlen
);
4918 if (netdev
->downscript
)
4919 strprint(retv
, inlen
, "%s", netdev
->downscript
);
4924 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
4925 struct lxc_conf
*c
, void *data
)
4929 struct lxc_netdev
*netdev
= data
;
4934 memset(retv
, 0, inlen
);
4940 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
4945 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
4946 struct lxc_conf
*c
, void *data
)
4950 struct lxc_netdev
*netdev
= data
;
4955 memset(retv
, 0, inlen
);
4961 strprint(retv
, inlen
, "%s", netdev
->mtu
);
4966 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
4967 struct lxc_conf
*c
, void *data
)
4971 struct lxc_netdev
*netdev
= data
;
4976 memset(retv
, 0, inlen
);
4981 if (netdev
->type
!= LXC_NET_VLAN
)
4984 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
4989 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
4990 struct lxc_conf
*c
, void *data
)
4993 char buf
[INET_ADDRSTRLEN
];
4995 struct lxc_netdev
*netdev
= data
;
5000 memset(retv
, 0, inlen
);
5005 if (netdev
->ipv4_gateway_auto
) {
5006 strprint(retv
, inlen
, "auto");
5007 } else if (netdev
->ipv4_gateway
) {
5008 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5009 strprint(retv
, inlen
, "%s", buf
);
5015 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5016 struct lxc_conf
*c
, void *data
)
5020 char buf
[INET_ADDRSTRLEN
];
5021 struct lxc_list
*it
;
5023 struct lxc_netdev
*netdev
= data
;
5028 memset(retv
, 0, inlen
);
5033 listlen
= lxc_list_len(&netdev
->ipv4
);
5034 lxc_list_for_each(it
, &netdev
->ipv4
) {
5035 struct lxc_inetdev
*i
= it
->elem
;
5036 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5037 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5038 (listlen
-- > 1) ? "\n" : "");
5044 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5045 struct lxc_conf
*c
, void *data
)
5048 char buf
[INET6_ADDRSTRLEN
];
5050 struct lxc_netdev
*netdev
= data
;
5055 memset(retv
, 0, inlen
);
5060 if (netdev
->ipv6_gateway_auto
) {
5061 strprint(retv
, inlen
, "auto");
5062 } else if (netdev
->ipv6_gateway
) {
5063 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5064 strprint(retv
, inlen
, "%s", buf
);
5070 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5071 struct lxc_conf
*c
, void *data
)
5075 char buf
[INET6_ADDRSTRLEN
];
5076 struct lxc_list
*it
;
5078 struct lxc_netdev
*netdev
= data
;
5083 memset(retv
, 0, inlen
);
5088 listlen
= lxc_list_len(&netdev
->ipv6
);
5089 lxc_list_for_each(it
, &netdev
->ipv6
) {
5090 struct lxc_inet6dev
*i
= it
->elem
;
5091 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5092 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5093 (listlen
-- > 1) ? "\n" : "");
5099 int lxc_list_config_items(char *retv
, int inlen
)
5108 memset(retv
, 0, inlen
);
5110 for (i
= 0; i
< config_size
; i
++) {
5111 char *s
= config
[i
].name
;
5113 if (s
[strlen(s
) - 1] == '.')
5116 strprint(retv
, inlen
, "%s\n", s
);
5122 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5131 memset(retv
, 0, inlen
);
5133 if (!strcmp(key
, "lxc.apparmor")) {
5134 strprint(retv
, inlen
, "allow_incomplete\n");
5135 strprint(retv
, inlen
, "allow_nesting\n");
5136 strprint(retv
, inlen
, "profile\n");
5137 strprint(retv
, inlen
, "raw\n");
5138 } else if (!strcmp(key
, "lxc.cgroup")) {
5139 strprint(retv
, inlen
, "dir\n");
5140 } else if (!strcmp(key
, "lxc.selinux")) {
5141 strprint(retv
, inlen
, "context\n");
5142 } else if (!strcmp(key
, "lxc.mount")) {
5143 strprint(retv
, inlen
, "auto\n");
5144 strprint(retv
, inlen
, "entry\n");
5145 strprint(retv
, inlen
, "fstab\n");
5146 } else if (!strcmp(key
, "lxc.rootfs")) {
5147 strprint(retv
, inlen
, "mount\n");
5148 strprint(retv
, inlen
, "options\n");
5149 strprint(retv
, inlen
, "path\n");
5150 } else if (!strcmp(key
, "lxc.uts")) {
5151 strprint(retv
, inlen
, "name\n");
5152 } else if (!strcmp(key
, "lxc.hook")) {
5153 strprint(retv
, inlen
, "autodev\n");
5154 strprint(retv
, inlen
, "clone\n");
5155 strprint(retv
, inlen
, "destroy\n");
5156 strprint(retv
, inlen
, "mount\n");
5157 strprint(retv
, inlen
, "post-stop\n");
5158 strprint(retv
, inlen
, "pre-mount\n");
5159 strprint(retv
, inlen
, "pre-start\n");
5160 strprint(retv
, inlen
, "start-host\n");
5161 strprint(retv
, inlen
, "start\n");
5162 strprint(retv
, inlen
, "stop\n");
5163 } else if (!strcmp(key
, "lxc.cap")) {
5164 strprint(retv
, inlen
, "drop\n");
5165 strprint(retv
, inlen
, "keep\n");
5166 } else if (!strcmp(key
, "lxc.console")) {
5167 strprint(retv
, inlen
, "logfile\n");
5168 strprint(retv
, inlen
, "path\n");
5169 } else if (!strcmp(key
, "lxc.seccomp")) {
5170 strprint(retv
, inlen
, "profile\n");
5171 } else if (!strcmp(key
, "lxc.signal")) {
5172 strprint(retv
, inlen
, "halt\n");
5173 strprint(retv
, inlen
, "reboot\n");
5174 strprint(retv
, inlen
, "stop\n");
5175 } else if (!strcmp(key
, "lxc.start")) {
5176 strprint(retv
, inlen
, "auto\n");
5177 strprint(retv
, inlen
, "delay\n");
5178 strprint(retv
, inlen
, "order\n");
5179 } else if (!strcmp(key
, "lxc.monitor")) {
5180 strprint(retv
, inlen
, "unshare\n");
5188 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5191 const char *idxstring
;
5192 struct lxc_netdev
*netdev
;
5196 idxstring
= key
+ 8;
5197 if (!isdigit(*idxstring
))
5200 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5204 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5211 memset(retv
, 0, inlen
);
5213 strprint(retv
, inlen
, "type\n");
5214 strprint(retv
, inlen
, "script.up\n");
5215 strprint(retv
, inlen
, "script.down\n");
5216 if (netdev
->type
!= LXC_NET_EMPTY
) {
5217 strprint(retv
, inlen
, "flags\n");
5218 strprint(retv
, inlen
, "link\n");
5219 strprint(retv
, inlen
, "name\n");
5220 strprint(retv
, inlen
, "hwaddr\n");
5221 strprint(retv
, inlen
, "mtu\n");
5222 strprint(retv
, inlen
, "ipv6.address\n");
5223 strprint(retv
, inlen
, "ipv6.gateway\n");
5224 strprint(retv
, inlen
, "ipv4.address\n");
5225 strprint(retv
, inlen
, "ipv4.gateway\n");
5228 switch (netdev
->type
) {
5230 strprint(retv
, inlen
, "veth.pair\n");
5232 case LXC_NET_MACVLAN
:
5233 strprint(retv
, inlen
, "macvlan.mode\n");
5236 strprint(retv
, inlen
, "vlan.id\n");