1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #define __STDC_FORMAT_MACROS
14 #include <netinet/in.h>
19 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/utsname.h>
31 #include "confile_utils.h"
32 #include "../include/netns_ifaddrs.h"
34 #include "lxcseccomp.h"
35 #include "memory_utils.h"
41 #if HAVE_SYS_PERSONALITY_H
42 #include <sys/personality.h>
46 #include "include/strlcpy.h"
50 #include "include/strlcat.h"
53 lxc_log_define(confile
, lxc
);
55 #define lxc_config_define(name) \
56 __hot static int set_config_##name(const char *, const char *, \
57 struct lxc_conf *, void *); \
58 __hot static int get_config_##name(const char *, char *, int, \
59 struct lxc_conf *, void *); \
60 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
63 lxc_config_define(autodev
);
64 lxc_config_define(autodev_tmpfs_size
);
65 lxc_config_define(apparmor_allow_incomplete
);
66 lxc_config_define(apparmor_allow_nesting
);
67 lxc_config_define(apparmor_profile
);
68 lxc_config_define(apparmor_raw
);
69 lxc_config_define(cap_drop
);
70 lxc_config_define(cap_keep
);
71 lxc_config_define(cgroup_controller
);
72 lxc_config_define(cgroup2_controller
);
73 lxc_config_define(cgroup_dir
);
74 lxc_config_define(cgroup_relative
);
75 lxc_config_define(console_buffer_size
);
76 lxc_config_define(console_logfile
);
77 lxc_config_define(console_path
);
78 lxc_config_define(console_rotate
);
79 lxc_config_define(console_size
);
80 lxc_config_define(environment
);
81 lxc_config_define(ephemeral
);
82 lxc_config_define(execute_cmd
);
83 lxc_config_define(group
);
84 lxc_config_define(hooks
);
85 lxc_config_define(hooks_version
);
86 lxc_config_define(idmaps
);
87 lxc_config_define(includefiles
);
88 lxc_config_define(init_cmd
);
89 lxc_config_define(init_cwd
);
90 lxc_config_define(init_gid
);
91 lxc_config_define(init_uid
);
92 lxc_config_define(keyring_session
);
93 lxc_config_define(log_file
);
94 lxc_config_define(log_level
);
95 lxc_config_define(log_syslog
);
96 lxc_config_define(monitor
);
97 lxc_config_define(monitor_signal_pdeath
);
98 lxc_config_define(mount
);
99 lxc_config_define(mount_auto
);
100 lxc_config_define(mount_fstab
);
101 lxc_config_define(namespace_clone
);
102 lxc_config_define(namespace_keep
);
103 lxc_config_define(namespace_share
);
104 lxc_config_define(net
);
105 lxc_config_define(net_flags
);
106 lxc_config_define(net_hwaddr
);
107 lxc_config_define(net_ipv4_address
);
108 lxc_config_define(net_ipv4_gateway
);
109 lxc_config_define(net_ipv6_address
);
110 lxc_config_define(net_ipv6_gateway
);
111 lxc_config_define(net_link
);
112 lxc_config_define(net_l2proxy
);
113 lxc_config_define(net_macvlan_mode
);
114 lxc_config_define(net_ipvlan_mode
);
115 lxc_config_define(net_ipvlan_isolation
);
116 lxc_config_define(net_mtu
);
117 lxc_config_define(net_name
);
118 lxc_config_define(net_nic
);
119 lxc_config_define(net_script_down
);
120 lxc_config_define(net_script_up
);
121 lxc_config_define(net_type
);
122 lxc_config_define(net_veth_mode
);
123 lxc_config_define(net_veth_pair
);
124 lxc_config_define(net_veth_ipv4_route
);
125 lxc_config_define(net_veth_ipv6_route
);
126 lxc_config_define(net_vlan_id
);
127 lxc_config_define(no_new_privs
);
128 lxc_config_define(personality
);
129 lxc_config_define(prlimit
);
130 lxc_config_define(pty_max
);
131 lxc_config_define(rootfs_managed
);
132 lxc_config_define(rootfs_mount
);
133 lxc_config_define(rootfs_options
);
134 lxc_config_define(rootfs_path
);
135 lxc_config_define(seccomp_profile
);
136 lxc_config_define(seccomp_allow_nesting
);
137 lxc_config_define(seccomp_notify_cookie
);
138 lxc_config_define(seccomp_notify_proxy
);
139 lxc_config_define(selinux_context
);
140 lxc_config_define(selinux_context_keyring
);
141 lxc_config_define(signal_halt
);
142 lxc_config_define(signal_reboot
);
143 lxc_config_define(signal_stop
);
144 lxc_config_define(start
);
145 lxc_config_define(tty_max
);
146 lxc_config_define(tty_dir
);
147 lxc_config_define(uts_name
);
148 lxc_config_define(sysctl
);
149 lxc_config_define(proc
);
153 * If a new config option is added to this table, be aware that
154 * the order in which the options are places into the table matters.
155 * That means that more specific options of a namespace have to be
156 * placed above more generic ones.
158 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
159 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
160 * has to be placed above lxc.ab.
162 static struct lxc_config_t config_jump_table
[] = {
163 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
164 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
165 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
166 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
167 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
168 { "lxc.autodev.tmpfs.size", set_config_autodev_tmpfs_size
, get_config_autodev_tmpfs_size
, clr_config_autodev_tmpfs_size
, },
169 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
170 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
171 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
172 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
173 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
174 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
175 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
176 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
177 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
178 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
179 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
180 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
181 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
182 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
183 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
184 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
185 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
186 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
187 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
188 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
189 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
190 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
191 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
192 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
193 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
194 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
195 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
196 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
197 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
198 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
199 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
200 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
201 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
202 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
203 { "lxc.keyring.session", set_config_keyring_session
, get_config_keyring_session
, clr_config_keyring_session
},
204 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
205 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
206 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
207 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
208 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
209 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
210 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
211 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
212 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
213 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
214 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
215 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
216 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
217 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
218 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
219 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
220 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
221 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
222 { "lxc.net.l2proxy", set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
223 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
224 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
225 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
226 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
227 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
228 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
229 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
230 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
231 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
232 { "lxc.net.veth.mode", set_config_net_veth_mode
, get_config_net_veth_mode
, clr_config_net_veth_mode
, },
233 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
234 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
235 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
236 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
237 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
238 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
239 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
240 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
241 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
242 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
243 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
244 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
245 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
246 { "lxc.seccomp.notify.cookie", set_config_seccomp_notify_cookie
, get_config_seccomp_notify_cookie
, clr_config_seccomp_notify_cookie
, },
247 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
248 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
249 { "lxc.selinux.context.keyring", set_config_selinux_context_keyring
, get_config_selinux_context_keyring
, clr_config_selinux_context_keyring
},
250 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
251 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
252 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
253 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
254 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
255 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
256 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
257 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
258 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
259 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
260 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
261 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
264 static const size_t config_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
266 struct lxc_config_t
*lxc_get_config(const char *key
)
270 for (i
= 0; i
< config_jump_table_size
; i
++)
271 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
272 return &config_jump_table
[i
];
277 static int set_config_net(const char *key
, const char *value
,
278 struct lxc_conf
*lxc_conf
, void *data
)
280 if (!lxc_config_value_empty(value
)) {
281 ERROR("lxc.net must not have a value");
285 return clr_config_net(key
, lxc_conf
, data
);
288 static int set_config_net_type(const char *key
, const char *value
,
289 struct lxc_conf
*lxc_conf
, void *data
)
291 struct lxc_netdev
*netdev
= data
;
293 if (lxc_config_value_empty(value
))
294 return clr_config_net_type(key
, lxc_conf
, data
);
299 if (strcmp(value
, "veth") == 0) {
300 netdev
->type
= LXC_NET_VETH
;
301 lxc_list_init(&netdev
->priv
.veth_attr
.ipv4_routes
);
302 lxc_list_init(&netdev
->priv
.veth_attr
.ipv6_routes
);
303 lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, "bridge");
304 } else if (strcmp(value
, "macvlan") == 0) {
305 netdev
->type
= LXC_NET_MACVLAN
;
306 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
307 } else if (strcmp(value
, "ipvlan") == 0) {
308 netdev
->type
= LXC_NET_IPVLAN
;
309 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
310 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
311 } else if (strcmp(value
, "vlan") == 0) {
312 netdev
->type
= LXC_NET_VLAN
;
313 } else if (strcmp(value
, "phys") == 0) {
314 netdev
->type
= LXC_NET_PHYS
;
315 } else if (strcmp(value
, "empty") == 0) {
316 netdev
->type
= LXC_NET_EMPTY
;
317 } else if (strcmp(value
, "none") == 0) {
318 netdev
->type
= LXC_NET_NONE
;
320 ERROR("Invalid network type %s", value
);
327 static int set_config_net_flags(const char *key
, const char *value
,
328 struct lxc_conf
*lxc_conf
, void *data
)
330 struct lxc_netdev
*netdev
= data
;
332 if (lxc_config_value_empty(value
))
333 return clr_config_net_flags(key
, lxc_conf
, data
);
338 netdev
->flags
|= IFF_UP
;
343 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
344 struct lxc_netdev
*netdev
)
346 struct netns_ifaddrs
*ifaddr
, *ifa
;
349 const char *type_key
= "lxc.net.type";
350 const char *link_key
= "lxc.net.link";
351 const char *tmpvalue
= "phys";
353 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0) {
354 SYSERROR("Failed to get network interfaces");
358 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
362 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
365 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
366 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
369 ret
= set_config_net_link(
370 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
372 ERROR("Failed to create matched ifnames");
376 ERROR("Failed to create matched ifnames");
382 netns_freeifaddrs(ifaddr
);
388 static int set_config_net_link(const char *key
, const char *value
,
389 struct lxc_conf
*lxc_conf
, void *data
)
391 struct lxc_netdev
*netdev
= data
;
394 if (lxc_config_value_empty(value
))
395 return clr_config_net_link(key
, lxc_conf
, data
);
400 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
401 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
403 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
408 static int set_config_net_l2proxy(const char *key
, const char *value
,
409 struct lxc_conf
*lxc_conf
, void *data
)
411 struct lxc_netdev
*netdev
= data
;
412 unsigned int val
= 0;
415 if (lxc_config_value_empty(value
))
416 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
419 return ret_set_errno(-1, EINVAL
);
421 ret
= lxc_safe_uint(value
, &val
);
423 return ret_set_errno(-1, -ret
);
427 netdev
->l2proxy
= false;
430 netdev
->l2proxy
= true;
434 return ret_set_errno(-1, EINVAL
);
437 static int set_config_net_name(const char *key
, const char *value
,
438 struct lxc_conf
*lxc_conf
, void *data
)
440 struct lxc_netdev
*netdev
= data
;
442 if (lxc_config_value_empty(value
))
443 return clr_config_net_name(key
, lxc_conf
, data
);
448 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
452 static int set_config_net_veth_mode(const char *key
, const char *value
,
453 struct lxc_conf
*lxc_conf
, void *data
)
455 struct lxc_netdev
*netdev
= data
;
457 if (lxc_config_value_empty(value
))
458 return clr_config_net_veth_mode(key
, lxc_conf
, data
);
463 return lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, value
);
466 static int set_config_net_veth_pair(const char *key
, const char *value
,
467 struct lxc_conf
*lxc_conf
, void *data
)
469 struct lxc_netdev
*netdev
= data
;
471 if (lxc_config_value_empty(value
))
472 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
477 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
, sizeof(netdev
->priv
.veth_attr
.pair
));
480 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
481 struct lxc_conf
*lxc_conf
, void *data
)
483 struct lxc_netdev
*netdev
= data
;
485 if (lxc_config_value_empty(value
))
486 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
491 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
494 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
495 struct lxc_conf
*lxc_conf
, void *data
)
497 struct lxc_netdev
*netdev
= data
;
499 if (lxc_config_value_empty(value
))
500 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
503 return ret_set_errno(-1, EINVAL
);
505 if (netdev
->type
!= LXC_NET_IPVLAN
) {
506 SYSERROR("Invalid ipvlan mode \"%s\", can only be used with ipvlan network", value
);
507 return ret_set_errno(-1, EINVAL
);
510 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
513 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
514 struct lxc_conf
*lxc_conf
, void *data
)
516 struct lxc_netdev
*netdev
= data
;
518 if (lxc_config_value_empty(value
))
519 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
522 return ret_set_errno(-1, EINVAL
);
524 if (netdev
->type
!= LXC_NET_IPVLAN
) {
525 SYSERROR("Invalid ipvlan isolation \"%s\", can only be used with ipvlan network", value
);
526 return ret_set_errno(-1, EINVAL
);
529 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
532 static int set_config_net_hwaddr(const char *key
, const char *value
,
533 struct lxc_conf
*lxc_conf
, void *data
)
535 struct lxc_netdev
*netdev
= data
;
538 if (lxc_config_value_empty(value
))
539 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
544 new_value
= strdup(value
);
548 rand_complete_hwaddr(new_value
);
550 if (lxc_config_value_empty(new_value
)) {
552 netdev
->hwaddr
= NULL
;
556 netdev
->hwaddr
= new_value
;
561 static int set_config_net_vlan_id(const char *key
, const char *value
,
562 struct lxc_conf
*lxc_conf
, void *data
)
565 struct lxc_netdev
*netdev
= data
;
567 if (lxc_config_value_empty(value
))
568 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
573 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
580 static int set_config_net_mtu(const char *key
, const char *value
,
581 struct lxc_conf
*lxc_conf
, void *data
)
583 struct lxc_netdev
*netdev
= data
;
585 if (lxc_config_value_empty(value
))
586 return clr_config_net_mtu(key
, lxc_conf
, data
);
591 return set_config_string_item(&netdev
->mtu
, value
);
594 static int set_config_net_ipv4_address(const char *key
, const char *value
,
595 struct lxc_conf
*lxc_conf
, void *data
)
598 struct lxc_netdev
*netdev
= data
;
599 struct lxc_inetdev
*inetdev
;
600 struct lxc_list
*list
;
601 char *cursor
, *slash
;
602 char *addr
= NULL
, *bcast
= NULL
, *prefix
= NULL
;
604 if (lxc_config_value_empty(value
))
605 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
610 inetdev
= malloc(sizeof(*inetdev
));
613 memset(inetdev
, 0, sizeof(*inetdev
));
615 list
= malloc(sizeof(*list
));
622 list
->elem
= inetdev
;
624 addr
= strdup(value
);
631 cursor
= strstr(addr
, " ");
637 slash
= strstr(addr
, "/");
643 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
644 if (!ret
|| ret
< 0) {
645 SYSERROR("Invalid ipv4 address \"%s\"", value
);
653 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
654 if (!ret
|| ret
< 0) {
655 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value
);
664 /* No prefix specified, determine it from the network class. */
666 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
674 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
677 /* If no broadcast address, let compute one from the
678 * prefix and address.
681 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
682 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
685 lxc_list_add_tail(&netdev
->ipv4
, list
);
691 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
692 struct lxc_conf
*lxc_conf
, void *data
)
694 struct lxc_netdev
*netdev
= data
;
696 if (lxc_config_value_empty(value
))
697 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
702 free(netdev
->ipv4_gateway
);
704 if (strcmp(value
, "auto") == 0) {
705 netdev
->ipv4_gateway
= NULL
;
706 netdev
->ipv4_gateway_auto
= true;
707 } else if (strcmp(value
, "dev") == 0) {
708 netdev
->ipv4_gateway
= NULL
;
709 netdev
->ipv4_gateway_auto
= false;
710 netdev
->ipv4_gateway_dev
= true;
715 gw
= malloc(sizeof(*gw
));
719 ret
= inet_pton(AF_INET
, value
, gw
);
720 if (!ret
|| ret
< 0) {
721 SYSERROR("Invalid ipv4 gateway address \"%s\"", value
);
726 netdev
->ipv4_gateway
= gw
;
727 netdev
->ipv4_gateway_auto
= false;
733 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
734 struct lxc_conf
*lxc_conf
, void *data
)
736 __do_free
char *valdup
= NULL
;
737 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
738 __do_free
struct lxc_list
*list
= NULL
;
740 char *netmask
, *slash
;
741 struct lxc_netdev
*netdev
= data
;
743 if (lxc_config_value_empty(value
))
744 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
747 return ret_set_errno(-1, EINVAL
);
749 if (netdev
->type
!= LXC_NET_VETH
) {
750 SYSERROR("Invalid ipv4 route \"%s\", can only be used with veth network", value
);
751 return ret_set_errno(-1, EINVAL
);
754 inetdev
= malloc(sizeof(*inetdev
));
757 memset(inetdev
, 0, sizeof(*inetdev
));
759 list
= malloc(sizeof(*list
));
764 list
->elem
= inetdev
;
766 valdup
= strdup(value
);
770 slash
= strchr(valdup
, '/');
772 return ret_set_errno(-1, EINVAL
);
777 return ret_set_errno(-1, EINVAL
);
781 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
782 if (ret
< 0 || inetdev
->prefix
> 32)
783 return ret_set_errno(-1, EINVAL
);
785 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
787 return ret_set_errno(-1, EINVAL
);
789 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv4_routes
, list
);
796 static int set_config_net_ipv6_address(const char *key
, const char *value
,
797 struct lxc_conf
*lxc_conf
, void *data
)
800 struct lxc_netdev
*netdev
= data
;
801 struct lxc_inet6dev
*inet6dev
;
802 struct lxc_list
*list
;
803 char *slash
, *valdup
, *netmask
;
805 if (lxc_config_value_empty(value
))
806 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
811 inet6dev
= malloc(sizeof(*inet6dev
));
814 memset(inet6dev
, 0, sizeof(*inet6dev
));
816 list
= malloc(sizeof(*list
));
823 list
->elem
= inet6dev
;
825 valdup
= strdup(value
);
832 inet6dev
->prefix
= 64;
833 slash
= strstr(valdup
, "/");
838 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
847 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
848 if (!ret
|| ret
< 0) {
849 SYSERROR("Invalid ipv6 address \"%s\"", valdup
);
856 lxc_list_add_tail(&netdev
->ipv6
, list
);
862 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
863 struct lxc_conf
*lxc_conf
, void *data
)
865 struct lxc_netdev
*netdev
= data
;
867 if (lxc_config_value_empty(value
))
868 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
873 free(netdev
->ipv6_gateway
);
875 if (strcmp(value
, "auto") == 0) {
876 netdev
->ipv6_gateway
= NULL
;
877 netdev
->ipv6_gateway_auto
= true;
878 } else if (strcmp(value
, "dev") == 0) {
879 netdev
->ipv6_gateway
= NULL
;
880 netdev
->ipv6_gateway_auto
= false;
881 netdev
->ipv6_gateway_dev
= true;
886 gw
= malloc(sizeof(*gw
));
890 ret
= inet_pton(AF_INET6
, value
, gw
);
891 if (!ret
|| ret
< 0) {
892 SYSERROR("Invalid ipv6 gateway address \"%s\"", value
);
897 netdev
->ipv6_gateway
= gw
;
898 netdev
->ipv6_gateway_auto
= false;
904 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
905 struct lxc_conf
*lxc_conf
, void *data
)
907 __do_free
char *valdup
= NULL
;
908 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
909 __do_free
struct lxc_list
*list
= NULL
;
911 char *netmask
, *slash
;
912 struct lxc_netdev
*netdev
= data
;
914 if (lxc_config_value_empty(value
))
915 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
918 return ret_set_errno(-1, EINVAL
);
920 if (netdev
->type
!= LXC_NET_VETH
) {
921 SYSERROR("Invalid ipv6 route \"%s\", can only be used with veth network", value
);
922 return ret_set_errno(-1, EINVAL
);
925 inet6dev
= malloc(sizeof(*inet6dev
));
928 memset(inet6dev
, 0, sizeof(*inet6dev
));
930 list
= malloc(sizeof(*list
));
935 list
->elem
= inet6dev
;
937 valdup
= strdup(value
);
941 slash
= strchr(valdup
, '/');
943 return ret_set_errno(-1, EINVAL
);
948 return ret_set_errno(-1, EINVAL
);
952 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
953 if (ret
< 0 || inet6dev
->prefix
> 128)
954 return ret_set_errno(-1, EINVAL
);
956 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
958 return ret_set_errno(-1, EINVAL
);
960 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv6_routes
, list
);
967 static int set_config_net_script_up(const char *key
, const char *value
,
968 struct lxc_conf
*lxc_conf
, void *data
)
970 struct lxc_netdev
*netdev
= data
;
972 if (lxc_config_value_empty(value
))
973 return clr_config_net_script_up(key
, lxc_conf
, data
);
978 return set_config_string_item(&netdev
->upscript
, value
);
981 static int set_config_net_script_down(const char *key
, const char *value
,
982 struct lxc_conf
*lxc_conf
, void *data
)
984 struct lxc_netdev
*netdev
= data
;
986 if (lxc_config_value_empty(value
))
987 return clr_config_net_script_down(key
, lxc_conf
, data
);
992 return set_config_string_item(&netdev
->downscript
, value
);
995 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, char *hook
)
997 struct lxc_list
*hooklist
;
999 hooklist
= malloc(sizeof(*hooklist
));
1005 hooklist
->elem
= hook
;
1006 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1011 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1012 struct lxc_conf
*lxc_conf
, void *data
)
1015 if (lxc_config_value_empty(value
))
1016 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1018 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1021 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1022 return ret_set_errno(-1, EINVAL
);
1031 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1032 struct lxc_conf
*lxc_conf
, void *data
)
1034 #ifdef HAVE_SECCOMP_NOTIFY
1035 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1037 return ret_set_errno(-1, ENOSYS
);
1041 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1042 struct lxc_conf
*lxc_conf
, void *data
)
1044 #ifdef HAVE_SECCOMP_NOTIFY
1047 if (lxc_config_value_empty(value
))
1048 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1050 if (strncmp(value
, "unix:", 5) != 0)
1051 return ret_set_errno(-1, EINVAL
);
1054 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1059 return ret_set_errno(-1, ENOSYS
);
1063 static int set_config_seccomp_profile(const char *key
, const char *value
,
1064 struct lxc_conf
*lxc_conf
, void *data
)
1066 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1069 static int set_config_execute_cmd(const char *key
, const char *value
,
1070 struct lxc_conf
*lxc_conf
, void *data
)
1072 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1075 static int set_config_init_cmd(const char *key
, const char *value
,
1076 struct lxc_conf
*lxc_conf
, void *data
)
1078 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1081 static int set_config_init_cwd(const char *key
, const char *value
,
1082 struct lxc_conf
*lxc_conf
, void *data
)
1084 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1087 static int set_config_init_uid(const char *key
, const char *value
,
1088 struct lxc_conf
*lxc_conf
, void *data
)
1090 unsigned int init_uid
;
1092 if (lxc_config_value_empty(value
)) {
1093 lxc_conf
->init_uid
= 0;
1097 if (lxc_safe_uint(value
, &init_uid
) < 0)
1100 lxc_conf
->init_uid
= init_uid
;
1105 static int set_config_init_gid(const char *key
, const char *value
,
1106 struct lxc_conf
*lxc_conf
, void *data
)
1108 unsigned int init_gid
;
1110 if (lxc_config_value_empty(value
)) {
1111 lxc_conf
->init_gid
= 0;
1115 if (lxc_safe_uint(value
, &init_gid
) < 0)
1118 lxc_conf
->init_gid
= init_gid
;
1123 static int set_config_hooks(const char *key
, const char *value
,
1124 struct lxc_conf
*lxc_conf
, void *data
)
1128 if (lxc_config_value_empty(value
))
1129 return lxc_clear_hooks(lxc_conf
, key
);
1131 if (strcmp(key
+ 4, "hook") == 0) {
1132 ERROR("lxc.hook must not have a value");
1136 copy
= strdup(value
);
1140 if (strcmp(key
+ 9, "pre-start") == 0)
1141 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, copy
);
1142 else if (strcmp(key
+ 9, "start-host") == 0)
1143 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, copy
);
1144 else if (strcmp(key
+ 9, "pre-mount") == 0)
1145 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, copy
);
1146 else if (strcmp(key
+ 9, "autodev") == 0)
1147 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, copy
);
1148 else if (strcmp(key
+ 9, "mount") == 0)
1149 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, copy
);
1150 else if (strcmp(key
+ 9, "start") == 0)
1151 return add_hook(lxc_conf
, LXCHOOK_START
, copy
);
1152 else if (strcmp(key
+ 9, "stop") == 0)
1153 return add_hook(lxc_conf
, LXCHOOK_STOP
, copy
);
1154 else if (strcmp(key
+ 9, "post-stop") == 0)
1155 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, copy
);
1156 else if (strcmp(key
+ 9, "clone") == 0)
1157 return add_hook(lxc_conf
, LXCHOOK_CLONE
, copy
);
1158 else if (strcmp(key
+ 9, "destroy") == 0)
1159 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, copy
);
1166 static int set_config_hooks_version(const char *key
, const char *value
,
1167 struct lxc_conf
*lxc_conf
, void *data
)
1172 if (lxc_config_value_empty(value
))
1173 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1175 ret
= lxc_safe_uint(value
, &tmp
);
1180 ERROR("Invalid hook version specified. Currently only 0 "
1181 "(legacy) and 1 are supported");
1185 lxc_conf
->hooks_version
= tmp
;
1190 static int set_config_personality(const char *key
, const char *value
,
1191 struct lxc_conf
*lxc_conf
, void *data
)
1193 signed long personality
= lxc_config_parse_arch(value
);
1195 if (personality
>= 0)
1196 lxc_conf
->personality
= personality
;
1198 WARN("Unsupported personality \"%s\"", value
);
1203 static int set_config_pty_max(const char *key
, const char *value
,
1204 struct lxc_conf
*lxc_conf
, void *data
)
1207 unsigned int max
= 0;
1209 if (lxc_config_value_empty(value
)) {
1210 lxc_conf
->pty_max
= 0;
1214 ret
= lxc_safe_uint(value
, &max
);
1218 lxc_conf
->pty_max
= max
;
1223 /* We only need to check whether the first byte of the key after the lxc.start.
1224 * prefix matches our expectations since they fortunately all start with a
1225 * different letter. If anything was wrong with the key we would have already
1226 * noticed when the callback was called.
1228 static int set_config_start(const char *key
, const char *value
,
1229 struct lxc_conf
*lxc_conf
, void *data
)
1233 is_empty
= lxc_config_value_empty(value
);
1235 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1237 lxc_conf
->start_auto
= 0;
1241 if (lxc_safe_uint(value
, &lxc_conf
->start_auto
) < 0)
1244 if (lxc_conf
->start_auto
> 1)
1248 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1250 lxc_conf
->start_delay
= 0;
1254 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1255 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1257 lxc_conf
->start_order
= 0;
1261 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1267 static int set_config_monitor(const char *key
, const char *value
,
1268 struct lxc_conf
*lxc_conf
, void *data
)
1270 if (lxc_config_value_empty(value
)) {
1271 lxc_conf
->monitor_unshare
= 0;
1275 if (strcmp(key
+ 12, "unshare") == 0)
1276 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1281 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1282 struct lxc_conf
*lxc_conf
, void *data
)
1284 if (lxc_config_value_empty(value
)) {
1285 lxc_conf
->monitor_signal_pdeath
= 0;
1289 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1292 sig_n
= sig_parse(value
);
1296 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1303 static int set_config_group(const char *key
, const char *value
,
1304 struct lxc_conf
*lxc_conf
, void *data
)
1306 char *groups
, *token
;
1307 struct lxc_list
*grouplist
;
1310 if (lxc_config_value_empty(value
))
1311 return lxc_clear_groups(lxc_conf
);
1313 groups
= strdup(value
);
1317 /* In case several groups are specified in a single line split these
1318 * groups in a single element for the list.
1320 lxc_iterate_parts(token
, groups
, " \t") {
1321 grouplist
= malloc(sizeof(*grouplist
));
1327 grouplist
->elem
= strdup(token
);
1328 if (!grouplist
->elem
) {
1334 lxc_list_add_tail(&lxc_conf
->groups
, grouplist
);
1342 static int set_config_environment(const char *key
, const char *value
,
1343 struct lxc_conf
*lxc_conf
, void *data
)
1345 struct lxc_list
*list_item
= NULL
;
1347 if (lxc_config_value_empty(value
))
1348 return lxc_clear_environment(lxc_conf
);
1350 list_item
= malloc(sizeof(*list_item
));
1354 if (!strchr(value
, '=')) {
1355 const char *env_val
;
1356 const char *env_key
= value
;
1357 const char *env_var
[3] = {0};
1359 env_val
= getenv(env_key
);
1363 env_var
[0] = env_key
;
1364 env_var
[1] = env_val
;
1365 list_item
->elem
= lxc_string_join("=", env_var
, false);
1367 list_item
->elem
= strdup(value
);
1370 if (!list_item
->elem
)
1373 lxc_list_add_tail(&lxc_conf
->environment
, list_item
);
1383 static int set_config_tty_max(const char *key
, const char *value
,
1384 struct lxc_conf
*lxc_conf
, void *data
)
1387 unsigned int nbtty
= 0;
1389 if (lxc_config_value_empty(value
)) {
1390 lxc_conf
->ttys
.max
= 0;
1394 ret
= lxc_safe_uint(value
, &nbtty
);
1398 lxc_conf
->ttys
.max
= nbtty
;
1403 static int set_config_tty_dir(const char *key
, const char *value
,
1404 struct lxc_conf
*lxc_conf
, void *data
)
1406 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1410 static int set_config_apparmor_profile(const char *key
, const char *value
,
1411 struct lxc_conf
*lxc_conf
, void *data
)
1413 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1416 static int set_config_apparmor_allow_incomplete(const char *key
,
1418 struct lxc_conf
*lxc_conf
,
1421 if (lxc_config_value_empty(value
)) {
1422 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1426 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
) < 0)
1429 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1435 static int set_config_apparmor_allow_nesting(const char *key
,
1437 struct lxc_conf
*lxc_conf
,
1440 if (lxc_config_value_empty(value
))
1441 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1443 if (lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
) < 0)
1446 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1452 static int set_config_apparmor_raw(const char *key
,
1454 struct lxc_conf
*lxc_conf
,
1458 struct lxc_list
*list
;
1460 if (lxc_config_value_empty(value
))
1461 return lxc_clear_apparmor_raw(lxc_conf
);
1463 list
= malloc(sizeof(*list
));
1469 elem
= strdup(value
);
1476 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, list
);
1481 static int set_config_selinux_context(const char *key
, const char *value
,
1482 struct lxc_conf
*lxc_conf
, void *data
)
1484 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1487 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1488 struct lxc_conf
*lxc_conf
, void *data
)
1490 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1493 static int set_config_keyring_session(const char *key
, const char *value
,
1494 struct lxc_conf
*lxc_conf
, void *data
)
1496 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1499 static int set_config_log_file(const char *key
, const char *value
,
1500 struct lxc_conf
*c
, void *data
)
1504 if (lxc_config_value_empty(value
)) {
1510 /* Store these values in the lxc_conf, and then try to set for actual
1513 ret
= set_config_path_item(&c
->logfile
, value
);
1515 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1520 static int set_config_log_level(const char *key
, const char *value
,
1521 struct lxc_conf
*lxc_conf
, void *data
)
1525 if (lxc_config_value_empty(value
)) {
1526 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1530 if (value
[0] >= '0' && value
[0] <= '9') {
1531 if (lxc_safe_int(value
, &newlevel
) < 0)
1534 newlevel
= lxc_log_priority_to_int(value
);
1537 /* Store these values in the lxc_conf, and then try to set for actual
1540 lxc_conf
->loglevel
= newlevel
;
1542 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1545 static int set_config_autodev(const char *key
, const char *value
,
1546 struct lxc_conf
*lxc_conf
, void *data
)
1548 if (lxc_config_value_empty(value
)) {
1549 lxc_conf
->autodev
= 0;
1553 if (lxc_safe_uint(value
, &lxc_conf
->autodev
) < 0)
1556 if (lxc_conf
->autodev
> 1)
1562 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1563 struct lxc_conf
*lxc_conf
, void *data
)
1565 if (lxc_config_value_empty(value
)) {
1566 lxc_conf
->autodevtmpfssize
= 500000;
1570 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1571 lxc_conf
->autodevtmpfssize
= 500000;
1576 static int set_config_signal_halt(const char *key
, const char *value
,
1577 struct lxc_conf
*lxc_conf
, void *data
)
1581 if (lxc_config_value_empty(value
)) {
1582 lxc_conf
->haltsignal
= 0;
1586 sig_n
= sig_parse(value
);
1590 lxc_conf
->haltsignal
= sig_n
;
1595 static int set_config_signal_reboot(const char *key
, const char *value
,
1596 struct lxc_conf
*lxc_conf
, void *data
)
1600 if (lxc_config_value_empty(value
)) {
1601 lxc_conf
->rebootsignal
= 0;
1605 sig_n
= sig_parse(value
);
1609 lxc_conf
->rebootsignal
= sig_n
;
1614 static int set_config_signal_stop(const char *key
, const char *value
,
1615 struct lxc_conf
*lxc_conf
, void *data
)
1619 if (lxc_config_value_empty(value
)) {
1620 lxc_conf
->stopsignal
= 0;
1624 sig_n
= sig_parse(value
);
1628 lxc_conf
->stopsignal
= sig_n
;
1633 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1634 struct lxc_conf
*lxc_conf
, int version
)
1636 const char *subkey
, *token
;
1638 struct lxc_list
*cglist
= NULL
;
1639 struct lxc_cgroup
*cgelem
= NULL
;
1641 if (lxc_config_value_empty(value
))
1642 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1644 if (version
== CGROUP2_SUPER_MAGIC
) {
1645 token
= "lxc.cgroup2.";
1647 } else if (version
== CGROUP_SUPER_MAGIC
) {
1648 token
= "lxc.cgroup.";
1654 if (strncmp(key
, token
, token_len
) != 0)
1657 subkey
= key
+ token_len
;
1658 if (*subkey
== '\0')
1661 cglist
= malloc(sizeof(*cglist
));
1665 cgelem
= malloc(sizeof(*cgelem
));
1668 memset(cgelem
, 0, sizeof(*cgelem
));
1670 cgelem
->subsystem
= strdup(subkey
);
1671 if (!cgelem
->subsystem
)
1674 cgelem
->value
= strdup(value
);
1678 cgelem
->version
= version
;
1680 lxc_list_add_elem(cglist
, cgelem
);
1682 if (version
== CGROUP2_SUPER_MAGIC
)
1683 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1685 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1692 free(cgelem
->subsystem
);
1693 free(cgelem
->value
);
1700 static int set_config_cgroup_controller(const char *key
, const char *value
,
1701 struct lxc_conf
*lxc_conf
, void *data
)
1703 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1704 CGROUP_SUPER_MAGIC
);
1707 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1708 struct lxc_conf
*lxc_conf
, void *data
)
1710 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1711 CGROUP2_SUPER_MAGIC
);
1715 static int set_config_cgroup_dir(const char *key
, const char *value
,
1716 struct lxc_conf
*lxc_conf
, void *data
)
1718 if (lxc_config_value_empty(value
))
1719 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1721 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1724 static int set_config_cgroup_relative(const char *key
, const char *value
,
1725 struct lxc_conf
*lxc_conf
, void *data
)
1727 unsigned int converted
;
1730 if (lxc_config_value_empty(value
))
1731 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1733 ret
= lxc_safe_uint(value
, &converted
);
1737 if (converted
== 1) {
1738 lxc_conf
->cgroup_meta
.relative
= true;
1742 if (converted
== 0) {
1743 lxc_conf
->cgroup_meta
.relative
= false;
1750 static bool parse_limit_value(const char **value
, rlim_t
*res
)
1752 char *endptr
= NULL
;
1754 if (strncmp(*value
, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1755 *res
= RLIM_INFINITY
;
1756 *value
+= STRLITERALLEN("unlimited");
1761 *res
= strtoull(*value
, &endptr
, 10);
1762 if (errno
|| !endptr
)
1770 static int set_config_prlimit(const char *key
, const char *value
,
1771 struct lxc_conf
*lxc_conf
, void *data
)
1773 struct lxc_list
*iter
;
1774 struct rlimit limit
;
1776 struct lxc_list
*limlist
= NULL
;
1777 struct lxc_limit
*limelem
= NULL
;
1779 if (lxc_config_value_empty(value
))
1780 return lxc_clear_limits(lxc_conf
, key
);
1782 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1785 key
+= STRLITERALLEN("lxc.prlimit.");
1787 /* soft limit comes first in the value */
1788 if (!parse_limit_value(&value
, &limit_value
))
1791 limit
.rlim_cur
= limit_value
;
1793 /* skip spaces and a colon */
1794 while (isspace(*value
))
1799 else if (*value
) /* any other character is an error here */
1802 while (isspace(*value
))
1805 /* optional hard limit */
1807 if (!parse_limit_value(&value
, &limit_value
))
1810 limit
.rlim_max
= limit_value
;
1812 /* check for trailing garbage */
1813 while (isspace(*value
))
1819 /* a single value sets both hard and soft limit */
1820 limit
.rlim_max
= limit
.rlim_cur
;
1823 /* find existing list element */
1824 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1825 limelem
= iter
->elem
;
1826 if (!strcmp(key
, limelem
->resource
)) {
1827 limelem
->limit
= limit
;
1832 /* allocate list element */
1833 limlist
= malloc(sizeof(*limlist
));
1837 limelem
= malloc(sizeof(*limelem
));
1840 memset(limelem
, 0, sizeof(*limelem
));
1842 limelem
->resource
= strdup(key
);
1843 if (!limelem
->resource
)
1846 limelem
->limit
= limit
;
1847 lxc_list_add_elem(limlist
, limelem
);;
1848 lxc_list_add_tail(&lxc_conf
->limits
, limlist
);
1856 free(limelem
->resource
);
1863 static int set_config_sysctl(const char *key
, const char *value
,
1864 struct lxc_conf
*lxc_conf
, void *data
)
1866 struct lxc_list
*iter
;
1867 char *replace_value
= NULL
;
1868 struct lxc_list
*sysctl_list
= NULL
;
1869 struct lxc_sysctl
*sysctl_elem
= NULL
;
1871 if (lxc_config_value_empty(value
))
1872 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1874 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1877 key
+= STRLITERALLEN("lxc.sysctl.");
1879 /* find existing list element */
1880 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1881 sysctl_elem
= iter
->elem
;
1883 if (strcmp(key
, sysctl_elem
->key
) != 0)
1886 replace_value
= strdup(value
);
1890 free(sysctl_elem
->value
);
1891 sysctl_elem
->value
= replace_value
;
1896 /* allocate list element */
1897 sysctl_list
= malloc(sizeof(*sysctl_list
));
1901 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1904 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1906 sysctl_elem
->key
= strdup(key
);
1907 if (!sysctl_elem
->key
)
1910 sysctl_elem
->value
= strdup(value
);
1911 if (!sysctl_elem
->value
)
1914 lxc_list_add_elem(sysctl_list
, sysctl_elem
);
1915 lxc_list_add_tail(&lxc_conf
->sysctls
, sysctl_list
);
1923 free(sysctl_elem
->key
);
1924 free(sysctl_elem
->value
);
1931 static int set_config_proc(const char *key
, const char *value
,
1932 struct lxc_conf
*lxc_conf
, void *data
)
1935 struct lxc_list
*proclist
= NULL
;
1936 struct lxc_proc
*procelem
= NULL
;
1938 if (lxc_config_value_empty(value
))
1939 return clr_config_proc(key
, lxc_conf
, NULL
);
1941 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
1944 subkey
= key
+ STRLITERALLEN("lxc.proc.");
1945 if (*subkey
== '\0')
1948 proclist
= malloc(sizeof(*proclist
));
1952 procelem
= malloc(sizeof(*procelem
));
1955 memset(procelem
, 0, sizeof(*procelem
));
1957 procelem
->filename
= strdup(subkey
);
1958 procelem
->value
= strdup(value
);
1960 if (!procelem
->filename
|| !procelem
->value
)
1963 proclist
->elem
= procelem
;
1965 lxc_list_add_tail(&lxc_conf
->procs
, proclist
);
1973 free(procelem
->filename
);
1974 free(procelem
->value
);
1981 static int set_config_idmaps(const char *key
, const char *value
,
1982 struct lxc_conf
*lxc_conf
, void *data
)
1984 unsigned long hostid
, nsid
, range
;
1987 struct lxc_list
*idmaplist
= NULL
;
1988 struct id_map
*idmap
= NULL
;
1990 if (lxc_config_value_empty(value
))
1991 return lxc_clear_idmaps(lxc_conf
);
1993 idmaplist
= malloc(sizeof(*idmaplist
));
1997 idmap
= malloc(sizeof(*idmap
));
2000 memset(idmap
, 0, sizeof(*idmap
));
2002 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2004 ERROR("Failed to parse id mappings");
2008 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2010 idmap
->idtype
= ID_TYPE_UID
;
2011 else if (type
== 'g')
2012 idmap
->idtype
= ID_TYPE_GID
;
2016 idmap
->hostid
= hostid
;
2018 idmap
->range
= range
;
2019 idmaplist
->elem
= idmap
;
2020 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
2022 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2023 if (idmap
->nsid
== 0)
2024 lxc_conf
->root_nsuid_map
= idmap
;
2026 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2027 if (idmap
->nsid
== 0)
2028 lxc_conf
->root_nsgid_map
= idmap
;
2041 static int set_config_mount_fstab(const char *key
, const char *value
,
2042 struct lxc_conf
*lxc_conf
, void *data
)
2044 if (lxc_config_value_empty(value
)) {
2045 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2049 return set_config_path_item(&lxc_conf
->fstab
, value
);
2052 static int set_config_mount_auto(const char *key
, const char *value
,
2053 struct lxc_conf
*lxc_conf
, void *data
)
2055 char *autos
, *token
;
2062 } allowed_auto_mounts
[] = {
2063 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2064 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2065 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2066 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2067 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2068 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2069 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2070 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2071 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2072 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2073 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2074 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2075 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2076 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2077 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2078 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2079 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2080 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2081 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2082 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2083 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2084 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2085 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2086 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2087 /* For adding anything that is just a single on/off, but has no
2088 * options: keep mask and flag identical and just define the enum
2089 * value as an unused bit so far
2094 if (lxc_config_value_empty(value
)) {
2095 lxc_conf
->auto_mounts
= 0;
2099 autos
= strdup(value
);
2103 lxc_iterate_parts(token
, autos
, " \t") {
2104 bool is_shmounts
= false;
2106 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2107 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
2110 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
2111 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
2117 if (!allowed_auto_mounts
[i
].token
) {
2118 ERROR("Invalid filesystem to automount \"%s\"", token
);
2122 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2123 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2126 char *container_path
;
2129 host_path
= token
+ STRLITERALLEN("shmounts:");
2130 if (*host_path
== '\0') {
2131 SYSERROR("Failed to copy shmounts host path");
2135 container_path
= strchr(host_path
, ':');
2136 if (!container_path
|| *(container_path
+ 1) == '\0')
2137 container_path
= "/dev/.lxc-mounts";
2139 *container_path
++ = '\0';
2141 lxc_conf
->shmount
.path_host
= strdup(host_path
);
2142 if (!lxc_conf
->shmount
.path_host
) {
2143 SYSERROR("Failed to copy shmounts host path");
2147 lxc_conf
->shmount
.path_cont
= strdup(container_path
);
2148 if(!lxc_conf
->shmount
.path_cont
) {
2149 SYSERROR("Failed to copy shmounts container path");
2163 static int set_config_mount(const char *key
, const char *value
,
2164 struct lxc_conf
*lxc_conf
, void *data
)
2167 struct lxc_list
*mntlist
;
2169 if (lxc_config_value_empty(value
))
2170 return lxc_clear_mount_entries(lxc_conf
);
2172 mntlist
= malloc(sizeof(*mntlist
));
2176 mntelem
= strdup(value
);
2181 mntlist
->elem
= mntelem
;
2183 lxc_list_add_tail(&lxc_conf
->mount_list
, mntlist
);
2188 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2189 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2192 static int set_config_cap_keep(const char *key
, const char *value
,
2193 struct lxc_conf
*lxc_conf
, void *data
)
2195 char *keepcaps
, *token
;
2196 struct lxc_list
*keeplist
;
2199 if (lxc_config_value_empty(value
))
2200 return lxc_clear_config_keepcaps(lxc_conf
);
2202 keepcaps
= strdup(value
);
2206 /* In case several capability keep is specified in a single line
2207 * split these caps in a single element for the list.
2209 lxc_iterate_parts(token
, keepcaps
, " \t") {
2210 if (!strcmp(token
, "none"))
2211 lxc_clear_config_keepcaps(lxc_conf
);
2213 keeplist
= malloc(sizeof(*keeplist
));
2217 keeplist
->elem
= strdup(token
);
2218 if (!keeplist
->elem
) {
2223 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
2234 static int set_config_cap_drop(const char *key
, const char *value
,
2235 struct lxc_conf
*lxc_conf
, void *data
)
2237 char *dropcaps
, *token
;
2238 struct lxc_list
*droplist
;
2241 if (lxc_config_value_empty(value
))
2242 return lxc_clear_config_caps(lxc_conf
);
2244 dropcaps
= strdup(value
);
2248 /* In case several capability drop is specified in a single line
2249 * split these caps in a single element for the list.
2251 lxc_iterate_parts(token
, dropcaps
, " \t") {
2252 droplist
= malloc(sizeof(*droplist
));
2256 droplist
->elem
= strdup(token
);
2257 if (!droplist
->elem
) {
2262 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
2273 static int set_config_console_path(const char *key
, const char *value
,
2274 struct lxc_conf
*lxc_conf
, void *data
)
2276 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2279 static int set_config_console_rotate(const char *key
, const char *value
,
2280 struct lxc_conf
*lxc_conf
, void *data
)
2282 if (lxc_config_value_empty(value
)) {
2283 lxc_conf
->console
.log_rotate
= 0;
2287 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
2290 if (lxc_conf
->console
.log_rotate
> 1) {
2291 ERROR("The \"lxc.console.rotate\" config key can only be set "
2299 static int set_config_console_logfile(const char *key
, const char *value
,
2300 struct lxc_conf
*lxc_conf
, void *data
)
2302 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2305 static int set_config_console_buffer_size(const char *key
, const char *value
,
2306 struct lxc_conf
*lxc_conf
, void *data
)
2310 uint64_t buffer_size
, pgsz
;
2312 if (lxc_config_value_empty(value
)) {
2313 lxc_conf
->console
.buffer_size
= 0;
2317 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2318 if (!strcmp(value
, "auto")) {
2319 lxc_conf
->console
.buffer_size
= 1 << 17;
2323 ret
= parse_byte_size_string(value
, &size
);
2330 /* must be at least a page size */
2331 pgsz
= lxc_getpagesize();
2332 if ((uint64_t)size
< pgsz
) {
2333 NOTICE("Requested ringbuffer size for the console is %" PRId64
2334 " but must be at least %" PRId64
2335 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2340 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2341 if (buffer_size
== 0)
2344 if (buffer_size
!= size
)
2345 NOTICE("Passed size was not a power of 2. Rounding log size to "
2346 "next power of two: %" PRIu64
" bytes", buffer_size
);
2348 lxc_conf
->console
.buffer_size
= buffer_size
;
2353 static int set_config_console_size(const char *key
, const char *value
,
2354 struct lxc_conf
*lxc_conf
, void *data
)
2358 uint64_t log_size
, pgsz
;
2360 if (lxc_config_value_empty(value
)) {
2361 lxc_conf
->console
.log_size
= 0;
2365 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2366 if (!strcmp(value
, "auto")) {
2367 lxc_conf
->console
.log_size
= 1 << 17;
2371 ret
= parse_byte_size_string(value
, &size
);
2378 /* must be at least a page size */
2379 pgsz
= lxc_getpagesize();
2380 if ((uint64_t)size
< pgsz
) {
2381 NOTICE("Requested ringbuffer size for the console is %" PRId64
2382 " but must be at least %" PRId64
2383 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2388 log_size
= lxc_find_next_power2((uint64_t)size
);
2392 if (log_size
!= size
)
2393 NOTICE("Passed size was not a power of 2. Rounding log size to "
2394 "next power of two: %" PRIu64
" bytes", log_size
);
2396 lxc_conf
->console
.log_size
= log_size
;
2402 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2403 * file, we expand it in the unexpanded_config, so that after a save_config we
2404 * store the hwaddr for re-use.
2405 * This is only called when reading the config file, not when executing a
2407 * 'x' and 'X' are substituted in-place.
2409 static void update_hwaddr(const char *line
)
2413 line
+= lxc_char_left_gc(line
, strlen(line
));
2417 if (!lxc_config_net_is_hwaddr(line
))
2420 /* Let config_net_hwaddr raise the error. */
2421 p
= strchr(line
, '=');
2432 rand_complete_hwaddr(p
);
2435 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2438 size_t len
= conf
->unexpanded_len
;
2440 update_hwaddr(line
);
2442 linelen
= strlen(line
);
2443 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2444 char *tmp
= realloc(conf
->unexpanded_config
,
2445 conf
->unexpanded_alloced
+ 1024);
2449 if (!conf
->unexpanded_config
)
2452 conf
->unexpanded_config
= tmp
;
2453 conf
->unexpanded_alloced
+= 1024;
2456 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2457 conf
->unexpanded_len
+= linelen
;
2458 if (line
[linelen
- 1] != '\n')
2459 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2460 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2465 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2467 __do_closedir
DIR *dir
= NULL
;
2468 struct dirent
*direntp
;
2469 char path
[PATH_MAX
];
2473 dir
= opendir(dirp
);
2477 while ((direntp
= readdir(dir
))) {
2480 fnam
= direntp
->d_name
;
2481 if (!strcmp(fnam
, "."))
2484 if (!strcmp(fnam
, ".."))
2488 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2491 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2492 if (len
< 0 || len
>= PATH_MAX
)
2495 ret
= lxc_config_read(path
, lxc_conf
, true);
2503 static int set_config_includefiles(const char *key
, const char *value
,
2504 struct lxc_conf
*lxc_conf
, void *data
)
2506 if (lxc_config_value_empty(value
)) {
2507 clr_config_includefiles(key
, lxc_conf
, NULL
);
2512 return do_includedir(value
, lxc_conf
);
2514 return lxc_config_read(value
, lxc_conf
, true);
2517 static int set_config_rootfs_path(const char *key
, const char *value
,
2518 struct lxc_conf
*lxc_conf
, void *data
)
2522 const char *container_path
;
2524 if (lxc_config_value_empty(value
)) {
2525 free(lxc_conf
->rootfs
.path
);
2526 lxc_conf
->rootfs
.path
= NULL
;
2530 dup
= strdup(value
);
2534 /* Split <storage type>:<container path> into <storage type> and
2535 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2536 * "rootfs.path" to <container path>.
2538 tmp
= strchr(dup
, ':');
2542 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2549 container_path
= tmp
;
2551 container_path
= value
;
2554 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2560 static int set_config_rootfs_managed(const char *key
, const char *value
,
2561 struct lxc_conf
*lxc_conf
, void *data
)
2563 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2566 static int set_config_rootfs_mount(const char *key
, const char *value
,
2567 struct lxc_conf
*lxc_conf
, void *data
)
2569 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2572 static int set_config_rootfs_options(const char *key
, const char *value
,
2573 struct lxc_conf
*lxc_conf
, void *data
)
2576 unsigned long mflags
= 0, pflags
= 0;
2577 char *mdata
= NULL
, *opts
= NULL
;
2578 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2580 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2584 ret
= parse_propagationopts(value
, &pflags
);
2590 ret
= set_config_string_item(&opts
, value
);
2596 rootfs
->mountflags
= mflags
| pflags
;
2597 rootfs
->options
= opts
;
2598 rootfs
->data
= mdata
;
2603 static int set_config_uts_name(const char *key
, const char *value
,
2604 struct lxc_conf
*lxc_conf
, void *data
)
2606 struct utsname
*utsname
;
2608 if (lxc_config_value_empty(value
)) {
2609 clr_config_uts_name(key
, lxc_conf
, NULL
);
2613 utsname
= malloc(sizeof(*utsname
));
2617 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2622 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2623 free(lxc_conf
->utsname
);
2624 lxc_conf
->utsname
= utsname
;
2629 static int set_config_namespace_clone(const char *key
, const char *value
,
2630 struct lxc_conf
*lxc_conf
, void *data
)
2635 if (lxc_config_value_empty(value
))
2636 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2638 if (lxc_conf
->ns_keep
!= 0) {
2640 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2641 "\"lxc.namespace.keep\"");
2649 lxc_iterate_parts(token
, ns
, " \t") {
2650 token
+= lxc_char_left_gc(token
, strlen(token
));
2651 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2652 cloneflag
= lxc_namespace_2_cloneflag(token
);
2653 if (cloneflag
< 0) {
2657 lxc_conf
->ns_clone
|= cloneflag
;
2664 static int set_config_namespace_keep(const char *key
, const char *value
,
2665 struct lxc_conf
*lxc_conf
, void *data
)
2670 if (lxc_config_value_empty(value
))
2671 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2673 if (lxc_conf
->ns_clone
!= 0) {
2675 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2676 "\"lxc.namespace.keep\"");
2684 lxc_iterate_parts(token
, ns
, " \t") {
2685 token
+= lxc_char_left_gc(token
, strlen(token
));
2686 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2687 cloneflag
= lxc_namespace_2_cloneflag(token
);
2688 if (cloneflag
< 0) {
2692 lxc_conf
->ns_keep
|= cloneflag
;
2699 static int set_config_namespace_share(const char *key
, const char *value
,
2700 struct lxc_conf
*lxc_conf
, void *data
)
2703 const char *namespace;
2705 if (lxc_config_value_empty(value
))
2706 return clr_config_namespace_share(key
, lxc_conf
, data
);
2708 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2709 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2713 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2716 struct parse_line_conf
{
2717 struct lxc_conf
*conf
;
2721 static int parse_line(char *buffer
, void *data
)
2723 char *dot
, *key
, *line
, *linep
, *value
;
2725 struct lxc_config_t
*config
;
2728 struct parse_line_conf
*plc
= data
;
2730 /* If there are newlines in the config file we should keep them. */
2731 empty_line
= lxc_is_line_empty(dup
);
2735 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2736 * modified the original string on the stack by replacing '=' by '\0'
2739 linep
= line
= strdup(dup
);
2743 if (!plc
->from_include
) {
2744 ret
= append_unexp_config_line(line
, plc
->conf
);
2752 line
+= lxc_char_left_gc(line
, strlen(line
));
2754 /* ignore comments */
2758 /* martian option - don't add it to the config itself */
2759 if (strncmp(line
, "lxc.", 4))
2764 dot
= strchr(line
, '=');
2766 ERROR("Invalid configuration line: %s", line
);
2774 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2776 value
+= lxc_char_left_gc(value
, strlen(value
));
2777 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2779 if (*value
== '\'' || *value
== '\"') {
2782 len
= strlen(value
);
2783 if (len
> 1 && value
[len
- 1] == *value
) {
2784 value
[len
- 1] = '\0';
2789 config
= lxc_get_config(key
);
2791 ERROR("Unknown configuration key \"%s\"", key
);
2795 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2803 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2805 char *dot
, *key
, *line
, *linep
, *value
;
2808 struct new_config_item
*new = NULL
;
2810 linep
= line
= strdup(dup
);
2814 line
+= lxc_char_left_gc(line
, strlen(line
));
2816 /* martian option - don't add it to the config itself */
2817 if (strncmp(line
, "lxc.", 4))
2821 dot
= strchr(line
, '=');
2823 ERROR("Invalid configuration item: %s", line
);
2831 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2833 value
+= lxc_char_left_gc(value
, strlen(value
));
2834 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2836 if (*value
== '\'' || *value
== '\"') {
2839 len
= strlen(value
);
2840 if (len
> 1 && value
[len
- 1] == *value
) {
2841 value
[len
- 1] = '\0';
2847 new = malloc(sizeof(struct new_config_item
));
2851 new->key
= strdup(key
);
2852 new->val
= strdup(value
);
2853 if (!new->val
|| !new->key
)
2861 if (ret
< 0 && new) {
2871 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2873 struct parse_line_conf c
;
2876 c
.from_include
= from_include
;
2878 /* Catch only the top level config file name in the structure. */
2880 conf
->rcfile
= strdup(file
);
2882 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2885 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2887 struct lxc_list
*dent
;
2889 dent
= malloc(sizeof(struct lxc_list
));
2893 dent
->elem
= parse_new_conf_line(arg
);
2899 lxc_list_add_tail(defines
, dent
);
2904 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
2906 struct lxc_list
*it
;
2909 lxc_list_for_each(it
, defines
) {
2910 struct new_config_item
*new_item
= it
->elem
;
2911 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
2916 lxc_config_define_free(defines
);
2921 void lxc_config_define_free(struct lxc_list
*defines
)
2923 struct lxc_list
*it
, *next
;
2925 lxc_list_for_each_safe(it
, defines
, next
) {
2926 struct new_config_item
*new_item
= it
->elem
;
2927 free(new_item
->key
);
2928 free(new_item
->val
);
2934 signed long lxc_config_parse_arch(const char *arch
)
2936 #if HAVE_SYS_PERSONALITY_H
2942 { "arm", PER_LINUX32
},
2943 { "armel", PER_LINUX32
},
2944 { "armhf", PER_LINUX32
},
2945 { "armv7l", PER_LINUX32
},
2946 { "athlon", PER_LINUX32
},
2947 { "i386", PER_LINUX32
},
2948 { "i486", PER_LINUX32
},
2949 { "i586", PER_LINUX32
},
2950 { "i686", PER_LINUX32
},
2951 { "linux32", PER_LINUX32
},
2952 { "mips", PER_LINUX32
},
2953 { "mipsel", PER_LINUX32
},
2954 { "ppc", PER_LINUX32
},
2955 { "powerpc", PER_LINUX32
},
2956 { "x86", PER_LINUX32
},
2957 { "amd64", PER_LINUX
},
2958 { "arm64", PER_LINUX
},
2959 { "linux64", PER_LINUX
},
2960 { "mips64", PER_LINUX
},
2961 { "mips64el", PER_LINUX
},
2962 { "ppc64", PER_LINUX
},
2963 { "ppc64el", PER_LINUX
},
2964 { "ppc64le", PER_LINUX
},
2965 { "powerpc64", PER_LINUX
},
2966 { "s390x", PER_LINUX
},
2967 { "x86_64", PER_LINUX
},
2969 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
2971 for (i
= 0; i
< len
; i
++)
2972 if (!strcmp(pername
[i
].name
, arch
))
2973 return pername
[i
].per
;
2979 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
2987 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
2988 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
2989 { "LSM", LXC_ATTACH_LSM_EXEC
},
2994 /* For the sake of backward compatibility, drop all privileges
2995 * if none is specified.
2997 for (i
= 0; all_privs
[i
].token
; i
++)
2998 *flags
|= all_privs
[i
].flag
;
3003 lxc_iterate_parts(token
, flaglist
, "|") {
3006 for (i
= 0; all_privs
[i
].token
; i
++)
3007 if (!strcmp(all_privs
[i
].token
, token
))
3008 aflag
= all_privs
[i
].flag
;
3019 /* Write out a configuration file. */
3020 int write_config(int fd
, const struct lxc_conf
*conf
)
3023 size_t len
= conf
->unexpanded_len
;
3028 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3030 SYSERROR("Failed to write configuration file");
3037 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3040 __do_free
char *tmp
= NULL
;
3044 len
= strlen(key
) + strlen(v
) + 4;
3045 tmp
= must_realloc(NULL
, len
);
3047 if (lxc_config_value_empty(v
))
3048 ret
= snprintf(tmp
, len
, "%s =", key
);
3050 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
3051 if (ret
< 0 || ret
>= len
)
3054 /* Save the line verbatim into unexpanded_conf */
3055 if (append_unexp_config_line(tmp
, conf
))
3061 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3065 char *lstart
= conf
->unexpanded_config
;
3067 if (!conf
->unexpanded_config
)
3071 lend
= strchr(lstart
, '\n');
3075 lend
= lstart
+ strlen(lstart
);
3079 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
3085 v
= lstart
[strlen(key
)];
3086 if (!isspace(v
) && v
!= '=') {
3092 conf
->unexpanded_len
-= (lend
- lstart
);
3094 if (*lend
== '\0') {
3099 memmove(lstart
, lend
, strlen(lend
) + 1);
3103 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3104 const char *newpath
, const char *oldname
,
3105 const char *newname
, const char *ovldir
)
3107 __do_free
char *newdir
= NULL
,
3111 size_t newdirlen
, olddirlen
;
3112 char *lstart
= conf
->unexpanded_config
;
3113 const char *key
= "lxc.mount.entry";
3115 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3116 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3117 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
3119 if (ret
< 0 || ret
>= olddirlen
+ 1)
3122 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3123 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3124 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
3126 if (ret
< 0 || ret
>= newdirlen
+ 1)
3129 if (!conf
->unexpanded_config
)
3133 lend
= strchr(lstart
, '\n');
3135 lend
= lstart
+ strlen(lstart
);
3139 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3142 p
= strchr(lstart
+ strlen(key
), '=');
3153 /* Whenever a lxc.mount.entry entry is found in a line we check
3154 * if the substring "overlay" is present before doing any
3155 * further work. We check for "overlay" because substrings need
3156 * to have at least one space before them in a valid overlay
3157 * lxc.mount.entry (/A B overlay). When the space before is
3158 * missing it is very likely that these substrings are part of a
3159 * path or something else. (Checking q >= lend ensures that we
3160 * only count matches in the current line.) */
3161 q
= strstr(p
, " overlay");
3162 if (!q
|| q
>= lend
)
3165 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3168 /* replace the olddir with newdir */
3169 if (olddirlen
>= newdirlen
) {
3170 size_t diff
= olddirlen
- newdirlen
;
3171 memcpy(q
, newdir
, newdirlen
);
3173 if (olddirlen
!= newdirlen
) {
3174 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3175 strlen(q
) - newdirlen
- diff
+ 1);
3177 conf
->unexpanded_len
-= diff
;
3181 size_t diff
= newdirlen
- olddirlen
;
3182 size_t oldlen
= conf
->unexpanded_len
;
3183 size_t newlen
= oldlen
+ diff
;
3184 size_t poffset
= q
- conf
->unexpanded_config
;
3186 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3190 conf
->unexpanded_len
= newlen
;
3191 conf
->unexpanded_alloced
= newlen
+ 1;
3192 new[newlen
- 1] = '\0';
3193 lend
= new + (lend
- conf
->unexpanded_config
);
3195 /* Move over the remainder to make room for the newdir.
3197 memmove(new + poffset
+ newdirlen
,
3198 new + poffset
+ olddirlen
,
3199 oldlen
- poffset
- olddirlen
+ 1);
3200 conf
->unexpanded_config
= new;
3202 memcpy(new + poffset
, newdir
, newdirlen
);
3213 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3214 const char *newpath
, const char *oldname
,
3215 const char *newname
)
3217 __do_free
char *newdir
= NULL
,
3221 char *lstart
= conf
->unexpanded_config
;
3222 size_t newdirlen
, olddirlen
;
3223 const char *key
= "lxc.hook";
3225 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3226 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3227 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3228 if (ret
< 0 || ret
>= olddirlen
+ 1)
3231 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3232 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3233 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3234 if (ret
< 0 || ret
>= newdirlen
+ 1)
3237 if (!conf
->unexpanded_config
)
3241 lend
= strchr(lstart
, '\n');
3243 lend
= lstart
+ strlen(lstart
);
3247 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3250 p
= strchr(lstart
+ strlen(key
), '=');
3261 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
3264 /* replace the olddir with newdir */
3265 if (olddirlen
>= newdirlen
) {
3266 size_t diff
= olddirlen
- newdirlen
;
3267 memcpy(p
, newdir
, newdirlen
);
3269 if (olddirlen
!= newdirlen
) {
3270 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3271 strlen(p
) - newdirlen
- diff
+ 1);
3273 conf
->unexpanded_len
-= diff
;
3277 size_t diff
= newdirlen
- olddirlen
;
3278 size_t oldlen
= conf
->unexpanded_len
;
3279 size_t newlen
= oldlen
+ diff
;
3280 size_t poffset
= p
- conf
->unexpanded_config
;
3282 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3286 conf
->unexpanded_len
= newlen
;
3287 conf
->unexpanded_alloced
= newlen
+ 1;
3288 new[newlen
- 1] = '\0';
3289 lend
= new + (lend
- conf
->unexpanded_config
);
3291 /* Move over the remainder to make room for the newdir.
3293 memmove(new + poffset
+ newdirlen
,
3294 new + poffset
+ olddirlen
,
3295 oldlen
- poffset
- olddirlen
+ 1);
3296 conf
->unexpanded_config
= new;
3298 memcpy(new + poffset
, newdir
, newdirlen
);
3312 ERROR("Error writing to new config"); \
3317 /* This is called only from clone. We wish to update all hwaddrs in the
3318 * unexpanded config file. We can't/don't want to update any which come from
3319 * lxc.includes (there shouldn't be any).
3320 * We can't just walk the c->lxc-conf->network list because that includes netifs
3321 * from the include files. So we update the ones which we find in the unexp
3322 * config file, then find the original macaddr in the conf->network, and update
3323 * that to the same value.
3325 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3327 char *lend
, *p
, *p2
;
3328 struct lxc_list
*it
;
3329 char *lstart
= conf
->unexpanded_config
;
3331 if (!conf
->unexpanded_config
)
3335 char newhwaddr
[18], oldhwaddr
[17];
3337 lend
= strchr(lstart
, '\n');
3339 lend
= lstart
+ strlen(lstart
);
3343 if (!lxc_config_net_is_hwaddr(lstart
)) {
3348 p
= strchr(lstart
, '=');
3361 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3364 if ((p2
- p
) != 17) {
3365 WARN("Bad hwaddr entry");
3370 memcpy(oldhwaddr
, p
, 17);
3372 if (!new_hwaddr(newhwaddr
))
3375 memcpy(p
, newhwaddr
, 17);
3376 lxc_list_for_each(it
, &conf
->network
) {
3377 struct lxc_netdev
*n
= it
->elem
;
3379 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3380 memcpy(n
->hwaddr
, newhwaddr
, 17);
3389 static int set_config_ephemeral(const char *key
, const char *value
,
3390 struct lxc_conf
*lxc_conf
, void *data
)
3392 if (lxc_config_value_empty(value
)) {
3393 lxc_conf
->ephemeral
= 0;
3397 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3400 if (lxc_conf
->ephemeral
> 1)
3406 static int set_config_log_syslog(const char *key
, const char *value
,
3407 struct lxc_conf
*lxc_conf
, void *data
)
3411 if (lxc_conf
->syslog
) {
3412 free(lxc_conf
->syslog
);
3413 lxc_conf
->syslog
= NULL
;
3416 if (lxc_config_value_empty(value
))
3419 facility
= lxc_syslog_priority_to_int(value
);
3420 if (facility
== -EINVAL
)
3423 lxc_log_syslog(facility
);
3425 return set_config_string_item(&lxc_conf
->syslog
, value
);
3428 static int set_config_no_new_privs(const char *key
, const char *value
,
3429 struct lxc_conf
*lxc_conf
, void *data
)
3433 if (lxc_config_value_empty(value
)) {
3434 lxc_conf
->no_new_privs
= false;
3438 if (lxc_safe_uint(value
, &v
) < 0)
3444 lxc_conf
->no_new_privs
= v
? true : false;
3449 /* Callbacks to get configuration items. */
3450 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3451 struct lxc_conf
*c
, void *data
)
3458 memset(retv
, 0, inlen
);
3460 #if HAVE_SYS_PERSONALITY_H
3463 switch (c
->personality
) {
3465 strprint(retv
, inlen
, "i686");
3468 strprint(retv
, inlen
, "x86_64");
3478 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3479 struct lxc_conf
*c
, void *data
)
3481 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3484 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3485 struct lxc_conf
*c
, void *data
)
3487 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3490 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3491 struct lxc_conf
*c
, void *data
)
3493 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3496 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3497 struct lxc_conf
*c
, void *data
)
3499 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3502 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3503 int inlen
, struct lxc_conf
*c
,
3506 return lxc_get_conf_int(c
, retv
, inlen
,
3507 c
->lsm_aa_allow_incomplete
);
3510 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3511 int inlen
, struct lxc_conf
*c
,
3514 return lxc_get_conf_int(c
, retv
, inlen
,
3515 c
->lsm_aa_allow_nesting
);
3518 static int get_config_apparmor_raw(const char *key
, char *retv
,
3519 int inlen
, struct lxc_conf
*c
,
3523 struct lxc_list
*it
;
3529 memset(retv
, 0, inlen
);
3531 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3532 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3538 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3539 struct lxc_conf
*c
, void *data
)
3541 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3544 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3545 struct lxc_conf
*c
, void *data
)
3547 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3550 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3551 struct lxc_conf
*c
, void *data
)
3553 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3557 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3558 * just the value(s) will be printed. Since there still could be more than one,
3559 * it is newline-separated.
3560 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3562 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3563 * 'lxc.cgroup.subsystem.key = value' format.
3565 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3566 int inlen
, struct lxc_conf
*c
,
3570 size_t namespaced_token_len
;
3571 char *global_token
, *namespaced_token
;
3572 struct lxc_list
*it
;
3574 bool get_all
= false;
3579 memset(retv
, 0, inlen
);
3581 if (version
== CGROUP2_SUPER_MAGIC
) {
3582 global_token
= "lxc.cgroup2";
3583 namespaced_token
= "lxc.cgroup2.";
3584 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3585 } else if (version
== CGROUP_SUPER_MAGIC
) {
3586 global_token
= "lxc.cgroup";
3587 namespaced_token
= "lxc.cgroup.";
3588 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3593 if (strcmp(key
, global_token
) == 0)
3595 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3596 key
+= namespaced_token_len
;
3600 lxc_list_for_each(it
, &c
->cgroup
) {
3601 struct lxc_cgroup
*cg
= it
->elem
;
3604 if (version
!= cg
->version
)
3607 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3608 cg
->subsystem
, cg
->value
);
3609 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3610 strprint(retv
, inlen
, "%s\n", cg
->value
);
3617 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3618 struct lxc_conf
*c
, void *data
)
3620 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3621 CGROUP_SUPER_MAGIC
);
3624 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3625 struct lxc_conf
*c
, void *data
)
3627 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3628 CGROUP2_SUPER_MAGIC
);
3631 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3632 struct lxc_conf
*lxc_conf
, void *data
)
3640 memset(retv
, 0, inlen
);
3642 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3647 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3648 int inlen
, struct lxc_conf
*lxc_conf
,
3651 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3652 lxc_conf
->cgroup_meta
.relative
);
3655 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3656 struct lxc_conf
*c
, void *data
)
3658 struct lxc_list
*it
;
3659 int len
, listlen
, ret
;
3661 /* "u 1000 1000000 65536"
3663 * let's render this as
3681 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3682 char buf
[__LXC_IDMAP_STR_BUF
];
3687 memset(retv
, 0, inlen
);
3689 listlen
= lxc_list_len(&c
->id_map
);
3690 lxc_list_for_each(it
, &c
->id_map
) {
3691 struct id_map
*map
= it
->elem
;
3692 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3693 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3694 map
->nsid
, map
->hostid
, map
->range
);
3695 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3698 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3704 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3705 struct lxc_conf
*c
, void *data
)
3708 v
= lxc_log_priority_to_string(c
->loglevel
);
3709 return lxc_get_conf_str(retv
, inlen
, v
);
3712 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3713 struct lxc_conf
*c
, void *data
)
3715 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3718 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3719 struct lxc_conf
*c
, void *data
)
3721 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3724 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3725 struct lxc_conf
*c
, void *data
)
3727 int len
, fulllen
= 0;
3728 const char *sep
= "";
3733 memset(retv
, 0, inlen
);
3735 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3738 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3739 case LXC_AUTO_PROC_MIXED
:
3740 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3743 case LXC_AUTO_PROC_RW
:
3744 strprint(retv
, inlen
, "%sproc:rw", sep
);
3751 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3752 case LXC_AUTO_SYS_RO
:
3753 strprint(retv
, inlen
, "%ssys:ro", sep
);
3756 case LXC_AUTO_SYS_RW
:
3757 strprint(retv
, inlen
, "%ssys:rw", sep
);
3760 case LXC_AUTO_SYS_MIXED
:
3761 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3768 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3769 case LXC_AUTO_CGROUP_NOSPEC
:
3770 strprint(retv
, inlen
, "%scgroup", sep
);
3772 case LXC_AUTO_CGROUP_MIXED
:
3773 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3775 case LXC_AUTO_CGROUP_RO
:
3776 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3778 case LXC_AUTO_CGROUP_RW
:
3779 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3781 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3782 strprint(retv
, inlen
, "%scgroup-full", sep
);
3784 case LXC_AUTO_CGROUP_FULL_MIXED
:
3785 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3787 case LXC_AUTO_CGROUP_FULL_RO
:
3788 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3790 case LXC_AUTO_CGROUP_FULL_RW
:
3791 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3800 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3801 struct lxc_conf
*c
, void *data
)
3803 int len
, fulllen
= 0;
3804 struct lxc_list
*it
;
3809 memset(retv
, 0, inlen
);
3811 lxc_list_for_each(it
, &c
->mount_list
) {
3812 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3818 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3819 struct lxc_conf
*c
, void *data
)
3821 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
3824 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
3825 struct lxc_conf
*c
, void *data
)
3827 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
3830 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
3831 struct lxc_conf
*c
, void *data
)
3833 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
3836 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
3837 struct lxc_conf
*c
, void *data
)
3839 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
3842 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
3843 struct lxc_conf
*c
, void *data
)
3845 return lxc_get_conf_str(
3847 c
->utsname
? c
->utsname
->nodename
: NULL
);
3850 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
3851 struct lxc_conf
*c
, void *data
)
3854 int len
, fulllen
= 0, found
= -1;
3855 struct lxc_list
*it
;
3858 subkey
= strchr(key
, '.');
3862 subkey
= strchr(subkey
+ 1, '.');
3866 if (*subkey
== '\0')
3869 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
3870 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
3882 memset(retv
, 0, inlen
);
3884 lxc_list_for_each(it
, &c
->hooks
[found
]) {
3885 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3891 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
3892 struct lxc_conf
*c
, void *data
)
3894 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
3897 static int get_config_net(const char *key
, char *retv
, int inlen
,
3898 struct lxc_conf
*c
, void *data
)
3900 int len
, fulllen
= 0;
3901 struct lxc_list
*it
;
3906 memset(retv
, 0, inlen
);
3908 lxc_list_for_each(it
, &c
->network
) {
3909 struct lxc_netdev
*n
= it
->elem
;
3910 const char *t
= lxc_net_type_to_str(n
->type
);
3911 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
3917 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
3918 struct lxc_conf
*c
, void *data
)
3920 int len
, fulllen
= 0;
3921 struct lxc_list
*it
;
3926 memset(retv
, 0, inlen
);
3928 lxc_list_for_each(it
, &c
->caps
) {
3929 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3935 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
3936 struct lxc_conf
*c
, void *data
)
3938 int len
, fulllen
= 0;
3939 struct lxc_list
*it
;
3944 memset(retv
, 0, inlen
);
3946 lxc_list_for_each(it
, &c
->keepcaps
) {
3947 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3953 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
3954 struct lxc_conf
*c
, void *data
)
3956 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
3959 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
3960 struct lxc_conf
*c
, void *data
)
3962 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
3965 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
3966 struct lxc_conf
*c
, void *data
)
3968 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
3972 static int get_config_console_buffer_size(const char *key
, char *retv
,
3973 int inlen
, struct lxc_conf
*c
,
3976 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
3979 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
3980 struct lxc_conf
*c
, void *data
)
3982 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
3985 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
3986 int inlen
, struct lxc_conf
*c
,
3990 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
3997 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
3998 struct lxc_conf
*c
, void *data
)
4000 #ifdef HAVE_SECCOMP_NOTIFY
4001 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4003 return ret_set_errno(-1, ENOSYS
);
4007 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4008 struct lxc_conf
*c
, void *data
)
4010 #ifdef HAVE_SECCOMP_NOTIFY
4011 return lxc_get_conf_str(retv
, inlen
,
4012 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4013 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4014 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4016 return ret_set_errno(-1, ENOSYS
);
4020 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4021 struct lxc_conf
*c
, void *data
)
4023 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4026 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4027 struct lxc_conf
*c
, void *data
)
4029 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4032 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4033 struct lxc_conf
*c
, void *data
)
4035 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4038 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4039 struct lxc_conf
*c
, void *data
)
4041 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4044 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4045 struct lxc_conf
*c
, void *data
)
4047 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4050 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4051 struct lxc_conf
*c
, void *data
)
4053 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4056 static int get_config_start(const char *key
, char *retv
, int inlen
,
4057 struct lxc_conf
*c
, void *data
)
4059 if (strcmp(key
+ 10, "auto") == 0)
4060 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4061 else if (strcmp(key
+ 10, "delay") == 0)
4062 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4063 else if (strcmp(key
+ 10, "order") == 0)
4064 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4069 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4070 struct lxc_conf
*c
, void *data
)
4072 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4075 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4076 struct lxc_conf
*c
, void *data
)
4078 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4081 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4082 int inlen
, struct lxc_conf
*c
,
4085 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4088 static int get_config_group(const char *key
, char *retv
, int inlen
,
4089 struct lxc_conf
*c
, void *data
)
4091 int len
, fulllen
= 0;
4092 struct lxc_list
*it
;
4097 memset(retv
, 0, inlen
);
4099 lxc_list_for_each(it
, &c
->groups
) {
4100 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4106 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4107 struct lxc_conf
*c
, void *data
)
4109 int len
, fulllen
= 0;
4110 struct lxc_list
*it
;
4115 memset(retv
, 0, inlen
);
4117 lxc_list_for_each(it
, &c
->environment
) {
4118 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4124 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4125 struct lxc_conf
*c
, void *data
)
4127 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4130 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4131 struct lxc_conf
*c
, void *data
)
4133 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4136 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4137 struct lxc_conf
*c
, void *data
)
4139 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4142 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4143 struct lxc_conf
*c
, void *data
)
4145 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4148 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4149 struct lxc_conf
*c
, void *data
)
4151 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4154 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4155 struct lxc_conf
*c
, void *data
)
4157 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4160 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4161 struct lxc_conf
*c
, void *data
)
4163 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4166 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4167 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4168 * printed, in 'lxc.prlimit.resource = value' format.
4170 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4171 struct lxc_conf
*c
, void *data
)
4173 int fulllen
= 0, len
;
4174 bool get_all
= false;
4175 struct lxc_list
*it
;
4180 memset(retv
, 0, inlen
);
4182 if (!strcmp(key
, "lxc.prlimit"))
4184 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
4189 lxc_list_for_each(it
, &c
->limits
) {
4190 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4191 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4193 struct lxc_limit
*lim
= it
->elem
;
4195 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4196 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4197 partlen
= STRLITERALLEN("unlimited");
4199 partlen
= sprintf(buf
, "%" PRIu64
,
4200 (uint64_t)lim
->limit
.rlim_cur
);
4203 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4204 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4205 memcpy(buf
+ partlen
, ":unlimited",
4206 STRLITERALLEN(":unlimited") + 1);
4208 sprintf(buf
+ partlen
, ":%" PRIu64
,
4209 (uint64_t)lim
->limit
.rlim_max
);
4213 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4214 lim
->resource
, buf
);
4215 } else if (strcmp(lim
->resource
, key
) == 0) {
4216 strprint(retv
, inlen
, "%s", buf
);
4223 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4224 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4225 * entries will be printed, in 'lxc.sysctl.key = value' format.
4227 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4228 struct lxc_conf
*c
, void *data
)
4231 struct lxc_list
*it
;
4233 bool get_all
= false;
4238 memset(retv
, 0, inlen
);
4240 if (strcmp(key
, "lxc.sysctl") == 0)
4242 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4243 key
+= STRLITERALLEN("lxc.sysctl.");
4247 lxc_list_for_each(it
, &c
->sysctls
) {
4248 struct lxc_sysctl
*elem
= it
->elem
;
4250 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
4252 } else if (strcmp(elem
->key
, key
) == 0) {
4253 strprint(retv
, inlen
, "%s", elem
->value
);
4260 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4261 struct lxc_conf
*c
, void *data
)
4263 struct lxc_list
*it
;
4266 bool get_all
= false;
4271 memset(retv
, 0, inlen
);
4273 if (strcmp(key
, "lxc.proc") == 0)
4275 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4276 key
+= STRLITERALLEN("lxc.proc.");
4280 lxc_list_for_each(it
, &c
->procs
) {
4281 struct lxc_proc
*proc
= it
->elem
;
4284 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4285 proc
->filename
, proc
->value
);
4286 } else if (strcmp(proc
->filename
, key
) == 0) {
4287 strprint(retv
, inlen
, "%s", proc
->value
);
4294 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4295 struct lxc_conf
*c
, void *data
)
4303 memset(retv
, 0, inlen
);
4305 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4306 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4307 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4313 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4314 struct lxc_conf
*c
, void *data
)
4322 memset(retv
, 0, inlen
);
4324 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4325 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4326 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4332 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4333 struct lxc_conf
*c
, void *data
)
4336 const char *namespace;
4342 memset(retv
, 0, inlen
);
4344 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4345 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4349 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4354 /* Callbacks to clear config items. */
4355 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4358 c
->personality
= -1;
4362 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4369 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4376 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4384 static inline int clr_config_apparmor_profile(const char *key
,
4385 struct lxc_conf
*c
, void *data
)
4387 free(c
->lsm_aa_profile
);
4388 c
->lsm_aa_profile
= NULL
;
4392 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4396 c
->lsm_aa_allow_incomplete
= 0;
4400 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4404 c
->lsm_aa_allow_nesting
= 0;
4408 static inline int clr_config_apparmor_raw(const char *key
,
4412 return lxc_clear_apparmor_raw(c
);
4415 static inline int clr_config_selinux_context(const char *key
,
4416 struct lxc_conf
*c
, void *data
)
4418 free(c
->lsm_se_context
);
4419 c
->lsm_se_context
= NULL
;
4423 static inline int clr_config_selinux_context_keyring(const char *key
,
4424 struct lxc_conf
*c
, void *data
)
4426 free(c
->lsm_se_keyring_context
);
4427 c
->lsm_se_keyring_context
= NULL
;
4431 static inline int clr_config_keyring_session(const char *key
,
4432 struct lxc_conf
*c
, void *data
)
4434 c
->keyring_disable_session
= false;
4438 static inline int clr_config_cgroup_controller(const char *key
,
4439 struct lxc_conf
*c
, void *data
)
4441 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4444 static inline int clr_config_cgroup2_controller(const char *key
,
4445 struct lxc_conf
*c
, void *data
)
4447 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4450 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4453 if (lxc_conf
->cgroup_meta
.dir
) {
4454 free(lxc_conf
->cgroup_meta
.dir
);
4455 lxc_conf
->cgroup_meta
.dir
= NULL
;
4461 static inline int clr_config_cgroup_relative(const char *key
,
4462 struct lxc_conf
*lxc_conf
,
4465 lxc_conf
->cgroup_meta
.relative
= false;
4469 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4472 return lxc_clear_idmaps(c
);
4475 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4478 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4482 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4490 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4493 return lxc_clear_mount_entries(c
);
4496 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4499 return lxc_clear_automounts(c
);
4502 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4510 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4513 free(c
->rootfs
.path
);
4514 c
->rootfs
.path
= NULL
;
4518 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4521 c
->rootfs
.managed
= true;
4525 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4528 free(c
->rootfs
.mount
);
4529 c
->rootfs
.mount
= NULL
;
4533 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4536 free(c
->rootfs
.options
);
4537 c
->rootfs
.options
= NULL
;
4539 free(c
->rootfs
.data
);
4540 c
->rootfs
.data
= NULL
;
4545 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4553 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4556 return lxc_clear_hooks(c
, key
);
4559 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4562 /* default to legacy hooks version */
4563 c
->hooks_version
= 0;
4567 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4570 lxc_free_networks(&c
->network
);
4575 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4578 return lxc_clear_config_caps(c
);
4581 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4584 return lxc_clear_config_keepcaps(c
);
4587 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4590 free(c
->console
.path
);
4591 c
->console
.path
= NULL
;
4595 static inline int clr_config_console_logfile(const char *key
,
4596 struct lxc_conf
*c
, void *data
)
4598 free(c
->console
.log_path
);
4599 c
->console
.log_path
= NULL
;
4603 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4606 c
->console
.log_rotate
= 0;
4610 static inline int clr_config_console_buffer_size(const char *key
,
4611 struct lxc_conf
*c
, void *data
)
4613 c
->console
.buffer_size
= 0;
4617 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4620 c
->console
.log_size
= 0;
4624 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4625 struct lxc_conf
*c
, void *data
)
4628 c
->seccomp
.allow_nesting
= 0;
4636 static inline int clr_config_seccomp_notify_cookie(const char *key
,
4637 struct lxc_conf
*c
, void *data
)
4639 #ifdef HAVE_SECCOMP_NOTIFY
4640 free(c
->seccomp
.notifier
.cookie
);
4641 c
->seccomp
.notifier
.cookie
= NULL
;
4644 return ret_set_errno(-1, ENOSYS
);
4648 static inline int clr_config_seccomp_notify_proxy(const char *key
,
4649 struct lxc_conf
*c
, void *data
)
4651 #ifdef HAVE_SECCOMP_NOTIFY
4652 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
4653 sizeof(c
->seccomp
.notifier
.proxy_addr
));
4656 return ret_set_errno(-1, ENOSYS
);
4660 static inline int clr_config_seccomp_profile(const char *key
,
4661 struct lxc_conf
*c
, void *data
)
4663 free(c
->seccomp
.seccomp
);
4664 c
->seccomp
.seccomp
= NULL
;
4668 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4675 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
4678 c
->autodevtmpfssize
= 500000;
4682 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4689 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4692 c
->rebootsignal
= 0;
4696 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4703 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4706 if (strcmp(key
+ 10, "auto") == 0)
4708 else if (strcmp(key
+ 10, "delay") == 0)
4710 else if (strcmp(key
+ 10, "order") == 0)
4716 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4724 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4727 c
->monitor_unshare
= 0;
4731 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4732 struct lxc_conf
*c
, void *data
)
4734 c
->monitor_signal_pdeath
= 0;
4738 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4741 return lxc_clear_groups(c
);
4744 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4747 return lxc_clear_environment(c
);
4750 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
4753 free(c
->execute_cmd
);
4754 c
->execute_cmd
= NULL
;
4758 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
4766 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
4774 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
4781 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
4788 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
4795 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
4798 c
->no_new_privs
= false;
4802 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
4805 return lxc_clear_limits(c
, key
);
4808 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
4811 return lxc_clear_sysctls(c
, key
);
4814 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
4817 return lxc_clear_procs(c
, key
);
4820 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
4823 lxc_clear_includes(c
);
4827 static int clr_config_namespace_clone(const char *key
,
4828 struct lxc_conf
*lxc_conf
, void *data
)
4830 lxc_conf
->ns_clone
= 0;
4834 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
4837 lxc_conf
->ns_keep
= 0;
4841 static int clr_config_namespace_share(const char *key
,
4842 struct lxc_conf
*lxc_conf
, void *data
)
4845 const char *namespace;
4847 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4848 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4852 free(lxc_conf
->ns_share
[ns_idx
]);
4853 lxc_conf
->ns_share
[ns_idx
] = NULL
;
4858 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
4859 struct lxc_conf
*c
, void *data
)
4864 static struct lxc_config_t
*get_network_config_ops(const char *key
,
4865 struct lxc_conf
*lxc_conf
,
4867 char **deindexed_key
)
4870 unsigned int tmpidx
;
4872 char *copy
, *idx_start
, *idx_end
;
4873 struct lxc_config_t
*config
= NULL
;
4875 /* check that this is a sensible network key */
4876 if (strncmp("lxc.net.", key
, 8)) {
4877 ERROR("Invalid network configuration key \"%s\"", key
);
4883 ERROR("Failed to duplicate string \"%s\"", key
);
4888 if (!isdigit(*(key
+ 8))) {
4889 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
4893 /* beginning of index string */
4894 idx_start
= (copy
+ 7);
4897 /* end of index string */
4898 idx_end
= strchr((copy
+ 8), '.');
4902 /* parse current index */
4903 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
4906 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
4912 /* This, of course is utterly nonsensical on so many levels, but
4913 * better safe than sorry.
4914 * (Checking for INT_MAX here is intentional.)
4916 if (tmpidx
== INT_MAX
) {
4917 SYSERROR("Number of configured networks would overflow the "
4923 numstrlen
= strlen((idx_start
+ 1));
4925 /* repair configuration key */
4928 /* lxc.net.<idx>.<subkey> */
4931 if (strlen(idx_end
+ 1) == 0) {
4932 ERROR("No subkey in network configuration key \"%s\"", key
);
4936 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
4937 copy
[strlen(key
) - (numstrlen
+ 1)] = '\0';
4939 config
= lxc_get_config(copy
);
4941 ERROR("Unknown network configuration key \"%s\"", key
);
4947 *deindexed_key
= copy
;
4956 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4957 * found. So we make sure next comes an integer, find the right callback (by
4958 * rewriting the key), and call it.
4960 static int set_config_net_nic(const char *key
, const char *value
,
4961 struct lxc_conf
*lxc_conf
, void *data
)
4964 const char *idxstring
;
4965 struct lxc_config_t
*config
;
4966 struct lxc_netdev
*netdev
;
4968 char *deindexed_key
= NULL
;
4970 idxstring
= key
+ 8;
4971 if (!isdigit(*idxstring
))
4974 if (lxc_config_value_empty(value
))
4975 return clr_config_net_nic(key
, lxc_conf
, data
);
4977 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
4978 if (!config
|| idx
< 0)
4981 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
4983 free(deindexed_key
);
4987 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
4988 free(deindexed_key
);
4993 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
4997 const char *idxstring
;
4998 struct lxc_config_t
*config
;
4999 struct lxc_netdev
*netdev
;
5001 char *deindexed_key
= NULL
;
5003 idxstring
= key
+ 8;
5004 if (!isdigit(*idxstring
))
5007 /* The left conjunct is pretty self-explanatory. The right conjunct
5008 * checks whether the two pointers are equal. If they are we know that
5009 * this is not a key that is namespaced any further and so we are
5010 * supposed to clear the whole network.
5012 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5013 unsigned int rmnetdevidx
;
5015 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
5018 /* Remove network from network list. */
5019 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5023 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5024 if (!config
|| idx
< 0)
5027 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
5029 free(deindexed_key
);
5033 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
5034 free(deindexed_key
);
5039 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5042 struct lxc_netdev
*netdev
= data
;
5052 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5055 struct lxc_netdev
*netdev
= data
;
5060 netdev
->name
[0] = '\0';
5065 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5068 struct lxc_netdev
*netdev
= data
;
5078 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5081 struct lxc_netdev
*netdev
= data
;
5086 netdev
->link
[0] = '\0';
5091 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5094 struct lxc_netdev
*netdev
= data
;
5097 return ret_set_errno(-1, EINVAL
);
5099 netdev
->l2proxy
= false;
5104 static int clr_config_net_macvlan_mode(const char *key
,
5105 struct lxc_conf
*lxc_conf
, void *data
)
5107 struct lxc_netdev
*netdev
= data
;
5112 if (netdev
->type
!= LXC_NET_MACVLAN
)
5115 netdev
->priv
.macvlan_attr
.mode
= -1;
5120 static int clr_config_net_ipvlan_mode(const char *key
,
5121 struct lxc_conf
*lxc_conf
, void *data
)
5123 struct lxc_netdev
*netdev
= data
;
5126 return ret_set_errno(-1, EINVAL
);
5128 if (netdev
->type
!= LXC_NET_IPVLAN
)
5131 netdev
->priv
.ipvlan_attr
.mode
= -1;
5136 static int clr_config_net_ipvlan_isolation(const char *key
,
5137 struct lxc_conf
*lxc_conf
, void *data
)
5139 struct lxc_netdev
*netdev
= data
;
5142 return ret_set_errno(-1, EINVAL
);
5144 if (netdev
->type
!= LXC_NET_IPVLAN
)
5147 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5152 static int clr_config_net_veth_mode(const char *key
,
5153 struct lxc_conf
*lxc_conf
, void *data
)
5155 struct lxc_netdev
*netdev
= data
;
5158 return ret_set_errno(-1, EINVAL
);
5160 if (netdev
->type
!= LXC_NET_VETH
)
5163 netdev
->priv
.veth_attr
.mode
= -1;
5168 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5171 struct lxc_netdev
*netdev
= data
;
5176 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5181 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5184 struct lxc_netdev
*netdev
= data
;
5189 free(netdev
->upscript
);
5190 netdev
->upscript
= NULL
;
5195 static int clr_config_net_script_down(const char *key
,
5196 struct lxc_conf
*lxc_conf
, void *data
)
5198 struct lxc_netdev
*netdev
= data
;
5203 free(netdev
->downscript
);
5204 netdev
->downscript
= NULL
;
5209 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5212 struct lxc_netdev
*netdev
= data
;
5217 free(netdev
->hwaddr
);
5218 netdev
->hwaddr
= NULL
;
5223 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5226 struct lxc_netdev
*netdev
= data
;
5237 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5240 struct lxc_netdev
*netdev
= data
;
5245 netdev
->priv
.vlan_attr
.vid
= 0;
5250 static int clr_config_net_ipv4_gateway(const char *key
,
5251 struct lxc_conf
*lxc_conf
, void *data
)
5253 struct lxc_netdev
*netdev
= data
;
5258 free(netdev
->ipv4_gateway
);
5259 netdev
->ipv4_gateway
= NULL
;
5264 static int clr_config_net_ipv4_address(const char *key
,
5265 struct lxc_conf
*lxc_conf
, void *data
)
5267 struct lxc_netdev
*netdev
= data
;
5268 struct lxc_list
*cur
, *next
;
5273 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
5282 static int clr_config_net_veth_ipv4_route(const char *key
,
5283 struct lxc_conf
*lxc_conf
, void *data
)
5285 struct lxc_netdev
*netdev
= data
;
5286 struct lxc_list
*cur
, *next
;
5291 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv4_routes
, next
) {
5300 static int clr_config_net_ipv6_gateway(const char *key
,
5301 struct lxc_conf
*lxc_conf
, void *data
)
5303 struct lxc_netdev
*netdev
= data
;
5308 free(netdev
->ipv6_gateway
);
5309 netdev
->ipv6_gateway
= NULL
;
5314 static int clr_config_net_ipv6_address(const char *key
,
5315 struct lxc_conf
*lxc_conf
, void *data
)
5317 struct lxc_netdev
*netdev
= data
;
5318 struct lxc_list
*cur
, *next
;
5323 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
5332 static int clr_config_net_veth_ipv6_route(const char *key
,
5333 struct lxc_conf
*lxc_conf
, void *data
)
5335 struct lxc_netdev
*netdev
= data
;
5336 struct lxc_list
*cur
, *next
;
5341 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv6_routes
, next
) {
5350 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
5351 struct lxc_conf
*c
, void *data
)
5354 const char *idxstring
;
5355 struct lxc_config_t
*config
;
5356 struct lxc_netdev
*netdev
;
5358 char *deindexed_key
= NULL
;
5360 idxstring
= key
+ 8;
5361 if (!isdigit(*idxstring
))
5364 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
5365 if (!config
|| idx
< 0)
5368 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5370 free(deindexed_key
);
5374 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
5375 free(deindexed_key
);
5380 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5381 struct lxc_conf
*c
, void *data
)
5385 struct lxc_netdev
*netdev
= data
;
5390 memset(retv
, 0, inlen
);
5395 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5400 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5401 struct lxc_conf
*c
, void *data
)
5405 struct lxc_netdev
*netdev
= data
;
5410 memset(retv
, 0, inlen
);
5415 if (netdev
->flags
& IFF_UP
)
5416 strprint(retv
, inlen
, "up");
5421 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5422 struct lxc_conf
*c
, void *data
)
5426 struct lxc_netdev
*netdev
= data
;
5431 memset(retv
, 0, inlen
);
5436 if (netdev
->link
[0] != '\0')
5437 strprint(retv
, inlen
, "%s", netdev
->link
);
5442 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5443 struct lxc_conf
*c
, void *data
)
5445 struct lxc_netdev
*netdev
= data
;
5446 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5449 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5450 struct lxc_conf
*c
, void *data
)
5454 struct lxc_netdev
*netdev
= data
;
5459 memset(retv
, 0, inlen
);
5464 if (netdev
->name
[0] != '\0')
5465 strprint(retv
, inlen
, "%s", netdev
->name
);
5470 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5471 struct lxc_conf
*c
, void *data
)
5476 struct lxc_netdev
*netdev
= data
;
5481 memset(retv
, 0, inlen
);
5486 if (netdev
->type
!= LXC_NET_MACVLAN
)
5489 switch (netdev
->priv
.macvlan_attr
.mode
) {
5490 case MACVLAN_MODE_PRIVATE
:
5493 case MACVLAN_MODE_VEPA
:
5496 case MACVLAN_MODE_BRIDGE
:
5499 case MACVLAN_MODE_PASSTHRU
:
5507 strprint(retv
, inlen
, "%s", mode
);
5512 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5513 struct lxc_conf
*c
, void *data
)
5518 struct lxc_netdev
*netdev
= data
;
5523 memset(retv
, 0, inlen
);
5526 return ret_set_errno(-1, EINVAL
);
5528 if (netdev
->type
!= LXC_NET_IPVLAN
)
5531 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5532 case IPVLAN_MODE_L3
:
5535 case IPVLAN_MODE_L3S
:
5538 case IPVLAN_MODE_L2
:
5546 strprint(retv
, inlen
, "%s", mode
);
5551 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5552 struct lxc_conf
*c
, void *data
)
5557 struct lxc_netdev
*netdev
= data
;
5562 memset(retv
, 0, inlen
);
5565 return ret_set_errno(-1, EINVAL
);
5567 if (netdev
->type
!= LXC_NET_IPVLAN
)
5570 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
5571 case IPVLAN_ISOLATION_BRIDGE
:
5574 case IPVLAN_ISOLATION_PRIVATE
:
5577 case IPVLAN_ISOLATION_VEPA
:
5585 strprint(retv
, inlen
, "%s", mode
);
5590 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
5591 struct lxc_conf
*c
, void *data
)
5596 struct lxc_netdev
*netdev
= data
;
5601 memset(retv
, 0, inlen
);
5604 return ret_set_errno(-1, EINVAL
);
5606 if (netdev
->type
!= LXC_NET_VETH
)
5609 switch (netdev
->priv
.veth_attr
.mode
) {
5610 case VETH_MODE_BRIDGE
:
5613 case VETH_MODE_ROUTER
:
5621 strprint(retv
, inlen
, "%s", mode
);
5626 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
5627 struct lxc_conf
*c
, void *data
)
5631 struct lxc_netdev
*netdev
= data
;
5636 memset(retv
, 0, inlen
);
5641 if (netdev
->type
!= LXC_NET_VETH
)
5644 strprint(retv
, inlen
, "%s",
5645 netdev
->priv
.veth_attr
.pair
[0] != '\0'
5646 ? netdev
->priv
.veth_attr
.pair
5647 : netdev
->priv
.veth_attr
.veth1
);
5652 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
5653 struct lxc_conf
*c
, void *data
)
5657 struct lxc_netdev
*netdev
= data
;
5662 memset(retv
, 0, inlen
);
5667 if (netdev
->upscript
)
5668 strprint(retv
, inlen
, "%s", netdev
->upscript
);
5673 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
5674 struct lxc_conf
*c
, void *data
)
5678 struct lxc_netdev
*netdev
= data
;
5683 memset(retv
, 0, inlen
);
5688 if (netdev
->downscript
)
5689 strprint(retv
, inlen
, "%s", netdev
->downscript
);
5694 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
5695 struct lxc_conf
*c
, void *data
)
5699 struct lxc_netdev
*netdev
= data
;
5704 memset(retv
, 0, inlen
);
5710 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
5715 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
5716 struct lxc_conf
*c
, void *data
)
5720 struct lxc_netdev
*netdev
= data
;
5725 memset(retv
, 0, inlen
);
5731 strprint(retv
, inlen
, "%s", netdev
->mtu
);
5736 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
5737 struct lxc_conf
*c
, void *data
)
5741 struct lxc_netdev
*netdev
= data
;
5746 memset(retv
, 0, inlen
);
5751 if (netdev
->type
!= LXC_NET_VLAN
)
5754 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
5759 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
5760 struct lxc_conf
*c
, void *data
)
5763 char buf
[INET_ADDRSTRLEN
];
5765 struct lxc_netdev
*netdev
= data
;
5770 memset(retv
, 0, inlen
);
5775 if (netdev
->ipv4_gateway_auto
) {
5776 strprint(retv
, inlen
, "auto");
5777 } else if (netdev
->ipv4_gateway_dev
) {
5778 strprint(retv
, inlen
, "dev");
5779 } else if (netdev
->ipv4_gateway
) {
5780 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
5781 strprint(retv
, inlen
, "%s", buf
);
5787 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
5788 struct lxc_conf
*c
, void *data
)
5792 char buf
[INET_ADDRSTRLEN
];
5793 struct lxc_list
*it
;
5795 struct lxc_netdev
*netdev
= data
;
5800 memset(retv
, 0, inlen
);
5805 listlen
= lxc_list_len(&netdev
->ipv4
);
5807 lxc_list_for_each(it
, &netdev
->ipv4
) {
5808 struct lxc_inetdev
*i
= it
->elem
;
5809 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5810 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5811 (listlen
-- > 1) ? "\n" : "");
5817 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
5818 struct lxc_conf
*c
, void *data
)
5822 char buf
[INET_ADDRSTRLEN
];
5823 struct lxc_list
*it
;
5825 struct lxc_netdev
*netdev
= data
;
5830 memset(retv
, 0, inlen
);
5833 return ret_set_errno(-1, EINVAL
);
5835 if (netdev
->type
!= LXC_NET_VETH
)
5838 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
5840 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv4_routes
) {
5841 struct lxc_inetdev
*i
= it
->elem
;
5842 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
5843 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5844 (listlen
-- > 1) ? "\n" : "");
5850 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
5851 struct lxc_conf
*c
, void *data
)
5854 char buf
[INET6_ADDRSTRLEN
];
5856 struct lxc_netdev
*netdev
= data
;
5861 memset(retv
, 0, inlen
);
5866 if (netdev
->ipv6_gateway_auto
) {
5867 strprint(retv
, inlen
, "auto");
5868 } else if (netdev
->ipv6_gateway_dev
) {
5869 strprint(retv
, inlen
, "dev");
5870 } else if (netdev
->ipv6_gateway
) {
5871 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
5872 strprint(retv
, inlen
, "%s", buf
);
5878 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
5879 struct lxc_conf
*c
, void *data
)
5883 char buf
[INET6_ADDRSTRLEN
];
5884 struct lxc_list
*it
;
5886 struct lxc_netdev
*netdev
= data
;
5891 memset(retv
, 0, inlen
);
5896 listlen
= lxc_list_len(&netdev
->ipv6
);
5898 lxc_list_for_each(it
, &netdev
->ipv6
) {
5899 struct lxc_inet6dev
*i
= it
->elem
;
5900 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5901 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5902 (listlen
-- > 1) ? "\n" : "");
5908 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
5909 struct lxc_conf
*c
, void *data
)
5913 char buf
[INET6_ADDRSTRLEN
];
5914 struct lxc_list
*it
;
5916 struct lxc_netdev
*netdev
= data
;
5921 memset(retv
, 0, inlen
);
5924 return ret_set_errno(-1, EINVAL
);
5926 if (netdev
->type
!= LXC_NET_VETH
)
5929 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
5931 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv6_routes
) {
5932 struct lxc_inet6dev
*i
= it
->elem
;
5933 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
5934 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
5935 (listlen
-- > 1) ? "\n" : "");
5941 int lxc_list_config_items(char *retv
, int inlen
)
5950 memset(retv
, 0, inlen
);
5952 for (i
= 0; i
< config_jump_table_size
; i
++) {
5953 char *s
= config_jump_table
[i
].name
;
5955 if (s
[strlen(s
) - 1] == '.')
5958 strprint(retv
, inlen
, "%s\n", s
);
5964 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
5973 memset(retv
, 0, inlen
);
5975 if (!strcmp(key
, "lxc.apparmor")) {
5976 strprint(retv
, inlen
, "allow_incomplete\n");
5977 strprint(retv
, inlen
, "allow_nesting\n");
5978 strprint(retv
, inlen
, "profile\n");
5979 strprint(retv
, inlen
, "raw\n");
5980 } else if (!strcmp(key
, "lxc.cgroup")) {
5981 strprint(retv
, inlen
, "dir\n");
5982 } else if (!strcmp(key
, "lxc.selinux")) {
5983 strprint(retv
, inlen
, "context\n");
5984 strprint(retv
, inlen
, "context.keyring\n");
5985 } else if (!strcmp(key
, "lxc.mount")) {
5986 strprint(retv
, inlen
, "auto\n");
5987 strprint(retv
, inlen
, "entry\n");
5988 strprint(retv
, inlen
, "fstab\n");
5989 } else if (!strcmp(key
, "lxc.rootfs")) {
5990 strprint(retv
, inlen
, "mount\n");
5991 strprint(retv
, inlen
, "options\n");
5992 strprint(retv
, inlen
, "path\n");
5993 } else if (!strcmp(key
, "lxc.uts")) {
5994 strprint(retv
, inlen
, "name\n");
5995 } else if (!strcmp(key
, "lxc.hook")) {
5996 strprint(retv
, inlen
, "autodev\n");
5997 strprint(retv
, inlen
, "autodevtmpfssize\n");
5998 strprint(retv
, inlen
, "clone\n");
5999 strprint(retv
, inlen
, "destroy\n");
6000 strprint(retv
, inlen
, "mount\n");
6001 strprint(retv
, inlen
, "post-stop\n");
6002 strprint(retv
, inlen
, "pre-mount\n");
6003 strprint(retv
, inlen
, "pre-start\n");
6004 strprint(retv
, inlen
, "start-host\n");
6005 strprint(retv
, inlen
, "start\n");
6006 strprint(retv
, inlen
, "stop\n");
6007 } else if (!strcmp(key
, "lxc.cap")) {
6008 strprint(retv
, inlen
, "drop\n");
6009 strprint(retv
, inlen
, "keep\n");
6010 } else if (!strcmp(key
, "lxc.console")) {
6011 strprint(retv
, inlen
, "logfile\n");
6012 strprint(retv
, inlen
, "path\n");
6013 } else if (!strcmp(key
, "lxc.seccomp")) {
6014 strprint(retv
, inlen
, "profile\n");
6015 } else if (!strcmp(key
, "lxc.signal")) {
6016 strprint(retv
, inlen
, "halt\n");
6017 strprint(retv
, inlen
, "reboot\n");
6018 strprint(retv
, inlen
, "stop\n");
6019 } else if (!strcmp(key
, "lxc.start")) {
6020 strprint(retv
, inlen
, "auto\n");
6021 strprint(retv
, inlen
, "delay\n");
6022 strprint(retv
, inlen
, "order\n");
6023 } else if (!strcmp(key
, "lxc.monitor")) {
6024 strprint(retv
, inlen
, "unshare\n");
6025 } else if (!strcmp(key
, "lxc.keyring")) {
6026 strprint(retv
, inlen
, "session\n");
6034 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6037 const char *idxstring
;
6038 struct lxc_netdev
*netdev
;
6042 idxstring
= key
+ 8;
6043 if (!isdigit(*idxstring
))
6046 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
6050 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
6057 memset(retv
, 0, inlen
);
6059 strprint(retv
, inlen
, "type\n");
6060 strprint(retv
, inlen
, "script.up\n");
6061 strprint(retv
, inlen
, "script.down\n");
6063 if (netdev
->type
!= LXC_NET_EMPTY
) {
6064 strprint(retv
, inlen
, "flags\n");
6065 strprint(retv
, inlen
, "link\n");
6066 strprint(retv
, inlen
, "name\n");
6067 strprint(retv
, inlen
, "hwaddr\n");
6068 strprint(retv
, inlen
, "mtu\n");
6069 strprint(retv
, inlen
, "ipv6.address\n");
6070 strprint(retv
, inlen
, "ipv6.gateway\n");
6071 strprint(retv
, inlen
, "ipv4.address\n");
6072 strprint(retv
, inlen
, "ipv4.gateway\n");
6075 switch (netdev
->type
) {
6077 strprint(retv
, inlen
, "veth.pair\n");
6078 strprint(retv
, inlen
, "veth.ipv4.route\n");
6079 strprint(retv
, inlen
, "veth.ipv6.route\n");
6081 case LXC_NET_MACVLAN
:
6082 strprint(retv
, inlen
, "macvlan.mode\n");
6084 case LXC_NET_IPVLAN
:
6085 strprint(retv
, inlen
, "ipvlan.mode\n");
6086 strprint(retv
, inlen
, "ipvlan.isolation\n");
6089 strprint(retv
, inlen
, "vlan.id\n");