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"
36 #include "memory_utils.h"
39 #include "storage/storage.h"
43 #include "include/strlcpy.h"
47 #include "include/strlcat.h"
50 lxc_log_define(confile
, lxc
);
52 #define lxc_config_define(name) \
53 __hot static int set_config_##name(const char *, const char *, \
54 struct lxc_conf *, void *); \
55 __hot static int get_config_##name(const char *, char *, int, \
56 struct lxc_conf *, void *); \
57 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
60 lxc_config_define(autodev
);
61 lxc_config_define(autodev_tmpfs_size
);
62 lxc_config_define(apparmor_allow_incomplete
);
63 lxc_config_define(apparmor_allow_nesting
);
64 lxc_config_define(apparmor_profile
);
65 lxc_config_define(apparmor_raw
);
66 lxc_config_define(cap_drop
);
67 lxc_config_define(cap_keep
);
68 lxc_config_define(cgroup_controller
);
69 lxc_config_define(cgroup2_controller
);
70 lxc_config_define(cgroup_dir
);
71 lxc_config_define(cgroup_monitor_dir
);
72 lxc_config_define(cgroup_monitor_pivot_dir
);
73 lxc_config_define(cgroup_container_dir
);
74 lxc_config_define(cgroup_container_inner_dir
);
75 lxc_config_define(cgroup_relative
);
76 lxc_config_define(console_buffer_size
);
77 lxc_config_define(console_logfile
);
78 lxc_config_define(console_path
);
79 lxc_config_define(console_rotate
);
80 lxc_config_define(console_size
);
81 lxc_config_define(unsupported_key
);
82 lxc_config_define(environment
);
83 lxc_config_define(ephemeral
);
84 lxc_config_define(execute_cmd
);
85 lxc_config_define(group
);
86 lxc_config_define(hooks
);
87 lxc_config_define(hooks_version
);
88 lxc_config_define(idmaps
);
89 lxc_config_define(includefiles
);
90 lxc_config_define(init_cmd
);
91 lxc_config_define(init_cwd
);
92 lxc_config_define(init_gid
);
93 lxc_config_define(init_uid
);
94 lxc_config_define(init_groups
);
95 lxc_config_define(jump_table_net
);
96 lxc_config_define(keyring_session
);
97 lxc_config_define(log_file
);
98 lxc_config_define(log_level
);
99 lxc_config_define(log_syslog
);
100 lxc_config_define(monitor
);
101 lxc_config_define(monitor_signal_pdeath
);
102 lxc_config_define(mount
);
103 lxc_config_define(mount_auto
);
104 lxc_config_define(mount_fstab
);
105 lxc_config_define(namespace_clone
);
106 lxc_config_define(namespace_keep
);
107 lxc_config_define(time_offset_boot
);
108 lxc_config_define(time_offset_monotonic
);
109 lxc_config_define(namespace_share
);
110 lxc_config_define(net
);
111 lxc_config_define(net_flags
);
112 lxc_config_define(net_hwaddr
);
113 lxc_config_define(net_ipv4_address
);
114 lxc_config_define(net_ipv4_gateway
);
115 lxc_config_define(net_ipv6_address
);
116 lxc_config_define(net_ipv6_gateway
);
117 lxc_config_define(net_link
);
118 lxc_config_define(net_l2proxy
);
119 lxc_config_define(net_macvlan_mode
);
120 lxc_config_define(net_ipvlan_mode
);
121 lxc_config_define(net_ipvlan_isolation
);
122 lxc_config_define(net_mtu
);
123 lxc_config_define(net_name
);
124 lxc_config_define(net_script_down
);
125 lxc_config_define(net_script_up
);
126 lxc_config_define(net_type
);
127 lxc_config_define(net_veth_mode
);
128 lxc_config_define(net_veth_pair
);
129 lxc_config_define(net_veth_ipv4_route
);
130 lxc_config_define(net_veth_ipv6_route
);
131 lxc_config_define(net_veth_vlan_id
);
132 lxc_config_define(net_veth_vlan_tagged_id
);
133 lxc_config_define(net_vlan_id
);
134 lxc_config_define(no_new_privs
);
135 lxc_config_define(personality
);
136 lxc_config_define(prlimit
);
137 lxc_config_define(pty_max
);
138 lxc_config_define(rootfs_managed
);
139 lxc_config_define(rootfs_mount
);
140 lxc_config_define(rootfs_options
);
141 lxc_config_define(rootfs_path
);
142 lxc_config_define(seccomp_profile
);
143 lxc_config_define(seccomp_allow_nesting
);
144 lxc_config_define(seccomp_notify_cookie
);
145 lxc_config_define(seccomp_notify_proxy
);
146 lxc_config_define(selinux_context
);
147 lxc_config_define(selinux_context_keyring
);
148 lxc_config_define(signal_halt
);
149 lxc_config_define(signal_reboot
);
150 lxc_config_define(signal_stop
);
151 lxc_config_define(start
);
152 lxc_config_define(tty_max
);
153 lxc_config_define(tty_dir
);
154 lxc_config_define(uts_name
);
155 lxc_config_define(sysctl
);
156 lxc_config_define(proc
);
158 static int set_config_unsupported_key(const char *key
, const char *value
,
159 struct lxc_conf
*lxc_conf
, void *data
)
161 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
164 static int get_config_unsupported_key(const char *key
, char *retv
, int inlen
,
165 struct lxc_conf
*c
, void *data
)
167 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
170 static int clr_config_unsupported_key(const char *key
,
171 struct lxc_conf
*lxc_conf
, void *data
)
173 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
178 * If a new config option is added to this table, be aware that
179 * the order in which the options are places into the table matters.
180 * That means that more specific options of a namespace have to be
181 * placed above more generic ones.
183 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
184 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
185 * has to be placed above lxc.ab.
187 static struct lxc_config_t config_jump_table
[] = {
188 { "lxc.arch", true, set_config_personality
, get_config_personality
, clr_config_personality
, },
189 { "lxc.apparmor.profile", true, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
190 { "lxc.apparmor.allow_incomplete", true, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
191 { "lxc.apparmor.allow_nesting", true, set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
192 { "lxc.apparmor.raw", true, set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
193 { "lxc.autodev.tmpfs.size", true, set_config_autodev_tmpfs_size
, get_config_autodev_tmpfs_size
, clr_config_autodev_tmpfs_size
, },
194 { "lxc.autodev", true, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
195 { "lxc.cap.drop", true, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
196 { "lxc.cap.keep", true, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
197 { "lxc.cgroup2", false, set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
198 { "lxc.cgroup.dir.monitor.pivot", true, set_config_cgroup_monitor_pivot_dir
, get_config_cgroup_monitor_pivot_dir
, clr_config_cgroup_monitor_pivot_dir
, },
199 { "lxc.cgroup.dir.monitor", true, set_config_cgroup_monitor_dir
, get_config_cgroup_monitor_dir
, clr_config_cgroup_monitor_dir
, },
200 { "lxc.cgroup.dir.container.inner", true, set_config_cgroup_container_inner_dir
, get_config_cgroup_container_inner_dir
, clr_config_cgroup_container_inner_dir
, },
201 { "lxc.cgroup.dir.container", true, set_config_cgroup_container_dir
, get_config_cgroup_container_dir
, clr_config_cgroup_container_dir
, },
202 { "lxc.cgroup.dir", true, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
203 { "lxc.cgroup.relative", true, set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
204 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
205 { "lxc.console.buffer.size", true, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
206 { "lxc.console.logfile", true, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
207 { "lxc.console.path", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
208 { "lxc.console.rotate", true, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
209 { "lxc.console.size", true, set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
210 { "lxc.environment", true, set_config_environment
, get_config_environment
, clr_config_environment
, },
211 { "lxc.ephemeral", true, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
212 { "lxc.execute.cmd", true, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
213 { "lxc.group", true, set_config_group
, get_config_group
, clr_config_group
, },
214 { "lxc.hook.autodev", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
215 { "lxc.hook.clone", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
216 { "lxc.hook.destroy", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
217 { "lxc.hook.mount", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
218 { "lxc.hook.post-stop", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
219 { "lxc.hook.pre-mount", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
220 { "lxc.hook.pre-start", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
221 { "lxc.hook.start", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
222 { "lxc.hook.start-host", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
223 { "lxc.hook.stop", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
224 { "lxc.hook.version", true, set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
225 { "lxc.hook", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
226 { "lxc.idmap", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
227 { "lxc.include", true, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
228 { "lxc.init.cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
229 { "lxc.init.gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
230 { "lxc.init.groups", true, set_config_init_groups
, get_config_init_groups
, clr_config_init_groups
, },
231 { "lxc.init.uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
232 { "lxc.init.cwd", true, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
233 { "lxc.keyring.session", true, set_config_keyring_session
, get_config_keyring_session
, clr_config_keyring_session
},
234 { "lxc.log.file", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
235 { "lxc.log.level", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
236 { "lxc.log.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
237 { "lxc.monitor.unshare", true, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
238 { "lxc.monitor.signal.pdeath", true, set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
239 { "lxc.mount.auto", true, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
240 { "lxc.mount.entry", true, set_config_mount
, get_config_mount
, clr_config_mount
, },
241 { "lxc.mount.fstab", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
242 { "lxc.namespace.clone", true, set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
243 { "lxc.namespace.keep", true, set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
244 { "lxc.namespace.share.", false, set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
245 { "lxc.time.offset.boot", true, set_config_time_offset_boot
, get_config_time_offset_boot
, clr_config_time_offset_boot
, },
246 { "lxc.time.offset.monotonic", true, set_config_time_offset_monotonic
, get_config_time_offset_monotonic
, clr_config_time_offset_monotonic
, },
247 { "lxc.net.", false, set_config_jump_table_net
, get_config_jump_table_net
, clr_config_jump_table_net
, },
248 { "lxc.net", true, set_config_net
, get_config_net
, clr_config_net
, },
249 { "lxc.no_new_privs", true, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
250 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
251 { "lxc.pty.max", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
252 { "lxc.rootfs.managed", true, set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
253 { "lxc.rootfs.mount", true, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
254 { "lxc.rootfs.options", true, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
255 { "lxc.rootfs.path", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
256 { "lxc.seccomp.allow_nesting", true, set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
257 { "lxc.seccomp.notify.cookie", true, set_config_seccomp_notify_cookie
, get_config_seccomp_notify_cookie
, clr_config_seccomp_notify_cookie
, },
258 { "lxc.seccomp.notify.proxy", true, set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
259 { "lxc.seccomp.profile", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
260 { "lxc.selinux.context.keyring", true, set_config_selinux_context_keyring
, get_config_selinux_context_keyring
, clr_config_selinux_context_keyring
},
261 { "lxc.selinux.context", true, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
262 { "lxc.signal.halt", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
263 { "lxc.signal.reboot", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
264 { "lxc.signal.stop", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
265 { "lxc.start.auto", true, set_config_start
, get_config_start
, clr_config_start
, },
266 { "lxc.start.delay", true, set_config_start
, get_config_start
, clr_config_start
, },
267 { "lxc.start.order", true, set_config_start
, get_config_start
, clr_config_start
, },
268 { "lxc.tty.dir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
269 { "lxc.tty.max", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
270 { "lxc.uts.name", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
271 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
272 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
275 static struct lxc_config_t unsupported_config_key
= {
278 set_config_unsupported_key
,
279 get_config_unsupported_key
,
280 clr_config_unsupported_key
,
283 struct lxc_config_net_t
{
287 static struct lxc_config_net_t config_jump_table_net
[] = {
288 /* If a longer key is added please update. */
289 #define NETWORK_SUBKEY_SIZE_MAX (STRLITERALLEN("veth.vlan.tagged.id") * 2)
290 { "flags", true, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
291 { "hwaddr", true, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
292 { "ipv4.address", true, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
293 { "ipv4.gateway", true, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
294 { "ipv6.address", true, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
295 { "ipv6.gateway", true, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
296 { "link", true, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
297 { "l2proxy", true, set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
298 { "macvlan.mode", true, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
299 { "ipvlan.mode", true, set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
300 { "ipvlan.isolation", true, set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
301 { "mtu", true, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
302 { "name", true, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
303 { "script.down", true, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
304 { "script.up", true, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
305 { "type", true, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
306 { "vlan.id", true, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
307 { "veth.mode", true, set_config_net_veth_mode
, get_config_net_veth_mode
, clr_config_net_veth_mode
, },
308 { "veth.pair", true, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
309 { "veth.ipv4.route", true, set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
310 { "veth.ipv6.route", true, set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
311 { "veth.vlan.id", true, set_config_net_veth_vlan_id
, get_config_net_veth_vlan_id
, clr_config_net_veth_vlan_id
, },
312 { "veth.vlan.tagged.id", true, set_config_net_veth_vlan_tagged_id
, get_config_net_veth_vlan_tagged_id
, clr_config_net_veth_vlan_tagged_id
, },
315 static struct lxc_config_net_t unsupported_config_net_key
= {
318 set_config_unsupported_key
,
319 get_config_unsupported_key
,
320 clr_config_unsupported_key
,
323 struct lxc_config_t
*lxc_get_config_exact(const char *key
)
327 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++)
328 if (strequal(config_jump_table
[i
].name
, key
))
329 return &config_jump_table
[i
];
334 /* Assume a reasonable subkey size limit. */
335 #define LXC_SUBKEY_LEN_MAX 256
337 static inline int match_config_item(const struct lxc_config_t
*entry
, const char *key
)
342 return strequal(entry
->name
, key
);
344 /* There should be no subkey longer than this. */
345 len
= strnlen(entry
->name
, LXC_SUBKEY_LEN_MAX
);
346 if (len
== LXC_SUBKEY_LEN_MAX
)
347 return error_ret(-E2BIG
, "Excessive subkey length");
349 return strnequal(entry
->name
, key
, len
);
352 struct lxc_config_t
*lxc_get_config(const char *key
)
354 for (size_t i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
355 struct lxc_config_t
*cur
= &config_jump_table
[i
];
357 switch (match_config_item(cur
, key
)) {
361 return &unsupported_config_key
;
367 return &unsupported_config_key
;
370 static inline bool match_config_net_item(const struct lxc_config_net_t
*entry
,
374 return strequal(entry
->name
, key
);
375 return strnequal(entry
->name
, key
, strlen(entry
->name
));
378 static struct lxc_config_net_t
*lxc_get_config_net(const char *key
)
380 for (size_t i
= 0; i
< ARRAY_SIZE(config_jump_table_net
); i
++) {
381 struct lxc_config_net_t
*cur
= &config_jump_table_net
[i
];
383 if (!match_config_net_item(cur
, key
))
389 return &unsupported_config_net_key
;
392 static int set_config_net(const char *key
, const char *value
,
393 struct lxc_conf
*lxc_conf
, void *data
)
395 if (!lxc_config_value_empty(value
))
396 return syserror_set(-EINVAL
, "lxc.net must not have a value");
398 return clr_config_net(key
, lxc_conf
, data
);
401 static int set_config_net_type(const char *key
, const char *value
,
402 struct lxc_conf
*lxc_conf
, void *data
)
404 struct lxc_netdev
*netdev
= data
;
407 return ret_errno(EINVAL
);
409 clr_config_net_type(key
, lxc_conf
, data
);
410 if (lxc_config_value_empty(value
))
413 if (strequal(value
, "veth")) {
414 netdev
->type
= LXC_NET_VETH
;
415 INIT_LIST_HEAD(&netdev
->priv
.veth_attr
.ipv4_routes
);
416 INIT_LIST_HEAD(&netdev
->priv
.veth_attr
.ipv6_routes
);
417 lxc_list_init(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
418 if (!lxc_veth_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
419 lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, "bridge");
420 } else if (strequal(value
, "macvlan")) {
421 netdev
->type
= LXC_NET_MACVLAN
;
422 if (!lxc_macvlan_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
423 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
424 } else if (strequal(value
, "ipvlan")) {
425 netdev
->type
= LXC_NET_IPVLAN
;
426 if (!lxc_ipvlan_flag_to_mode(netdev
->priv
.ipvlan_attr
.mode
))
427 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
428 if (!lxc_ipvlan_flag_to_isolation(netdev
->priv
.ipvlan_attr
.isolation
))
429 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
430 } else if (strequal(value
, "vlan")) {
431 netdev
->type
= LXC_NET_VLAN
;
432 } else if (strequal(value
, "phys")) {
433 netdev
->type
= LXC_NET_PHYS
;
434 } else if (strequal(value
, "empty")) {
435 netdev
->type
= LXC_NET_EMPTY
;
436 /* We don't support custom loopback device names. */
437 (void)strlcpy(netdev
->name
, "lo", IFNAMSIZ
);
438 } else if (strequal(value
, "none")) {
439 netdev
->type
= LXC_NET_NONE
;
441 return log_error(-1, "Invalid network type %s", value
);
447 static int set_config_net_flags(const char *key
, const char *value
,
448 struct lxc_conf
*lxc_conf
, void *data
)
450 struct lxc_netdev
*netdev
= data
;
453 return ret_errno(EINVAL
);
455 if (lxc_config_value_empty(value
))
456 return clr_config_net_flags(key
, lxc_conf
, data
);
458 netdev
->flags
|= IFF_UP
;
463 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
464 struct lxc_netdev
*netdev
)
466 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddr
= NULL
;
467 struct netns_ifaddrs
*ifa
;
470 const char *type_key
= "lxc.net.type";
471 const char *link_key
= "lxc.net.link";
472 const char *tmpvalue
= "phys";
474 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0)
475 return log_error_errno(-1, errno
, "Failed to get network interfaces");
477 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
481 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
484 if (strnequal(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
485 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
488 ret
= set_config_net_link(
489 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
491 ERROR("Failed to create matched ifnames");
495 ERROR("Failed to create matched ifnames");
504 static int set_config_net_link(const char *key
, const char *value
,
505 struct lxc_conf
*lxc_conf
, void *data
)
507 struct lxc_netdev
*netdev
= data
;
511 return ret_errno(EINVAL
);
513 if (lxc_config_value_empty(value
))
514 return clr_config_net_link(key
, lxc_conf
, data
);
516 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
517 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
519 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
524 static int set_config_net_l2proxy(const char *key
, const char *value
,
525 struct lxc_conf
*lxc_conf
, void *data
)
527 struct lxc_netdev
*netdev
= data
;
528 unsigned int val
= 0;
532 return ret_errno(EINVAL
);
534 if (lxc_config_value_empty(value
))
535 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
537 ret
= lxc_safe_uint(value
, &val
);
539 return ret_errno(ret
);
543 netdev
->l2proxy
= false;
546 netdev
->l2proxy
= true;
550 return ret_errno(EINVAL
);
553 static int set_config_net_name(const char *key
, const char *value
,
554 struct lxc_conf
*lxc_conf
, void *data
)
556 struct lxc_netdev
*netdev
= data
;
559 return ret_errno(EINVAL
);
561 if (lxc_config_value_empty(value
))
562 return clr_config_net_name(key
, lxc_conf
, data
);
564 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
568 static int set_config_net_veth_mode(const char *key
, const char *value
,
569 struct lxc_conf
*lxc_conf
, void *data
)
571 struct lxc_netdev
*netdev
= data
;
574 return ret_errno(EINVAL
);
576 if (netdev
->type
!= LXC_NET_VETH
)
577 return ret_errno(EINVAL
);
579 if (lxc_config_value_empty(value
))
580 return clr_config_net_veth_mode(key
, lxc_conf
, data
);
583 return ret_errno(EINVAL
);
585 return lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, value
);
588 static int set_config_net_veth_pair(const char *key
, const char *value
,
589 struct lxc_conf
*lxc_conf
, void *data
)
591 struct lxc_netdev
*netdev
= data
;
594 return ret_errno(EINVAL
);
596 if (netdev
->type
!= LXC_NET_VETH
)
597 return ret_errno(EINVAL
);
599 if (lxc_config_value_empty(value
))
600 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
602 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
,
603 sizeof(netdev
->priv
.veth_attr
.pair
));
606 static int set_config_net_veth_vlan_id(const char *key
, const char *value
,
607 struct lxc_conf
*lxc_conf
, void *data
)
610 struct lxc_netdev
*netdev
= data
;
613 return ret_errno(EINVAL
);
615 if (netdev
->type
!= LXC_NET_VETH
)
616 return ret_errno(EINVAL
);
618 if (lxc_config_value_empty(value
))
619 return clr_config_net_veth_vlan_id(key
, lxc_conf
, data
);
621 if (strequal(value
, "none")) {
622 netdev
->priv
.veth_attr
.vlan_id
= BRIDGE_VLAN_NONE
;
624 unsigned short vlan_id
;
626 ret
= get_u16(&vlan_id
, value
, 0);
628 return ret_errno(EINVAL
);
630 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
631 return ret_errno(EINVAL
);
633 netdev
->priv
.veth_attr
.vlan_id
= vlan_id
;
636 netdev
->priv
.veth_attr
.vlan_id_set
= true;
640 static int set_config_net_veth_vlan_tagged_id(const char *key
, const char *value
,
641 struct lxc_conf
*lxc_conf
,
644 __do_free
struct lxc_list
*list
= NULL
;
646 unsigned short vlan_id
;
647 struct lxc_netdev
*netdev
= data
;
650 return ret_errno(EINVAL
);
652 if (netdev
->type
!= LXC_NET_VETH
)
653 return ret_errno(EINVAL
);
655 if (lxc_config_value_empty(value
))
656 return clr_config_net_veth_vlan_tagged_id(key
, lxc_conf
, data
);
658 ret
= get_u16(&vlan_id
, value
, 0);
660 return ret_errno(EINVAL
);
662 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
663 return ret_errno(EINVAL
);
665 list
= lxc_list_new();
667 return ret_errno(ENOMEM
);
669 list
->elem
= UINT_TO_PTR(vlan_id
);
671 lxc_list_add_tail(&netdev
->priv
.veth_attr
.vlan_tagged_ids
, move_ptr(list
));
676 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
677 struct lxc_conf
*lxc_conf
, void *data
)
679 struct lxc_netdev
*netdev
= data
;
682 return ret_errno(EINVAL
);
684 if (netdev
->type
!= LXC_NET_MACVLAN
)
685 return ret_errno(EINVAL
);
687 if (lxc_config_value_empty(value
))
688 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
690 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
693 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
694 struct lxc_conf
*lxc_conf
, void *data
)
696 struct lxc_netdev
*netdev
= data
;
699 return ret_errno(EINVAL
);
701 if (netdev
->type
!= LXC_NET_IPVLAN
)
702 return syserror_set(-EINVAL
, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network", value
);
704 if (lxc_config_value_empty(value
))
705 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
707 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
710 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
711 struct lxc_conf
*lxc_conf
, void *data
)
713 struct lxc_netdev
*netdev
= data
;
716 return ret_errno(EINVAL
);
718 if (netdev
->type
!= LXC_NET_IPVLAN
)
719 return syserror_set(-EINVAL
, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network", value
);
721 if (lxc_config_value_empty(value
))
722 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
724 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
727 static int set_config_net_hwaddr(const char *key
, const char *value
,
728 struct lxc_conf
*lxc_conf
, void *data
)
730 __do_free
char *new_value
= NULL
;
731 struct lxc_netdev
*netdev
= data
;
734 return ret_errno(EINVAL
);
736 clr_config_net_hwaddr(key
, lxc_conf
, data
);
737 if (lxc_config_value_empty(value
))
740 new_value
= strdup(value
);
742 return ret_errno(ENOMEM
);
744 rand_complete_hwaddr(new_value
);
745 if (!lxc_config_value_empty(new_value
))
746 netdev
->hwaddr
= move_ptr(new_value
);
751 static int set_config_net_vlan_id(const char *key
, const char *value
,
752 struct lxc_conf
*lxc_conf
, void *data
)
755 struct lxc_netdev
*netdev
= data
;
758 return ret_errno(EINVAL
);
760 if (netdev
->type
!= LXC_NET_VLAN
)
761 return ret_errno(EINVAL
);
763 if (lxc_config_value_empty(value
))
764 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
766 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
773 static int set_config_net_mtu(const char *key
, const char *value
,
774 struct lxc_conf
*lxc_conf
, void *data
)
776 struct lxc_netdev
*netdev
= data
;
779 return ret_errno(EINVAL
);
781 clr_config_net_mtu(key
, lxc_conf
, data
);
782 if (lxc_config_value_empty(value
))
785 return set_config_string_item(&netdev
->mtu
, value
);
788 static int set_config_net_ipv4_address(const char *key
, const char *value
,
789 struct lxc_conf
*lxc_conf
, void *data
)
791 __do_free
char *addr
= NULL
;
792 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
794 struct lxc_netdev
*netdev
= data
;
795 char *cursor
, *slash
;
796 char *bcast
= NULL
, *prefix
= NULL
;
799 return ret_errno(EINVAL
);
801 if (lxc_config_value_empty(value
))
802 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
804 inetdev
= zalloc(sizeof(*inetdev
));
806 return ret_errno(ENOMEM
);
808 addr
= strdup(value
);
810 return ret_errno(ENOMEM
);
812 cursor
= strstr(addr
, " ");
818 slash
= strstr(addr
, "/");
824 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
826 return log_error_errno(-1, errno
, "Invalid ipv4 address \"%s\"", value
);
829 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
831 return log_error_errno(-1, errno
, "Invalid ipv4 broadcast address \"%s\"", value
);
835 /* No prefix specified, determine it from the network class. */
838 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
840 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
841 if (ret
|| inetdev
->prefix
> 32)
842 return ret_errno(EINVAL
);
844 /* If no broadcast address, compute one from the prefix and address. */
846 unsigned int shift
= LAST_BIT_PER_TYPE(inetdev
->prefix
);
848 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
849 if (inetdev
->prefix
< shift
)
850 shift
= inetdev
->prefix
;
851 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> shift
);
854 list_add_tail(&inetdev
->head
, &netdev
->ipv4_addresses
);
860 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
861 struct lxc_conf
*lxc_conf
, void *data
)
863 struct lxc_netdev
*netdev
= data
;
866 return ret_errno(EINVAL
);
868 clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
869 if (lxc_config_value_empty(value
))
872 if (strequal(value
, "auto")) {
873 netdev
->ipv4_gateway
= NULL
;
874 netdev
->ipv4_gateway_auto
= true;
875 } else if (strequal(value
, "dev")) {
876 netdev
->ipv4_gateway
= NULL
;
877 netdev
->ipv4_gateway_auto
= false;
878 netdev
->ipv4_gateway_dev
= true;
880 __do_free
struct in_addr
*gw
= NULL
;
883 gw
= zalloc(sizeof(*gw
));
885 return ret_errno(ENOMEM
);
887 ret
= inet_pton(AF_INET
, value
, gw
);
889 return log_error_errno(-1, errno
, "Invalid ipv4 gateway address \"%s\"", value
);
891 netdev
->ipv4_gateway
= move_ptr(gw
);
892 netdev
->ipv4_gateway_auto
= false;
898 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
899 struct lxc_conf
*lxc_conf
, void *data
)
901 __do_free
char *valdup
= NULL
;
902 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
904 char *netmask
, *slash
;
905 struct lxc_netdev
*netdev
= data
;
908 return ret_errno(EINVAL
);
910 if (netdev
->type
!= LXC_NET_VETH
)
911 return syserror_set(-EINVAL
, "Invalid ipv4 route \"%s\", can only be used with veth network", value
);
913 if (lxc_config_value_empty(value
))
914 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
916 inetdev
= zalloc(sizeof(*inetdev
));
918 return ret_errno(ENOMEM
);
920 valdup
= strdup(value
);
922 return ret_errno(ENOMEM
);
924 slash
= strchr(valdup
, '/');
926 return ret_errno(EINVAL
);
931 return ret_errno(EINVAL
);
935 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
936 if (ret
< 0 || inetdev
->prefix
> 32)
937 return ret_errno(EINVAL
);
939 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
941 return ret_errno(EINVAL
);
943 list_add_tail(&inetdev
->head
, &netdev
->priv
.veth_attr
.ipv4_routes
);
949 static int set_config_net_ipv6_address(const char *key
, const char *value
,
950 struct lxc_conf
*lxc_conf
, void *data
)
952 __do_free
char *valdup
= NULL
;
953 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
955 struct lxc_netdev
*netdev
= data
;
956 char *slash
, *netmask
;
959 return ret_errno(EINVAL
);
961 if (lxc_config_value_empty(value
))
962 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
964 inet6dev
= zalloc(sizeof(*inet6dev
));
966 return ret_errno(ENOMEM
);
968 valdup
= strdup(value
);
970 return ret_errno(ENOMEM
);
972 inet6dev
->prefix
= 64;
973 slash
= strstr(valdup
, "/");
978 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
983 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
985 return log_error_errno(-EINVAL
, EINVAL
, "Invalid ipv6 address \"%s\"", valdup
);
987 list_add_tail(&inet6dev
->head
, &netdev
->ipv6_addresses
);
993 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
994 struct lxc_conf
*lxc_conf
, void *data
)
996 struct lxc_netdev
*netdev
= data
;
999 return ret_errno(EINVAL
);
1001 clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
1002 if (lxc_config_value_empty(value
))
1005 if (strequal(value
, "auto")) {
1006 netdev
->ipv6_gateway
= NULL
;
1007 netdev
->ipv6_gateway_auto
= true;
1008 } else if (strequal(value
, "dev")) {
1009 netdev
->ipv6_gateway
= NULL
;
1010 netdev
->ipv6_gateway_auto
= false;
1011 netdev
->ipv6_gateway_dev
= true;
1014 __do_free
struct in6_addr
*gw
= NULL
;
1016 gw
= zalloc(sizeof(*gw
));
1018 return ret_errno(ENOMEM
);
1020 ret
= inet_pton(AF_INET6
, value
, gw
);
1021 if (!ret
|| ret
< 0)
1022 return log_error_errno(-EINVAL
, EINVAL
,
1023 "Invalid ipv6 gateway address \"%s\"", value
);
1025 netdev
->ipv6_gateway
= move_ptr(gw
);
1026 netdev
->ipv6_gateway_auto
= false;
1032 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
1033 struct lxc_conf
*lxc_conf
, void *data
)
1035 __do_free
char *valdup
= NULL
;
1036 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
1038 char *netmask
, *slash
;
1039 struct lxc_netdev
*netdev
= data
;
1042 return ret_errno(EINVAL
);
1044 if (netdev
->type
!= LXC_NET_VETH
)
1045 return syserror_set(-EINVAL
, "Invalid ipv6 route \"%s\", can only be used with veth network", value
);
1047 if (lxc_config_value_empty(value
))
1048 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
1050 inet6dev
= zalloc(sizeof(*inet6dev
));
1052 return ret_errno(ENOMEM
);
1054 valdup
= strdup(value
);
1056 return ret_errno(ENOMEM
);
1058 slash
= strchr(valdup
, '/');
1060 return ret_errno(EINVAL
);
1065 return ret_errno(EINVAL
);
1069 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
1070 if (ret
< 0 || inet6dev
->prefix
> 128)
1071 return ret_errno(EINVAL
);
1073 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
1074 if (!ret
|| ret
< 0)
1075 return ret_errno(EINVAL
);
1077 list_add_tail(&inet6dev
->head
, &netdev
->priv
.veth_attr
.ipv6_routes
);
1083 static int set_config_net_script_up(const char *key
, const char *value
,
1084 struct lxc_conf
*lxc_conf
, void *data
)
1086 struct lxc_netdev
*netdev
= data
;
1089 return ret_errno(EINVAL
);
1091 clr_config_net_script_up(key
, lxc_conf
, data
);
1092 if (lxc_config_value_empty(value
))
1095 return set_config_string_item(&netdev
->upscript
, value
);
1098 static int set_config_net_script_down(const char *key
, const char *value
,
1099 struct lxc_conf
*lxc_conf
, void *data
)
1101 struct lxc_netdev
*netdev
= data
;
1104 return ret_errno(EINVAL
);
1106 clr_config_net_script_down(key
, lxc_conf
, data
);
1107 if (lxc_config_value_empty(value
))
1110 return set_config_string_item(&netdev
->downscript
, value
);
1113 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, __owns
char *hook
)
1115 __do_free
char *val
= hook
;
1116 struct lxc_list
*hooklist
;
1118 hooklist
= lxc_list_new();
1120 return ret_errno(ENOMEM
);
1122 hooklist
->elem
= move_ptr(val
);
1123 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1128 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1129 struct lxc_conf
*lxc_conf
, void *data
)
1132 if (lxc_config_value_empty(value
))
1133 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1135 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1138 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1139 return ret_errno(EINVAL
);
1143 return ret_errno(ENOSYS
);
1147 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1148 struct lxc_conf
*lxc_conf
, void *data
)
1150 #ifdef HAVE_SECCOMP_NOTIFY
1151 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1153 return ret_errno(ENOSYS
);
1157 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1158 struct lxc_conf
*lxc_conf
, void *data
)
1160 #ifdef HAVE_SECCOMP_NOTIFY
1163 if (lxc_config_value_empty(value
))
1164 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1166 if (!strnequal(value
, "unix:", 5))
1167 return ret_errno(EINVAL
);
1170 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1175 return ret_errno(ENOSYS
);
1179 static int set_config_seccomp_profile(const char *key
, const char *value
,
1180 struct lxc_conf
*lxc_conf
, void *data
)
1183 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1185 return ret_errno(ENOSYS
);
1189 static int set_config_execute_cmd(const char *key
, const char *value
,
1190 struct lxc_conf
*lxc_conf
, void *data
)
1192 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1195 static int set_config_init_cmd(const char *key
, const char *value
,
1196 struct lxc_conf
*lxc_conf
, void *data
)
1198 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1201 static int set_config_init_cwd(const char *key
, const char *value
,
1202 struct lxc_conf
*lxc_conf
, void *data
)
1204 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1207 static int set_config_init_uid(const char *key
, const char *value
,
1208 struct lxc_conf
*lxc_conf
, void *data
)
1210 unsigned int init_uid
;
1212 if (lxc_config_value_empty(value
)) {
1213 lxc_conf
->init_uid
= 0;
1217 if (lxc_safe_uint(value
, &init_uid
) < 0)
1220 lxc_conf
->init_uid
= init_uid
;
1225 static int set_config_init_gid(const char *key
, const char *value
,
1226 struct lxc_conf
*lxc_conf
, void *data
)
1228 unsigned int init_gid
;
1230 if (lxc_config_value_empty(value
)) {
1231 lxc_conf
->init_gid
= 0;
1235 if (lxc_safe_uint(value
, &init_gid
) < 0)
1238 lxc_conf
->init_gid
= init_gid
;
1243 static int set_config_init_groups(const char *key
, const char *value
,
1244 struct lxc_conf
*lxc_conf
, void *data
)
1246 __do_free
char *value_dup
= NULL
;
1247 gid_t
*init_groups
= NULL
;
1248 size_t num_groups
= 0;
1252 if (lxc_config_value_empty(value
))
1253 return clr_config_init_groups(key
, lxc_conf
, NULL
);
1255 value_dup
= strdup(value
);
1259 lxc_iterate_parts(token
, value_dup
, ",")
1262 if (num_groups
== INT_MAX
)
1263 return log_error_errno(-ERANGE
, ERANGE
, "Excessive number of supplementary groups specified");
1265 /* This means the string wasn't empty and all we found was garbage. */
1266 if (num_groups
== 0)
1267 return log_error_errno(-EINVAL
, EINVAL
, "No valid groups specified %s", value
);
1269 idx
= lxc_conf
->init_groups
.size
;
1270 init_groups
= realloc(lxc_conf
->init_groups
.list
, sizeof(gid_t
) * (idx
+ num_groups
));
1272 return ret_errno(ENOMEM
);
1275 * Once the realloc() succeeded we need to hand control of the memory
1276 * back to the config otherwise we risk a double-free when
1277 * lxc_conf_free() is called.
1279 lxc_conf
->init_groups
.list
= init_groups
;
1281 /* Restore duplicated value so we can call lxc_iterate_parts() again. */
1282 strcpy(value_dup
, value
);
1284 lxc_iterate_parts(token
, value_dup
, ",") {
1289 ret
= lxc_safe_uint(token
, &group
);
1291 return log_error_errno(ret
, -ret
, "Failed to parse group %s", token
);
1293 init_groups
[idx
++] = group
;
1296 lxc_conf
->init_groups
.size
+= num_groups
;
1301 static int set_config_hooks(const char *key
, const char *value
,
1302 struct lxc_conf
*lxc_conf
, void *data
)
1304 __do_free
char *copy
= NULL
;
1306 if (lxc_config_value_empty(value
))
1307 return lxc_clear_hooks(lxc_conf
, key
);
1309 if (strequal(key
+ 4, "hook"))
1310 return log_error_errno(-EINVAL
, EINVAL
, "lxc.hook must not have a value");
1312 copy
= strdup(value
);
1314 return ret_errno(ENOMEM
);
1316 if (strequal(key
+ 9, "pre-start"))
1317 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, move_ptr(copy
));
1318 else if (strequal(key
+ 9, "start-host"))
1319 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, move_ptr(copy
));
1320 else if (strequal(key
+ 9, "pre-mount"))
1321 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, move_ptr(copy
));
1322 else if (strequal(key
+ 9, "autodev"))
1323 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, move_ptr(copy
));
1324 else if (strequal(key
+ 9, "mount"))
1325 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, move_ptr(copy
));
1326 else if (strequal(key
+ 9, "start"))
1327 return add_hook(lxc_conf
, LXCHOOK_START
, move_ptr(copy
));
1328 else if (strequal(key
+ 9, "stop"))
1329 return add_hook(lxc_conf
, LXCHOOK_STOP
, move_ptr(copy
));
1330 else if (strequal(key
+ 9, "post-stop"))
1331 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, move_ptr(copy
));
1332 else if (strequal(key
+ 9, "clone"))
1333 return add_hook(lxc_conf
, LXCHOOK_CLONE
, move_ptr(copy
));
1334 else if (strequal(key
+ 9, "destroy"))
1335 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, move_ptr(copy
));
1337 return ret_errno(EINVAL
);
1340 static int set_config_hooks_version(const char *key
, const char *value
,
1341 struct lxc_conf
*lxc_conf
, void *data
)
1346 if (lxc_config_value_empty(value
))
1347 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1349 ret
= lxc_safe_uint(value
, &tmp
);
1354 return syserror_set(-EINVAL
, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
1356 lxc_conf
->hooks_version
= tmp
;
1361 static int set_config_personality(const char *key
, const char *value
,
1362 struct lxc_conf
*lxc_conf
, void *data
)
1365 personality_t personality
;
1367 ret
= lxc_config_parse_arch(value
, &personality
);
1369 return syserror("Unsupported personality \"%s\"", value
);
1371 lxc_conf
->personality
= personality
;
1375 static int set_config_pty_max(const char *key
, const char *value
,
1376 struct lxc_conf
*lxc_conf
, void *data
)
1379 unsigned int max
= 0;
1381 if (lxc_config_value_empty(value
)) {
1382 lxc_conf
->pty_max
= 0;
1386 ret
= lxc_safe_uint(value
, &max
);
1388 return ret_errno(EINVAL
);
1390 lxc_conf
->pty_max
= max
;
1395 /* We only need to check whether the first byte of the key after the lxc.start.
1396 * prefix matches our expectations since they fortunately all start with a
1397 * different letter. If anything was wrong with the key we would have already
1398 * noticed when the callback was called.
1400 static int set_config_start(const char *key
, const char *value
,
1401 struct lxc_conf
*lxc_conf
, void *data
)
1406 is_empty
= lxc_config_value_empty(value
);
1408 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1410 lxc_conf
->start_auto
= 0;
1414 ret
= lxc_safe_uint(value
, &lxc_conf
->start_auto
);
1418 if (lxc_conf
->start_auto
> 1)
1419 return ret_errno(EINVAL
);
1422 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1424 lxc_conf
->start_delay
= 0;
1428 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1429 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1431 lxc_conf
->start_order
= 0;
1435 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1438 return ret_errno(EINVAL
);
1441 static int set_config_monitor(const char *key
, const char *value
,
1442 struct lxc_conf
*lxc_conf
, void *data
)
1444 if (lxc_config_value_empty(value
)) {
1445 lxc_conf
->monitor_unshare
= 0;
1449 if (strequal(key
+ 12, "unshare"))
1450 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1452 return ret_errno(EINVAL
);
1455 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1456 struct lxc_conf
*lxc_conf
, void *data
)
1458 if (lxc_config_value_empty(value
)) {
1459 lxc_conf
->monitor_signal_pdeath
= 0;
1463 if (strequal(key
+ 12, "signal.pdeath")) {
1466 sig_n
= sig_parse(value
);
1468 return ret_errno(EINVAL
);
1470 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1474 return ret_errno(EINVAL
);
1477 static int set_config_group(const char *key
, const char *value
,
1478 struct lxc_conf
*lxc_conf
, void *data
)
1480 __do_free
char *groups
= NULL
;
1483 if (lxc_config_value_empty(value
))
1484 return lxc_clear_groups(lxc_conf
);
1486 groups
= strdup(value
);
1488 return ret_errno(ENOMEM
);
1490 /* In case several groups are specified in a single line split these
1491 * groups in a single element for the list.
1493 lxc_iterate_parts(token
, groups
, " \t") {
1494 __do_free
struct lxc_list
*grouplist
= NULL
;
1496 grouplist
= lxc_list_new();
1498 return ret_errno(ENOMEM
);
1500 grouplist
->elem
= strdup(token
);
1501 if (!grouplist
->elem
)
1502 return ret_errno(ENOMEM
);
1504 lxc_list_add_tail(&lxc_conf
->groups
, move_ptr(grouplist
));
1510 static int set_config_environment(const char *key
, const char *value
,
1511 struct lxc_conf
*lxc_conf
, void *data
)
1513 __do_free
char *dup
= NULL
, *val
= NULL
;
1514 __do_free
struct environment_entry
*new_env
= NULL
;
1517 if (lxc_config_value_empty(value
))
1518 return lxc_clear_environment(lxc_conf
);
1520 new_env
= zalloc(sizeof(struct environment_entry
));
1522 return ret_errno(ENOMEM
);
1524 dup
= strdup(value
);
1526 return ret_errno(ENOMEM
);
1528 env_val
= strchr(dup
, '=');
1530 env_val
= getenv(dup
);
1536 return ret_errno(ENOENT
);
1538 val
= strdup(env_val
);
1540 return ret_errno(ENOMEM
);
1542 new_env
->key
= move_ptr(dup
);
1543 new_env
->val
= move_ptr(val
);
1545 list_add_tail(&new_env
->head
, &lxc_conf
->environment
);
1551 static int set_config_tty_max(const char *key
, const char *value
,
1552 struct lxc_conf
*lxc_conf
, void *data
)
1555 unsigned int nbtty
= 0;
1557 if (lxc_config_value_empty(value
)) {
1558 lxc_conf
->ttys
.max
= 0;
1562 ret
= lxc_safe_uint(value
, &nbtty
);
1566 lxc_conf
->ttys
.max
= nbtty
;
1571 static int set_config_tty_dir(const char *key
, const char *value
,
1572 struct lxc_conf
*lxc_conf
, void *data
)
1574 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1578 static int set_config_apparmor_profile(const char *key
, const char *value
,
1579 struct lxc_conf
*lxc_conf
, void *data
)
1582 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1584 return syserror_set(-EINVAL
, "Built without AppArmor support");
1588 static int set_config_apparmor_allow_incomplete(const char *key
,
1590 struct lxc_conf
*lxc_conf
,
1596 if (lxc_config_value_empty(value
)) {
1597 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1601 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
);
1605 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1606 return ret_errno(EINVAL
);
1610 return syserror_set(-EINVAL
, "Built without AppArmor support");
1614 static int set_config_apparmor_allow_nesting(const char *key
,
1616 struct lxc_conf
*lxc_conf
,
1622 if (lxc_config_value_empty(value
))
1623 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1625 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
);
1629 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1630 return ret_errno(EINVAL
);
1634 return syserror_set(-EINVAL
, "Built without AppArmor support");
1638 static int set_config_apparmor_raw(const char *key
,
1640 struct lxc_conf
*lxc_conf
,
1644 __do_free
char *elem
= NULL
;
1645 __do_free
struct lxc_list
*list
= NULL
;
1647 if (lxc_config_value_empty(value
))
1648 return lxc_clear_apparmor_raw(lxc_conf
);
1650 list
= lxc_list_new();
1652 return ret_errno(ENOMEM
);
1654 elem
= strdup(value
);
1656 return ret_errno(ENOMEM
);
1658 list
->elem
= move_ptr(elem
);
1659 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, move_ptr(list
));
1663 return syserror_set(-EINVAL
, "Built without AppArmor support");
1667 static int set_config_selinux_context(const char *key
, const char *value
,
1668 struct lxc_conf
*lxc_conf
, void *data
)
1671 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1673 return syserror_set(-EINVAL
, "Built without SELinux support");
1677 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1678 struct lxc_conf
*lxc_conf
, void *data
)
1681 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1683 return syserror_set(-EINVAL
, "Built without SELinux support");
1687 static int set_config_keyring_session(const char *key
, const char *value
,
1688 struct lxc_conf
*lxc_conf
, void *data
)
1690 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1693 static int set_config_log_file(const char *key
, const char *value
,
1694 struct lxc_conf
*c
, void *data
)
1698 if (lxc_config_value_empty(value
)) {
1699 free_disarm(c
->logfile
);
1704 * Store these values in the lxc_conf, and then try to set for actual
1707 ret
= set_config_path_item(&c
->logfile
, value
);
1709 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1714 static int set_config_log_level(const char *key
, const char *value
,
1715 struct lxc_conf
*lxc_conf
, void *data
)
1719 if (lxc_config_value_empty(value
)) {
1720 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1724 if (value
[0] >= '0' && value
[0] <= '9') {
1727 ret
= lxc_safe_int(value
, &newlevel
);
1729 return ret_errno(EINVAL
);
1731 newlevel
= lxc_log_priority_to_int(value
);
1735 * Store these values in the lxc_conf, and then try to set for actual
1738 lxc_conf
->loglevel
= newlevel
;
1740 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1743 static int set_config_autodev(const char *key
, const char *value
,
1744 struct lxc_conf
*lxc_conf
, void *data
)
1748 if (lxc_config_value_empty(value
)) {
1749 lxc_conf
->autodev
= 0;
1753 ret
= lxc_safe_uint(value
, &lxc_conf
->autodev
);
1755 return ret_errno(EINVAL
);
1757 if (lxc_conf
->autodev
> 1)
1758 return ret_errno(EINVAL
);
1763 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1764 struct lxc_conf
*lxc_conf
, void *data
)
1766 if (lxc_config_value_empty(value
)) {
1767 lxc_conf
->autodevtmpfssize
= 500000;
1771 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1772 lxc_conf
->autodevtmpfssize
= 500000;
1777 static int set_config_signal_halt(const char *key
, const char *value
,
1778 struct lxc_conf
*lxc_conf
, void *data
)
1782 if (lxc_config_value_empty(value
)) {
1783 lxc_conf
->haltsignal
= 0;
1787 sig_n
= sig_parse(value
);
1789 return ret_errno(EINVAL
);
1791 lxc_conf
->haltsignal
= sig_n
;
1796 static int set_config_signal_reboot(const char *key
, const char *value
,
1797 struct lxc_conf
*lxc_conf
, void *data
)
1801 if (lxc_config_value_empty(value
)) {
1802 lxc_conf
->rebootsignal
= 0;
1806 sig_n
= sig_parse(value
);
1808 return ret_errno(EINVAL
);
1810 lxc_conf
->rebootsignal
= sig_n
;
1815 static int set_config_signal_stop(const char *key
, const char *value
,
1816 struct lxc_conf
*lxc_conf
, void *data
)
1820 if (lxc_config_value_empty(value
)) {
1821 lxc_conf
->stopsignal
= 0;
1825 sig_n
= sig_parse(value
);
1827 return ret_errno(EINVAL
);
1829 lxc_conf
->stopsignal
= sig_n
;
1834 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1835 struct lxc_conf
*lxc_conf
, int version
)
1837 call_cleaner(free_lxc_cgroup
) struct lxc_cgroup
*new_cgroup
= NULL
;
1838 const char *subkey
, *token
;
1841 if (lxc_config_value_empty(value
))
1842 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1844 if (version
== CGROUP2_SUPER_MAGIC
) {
1845 token
= "lxc.cgroup2.";
1847 } else if (version
== CGROUP_SUPER_MAGIC
) {
1848 token
= "lxc.cgroup.";
1851 return ret_errno(EINVAL
);
1854 if (!strnequal(key
, token
, token_len
))
1855 return ret_errno(EINVAL
);
1857 subkey
= key
+ token_len
;
1858 if (*subkey
== '\0')
1859 return ret_errno(EINVAL
);
1861 new_cgroup
= zalloc(sizeof(*new_cgroup
));
1863 return ret_errno(ENOMEM
);
1865 new_cgroup
->subsystem
= strdup(subkey
);
1866 if (!new_cgroup
->subsystem
)
1867 return ret_errno(ENOMEM
);
1869 new_cgroup
->value
= strdup(value
);
1870 if (!new_cgroup
->value
)
1871 return ret_errno(ENOMEM
);
1873 new_cgroup
->version
= version
;
1875 if (version
== CGROUP2_SUPER_MAGIC
)
1876 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup2
);
1878 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup
);
1879 move_ptr(new_cgroup
);
1884 static int set_config_cgroup_controller(const char *key
, const char *value
,
1885 struct lxc_conf
*lxc_conf
, void *data
)
1887 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1888 CGROUP_SUPER_MAGIC
);
1891 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1892 struct lxc_conf
*lxc_conf
, void *data
)
1894 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1895 CGROUP2_SUPER_MAGIC
);
1898 static int set_config_cgroup_dir(const char *key
, const char *value
,
1899 struct lxc_conf
*lxc_conf
, void *data
)
1901 if (!strequal(key
, "lxc.cgroup.dir"))
1902 return ret_errno(EINVAL
);
1904 if (lxc_config_value_empty(value
))
1905 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1908 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1911 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1913 return set_config_path_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1916 static int set_config_cgroup_monitor_dir(const char *key
, const char *value
,
1917 struct lxc_conf
*lxc_conf
, void *data
)
1919 if (lxc_config_value_empty(value
))
1920 return clr_config_cgroup_monitor_dir(key
, lxc_conf
, NULL
);
1923 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1926 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1928 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_dir
, value
);
1931 static int set_config_cgroup_monitor_pivot_dir(const char *key
, const char *value
,
1932 struct lxc_conf
*lxc_conf
, void *data
)
1934 if (lxc_config_value_empty(value
))
1935 return clr_config_cgroup_monitor_pivot_dir(key
, lxc_conf
, NULL
);
1938 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1941 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1943 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_pivot_dir
, value
);
1946 static int set_config_cgroup_container_dir(const char *key
, const char *value
,
1947 struct lxc_conf
*lxc_conf
,
1950 if (lxc_config_value_empty(value
))
1951 return clr_config_cgroup_container_dir(key
, lxc_conf
, NULL
);
1954 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1957 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1959 return set_config_path_item(&lxc_conf
->cgroup_meta
.container_dir
, value
);
1962 static int set_config_cgroup_container_inner_dir(const char *key
,
1964 struct lxc_conf
*lxc_conf
,
1967 if (lxc_config_value_empty(value
))
1968 return clr_config_cgroup_container_inner_dir(key
, lxc_conf
, NULL
);
1971 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1973 if (strchr(value
, '/') || strequal(value
, ".") || strequal(value
, ".."))
1974 return log_error_errno(-EINVAL
, EINVAL
, "lxc.cgroup.dir.container.inner must be a single directory name");
1976 return set_config_string_item(&lxc_conf
->cgroup_meta
.namespace_dir
, value
);
1979 static int set_config_cgroup_relative(const char *key
, const char *value
,
1980 struct lxc_conf
*lxc_conf
, void *data
)
1982 unsigned int converted
;
1985 if (lxc_config_value_empty(value
))
1986 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1988 ret
= lxc_safe_uint(value
, &converted
);
1992 if (converted
== 1) {
1993 lxc_conf
->cgroup_meta
.relative
= true;
1997 if (converted
== 0) {
1998 lxc_conf
->cgroup_meta
.relative
= false;
2002 return ret_errno(EINVAL
);
2005 static bool parse_limit_value(const char **value
, rlim_t
*res
)
2007 char *endptr
= NULL
;
2009 if (strnequal(*value
, "unlimited", STRLITERALLEN("unlimited"))) {
2010 *res
= RLIM_INFINITY
;
2011 *value
+= STRLITERALLEN("unlimited");
2016 *res
= strtoull(*value
, &endptr
, 10);
2017 if (errno
|| !endptr
)
2025 static int set_config_prlimit(const char *key
, const char *value
,
2026 struct lxc_conf
*lxc_conf
, void *data
)
2028 call_cleaner(free_lxc_limit
) struct lxc_limit
*new_lim
= NULL
;
2029 struct rlimit limit
;
2031 struct lxc_limit
*lim
;
2033 if (lxc_config_value_empty(value
))
2034 return lxc_clear_limits(lxc_conf
, key
);
2036 if (!strnequal(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
2037 return ret_errno(EINVAL
);
2039 key
+= STRLITERALLEN("lxc.prlimit.");
2041 /* soft limit comes first in the value */
2042 if (!parse_limit_value(&value
, &limit_value
))
2043 return ret_errno(EINVAL
);
2045 limit
.rlim_cur
= limit_value
;
2047 /* skip spaces and a colon */
2048 while (isspace(*value
))
2053 else if (*value
) /* any other character is an error here */
2054 return ret_errno(EINVAL
);
2056 while (isspace(*value
))
2059 /* optional hard limit */
2061 if (!parse_limit_value(&value
, &limit_value
))
2062 return ret_errno(EINVAL
);
2064 limit
.rlim_max
= limit_value
;
2066 /* check for trailing garbage */
2067 while (isspace(*value
))
2071 return ret_errno(EINVAL
);
2073 /* a single value sets both hard and soft limit */
2074 limit
.rlim_max
= limit
.rlim_cur
;
2077 /* find existing list element */
2078 list_for_each_entry(lim
, &lxc_conf
->limits
, head
) {
2079 if (!strequal(key
, lim
->resource
))
2086 new_lim
= zalloc(sizeof(*new_lim
));
2088 return ret_errno(ENOMEM
);
2090 new_lim
->resource
= strdup(key
);
2091 if (!new_lim
->resource
)
2092 return ret_errno(ENOMEM
);
2094 new_lim
->limit
= limit
;
2095 list_add_tail(&new_lim
->head
, &lxc_conf
->limits
);
2101 static int set_config_sysctl(const char *key
, const char *value
,
2102 struct lxc_conf
*lxc_conf
, void *data
)
2104 call_cleaner(free_lxc_sysctl
) struct lxc_sysctl
*sysctl_elem
= NULL
;
2105 struct lxc_sysctl
*sysctl
, *nsysctl
;
2107 if (lxc_config_value_empty(value
))
2108 return clr_config_sysctl(key
, lxc_conf
, NULL
);
2110 if (!strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
2111 return ret_errno(EINVAL
);
2113 key
+= STRLITERALLEN("lxc.sysctl.");
2114 if (is_empty_string(key
))
2115 return ret_errno(EINVAL
);
2117 /* find existing list element */
2118 list_for_each_entry_safe(sysctl
, nsysctl
, &lxc_conf
->sysctls
, head
) {
2119 __do_free
char *replace_value
= NULL
;
2121 if (!strequal(key
, sysctl
->key
))
2124 replace_value
= strdup(value
);
2126 return ret_errno(EINVAL
);
2128 free(sysctl
->value
);
2129 sysctl
->value
= move_ptr(replace_value
);
2134 sysctl_elem
= zalloc(sizeof(*sysctl_elem
));
2136 return ret_errno(ENOMEM
);
2138 sysctl_elem
->key
= strdup(key
);
2139 if (!sysctl_elem
->key
)
2140 return ret_errno(ENOMEM
);
2142 sysctl_elem
->value
= strdup(value
);
2143 if (!sysctl_elem
->value
)
2144 return ret_errno(ENOMEM
);
2146 list_add_tail(&sysctl_elem
->head
, &lxc_conf
->sysctls
);
2147 move_ptr(sysctl_elem
);
2152 static int set_config_proc(const char *key
, const char *value
,
2153 struct lxc_conf
*lxc_conf
, void *data
)
2155 call_cleaner(free_lxc_proc
) struct lxc_proc
*new_proc
= NULL
;
2158 if (lxc_config_value_empty(value
))
2159 return clr_config_proc(key
, lxc_conf
, NULL
);
2161 if (!strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
2162 return ret_errno(EINVAL
);
2164 subkey
= key
+ STRLITERALLEN("lxc.proc.");
2165 if (*subkey
== '\0')
2166 return ret_errno(EINVAL
);
2168 new_proc
= zalloc(sizeof(*new_proc
));
2170 return ret_errno(ENOMEM
);
2172 new_proc
->filename
= strdup(subkey
);
2173 if (!new_proc
->filename
)
2174 return ret_errno(ENOMEM
);
2176 new_proc
->value
= strdup(value
);
2177 if (!new_proc
->value
)
2178 return ret_errno(ENOMEM
);
2180 list_add_tail(&new_proc
->head
, &lxc_conf
->procs
);
2186 static int set_config_idmaps(const char *key
, const char *value
,
2187 struct lxc_conf
*lxc_conf
, void *data
)
2189 __do_free
struct id_map
*idmap
= NULL
;
2190 unsigned long hostid
, nsid
, range
;
2194 if (lxc_config_value_empty(value
))
2195 return lxc_clear_idmaps(lxc_conf
);
2197 idmap
= zalloc(sizeof(*idmap
));
2199 return ret_errno(ENOMEM
);
2201 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2203 return log_error_errno(-EINVAL
, EINVAL
, "Failed to parse id mappings");
2205 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2207 idmap
->idtype
= ID_TYPE_UID
;
2208 else if (type
== 'g')
2209 idmap
->idtype
= ID_TYPE_GID
;
2211 return ret_errno(EINVAL
);
2213 idmap
->hostid
= hostid
;
2215 idmap
->range
= range
;
2216 list_add_tail(&idmap
->head
, &lxc_conf
->id_map
);
2218 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2219 if (idmap
->nsid
== 0)
2220 lxc_conf
->root_nsuid_map
= idmap
;
2222 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2223 if (idmap
->nsid
== 0)
2224 lxc_conf
->root_nsgid_map
= idmap
;
2231 static int set_config_mount_fstab(const char *key
, const char *value
,
2232 struct lxc_conf
*lxc_conf
, void *data
)
2234 if (lxc_config_value_empty(value
)) {
2235 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2236 return ret_errno(EINVAL
);
2239 return set_config_path_item(&lxc_conf
->fstab
, value
);
2242 static int set_config_mount_auto(const char *key
, const char *value
,
2243 struct lxc_conf
*lxc_conf
, void *data
)
2245 __do_free
char *autos
= NULL
;
2252 } allowed_auto_mounts
[] = {
2253 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2254 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2255 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2256 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2257 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2258 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2259 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2260 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2261 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2262 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2263 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2264 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2265 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2266 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2267 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2268 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2269 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2270 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2271 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2272 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2273 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2274 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2275 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2276 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2278 * For adding anything that is just a single on/off, but has no
2279 * options: keep mask and flag identical and just define the
2280 * enum value as an unused bit so far
2285 if (lxc_config_value_empty(value
)) {
2286 lxc_conf
->auto_mounts
= 0;
2290 autos
= strdup(value
);
2292 return ret_errno(ENOMEM
);
2294 lxc_iterate_parts(token
, autos
, " \t") {
2295 bool is_shmounts
= false;
2297 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2298 if (strequal(allowed_auto_mounts
[i
].token
, token
))
2301 if (strequal("shmounts:", allowed_auto_mounts
[i
].token
) &&
2302 strnequal("shmounts:", token
, STRLITERALLEN("shmounts:"))) {
2308 if (!allowed_auto_mounts
[i
].token
)
2309 return log_error_errno(-EINVAL
, EINVAL
, "Invalid filesystem to automount \"%s\"", token
);
2311 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2312 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2315 __do_free
char *container_path
= NULL
, *host_path
= NULL
;
2318 val
= token
+ STRLITERALLEN("shmounts:");
2320 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2322 host_path
= strdup(val
);
2324 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2326 val
= strchr(host_path
, ':');
2327 if (!val
|| *(val
+ 1) == '\0')
2328 val
= "/dev/.lxc-mounts";
2332 container_path
= strdup(val
);
2334 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts container path");
2336 free_disarm(lxc_conf
->shmount
.path_host
);
2337 lxc_conf
->shmount
.path_host
= move_ptr(host_path
);
2339 free_disarm(lxc_conf
->shmount
.path_cont
);
2340 lxc_conf
->shmount
.path_cont
= move_ptr(container_path
);
2347 static int set_config_mount(const char *key
, const char *value
,
2348 struct lxc_conf
*lxc_conf
, void *data
)
2350 __do_free
char *mntelem
= NULL
;
2351 __do_free
struct string_entry
*entry
= NULL
;
2353 if (lxc_config_value_empty(value
))
2354 return lxc_clear_mount_entries(lxc_conf
);
2356 entry
= zalloc(sizeof(struct string_entry
));
2358 return ret_errno(ENOMEM
);
2360 mntelem
= strdup(value
);
2362 return ret_errno(ENOMEM
);
2364 entry
->val
= move_ptr(mntelem
);
2365 list_add_tail(&entry
->head
, &lxc_conf
->mount_entries
);
2371 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2372 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2375 static int add_cap_entry(struct lxc_conf
*conf
, char *caps
, bool keep
)
2380 * In case several capability keep is specified in a single line split
2381 * these caps in a single element for the list.
2383 lxc_iterate_parts(token
, caps
, " \t") {
2384 __do_free
struct cap_entry
*new_cap
= NULL
;
2387 if (strequal(token
, "none")) {
2389 return syserror_set(-EINVAL
, "The \"none\" keyword is only valid when keeping caps");
2391 lxc_clear_config_caps(conf
);
2395 cap
= parse_cap(token
);
2398 return syserror_set(-EINVAL
, "Invalid capability specified");
2400 INFO("Ignoring unknown capability \"%s\"", token
);
2404 new_cap
= zalloc(sizeof(struct cap_entry
));
2406 return ret_errno(ENOMEM
);
2408 new_cap
->cap_name
= strdup(token
);
2409 if (!new_cap
->cap_name
)
2410 return ret_errno(ENOMEM
);
2413 list_add_tail(&new_cap
->head
, &conf
->caps
.list
);
2420 static int set_config_cap_keep(const char *key
, const char *value
,
2421 struct lxc_conf
*lxc_conf
, void *data
)
2423 __do_free
char *caps
= NULL
;
2426 if (lxc_config_value_empty(value
))
2427 return lxc_clear_config_caps(lxc_conf
);
2429 caps
= strdup(value
);
2431 return ret_errno(ENOMEM
);
2433 if (!lxc_conf
->caps
.keep
&& !list_empty(&lxc_conf
->caps
.list
))
2434 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2436 ret
= add_cap_entry(lxc_conf
, caps
, true);
2440 lxc_conf
->caps
.keep
= true;
2444 static int set_config_cap_drop(const char *key
, const char *value
,
2445 struct lxc_conf
*lxc_conf
, void *data
)
2447 __do_free
char *caps
= NULL
;
2450 if (lxc_config_value_empty(value
))
2451 return lxc_clear_config_caps(lxc_conf
);
2453 if (lxc_conf
->caps
.keep
)
2454 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2456 caps
= strdup(value
);
2458 return ret_errno(ENOMEM
);
2460 ret
= add_cap_entry(lxc_conf
, caps
, false);
2464 lxc_conf
->caps
.keep
= false;
2468 static int set_config_console_path(const char *key
, const char *value
,
2469 struct lxc_conf
*lxc_conf
, void *data
)
2471 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2474 static int set_config_console_rotate(const char *key
, const char *value
,
2475 struct lxc_conf
*lxc_conf
, void *data
)
2479 if (lxc_config_value_empty(value
)) {
2480 lxc_conf
->console
.log_rotate
= 0;
2484 ret
= lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
);
2486 return ret_errno(EINVAL
);
2488 if (lxc_conf
->console
.log_rotate
> 1)
2489 return log_error_errno(-EINVAL
, EINVAL
, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
2494 static int set_config_console_logfile(const char *key
, const char *value
,
2495 struct lxc_conf
*lxc_conf
, void *data
)
2497 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2500 static int set_config_console_buffer_size(const char *key
, const char *value
,
2501 struct lxc_conf
*lxc_conf
, void *data
)
2505 uint64_t buffer_size
, pgsz
;
2507 if (lxc_config_value_empty(value
)) {
2508 lxc_conf
->console
.buffer_size
= 0;
2512 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2513 if (strequal(value
, "auto")) {
2514 lxc_conf
->console
.buffer_size
= 1 << 17;
2518 ret
= parse_byte_size_string(value
, &size
);
2523 return ret_errno(EINVAL
);
2525 /* must be at least a page size */
2526 pgsz
= lxc_getpagesize();
2527 if ((uint64_t)size
< pgsz
) {
2528 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2533 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2534 if (buffer_size
== 0)
2535 return ret_errno(EINVAL
);
2537 if (buffer_size
!= size
)
2538 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", buffer_size
);
2540 lxc_conf
->console
.buffer_size
= buffer_size
;
2545 static int set_config_console_size(const char *key
, const char *value
,
2546 struct lxc_conf
*lxc_conf
, void *data
)
2550 uint64_t log_size
, pgsz
;
2552 if (lxc_config_value_empty(value
)) {
2553 lxc_conf
->console
.log_size
= 0;
2557 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2558 if (strequal(value
, "auto")) {
2559 lxc_conf
->console
.log_size
= 1 << 17;
2563 ret
= parse_byte_size_string(value
, &size
);
2565 return ret_errno(EINVAL
);
2568 return ret_errno(EINVAL
);
2570 /* must be at least a page size */
2571 pgsz
= lxc_getpagesize();
2572 if ((uint64_t)size
< pgsz
) {
2573 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2578 log_size
= lxc_find_next_power2((uint64_t)size
);
2580 return ret_errno(EINVAL
);
2582 if (log_size
!= size
)
2583 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", log_size
);
2585 lxc_conf
->console
.log_size
= log_size
;
2591 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2592 * file, we expand it in the unexpanded_config, so that after a save_config we
2593 * store the hwaddr for re-use.
2594 * This is only called when reading the config file, not when executing a
2596 * 'x' and 'X' are substituted in-place.
2598 static void update_hwaddr(const char *line
)
2602 line
+= lxc_char_left_gc(line
, strlen(line
));
2606 if (!lxc_config_net_is_hwaddr(line
))
2609 /* Let config_net_hwaddr raise the error. */
2610 p
= strchr(line
, '=');
2621 rand_complete_hwaddr(p
);
2624 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2627 size_t len
= conf
->unexpanded_len
;
2629 update_hwaddr(line
);
2631 linelen
= strlen(line
);
2632 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2635 tmp
= realloc(conf
->unexpanded_config
, conf
->unexpanded_alloced
+ 1024);
2637 return ret_errno(EINVAL
);
2639 if (!conf
->unexpanded_config
)
2642 conf
->unexpanded_config
= tmp
;
2643 conf
->unexpanded_alloced
+= 1024;
2646 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2647 conf
->unexpanded_len
+= linelen
;
2648 if (line
[linelen
- 1] != '\n')
2649 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2650 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2655 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2657 __do_closedir
DIR *dir
= NULL
;
2658 struct dirent
*direntp
;
2661 dir
= opendir(dirp
);
2663 return errno
== ENOENT
? 0 : -errno
;
2665 while ((direntp
= readdir(dir
))) {
2667 char path
[PATH_MAX
];
2669 fnam
= direntp
->d_name
;
2670 if (strequal(fnam
, "."))
2673 if (strequal(fnam
, ".."))
2677 if (len
< 6 || !strnequal(fnam
+ len
- 5, ".conf", 5))
2680 len
= strnprintf(path
, sizeof(path
), "%s/%s", dirp
, fnam
);
2682 return ret_errno(EIO
);
2684 ret
= lxc_config_read(path
, lxc_conf
, true);
2692 static int set_config_includefiles(const char *key
, const char *value
,
2693 struct lxc_conf
*lxc_conf
, void *data
)
2695 if (lxc_config_value_empty(value
)) {
2696 clr_config_includefiles(key
, lxc_conf
, NULL
);
2700 if (value
[strlen(value
)-1] == '/' || is_dir(value
))
2701 return do_includedir(value
, lxc_conf
);
2703 return lxc_config_read(value
, lxc_conf
, true);
2706 static int set_config_rootfs_path(const char *key
, const char *value
,
2707 struct lxc_conf
*lxc_conf
, void *data
)
2709 __do_free
char *dup
= NULL
;
2712 const char *container_path
;
2714 if (lxc_config_value_empty(value
)) {
2715 free(lxc_conf
->rootfs
.path
);
2716 lxc_conf
->rootfs
.path
= NULL
;
2720 dup
= strdup(value
);
2722 return ret_errno(ENOMEM
);
2724 /* Split <storage type>:<container path> into <storage type> and
2725 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2726 * "rootfs.path" to <container path>.
2728 tmp
= strchr(dup
, ':');
2732 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2734 return ret_errno(ENOMEM
);
2737 container_path
= tmp
;
2739 container_path
= value
;
2742 return set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2745 static int set_config_rootfs_managed(const char *key
, const char *value
,
2746 struct lxc_conf
*lxc_conf
, void *data
)
2748 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2751 static int set_config_rootfs_mount(const char *key
, const char *value
,
2752 struct lxc_conf
*lxc_conf
, void *data
)
2754 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2757 static int set_config_rootfs_options(const char *key
, const char *value
,
2758 struct lxc_conf
*lxc_conf
, void *data
)
2760 __do_free
char *dup
= NULL
, *raw_options
= NULL
;
2761 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2762 struct lxc_mount_options
*mnt_opts
= &rootfs
->mnt_opts
;
2765 clr_config_rootfs_options(key
, lxc_conf
, data
);
2766 if (lxc_config_value_empty(value
))
2769 ret
= set_config_string_item(&raw_options
, value
);
2771 return ret_errno(ENOMEM
);
2773 dup
= strdup(value
);
2777 ret
= parse_lxc_mount_attrs(mnt_opts
, dup
);
2781 /* Make sure we're only valid LXC specific mount options. */
2782 if (mnt_opts
->create_dir
|| mnt_opts
->create_file
||
2783 mnt_opts
->optional
|| mnt_opts
->relative
)
2784 return syserror_set(-EINVAL
, "Invalid LXC specific mount option for rootfs mount");
2786 ret
= parse_mount_attrs(mnt_opts
, dup
);
2788 return ret_errno(EINVAL
);
2790 rootfs
->mnt_opts
.raw_options
= move_ptr(raw_options
);
2794 static int set_config_uts_name(const char *key
, const char *value
,
2795 struct lxc_conf
*lxc_conf
, void *data
)
2797 __do_free
struct utsname
*utsname
= NULL
;
2799 if (lxc_config_value_empty(value
)) {
2800 clr_config_uts_name(key
, lxc_conf
, NULL
);
2804 utsname
= zalloc(sizeof(*utsname
));
2806 return ret_errno(ENOMEM
);
2808 if (strlen(value
) >= sizeof(utsname
->nodename
))
2809 return ret_errno(EINVAL
);
2811 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2812 free(lxc_conf
->utsname
);
2813 lxc_conf
->utsname
= move_ptr(utsname
);
2818 static int set_config_namespace_clone(const char *key
, const char *value
,
2819 struct lxc_conf
*lxc_conf
, void *data
)
2821 __do_free
char *ns
= NULL
;
2825 if (lxc_config_value_empty(value
))
2826 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2828 if (lxc_conf
->ns_keep
!= 0)
2829 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2833 return ret_errno(ENOMEM
);
2835 lxc_iterate_parts(token
, ns
, " \t") {
2836 token
+= lxc_char_left_gc(token
, strlen(token
));
2837 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2838 cloneflag
= lxc_namespace_2_cloneflag(token
);
2840 return ret_errno(EINVAL
);
2841 lxc_conf
->ns_clone
|= cloneflag
;
2847 static int set_config_namespace_keep(const char *key
, const char *value
,
2848 struct lxc_conf
*lxc_conf
, void *data
)
2850 __do_free
char *ns
= NULL
;
2854 if (lxc_config_value_empty(value
))
2855 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2857 if (lxc_conf
->ns_clone
!= 0)
2858 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2862 return ret_errno(ENOMEM
);
2864 lxc_iterate_parts(token
, ns
, " \t") {
2865 token
+= lxc_char_left_gc(token
, strlen(token
));
2866 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2867 cloneflag
= lxc_namespace_2_cloneflag(token
);
2869 return ret_errno(EINVAL
);
2870 lxc_conf
->ns_keep
|= cloneflag
;
2876 static int set_config_time_offset_boot(const char *key
, const char *value
,
2877 struct lxc_conf
*lxc_conf
, void *data
)
2882 char buf
[STRLITERALLEN("ms") + 1];
2884 if (lxc_config_value_empty(value
))
2885 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2887 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2891 unit
= lxc_trim_whitespace_in_place(buf
);
2892 if (strequal(unit
, "h")) {
2893 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2894 return ret_errno(EOVERFLOW
);
2895 } else if (strequal(unit
, "m")) {
2896 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2897 return ret_errno(EOVERFLOW
);
2898 } else if (strequal(unit
, "s")) {
2899 lxc_conf
->timens
.s_boot
= offset
;
2900 } else if (strequal(unit
, "ms")) {
2901 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2902 return ret_errno(EOVERFLOW
);
2903 } else if (strequal(unit
, "us")) {
2904 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2905 return ret_errno(EOVERFLOW
);
2906 } else if (strequal(unit
, "ns")) {
2907 lxc_conf
->timens
.ns_boot
= offset
;
2909 return ret_errno(EINVAL
);
2915 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2916 struct lxc_conf
*lxc_conf
, void *data
)
2921 char buf
[STRLITERALLEN("ms") + 1];
2923 if (lxc_config_value_empty(value
))
2924 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2926 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2930 unit
= lxc_trim_whitespace_in_place(buf
);
2931 if (strequal(unit
, "h")) {
2932 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2933 return ret_errno(EOVERFLOW
);
2934 } else if (strequal(unit
, "m")) {
2935 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2936 return ret_errno(EOVERFLOW
);
2937 } else if (strequal(unit
, "s")) {
2938 lxc_conf
->timens
.s_monotonic
= offset
;
2939 } else if (strequal(unit
, "ms")) {
2940 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2941 return ret_errno(EOVERFLOW
);
2942 } else if (strequal(unit
, "us")) {
2943 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2944 return ret_errno(EOVERFLOW
);
2945 } else if (strequal(unit
, "ns")) {
2946 lxc_conf
->timens
.ns_monotonic
= offset
;
2948 return ret_errno(EINVAL
);
2954 static int set_config_namespace_share(const char *key
, const char *value
,
2955 struct lxc_conf
*lxc_conf
, void *data
)
2958 const char *namespace;
2960 if (lxc_config_value_empty(value
))
2961 return clr_config_namespace_share(key
, lxc_conf
, data
);
2963 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2964 if (is_empty_string(namespace))
2965 return ret_errno(EINVAL
);
2967 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2971 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2974 struct parse_line_conf
{
2975 struct lxc_conf
*conf
;
2979 static int parse_line(char *buffer
, void *data
)
2981 __do_free
char *linep
= NULL
;
2982 char *dot
, *key
, *line
, *value
;
2984 struct lxc_config_t
*config
;
2987 struct parse_line_conf
*plc
= data
;
2990 return syserror_set(-EINVAL
, "Missing config");
2992 /* If there are newlines in the config file we should keep them. */
2993 empty_line
= lxc_is_line_empty(dup
);
2997 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2998 * modified the original string on the stack by replacing '=' by '\0'
3001 linep
= line
= strdup(dup
);
3003 return ret_errno(ENOMEM
);
3005 if (!plc
->from_include
) {
3006 ret
= append_unexp_config_line(line
, plc
->conf
);
3014 line
+= lxc_char_left_gc(line
, strlen(line
));
3016 /* ignore comments */
3020 /* martian option - don't add it to the config itself */
3021 if (!strnequal(line
, "lxc.", 4))
3024 dot
= strchr(line
, '=');
3026 return log_error_errno(-EINVAL
, EINVAL
, "Invalid configuration line: %s", line
);
3032 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3034 value
+= lxc_char_left_gc(value
, strlen(value
));
3035 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3037 if (*value
== '\'' || *value
== '\"') {
3040 len
= strlen(value
);
3041 if (len
> 1 && value
[len
- 1] == *value
) {
3042 value
[len
- 1] = '\0';
3047 config
= lxc_get_config(key
);
3048 return config
->set(key
, value
, plc
->conf
, NULL
);
3051 static struct new_config_item
*parse_new_conf_line(char *buffer
)
3053 __do_free
char *k
= NULL
, *linep
= NULL
, *v
= NULL
;
3054 __do_free
struct new_config_item
*new = NULL
;
3056 char *dot
, *key
, *line
, *value
;
3058 if (is_empty_string(buffer
))
3059 return log_error_errno(NULL
, EINVAL
, "Empty configuration line");
3061 linep
= line
= strdup(dup
);
3065 line
+= lxc_char_left_gc(line
, strlen(line
));
3067 /* martian option - don't add it to the config itself */
3068 if (!strnequal(line
, "lxc.", 4))
3071 dot
= strchr(line
, '=');
3073 return log_error_errno(NULL
, EINVAL
, "Invalid configuration line: %s", line
);
3079 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3081 value
+= lxc_char_left_gc(value
, strlen(value
));
3082 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3084 if (*value
== '\'' || *value
== '\"') {
3087 len
= strlen(value
);
3088 if (len
> 1 && value
[len
- 1] == *value
) {
3089 value
[len
- 1] = '\0';
3094 new = zalloc(sizeof(struct new_config_item
));
3106 new->key
= move_ptr(k
);
3107 new->val
= move_ptr(v
);
3108 return move_ptr(new);
3111 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
3113 struct parse_line_conf plc
;
3116 return syserror_set(-EINVAL
, "Missing config");
3119 plc
.from_include
= from_include
;
3121 /* Catch only the top level config file name in the structure. */
3123 conf
->rcfile
= strdup(file
);
3125 return lxc_file_for_each_line_mmap(file
, parse_line
, &plc
);
3128 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3130 __do_free
struct lxc_list
*dent
= NULL
;
3132 dent
= lxc_list_new();
3134 return ret_errno(ENOMEM
);
3136 dent
->elem
= parse_new_conf_line(arg
);
3138 return ret_errno(ENOMEM
);
3140 lxc_list_add_tail(defines
, move_ptr(dent
));
3145 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3147 struct lxc_list
*it
;
3150 lxc_list_for_each(it
, defines
) {
3151 struct new_config_item
*new_item
= it
->elem
;
3152 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3157 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3158 lxc_config_define_free(defines
);
3159 #endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
3164 void lxc_config_define_free(struct lxc_list
*defines
)
3166 struct lxc_list
*it
, *next
;
3168 lxc_list_for_each_safe(it
, defines
, next
) {
3169 struct new_config_item
*new_item
= it
->elem
;
3170 free(new_item
->key
);
3171 free(new_item
->val
);
3178 int lxc_config_parse_arch(const char *arch
, signed long *persona
)
3180 static struct per_name
{
3184 { "arm", PER_LINUX32
},
3185 { "armel", PER_LINUX32
},
3186 { "armhf", PER_LINUX32
},
3187 { "armv7l", PER_LINUX32
},
3188 { "athlon", PER_LINUX32
},
3189 { "i386", PER_LINUX32
},
3190 { "i486", PER_LINUX32
},
3191 { "i586", PER_LINUX32
},
3192 { "i686", PER_LINUX32
},
3193 { "linux32", PER_LINUX32
},
3194 { "mips", PER_LINUX32
},
3195 { "mipsel", PER_LINUX32
},
3196 { "ppc", PER_LINUX32
},
3197 { "powerpc", PER_LINUX32
},
3198 { "x86", PER_LINUX32
},
3199 { "aarch64", PER_LINUX
},
3200 { "amd64", PER_LINUX
},
3201 { "arm64", PER_LINUX
},
3202 { "linux64", PER_LINUX
},
3203 { "mips64", PER_LINUX
},
3204 { "mips64el", PER_LINUX
},
3205 { "ppc64", PER_LINUX
},
3206 { "ppc64el", PER_LINUX
},
3207 { "ppc64le", PER_LINUX
},
3208 { "powerpc64", PER_LINUX
},
3209 { "s390x", PER_LINUX
},
3210 { "x86_64", PER_LINUX
},
3213 for (int i
= 0; i
< ARRAY_SIZE(pername
); i
++) {
3214 if (!strequal(pername
[i
].name
, arch
))
3217 *persona
= pername
[i
].per
;
3221 return ret_errno(EINVAL
);
3224 int lxc_fill_elevated_privileges(char *flaglist
, unsigned int *flags
)
3226 unsigned int flags_tmp
= 0;
3232 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3233 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3234 { "LSM", LXC_ATTACH_LSM_EXEC
},
3240 * For the sake of backward compatibility, keep all privileges
3241 * if no specific privileges are specified.
3243 for (unsigned int i
= 0; all_privs
[i
].token
; i
++)
3244 flags_tmp
|= all_privs
[i
].flag
;
3250 lxc_iterate_parts(token
, flaglist
, "|") {
3251 bool valid_token
= false;
3253 for (unsigned int i
= 0; all_privs
[i
].token
; i
++) {
3254 if (!strequal(all_privs
[i
].token
, token
))
3258 flags_tmp
|= all_privs
[i
].flag
;
3262 return syserror_set(-EINVAL
, "Invalid elevated privilege \"%s\" requested", token
);
3269 /* Write out a configuration file. */
3270 int write_config(int fd
, const struct lxc_conf
*conf
)
3273 size_t len
= conf
->unexpanded_len
;
3278 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3280 return log_error_errno(-errno
, errno
, "Failed to write configuration file");
3285 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3288 __do_free
char *tmp
= NULL
;
3292 len
= strlen(key
) + strlen(v
) + 4;
3293 tmp
= must_realloc(NULL
, len
);
3295 if (lxc_config_value_empty(v
))
3296 ret
= strnprintf(tmp
, len
, "%s =", key
);
3298 ret
= strnprintf(tmp
, len
, "%s = %s", key
, v
);
3302 /* Save the line verbatim into unexpanded_conf */
3303 if (append_unexp_config_line(tmp
, conf
))
3309 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3313 char *lstart
= conf
->unexpanded_config
;
3315 if (!conf
->unexpanded_config
)
3319 lend
= strchr(lstart
, '\n');
3323 lend
= lstart
+ strlen(lstart
);
3327 if (!strnequal(lstart
, key
, strlen(key
))) {
3333 v
= lstart
[strlen(key
)];
3334 if (!isspace(v
) && v
!= '=') {
3340 conf
->unexpanded_len
-= (lend
- lstart
);
3342 if (*lend
== '\0') {
3347 memmove(lstart
, lend
, strlen(lend
) + 1);
3351 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3352 const char *newpath
, const char *oldname
,
3353 const char *newname
, const char *ovldir
)
3355 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3356 char *lstart
= conf
->unexpanded_config
;
3357 const char *key
= "lxc.mount.entry";
3360 size_t newdirlen
, olddirlen
;
3362 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3363 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3364 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
, oldname
);
3368 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3369 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3370 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
, newname
);
3374 if (!conf
->unexpanded_config
)
3378 lend
= strchr(lstart
, '\n');
3380 lend
= lstart
+ strlen(lstart
);
3384 if (!strnequal(lstart
, key
, strlen(key
)))
3387 p
= strchr(lstart
+ strlen(key
), '=');
3398 /* Whenever a lxc.mount.entry entry is found in a line we check
3399 * if the substring "overlay" is present before doing any
3400 * further work. We check for "overlay" because substrings need
3401 * to have at least one space before them in a valid overlay
3402 * lxc.mount.entry (/A B overlay). When the space before is
3403 * missing it is very likely that these substrings are part of a
3404 * path or something else. (Checking q >= lend ensures that we
3405 * only count matches in the current line.) */
3406 q
= strstr(p
, " overlay");
3407 if (!q
|| q
>= lend
)
3410 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3413 /* replace the olddir with newdir */
3414 if (olddirlen
>= newdirlen
) {
3415 size_t diff
= olddirlen
- newdirlen
;
3416 memcpy(q
, newdir
, newdirlen
);
3418 if (olddirlen
!= newdirlen
) {
3419 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3420 strlen(q
) - newdirlen
- diff
+ 1);
3422 conf
->unexpanded_len
-= diff
;
3426 size_t diff
= newdirlen
- olddirlen
;
3427 size_t oldlen
= conf
->unexpanded_len
;
3428 size_t newlen
= oldlen
+ diff
;
3429 size_t poffset
= q
- conf
->unexpanded_config
;
3431 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3435 conf
->unexpanded_len
= newlen
;
3436 conf
->unexpanded_alloced
= newlen
+ 1;
3437 new[newlen
- 1] = '\0';
3438 lend
= new + (lend
- conf
->unexpanded_config
);
3440 /* Move over the remainder to make room for the newdir.
3442 memmove(new + poffset
+ newdirlen
,
3443 new + poffset
+ olddirlen
,
3444 oldlen
- poffset
- olddirlen
+ 1);
3445 conf
->unexpanded_config
= new;
3447 memcpy(new + poffset
, newdir
, newdirlen
);
3458 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3459 const char *newpath
, const char *oldname
,
3460 const char *newname
)
3462 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3463 char *lstart
= conf
->unexpanded_config
;
3464 const char *key
= "lxc.hook";
3467 size_t newdirlen
, olddirlen
;
3469 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3470 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3471 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3475 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3476 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3477 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3481 if (!conf
->unexpanded_config
)
3485 lend
= strchr(lstart
, '\n');
3487 lend
= lstart
+ strlen(lstart
);
3491 if (!strnequal(lstart
, key
, strlen(key
)))
3494 p
= strchr(lstart
+ strlen(key
), '=');
3505 if (!strnequal(p
, olddir
, strlen(olddir
)))
3508 /* replace the olddir with newdir */
3509 if (olddirlen
>= newdirlen
) {
3510 size_t diff
= olddirlen
- newdirlen
;
3511 memcpy(p
, newdir
, newdirlen
);
3513 if (olddirlen
!= newdirlen
) {
3514 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3515 strlen(p
) - newdirlen
- diff
+ 1);
3517 conf
->unexpanded_len
-= diff
;
3521 size_t diff
= newdirlen
- olddirlen
;
3522 size_t oldlen
= conf
->unexpanded_len
;
3523 size_t newlen
= oldlen
+ diff
;
3524 size_t poffset
= p
- conf
->unexpanded_config
;
3526 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3530 conf
->unexpanded_len
= newlen
;
3531 conf
->unexpanded_alloced
= newlen
+ 1;
3532 new[newlen
- 1] = '\0';
3533 lend
= new + (lend
- conf
->unexpanded_config
);
3535 /* Move over the remainder to make room for the newdir.
3537 memmove(new + poffset
+ newdirlen
,
3538 new + poffset
+ olddirlen
,
3539 oldlen
- poffset
- olddirlen
+ 1);
3540 conf
->unexpanded_config
= new;
3542 memcpy(new + poffset
, newdir
, newdirlen
);
3556 ERROR("Error writing to new config"); \
3561 /* This is called only from clone. We wish to update all hwaddrs in the
3562 * unexpanded config file. We can't/don't want to update any which come from
3563 * lxc.includes (there shouldn't be any).
3564 * We can't just walk the c->lxc-conf->network list because that includes netifs
3565 * from the include files. So we update the ones which we find in the unexp
3566 * config file, then find the original macaddr in the conf->network, and update
3567 * that to the same value.
3569 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3571 char *lend
, *p
, *p2
;
3572 char *lstart
= conf
->unexpanded_config
;
3574 if (!conf
->unexpanded_config
)
3578 char newhwaddr
[18], oldhwaddr
[17];
3579 struct lxc_netdev
*netdev
;
3581 lend
= strchr(lstart
, '\n');
3583 lend
= lstart
+ strlen(lstart
);
3587 if (!lxc_config_net_is_hwaddr(lstart
)) {
3592 p
= strchr(lstart
, '=');
3605 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3608 if ((p2
- p
) != 17) {
3609 WARN("Bad hwaddr entry");
3614 memcpy(oldhwaddr
, p
, 17);
3616 if (!new_hwaddr(newhwaddr
))
3619 memcpy(p
, newhwaddr
, 17);
3620 list_for_each_entry(netdev
, &conf
->netdevs
, head
) {
3621 if (netdev
->hwaddr
&& memcmp(oldhwaddr
, netdev
->hwaddr
, 17) == 0)
3622 memcpy(netdev
->hwaddr
, newhwaddr
, 17);
3631 static int set_config_ephemeral(const char *key
, const char *value
,
3632 struct lxc_conf
*lxc_conf
, void *data
)
3636 if (lxc_config_value_empty(value
)) {
3637 lxc_conf
->ephemeral
= 0;
3641 ret
= lxc_safe_uint(value
, &lxc_conf
->ephemeral
);
3645 if (lxc_conf
->ephemeral
> 1)
3646 return ret_errno(EINVAL
);
3651 static int set_config_log_syslog(const char *key
, const char *value
,
3652 struct lxc_conf
*lxc_conf
, void *data
)
3656 if (lxc_conf
->syslog
)
3657 free_disarm(lxc_conf
->syslog
);
3659 if (lxc_config_value_empty(value
))
3662 facility
= lxc_syslog_priority_to_int(value
);
3663 if (facility
== -EINVAL
)
3664 return ret_errno(EINVAL
);
3666 lxc_log_syslog(facility
);
3668 return set_config_string_item(&lxc_conf
->syslog
, value
);
3671 static int set_config_no_new_privs(const char *key
, const char *value
,
3672 struct lxc_conf
*lxc_conf
, void *data
)
3677 if (lxc_config_value_empty(value
)) {
3678 lxc_conf
->no_new_privs
= false;
3682 ret
= lxc_safe_uint(value
, &v
);
3687 return ret_errno(EINVAL
);
3689 lxc_conf
->no_new_privs
= v
? true : false;
3694 /* Callbacks to get configuration items. */
3695 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3696 struct lxc_conf
*c
, void *data
)
3703 memset(retv
, 0, inlen
);
3707 switch (c
->personality
) {
3709 strprint(retv
, inlen
, "i686");
3712 strprint(retv
, inlen
, "x86_64");
3721 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3722 struct lxc_conf
*c
, void *data
)
3724 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3727 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3728 struct lxc_conf
*c
, void *data
)
3730 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3733 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3734 struct lxc_conf
*c
, void *data
)
3736 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3739 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3740 struct lxc_conf
*c
, void *data
)
3743 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3745 return syserror_set(-EINVAL
, "Built without AppArmor support");
3749 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3750 int inlen
, struct lxc_conf
*c
,
3754 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_incomplete
);
3756 return syserror_set(-EINVAL
, "Built without AppArmor support");
3760 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3761 int inlen
, struct lxc_conf
*c
,
3765 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_nesting
);
3767 return syserror_set(-EINVAL
, "Built without AppArmor support");
3771 static int get_config_apparmor_raw(const char *key
, char *retv
,
3772 int inlen
, struct lxc_conf
*c
,
3777 struct lxc_list
*it
;
3783 memset(retv
, 0, inlen
);
3785 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3786 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3791 return syserror_set(-EINVAL
, "Built without AppArmor support");
3795 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3796 struct lxc_conf
*c
, void *data
)
3799 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3801 return syserror_set(-EINVAL
, "Built without SELinux support");
3805 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3806 struct lxc_conf
*c
, void *data
)
3809 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3811 return syserror_set(-EINVAL
, "Built without SELinux support");
3815 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3816 struct lxc_conf
*c
, void *data
)
3818 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3822 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3823 * just the value(s) will be printed. Since there still could be more than one,
3824 * it is newline-separated.
3825 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3827 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3828 * 'lxc.cgroup.subsystem.key = value' format.
3830 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3831 int inlen
, struct lxc_conf
*c
,
3835 bool get_all
= false;
3837 size_t namespaced_token_len
;
3838 char *global_token
, *namespaced_token
;
3839 struct list_head
*list
;
3840 struct lxc_cgroup
*cgroup
;
3845 memset(retv
, 0, inlen
);
3847 if (version
== CGROUP2_SUPER_MAGIC
) {
3848 global_token
= "lxc.cgroup2";
3849 namespaced_token
= "lxc.cgroup2.";
3850 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3852 } else if (version
== CGROUP_SUPER_MAGIC
) {
3853 global_token
= "lxc.cgroup";
3854 namespaced_token
= "lxc.cgroup.";
3855 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3858 return ret_errno(EINVAL
);
3861 if (strequal(key
, global_token
))
3863 else if (strnequal(key
, namespaced_token
, namespaced_token_len
))
3864 key
+= namespaced_token_len
;
3866 return ret_errno(EINVAL
);
3868 list_for_each_entry(cgroup
, list
, head
) {
3870 if (version
!= cgroup
->version
)
3873 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3874 cgroup
->subsystem
, cgroup
->value
);
3875 } else if (strequal(cgroup
->subsystem
, key
)) {
3876 strprint(retv
, inlen
, "%s\n", cgroup
->value
);
3883 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3884 struct lxc_conf
*c
, void *data
)
3886 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3887 CGROUP_SUPER_MAGIC
);
3890 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3891 struct lxc_conf
*c
, void *data
)
3893 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3894 CGROUP2_SUPER_MAGIC
);
3897 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3898 struct lxc_conf
*lxc_conf
, void *data
)
3903 if (!strequal(key
, "lxc.cgroup.dir"))
3904 return ret_errno(EINVAL
);
3909 memset(retv
, 0, inlen
);
3911 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3916 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3917 struct lxc_conf
*lxc_conf
, void *data
)
3925 memset(retv
, 0, inlen
);
3927 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3932 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3933 struct lxc_conf
*lxc_conf
, void *data
)
3941 memset(retv
, 0, inlen
);
3943 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3948 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3950 struct lxc_conf
*lxc_conf
,
3959 memset(retv
, 0, inlen
);
3961 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3966 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3968 struct lxc_conf
*lxc_conf
,
3977 memset(retv
, 0, inlen
);
3979 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3984 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3985 int inlen
, struct lxc_conf
*lxc_conf
,
3988 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3989 lxc_conf
->cgroup_meta
.relative
);
3992 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3993 struct lxc_conf
*c
, void *data
)
3996 int len
, listlen
, ret
;
3998 /* "u 1000 1000000 65536"
4000 * let's render this as
4018 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
4019 char buf
[__LXC_IDMAP_STR_BUF
];
4024 memset(retv
, 0, inlen
);
4026 listlen
= list_len(&c
->id_map
);
4027 list_for_each_entry(map
, &c
->id_map
, head
) {
4028 ret
= strnprintf(buf
, sizeof(buf
), "%c %lu %lu %lu",
4029 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
4030 map
->nsid
, map
->hostid
, map
->range
);
4032 return ret_errno(EIO
);
4034 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
4040 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
4041 struct lxc_conf
*c
, void *data
)
4044 v
= lxc_log_priority_to_string(c
->loglevel
);
4045 return lxc_get_conf_str(retv
, inlen
, v
);
4048 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
4049 struct lxc_conf
*c
, void *data
)
4051 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
4054 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
4055 struct lxc_conf
*c
, void *data
)
4057 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
4060 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
4061 struct lxc_conf
*c
, void *data
)
4063 int len
, fulllen
= 0;
4064 const char *sep
= "";
4069 memset(retv
, 0, inlen
);
4071 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
4074 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
4075 case LXC_AUTO_PROC_MIXED
:
4076 strprint(retv
, inlen
, "%sproc:mixed", sep
);
4079 case LXC_AUTO_PROC_RW
:
4080 strprint(retv
, inlen
, "%sproc:rw", sep
);
4087 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
4088 case LXC_AUTO_SYS_RO
:
4089 strprint(retv
, inlen
, "%ssys:ro", sep
);
4092 case LXC_AUTO_SYS_RW
:
4093 strprint(retv
, inlen
, "%ssys:rw", sep
);
4096 case LXC_AUTO_SYS_MIXED
:
4097 strprint(retv
, inlen
, "%ssys:mixed", sep
);
4104 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
4105 case LXC_AUTO_CGROUP_NOSPEC
:
4106 strprint(retv
, inlen
, "%scgroup", sep
);
4108 case LXC_AUTO_CGROUP_MIXED
:
4109 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
4111 case LXC_AUTO_CGROUP_RO
:
4112 strprint(retv
, inlen
, "%scgroup:ro", sep
);
4114 case LXC_AUTO_CGROUP_RW
:
4115 strprint(retv
, inlen
, "%scgroup:rw", sep
);
4117 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
4118 strprint(retv
, inlen
, "%scgroup-full", sep
);
4120 case LXC_AUTO_CGROUP_FULL_MIXED
:
4121 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
4123 case LXC_AUTO_CGROUP_FULL_RO
:
4124 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
4126 case LXC_AUTO_CGROUP_FULL_RW
:
4127 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
4136 static int get_config_mount(const char *key
, char *retv
, int inlen
,
4137 struct lxc_conf
*c
, void *data
)
4139 int len
, fulllen
= 0;
4140 struct string_entry
*entry
;
4145 memset(retv
, 0, inlen
);
4147 list_for_each_entry(entry
, &c
->mount_entries
, head
) {
4148 strprint(retv
, inlen
, "%s\n", entry
->val
);
4154 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4155 struct lxc_conf
*c
, void *data
)
4157 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4160 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4161 struct lxc_conf
*c
, void *data
)
4163 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4166 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4167 struct lxc_conf
*c
, void *data
)
4169 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4172 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4173 struct lxc_conf
*c
, void *data
)
4175 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mnt_opts
.raw_options
);
4178 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4179 struct lxc_conf
*c
, void *data
)
4181 return lxc_get_conf_str(
4183 c
->utsname
? c
->utsname
->nodename
: NULL
);
4186 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4187 struct lxc_conf
*c
, void *data
)
4190 int len
, fulllen
= 0, found
= -1;
4191 struct lxc_list
*it
;
4194 subkey
= strchr(key
, '.');
4196 return ret_errno(EINVAL
);
4198 subkey
= strchr(subkey
+ 1, '.');
4200 return ret_errno(EINVAL
);
4202 if (*subkey
== '\0')
4203 return ret_errno(EINVAL
);
4205 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4206 if (strequal(lxchook_names
[i
], subkey
)) {
4213 return ret_errno(EINVAL
);
4218 memset(retv
, 0, inlen
);
4220 lxc_list_for_each(it
, &c
->hooks
[found
]) {
4221 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4227 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4228 struct lxc_conf
*c
, void *data
)
4230 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4233 static int get_config_net(const char *key
, char *retv
, int inlen
,
4234 struct lxc_conf
*c
, void *data
)
4236 int len
, fulllen
= 0;
4237 struct lxc_netdev
*netdev
;
4242 memset(retv
, 0, inlen
);
4244 list_for_each_entry(netdev
, &c
->netdevs
, head
) {
4245 const char *t
= lxc_net_type_to_str(netdev
->type
);
4246 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4252 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4253 struct lxc_conf
*c
, void *data
)
4255 int len
, fulllen
= 0;
4256 struct cap_entry
*cap
;
4261 memset(retv
, 0, inlen
);
4263 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4264 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4270 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4271 struct lxc_conf
*c
, void *data
)
4273 int len
, fulllen
= 0;
4274 struct cap_entry
*cap
;
4279 memset(retv
, 0, inlen
);
4281 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4282 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4288 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4289 struct lxc_conf
*c
, void *data
)
4291 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4294 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4295 struct lxc_conf
*c
, void *data
)
4297 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4300 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4301 struct lxc_conf
*c
, void *data
)
4303 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4307 static int get_config_console_buffer_size(const char *key
, char *retv
,
4308 int inlen
, struct lxc_conf
*c
,
4311 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4314 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4315 struct lxc_conf
*c
, void *data
)
4317 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4320 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4321 int inlen
, struct lxc_conf
*c
,
4325 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4327 return ret_errno(ENOSYS
);
4331 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4332 struct lxc_conf
*c
, void *data
)
4334 #ifdef HAVE_SECCOMP_NOTIFY
4335 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4337 return ret_errno(ENOSYS
);
4341 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4342 struct lxc_conf
*c
, void *data
)
4344 #ifdef HAVE_SECCOMP_NOTIFY
4345 return lxc_get_conf_str(retv
, inlen
,
4346 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4347 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4348 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4350 return ret_errno(ENOSYS
);
4354 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4355 struct lxc_conf
*c
, void *data
)
4358 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4360 return ret_errno(ENOSYS
);
4364 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4365 struct lxc_conf
*c
, void *data
)
4367 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4370 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4371 struct lxc_conf
*c
, void *data
)
4373 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4376 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4377 struct lxc_conf
*c
, void *data
)
4379 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4382 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4383 struct lxc_conf
*c
, void *data
)
4385 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4388 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4389 struct lxc_conf
*c
, void *data
)
4391 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4394 static int get_config_start(const char *key
, char *retv
, int inlen
,
4395 struct lxc_conf
*c
, void *data
)
4397 if (strequal(key
+ 10, "auto"))
4398 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4399 else if (strequal(key
+ 10, "delay"))
4400 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4401 else if (strequal(key
+ 10, "order"))
4402 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4404 return ret_errno(EINVAL
);
4407 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4408 struct lxc_conf
*c
, void *data
)
4410 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4413 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4414 struct lxc_conf
*c
, void *data
)
4416 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4419 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4420 int inlen
, struct lxc_conf
*c
,
4423 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4426 static int get_config_group(const char *key
, char *retv
, int inlen
,
4427 struct lxc_conf
*c
, void *data
)
4429 int len
, fulllen
= 0;
4430 struct lxc_list
*it
;
4435 memset(retv
, 0, inlen
);
4437 lxc_list_for_each(it
, &c
->groups
) {
4438 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4444 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4445 struct lxc_conf
*c
, void *data
)
4447 int len
, fulllen
= 0;
4448 struct environment_entry
*env
;
4453 memset(retv
, 0, inlen
);
4455 list_for_each_entry(env
, &c
->environment
, head
) {
4456 strprint(retv
, inlen
, "%s=%s\n", env
->key
, env
->val
);
4462 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4463 struct lxc_conf
*c
, void *data
)
4465 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4468 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4469 struct lxc_conf
*c
, void *data
)
4471 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4474 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4475 struct lxc_conf
*c
, void *data
)
4477 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4480 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4481 struct lxc_conf
*c
, void *data
)
4483 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4486 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4487 struct lxc_conf
*c
, void *data
)
4489 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4492 static int get_config_init_groups(const char *key
, char *retv
, int inlen
,
4493 struct lxc_conf
*c
, void *data
)
4495 int fulllen
= 0, len
;
4500 memset(retv
, 0, inlen
);
4502 if (c
->init_groups
.size
== 0)
4505 for (int i
= 0; i
< c
->init_groups
.size
; i
++)
4506 strprint(retv
, inlen
, "%s%d", (i
> 0) ? "," : "",
4507 c
->init_groups
.list
[i
]);
4512 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4513 struct lxc_conf
*c
, void *data
)
4515 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4518 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4519 struct lxc_conf
*c
, void *data
)
4521 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4524 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4525 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4526 * printed, in 'lxc.prlimit.resource = value' format.
4528 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4529 struct lxc_conf
*c
, void *data
)
4531 int fulllen
= 0, len
;
4532 bool get_all
= false;
4533 struct lxc_limit
*lim
;
4538 memset(retv
, 0, inlen
);
4540 if (strequal(key
, "lxc.prlimit"))
4542 else if (strnequal(key
, "lxc.prlimit.", 12))
4545 return ret_errno(EINVAL
);
4547 list_for_each_entry(lim
, &c
->limits
, head
) {
4548 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4549 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4552 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4553 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4554 partlen
= STRLITERALLEN("unlimited");
4556 partlen
= sprintf(buf
, "%" PRIu64
,
4557 (uint64_t)lim
->limit
.rlim_cur
);
4560 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4561 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4562 memcpy(buf
+ partlen
, ":unlimited",
4563 STRLITERALLEN(":unlimited") + 1);
4565 sprintf(buf
+ partlen
, ":%" PRIu64
,
4566 (uint64_t)lim
->limit
.rlim_max
);
4570 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4571 lim
->resource
, buf
);
4572 } else if (strequal(lim
->resource
, key
)) {
4573 strprint(retv
, inlen
, "%s", buf
);
4580 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4581 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4582 * entries will be printed, in 'lxc.sysctl.key = value' format.
4584 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4585 struct lxc_conf
*c
, void *data
)
4588 bool get_all
= false;
4590 struct lxc_sysctl
*sysctl
;
4595 memset(retv
, 0, inlen
);
4597 if (strequal(key
, "lxc.sysctl"))
4599 else if (strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4600 key
+= STRLITERALLEN("lxc.sysctl.");
4602 return ret_errno(EINVAL
);
4604 list_for_each_entry(sysctl
, &c
->sysctls
, head
) {
4606 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", sysctl
->key
,
4608 } else if (strequal(sysctl
->key
, key
)) {
4609 strprint(retv
, inlen
, "%s", sysctl
->value
);
4616 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4617 struct lxc_conf
*c
, void *data
)
4620 bool get_all
= false;
4622 struct lxc_proc
*proc
;
4627 memset(retv
, 0, inlen
);
4629 if (strequal(key
, "lxc.proc"))
4631 else if (strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4632 key
+= STRLITERALLEN("lxc.proc.");
4634 return ret_errno(EINVAL
);
4636 list_for_each_entry(proc
, &c
->procs
, head
) {
4638 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4639 proc
->filename
, proc
->value
);
4640 } else if (strequal(proc
->filename
, key
)) {
4641 strprint(retv
, inlen
, "%s", proc
->value
);
4648 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4649 struct lxc_conf
*c
, void *data
)
4657 memset(retv
, 0, inlen
);
4659 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4660 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4661 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4667 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4668 struct lxc_conf
*c
, void *data
)
4676 memset(retv
, 0, inlen
);
4678 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4679 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4680 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4686 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4695 memset(retv
, 0, inlen
);
4697 if (c
->timens
.s_boot
) {
4698 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4700 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4706 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4707 struct lxc_conf
*c
, void *data
)
4715 memset(retv
, 0, inlen
);
4717 if (c
->timens
.s_monotonic
) {
4718 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4720 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4726 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4727 struct lxc_conf
*c
, void *data
)
4730 const char *namespace;
4736 memset(retv
, 0, inlen
);
4738 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4739 if (is_empty_string(namespace))
4740 return ret_errno(EINVAL
);
4742 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4746 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4751 /* Callbacks to clear config items. */
4752 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4755 c
->personality
= -1;
4759 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4766 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4773 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4776 free_disarm(c
->ttys
.dir
);
4780 static inline int clr_config_apparmor_profile(const char *key
,
4781 struct lxc_conf
*c
, void *data
)
4784 free_disarm(c
->lsm_aa_profile
);
4787 return syserror_set(-EINVAL
, "Built without AppArmor support");
4791 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4796 c
->lsm_aa_allow_incomplete
= 0;
4799 return syserror_set(-EINVAL
, "Built without AppArmor support");
4803 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4808 c
->lsm_aa_allow_nesting
= 0;
4811 return syserror_set(-EINVAL
, "Built without AppArmor support");
4815 static inline int clr_config_apparmor_raw(const char *key
,
4820 return lxc_clear_apparmor_raw(c
);
4822 return syserror_set(-EINVAL
, "Built without AppArmor support");
4826 static inline int clr_config_selinux_context(const char *key
,
4827 struct lxc_conf
*c
, void *data
)
4830 free_disarm(c
->lsm_se_context
);
4833 return syserror_set(-EINVAL
, "Built without SELinux support");
4837 static inline int clr_config_selinux_context_keyring(const char *key
,
4838 struct lxc_conf
*c
, void *data
)
4841 free_disarm(c
->lsm_se_keyring_context
);
4844 return syserror_set(-EINVAL
, "Built without SELinux support");
4848 static inline int clr_config_keyring_session(const char *key
,
4849 struct lxc_conf
*c
, void *data
)
4851 c
->keyring_disable_session
= false;
4855 static inline int clr_config_cgroup_controller(const char *key
,
4856 struct lxc_conf
*c
, void *data
)
4858 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4861 static inline int clr_config_cgroup2_controller(const char *key
,
4862 struct lxc_conf
*c
, void *data
)
4864 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4867 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4870 if (!strequal(key
, "lxc.cgroup.dir"))
4871 return ret_errno(EINVAL
);
4873 if (lxc_conf
->cgroup_meta
.dir
)
4874 free_disarm(lxc_conf
->cgroup_meta
.dir
);
4879 static int clr_config_cgroup_monitor_dir(const char *key
,
4880 struct lxc_conf
*lxc_conf
,
4883 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4887 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4888 struct lxc_conf
*lxc_conf
,
4891 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4895 static int clr_config_cgroup_container_dir(const char *key
,
4896 struct lxc_conf
*lxc_conf
,
4899 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4903 static int clr_config_cgroup_container_inner_dir(const char *key
,
4904 struct lxc_conf
*lxc_conf
,
4907 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4911 static inline int clr_config_cgroup_relative(const char *key
,
4912 struct lxc_conf
*lxc_conf
,
4915 lxc_conf
->cgroup_meta
.relative
= false;
4919 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4922 return lxc_clear_idmaps(c
);
4925 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4928 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4932 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4935 free_disarm(c
->logfile
);
4939 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4942 return lxc_clear_mount_entries(c
);
4945 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4948 return lxc_clear_automounts(c
);
4951 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4954 free_disarm(c
->fstab
);
4958 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4961 free_disarm(c
->rootfs
.path
);
4965 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4968 c
->rootfs
.managed
= true;
4972 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4975 free_disarm(c
->rootfs
.mount
);
4979 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4982 put_lxc_mount_options(&c
->rootfs
.mnt_opts
);
4987 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4990 free_disarm(c
->utsname
);
4994 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4997 return lxc_clear_hooks(c
, key
);
5000 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
5003 /* default to legacy hooks version */
5004 c
->hooks_version
= 0;
5008 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
5011 lxc_free_networks(c
);
5016 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
5019 return lxc_clear_config_caps(c
);
5022 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
5025 return lxc_clear_config_caps(c
);
5028 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
5031 free_disarm(c
->console
.path
);
5035 static inline int clr_config_console_logfile(const char *key
,
5036 struct lxc_conf
*c
, void *data
)
5038 free_disarm(c
->console
.log_path
);
5042 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
5045 c
->console
.log_rotate
= 0;
5049 static inline int clr_config_console_buffer_size(const char *key
,
5050 struct lxc_conf
*c
, void *data
)
5052 c
->console
.buffer_size
= 0;
5056 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
5059 c
->console
.log_size
= 0;
5063 static inline int clr_config_seccomp_allow_nesting(const char *key
,
5064 struct lxc_conf
*c
, void *data
)
5067 c
->seccomp
.allow_nesting
= 0;
5070 return ret_errno(ENOSYS
);
5074 static inline int clr_config_seccomp_notify_cookie(const char *key
,
5075 struct lxc_conf
*c
, void *data
)
5077 #ifdef HAVE_SECCOMP_NOTIFY
5078 free_disarm(c
->seccomp
.notifier
.cookie
);
5081 return ret_errno(ENOSYS
);
5085 static inline int clr_config_seccomp_notify_proxy(const char *key
,
5086 struct lxc_conf
*c
, void *data
)
5088 #ifdef HAVE_SECCOMP_NOTIFY
5089 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
5090 sizeof(c
->seccomp
.notifier
.proxy_addr
));
5093 return ret_errno(ENOSYS
);
5097 static inline int clr_config_seccomp_profile(const char *key
,
5098 struct lxc_conf
*c
, void *data
)
5100 free_disarm(c
->seccomp
.seccomp
);
5104 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
5111 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
5114 c
->autodevtmpfssize
= 500000;
5118 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
5125 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
5128 c
->rebootsignal
= 0;
5132 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
5139 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
5142 if (strequal(key
+ 10, "auto"))
5144 else if (strequal(key
+ 10, "delay"))
5146 else if (strequal(key
+ 10, "order"))
5152 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
5155 free_disarm(c
->syslog
);
5159 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
5162 c
->monitor_unshare
= 0;
5166 static inline int clr_config_monitor_signal_pdeath(const char *key
,
5167 struct lxc_conf
*c
, void *data
)
5169 c
->monitor_signal_pdeath
= 0;
5173 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
5176 return lxc_clear_groups(c
);
5179 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5182 return lxc_clear_environment(c
);
5185 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5188 free_disarm(c
->execute_cmd
);
5192 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5195 free_disarm(c
->init_cmd
);
5199 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5202 free_disarm(c
->init_cwd
);
5206 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5213 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5220 static inline int clr_config_init_groups(const char *key
, struct lxc_conf
*c
,
5223 c
->init_groups
.size
= 0;
5224 free_disarm(c
->init_groups
.list
);
5228 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5235 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5238 c
->no_new_privs
= false;
5242 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5245 return lxc_clear_limits(c
, key
);
5248 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5251 return lxc_clear_sysctls(c
, key
);
5254 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5257 return lxc_clear_procs(c
, key
);
5260 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5266 static int clr_config_namespace_clone(const char *key
,
5267 struct lxc_conf
*lxc_conf
, void *data
)
5269 lxc_conf
->ns_clone
= 0;
5273 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5276 lxc_conf
->ns_keep
= 0;
5280 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5282 lxc_conf
->timens
.s_boot
= 0;
5283 lxc_conf
->timens
.ns_boot
= 0;
5287 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5289 lxc_conf
->timens
.s_monotonic
= 0;
5290 lxc_conf
->timens
.ns_monotonic
= 0;
5294 static int clr_config_namespace_share(const char *key
,
5295 struct lxc_conf
*lxc_conf
, void *data
)
5298 const char *namespace;
5300 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5301 if (is_empty_string(namespace))
5302 return ret_errno(EINVAL
);
5304 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5308 free(lxc_conf
->ns_share
[ns_idx
]);
5309 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5314 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5315 struct lxc_conf
*c
, void *data
)
5317 return ret_errno(ENOSYS
);
5320 struct config_net_info
{
5321 char buf
[NETWORK_SUBKEY_SIZE_MAX
];
5323 const struct lxc_config_net_t
*ops
;
5324 struct lxc_netdev
*netdev
;
5327 static int get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
,
5328 struct config_net_info
*info
, bool allocate
)
5333 const char *idx_start
;
5335 if (is_empty_string(key
))
5336 return ret_errno(EINVAL
);
5338 /* check that this is a sensible network key */
5339 if (!strnequal("lxc.net.", key
, STRLITERALLEN("lxc.net.")))
5340 return syserror_set(-EINVAL
, "Invalid network configuration key \"%s\"", key
);
5343 /* beginning of index string */
5344 idx_start
= key
+ STRLITERALLEN("lxc.net.");
5345 if (!isdigit(*idx_start
))
5346 return syserror_set(-EINVAL
, "Failed to detect digit in string \"%s\"", key
+ 8);
5348 ret
= lxc_safe_int64_residual(idx_start
, &tmpidx
, 10, info
->buf
, sizeof(info
->buf
));
5350 return syserror("Failed to parse network index");
5352 if (tmpidx
< 0 || tmpidx
>= INT_MAX
)
5353 return syserror_set(-ERANGE
, "Number of configured networks would overflow the counter");
5354 idx
= (unsigned int)tmpidx
;
5356 info
->netdev
= lxc_get_netdev_by_idx(lxc_conf
, idx
, allocate
);
5358 return ret_errno(EINVAL
);
5360 /* Make sure subkey points to the empty string. */
5361 info
->subkey
= info
->buf
;
5362 if (is_empty_string(info
->subkey
))
5363 return ret_errno(ENOENT
);
5365 if (info
->subkey
[0] != '.')
5366 return syserror_set(-EINVAL
, "Invalid subkey");
5369 /* lxc.net.<idx>.<subkey> */
5370 info
->ops
= lxc_get_config_net(info
->subkey
);
5371 if (info
->ops
== &unsupported_config_net_key
)
5372 return syserror_set(-ENOENT
, "Unknown network configuration key \"%s\"", key
);
5377 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5378 * found. So we make sure next comes an integer, find the right callback (by
5379 * rewriting the key), and call it.
5381 static int set_config_jump_table_net(const char *key
, const char *value
,
5382 struct lxc_conf
*lxc_conf
, void *data
)
5384 struct config_net_info info
= {};
5386 const char *idxstring
;
5388 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5389 if (!isdigit(*idxstring
))
5390 return ret_errno(EINVAL
);
5392 if (lxc_config_value_empty(value
))
5393 return clr_config_jump_table_net(key
, lxc_conf
, data
);
5395 ret
= get_network_config_ops(key
, lxc_conf
, &info
, true);
5399 return info
.ops
->set(info
.subkey
, value
, lxc_conf
, info
.netdev
);
5402 static int clr_config_jump_table_net(const char *key
, struct lxc_conf
*lxc_conf
,
5405 struct config_net_info info
= {};
5407 const char *idxstring
;
5409 idxstring
= key
+ 8;
5410 if (!isdigit(*idxstring
))
5411 return ret_errno(EINVAL
);
5413 /* The left conjunct is pretty self-explanatory. The right conjunct
5414 * checks whether the two pointers are equal. If they are we know that
5415 * this is not a key that is namespaced any further and so we are
5416 * supposed to clear the whole network.
5418 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5419 unsigned int rmnetdevidx
;
5421 ret
= lxc_safe_uint(idxstring
, &rmnetdevidx
);
5425 /* Remove network from network list. */
5426 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5430 ret
= get_network_config_ops(key
, lxc_conf
, &info
, false);
5434 return info
.ops
->clr(info
.subkey
, lxc_conf
, info
.netdev
);
5437 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5440 struct lxc_netdev
*netdev
= data
;
5443 return ret_errno(EINVAL
);
5445 lxc_clear_netdev(netdev
);
5450 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5453 struct lxc_netdev
*netdev
= data
;
5456 return ret_errno(EINVAL
);
5458 netdev
->name
[0] = '\0';
5463 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5466 struct lxc_netdev
*netdev
= data
;
5469 return ret_errno(EINVAL
);
5476 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5479 struct lxc_netdev
*netdev
= data
;
5482 return ret_errno(EINVAL
);
5484 netdev
->link
[0] = '\0';
5489 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5492 struct lxc_netdev
*netdev
= data
;
5495 return ret_errno(EINVAL
);
5497 netdev
->l2proxy
= false;
5502 static int clr_config_net_macvlan_mode(const char *key
,
5503 struct lxc_conf
*lxc_conf
, void *data
)
5505 struct lxc_netdev
*netdev
= data
;
5508 return ret_errno(EINVAL
);
5510 if (netdev
->type
!= LXC_NET_MACVLAN
)
5513 netdev
->priv
.macvlan_attr
.mode
= -1;
5518 static int clr_config_net_ipvlan_mode(const char *key
,
5519 struct lxc_conf
*lxc_conf
, void *data
)
5521 struct lxc_netdev
*netdev
= data
;
5524 return ret_errno(EINVAL
);
5526 if (netdev
->type
!= LXC_NET_IPVLAN
)
5529 netdev
->priv
.ipvlan_attr
.mode
= -1;
5534 static int clr_config_net_ipvlan_isolation(const char *key
,
5535 struct lxc_conf
*lxc_conf
, void *data
)
5537 struct lxc_netdev
*netdev
= data
;
5540 return ret_errno(EINVAL
);
5542 if (netdev
->type
!= LXC_NET_IPVLAN
)
5545 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5550 static int clr_config_net_veth_mode(const char *key
,
5551 struct lxc_conf
*lxc_conf
, void *data
)
5553 struct lxc_netdev
*netdev
= data
;
5556 return ret_errno(EINVAL
);
5558 if (netdev
->type
!= LXC_NET_VETH
)
5561 netdev
->priv
.veth_attr
.mode
= -1;
5566 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5569 struct lxc_netdev
*netdev
= data
;
5572 return ret_errno(EINVAL
);
5574 if (netdev
->type
!= LXC_NET_VETH
)
5577 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5582 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5585 struct lxc_netdev
*netdev
= data
;
5588 return ret_errno(EINVAL
);
5590 if (netdev
->type
!= LXC_NET_VETH
)
5593 netdev
->priv
.veth_attr
.vlan_id
= 0;
5594 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5599 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5600 struct lxc_conf
*lxc_conf
, void *data
)
5602 struct lxc_netdev
*netdev
= data
;
5603 struct lxc_list
*cur
, *next
;
5606 return ret_errno(EINVAL
);
5608 if (netdev
->type
!= LXC_NET_VETH
)
5611 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5620 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5623 struct lxc_netdev
*netdev
= data
;
5626 return ret_errno(EINVAL
);
5628 free_disarm(netdev
->upscript
);
5633 static int clr_config_net_script_down(const char *key
,
5634 struct lxc_conf
*lxc_conf
, void *data
)
5636 struct lxc_netdev
*netdev
= data
;
5639 return ret_errno(EINVAL
);
5641 free_disarm(netdev
->downscript
);
5646 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5649 struct lxc_netdev
*netdev
= data
;
5652 return ret_errno(EINVAL
);
5654 free_disarm(netdev
->hwaddr
);
5659 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5662 struct lxc_netdev
*netdev
= data
;
5665 return ret_errno(EINVAL
);
5667 free_disarm(netdev
->mtu
);
5672 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5675 struct lxc_netdev
*netdev
= data
;
5678 return ret_errno(EINVAL
);
5680 if (netdev
->type
!= LXC_NET_VLAN
)
5683 netdev
->priv
.vlan_attr
.vid
= 0;
5688 static int clr_config_net_ipv4_gateway(const char *key
,
5689 struct lxc_conf
*lxc_conf
, void *data
)
5691 struct lxc_netdev
*netdev
= data
;
5694 return ret_errno(EINVAL
);
5696 free_disarm(netdev
->ipv4_gateway
);
5701 static int clr_config_net_ipv4_address(const char *key
,
5702 struct lxc_conf
*lxc_conf
, void *data
)
5704 struct lxc_netdev
*netdev
= data
;
5705 struct lxc_inetdev
*inetdev
, *ninetdev
;
5708 return ret_errno(EINVAL
);
5710 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->ipv4_addresses
, head
) {
5711 list_del(&inetdev
->head
);
5718 static int clr_config_net_veth_ipv4_route(const char *key
,
5719 struct lxc_conf
*lxc_conf
, void *data
)
5721 struct lxc_netdev
*netdev
= data
;
5722 struct lxc_inetdev
*inetdev
, *ninetdev
;
5725 return ret_errno(EINVAL
);
5727 if (netdev
->type
!= LXC_NET_VETH
)
5730 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
5731 list_del(&inetdev
->head
);
5738 static int clr_config_net_ipv6_gateway(const char *key
,
5739 struct lxc_conf
*lxc_conf
, void *data
)
5741 struct lxc_netdev
*netdev
= data
;
5744 return ret_errno(EINVAL
);
5746 free_disarm(netdev
->ipv6_gateway
);
5751 static int clr_config_net_ipv6_address(const char *key
,
5752 struct lxc_conf
*lxc_conf
, void *data
)
5754 struct lxc_netdev
*netdev
= data
;
5755 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5758 return ret_errno(EINVAL
);
5760 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->ipv6_addresses
, head
) {
5761 list_del(&inet6dev
->head
);
5768 static int clr_config_net_veth_ipv6_route(const char *key
,
5769 struct lxc_conf
*lxc_conf
, void *data
)
5771 struct lxc_netdev
*netdev
= data
;
5772 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5775 return ret_errno(EINVAL
);
5777 if (netdev
->type
!= LXC_NET_VETH
)
5780 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
5781 list_del(&inet6dev
->head
);
5788 static int get_config_jump_table_net(const char *key
, char *retv
, int inlen
,
5789 struct lxc_conf
*c
, void *data
)
5791 struct config_net_info info
= {};
5793 const char *idxstring
;
5795 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5796 if (!isdigit(*idxstring
))
5797 return ret_errno(EINVAL
);
5799 ret
= get_network_config_ops(key
, c
, &info
, false);
5803 return info
.ops
->get(info
.subkey
, retv
, inlen
, c
, info
.netdev
);
5806 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5807 struct lxc_conf
*c
, void *data
)
5811 struct lxc_netdev
*netdev
= data
;
5814 return ret_errno(EINVAL
);
5819 memset(retv
, 0, inlen
);
5821 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5826 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5827 struct lxc_conf
*c
, void *data
)
5831 struct lxc_netdev
*netdev
= data
;
5834 return ret_errno(EINVAL
);
5839 memset(retv
, 0, inlen
);
5841 if (netdev
->flags
& IFF_UP
)
5842 strprint(retv
, inlen
, "up");
5847 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5848 struct lxc_conf
*c
, void *data
)
5852 struct lxc_netdev
*netdev
= data
;
5855 return ret_errno(EINVAL
);
5860 memset(retv
, 0, inlen
);
5862 if (netdev
->link
[0] != '\0')
5863 strprint(retv
, inlen
, "%s", netdev
->link
);
5868 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5869 struct lxc_conf
*c
, void *data
)
5871 struct lxc_netdev
*netdev
= data
;
5874 return ret_errno(EINVAL
);
5876 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5879 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5880 struct lxc_conf
*c
, void *data
)
5884 struct lxc_netdev
*netdev
= data
;
5887 return ret_errno(EINVAL
);
5892 memset(retv
, 0, inlen
);
5894 if (netdev
->name
[0] != '\0')
5895 strprint(retv
, inlen
, "%s", netdev
->name
);
5900 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5901 struct lxc_conf
*c
, void *data
)
5906 struct lxc_netdev
*netdev
= data
;
5909 return ret_errno(EINVAL
);
5911 if (netdev
->type
!= LXC_NET_MACVLAN
)
5912 return ret_errno(EINVAL
);
5917 memset(retv
, 0, inlen
);
5919 switch (netdev
->priv
.macvlan_attr
.mode
) {
5920 case MACVLAN_MODE_PRIVATE
:
5923 case MACVLAN_MODE_VEPA
:
5926 case MACVLAN_MODE_BRIDGE
:
5929 case MACVLAN_MODE_PASSTHRU
:
5937 strprint(retv
, inlen
, "%s", mode
);
5942 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5943 struct lxc_conf
*c
, void *data
)
5946 struct lxc_netdev
*netdev
= data
;
5951 return ret_errno(EINVAL
);
5953 if (netdev
->type
!= LXC_NET_IPVLAN
)
5954 return ret_errno(EINVAL
);
5959 memset(retv
, 0, inlen
);
5961 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5962 case IPVLAN_MODE_L3
:
5965 case IPVLAN_MODE_L3S
:
5968 case IPVLAN_MODE_L2
:
5976 strprint(retv
, inlen
, "%s", mode
);
5981 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5982 struct lxc_conf
*c
, void *data
)
5985 struct lxc_netdev
*netdev
= data
;
5990 return ret_errno(EINVAL
);
5992 if (netdev
->type
!= LXC_NET_IPVLAN
)
5993 return ret_errno(EINVAL
);
5998 memset(retv
, 0, inlen
);
6000 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
6001 case IPVLAN_ISOLATION_BRIDGE
:
6004 case IPVLAN_ISOLATION_PRIVATE
:
6007 case IPVLAN_ISOLATION_VEPA
:
6015 strprint(retv
, inlen
, "%s", mode
);
6020 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
6021 struct lxc_conf
*c
, void *data
)
6024 struct lxc_netdev
*netdev
= data
;
6029 return ret_errno(EINVAL
);
6031 if (netdev
->type
!= LXC_NET_VETH
)
6032 return ret_errno(EINVAL
);
6037 memset(retv
, 0, inlen
);
6039 switch (netdev
->priv
.veth_attr
.mode
) {
6040 case VETH_MODE_BRIDGE
:
6043 case VETH_MODE_ROUTER
:
6051 strprint(retv
, inlen
, "%s", mode
);
6056 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
6057 struct lxc_conf
*c
, void *data
)
6061 struct lxc_netdev
*netdev
= data
;
6064 return ret_errno(EINVAL
);
6066 if (netdev
->type
!= LXC_NET_VETH
)
6067 return ret_errno(EINVAL
);
6072 memset(retv
, 0, inlen
);
6074 strprint(retv
, inlen
, "%s",
6075 netdev
->priv
.veth_attr
.pair
[0] != '\0'
6076 ? netdev
->priv
.veth_attr
.pair
6077 : netdev
->priv
.veth_attr
.veth1
);
6082 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
6083 struct lxc_conf
*c
, void *data
)
6087 struct lxc_netdev
*netdev
= data
;
6090 return ret_errno(EINVAL
);
6092 if (netdev
->type
!= LXC_NET_VETH
)
6093 return ret_errno(EINVAL
);
6098 memset(retv
, 0, inlen
);
6100 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
6105 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
,
6106 int inlen
, struct lxc_conf
*c
,
6111 struct lxc_list
*it
;
6113 struct lxc_netdev
*netdev
= data
;
6116 return ret_errno(EINVAL
);
6118 if (netdev
->type
!= LXC_NET_VETH
)
6119 return ret_errno(EINVAL
);
6124 memset(retv
, 0, inlen
);
6126 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
6128 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
6129 unsigned short i
= PTR_TO_USHORT(it
->elem
);
6130 strprint(retv
, inlen
, "%u%s", i
, (listlen
-- > 1) ? "\n" : "");
6136 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6137 struct lxc_conf
*c
, void *data
)
6141 struct lxc_netdev
*netdev
= data
;
6144 return ret_errno(EINVAL
);
6149 memset(retv
, 0, inlen
);
6151 if (netdev
->upscript
)
6152 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6157 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6158 struct lxc_conf
*c
, void *data
)
6162 struct lxc_netdev
*netdev
= data
;
6165 return ret_errno(EINVAL
);
6170 memset(retv
, 0, inlen
);
6172 if (netdev
->downscript
)
6173 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6178 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6179 struct lxc_conf
*c
, void *data
)
6183 struct lxc_netdev
*netdev
= data
;
6186 return ret_errno(EINVAL
);
6191 memset(retv
, 0, inlen
);
6194 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6199 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6200 struct lxc_conf
*c
, void *data
)
6204 struct lxc_netdev
*netdev
= data
;
6207 return ret_errno(EINVAL
);
6212 memset(retv
, 0, inlen
);
6215 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6220 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6221 struct lxc_conf
*c
, void *data
)
6225 struct lxc_netdev
*netdev
= data
;
6228 return ret_errno(EINVAL
);
6230 if (netdev
->type
!= LXC_NET_VLAN
)
6231 return ret_errno(EINVAL
);
6236 memset(retv
, 0, inlen
);
6238 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6243 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6244 struct lxc_conf
*c
, void *data
)
6247 char buf
[INET_ADDRSTRLEN
];
6249 struct lxc_netdev
*netdev
= data
;
6252 return ret_errno(EINVAL
);
6257 memset(retv
, 0, inlen
);
6259 if (netdev
->ipv4_gateway_auto
) {
6260 strprint(retv
, inlen
, "auto");
6261 } else if (netdev
->ipv4_gateway_dev
) {
6262 strprint(retv
, inlen
, "dev");
6263 } else if (netdev
->ipv4_gateway
) {
6264 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
)))
6266 strprint(retv
, inlen
, "%s", buf
);
6272 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6273 struct lxc_conf
*c
, void *data
)
6276 struct lxc_netdev
*netdev
= data
;
6279 char buf
[INET_ADDRSTRLEN
];
6280 struct lxc_inetdev
*inetdev
;
6283 return ret_errno(EINVAL
);
6288 memset(retv
, 0, inlen
);
6290 listlen
= list_len(&netdev
->ipv4_addresses
);
6292 list_for_each_entry(inetdev
, &netdev
->ipv4_addresses
, head
) {
6293 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6295 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6296 (listlen
-- > 1) ? "\n" : "");
6302 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6303 struct lxc_conf
*c
, void *data
)
6307 char buf
[INET_ADDRSTRLEN
];
6308 struct lxc_inetdev
*inetdev
;
6310 struct lxc_netdev
*netdev
= data
;
6313 return ret_errno(EINVAL
);
6315 if (netdev
->type
!= LXC_NET_VETH
)
6316 return ret_errno(EINVAL
);
6321 memset(retv
, 0, inlen
);
6323 listlen
= list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
6324 list_for_each_entry(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
6325 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6327 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6328 (listlen
-- > 1) ? "\n" : "");
6334 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6335 struct lxc_conf
*c
, void *data
)
6338 char buf
[INET6_ADDRSTRLEN
];
6340 struct lxc_netdev
*netdev
= data
;
6343 return ret_errno(EINVAL
);
6348 memset(retv
, 0, inlen
);
6350 if (netdev
->ipv6_gateway_auto
) {
6351 strprint(retv
, inlen
, "auto");
6352 } else if (netdev
->ipv6_gateway_dev
) {
6353 strprint(retv
, inlen
, "dev");
6354 } else if (netdev
->ipv6_gateway
) {
6355 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
)))
6357 strprint(retv
, inlen
, "%s", buf
);
6363 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6364 struct lxc_conf
*c
, void *data
)
6368 char buf
[INET6_ADDRSTRLEN
];
6370 struct lxc_netdev
*netdev
= data
;
6371 struct lxc_inet6dev
*inet6dev
;
6374 return ret_errno(EINVAL
);
6379 memset(retv
, 0, inlen
);
6381 listlen
= list_len(&netdev
->ipv6_addresses
);
6382 list_for_each_entry(inet6dev
, &netdev
->ipv6_addresses
, head
) {
6383 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6385 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6386 (listlen
-- > 1) ? "\n" : "");
6392 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6393 struct lxc_conf
*c
, void *data
)
6397 char buf
[INET6_ADDRSTRLEN
];
6398 struct lxc_inet6dev
*inet6dev
;
6400 struct lxc_netdev
*netdev
= data
;
6403 return ret_errno(EINVAL
);
6405 if (netdev
->type
!= LXC_NET_VETH
)
6406 return ret_errno(EINVAL
);
6411 memset(retv
, 0, inlen
);
6413 listlen
= list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
6414 list_for_each_entry(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
6415 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6417 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6418 (listlen
-- > 1) ? "\n" : "");
6424 int lxc_list_config_items(char *retv
, int inlen
)
6433 memset(retv
, 0, inlen
);
6435 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
6436 char *s
= config_jump_table
[i
].name
;
6438 if (s
[strlen(s
) - 1] == '.')
6441 strprint(retv
, inlen
, "%s\n", s
);
6447 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6456 memset(retv
, 0, inlen
);
6458 if (strequal(key
, "lxc.apparmor")) {
6459 strprint(retv
, inlen
, "allow_incomplete\n");
6460 strprint(retv
, inlen
, "allow_nesting\n");
6461 strprint(retv
, inlen
, "profile\n");
6462 strprint(retv
, inlen
, "raw\n");
6463 } else if (strequal(key
, "lxc.cgroup")) {
6464 strprint(retv
, inlen
, "dir\n");
6465 } else if (strequal(key
, "lxc.selinux")) {
6466 strprint(retv
, inlen
, "context\n");
6467 strprint(retv
, inlen
, "context.keyring\n");
6468 } else if (strequal(key
, "lxc.mount")) {
6469 strprint(retv
, inlen
, "auto\n");
6470 strprint(retv
, inlen
, "entry\n");
6471 strprint(retv
, inlen
, "fstab\n");
6472 } else if (strequal(key
, "lxc.rootfs")) {
6473 strprint(retv
, inlen
, "mount\n");
6474 strprint(retv
, inlen
, "options\n");
6475 strprint(retv
, inlen
, "path\n");
6476 } else if (strequal(key
, "lxc.uts")) {
6477 strprint(retv
, inlen
, "name\n");
6478 } else if (strequal(key
, "lxc.hook")) {
6479 strprint(retv
, inlen
, "autodev\n");
6480 strprint(retv
, inlen
, "autodevtmpfssize\n");
6481 strprint(retv
, inlen
, "clone\n");
6482 strprint(retv
, inlen
, "destroy\n");
6483 strprint(retv
, inlen
, "mount\n");
6484 strprint(retv
, inlen
, "post-stop\n");
6485 strprint(retv
, inlen
, "pre-mount\n");
6486 strprint(retv
, inlen
, "pre-start\n");
6487 strprint(retv
, inlen
, "start-host\n");
6488 strprint(retv
, inlen
, "start\n");
6489 strprint(retv
, inlen
, "stop\n");
6490 } else if (strequal(key
, "lxc.cap")) {
6491 strprint(retv
, inlen
, "drop\n");
6492 strprint(retv
, inlen
, "keep\n");
6493 } else if (strequal(key
, "lxc.console")) {
6494 strprint(retv
, inlen
, "logfile\n");
6495 strprint(retv
, inlen
, "path\n");
6496 } else if (strequal(key
, "lxc.seccomp")) {
6497 strprint(retv
, inlen
, "profile\n");
6498 } else if (strequal(key
, "lxc.signal")) {
6499 strprint(retv
, inlen
, "halt\n");
6500 strprint(retv
, inlen
, "reboot\n");
6501 strprint(retv
, inlen
, "stop\n");
6502 } else if (strequal(key
, "lxc.start")) {
6503 strprint(retv
, inlen
, "auto\n");
6504 strprint(retv
, inlen
, "delay\n");
6505 strprint(retv
, inlen
, "order\n");
6506 } else if (strequal(key
, "lxc.monitor")) {
6507 strprint(retv
, inlen
, "unshare\n");
6508 } else if (strequal(key
, "lxc.keyring")) {
6509 strprint(retv
, inlen
, "session\n");
6511 fulllen
= ret_errno(EINVAL
);
6517 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6519 struct config_net_info info
= {};
6520 struct lxc_netdev
*netdev
;
6522 const char *idxstring
;
6525 idxstring
= key
+ STRLITERALLEN("lxc.net.");
6526 if (!isdigit(*idxstring
))
6527 return ret_errno(EINVAL
);
6529 ret
= get_network_config_ops(key
, c
, &info
, false);
6532 return ret_errno(EINVAL
);
6534 netdev
= info
.netdev
;
6539 memset(retv
, 0, inlen
);
6541 strprint(retv
, inlen
, "type\n");
6542 strprint(retv
, inlen
, "script.up\n");
6543 strprint(retv
, inlen
, "script.down\n");
6545 if (netdev
->type
!= LXC_NET_EMPTY
) {
6546 strprint(retv
, inlen
, "flags\n");
6547 strprint(retv
, inlen
, "link\n");
6548 strprint(retv
, inlen
, "name\n");
6549 strprint(retv
, inlen
, "hwaddr\n");
6550 strprint(retv
, inlen
, "mtu\n");
6551 strprint(retv
, inlen
, "ipv6.address\n");
6552 strprint(retv
, inlen
, "ipv6.gateway\n");
6553 strprint(retv
, inlen
, "ipv4.address\n");
6554 strprint(retv
, inlen
, "ipv4.gateway\n");
6557 switch (netdev
->type
) {
6559 strprint(retv
, inlen
, "veth.pair\n");
6560 strprint(retv
, inlen
, "veth.ipv4.route\n");
6561 strprint(retv
, inlen
, "veth.ipv6.route\n");
6562 strprint(retv
, inlen
, "veth.vlan.id\n");
6564 case LXC_NET_MACVLAN
:
6565 strprint(retv
, inlen
, "macvlan.mode\n");
6567 case LXC_NET_IPVLAN
:
6568 strprint(retv
, inlen
, "ipvlan.mode\n");
6569 strprint(retv
, inlen
, "ipvlan.isolation\n");
6572 strprint(retv
, inlen
, "vlan.id\n");