1 /* SPDX-License-Identifier: LGPL-2.1+ */
12 #include <netinet/in.h>
17 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/utsname.h>
28 #include "confile_utils.h"
29 #include "netns_ifaddrs.h"
31 #include "lxcseccomp.h"
33 #include "memory_utils.h"
46 #if HAVE_SYS_RESOURCE_H
47 #include <sys/resource.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
);
157 lxc_config_define(sched_core
);
159 static int set_config_unsupported_key(const char *key
, const char *value
,
160 struct lxc_conf
*lxc_conf
, void *data
)
162 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
165 static int get_config_unsupported_key(const char *key
, char *retv
, int inlen
,
166 struct lxc_conf
*c
, void *data
)
168 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
171 static int clr_config_unsupported_key(const char *key
,
172 struct lxc_conf
*lxc_conf
, void *data
)
174 return syserror_set(-EINVAL
, "Unsupported config key \"%s\"", key
);
179 * If a new config option is added to this table, be aware that
180 * the order in which the options are places into the table matters.
181 * That means that more specific options of a namespace have to be
182 * placed above more generic ones.
184 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
185 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
186 * has to be placed above lxc.ab.
188 static struct lxc_config_t config_jump_table
[] = {
189 { "lxc.arch", true, set_config_personality
, get_config_personality
, clr_config_personality
, },
190 { "lxc.apparmor.profile", true, set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
191 { "lxc.apparmor.allow_incomplete", true, set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
192 { "lxc.apparmor.allow_nesting", true, set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
193 { "lxc.apparmor.raw", true, set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
194 { "lxc.autodev.tmpfs.size", true, set_config_autodev_tmpfs_size
, get_config_autodev_tmpfs_size
, clr_config_autodev_tmpfs_size
, },
195 { "lxc.autodev", true, set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
196 { "lxc.cap.drop", true, set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
197 { "lxc.cap.keep", true, set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
198 { "lxc.cgroup2", false, set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
199 { "lxc.cgroup.dir.monitor.pivot", true, set_config_cgroup_monitor_pivot_dir
, get_config_cgroup_monitor_pivot_dir
, clr_config_cgroup_monitor_pivot_dir
, },
200 { "lxc.cgroup.dir.monitor", true, set_config_cgroup_monitor_dir
, get_config_cgroup_monitor_dir
, clr_config_cgroup_monitor_dir
, },
201 { "lxc.cgroup.dir.container.inner", true, set_config_cgroup_container_inner_dir
, get_config_cgroup_container_inner_dir
, clr_config_cgroup_container_inner_dir
, },
202 { "lxc.cgroup.dir.container", true, set_config_cgroup_container_dir
, get_config_cgroup_container_dir
, clr_config_cgroup_container_dir
, },
203 { "lxc.cgroup.dir", true, set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
204 { "lxc.cgroup.relative", true, set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
205 { "lxc.cgroup", false, set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
206 { "lxc.console.buffer.size", true, set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
207 { "lxc.console.logfile", true, set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
208 { "lxc.console.path", true, set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
209 { "lxc.console.rotate", true, set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
210 { "lxc.console.size", true, set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
211 { "lxc.sched.core", true, set_config_sched_core
, get_config_sched_core
, clr_config_sched_core
, },
212 { "lxc.environment", true, set_config_environment
, get_config_environment
, clr_config_environment
, },
213 { "lxc.ephemeral", true, set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
214 { "lxc.execute.cmd", true, set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
215 { "lxc.group", true, set_config_group
, get_config_group
, clr_config_group
, },
216 { "lxc.hook.autodev", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
217 { "lxc.hook.clone", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
218 { "lxc.hook.destroy", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
219 { "lxc.hook.mount", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
220 { "lxc.hook.post-stop", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
221 { "lxc.hook.pre-mount", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
222 { "lxc.hook.pre-start", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
223 { "lxc.hook.start", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
224 { "lxc.hook.start-host", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
225 { "lxc.hook.stop", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
226 { "lxc.hook.version", true, set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
227 { "lxc.hook", true, set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
228 { "lxc.idmap", true, set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
229 { "lxc.include", true, set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
230 { "lxc.init.cmd", true, set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
231 { "lxc.init.gid", true, set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
232 { "lxc.init.groups", true, set_config_init_groups
, get_config_init_groups
, clr_config_init_groups
, },
233 { "lxc.init.uid", true, set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
234 { "lxc.init.cwd", true, set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
235 { "lxc.keyring.session", true, set_config_keyring_session
, get_config_keyring_session
, clr_config_keyring_session
},
236 { "lxc.log.file", true, set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
237 { "lxc.log.level", true, set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
238 { "lxc.log.syslog", true, set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
239 { "lxc.monitor.unshare", true, set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
240 { "lxc.monitor.signal.pdeath", true, set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
241 { "lxc.mount.auto", true, set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
242 { "lxc.mount.entry", true, set_config_mount
, get_config_mount
, clr_config_mount
, },
243 { "lxc.mount.fstab", true, set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
244 { "lxc.namespace.clone", true, set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
245 { "lxc.namespace.keep", true, set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
246 { "lxc.namespace.share.", false, set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
247 { "lxc.time.offset.boot", true, set_config_time_offset_boot
, get_config_time_offset_boot
, clr_config_time_offset_boot
, },
248 { "lxc.time.offset.monotonic", true, set_config_time_offset_monotonic
, get_config_time_offset_monotonic
, clr_config_time_offset_monotonic
, },
249 { "lxc.net.", false, set_config_jump_table_net
, get_config_jump_table_net
, clr_config_jump_table_net
, },
250 { "lxc.net", true, set_config_net
, get_config_net
, clr_config_net
, },
251 { "lxc.no_new_privs", true, set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
252 { "lxc.prlimit", false, set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
253 { "lxc.pty.max", true, set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
254 { "lxc.rootfs.managed", true, set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
255 { "lxc.rootfs.mount", true, set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
256 { "lxc.rootfs.options", true, set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
257 { "lxc.rootfs.path", true, set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
258 { "lxc.seccomp.allow_nesting", true, set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
259 { "lxc.seccomp.notify.cookie", true, set_config_seccomp_notify_cookie
, get_config_seccomp_notify_cookie
, clr_config_seccomp_notify_cookie
, },
260 { "lxc.seccomp.notify.proxy", true, set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
261 { "lxc.seccomp.profile", true, set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
262 { "lxc.selinux.context.keyring", true, set_config_selinux_context_keyring
, get_config_selinux_context_keyring
, clr_config_selinux_context_keyring
},
263 { "lxc.selinux.context", true, set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
264 { "lxc.signal.halt", true, set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
265 { "lxc.signal.reboot", true, set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
266 { "lxc.signal.stop", true, set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
267 { "lxc.start.auto", true, set_config_start
, get_config_start
, clr_config_start
, },
268 { "lxc.start.delay", true, set_config_start
, get_config_start
, clr_config_start
, },
269 { "lxc.start.order", true, set_config_start
, get_config_start
, clr_config_start
, },
270 { "lxc.tty.dir", true, set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
271 { "lxc.tty.max", true, set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
272 { "lxc.uts.name", true, set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
273 { "lxc.sysctl", false, set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
274 { "lxc.proc", false, set_config_proc
, get_config_proc
, clr_config_proc
, },
277 static struct lxc_config_t unsupported_config_key
= {
280 set_config_unsupported_key
,
281 get_config_unsupported_key
,
282 clr_config_unsupported_key
,
285 struct lxc_config_net_t
{
289 static struct lxc_config_net_t config_jump_table_net
[] = {
290 /* If a longer key is added please update. */
291 #define NETWORK_SUBKEY_SIZE_MAX (STRLITERALLEN("veth.vlan.tagged.id") * 2)
292 { "flags", true, set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
293 { "hwaddr", true, set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
294 { "ipv4.address", true, set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
295 { "ipv4.gateway", true, set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
296 { "ipv6.address", true, set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
297 { "ipv6.gateway", true, set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
298 { "link", true, set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
299 { "l2proxy", true, set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
300 { "macvlan.mode", true, set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
301 { "ipvlan.mode", true, set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
302 { "ipvlan.isolation", true, set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
303 { "mtu", true, set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
304 { "name", true, set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
305 { "script.down", true, set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
306 { "script.up", true, set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
307 { "type", true, set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
308 { "vlan.id", true, set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
309 { "veth.mode", true, set_config_net_veth_mode
, get_config_net_veth_mode
, clr_config_net_veth_mode
, },
310 { "veth.pair", true, set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
311 { "veth.ipv4.route", true, set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
312 { "veth.ipv6.route", true, set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
313 { "veth.vlan.id", true, set_config_net_veth_vlan_id
, get_config_net_veth_vlan_id
, clr_config_net_veth_vlan_id
, },
314 { "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
, },
317 static struct lxc_config_net_t unsupported_config_net_key
= {
320 set_config_unsupported_key
,
321 get_config_unsupported_key
,
322 clr_config_unsupported_key
,
325 struct lxc_config_t
*lxc_get_config_exact(const char *key
)
329 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++)
330 if (strequal(config_jump_table
[i
].name
, key
))
331 return &config_jump_table
[i
];
336 /* Assume a reasonable subkey size limit. */
337 #define LXC_SUBKEY_LEN_MAX 256
339 static inline int match_config_item(const struct lxc_config_t
*entry
, const char *key
)
344 return strequal(entry
->name
, key
);
346 /* There should be no subkey longer than this. */
347 len
= strnlen(entry
->name
, LXC_SUBKEY_LEN_MAX
);
348 if (len
== LXC_SUBKEY_LEN_MAX
)
349 return error_ret(-E2BIG
, "Excessive subkey length");
351 return strnequal(entry
->name
, key
, len
);
354 struct lxc_config_t
*lxc_get_config(const char *key
)
356 for (size_t i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
357 struct lxc_config_t
*cur
= &config_jump_table
[i
];
359 switch (match_config_item(cur
, key
)) {
363 return &unsupported_config_key
;
369 return &unsupported_config_key
;
372 static inline bool match_config_net_item(const struct lxc_config_net_t
*entry
,
376 return strequal(entry
->name
, key
);
377 return strnequal(entry
->name
, key
, strlen(entry
->name
));
380 static struct lxc_config_net_t
*lxc_get_config_net(const char *key
)
382 for (size_t i
= 0; i
< ARRAY_SIZE(config_jump_table_net
); i
++) {
383 struct lxc_config_net_t
*cur
= &config_jump_table_net
[i
];
385 if (!match_config_net_item(cur
, key
))
391 return &unsupported_config_net_key
;
394 static int set_config_net(const char *key
, const char *value
,
395 struct lxc_conf
*lxc_conf
, void *data
)
397 if (!lxc_config_value_empty(value
))
398 return syserror_set(-EINVAL
, "lxc.net must not have a value");
400 return clr_config_net(key
, lxc_conf
, data
);
403 static int set_config_net_type(const char *key
, const char *value
,
404 struct lxc_conf
*lxc_conf
, void *data
)
406 struct lxc_netdev
*netdev
= data
;
409 return ret_errno(EINVAL
);
411 clr_config_net_type(key
, lxc_conf
, data
);
412 if (lxc_config_value_empty(value
))
415 if (strequal(value
, "veth")) {
416 netdev
->type
= LXC_NET_VETH
;
417 INIT_LIST_HEAD(&netdev
->priv
.veth_attr
.ipv4_routes
);
418 INIT_LIST_HEAD(&netdev
->priv
.veth_attr
.ipv6_routes
);
419 lxc_list_init(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
420 if (!lxc_veth_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
421 lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, "bridge");
422 } else if (strequal(value
, "macvlan")) {
423 netdev
->type
= LXC_NET_MACVLAN
;
424 if (!lxc_macvlan_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
425 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
426 } else if (strequal(value
, "ipvlan")) {
427 netdev
->type
= LXC_NET_IPVLAN
;
428 if (!lxc_ipvlan_flag_to_mode(netdev
->priv
.ipvlan_attr
.mode
))
429 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
430 if (!lxc_ipvlan_flag_to_isolation(netdev
->priv
.ipvlan_attr
.isolation
))
431 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
432 } else if (strequal(value
, "vlan")) {
433 netdev
->type
= LXC_NET_VLAN
;
434 } else if (strequal(value
, "phys")) {
435 netdev
->type
= LXC_NET_PHYS
;
436 } else if (strequal(value
, "empty")) {
437 netdev
->type
= LXC_NET_EMPTY
;
438 /* We don't support custom loopback device names. */
439 (void)strlcpy(netdev
->name
, "lo", IFNAMSIZ
);
440 } else if (strequal(value
, "none")) {
441 netdev
->type
= LXC_NET_NONE
;
443 return log_error(-1, "Invalid network type %s", value
);
449 static int set_config_net_flags(const char *key
, const char *value
,
450 struct lxc_conf
*lxc_conf
, void *data
)
452 struct lxc_netdev
*netdev
= data
;
455 return ret_errno(EINVAL
);
457 if (lxc_config_value_empty(value
))
458 return clr_config_net_flags(key
, lxc_conf
, data
);
460 netdev
->flags
|= IFF_UP
;
465 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
466 struct lxc_netdev
*netdev
)
468 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddr
= NULL
;
469 struct netns_ifaddrs
*ifa
;
472 const char *type_key
= "lxc.net.type";
473 const char *link_key
= "lxc.net.link";
474 const char *tmpvalue
= "phys";
476 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0)
477 return log_error_errno(-1, errno
, "Failed to get network interfaces");
479 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
483 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
486 if (strnequal(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
487 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
490 ret
= set_config_net_link(
491 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
493 ERROR("Failed to create matched ifnames");
497 ERROR("Failed to create matched ifnames");
506 static int set_config_net_link(const char *key
, const char *value
,
507 struct lxc_conf
*lxc_conf
, void *data
)
509 struct lxc_netdev
*netdev
= data
;
513 return ret_errno(EINVAL
);
515 if (lxc_config_value_empty(value
))
516 return clr_config_net_link(key
, lxc_conf
, data
);
518 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
519 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
521 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
526 static int set_config_net_l2proxy(const char *key
, const char *value
,
527 struct lxc_conf
*lxc_conf
, void *data
)
529 struct lxc_netdev
*netdev
= data
;
530 unsigned int val
= 0;
534 return ret_errno(EINVAL
);
536 if (lxc_config_value_empty(value
))
537 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
539 ret
= lxc_safe_uint(value
, &val
);
541 return ret_errno(ret
);
545 netdev
->l2proxy
= false;
548 netdev
->l2proxy
= true;
552 return ret_errno(EINVAL
);
555 static int set_config_net_name(const char *key
, const char *value
,
556 struct lxc_conf
*lxc_conf
, void *data
)
558 struct lxc_netdev
*netdev
= data
;
561 return ret_errno(EINVAL
);
563 if (lxc_config_value_empty(value
))
564 return clr_config_net_name(key
, lxc_conf
, data
);
566 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
570 static int set_config_net_veth_mode(const char *key
, const char *value
,
571 struct lxc_conf
*lxc_conf
, void *data
)
573 struct lxc_netdev
*netdev
= data
;
576 return ret_errno(EINVAL
);
578 if (netdev
->type
!= LXC_NET_VETH
)
579 return ret_errno(EINVAL
);
581 if (lxc_config_value_empty(value
))
582 return clr_config_net_veth_mode(key
, lxc_conf
, data
);
585 return ret_errno(EINVAL
);
587 return lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, value
);
590 static int set_config_net_veth_pair(const char *key
, const char *value
,
591 struct lxc_conf
*lxc_conf
, void *data
)
593 struct lxc_netdev
*netdev
= data
;
596 return ret_errno(EINVAL
);
598 if (netdev
->type
!= LXC_NET_VETH
)
599 return ret_errno(EINVAL
);
601 if (lxc_config_value_empty(value
))
602 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
604 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
,
605 sizeof(netdev
->priv
.veth_attr
.pair
));
608 static int set_config_net_veth_vlan_id(const char *key
, const char *value
,
609 struct lxc_conf
*lxc_conf
, void *data
)
612 struct lxc_netdev
*netdev
= data
;
615 return ret_errno(EINVAL
);
617 if (netdev
->type
!= LXC_NET_VETH
)
618 return ret_errno(EINVAL
);
620 if (lxc_config_value_empty(value
))
621 return clr_config_net_veth_vlan_id(key
, lxc_conf
, data
);
623 if (strequal(value
, "none")) {
624 netdev
->priv
.veth_attr
.vlan_id
= BRIDGE_VLAN_NONE
;
626 unsigned short vlan_id
;
628 ret
= get_u16(&vlan_id
, value
, 0);
630 return ret_errno(EINVAL
);
632 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
633 return ret_errno(EINVAL
);
635 netdev
->priv
.veth_attr
.vlan_id
= vlan_id
;
638 netdev
->priv
.veth_attr
.vlan_id_set
= true;
642 static int set_config_net_veth_vlan_tagged_id(const char *key
, const char *value
,
643 struct lxc_conf
*lxc_conf
,
646 __do_free
struct lxc_list
*list
= NULL
;
648 unsigned short vlan_id
;
649 struct lxc_netdev
*netdev
= data
;
652 return ret_errno(EINVAL
);
654 if (netdev
->type
!= LXC_NET_VETH
)
655 return ret_errno(EINVAL
);
657 if (lxc_config_value_empty(value
))
658 return clr_config_net_veth_vlan_tagged_id(key
, lxc_conf
, data
);
660 ret
= get_u16(&vlan_id
, value
, 0);
662 return ret_errno(EINVAL
);
664 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
665 return ret_errno(EINVAL
);
667 list
= lxc_list_new();
669 return ret_errno(ENOMEM
);
671 list
->elem
= UINT_TO_PTR(vlan_id
);
673 lxc_list_add_tail(&netdev
->priv
.veth_attr
.vlan_tagged_ids
, move_ptr(list
));
678 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
679 struct lxc_conf
*lxc_conf
, void *data
)
681 struct lxc_netdev
*netdev
= data
;
684 return ret_errno(EINVAL
);
686 if (netdev
->type
!= LXC_NET_MACVLAN
)
687 return ret_errno(EINVAL
);
689 if (lxc_config_value_empty(value
))
690 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
692 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
695 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
696 struct lxc_conf
*lxc_conf
, void *data
)
698 struct lxc_netdev
*netdev
= data
;
701 return ret_errno(EINVAL
);
703 if (netdev
->type
!= LXC_NET_IPVLAN
)
704 return syserror_set(-EINVAL
, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network", value
);
706 if (lxc_config_value_empty(value
))
707 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
709 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
712 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
713 struct lxc_conf
*lxc_conf
, void *data
)
715 struct lxc_netdev
*netdev
= data
;
718 return ret_errno(EINVAL
);
720 if (netdev
->type
!= LXC_NET_IPVLAN
)
721 return syserror_set(-EINVAL
, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network", value
);
723 if (lxc_config_value_empty(value
))
724 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
726 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
729 static int set_config_net_hwaddr(const char *key
, const char *value
,
730 struct lxc_conf
*lxc_conf
, void *data
)
732 __do_free
char *new_value
= NULL
;
733 struct lxc_netdev
*netdev
= data
;
736 return ret_errno(EINVAL
);
738 clr_config_net_hwaddr(key
, lxc_conf
, data
);
739 if (lxc_config_value_empty(value
))
742 new_value
= strdup(value
);
744 return ret_errno(ENOMEM
);
746 rand_complete_hwaddr(new_value
);
747 if (!lxc_config_value_empty(new_value
))
748 netdev
->hwaddr
= move_ptr(new_value
);
753 static int set_config_net_vlan_id(const char *key
, const char *value
,
754 struct lxc_conf
*lxc_conf
, void *data
)
757 struct lxc_netdev
*netdev
= data
;
760 return ret_errno(EINVAL
);
762 if (netdev
->type
!= LXC_NET_VLAN
)
763 return ret_errno(EINVAL
);
765 if (lxc_config_value_empty(value
))
766 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
768 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
775 static int set_config_net_mtu(const char *key
, const char *value
,
776 struct lxc_conf
*lxc_conf
, void *data
)
778 struct lxc_netdev
*netdev
= data
;
781 return ret_errno(EINVAL
);
783 clr_config_net_mtu(key
, lxc_conf
, data
);
784 if (lxc_config_value_empty(value
))
787 return set_config_string_item(&netdev
->mtu
, value
);
790 static int set_config_net_ipv4_address(const char *key
, const char *value
,
791 struct lxc_conf
*lxc_conf
, void *data
)
793 __do_free
char *addr
= NULL
;
794 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
796 struct lxc_netdev
*netdev
= data
;
797 char *cursor
, *slash
;
798 char *bcast
= NULL
, *prefix
= NULL
;
801 return ret_errno(EINVAL
);
803 if (lxc_config_value_empty(value
))
804 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
806 inetdev
= zalloc(sizeof(*inetdev
));
808 return ret_errno(ENOMEM
);
810 addr
= strdup(value
);
812 return ret_errno(ENOMEM
);
814 cursor
= strstr(addr
, " ");
820 slash
= strstr(addr
, "/");
826 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
828 return log_error_errno(-1, errno
, "Invalid ipv4 address \"%s\"", value
);
831 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
833 return log_error_errno(-1, errno
, "Invalid ipv4 broadcast address \"%s\"", value
);
837 /* No prefix specified, determine it from the network class. */
840 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
842 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
843 if (ret
|| inetdev
->prefix
> 32)
844 return ret_errno(EINVAL
);
846 /* If no broadcast address, compute one from the prefix and address. */
848 unsigned int shift
= LAST_BIT_PER_TYPE(inetdev
->prefix
);
850 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
851 if (inetdev
->prefix
< shift
)
852 shift
= inetdev
->prefix
;
853 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> shift
);
856 list_add_tail(&inetdev
->head
, &netdev
->ipv4_addresses
);
862 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
863 struct lxc_conf
*lxc_conf
, void *data
)
865 struct lxc_netdev
*netdev
= data
;
868 return ret_errno(EINVAL
);
870 clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
871 if (lxc_config_value_empty(value
))
874 if (strequal(value
, "auto")) {
875 netdev
->ipv4_gateway
= NULL
;
876 netdev
->ipv4_gateway_auto
= true;
877 } else if (strequal(value
, "dev")) {
878 netdev
->ipv4_gateway
= NULL
;
879 netdev
->ipv4_gateway_auto
= false;
880 netdev
->ipv4_gateway_dev
= true;
882 __do_free
struct in_addr
*gw
= NULL
;
885 gw
= zalloc(sizeof(*gw
));
887 return ret_errno(ENOMEM
);
889 ret
= inet_pton(AF_INET
, value
, gw
);
891 return log_error_errno(-1, errno
, "Invalid ipv4 gateway address \"%s\"", value
);
893 netdev
->ipv4_gateway
= move_ptr(gw
);
894 netdev
->ipv4_gateway_auto
= false;
900 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
901 struct lxc_conf
*lxc_conf
, void *data
)
903 __do_free
char *valdup
= NULL
;
904 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
906 char *netmask
, *slash
;
907 struct lxc_netdev
*netdev
= data
;
910 return ret_errno(EINVAL
);
912 if (netdev
->type
!= LXC_NET_VETH
)
913 return syserror_set(-EINVAL
, "Invalid ipv4 route \"%s\", can only be used with veth network", value
);
915 if (lxc_config_value_empty(value
))
916 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
918 inetdev
= zalloc(sizeof(*inetdev
));
920 return ret_errno(ENOMEM
);
922 valdup
= strdup(value
);
924 return ret_errno(ENOMEM
);
926 slash
= strchr(valdup
, '/');
928 return ret_errno(EINVAL
);
933 return ret_errno(EINVAL
);
937 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
938 if (ret
< 0 || inetdev
->prefix
> 32)
939 return ret_errno(EINVAL
);
941 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
943 return ret_errno(EINVAL
);
945 list_add_tail(&inetdev
->head
, &netdev
->priv
.veth_attr
.ipv4_routes
);
951 static int set_config_net_ipv6_address(const char *key
, const char *value
,
952 struct lxc_conf
*lxc_conf
, void *data
)
954 __do_free
char *valdup
= NULL
;
955 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
957 struct lxc_netdev
*netdev
= data
;
958 char *slash
, *netmask
;
961 return ret_errno(EINVAL
);
963 if (lxc_config_value_empty(value
))
964 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
966 inet6dev
= zalloc(sizeof(*inet6dev
));
968 return ret_errno(ENOMEM
);
970 valdup
= strdup(value
);
972 return ret_errno(ENOMEM
);
974 inet6dev
->prefix
= 64;
975 slash
= strstr(valdup
, "/");
980 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
985 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
987 return log_error_errno(-EINVAL
, EINVAL
, "Invalid ipv6 address \"%s\"", valdup
);
989 list_add_tail(&inet6dev
->head
, &netdev
->ipv6_addresses
);
995 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
996 struct lxc_conf
*lxc_conf
, void *data
)
998 struct lxc_netdev
*netdev
= data
;
1001 return ret_errno(EINVAL
);
1003 clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
1004 if (lxc_config_value_empty(value
))
1007 if (strequal(value
, "auto")) {
1008 netdev
->ipv6_gateway
= NULL
;
1009 netdev
->ipv6_gateway_auto
= true;
1010 } else if (strequal(value
, "dev")) {
1011 netdev
->ipv6_gateway
= NULL
;
1012 netdev
->ipv6_gateway_auto
= false;
1013 netdev
->ipv6_gateway_dev
= true;
1016 __do_free
struct in6_addr
*gw
= NULL
;
1018 gw
= zalloc(sizeof(*gw
));
1020 return ret_errno(ENOMEM
);
1022 ret
= inet_pton(AF_INET6
, value
, gw
);
1023 if (!ret
|| ret
< 0)
1024 return log_error_errno(-EINVAL
, EINVAL
,
1025 "Invalid ipv6 gateway address \"%s\"", value
);
1027 netdev
->ipv6_gateway
= move_ptr(gw
);
1028 netdev
->ipv6_gateway_auto
= false;
1034 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
1035 struct lxc_conf
*lxc_conf
, void *data
)
1037 __do_free
char *valdup
= NULL
;
1038 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
1040 char *netmask
, *slash
;
1041 struct lxc_netdev
*netdev
= data
;
1044 return ret_errno(EINVAL
);
1046 if (netdev
->type
!= LXC_NET_VETH
)
1047 return syserror_set(-EINVAL
, "Invalid ipv6 route \"%s\", can only be used with veth network", value
);
1049 if (lxc_config_value_empty(value
))
1050 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
1052 inet6dev
= zalloc(sizeof(*inet6dev
));
1054 return ret_errno(ENOMEM
);
1056 valdup
= strdup(value
);
1058 return ret_errno(ENOMEM
);
1060 slash
= strchr(valdup
, '/');
1062 return ret_errno(EINVAL
);
1067 return ret_errno(EINVAL
);
1071 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
1072 if (ret
< 0 || inet6dev
->prefix
> 128)
1073 return ret_errno(EINVAL
);
1075 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
1076 if (!ret
|| ret
< 0)
1077 return ret_errno(EINVAL
);
1079 list_add_tail(&inet6dev
->head
, &netdev
->priv
.veth_attr
.ipv6_routes
);
1085 static int set_config_net_script_up(const char *key
, const char *value
,
1086 struct lxc_conf
*lxc_conf
, void *data
)
1088 struct lxc_netdev
*netdev
= data
;
1091 return ret_errno(EINVAL
);
1093 clr_config_net_script_up(key
, lxc_conf
, data
);
1094 if (lxc_config_value_empty(value
))
1097 return set_config_string_item(&netdev
->upscript
, value
);
1100 static int set_config_net_script_down(const char *key
, const char *value
,
1101 struct lxc_conf
*lxc_conf
, void *data
)
1103 struct lxc_netdev
*netdev
= data
;
1106 return ret_errno(EINVAL
);
1108 clr_config_net_script_down(key
, lxc_conf
, data
);
1109 if (lxc_config_value_empty(value
))
1112 return set_config_string_item(&netdev
->downscript
, value
);
1115 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, __owns
char *hook
)
1117 __do_free
char *val
= hook
;
1118 __do_free
struct string_entry
*entry
;
1120 entry
= zalloc(sizeof(struct string_entry
));
1122 return ret_errno(ENOMEM
);
1124 entry
->val
= move_ptr(val
);
1125 list_add_tail(&entry
->head
, &lxc_conf
->hooks
[which
]);
1131 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1132 struct lxc_conf
*lxc_conf
, void *data
)
1135 if (lxc_config_value_empty(value
))
1136 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1138 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1141 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1142 return ret_errno(EINVAL
);
1146 return ret_errno(ENOSYS
);
1150 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1151 struct lxc_conf
*lxc_conf
, void *data
)
1153 #ifdef HAVE_SECCOMP_NOTIFY
1154 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1156 return ret_errno(ENOSYS
);
1160 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1161 struct lxc_conf
*lxc_conf
, void *data
)
1163 #ifdef HAVE_SECCOMP_NOTIFY
1166 if (lxc_config_value_empty(value
))
1167 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1169 if (!strnequal(value
, "unix:", 5))
1170 return ret_errno(EINVAL
);
1173 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1178 return ret_errno(ENOSYS
);
1182 static int set_config_seccomp_profile(const char *key
, const char *value
,
1183 struct lxc_conf
*lxc_conf
, void *data
)
1186 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1188 return ret_errno(ENOSYS
);
1192 static int set_config_execute_cmd(const char *key
, const char *value
,
1193 struct lxc_conf
*lxc_conf
, void *data
)
1195 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1198 static int set_config_init_cmd(const char *key
, const char *value
,
1199 struct lxc_conf
*lxc_conf
, void *data
)
1201 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1204 static int set_config_init_cwd(const char *key
, const char *value
,
1205 struct lxc_conf
*lxc_conf
, void *data
)
1207 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1210 static int set_config_init_uid(const char *key
, const char *value
,
1211 struct lxc_conf
*lxc_conf
, void *data
)
1213 unsigned int init_uid
;
1215 if (lxc_config_value_empty(value
)) {
1216 lxc_conf
->init_uid
= 0;
1220 if (lxc_safe_uint(value
, &init_uid
) < 0)
1223 lxc_conf
->init_uid
= init_uid
;
1228 static int set_config_init_gid(const char *key
, const char *value
,
1229 struct lxc_conf
*lxc_conf
, void *data
)
1231 unsigned int init_gid
;
1233 if (lxc_config_value_empty(value
)) {
1234 lxc_conf
->init_gid
= 0;
1238 if (lxc_safe_uint(value
, &init_gid
) < 0)
1241 lxc_conf
->init_gid
= init_gid
;
1246 static int set_config_init_groups(const char *key
, const char *value
,
1247 struct lxc_conf
*lxc_conf
, void *data
)
1249 __do_free
char *value_dup
= NULL
;
1250 gid_t
*init_groups
= NULL
;
1251 size_t num_groups
= 0;
1255 if (lxc_config_value_empty(value
))
1256 return clr_config_init_groups(key
, lxc_conf
, NULL
);
1258 value_dup
= strdup(value
);
1262 lxc_iterate_parts(token
, value_dup
, ",")
1265 if (num_groups
== INT_MAX
)
1266 return log_error_errno(-ERANGE
, ERANGE
, "Excessive number of supplementary groups specified");
1268 /* This means the string wasn't empty and all we found was garbage. */
1269 if (num_groups
== 0)
1270 return log_error_errno(-EINVAL
, EINVAL
, "No valid groups specified %s", value
);
1272 idx
= lxc_conf
->init_groups
.size
;
1273 init_groups
= realloc(lxc_conf
->init_groups
.list
, sizeof(gid_t
) * (idx
+ num_groups
));
1275 return ret_errno(ENOMEM
);
1278 * Once the realloc() succeeded we need to hand control of the memory
1279 * back to the config otherwise we risk a double-free when
1280 * lxc_conf_free() is called.
1282 lxc_conf
->init_groups
.list
= init_groups
;
1284 /* Restore duplicated value so we can call lxc_iterate_parts() again. */
1285 strcpy(value_dup
, value
);
1287 lxc_iterate_parts(token
, value_dup
, ",") {
1292 ret
= lxc_safe_uint(token
, &group
);
1294 return log_error_errno(ret
, -ret
, "Failed to parse group %s", token
);
1296 init_groups
[idx
++] = group
;
1299 lxc_conf
->init_groups
.size
+= num_groups
;
1304 static int set_config_hooks(const char *key
, const char *value
,
1305 struct lxc_conf
*lxc_conf
, void *data
)
1307 __do_free
char *copy
= NULL
;
1309 if (lxc_config_value_empty(value
))
1310 return lxc_clear_hooks(lxc_conf
, key
);
1312 if (strequal(key
+ 4, "hook"))
1313 return log_error_errno(-EINVAL
, EINVAL
, "lxc.hook must not have a value");
1315 copy
= strdup(value
);
1317 return ret_errno(ENOMEM
);
1319 if (strequal(key
+ 9, "pre-start"))
1320 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, move_ptr(copy
));
1321 else if (strequal(key
+ 9, "start-host"))
1322 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, move_ptr(copy
));
1323 else if (strequal(key
+ 9, "pre-mount"))
1324 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, move_ptr(copy
));
1325 else if (strequal(key
+ 9, "autodev"))
1326 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, move_ptr(copy
));
1327 else if (strequal(key
+ 9, "mount"))
1328 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, move_ptr(copy
));
1329 else if (strequal(key
+ 9, "start"))
1330 return add_hook(lxc_conf
, LXCHOOK_START
, move_ptr(copy
));
1331 else if (strequal(key
+ 9, "stop"))
1332 return add_hook(lxc_conf
, LXCHOOK_STOP
, move_ptr(copy
));
1333 else if (strequal(key
+ 9, "post-stop"))
1334 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, move_ptr(copy
));
1335 else if (strequal(key
+ 9, "clone"))
1336 return add_hook(lxc_conf
, LXCHOOK_CLONE
, move_ptr(copy
));
1337 else if (strequal(key
+ 9, "destroy"))
1338 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, move_ptr(copy
));
1340 return ret_errno(EINVAL
);
1343 static int set_config_hooks_version(const char *key
, const char *value
,
1344 struct lxc_conf
*lxc_conf
, void *data
)
1349 if (lxc_config_value_empty(value
))
1350 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1352 ret
= lxc_safe_uint(value
, &tmp
);
1357 return syserror_set(-EINVAL
, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
1359 lxc_conf
->hooks_version
= tmp
;
1364 static int set_config_personality(const char *key
, const char *value
,
1365 struct lxc_conf
*lxc_conf
, void *data
)
1368 personality_t personality
;
1370 ret
= lxc_config_parse_arch(value
, &personality
);
1372 return syserror("Unsupported personality \"%s\"", value
);
1374 lxc_conf
->personality
= personality
;
1378 static int set_config_pty_max(const char *key
, const char *value
,
1379 struct lxc_conf
*lxc_conf
, void *data
)
1382 unsigned int max
= 0;
1384 if (lxc_config_value_empty(value
)) {
1385 lxc_conf
->pty_max
= 0;
1389 ret
= lxc_safe_uint(value
, &max
);
1391 return ret_errno(EINVAL
);
1393 lxc_conf
->pty_max
= max
;
1398 /* We only need to check whether the first byte of the key after the lxc.start.
1399 * prefix matches our expectations since they fortunately all start with a
1400 * different letter. If anything was wrong with the key we would have already
1401 * noticed when the callback was called.
1403 static int set_config_start(const char *key
, const char *value
,
1404 struct lxc_conf
*lxc_conf
, void *data
)
1409 is_empty
= lxc_config_value_empty(value
);
1411 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1413 lxc_conf
->start_auto
= 0;
1417 ret
= lxc_safe_uint(value
, &lxc_conf
->start_auto
);
1421 if (lxc_conf
->start_auto
> 1)
1422 return ret_errno(EINVAL
);
1425 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1427 lxc_conf
->start_delay
= 0;
1431 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1432 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1434 lxc_conf
->start_order
= 0;
1438 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1441 return ret_errno(EINVAL
);
1444 static int set_config_monitor(const char *key
, const char *value
,
1445 struct lxc_conf
*lxc_conf
, void *data
)
1447 if (lxc_config_value_empty(value
)) {
1448 lxc_conf
->monitor_unshare
= 0;
1452 if (strequal(key
+ 12, "unshare"))
1453 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1455 return ret_errno(EINVAL
);
1458 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1459 struct lxc_conf
*lxc_conf
, void *data
)
1461 if (lxc_config_value_empty(value
)) {
1462 lxc_conf
->monitor_signal_pdeath
= 0;
1466 if (strequal(key
+ 12, "signal.pdeath")) {
1469 sig_n
= sig_parse(value
);
1471 return ret_errno(EINVAL
);
1473 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1477 return ret_errno(EINVAL
);
1480 static int set_config_group(const char *key
, const char *value
,
1481 struct lxc_conf
*lxc_conf
, void *data
)
1483 __do_free
char *groups
= NULL
;
1486 if (lxc_config_value_empty(value
))
1487 return lxc_clear_groups(lxc_conf
);
1489 groups
= strdup(value
);
1491 return ret_errno(ENOMEM
);
1494 * In case several groups are specified in a single line split these
1495 * groups in a single element for the list.
1497 lxc_iterate_parts(token
, groups
, " \t") {
1498 __do_free
char *val
= NULL
;
1499 __do_free
struct string_entry
*entry
= NULL
;
1501 entry
= zalloc(sizeof(struct string_entry
));
1503 return ret_errno(ENOMEM
);
1505 val
= strdup(token
);
1507 return ret_errno(ENOMEM
);
1509 entry
->val
= move_ptr(val
);
1510 list_add_tail(&entry
->head
, &lxc_conf
->groups
);
1517 static int set_config_environment(const char *key
, const char *value
,
1518 struct lxc_conf
*lxc_conf
, void *data
)
1520 __do_free
char *dup
= NULL
, *val
= NULL
;
1521 __do_free
struct environment_entry
*new_env
= NULL
;
1524 if (lxc_config_value_empty(value
))
1525 return lxc_clear_environment(lxc_conf
);
1527 new_env
= zalloc(sizeof(struct environment_entry
));
1529 return ret_errno(ENOMEM
);
1531 dup
= strdup(value
);
1533 return ret_errno(ENOMEM
);
1535 env_val
= strchr(dup
, '=');
1537 env_val
= getenv(dup
);
1543 return ret_errno(ENOENT
);
1545 val
= strdup(env_val
);
1547 return ret_errno(ENOMEM
);
1549 new_env
->key
= move_ptr(dup
);
1550 new_env
->val
= move_ptr(val
);
1552 list_add_tail(&new_env
->head
, &lxc_conf
->environment
);
1558 static int set_config_tty_max(const char *key
, const char *value
,
1559 struct lxc_conf
*lxc_conf
, void *data
)
1562 unsigned int nbtty
= 0;
1564 if (lxc_config_value_empty(value
)) {
1565 lxc_conf
->ttys
.max
= 0;
1569 ret
= lxc_safe_uint(value
, &nbtty
);
1573 lxc_conf
->ttys
.max
= nbtty
;
1578 static int set_config_tty_dir(const char *key
, const char *value
,
1579 struct lxc_conf
*lxc_conf
, void *data
)
1581 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1585 static int set_config_apparmor_profile(const char *key
, const char *value
,
1586 struct lxc_conf
*lxc_conf
, void *data
)
1589 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1591 return syserror_set(-EINVAL
, "Built without AppArmor support");
1595 static int set_config_apparmor_allow_incomplete(const char *key
,
1597 struct lxc_conf
*lxc_conf
,
1603 if (lxc_config_value_empty(value
)) {
1604 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1608 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
);
1612 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1613 return ret_errno(EINVAL
);
1617 return syserror_set(-EINVAL
, "Built without AppArmor support");
1621 static int set_config_apparmor_allow_nesting(const char *key
,
1623 struct lxc_conf
*lxc_conf
,
1629 if (lxc_config_value_empty(value
))
1630 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1632 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
);
1636 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1637 return ret_errno(EINVAL
);
1641 return syserror_set(-EINVAL
, "Built without AppArmor support");
1645 static int set_config_apparmor_raw(const char *key
,
1647 struct lxc_conf
*lxc_conf
,
1651 __do_free
char *elem
= NULL
;
1652 __do_free
struct string_entry
*entry
= NULL
;
1654 if (lxc_config_value_empty(value
))
1655 return lxc_clear_apparmor_raw(lxc_conf
);
1657 entry
= zalloc(sizeof(struct string_entry
));
1659 return ret_errno(ENOMEM
);
1661 elem
= strdup(value
);
1663 return ret_errno(ENOMEM
);
1665 entry
->val
= move_ptr(elem
);
1666 list_add_tail(&entry
->head
, &lxc_conf
->lsm_aa_raw
);
1671 return syserror_set(-EINVAL
, "Built without AppArmor support");
1675 static int set_config_selinux_context(const char *key
, const char *value
,
1676 struct lxc_conf
*lxc_conf
, void *data
)
1679 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1681 return syserror_set(-EINVAL
, "Built without SELinux support");
1685 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1686 struct lxc_conf
*lxc_conf
, void *data
)
1689 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1691 return syserror_set(-EINVAL
, "Built without SELinux support");
1695 static int set_config_keyring_session(const char *key
, const char *value
,
1696 struct lxc_conf
*lxc_conf
, void *data
)
1698 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1701 static int set_config_log_file(const char *key
, const char *value
,
1702 struct lxc_conf
*c
, void *data
)
1706 if (lxc_config_value_empty(value
)) {
1707 free_disarm(c
->logfile
);
1712 * Store these values in the lxc_conf, and then try to set for actual
1715 ret
= set_config_path_item(&c
->logfile
, value
);
1717 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1722 static int set_config_log_level(const char *key
, const char *value
,
1723 struct lxc_conf
*lxc_conf
, void *data
)
1727 if (lxc_config_value_empty(value
)) {
1728 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1732 if (value
[0] >= '0' && value
[0] <= '9') {
1735 ret
= lxc_safe_int(value
, &newlevel
);
1737 return ret_errno(EINVAL
);
1739 newlevel
= lxc_log_priority_to_int(value
);
1743 * Store these values in the lxc_conf, and then try to set for actual
1746 lxc_conf
->loglevel
= newlevel
;
1748 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1751 static int set_config_autodev(const char *key
, const char *value
,
1752 struct lxc_conf
*lxc_conf
, void *data
)
1756 if (lxc_config_value_empty(value
)) {
1757 lxc_conf
->autodev
= 0;
1761 ret
= lxc_safe_uint(value
, &lxc_conf
->autodev
);
1763 return ret_errno(EINVAL
);
1765 if (lxc_conf
->autodev
> 1)
1766 return ret_errno(EINVAL
);
1771 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1772 struct lxc_conf
*lxc_conf
, void *data
)
1774 if (lxc_config_value_empty(value
)) {
1775 lxc_conf
->autodevtmpfssize
= 500000;
1779 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1780 lxc_conf
->autodevtmpfssize
= 500000;
1785 static int set_config_signal_halt(const char *key
, const char *value
,
1786 struct lxc_conf
*lxc_conf
, void *data
)
1790 if (lxc_config_value_empty(value
)) {
1791 lxc_conf
->haltsignal
= 0;
1795 sig_n
= sig_parse(value
);
1797 return ret_errno(EINVAL
);
1799 lxc_conf
->haltsignal
= sig_n
;
1804 static int set_config_signal_reboot(const char *key
, const char *value
,
1805 struct lxc_conf
*lxc_conf
, void *data
)
1809 if (lxc_config_value_empty(value
)) {
1810 lxc_conf
->rebootsignal
= 0;
1814 sig_n
= sig_parse(value
);
1816 return ret_errno(EINVAL
);
1818 lxc_conf
->rebootsignal
= sig_n
;
1823 static int set_config_signal_stop(const char *key
, const char *value
,
1824 struct lxc_conf
*lxc_conf
, void *data
)
1828 if (lxc_config_value_empty(value
)) {
1829 lxc_conf
->stopsignal
= 0;
1833 sig_n
= sig_parse(value
);
1835 return ret_errno(EINVAL
);
1837 lxc_conf
->stopsignal
= sig_n
;
1842 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1843 struct lxc_conf
*lxc_conf
, int version
)
1845 call_cleaner(free_lxc_cgroup
) struct lxc_cgroup
*new_cgroup
= NULL
;
1846 const char *subkey
, *token
;
1849 if (lxc_config_value_empty(value
))
1850 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1852 if (version
== CGROUP2_SUPER_MAGIC
) {
1853 token
= "lxc.cgroup2.";
1855 } else if (version
== CGROUP_SUPER_MAGIC
) {
1856 token
= "lxc.cgroup.";
1859 return ret_errno(EINVAL
);
1862 if (!strnequal(key
, token
, token_len
))
1863 return ret_errno(EINVAL
);
1865 subkey
= key
+ token_len
;
1866 if (*subkey
== '\0')
1867 return ret_errno(EINVAL
);
1869 new_cgroup
= zalloc(sizeof(*new_cgroup
));
1871 return ret_errno(ENOMEM
);
1873 new_cgroup
->subsystem
= strdup(subkey
);
1874 if (!new_cgroup
->subsystem
)
1875 return ret_errno(ENOMEM
);
1877 new_cgroup
->value
= strdup(value
);
1878 if (!new_cgroup
->value
)
1879 return ret_errno(ENOMEM
);
1881 new_cgroup
->version
= version
;
1883 if (version
== CGROUP2_SUPER_MAGIC
)
1884 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup2
);
1886 list_add_tail(&new_cgroup
->head
, &lxc_conf
->cgroup
);
1887 move_ptr(new_cgroup
);
1892 static int set_config_cgroup_controller(const char *key
, const char *value
,
1893 struct lxc_conf
*lxc_conf
, void *data
)
1895 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1896 CGROUP_SUPER_MAGIC
);
1899 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1900 struct lxc_conf
*lxc_conf
, void *data
)
1902 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1903 CGROUP2_SUPER_MAGIC
);
1906 static int set_config_cgroup_dir(const char *key
, const char *value
,
1907 struct lxc_conf
*lxc_conf
, void *data
)
1909 if (!strequal(key
, "lxc.cgroup.dir"))
1910 return ret_errno(EINVAL
);
1912 if (lxc_config_value_empty(value
))
1913 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1916 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1919 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1921 return set_config_path_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1924 static int set_config_cgroup_monitor_dir(const char *key
, const char *value
,
1925 struct lxc_conf
*lxc_conf
, void *data
)
1927 if (lxc_config_value_empty(value
))
1928 return clr_config_cgroup_monitor_dir(key
, lxc_conf
, NULL
);
1931 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1934 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1936 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_dir
, value
);
1939 static int set_config_cgroup_monitor_pivot_dir(const char *key
, const char *value
,
1940 struct lxc_conf
*lxc_conf
, void *data
)
1942 if (lxc_config_value_empty(value
))
1943 return clr_config_cgroup_monitor_pivot_dir(key
, lxc_conf
, NULL
);
1946 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1949 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1951 return set_config_path_item(&lxc_conf
->cgroup_meta
.monitor_pivot_dir
, value
);
1954 static int set_config_cgroup_container_dir(const char *key
, const char *value
,
1955 struct lxc_conf
*lxc_conf
,
1958 if (lxc_config_value_empty(value
))
1959 return clr_config_cgroup_container_dir(key
, lxc_conf
, NULL
);
1962 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1965 return syserror_set(-EINVAL
, "%s paths may not walk upwards via \"../\"", key
);
1967 return set_config_path_item(&lxc_conf
->cgroup_meta
.container_dir
, value
);
1970 static int set_config_cgroup_container_inner_dir(const char *key
,
1972 struct lxc_conf
*lxc_conf
,
1975 if (lxc_config_value_empty(value
))
1976 return clr_config_cgroup_container_inner_dir(key
, lxc_conf
, NULL
);
1979 return syserror_set(-EINVAL
, "%s paths may not be absolute", key
);
1981 if (strchr(value
, '/') || strequal(value
, ".") || strequal(value
, ".."))
1982 return log_error_errno(-EINVAL
, EINVAL
, "lxc.cgroup.dir.container.inner must be a single directory name");
1984 return set_config_string_item(&lxc_conf
->cgroup_meta
.namespace_dir
, value
);
1987 static int set_config_cgroup_relative(const char *key
, const char *value
,
1988 struct lxc_conf
*lxc_conf
, void *data
)
1990 unsigned int converted
;
1993 if (lxc_config_value_empty(value
))
1994 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1996 ret
= lxc_safe_uint(value
, &converted
);
2000 if (converted
== 1) {
2001 lxc_conf
->cgroup_meta
.relative
= true;
2005 if (converted
== 0) {
2006 lxc_conf
->cgroup_meta
.relative
= false;
2010 return ret_errno(EINVAL
);
2013 static bool parse_limit_value(const char **value
, rlim_t
*res
)
2015 char *endptr
= NULL
;
2017 if (strnequal(*value
, "unlimited", STRLITERALLEN("unlimited"))) {
2018 *res
= RLIM_INFINITY
;
2019 *value
+= STRLITERALLEN("unlimited");
2024 *res
= strtoull(*value
, &endptr
, 10);
2025 if (errno
|| !endptr
)
2033 static int set_config_prlimit(const char *key
, const char *value
,
2034 struct lxc_conf
*lxc_conf
, void *data
)
2036 call_cleaner(free_lxc_limit
) struct lxc_limit
*new_lim
= NULL
;
2037 struct rlimit limit
;
2039 struct lxc_limit
*lim
;
2041 if (lxc_config_value_empty(value
))
2042 return lxc_clear_limits(lxc_conf
, key
);
2044 if (!strnequal(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
2045 return ret_errno(EINVAL
);
2047 key
+= STRLITERALLEN("lxc.prlimit.");
2049 /* soft limit comes first in the value */
2050 if (!parse_limit_value(&value
, &limit_value
))
2051 return ret_errno(EINVAL
);
2053 limit
.rlim_cur
= limit_value
;
2055 /* skip spaces and a colon */
2056 while (isspace(*value
))
2061 else if (*value
) /* any other character is an error here */
2062 return ret_errno(EINVAL
);
2064 while (isspace(*value
))
2067 /* optional hard limit */
2069 if (!parse_limit_value(&value
, &limit_value
))
2070 return ret_errno(EINVAL
);
2072 limit
.rlim_max
= limit_value
;
2074 /* check for trailing garbage */
2075 while (isspace(*value
))
2079 return ret_errno(EINVAL
);
2081 /* a single value sets both hard and soft limit */
2082 limit
.rlim_max
= limit
.rlim_cur
;
2085 /* find existing list element */
2086 list_for_each_entry(lim
, &lxc_conf
->limits
, head
) {
2087 if (!strequal(key
, lim
->resource
))
2094 new_lim
= zalloc(sizeof(*new_lim
));
2096 return ret_errno(ENOMEM
);
2098 new_lim
->resource
= strdup(key
);
2099 if (!new_lim
->resource
)
2100 return ret_errno(ENOMEM
);
2102 new_lim
->limit
= limit
;
2103 list_add_tail(&new_lim
->head
, &lxc_conf
->limits
);
2109 static int set_config_sysctl(const char *key
, const char *value
,
2110 struct lxc_conf
*lxc_conf
, void *data
)
2112 call_cleaner(free_lxc_sysctl
) struct lxc_sysctl
*sysctl_elem
= NULL
;
2113 struct lxc_sysctl
*sysctl
, *nsysctl
;
2115 if (lxc_config_value_empty(value
))
2116 return clr_config_sysctl(key
, lxc_conf
, NULL
);
2118 if (!strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
2119 return ret_errno(EINVAL
);
2121 key
+= STRLITERALLEN("lxc.sysctl.");
2122 if (is_empty_string(key
))
2123 return ret_errno(EINVAL
);
2125 /* find existing list element */
2126 list_for_each_entry_safe(sysctl
, nsysctl
, &lxc_conf
->sysctls
, head
) {
2127 __do_free
char *replace_value
= NULL
;
2129 if (!strequal(key
, sysctl
->key
))
2132 replace_value
= strdup(value
);
2134 return ret_errno(EINVAL
);
2136 free(sysctl
->value
);
2137 sysctl
->value
= move_ptr(replace_value
);
2142 sysctl_elem
= zalloc(sizeof(*sysctl_elem
));
2144 return ret_errno(ENOMEM
);
2146 sysctl_elem
->key
= strdup(key
);
2147 if (!sysctl_elem
->key
)
2148 return ret_errno(ENOMEM
);
2150 sysctl_elem
->value
= strdup(value
);
2151 if (!sysctl_elem
->value
)
2152 return ret_errno(ENOMEM
);
2154 list_add_tail(&sysctl_elem
->head
, &lxc_conf
->sysctls
);
2155 move_ptr(sysctl_elem
);
2160 static int set_config_proc(const char *key
, const char *value
,
2161 struct lxc_conf
*lxc_conf
, void *data
)
2163 call_cleaner(free_lxc_proc
) struct lxc_proc
*new_proc
= NULL
;
2166 if (lxc_config_value_empty(value
))
2167 return clr_config_proc(key
, lxc_conf
, NULL
);
2169 if (!strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
2170 return ret_errno(EINVAL
);
2172 subkey
= key
+ STRLITERALLEN("lxc.proc.");
2173 if (*subkey
== '\0')
2174 return ret_errno(EINVAL
);
2176 new_proc
= zalloc(sizeof(*new_proc
));
2178 return ret_errno(ENOMEM
);
2180 new_proc
->filename
= strdup(subkey
);
2181 if (!new_proc
->filename
)
2182 return ret_errno(ENOMEM
);
2184 new_proc
->value
= strdup(value
);
2185 if (!new_proc
->value
)
2186 return ret_errno(ENOMEM
);
2188 list_add_tail(&new_proc
->head
, &lxc_conf
->procs
);
2194 static int set_config_idmaps(const char *key
, const char *value
,
2195 struct lxc_conf
*lxc_conf
, void *data
)
2197 __do_free
struct id_map
*idmap
= NULL
;
2198 unsigned long hostid
, nsid
, range
;
2202 if (lxc_config_value_empty(value
))
2203 return lxc_clear_idmaps(lxc_conf
);
2205 idmap
= zalloc(sizeof(*idmap
));
2207 return ret_errno(ENOMEM
);
2209 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2211 return log_error_errno(-EINVAL
, EINVAL
, "Failed to parse id mappings");
2213 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2215 idmap
->idtype
= ID_TYPE_UID
;
2216 else if (type
== 'g')
2217 idmap
->idtype
= ID_TYPE_GID
;
2219 return ret_errno(EINVAL
);
2221 idmap
->hostid
= hostid
;
2223 idmap
->range
= range
;
2224 list_add_tail(&idmap
->head
, &lxc_conf
->id_map
);
2226 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2227 if (idmap
->nsid
== 0)
2228 lxc_conf
->root_nsuid_map
= idmap
;
2230 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2231 if (idmap
->nsid
== 0)
2232 lxc_conf
->root_nsgid_map
= idmap
;
2239 static int set_config_mount_fstab(const char *key
, const char *value
,
2240 struct lxc_conf
*lxc_conf
, void *data
)
2242 if (lxc_config_value_empty(value
)) {
2243 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2244 return ret_errno(EINVAL
);
2247 return set_config_path_item(&lxc_conf
->fstab
, value
);
2250 static int set_config_mount_auto(const char *key
, const char *value
,
2251 struct lxc_conf
*lxc_conf
, void *data
)
2253 __do_free
char *autos
= NULL
;
2260 } allowed_auto_mounts
[] = {
2261 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2262 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2263 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2264 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2265 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2266 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2267 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2268 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2269 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2270 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2271 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2272 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2273 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2274 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2275 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2276 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2277 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2278 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2279 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2280 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2281 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2282 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2283 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2284 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2286 * For adding anything that is just a single on/off, but has no
2287 * options: keep mask and flag identical and just define the
2288 * enum value as an unused bit so far
2293 if (lxc_config_value_empty(value
)) {
2294 lxc_conf
->auto_mounts
= 0;
2298 autos
= strdup(value
);
2300 return ret_errno(ENOMEM
);
2302 lxc_iterate_parts(token
, autos
, " \t") {
2303 bool is_shmounts
= false;
2305 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2306 if (strequal(allowed_auto_mounts
[i
].token
, token
))
2309 if (strequal("shmounts:", allowed_auto_mounts
[i
].token
) &&
2310 strnequal("shmounts:", token
, STRLITERALLEN("shmounts:"))) {
2316 if (!allowed_auto_mounts
[i
].token
)
2317 return log_error_errno(-EINVAL
, EINVAL
, "Invalid filesystem to automount \"%s\"", token
);
2319 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2320 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2323 __do_free
char *container_path
= NULL
, *host_path
= NULL
;
2326 val
= token
+ STRLITERALLEN("shmounts:");
2328 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2330 host_path
= strdup(val
);
2332 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2334 val
= strchr(host_path
, ':');
2335 if (!val
|| *(val
+ 1) == '\0')
2336 val
= "/dev/.lxc-mounts";
2340 container_path
= strdup(val
);
2342 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts container path");
2344 free_disarm(lxc_conf
->shmount
.path_host
);
2345 lxc_conf
->shmount
.path_host
= move_ptr(host_path
);
2347 free_disarm(lxc_conf
->shmount
.path_cont
);
2348 lxc_conf
->shmount
.path_cont
= move_ptr(container_path
);
2355 static int set_config_mount(const char *key
, const char *value
,
2356 struct lxc_conf
*lxc_conf
, void *data
)
2358 __do_free
char *mntelem
= NULL
;
2359 __do_free
struct string_entry
*entry
= NULL
;
2361 if (lxc_config_value_empty(value
))
2362 return lxc_clear_mount_entries(lxc_conf
);
2364 entry
= zalloc(sizeof(struct string_entry
));
2366 return ret_errno(ENOMEM
);
2368 mntelem
= strdup(value
);
2370 return ret_errno(ENOMEM
);
2372 entry
->val
= move_ptr(mntelem
);
2373 list_add_tail(&entry
->head
, &lxc_conf
->mount_entries
);
2379 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2380 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2383 static int add_cap_entry(struct lxc_conf
*conf
, char *caps
, bool keep
)
2388 * In case several capability keep is specified in a single line split
2389 * these caps in a single element for the list.
2391 lxc_iterate_parts(token
, caps
, " \t") {
2392 __do_free
struct cap_entry
*new_cap
= NULL
;
2395 if (strequal(token
, "none")) {
2397 return syserror_set(-EINVAL
, "The \"none\" keyword is only valid when keeping caps");
2399 lxc_clear_config_caps(conf
);
2403 cap
= parse_cap(token
);
2406 return syserror_set(-EINVAL
, "Invalid capability specified");
2408 INFO("Ignoring unknown capability \"%s\"", token
);
2412 new_cap
= zalloc(sizeof(struct cap_entry
));
2414 return ret_errno(ENOMEM
);
2416 new_cap
->cap_name
= strdup(token
);
2417 if (!new_cap
->cap_name
)
2418 return ret_errno(ENOMEM
);
2421 list_add_tail(&new_cap
->head
, &conf
->caps
.list
);
2428 static int set_config_cap_keep(const char *key
, const char *value
,
2429 struct lxc_conf
*lxc_conf
, void *data
)
2431 __do_free
char *caps
= NULL
;
2434 if (lxc_config_value_empty(value
))
2435 return lxc_clear_config_caps(lxc_conf
);
2437 caps
= strdup(value
);
2439 return ret_errno(ENOMEM
);
2441 if (!lxc_conf
->caps
.keep
&& !list_empty(&lxc_conf
->caps
.list
))
2442 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2444 ret
= add_cap_entry(lxc_conf
, caps
, true);
2448 lxc_conf
->caps
.keep
= true;
2452 static int set_config_cap_drop(const char *key
, const char *value
,
2453 struct lxc_conf
*lxc_conf
, void *data
)
2455 __do_free
char *caps
= NULL
;
2458 if (lxc_config_value_empty(value
))
2459 return lxc_clear_config_caps(lxc_conf
);
2461 if (lxc_conf
->caps
.keep
)
2462 return syserror_set(-EINVAL
, "Keeping and dropping capabilities are mutually exclusive");
2464 caps
= strdup(value
);
2466 return ret_errno(ENOMEM
);
2468 ret
= add_cap_entry(lxc_conf
, caps
, false);
2472 lxc_conf
->caps
.keep
= false;
2476 static int set_config_console_path(const char *key
, const char *value
,
2477 struct lxc_conf
*lxc_conf
, void *data
)
2479 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2482 static int set_config_console_rotate(const char *key
, const char *value
,
2483 struct lxc_conf
*lxc_conf
, void *data
)
2487 if (lxc_config_value_empty(value
)) {
2488 lxc_conf
->console
.log_rotate
= 0;
2492 ret
= lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
);
2494 return ret_errno(EINVAL
);
2496 if (lxc_conf
->console
.log_rotate
> 1)
2497 return log_error_errno(-EINVAL
, EINVAL
, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
2502 static int set_config_console_logfile(const char *key
, const char *value
,
2503 struct lxc_conf
*lxc_conf
, void *data
)
2505 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2508 static int set_config_console_buffer_size(const char *key
, const char *value
,
2509 struct lxc_conf
*lxc_conf
, void *data
)
2513 uint64_t buffer_size
, pgsz
;
2515 if (lxc_config_value_empty(value
)) {
2516 lxc_conf
->console
.buffer_size
= 0;
2520 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2521 if (strequal(value
, "auto")) {
2522 lxc_conf
->console
.buffer_size
= 1 << 17;
2526 ret
= parse_byte_size_string(value
, &size
);
2531 return ret_errno(EINVAL
);
2533 /* must be at least a page size */
2534 pgsz
= lxc_getpagesize();
2535 if ((uint64_t)size
< pgsz
) {
2536 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2541 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2542 if (buffer_size
== 0)
2543 return ret_errno(EINVAL
);
2545 if (buffer_size
!= (uint64_t)size
)
2546 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", buffer_size
);
2548 lxc_conf
->console
.buffer_size
= buffer_size
;
2553 static int set_config_console_size(const char *key
, const char *value
,
2554 struct lxc_conf
*lxc_conf
, void *data
)
2558 uint64_t log_size
, pgsz
;
2560 if (lxc_config_value_empty(value
)) {
2561 lxc_conf
->console
.log_size
= 0;
2565 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2566 if (strequal(value
, "auto")) {
2567 lxc_conf
->console
.log_size
= 1 << 17;
2571 ret
= parse_byte_size_string(value
, &size
);
2573 return ret_errno(EINVAL
);
2576 return ret_errno(EINVAL
);
2578 /* must be at least a page size */
2579 pgsz
= lxc_getpagesize();
2580 if ((uint64_t)size
< pgsz
) {
2581 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2586 log_size
= lxc_find_next_power2((uint64_t)size
);
2588 return ret_errno(EINVAL
);
2590 if (log_size
!= (uint64_t)size
)
2591 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", log_size
);
2593 lxc_conf
->console
.log_size
= log_size
;
2599 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2600 * file, we expand it in the unexpanded_config, so that after a save_config we
2601 * store the hwaddr for re-use.
2602 * This is only called when reading the config file, not when executing a
2604 * 'x' and 'X' are substituted in-place.
2606 static void update_hwaddr(const char *line
)
2610 line
+= lxc_char_left_gc(line
, strlen(line
));
2614 if (!lxc_config_net_is_hwaddr(line
))
2617 /* Let config_net_hwaddr raise the error. */
2618 p
= strchr(line
, '=');
2629 rand_complete_hwaddr(p
);
2632 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2635 size_t len
= conf
->unexpanded_len
;
2637 update_hwaddr(line
);
2639 linelen
= strlen(line
);
2640 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2643 tmp
= realloc(conf
->unexpanded_config
, conf
->unexpanded_alloced
+ 1024);
2645 return ret_errno(EINVAL
);
2647 if (!conf
->unexpanded_config
)
2650 conf
->unexpanded_config
= tmp
;
2651 conf
->unexpanded_alloced
+= 1024;
2654 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2655 conf
->unexpanded_len
+= linelen
;
2656 if (line
[linelen
- 1] != '\n')
2657 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2658 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2663 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2665 __do_closedir
DIR *dir
= NULL
;
2666 struct dirent
*direntp
;
2669 dir
= opendir(dirp
);
2671 return errno
== ENOENT
? 0 : -errno
;
2673 while ((direntp
= readdir(dir
))) {
2675 char path
[PATH_MAX
];
2677 fnam
= direntp
->d_name
;
2678 if (strequal(fnam
, "."))
2681 if (strequal(fnam
, ".."))
2685 if (len
< 6 || !strnequal(fnam
+ len
- 5, ".conf", 5))
2688 len
= strnprintf(path
, sizeof(path
), "%s/%s", dirp
, fnam
);
2690 return ret_errno(EIO
);
2692 ret
= lxc_config_read(path
, lxc_conf
, true);
2700 static int set_config_includefiles(const char *key
, const char *value
,
2701 struct lxc_conf
*lxc_conf
, void *data
)
2703 if (lxc_config_value_empty(value
)) {
2704 clr_config_includefiles(key
, lxc_conf
, NULL
);
2708 if (value
[strlen(value
)-1] == '/' || is_dir(value
))
2709 return do_includedir(value
, lxc_conf
);
2711 return lxc_config_read(value
, lxc_conf
, true);
2714 static int set_config_rootfs_path(const char *key
, const char *value
,
2715 struct lxc_conf
*lxc_conf
, void *data
)
2717 __do_free
char *dup
= NULL
;
2720 const char *container_path
;
2722 if (lxc_config_value_empty(value
)) {
2723 free(lxc_conf
->rootfs
.path
);
2724 lxc_conf
->rootfs
.path
= NULL
;
2728 dup
= strdup(value
);
2730 return ret_errno(ENOMEM
);
2732 /* Split <storage type>:<container path> into <storage type> and
2733 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2734 * "rootfs.path" to <container path>.
2736 tmp
= strchr(dup
, ':');
2740 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2742 return ret_errno(ENOMEM
);
2745 container_path
= tmp
;
2747 container_path
= value
;
2750 return set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2753 static int set_config_rootfs_managed(const char *key
, const char *value
,
2754 struct lxc_conf
*lxc_conf
, void *data
)
2756 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2759 static int set_config_rootfs_mount(const char *key
, const char *value
,
2760 struct lxc_conf
*lxc_conf
, void *data
)
2762 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2765 static int set_config_rootfs_options(const char *key
, const char *value
,
2766 struct lxc_conf
*lxc_conf
, void *data
)
2768 __do_free
char *dup
= NULL
, *raw_options
= NULL
;
2769 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2770 struct lxc_mount_options
*mnt_opts
= &rootfs
->mnt_opts
;
2773 clr_config_rootfs_options(key
, lxc_conf
, data
);
2774 if (lxc_config_value_empty(value
))
2777 ret
= set_config_string_item(&raw_options
, value
);
2779 return ret_errno(ENOMEM
);
2781 dup
= strdup(value
);
2785 ret
= parse_lxc_mount_attrs(mnt_opts
, dup
);
2789 /* Make sure we're only valid LXC specific mount options. */
2790 if (mnt_opts
->create_dir
|| mnt_opts
->create_file
||
2791 mnt_opts
->optional
|| mnt_opts
->relative
)
2792 return syserror_set(-EINVAL
, "Invalid LXC specific mount option for rootfs mount");
2794 ret
= parse_mount_attrs(mnt_opts
, dup
);
2796 return ret_errno(EINVAL
);
2798 rootfs
->mnt_opts
.raw_options
= move_ptr(raw_options
);
2802 static int set_config_uts_name(const char *key
, const char *value
,
2803 struct lxc_conf
*lxc_conf
, void *data
)
2805 __do_free
struct utsname
*utsname
= NULL
;
2807 if (lxc_config_value_empty(value
)) {
2808 clr_config_uts_name(key
, lxc_conf
, NULL
);
2812 utsname
= zalloc(sizeof(*utsname
));
2814 return ret_errno(ENOMEM
);
2816 if (strlen(value
) >= sizeof(utsname
->nodename
))
2817 return ret_errno(EINVAL
);
2819 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2820 free(lxc_conf
->utsname
);
2821 lxc_conf
->utsname
= move_ptr(utsname
);
2826 static int set_config_namespace_clone(const char *key
, const char *value
,
2827 struct lxc_conf
*lxc_conf
, void *data
)
2829 __do_free
char *ns
= NULL
;
2833 if (lxc_config_value_empty(value
))
2834 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2836 if (lxc_conf
->ns_keep
!= 0)
2837 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2841 return ret_errno(ENOMEM
);
2843 lxc_iterate_parts(token
, ns
, " \t") {
2844 token
+= lxc_char_left_gc(token
, strlen(token
));
2845 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2846 cloneflag
= lxc_namespace_2_cloneflag(token
);
2848 return ret_errno(EINVAL
);
2849 lxc_conf
->ns_clone
|= cloneflag
;
2855 static int set_config_namespace_keep(const char *key
, const char *value
,
2856 struct lxc_conf
*lxc_conf
, void *data
)
2858 __do_free
char *ns
= NULL
;
2862 if (lxc_config_value_empty(value
))
2863 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2865 if (lxc_conf
->ns_clone
!= 0)
2866 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2870 return ret_errno(ENOMEM
);
2872 lxc_iterate_parts(token
, ns
, " \t") {
2873 token
+= lxc_char_left_gc(token
, strlen(token
));
2874 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2875 cloneflag
= lxc_namespace_2_cloneflag(token
);
2877 return ret_errno(EINVAL
);
2878 lxc_conf
->ns_keep
|= cloneflag
;
2884 static int set_config_time_offset_boot(const char *key
, const char *value
,
2885 struct lxc_conf
*lxc_conf
, void *data
)
2890 char buf
[STRLITERALLEN("ms") + 1];
2892 if (lxc_config_value_empty(value
))
2893 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2895 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2899 unit
= lxc_trim_whitespace_in_place(buf
);
2900 if (strequal(unit
, "h")) {
2901 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2902 return ret_errno(EOVERFLOW
);
2903 } else if (strequal(unit
, "m")) {
2904 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2905 return ret_errno(EOVERFLOW
);
2906 } else if (strequal(unit
, "s")) {
2907 lxc_conf
->timens
.s_boot
= offset
;
2908 } else if (strequal(unit
, "ms")) {
2909 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2910 return ret_errno(EOVERFLOW
);
2911 } else if (strequal(unit
, "us")) {
2912 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2913 return ret_errno(EOVERFLOW
);
2914 } else if (strequal(unit
, "ns")) {
2915 lxc_conf
->timens
.ns_boot
= offset
;
2917 return ret_errno(EINVAL
);
2923 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2924 struct lxc_conf
*lxc_conf
, void *data
)
2929 char buf
[STRLITERALLEN("ms") + 1];
2931 if (lxc_config_value_empty(value
))
2932 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2934 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2938 unit
= lxc_trim_whitespace_in_place(buf
);
2939 if (strequal(unit
, "h")) {
2940 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2941 return ret_errno(EOVERFLOW
);
2942 } else if (strequal(unit
, "m")) {
2943 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2944 return ret_errno(EOVERFLOW
);
2945 } else if (strequal(unit
, "s")) {
2946 lxc_conf
->timens
.s_monotonic
= offset
;
2947 } else if (strequal(unit
, "ms")) {
2948 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2949 return ret_errno(EOVERFLOW
);
2950 } else if (strequal(unit
, "us")) {
2951 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2952 return ret_errno(EOVERFLOW
);
2953 } else if (strequal(unit
, "ns")) {
2954 lxc_conf
->timens
.ns_monotonic
= offset
;
2956 return ret_errno(EINVAL
);
2962 static int set_config_namespace_share(const char *key
, const char *value
,
2963 struct lxc_conf
*lxc_conf
, void *data
)
2966 const char *namespace;
2968 if (lxc_config_value_empty(value
))
2969 return clr_config_namespace_share(key
, lxc_conf
, data
);
2971 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2972 if (is_empty_string(namespace))
2973 return ret_errno(EINVAL
);
2975 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2979 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2982 struct parse_line_conf
{
2983 struct lxc_conf
*conf
;
2987 static int parse_line(char *buffer
, void *data
)
2989 __do_free
char *linep
= NULL
;
2990 char *dot
, *key
, *line
, *value
;
2992 struct lxc_config_t
*config
;
2995 struct parse_line_conf
*plc
= data
;
2998 return syserror_set(-EINVAL
, "Missing config");
3000 /* If there are newlines in the config file we should keep them. */
3001 empty_line
= lxc_is_line_empty(dup
);
3005 /* We have to dup the buffer otherwise, at the re-exec for reboot we
3006 * modified the original string on the stack by replacing '=' by '\0'
3009 linep
= line
= strdup(dup
);
3011 return ret_errno(ENOMEM
);
3013 if (!plc
->from_include
) {
3014 ret
= append_unexp_config_line(line
, plc
->conf
);
3022 line
+= lxc_char_left_gc(line
, strlen(line
));
3024 /* ignore comments */
3028 /* martian option - don't add it to the config itself */
3029 if (!strnequal(line
, "lxc.", 4))
3032 dot
= strchr(line
, '=');
3034 return log_error_errno(-EINVAL
, EINVAL
, "Invalid configuration line: %s", line
);
3040 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3042 value
+= lxc_char_left_gc(value
, strlen(value
));
3043 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3045 if (*value
== '\'' || *value
== '\"') {
3048 len
= strlen(value
);
3049 if (len
> 1 && value
[len
- 1] == *value
) {
3050 value
[len
- 1] = '\0';
3055 config
= lxc_get_config(key
);
3056 return config
->set(key
, value
, plc
->conf
, NULL
);
3059 static struct new_config_item
*parse_new_conf_line(char *buffer
)
3061 __do_free
char *k
= NULL
, *linep
= NULL
, *v
= NULL
;
3062 __do_free
struct new_config_item
*new = NULL
;
3064 char *dot
, *key
, *line
, *value
;
3066 if (is_empty_string(buffer
))
3067 return log_error_errno(NULL
, EINVAL
, "Empty configuration line");
3069 linep
= line
= strdup(dup
);
3073 line
+= lxc_char_left_gc(line
, strlen(line
));
3075 /* martian option - don't add it to the config itself */
3076 if (!strnequal(line
, "lxc.", 4))
3079 dot
= strchr(line
, '=');
3081 return log_error_errno(NULL
, EINVAL
, "Invalid configuration line: %s", line
);
3087 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
3089 value
+= lxc_char_left_gc(value
, strlen(value
));
3090 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
3092 if (*value
== '\'' || *value
== '\"') {
3095 len
= strlen(value
);
3096 if (len
> 1 && value
[len
- 1] == *value
) {
3097 value
[len
- 1] = '\0';
3102 new = zalloc(sizeof(struct new_config_item
));
3114 new->key
= move_ptr(k
);
3115 new->val
= move_ptr(v
);
3116 return move_ptr(new);
3119 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
3121 struct parse_line_conf plc
;
3124 return syserror_set(-EINVAL
, "Missing config");
3127 plc
.from_include
= from_include
;
3129 /* Catch only the top level config file name in the structure. */
3131 conf
->rcfile
= strdup(file
);
3133 return lxc_file_for_each_line_mmap(file
, parse_line
, &plc
);
3136 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3138 __do_free
struct lxc_list
*dent
= NULL
;
3140 dent
= lxc_list_new();
3142 return ret_errno(ENOMEM
);
3144 dent
->elem
= parse_new_conf_line(arg
);
3146 return ret_errno(ENOMEM
);
3148 lxc_list_add_tail(defines
, move_ptr(dent
));
3153 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3155 struct lxc_list
*it
;
3158 lxc_list_for_each(it
, defines
) {
3159 struct new_config_item
*new_item
= it
->elem
;
3160 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3165 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
3166 lxc_config_define_free(defines
);
3167 #endif /* !FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
3172 void lxc_config_define_free(struct lxc_list
*defines
)
3174 struct lxc_list
*it
, *next
;
3176 lxc_list_for_each_safe(it
, defines
, next
) {
3177 struct new_config_item
*new_item
= it
->elem
;
3178 free(new_item
->key
);
3179 free(new_item
->val
);
3186 int lxc_config_parse_arch(const char *arch
, signed long *persona
)
3188 static struct per_name
{
3192 { "arm", PER_LINUX32
},
3193 { "armel", PER_LINUX32
},
3194 { "armhf", PER_LINUX32
},
3195 { "armv7l", PER_LINUX32
},
3196 { "athlon", PER_LINUX32
},
3197 { "i386", PER_LINUX32
},
3198 { "i486", PER_LINUX32
},
3199 { "i586", PER_LINUX32
},
3200 { "i686", PER_LINUX32
},
3201 { "linux32", PER_LINUX32
},
3202 { "mips", PER_LINUX32
},
3203 { "mipsel", PER_LINUX32
},
3204 { "ppc", PER_LINUX32
},
3205 { "powerpc", PER_LINUX32
},
3206 { "x86", PER_LINUX32
},
3207 { "aarch64", PER_LINUX
},
3208 { "amd64", PER_LINUX
},
3209 { "arm64", PER_LINUX
},
3210 { "linux64", PER_LINUX
},
3211 { "mips64", PER_LINUX
},
3212 { "mips64el", PER_LINUX
},
3213 { "ppc64", PER_LINUX
},
3214 { "ppc64el", PER_LINUX
},
3215 { "ppc64le", PER_LINUX
},
3216 { "powerpc64", PER_LINUX
},
3217 { "s390x", PER_LINUX
},
3218 { "x86_64", PER_LINUX
},
3221 for (size_t i
= 0; i
< ARRAY_SIZE(pername
); i
++) {
3222 if (!strequal(pername
[i
].name
, arch
))
3225 *persona
= pername
[i
].per
;
3229 return ret_errno(EINVAL
);
3232 int lxc_fill_elevated_privileges(char *flaglist
, unsigned int *flags
)
3234 unsigned int flags_tmp
= 0;
3240 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3241 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3242 { "LSM", LXC_ATTACH_LSM_EXEC
},
3248 * For the sake of backward compatibility, keep all privileges
3249 * if no specific privileges are specified.
3251 for (unsigned int i
= 0; all_privs
[i
].token
; i
++)
3252 flags_tmp
|= all_privs
[i
].flag
;
3258 lxc_iterate_parts(token
, flaglist
, "|") {
3259 bool valid_token
= false;
3261 for (unsigned int i
= 0; all_privs
[i
].token
; i
++) {
3262 if (!strequal(all_privs
[i
].token
, token
))
3266 flags_tmp
|= all_privs
[i
].flag
;
3270 return syserror_set(-EINVAL
, "Invalid elevated privilege \"%s\" requested", token
);
3277 /* Write out a configuration file. */
3278 int write_config(int fd
, const struct lxc_conf
*conf
)
3281 size_t len
= conf
->unexpanded_len
;
3286 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3288 return log_error_errno(-errno
, errno
, "Failed to write configuration file");
3293 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3296 __do_free
char *tmp
= NULL
;
3300 len
= strlen(key
) + strlen(v
) + 4;
3301 tmp
= must_realloc(NULL
, len
);
3303 if (lxc_config_value_empty(v
))
3304 ret
= strnprintf(tmp
, len
, "%s =", key
);
3306 ret
= strnprintf(tmp
, len
, "%s = %s", key
, v
);
3310 /* Save the line verbatim into unexpanded_conf */
3311 if (append_unexp_config_line(tmp
, conf
))
3317 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3321 char *lstart
= conf
->unexpanded_config
;
3323 if (!conf
->unexpanded_config
)
3327 lend
= strchr(lstart
, '\n');
3331 lend
= lstart
+ strlen(lstart
);
3335 if (!strnequal(lstart
, key
, strlen(key
))) {
3341 v
= lstart
[strlen(key
)];
3342 if (!isspace(v
) && v
!= '=') {
3348 conf
->unexpanded_len
-= (lend
- lstart
);
3350 if (*lend
== '\0') {
3355 memmove(lstart
, lend
, strlen(lend
) + 1);
3359 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3360 const char *newpath
, const char *oldname
,
3361 const char *newname
, const char *ovldir
)
3363 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3364 char *lstart
= conf
->unexpanded_config
;
3365 const char *key
= "lxc.mount.entry";
3368 size_t newdirlen
, olddirlen
;
3370 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3371 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3372 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
, oldname
);
3376 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3377 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3378 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
, newname
);
3382 if (!conf
->unexpanded_config
)
3386 lend
= strchr(lstart
, '\n');
3388 lend
= lstart
+ strlen(lstart
);
3392 if (!strnequal(lstart
, key
, strlen(key
)))
3395 p
= strchr(lstart
+ strlen(key
), '=');
3406 /* Whenever a lxc.mount.entry entry is found in a line we check
3407 * if the substring "overlay" is present before doing any
3408 * further work. We check for "overlay" because substrings need
3409 * to have at least one space before them in a valid overlay
3410 * lxc.mount.entry (/A B overlay). When the space before is
3411 * missing it is very likely that these substrings are part of a
3412 * path or something else. (Checking q >= lend ensures that we
3413 * only count matches in the current line.) */
3414 q
= strstr(p
, " overlay");
3415 if (!q
|| q
>= lend
)
3418 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3421 /* replace the olddir with newdir */
3422 if (olddirlen
>= newdirlen
) {
3423 size_t diff
= olddirlen
- newdirlen
;
3424 memcpy(q
, newdir
, newdirlen
);
3426 if (olddirlen
!= newdirlen
) {
3427 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3428 strlen(q
) - newdirlen
- diff
+ 1);
3430 conf
->unexpanded_len
-= diff
;
3434 size_t diff
= newdirlen
- olddirlen
;
3435 size_t oldlen
= conf
->unexpanded_len
;
3436 size_t newlen
= oldlen
+ diff
;
3437 size_t poffset
= q
- conf
->unexpanded_config
;
3439 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3443 conf
->unexpanded_len
= newlen
;
3444 conf
->unexpanded_alloced
= newlen
+ 1;
3445 new[newlen
- 1] = '\0';
3446 lend
= new + (lend
- conf
->unexpanded_config
);
3448 /* Move over the remainder to make room for the newdir.
3450 memmove(new + poffset
+ newdirlen
,
3451 new + poffset
+ olddirlen
,
3452 oldlen
- poffset
- olddirlen
+ 1);
3453 conf
->unexpanded_config
= new;
3455 memcpy(new + poffset
, newdir
, newdirlen
);
3466 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3467 const char *newpath
, const char *oldname
,
3468 const char *newname
)
3470 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3471 char *lstart
= conf
->unexpanded_config
;
3472 const char *key
= "lxc.hook";
3475 size_t newdirlen
, olddirlen
;
3477 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3478 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3479 ret
= strnprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3483 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3484 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3485 ret
= strnprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3489 if (!conf
->unexpanded_config
)
3493 lend
= strchr(lstart
, '\n');
3495 lend
= lstart
+ strlen(lstart
);
3499 if (!strnequal(lstart
, key
, strlen(key
)))
3502 p
= strchr(lstart
+ strlen(key
), '=');
3513 if (!strnequal(p
, olddir
, strlen(olddir
)))
3516 /* replace the olddir with newdir */
3517 if (olddirlen
>= newdirlen
) {
3518 size_t diff
= olddirlen
- newdirlen
;
3519 memcpy(p
, newdir
, newdirlen
);
3521 if (olddirlen
!= newdirlen
) {
3522 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3523 strlen(p
) - newdirlen
- diff
+ 1);
3525 conf
->unexpanded_len
-= diff
;
3529 size_t diff
= newdirlen
- olddirlen
;
3530 size_t oldlen
= conf
->unexpanded_len
;
3531 size_t newlen
= oldlen
+ diff
;
3532 size_t poffset
= p
- conf
->unexpanded_config
;
3534 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3538 conf
->unexpanded_len
= newlen
;
3539 conf
->unexpanded_alloced
= newlen
+ 1;
3540 new[newlen
- 1] = '\0';
3541 lend
= new + (lend
- conf
->unexpanded_config
);
3543 /* Move over the remainder to make room for the newdir.
3545 memmove(new + poffset
+ newdirlen
,
3546 new + poffset
+ olddirlen
,
3547 oldlen
- poffset
- olddirlen
+ 1);
3548 conf
->unexpanded_config
= new;
3550 memcpy(new + poffset
, newdir
, newdirlen
);
3564 ERROR("Error writing to new config"); \
3569 /* This is called only from clone. We wish to update all hwaddrs in the
3570 * unexpanded config file. We can't/don't want to update any which come from
3571 * lxc.includes (there shouldn't be any).
3572 * We can't just walk the c->lxc-conf->network list because that includes netifs
3573 * from the include files. So we update the ones which we find in the unexp
3574 * config file, then find the original macaddr in the conf->network, and update
3575 * that to the same value.
3577 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3579 char *lend
, *p
, *p2
;
3580 char *lstart
= conf
->unexpanded_config
;
3582 if (!conf
->unexpanded_config
)
3586 char newhwaddr
[18], oldhwaddr
[17];
3587 struct lxc_netdev
*netdev
;
3589 lend
= strchr(lstart
, '\n');
3591 lend
= lstart
+ strlen(lstart
);
3595 if (!lxc_config_net_is_hwaddr(lstart
)) {
3600 p
= strchr(lstart
, '=');
3613 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3616 if ((p2
- p
) != 17) {
3617 WARN("Bad hwaddr entry");
3622 memcpy(oldhwaddr
, p
, 17);
3624 if (!new_hwaddr(newhwaddr
))
3627 memcpy(p
, newhwaddr
, 17);
3628 list_for_each_entry(netdev
, &conf
->netdevs
, head
) {
3629 if (netdev
->hwaddr
&& memcmp(oldhwaddr
, netdev
->hwaddr
, 17) == 0)
3630 memcpy(netdev
->hwaddr
, newhwaddr
, 17);
3639 static int set_config_ephemeral(const char *key
, const char *value
,
3640 struct lxc_conf
*lxc_conf
, void *data
)
3644 if (lxc_config_value_empty(value
)) {
3645 lxc_conf
->ephemeral
= 0;
3649 ret
= lxc_safe_uint(value
, &lxc_conf
->ephemeral
);
3653 if (lxc_conf
->ephemeral
> 1)
3654 return ret_errno(EINVAL
);
3659 static int set_config_log_syslog(const char *key
, const char *value
,
3660 struct lxc_conf
*lxc_conf
, void *data
)
3664 if (lxc_conf
->syslog
)
3665 free_disarm(lxc_conf
->syslog
);
3667 if (lxc_config_value_empty(value
))
3670 facility
= lxc_syslog_priority_to_int(value
);
3671 if (facility
== -EINVAL
)
3672 return ret_errno(EINVAL
);
3674 lxc_log_syslog(facility
);
3676 return set_config_string_item(&lxc_conf
->syslog
, value
);
3679 static int set_config_no_new_privs(const char *key
, const char *value
,
3680 struct lxc_conf
*lxc_conf
, void *data
)
3685 if (lxc_config_value_empty(value
)) {
3686 lxc_conf
->no_new_privs
= false;
3690 ret
= lxc_safe_uint(value
, &v
);
3695 return ret_errno(EINVAL
);
3697 lxc_conf
->no_new_privs
= v
? true : false;
3702 /* Callbacks to get configuration items. */
3703 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3704 struct lxc_conf
*c
, void *data
)
3711 memset(retv
, 0, inlen
);
3715 switch (c
->personality
) {
3717 strprint(retv
, inlen
, "i686");
3720 strprint(retv
, inlen
, "x86_64");
3729 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3730 struct lxc_conf
*c
, void *data
)
3732 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3735 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3736 struct lxc_conf
*c
, void *data
)
3738 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3741 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3742 struct lxc_conf
*c
, void *data
)
3744 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3747 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3748 struct lxc_conf
*c
, void *data
)
3751 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3753 return syserror_set(-EINVAL
, "Built without AppArmor support");
3757 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3758 int inlen
, struct lxc_conf
*c
,
3762 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_incomplete
);
3764 return syserror_set(-EINVAL
, "Built without AppArmor support");
3768 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3769 int inlen
, struct lxc_conf
*c
,
3773 return lxc_get_conf_int(c
, retv
, inlen
, c
->lsm_aa_allow_nesting
);
3775 return syserror_set(-EINVAL
, "Built without AppArmor support");
3779 static int get_config_apparmor_raw(const char *key
, char *retv
,
3780 int inlen
, struct lxc_conf
*c
,
3785 struct string_entry
*entry
;
3791 memset(retv
, 0, inlen
);
3793 list_for_each_entry(entry
, &c
->lsm_aa_raw
, head
) {
3794 strprint(retv
, inlen
, "%s\n", entry
->val
);
3799 return syserror_set(-EINVAL
, "Built without AppArmor support");
3803 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3804 struct lxc_conf
*c
, void *data
)
3807 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3809 return syserror_set(-EINVAL
, "Built without SELinux support");
3813 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3814 struct lxc_conf
*c
, void *data
)
3817 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3819 return syserror_set(-EINVAL
, "Built without SELinux support");
3823 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3824 struct lxc_conf
*c
, void *data
)
3826 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3830 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3831 * just the value(s) will be printed. Since there still could be more than one,
3832 * it is newline-separated.
3833 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3835 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3836 * 'lxc.cgroup.subsystem.key = value' format.
3838 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3839 int inlen
, struct lxc_conf
*c
,
3843 bool get_all
= false;
3845 size_t namespaced_token_len
;
3846 char *global_token
, *namespaced_token
;
3847 struct list_head
*list
;
3848 struct lxc_cgroup
*cgroup
;
3853 memset(retv
, 0, inlen
);
3855 if (version
== CGROUP2_SUPER_MAGIC
) {
3856 global_token
= "lxc.cgroup2";
3857 namespaced_token
= "lxc.cgroup2.";
3858 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3860 } else if (version
== CGROUP_SUPER_MAGIC
) {
3861 global_token
= "lxc.cgroup";
3862 namespaced_token
= "lxc.cgroup.";
3863 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3866 return ret_errno(EINVAL
);
3869 if (strequal(key
, global_token
))
3871 else if (strnequal(key
, namespaced_token
, namespaced_token_len
))
3872 key
+= namespaced_token_len
;
3874 return ret_errno(EINVAL
);
3876 list_for_each_entry(cgroup
, list
, head
) {
3878 if (version
!= cgroup
->version
)
3881 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3882 cgroup
->subsystem
, cgroup
->value
);
3883 } else if (strequal(cgroup
->subsystem
, key
)) {
3884 strprint(retv
, inlen
, "%s\n", cgroup
->value
);
3891 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3892 struct lxc_conf
*c
, void *data
)
3894 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3895 CGROUP_SUPER_MAGIC
);
3898 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3899 struct lxc_conf
*c
, void *data
)
3901 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3902 CGROUP2_SUPER_MAGIC
);
3905 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3906 struct lxc_conf
*lxc_conf
, void *data
)
3911 if (!strequal(key
, "lxc.cgroup.dir"))
3912 return ret_errno(EINVAL
);
3917 memset(retv
, 0, inlen
);
3919 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3924 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3925 struct lxc_conf
*lxc_conf
, void *data
)
3933 memset(retv
, 0, inlen
);
3935 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3940 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3941 struct lxc_conf
*lxc_conf
, void *data
)
3949 memset(retv
, 0, inlen
);
3951 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3956 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3958 struct lxc_conf
*lxc_conf
,
3967 memset(retv
, 0, inlen
);
3969 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3974 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3976 struct lxc_conf
*lxc_conf
,
3985 memset(retv
, 0, inlen
);
3987 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3992 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3993 int inlen
, struct lxc_conf
*lxc_conf
,
3996 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3997 lxc_conf
->cgroup_meta
.relative
);
4000 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
4001 struct lxc_conf
*c
, void *data
)
4004 int len
, listlen
, ret
;
4006 /* "u 1000 1000000 65536"
4008 * let's render this as
4026 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
4027 char buf
[__LXC_IDMAP_STR_BUF
];
4032 memset(retv
, 0, inlen
);
4034 listlen
= list_len(map
, &c
->id_map
, head
);
4035 list_for_each_entry(map
, &c
->id_map
, head
) {
4036 ret
= strnprintf(buf
, sizeof(buf
), "%c %lu %lu %lu",
4037 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
4038 map
->nsid
, map
->hostid
, map
->range
);
4040 return ret_errno(EIO
);
4042 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
4048 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
4049 struct lxc_conf
*c
, void *data
)
4052 v
= lxc_log_priority_to_string(c
->loglevel
);
4053 return lxc_get_conf_str(retv
, inlen
, v
);
4056 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
4057 struct lxc_conf
*c
, void *data
)
4059 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
4062 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
4063 struct lxc_conf
*c
, void *data
)
4065 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
4068 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
4069 struct lxc_conf
*c
, void *data
)
4071 int len
, fulllen
= 0;
4072 const char *sep
= "";
4077 memset(retv
, 0, inlen
);
4079 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
4082 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
4083 case LXC_AUTO_PROC_MIXED
:
4084 strprint(retv
, inlen
, "%sproc:mixed", sep
);
4087 case LXC_AUTO_PROC_RW
:
4088 strprint(retv
, inlen
, "%sproc:rw", sep
);
4095 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
4096 case LXC_AUTO_SYS_RO
:
4097 strprint(retv
, inlen
, "%ssys:ro", sep
);
4100 case LXC_AUTO_SYS_RW
:
4101 strprint(retv
, inlen
, "%ssys:rw", sep
);
4104 case LXC_AUTO_SYS_MIXED
:
4105 strprint(retv
, inlen
, "%ssys:mixed", sep
);
4112 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
4113 case LXC_AUTO_CGROUP_NOSPEC
:
4114 strprint(retv
, inlen
, "%scgroup", sep
);
4116 case LXC_AUTO_CGROUP_MIXED
:
4117 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
4119 case LXC_AUTO_CGROUP_RO
:
4120 strprint(retv
, inlen
, "%scgroup:ro", sep
);
4122 case LXC_AUTO_CGROUP_RW
:
4123 strprint(retv
, inlen
, "%scgroup:rw", sep
);
4125 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
4126 strprint(retv
, inlen
, "%scgroup-full", sep
);
4128 case LXC_AUTO_CGROUP_FULL_MIXED
:
4129 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
4131 case LXC_AUTO_CGROUP_FULL_RO
:
4132 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
4134 case LXC_AUTO_CGROUP_FULL_RW
:
4135 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
4144 static int get_config_mount(const char *key
, char *retv
, int inlen
,
4145 struct lxc_conf
*c
, void *data
)
4147 int len
, fulllen
= 0;
4148 struct string_entry
*entry
;
4153 memset(retv
, 0, inlen
);
4155 list_for_each_entry(entry
, &c
->mount_entries
, head
) {
4156 strprint(retv
, inlen
, "%s\n", entry
->val
);
4162 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4163 struct lxc_conf
*c
, void *data
)
4165 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4168 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4169 struct lxc_conf
*c
, void *data
)
4171 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4174 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4175 struct lxc_conf
*c
, void *data
)
4177 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4180 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4181 struct lxc_conf
*c
, void *data
)
4183 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mnt_opts
.raw_options
);
4186 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4187 struct lxc_conf
*c
, void *data
)
4189 return lxc_get_conf_str(
4191 c
->utsname
? c
->utsname
->nodename
: NULL
);
4194 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4195 struct lxc_conf
*c
, void *data
)
4198 int len
, fulllen
= 0, found
= -1;
4199 struct string_entry
*entry
;
4202 subkey
= strchr(key
, '.');
4204 return ret_errno(EINVAL
);
4206 subkey
= strchr(subkey
+ 1, '.');
4208 return ret_errno(EINVAL
);
4210 if (*subkey
== '\0')
4211 return ret_errno(EINVAL
);
4213 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4214 if (strequal(lxchook_names
[i
], subkey
)) {
4221 return ret_errno(EINVAL
);
4226 memset(retv
, 0, inlen
);
4228 list_for_each_entry(entry
, &c
->hooks
[found
], head
) {
4229 strprint(retv
, inlen
, "%s\n", entry
->val
);
4235 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4236 struct lxc_conf
*c
, void *data
)
4238 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4241 static int get_config_net(const char *key
, char *retv
, int inlen
,
4242 struct lxc_conf
*c
, void *data
)
4244 int len
, fulllen
= 0;
4245 struct lxc_netdev
*netdev
;
4250 memset(retv
, 0, inlen
);
4252 list_for_each_entry(netdev
, &c
->netdevs
, head
) {
4253 const char *t
= lxc_net_type_to_str(netdev
->type
);
4254 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4260 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4261 struct lxc_conf
*c
, void *data
)
4263 int len
, fulllen
= 0;
4264 struct cap_entry
*cap
;
4269 memset(retv
, 0, inlen
);
4271 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4272 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4278 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4279 struct lxc_conf
*c
, void *data
)
4281 int len
, fulllen
= 0;
4282 struct cap_entry
*cap
;
4287 memset(retv
, 0, inlen
);
4289 list_for_each_entry(cap
, &c
->caps
.list
, head
) {
4290 strprint(retv
, inlen
, "%s\n", cap
->cap_name
);
4296 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4297 struct lxc_conf
*c
, void *data
)
4299 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4302 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4303 struct lxc_conf
*c
, void *data
)
4305 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4308 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4309 struct lxc_conf
*c
, void *data
)
4311 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4315 static int get_config_console_buffer_size(const char *key
, char *retv
,
4316 int inlen
, struct lxc_conf
*c
,
4319 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4322 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4323 struct lxc_conf
*c
, void *data
)
4325 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4328 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4329 int inlen
, struct lxc_conf
*c
,
4333 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4335 return ret_errno(ENOSYS
);
4339 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4340 struct lxc_conf
*c
, void *data
)
4342 #ifdef HAVE_SECCOMP_NOTIFY
4343 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4345 return ret_errno(ENOSYS
);
4349 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4350 struct lxc_conf
*c
, void *data
)
4352 #ifdef HAVE_SECCOMP_NOTIFY
4353 return lxc_get_conf_str(retv
, inlen
,
4354 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4355 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4356 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4358 return ret_errno(ENOSYS
);
4362 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4363 struct lxc_conf
*c
, void *data
)
4366 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4368 return ret_errno(ENOSYS
);
4372 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4373 struct lxc_conf
*c
, void *data
)
4375 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4378 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4379 struct lxc_conf
*c
, void *data
)
4381 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4384 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4385 struct lxc_conf
*c
, void *data
)
4387 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4390 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4391 struct lxc_conf
*c
, void *data
)
4393 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4396 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4397 struct lxc_conf
*c
, void *data
)
4399 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4402 static int get_config_start(const char *key
, char *retv
, int inlen
,
4403 struct lxc_conf
*c
, void *data
)
4405 if (strequal(key
+ 10, "auto"))
4406 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4407 else if (strequal(key
+ 10, "delay"))
4408 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4409 else if (strequal(key
+ 10, "order"))
4410 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4412 return ret_errno(EINVAL
);
4415 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4416 struct lxc_conf
*c
, void *data
)
4418 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4421 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4422 struct lxc_conf
*c
, void *data
)
4424 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4427 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4428 int inlen
, struct lxc_conf
*c
,
4431 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4434 static int get_config_group(const char *key
, char *retv
, int inlen
,
4435 struct lxc_conf
*c
, void *data
)
4437 int len
, fulllen
= 0;
4438 struct string_entry
*entry
;
4443 memset(retv
, 0, inlen
);
4445 list_for_each_entry(entry
, &c
->groups
, head
) {
4446 strprint(retv
, inlen
, "%s\n", entry
->val
);
4452 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4453 struct lxc_conf
*c
, void *data
)
4455 int len
, fulllen
= 0;
4456 struct environment_entry
*env
;
4461 memset(retv
, 0, inlen
);
4463 list_for_each_entry(env
, &c
->environment
, head
) {
4464 strprint(retv
, inlen
, "%s=%s\n", env
->key
, env
->val
);
4470 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4471 struct lxc_conf
*c
, void *data
)
4473 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4476 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4477 struct lxc_conf
*c
, void *data
)
4479 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4482 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4483 struct lxc_conf
*c
, void *data
)
4485 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4488 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4489 struct lxc_conf
*c
, void *data
)
4491 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4494 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4495 struct lxc_conf
*c
, void *data
)
4497 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4500 static int get_config_init_groups(const char *key
, char *retv
, int inlen
,
4501 struct lxc_conf
*c
, void *data
)
4503 int fulllen
= 0, len
;
4508 memset(retv
, 0, inlen
);
4510 if (c
->init_groups
.size
== 0)
4513 for (size_t i
= 0; i
< c
->init_groups
.size
; i
++)
4514 strprint(retv
, inlen
, "%s%d", (i
> 0) ? "," : "",
4515 c
->init_groups
.list
[i
]);
4520 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4521 struct lxc_conf
*c
, void *data
)
4523 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4526 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4527 struct lxc_conf
*c
, void *data
)
4529 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4532 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4533 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4534 * printed, in 'lxc.prlimit.resource = value' format.
4536 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4537 struct lxc_conf
*c
, void *data
)
4539 int fulllen
= 0, len
;
4540 bool get_all
= false;
4541 struct lxc_limit
*lim
;
4546 memset(retv
, 0, inlen
);
4548 if (strequal(key
, "lxc.prlimit"))
4550 else if (strnequal(key
, "lxc.prlimit.", 12))
4553 return ret_errno(EINVAL
);
4555 list_for_each_entry(lim
, &c
->limits
, head
) {
4556 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4557 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4560 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4561 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4562 partlen
= STRLITERALLEN("unlimited");
4564 partlen
= sprintf(buf
, "%" PRIu64
,
4565 (uint64_t)lim
->limit
.rlim_cur
);
4568 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4569 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4570 memcpy(buf
+ partlen
, ":unlimited",
4571 STRLITERALLEN(":unlimited") + 1);
4573 sprintf(buf
+ partlen
, ":%" PRIu64
,
4574 (uint64_t)lim
->limit
.rlim_max
);
4578 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4579 lim
->resource
, buf
);
4580 } else if (strequal(lim
->resource
, key
)) {
4581 strprint(retv
, inlen
, "%s", buf
);
4588 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4589 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4590 * entries will be printed, in 'lxc.sysctl.key = value' format.
4592 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4593 struct lxc_conf
*c
, void *data
)
4596 bool get_all
= false;
4598 struct lxc_sysctl
*sysctl
;
4603 memset(retv
, 0, inlen
);
4605 if (strequal(key
, "lxc.sysctl"))
4607 else if (strnequal(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
4608 key
+= STRLITERALLEN("lxc.sysctl.");
4610 return ret_errno(EINVAL
);
4612 list_for_each_entry(sysctl
, &c
->sysctls
, head
) {
4614 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", sysctl
->key
,
4616 } else if (strequal(sysctl
->key
, key
)) {
4617 strprint(retv
, inlen
, "%s", sysctl
->value
);
4624 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4625 struct lxc_conf
*c
, void *data
)
4628 bool get_all
= false;
4630 struct lxc_proc
*proc
;
4635 memset(retv
, 0, inlen
);
4637 if (strequal(key
, "lxc.proc"))
4639 else if (strnequal(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
4640 key
+= STRLITERALLEN("lxc.proc.");
4642 return ret_errno(EINVAL
);
4644 list_for_each_entry(proc
, &c
->procs
, head
) {
4646 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4647 proc
->filename
, proc
->value
);
4648 } else if (strequal(proc
->filename
, key
)) {
4649 strprint(retv
, inlen
, "%s", proc
->value
);
4656 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4657 struct lxc_conf
*c
, void *data
)
4665 memset(retv
, 0, inlen
);
4667 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4668 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4669 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4675 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4676 struct lxc_conf
*c
, void *data
)
4684 memset(retv
, 0, inlen
);
4686 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4687 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4688 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4694 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4703 memset(retv
, 0, inlen
);
4705 if (c
->timens
.s_boot
) {
4706 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4708 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4714 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4715 struct lxc_conf
*c
, void *data
)
4723 memset(retv
, 0, inlen
);
4725 if (c
->timens
.s_monotonic
) {
4726 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4728 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4734 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4735 struct lxc_conf
*c
, void *data
)
4738 const char *namespace;
4744 memset(retv
, 0, inlen
);
4746 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4747 if (is_empty_string(namespace))
4748 return ret_errno(EINVAL
);
4750 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4754 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4759 /* Callbacks to clear config items. */
4760 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4763 c
->personality
= -1;
4767 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4774 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4781 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4784 free_disarm(c
->ttys
.dir
);
4788 static inline int clr_config_apparmor_profile(const char *key
,
4789 struct lxc_conf
*c
, void *data
)
4792 free_disarm(c
->lsm_aa_profile
);
4795 return syserror_set(-EINVAL
, "Built without AppArmor support");
4799 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4804 c
->lsm_aa_allow_incomplete
= 0;
4807 return syserror_set(-EINVAL
, "Built without AppArmor support");
4811 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4816 c
->lsm_aa_allow_nesting
= 0;
4819 return syserror_set(-EINVAL
, "Built without AppArmor support");
4823 static inline int clr_config_apparmor_raw(const char *key
,
4828 return lxc_clear_apparmor_raw(c
);
4830 return syserror_set(-EINVAL
, "Built without AppArmor support");
4834 static inline int clr_config_selinux_context(const char *key
,
4835 struct lxc_conf
*c
, void *data
)
4838 free_disarm(c
->lsm_se_context
);
4841 return syserror_set(-EINVAL
, "Built without SELinux support");
4845 static inline int clr_config_selinux_context_keyring(const char *key
,
4846 struct lxc_conf
*c
, void *data
)
4849 free_disarm(c
->lsm_se_keyring_context
);
4852 return syserror_set(-EINVAL
, "Built without SELinux support");
4856 static inline int clr_config_keyring_session(const char *key
,
4857 struct lxc_conf
*c
, void *data
)
4859 c
->keyring_disable_session
= false;
4863 static inline int clr_config_cgroup_controller(const char *key
,
4864 struct lxc_conf
*c
, void *data
)
4866 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4869 static inline int clr_config_cgroup2_controller(const char *key
,
4870 struct lxc_conf
*c
, void *data
)
4872 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4875 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4878 if (!strequal(key
, "lxc.cgroup.dir"))
4879 return ret_errno(EINVAL
);
4881 if (lxc_conf
->cgroup_meta
.dir
)
4882 free_disarm(lxc_conf
->cgroup_meta
.dir
);
4887 static int clr_config_cgroup_monitor_dir(const char *key
,
4888 struct lxc_conf
*lxc_conf
,
4891 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4895 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4896 struct lxc_conf
*lxc_conf
,
4899 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4903 static int clr_config_cgroup_container_dir(const char *key
,
4904 struct lxc_conf
*lxc_conf
,
4907 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4911 static int clr_config_cgroup_container_inner_dir(const char *key
,
4912 struct lxc_conf
*lxc_conf
,
4915 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4919 static inline int clr_config_cgroup_relative(const char *key
,
4920 struct lxc_conf
*lxc_conf
,
4923 lxc_conf
->cgroup_meta
.relative
= false;
4927 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4930 return lxc_clear_idmaps(c
);
4933 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4936 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4940 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4943 free_disarm(c
->logfile
);
4947 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4950 return lxc_clear_mount_entries(c
);
4953 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4956 return lxc_clear_automounts(c
);
4959 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4962 free_disarm(c
->fstab
);
4966 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4969 free_disarm(c
->rootfs
.path
);
4973 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4976 c
->rootfs
.managed
= true;
4980 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4983 free_disarm(c
->rootfs
.mount
);
4987 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4990 put_lxc_mount_options(&c
->rootfs
.mnt_opts
);
4995 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4998 free_disarm(c
->utsname
);
5002 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
5005 return lxc_clear_hooks(c
, key
);
5008 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
5011 /* default to legacy hooks version */
5012 c
->hooks_version
= 0;
5016 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
5019 lxc_free_networks(c
);
5024 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
5027 return lxc_clear_config_caps(c
);
5030 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
5033 return lxc_clear_config_caps(c
);
5036 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
5039 free_disarm(c
->console
.path
);
5043 static inline int clr_config_console_logfile(const char *key
,
5044 struct lxc_conf
*c
, void *data
)
5046 free_disarm(c
->console
.log_path
);
5050 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
5053 c
->console
.log_rotate
= 0;
5057 static inline int clr_config_console_buffer_size(const char *key
,
5058 struct lxc_conf
*c
, void *data
)
5060 c
->console
.buffer_size
= 0;
5064 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
5067 c
->console
.log_size
= 0;
5071 static inline int clr_config_seccomp_allow_nesting(const char *key
,
5072 struct lxc_conf
*c
, void *data
)
5075 c
->seccomp
.allow_nesting
= 0;
5078 return ret_errno(ENOSYS
);
5082 static inline int clr_config_seccomp_notify_cookie(const char *key
,
5083 struct lxc_conf
*c
, void *data
)
5085 #ifdef HAVE_SECCOMP_NOTIFY
5086 free_disarm(c
->seccomp
.notifier
.cookie
);
5089 return ret_errno(ENOSYS
);
5093 static inline int clr_config_seccomp_notify_proxy(const char *key
,
5094 struct lxc_conf
*c
, void *data
)
5096 #ifdef HAVE_SECCOMP_NOTIFY
5097 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
5098 sizeof(c
->seccomp
.notifier
.proxy_addr
));
5101 return ret_errno(ENOSYS
);
5105 static inline int clr_config_seccomp_profile(const char *key
,
5106 struct lxc_conf
*c
, void *data
)
5108 free_disarm(c
->seccomp
.seccomp
);
5112 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
5119 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
5122 c
->autodevtmpfssize
= 500000;
5126 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
5133 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
5136 c
->rebootsignal
= 0;
5140 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
5147 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
5150 if (strequal(key
+ 10, "auto"))
5152 else if (strequal(key
+ 10, "delay"))
5154 else if (strequal(key
+ 10, "order"))
5160 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
5163 free_disarm(c
->syslog
);
5167 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
5170 c
->monitor_unshare
= 0;
5174 static inline int clr_config_monitor_signal_pdeath(const char *key
,
5175 struct lxc_conf
*c
, void *data
)
5177 c
->monitor_signal_pdeath
= 0;
5181 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
5184 return lxc_clear_groups(c
);
5187 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5190 return lxc_clear_environment(c
);
5193 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5196 free_disarm(c
->execute_cmd
);
5200 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5203 free_disarm(c
->init_cmd
);
5207 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5210 free_disarm(c
->init_cwd
);
5214 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5221 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5228 static inline int clr_config_init_groups(const char *key
, struct lxc_conf
*c
,
5231 c
->init_groups
.size
= 0;
5232 free_disarm(c
->init_groups
.list
);
5236 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5243 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5246 c
->no_new_privs
= false;
5250 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5253 return lxc_clear_limits(c
, key
);
5256 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5259 return lxc_clear_sysctls(c
, key
);
5262 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5265 return lxc_clear_procs(c
, key
);
5268 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5274 static int clr_config_namespace_clone(const char *key
,
5275 struct lxc_conf
*lxc_conf
, void *data
)
5277 lxc_conf
->ns_clone
= 0;
5281 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5284 lxc_conf
->ns_keep
= 0;
5288 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5290 lxc_conf
->timens
.s_boot
= 0;
5291 lxc_conf
->timens
.ns_boot
= 0;
5295 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5297 lxc_conf
->timens
.s_monotonic
= 0;
5298 lxc_conf
->timens
.ns_monotonic
= 0;
5302 static int clr_config_namespace_share(const char *key
,
5303 struct lxc_conf
*lxc_conf
, void *data
)
5306 const char *namespace;
5308 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5309 if (is_empty_string(namespace))
5310 return ret_errno(EINVAL
);
5312 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5316 free(lxc_conf
->ns_share
[ns_idx
]);
5317 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5322 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5323 struct lxc_conf
*c
, void *data
)
5325 return ret_errno(ENOSYS
);
5328 struct config_net_info
{
5329 char buf
[NETWORK_SUBKEY_SIZE_MAX
];
5331 const struct lxc_config_net_t
*ops
;
5332 struct lxc_netdev
*netdev
;
5335 static int get_network_config_ops(const char *key
, struct lxc_conf
*lxc_conf
,
5336 struct config_net_info
*info
, bool allocate
)
5341 const char *idx_start
;
5343 if (is_empty_string(key
))
5344 return ret_errno(EINVAL
);
5346 /* check that this is a sensible network key */
5347 if (!strnequal("lxc.net.", key
, STRLITERALLEN("lxc.net.")))
5348 return syserror_set(-EINVAL
, "Invalid network configuration key \"%s\"", key
);
5351 /* beginning of index string */
5352 idx_start
= key
+ STRLITERALLEN("lxc.net.");
5353 if (!isdigit(*idx_start
))
5354 return syserror_set(-EINVAL
, "Failed to detect digit in string \"%s\"", key
+ 8);
5356 ret
= lxc_safe_int64_residual(idx_start
, &tmpidx
, 10, info
->buf
, sizeof(info
->buf
));
5358 return syserror("Failed to parse network index");
5360 if (tmpidx
< 0 || tmpidx
>= INT_MAX
)
5361 return syserror_set(-ERANGE
, "Number of configured networks would overflow the counter");
5362 idx
= (unsigned int)tmpidx
;
5364 info
->netdev
= lxc_get_netdev_by_idx(lxc_conf
, idx
, allocate
);
5366 return ret_errno(EINVAL
);
5368 /* Make sure subkey points to the empty string. */
5369 info
->subkey
= info
->buf
;
5370 if (is_empty_string(info
->subkey
))
5371 return ret_errno(ENOENT
);
5373 if (info
->subkey
[0] != '.')
5374 return syserror_set(-EINVAL
, "Invalid subkey");
5377 /* lxc.net.<idx>.<subkey> */
5378 info
->ops
= lxc_get_config_net(info
->subkey
);
5379 if (info
->ops
== &unsupported_config_net_key
)
5380 return syserror_set(-ENOENT
, "Unknown network configuration key \"%s\"", key
);
5385 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5386 * found. So we make sure next comes an integer, find the right callback (by
5387 * rewriting the key), and call it.
5389 static int set_config_jump_table_net(const char *key
, const char *value
,
5390 struct lxc_conf
*lxc_conf
, void *data
)
5392 struct config_net_info info
= {};
5394 const char *idxstring
;
5396 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5397 if (!isdigit(*idxstring
))
5398 return ret_errno(EINVAL
);
5400 if (lxc_config_value_empty(value
))
5401 return clr_config_jump_table_net(key
, lxc_conf
, data
);
5403 ret
= get_network_config_ops(key
, lxc_conf
, &info
, true);
5407 return info
.ops
->set(info
.subkey
, value
, lxc_conf
, info
.netdev
);
5410 static int clr_config_jump_table_net(const char *key
, struct lxc_conf
*lxc_conf
,
5413 struct config_net_info info
= {};
5415 const char *idxstring
;
5417 idxstring
= key
+ 8;
5418 if (!isdigit(*idxstring
))
5419 return ret_errno(EINVAL
);
5421 /* The left conjunct is pretty self-explanatory. The right conjunct
5422 * checks whether the two pointers are equal. If they are we know that
5423 * this is not a key that is namespaced any further and so we are
5424 * supposed to clear the whole network.
5426 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5427 unsigned int rmnetdevidx
;
5429 ret
= lxc_safe_uint(idxstring
, &rmnetdevidx
);
5433 /* Remove network from network list. */
5434 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5438 ret
= get_network_config_ops(key
, lxc_conf
, &info
, false);
5442 return info
.ops
->clr(info
.subkey
, lxc_conf
, info
.netdev
);
5445 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5448 struct lxc_netdev
*netdev
= data
;
5451 return ret_errno(EINVAL
);
5453 lxc_clear_netdev(netdev
);
5458 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5461 struct lxc_netdev
*netdev
= data
;
5464 return ret_errno(EINVAL
);
5466 netdev
->name
[0] = '\0';
5471 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5474 struct lxc_netdev
*netdev
= data
;
5477 return ret_errno(EINVAL
);
5484 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5487 struct lxc_netdev
*netdev
= data
;
5490 return ret_errno(EINVAL
);
5492 netdev
->link
[0] = '\0';
5497 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5500 struct lxc_netdev
*netdev
= data
;
5503 return ret_errno(EINVAL
);
5505 netdev
->l2proxy
= false;
5510 static int clr_config_net_macvlan_mode(const char *key
,
5511 struct lxc_conf
*lxc_conf
, void *data
)
5513 struct lxc_netdev
*netdev
= data
;
5516 return ret_errno(EINVAL
);
5518 if (netdev
->type
!= LXC_NET_MACVLAN
)
5521 netdev
->priv
.macvlan_attr
.mode
= -1;
5526 static int clr_config_net_ipvlan_mode(const char *key
,
5527 struct lxc_conf
*lxc_conf
, void *data
)
5529 struct lxc_netdev
*netdev
= data
;
5532 return ret_errno(EINVAL
);
5534 if (netdev
->type
!= LXC_NET_IPVLAN
)
5537 netdev
->priv
.ipvlan_attr
.mode
= -1;
5542 static int clr_config_net_ipvlan_isolation(const char *key
,
5543 struct lxc_conf
*lxc_conf
, void *data
)
5545 struct lxc_netdev
*netdev
= data
;
5548 return ret_errno(EINVAL
);
5550 if (netdev
->type
!= LXC_NET_IPVLAN
)
5553 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5558 static int clr_config_net_veth_mode(const char *key
,
5559 struct lxc_conf
*lxc_conf
, void *data
)
5561 struct lxc_netdev
*netdev
= data
;
5564 return ret_errno(EINVAL
);
5566 if (netdev
->type
!= LXC_NET_VETH
)
5569 netdev
->priv
.veth_attr
.mode
= -1;
5574 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5577 struct lxc_netdev
*netdev
= data
;
5580 return ret_errno(EINVAL
);
5582 if (netdev
->type
!= LXC_NET_VETH
)
5585 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5590 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5593 struct lxc_netdev
*netdev
= data
;
5596 return ret_errno(EINVAL
);
5598 if (netdev
->type
!= LXC_NET_VETH
)
5601 netdev
->priv
.veth_attr
.vlan_id
= 0;
5602 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5607 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5608 struct lxc_conf
*lxc_conf
, void *data
)
5610 struct lxc_netdev
*netdev
= data
;
5611 struct lxc_list
*cur
, *next
;
5614 return ret_errno(EINVAL
);
5616 if (netdev
->type
!= LXC_NET_VETH
)
5619 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5628 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5631 struct lxc_netdev
*netdev
= data
;
5634 return ret_errno(EINVAL
);
5636 free_disarm(netdev
->upscript
);
5641 static int clr_config_net_script_down(const char *key
,
5642 struct lxc_conf
*lxc_conf
, void *data
)
5644 struct lxc_netdev
*netdev
= data
;
5647 return ret_errno(EINVAL
);
5649 free_disarm(netdev
->downscript
);
5654 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5657 struct lxc_netdev
*netdev
= data
;
5660 return ret_errno(EINVAL
);
5662 free_disarm(netdev
->hwaddr
);
5667 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5670 struct lxc_netdev
*netdev
= data
;
5673 return ret_errno(EINVAL
);
5675 free_disarm(netdev
->mtu
);
5680 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5683 struct lxc_netdev
*netdev
= data
;
5686 return ret_errno(EINVAL
);
5688 if (netdev
->type
!= LXC_NET_VLAN
)
5691 netdev
->priv
.vlan_attr
.vid
= 0;
5696 static int clr_config_net_ipv4_gateway(const char *key
,
5697 struct lxc_conf
*lxc_conf
, void *data
)
5699 struct lxc_netdev
*netdev
= data
;
5702 return ret_errno(EINVAL
);
5704 free_disarm(netdev
->ipv4_gateway
);
5709 static int clr_config_net_ipv4_address(const char *key
,
5710 struct lxc_conf
*lxc_conf
, void *data
)
5712 struct lxc_netdev
*netdev
= data
;
5713 struct lxc_inetdev
*inetdev
, *ninetdev
;
5716 return ret_errno(EINVAL
);
5718 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->ipv4_addresses
, head
) {
5719 list_del(&inetdev
->head
);
5726 static int clr_config_net_veth_ipv4_route(const char *key
,
5727 struct lxc_conf
*lxc_conf
, void *data
)
5729 struct lxc_netdev
*netdev
= data
;
5730 struct lxc_inetdev
*inetdev
, *ninetdev
;
5733 return ret_errno(EINVAL
);
5735 if (netdev
->type
!= LXC_NET_VETH
)
5738 list_for_each_entry_safe(inetdev
, ninetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
5739 list_del(&inetdev
->head
);
5746 static int clr_config_net_ipv6_gateway(const char *key
,
5747 struct lxc_conf
*lxc_conf
, void *data
)
5749 struct lxc_netdev
*netdev
= data
;
5752 return ret_errno(EINVAL
);
5754 free_disarm(netdev
->ipv6_gateway
);
5759 static int clr_config_net_ipv6_address(const char *key
,
5760 struct lxc_conf
*lxc_conf
, void *data
)
5762 struct lxc_netdev
*netdev
= data
;
5763 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5766 return ret_errno(EINVAL
);
5768 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->ipv6_addresses
, head
) {
5769 list_del(&inet6dev
->head
);
5776 static int clr_config_net_veth_ipv6_route(const char *key
,
5777 struct lxc_conf
*lxc_conf
, void *data
)
5779 struct lxc_netdev
*netdev
= data
;
5780 struct lxc_inet6dev
*inet6dev
, *ninet6dev
;
5783 return ret_errno(EINVAL
);
5785 if (netdev
->type
!= LXC_NET_VETH
)
5788 list_for_each_entry_safe(inet6dev
, ninet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
5789 list_del(&inet6dev
->head
);
5796 static int get_config_jump_table_net(const char *key
, char *retv
, int inlen
,
5797 struct lxc_conf
*c
, void *data
)
5799 struct config_net_info info
= {};
5801 const char *idxstring
;
5803 idxstring
= key
+ STRLITERALLEN("lxc.net.");
5804 if (!isdigit(*idxstring
))
5805 return ret_errno(EINVAL
);
5807 ret
= get_network_config_ops(key
, c
, &info
, false);
5811 return info
.ops
->get(info
.subkey
, retv
, inlen
, c
, info
.netdev
);
5814 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5815 struct lxc_conf
*c
, void *data
)
5819 struct lxc_netdev
*netdev
= data
;
5822 return ret_errno(EINVAL
);
5827 memset(retv
, 0, inlen
);
5829 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5834 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5835 struct lxc_conf
*c
, void *data
)
5839 struct lxc_netdev
*netdev
= data
;
5842 return ret_errno(EINVAL
);
5847 memset(retv
, 0, inlen
);
5849 if (netdev
->flags
& IFF_UP
)
5850 strprint(retv
, inlen
, "up");
5855 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5856 struct lxc_conf
*c
, void *data
)
5860 struct lxc_netdev
*netdev
= data
;
5863 return ret_errno(EINVAL
);
5868 memset(retv
, 0, inlen
);
5870 if (netdev
->link
[0] != '\0')
5871 strprint(retv
, inlen
, "%s", netdev
->link
);
5876 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5877 struct lxc_conf
*c
, void *data
)
5879 struct lxc_netdev
*netdev
= data
;
5882 return ret_errno(EINVAL
);
5884 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5887 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5888 struct lxc_conf
*c
, void *data
)
5892 struct lxc_netdev
*netdev
= data
;
5895 return ret_errno(EINVAL
);
5900 memset(retv
, 0, inlen
);
5902 if (netdev
->name
[0] != '\0')
5903 strprint(retv
, inlen
, "%s", netdev
->name
);
5908 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5909 struct lxc_conf
*c
, void *data
)
5914 struct lxc_netdev
*netdev
= data
;
5917 return ret_errno(EINVAL
);
5919 if (netdev
->type
!= LXC_NET_MACVLAN
)
5920 return ret_errno(EINVAL
);
5925 memset(retv
, 0, inlen
);
5927 switch (netdev
->priv
.macvlan_attr
.mode
) {
5928 case MACVLAN_MODE_PRIVATE
:
5931 case MACVLAN_MODE_VEPA
:
5934 case MACVLAN_MODE_BRIDGE
:
5937 case MACVLAN_MODE_PASSTHRU
:
5945 strprint(retv
, inlen
, "%s", mode
);
5950 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5951 struct lxc_conf
*c
, void *data
)
5954 struct lxc_netdev
*netdev
= data
;
5959 return ret_errno(EINVAL
);
5961 if (netdev
->type
!= LXC_NET_IPVLAN
)
5962 return ret_errno(EINVAL
);
5967 memset(retv
, 0, inlen
);
5969 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5970 case IPVLAN_MODE_L3
:
5973 case IPVLAN_MODE_L3S
:
5976 case IPVLAN_MODE_L2
:
5984 strprint(retv
, inlen
, "%s", mode
);
5989 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5990 struct lxc_conf
*c
, void *data
)
5993 struct lxc_netdev
*netdev
= data
;
5998 return ret_errno(EINVAL
);
6000 if (netdev
->type
!= LXC_NET_IPVLAN
)
6001 return ret_errno(EINVAL
);
6006 memset(retv
, 0, inlen
);
6008 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
6009 case IPVLAN_ISOLATION_BRIDGE
:
6012 case IPVLAN_ISOLATION_PRIVATE
:
6015 case IPVLAN_ISOLATION_VEPA
:
6023 strprint(retv
, inlen
, "%s", mode
);
6028 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
6029 struct lxc_conf
*c
, void *data
)
6032 struct lxc_netdev
*netdev
= data
;
6037 return ret_errno(EINVAL
);
6039 if (netdev
->type
!= LXC_NET_VETH
)
6040 return ret_errno(EINVAL
);
6045 memset(retv
, 0, inlen
);
6047 switch (netdev
->priv
.veth_attr
.mode
) {
6048 case VETH_MODE_BRIDGE
:
6051 case VETH_MODE_ROUTER
:
6059 strprint(retv
, inlen
, "%s", mode
);
6064 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
6065 struct lxc_conf
*c
, void *data
)
6069 struct lxc_netdev
*netdev
= data
;
6072 return ret_errno(EINVAL
);
6074 if (netdev
->type
!= LXC_NET_VETH
)
6075 return ret_errno(EINVAL
);
6080 memset(retv
, 0, inlen
);
6082 strprint(retv
, inlen
, "%s",
6083 netdev
->priv
.veth_attr
.pair
[0] != '\0'
6084 ? netdev
->priv
.veth_attr
.pair
6085 : netdev
->priv
.veth_attr
.veth1
);
6090 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
6091 struct lxc_conf
*c
, void *data
)
6095 struct lxc_netdev
*netdev
= data
;
6098 return ret_errno(EINVAL
);
6100 if (netdev
->type
!= LXC_NET_VETH
)
6101 return ret_errno(EINVAL
);
6106 memset(retv
, 0, inlen
);
6108 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
6113 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
,
6114 int inlen
, struct lxc_conf
*c
,
6119 struct lxc_list
*it
;
6121 struct lxc_netdev
*netdev
= data
;
6124 return ret_errno(EINVAL
);
6126 if (netdev
->type
!= LXC_NET_VETH
)
6127 return ret_errno(EINVAL
);
6132 memset(retv
, 0, inlen
);
6134 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
6136 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
6137 unsigned short i
= PTR_TO_USHORT(it
->elem
);
6138 strprint(retv
, inlen
, "%u%s", i
, (listlen
-- > 1) ? "\n" : "");
6144 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6145 struct lxc_conf
*c
, void *data
)
6149 struct lxc_netdev
*netdev
= data
;
6152 return ret_errno(EINVAL
);
6157 memset(retv
, 0, inlen
);
6159 if (netdev
->upscript
)
6160 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6165 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6166 struct lxc_conf
*c
, void *data
)
6170 struct lxc_netdev
*netdev
= data
;
6173 return ret_errno(EINVAL
);
6178 memset(retv
, 0, inlen
);
6180 if (netdev
->downscript
)
6181 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6186 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6187 struct lxc_conf
*c
, void *data
)
6191 struct lxc_netdev
*netdev
= data
;
6194 return ret_errno(EINVAL
);
6199 memset(retv
, 0, inlen
);
6202 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6207 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6208 struct lxc_conf
*c
, void *data
)
6212 struct lxc_netdev
*netdev
= data
;
6215 return ret_errno(EINVAL
);
6220 memset(retv
, 0, inlen
);
6223 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6228 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6229 struct lxc_conf
*c
, void *data
)
6233 struct lxc_netdev
*netdev
= data
;
6236 return ret_errno(EINVAL
);
6238 if (netdev
->type
!= LXC_NET_VLAN
)
6239 return ret_errno(EINVAL
);
6244 memset(retv
, 0, inlen
);
6246 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6251 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6252 struct lxc_conf
*c
, void *data
)
6255 char buf
[INET_ADDRSTRLEN
];
6257 struct lxc_netdev
*netdev
= data
;
6260 return ret_errno(EINVAL
);
6265 memset(retv
, 0, inlen
);
6267 if (netdev
->ipv4_gateway_auto
) {
6268 strprint(retv
, inlen
, "auto");
6269 } else if (netdev
->ipv4_gateway_dev
) {
6270 strprint(retv
, inlen
, "dev");
6271 } else if (netdev
->ipv4_gateway
) {
6272 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
)))
6274 strprint(retv
, inlen
, "%s", buf
);
6280 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6281 struct lxc_conf
*c
, void *data
)
6284 struct lxc_netdev
*netdev
= data
;
6287 char buf
[INET_ADDRSTRLEN
];
6288 struct lxc_inetdev
*inetdev
;
6291 return ret_errno(EINVAL
);
6296 memset(retv
, 0, inlen
);
6298 listlen
= list_len(inetdev
, &netdev
->ipv4_addresses
, head
);
6300 list_for_each_entry(inetdev
, &netdev
->ipv4_addresses
, head
) {
6301 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6303 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6304 (listlen
-- > 1) ? "\n" : "");
6310 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6311 struct lxc_conf
*c
, void *data
)
6315 char buf
[INET_ADDRSTRLEN
];
6316 struct lxc_inetdev
*inetdev
;
6318 struct lxc_netdev
*netdev
= data
;
6321 return ret_errno(EINVAL
);
6323 if (netdev
->type
!= LXC_NET_VETH
)
6324 return ret_errno(EINVAL
);
6329 memset(retv
, 0, inlen
);
6331 listlen
= list_len(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
);
6332 list_for_each_entry(inetdev
, &netdev
->priv
.veth_attr
.ipv4_routes
, head
) {
6333 if (!inet_ntop(AF_INET
, &inetdev
->addr
, buf
, sizeof(buf
)))
6335 strprint(retv
, inlen
, "%s/%u%s", buf
, inetdev
->prefix
,
6336 (listlen
-- > 1) ? "\n" : "");
6342 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6343 struct lxc_conf
*c
, void *data
)
6346 char buf
[INET6_ADDRSTRLEN
];
6348 struct lxc_netdev
*netdev
= data
;
6351 return ret_errno(EINVAL
);
6356 memset(retv
, 0, inlen
);
6358 if (netdev
->ipv6_gateway_auto
) {
6359 strprint(retv
, inlen
, "auto");
6360 } else if (netdev
->ipv6_gateway_dev
) {
6361 strprint(retv
, inlen
, "dev");
6362 } else if (netdev
->ipv6_gateway
) {
6363 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
)))
6365 strprint(retv
, inlen
, "%s", buf
);
6371 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6372 struct lxc_conf
*c
, void *data
)
6376 char buf
[INET6_ADDRSTRLEN
];
6378 struct lxc_netdev
*netdev
= data
;
6379 struct lxc_inet6dev
*inet6dev
;
6382 return ret_errno(EINVAL
);
6387 memset(retv
, 0, inlen
);
6389 listlen
= list_len(inet6dev
, &netdev
->ipv6_addresses
, head
);
6390 list_for_each_entry(inet6dev
, &netdev
->ipv6_addresses
, head
) {
6391 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6393 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6394 (listlen
-- > 1) ? "\n" : "");
6400 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6401 struct lxc_conf
*c
, void *data
)
6405 char buf
[INET6_ADDRSTRLEN
];
6406 struct lxc_inet6dev
*inet6dev
;
6408 struct lxc_netdev
*netdev
= data
;
6411 return ret_errno(EINVAL
);
6413 if (netdev
->type
!= LXC_NET_VETH
)
6414 return ret_errno(EINVAL
);
6419 memset(retv
, 0, inlen
);
6421 listlen
= list_len(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
);
6422 list_for_each_entry(inet6dev
, &netdev
->priv
.veth_attr
.ipv6_routes
, head
) {
6423 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, buf
, sizeof(buf
)))
6425 strprint(retv
, inlen
, "%s/%u%s", buf
, inet6dev
->prefix
,
6426 (listlen
-- > 1) ? "\n" : "");
6432 int lxc_list_config_items(char *retv
, int inlen
)
6441 memset(retv
, 0, inlen
);
6443 for (i
= 0; i
< ARRAY_SIZE(config_jump_table
); i
++) {
6444 char *s
= config_jump_table
[i
].name
;
6446 if (s
[strlen(s
) - 1] == '.')
6449 strprint(retv
, inlen
, "%s\n", s
);
6455 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6464 memset(retv
, 0, inlen
);
6466 if (strequal(key
, "lxc.apparmor")) {
6467 strprint(retv
, inlen
, "allow_incomplete\n");
6468 strprint(retv
, inlen
, "allow_nesting\n");
6469 strprint(retv
, inlen
, "profile\n");
6470 strprint(retv
, inlen
, "raw\n");
6471 } else if (strequal(key
, "lxc.cgroup")) {
6472 strprint(retv
, inlen
, "dir\n");
6473 } else if (strequal(key
, "lxc.selinux")) {
6474 strprint(retv
, inlen
, "context\n");
6475 strprint(retv
, inlen
, "context.keyring\n");
6476 } else if (strequal(key
, "lxc.mount")) {
6477 strprint(retv
, inlen
, "auto\n");
6478 strprint(retv
, inlen
, "entry\n");
6479 strprint(retv
, inlen
, "fstab\n");
6480 } else if (strequal(key
, "lxc.rootfs")) {
6481 strprint(retv
, inlen
, "mount\n");
6482 strprint(retv
, inlen
, "options\n");
6483 strprint(retv
, inlen
, "path\n");
6484 } else if (strequal(key
, "lxc.uts")) {
6485 strprint(retv
, inlen
, "name\n");
6486 } else if (strequal(key
, "lxc.hook")) {
6487 strprint(retv
, inlen
, "autodev\n");
6488 strprint(retv
, inlen
, "autodevtmpfssize\n");
6489 strprint(retv
, inlen
, "clone\n");
6490 strprint(retv
, inlen
, "destroy\n");
6491 strprint(retv
, inlen
, "mount\n");
6492 strprint(retv
, inlen
, "post-stop\n");
6493 strprint(retv
, inlen
, "pre-mount\n");
6494 strprint(retv
, inlen
, "pre-start\n");
6495 strprint(retv
, inlen
, "start-host\n");
6496 strprint(retv
, inlen
, "start\n");
6497 strprint(retv
, inlen
, "stop\n");
6498 } else if (strequal(key
, "lxc.cap")) {
6499 strprint(retv
, inlen
, "drop\n");
6500 strprint(retv
, inlen
, "keep\n");
6501 } else if (strequal(key
, "lxc.console")) {
6502 strprint(retv
, inlen
, "logfile\n");
6503 strprint(retv
, inlen
, "path\n");
6504 } else if (strequal(key
, "lxc.seccomp")) {
6505 strprint(retv
, inlen
, "profile\n");
6506 } else if (strequal(key
, "lxc.signal")) {
6507 strprint(retv
, inlen
, "halt\n");
6508 strprint(retv
, inlen
, "reboot\n");
6509 strprint(retv
, inlen
, "stop\n");
6510 } else if (strequal(key
, "lxc.start")) {
6511 strprint(retv
, inlen
, "auto\n");
6512 strprint(retv
, inlen
, "delay\n");
6513 strprint(retv
, inlen
, "order\n");
6514 } else if (strequal(key
, "lxc.monitor")) {
6515 strprint(retv
, inlen
, "unshare\n");
6516 } else if (strequal(key
, "lxc.keyring")) {
6517 strprint(retv
, inlen
, "session\n");
6519 fulllen
= ret_errno(EINVAL
);
6525 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6527 struct config_net_info info
= {};
6528 struct lxc_netdev
*netdev
;
6530 const char *idxstring
;
6533 idxstring
= key
+ STRLITERALLEN("lxc.net.");
6534 if (!isdigit(*idxstring
))
6535 return ret_errno(EINVAL
);
6537 ret
= get_network_config_ops(key
, c
, &info
, false);
6540 return ret_errno(EINVAL
);
6542 netdev
= info
.netdev
;
6547 memset(retv
, 0, inlen
);
6549 strprint(retv
, inlen
, "type\n");
6550 strprint(retv
, inlen
, "script.up\n");
6551 strprint(retv
, inlen
, "script.down\n");
6553 if (netdev
->type
!= LXC_NET_EMPTY
) {
6554 strprint(retv
, inlen
, "flags\n");
6555 strprint(retv
, inlen
, "link\n");
6556 strprint(retv
, inlen
, "name\n");
6557 strprint(retv
, inlen
, "hwaddr\n");
6558 strprint(retv
, inlen
, "mtu\n");
6559 strprint(retv
, inlen
, "ipv6.address\n");
6560 strprint(retv
, inlen
, "ipv6.gateway\n");
6561 strprint(retv
, inlen
, "ipv4.address\n");
6562 strprint(retv
, inlen
, "ipv4.gateway\n");
6565 switch (netdev
->type
) {
6567 strprint(retv
, inlen
, "veth.pair\n");
6568 strprint(retv
, inlen
, "veth.ipv4.route\n");
6569 strprint(retv
, inlen
, "veth.ipv6.route\n");
6570 strprint(retv
, inlen
, "veth.vlan.id\n");
6572 case LXC_NET_MACVLAN
:
6573 strprint(retv
, inlen
, "macvlan.mode\n");
6575 case LXC_NET_IPVLAN
:
6576 strprint(retv
, inlen
, "ipvlan.mode\n");
6577 strprint(retv
, inlen
, "ipvlan.isolation\n");
6580 strprint(retv
, inlen
, "vlan.id\n");
6589 static int set_config_sched_core(const char *key
, const char *value
,
6590 struct lxc_conf
*lxc_conf
, void *data
)
6595 if (lxc_config_value_empty(value
))
6596 return clr_config_sched_core(key
, lxc_conf
, data
);
6598 ret
= lxc_safe_uint(value
, &nr
);
6600 return ret_errno(EINVAL
);
6602 if (nr
!= 0 && nr
!= 1)
6603 return ret_errno(EINVAL
);
6605 lxc_conf
->sched_core
= (nr
== 1);
6609 static int get_config_sched_core(const char *key
, char *retv
, int inlen
,
6610 struct lxc_conf
*c
, void *data
)
6612 return lxc_get_conf_bool(c
, retv
, inlen
, c
->sched_core
);
6615 static int clr_config_sched_core(const char *key
, struct lxc_conf
*c
, void *data
)
6617 c
->sched_core
= false;