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 lxc_list
*mntlist
= NULL
;
2353 if (lxc_config_value_empty(value
))
2354 return lxc_clear_mount_entries(lxc_conf
);
2356 mntlist
= lxc_list_new();
2358 return ret_errno(ENOMEM
);
2360 mntelem
= strdup(value
);
2362 return ret_errno(ENOMEM
);
2364 mntlist
->elem
= move_ptr(mntelem
);
2365 lxc_list_add_tail(&lxc_conf
->mount_list
, move_ptr(mntlist
));
2370 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2371 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2374 static int add_cap_entry(struct lxc_conf
*conf
, char *caps
, bool keep
)
2379 * In case several capability keep is specified in a single line split
2380 * these caps in a single element for the list.
2382 lxc_iterate_parts(token
, caps
, " \t") {
2383 __do_free
struct cap_entry
*new_cap
= NULL
;
2386 if (strequal(token
, "none")) {
2388 return syserror_set(-EINVAL
, "The \"none\" keyword is only valid when keeping caps");
2390 lxc_clear_config_caps(conf
);
2394 cap
= parse_cap(token
);
2397 return syserror_set(-EINVAL
, "Invalid capability specified");
2399 INFO("Ignoring unknown capability \"%s\"", token
);
2403 new_cap
= zalloc(sizeof(struct cap_entry
));
2405 return ret_errno(ENOMEM
);
2407 new_cap
->cap_name
= strdup(token
);
2408 if (!new_cap
->cap_name
)
2409 return ret_errno(ENOMEM
);
2412 list_add_tail(&new_cap
->head
, &conf
->caps
.list
);
2419 static int set_config_cap_keep(const char *key
, const char *value
,
2420 struct lxc_conf
*lxc_conf
, void *data
)
2422 __do_free
char *caps
= NULL
;
2425 if (lxc_config_value_empty(value
))
2426 return lxc_clear_config_caps(lxc_conf
);
2428 caps
= strdup(value
);
2430 return ret_errno(ENOMEM
);
2432 if (!lxc_conf
->caps
.keep
&& !list_empty(&lxc_conf
->caps
.list
))
2433 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2435 ret
= add_cap_entry(lxc_conf
, caps
, true);
2439 lxc_conf
->caps
.keep
= true;
2443 static int set_config_cap_drop(const char *key
, const char *value
,
2444 struct lxc_conf
*lxc_conf
, void *data
)
2446 __do_free
char *caps
= NULL
;
2449 if (lxc_config_value_empty(value
))
2450 return lxc_clear_config_caps(lxc_conf
);
2452 if (lxc_conf
->caps
.keep
)
2453 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2455 caps
= strdup(value
);
2457 return ret_errno(ENOMEM
);
2459 ret
= add_cap_entry(lxc_conf
, caps
, false);
2463 lxc_conf
->caps
.keep
= false;
2467 static int set_config_console_path(const char *key
, const char *value
,
2468 struct lxc_conf
*lxc_conf
, void *data
)
2470 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2473 static int set_config_console_rotate(const char *key
, const char *value
,
2474 struct lxc_conf
*lxc_conf
, void *data
)
2478 if (lxc_config_value_empty(value
)) {
2479 lxc_conf
->console
.log_rotate
= 0;
2483 ret
= lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
);
2485 return ret_errno(EINVAL
);
2487 if (lxc_conf
->console
.log_rotate
> 1)
2488 return log_error_errno(-EINVAL
, EINVAL
, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
2493 static int set_config_console_logfile(const char *key
, const char *value
,
2494 struct lxc_conf
*lxc_conf
, void *data
)
2496 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2499 static int set_config_console_buffer_size(const char *key
, const char *value
,
2500 struct lxc_conf
*lxc_conf
, void *data
)
2504 uint64_t buffer_size
, pgsz
;
2506 if (lxc_config_value_empty(value
)) {
2507 lxc_conf
->console
.buffer_size
= 0;
2511 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2512 if (strequal(value
, "auto")) {
2513 lxc_conf
->console
.buffer_size
= 1 << 17;
2517 ret
= parse_byte_size_string(value
, &size
);
2522 return ret_errno(EINVAL
);
2524 /* must be at least a page size */
2525 pgsz
= lxc_getpagesize();
2526 if ((uint64_t)size
< pgsz
) {
2527 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2532 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2533 if (buffer_size
== 0)
2534 return ret_errno(EINVAL
);
2536 if (buffer_size
!= size
)
2537 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", buffer_size
);
2539 lxc_conf
->console
.buffer_size
= buffer_size
;
2544 static int set_config_console_size(const char *key
, const char *value
,
2545 struct lxc_conf
*lxc_conf
, void *data
)
2549 uint64_t log_size
, pgsz
;
2551 if (lxc_config_value_empty(value
)) {
2552 lxc_conf
->console
.log_size
= 0;
2556 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2557 if (strequal(value
, "auto")) {
2558 lxc_conf
->console
.log_size
= 1 << 17;
2562 ret
= parse_byte_size_string(value
, &size
);
2564 return ret_errno(EINVAL
);
2567 return ret_errno(EINVAL
);
2569 /* must be at least a page size */
2570 pgsz
= lxc_getpagesize();
2571 if ((uint64_t)size
< pgsz
) {
2572 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2577 log_size
= lxc_find_next_power2((uint64_t)size
);
2579 return ret_errno(EINVAL
);
2581 if (log_size
!= size
)
2582 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", log_size
);
2584 lxc_conf
->console
.log_size
= log_size
;
2590 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2591 * file, we expand it in the unexpanded_config, so that after a save_config we
2592 * store the hwaddr for re-use.
2593 * This is only called when reading the config file, not when executing a
2595 * 'x' and 'X' are substituted in-place.
2597 static void update_hwaddr(const char *line
)
2601 line
+= lxc_char_left_gc(line
, strlen(line
));
2605 if (!lxc_config_net_is_hwaddr(line
))
2608 /* Let config_net_hwaddr raise the error. */
2609 p
= strchr(line
, '=');
2620 rand_complete_hwaddr(p
);
2623 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2626 size_t len
= conf
->unexpanded_len
;
2628 update_hwaddr(line
);
2630 linelen
= strlen(line
);
2631 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2634 tmp
= realloc(conf
->unexpanded_config
, conf
->unexpanded_alloced
+ 1024);
2636 return ret_errno(EINVAL
);
2638 if (!conf
->unexpanded_config
)
2641 conf
->unexpanded_config
= tmp
;
2642 conf
->unexpanded_alloced
+= 1024;
2645 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2646 conf
->unexpanded_len
+= linelen
;
2647 if (line
[linelen
- 1] != '\n')
2648 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2649 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2654 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2656 __do_closedir
DIR *dir
= NULL
;
2657 struct dirent
*direntp
;
2660 dir
= opendir(dirp
);
2662 return errno
== ENOENT
? 0 : -errno
;
2664 while ((direntp
= readdir(dir
))) {
2666 char path
[PATH_MAX
];
2668 fnam
= direntp
->d_name
;
2669 if (strequal(fnam
, "."))
2672 if (strequal(fnam
, ".."))
2676 if (len
< 6 || !strnequal(fnam
+ len
- 5, ".conf", 5))
2679 len
= strnprintf(path
, sizeof(path
), "%s/%s", dirp
, fnam
);
2681 return ret_errno(EIO
);
2683 ret
= lxc_config_read(path
, lxc_conf
, true);
2691 static int set_config_includefiles(const char *key
, const char *value
,
2692 struct lxc_conf
*lxc_conf
, void *data
)
2694 if (lxc_config_value_empty(value
)) {
2695 clr_config_includefiles(key
, lxc_conf
, NULL
);
2699 if (value
[strlen(value
)-1] == '/' || is_dir(value
))
2700 return do_includedir(value
, lxc_conf
);
2702 return lxc_config_read(value
, lxc_conf
, true);
2705 static int set_config_rootfs_path(const char *key
, const char *value
,
2706 struct lxc_conf
*lxc_conf
, void *data
)
2708 __do_free
char *dup
= NULL
;
2711 const char *container_path
;
2713 if (lxc_config_value_empty(value
)) {
2714 free(lxc_conf
->rootfs
.path
);
2715 lxc_conf
->rootfs
.path
= NULL
;
2719 dup
= strdup(value
);
2721 return ret_errno(ENOMEM
);
2723 /* Split <storage type>:<container path> into <storage type> and
2724 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2725 * "rootfs.path" to <container path>.
2727 tmp
= strchr(dup
, ':');
2731 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2733 return ret_errno(ENOMEM
);
2736 container_path
= tmp
;
2738 container_path
= value
;
2741 return set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2744 static int set_config_rootfs_managed(const char *key
, const char *value
,
2745 struct lxc_conf
*lxc_conf
, void *data
)
2747 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2750 static int set_config_rootfs_mount(const char *key
, const char *value
,
2751 struct lxc_conf
*lxc_conf
, void *data
)
2753 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2756 static int set_config_rootfs_options(const char *key
, const char *value
,
2757 struct lxc_conf
*lxc_conf
, void *data
)
2759 __do_free
char *dup
= NULL
, *raw_options
= NULL
;
2760 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2761 struct lxc_mount_options
*mnt_opts
= &rootfs
->mnt_opts
;
2764 clr_config_rootfs_options(key
, lxc_conf
, data
);
2765 if (lxc_config_value_empty(value
))
2768 ret
= set_config_string_item(&raw_options
, value
);
2770 return ret_errno(ENOMEM
);
2772 dup
= strdup(value
);
2776 ret
= parse_lxc_mount_attrs(mnt_opts
, dup
);
2780 /* Make sure we're only valid LXC specific mount options. */
2781 if (mnt_opts
->create_dir
|| mnt_opts
->create_file
||
2782 mnt_opts
->optional
|| mnt_opts
->relative
)
2783 return syserror_set(-EINVAL
, "Invalid LXC specific mount option for rootfs mount");
2785 ret
= parse_mount_attrs(mnt_opts
, dup
);
2787 return ret_errno(EINVAL
);
2789 rootfs
->mnt_opts
.raw_options
= move_ptr(raw_options
);
2793 static int set_config_uts_name(const char *key
, const char *value
,
2794 struct lxc_conf
*lxc_conf
, void *data
)
2796 __do_free
struct utsname
*utsname
= NULL
;
2798 if (lxc_config_value_empty(value
)) {
2799 clr_config_uts_name(key
, lxc_conf
, NULL
);
2803 utsname
= zalloc(sizeof(*utsname
));
2805 return ret_errno(ENOMEM
);
2807 if (strlen(value
) >= sizeof(utsname
->nodename
))
2808 return ret_errno(EINVAL
);
2810 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2811 free(lxc_conf
->utsname
);
2812 lxc_conf
->utsname
= move_ptr(utsname
);
2817 static int set_config_namespace_clone(const char *key
, const char *value
,
2818 struct lxc_conf
*lxc_conf
, void *data
)
2820 __do_free
char *ns
= NULL
;
2824 if (lxc_config_value_empty(value
))
2825 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2827 if (lxc_conf
->ns_keep
!= 0)
2828 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2832 return ret_errno(ENOMEM
);
2834 lxc_iterate_parts(token
, ns
, " \t") {
2835 token
+= lxc_char_left_gc(token
, strlen(token
));
2836 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2837 cloneflag
= lxc_namespace_2_cloneflag(token
);
2839 return ret_errno(EINVAL
);
2840 lxc_conf
->ns_clone
|= cloneflag
;
2846 static int set_config_namespace_keep(const char *key
, const char *value
,
2847 struct lxc_conf
*lxc_conf
, void *data
)
2849 __do_free
char *ns
= NULL
;
2853 if (lxc_config_value_empty(value
))
2854 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2856 if (lxc_conf
->ns_clone
!= 0)
2857 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2861 return ret_errno(ENOMEM
);
2863 lxc_iterate_parts(token
, ns
, " \t") {
2864 token
+= lxc_char_left_gc(token
, strlen(token
));
2865 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2866 cloneflag
= lxc_namespace_2_cloneflag(token
);
2868 return ret_errno(EINVAL
);
2869 lxc_conf
->ns_keep
|= cloneflag
;
2875 static int set_config_time_offset_boot(const char *key
, const char *value
,
2876 struct lxc_conf
*lxc_conf
, void *data
)
2881 char buf
[STRLITERALLEN("ms") + 1];
2883 if (lxc_config_value_empty(value
))
2884 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2886 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2890 unit
= lxc_trim_whitespace_in_place(buf
);
2891 if (strequal(unit
, "h")) {
2892 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2893 return ret_errno(EOVERFLOW
);
2894 } else if (strequal(unit
, "m")) {
2895 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2896 return ret_errno(EOVERFLOW
);
2897 } else if (strequal(unit
, "s")) {
2898 lxc_conf
->timens
.s_boot
= offset
;
2899 } else if (strequal(unit
, "ms")) {
2900 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2901 return ret_errno(EOVERFLOW
);
2902 } else if (strequal(unit
, "us")) {
2903 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2904 return ret_errno(EOVERFLOW
);
2905 } else if (strequal(unit
, "ns")) {
2906 lxc_conf
->timens
.ns_boot
= offset
;
2908 return ret_errno(EINVAL
);
2914 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2915 struct lxc_conf
*lxc_conf
, void *data
)
2920 char buf
[STRLITERALLEN("ms") + 1];
2922 if (lxc_config_value_empty(value
))
2923 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2925 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2929 unit
= lxc_trim_whitespace_in_place(buf
);
2930 if (strequal(unit
, "h")) {
2931 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2932 return ret_errno(EOVERFLOW
);
2933 } else if (strequal(unit
, "m")) {
2934 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2935 return ret_errno(EOVERFLOW
);
2936 } else if (strequal(unit
, "s")) {
2937 lxc_conf
->timens
.s_monotonic
= offset
;
2938 } else if (strequal(unit
, "ms")) {
2939 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2940 return ret_errno(EOVERFLOW
);
2941 } else if (strequal(unit
, "us")) {
2942 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2943 return ret_errno(EOVERFLOW
);
2944 } else if (strequal(unit
, "ns")) {
2945 lxc_conf
->timens
.ns_monotonic
= offset
;
2947 return ret_errno(EINVAL
);
2953 static int set_config_namespace_share(const char *key
, const char *value
,
2954 struct lxc_conf
*lxc_conf
, void *data
)
2957 const char *namespace;
2959 if (lxc_config_value_empty(value
))
2960 return clr_config_namespace_share(key
, lxc_conf
, data
);
2962 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2963 if (is_empty_string(namespace))
2964 return ret_errno(EINVAL
);
2966 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2970 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2973 struct parse_line_conf
{
2974 struct lxc_conf
*conf
;
2978 static int parse_line(char *buffer
, void *data
)
2980 __do_free
char *linep
= NULL
;
2981 char *dot
, *key
, *line
, *value
;
2983 struct lxc_config_t
*config
;
2986 struct parse_line_conf
*plc
= data
;
2989 return syserror_set(-EINVAL
, "Missing config");
2991 /* If there are newlines in the config file we should keep them. */
2992 empty_line
= lxc_is_line_empty(dup
);
2996 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2997 * modified the original string on the stack by replacing '=' by '\0'
3000 linep
= line
= strdup(dup
);
3002 return ret_errno(ENOMEM
);
3004 if (!plc
->from_include
) {
3005 ret
= append_unexp_config_line(line
, plc
->conf
);
3013 line
+= lxc_char_left_gc(line
, strlen(line
));
3015 /* ignore comments */
3019 /* martian option - don't add it to the config itself */
3020 if (!strnequal(line
, "lxc.", 4))
3023 dot
= strchr(line
, '=');
3025 return log_error_errno(-EINVAL
, EINVAL
, "Invalid configuration line: %s", line
);
3031 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3033 value
+= lxc_char_left_gc(value
, strlen(value
));
3034 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3036 if (*value
== '\'' || *value
== '\"') {
3039 len
= strlen(value
);
3040 if (len
> 1 && value
[len
- 1] == *value
) {
3041 value
[len
- 1] = '\0';
3046 config
= lxc_get_config(key
);
3047 return config
->set(key
, value
, plc
->conf
, NULL
);
3050 static struct new_config_item
*parse_new_conf_line(char *buffer
)
3052 __do_free
char *k
= NULL
, *linep
= NULL
, *v
= NULL
;
3053 __do_free
struct new_config_item
*new = NULL
;
3055 char *dot
, *key
, *line
, *value
;
3057 if (is_empty_string(buffer
))
3058 return log_error_errno(NULL
, EINVAL
, "Empty configuration line");
3060 linep
= line
= strdup(dup
);
3064 line
+= lxc_char_left_gc(line
, strlen(line
));
3066 /* martian option - don't add it to the config itself */
3067 if (!strnequal(line
, "lxc.", 4))
3070 dot
= strchr(line
, '=');
3072 return log_error_errno(NULL
, EINVAL
, "Invalid configuration line: %s", line
);
3078 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3080 value
+= lxc_char_left_gc(value
, strlen(value
));
3081 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3083 if (*value
== '\'' || *value
== '\"') {
3086 len
= strlen(value
);
3087 if (len
> 1 && value
[len
- 1] == *value
) {
3088 value
[len
- 1] = '\0';
3093 new = zalloc(sizeof(struct new_config_item
));
3105 new->key
= move_ptr(k
);
3106 new->val
= move_ptr(v
);
3107 return move_ptr(new);
3110 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
3112 struct parse_line_conf plc
;
3115 return syserror_set(-EINVAL
, "Missing config");
3118 plc
.from_include
= from_include
;
3120 /* Catch only the top level config file name in the structure. */
3122 conf
->rcfile
= strdup(file
);
3124 return lxc_file_for_each_line_mmap(file
, parse_line
, &plc
);
3127 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3129 __do_free
struct lxc_list
*dent
= NULL
;
3131 dent
= lxc_list_new();
3133 return ret_errno(ENOMEM
);
3135 dent
->elem
= parse_new_conf_line(arg
);
3137 return ret_errno(ENOMEM
);
3139 lxc_list_add_tail(defines
, move_ptr(dent
));
3144 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3146 struct lxc_list
*it
;
3149 lxc_list_for_each(it
, defines
) {
3150 struct new_config_item
*new_item
= it
->elem
;
3151 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3156 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3157 lxc_config_define_free(defines
);
3158 #endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
3163 void lxc_config_define_free(struct lxc_list
*defines
)
3165 struct lxc_list
*it
, *next
;
3167 lxc_list_for_each_safe(it
, defines
, next
) {
3168 struct new_config_item
*new_item
= it
->elem
;
3169 free(new_item
->key
);
3170 free(new_item
->val
);
3177 int lxc_config_parse_arch(const char *arch
, signed long *persona
)
3179 static struct per_name
{
3183 { "arm", PER_LINUX32
},
3184 { "armel", PER_LINUX32
},
3185 { "armhf", PER_LINUX32
},
3186 { "armv7l", PER_LINUX32
},
3187 { "athlon", PER_LINUX32
},
3188 { "i386", PER_LINUX32
},
3189 { "i486", PER_LINUX32
},
3190 { "i586", PER_LINUX32
},
3191 { "i686", PER_LINUX32
},
3192 { "linux32", PER_LINUX32
},
3193 { "mips", PER_LINUX32
},
3194 { "mipsel", PER_LINUX32
},
3195 { "ppc", PER_LINUX32
},
3196 { "powerpc", PER_LINUX32
},
3197 { "x86", PER_LINUX32
},
3198 { "aarch64", PER_LINUX
},
3199 { "amd64", PER_LINUX
},
3200 { "arm64", PER_LINUX
},
3201 { "linux64", PER_LINUX
},
3202 { "mips64", PER_LINUX
},
3203 { "mips64el", PER_LINUX
},
3204 { "ppc64", PER_LINUX
},
3205 { "ppc64el", PER_LINUX
},
3206 { "ppc64le", PER_LINUX
},
3207 { "powerpc64", PER_LINUX
},
3208 { "s390x", PER_LINUX
},
3209 { "x86_64", PER_LINUX
},
3212 for (int i
= 0; i
< ARRAY_SIZE(pername
); i
++) {
3213 if (!strequal(pername
[i
].name
, arch
))
3216 *persona
= pername
[i
].per
;
3220 return ret_errno(EINVAL
);
3223 int lxc_fill_elevated_privileges(char *flaglist
, unsigned int *flags
)
3225 unsigned int flags_tmp
= 0;
3231 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3232 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3233 { "LSM", LXC_ATTACH_LSM_EXEC
},
3239 * For the sake of backward compatibility, keep all privileges
3240 * if no specific privileges are specified.
3242 for (unsigned int i
= 0; all_privs
[i
].token
; i
++)
3243 flags_tmp
|= all_privs
[i
].flag
;
3249 lxc_iterate_parts(token
, flaglist
, "|") {
3250 bool valid_token
= false;
3252 for (unsigned int i
= 0; all_privs
[i
].token
; i
++) {
3253 if (!strequal(all_privs
[i
].token
, token
))
3257 flags_tmp
|= all_privs
[i
].flag
;
3261 return syserror_set(-EINVAL
, "Invalid elevated privilege \"%s\" requested", token
);
3268 /* Write out a configuration file. */
3269 int write_config(int fd
, const struct lxc_conf
*conf
)
3272 size_t len
= conf
->unexpanded_len
;
3277 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3279 return log_error_errno(-errno
, errno
, "Failed to write configuration file");
3284 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3287 __do_free
char *tmp
= NULL
;
3291 len
= strlen(key
) + strlen(v
) + 4;
3292 tmp
= must_realloc(NULL
, len
);
3294 if (lxc_config_value_empty(v
))
3295 ret
= strnprintf(tmp
, len
, "%s =", key
);
3297 ret
= strnprintf(tmp
, len
, "%s = %s", key
, v
);
3301 /* Save the line verbatim into unexpanded_conf */
3302 if (append_unexp_config_line(tmp
, conf
))
3308 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3312 char *lstart
= conf
->unexpanded_config
;
3314 if (!conf
->unexpanded_config
)
3318 lend
= strchr(lstart
, '\n');
3322 lend
= lstart
+ strlen(lstart
);
3326 if (!strnequal(lstart
, key
, strlen(key
))) {
3332 v
= lstart
[strlen(key
)];
3333 if (!isspace(v
) && v
!= '=') {
3339 conf
->unexpanded_len
-= (lend
- lstart
);
3341 if (*lend
== '\0') {
3346 memmove(lstart
, lend
, strlen(lend
) + 1);
3350 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3351 const char *newpath
, const char *oldname
,
3352 const char *newname
, const char *ovldir
)
3354 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3355 char *lstart
= conf
->unexpanded_config
;
3356 const char *key
= "lxc.mount.entry";
3359 size_t newdirlen
, olddirlen
;
3361 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3362 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3363 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
, oldname
);
3367 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3368 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3369 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
, newname
);
3373 if (!conf
->unexpanded_config
)
3377 lend
= strchr(lstart
, '\n');
3379 lend
= lstart
+ strlen(lstart
);
3383 if (!strnequal(lstart
, key
, strlen(key
)))
3386 p
= strchr(lstart
+ strlen(key
), '=');
3397 /* Whenever a lxc.mount.entry entry is found in a line we check
3398 * if the substring "overlay" is present before doing any
3399 * further work. We check for "overlay" because substrings need
3400 * to have at least one space before them in a valid overlay
3401 * lxc.mount.entry (/A B overlay). When the space before is
3402 * missing it is very likely that these substrings are part of a
3403 * path or something else. (Checking q >= lend ensures that we
3404 * only count matches in the current line.) */
3405 q
= strstr(p
, " overlay");
3406 if (!q
|| q
>= lend
)
3409 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3412 /* replace the olddir with newdir */
3413 if (olddirlen
>= newdirlen
) {
3414 size_t diff
= olddirlen
- newdirlen
;
3415 memcpy(q
, newdir
, newdirlen
);
3417 if (olddirlen
!= newdirlen
) {
3418 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3419 strlen(q
) - newdirlen
- diff
+ 1);
3421 conf
->unexpanded_len
-= diff
;
3425 size_t diff
= newdirlen
- olddirlen
;
3426 size_t oldlen
= conf
->unexpanded_len
;
3427 size_t newlen
= oldlen
+ diff
;
3428 size_t poffset
= q
- conf
->unexpanded_config
;
3430 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3434 conf
->unexpanded_len
= newlen
;
3435 conf
->unexpanded_alloced
= newlen
+ 1;
3436 new[newlen
- 1] = '\0';
3437 lend
= new + (lend
- conf
->unexpanded_config
);
3439 /* Move over the remainder to make room for the newdir.
3441 memmove(new + poffset
+ newdirlen
,
3442 new + poffset
+ olddirlen
,
3443 oldlen
- poffset
- olddirlen
+ 1);
3444 conf
->unexpanded_config
= new;
3446 memcpy(new + poffset
, newdir
, newdirlen
);
3457 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3458 const char *newpath
, const char *oldname
,
3459 const char *newname
)
3461 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3462 char *lstart
= conf
->unexpanded_config
;
3463 const char *key
= "lxc.hook";
3466 size_t newdirlen
, olddirlen
;
3468 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3469 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3470 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3474 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3475 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3476 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3480 if (!conf
->unexpanded_config
)
3484 lend
= strchr(lstart
, '\n');
3486 lend
= lstart
+ strlen(lstart
);
3490 if (!strnequal(lstart
, key
, strlen(key
)))
3493 p
= strchr(lstart
+ strlen(key
), '=');
3504 if (!strnequal(p
, olddir
, strlen(olddir
)))
3507 /* replace the olddir with newdir */
3508 if (olddirlen
>= newdirlen
) {
3509 size_t diff
= olddirlen
- newdirlen
;
3510 memcpy(p
, newdir
, newdirlen
);
3512 if (olddirlen
!= newdirlen
) {
3513 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3514 strlen(p
) - newdirlen
- diff
+ 1);
3516 conf
->unexpanded_len
-= diff
;
3520 size_t diff
= newdirlen
- olddirlen
;
3521 size_t oldlen
= conf
->unexpanded_len
;
3522 size_t newlen
= oldlen
+ diff
;
3523 size_t poffset
= p
- conf
->unexpanded_config
;
3525 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3529 conf
->unexpanded_len
= newlen
;
3530 conf
->unexpanded_alloced
= newlen
+ 1;
3531 new[newlen
- 1] = '\0';
3532 lend
= new + (lend
- conf
->unexpanded_config
);
3534 /* Move over the remainder to make room for the newdir.
3536 memmove(new + poffset
+ newdirlen
,
3537 new + poffset
+ olddirlen
,
3538 oldlen
- poffset
- olddirlen
+ 1);
3539 conf
->unexpanded_config
= new;
3541 memcpy(new + poffset
, newdir
, newdirlen
);
3555 ERROR("Error writing to new config"); \
3560 /* This is called only from clone. We wish to update all hwaddrs in the
3561 * unexpanded config file. We can't/don't want to update any which come from
3562 * lxc.includes (there shouldn't be any).
3563 * We can't just walk the c->lxc-conf->network list because that includes netifs
3564 * from the include files. So we update the ones which we find in the unexp
3565 * config file, then find the original macaddr in the conf->network, and update
3566 * that to the same value.
3568 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3570 char *lend
, *p
, *p2
;
3571 char *lstart
= conf
->unexpanded_config
;
3573 if (!conf
->unexpanded_config
)
3577 char newhwaddr
[18], oldhwaddr
[17];
3578 struct lxc_netdev
*netdev
;
3580 lend
= strchr(lstart
, '\n');
3582 lend
= lstart
+ strlen(lstart
);
3586 if (!lxc_config_net_is_hwaddr(lstart
)) {
3591 p
= strchr(lstart
, '=');
3604 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3607 if ((p2
- p
) != 17) {
3608 WARN("Bad hwaddr entry");
3613 memcpy(oldhwaddr
, p
, 17);
3615 if (!new_hwaddr(newhwaddr
))
3618 memcpy(p
, newhwaddr
, 17);
3619 list_for_each_entry(netdev
, &conf
->netdevs
, head
) {
3620 if (netdev
->hwaddr
&& memcmp(oldhwaddr
, netdev
->hwaddr
, 17) == 0)
3621 memcpy(netdev
->hwaddr
, newhwaddr
, 17);
3630 static int set_config_ephemeral(const char *key
, const char *value
,
3631 struct lxc_conf
*lxc_conf
, void *data
)
3635 if (lxc_config_value_empty(value
)) {
3636 lxc_conf
->ephemeral
= 0;
3640 ret
= lxc_safe_uint(value
, &lxc_conf
->ephemeral
);
3644 if (lxc_conf
->ephemeral
> 1)
3645 return ret_errno(EINVAL
);
3650 static int set_config_log_syslog(const char *key
, const char *value
,
3651 struct lxc_conf
*lxc_conf
, void *data
)
3655 if (lxc_conf
->syslog
)
3656 free_disarm(lxc_conf
->syslog
);
3658 if (lxc_config_value_empty(value
))
3661 facility
= lxc_syslog_priority_to_int(value
);
3662 if (facility
== -EINVAL
)
3663 return ret_errno(EINVAL
);
3665 lxc_log_syslog(facility
);
3667 return set_config_string_item(&lxc_conf
->syslog
, value
);
3670 static int set_config_no_new_privs(const char *key
, const char *value
,
3671 struct lxc_conf
*lxc_conf
, void *data
)
3676 if (lxc_config_value_empty(value
)) {
3677 lxc_conf
->no_new_privs
= false;
3681 ret
= lxc_safe_uint(value
, &v
);
3686 return ret_errno(EINVAL
);
3688 lxc_conf
->no_new_privs
= v
? true : false;
3693 /* Callbacks to get configuration items. */
3694 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3695 struct lxc_conf
*c
, void *data
)
3702 memset(retv
, 0, inlen
);
3706 switch (c
->personality
) {
3708 strprint(retv
, inlen
, "i686");
3711 strprint(retv
, inlen
, "x86_64");
3720 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3721 struct lxc_conf
*c
, void *data
)
3723 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3726 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3727 struct lxc_conf
*c
, void *data
)
3729 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3732 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3733 struct lxc_conf
*c
, void *data
)
3735 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3738 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3739 struct lxc_conf
*c
, void *data
)
3742 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3744 return syserror_set(-EINVAL
, "Built without AppArmor support");
3748 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3749 int inlen
, struct lxc_conf
*c
,
3753 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_incomplete
);
3755 return syserror_set(-EINVAL
, "Built without AppArmor support");
3759 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3760 int inlen
, struct lxc_conf
*c
,
3764 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_nesting
);
3766 return syserror_set(-EINVAL
, "Built without AppArmor support");
3770 static int get_config_apparmor_raw(const char *key
, char *retv
,
3771 int inlen
, struct lxc_conf
*c
,
3776 struct lxc_list
*it
;
3782 memset(retv
, 0, inlen
);
3784 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3785 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3790 return syserror_set(-EINVAL
, "Built without AppArmor support");
3794 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3795 struct lxc_conf
*c
, void *data
)
3798 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3800 return syserror_set(-EINVAL
, "Built without SELinux support");
3804 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3805 struct lxc_conf
*c
, void *data
)
3808 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3810 return syserror_set(-EINVAL
, "Built without SELinux support");
3814 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3815 struct lxc_conf
*c
, void *data
)
3817 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3821 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3822 * just the value(s) will be printed. Since there still could be more than one,
3823 * it is newline-separated.
3824 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3826 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3827 * 'lxc.cgroup.subsystem.key = value' format.
3829 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3830 int inlen
, struct lxc_conf
*c
,
3834 bool get_all
= false;
3836 size_t namespaced_token_len
;
3837 char *global_token
, *namespaced_token
;
3838 struct list_head
*list
;
3839 struct lxc_cgroup
*cgroup
;
3844 memset(retv
, 0, inlen
);
3846 if (version
== CGROUP2_SUPER_MAGIC
) {
3847 global_token
= "lxc.cgroup2";
3848 namespaced_token
= "lxc.cgroup2.";
3849 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3851 } else if (version
== CGROUP_SUPER_MAGIC
) {
3852 global_token
= "lxc.cgroup";
3853 namespaced_token
= "lxc.cgroup.";
3854 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3857 return ret_errno(EINVAL
);
3860 if (strequal(key
, global_token
))
3862 else if (strnequal(key
, namespaced_token
, namespaced_token_len
))
3863 key
+= namespaced_token_len
;
3865 return ret_errno(EINVAL
);
3867 list_for_each_entry(cgroup
, list
, head
) {
3869 if (version
!= cgroup
->version
)
3872 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3873 cgroup
->subsystem
, cgroup
->value
);
3874 } else if (strequal(cgroup
->subsystem
, key
)) {
3875 strprint(retv
, inlen
, "%s\n", cgroup
->value
);
3882 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3883 struct lxc_conf
*c
, void *data
)
3885 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3886 CGROUP_SUPER_MAGIC
);
3889 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3890 struct lxc_conf
*c
, void *data
)
3892 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3893 CGROUP2_SUPER_MAGIC
);
3896 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3897 struct lxc_conf
*lxc_conf
, void *data
)
3902 if (!strequal(key
, "lxc.cgroup.dir"))
3903 return ret_errno(EINVAL
);
3908 memset(retv
, 0, inlen
);
3910 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3915 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3916 struct lxc_conf
*lxc_conf
, void *data
)
3924 memset(retv
, 0, inlen
);
3926 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3931 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3932 struct lxc_conf
*lxc_conf
, void *data
)
3940 memset(retv
, 0, inlen
);
3942 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3947 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3949 struct lxc_conf
*lxc_conf
,
3958 memset(retv
, 0, inlen
);
3960 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3965 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3967 struct lxc_conf
*lxc_conf
,
3976 memset(retv
, 0, inlen
);
3978 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3983 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3984 int inlen
, struct lxc_conf
*lxc_conf
,
3987 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3988 lxc_conf
->cgroup_meta
.relative
);
3991 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3992 struct lxc_conf
*c
, void *data
)
3995 int len
, listlen
, ret
;
3997 /* "u 1000 1000000 65536"
3999 * let's render this as
4017 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
4018 char buf
[__LXC_IDMAP_STR_BUF
];
4023 memset(retv
, 0, inlen
);
4025 listlen
= list_len(&c
->id_map
);
4026 list_for_each_entry(map
, &c
->id_map
, head
) {
4027 ret
= strnprintf(buf
, sizeof(buf
), "%c %lu %lu %lu",
4028 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
4029 map
->nsid
, map
->hostid
, map
->range
);
4031 return ret_errno(EIO
);
4033 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
4039 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
4040 struct lxc_conf
*c
, void *data
)
4043 v
= lxc_log_priority_to_string(c
->loglevel
);
4044 return lxc_get_conf_str(retv
, inlen
, v
);
4047 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
4048 struct lxc_conf
*c
, void *data
)
4050 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
4053 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
4054 struct lxc_conf
*c
, void *data
)
4056 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
4059 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
4060 struct lxc_conf
*c
, void *data
)
4062 int len
, fulllen
= 0;
4063 const char *sep
= "";
4068 memset(retv
, 0, inlen
);
4070 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
4073 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
4074 case LXC_AUTO_PROC_MIXED
:
4075 strprint(retv
, inlen
, "%sproc:mixed", sep
);
4078 case LXC_AUTO_PROC_RW
:
4079 strprint(retv
, inlen
, "%sproc:rw", sep
);
4086 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
4087 case LXC_AUTO_SYS_RO
:
4088 strprint(retv
, inlen
, "%ssys:ro", sep
);
4091 case LXC_AUTO_SYS_RW
:
4092 strprint(retv
, inlen
, "%ssys:rw", sep
);
4095 case LXC_AUTO_SYS_MIXED
:
4096 strprint(retv
, inlen
, "%ssys:mixed", sep
);
4103 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
4104 case LXC_AUTO_CGROUP_NOSPEC
:
4105 strprint(retv
, inlen
, "%scgroup", sep
);
4107 case LXC_AUTO_CGROUP_MIXED
:
4108 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
4110 case LXC_AUTO_CGROUP_RO
:
4111 strprint(retv
, inlen
, "%scgroup:ro", sep
);
4113 case LXC_AUTO_CGROUP_RW
:
4114 strprint(retv
, inlen
, "%scgroup:rw", sep
);
4116 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
4117 strprint(retv
, inlen
, "%scgroup-full", sep
);
4119 case LXC_AUTO_CGROUP_FULL_MIXED
:
4120 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
4122 case LXC_AUTO_CGROUP_FULL_RO
:
4123 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
4125 case LXC_AUTO_CGROUP_FULL_RW
:
4126 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
4135 static int get_config_mount(const char *key
, char *retv
, int inlen
,
4136 struct lxc_conf
*c
, void *data
)
4138 int len
, fulllen
= 0;
4139 struct lxc_list
*it
;
4144 memset(retv
, 0, inlen
);
4146 lxc_list_for_each(it
, &c
->mount_list
) {
4147 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4153 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4154 struct lxc_conf
*c
, void *data
)
4156 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4159 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4160 struct lxc_conf
*c
, void *data
)
4162 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4165 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4166 struct lxc_conf
*c
, void *data
)
4168 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4171 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4172 struct lxc_conf
*c
, void *data
)
4174 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mnt_opts
.raw_options
);
4177 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4178 struct lxc_conf
*c
, void *data
)
4180 return lxc_get_conf_str(
4182 c
->utsname
? c
->utsname
->nodename
: NULL
);
4185 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4186 struct lxc_conf
*c
, void *data
)
4189 int len
, fulllen
= 0, found
= -1;
4190 struct lxc_list
*it
;
4193 subkey
= strchr(key
, '.');
4195 return ret_errno(EINVAL
);
4197 subkey
= strchr(subkey
+ 1, '.');
4199 return ret_errno(EINVAL
);
4201 if (*subkey
== '\0')
4202 return ret_errno(EINVAL
);
4204 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4205 if (strequal(lxchook_names
[i
], subkey
)) {
4212 return ret_errno(EINVAL
);
4217 memset(retv
, 0, inlen
);
4219 lxc_list_for_each(it
, &c
->hooks
[found
]) {
4220 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4226 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4227 struct lxc_conf
*c
, void *data
)
4229 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4232 static int get_config_net(const char *key
, char *retv
, int inlen
,
4233 struct lxc_conf
*c
, void *data
)
4235 int len
, fulllen
= 0;
4236 struct lxc_netdev
*netdev
;
4241 memset(retv
, 0, inlen
);
4243 list_for_each_entry(netdev
, &c
->netdevs
, head
) {
4244 const char *t
= lxc_net_type_to_str(netdev
->type
);
4245 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4251 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4252 struct lxc_conf
*c
, void *data
)
4254 int len
, fulllen
= 0;
4255 struct cap_entry
*cap
;
4260 memset(retv
, 0, inlen
);
4262 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4263 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4269 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4270 struct lxc_conf
*c
, void *data
)
4272 int len
, fulllen
= 0;
4273 struct cap_entry
*cap
;
4278 memset(retv
, 0, inlen
);
4280 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4281 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4287 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4288 struct lxc_conf
*c
, void *data
)
4290 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4293 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4294 struct lxc_conf
*c
, void *data
)
4296 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4299 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4300 struct lxc_conf
*c
, void *data
)
4302 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4306 static int get_config_console_buffer_size(const char *key
, char *retv
,
4307 int inlen
, struct lxc_conf
*c
,
4310 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4313 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4314 struct lxc_conf
*c
, void *data
)
4316 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4319 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4320 int inlen
, struct lxc_conf
*c
,
4324 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4326 return ret_errno(ENOSYS
);
4330 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4331 struct lxc_conf
*c
, void *data
)
4333 #ifdef HAVE_SECCOMP_NOTIFY
4334 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4336 return ret_errno(ENOSYS
);
4340 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4341 struct lxc_conf
*c
, void *data
)
4343 #ifdef HAVE_SECCOMP_NOTIFY
4344 return lxc_get_conf_str(retv
, inlen
,
4345 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4346 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4347 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4349 return ret_errno(ENOSYS
);
4353 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4354 struct lxc_conf
*c
, void *data
)
4357 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4359 return ret_errno(ENOSYS
);
4363 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4364 struct lxc_conf
*c
, void *data
)
4366 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4369 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4370 struct lxc_conf
*c
, void *data
)
4372 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4375 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4376 struct lxc_conf
*c
, void *data
)
4378 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4381 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4382 struct lxc_conf
*c
, void *data
)
4384 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4387 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4388 struct lxc_conf
*c
, void *data
)
4390 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4393 static int get_config_start(const char *key
, char *retv
, int inlen
,
4394 struct lxc_conf
*c
, void *data
)
4396 if (strequal(key
+ 10, "auto"))
4397 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4398 else if (strequal(key
+ 10, "delay"))
4399 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4400 else if (strequal(key
+ 10, "order"))
4401 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4403 return ret_errno(EINVAL
);
4406 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4407 struct lxc_conf
*c
, void *data
)
4409 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4412 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4413 struct lxc_conf
*c
, void *data
)
4415 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4418 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4419 int inlen
, struct lxc_conf
*c
,
4422 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4425 static int get_config_group(const char *key
, char *retv
, int inlen
,
4426 struct lxc_conf
*c
, void *data
)
4428 int len
, fulllen
= 0;
4429 struct lxc_list
*it
;
4434 memset(retv
, 0, inlen
);
4436 lxc_list_for_each(it
, &c
->groups
) {
4437 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4443 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4444 struct lxc_conf
*c
, void *data
)
4446 int len
, fulllen
= 0;
4447 struct environment_entry
*env
;
4452 memset(retv
, 0, inlen
);
4454 list_for_each_entry(env
, &c
->environment
, head
) {
4455 strprint(retv
, inlen
, "%s=%s\n", env
->key
, env
->val
);
4461 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4462 struct lxc_conf
*c
, void *data
)
4464 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4467 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4468 struct lxc_conf
*c
, void *data
)
4470 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4473 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4474 struct lxc_conf
*c
, void *data
)
4476 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4479 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4480 struct lxc_conf
*c
, void *data
)
4482 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4485 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4486 struct lxc_conf
*c
, void *data
)
4488 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4491 static int get_config_init_groups(const char *key
, char *retv
, int inlen
,
4492 struct lxc_conf
*c
, void *data
)
4494 int fulllen
= 0, len
;
4499 memset(retv
, 0, inlen
);
4501 if (c
->init_groups
.size
== 0)
4504 for (int i
= 0; i
< c
->init_groups
.size
; i
++)
4505 strprint(retv
, inlen
, "%s%d", (i
> 0) ? "," : "",
4506 c
->init_groups
.list
[i
]);
4511 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4512 struct lxc_conf
*c
, void *data
)
4514 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4517 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4518 struct lxc_conf
*c
, void *data
)
4520 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4523 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4524 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4525 * printed, in 'lxc.prlimit.resource = value' format.
4527 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4528 struct lxc_conf
*c
, void *data
)
4530 int fulllen
= 0, len
;
4531 bool get_all
= false;
4532 struct lxc_limit
*lim
;
4537 memset(retv
, 0, inlen
);
4539 if (strequal(key
, "lxc.prlimit"))
4541 else if (strnequal(key
, "lxc.prlimit.", 12))
4544 return ret_errno(EINVAL
);
4546 list_for_each_entry(lim
, &c
->limits
, head
) {
4547 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4548 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4551 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4552 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4553 partlen
= STRLITERALLEN("unlimited");
4555 partlen
= sprintf(buf
, "%" PRIu64
,
4556 (uint64_t)lim
->limit
.rlim_cur
);
4559 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4560 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4561 memcpy(buf
+ partlen
, ":unlimited",
4562 STRLITERALLEN(":unlimited") + 1);
4564 sprintf(buf
+ partlen
, ":%" PRIu64
,
4565 (uint64_t)lim
->limit
.rlim_max
);
4569 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4570 lim
->resource
, buf
);
4571 } else if (strequal(lim
->resource
, key
)) {
4572 strprint(retv
, inlen
, "%s", buf
);
4579 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4580 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4581 * entries will be printed, in 'lxc.sysctl.key = value' format.
4583 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4584 struct lxc_conf
*c
, void *data
)
4587 bool get_all
= false;
4589 struct lxc_sysctl
*sysctl
;
4594 memset(retv
, 0, inlen
);
4596 if (strequal(key
, "lxc.sysctl"))
4598 else if (strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4599 key
+= STRLITERALLEN("lxc.sysctl.");
4601 return ret_errno(EINVAL
);
4603 list_for_each_entry(sysctl
, &c
->sysctls
, head
) {
4605 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", sysctl
->key
,
4607 } else if (strequal(sysctl
->key
, key
)) {
4608 strprint(retv
, inlen
, "%s", sysctl
->value
);
4615 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4616 struct lxc_conf
*c
, void *data
)
4619 bool get_all
= false;
4621 struct lxc_proc
*proc
;
4626 memset(retv
, 0, inlen
);
4628 if (strequal(key
, "lxc.proc"))
4630 else if (strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4631 key
+= STRLITERALLEN("lxc.proc.");
4633 return ret_errno(EINVAL
);
4635 list_for_each_entry(proc
, &c
->procs
, head
) {
4637 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4638 proc
->filename
, proc
->value
);
4639 } else if (strequal(proc
->filename
, key
)) {
4640 strprint(retv
, inlen
, "%s", proc
->value
);
4647 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4648 struct lxc_conf
*c
, void *data
)
4656 memset(retv
, 0, inlen
);
4658 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4659 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4660 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4666 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4667 struct lxc_conf
*c
, void *data
)
4675 memset(retv
, 0, inlen
);
4677 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4678 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4679 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4685 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4694 memset(retv
, 0, inlen
);
4696 if (c
->timens
.s_boot
) {
4697 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4699 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4705 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4706 struct lxc_conf
*c
, void *data
)
4714 memset(retv
, 0, inlen
);
4716 if (c
->timens
.s_monotonic
) {
4717 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4719 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4725 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4726 struct lxc_conf
*c
, void *data
)
4729 const char *namespace;
4735 memset(retv
, 0, inlen
);
4737 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4738 if (is_empty_string(namespace))
4739 return ret_errno(EINVAL
);
4741 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4745 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4750 /* Callbacks to clear config items. */
4751 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4754 c
->personality
= -1;
4758 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4765 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4772 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4775 free_disarm(c
->ttys
.dir
);
4779 static inline int clr_config_apparmor_profile(const char *key
,
4780 struct lxc_conf
*c
, void *data
)
4783 free_disarm(c
->lsm_aa_profile
);
4786 return syserror_set(-EINVAL
, "Built without AppArmor support");
4790 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4795 c
->lsm_aa_allow_incomplete
= 0;
4798 return syserror_set(-EINVAL
, "Built without AppArmor support");
4802 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4807 c
->lsm_aa_allow_nesting
= 0;
4810 return syserror_set(-EINVAL
, "Built without AppArmor support");
4814 static inline int clr_config_apparmor_raw(const char *key
,
4819 return lxc_clear_apparmor_raw(c
);
4821 return syserror_set(-EINVAL
, "Built without AppArmor support");
4825 static inline int clr_config_selinux_context(const char *key
,
4826 struct lxc_conf
*c
, void *data
)
4829 free_disarm(c
->lsm_se_context
);
4832 return syserror_set(-EINVAL
, "Built without SELinux support");
4836 static inline int clr_config_selinux_context_keyring(const char *key
,
4837 struct lxc_conf
*c
, void *data
)
4840 free_disarm(c
->lsm_se_keyring_context
);
4843 return syserror_set(-EINVAL
, "Built without SELinux support");
4847 static inline int clr_config_keyring_session(const char *key
,
4848 struct lxc_conf
*c
, void *data
)
4850 c
->keyring_disable_session
= false;
4854 static inline int clr_config_cgroup_controller(const char *key
,
4855 struct lxc_conf
*c
, void *data
)
4857 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4860 static inline int clr_config_cgroup2_controller(const char *key
,
4861 struct lxc_conf
*c
, void *data
)
4863 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4866 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4869 if (!strequal(key
, "lxc.cgroup.dir"))
4870 return ret_errno(EINVAL
);
4872 if (lxc_conf
->cgroup_meta
.dir
)
4873 free_disarm(lxc_conf
->cgroup_meta
.dir
);
4878 static int clr_config_cgroup_monitor_dir(const char *key
,
4879 struct lxc_conf
*lxc_conf
,
4882 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4886 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4887 struct lxc_conf
*lxc_conf
,
4890 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4894 static int clr_config_cgroup_container_dir(const char *key
,
4895 struct lxc_conf
*lxc_conf
,
4898 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4902 static int clr_config_cgroup_container_inner_dir(const char *key
,
4903 struct lxc_conf
*lxc_conf
,
4906 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4910 static inline int clr_config_cgroup_relative(const char *key
,
4911 struct lxc_conf
*lxc_conf
,
4914 lxc_conf
->cgroup_meta
.relative
= false;
4918 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4921 return lxc_clear_idmaps(c
);
4924 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4927 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4931 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4934 free_disarm(c
->logfile
);
4938 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4941 return lxc_clear_mount_entries(c
);
4944 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4947 return lxc_clear_automounts(c
);
4950 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4953 free_disarm(c
->fstab
);
4957 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4960 free_disarm(c
->rootfs
.path
);
4964 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4967 c
->rootfs
.managed
= true;
4971 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4974 free_disarm(c
->rootfs
.mount
);
4978 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4981 put_lxc_mount_options(&c
->rootfs
.mnt_opts
);
4986 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4989 free_disarm(c
->utsname
);
4993 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4996 return lxc_clear_hooks(c
, key
);
4999 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
5002 /* default to legacy hooks version */
5003 c
->hooks_version
= 0;
5007 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
5010 lxc_free_networks(c
);
5015 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
5018 return lxc_clear_config_caps(c
);
5021 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
5024 return lxc_clear_config_caps(c
);
5027 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
5030 free_disarm(c
->console
.path
);
5034 static inline int clr_config_console_logfile(const char *key
,
5035 struct lxc_conf
*c
, void *data
)
5037 free_disarm(c
->console
.log_path
);
5041 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
5044 c
->console
.log_rotate
= 0;
5048 static inline int clr_config_console_buffer_size(const char *key
,
5049 struct lxc_conf
*c
, void *data
)
5051 c
->console
.buffer_size
= 0;
5055 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
5058 c
->console
.log_size
= 0;
5062 static inline int clr_config_seccomp_allow_nesting(const char *key
,
5063 struct lxc_conf
*c
, void *data
)
5066 c
->seccomp
.allow_nesting
= 0;
5069 return ret_errno(ENOSYS
);
5073 static inline int clr_config_seccomp_notify_cookie(const char *key
,
5074 struct lxc_conf
*c
, void *data
)
5076 #ifdef HAVE_SECCOMP_NOTIFY
5077 free_disarm(c
->seccomp
.notifier
.cookie
);
5080 return ret_errno(ENOSYS
);
5084 static inline int clr_config_seccomp_notify_proxy(const char *key
,
5085 struct lxc_conf
*c
, void *data
)
5087 #ifdef HAVE_SECCOMP_NOTIFY
5088 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
5089 sizeof(c
->seccomp
.notifier
.proxy_addr
));
5092 return ret_errno(ENOSYS
);
5096 static inline int clr_config_seccomp_profile(const char *key
,
5097 struct lxc_conf
*c
, void *data
)
5099 free_disarm(c
->seccomp
.seccomp
);
5103 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
5110 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
5113 c
->autodevtmpfssize
= 500000;
5117 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
5124 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
5127 c
->rebootsignal
= 0;
5131 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
5138 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
5141 if (strequal(key
+ 10, "auto"))
5143 else if (strequal(key
+ 10, "delay"))
5145 else if (strequal(key
+ 10, "order"))
5151 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
5154 free_disarm(c
->syslog
);
5158 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
5161 c
->monitor_unshare
= 0;
5165 static inline int clr_config_monitor_signal_pdeath(const char *key
,
5166 struct lxc_conf
*c
, void *data
)
5168 c
->monitor_signal_pdeath
= 0;
5172 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
5175 return lxc_clear_groups(c
);
5178 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5181 return lxc_clear_environment(c
);
5184 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5187 free_disarm(c
->execute_cmd
);
5191 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5194 free_disarm(c
->init_cmd
);
5198 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5201 free_disarm(c
->init_cwd
);
5205 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5212 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5219 static inline int clr_config_init_groups(const char *key
, struct lxc_conf
*c
,
5222 c
->init_groups
.size
= 0;
5223 free_disarm(c
->init_groups
.list
);
5227 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5234 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5237 c
->no_new_privs
= false;
5241 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5244 return lxc_clear_limits(c
, key
);
5247 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5250 return lxc_clear_sysctls(c
, key
);
5253 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5256 return lxc_clear_procs(c
, key
);
5259 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5265 static int clr_config_namespace_clone(const char *key
,
5266 struct lxc_conf
*lxc_conf
, void *data
)
5268 lxc_conf
->ns_clone
= 0;
5272 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5275 lxc_conf
->ns_keep
= 0;
5279 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5281 lxc_conf
->timens
.s_boot
= 0;
5282 lxc_conf
->timens
.ns_boot
= 0;
5286 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5288 lxc_conf
->timens
.s_monotonic
= 0;
5289 lxc_conf
->timens
.ns_monotonic
= 0;
5293 static int clr_config_namespace_share(const char *key
,
5294 struct lxc_conf
*lxc_conf
, void *data
)
5297 const char *namespace;
5299 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5300 if (is_empty_string(namespace))
5301 return ret_errno(EINVAL
);
5303 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5307 free(lxc_conf
->ns_share
[ns_idx
]);
5308 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5313 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5314 struct lxc_conf
*c
, void *data
)
5316 return ret_errno(ENOSYS
);
5319 struct config_net_info
{
5320 char buf
[NETWORK_SUBKEY_SIZE_MAX
];
5322 const struct lxc_config_net_t
*ops
;
5323 struct lxc_netdev
*netdev
;
5326 static int get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
,
5327 struct config_net_info
*info
, bool allocate
)
5332 const char *idx_start
;
5334 if (is_empty_string(key
))
5335 return ret_errno(EINVAL
);
5337 /* check that this is a sensible network key */
5338 if (!strnequal("lxc.net.", key
, STRLITERALLEN("lxc.net.")))
5339 return syserror_set(-EINVAL
, "Invalid network configuration key \"%s\"", key
);
5342 /* beginning of index string */
5343 idx_start
= key
+ STRLITERALLEN("lxc.net.");
5344 if (!isdigit(*idx_start
))
5345 return syserror_set(-EINVAL
, "Failed to detect digit in string \"%s\"", key
+ 8);
5347 ret
= lxc_safe_int64_residual(idx_start
, &tmpidx
, 10, info
->buf
, sizeof(info
->buf
));
5349 return syserror("Failed to parse network index");
5351 if (tmpidx
< 0 || tmpidx
>= INT_MAX
)
5352 return syserror_set(-ERANGE
, "Number of configured networks would overflow the counter");
5353 idx
= (unsigned int)tmpidx
;
5355 info
->netdev
= lxc_get_netdev_by_idx(lxc_conf
, idx
, allocate
);
5357 return ret_errno(EINVAL
);
5359 /* Make sure subkey points to the empty string. */
5360 info
->subkey
= info
->buf
;
5361 if (is_empty_string(info
->subkey
))
5362 return ret_errno(ENOENT
);
5364 if (info
->subkey
[0] != '.')
5365 return syserror_set(-EINVAL
, "Invalid subkey");
5368 /* lxc.net.<idx>.<subkey> */
5369 info
->ops
= lxc_get_config_net(info
->subkey
);
5370 if (info
->ops
== &unsupported_config_net_key
)
5371 return syserror_set(-ENOENT
, "Unknown network configuration key \"%s\"", key
);
5376 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5377 * found. So we make sure next comes an integer, find the right callback (by
5378 * rewriting the key), and call it.
5380 static int set_config_jump_table_net(const char *key
, const char *value
,
5381 struct lxc_conf
*lxc_conf
, void *data
)
5383 struct config_net_info info
= {};
5385 const char *idxstring
;
5387 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5388 if (!isdigit(*idxstring
))
5389 return ret_errno(EINVAL
);
5391 if (lxc_config_value_empty(value
))
5392 return clr_config_jump_table_net(key
, lxc_conf
, data
);
5394 ret
= get_network_config_ops(key
, lxc_conf
, &info
, true);
5398 return info
.ops
->set(info
.subkey
, value
, lxc_conf
, info
.netdev
);
5401 static int clr_config_jump_table_net(const char *key
, struct lxc_conf
*lxc_conf
,
5404 struct config_net_info info
= {};
5406 const char *idxstring
;
5408 idxstring
= key
+ 8;
5409 if (!isdigit(*idxstring
))
5410 return ret_errno(EINVAL
);
5412 /* The left conjunct is pretty self-explanatory. The right conjunct
5413 * checks whether the two pointers are equal. If they are we know that
5414 * this is not a key that is namespaced any further and so we are
5415 * supposed to clear the whole network.
5417 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5418 unsigned int rmnetdevidx
;
5420 ret
= lxc_safe_uint(idxstring
, &rmnetdevidx
);
5424 /* Remove network from network list. */
5425 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5429 ret
= get_network_config_ops(key
, lxc_conf
, &info
, false);
5433 return info
.ops
->clr(info
.subkey
, lxc_conf
, info
.netdev
);
5436 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5439 struct lxc_netdev
*netdev
= data
;
5442 return ret_errno(EINVAL
);
5444 lxc_clear_netdev(netdev
);
5449 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5452 struct lxc_netdev
*netdev
= data
;
5455 return ret_errno(EINVAL
);
5457 netdev
->name
[0] = '\0';
5462 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5465 struct lxc_netdev
*netdev
= data
;
5468 return ret_errno(EINVAL
);
5475 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5478 struct lxc_netdev
*netdev
= data
;
5481 return ret_errno(EINVAL
);
5483 netdev
->link
[0] = '\0';
5488 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5491 struct lxc_netdev
*netdev
= data
;
5494 return ret_errno(EINVAL
);
5496 netdev
->l2proxy
= false;
5501 static int clr_config_net_macvlan_mode(const char *key
,
5502 struct lxc_conf
*lxc_conf
, void *data
)
5504 struct lxc_netdev
*netdev
= data
;
5507 return ret_errno(EINVAL
);
5509 if (netdev
->type
!= LXC_NET_MACVLAN
)
5512 netdev
->priv
.macvlan_attr
.mode
= -1;
5517 static int clr_config_net_ipvlan_mode(const char *key
,
5518 struct lxc_conf
*lxc_conf
, void *data
)
5520 struct lxc_netdev
*netdev
= data
;
5523 return ret_errno(EINVAL
);
5525 if (netdev
->type
!= LXC_NET_IPVLAN
)
5528 netdev
->priv
.ipvlan_attr
.mode
= -1;
5533 static int clr_config_net_ipvlan_isolation(const char *key
,
5534 struct lxc_conf
*lxc_conf
, void *data
)
5536 struct lxc_netdev
*netdev
= data
;
5539 return ret_errno(EINVAL
);
5541 if (netdev
->type
!= LXC_NET_IPVLAN
)
5544 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5549 static int clr_config_net_veth_mode(const char *key
,
5550 struct lxc_conf
*lxc_conf
, void *data
)
5552 struct lxc_netdev
*netdev
= data
;
5555 return ret_errno(EINVAL
);
5557 if (netdev
->type
!= LXC_NET_VETH
)
5560 netdev
->priv
.veth_attr
.mode
= -1;
5565 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5568 struct lxc_netdev
*netdev
= data
;
5571 return ret_errno(EINVAL
);
5573 if (netdev
->type
!= LXC_NET_VETH
)
5576 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5581 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5584 struct lxc_netdev
*netdev
= data
;
5587 return ret_errno(EINVAL
);
5589 if (netdev
->type
!= LXC_NET_VETH
)
5592 netdev
->priv
.veth_attr
.vlan_id
= 0;
5593 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5598 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5599 struct lxc_conf
*lxc_conf
, void *data
)
5601 struct lxc_netdev
*netdev
= data
;
5602 struct lxc_list
*cur
, *next
;
5605 return ret_errno(EINVAL
);
5607 if (netdev
->type
!= LXC_NET_VETH
)
5610 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5619 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5622 struct lxc_netdev
*netdev
= data
;
5625 return ret_errno(EINVAL
);
5627 free_disarm(netdev
->upscript
);
5632 static int clr_config_net_script_down(const char *key
,
5633 struct lxc_conf
*lxc_conf
, void *data
)
5635 struct lxc_netdev
*netdev
= data
;
5638 return ret_errno(EINVAL
);
5640 free_disarm(netdev
->downscript
);
5645 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5648 struct lxc_netdev
*netdev
= data
;
5651 return ret_errno(EINVAL
);
5653 free_disarm(netdev
->hwaddr
);
5658 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5661 struct lxc_netdev
*netdev
= data
;
5664 return ret_errno(EINVAL
);
5666 free_disarm(netdev
->mtu
);
5671 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5674 struct lxc_netdev
*netdev
= data
;
5677 return ret_errno(EINVAL
);
5679 if (netdev
->type
!= LXC_NET_VLAN
)
5682 netdev
->priv
.vlan_attr
.vid
= 0;
5687 static int clr_config_net_ipv4_gateway(const char *key
,
5688 struct lxc_conf
*lxc_conf
, void *data
)
5690 struct lxc_netdev
*netdev
= data
;
5693 return ret_errno(EINVAL
);
5695 free_disarm(netdev
->ipv4_gateway
);
5700 static int clr_config_net_ipv4_address(const char *key
,
5701 struct lxc_conf
*lxc_conf
, void *data
)
5703 struct lxc_netdev
*netdev
= data
;
5704 struct lxc_inetdev
*inetdev
, *ninetdev
;
5707 return ret_errno(EINVAL
);
5709 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->ipv4_addresses
, head
) {
5710 list_del(&inetdev
->head
);
5717 static int clr_config_net_veth_ipv4_route(const char *key
,
5718 struct lxc_conf
*lxc_conf
, void *data
)
5720 struct lxc_netdev
*netdev
= data
;
5721 struct lxc_inetdev
*inetdev
, *ninetdev
;
5724 return ret_errno(EINVAL
);
5726 if (netdev
->type
!= LXC_NET_VETH
)
5729 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
5730 list_del(&inetdev
->head
);
5737 static int clr_config_net_ipv6_gateway(const char *key
,
5738 struct lxc_conf
*lxc_conf
, void *data
)
5740 struct lxc_netdev
*netdev
= data
;
5743 return ret_errno(EINVAL
);
5745 free_disarm(netdev
->ipv6_gateway
);
5750 static int clr_config_net_ipv6_address(const char *key
,
5751 struct lxc_conf
*lxc_conf
, void *data
)
5753 struct lxc_netdev
*netdev
= data
;
5754 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5757 return ret_errno(EINVAL
);
5759 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->ipv6_addresses
, head
) {
5760 list_del(&inet6dev
->head
);
5767 static int clr_config_net_veth_ipv6_route(const char *key
,
5768 struct lxc_conf
*lxc_conf
, void *data
)
5770 struct lxc_netdev
*netdev
= data
;
5771 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5774 return ret_errno(EINVAL
);
5776 if (netdev
->type
!= LXC_NET_VETH
)
5779 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
5780 list_del(&inet6dev
->head
);
5787 static int get_config_jump_table_net(const char *key
, char *retv
, int inlen
,
5788 struct lxc_conf
*c
, void *data
)
5790 struct config_net_info info
= {};
5792 const char *idxstring
;
5794 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5795 if (!isdigit(*idxstring
))
5796 return ret_errno(EINVAL
);
5798 ret
= get_network_config_ops(key
, c
, &info
, false);
5802 return info
.ops
->get(info
.subkey
, retv
, inlen
, c
, info
.netdev
);
5805 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5806 struct lxc_conf
*c
, void *data
)
5810 struct lxc_netdev
*netdev
= data
;
5813 return ret_errno(EINVAL
);
5818 memset(retv
, 0, inlen
);
5820 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5825 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5826 struct lxc_conf
*c
, void *data
)
5830 struct lxc_netdev
*netdev
= data
;
5833 return ret_errno(EINVAL
);
5838 memset(retv
, 0, inlen
);
5840 if (netdev
->flags
& IFF_UP
)
5841 strprint(retv
, inlen
, "up");
5846 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5847 struct lxc_conf
*c
, void *data
)
5851 struct lxc_netdev
*netdev
= data
;
5854 return ret_errno(EINVAL
);
5859 memset(retv
, 0, inlen
);
5861 if (netdev
->link
[0] != '\0')
5862 strprint(retv
, inlen
, "%s", netdev
->link
);
5867 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5868 struct lxc_conf
*c
, void *data
)
5870 struct lxc_netdev
*netdev
= data
;
5873 return ret_errno(EINVAL
);
5875 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5878 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5879 struct lxc_conf
*c
, void *data
)
5883 struct lxc_netdev
*netdev
= data
;
5886 return ret_errno(EINVAL
);
5891 memset(retv
, 0, inlen
);
5893 if (netdev
->name
[0] != '\0')
5894 strprint(retv
, inlen
, "%s", netdev
->name
);
5899 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5900 struct lxc_conf
*c
, void *data
)
5905 struct lxc_netdev
*netdev
= data
;
5908 return ret_errno(EINVAL
);
5910 if (netdev
->type
!= LXC_NET_MACVLAN
)
5911 return ret_errno(EINVAL
);
5916 memset(retv
, 0, inlen
);
5918 switch (netdev
->priv
.macvlan_attr
.mode
) {
5919 case MACVLAN_MODE_PRIVATE
:
5922 case MACVLAN_MODE_VEPA
:
5925 case MACVLAN_MODE_BRIDGE
:
5928 case MACVLAN_MODE_PASSTHRU
:
5936 strprint(retv
, inlen
, "%s", mode
);
5941 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5942 struct lxc_conf
*c
, void *data
)
5945 struct lxc_netdev
*netdev
= data
;
5950 return ret_errno(EINVAL
);
5952 if (netdev
->type
!= LXC_NET_IPVLAN
)
5953 return ret_errno(EINVAL
);
5958 memset(retv
, 0, inlen
);
5960 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5961 case IPVLAN_MODE_L3
:
5964 case IPVLAN_MODE_L3S
:
5967 case IPVLAN_MODE_L2
:
5975 strprint(retv
, inlen
, "%s", mode
);
5980 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5981 struct lxc_conf
*c
, void *data
)
5984 struct lxc_netdev
*netdev
= data
;
5989 return ret_errno(EINVAL
);
5991 if (netdev
->type
!= LXC_NET_IPVLAN
)
5992 return ret_errno(EINVAL
);
5997 memset(retv
, 0, inlen
);
5999 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
6000 case IPVLAN_ISOLATION_BRIDGE
:
6003 case IPVLAN_ISOLATION_PRIVATE
:
6006 case IPVLAN_ISOLATION_VEPA
:
6014 strprint(retv
, inlen
, "%s", mode
);
6019 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
6020 struct lxc_conf
*c
, void *data
)
6023 struct lxc_netdev
*netdev
= data
;
6028 return ret_errno(EINVAL
);
6030 if (netdev
->type
!= LXC_NET_VETH
)
6031 return ret_errno(EINVAL
);
6036 memset(retv
, 0, inlen
);
6038 switch (netdev
->priv
.veth_attr
.mode
) {
6039 case VETH_MODE_BRIDGE
:
6042 case VETH_MODE_ROUTER
:
6050 strprint(retv
, inlen
, "%s", mode
);
6055 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
6056 struct lxc_conf
*c
, void *data
)
6060 struct lxc_netdev
*netdev
= data
;
6063 return ret_errno(EINVAL
);
6065 if (netdev
->type
!= LXC_NET_VETH
)
6066 return ret_errno(EINVAL
);
6071 memset(retv
, 0, inlen
);
6073 strprint(retv
, inlen
, "%s",
6074 netdev
->priv
.veth_attr
.pair
[0] != '\0'
6075 ? netdev
->priv
.veth_attr
.pair
6076 : netdev
->priv
.veth_attr
.veth1
);
6081 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
6082 struct lxc_conf
*c
, void *data
)
6086 struct lxc_netdev
*netdev
= data
;
6089 return ret_errno(EINVAL
);
6091 if (netdev
->type
!= LXC_NET_VETH
)
6092 return ret_errno(EINVAL
);
6097 memset(retv
, 0, inlen
);
6099 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
6104 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
,
6105 int inlen
, struct lxc_conf
*c
,
6110 struct lxc_list
*it
;
6112 struct lxc_netdev
*netdev
= data
;
6115 return ret_errno(EINVAL
);
6117 if (netdev
->type
!= LXC_NET_VETH
)
6118 return ret_errno(EINVAL
);
6123 memset(retv
, 0, inlen
);
6125 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
6127 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
6128 unsigned short i
= PTR_TO_USHORT(it
->elem
);
6129 strprint(retv
, inlen
, "%u%s", i
, (listlen
-- > 1) ? "\n" : "");
6135 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6136 struct lxc_conf
*c
, void *data
)
6140 struct lxc_netdev
*netdev
= data
;
6143 return ret_errno(EINVAL
);
6148 memset(retv
, 0, inlen
);
6150 if (netdev
->upscript
)
6151 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6156 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6157 struct lxc_conf
*c
, void *data
)
6161 struct lxc_netdev
*netdev
= data
;
6164 return ret_errno(EINVAL
);
6169 memset(retv
, 0, inlen
);
6171 if (netdev
->downscript
)
6172 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6177 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6178 struct lxc_conf
*c
, void *data
)
6182 struct lxc_netdev
*netdev
= data
;
6185 return ret_errno(EINVAL
);
6190 memset(retv
, 0, inlen
);
6193 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6198 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6199 struct lxc_conf
*c
, void *data
)
6203 struct lxc_netdev
*netdev
= data
;
6206 return ret_errno(EINVAL
);
6211 memset(retv
, 0, inlen
);
6214 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6219 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6220 struct lxc_conf
*c
, void *data
)
6224 struct lxc_netdev
*netdev
= data
;
6227 return ret_errno(EINVAL
);
6229 if (netdev
->type
!= LXC_NET_VLAN
)
6230 return ret_errno(EINVAL
);
6235 memset(retv
, 0, inlen
);
6237 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6242 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6243 struct lxc_conf
*c
, void *data
)
6246 char buf
[INET_ADDRSTRLEN
];
6248 struct lxc_netdev
*netdev
= data
;
6251 return ret_errno(EINVAL
);
6256 memset(retv
, 0, inlen
);
6258 if (netdev
->ipv4_gateway_auto
) {
6259 strprint(retv
, inlen
, "auto");
6260 } else if (netdev
->ipv4_gateway_dev
) {
6261 strprint(retv
, inlen
, "dev");
6262 } else if (netdev
->ipv4_gateway
) {
6263 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
)))
6265 strprint(retv
, inlen
, "%s", buf
);
6271 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6272 struct lxc_conf
*c
, void *data
)
6275 struct lxc_netdev
*netdev
= data
;
6278 char buf
[INET_ADDRSTRLEN
];
6279 struct lxc_inetdev
*inetdev
;
6282 return ret_errno(EINVAL
);
6287 memset(retv
, 0, inlen
);
6289 listlen
= list_len(&netdev
->ipv4_addresses
);
6291 list_for_each_entry(inetdev
, &netdev
->ipv4_addresses
, head
) {
6292 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6294 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6295 (listlen
-- > 1) ? "\n" : "");
6301 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6302 struct lxc_conf
*c
, void *data
)
6306 char buf
[INET_ADDRSTRLEN
];
6307 struct lxc_inetdev
*inetdev
;
6309 struct lxc_netdev
*netdev
= data
;
6312 return ret_errno(EINVAL
);
6314 if (netdev
->type
!= LXC_NET_VETH
)
6315 return ret_errno(EINVAL
);
6320 memset(retv
, 0, inlen
);
6322 listlen
= list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
6323 list_for_each_entry(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
6324 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6326 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6327 (listlen
-- > 1) ? "\n" : "");
6333 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6334 struct lxc_conf
*c
, void *data
)
6337 char buf
[INET6_ADDRSTRLEN
];
6339 struct lxc_netdev
*netdev
= data
;
6342 return ret_errno(EINVAL
);
6347 memset(retv
, 0, inlen
);
6349 if (netdev
->ipv6_gateway_auto
) {
6350 strprint(retv
, inlen
, "auto");
6351 } else if (netdev
->ipv6_gateway_dev
) {
6352 strprint(retv
, inlen
, "dev");
6353 } else if (netdev
->ipv6_gateway
) {
6354 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
)))
6356 strprint(retv
, inlen
, "%s", buf
);
6362 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6363 struct lxc_conf
*c
, void *data
)
6367 char buf
[INET6_ADDRSTRLEN
];
6369 struct lxc_netdev
*netdev
= data
;
6370 struct lxc_inet6dev
*inet6dev
;
6373 return ret_errno(EINVAL
);
6378 memset(retv
, 0, inlen
);
6380 listlen
= list_len(&netdev
->ipv6_addresses
);
6381 list_for_each_entry(inet6dev
, &netdev
->ipv6_addresses
, head
) {
6382 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6384 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6385 (listlen
-- > 1) ? "\n" : "");
6391 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6392 struct lxc_conf
*c
, void *data
)
6396 char buf
[INET6_ADDRSTRLEN
];
6397 struct lxc_inet6dev
*inet6dev
;
6399 struct lxc_netdev
*netdev
= data
;
6402 return ret_errno(EINVAL
);
6404 if (netdev
->type
!= LXC_NET_VETH
)
6405 return ret_errno(EINVAL
);
6410 memset(retv
, 0, inlen
);
6412 listlen
= list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
6413 list_for_each_entry(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
6414 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6416 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6417 (listlen
-- > 1) ? "\n" : "");
6423 int lxc_list_config_items(char *retv
, int inlen
)
6432 memset(retv
, 0, inlen
);
6434 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
6435 char *s
= config_jump_table
[i
].name
;
6437 if (s
[strlen(s
) - 1] == '.')
6440 strprint(retv
, inlen
, "%s\n", s
);
6446 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6455 memset(retv
, 0, inlen
);
6457 if (strequal(key
, "lxc.apparmor")) {
6458 strprint(retv
, inlen
, "allow_incomplete\n");
6459 strprint(retv
, inlen
, "allow_nesting\n");
6460 strprint(retv
, inlen
, "profile\n");
6461 strprint(retv
, inlen
, "raw\n");
6462 } else if (strequal(key
, "lxc.cgroup")) {
6463 strprint(retv
, inlen
, "dir\n");
6464 } else if (strequal(key
, "lxc.selinux")) {
6465 strprint(retv
, inlen
, "context\n");
6466 strprint(retv
, inlen
, "context.keyring\n");
6467 } else if (strequal(key
, "lxc.mount")) {
6468 strprint(retv
, inlen
, "auto\n");
6469 strprint(retv
, inlen
, "entry\n");
6470 strprint(retv
, inlen
, "fstab\n");
6471 } else if (strequal(key
, "lxc.rootfs")) {
6472 strprint(retv
, inlen
, "mount\n");
6473 strprint(retv
, inlen
, "options\n");
6474 strprint(retv
, inlen
, "path\n");
6475 } else if (strequal(key
, "lxc.uts")) {
6476 strprint(retv
, inlen
, "name\n");
6477 } else if (strequal(key
, "lxc.hook")) {
6478 strprint(retv
, inlen
, "autodev\n");
6479 strprint(retv
, inlen
, "autodevtmpfssize\n");
6480 strprint(retv
, inlen
, "clone\n");
6481 strprint(retv
, inlen
, "destroy\n");
6482 strprint(retv
, inlen
, "mount\n");
6483 strprint(retv
, inlen
, "post-stop\n");
6484 strprint(retv
, inlen
, "pre-mount\n");
6485 strprint(retv
, inlen
, "pre-start\n");
6486 strprint(retv
, inlen
, "start-host\n");
6487 strprint(retv
, inlen
, "start\n");
6488 strprint(retv
, inlen
, "stop\n");
6489 } else if (strequal(key
, "lxc.cap")) {
6490 strprint(retv
, inlen
, "drop\n");
6491 strprint(retv
, inlen
, "keep\n");
6492 } else if (strequal(key
, "lxc.console")) {
6493 strprint(retv
, inlen
, "logfile\n");
6494 strprint(retv
, inlen
, "path\n");
6495 } else if (strequal(key
, "lxc.seccomp")) {
6496 strprint(retv
, inlen
, "profile\n");
6497 } else if (strequal(key
, "lxc.signal")) {
6498 strprint(retv
, inlen
, "halt\n");
6499 strprint(retv
, inlen
, "reboot\n");
6500 strprint(retv
, inlen
, "stop\n");
6501 } else if (strequal(key
, "lxc.start")) {
6502 strprint(retv
, inlen
, "auto\n");
6503 strprint(retv
, inlen
, "delay\n");
6504 strprint(retv
, inlen
, "order\n");
6505 } else if (strequal(key
, "lxc.monitor")) {
6506 strprint(retv
, inlen
, "unshare\n");
6507 } else if (strequal(key
, "lxc.keyring")) {
6508 strprint(retv
, inlen
, "session\n");
6510 fulllen
= ret_errno(EINVAL
);
6516 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6518 struct config_net_info info
= {};
6519 struct lxc_netdev
*netdev
;
6521 const char *idxstring
;
6524 idxstring
= key
+ STRLITERALLEN("lxc.net.");
6525 if (!isdigit(*idxstring
))
6526 return ret_errno(EINVAL
);
6528 ret
= get_network_config_ops(key
, c
, &info
, false);
6531 return ret_errno(EINVAL
);
6533 netdev
= info
.netdev
;
6538 memset(retv
, 0, inlen
);
6540 strprint(retv
, inlen
, "type\n");
6541 strprint(retv
, inlen
, "script.up\n");
6542 strprint(retv
, inlen
, "script.down\n");
6544 if (netdev
->type
!= LXC_NET_EMPTY
) {
6545 strprint(retv
, inlen
, "flags\n");
6546 strprint(retv
, inlen
, "link\n");
6547 strprint(retv
, inlen
, "name\n");
6548 strprint(retv
, inlen
, "hwaddr\n");
6549 strprint(retv
, inlen
, "mtu\n");
6550 strprint(retv
, inlen
, "ipv6.address\n");
6551 strprint(retv
, inlen
, "ipv6.gateway\n");
6552 strprint(retv
, inlen
, "ipv4.address\n");
6553 strprint(retv
, inlen
, "ipv4.gateway\n");
6556 switch (netdev
->type
) {
6558 strprint(retv
, inlen
, "veth.pair\n");
6559 strprint(retv
, inlen
, "veth.ipv4.route\n");
6560 strprint(retv
, inlen
, "veth.ipv6.route\n");
6561 strprint(retv
, inlen
, "veth.vlan.id\n");
6563 case LXC_NET_MACVLAN
:
6564 strprint(retv
, inlen
, "macvlan.mode\n");
6566 case LXC_NET_IPVLAN
:
6567 strprint(retv
, inlen
, "ipvlan.mode\n");
6568 strprint(retv
, inlen
, "ipvlan.isolation\n");
6571 strprint(retv
, inlen
, "vlan.id\n");