2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
6 * Daniel Lezcano <daniel.lezcano at free.fr>
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 #define __STDC_FORMAT_MACROS
29 #include <arpa/inet.h>
36 #include <netinet/in.h>
41 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/utsname.h>
52 #include "confile_utils.h"
53 #include "../include/netns_ifaddrs.h"
55 #include "lxcseccomp.h"
61 #if HAVE_SYS_PERSONALITY_H
62 #include <sys/personality.h>
66 #include "include/strlcpy.h"
70 #include "include/strlcat.h"
73 lxc_log_define(confile
, lxc
);
75 #define lxc_config_define(name) \
76 __hot static int set_config_##name(const char *, const char *, \
77 struct lxc_conf *, void *); \
78 __hot static int get_config_##name(const char *, char *, int, \
79 struct lxc_conf *, void *); \
80 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
83 lxc_config_define(autodev
);
84 lxc_config_define(apparmor_allow_incomplete
);
85 lxc_config_define(apparmor_allow_nesting
);
86 lxc_config_define(apparmor_profile
);
87 lxc_config_define(apparmor_raw
);
88 lxc_config_define(cap_drop
);
89 lxc_config_define(cap_keep
);
90 lxc_config_define(cgroup_controller
);
91 lxc_config_define(cgroup2_controller
);
92 lxc_config_define(cgroup_dir
);
93 lxc_config_define(cgroup_relative
);
94 lxc_config_define(console_buffer_size
);
95 lxc_config_define(console_logfile
);
96 lxc_config_define(console_path
);
97 lxc_config_define(console_rotate
);
98 lxc_config_define(console_size
);
99 lxc_config_define(environment
);
100 lxc_config_define(ephemeral
);
101 lxc_config_define(execute_cmd
);
102 lxc_config_define(group
);
103 lxc_config_define(hooks
);
104 lxc_config_define(hooks_version
);
105 lxc_config_define(idmaps
);
106 lxc_config_define(includefiles
);
107 lxc_config_define(init_cmd
);
108 lxc_config_define(init_cwd
);
109 lxc_config_define(init_gid
);
110 lxc_config_define(init_uid
);
111 lxc_config_define(log_file
);
112 lxc_config_define(log_level
);
113 lxc_config_define(log_syslog
);
114 lxc_config_define(monitor
);
115 lxc_config_define(monitor_signal_pdeath
);
116 lxc_config_define(mount
);
117 lxc_config_define(mount_auto
);
118 lxc_config_define(mount_fstab
);
119 lxc_config_define(namespace_clone
);
120 lxc_config_define(namespace_keep
);
121 lxc_config_define(namespace_share
);
122 lxc_config_define(net
);
123 lxc_config_define(net_flags
);
124 lxc_config_define(net_hwaddr
);
125 lxc_config_define(net_ipv4_address
);
126 lxc_config_define(net_ipv4_gateway
);
127 lxc_config_define(net_ipv6_address
);
128 lxc_config_define(net_ipv6_gateway
);
129 lxc_config_define(net_link
);
130 lxc_config_define(net_macvlan_mode
);
131 lxc_config_define(net_mtu
);
132 lxc_config_define(net_name
);
133 lxc_config_define(net_nic
);
134 lxc_config_define(net_script_down
);
135 lxc_config_define(net_script_up
);
136 lxc_config_define(net_type
);
137 lxc_config_define(net_veth_pair
);
138 lxc_config_define(net_vlan_id
);
139 lxc_config_define(no_new_privs
);
140 lxc_config_define(personality
);
141 lxc_config_define(prlimit
);
142 lxc_config_define(pty_max
);
143 lxc_config_define(rootfs_managed
);
144 lxc_config_define(rootfs_mount
);
145 lxc_config_define(rootfs_options
);
146 lxc_config_define(rootfs_path
);
147 lxc_config_define(seccomp_profile
);
148 lxc_config_define(selinux_context
);
149 lxc_config_define(signal_halt
);
150 lxc_config_define(signal_reboot
);
151 lxc_config_define(signal_stop
);
152 lxc_config_define(start
);
153 lxc_config_define(tty_max
);
154 lxc_config_define(tty_dir
);
155 lxc_config_define(uts_name
);
156 lxc_config_define(sysctl
);
157 lxc_config_define(proc
);
159 static struct lxc_config_t config_jump_table
[] = {
160 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
161 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
162 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
163 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
164 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
165 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
166 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
167 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
168 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
169 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
170 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
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_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
251 struct lxc_config_t
*lxc_get_config(const char *key
)
255 for (i
= 0; i
< config_jump_table_size
; i
++)
256 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
257 return &config_jump_table
[i
];
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 netns_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 (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0) {
333 SYSERROR("Failed to get network interfaces");
337 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
341 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
344 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
345 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
348 ret
= set_config_net_link(
349 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
351 ERROR("Failed to create matched ifnames");
355 ERROR("Failed to create matched ifnames");
361 netns_freeifaddrs(ifaddr
);
367 static int set_config_net_link(const char *key
, const char *value
,
368 struct lxc_conf
*lxc_conf
, void *data
)
370 struct lxc_netdev
*netdev
= data
;
373 if (lxc_config_value_empty(value
))
374 return clr_config_net_link(key
, lxc_conf
, data
);
379 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
380 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
382 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
387 static int set_config_net_name(const char *key
, const char *value
,
388 struct lxc_conf
*lxc_conf
, void *data
)
390 struct lxc_netdev
*netdev
= data
;
392 if (lxc_config_value_empty(value
))
393 return clr_config_net_name(key
, lxc_conf
, data
);
398 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
401 static int set_config_net_veth_pair(const char *key
, const char *value
,
402 struct lxc_conf
*lxc_conf
, void *data
)
404 struct lxc_netdev
*netdev
= data
;
406 if (lxc_config_value_empty(value
))
407 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
412 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
415 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
416 struct lxc_conf
*lxc_conf
, void *data
)
418 struct lxc_netdev
*netdev
= data
;
420 if (lxc_config_value_empty(value
))
421 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
426 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
429 static int set_config_net_hwaddr(const char *key
, const char *value
,
430 struct lxc_conf
*lxc_conf
, void *data
)
432 struct lxc_netdev
*netdev
= data
;
435 if (lxc_config_value_empty(value
))
436 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
441 new_value
= strdup(value
);
445 rand_complete_hwaddr(new_value
);
447 if (lxc_config_value_empty(new_value
)) {
449 netdev
->hwaddr
= NULL
;
453 netdev
->hwaddr
= new_value
;
458 static int set_config_net_vlan_id(const char *key
, const char *value
,
459 struct lxc_conf
*lxc_conf
, void *data
)
462 struct lxc_netdev
*netdev
= data
;
464 if (lxc_config_value_empty(value
))
465 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
470 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
477 static int set_config_net_mtu(const char *key
, const char *value
,
478 struct lxc_conf
*lxc_conf
, void *data
)
480 struct lxc_netdev
*netdev
= data
;
482 if (lxc_config_value_empty(value
))
483 return clr_config_net_mtu(key
, lxc_conf
, data
);
488 return set_config_string_item(&netdev
->mtu
, value
);
491 static int set_config_net_ipv4_address(const char *key
, const char *value
,
492 struct lxc_conf
*lxc_conf
, void *data
)
495 struct lxc_netdev
*netdev
= data
;
496 struct lxc_inetdev
*inetdev
;
497 struct lxc_list
*list
;
498 char *cursor
, *slash
;
499 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
501 if (lxc_config_value_empty(value
))
502 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
507 inetdev
= malloc(sizeof(*inetdev
));
510 memset(inetdev
, 0, sizeof(*inetdev
));
512 list
= malloc(sizeof(*list
));
519 list
->elem
= inetdev
;
521 addr
= strdup(value
);
528 cursor
= strstr(addr
, " ");
534 slash
= strstr(addr
, "/");
540 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
541 if (!ret
|| ret
< 0) {
542 SYSERROR("Invalid ipv4 address \"%s\"", value
);
550 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
551 if (!ret
|| ret
< 0) {
552 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
561 /* No prefix specified, determine it from the network class. */
563 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
571 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
574 /* If no broadcast address, let compute one from the
575 * prefix and address.
578 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
579 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
582 lxc_list_add_tail(&netdev
->ipv4
, list
);
588 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
589 struct lxc_conf
*lxc_conf
, void *data
)
591 struct lxc_netdev
*netdev
= data
;
593 if (lxc_config_value_empty(value
))
594 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
599 free(netdev
->ipv4_gateway
);
601 if (!strcmp(value
, "auto")) {
602 netdev
->ipv4_gateway
= NULL
;
603 netdev
->ipv4_gateway_auto
= true;
608 gw
= malloc(sizeof(*gw
));
612 ret
= inet_pton(AF_INET
, value
, gw
);
613 if (!ret
|| ret
< 0) {
614 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
619 netdev
->ipv4_gateway
= gw
;
620 netdev
->ipv4_gateway_auto
= false;
626 static int set_config_net_ipv6_address(const char *key
, const char *value
,
627 struct lxc_conf
*lxc_conf
, void *data
)
630 struct lxc_netdev
*netdev
= data
;
631 struct lxc_inet6dev
*inet6dev
;
632 struct lxc_list
*list
;
633 char *slash
, *valdup
, *netmask
;
635 if (lxc_config_value_empty(value
))
636 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
641 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
, "/");
668 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
677 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
678 if (!ret
|| ret
< 0) {
679 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
686 lxc_list_add_tail(&netdev
->ipv6
, list
);
692 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
693 struct lxc_conf
*lxc_conf
, void *data
)
695 struct lxc_netdev
*netdev
= data
;
697 if (lxc_config_value_empty(value
))
698 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
703 free(netdev
->ipv6_gateway
);
705 if (!strcmp(value
, "auto")) {
706 netdev
->ipv6_gateway
= NULL
;
707 netdev
->ipv6_gateway_auto
= true;
712 gw
= malloc(sizeof(*gw
));
716 ret
= inet_pton(AF_INET6
, value
, gw
);
717 if (!ret
|| ret
< 0) {
718 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
723 netdev
->ipv6_gateway
= gw
;
724 netdev
->ipv6_gateway_auto
= false;
730 static int set_config_net_script_up(const char *key
, const char *value
,
731 struct lxc_conf
*lxc_conf
, void *data
)
733 struct lxc_netdev
*netdev
= data
;
735 if (lxc_config_value_empty(value
))
736 return clr_config_net_script_up(key
, lxc_conf
, data
);
741 return set_config_string_item(&netdev
->upscript
, value
);
744 static int set_config_net_script_down(const char *key
, const char *value
,
745 struct lxc_conf
*lxc_conf
, void *data
)
747 struct lxc_netdev
*netdev
= data
;
749 if (lxc_config_value_empty(value
))
750 return clr_config_net_script_down(key
, lxc_conf
, data
);
755 return set_config_string_item(&netdev
->downscript
, value
);
758 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
760 struct lxc_list
*hooklist
;
762 hooklist
= malloc(sizeof(*hooklist
));
768 hooklist
->elem
= hook
;
769 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
774 static int set_config_seccomp_profile(const char *key
, const char *value
,
775 struct lxc_conf
*lxc_conf
, void *data
)
777 return set_config_path_item(&lxc_conf
->seccomp
, value
);
780 static int set_config_execute_cmd(const char *key
, const char *value
,
781 struct lxc_conf
*lxc_conf
, void *data
)
783 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
786 static int set_config_init_cmd(const char *key
, const char *value
,
787 struct lxc_conf
*lxc_conf
, void *data
)
789 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
792 static int set_config_init_cwd(const char *key
, const char *value
,
793 struct lxc_conf
*lxc_conf
, void *data
)
795 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
798 static int set_config_init_uid(const char *key
, const char *value
,
799 struct lxc_conf
*lxc_conf
, void *data
)
801 unsigned int init_uid
;
803 if (lxc_config_value_empty(value
)) {
804 lxc_conf
->init_uid
= 0;
808 if (lxc_safe_uint(value
, &init_uid
) < 0)
811 lxc_conf
->init_uid
= init_uid
;
816 static int set_config_init_gid(const char *key
, const char *value
,
817 struct lxc_conf
*lxc_conf
, void *data
)
819 unsigned int init_gid
;
821 if (lxc_config_value_empty(value
)) {
822 lxc_conf
->init_gid
= 0;
826 if (lxc_safe_uint(value
, &init_gid
) < 0)
829 lxc_conf
->init_gid
= init_gid
;
834 static int set_config_hooks(const char *key
, const char *value
,
835 struct lxc_conf
*lxc_conf
, void *data
)
839 if (lxc_config_value_empty(value
))
840 return lxc_clear_hooks(lxc_conf
, key
);
842 if (strcmp(key
+ 4, "hook") == 0) {
843 ERROR("lxc.hook must not have a value");
847 copy
= strdup(value
);
851 if (strcmp(key
+ 9, "pre-start") == 0)
852 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
853 else if (strcmp(key
+ 9, "start-host") == 0)
854 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
855 else if (strcmp(key
+ 9, "pre-mount") == 0)
856 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
857 else if (strcmp(key
+ 9, "autodev") == 0)
858 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
859 else if (strcmp(key
+ 9, "mount") == 0)
860 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
861 else if (strcmp(key
+ 9, "start") == 0)
862 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
863 else if (strcmp(key
+ 9, "stop") == 0)
864 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
865 else if (strcmp(key
+ 9, "post-stop") == 0)
866 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
867 else if (strcmp(key
+ 9, "clone") == 0)
868 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
869 else if (strcmp(key
+ 9, "destroy") == 0)
870 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
877 static int set_config_hooks_version(const char *key
, const char *value
,
878 struct lxc_conf
*lxc_conf
, void *data
)
883 if (lxc_config_value_empty(value
))
884 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
886 ret
= lxc_safe_uint(value
, &tmp
);
891 ERROR("Invalid hook version specified. Currently only 0 "
892 "(legacy) and 1 are supported");
896 lxc_conf
->hooks_version
= tmp
;
901 static int set_config_personality(const char *key
, const char *value
,
902 struct lxc_conf
*lxc_conf
, void *data
)
904 signed long personality
= lxc_config_parse_arch(value
);
906 if (personality
>= 0)
907 lxc_conf
->personality
= personality
;
909 WARN("Unsupported personality \"%s\"", value
);
914 static int set_config_pty_max(const char *key
, const char *value
,
915 struct lxc_conf
*lxc_conf
, void *data
)
918 unsigned int max
= 0;
920 if (lxc_config_value_empty(value
)) {
921 lxc_conf
->pty_max
= 0;
925 ret
= lxc_safe_uint(value
, &max
);
929 lxc_conf
->pty_max
= max
;
934 /* We only need to check whether the first byte of the key after the lxc.start.
935 * prefix matches our expectations since they fortunately all start with a
936 * different letter. If anything was wrong with the key we would have already
937 * noticed when the callback was called.
939 static int set_config_start(const char *key
, const char *value
,
940 struct lxc_conf
*lxc_conf
, void *data
)
944 is_empty
= lxc_config_value_empty(value
);
946 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
948 lxc_conf
->start_auto
= 0;
952 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
955 if (lxc_conf
->start_auto
> 1)
959 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
961 lxc_conf
->start_delay
= 0;
965 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
966 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
968 lxc_conf
->start_order
= 0;
972 return lxc_safe_int(value
, &lxc_conf
->start_order
);
978 static int set_config_monitor(const char *key
, const char *value
,
979 struct lxc_conf
*lxc_conf
, void *data
)
981 if (lxc_config_value_empty(value
)) {
982 lxc_conf
->monitor_unshare
= 0;
986 if (strcmp(key
+ 12, "unshare") == 0)
987 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
992 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
993 struct lxc_conf
*lxc_conf
, void *data
)
995 if (lxc_config_value_empty(value
)) {
996 lxc_conf
->monitor_signal_pdeath
= 0;
1000 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1003 sig_n
= sig_parse(value
);
1007 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1014 static int set_config_group(const char *key
, const char *value
,
1015 struct lxc_conf
*lxc_conf
, void *data
)
1017 char *groups
, *token
;
1018 struct lxc_list
*grouplist
;
1021 if (lxc_config_value_empty(value
))
1022 return lxc_clear_groups(lxc_conf
);
1024 groups
= strdup(value
);
1028 /* In case several groups are specified in a single line split these
1029 * groups in a single element for the list.
1031 lxc_iterate_parts(token
, groups
, " \t") {
1032 grouplist
= malloc(sizeof(*grouplist
));
1038 grouplist
->elem
= strdup(token
);
1039 if (!grouplist
->elem
) {
1045 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1053 static int set_config_environment(const char *key
, const char *value
,
1054 struct lxc_conf
*lxc_conf
, void *data
)
1056 struct lxc_list
*list_item
= NULL
;
1058 if (lxc_config_value_empty(value
))
1059 return lxc_clear_environment(lxc_conf
);
1061 list_item
= malloc(sizeof(*list_item
));
1065 if (!strchr(value
, '=')) {
1066 const char *env_val
;
1067 const char *env_key
= value
;
1068 const char *env_var
[3] = {0};
1070 env_val
= getenv(env_key
);
1074 env_var
[0] = env_key
;
1075 env_var
[1] = env_val
;
1076 list_item
->elem
= lxc_string_join("=", env_var
, false);
1078 list_item
->elem
= strdup(value
);
1081 if (!list_item
->elem
)
1084 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1094 static int set_config_tty_max(const char *key
, const char *value
,
1095 struct lxc_conf
*lxc_conf
, void *data
)
1098 unsigned int nbtty
= 0;
1100 if (lxc_config_value_empty(value
)) {
1101 lxc_conf
->ttys
.max
= 0;
1105 ret
= lxc_safe_uint(value
, &nbtty
);
1109 lxc_conf
->ttys
.max
= nbtty
;
1114 static int set_config_tty_dir(const char *key
, const char *value
,
1115 struct lxc_conf
*lxc_conf
, void *data
)
1117 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1121 static int set_config_apparmor_profile(const char *key
, const char *value
,
1122 struct lxc_conf
*lxc_conf
, void *data
)
1124 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1127 static int set_config_apparmor_allow_incomplete(const char *key
,
1129 struct lxc_conf
*lxc_conf
,
1132 if (lxc_config_value_empty(value
)) {
1133 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1137 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1140 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1146 static int set_config_apparmor_allow_nesting(const char *key
,
1148 struct lxc_conf
*lxc_conf
,
1151 if (lxc_config_value_empty(value
))
1152 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1154 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1157 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1163 static int set_config_apparmor_raw(const char *key
,
1165 struct lxc_conf
*lxc_conf
,
1169 struct lxc_list
*list
;
1171 if (lxc_config_value_empty(value
))
1172 return lxc_clear_apparmor_raw(lxc_conf
);
1174 list
= malloc(sizeof(*list
));
1180 elem
= strdup(value
);
1187 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1192 static int set_config_selinux_context(const char *key
, const char *value
,
1193 struct lxc_conf
*lxc_conf
, void *data
)
1195 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1198 static int set_config_log_file(const char *key
, const char *value
,
1199 struct lxc_conf
*c
, void *data
)
1203 if (lxc_config_value_empty(value
)) {
1209 /* Store these values in the lxc_conf, and then try to set for actual
1212 ret
= set_config_path_item(&c
->logfile
, value
);
1214 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1219 static int set_config_log_level(const char *key
, const char *value
,
1220 struct lxc_conf
*lxc_conf
, void *data
)
1224 if (lxc_config_value_empty(value
)) {
1225 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1229 if (value
[0] >= '0' && value
[0] <= '9') {
1230 if (lxc_safe_int(value
, &newlevel
) < 0)
1233 newlevel
= lxc_log_priority_to_int(value
);
1236 /* Store these values in the lxc_conf, and then try to set for actual
1239 lxc_conf
->loglevel
= newlevel
;
1241 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1244 static int set_config_autodev(const char *key
, const char *value
,
1245 struct lxc_conf
*lxc_conf
, void *data
)
1247 if (lxc_config_value_empty(value
)) {
1248 lxc_conf
->autodev
= 0;
1252 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1255 if (lxc_conf
->autodev
> 1)
1261 static int set_config_signal_halt(const char *key
, const char *value
,
1262 struct lxc_conf
*lxc_conf
, void *data
)
1266 if (lxc_config_value_empty(value
)) {
1267 lxc_conf
->haltsignal
= 0;
1271 sig_n
= sig_parse(value
);
1275 lxc_conf
->haltsignal
= sig_n
;
1280 static int set_config_signal_reboot(const char *key
, const char *value
,
1281 struct lxc_conf
*lxc_conf
, void *data
)
1285 if (lxc_config_value_empty(value
)) {
1286 lxc_conf
->rebootsignal
= 0;
1290 sig_n
= sig_parse(value
);
1294 lxc_conf
->rebootsignal
= sig_n
;
1299 static int set_config_signal_stop(const char *key
, const char *value
,
1300 struct lxc_conf
*lxc_conf
, void *data
)
1304 if (lxc_config_value_empty(value
)) {
1305 lxc_conf
->stopsignal
= 0;
1309 sig_n
= sig_parse(value
);
1313 lxc_conf
->stopsignal
= sig_n
;
1318 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1319 struct lxc_conf
*lxc_conf
, int version
)
1321 const char *subkey
, *token
;
1323 struct lxc_list
*cglist
= NULL
;
1324 struct lxc_cgroup
*cgelem
= NULL
;
1326 if (lxc_config_value_empty(value
))
1327 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1329 if (version
== CGROUP2_SUPER_MAGIC
) {
1330 token
= "lxc.cgroup2.";
1332 } else if (version
== CGROUP_SUPER_MAGIC
) {
1333 token
= "lxc.cgroup.";
1339 if (strncmp(key
, token
, token_len
) != 0)
1342 subkey
= key
+ token_len
;
1343 if (*subkey
== '\0')
1346 cglist
= malloc(sizeof(*cglist
));
1350 cgelem
= malloc(sizeof(*cgelem
));
1353 memset(cgelem
, 0, sizeof(*cgelem
));
1355 cgelem
->subsystem
= strdup(subkey
);
1356 if (!cgelem
->subsystem
)
1359 cgelem
->value
= strdup(value
);
1363 cgelem
->version
= version
;
1365 lxc_list_add_elem(cglist
, cgelem
);
1367 if (version
== CGROUP2_SUPER_MAGIC
)
1368 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1370 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1377 free(cgelem
->subsystem
);
1378 free(cgelem
->value
);
1385 static int set_config_cgroup_controller(const char *key
, const char *value
,
1386 struct lxc_conf
*lxc_conf
, void *data
)
1388 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1389 CGROUP_SUPER_MAGIC
);
1392 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1393 struct lxc_conf
*lxc_conf
, void *data
)
1395 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1396 CGROUP2_SUPER_MAGIC
);
1400 static int set_config_cgroup_dir(const char *key
, const char *value
,
1401 struct lxc_conf
*lxc_conf
, void *data
)
1403 if (lxc_config_value_empty(value
))
1404 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1406 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1409 static int set_config_cgroup_relative(const char *key
, const char *value
,
1410 struct lxc_conf
*lxc_conf
, void *data
)
1412 unsigned int converted
;
1415 if (lxc_config_value_empty(value
))
1416 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1418 ret
= lxc_safe_uint(value
, &converted
);
1422 if (converted
== 1) {
1423 lxc_conf
->cgroup_meta
.relative
= true;
1427 if (converted
== 0) {
1428 lxc_conf
->cgroup_meta
.relative
= false;
1435 static int set_config_prlimit(const char *key
, const char *value
,
1436 struct lxc_conf
*lxc_conf
, void *data
)
1438 struct lxc_list
*iter
;
1439 struct rlimit limit
;
1441 struct lxc_list
*limlist
= NULL
;
1442 struct lxc_limit
*limelem
= NULL
;
1444 if (lxc_config_value_empty(value
))
1445 return lxc_clear_limits(lxc_conf
, key
);
1447 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1450 key
+= STRLITERALLEN("lxc.prlimit.");
1452 /* soft limit comes first in the value */
1453 if (!parse_limit_value(&value
, &limit_value
))
1456 limit
.rlim_cur
= limit_value
;
1458 /* skip spaces and a colon */
1459 while (isspace(*value
))
1464 else if (*value
) /* any other character is an error here */
1467 while (isspace(*value
))
1470 /* optional hard limit */
1472 if (!parse_limit_value(&value
, &limit_value
))
1475 limit
.rlim_max
= limit_value
;
1477 /* check for trailing garbage */
1478 while (isspace(*value
))
1484 /* a single value sets both hard and soft limit */
1485 limit
.rlim_max
= limit
.rlim_cur
;
1488 /* find existing list element */
1489 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1490 limelem
= iter
->elem
;
1491 if (!strcmp(key
, limelem
->resource
)) {
1492 limelem
->limit
= limit
;
1497 /* allocate list element */
1498 limlist
= malloc(sizeof(*limlist
));
1502 limelem
= malloc(sizeof(*limelem
));
1505 memset(limelem
, 0, sizeof(*limelem
));
1507 limelem
->resource
= strdup(key
);
1508 if (!limelem
->resource
)
1511 limelem
->limit
= limit
;
1512 lxc_list_add_elem(limlist
, limelem
);;
1513 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1521 free(limelem
->resource
);
1528 static int set_config_sysctl(const char *key
, const char *value
,
1529 struct lxc_conf
*lxc_conf
, void *data
)
1531 struct lxc_list
*iter
;
1532 char *replace_value
= NULL
;
1533 struct lxc_list
*sysctl_list
= NULL
;
1534 struct lxc_sysctl
*sysctl_elem
= NULL
;
1536 if (lxc_config_value_empty(value
))
1537 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1539 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1542 key
+= STRLITERALLEN("lxc.sysctl.");
1544 /* find existing list element */
1545 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1546 sysctl_elem
= iter
->elem
;
1548 if (strcmp(key
, sysctl_elem
->key
) != 0)
1551 replace_value
= strdup(value
);
1555 free(sysctl_elem
->value
);
1556 sysctl_elem
->value
= replace_value
;
1561 /* allocate list element */
1562 sysctl_list
= malloc(sizeof(*sysctl_list
));
1566 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1569 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1571 sysctl_elem
->key
= strdup(key
);
1572 if (!sysctl_elem
->key
)
1575 sysctl_elem
->value
= strdup(value
);
1576 if (!sysctl_elem
->value
)
1579 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1580 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1588 free(sysctl_elem
->key
);
1589 free(sysctl_elem
->value
);
1596 static int set_config_proc(const char *key
, const char *value
,
1597 struct lxc_conf
*lxc_conf
, void *data
)
1600 struct lxc_list
*proclist
= NULL
;
1601 struct lxc_proc
*procelem
= NULL
;
1603 if (lxc_config_value_empty(value
))
1604 return clr_config_proc(key
, lxc_conf
, NULL
);
1606 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
1609 subkey
= key
+ STRLITERALLEN("lxc.proc.");
1610 if (*subkey
== '\0')
1613 proclist
= malloc(sizeof(*proclist
));
1617 procelem
= malloc(sizeof(*procelem
));
1620 memset(procelem
, 0, sizeof(*procelem
));
1622 procelem
->filename
= strdup(subkey
);
1623 procelem
->value
= strdup(value
);
1625 if (!procelem
->filename
|| !procelem
->value
)
1628 proclist
->elem
= procelem
;
1630 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1638 free(procelem
->filename
);
1639 free(procelem
->value
);
1646 static int set_config_idmaps(const char *key
, const char *value
,
1647 struct lxc_conf
*lxc_conf
, void *data
)
1649 unsigned long hostid
, nsid
, range
;
1652 struct lxc_list
*idmaplist
= NULL
;
1653 struct id_map
*idmap
= NULL
;
1655 if (lxc_config_value_empty(value
))
1656 return lxc_clear_idmaps(lxc_conf
);
1658 idmaplist
= malloc(sizeof(*idmaplist
));
1662 idmap
= malloc(sizeof(*idmap
));
1665 memset(idmap
, 0, sizeof(*idmap
));
1667 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
1669 ERROR("Failed to parse id mappings");
1673 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
1675 idmap
->idtype
= ID_TYPE_UID
;
1676 else if (type
== 'g')
1677 idmap
->idtype
= ID_TYPE_GID
;
1681 idmap
->hostid
= hostid
;
1683 idmap
->range
= range
;
1684 idmaplist
->elem
= idmap
;
1685 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
1687 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
1688 if (idmap
->nsid
== 0)
1689 lxc_conf
->root_nsuid_map
= idmap
;
1691 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
1692 if (idmap
->nsid
== 0)
1693 lxc_conf
->root_nsgid_map
= idmap
;
1706 static int set_config_mount_fstab(const char *key
, const char *value
,
1707 struct lxc_conf
*lxc_conf
, void *data
)
1709 if (lxc_config_value_empty(value
)) {
1710 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
1714 return set_config_path_item(&lxc_conf
->fstab
, value
);
1717 static int set_config_mount_auto(const char *key
, const char *value
,
1718 struct lxc_conf
*lxc_conf
, void *data
)
1720 char *autos
, *token
;
1727 } allowed_auto_mounts
[] = {
1728 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1729 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
1730 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
1731 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1732 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
1733 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
1734 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
1735 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
1736 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
1737 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
1738 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
1739 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1740 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1741 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
1742 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
1743 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
1744 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
1745 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
1746 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
1747 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
1748 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
1749 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
1750 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
1751 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
1752 /* For adding anything that is just a single on/off, but has no
1753 * options: keep mask and flag identical and just define the enum
1754 * value as an unused bit so far
1759 if (lxc_config_value_empty(value
)) {
1760 lxc_conf
->auto_mounts
= 0;
1764 autos
= strdup(value
);
1768 lxc_iterate_parts(token
, autos
, " \t") {
1769 bool is_shmounts
= false;
1771 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
1772 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
1775 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
1776 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
1782 if (!allowed_auto_mounts
[i
].token
) {
1783 ERROR("Invalid filesystem to automount \"%s\"", token
);
1787 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
1788 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
1791 lxc_conf
->shmount
.path_host
= strdup(token
+ STRLITERALLEN("shmounts:"));
1792 if (!lxc_conf
->shmount
.path_host
) {
1793 SYSERROR("Failed to copy shmounts host path");
1797 if (strcmp(lxc_conf
->shmount
.path_host
, "") == 0) {
1798 ERROR("Invalid shmounts path: empty");
1802 lxc_conf
->shmount
.path_cont
= strdup("/dev/.lxc-mounts");
1803 if(!lxc_conf
->shmount
.path_cont
) {
1804 SYSERROR("Failed to copy shmounts container path");
1818 static int set_config_mount(const char *key
, const char *value
,
1819 struct lxc_conf
*lxc_conf
, void *data
)
1822 struct lxc_list
*mntlist
;
1824 if (lxc_config_value_empty(value
))
1825 return lxc_clear_mount_entries(lxc_conf
);
1827 mntlist
= malloc(sizeof(*mntlist
));
1831 mntelem
= strdup(value
);
1836 mntlist
->elem
= mntelem
;
1838 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
1843 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
1844 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
1847 static int set_config_cap_keep(const char *key
, const char *value
,
1848 struct lxc_conf
*lxc_conf
, void *data
)
1850 char *keepcaps
, *token
;
1851 struct lxc_list
*keeplist
;
1854 if (lxc_config_value_empty(value
))
1855 return lxc_clear_config_keepcaps(lxc_conf
);
1857 keepcaps
= strdup(value
);
1861 /* In case several capability keep is specified in a single line
1862 * split these caps in a single element for the list.
1864 lxc_iterate_parts(token
, keepcaps
, " \t") {
1865 if (!strcmp(token
, "none"))
1866 lxc_clear_config_keepcaps(lxc_conf
);
1868 keeplist
= malloc(sizeof(*keeplist
));
1872 keeplist
->elem
= strdup(token
);
1873 if (!keeplist
->elem
) {
1878 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
1889 static int set_config_cap_drop(const char *key
, const char *value
,
1890 struct lxc_conf
*lxc_conf
, void *data
)
1892 char *dropcaps
, *token
;
1893 struct lxc_list
*droplist
;
1896 if (lxc_config_value_empty(value
))
1897 return lxc_clear_config_caps(lxc_conf
);
1899 dropcaps
= strdup(value
);
1903 /* In case several capability drop is specified in a single line
1904 * split these caps in a single element for the list.
1906 lxc_iterate_parts(token
, dropcaps
, " \t") {
1907 droplist
= malloc(sizeof(*droplist
));
1911 droplist
->elem
= strdup(token
);
1912 if (!droplist
->elem
) {
1917 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
1928 static int set_config_console_path(const char *key
, const char *value
,
1929 struct lxc_conf
*lxc_conf
, void *data
)
1931 return set_config_path_item(&lxc_conf
->console
.path
, value
);
1934 static int set_config_console_rotate(const char *key
, const char *value
,
1935 struct lxc_conf
*lxc_conf
, void *data
)
1937 if (lxc_config_value_empty(value
)) {
1938 lxc_conf
->console
.log_rotate
= 0;
1942 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
1945 if (lxc_conf
->console
.log_rotate
> 1) {
1946 ERROR("The \"lxc.console.rotate\" config key can only be set "
1954 static int set_config_console_logfile(const char *key
, const char *value
,
1955 struct lxc_conf
*lxc_conf
, void *data
)
1957 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
1960 static int set_config_console_buffer_size(const char *key
, const char *value
,
1961 struct lxc_conf
*lxc_conf
, void *data
)
1965 uint64_t buffer_size
, pgsz
;
1967 if (lxc_config_value_empty(value
)) {
1968 lxc_conf
->console
.buffer_size
= 0;
1972 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1973 if (!strcmp(value
, "auto")) {
1974 lxc_conf
->console
.buffer_size
= 1 << 17;
1978 ret
= parse_byte_size_string(value
, &size
);
1985 /* must be at least a page size */
1986 pgsz
= lxc_getpagesize();
1987 if ((uint64_t)size
< pgsz
) {
1988 NOTICE("Requested ringbuffer size for the console is %" PRId64
1989 " but must be at least %" PRId64
1990 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
1995 buffer_size
= lxc_find_next_power2((uint64_t)size
);
1996 if (buffer_size
== 0)
1999 if (buffer_size
!= size
)
2000 NOTICE("Passed size was not a power of 2. Rounding log size to "
2001 "next power of two: %" PRIu64
" bytes", buffer_size
);
2003 lxc_conf
->console
.buffer_size
= buffer_size
;
2008 static int set_config_console_size(const char *key
, const char *value
,
2009 struct lxc_conf
*lxc_conf
, void *data
)
2013 uint64_t log_size
, pgsz
;
2015 if (lxc_config_value_empty(value
)) {
2016 lxc_conf
->console
.log_size
= 0;
2020 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2021 if (!strcmp(value
, "auto")) {
2022 lxc_conf
->console
.log_size
= 1 << 17;
2026 ret
= parse_byte_size_string(value
, &size
);
2033 /* must be at least a page size */
2034 pgsz
= lxc_getpagesize();
2035 if ((uint64_t)size
< pgsz
) {
2036 NOTICE("Requested ringbuffer size for the console is %" PRId64
2037 " but must be at least %" PRId64
2038 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2043 log_size
= lxc_find_next_power2((uint64_t)size
);
2047 if (log_size
!= size
)
2048 NOTICE("Passed size was not a power of 2. Rounding log size to "
2049 "next power of two: %" PRIu64
" bytes", log_size
);
2051 lxc_conf
->console
.log_size
= log_size
;
2056 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2059 size_t len
= conf
->unexpanded_len
;
2061 update_hwaddr(line
);
2063 linelen
= strlen(line
);
2064 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2065 char *tmp
= realloc(conf
->unexpanded_config
,
2066 conf
->unexpanded_alloced
+ 1024);
2070 if (!conf
->unexpanded_config
)
2073 conf
->unexpanded_config
= tmp
;
2074 conf
->unexpanded_alloced
+= 1024;
2077 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2078 conf
->unexpanded_len
+= linelen
;
2079 if (line
[linelen
- 1] != '\n')
2080 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2081 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2086 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2088 struct dirent
*direntp
;
2090 char path
[PATH_MAX
];
2094 dir
= opendir(dirp
);
2098 while ((direntp
= readdir(dir
))) {
2101 fnam
= direntp
->d_name
;
2102 if (!strcmp(fnam
, "."))
2105 if (!strcmp(fnam
, ".."))
2109 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2112 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2113 if (len
< 0 || len
>= PATH_MAX
) {
2118 ret
= lxc_config_read(path
, lxc_conf
, true);
2130 static int set_config_includefiles(const char *key
, const char *value
,
2131 struct lxc_conf
*lxc_conf
, void *data
)
2133 if (lxc_config_value_empty(value
)) {
2134 clr_config_includefiles(key
, lxc_conf
, NULL
);
2139 return do_includedir(value
, lxc_conf
);
2141 return lxc_config_read(value
, lxc_conf
, true);
2144 static int set_config_rootfs_path(const char *key
, const char *value
,
2145 struct lxc_conf
*lxc_conf
, void *data
)
2149 const char *container_path
;
2151 if (lxc_config_value_empty(value
)) {
2152 free(lxc_conf
->rootfs
.path
);
2153 lxc_conf
->rootfs
.path
= NULL
;
2157 dup
= strdup(value
);
2161 /* Split <storage type>:<container path> into <storage type> and
2162 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2163 * "rootfs.path" to <container path>.
2165 tmp
= strchr(dup
, ':');
2169 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2176 container_path
= tmp
;
2178 container_path
= value
;
2181 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2187 static int set_config_rootfs_managed(const char *key
, const char *value
,
2188 struct lxc_conf
*lxc_conf
, void *data
)
2190 unsigned int val
= 0;
2192 if (lxc_config_value_empty(value
)) {
2193 lxc_conf
->rootfs
.managed
= true;
2197 if (lxc_safe_uint(value
, &val
) < 0)
2202 lxc_conf
->rootfs
.managed
= false;
2205 lxc_conf
->rootfs
.managed
= true;
2212 static int set_config_rootfs_mount(const char *key
, const char *value
,
2213 struct lxc_conf
*lxc_conf
, void *data
)
2215 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2218 static int set_config_rootfs_options(const char *key
, const char *value
,
2219 struct lxc_conf
*lxc_conf
, void *data
)
2222 unsigned long mflags
= 0, pflags
= 0;
2223 char *mdata
= NULL
, *opts
= NULL
;
2224 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2226 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2230 ret
= parse_propagationopts(value
, &pflags
);
2236 ret
= set_config_string_item(&opts
, value
);
2242 rootfs
->mountflags
= mflags
| pflags
;
2243 rootfs
->options
= opts
;
2244 rootfs
->data
= mdata
;
2249 static int set_config_uts_name(const char *key
, const char *value
,
2250 struct lxc_conf
*lxc_conf
, void *data
)
2252 struct utsname
*utsname
;
2254 if (lxc_config_value_empty(value
)) {
2255 clr_config_uts_name(key
, lxc_conf
, NULL
);
2259 utsname
= malloc(sizeof(*utsname
));
2263 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2268 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2269 free(lxc_conf
->utsname
);
2270 lxc_conf
->utsname
= utsname
;
2275 static int set_config_namespace_clone(const char *key
, const char *value
,
2276 struct lxc_conf
*lxc_conf
, void *data
)
2281 if (lxc_config_value_empty(value
))
2282 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2284 if (lxc_conf
->ns_keep
!= 0) {
2286 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2287 "\"lxc.namespace.keep\"");
2295 lxc_iterate_parts(token
, ns
, " \t") {
2296 token
+= lxc_char_left_gc(token
, strlen(token
));
2297 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2298 cloneflag
= lxc_namespace_2_cloneflag(token
);
2299 if (cloneflag
< 0) {
2303 lxc_conf
->ns_clone
|= cloneflag
;
2310 static int set_config_namespace_keep(const char *key
, const char *value
,
2311 struct lxc_conf
*lxc_conf
, void *data
)
2316 if (lxc_config_value_empty(value
))
2317 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2319 if (lxc_conf
->ns_clone
!= 0) {
2321 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2322 "\"lxc.namespace.keep\"");
2330 lxc_iterate_parts(token
, ns
, " \t") {
2331 token
+= lxc_char_left_gc(token
, strlen(token
));
2332 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2333 cloneflag
= lxc_namespace_2_cloneflag(token
);
2334 if (cloneflag
< 0) {
2338 lxc_conf
->ns_keep
|= cloneflag
;
2345 static int set_config_namespace_share(const char *key
, const char *value
,
2346 struct lxc_conf
*lxc_conf
, void *data
)
2349 const char *namespace;
2351 if (lxc_config_value_empty(value
))
2352 return clr_config_namespace_share(key
, lxc_conf
, data
);
2354 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2355 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2359 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2362 struct parse_line_conf
{
2363 struct lxc_conf
*conf
;
2367 static int parse_line(char *buffer
, void *data
)
2369 char *dot
, *key
, *line
, *linep
, *value
;
2371 struct lxc_config_t
*config
;
2374 struct parse_line_conf
*plc
= data
;
2376 /* If there are newlines in the config file we should keep them. */
2377 empty_line
= lxc_is_line_empty(dup
);
2381 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2382 * modified the original string on the stack by replacing '=' by '\0'
2385 linep
= line
= strdup(dup
);
2389 if (!plc
->from_include
) {
2390 ret
= append_unexp_config_line(line
, plc
->conf
);
2398 line
+= lxc_char_left_gc(line
, strlen(line
));
2400 /* ignore comments */
2404 /* martian option - don't add it to the config itself */
2405 if (strncmp(line
, "lxc.", 4))
2410 dot
= strchr(line
, '=');
2412 ERROR("Invalid configuration line: %s", line
);
2420 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2422 value
+= lxc_char_left_gc(value
, strlen(value
));
2423 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2425 if (*value
== '\'' || *value
== '\"') {
2428 len
= strlen(value
);
2429 if (len
> 1 && value
[len
- 1] == *value
) {
2430 value
[len
- 1] = '\0';
2435 config
= lxc_get_config(key
);
2437 ERROR("Unknown configuration key \"%s\"", key
);
2441 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2449 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2451 char *dot
, *key
, *line
, *linep
, *value
;
2454 struct new_config_item
*new = NULL
;
2456 linep
= line
= strdup(dup
);
2460 line
+= lxc_char_left_gc(line
, strlen(line
));
2462 /* martian option - don't add it to the config itself */
2463 if (strncmp(line
, "lxc.", 4))
2467 dot
= strchr(line
, '=');
2469 ERROR("Invalid configuration item: %s", line
);
2477 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2479 value
+= lxc_char_left_gc(value
, strlen(value
));
2480 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2482 if (*value
== '\'' || *value
== '\"') {
2485 len
= strlen(value
);
2486 if (len
> 1 && value
[len
- 1] == *value
) {
2487 value
[len
- 1] = '\0';
2493 new = malloc(sizeof(struct new_config_item
));
2497 new->key
= strdup(key
);
2498 new->val
= strdup(value
);
2499 if (!new->val
|| !new->key
)
2507 if (ret
< 0 && new) {
2517 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2519 struct parse_line_conf c
;
2522 c
.from_include
= from_include
;
2524 /* Catch only the top level config file name in the structure. */
2526 conf
->rcfile
= strdup(file
);
2528 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2531 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2533 struct lxc_list
*dent
;
2535 dent
= malloc(sizeof(struct lxc_list
));
2539 dent
->elem
= parse_new_conf_line(arg
);
2545 lxc_list_add_tail(defines
, dent
);
2550 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2552 struct lxc_list
*it
;
2555 lxc_list_for_each(it
, defines
) {
2556 struct new_config_item
*new_item
= it
->elem
;
2557 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2562 lxc_config_define_free(defines
);
2567 void lxc_config_define_free(struct lxc_list
*defines
)
2569 struct lxc_list
*it
, *next
;
2571 lxc_list_for_each_safe(it
, defines
, next
) {
2572 struct new_config_item
*new_item
= it
->elem
;
2573 free(new_item
->key
);
2574 free(new_item
->val
);
2580 signed long lxc_config_parse_arch(const char *arch
)
2582 #if HAVE_SYS_PERSONALITY_H
2588 { "arm", PER_LINUX32
},
2589 { "armel", PER_LINUX32
},
2590 { "armhf", PER_LINUX32
},
2591 { "armv7l", PER_LINUX32
},
2592 { "athlon", PER_LINUX32
},
2593 { "i386", PER_LINUX32
},
2594 { "i486", PER_LINUX32
},
2595 { "i586", PER_LINUX32
},
2596 { "i686", PER_LINUX32
},
2597 { "linux32", PER_LINUX32
},
2598 { "mips", PER_LINUX32
},
2599 { "mipsel", PER_LINUX32
},
2600 { "ppc", PER_LINUX32
},
2601 { "powerpc", PER_LINUX32
},
2602 { "x86", PER_LINUX32
},
2603 { "amd64", PER_LINUX
},
2604 { "arm64", PER_LINUX
},
2605 { "linux64", PER_LINUX
},
2606 { "mips64", PER_LINUX
},
2607 { "mips64el", PER_LINUX
},
2608 { "ppc64", PER_LINUX
},
2609 { "ppc64el", PER_LINUX
},
2610 { "ppc64le", PER_LINUX
},
2611 { "powerpc64", PER_LINUX
},
2612 { "s390x", PER_LINUX
},
2613 { "x86_64", PER_LINUX
},
2615 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2617 for (i
= 0; i
< len
; i
++)
2618 if (!strcmp(pername
[i
].name
, arch
))
2619 return pername
[i
].per
;
2625 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2633 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2634 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2635 { "LSM", LXC_ATTACH_LSM_EXEC
},
2640 /* For the sake of backward compatibility, drop all privileges
2641 * if none is specified.
2643 for (i
= 0; all_privs
[i
].token
; i
++)
2644 *flags
|= all_privs
[i
].flag
;
2649 lxc_iterate_parts(token
, flaglist
, "|") {
2652 for (i
= 0; all_privs
[i
].token
; i
++)
2653 if (!strcmp(all_privs
[i
].token
, token
))
2654 aflag
= all_privs
[i
].flag
;
2665 /* Write out a configuration file. */
2666 int write_config(int fd
, const struct lxc_conf
*conf
)
2669 size_t len
= conf
->unexpanded_len
;
2674 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
2676 SYSERROR("Failed to write configuration file");
2683 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2690 len
= strlen(key
) + strlen(v
) + 4;
2693 if (lxc_config_value_empty(v
))
2694 ret
= snprintf(tmp
, len
, "%s =", key
);
2696 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
2697 if (ret
< 0 || ret
>= len
)
2700 /* Save the line verbatim into unexpanded_conf */
2701 if (append_unexp_config_line(tmp
, conf
))
2707 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
2711 char *lstart
= conf
->unexpanded_config
;
2713 if (!conf
->unexpanded_config
)
2717 lend
= strchr(lstart
, '\n');
2721 lend
= lstart
+ strlen(lstart
);
2725 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
2731 v
= lstart
[strlen(key
)];
2732 if (!isspace(v
) && v
!= '=') {
2738 conf
->unexpanded_len
-= (lend
- lstart
);
2740 if (*lend
== '\0') {
2745 memmove(lstart
, lend
, strlen(lend
) + 1);
2749 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
2750 const char *newpath
, const char *oldname
,
2751 const char *newname
, const char *ovldir
)
2754 char *lend
, *newdir
, *olddir
, *p
, *q
;
2755 size_t newdirlen
, olddirlen
;
2756 char *lstart
= conf
->unexpanded_config
;
2757 const char *key
= "lxc.mount.entry";
2759 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
2760 olddir
= alloca(olddirlen
+ 1);
2761 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
2763 if (ret
< 0 || ret
>= olddirlen
+ 1)
2766 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
2767 newdir
= alloca(newdirlen
+ 1);
2768 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
2770 if (ret
< 0 || ret
>= newdirlen
+ 1)
2773 if (!conf
->unexpanded_config
)
2777 lend
= strchr(lstart
, '\n');
2779 lend
= lstart
+ strlen(lstart
);
2783 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2786 p
= strchr(lstart
+ strlen(key
), '=');
2797 /* Whenever a lxc.mount.entry entry is found in a line we check
2798 * if the substring "overlay" is present before doing any
2799 * further work. We check for "overlay" because substrings need
2800 * to have at least one space before them in a valid overlay
2801 * lxc.mount.entry (/A B overlay). When the space before is
2802 * missing it is very likely that these substrings are part of a
2803 * path or something else. (Checking q >= lend ensures that we
2804 * only count matches in the current line.) */
2805 q
= strstr(p
, " overlay");
2806 if (!q
|| q
>= lend
)
2809 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
2812 /* replace the olddir with newdir */
2813 if (olddirlen
>= newdirlen
) {
2814 size_t diff
= olddirlen
- newdirlen
;
2815 memcpy(q
, newdir
, newdirlen
);
2817 if (olddirlen
!= newdirlen
) {
2818 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
2819 strlen(q
) - newdirlen
- diff
+ 1);
2821 conf
->unexpanded_len
-= diff
;
2825 size_t diff
= newdirlen
- olddirlen
;
2826 size_t oldlen
= conf
->unexpanded_len
;
2827 size_t newlen
= oldlen
+ diff
;
2828 size_t poffset
= q
- conf
->unexpanded_config
;
2830 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2834 conf
->unexpanded_len
= newlen
;
2835 conf
->unexpanded_alloced
= newlen
+ 1;
2836 new[newlen
- 1] = '\0';
2837 lend
= new + (lend
- conf
->unexpanded_config
);
2839 /* Move over the remainder to make room for the newdir.
2841 memmove(new + poffset
+ newdirlen
,
2842 new + poffset
+ olddirlen
,
2843 oldlen
- poffset
- olddirlen
+ 1);
2844 conf
->unexpanded_config
= new;
2846 memcpy(new + poffset
, newdir
, newdirlen
);
2857 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
2858 const char *newpath
, const char *oldname
,
2859 const char *newname
)
2862 char *lend
, *newdir
, *olddir
, *p
;
2863 char *lstart
= conf
->unexpanded_config
;
2864 size_t newdirlen
, olddirlen
;
2865 const char *key
= "lxc.hook";
2867 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
2868 olddir
= alloca(olddirlen
+ 1);
2869 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
2870 if (ret
< 0 || ret
>= olddirlen
+ 1)
2873 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
2874 newdir
= alloca(newdirlen
+ 1);
2875 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
2876 if (ret
< 0 || ret
>= newdirlen
+ 1)
2879 if (!conf
->unexpanded_config
)
2883 lend
= strchr(lstart
, '\n');
2885 lend
= lstart
+ strlen(lstart
);
2889 if (strncmp(lstart
, key
, strlen(key
)) != 0)
2892 p
= strchr(lstart
+ strlen(key
), '=');
2903 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
2906 /* replace the olddir with newdir */
2907 if (olddirlen
>= newdirlen
) {
2908 size_t diff
= olddirlen
- newdirlen
;
2909 memcpy(p
, newdir
, newdirlen
);
2911 if (olddirlen
!= newdirlen
) {
2912 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
2913 strlen(p
) - newdirlen
- diff
+ 1);
2915 conf
->unexpanded_len
-= diff
;
2919 size_t diff
= newdirlen
- olddirlen
;
2920 size_t oldlen
= conf
->unexpanded_len
;
2921 size_t newlen
= oldlen
+ diff
;
2922 size_t poffset
= p
- conf
->unexpanded_config
;
2924 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
2928 conf
->unexpanded_len
= newlen
;
2929 conf
->unexpanded_alloced
= newlen
+ 1;
2930 new[newlen
- 1] = '\0';
2931 lend
= new + (lend
- conf
->unexpanded_config
);
2933 /* Move over the remainder to make room for the newdir.
2935 memmove(new + poffset
+ newdirlen
,
2936 new + poffset
+ olddirlen
,
2937 oldlen
- poffset
- olddirlen
+ 1);
2938 conf
->unexpanded_config
= new;
2940 memcpy(new + poffset
, newdir
, newdirlen
);
2954 ERROR("Error writing to new config"); \
2959 /* This is called only from clone. We wish to update all hwaddrs in the
2960 * unexpanded config file. We can't/don't want to update any which come from
2961 * lxc.includes (there shouldn't be any).
2962 * We can't just walk the c->lxc-conf->network list because that includes netifs
2963 * from the include files. So we update the ones which we find in the unexp
2964 * config file, then find the original macaddr in the conf->network, and update
2965 * that to the same value.
2967 bool network_new_hwaddrs(struct lxc_conf
*conf
)
2969 char *lend
, *p
, *p2
;
2970 struct lxc_list
*it
;
2971 char *lstart
= conf
->unexpanded_config
;
2973 if (!conf
->unexpanded_config
)
2977 char newhwaddr
[18], oldhwaddr
[17];
2979 lend
= strchr(lstart
, '\n');
2981 lend
= lstart
+ strlen(lstart
);
2985 if (!lxc_config_net_hwaddr(lstart
)) {
2990 p
= strchr(lstart
, '=');
3003 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3006 if ((p2
- p
) != 17) {
3007 WARN("Bad hwaddr entry");
3012 memcpy(oldhwaddr
, p
, 17);
3014 if (!new_hwaddr(newhwaddr
))
3017 memcpy(p
, newhwaddr
, 17);
3018 lxc_list_for_each(it
, &conf
->network
) {
3019 struct lxc_netdev
*n
= it
->elem
;
3021 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3022 memcpy(n
->hwaddr
, newhwaddr
, 17);
3031 static int set_config_ephemeral(const char *key
, const char *value
,
3032 struct lxc_conf
*lxc_conf
, void *data
)
3034 if (lxc_config_value_empty(value
)) {
3035 lxc_conf
->ephemeral
= 0;
3039 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3042 if (lxc_conf
->ephemeral
> 1)
3048 static int set_config_log_syslog(const char *key
, const char *value
,
3049 struct lxc_conf
*lxc_conf
, void *data
)
3053 if (lxc_conf
->syslog
) {
3054 free(lxc_conf
->syslog
);
3055 lxc_conf
->syslog
= NULL
;
3058 if (lxc_config_value_empty(value
))
3061 facility
= lxc_syslog_priority_to_int(value
);
3062 if (facility
== -EINVAL
)
3065 lxc_log_syslog(facility
);
3067 return set_config_string_item(&lxc_conf
->syslog
, value
);
3070 static int set_config_no_new_privs(const char *key
, const char *value
,
3071 struct lxc_conf
*lxc_conf
, void *data
)
3075 if (lxc_config_value_empty(value
)) {
3076 lxc_conf
->no_new_privs
= false;
3080 if (lxc_safe_uint(value
, &v
) < 0)
3086 lxc_conf
->no_new_privs
= v
? true : false;
3091 /* Callbacks to get configuration items. */
3092 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3093 struct lxc_conf
*c
, void *data
)
3100 memset(retv
, 0, inlen
);
3102 #if HAVE_SYS_PERSONALITY_H
3105 switch (c
->personality
) {
3107 strprint(retv
, inlen
, "i686");
3110 strprint(retv
, inlen
, "x86_64");
3120 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3121 struct lxc_conf
*c
, void *data
)
3123 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3126 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3127 struct lxc_conf
*c
, void *data
)
3129 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3132 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3133 struct lxc_conf
*c
, void *data
)
3135 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3138 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3139 struct lxc_conf
*c
, void *data
)
3141 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3144 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3145 int inlen
, struct lxc_conf
*c
,
3148 return lxc_get_conf_int(c
, retv
, inlen
,
3149 c
->lsm_aa_allow_incomplete
);
3152 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3153 int inlen
, struct lxc_conf
*c
,
3156 return lxc_get_conf_int(c
, retv
, inlen
,
3157 c
->lsm_aa_allow_nesting
);
3160 static int get_config_apparmor_raw(const char *key
, char *retv
,
3161 int inlen
, struct lxc_conf
*c
,
3165 struct lxc_list
*it
;
3171 memset(retv
, 0, inlen
);
3173 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3174 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3180 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3181 struct lxc_conf
*c
, void *data
)
3183 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3186 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3187 * just the value(s) will be printed. Since there still could be more than one,
3188 * it is newline-separated.
3189 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3191 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3192 * 'lxc.cgroup.subsystem.key = value' format.
3194 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3195 int inlen
, struct lxc_conf
*c
,
3199 size_t namespaced_token_len
;
3200 char *global_token
, *namespaced_token
;
3201 struct lxc_list
*it
;
3203 bool get_all
= false;
3208 memset(retv
, 0, inlen
);
3210 if (version
== CGROUP2_SUPER_MAGIC
) {
3211 global_token
= "lxc.cgroup2";
3212 namespaced_token
= "lxc.cgroup2.";
3213 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3214 } else if (version
== CGROUP_SUPER_MAGIC
) {
3215 global_token
= "lxc.cgroup";
3216 namespaced_token
= "lxc.cgroup.";
3217 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3222 if (strcmp(key
, global_token
) == 0)
3224 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3225 key
+= namespaced_token_len
;
3229 lxc_list_for_each(it
, &c
->cgroup
) {
3230 struct lxc_cgroup
*cg
= it
->elem
;
3233 if (version
!= cg
->version
)
3236 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3237 cg
->subsystem
, cg
->value
);
3238 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3239 strprint(retv
, inlen
, "%s\n", cg
->value
);
3246 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3247 struct lxc_conf
*c
, void *data
)
3249 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3250 CGROUP_SUPER_MAGIC
);
3253 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3254 struct lxc_conf
*c
, void *data
)
3256 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3257 CGROUP2_SUPER_MAGIC
);
3260 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3261 struct lxc_conf
*lxc_conf
, void *data
)
3269 memset(retv
, 0, inlen
);
3271 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3276 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3277 int inlen
, struct lxc_conf
*lxc_conf
,
3280 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3281 lxc_conf
->cgroup_meta
.relative
);
3284 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3285 struct lxc_conf
*c
, void *data
)
3287 struct lxc_list
*it
;
3288 int len
, listlen
, ret
;
3290 /* "u 1000 1000000 65536"
3292 * let's render this as
3310 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3311 char buf
[__LXC_IDMAP_STR_BUF
];
3316 memset(retv
, 0, inlen
);
3318 listlen
= lxc_list_len(&c
->id_map
);
3319 lxc_list_for_each(it
, &c
->id_map
) {
3320 struct id_map
*map
= it
->elem
;
3321 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3322 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3323 map
->nsid
, map
->hostid
, map
->range
);
3324 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3327 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3333 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3334 struct lxc_conf
*c
, void *data
)
3337 v
= lxc_log_priority_to_string(c
->loglevel
);
3338 return lxc_get_conf_str(retv
, inlen
, v
);
3341 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3342 struct lxc_conf
*c
, void *data
)
3344 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3347 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3348 struct lxc_conf
*c
, void *data
)
3350 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3353 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3354 struct lxc_conf
*c
, void *data
)
3356 int len
, fulllen
= 0;
3357 const char *sep
= "";
3362 memset(retv
, 0, inlen
);
3364 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3367 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3368 case LXC_AUTO_PROC_MIXED
:
3369 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3372 case LXC_AUTO_PROC_RW
:
3373 strprint(retv
, inlen
, "%sproc:rw", sep
);
3380 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3381 case LXC_AUTO_SYS_RO
:
3382 strprint(retv
, inlen
, "%ssys:ro", sep
);
3385 case LXC_AUTO_SYS_RW
:
3386 strprint(retv
, inlen
, "%ssys:rw", sep
);
3389 case LXC_AUTO_SYS_MIXED
:
3390 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3397 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3398 case LXC_AUTO_CGROUP_NOSPEC
:
3399 strprint(retv
, inlen
, "%scgroup", sep
);
3401 case LXC_AUTO_CGROUP_MIXED
:
3402 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3404 case LXC_AUTO_CGROUP_RO
:
3405 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3407 case LXC_AUTO_CGROUP_RW
:
3408 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3410 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3411 strprint(retv
, inlen
, "%scgroup-full", sep
);
3413 case LXC_AUTO_CGROUP_FULL_MIXED
:
3414 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3416 case LXC_AUTO_CGROUP_FULL_RO
:
3417 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3419 case LXC_AUTO_CGROUP_FULL_RW
:
3420 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3429 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3430 struct lxc_conf
*c
, void *data
)
3432 int len
, fulllen
= 0;
3433 struct lxc_list
*it
;
3438 memset(retv
, 0, inlen
);
3440 lxc_list_for_each(it
, &c
->mount_list
) {
3441 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3447 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3448 struct lxc_conf
*c
, void *data
)
3450 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3453 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3454 struct lxc_conf
*c
, void *data
)
3456 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3459 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3460 struct lxc_conf
*c
, void *data
)
3462 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3465 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3466 struct lxc_conf
*c
, void *data
)
3468 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3471 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3472 struct lxc_conf
*c
, void *data
)
3474 return lxc_get_conf_str(
3476 c
->utsname
? c
->utsname
->nodename
: NULL
);
3479 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3480 struct lxc_conf
*c
, void *data
)
3483 int len
, fulllen
= 0, found
= -1;
3484 struct lxc_list
*it
;
3487 subkey
= strchr(key
, '.');
3491 subkey
= strchr(subkey
+ 1, '.');
3495 if (*subkey
== '\0')
3498 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3499 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3511 memset(retv
, 0, inlen
);
3513 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3514 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3520 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3521 struct lxc_conf
*c
, void *data
)
3523 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3526 static int get_config_net(const char *key
, char *retv
, int inlen
,
3527 struct lxc_conf
*c
, void *data
)
3529 int len
, fulllen
= 0;
3530 struct lxc_list
*it
;
3535 memset(retv
, 0, inlen
);
3537 lxc_list_for_each(it
, &c
->network
) {
3538 struct lxc_netdev
*n
= it
->elem
;
3539 const char *t
= lxc_net_type_to_str(n
->type
);
3540 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3546 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3547 struct lxc_conf
*c
, void *data
)
3549 int len
, fulllen
= 0;
3550 struct lxc_list
*it
;
3555 memset(retv
, 0, inlen
);
3557 lxc_list_for_each(it
, &c
->caps
) {
3558 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3564 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3565 struct lxc_conf
*c
, void *data
)
3567 int len
, fulllen
= 0;
3568 struct lxc_list
*it
;
3573 memset(retv
, 0, inlen
);
3575 lxc_list_for_each(it
, &c
->keepcaps
) {
3576 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3582 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3583 struct lxc_conf
*c
, void *data
)
3585 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3588 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3589 struct lxc_conf
*c
, void *data
)
3591 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3594 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3595 struct lxc_conf
*c
, void *data
)
3597 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3601 static int get_config_console_buffer_size(const char *key
, char *retv
,
3602 int inlen
, struct lxc_conf
*c
,
3605 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3608 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3609 struct lxc_conf
*c
, void *data
)
3611 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3615 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
3616 struct lxc_conf
*c
, void *data
)
3618 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
);
3621 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
3622 struct lxc_conf
*c
, void *data
)
3624 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
3627 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
3628 struct lxc_conf
*c
, void *data
)
3630 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
3633 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
3634 struct lxc_conf
*c
, void *data
)
3636 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
3639 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
3640 struct lxc_conf
*c
, void *data
)
3642 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
3645 static int get_config_start(const char *key
, char *retv
, int inlen
,
3646 struct lxc_conf
*c
, void *data
)
3648 if (strcmp(key
+ 10, "auto") == 0)
3649 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
3650 else if (strcmp(key
+ 10, "delay") == 0)
3651 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
3652 else if (strcmp(key
+ 10, "order") == 0)
3653 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
3658 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
3659 struct lxc_conf
*c
, void *data
)
3661 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
3664 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
3665 struct lxc_conf
*c
, void *data
)
3667 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
3670 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
3671 int inlen
, struct lxc_conf
*c
,
3674 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
3677 static int get_config_group(const char *key
, char *retv
, int inlen
,
3678 struct lxc_conf
*c
, void *data
)
3680 int len
, fulllen
= 0;
3681 struct lxc_list
*it
;
3686 memset(retv
, 0, inlen
);
3688 lxc_list_for_each(it
, &c
->groups
) {
3689 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3695 static int get_config_environment(const char *key
, char *retv
, int inlen
,
3696 struct lxc_conf
*c
, void *data
)
3698 int len
, fulllen
= 0;
3699 struct lxc_list
*it
;
3704 memset(retv
, 0, inlen
);
3706 lxc_list_for_each(it
, &c
->environment
) {
3707 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3713 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
3714 struct lxc_conf
*c
, void *data
)
3716 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
3719 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
3720 struct lxc_conf
*c
, void *data
)
3722 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
3725 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
3726 struct lxc_conf
*c
, void *data
)
3728 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
3731 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
3732 struct lxc_conf
*c
, void *data
)
3734 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
3737 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
3738 struct lxc_conf
*c
, void *data
)
3740 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
3743 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
3744 struct lxc_conf
*c
, void *data
)
3746 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
3749 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
3750 struct lxc_conf
*c
, void *data
)
3752 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
3755 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3756 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3757 * printed, in 'lxc.prlimit.resource = value' format.
3759 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
3760 struct lxc_conf
*c
, void *data
)
3762 int fulllen
= 0, len
;
3763 bool get_all
= false;
3764 struct lxc_list
*it
;
3769 memset(retv
, 0, inlen
);
3771 if (!strcmp(key
, "lxc.prlimit"))
3773 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
3778 lxc_list_for_each(it
, &c
->limits
) {
3779 /* 2 colon separated 64 bit integers or the word 'unlimited' */
3780 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
3782 struct lxc_limit
*lim
= it
->elem
;
3784 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
3785 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
3786 partlen
= STRLITERALLEN("unlimited");
3788 partlen
= sprintf(buf
, "%" PRIu64
,
3789 (uint64_t)lim
->limit
.rlim_cur
);
3792 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
3793 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
3794 memcpy(buf
+ partlen
, ":unlimited",
3795 STRLITERALLEN(":unlimited") + 1);
3797 sprintf(buf
+ partlen
, ":%" PRIu64
,
3798 (uint64_t)lim
->limit
.rlim_max
);
3802 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
3803 lim
->resource
, buf
);
3804 } else if (strcmp(lim
->resource
, key
) == 0) {
3805 strprint(retv
, inlen
, "%s", buf
);
3812 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3813 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3814 * entries will be printed, in 'lxc.sysctl.key = value' format.
3816 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
3817 struct lxc_conf
*c
, void *data
)
3820 struct lxc_list
*it
;
3822 bool get_all
= false;
3827 memset(retv
, 0, inlen
);
3829 if (strcmp(key
, "lxc.sysctl") == 0)
3831 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
3832 key
+= STRLITERALLEN("lxc.sysctl.");
3836 lxc_list_for_each(it
, &c
->sysctls
) {
3837 struct lxc_sysctl
*elem
= it
->elem
;
3839 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
3841 } else if (strcmp(elem
->key
, key
) == 0) {
3842 strprint(retv
, inlen
, "%s", elem
->value
);
3849 static int get_config_proc(const char *key
, char *retv
, int inlen
,
3850 struct lxc_conf
*c
, void *data
)
3852 struct lxc_list
*it
;
3855 bool get_all
= false;
3860 memset(retv
, 0, inlen
);
3862 if (strcmp(key
, "lxc.proc") == 0)
3864 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
3865 key
+= STRLITERALLEN("lxc.proc.");
3869 lxc_list_for_each(it
, &c
->procs
) {
3870 struct lxc_proc
*proc
= it
->elem
;
3873 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
3874 proc
->filename
, proc
->value
);
3875 } else if (strcmp(proc
->filename
, key
) == 0) {
3876 strprint(retv
, inlen
, "%s", proc
->value
);
3883 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
3884 struct lxc_conf
*c
, void *data
)
3892 memset(retv
, 0, inlen
);
3894 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3895 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
3896 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3902 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
3903 struct lxc_conf
*c
, void *data
)
3911 memset(retv
, 0, inlen
);
3913 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
3914 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
3915 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
3921 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
3922 struct lxc_conf
*c
, void *data
)
3925 const char *namespace;
3931 memset(retv
, 0, inlen
);
3933 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
3934 ns_idx
= lxc_namespace_2_ns_idx(namespace);
3938 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
3943 /* Callbacks to clear config items. */
3944 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
3947 c
->personality
= -1;
3951 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
3958 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
3965 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
3973 static inline int clr_config_apparmor_profile(const char *key
,
3974 struct lxc_conf
*c
, void *data
)
3976 free(c
->lsm_aa_profile
);
3977 c
->lsm_aa_profile
= NULL
;
3981 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
3985 c
->lsm_aa_allow_incomplete
= 0;
3989 static inline int clr_config_apparmor_allow_nesting(const char *key
,
3993 c
->lsm_aa_allow_nesting
= 0;
3997 static inline int clr_config_apparmor_raw(const char *key
,
4001 return lxc_clear_apparmor_raw(c
);
4004 static inline int clr_config_selinux_context(const char *key
,
4005 struct lxc_conf
*c
, void *data
)
4007 free(c
->lsm_se_context
);
4008 c
->lsm_se_context
= NULL
;
4012 static inline int clr_config_cgroup_controller(const char *key
,
4013 struct lxc_conf
*c
, void *data
)
4015 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4018 static inline int clr_config_cgroup2_controller(const char *key
,
4019 struct lxc_conf
*c
, void *data
)
4021 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4024 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4027 if (lxc_conf
->cgroup_meta
.dir
) {
4028 free(lxc_conf
->cgroup_meta
.dir
);
4029 lxc_conf
->cgroup_meta
.dir
= NULL
;
4035 static inline int clr_config_cgroup_relative(const char *key
,
4036 struct lxc_conf
*lxc_conf
,
4039 lxc_conf
->cgroup_meta
.relative
= false;
4043 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4046 return lxc_clear_idmaps(c
);
4049 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4052 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4056 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4064 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4067 return lxc_clear_mount_entries(c
);
4070 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4073 return lxc_clear_automounts(c
);
4076 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4084 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4087 free(c
->rootfs
.path
);
4088 c
->rootfs
.path
= NULL
;
4092 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4095 c
->rootfs
.managed
= true;
4099 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4102 free(c
->rootfs
.mount
);
4103 c
->rootfs
.mount
= NULL
;
4107 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4110 free(c
->rootfs
.options
);
4111 c
->rootfs
.options
= NULL
;
4113 free(c
->rootfs
.data
);
4114 c
->rootfs
.data
= NULL
;
4119 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4127 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4130 return lxc_clear_hooks(c
, key
);
4133 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4136 /* default to legacy hooks version */
4137 c
->hooks_version
= 0;
4141 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4144 lxc_free_networks(&c
->network
);
4149 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4152 return lxc_clear_config_caps(c
);
4155 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4158 return lxc_clear_config_keepcaps(c
);
4161 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4164 free(c
->console
.path
);
4165 c
->console
.path
= NULL
;
4169 static inline int clr_config_console_logfile(const char *key
,
4170 struct lxc_conf
*c
, void *data
)
4172 free(c
->console
.log_path
);
4173 c
->console
.log_path
= NULL
;
4177 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4180 c
->console
.log_rotate
= 0;
4184 static inline int clr_config_console_buffer_size(const char *key
,
4185 struct lxc_conf
*c
, void *data
)
4187 c
->console
.buffer_size
= 0;
4191 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4194 c
->console
.log_size
= 0;
4198 static inline int clr_config_seccomp_profile(const char *key
,
4199 struct lxc_conf
*c
, void *data
)
4206 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4213 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4220 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4223 c
->rebootsignal
= 0;
4227 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4234 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4237 if (strcmp(key
+ 10, "auto") == 0)
4239 else if (strcmp(key
+ 10, "delay") == 0)
4241 else if (strcmp(key
+ 10, "order") == 0)
4247 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4255 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4258 c
->monitor_unshare
= 0;
4262 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4263 struct lxc_conf
*c
, void *data
)
4265 c
->monitor_signal_pdeath
= 0;
4269 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4272 return lxc_clear_groups(c
);
4275 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4278 return lxc_clear_environment(c
);
4281 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4284 free(c
->execute_cmd
);
4285 c
->execute_cmd
= NULL
;
4289 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4297 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4305 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4312 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4319 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4326 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4329 c
->no_new_privs
= false;
4333 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4336 return lxc_clear_limits(c
, key
);
4339 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4342 return lxc_clear_sysctls(c
, key
);
4345 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4348 return lxc_clear_procs(c
, key
);
4351 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4354 lxc_clear_includes(c
);
4358 static int clr_config_namespace_clone(const char *key
,
4359 struct lxc_conf
*lxc_conf
, void *data
)
4361 lxc_conf
->ns_clone
= 0;
4365 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4368 lxc_conf
->ns_keep
= 0;
4372 static int clr_config_namespace_share(const char *key
,
4373 struct lxc_conf
*lxc_conf
, void *data
)
4376 const char *namespace;
4378 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4379 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4383 free(lxc_conf
->ns_share
[ns_idx
]);
4384 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4389 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4390 struct lxc_conf
*c
, void *data
)
4395 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4396 struct lxc_conf
*lxc_conf
,
4398 char **deindexed_key
)
4401 unsigned int tmpidx
;
4403 char *copy
, *idx_start
, *idx_end
;
4404 struct lxc_config_t
*config
= NULL
;
4406 /* check that this is a sensible network key */
4407 if (strncmp("lxc.net.", key
, 8)) {
4408 ERROR("Invalid network configuration key \"%s\"", key
);
4414 ERROR("Failed to duplicate string \"%s\"", key
);
4419 if (!isdigit(*(key
+ 8))) {
4420 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4424 /* beginning of index string */
4425 idx_start
= (copy
+ 7);
4428 /* end of index string */
4429 idx_end
= strchr((copy
+ 8), '.');
4433 /* parse current index */
4434 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4437 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
4443 /* This, of course is utterly nonsensical on so many levels, but
4444 * better safe than sorry.
4445 * (Checking for INT_MAX here is intentional.)
4447 if (tmpidx
== INT_MAX
) {
4448 SYSERROR("Number of configured networks would overflow the "
4454 numstrlen
= strlen((idx_start
+ 1));
4456 /* repair configuration key */
4459 /* lxc.net.<idx>.<subkey> */
4462 if (strlen(idx_end
+ 1) == 0) {
4463 ERROR("No subkey in network configuration key \"%s\"", key
);
4467 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4468 copy
[strlen(key
) - numstrlen
+ 1] = '\0';
4470 config
= lxc_get_config(copy
);
4472 ERROR("Unknown network configuration key \"%s\"", key
);
4478 *deindexed_key
= copy
;
4487 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4488 * found. So we make sure next comes an integer, find the right callback (by
4489 * rewriting the key), and call it.
4491 static int set_config_net_nic(const char *key
, const char *value
,
4492 struct lxc_conf
*lxc_conf
, void *data
)
4495 const char *idxstring
;
4496 struct lxc_config_t
*config
;
4497 struct lxc_netdev
*netdev
;
4499 char *deindexed_key
= NULL
;
4501 idxstring
= key
+ 8;
4502 if (!isdigit(*idxstring
))
4505 if (lxc_config_value_empty(value
))
4506 return clr_config_net_nic(key
, lxc_conf
, data
);
4508 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4509 if (!config
|| idx
< 0)
4512 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4514 free(deindexed_key
);
4518 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4519 free(deindexed_key
);
4524 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4528 const char *idxstring
;
4529 struct lxc_config_t
*config
;
4530 struct lxc_netdev
*netdev
;
4532 char *deindexed_key
= NULL
;
4534 idxstring
= key
+ 8;
4535 if (!isdigit(*idxstring
))
4538 /* The left conjunct is pretty self-explanatory. The right conjunct
4539 * checks whether the two pointers are equal. If they are we know that
4540 * this is not a key that is namespaced any further and so we are
4541 * supposed to clear the whole network.
4543 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
4544 unsigned int rmnetdevidx
;
4546 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
4549 /* Remove network from network list. */
4550 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
4554 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4555 if (!config
|| idx
< 0)
4558 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
4560 free(deindexed_key
);
4564 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
4565 free(deindexed_key
);
4570 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
4573 struct lxc_netdev
*netdev
= data
;
4583 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
4586 struct lxc_netdev
*netdev
= data
;
4591 netdev
->name
[0] = '\0';
4596 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
4599 struct lxc_netdev
*netdev
= data
;
4609 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
4612 struct lxc_netdev
*netdev
= data
;
4617 netdev
->link
[0] = '\0';
4622 static int clr_config_net_macvlan_mode(const char *key
,
4623 struct lxc_conf
*lxc_conf
, void *data
)
4625 struct lxc_netdev
*netdev
= data
;
4630 if (netdev
->type
!= LXC_NET_MACVLAN
)
4633 netdev
->priv
.macvlan_attr
.mode
= -1;
4638 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
4641 struct lxc_netdev
*netdev
= data
;
4646 netdev
->priv
.veth_attr
.pair
[0] = '\0';
4651 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
4654 struct lxc_netdev
*netdev
= data
;
4659 free(netdev
->upscript
);
4660 netdev
->upscript
= NULL
;
4665 static int clr_config_net_script_down(const char *key
,
4666 struct lxc_conf
*lxc_conf
, void *data
)
4668 struct lxc_netdev
*netdev
= data
;
4673 free(netdev
->downscript
);
4674 netdev
->downscript
= NULL
;
4679 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
4682 struct lxc_netdev
*netdev
= data
;
4687 free(netdev
->hwaddr
);
4688 netdev
->hwaddr
= NULL
;
4693 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
4696 struct lxc_netdev
*netdev
= data
;
4707 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
4710 struct lxc_netdev
*netdev
= data
;
4715 netdev
->priv
.vlan_attr
.vid
= 0;
4720 static int clr_config_net_ipv4_gateway(const char *key
,
4721 struct lxc_conf
*lxc_conf
, void *data
)
4723 struct lxc_netdev
*netdev
= data
;
4728 free(netdev
->ipv4_gateway
);
4729 netdev
->ipv4_gateway
= NULL
;
4734 static int clr_config_net_ipv4_address(const char *key
,
4735 struct lxc_conf
*lxc_conf
, void *data
)
4737 struct lxc_netdev
*netdev
= data
;
4738 struct lxc_list
*cur
, *next
;
4743 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
4752 static int clr_config_net_ipv6_gateway(const char *key
,
4753 struct lxc_conf
*lxc_conf
, void *data
)
4755 struct lxc_netdev
*netdev
= data
;
4760 free(netdev
->ipv6_gateway
);
4761 netdev
->ipv6_gateway
= NULL
;
4766 static int clr_config_net_ipv6_address(const char *key
,
4767 struct lxc_conf
*lxc_conf
, void *data
)
4769 struct lxc_netdev
*netdev
= data
;
4770 struct lxc_list
*cur
, *next
;
4775 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
4784 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
4785 struct lxc_conf
*c
, void *data
)
4788 const char *idxstring
;
4789 struct lxc_config_t
*config
;
4790 struct lxc_netdev
*netdev
;
4792 char *deindexed_key
= NULL
;
4794 idxstring
= key
+ 8;
4795 if (!isdigit(*idxstring
))
4798 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
4799 if (!config
|| idx
< 0)
4802 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
4804 free(deindexed_key
);
4808 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
4809 free(deindexed_key
);
4814 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
4815 struct lxc_conf
*c
, void *data
)
4819 struct lxc_netdev
*netdev
= data
;
4824 memset(retv
, 0, inlen
);
4829 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
4834 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
4835 struct lxc_conf
*c
, void *data
)
4839 struct lxc_netdev
*netdev
= data
;
4844 memset(retv
, 0, inlen
);
4849 if (netdev
->flags
& IFF_UP
)
4850 strprint(retv
, inlen
, "up");
4855 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
4856 struct lxc_conf
*c
, void *data
)
4860 struct lxc_netdev
*netdev
= data
;
4865 memset(retv
, 0, inlen
);
4870 if (netdev
->link
[0] != '\0')
4871 strprint(retv
, inlen
, "%s", netdev
->link
);
4876 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
4877 struct lxc_conf
*c
, void *data
)
4881 struct lxc_netdev
*netdev
= data
;
4886 memset(retv
, 0, inlen
);
4891 if (netdev
->name
[0] != '\0')
4892 strprint(retv
, inlen
, "%s", netdev
->name
);
4897 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
4898 struct lxc_conf
*c
, void *data
)
4903 struct lxc_netdev
*netdev
= data
;
4908 memset(retv
, 0, inlen
);
4913 if (netdev
->type
!= LXC_NET_MACVLAN
)
4916 switch (netdev
->priv
.macvlan_attr
.mode
) {
4917 case MACVLAN_MODE_PRIVATE
:
4920 case MACVLAN_MODE_VEPA
:
4923 case MACVLAN_MODE_BRIDGE
:
4926 case MACVLAN_MODE_PASSTHRU
:
4934 strprint(retv
, inlen
, "%s", mode
);
4939 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
4940 struct lxc_conf
*c
, void *data
)
4944 struct lxc_netdev
*netdev
= data
;
4949 memset(retv
, 0, inlen
);
4954 if (netdev
->type
!= LXC_NET_VETH
)
4957 strprint(retv
, inlen
, "%s",
4958 netdev
->priv
.veth_attr
.pair
[0] != '\0'
4959 ? netdev
->priv
.veth_attr
.pair
4960 : netdev
->priv
.veth_attr
.veth1
);
4965 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
4966 struct lxc_conf
*c
, void *data
)
4970 struct lxc_netdev
*netdev
= data
;
4975 memset(retv
, 0, inlen
);
4980 if (netdev
->upscript
)
4981 strprint(retv
, inlen
, "%s", netdev
->upscript
);
4986 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
4987 struct lxc_conf
*c
, void *data
)
4991 struct lxc_netdev
*netdev
= data
;
4996 memset(retv
, 0, inlen
);
5001 if (netdev
->downscript
)
5002 strprint(retv
, inlen
, "%s", netdev
->downscript
);
5007 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
5008 struct lxc_conf
*c
, void *data
)
5012 struct lxc_netdev
*netdev
= data
;
5017 memset(retv
, 0, inlen
);
5023 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
5028 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
5029 struct lxc_conf
*c
, void *data
)
5033 struct lxc_netdev
*netdev
= data
;
5038 memset(retv
, 0, inlen
);
5044 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5049 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5050 struct lxc_conf
*c
, void *data
)
5054 struct lxc_netdev
*netdev
= data
;
5059 memset(retv
, 0, inlen
);
5064 if (netdev
->type
!= LXC_NET_VLAN
)
5067 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5072 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5073 struct lxc_conf
*c
, void *data
)
5076 char buf
[INET_ADDRSTRLEN
];
5078 struct lxc_netdev
*netdev
= data
;
5083 memset(retv
, 0, inlen
);
5088 if (netdev
->ipv4_gateway_auto
) {
5089 strprint(retv
, inlen
, "auto");
5090 } else if (netdev
->ipv4_gateway
) {
5091 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5092 strprint(retv
, inlen
, "%s", buf
);
5098 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5099 struct lxc_conf
*c
, void *data
)
5103 char buf
[INET_ADDRSTRLEN
];
5104 struct lxc_list
*it
;
5106 struct lxc_netdev
*netdev
= data
;
5111 memset(retv
, 0, inlen
);
5116 listlen
= lxc_list_len(&netdev
->ipv4
);
5118 lxc_list_for_each(it
, &netdev
->ipv4
) {
5119 struct lxc_inetdev
*i
= it
->elem
;
5120 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5121 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5122 (listlen
-- > 1) ? "\n" : "");
5128 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5129 struct lxc_conf
*c
, void *data
)
5132 char buf
[INET6_ADDRSTRLEN
];
5134 struct lxc_netdev
*netdev
= data
;
5139 memset(retv
, 0, inlen
);
5144 if (netdev
->ipv6_gateway_auto
) {
5145 strprint(retv
, inlen
, "auto");
5146 } else if (netdev
->ipv6_gateway
) {
5147 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5148 strprint(retv
, inlen
, "%s", buf
);
5154 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5155 struct lxc_conf
*c
, void *data
)
5159 char buf
[INET6_ADDRSTRLEN
];
5160 struct lxc_list
*it
;
5162 struct lxc_netdev
*netdev
= data
;
5167 memset(retv
, 0, inlen
);
5172 listlen
= lxc_list_len(&netdev
->ipv6
);
5174 lxc_list_for_each(it
, &netdev
->ipv6
) {
5175 struct lxc_inet6dev
*i
= it
->elem
;
5176 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5177 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5178 (listlen
-- > 1) ? "\n" : "");
5184 int lxc_list_config_items(char *retv
, int inlen
)
5193 memset(retv
, 0, inlen
);
5195 for (i
= 0; i
< config_jump_table_size
; i
++) {
5196 char *s
= config_jump_table
[i
].name
;
5198 if (s
[strlen(s
) - 1] == '.')
5201 strprint(retv
, inlen
, "%s\n", s
);
5207 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5216 memset(retv
, 0, inlen
);
5218 if (!strcmp(key
, "lxc.apparmor")) {
5219 strprint(retv
, inlen
, "allow_incomplete\n");
5220 strprint(retv
, inlen
, "allow_nesting\n");
5221 strprint(retv
, inlen
, "profile\n");
5222 strprint(retv
, inlen
, "raw\n");
5223 } else if (!strcmp(key
, "lxc.cgroup")) {
5224 strprint(retv
, inlen
, "dir\n");
5225 } else if (!strcmp(key
, "lxc.selinux")) {
5226 strprint(retv
, inlen
, "context\n");
5227 } else if (!strcmp(key
, "lxc.mount")) {
5228 strprint(retv
, inlen
, "auto\n");
5229 strprint(retv
, inlen
, "entry\n");
5230 strprint(retv
, inlen
, "fstab\n");
5231 } else if (!strcmp(key
, "lxc.rootfs")) {
5232 strprint(retv
, inlen
, "mount\n");
5233 strprint(retv
, inlen
, "options\n");
5234 strprint(retv
, inlen
, "path\n");
5235 } else if (!strcmp(key
, "lxc.uts")) {
5236 strprint(retv
, inlen
, "name\n");
5237 } else if (!strcmp(key
, "lxc.hook")) {
5238 strprint(retv
, inlen
, "autodev\n");
5239 strprint(retv
, inlen
, "clone\n");
5240 strprint(retv
, inlen
, "destroy\n");
5241 strprint(retv
, inlen
, "mount\n");
5242 strprint(retv
, inlen
, "post-stop\n");
5243 strprint(retv
, inlen
, "pre-mount\n");
5244 strprint(retv
, inlen
, "pre-start\n");
5245 strprint(retv
, inlen
, "start-host\n");
5246 strprint(retv
, inlen
, "start\n");
5247 strprint(retv
, inlen
, "stop\n");
5248 } else if (!strcmp(key
, "lxc.cap")) {
5249 strprint(retv
, inlen
, "drop\n");
5250 strprint(retv
, inlen
, "keep\n");
5251 } else if (!strcmp(key
, "lxc.console")) {
5252 strprint(retv
, inlen
, "logfile\n");
5253 strprint(retv
, inlen
, "path\n");
5254 } else if (!strcmp(key
, "lxc.seccomp")) {
5255 strprint(retv
, inlen
, "profile\n");
5256 } else if (!strcmp(key
, "lxc.signal")) {
5257 strprint(retv
, inlen
, "halt\n");
5258 strprint(retv
, inlen
, "reboot\n");
5259 strprint(retv
, inlen
, "stop\n");
5260 } else if (!strcmp(key
, "lxc.start")) {
5261 strprint(retv
, inlen
, "auto\n");
5262 strprint(retv
, inlen
, "delay\n");
5263 strprint(retv
, inlen
, "order\n");
5264 } else if (!strcmp(key
, "lxc.monitor")) {
5265 strprint(retv
, inlen
, "unshare\n");
5273 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
5276 const char *idxstring
;
5277 struct lxc_netdev
*netdev
;
5281 idxstring
= key
+ 8;
5282 if (!isdigit(*idxstring
))
5285 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
5289 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5296 memset(retv
, 0, inlen
);
5298 strprint(retv
, inlen
, "type\n");
5299 strprint(retv
, inlen
, "script.up\n");
5300 strprint(retv
, inlen
, "script.down\n");
5302 if (netdev
->type
!= LXC_NET_EMPTY
) {
5303 strprint(retv
, inlen
, "flags\n");
5304 strprint(retv
, inlen
, "link\n");
5305 strprint(retv
, inlen
, "name\n");
5306 strprint(retv
, inlen
, "hwaddr\n");
5307 strprint(retv
, inlen
, "mtu\n");
5308 strprint(retv
, inlen
, "ipv6.address\n");
5309 strprint(retv
, inlen
, "ipv6.gateway\n");
5310 strprint(retv
, inlen
, "ipv4.address\n");
5311 strprint(retv
, inlen
, "ipv4.gateway\n");
5314 switch (netdev
->type
) {
5316 strprint(retv
, inlen
, "veth.pair\n");
5318 case LXC_NET_MACVLAN
:
5319 strprint(retv
, inlen
, "macvlan.mode\n");
5322 strprint(retv
, inlen
, "vlan.id\n");