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 "netns_ifaddrs.h"
34 #include "lxcseccomp.h"
36 #include "memory_utils.h"
39 #include "storage/storage.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 __do_free
struct string_entry
*entry
;
1118 entry
= zalloc(sizeof(struct string_entry
));
1120 return ret_errno(ENOMEM
);
1122 entry
->val
= move_ptr(val
);
1123 list_add_tail(&entry
->head
, &lxc_conf
->hooks
[which
]);
1129 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1130 struct lxc_conf
*lxc_conf
, void *data
)
1133 if (lxc_config_value_empty(value
))
1134 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1136 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1139 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1140 return ret_errno(EINVAL
);
1144 return ret_errno(ENOSYS
);
1148 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1149 struct lxc_conf
*lxc_conf
, void *data
)
1151 #ifdef HAVE_SECCOMP_NOTIFY
1152 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1154 return ret_errno(ENOSYS
);
1158 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1159 struct lxc_conf
*lxc_conf
, void *data
)
1161 #ifdef HAVE_SECCOMP_NOTIFY
1164 if (lxc_config_value_empty(value
))
1165 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1167 if (!strnequal(value
, "unix:", 5))
1168 return ret_errno(EINVAL
);
1171 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1176 return ret_errno(ENOSYS
);
1180 static int set_config_seccomp_profile(const char *key
, const char *value
,
1181 struct lxc_conf
*lxc_conf
, void *data
)
1184 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1186 return ret_errno(ENOSYS
);
1190 static int set_config_execute_cmd(const char *key
, const char *value
,
1191 struct lxc_conf
*lxc_conf
, void *data
)
1193 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1196 static int set_config_init_cmd(const char *key
, const char *value
,
1197 struct lxc_conf
*lxc_conf
, void *data
)
1199 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1202 static int set_config_init_cwd(const char *key
, const char *value
,
1203 struct lxc_conf
*lxc_conf
, void *data
)
1205 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1208 static int set_config_init_uid(const char *key
, const char *value
,
1209 struct lxc_conf
*lxc_conf
, void *data
)
1211 unsigned int init_uid
;
1213 if (lxc_config_value_empty(value
)) {
1214 lxc_conf
->init_uid
= 0;
1218 if (lxc_safe_uint(value
, &init_uid
) < 0)
1221 lxc_conf
->init_uid
= init_uid
;
1226 static int set_config_init_gid(const char *key
, const char *value
,
1227 struct lxc_conf
*lxc_conf
, void *data
)
1229 unsigned int init_gid
;
1231 if (lxc_config_value_empty(value
)) {
1232 lxc_conf
->init_gid
= 0;
1236 if (lxc_safe_uint(value
, &init_gid
) < 0)
1239 lxc_conf
->init_gid
= init_gid
;
1244 static int set_config_init_groups(const char *key
, const char *value
,
1245 struct lxc_conf
*lxc_conf
, void *data
)
1247 __do_free
char *value_dup
= NULL
;
1248 gid_t
*init_groups
= NULL
;
1249 size_t num_groups
= 0;
1253 if (lxc_config_value_empty(value
))
1254 return clr_config_init_groups(key
, lxc_conf
, NULL
);
1256 value_dup
= strdup(value
);
1260 lxc_iterate_parts(token
, value_dup
, ",")
1263 if (num_groups
== INT_MAX
)
1264 return log_error_errno(-ERANGE
, ERANGE
, "Excessive number of supplementary groups specified");
1266 /* This means the string wasn't empty and all we found was garbage. */
1267 if (num_groups
== 0)
1268 return log_error_errno(-EINVAL
, EINVAL
, "No valid groups specified %s", value
);
1270 idx
= lxc_conf
->init_groups
.size
;
1271 init_groups
= realloc(lxc_conf
->init_groups
.list
, sizeof(gid_t
) * (idx
+ num_groups
));
1273 return ret_errno(ENOMEM
);
1276 * Once the realloc() succeeded we need to hand control of the memory
1277 * back to the config otherwise we risk a double-free when
1278 * lxc_conf_free() is called.
1280 lxc_conf
->init_groups
.list
= init_groups
;
1282 /* Restore duplicated value so we can call lxc_iterate_parts() again. */
1283 strcpy(value_dup
, value
);
1285 lxc_iterate_parts(token
, value_dup
, ",") {
1290 ret
= lxc_safe_uint(token
, &group
);
1292 return log_error_errno(ret
, -ret
, "Failed to parse group %s", token
);
1294 init_groups
[idx
++] = group
;
1297 lxc_conf
->init_groups
.size
+= num_groups
;
1302 static int set_config_hooks(const char *key
, const char *value
,
1303 struct lxc_conf
*lxc_conf
, void *data
)
1305 __do_free
char *copy
= NULL
;
1307 if (lxc_config_value_empty(value
))
1308 return lxc_clear_hooks(lxc_conf
, key
);
1310 if (strequal(key
+ 4, "hook"))
1311 return log_error_errno(-EINVAL
, EINVAL
, "lxc.hook must not have a value");
1313 copy
= strdup(value
);
1315 return ret_errno(ENOMEM
);
1317 if (strequal(key
+ 9, "pre-start"))
1318 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, move_ptr(copy
));
1319 else if (strequal(key
+ 9, "start-host"))
1320 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, move_ptr(copy
));
1321 else if (strequal(key
+ 9, "pre-mount"))
1322 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, move_ptr(copy
));
1323 else if (strequal(key
+ 9, "autodev"))
1324 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, move_ptr(copy
));
1325 else if (strequal(key
+ 9, "mount"))
1326 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, move_ptr(copy
));
1327 else if (strequal(key
+ 9, "start"))
1328 return add_hook(lxc_conf
, LXCHOOK_START
, move_ptr(copy
));
1329 else if (strequal(key
+ 9, "stop"))
1330 return add_hook(lxc_conf
, LXCHOOK_STOP
, move_ptr(copy
));
1331 else if (strequal(key
+ 9, "post-stop"))
1332 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, move_ptr(copy
));
1333 else if (strequal(key
+ 9, "clone"))
1334 return add_hook(lxc_conf
, LXCHOOK_CLONE
, move_ptr(copy
));
1335 else if (strequal(key
+ 9, "destroy"))
1336 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, move_ptr(copy
));
1338 return ret_errno(EINVAL
);
1341 static int set_config_hooks_version(const char *key
, const char *value
,
1342 struct lxc_conf
*lxc_conf
, void *data
)
1347 if (lxc_config_value_empty(value
))
1348 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1350 ret
= lxc_safe_uint(value
, &tmp
);
1355 return syserror_set(-EINVAL
, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
1357 lxc_conf
->hooks_version
= tmp
;
1362 static int set_config_personality(const char *key
, const char *value
,
1363 struct lxc_conf
*lxc_conf
, void *data
)
1366 personality_t personality
;
1368 ret
= lxc_config_parse_arch(value
, &personality
);
1370 return syserror("Unsupported personality \"%s\"", value
);
1372 lxc_conf
->personality
= personality
;
1376 static int set_config_pty_max(const char *key
, const char *value
,
1377 struct lxc_conf
*lxc_conf
, void *data
)
1380 unsigned int max
= 0;
1382 if (lxc_config_value_empty(value
)) {
1383 lxc_conf
->pty_max
= 0;
1387 ret
= lxc_safe_uint(value
, &max
);
1389 return ret_errno(EINVAL
);
1391 lxc_conf
->pty_max
= max
;
1396 /* We only need to check whether the first byte of the key after the lxc.start.
1397 * prefix matches our expectations since they fortunately all start with a
1398 * different letter. If anything was wrong with the key we would have already
1399 * noticed when the callback was called.
1401 static int set_config_start(const char *key
, const char *value
,
1402 struct lxc_conf
*lxc_conf
, void *data
)
1407 is_empty
= lxc_config_value_empty(value
);
1409 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1411 lxc_conf
->start_auto
= 0;
1415 ret
= lxc_safe_uint(value
, &lxc_conf
->start_auto
);
1419 if (lxc_conf
->start_auto
> 1)
1420 return ret_errno(EINVAL
);
1423 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1425 lxc_conf
->start_delay
= 0;
1429 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1430 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1432 lxc_conf
->start_order
= 0;
1436 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1439 return ret_errno(EINVAL
);
1442 static int set_config_monitor(const char *key
, const char *value
,
1443 struct lxc_conf
*lxc_conf
, void *data
)
1445 if (lxc_config_value_empty(value
)) {
1446 lxc_conf
->monitor_unshare
= 0;
1450 if (strequal(key
+ 12, "unshare"))
1451 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1453 return ret_errno(EINVAL
);
1456 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1457 struct lxc_conf
*lxc_conf
, void *data
)
1459 if (lxc_config_value_empty(value
)) {
1460 lxc_conf
->monitor_signal_pdeath
= 0;
1464 if (strequal(key
+ 12, "signal.pdeath")) {
1467 sig_n
= sig_parse(value
);
1469 return ret_errno(EINVAL
);
1471 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1475 return ret_errno(EINVAL
);
1478 static int set_config_group(const char *key
, const char *value
,
1479 struct lxc_conf
*lxc_conf
, void *data
)
1481 __do_free
char *groups
= NULL
;
1484 if (lxc_config_value_empty(value
))
1485 return lxc_clear_groups(lxc_conf
);
1487 groups
= strdup(value
);
1489 return ret_errno(ENOMEM
);
1492 * In case several groups are specified in a single line split these
1493 * groups in a single element for the list.
1495 lxc_iterate_parts(token
, groups
, " \t") {
1496 __do_free
char *val
= NULL
;
1497 __do_free
struct string_entry
*entry
= NULL
;
1499 entry
= zalloc(sizeof(struct string_entry
));
1501 return ret_errno(ENOMEM
);
1503 val
= strdup(token
);
1505 return ret_errno(ENOMEM
);
1507 entry
->val
= move_ptr(val
);
1508 list_add_tail(&entry
->head
, &lxc_conf
->groups
);
1515 static int set_config_environment(const char *key
, const char *value
,
1516 struct lxc_conf
*lxc_conf
, void *data
)
1518 __do_free
char *dup
= NULL
, *val
= NULL
;
1519 __do_free
struct environment_entry
*new_env
= NULL
;
1522 if (lxc_config_value_empty(value
))
1523 return lxc_clear_environment(lxc_conf
);
1525 new_env
= zalloc(sizeof(struct environment_entry
));
1527 return ret_errno(ENOMEM
);
1529 dup
= strdup(value
);
1531 return ret_errno(ENOMEM
);
1533 env_val
= strchr(dup
, '=');
1535 env_val
= getenv(dup
);
1541 return ret_errno(ENOENT
);
1543 val
= strdup(env_val
);
1545 return ret_errno(ENOMEM
);
1547 new_env
->key
= move_ptr(dup
);
1548 new_env
->val
= move_ptr(val
);
1550 list_add_tail(&new_env
->head
, &lxc_conf
->environment
);
1556 static int set_config_tty_max(const char *key
, const char *value
,
1557 struct lxc_conf
*lxc_conf
, void *data
)
1560 unsigned int nbtty
= 0;
1562 if (lxc_config_value_empty(value
)) {
1563 lxc_conf
->ttys
.max
= 0;
1567 ret
= lxc_safe_uint(value
, &nbtty
);
1571 lxc_conf
->ttys
.max
= nbtty
;
1576 static int set_config_tty_dir(const char *key
, const char *value
,
1577 struct lxc_conf
*lxc_conf
, void *data
)
1579 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1583 static int set_config_apparmor_profile(const char *key
, const char *value
,
1584 struct lxc_conf
*lxc_conf
, void *data
)
1587 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1589 return syserror_set(-EINVAL
, "Built without AppArmor support");
1593 static int set_config_apparmor_allow_incomplete(const char *key
,
1595 struct lxc_conf
*lxc_conf
,
1601 if (lxc_config_value_empty(value
)) {
1602 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1606 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
);
1610 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1611 return ret_errno(EINVAL
);
1615 return syserror_set(-EINVAL
, "Built without AppArmor support");
1619 static int set_config_apparmor_allow_nesting(const char *key
,
1621 struct lxc_conf
*lxc_conf
,
1627 if (lxc_config_value_empty(value
))
1628 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1630 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
);
1634 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1635 return ret_errno(EINVAL
);
1639 return syserror_set(-EINVAL
, "Built without AppArmor support");
1643 static int set_config_apparmor_raw(const char *key
,
1645 struct lxc_conf
*lxc_conf
,
1649 __do_free
char *elem
= NULL
;
1650 __do_free
struct string_entry
*entry
= NULL
;
1652 if (lxc_config_value_empty(value
))
1653 return lxc_clear_apparmor_raw(lxc_conf
);
1655 entry
= zalloc(sizeof(struct string_entry
));
1657 return ret_errno(ENOMEM
);
1659 elem
= strdup(value
);
1661 return ret_errno(ENOMEM
);
1663 entry
->val
= move_ptr(elem
);
1664 list_add_tail(&entry
->head
, &lxc_conf
->lsm_aa_raw
);
1669 return syserror_set(-EINVAL
, "Built without AppArmor support");
1673 static int set_config_selinux_context(const char *key
, const char *value
,
1674 struct lxc_conf
*lxc_conf
, void *data
)
1677 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1679 return syserror_set(-EINVAL
, "Built without SELinux support");
1683 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1684 struct lxc_conf
*lxc_conf
, void *data
)
1687 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1689 return syserror_set(-EINVAL
, "Built without SELinux support");
1693 static int set_config_keyring_session(const char *key
, const char *value
,
1694 struct lxc_conf
*lxc_conf
, void *data
)
1696 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1699 static int set_config_log_file(const char *key
, const char *value
,
1700 struct lxc_conf
*c
, void *data
)
1704 if (lxc_config_value_empty(value
)) {
1705 free_disarm(c
->logfile
);
1710 * Store these values in the lxc_conf, and then try to set for actual
1713 ret
= set_config_path_item(&c
->logfile
, value
);
1715 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1720 static int set_config_log_level(const char *key
, const char *value
,
1721 struct lxc_conf
*lxc_conf
, void *data
)
1725 if (lxc_config_value_empty(value
)) {
1726 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1730 if (value
[0] >= '0' && value
[0] <= '9') {
1733 ret
= lxc_safe_int(value
, &newlevel
);
1735 return ret_errno(EINVAL
);
1737 newlevel
= lxc_log_priority_to_int(value
);
1741 * Store these values in the lxc_conf, and then try to set for actual
1744 lxc_conf
->loglevel
= newlevel
;
1746 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1749 static int set_config_autodev(const char *key
, const char *value
,
1750 struct lxc_conf
*lxc_conf
, void *data
)
1754 if (lxc_config_value_empty(value
)) {
1755 lxc_conf
->autodev
= 0;
1759 ret
= lxc_safe_uint(value
, &lxc_conf
->autodev
);
1761 return ret_errno(EINVAL
);
1763 if (lxc_conf
->autodev
> 1)
1764 return ret_errno(EINVAL
);
1769 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1770 struct lxc_conf
*lxc_conf
, void *data
)
1772 if (lxc_config_value_empty(value
)) {
1773 lxc_conf
->autodevtmpfssize
= 500000;
1777 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1778 lxc_conf
->autodevtmpfssize
= 500000;
1783 static int set_config_signal_halt(const char *key
, const char *value
,
1784 struct lxc_conf
*lxc_conf
, void *data
)
1788 if (lxc_config_value_empty(value
)) {
1789 lxc_conf
->haltsignal
= 0;
1793 sig_n
= sig_parse(value
);
1795 return ret_errno(EINVAL
);
1797 lxc_conf
->haltsignal
= sig_n
;
1802 static int set_config_signal_reboot(const char *key
, const char *value
,
1803 struct lxc_conf
*lxc_conf
, void *data
)
1807 if (lxc_config_value_empty(value
)) {
1808 lxc_conf
->rebootsignal
= 0;
1812 sig_n
= sig_parse(value
);
1814 return ret_errno(EINVAL
);
1816 lxc_conf
->rebootsignal
= sig_n
;
1821 static int set_config_signal_stop(const char *key
, const char *value
,
1822 struct lxc_conf
*lxc_conf
, void *data
)
1826 if (lxc_config_value_empty(value
)) {
1827 lxc_conf
->stopsignal
= 0;
1831 sig_n
= sig_parse(value
);
1833 return ret_errno(EINVAL
);
1835 lxc_conf
->stopsignal
= sig_n
;
1840 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1841 struct lxc_conf
*lxc_conf
, int version
)
1843 call_cleaner(free_lxc_cgroup
) struct lxc_cgroup
*new_cgroup
= NULL
;
1844 const char *subkey
, *token
;
1847 if (lxc_config_value_empty(value
))
1848 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1850 if (version
== CGROUP2_SUPER_MAGIC
) {
1851 token
= "lxc.cgroup2.";
1853 } else if (version
== CGROUP_SUPER_MAGIC
) {
1854 token
= "lxc.cgroup.";
1857 return ret_errno(EINVAL
);
1860 if (!strnequal(key
, token
, token_len
))
1861 return ret_errno(EINVAL
);
1863 subkey
= key
+ token_len
;
1864 if (*subkey
== '\0')
1865 return ret_errno(EINVAL
);
1867 new_cgroup
= zalloc(sizeof(*new_cgroup
));
1869 return ret_errno(ENOMEM
);
1871 new_cgroup
->subsystem
= strdup(subkey
);
1872 if (!new_cgroup
->subsystem
)
1873 return ret_errno(ENOMEM
);
1875 new_cgroup
->value
= strdup(value
);
1876 if (!new_cgroup
->value
)
1877 return ret_errno(ENOMEM
);
1879 new_cgroup
->version
= version
;
1881 if (version
== CGROUP2_SUPER_MAGIC
)
1882 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup2
);
1884 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup
);
1885 move_ptr(new_cgroup
);
1890 static int set_config_cgroup_controller(const char *key
, const char *value
,
1891 struct lxc_conf
*lxc_conf
, void *data
)
1893 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1894 CGROUP_SUPER_MAGIC
);
1897 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1898 struct lxc_conf
*lxc_conf
, void *data
)
1900 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1901 CGROUP2_SUPER_MAGIC
);
1904 static int set_config_cgroup_dir(const char *key
, const char *value
,
1905 struct lxc_conf
*lxc_conf
, void *data
)
1907 if (!strequal(key
, "lxc.cgroup.dir"))
1908 return ret_errno(EINVAL
);
1910 if (lxc_config_value_empty(value
))
1911 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1914 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1917 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1919 return set_config_path_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1922 static int set_config_cgroup_monitor_dir(const char *key
, const char *value
,
1923 struct lxc_conf
*lxc_conf
, void *data
)
1925 if (lxc_config_value_empty(value
))
1926 return clr_config_cgroup_monitor_dir(key
, lxc_conf
, NULL
);
1929 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1932 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1934 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_dir
, value
);
1937 static int set_config_cgroup_monitor_pivot_dir(const char *key
, const char *value
,
1938 struct lxc_conf
*lxc_conf
, void *data
)
1940 if (lxc_config_value_empty(value
))
1941 return clr_config_cgroup_monitor_pivot_dir(key
, lxc_conf
, NULL
);
1944 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1947 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1949 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_pivot_dir
, value
);
1952 static int set_config_cgroup_container_dir(const char *key
, const char *value
,
1953 struct lxc_conf
*lxc_conf
,
1956 if (lxc_config_value_empty(value
))
1957 return clr_config_cgroup_container_dir(key
, lxc_conf
, NULL
);
1960 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1963 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1965 return set_config_path_item(&lxc_conf
->cgroup_meta
.container_dir
, value
);
1968 static int set_config_cgroup_container_inner_dir(const char *key
,
1970 struct lxc_conf
*lxc_conf
,
1973 if (lxc_config_value_empty(value
))
1974 return clr_config_cgroup_container_inner_dir(key
, lxc_conf
, NULL
);
1977 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1979 if (strchr(value
, '/') || strequal(value
, ".") || strequal(value
, ".."))
1980 return log_error_errno(-EINVAL
, EINVAL
, "lxc.cgroup.dir.container.inner must be a single directory name");
1982 return set_config_string_item(&lxc_conf
->cgroup_meta
.namespace_dir
, value
);
1985 static int set_config_cgroup_relative(const char *key
, const char *value
,
1986 struct lxc_conf
*lxc_conf
, void *data
)
1988 unsigned int converted
;
1991 if (lxc_config_value_empty(value
))
1992 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1994 ret
= lxc_safe_uint(value
, &converted
);
1998 if (converted
== 1) {
1999 lxc_conf
->cgroup_meta
.relative
= true;
2003 if (converted
== 0) {
2004 lxc_conf
->cgroup_meta
.relative
= false;
2008 return ret_errno(EINVAL
);
2011 static bool parse_limit_value(const char **value
, rlim_t
*res
)
2013 char *endptr
= NULL
;
2015 if (strnequal(*value
, "unlimited", STRLITERALLEN("unlimited"))) {
2016 *res
= RLIM_INFINITY
;
2017 *value
+= STRLITERALLEN("unlimited");
2022 *res
= strtoull(*value
, &endptr
, 10);
2023 if (errno
|| !endptr
)
2031 static int set_config_prlimit(const char *key
, const char *value
,
2032 struct lxc_conf
*lxc_conf
, void *data
)
2034 call_cleaner(free_lxc_limit
) struct lxc_limit
*new_lim
= NULL
;
2035 struct rlimit limit
;
2037 struct lxc_limit
*lim
;
2039 if (lxc_config_value_empty(value
))
2040 return lxc_clear_limits(lxc_conf
, key
);
2042 if (!strnequal(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
2043 return ret_errno(EINVAL
);
2045 key
+= STRLITERALLEN("lxc.prlimit.");
2047 /* soft limit comes first in the value */
2048 if (!parse_limit_value(&value
, &limit_value
))
2049 return ret_errno(EINVAL
);
2051 limit
.rlim_cur
= limit_value
;
2053 /* skip spaces and a colon */
2054 while (isspace(*value
))
2059 else if (*value
) /* any other character is an error here */
2060 return ret_errno(EINVAL
);
2062 while (isspace(*value
))
2065 /* optional hard limit */
2067 if (!parse_limit_value(&value
, &limit_value
))
2068 return ret_errno(EINVAL
);
2070 limit
.rlim_max
= limit_value
;
2072 /* check for trailing garbage */
2073 while (isspace(*value
))
2077 return ret_errno(EINVAL
);
2079 /* a single value sets both hard and soft limit */
2080 limit
.rlim_max
= limit
.rlim_cur
;
2083 /* find existing list element */
2084 list_for_each_entry(lim
, &lxc_conf
->limits
, head
) {
2085 if (!strequal(key
, lim
->resource
))
2092 new_lim
= zalloc(sizeof(*new_lim
));
2094 return ret_errno(ENOMEM
);
2096 new_lim
->resource
= strdup(key
);
2097 if (!new_lim
->resource
)
2098 return ret_errno(ENOMEM
);
2100 new_lim
->limit
= limit
;
2101 list_add_tail(&new_lim
->head
, &lxc_conf
->limits
);
2107 static int set_config_sysctl(const char *key
, const char *value
,
2108 struct lxc_conf
*lxc_conf
, void *data
)
2110 call_cleaner(free_lxc_sysctl
) struct lxc_sysctl
*sysctl_elem
= NULL
;
2111 struct lxc_sysctl
*sysctl
, *nsysctl
;
2113 if (lxc_config_value_empty(value
))
2114 return clr_config_sysctl(key
, lxc_conf
, NULL
);
2116 if (!strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
2117 return ret_errno(EINVAL
);
2119 key
+= STRLITERALLEN("lxc.sysctl.");
2120 if (is_empty_string(key
))
2121 return ret_errno(EINVAL
);
2123 /* find existing list element */
2124 list_for_each_entry_safe(sysctl
, nsysctl
, &lxc_conf
->sysctls
, head
) {
2125 __do_free
char *replace_value
= NULL
;
2127 if (!strequal(key
, sysctl
->key
))
2130 replace_value
= strdup(value
);
2132 return ret_errno(EINVAL
);
2134 free(sysctl
->value
);
2135 sysctl
->value
= move_ptr(replace_value
);
2140 sysctl_elem
= zalloc(sizeof(*sysctl_elem
));
2142 return ret_errno(ENOMEM
);
2144 sysctl_elem
->key
= strdup(key
);
2145 if (!sysctl_elem
->key
)
2146 return ret_errno(ENOMEM
);
2148 sysctl_elem
->value
= strdup(value
);
2149 if (!sysctl_elem
->value
)
2150 return ret_errno(ENOMEM
);
2152 list_add_tail(&sysctl_elem
->head
, &lxc_conf
->sysctls
);
2153 move_ptr(sysctl_elem
);
2158 static int set_config_proc(const char *key
, const char *value
,
2159 struct lxc_conf
*lxc_conf
, void *data
)
2161 call_cleaner(free_lxc_proc
) struct lxc_proc
*new_proc
= NULL
;
2164 if (lxc_config_value_empty(value
))
2165 return clr_config_proc(key
, lxc_conf
, NULL
);
2167 if (!strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
2168 return ret_errno(EINVAL
);
2170 subkey
= key
+ STRLITERALLEN("lxc.proc.");
2171 if (*subkey
== '\0')
2172 return ret_errno(EINVAL
);
2174 new_proc
= zalloc(sizeof(*new_proc
));
2176 return ret_errno(ENOMEM
);
2178 new_proc
->filename
= strdup(subkey
);
2179 if (!new_proc
->filename
)
2180 return ret_errno(ENOMEM
);
2182 new_proc
->value
= strdup(value
);
2183 if (!new_proc
->value
)
2184 return ret_errno(ENOMEM
);
2186 list_add_tail(&new_proc
->head
, &lxc_conf
->procs
);
2192 static int set_config_idmaps(const char *key
, const char *value
,
2193 struct lxc_conf
*lxc_conf
, void *data
)
2195 __do_free
struct id_map
*idmap
= NULL
;
2196 unsigned long hostid
, nsid
, range
;
2200 if (lxc_config_value_empty(value
))
2201 return lxc_clear_idmaps(lxc_conf
);
2203 idmap
= zalloc(sizeof(*idmap
));
2205 return ret_errno(ENOMEM
);
2207 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2209 return log_error_errno(-EINVAL
, EINVAL
, "Failed to parse id mappings");
2211 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2213 idmap
->idtype
= ID_TYPE_UID
;
2214 else if (type
== 'g')
2215 idmap
->idtype
= ID_TYPE_GID
;
2217 return ret_errno(EINVAL
);
2219 idmap
->hostid
= hostid
;
2221 idmap
->range
= range
;
2222 list_add_tail(&idmap
->head
, &lxc_conf
->id_map
);
2224 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2225 if (idmap
->nsid
== 0)
2226 lxc_conf
->root_nsuid_map
= idmap
;
2228 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2229 if (idmap
->nsid
== 0)
2230 lxc_conf
->root_nsgid_map
= idmap
;
2237 static int set_config_mount_fstab(const char *key
, const char *value
,
2238 struct lxc_conf
*lxc_conf
, void *data
)
2240 if (lxc_config_value_empty(value
)) {
2241 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2242 return ret_errno(EINVAL
);
2245 return set_config_path_item(&lxc_conf
->fstab
, value
);
2248 static int set_config_mount_auto(const char *key
, const char *value
,
2249 struct lxc_conf
*lxc_conf
, void *data
)
2251 __do_free
char *autos
= NULL
;
2258 } allowed_auto_mounts
[] = {
2259 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2260 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2261 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2262 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2263 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2264 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2265 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2266 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2267 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2268 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2269 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2270 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2271 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2272 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2273 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2274 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2275 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2276 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2277 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2278 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2279 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2280 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2281 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2282 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2284 * For adding anything that is just a single on/off, but has no
2285 * options: keep mask and flag identical and just define the
2286 * enum value as an unused bit so far
2291 if (lxc_config_value_empty(value
)) {
2292 lxc_conf
->auto_mounts
= 0;
2296 autos
= strdup(value
);
2298 return ret_errno(ENOMEM
);
2300 lxc_iterate_parts(token
, autos
, " \t") {
2301 bool is_shmounts
= false;
2303 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2304 if (strequal(allowed_auto_mounts
[i
].token
, token
))
2307 if (strequal("shmounts:", allowed_auto_mounts
[i
].token
) &&
2308 strnequal("shmounts:", token
, STRLITERALLEN("shmounts:"))) {
2314 if (!allowed_auto_mounts
[i
].token
)
2315 return log_error_errno(-EINVAL
, EINVAL
, "Invalid filesystem to automount \"%s\"", token
);
2317 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2318 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2321 __do_free
char *container_path
= NULL
, *host_path
= NULL
;
2324 val
= token
+ STRLITERALLEN("shmounts:");
2326 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2328 host_path
= strdup(val
);
2330 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2332 val
= strchr(host_path
, ':');
2333 if (!val
|| *(val
+ 1) == '\0')
2334 val
= "/dev/.lxc-mounts";
2338 container_path
= strdup(val
);
2340 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts container path");
2342 free_disarm(lxc_conf
->shmount
.path_host
);
2343 lxc_conf
->shmount
.path_host
= move_ptr(host_path
);
2345 free_disarm(lxc_conf
->shmount
.path_cont
);
2346 lxc_conf
->shmount
.path_cont
= move_ptr(container_path
);
2353 static int set_config_mount(const char *key
, const char *value
,
2354 struct lxc_conf
*lxc_conf
, void *data
)
2356 __do_free
char *mntelem
= NULL
;
2357 __do_free
struct string_entry
*entry
= NULL
;
2359 if (lxc_config_value_empty(value
))
2360 return lxc_clear_mount_entries(lxc_conf
);
2362 entry
= zalloc(sizeof(struct string_entry
));
2364 return ret_errno(ENOMEM
);
2366 mntelem
= strdup(value
);
2368 return ret_errno(ENOMEM
);
2370 entry
->val
= move_ptr(mntelem
);
2371 list_add_tail(&entry
->head
, &lxc_conf
->mount_entries
);
2377 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2378 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2381 static int add_cap_entry(struct lxc_conf
*conf
, char *caps
, bool keep
)
2386 * In case several capability keep is specified in a single line split
2387 * these caps in a single element for the list.
2389 lxc_iterate_parts(token
, caps
, " \t") {
2390 __do_free
struct cap_entry
*new_cap
= NULL
;
2393 if (strequal(token
, "none")) {
2395 return syserror_set(-EINVAL
, "The \"none\" keyword is only valid when keeping caps");
2397 lxc_clear_config_caps(conf
);
2401 cap
= parse_cap(token
);
2404 return syserror_set(-EINVAL
, "Invalid capability specified");
2406 INFO("Ignoring unknown capability \"%s\"", token
);
2410 new_cap
= zalloc(sizeof(struct cap_entry
));
2412 return ret_errno(ENOMEM
);
2414 new_cap
->cap_name
= strdup(token
);
2415 if (!new_cap
->cap_name
)
2416 return ret_errno(ENOMEM
);
2419 list_add_tail(&new_cap
->head
, &conf
->caps
.list
);
2426 static int set_config_cap_keep(const char *key
, const char *value
,
2427 struct lxc_conf
*lxc_conf
, void *data
)
2429 __do_free
char *caps
= NULL
;
2432 if (lxc_config_value_empty(value
))
2433 return lxc_clear_config_caps(lxc_conf
);
2435 caps
= strdup(value
);
2437 return ret_errno(ENOMEM
);
2439 if (!lxc_conf
->caps
.keep
&& !list_empty(&lxc_conf
->caps
.list
))
2440 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2442 ret
= add_cap_entry(lxc_conf
, caps
, true);
2446 lxc_conf
->caps
.keep
= true;
2450 static int set_config_cap_drop(const char *key
, const char *value
,
2451 struct lxc_conf
*lxc_conf
, void *data
)
2453 __do_free
char *caps
= NULL
;
2456 if (lxc_config_value_empty(value
))
2457 return lxc_clear_config_caps(lxc_conf
);
2459 if (lxc_conf
->caps
.keep
)
2460 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2462 caps
= strdup(value
);
2464 return ret_errno(ENOMEM
);
2466 ret
= add_cap_entry(lxc_conf
, caps
, false);
2470 lxc_conf
->caps
.keep
= false;
2474 static int set_config_console_path(const char *key
, const char *value
,
2475 struct lxc_conf
*lxc_conf
, void *data
)
2477 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2480 static int set_config_console_rotate(const char *key
, const char *value
,
2481 struct lxc_conf
*lxc_conf
, void *data
)
2485 if (lxc_config_value_empty(value
)) {
2486 lxc_conf
->console
.log_rotate
= 0;
2490 ret
= lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
);
2492 return ret_errno(EINVAL
);
2494 if (lxc_conf
->console
.log_rotate
> 1)
2495 return log_error_errno(-EINVAL
, EINVAL
, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
2500 static int set_config_console_logfile(const char *key
, const char *value
,
2501 struct lxc_conf
*lxc_conf
, void *data
)
2503 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2506 static int set_config_console_buffer_size(const char *key
, const char *value
,
2507 struct lxc_conf
*lxc_conf
, void *data
)
2511 uint64_t buffer_size
, pgsz
;
2513 if (lxc_config_value_empty(value
)) {
2514 lxc_conf
->console
.buffer_size
= 0;
2518 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2519 if (strequal(value
, "auto")) {
2520 lxc_conf
->console
.buffer_size
= 1 << 17;
2524 ret
= parse_byte_size_string(value
, &size
);
2529 return ret_errno(EINVAL
);
2531 /* must be at least a page size */
2532 pgsz
= lxc_getpagesize();
2533 if ((uint64_t)size
< pgsz
) {
2534 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2539 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2540 if (buffer_size
== 0)
2541 return ret_errno(EINVAL
);
2543 if (buffer_size
!= size
)
2544 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", buffer_size
);
2546 lxc_conf
->console
.buffer_size
= buffer_size
;
2551 static int set_config_console_size(const char *key
, const char *value
,
2552 struct lxc_conf
*lxc_conf
, void *data
)
2556 uint64_t log_size
, pgsz
;
2558 if (lxc_config_value_empty(value
)) {
2559 lxc_conf
->console
.log_size
= 0;
2563 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2564 if (strequal(value
, "auto")) {
2565 lxc_conf
->console
.log_size
= 1 << 17;
2569 ret
= parse_byte_size_string(value
, &size
);
2571 return ret_errno(EINVAL
);
2574 return ret_errno(EINVAL
);
2576 /* must be at least a page size */
2577 pgsz
= lxc_getpagesize();
2578 if ((uint64_t)size
< pgsz
) {
2579 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2584 log_size
= lxc_find_next_power2((uint64_t)size
);
2586 return ret_errno(EINVAL
);
2588 if (log_size
!= size
)
2589 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", log_size
);
2591 lxc_conf
->console
.log_size
= log_size
;
2597 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2598 * file, we expand it in the unexpanded_config, so that after a save_config we
2599 * store the hwaddr for re-use.
2600 * This is only called when reading the config file, not when executing a
2602 * 'x' and 'X' are substituted in-place.
2604 static void update_hwaddr(const char *line
)
2608 line
+= lxc_char_left_gc(line
, strlen(line
));
2612 if (!lxc_config_net_is_hwaddr(line
))
2615 /* Let config_net_hwaddr raise the error. */
2616 p
= strchr(line
, '=');
2627 rand_complete_hwaddr(p
);
2630 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2633 size_t len
= conf
->unexpanded_len
;
2635 update_hwaddr(line
);
2637 linelen
= strlen(line
);
2638 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2641 tmp
= realloc(conf
->unexpanded_config
, conf
->unexpanded_alloced
+ 1024);
2643 return ret_errno(EINVAL
);
2645 if (!conf
->unexpanded_config
)
2648 conf
->unexpanded_config
= tmp
;
2649 conf
->unexpanded_alloced
+= 1024;
2652 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2653 conf
->unexpanded_len
+= linelen
;
2654 if (line
[linelen
- 1] != '\n')
2655 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2656 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2661 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2663 __do_closedir
DIR *dir
= NULL
;
2664 struct dirent
*direntp
;
2667 dir
= opendir(dirp
);
2669 return errno
== ENOENT
? 0 : -errno
;
2671 while ((direntp
= readdir(dir
))) {
2673 char path
[PATH_MAX
];
2675 fnam
= direntp
->d_name
;
2676 if (strequal(fnam
, "."))
2679 if (strequal(fnam
, ".."))
2683 if (len
< 6 || !strnequal(fnam
+ len
- 5, ".conf", 5))
2686 len
= strnprintf(path
, sizeof(path
), "%s/%s", dirp
, fnam
);
2688 return ret_errno(EIO
);
2690 ret
= lxc_config_read(path
, lxc_conf
, true);
2698 static int set_config_includefiles(const char *key
, const char *value
,
2699 struct lxc_conf
*lxc_conf
, void *data
)
2701 if (lxc_config_value_empty(value
)) {
2702 clr_config_includefiles(key
, lxc_conf
, NULL
);
2706 if (value
[strlen(value
)-1] == '/' || is_dir(value
))
2707 return do_includedir(value
, lxc_conf
);
2709 return lxc_config_read(value
, lxc_conf
, true);
2712 static int set_config_rootfs_path(const char *key
, const char *value
,
2713 struct lxc_conf
*lxc_conf
, void *data
)
2715 __do_free
char *dup
= NULL
;
2718 const char *container_path
;
2720 if (lxc_config_value_empty(value
)) {
2721 free(lxc_conf
->rootfs
.path
);
2722 lxc_conf
->rootfs
.path
= NULL
;
2726 dup
= strdup(value
);
2728 return ret_errno(ENOMEM
);
2730 /* Split <storage type>:<container path> into <storage type> and
2731 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2732 * "rootfs.path" to <container path>.
2734 tmp
= strchr(dup
, ':');
2738 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2740 return ret_errno(ENOMEM
);
2743 container_path
= tmp
;
2745 container_path
= value
;
2748 return set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2751 static int set_config_rootfs_managed(const char *key
, const char *value
,
2752 struct lxc_conf
*lxc_conf
, void *data
)
2754 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2757 static int set_config_rootfs_mount(const char *key
, const char *value
,
2758 struct lxc_conf
*lxc_conf
, void *data
)
2760 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2763 static int set_config_rootfs_options(const char *key
, const char *value
,
2764 struct lxc_conf
*lxc_conf
, void *data
)
2766 __do_free
char *dup
= NULL
, *raw_options
= NULL
;
2767 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2768 struct lxc_mount_options
*mnt_opts
= &rootfs
->mnt_opts
;
2771 clr_config_rootfs_options(key
, lxc_conf
, data
);
2772 if (lxc_config_value_empty(value
))
2775 ret
= set_config_string_item(&raw_options
, value
);
2777 return ret_errno(ENOMEM
);
2779 dup
= strdup(value
);
2783 ret
= parse_lxc_mount_attrs(mnt_opts
, dup
);
2787 /* Make sure we're only valid LXC specific mount options. */
2788 if (mnt_opts
->create_dir
|| mnt_opts
->create_file
||
2789 mnt_opts
->optional
|| mnt_opts
->relative
)
2790 return syserror_set(-EINVAL
, "Invalid LXC specific mount option for rootfs mount");
2792 ret
= parse_mount_attrs(mnt_opts
, dup
);
2794 return ret_errno(EINVAL
);
2796 rootfs
->mnt_opts
.raw_options
= move_ptr(raw_options
);
2800 static int set_config_uts_name(const char *key
, const char *value
,
2801 struct lxc_conf
*lxc_conf
, void *data
)
2803 __do_free
struct utsname
*utsname
= NULL
;
2805 if (lxc_config_value_empty(value
)) {
2806 clr_config_uts_name(key
, lxc_conf
, NULL
);
2810 utsname
= zalloc(sizeof(*utsname
));
2812 return ret_errno(ENOMEM
);
2814 if (strlen(value
) >= sizeof(utsname
->nodename
))
2815 return ret_errno(EINVAL
);
2817 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2818 free(lxc_conf
->utsname
);
2819 lxc_conf
->utsname
= move_ptr(utsname
);
2824 static int set_config_namespace_clone(const char *key
, const char *value
,
2825 struct lxc_conf
*lxc_conf
, void *data
)
2827 __do_free
char *ns
= NULL
;
2831 if (lxc_config_value_empty(value
))
2832 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2834 if (lxc_conf
->ns_keep
!= 0)
2835 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2839 return ret_errno(ENOMEM
);
2841 lxc_iterate_parts(token
, ns
, " \t") {
2842 token
+= lxc_char_left_gc(token
, strlen(token
));
2843 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2844 cloneflag
= lxc_namespace_2_cloneflag(token
);
2846 return ret_errno(EINVAL
);
2847 lxc_conf
->ns_clone
|= cloneflag
;
2853 static int set_config_namespace_keep(const char *key
, const char *value
,
2854 struct lxc_conf
*lxc_conf
, void *data
)
2856 __do_free
char *ns
= NULL
;
2860 if (lxc_config_value_empty(value
))
2861 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2863 if (lxc_conf
->ns_clone
!= 0)
2864 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2868 return ret_errno(ENOMEM
);
2870 lxc_iterate_parts(token
, ns
, " \t") {
2871 token
+= lxc_char_left_gc(token
, strlen(token
));
2872 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2873 cloneflag
= lxc_namespace_2_cloneflag(token
);
2875 return ret_errno(EINVAL
);
2876 lxc_conf
->ns_keep
|= cloneflag
;
2882 static int set_config_time_offset_boot(const char *key
, const char *value
,
2883 struct lxc_conf
*lxc_conf
, void *data
)
2888 char buf
[STRLITERALLEN("ms") + 1];
2890 if (lxc_config_value_empty(value
))
2891 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2893 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2897 unit
= lxc_trim_whitespace_in_place(buf
);
2898 if (strequal(unit
, "h")) {
2899 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2900 return ret_errno(EOVERFLOW
);
2901 } else if (strequal(unit
, "m")) {
2902 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2903 return ret_errno(EOVERFLOW
);
2904 } else if (strequal(unit
, "s")) {
2905 lxc_conf
->timens
.s_boot
= offset
;
2906 } else if (strequal(unit
, "ms")) {
2907 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2908 return ret_errno(EOVERFLOW
);
2909 } else if (strequal(unit
, "us")) {
2910 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2911 return ret_errno(EOVERFLOW
);
2912 } else if (strequal(unit
, "ns")) {
2913 lxc_conf
->timens
.ns_boot
= offset
;
2915 return ret_errno(EINVAL
);
2921 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2922 struct lxc_conf
*lxc_conf
, void *data
)
2927 char buf
[STRLITERALLEN("ms") + 1];
2929 if (lxc_config_value_empty(value
))
2930 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2932 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2936 unit
= lxc_trim_whitespace_in_place(buf
);
2937 if (strequal(unit
, "h")) {
2938 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2939 return ret_errno(EOVERFLOW
);
2940 } else if (strequal(unit
, "m")) {
2941 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2942 return ret_errno(EOVERFLOW
);
2943 } else if (strequal(unit
, "s")) {
2944 lxc_conf
->timens
.s_monotonic
= offset
;
2945 } else if (strequal(unit
, "ms")) {
2946 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2947 return ret_errno(EOVERFLOW
);
2948 } else if (strequal(unit
, "us")) {
2949 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2950 return ret_errno(EOVERFLOW
);
2951 } else if (strequal(unit
, "ns")) {
2952 lxc_conf
->timens
.ns_monotonic
= offset
;
2954 return ret_errno(EINVAL
);
2960 static int set_config_namespace_share(const char *key
, const char *value
,
2961 struct lxc_conf
*lxc_conf
, void *data
)
2964 const char *namespace;
2966 if (lxc_config_value_empty(value
))
2967 return clr_config_namespace_share(key
, lxc_conf
, data
);
2969 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2970 if (is_empty_string(namespace))
2971 return ret_errno(EINVAL
);
2973 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2977 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2980 struct parse_line_conf
{
2981 struct lxc_conf
*conf
;
2985 static int parse_line(char *buffer
, void *data
)
2987 __do_free
char *linep
= NULL
;
2988 char *dot
, *key
, *line
, *value
;
2990 struct lxc_config_t
*config
;
2993 struct parse_line_conf
*plc
= data
;
2996 return syserror_set(-EINVAL
, "Missing config");
2998 /* If there are newlines in the config file we should keep them. */
2999 empty_line
= lxc_is_line_empty(dup
);
3003 /* We have to dup the buffer otherwise, at the re-exec for reboot we
3004 * modified the original string on the stack by replacing '=' by '\0'
3007 linep
= line
= strdup(dup
);
3009 return ret_errno(ENOMEM
);
3011 if (!plc
->from_include
) {
3012 ret
= append_unexp_config_line(line
, plc
->conf
);
3020 line
+= lxc_char_left_gc(line
, strlen(line
));
3022 /* ignore comments */
3026 /* martian option - don't add it to the config itself */
3027 if (!strnequal(line
, "lxc.", 4))
3030 dot
= strchr(line
, '=');
3032 return log_error_errno(-EINVAL
, EINVAL
, "Invalid configuration line: %s", line
);
3038 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3040 value
+= lxc_char_left_gc(value
, strlen(value
));
3041 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3043 if (*value
== '\'' || *value
== '\"') {
3046 len
= strlen(value
);
3047 if (len
> 1 && value
[len
- 1] == *value
) {
3048 value
[len
- 1] = '\0';
3053 config
= lxc_get_config(key
);
3054 return config
->set(key
, value
, plc
->conf
, NULL
);
3057 static struct new_config_item
*parse_new_conf_line(char *buffer
)
3059 __do_free
char *k
= NULL
, *linep
= NULL
, *v
= NULL
;
3060 __do_free
struct new_config_item
*new = NULL
;
3062 char *dot
, *key
, *line
, *value
;
3064 if (is_empty_string(buffer
))
3065 return log_error_errno(NULL
, EINVAL
, "Empty configuration line");
3067 linep
= line
= strdup(dup
);
3071 line
+= lxc_char_left_gc(line
, strlen(line
));
3073 /* martian option - don't add it to the config itself */
3074 if (!strnequal(line
, "lxc.", 4))
3077 dot
= strchr(line
, '=');
3079 return log_error_errno(NULL
, EINVAL
, "Invalid configuration line: %s", line
);
3085 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3087 value
+= lxc_char_left_gc(value
, strlen(value
));
3088 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3090 if (*value
== '\'' || *value
== '\"') {
3093 len
= strlen(value
);
3094 if (len
> 1 && value
[len
- 1] == *value
) {
3095 value
[len
- 1] = '\0';
3100 new = zalloc(sizeof(struct new_config_item
));
3112 new->key
= move_ptr(k
);
3113 new->val
= move_ptr(v
);
3114 return move_ptr(new);
3117 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
3119 struct parse_line_conf plc
;
3122 return syserror_set(-EINVAL
, "Missing config");
3125 plc
.from_include
= from_include
;
3127 /* Catch only the top level config file name in the structure. */
3129 conf
->rcfile
= strdup(file
);
3131 return lxc_file_for_each_line_mmap(file
, parse_line
, &plc
);
3134 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3136 __do_free
struct lxc_list
*dent
= NULL
;
3138 dent
= lxc_list_new();
3140 return ret_errno(ENOMEM
);
3142 dent
->elem
= parse_new_conf_line(arg
);
3144 return ret_errno(ENOMEM
);
3146 lxc_list_add_tail(defines
, move_ptr(dent
));
3151 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3153 struct lxc_list
*it
;
3156 lxc_list_for_each(it
, defines
) {
3157 struct new_config_item
*new_item
= it
->elem
;
3158 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3163 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3164 lxc_config_define_free(defines
);
3165 #endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
3170 void lxc_config_define_free(struct lxc_list
*defines
)
3172 struct lxc_list
*it
, *next
;
3174 lxc_list_for_each_safe(it
, defines
, next
) {
3175 struct new_config_item
*new_item
= it
->elem
;
3176 free(new_item
->key
);
3177 free(new_item
->val
);
3184 int lxc_config_parse_arch(const char *arch
, signed long *persona
)
3186 static struct per_name
{
3190 { "arm", PER_LINUX32
},
3191 { "armel", PER_LINUX32
},
3192 { "armhf", PER_LINUX32
},
3193 { "armv7l", PER_LINUX32
},
3194 { "athlon", PER_LINUX32
},
3195 { "i386", PER_LINUX32
},
3196 { "i486", PER_LINUX32
},
3197 { "i586", PER_LINUX32
},
3198 { "i686", PER_LINUX32
},
3199 { "linux32", PER_LINUX32
},
3200 { "mips", PER_LINUX32
},
3201 { "mipsel", PER_LINUX32
},
3202 { "ppc", PER_LINUX32
},
3203 { "powerpc", PER_LINUX32
},
3204 { "x86", PER_LINUX32
},
3205 { "aarch64", PER_LINUX
},
3206 { "amd64", PER_LINUX
},
3207 { "arm64", PER_LINUX
},
3208 { "linux64", PER_LINUX
},
3209 { "mips64", PER_LINUX
},
3210 { "mips64el", PER_LINUX
},
3211 { "ppc64", PER_LINUX
},
3212 { "ppc64el", PER_LINUX
},
3213 { "ppc64le", PER_LINUX
},
3214 { "powerpc64", PER_LINUX
},
3215 { "s390x", PER_LINUX
},
3216 { "x86_64", PER_LINUX
},
3219 for (int i
= 0; i
< ARRAY_SIZE(pername
); i
++) {
3220 if (!strequal(pername
[i
].name
, arch
))
3223 *persona
= pername
[i
].per
;
3227 return ret_errno(EINVAL
);
3230 int lxc_fill_elevated_privileges(char *flaglist
, unsigned int *flags
)
3232 unsigned int flags_tmp
= 0;
3238 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3239 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3240 { "LSM", LXC_ATTACH_LSM_EXEC
},
3246 * For the sake of backward compatibility, keep all privileges
3247 * if no specific privileges are specified.
3249 for (unsigned int i
= 0; all_privs
[i
].token
; i
++)
3250 flags_tmp
|= all_privs
[i
].flag
;
3256 lxc_iterate_parts(token
, flaglist
, "|") {
3257 bool valid_token
= false;
3259 for (unsigned int i
= 0; all_privs
[i
].token
; i
++) {
3260 if (!strequal(all_privs
[i
].token
, token
))
3264 flags_tmp
|= all_privs
[i
].flag
;
3268 return syserror_set(-EINVAL
, "Invalid elevated privilege \"%s\" requested", token
);
3275 /* Write out a configuration file. */
3276 int write_config(int fd
, const struct lxc_conf
*conf
)
3279 size_t len
= conf
->unexpanded_len
;
3284 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3286 return log_error_errno(-errno
, errno
, "Failed to write configuration file");
3291 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3294 __do_free
char *tmp
= NULL
;
3298 len
= strlen(key
) + strlen(v
) + 4;
3299 tmp
= must_realloc(NULL
, len
);
3301 if (lxc_config_value_empty(v
))
3302 ret
= strnprintf(tmp
, len
, "%s =", key
);
3304 ret
= strnprintf(tmp
, len
, "%s = %s", key
, v
);
3308 /* Save the line verbatim into unexpanded_conf */
3309 if (append_unexp_config_line(tmp
, conf
))
3315 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3319 char *lstart
= conf
->unexpanded_config
;
3321 if (!conf
->unexpanded_config
)
3325 lend
= strchr(lstart
, '\n');
3329 lend
= lstart
+ strlen(lstart
);
3333 if (!strnequal(lstart
, key
, strlen(key
))) {
3339 v
= lstart
[strlen(key
)];
3340 if (!isspace(v
) && v
!= '=') {
3346 conf
->unexpanded_len
-= (lend
- lstart
);
3348 if (*lend
== '\0') {
3353 memmove(lstart
, lend
, strlen(lend
) + 1);
3357 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3358 const char *newpath
, const char *oldname
,
3359 const char *newname
, const char *ovldir
)
3361 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3362 char *lstart
= conf
->unexpanded_config
;
3363 const char *key
= "lxc.mount.entry";
3366 size_t newdirlen
, olddirlen
;
3368 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3369 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3370 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
, oldname
);
3374 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3375 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3376 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
, newname
);
3380 if (!conf
->unexpanded_config
)
3384 lend
= strchr(lstart
, '\n');
3386 lend
= lstart
+ strlen(lstart
);
3390 if (!strnequal(lstart
, key
, strlen(key
)))
3393 p
= strchr(lstart
+ strlen(key
), '=');
3404 /* Whenever a lxc.mount.entry entry is found in a line we check
3405 * if the substring "overlay" is present before doing any
3406 * further work. We check for "overlay" because substrings need
3407 * to have at least one space before them in a valid overlay
3408 * lxc.mount.entry (/A B overlay). When the space before is
3409 * missing it is very likely that these substrings are part of a
3410 * path or something else. (Checking q >= lend ensures that we
3411 * only count matches in the current line.) */
3412 q
= strstr(p
, " overlay");
3413 if (!q
|| q
>= lend
)
3416 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3419 /* replace the olddir with newdir */
3420 if (olddirlen
>= newdirlen
) {
3421 size_t diff
= olddirlen
- newdirlen
;
3422 memcpy(q
, newdir
, newdirlen
);
3424 if (olddirlen
!= newdirlen
) {
3425 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3426 strlen(q
) - newdirlen
- diff
+ 1);
3428 conf
->unexpanded_len
-= diff
;
3432 size_t diff
= newdirlen
- olddirlen
;
3433 size_t oldlen
= conf
->unexpanded_len
;
3434 size_t newlen
= oldlen
+ diff
;
3435 size_t poffset
= q
- conf
->unexpanded_config
;
3437 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3441 conf
->unexpanded_len
= newlen
;
3442 conf
->unexpanded_alloced
= newlen
+ 1;
3443 new[newlen
- 1] = '\0';
3444 lend
= new + (lend
- conf
->unexpanded_config
);
3446 /* Move over the remainder to make room for the newdir.
3448 memmove(new + poffset
+ newdirlen
,
3449 new + poffset
+ olddirlen
,
3450 oldlen
- poffset
- olddirlen
+ 1);
3451 conf
->unexpanded_config
= new;
3453 memcpy(new + poffset
, newdir
, newdirlen
);
3464 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3465 const char *newpath
, const char *oldname
,
3466 const char *newname
)
3468 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3469 char *lstart
= conf
->unexpanded_config
;
3470 const char *key
= "lxc.hook";
3473 size_t newdirlen
, olddirlen
;
3475 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3476 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3477 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3481 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3482 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3483 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3487 if (!conf
->unexpanded_config
)
3491 lend
= strchr(lstart
, '\n');
3493 lend
= lstart
+ strlen(lstart
);
3497 if (!strnequal(lstart
, key
, strlen(key
)))
3500 p
= strchr(lstart
+ strlen(key
), '=');
3511 if (!strnequal(p
, olddir
, strlen(olddir
)))
3514 /* replace the olddir with newdir */
3515 if (olddirlen
>= newdirlen
) {
3516 size_t diff
= olddirlen
- newdirlen
;
3517 memcpy(p
, newdir
, newdirlen
);
3519 if (olddirlen
!= newdirlen
) {
3520 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3521 strlen(p
) - newdirlen
- diff
+ 1);
3523 conf
->unexpanded_len
-= diff
;
3527 size_t diff
= newdirlen
- olddirlen
;
3528 size_t oldlen
= conf
->unexpanded_len
;
3529 size_t newlen
= oldlen
+ diff
;
3530 size_t poffset
= p
- conf
->unexpanded_config
;
3532 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3536 conf
->unexpanded_len
= newlen
;
3537 conf
->unexpanded_alloced
= newlen
+ 1;
3538 new[newlen
- 1] = '\0';
3539 lend
= new + (lend
- conf
->unexpanded_config
);
3541 /* Move over the remainder to make room for the newdir.
3543 memmove(new + poffset
+ newdirlen
,
3544 new + poffset
+ olddirlen
,
3545 oldlen
- poffset
- olddirlen
+ 1);
3546 conf
->unexpanded_config
= new;
3548 memcpy(new + poffset
, newdir
, newdirlen
);
3562 ERROR("Error writing to new config"); \
3567 /* This is called only from clone. We wish to update all hwaddrs in the
3568 * unexpanded config file. We can't/don't want to update any which come from
3569 * lxc.includes (there shouldn't be any).
3570 * We can't just walk the c->lxc-conf->network list because that includes netifs
3571 * from the include files. So we update the ones which we find in the unexp
3572 * config file, then find the original macaddr in the conf->network, and update
3573 * that to the same value.
3575 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3577 char *lend
, *p
, *p2
;
3578 char *lstart
= conf
->unexpanded_config
;
3580 if (!conf
->unexpanded_config
)
3584 char newhwaddr
[18], oldhwaddr
[17];
3585 struct lxc_netdev
*netdev
;
3587 lend
= strchr(lstart
, '\n');
3589 lend
= lstart
+ strlen(lstart
);
3593 if (!lxc_config_net_is_hwaddr(lstart
)) {
3598 p
= strchr(lstart
, '=');
3611 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3614 if ((p2
- p
) != 17) {
3615 WARN("Bad hwaddr entry");
3620 memcpy(oldhwaddr
, p
, 17);
3622 if (!new_hwaddr(newhwaddr
))
3625 memcpy(p
, newhwaddr
, 17);
3626 list_for_each_entry(netdev
, &conf
->netdevs
, head
) {
3627 if (netdev
->hwaddr
&& memcmp(oldhwaddr
, netdev
->hwaddr
, 17) == 0)
3628 memcpy(netdev
->hwaddr
, newhwaddr
, 17);
3637 static int set_config_ephemeral(const char *key
, const char *value
,
3638 struct lxc_conf
*lxc_conf
, void *data
)
3642 if (lxc_config_value_empty(value
)) {
3643 lxc_conf
->ephemeral
= 0;
3647 ret
= lxc_safe_uint(value
, &lxc_conf
->ephemeral
);
3651 if (lxc_conf
->ephemeral
> 1)
3652 return ret_errno(EINVAL
);
3657 static int set_config_log_syslog(const char *key
, const char *value
,
3658 struct lxc_conf
*lxc_conf
, void *data
)
3662 if (lxc_conf
->syslog
)
3663 free_disarm(lxc_conf
->syslog
);
3665 if (lxc_config_value_empty(value
))
3668 facility
= lxc_syslog_priority_to_int(value
);
3669 if (facility
== -EINVAL
)
3670 return ret_errno(EINVAL
);
3672 lxc_log_syslog(facility
);
3674 return set_config_string_item(&lxc_conf
->syslog
, value
);
3677 static int set_config_no_new_privs(const char *key
, const char *value
,
3678 struct lxc_conf
*lxc_conf
, void *data
)
3683 if (lxc_config_value_empty(value
)) {
3684 lxc_conf
->no_new_privs
= false;
3688 ret
= lxc_safe_uint(value
, &v
);
3693 return ret_errno(EINVAL
);
3695 lxc_conf
->no_new_privs
= v
? true : false;
3700 /* Callbacks to get configuration items. */
3701 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3702 struct lxc_conf
*c
, void *data
)
3709 memset(retv
, 0, inlen
);
3713 switch (c
->personality
) {
3715 strprint(retv
, inlen
, "i686");
3718 strprint(retv
, inlen
, "x86_64");
3727 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3728 struct lxc_conf
*c
, void *data
)
3730 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3733 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3734 struct lxc_conf
*c
, void *data
)
3736 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3739 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3740 struct lxc_conf
*c
, void *data
)
3742 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3745 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3746 struct lxc_conf
*c
, void *data
)
3749 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3751 return syserror_set(-EINVAL
, "Built without AppArmor support");
3755 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3756 int inlen
, struct lxc_conf
*c
,
3760 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_incomplete
);
3762 return syserror_set(-EINVAL
, "Built without AppArmor support");
3766 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3767 int inlen
, struct lxc_conf
*c
,
3771 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_nesting
);
3773 return syserror_set(-EINVAL
, "Built without AppArmor support");
3777 static int get_config_apparmor_raw(const char *key
, char *retv
,
3778 int inlen
, struct lxc_conf
*c
,
3783 struct string_entry
*entry
;
3789 memset(retv
, 0, inlen
);
3791 list_for_each_entry(entry
, &c
->lsm_aa_raw
, head
) {
3792 strprint(retv
, inlen
, "%s\n", entry
->val
);
3797 return syserror_set(-EINVAL
, "Built without AppArmor support");
3801 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3802 struct lxc_conf
*c
, void *data
)
3805 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3807 return syserror_set(-EINVAL
, "Built without SELinux support");
3811 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3812 struct lxc_conf
*c
, void *data
)
3815 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3817 return syserror_set(-EINVAL
, "Built without SELinux support");
3821 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3822 struct lxc_conf
*c
, void *data
)
3824 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3828 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3829 * just the value(s) will be printed. Since there still could be more than one,
3830 * it is newline-separated.
3831 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3833 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3834 * 'lxc.cgroup.subsystem.key = value' format.
3836 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3837 int inlen
, struct lxc_conf
*c
,
3841 bool get_all
= false;
3843 size_t namespaced_token_len
;
3844 char *global_token
, *namespaced_token
;
3845 struct list_head
*list
;
3846 struct lxc_cgroup
*cgroup
;
3851 memset(retv
, 0, inlen
);
3853 if (version
== CGROUP2_SUPER_MAGIC
) {
3854 global_token
= "lxc.cgroup2";
3855 namespaced_token
= "lxc.cgroup2.";
3856 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3858 } else if (version
== CGROUP_SUPER_MAGIC
) {
3859 global_token
= "lxc.cgroup";
3860 namespaced_token
= "lxc.cgroup.";
3861 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3864 return ret_errno(EINVAL
);
3867 if (strequal(key
, global_token
))
3869 else if (strnequal(key
, namespaced_token
, namespaced_token_len
))
3870 key
+= namespaced_token_len
;
3872 return ret_errno(EINVAL
);
3874 list_for_each_entry(cgroup
, list
, head
) {
3876 if (version
!= cgroup
->version
)
3879 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3880 cgroup
->subsystem
, cgroup
->value
);
3881 } else if (strequal(cgroup
->subsystem
, key
)) {
3882 strprint(retv
, inlen
, "%s\n", cgroup
->value
);
3889 static int get_config_cgroup_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 CGROUP_SUPER_MAGIC
);
3896 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3897 struct lxc_conf
*c
, void *data
)
3899 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3900 CGROUP2_SUPER_MAGIC
);
3903 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3904 struct lxc_conf
*lxc_conf
, void *data
)
3909 if (!strequal(key
, "lxc.cgroup.dir"))
3910 return ret_errno(EINVAL
);
3915 memset(retv
, 0, inlen
);
3917 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3922 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3923 struct lxc_conf
*lxc_conf
, void *data
)
3931 memset(retv
, 0, inlen
);
3933 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3938 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3939 struct lxc_conf
*lxc_conf
, void *data
)
3947 memset(retv
, 0, inlen
);
3949 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3954 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3956 struct lxc_conf
*lxc_conf
,
3965 memset(retv
, 0, inlen
);
3967 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3972 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3974 struct lxc_conf
*lxc_conf
,
3983 memset(retv
, 0, inlen
);
3985 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3990 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3991 int inlen
, struct lxc_conf
*lxc_conf
,
3994 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3995 lxc_conf
->cgroup_meta
.relative
);
3998 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3999 struct lxc_conf
*c
, void *data
)
4002 int len
, listlen
, ret
;
4004 /* "u 1000 1000000 65536"
4006 * let's render this as
4024 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
4025 char buf
[__LXC_IDMAP_STR_BUF
];
4030 memset(retv
, 0, inlen
);
4032 listlen
= list_len(map
, &c
->id_map
, head
);
4033 list_for_each_entry(map
, &c
->id_map
, head
) {
4034 ret
= strnprintf(buf
, sizeof(buf
), "%c %lu %lu %lu",
4035 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
4036 map
->nsid
, map
->hostid
, map
->range
);
4038 return ret_errno(EIO
);
4040 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
4046 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
4047 struct lxc_conf
*c
, void *data
)
4050 v
= lxc_log_priority_to_string(c
->loglevel
);
4051 return lxc_get_conf_str(retv
, inlen
, v
);
4054 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
4055 struct lxc_conf
*c
, void *data
)
4057 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
4060 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
4061 struct lxc_conf
*c
, void *data
)
4063 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
4066 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
4067 struct lxc_conf
*c
, void *data
)
4069 int len
, fulllen
= 0;
4070 const char *sep
= "";
4075 memset(retv
, 0, inlen
);
4077 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
4080 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
4081 case LXC_AUTO_PROC_MIXED
:
4082 strprint(retv
, inlen
, "%sproc:mixed", sep
);
4085 case LXC_AUTO_PROC_RW
:
4086 strprint(retv
, inlen
, "%sproc:rw", sep
);
4093 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
4094 case LXC_AUTO_SYS_RO
:
4095 strprint(retv
, inlen
, "%ssys:ro", sep
);
4098 case LXC_AUTO_SYS_RW
:
4099 strprint(retv
, inlen
, "%ssys:rw", sep
);
4102 case LXC_AUTO_SYS_MIXED
:
4103 strprint(retv
, inlen
, "%ssys:mixed", sep
);
4110 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
4111 case LXC_AUTO_CGROUP_NOSPEC
:
4112 strprint(retv
, inlen
, "%scgroup", sep
);
4114 case LXC_AUTO_CGROUP_MIXED
:
4115 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
4117 case LXC_AUTO_CGROUP_RO
:
4118 strprint(retv
, inlen
, "%scgroup:ro", sep
);
4120 case LXC_AUTO_CGROUP_RW
:
4121 strprint(retv
, inlen
, "%scgroup:rw", sep
);
4123 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
4124 strprint(retv
, inlen
, "%scgroup-full", sep
);
4126 case LXC_AUTO_CGROUP_FULL_MIXED
:
4127 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
4129 case LXC_AUTO_CGROUP_FULL_RO
:
4130 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
4132 case LXC_AUTO_CGROUP_FULL_RW
:
4133 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
4142 static int get_config_mount(const char *key
, char *retv
, int inlen
,
4143 struct lxc_conf
*c
, void *data
)
4145 int len
, fulllen
= 0;
4146 struct string_entry
*entry
;
4151 memset(retv
, 0, inlen
);
4153 list_for_each_entry(entry
, &c
->mount_entries
, head
) {
4154 strprint(retv
, inlen
, "%s\n", entry
->val
);
4160 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4161 struct lxc_conf
*c
, void *data
)
4163 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4166 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4167 struct lxc_conf
*c
, void *data
)
4169 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4172 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4173 struct lxc_conf
*c
, void *data
)
4175 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4178 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4179 struct lxc_conf
*c
, void *data
)
4181 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mnt_opts
.raw_options
);
4184 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4185 struct lxc_conf
*c
, void *data
)
4187 return lxc_get_conf_str(
4189 c
->utsname
? c
->utsname
->nodename
: NULL
);
4192 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4193 struct lxc_conf
*c
, void *data
)
4196 int len
, fulllen
= 0, found
= -1;
4197 struct string_entry
*entry
;
4200 subkey
= strchr(key
, '.');
4202 return ret_errno(EINVAL
);
4204 subkey
= strchr(subkey
+ 1, '.');
4206 return ret_errno(EINVAL
);
4208 if (*subkey
== '\0')
4209 return ret_errno(EINVAL
);
4211 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4212 if (strequal(lxchook_names
[i
], subkey
)) {
4219 return ret_errno(EINVAL
);
4224 memset(retv
, 0, inlen
);
4226 list_for_each_entry(entry
, &c
->hooks
[found
], head
) {
4227 strprint(retv
, inlen
, "%s\n", entry
->val
);
4233 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4234 struct lxc_conf
*c
, void *data
)
4236 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4239 static int get_config_net(const char *key
, char *retv
, int inlen
,
4240 struct lxc_conf
*c
, void *data
)
4242 int len
, fulllen
= 0;
4243 struct lxc_netdev
*netdev
;
4248 memset(retv
, 0, inlen
);
4250 list_for_each_entry(netdev
, &c
->netdevs
, head
) {
4251 const char *t
= lxc_net_type_to_str(netdev
->type
);
4252 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4258 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4259 struct lxc_conf
*c
, void *data
)
4261 int len
, fulllen
= 0;
4262 struct cap_entry
*cap
;
4267 memset(retv
, 0, inlen
);
4269 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4270 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4276 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4277 struct lxc_conf
*c
, void *data
)
4279 int len
, fulllen
= 0;
4280 struct cap_entry
*cap
;
4285 memset(retv
, 0, inlen
);
4287 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4288 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4294 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4295 struct lxc_conf
*c
, void *data
)
4297 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4300 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4301 struct lxc_conf
*c
, void *data
)
4303 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4306 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4307 struct lxc_conf
*c
, void *data
)
4309 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4313 static int get_config_console_buffer_size(const char *key
, char *retv
,
4314 int inlen
, struct lxc_conf
*c
,
4317 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4320 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4321 struct lxc_conf
*c
, void *data
)
4323 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4326 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4327 int inlen
, struct lxc_conf
*c
,
4331 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4333 return ret_errno(ENOSYS
);
4337 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4338 struct lxc_conf
*c
, void *data
)
4340 #ifdef HAVE_SECCOMP_NOTIFY
4341 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4343 return ret_errno(ENOSYS
);
4347 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4348 struct lxc_conf
*c
, void *data
)
4350 #ifdef HAVE_SECCOMP_NOTIFY
4351 return lxc_get_conf_str(retv
, inlen
,
4352 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4353 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4354 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4356 return ret_errno(ENOSYS
);
4360 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4361 struct lxc_conf
*c
, void *data
)
4364 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4366 return ret_errno(ENOSYS
);
4370 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4371 struct lxc_conf
*c
, void *data
)
4373 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4376 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4377 struct lxc_conf
*c
, void *data
)
4379 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4382 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4383 struct lxc_conf
*c
, void *data
)
4385 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4388 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4389 struct lxc_conf
*c
, void *data
)
4391 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4394 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4395 struct lxc_conf
*c
, void *data
)
4397 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4400 static int get_config_start(const char *key
, char *retv
, int inlen
,
4401 struct lxc_conf
*c
, void *data
)
4403 if (strequal(key
+ 10, "auto"))
4404 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4405 else if (strequal(key
+ 10, "delay"))
4406 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4407 else if (strequal(key
+ 10, "order"))
4408 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4410 return ret_errno(EINVAL
);
4413 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4414 struct lxc_conf
*c
, void *data
)
4416 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4419 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4420 struct lxc_conf
*c
, void *data
)
4422 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4425 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4426 int inlen
, struct lxc_conf
*c
,
4429 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4432 static int get_config_group(const char *key
, char *retv
, int inlen
,
4433 struct lxc_conf
*c
, void *data
)
4435 int len
, fulllen
= 0;
4436 struct string_entry
*entry
;
4441 memset(retv
, 0, inlen
);
4443 list_for_each_entry(entry
, &c
->groups
, head
) {
4444 strprint(retv
, inlen
, "%s\n", entry
->val
);
4450 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4451 struct lxc_conf
*c
, void *data
)
4453 int len
, fulllen
= 0;
4454 struct environment_entry
*env
;
4459 memset(retv
, 0, inlen
);
4461 list_for_each_entry(env
, &c
->environment
, head
) {
4462 strprint(retv
, inlen
, "%s=%s\n", env
->key
, env
->val
);
4468 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4469 struct lxc_conf
*c
, void *data
)
4471 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4474 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4475 struct lxc_conf
*c
, void *data
)
4477 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4480 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4481 struct lxc_conf
*c
, void *data
)
4483 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4486 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4487 struct lxc_conf
*c
, void *data
)
4489 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4492 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4493 struct lxc_conf
*c
, void *data
)
4495 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4498 static int get_config_init_groups(const char *key
, char *retv
, int inlen
,
4499 struct lxc_conf
*c
, void *data
)
4501 int fulllen
= 0, len
;
4506 memset(retv
, 0, inlen
);
4508 if (c
->init_groups
.size
== 0)
4511 for (int i
= 0; i
< c
->init_groups
.size
; i
++)
4512 strprint(retv
, inlen
, "%s%d", (i
> 0) ? "," : "",
4513 c
->init_groups
.list
[i
]);
4518 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4519 struct lxc_conf
*c
, void *data
)
4521 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4524 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4525 struct lxc_conf
*c
, void *data
)
4527 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4530 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4531 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4532 * printed, in 'lxc.prlimit.resource = value' format.
4534 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4535 struct lxc_conf
*c
, void *data
)
4537 int fulllen
= 0, len
;
4538 bool get_all
= false;
4539 struct lxc_limit
*lim
;
4544 memset(retv
, 0, inlen
);
4546 if (strequal(key
, "lxc.prlimit"))
4548 else if (strnequal(key
, "lxc.prlimit.", 12))
4551 return ret_errno(EINVAL
);
4553 list_for_each_entry(lim
, &c
->limits
, head
) {
4554 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4555 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4558 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4559 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4560 partlen
= STRLITERALLEN("unlimited");
4562 partlen
= sprintf(buf
, "%" PRIu64
,
4563 (uint64_t)lim
->limit
.rlim_cur
);
4566 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4567 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4568 memcpy(buf
+ partlen
, ":unlimited",
4569 STRLITERALLEN(":unlimited") + 1);
4571 sprintf(buf
+ partlen
, ":%" PRIu64
,
4572 (uint64_t)lim
->limit
.rlim_max
);
4576 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4577 lim
->resource
, buf
);
4578 } else if (strequal(lim
->resource
, key
)) {
4579 strprint(retv
, inlen
, "%s", buf
);
4586 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4587 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4588 * entries will be printed, in 'lxc.sysctl.key = value' format.
4590 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4591 struct lxc_conf
*c
, void *data
)
4594 bool get_all
= false;
4596 struct lxc_sysctl
*sysctl
;
4601 memset(retv
, 0, inlen
);
4603 if (strequal(key
, "lxc.sysctl"))
4605 else if (strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4606 key
+= STRLITERALLEN("lxc.sysctl.");
4608 return ret_errno(EINVAL
);
4610 list_for_each_entry(sysctl
, &c
->sysctls
, head
) {
4612 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", sysctl
->key
,
4614 } else if (strequal(sysctl
->key
, key
)) {
4615 strprint(retv
, inlen
, "%s", sysctl
->value
);
4622 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4623 struct lxc_conf
*c
, void *data
)
4626 bool get_all
= false;
4628 struct lxc_proc
*proc
;
4633 memset(retv
, 0, inlen
);
4635 if (strequal(key
, "lxc.proc"))
4637 else if (strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4638 key
+= STRLITERALLEN("lxc.proc.");
4640 return ret_errno(EINVAL
);
4642 list_for_each_entry(proc
, &c
->procs
, head
) {
4644 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4645 proc
->filename
, proc
->value
);
4646 } else if (strequal(proc
->filename
, key
)) {
4647 strprint(retv
, inlen
, "%s", proc
->value
);
4654 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4655 struct lxc_conf
*c
, void *data
)
4663 memset(retv
, 0, inlen
);
4665 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4666 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4667 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4673 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4674 struct lxc_conf
*c
, void *data
)
4682 memset(retv
, 0, inlen
);
4684 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4685 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4686 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4692 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4701 memset(retv
, 0, inlen
);
4703 if (c
->timens
.s_boot
) {
4704 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4706 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4712 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4713 struct lxc_conf
*c
, void *data
)
4721 memset(retv
, 0, inlen
);
4723 if (c
->timens
.s_monotonic
) {
4724 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4726 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4732 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4733 struct lxc_conf
*c
, void *data
)
4736 const char *namespace;
4742 memset(retv
, 0, inlen
);
4744 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4745 if (is_empty_string(namespace))
4746 return ret_errno(EINVAL
);
4748 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4752 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4757 /* Callbacks to clear config items. */
4758 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4761 c
->personality
= -1;
4765 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4772 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4779 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4782 free_disarm(c
->ttys
.dir
);
4786 static inline int clr_config_apparmor_profile(const char *key
,
4787 struct lxc_conf
*c
, void *data
)
4790 free_disarm(c
->lsm_aa_profile
);
4793 return syserror_set(-EINVAL
, "Built without AppArmor support");
4797 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4802 c
->lsm_aa_allow_incomplete
= 0;
4805 return syserror_set(-EINVAL
, "Built without AppArmor support");
4809 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4814 c
->lsm_aa_allow_nesting
= 0;
4817 return syserror_set(-EINVAL
, "Built without AppArmor support");
4821 static inline int clr_config_apparmor_raw(const char *key
,
4826 return lxc_clear_apparmor_raw(c
);
4828 return syserror_set(-EINVAL
, "Built without AppArmor support");
4832 static inline int clr_config_selinux_context(const char *key
,
4833 struct lxc_conf
*c
, void *data
)
4836 free_disarm(c
->lsm_se_context
);
4839 return syserror_set(-EINVAL
, "Built without SELinux support");
4843 static inline int clr_config_selinux_context_keyring(const char *key
,
4844 struct lxc_conf
*c
, void *data
)
4847 free_disarm(c
->lsm_se_keyring_context
);
4850 return syserror_set(-EINVAL
, "Built without SELinux support");
4854 static inline int clr_config_keyring_session(const char *key
,
4855 struct lxc_conf
*c
, void *data
)
4857 c
->keyring_disable_session
= false;
4861 static inline int clr_config_cgroup_controller(const char *key
,
4862 struct lxc_conf
*c
, void *data
)
4864 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4867 static inline int clr_config_cgroup2_controller(const char *key
,
4868 struct lxc_conf
*c
, void *data
)
4870 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4873 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4876 if (!strequal(key
, "lxc.cgroup.dir"))
4877 return ret_errno(EINVAL
);
4879 if (lxc_conf
->cgroup_meta
.dir
)
4880 free_disarm(lxc_conf
->cgroup_meta
.dir
);
4885 static int clr_config_cgroup_monitor_dir(const char *key
,
4886 struct lxc_conf
*lxc_conf
,
4889 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4893 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4894 struct lxc_conf
*lxc_conf
,
4897 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4901 static int clr_config_cgroup_container_dir(const char *key
,
4902 struct lxc_conf
*lxc_conf
,
4905 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4909 static int clr_config_cgroup_container_inner_dir(const char *key
,
4910 struct lxc_conf
*lxc_conf
,
4913 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4917 static inline int clr_config_cgroup_relative(const char *key
,
4918 struct lxc_conf
*lxc_conf
,
4921 lxc_conf
->cgroup_meta
.relative
= false;
4925 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4928 return lxc_clear_idmaps(c
);
4931 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4934 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4938 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4941 free_disarm(c
->logfile
);
4945 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4948 return lxc_clear_mount_entries(c
);
4951 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4954 return lxc_clear_automounts(c
);
4957 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4960 free_disarm(c
->fstab
);
4964 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4967 free_disarm(c
->rootfs
.path
);
4971 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4974 c
->rootfs
.managed
= true;
4978 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4981 free_disarm(c
->rootfs
.mount
);
4985 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4988 put_lxc_mount_options(&c
->rootfs
.mnt_opts
);
4993 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4996 free_disarm(c
->utsname
);
5000 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
5003 return lxc_clear_hooks(c
, key
);
5006 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
5009 /* default to legacy hooks version */
5010 c
->hooks_version
= 0;
5014 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
5017 lxc_free_networks(c
);
5022 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
5025 return lxc_clear_config_caps(c
);
5028 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
5031 return lxc_clear_config_caps(c
);
5034 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
5037 free_disarm(c
->console
.path
);
5041 static inline int clr_config_console_logfile(const char *key
,
5042 struct lxc_conf
*c
, void *data
)
5044 free_disarm(c
->console
.log_path
);
5048 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
5051 c
->console
.log_rotate
= 0;
5055 static inline int clr_config_console_buffer_size(const char *key
,
5056 struct lxc_conf
*c
, void *data
)
5058 c
->console
.buffer_size
= 0;
5062 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
5065 c
->console
.log_size
= 0;
5069 static inline int clr_config_seccomp_allow_nesting(const char *key
,
5070 struct lxc_conf
*c
, void *data
)
5073 c
->seccomp
.allow_nesting
= 0;
5076 return ret_errno(ENOSYS
);
5080 static inline int clr_config_seccomp_notify_cookie(const char *key
,
5081 struct lxc_conf
*c
, void *data
)
5083 #ifdef HAVE_SECCOMP_NOTIFY
5084 free_disarm(c
->seccomp
.notifier
.cookie
);
5087 return ret_errno(ENOSYS
);
5091 static inline int clr_config_seccomp_notify_proxy(const char *key
,
5092 struct lxc_conf
*c
, void *data
)
5094 #ifdef HAVE_SECCOMP_NOTIFY
5095 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
5096 sizeof(c
->seccomp
.notifier
.proxy_addr
));
5099 return ret_errno(ENOSYS
);
5103 static inline int clr_config_seccomp_profile(const char *key
,
5104 struct lxc_conf
*c
, void *data
)
5106 free_disarm(c
->seccomp
.seccomp
);
5110 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
5117 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
5120 c
->autodevtmpfssize
= 500000;
5124 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
5131 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
5134 c
->rebootsignal
= 0;
5138 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
5145 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
5148 if (strequal(key
+ 10, "auto"))
5150 else if (strequal(key
+ 10, "delay"))
5152 else if (strequal(key
+ 10, "order"))
5158 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
5161 free_disarm(c
->syslog
);
5165 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
5168 c
->monitor_unshare
= 0;
5172 static inline int clr_config_monitor_signal_pdeath(const char *key
,
5173 struct lxc_conf
*c
, void *data
)
5175 c
->monitor_signal_pdeath
= 0;
5179 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
5182 return lxc_clear_groups(c
);
5185 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5188 return lxc_clear_environment(c
);
5191 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5194 free_disarm(c
->execute_cmd
);
5198 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5201 free_disarm(c
->init_cmd
);
5205 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5208 free_disarm(c
->init_cwd
);
5212 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5219 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5226 static inline int clr_config_init_groups(const char *key
, struct lxc_conf
*c
,
5229 c
->init_groups
.size
= 0;
5230 free_disarm(c
->init_groups
.list
);
5234 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5241 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5244 c
->no_new_privs
= false;
5248 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5251 return lxc_clear_limits(c
, key
);
5254 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5257 return lxc_clear_sysctls(c
, key
);
5260 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5263 return lxc_clear_procs(c
, key
);
5266 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5272 static int clr_config_namespace_clone(const char *key
,
5273 struct lxc_conf
*lxc_conf
, void *data
)
5275 lxc_conf
->ns_clone
= 0;
5279 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5282 lxc_conf
->ns_keep
= 0;
5286 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5288 lxc_conf
->timens
.s_boot
= 0;
5289 lxc_conf
->timens
.ns_boot
= 0;
5293 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5295 lxc_conf
->timens
.s_monotonic
= 0;
5296 lxc_conf
->timens
.ns_monotonic
= 0;
5300 static int clr_config_namespace_share(const char *key
,
5301 struct lxc_conf
*lxc_conf
, void *data
)
5304 const char *namespace;
5306 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5307 if (is_empty_string(namespace))
5308 return ret_errno(EINVAL
);
5310 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5314 free(lxc_conf
->ns_share
[ns_idx
]);
5315 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5320 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5321 struct lxc_conf
*c
, void *data
)
5323 return ret_errno(ENOSYS
);
5326 struct config_net_info
{
5327 char buf
[NETWORK_SUBKEY_SIZE_MAX
];
5329 const struct lxc_config_net_t
*ops
;
5330 struct lxc_netdev
*netdev
;
5333 static int get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
,
5334 struct config_net_info
*info
, bool allocate
)
5339 const char *idx_start
;
5341 if (is_empty_string(key
))
5342 return ret_errno(EINVAL
);
5344 /* check that this is a sensible network key */
5345 if (!strnequal("lxc.net.", key
, STRLITERALLEN("lxc.net.")))
5346 return syserror_set(-EINVAL
, "Invalid network configuration key \"%s\"", key
);
5349 /* beginning of index string */
5350 idx_start
= key
+ STRLITERALLEN("lxc.net.");
5351 if (!isdigit(*idx_start
))
5352 return syserror_set(-EINVAL
, "Failed to detect digit in string \"%s\"", key
+ 8);
5354 ret
= lxc_safe_int64_residual(idx_start
, &tmpidx
, 10, info
->buf
, sizeof(info
->buf
));
5356 return syserror("Failed to parse network index");
5358 if (tmpidx
< 0 || tmpidx
>= INT_MAX
)
5359 return syserror_set(-ERANGE
, "Number of configured networks would overflow the counter");
5360 idx
= (unsigned int)tmpidx
;
5362 info
->netdev
= lxc_get_netdev_by_idx(lxc_conf
, idx
, allocate
);
5364 return ret_errno(EINVAL
);
5366 /* Make sure subkey points to the empty string. */
5367 info
->subkey
= info
->buf
;
5368 if (is_empty_string(info
->subkey
))
5369 return ret_errno(ENOENT
);
5371 if (info
->subkey
[0] != '.')
5372 return syserror_set(-EINVAL
, "Invalid subkey");
5375 /* lxc.net.<idx>.<subkey> */
5376 info
->ops
= lxc_get_config_net(info
->subkey
);
5377 if (info
->ops
== &unsupported_config_net_key
)
5378 return syserror_set(-ENOENT
, "Unknown network configuration key \"%s\"", key
);
5383 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5384 * found. So we make sure next comes an integer, find the right callback (by
5385 * rewriting the key), and call it.
5387 static int set_config_jump_table_net(const char *key
, const char *value
,
5388 struct lxc_conf
*lxc_conf
, void *data
)
5390 struct config_net_info info
= {};
5392 const char *idxstring
;
5394 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5395 if (!isdigit(*idxstring
))
5396 return ret_errno(EINVAL
);
5398 if (lxc_config_value_empty(value
))
5399 return clr_config_jump_table_net(key
, lxc_conf
, data
);
5401 ret
= get_network_config_ops(key
, lxc_conf
, &info
, true);
5405 return info
.ops
->set(info
.subkey
, value
, lxc_conf
, info
.netdev
);
5408 static int clr_config_jump_table_net(const char *key
, struct lxc_conf
*lxc_conf
,
5411 struct config_net_info info
= {};
5413 const char *idxstring
;
5415 idxstring
= key
+ 8;
5416 if (!isdigit(*idxstring
))
5417 return ret_errno(EINVAL
);
5419 /* The left conjunct is pretty self-explanatory. The right conjunct
5420 * checks whether the two pointers are equal. If they are we know that
5421 * this is not a key that is namespaced any further and so we are
5422 * supposed to clear the whole network.
5424 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5425 unsigned int rmnetdevidx
;
5427 ret
= lxc_safe_uint(idxstring
, &rmnetdevidx
);
5431 /* Remove network from network list. */
5432 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5436 ret
= get_network_config_ops(key
, lxc_conf
, &info
, false);
5440 return info
.ops
->clr(info
.subkey
, lxc_conf
, info
.netdev
);
5443 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5446 struct lxc_netdev
*netdev
= data
;
5449 return ret_errno(EINVAL
);
5451 lxc_clear_netdev(netdev
);
5456 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5459 struct lxc_netdev
*netdev
= data
;
5462 return ret_errno(EINVAL
);
5464 netdev
->name
[0] = '\0';
5469 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5472 struct lxc_netdev
*netdev
= data
;
5475 return ret_errno(EINVAL
);
5482 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5485 struct lxc_netdev
*netdev
= data
;
5488 return ret_errno(EINVAL
);
5490 netdev
->link
[0] = '\0';
5495 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5498 struct lxc_netdev
*netdev
= data
;
5501 return ret_errno(EINVAL
);
5503 netdev
->l2proxy
= false;
5508 static int clr_config_net_macvlan_mode(const char *key
,
5509 struct lxc_conf
*lxc_conf
, void *data
)
5511 struct lxc_netdev
*netdev
= data
;
5514 return ret_errno(EINVAL
);
5516 if (netdev
->type
!= LXC_NET_MACVLAN
)
5519 netdev
->priv
.macvlan_attr
.mode
= -1;
5524 static int clr_config_net_ipvlan_mode(const char *key
,
5525 struct lxc_conf
*lxc_conf
, void *data
)
5527 struct lxc_netdev
*netdev
= data
;
5530 return ret_errno(EINVAL
);
5532 if (netdev
->type
!= LXC_NET_IPVLAN
)
5535 netdev
->priv
.ipvlan_attr
.mode
= -1;
5540 static int clr_config_net_ipvlan_isolation(const char *key
,
5541 struct lxc_conf
*lxc_conf
, void *data
)
5543 struct lxc_netdev
*netdev
= data
;
5546 return ret_errno(EINVAL
);
5548 if (netdev
->type
!= LXC_NET_IPVLAN
)
5551 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5556 static int clr_config_net_veth_mode(const char *key
,
5557 struct lxc_conf
*lxc_conf
, void *data
)
5559 struct lxc_netdev
*netdev
= data
;
5562 return ret_errno(EINVAL
);
5564 if (netdev
->type
!= LXC_NET_VETH
)
5567 netdev
->priv
.veth_attr
.mode
= -1;
5572 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5575 struct lxc_netdev
*netdev
= data
;
5578 return ret_errno(EINVAL
);
5580 if (netdev
->type
!= LXC_NET_VETH
)
5583 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5588 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5591 struct lxc_netdev
*netdev
= data
;
5594 return ret_errno(EINVAL
);
5596 if (netdev
->type
!= LXC_NET_VETH
)
5599 netdev
->priv
.veth_attr
.vlan_id
= 0;
5600 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5605 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5606 struct lxc_conf
*lxc_conf
, void *data
)
5608 struct lxc_netdev
*netdev
= data
;
5609 struct lxc_list
*cur
, *next
;
5612 return ret_errno(EINVAL
);
5614 if (netdev
->type
!= LXC_NET_VETH
)
5617 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5626 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5629 struct lxc_netdev
*netdev
= data
;
5632 return ret_errno(EINVAL
);
5634 free_disarm(netdev
->upscript
);
5639 static int clr_config_net_script_down(const char *key
,
5640 struct lxc_conf
*lxc_conf
, void *data
)
5642 struct lxc_netdev
*netdev
= data
;
5645 return ret_errno(EINVAL
);
5647 free_disarm(netdev
->downscript
);
5652 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5655 struct lxc_netdev
*netdev
= data
;
5658 return ret_errno(EINVAL
);
5660 free_disarm(netdev
->hwaddr
);
5665 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5668 struct lxc_netdev
*netdev
= data
;
5671 return ret_errno(EINVAL
);
5673 free_disarm(netdev
->mtu
);
5678 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5681 struct lxc_netdev
*netdev
= data
;
5684 return ret_errno(EINVAL
);
5686 if (netdev
->type
!= LXC_NET_VLAN
)
5689 netdev
->priv
.vlan_attr
.vid
= 0;
5694 static int clr_config_net_ipv4_gateway(const char *key
,
5695 struct lxc_conf
*lxc_conf
, void *data
)
5697 struct lxc_netdev
*netdev
= data
;
5700 return ret_errno(EINVAL
);
5702 free_disarm(netdev
->ipv4_gateway
);
5707 static int clr_config_net_ipv4_address(const char *key
,
5708 struct lxc_conf
*lxc_conf
, void *data
)
5710 struct lxc_netdev
*netdev
= data
;
5711 struct lxc_inetdev
*inetdev
, *ninetdev
;
5714 return ret_errno(EINVAL
);
5716 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->ipv4_addresses
, head
) {
5717 list_del(&inetdev
->head
);
5724 static int clr_config_net_veth_ipv4_route(const char *key
,
5725 struct lxc_conf
*lxc_conf
, void *data
)
5727 struct lxc_netdev
*netdev
= data
;
5728 struct lxc_inetdev
*inetdev
, *ninetdev
;
5731 return ret_errno(EINVAL
);
5733 if (netdev
->type
!= LXC_NET_VETH
)
5736 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
5737 list_del(&inetdev
->head
);
5744 static int clr_config_net_ipv6_gateway(const char *key
,
5745 struct lxc_conf
*lxc_conf
, void *data
)
5747 struct lxc_netdev
*netdev
= data
;
5750 return ret_errno(EINVAL
);
5752 free_disarm(netdev
->ipv6_gateway
);
5757 static int clr_config_net_ipv6_address(const char *key
,
5758 struct lxc_conf
*lxc_conf
, void *data
)
5760 struct lxc_netdev
*netdev
= data
;
5761 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5764 return ret_errno(EINVAL
);
5766 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->ipv6_addresses
, head
) {
5767 list_del(&inet6dev
->head
);
5774 static int clr_config_net_veth_ipv6_route(const char *key
,
5775 struct lxc_conf
*lxc_conf
, void *data
)
5777 struct lxc_netdev
*netdev
= data
;
5778 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5781 return ret_errno(EINVAL
);
5783 if (netdev
->type
!= LXC_NET_VETH
)
5786 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
5787 list_del(&inet6dev
->head
);
5794 static int get_config_jump_table_net(const char *key
, char *retv
, int inlen
,
5795 struct lxc_conf
*c
, void *data
)
5797 struct config_net_info info
= {};
5799 const char *idxstring
;
5801 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5802 if (!isdigit(*idxstring
))
5803 return ret_errno(EINVAL
);
5805 ret
= get_network_config_ops(key
, c
, &info
, false);
5809 return info
.ops
->get(info
.subkey
, retv
, inlen
, c
, info
.netdev
);
5812 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5813 struct lxc_conf
*c
, void *data
)
5817 struct lxc_netdev
*netdev
= data
;
5820 return ret_errno(EINVAL
);
5825 memset(retv
, 0, inlen
);
5827 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5832 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5833 struct lxc_conf
*c
, void *data
)
5837 struct lxc_netdev
*netdev
= data
;
5840 return ret_errno(EINVAL
);
5845 memset(retv
, 0, inlen
);
5847 if (netdev
->flags
& IFF_UP
)
5848 strprint(retv
, inlen
, "up");
5853 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5854 struct lxc_conf
*c
, void *data
)
5858 struct lxc_netdev
*netdev
= data
;
5861 return ret_errno(EINVAL
);
5866 memset(retv
, 0, inlen
);
5868 if (netdev
->link
[0] != '\0')
5869 strprint(retv
, inlen
, "%s", netdev
->link
);
5874 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5875 struct lxc_conf
*c
, void *data
)
5877 struct lxc_netdev
*netdev
= data
;
5880 return ret_errno(EINVAL
);
5882 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5885 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5886 struct lxc_conf
*c
, void *data
)
5890 struct lxc_netdev
*netdev
= data
;
5893 return ret_errno(EINVAL
);
5898 memset(retv
, 0, inlen
);
5900 if (netdev
->name
[0] != '\0')
5901 strprint(retv
, inlen
, "%s", netdev
->name
);
5906 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5907 struct lxc_conf
*c
, void *data
)
5912 struct lxc_netdev
*netdev
= data
;
5915 return ret_errno(EINVAL
);
5917 if (netdev
->type
!= LXC_NET_MACVLAN
)
5918 return ret_errno(EINVAL
);
5923 memset(retv
, 0, inlen
);
5925 switch (netdev
->priv
.macvlan_attr
.mode
) {
5926 case MACVLAN_MODE_PRIVATE
:
5929 case MACVLAN_MODE_VEPA
:
5932 case MACVLAN_MODE_BRIDGE
:
5935 case MACVLAN_MODE_PASSTHRU
:
5943 strprint(retv
, inlen
, "%s", mode
);
5948 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5949 struct lxc_conf
*c
, void *data
)
5952 struct lxc_netdev
*netdev
= data
;
5957 return ret_errno(EINVAL
);
5959 if (netdev
->type
!= LXC_NET_IPVLAN
)
5960 return ret_errno(EINVAL
);
5965 memset(retv
, 0, inlen
);
5967 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5968 case IPVLAN_MODE_L3
:
5971 case IPVLAN_MODE_L3S
:
5974 case IPVLAN_MODE_L2
:
5982 strprint(retv
, inlen
, "%s", mode
);
5987 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5988 struct lxc_conf
*c
, void *data
)
5991 struct lxc_netdev
*netdev
= data
;
5996 return ret_errno(EINVAL
);
5998 if (netdev
->type
!= LXC_NET_IPVLAN
)
5999 return ret_errno(EINVAL
);
6004 memset(retv
, 0, inlen
);
6006 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
6007 case IPVLAN_ISOLATION_BRIDGE
:
6010 case IPVLAN_ISOLATION_PRIVATE
:
6013 case IPVLAN_ISOLATION_VEPA
:
6021 strprint(retv
, inlen
, "%s", mode
);
6026 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
6027 struct lxc_conf
*c
, void *data
)
6030 struct lxc_netdev
*netdev
= data
;
6035 return ret_errno(EINVAL
);
6037 if (netdev
->type
!= LXC_NET_VETH
)
6038 return ret_errno(EINVAL
);
6043 memset(retv
, 0, inlen
);
6045 switch (netdev
->priv
.veth_attr
.mode
) {
6046 case VETH_MODE_BRIDGE
:
6049 case VETH_MODE_ROUTER
:
6057 strprint(retv
, inlen
, "%s", mode
);
6062 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
6063 struct lxc_conf
*c
, void *data
)
6067 struct lxc_netdev
*netdev
= data
;
6070 return ret_errno(EINVAL
);
6072 if (netdev
->type
!= LXC_NET_VETH
)
6073 return ret_errno(EINVAL
);
6078 memset(retv
, 0, inlen
);
6080 strprint(retv
, inlen
, "%s",
6081 netdev
->priv
.veth_attr
.pair
[0] != '\0'
6082 ? netdev
->priv
.veth_attr
.pair
6083 : netdev
->priv
.veth_attr
.veth1
);
6088 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
6089 struct lxc_conf
*c
, void *data
)
6093 struct lxc_netdev
*netdev
= data
;
6096 return ret_errno(EINVAL
);
6098 if (netdev
->type
!= LXC_NET_VETH
)
6099 return ret_errno(EINVAL
);
6104 memset(retv
, 0, inlen
);
6106 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
6111 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
,
6112 int inlen
, struct lxc_conf
*c
,
6117 struct lxc_list
*it
;
6119 struct lxc_netdev
*netdev
= data
;
6122 return ret_errno(EINVAL
);
6124 if (netdev
->type
!= LXC_NET_VETH
)
6125 return ret_errno(EINVAL
);
6130 memset(retv
, 0, inlen
);
6132 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
6134 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
6135 unsigned short i
= PTR_TO_USHORT(it
->elem
);
6136 strprint(retv
, inlen
, "%u%s", i
, (listlen
-- > 1) ? "\n" : "");
6142 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6143 struct lxc_conf
*c
, void *data
)
6147 struct lxc_netdev
*netdev
= data
;
6150 return ret_errno(EINVAL
);
6155 memset(retv
, 0, inlen
);
6157 if (netdev
->upscript
)
6158 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6163 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6164 struct lxc_conf
*c
, void *data
)
6168 struct lxc_netdev
*netdev
= data
;
6171 return ret_errno(EINVAL
);
6176 memset(retv
, 0, inlen
);
6178 if (netdev
->downscript
)
6179 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6184 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6185 struct lxc_conf
*c
, void *data
)
6189 struct lxc_netdev
*netdev
= data
;
6192 return ret_errno(EINVAL
);
6197 memset(retv
, 0, inlen
);
6200 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6205 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6206 struct lxc_conf
*c
, void *data
)
6210 struct lxc_netdev
*netdev
= data
;
6213 return ret_errno(EINVAL
);
6218 memset(retv
, 0, inlen
);
6221 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6226 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6227 struct lxc_conf
*c
, void *data
)
6231 struct lxc_netdev
*netdev
= data
;
6234 return ret_errno(EINVAL
);
6236 if (netdev
->type
!= LXC_NET_VLAN
)
6237 return ret_errno(EINVAL
);
6242 memset(retv
, 0, inlen
);
6244 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6249 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6250 struct lxc_conf
*c
, void *data
)
6253 char buf
[INET_ADDRSTRLEN
];
6255 struct lxc_netdev
*netdev
= data
;
6258 return ret_errno(EINVAL
);
6263 memset(retv
, 0, inlen
);
6265 if (netdev
->ipv4_gateway_auto
) {
6266 strprint(retv
, inlen
, "auto");
6267 } else if (netdev
->ipv4_gateway_dev
) {
6268 strprint(retv
, inlen
, "dev");
6269 } else if (netdev
->ipv4_gateway
) {
6270 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
)))
6272 strprint(retv
, inlen
, "%s", buf
);
6278 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6279 struct lxc_conf
*c
, void *data
)
6282 struct lxc_netdev
*netdev
= data
;
6285 char buf
[INET_ADDRSTRLEN
];
6286 struct lxc_inetdev
*inetdev
;
6289 return ret_errno(EINVAL
);
6294 memset(retv
, 0, inlen
);
6296 listlen
= list_len(inetdev
, &netdev
->ipv4_addresses
, head
);
6298 list_for_each_entry(inetdev
, &netdev
->ipv4_addresses
, head
) {
6299 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6301 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6302 (listlen
-- > 1) ? "\n" : "");
6308 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6309 struct lxc_conf
*c
, void *data
)
6313 char buf
[INET_ADDRSTRLEN
];
6314 struct lxc_inetdev
*inetdev
;
6316 struct lxc_netdev
*netdev
= data
;
6319 return ret_errno(EINVAL
);
6321 if (netdev
->type
!= LXC_NET_VETH
)
6322 return ret_errno(EINVAL
);
6327 memset(retv
, 0, inlen
);
6329 listlen
= list_len(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
);
6330 list_for_each_entry(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
6331 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6333 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6334 (listlen
-- > 1) ? "\n" : "");
6340 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6341 struct lxc_conf
*c
, void *data
)
6344 char buf
[INET6_ADDRSTRLEN
];
6346 struct lxc_netdev
*netdev
= data
;
6349 return ret_errno(EINVAL
);
6354 memset(retv
, 0, inlen
);
6356 if (netdev
->ipv6_gateway_auto
) {
6357 strprint(retv
, inlen
, "auto");
6358 } else if (netdev
->ipv6_gateway_dev
) {
6359 strprint(retv
, inlen
, "dev");
6360 } else if (netdev
->ipv6_gateway
) {
6361 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
)))
6363 strprint(retv
, inlen
, "%s", buf
);
6369 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6370 struct lxc_conf
*c
, void *data
)
6374 char buf
[INET6_ADDRSTRLEN
];
6376 struct lxc_netdev
*netdev
= data
;
6377 struct lxc_inet6dev
*inet6dev
;
6380 return ret_errno(EINVAL
);
6385 memset(retv
, 0, inlen
);
6387 listlen
= list_len(inet6dev
, &netdev
->ipv6_addresses
, head
);
6388 list_for_each_entry(inet6dev
, &netdev
->ipv6_addresses
, head
) {
6389 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6391 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6392 (listlen
-- > 1) ? "\n" : "");
6398 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6399 struct lxc_conf
*c
, void *data
)
6403 char buf
[INET6_ADDRSTRLEN
];
6404 struct lxc_inet6dev
*inet6dev
;
6406 struct lxc_netdev
*netdev
= data
;
6409 return ret_errno(EINVAL
);
6411 if (netdev
->type
!= LXC_NET_VETH
)
6412 return ret_errno(EINVAL
);
6417 memset(retv
, 0, inlen
);
6419 listlen
= list_len(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
);
6420 list_for_each_entry(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
6421 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6423 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6424 (listlen
-- > 1) ? "\n" : "");
6430 int lxc_list_config_items(char *retv
, int inlen
)
6439 memset(retv
, 0, inlen
);
6441 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
6442 char *s
= config_jump_table
[i
].name
;
6444 if (s
[strlen(s
) - 1] == '.')
6447 strprint(retv
, inlen
, "%s\n", s
);
6453 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6462 memset(retv
, 0, inlen
);
6464 if (strequal(key
, "lxc.apparmor")) {
6465 strprint(retv
, inlen
, "allow_incomplete\n");
6466 strprint(retv
, inlen
, "allow_nesting\n");
6467 strprint(retv
, inlen
, "profile\n");
6468 strprint(retv
, inlen
, "raw\n");
6469 } else if (strequal(key
, "lxc.cgroup")) {
6470 strprint(retv
, inlen
, "dir\n");
6471 } else if (strequal(key
, "lxc.selinux")) {
6472 strprint(retv
, inlen
, "context\n");
6473 strprint(retv
, inlen
, "context.keyring\n");
6474 } else if (strequal(key
, "lxc.mount")) {
6475 strprint(retv
, inlen
, "auto\n");
6476 strprint(retv
, inlen
, "entry\n");
6477 strprint(retv
, inlen
, "fstab\n");
6478 } else if (strequal(key
, "lxc.rootfs")) {
6479 strprint(retv
, inlen
, "mount\n");
6480 strprint(retv
, inlen
, "options\n");
6481 strprint(retv
, inlen
, "path\n");
6482 } else if (strequal(key
, "lxc.uts")) {
6483 strprint(retv
, inlen
, "name\n");
6484 } else if (strequal(key
, "lxc.hook")) {
6485 strprint(retv
, inlen
, "autodev\n");
6486 strprint(retv
, inlen
, "autodevtmpfssize\n");
6487 strprint(retv
, inlen
, "clone\n");
6488 strprint(retv
, inlen
, "destroy\n");
6489 strprint(retv
, inlen
, "mount\n");
6490 strprint(retv
, inlen
, "post-stop\n");
6491 strprint(retv
, inlen
, "pre-mount\n");
6492 strprint(retv
, inlen
, "pre-start\n");
6493 strprint(retv
, inlen
, "start-host\n");
6494 strprint(retv
, inlen
, "start\n");
6495 strprint(retv
, inlen
, "stop\n");
6496 } else if (strequal(key
, "lxc.cap")) {
6497 strprint(retv
, inlen
, "drop\n");
6498 strprint(retv
, inlen
, "keep\n");
6499 } else if (strequal(key
, "lxc.console")) {
6500 strprint(retv
, inlen
, "logfile\n");
6501 strprint(retv
, inlen
, "path\n");
6502 } else if (strequal(key
, "lxc.seccomp")) {
6503 strprint(retv
, inlen
, "profile\n");
6504 } else if (strequal(key
, "lxc.signal")) {
6505 strprint(retv
, inlen
, "halt\n");
6506 strprint(retv
, inlen
, "reboot\n");
6507 strprint(retv
, inlen
, "stop\n");
6508 } else if (strequal(key
, "lxc.start")) {
6509 strprint(retv
, inlen
, "auto\n");
6510 strprint(retv
, inlen
, "delay\n");
6511 strprint(retv
, inlen
, "order\n");
6512 } else if (strequal(key
, "lxc.monitor")) {
6513 strprint(retv
, inlen
, "unshare\n");
6514 } else if (strequal(key
, "lxc.keyring")) {
6515 strprint(retv
, inlen
, "session\n");
6517 fulllen
= ret_errno(EINVAL
);
6523 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6525 struct config_net_info info
= {};
6526 struct lxc_netdev
*netdev
;
6528 const char *idxstring
;
6531 idxstring
= key
+ STRLITERALLEN("lxc.net.");
6532 if (!isdigit(*idxstring
))
6533 return ret_errno(EINVAL
);
6535 ret
= get_network_config_ops(key
, c
, &info
, false);
6538 return ret_errno(EINVAL
);
6540 netdev
= info
.netdev
;
6545 memset(retv
, 0, inlen
);
6547 strprint(retv
, inlen
, "type\n");
6548 strprint(retv
, inlen
, "script.up\n");
6549 strprint(retv
, inlen
, "script.down\n");
6551 if (netdev
->type
!= LXC_NET_EMPTY
) {
6552 strprint(retv
, inlen
, "flags\n");
6553 strprint(retv
, inlen
, "link\n");
6554 strprint(retv
, inlen
, "name\n");
6555 strprint(retv
, inlen
, "hwaddr\n");
6556 strprint(retv
, inlen
, "mtu\n");
6557 strprint(retv
, inlen
, "ipv6.address\n");
6558 strprint(retv
, inlen
, "ipv6.gateway\n");
6559 strprint(retv
, inlen
, "ipv4.address\n");
6560 strprint(retv
, inlen
, "ipv4.gateway\n");
6563 switch (netdev
->type
) {
6565 strprint(retv
, inlen
, "veth.pair\n");
6566 strprint(retv
, inlen
, "veth.ipv4.route\n");
6567 strprint(retv
, inlen
, "veth.ipv6.route\n");
6568 strprint(retv
, inlen
, "veth.vlan.id\n");
6570 case LXC_NET_MACVLAN
:
6571 strprint(retv
, inlen
, "macvlan.mode\n");
6573 case LXC_NET_IPVLAN
:
6574 strprint(retv
, inlen
, "ipvlan.mode\n");
6575 strprint(retv
, inlen
, "ipvlan.isolation\n");
6578 strprint(retv
, inlen
, "vlan.id\n");