1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #define __STDC_FORMAT_MACROS
14 #include <netinet/in.h>
19 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/utsname.h>
31 #include "confile_utils.h"
32 #include "../include/netns_ifaddrs.h"
34 #include "lxcseccomp.h"
36 #include "memory_utils.h"
42 #if HAVE_SYS_PERSONALITY_H
43 #include <sys/personality.h>
47 #include "include/strlcpy.h"
51 #include "include/strlcat.h"
54 lxc_log_define(confile
, lxc
);
56 #define lxc_config_define(name) \
57 __hot static int set_config_##name(const char *, const char *, \
58 struct lxc_conf *, void *); \
59 __hot static int get_config_##name(const char *, char *, int, \
60 struct lxc_conf *, void *); \
61 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
64 lxc_config_define(autodev
);
65 lxc_config_define(autodev_tmpfs_size
);
66 lxc_config_define(apparmor_allow_incomplete
);
67 lxc_config_define(apparmor_allow_nesting
);
68 lxc_config_define(apparmor_profile
);
69 lxc_config_define(apparmor_raw
);
70 lxc_config_define(cap_drop
);
71 lxc_config_define(cap_keep
);
72 lxc_config_define(cgroup_controller
);
73 lxc_config_define(cgroup2_controller
);
74 lxc_config_define(cgroup_dir
);
75 lxc_config_define(cgroup_monitor_dir
);
76 lxc_config_define(cgroup_monitor_pivot_dir
);
77 lxc_config_define(cgroup_container_dir
);
78 lxc_config_define(cgroup_container_inner_dir
);
79 lxc_config_define(cgroup_relative
);
80 lxc_config_define(console_buffer_size
);
81 lxc_config_define(console_logfile
);
82 lxc_config_define(console_path
);
83 lxc_config_define(console_rotate
);
84 lxc_config_define(console_size
);
85 lxc_config_define(environment
);
86 lxc_config_define(ephemeral
);
87 lxc_config_define(execute_cmd
);
88 lxc_config_define(group
);
89 lxc_config_define(hooks
);
90 lxc_config_define(hooks_version
);
91 lxc_config_define(idmaps
);
92 lxc_config_define(includefiles
);
93 lxc_config_define(init_cmd
);
94 lxc_config_define(init_cwd
);
95 lxc_config_define(init_gid
);
96 lxc_config_define(init_uid
);
97 lxc_config_define(keyring_session
);
98 lxc_config_define(log_file
);
99 lxc_config_define(log_level
);
100 lxc_config_define(log_syslog
);
101 lxc_config_define(monitor
);
102 lxc_config_define(monitor_signal_pdeath
);
103 lxc_config_define(mount
);
104 lxc_config_define(mount_auto
);
105 lxc_config_define(mount_fstab
);
106 lxc_config_define(namespace_clone
);
107 lxc_config_define(namespace_keep
);
108 lxc_config_define(time_offset_boot
);
109 lxc_config_define(time_offset_monotonic
);
110 lxc_config_define(namespace_share
);
111 lxc_config_define(net
);
112 lxc_config_define(net_flags
);
113 lxc_config_define(net_hwaddr
);
114 lxc_config_define(net_ipv4_address
);
115 lxc_config_define(net_ipv4_gateway
);
116 lxc_config_define(net_ipv6_address
);
117 lxc_config_define(net_ipv6_gateway
);
118 lxc_config_define(net_link
);
119 lxc_config_define(net_l2proxy
);
120 lxc_config_define(net_macvlan_mode
);
121 lxc_config_define(net_ipvlan_mode
);
122 lxc_config_define(net_ipvlan_isolation
);
123 lxc_config_define(net_mtu
);
124 lxc_config_define(net_name
);
125 lxc_config_define(net_nic
);
126 lxc_config_define(net_script_down
);
127 lxc_config_define(net_script_up
);
128 lxc_config_define(net_type
);
129 lxc_config_define(net_veth_mode
);
130 lxc_config_define(net_veth_pair
);
131 lxc_config_define(net_veth_ipv4_route
);
132 lxc_config_define(net_veth_ipv6_route
);
133 lxc_config_define(net_veth_vlan_id
);
134 lxc_config_define(net_veth_vlan_tagged_id
);
135 lxc_config_define(net_vlan_id
);
136 lxc_config_define(no_new_privs
);
137 lxc_config_define(personality
);
138 lxc_config_define(prlimit
);
139 lxc_config_define(pty_max
);
140 lxc_config_define(rootfs_managed
);
141 lxc_config_define(rootfs_mount
);
142 lxc_config_define(rootfs_options
);
143 lxc_config_define(rootfs_path
);
144 lxc_config_define(seccomp_profile
);
145 lxc_config_define(seccomp_allow_nesting
);
146 lxc_config_define(seccomp_notify_cookie
);
147 lxc_config_define(seccomp_notify_proxy
);
148 lxc_config_define(selinux_context
);
149 lxc_config_define(selinux_context_keyring
);
150 lxc_config_define(signal_halt
);
151 lxc_config_define(signal_reboot
);
152 lxc_config_define(signal_stop
);
153 lxc_config_define(start
);
154 lxc_config_define(tty_max
);
155 lxc_config_define(tty_dir
);
156 lxc_config_define(uts_name
);
157 lxc_config_define(sysctl
);
158 lxc_config_define(proc
);
162 * If a new config option is added to this table, be aware that
163 * the order in which the options are places into the table matters.
164 * That means that more specific options of a namespace have to be
165 * placed above more generic ones.
167 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
168 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
169 * has to be placed above lxc.ab.
171 static struct lxc_config_t config_jump_table
[] = {
172 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
173 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
174 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
175 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
176 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
177 { "lxc.autodev.tmpfs.size", set_config_autodev_tmpfs_size
, get_config_autodev_tmpfs_size
, clr_config_autodev_tmpfs_size
, },
178 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
179 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
180 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
181 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
182 { "lxc.cgroup.dir.monitor.pivot", set_config_cgroup_monitor_pivot_dir
, get_config_cgroup_monitor_pivot_dir
, clr_config_cgroup_monitor_pivot_dir
, },
183 { "lxc.cgroup.dir.monitor", set_config_cgroup_monitor_dir
, get_config_cgroup_monitor_dir
, clr_config_cgroup_monitor_dir
, },
184 { "lxc.cgroup.dir.container.inner", set_config_cgroup_container_inner_dir
, get_config_cgroup_container_inner_dir
, clr_config_cgroup_container_inner_dir
, },
185 { "lxc.cgroup.dir.container", set_config_cgroup_container_dir
, get_config_cgroup_container_dir
, clr_config_cgroup_container_dir
, },
186 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
187 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
188 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
189 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
190 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
191 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
192 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
193 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
194 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
195 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
196 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
197 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
198 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
199 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
200 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
201 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
202 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
203 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
204 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
205 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
206 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
207 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
208 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
209 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
210 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
211 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
212 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
213 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
214 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
215 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
216 { "lxc.keyring.session", set_config_keyring_session
, get_config_keyring_session
, clr_config_keyring_session
},
217 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
218 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
219 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
220 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
221 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
222 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
223 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
224 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
225 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
226 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
227 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
228 { "lxc.time.offset.boot", set_config_time_offset_boot
, get_config_time_offset_boot
, clr_config_time_offset_boot
, },
229 { "lxc.time.offset.monotonic", set_config_time_offset_monotonic
, get_config_time_offset_monotonic
, clr_config_time_offset_monotonic
, },
230 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
231 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
232 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
233 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
234 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
235 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
236 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
237 { "lxc.net.l2proxy", set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
238 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
239 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
240 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
241 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
242 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
243 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
244 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
245 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
246 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
247 { "lxc.net.veth.mode", set_config_net_veth_mode
, get_config_net_veth_mode
, clr_config_net_veth_mode
, },
248 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
249 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
250 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
251 { "lxc.net.veth.vlan.id", set_config_net_veth_vlan_id
, get_config_net_veth_vlan_id
, clr_config_net_veth_vlan_id
, },
252 { "lxc.net.veth.vlan.tagged.id", set_config_net_veth_vlan_tagged_id
, get_config_net_veth_vlan_tagged_id
, clr_config_net_veth_vlan_tagged_id
, },
253 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
254 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
255 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
256 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
257 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
258 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
259 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
260 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
261 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
262 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
263 { "lxc.seccomp.notify.cookie", set_config_seccomp_notify_cookie
, get_config_seccomp_notify_cookie
, clr_config_seccomp_notify_cookie
, },
264 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
265 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
266 { "lxc.selinux.context.keyring", set_config_selinux_context_keyring
, get_config_selinux_context_keyring
, clr_config_selinux_context_keyring
},
267 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
268 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
269 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
270 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
271 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
272 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
273 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
274 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
275 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
276 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
277 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
278 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
281 static const size_t config_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
283 struct lxc_config_t
*lxc_get_config_exact(const char *key
)
287 for (i
= 0; i
< config_jump_table_size
; i
++)
288 if (!strcmp(config_jump_table
[i
].name
, key
))
289 return &config_jump_table
[i
];
295 struct lxc_config_t
*lxc_get_config(const char *key
)
299 for (i
= 0; i
< config_jump_table_size
; i
++)
300 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
301 return &config_jump_table
[i
];
306 static int set_config_net(const char *key
, const char *value
,
307 struct lxc_conf
*lxc_conf
, void *data
)
309 if (!lxc_config_value_empty(value
)) {
310 ERROR("lxc.net must not have a value");
314 return clr_config_net(key
, lxc_conf
, data
);
317 static int set_config_net_type(const char *key
, const char *value
,
318 struct lxc_conf
*lxc_conf
, void *data
)
320 struct lxc_netdev
*netdev
= data
;
322 if (lxc_config_value_empty(value
))
323 return clr_config_net_type(key
, lxc_conf
, data
);
326 return ret_errno(EINVAL
);
328 if (strcmp(value
, "veth") == 0) {
329 netdev
->type
= LXC_NET_VETH
;
330 lxc_list_init(&netdev
->priv
.veth_attr
.ipv4_routes
);
331 lxc_list_init(&netdev
->priv
.veth_attr
.ipv6_routes
);
332 lxc_list_init(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
333 if (!lxc_veth_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
334 lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, "bridge");
335 } else if (strcmp(value
, "macvlan") == 0) {
336 netdev
->type
= LXC_NET_MACVLAN
;
337 if (!lxc_macvlan_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
338 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
339 } else if (strcmp(value
, "ipvlan") == 0) {
340 netdev
->type
= LXC_NET_IPVLAN
;
341 if (!lxc_ipvlan_flag_to_mode(netdev
->priv
.ipvlan_attr
.mode
))
342 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
343 if (!lxc_ipvlan_flag_to_isolation(netdev
->priv
.ipvlan_attr
.isolation
))
344 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
345 } else if (strcmp(value
, "vlan") == 0) {
346 netdev
->type
= LXC_NET_VLAN
;
347 } else if (strcmp(value
, "phys") == 0) {
348 netdev
->type
= LXC_NET_PHYS
;
349 } else if (strcmp(value
, "empty") == 0) {
350 netdev
->type
= LXC_NET_EMPTY
;
351 } else if (strcmp(value
, "none") == 0) {
352 netdev
->type
= LXC_NET_NONE
;
354 return log_error(-1, "Invalid network type %s", value
);
360 static int set_config_net_flags(const char *key
, const char *value
,
361 struct lxc_conf
*lxc_conf
, void *data
)
363 struct lxc_netdev
*netdev
= data
;
365 if (lxc_config_value_empty(value
))
366 return clr_config_net_flags(key
, lxc_conf
, data
);
369 return ret_errno(EINVAL
);
371 netdev
->flags
|= IFF_UP
;
376 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
377 struct lxc_netdev
*netdev
)
379 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddr
= NULL
;
380 struct netns_ifaddrs
*ifa
;
383 const char *type_key
= "lxc.net.type";
384 const char *link_key
= "lxc.net.link";
385 const char *tmpvalue
= "phys";
387 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0)
388 return log_error_errno(-1, errno
, "Failed to get network interfaces");
390 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
394 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
397 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
398 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
401 ret
= set_config_net_link(
402 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
404 ERROR("Failed to create matched ifnames");
408 ERROR("Failed to create matched ifnames");
417 static int set_config_net_link(const char *key
, const char *value
,
418 struct lxc_conf
*lxc_conf
, void *data
)
420 struct lxc_netdev
*netdev
= data
;
423 if (lxc_config_value_empty(value
))
424 return clr_config_net_link(key
, lxc_conf
, data
);
427 return ret_errno(EINVAL
);
429 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
430 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
432 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
437 static int set_config_net_l2proxy(const char *key
, const char *value
,
438 struct lxc_conf
*lxc_conf
, void *data
)
440 struct lxc_netdev
*netdev
= data
;
441 unsigned int val
= 0;
444 if (lxc_config_value_empty(value
))
445 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
448 return ret_errno(EINVAL
);
450 ret
= lxc_safe_uint(value
, &val
);
452 return ret_errno(-ret
);
456 netdev
->l2proxy
= false;
459 netdev
->l2proxy
= true;
463 return ret_set_errno(-1, EINVAL
);
466 static int set_config_net_name(const char *key
, const char *value
,
467 struct lxc_conf
*lxc_conf
, void *data
)
469 struct lxc_netdev
*netdev
= data
;
471 if (lxc_config_value_empty(value
))
472 return clr_config_net_name(key
, lxc_conf
, data
);
475 return ret_errno(EINVAL
);
477 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
481 static int set_config_net_veth_mode(const char *key
, const char *value
,
482 struct lxc_conf
*lxc_conf
, void *data
)
484 struct lxc_netdev
*netdev
= data
;
486 if (lxc_config_value_empty(value
))
487 return clr_config_net_veth_mode(key
, lxc_conf
, data
);
490 return ret_errno(EINVAL
);
492 return lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, value
);
495 static int set_config_net_veth_pair(const char *key
, const char *value
,
496 struct lxc_conf
*lxc_conf
, void *data
)
498 struct lxc_netdev
*netdev
= data
;
500 if (lxc_config_value_empty(value
))
501 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
504 return ret_errno(EINVAL
);
506 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
,
507 sizeof(netdev
->priv
.veth_attr
.pair
));
510 static int set_config_net_veth_vlan_id(const char *key
, const char *value
,
511 struct lxc_conf
*lxc_conf
, void *data
)
514 struct lxc_netdev
*netdev
= data
;
517 return ret_errno(EINVAL
);
519 if (lxc_config_value_empty(value
))
520 return clr_config_net_veth_vlan_id(key
, lxc_conf
, data
);
522 if (strcmp(value
, "none") == 0) {
523 netdev
->priv
.veth_attr
.vlan_id
= BRIDGE_VLAN_NONE
;
525 unsigned short vlan_id
;
526 ret
= get_u16(&vlan_id
, value
, 0);
528 return ret_errno(EINVAL
);
530 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
531 return ret_errno(EINVAL
);
533 netdev
->priv
.veth_attr
.vlan_id
= vlan_id
;
536 netdev
->priv
.veth_attr
.vlan_id_set
= true;
540 static int set_config_net_veth_vlan_tagged_id(const char *key
, const char *value
,
541 struct lxc_conf
*lxc_conf
, void *data
)
543 __do_free
struct lxc_list
*list
= NULL
;
545 unsigned short vlan_id
;
546 struct lxc_netdev
*netdev
= data
;
549 return ret_errno(EINVAL
);
551 if (lxc_config_value_empty(value
))
552 return clr_config_net_veth_vlan_tagged_id(key
, lxc_conf
, data
);
554 ret
= get_u16(&vlan_id
, value
, 0);
558 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
561 list
= malloc(sizeof(*list
));
563 return ret_errno(ENOMEM
);
566 list
->elem
= UINT_TO_PTR(vlan_id
);
568 lxc_list_add_tail(&netdev
->priv
.veth_attr
.vlan_tagged_ids
, move_ptr(list
));
573 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
574 struct lxc_conf
*lxc_conf
, void *data
)
576 struct lxc_netdev
*netdev
= data
;
578 if (lxc_config_value_empty(value
))
579 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
582 return ret_errno(EINVAL
);
584 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
587 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
588 struct lxc_conf
*lxc_conf
, void *data
)
590 struct lxc_netdev
*netdev
= data
;
592 if (lxc_config_value_empty(value
))
593 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
596 return ret_errno(EINVAL
);
598 if (netdev
->type
!= LXC_NET_IPVLAN
)
599 return log_error_errno(-EINVAL
,
600 EINVAL
, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network",
603 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
606 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
607 struct lxc_conf
*lxc_conf
, void *data
)
609 struct lxc_netdev
*netdev
= data
;
611 if (lxc_config_value_empty(value
))
612 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
615 return ret_errno(EINVAL
);
617 if (netdev
->type
!= LXC_NET_IPVLAN
)
618 return log_error_errno(-EINVAL
,
619 EINVAL
, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network",
622 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
625 static int set_config_net_hwaddr(const char *key
, const char *value
,
626 struct lxc_conf
*lxc_conf
, void *data
)
628 struct lxc_netdev
*netdev
= data
;
631 if (lxc_config_value_empty(value
))
632 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
635 return ret_errno(EINVAL
);
637 new_value
= strdup(value
);
639 return ret_errno(ENOMEM
);
641 rand_complete_hwaddr(new_value
);
643 if (lxc_config_value_empty(new_value
)) {
645 netdev
->hwaddr
= NULL
;
649 netdev
->hwaddr
= new_value
;
654 static int set_config_net_vlan_id(const char *key
, const char *value
,
655 struct lxc_conf
*lxc_conf
, void *data
)
658 struct lxc_netdev
*netdev
= data
;
660 if (lxc_config_value_empty(value
))
661 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
664 return ret_errno(EINVAL
);
666 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
673 static int set_config_net_mtu(const char *key
, const char *value
,
674 struct lxc_conf
*lxc_conf
, void *data
)
676 struct lxc_netdev
*netdev
= data
;
678 if (lxc_config_value_empty(value
))
679 return clr_config_net_mtu(key
, lxc_conf
, data
);
682 return ret_errno(EINVAL
);
684 return set_config_string_item(&netdev
->mtu
, value
);
687 static int set_config_net_ipv4_address(const char *key
, const char *value
,
688 struct lxc_conf
*lxc_conf
, void *data
)
690 __do_free
char *addr
= NULL
;
691 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
692 __do_free
struct lxc_list
*list
= NULL
;
694 struct lxc_netdev
*netdev
= data
;
695 char *cursor
, *slash
;
696 char *bcast
= NULL
, *prefix
= NULL
;
698 if (lxc_config_value_empty(value
))
699 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
702 return ret_errno(EINVAL
);
704 inetdev
= malloc(sizeof(*inetdev
));
706 return ret_errno(ENOMEM
);
707 memset(inetdev
, 0, sizeof(*inetdev
));
709 list
= malloc(sizeof(*list
));
711 return ret_errno(ENOMEM
);
715 addr
= strdup(value
);
717 return ret_errno(ENOMEM
);
719 cursor
= strstr(addr
, " ");
725 slash
= strstr(addr
, "/");
731 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
733 return log_error_errno(-1, errno
, "Invalid ipv4 address \"%s\"", value
);
736 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
738 return log_error_errno(-1, errno
, "Invalid ipv4 broadcast address \"%s\"", value
);
742 /* No prefix specified, determine it from the network class. */
744 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
748 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
751 /* If no broadcast address, let compute one from the
752 * prefix and address.
755 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
756 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
759 list
->elem
= inetdev
;
760 lxc_list_add_tail(&netdev
->ipv4
, list
);
767 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
768 struct lxc_conf
*lxc_conf
, void *data
)
770 struct lxc_netdev
*netdev
= data
;
772 if (lxc_config_value_empty(value
))
773 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
778 free(netdev
->ipv4_gateway
);
780 if (strcmp(value
, "auto") == 0) {
781 netdev
->ipv4_gateway
= NULL
;
782 netdev
->ipv4_gateway_auto
= true;
783 } else if (strcmp(value
, "dev") == 0) {
784 netdev
->ipv4_gateway
= NULL
;
785 netdev
->ipv4_gateway_auto
= false;
786 netdev
->ipv4_gateway_dev
= true;
788 __do_free
struct in_addr
*gw
= NULL
;
791 gw
= malloc(sizeof(*gw
));
793 return ret_errno(ENOMEM
);
795 ret
= inet_pton(AF_INET
, value
, gw
);
797 return log_error_errno(-1, errno
, "Invalid ipv4 gateway address \"%s\"", value
);
799 netdev
->ipv4_gateway
= move_ptr(gw
);
800 netdev
->ipv4_gateway_auto
= false;
806 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
807 struct lxc_conf
*lxc_conf
, void *data
)
809 __do_free
char *valdup
= NULL
;
810 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
811 __do_free
struct lxc_list
*list
= NULL
;
813 char *netmask
, *slash
;
814 struct lxc_netdev
*netdev
= data
;
816 if (lxc_config_value_empty(value
))
817 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
820 return ret_errno(EINVAL
);
822 if (netdev
->type
!= LXC_NET_VETH
)
823 return log_error_errno(-EINVAL
,
824 EINVAL
, "Invalid ipv4 route \"%s\", can only be used with veth network",
827 inetdev
= malloc(sizeof(*inetdev
));
829 return ret_errno(ENOMEM
);
830 memset(inetdev
, 0, sizeof(*inetdev
));
832 list
= malloc(sizeof(*list
));
834 return ret_errno(ENOMEM
);
837 list
->elem
= inetdev
;
839 valdup
= strdup(value
);
841 return ret_errno(ENOMEM
);
843 slash
= strchr(valdup
, '/');
845 return ret_errno(EINVAL
);
850 return ret_errno(EINVAL
);
854 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
855 if (ret
< 0 || inetdev
->prefix
> 32)
856 return ret_errno(EINVAL
);
858 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
860 return ret_errno(EINVAL
);
862 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv4_routes
, list
);
869 static int set_config_net_ipv6_address(const char *key
, const char *value
,
870 struct lxc_conf
*lxc_conf
, void *data
)
872 __do_free
char *valdup
= NULL
;
873 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
874 __do_free
struct lxc_list
*list
= NULL
;
876 struct lxc_netdev
*netdev
= data
;
877 char *slash
, *netmask
;
879 if (lxc_config_value_empty(value
))
880 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
883 return ret_errno(EINVAL
);
885 inet6dev
= malloc(sizeof(*inet6dev
));
887 return ret_errno(ENOMEM
);
888 memset(inet6dev
, 0, sizeof(*inet6dev
));
890 list
= malloc(sizeof(*list
));
892 return ret_errno(ENOMEM
);
896 valdup
= strdup(value
);
898 return ret_errno(ENOMEM
);
900 inet6dev
->prefix
= 64;
901 slash
= strstr(valdup
, "/");
906 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
911 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
913 return log_error_errno(-EINVAL
, EINVAL
, "Invalid ipv6 address \"%s\"", valdup
);
915 list
->elem
= inet6dev
;
916 lxc_list_add_tail(&netdev
->ipv6
, list
);
923 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
924 struct lxc_conf
*lxc_conf
, void *data
)
926 struct lxc_netdev
*netdev
= data
;
928 if (lxc_config_value_empty(value
))
929 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
932 return ret_errno(EINVAL
);
934 free(netdev
->ipv6_gateway
);
936 if (strcmp(value
, "auto") == 0) {
937 netdev
->ipv6_gateway
= NULL
;
938 netdev
->ipv6_gateway_auto
= true;
939 } else if (strcmp(value
, "dev") == 0) {
940 netdev
->ipv6_gateway
= NULL
;
941 netdev
->ipv6_gateway_auto
= false;
942 netdev
->ipv6_gateway_dev
= true;
945 __do_free
struct in6_addr
*gw
= NULL
;
947 gw
= malloc(sizeof(*gw
));
949 return ret_errno(ENOMEM
);
951 ret
= inet_pton(AF_INET6
, value
, gw
);
953 return log_error_errno(-EINVAL
, EINVAL
,
954 "Invalid ipv6 gateway address \"%s\"", value
);
956 netdev
->ipv6_gateway
= move_ptr(gw
);
957 netdev
->ipv6_gateway_auto
= false;
963 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
964 struct lxc_conf
*lxc_conf
, void *data
)
966 __do_free
char *valdup
= NULL
;
967 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
968 __do_free
struct lxc_list
*list
= NULL
;
970 char *netmask
, *slash
;
971 struct lxc_netdev
*netdev
= data
;
973 if (lxc_config_value_empty(value
))
974 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
977 return ret_errno(EINVAL
);
979 if (netdev
->type
!= LXC_NET_VETH
)
980 return log_error_errno(-EINVAL
,
981 EINVAL
, "Invalid ipv6 route \"%s\", can only be used with veth network",
984 inet6dev
= malloc(sizeof(*inet6dev
));
986 return ret_errno(ENOMEM
);
987 memset(inet6dev
, 0, sizeof(*inet6dev
));
989 list
= malloc(sizeof(*list
));
991 return ret_errno(ENOMEM
);
995 valdup
= strdup(value
);
999 slash
= strchr(valdup
, '/');
1001 return ret_errno(EINVAL
);
1006 return ret_errno(EINVAL
);
1010 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
1011 if (ret
< 0 || inet6dev
->prefix
> 128)
1012 return ret_errno(EINVAL
);
1014 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
1015 if (!ret
|| ret
< 0)
1016 return ret_errno(EINVAL
);
1018 list
->elem
= inet6dev
;
1019 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv6_routes
, list
);
1026 static int set_config_net_script_up(const char *key
, const char *value
,
1027 struct lxc_conf
*lxc_conf
, void *data
)
1029 struct lxc_netdev
*netdev
= data
;
1031 if (lxc_config_value_empty(value
))
1032 return clr_config_net_script_up(key
, lxc_conf
, data
);
1035 return ret_errno(EINVAL
);
1037 return set_config_string_item(&netdev
->upscript
, value
);
1040 static int set_config_net_script_down(const char *key
, const char *value
,
1041 struct lxc_conf
*lxc_conf
, void *data
)
1043 struct lxc_netdev
*netdev
= data
;
1045 if (lxc_config_value_empty(value
))
1046 return clr_config_net_script_down(key
, lxc_conf
, data
);
1049 return ret_errno(EINVAL
);
1051 return set_config_string_item(&netdev
->downscript
, value
);
1054 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, __owns
char *hook
)
1056 __do_free
char *val
= hook
;
1057 struct lxc_list
*hooklist
;
1059 hooklist
= malloc(sizeof(*hooklist
));
1061 return ret_errno(ENOMEM
);
1063 hooklist
->elem
= move_ptr(val
);
1064 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1069 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1070 struct lxc_conf
*lxc_conf
, void *data
)
1073 if (lxc_config_value_empty(value
))
1074 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1076 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1079 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1080 return ret_set_errno(-1, EINVAL
);
1089 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1090 struct lxc_conf
*lxc_conf
, void *data
)
1092 #ifdef HAVE_SECCOMP_NOTIFY
1093 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1095 return ret_set_errno(-1, ENOSYS
);
1099 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1100 struct lxc_conf
*lxc_conf
, void *data
)
1102 #ifdef HAVE_SECCOMP_NOTIFY
1105 if (lxc_config_value_empty(value
))
1106 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1108 if (strncmp(value
, "unix:", 5) != 0)
1109 return ret_set_errno(-1, EINVAL
);
1112 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1117 return ret_set_errno(-1, ENOSYS
);
1121 static int set_config_seccomp_profile(const char *key
, const char *value
,
1122 struct lxc_conf
*lxc_conf
, void *data
)
1124 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1127 static int set_config_execute_cmd(const char *key
, const char *value
,
1128 struct lxc_conf
*lxc_conf
, void *data
)
1130 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1133 static int set_config_init_cmd(const char *key
, const char *value
,
1134 struct lxc_conf
*lxc_conf
, void *data
)
1136 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1139 static int set_config_init_cwd(const char *key
, const char *value
,
1140 struct lxc_conf
*lxc_conf
, void *data
)
1142 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1145 static int set_config_init_uid(const char *key
, const char *value
,
1146 struct lxc_conf
*lxc_conf
, void *data
)
1148 unsigned int init_uid
;
1150 if (lxc_config_value_empty(value
)) {
1151 lxc_conf
->init_uid
= 0;
1155 if (lxc_safe_uint(value
, &init_uid
) < 0)
1158 lxc_conf
->init_uid
= init_uid
;
1163 static int set_config_init_gid(const char *key
, const char *value
,
1164 struct lxc_conf
*lxc_conf
, void *data
)
1166 unsigned int init_gid
;
1168 if (lxc_config_value_empty(value
)) {
1169 lxc_conf
->init_gid
= 0;
1173 if (lxc_safe_uint(value
, &init_gid
) < 0)
1176 lxc_conf
->init_gid
= init_gid
;
1181 static int set_config_hooks(const char *key
, const char *value
,
1182 struct lxc_conf
*lxc_conf
, void *data
)
1184 __do_free
char *copy
= NULL
;
1186 if (lxc_config_value_empty(value
))
1187 return lxc_clear_hooks(lxc_conf
, key
);
1189 if (strcmp(key
+ 4, "hook") == 0)
1190 return log_error_errno(-EINVAL
, EINVAL
, "lxc.hook must not have a value");
1192 copy
= strdup(value
);
1194 return ret_errno(ENOMEM
);
1196 if (strcmp(key
+ 9, "pre-start") == 0)
1197 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, move_ptr(copy
));
1198 else if (strcmp(key
+ 9, "start-host") == 0)
1199 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, move_ptr(copy
));
1200 else if (strcmp(key
+ 9, "pre-mount") == 0)
1201 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, move_ptr(copy
));
1202 else if (strcmp(key
+ 9, "autodev") == 0)
1203 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, move_ptr(copy
));
1204 else if (strcmp(key
+ 9, "mount") == 0)
1205 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, move_ptr(copy
));
1206 else if (strcmp(key
+ 9, "start") == 0)
1207 return add_hook(lxc_conf
, LXCHOOK_START
, move_ptr(copy
));
1208 else if (strcmp(key
+ 9, "stop") == 0)
1209 return add_hook(lxc_conf
, LXCHOOK_STOP
, move_ptr(copy
));
1210 else if (strcmp(key
+ 9, "post-stop") == 0)
1211 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, move_ptr(copy
));
1212 else if (strcmp(key
+ 9, "clone") == 0)
1213 return add_hook(lxc_conf
, LXCHOOK_CLONE
, move_ptr(copy
));
1214 else if (strcmp(key
+ 9, "destroy") == 0)
1215 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, move_ptr(copy
));
1220 static int set_config_hooks_version(const char *key
, const char *value
,
1221 struct lxc_conf
*lxc_conf
, void *data
)
1226 if (lxc_config_value_empty(value
))
1227 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1229 ret
= lxc_safe_uint(value
, &tmp
);
1234 return log_error_errno(-EINVAL
,
1235 EINVAL
, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
1237 lxc_conf
->hooks_version
= tmp
;
1242 static int set_config_personality(const char *key
, const char *value
,
1243 struct lxc_conf
*lxc_conf
, void *data
)
1245 signed long personality
;
1247 personality
= lxc_config_parse_arch(value
);
1248 if (personality
>= 0)
1249 lxc_conf
->personality
= personality
;
1251 WARN("Unsupported personality \"%s\"", value
);
1256 static int set_config_pty_max(const char *key
, const char *value
,
1257 struct lxc_conf
*lxc_conf
, void *data
)
1260 unsigned int max
= 0;
1262 if (lxc_config_value_empty(value
)) {
1263 lxc_conf
->pty_max
= 0;
1267 ret
= lxc_safe_uint(value
, &max
);
1269 return ret_errno(EINVAL
);
1271 lxc_conf
->pty_max
= max
;
1276 /* We only need to check whether the first byte of the key after the lxc.start.
1277 * prefix matches our expectations since they fortunately all start with a
1278 * different letter. If anything was wrong with the key we would have already
1279 * noticed when the callback was called.
1281 static int set_config_start(const char *key
, const char *value
,
1282 struct lxc_conf
*lxc_conf
, void *data
)
1287 is_empty
= lxc_config_value_empty(value
);
1289 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1291 lxc_conf
->start_auto
= 0;
1295 ret
= lxc_safe_uint(value
, &lxc_conf
->start_auto
);
1299 if (lxc_conf
->start_auto
> 1)
1300 return ret_errno(EINVAL
);
1303 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1305 lxc_conf
->start_delay
= 0;
1309 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1310 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1312 lxc_conf
->start_order
= 0;
1316 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1319 return ret_errno(EINVAL
);
1322 static int set_config_monitor(const char *key
, const char *value
,
1323 struct lxc_conf
*lxc_conf
, void *data
)
1325 if (lxc_config_value_empty(value
)) {
1326 lxc_conf
->monitor_unshare
= 0;
1330 if (strcmp(key
+ 12, "unshare") == 0)
1331 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1333 return ret_errno(EINVAL
);
1336 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1337 struct lxc_conf
*lxc_conf
, void *data
)
1339 if (lxc_config_value_empty(value
)) {
1340 lxc_conf
->monitor_signal_pdeath
= 0;
1344 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1347 sig_n
= sig_parse(value
);
1349 return ret_errno(EINVAL
);
1351 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1355 return ret_errno(EINVAL
);
1358 static int set_config_group(const char *key
, const char *value
,
1359 struct lxc_conf
*lxc_conf
, void *data
)
1361 __do_free
char *groups
= NULL
;
1364 if (lxc_config_value_empty(value
))
1365 return lxc_clear_groups(lxc_conf
);
1367 groups
= strdup(value
);
1369 return ret_errno(ENOMEM
);
1371 /* In case several groups are specified in a single line split these
1372 * groups in a single element for the list.
1374 lxc_iterate_parts(token
, groups
, " \t") {
1375 __do_free
struct lxc_list
*grouplist
= NULL
;
1377 grouplist
= malloc(sizeof(*grouplist
));
1379 return ret_errno(ENOMEM
);
1381 grouplist
->elem
= strdup(token
);
1382 if (!grouplist
->elem
)
1383 return ret_errno(ENOMEM
);
1385 lxc_list_add_tail(&lxc_conf
->groups
, move_ptr(grouplist
));
1391 static int set_config_environment(const char *key
, const char *value
,
1392 struct lxc_conf
*lxc_conf
, void *data
)
1394 __do_free
struct lxc_list
*list_item
= NULL
;
1396 if (lxc_config_value_empty(value
))
1397 return lxc_clear_environment(lxc_conf
);
1399 list_item
= malloc(sizeof(*list_item
));
1401 return ret_errno(ENOMEM
);
1403 if (!strchr(value
, '=')) {
1404 const char *env_val
;
1405 const char *env_key
= value
;
1406 const char *env_var
[3] = {0};
1408 env_val
= getenv(env_key
);
1410 return ret_errno(ENOENT
);
1412 env_var
[0] = env_key
;
1413 env_var
[1] = env_val
;
1414 list_item
->elem
= lxc_string_join("=", env_var
, false);
1416 list_item
->elem
= strdup(value
);
1419 if (!list_item
->elem
)
1420 return ret_errno(ENOMEM
);
1422 lxc_list_add_tail(&lxc_conf
->environment
, move_ptr(list_item
));
1427 static int set_config_tty_max(const char *key
, const char *value
,
1428 struct lxc_conf
*lxc_conf
, void *data
)
1431 unsigned int nbtty
= 0;
1433 if (lxc_config_value_empty(value
)) {
1434 lxc_conf
->ttys
.max
= 0;
1438 ret
= lxc_safe_uint(value
, &nbtty
);
1442 lxc_conf
->ttys
.max
= nbtty
;
1447 static int set_config_tty_dir(const char *key
, const char *value
,
1448 struct lxc_conf
*lxc_conf
, void *data
)
1450 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1454 static int set_config_apparmor_profile(const char *key
, const char *value
,
1455 struct lxc_conf
*lxc_conf
, void *data
)
1457 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1460 static int set_config_apparmor_allow_incomplete(const char *key
,
1462 struct lxc_conf
*lxc_conf
,
1467 if (lxc_config_value_empty(value
)) {
1468 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1472 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
);
1476 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1477 return ret_errno(EINVAL
);
1482 static int set_config_apparmor_allow_nesting(const char *key
,
1484 struct lxc_conf
*lxc_conf
,
1489 if (lxc_config_value_empty(value
))
1490 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1492 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
);
1496 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1497 return ret_errno(EINVAL
);
1502 static int set_config_apparmor_raw(const char *key
,
1504 struct lxc_conf
*lxc_conf
,
1507 __do_free
char *elem
= NULL
;
1508 __do_free
struct lxc_list
*list
= NULL
;
1510 if (lxc_config_value_empty(value
))
1511 return lxc_clear_apparmor_raw(lxc_conf
);
1513 list
= malloc(sizeof(*list
));
1515 return ret_errno(ENOMEM
);
1517 elem
= strdup(value
);
1519 return ret_errno(ENOMEM
);
1521 list
->elem
= move_ptr(elem
);
1522 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, move_ptr(list
));
1527 static int set_config_selinux_context(const char *key
, const char *value
,
1528 struct lxc_conf
*lxc_conf
, void *data
)
1530 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1533 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1534 struct lxc_conf
*lxc_conf
, void *data
)
1536 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1539 static int set_config_keyring_session(const char *key
, const char *value
,
1540 struct lxc_conf
*lxc_conf
, void *data
)
1542 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1545 static int set_config_log_file(const char *key
, const char *value
,
1546 struct lxc_conf
*c
, void *data
)
1550 if (lxc_config_value_empty(value
)) {
1551 free_disarm(c
->logfile
);
1556 * Store these values in the lxc_conf, and then try to set for actual
1559 ret
= set_config_path_item(&c
->logfile
, value
);
1561 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1566 static int set_config_log_level(const char *key
, const char *value
,
1567 struct lxc_conf
*lxc_conf
, void *data
)
1571 if (lxc_config_value_empty(value
)) {
1572 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1576 if (value
[0] >= '0' && value
[0] <= '9') {
1579 ret
= lxc_safe_int(value
, &newlevel
);
1581 return ret_errno(EINVAL
);
1583 newlevel
= lxc_log_priority_to_int(value
);
1587 * Store these values in the lxc_conf, and then try to set for actual
1590 lxc_conf
->loglevel
= newlevel
;
1592 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1595 static int set_config_autodev(const char *key
, const char *value
,
1596 struct lxc_conf
*lxc_conf
, void *data
)
1600 if (lxc_config_value_empty(value
)) {
1601 lxc_conf
->autodev
= 0;
1605 ret
= lxc_safe_uint(value
, &lxc_conf
->autodev
);
1607 return ret_errno(EINVAL
);
1609 if (lxc_conf
->autodev
> 1)
1610 return ret_errno(EINVAL
);
1615 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1616 struct lxc_conf
*lxc_conf
, void *data
)
1618 if (lxc_config_value_empty(value
)) {
1619 lxc_conf
->autodevtmpfssize
= 500000;
1623 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1624 lxc_conf
->autodevtmpfssize
= 500000;
1629 static int set_config_signal_halt(const char *key
, const char *value
,
1630 struct lxc_conf
*lxc_conf
, void *data
)
1634 if (lxc_config_value_empty(value
)) {
1635 lxc_conf
->haltsignal
= 0;
1639 sig_n
= sig_parse(value
);
1641 return ret_errno(EINVAL
);
1643 lxc_conf
->haltsignal
= sig_n
;
1648 static int set_config_signal_reboot(const char *key
, const char *value
,
1649 struct lxc_conf
*lxc_conf
, void *data
)
1653 if (lxc_config_value_empty(value
)) {
1654 lxc_conf
->rebootsignal
= 0;
1658 sig_n
= sig_parse(value
);
1660 return ret_errno(EINVAL
);
1662 lxc_conf
->rebootsignal
= sig_n
;
1667 static int set_config_signal_stop(const char *key
, const char *value
,
1668 struct lxc_conf
*lxc_conf
, void *data
)
1672 if (lxc_config_value_empty(value
)) {
1673 lxc_conf
->stopsignal
= 0;
1677 sig_n
= sig_parse(value
);
1679 return ret_errno(EINVAL
);
1681 lxc_conf
->stopsignal
= sig_n
;
1686 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1687 struct lxc_conf
*lxc_conf
, int version
)
1689 __do_free
struct lxc_list
*cglist
= NULL
;
1690 call_cleaner(free_lxc_cgroup
) struct lxc_cgroup
*cgelem
= NULL
;
1691 const char *subkey
, *token
;
1694 if (lxc_config_value_empty(value
))
1695 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1697 if (version
== CGROUP2_SUPER_MAGIC
) {
1698 token
= "lxc.cgroup2.";
1700 } else if (version
== CGROUP_SUPER_MAGIC
) {
1701 token
= "lxc.cgroup.";
1704 return ret_errno(EINVAL
);
1707 if (strncmp(key
, token
, token_len
) != 0)
1708 return ret_errno(EINVAL
);
1710 subkey
= key
+ token_len
;
1711 if (*subkey
== '\0')
1712 return ret_errno(EINVAL
);
1714 cglist
= malloc(sizeof(*cglist
));
1716 return ret_errno(ENOMEM
);
1718 cgelem
= malloc(sizeof(*cgelem
));
1720 return ret_errno(ENOMEM
);
1721 memset(cgelem
, 0, sizeof(*cgelem
));
1723 cgelem
->subsystem
= strdup(subkey
);
1724 if (!cgelem
->subsystem
)
1725 return ret_errno(ENOMEM
);
1727 cgelem
->value
= strdup(value
);
1729 return ret_errno(ENOMEM
);
1731 cgelem
->version
= version
;
1733 lxc_list_add_elem(cglist
, move_ptr(cgelem
));
1735 if (version
== CGROUP2_SUPER_MAGIC
)
1736 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1738 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1744 static int set_config_cgroup_controller(const char *key
, const char *value
,
1745 struct lxc_conf
*lxc_conf
, void *data
)
1747 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1748 CGROUP_SUPER_MAGIC
);
1751 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1752 struct lxc_conf
*lxc_conf
, void *data
)
1754 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1755 CGROUP2_SUPER_MAGIC
);
1759 static int set_config_cgroup_dir(const char *key
, const char *value
,
1760 struct lxc_conf
*lxc_conf
, void *data
)
1762 if (lxc_config_value_empty(value
))
1763 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1765 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1768 static int set_config_cgroup_monitor_dir(const char *key
, const char *value
,
1769 struct lxc_conf
*lxc_conf
, void *data
)
1771 if (lxc_config_value_empty(value
))
1772 return clr_config_cgroup_monitor_dir(key
, lxc_conf
, NULL
);
1774 return set_config_string_item(&lxc_conf
->cgroup_meta
.monitor_dir
,
1778 static int set_config_cgroup_monitor_pivot_dir(const char *key
, const char *value
,
1779 struct lxc_conf
*lxc_conf
, void *data
)
1781 if (lxc_config_value_empty(value
))
1782 return clr_config_cgroup_monitor_pivot_dir(key
, lxc_conf
, NULL
);
1784 return set_config_string_item(&lxc_conf
->cgroup_meta
.monitor_pivot_dir
,
1788 static int set_config_cgroup_container_dir(const char *key
, const char *value
,
1789 struct lxc_conf
*lxc_conf
,
1792 if (lxc_config_value_empty(value
))
1793 return clr_config_cgroup_container_dir(key
, lxc_conf
, NULL
);
1795 return set_config_string_item(&lxc_conf
->cgroup_meta
.container_dir
,
1799 static int set_config_cgroup_container_inner_dir(const char *key
,
1801 struct lxc_conf
*lxc_conf
,
1804 if (lxc_config_value_empty(value
))
1805 return clr_config_cgroup_container_inner_dir(key
, lxc_conf
, NULL
);
1807 if (strchr(value
, '/') ||
1808 strcmp(value
, ".") == 0 ||
1809 strcmp(value
, "..") == 0)
1810 return log_error_errno(-EINVAL
, EINVAL
, "lxc.cgroup.dir.container.inner must be a single directory name");
1812 return set_config_string_item(&lxc_conf
->cgroup_meta
.namespace_dir
, value
);
1815 static int set_config_cgroup_relative(const char *key
, const char *value
,
1816 struct lxc_conf
*lxc_conf
, void *data
)
1818 unsigned int converted
;
1821 if (lxc_config_value_empty(value
))
1822 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1824 ret
= lxc_safe_uint(value
, &converted
);
1828 if (converted
== 1) {
1829 lxc_conf
->cgroup_meta
.relative
= true;
1833 if (converted
== 0) {
1834 lxc_conf
->cgroup_meta
.relative
= false;
1838 return ret_errno(EINVAL
);
1841 static bool parse_limit_value(const char **value
, rlim_t
*res
)
1843 char *endptr
= NULL
;
1845 if (strncmp(*value
, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1846 *res
= RLIM_INFINITY
;
1847 *value
+= STRLITERALLEN("unlimited");
1852 *res
= strtoull(*value
, &endptr
, 10);
1853 if (errno
|| !endptr
)
1861 static int set_config_prlimit(const char *key
, const char *value
,
1862 struct lxc_conf
*lxc_conf
, void *data
)
1864 __do_free
struct lxc_list
*limlist
= NULL
;
1865 call_cleaner(free_lxc_limit
) struct lxc_limit
*limelem
= NULL
;
1866 struct lxc_list
*iter
;
1867 struct rlimit limit
;
1870 if (lxc_config_value_empty(value
))
1871 return lxc_clear_limits(lxc_conf
, key
);
1873 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1874 return ret_errno(EINVAL
);
1876 key
+= STRLITERALLEN("lxc.prlimit.");
1878 /* soft limit comes first in the value */
1879 if (!parse_limit_value(&value
, &limit_value
))
1880 return ret_errno(EINVAL
);
1882 limit
.rlim_cur
= limit_value
;
1884 /* skip spaces and a colon */
1885 while (isspace(*value
))
1890 else if (*value
) /* any other character is an error here */
1891 return ret_errno(EINVAL
);
1893 while (isspace(*value
))
1896 /* optional hard limit */
1898 if (!parse_limit_value(&value
, &limit_value
))
1899 return ret_errno(EINVAL
);
1901 limit
.rlim_max
= limit_value
;
1903 /* check for trailing garbage */
1904 while (isspace(*value
))
1908 return ret_errno(EINVAL
);
1910 /* a single value sets both hard and soft limit */
1911 limit
.rlim_max
= limit
.rlim_cur
;
1914 /* find existing list element */
1915 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1916 limelem
= iter
->elem
;
1917 if (!strcmp(key
, limelem
->resource
)) {
1918 limelem
->limit
= limit
;
1923 /* allocate list element */
1924 limlist
= malloc(sizeof(*limlist
));
1926 return ret_errno(ENOMEM
);
1928 limelem
= malloc(sizeof(*limelem
));
1930 return ret_errno(ENOMEM
);
1931 memset(limelem
, 0, sizeof(*limelem
));
1933 limelem
->resource
= strdup(key
);
1934 if (!limelem
->resource
)
1935 return ret_errno(ENOMEM
);
1937 limelem
->limit
= limit
;
1938 lxc_list_add_elem(limlist
, move_ptr(limelem
));;
1939 lxc_list_add_tail(&lxc_conf
->limits
, move_ptr(limlist
));
1944 static int set_config_sysctl(const char *key
, const char *value
,
1945 struct lxc_conf
*lxc_conf
, void *data
)
1947 __do_free
struct lxc_list
*sysctl_list
= NULL
;
1948 call_cleaner(free_lxc_sysctl
) struct lxc_sysctl
*sysctl_elem
= NULL
;
1949 struct lxc_list
*iter
;
1951 if (lxc_config_value_empty(value
))
1952 return clr_config_sysctl(key
, lxc_conf
, NULL
);
1954 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
1957 key
+= STRLITERALLEN("lxc.sysctl.");
1959 /* find existing list element */
1960 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
1961 __do_free
char *replace_value
= NULL
;
1963 sysctl_elem
= iter
->elem
;
1965 if (strcmp(key
, sysctl_elem
->key
) != 0)
1968 replace_value
= strdup(value
);
1970 return ret_errno(EINVAL
);
1972 free(sysctl_elem
->value
);
1973 sysctl_elem
->value
= move_ptr(replace_value
);
1978 /* allocate list element */
1979 sysctl_list
= malloc(sizeof(*sysctl_list
));
1981 return ret_errno(ENOMEM
);
1983 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
1985 return ret_errno(ENOMEM
);
1986 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
1988 sysctl_elem
->key
= strdup(key
);
1989 if (!sysctl_elem
->key
)
1990 return ret_errno(ENOMEM
);
1992 sysctl_elem
->value
= strdup(value
);
1993 if (!sysctl_elem
->value
)
1994 return ret_errno(ENOMEM
);
1996 lxc_list_add_elem(sysctl_list
, move_ptr(sysctl_elem
));
1997 lxc_list_add_tail(&lxc_conf
->sysctls
, move_ptr(sysctl_list
));
2002 static int set_config_proc(const char *key
, const char *value
,
2003 struct lxc_conf
*lxc_conf
, void *data
)
2005 __do_free
struct lxc_list
*proclist
= NULL
;
2006 call_cleaner(free_lxc_proc
) struct lxc_proc
*procelem
= NULL
;
2009 if (lxc_config_value_empty(value
))
2010 return clr_config_proc(key
, lxc_conf
, NULL
);
2012 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
2015 subkey
= key
+ STRLITERALLEN("lxc.proc.");
2016 if (*subkey
== '\0')
2017 return ret_errno(EINVAL
);
2019 proclist
= malloc(sizeof(*proclist
));
2021 return ret_errno(ENOMEM
);
2023 procelem
= malloc(sizeof(*procelem
));
2025 return ret_errno(ENOMEM
);
2026 memset(procelem
, 0, sizeof(*procelem
));
2028 procelem
->filename
= strdup(subkey
);
2029 if (!procelem
->filename
)
2030 return ret_errno(ENOMEM
);
2032 procelem
->value
= strdup(value
);
2033 if (!procelem
->value
)
2034 return ret_errno(ENOMEM
);
2036 proclist
->elem
= move_ptr(procelem
);
2037 lxc_list_add_tail(&lxc_conf
->procs
, move_ptr(proclist
));
2042 static int set_config_idmaps(const char *key
, const char *value
,
2043 struct lxc_conf
*lxc_conf
, void *data
)
2045 __do_free
struct lxc_list
*idmaplist
= NULL
;
2046 __do_free
struct id_map
*idmap
= NULL
;
2047 unsigned long hostid
, nsid
, range
;
2051 if (lxc_config_value_empty(value
))
2052 return lxc_clear_idmaps(lxc_conf
);
2054 idmaplist
= malloc(sizeof(*idmaplist
));
2056 return ret_errno(ENOMEM
);
2058 idmap
= malloc(sizeof(*idmap
));
2060 return ret_errno(ENOMEM
);
2061 memset(idmap
, 0, sizeof(*idmap
));
2063 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2065 return log_error_errno(-EINVAL
, EINVAL
, "Failed to parse id mappings");
2067 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2069 idmap
->idtype
= ID_TYPE_UID
;
2070 else if (type
== 'g')
2071 idmap
->idtype
= ID_TYPE_GID
;
2073 return ret_errno(EINVAL
);
2075 idmap
->hostid
= hostid
;
2077 idmap
->range
= range
;
2078 idmaplist
->elem
= idmap
;
2079 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
2081 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2082 if (idmap
->nsid
== 0)
2083 lxc_conf
->root_nsuid_map
= idmap
;
2085 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2086 if (idmap
->nsid
== 0)
2087 lxc_conf
->root_nsgid_map
= idmap
;
2090 move_ptr(idmaplist
);
2095 static int set_config_mount_fstab(const char *key
, const char *value
,
2096 struct lxc_conf
*lxc_conf
, void *data
)
2098 if (lxc_config_value_empty(value
)) {
2099 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2100 return ret_errno(EINVAL
);
2103 return set_config_path_item(&lxc_conf
->fstab
, value
);
2106 static int set_config_mount_auto(const char *key
, const char *value
,
2107 struct lxc_conf
*lxc_conf
, void *data
)
2109 __do_free
char *autos
= NULL
;
2116 } allowed_auto_mounts
[] = {
2117 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2118 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2119 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2120 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2121 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2122 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2123 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2124 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2125 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2126 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2127 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2128 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2129 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2130 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2131 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2132 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2133 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2134 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2135 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2136 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2137 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2138 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2139 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2140 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2142 * For adding anything that is just a single on/off, but has no
2143 * options: keep mask and flag identical and just define the
2144 * enum value as an unused bit so far
2149 if (lxc_config_value_empty(value
)) {
2150 lxc_conf
->auto_mounts
= 0;
2154 autos
= strdup(value
);
2156 return ret_errno(ENOMEM
);
2158 lxc_iterate_parts(token
, autos
, " \t") {
2159 bool is_shmounts
= false;
2161 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2162 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
2165 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
2166 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
2172 if (!allowed_auto_mounts
[i
].token
)
2173 return log_error_errno(-EINVAL
, EINVAL
, "Invalid filesystem to automount \"%s\"", token
);
2175 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2176 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2179 __do_free
char *container_path
= NULL
, *host_path
= NULL
;
2182 val
= token
+ STRLITERALLEN("shmounts:");
2184 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2186 host_path
= strdup(val
);
2188 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2190 val
= strchr(host_path
, ':');
2191 if (!val
|| *(val
+ 1) == '\0')
2192 val
= "/dev/.lxc-mounts";
2196 container_path
= strdup(val
);
2198 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts container path");
2200 lxc_conf
->shmount
.path_host
= move_ptr(host_path
);
2201 lxc_conf
->shmount
.path_cont
= move_ptr(container_path
);
2208 static int set_config_mount(const char *key
, const char *value
,
2209 struct lxc_conf
*lxc_conf
, void *data
)
2211 __do_free
char *mntelem
= NULL
;
2212 __do_free
struct lxc_list
*mntlist
= NULL
;
2214 if (lxc_config_value_empty(value
))
2215 return lxc_clear_mount_entries(lxc_conf
);
2217 mntlist
= malloc(sizeof(*mntlist
));
2219 return ret_errno(ENOMEM
);
2221 mntelem
= strdup(value
);
2223 return ret_errno(ENOMEM
);
2225 mntlist
->elem
= move_ptr(mntelem
);
2226 lxc_list_add_tail(&lxc_conf
->mount_list
, move_ptr(mntlist
));
2231 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2232 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2235 static int set_config_cap_keep(const char *key
, const char *value
,
2236 struct lxc_conf
*lxc_conf
, void *data
)
2238 char *keepcaps
, *token
;
2239 struct lxc_list
*keeplist
;
2242 if (lxc_config_value_empty(value
))
2243 return lxc_clear_config_keepcaps(lxc_conf
);
2245 keepcaps
= strdup(value
);
2249 /* In case several capability keep is specified in a single line
2250 * split these caps in a single element for the list.
2252 lxc_iterate_parts(token
, keepcaps
, " \t") {
2253 if (!strcmp(token
, "none"))
2254 lxc_clear_config_keepcaps(lxc_conf
);
2256 keeplist
= malloc(sizeof(*keeplist
));
2260 keeplist
->elem
= strdup(token
);
2261 if (!keeplist
->elem
) {
2266 lxc_list_add_tail(&lxc_conf
->keepcaps
, keeplist
);
2277 static int set_config_cap_drop(const char *key
, const char *value
,
2278 struct lxc_conf
*lxc_conf
, void *data
)
2280 char *dropcaps
, *token
;
2281 struct lxc_list
*droplist
;
2284 if (lxc_config_value_empty(value
))
2285 return lxc_clear_config_caps(lxc_conf
);
2287 dropcaps
= strdup(value
);
2291 /* In case several capability drop is specified in a single line
2292 * split these caps in a single element for the list.
2294 lxc_iterate_parts(token
, dropcaps
, " \t") {
2295 droplist
= malloc(sizeof(*droplist
));
2299 droplist
->elem
= strdup(token
);
2300 if (!droplist
->elem
) {
2305 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
2316 static int set_config_console_path(const char *key
, const char *value
,
2317 struct lxc_conf
*lxc_conf
, void *data
)
2319 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2322 static int set_config_console_rotate(const char *key
, const char *value
,
2323 struct lxc_conf
*lxc_conf
, void *data
)
2325 if (lxc_config_value_empty(value
)) {
2326 lxc_conf
->console
.log_rotate
= 0;
2330 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
2333 if (lxc_conf
->console
.log_rotate
> 1) {
2334 ERROR("The \"lxc.console.rotate\" config key can only be set "
2342 static int set_config_console_logfile(const char *key
, const char *value
,
2343 struct lxc_conf
*lxc_conf
, void *data
)
2345 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2348 static int set_config_console_buffer_size(const char *key
, const char *value
,
2349 struct lxc_conf
*lxc_conf
, void *data
)
2353 uint64_t buffer_size
, pgsz
;
2355 if (lxc_config_value_empty(value
)) {
2356 lxc_conf
->console
.buffer_size
= 0;
2360 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2361 if (!strcmp(value
, "auto")) {
2362 lxc_conf
->console
.buffer_size
= 1 << 17;
2366 ret
= parse_byte_size_string(value
, &size
);
2373 /* must be at least a page size */
2374 pgsz
= lxc_getpagesize();
2375 if ((uint64_t)size
< pgsz
) {
2376 NOTICE("Requested ringbuffer size for the console is %" PRId64
2377 " but must be at least %" PRId64
2378 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2383 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2384 if (buffer_size
== 0)
2387 if (buffer_size
!= size
)
2388 NOTICE("Passed size was not a power of 2. Rounding log size to "
2389 "next power of two: %" PRIu64
" bytes", buffer_size
);
2391 lxc_conf
->console
.buffer_size
= buffer_size
;
2396 static int set_config_console_size(const char *key
, const char *value
,
2397 struct lxc_conf
*lxc_conf
, void *data
)
2401 uint64_t log_size
, pgsz
;
2403 if (lxc_config_value_empty(value
)) {
2404 lxc_conf
->console
.log_size
= 0;
2408 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2409 if (!strcmp(value
, "auto")) {
2410 lxc_conf
->console
.log_size
= 1 << 17;
2414 ret
= parse_byte_size_string(value
, &size
);
2421 /* must be at least a page size */
2422 pgsz
= lxc_getpagesize();
2423 if ((uint64_t)size
< pgsz
) {
2424 NOTICE("Requested ringbuffer size for the console is %" PRId64
2425 " but must be at least %" PRId64
2426 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2431 log_size
= lxc_find_next_power2((uint64_t)size
);
2435 if (log_size
!= size
)
2436 NOTICE("Passed size was not a power of 2. Rounding log size to "
2437 "next power of two: %" PRIu64
" bytes", log_size
);
2439 lxc_conf
->console
.log_size
= log_size
;
2445 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2446 * file, we expand it in the unexpanded_config, so that after a save_config we
2447 * store the hwaddr for re-use.
2448 * This is only called when reading the config file, not when executing a
2450 * 'x' and 'X' are substituted in-place.
2452 static void update_hwaddr(const char *line
)
2456 line
+= lxc_char_left_gc(line
, strlen(line
));
2460 if (!lxc_config_net_is_hwaddr(line
))
2463 /* Let config_net_hwaddr raise the error. */
2464 p
= strchr(line
, '=');
2475 rand_complete_hwaddr(p
);
2478 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2481 size_t len
= conf
->unexpanded_len
;
2483 update_hwaddr(line
);
2485 linelen
= strlen(line
);
2486 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2487 char *tmp
= realloc(conf
->unexpanded_config
,
2488 conf
->unexpanded_alloced
+ 1024);
2492 if (!conf
->unexpanded_config
)
2495 conf
->unexpanded_config
= tmp
;
2496 conf
->unexpanded_alloced
+= 1024;
2499 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2500 conf
->unexpanded_len
+= linelen
;
2501 if (line
[linelen
- 1] != '\n')
2502 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2503 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2508 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2510 __do_closedir
DIR *dir
= NULL
;
2511 struct dirent
*direntp
;
2512 char path
[PATH_MAX
];
2516 dir
= opendir(dirp
);
2520 while ((direntp
= readdir(dir
))) {
2523 fnam
= direntp
->d_name
;
2524 if (!strcmp(fnam
, "."))
2527 if (!strcmp(fnam
, ".."))
2531 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2534 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2535 if (len
< 0 || len
>= PATH_MAX
)
2538 ret
= lxc_config_read(path
, lxc_conf
, true);
2546 static int set_config_includefiles(const char *key
, const char *value
,
2547 struct lxc_conf
*lxc_conf
, void *data
)
2549 if (lxc_config_value_empty(value
)) {
2550 clr_config_includefiles(key
, lxc_conf
, NULL
);
2555 return do_includedir(value
, lxc_conf
);
2557 return lxc_config_read(value
, lxc_conf
, true);
2560 static int set_config_rootfs_path(const char *key
, const char *value
,
2561 struct lxc_conf
*lxc_conf
, void *data
)
2565 const char *container_path
;
2567 if (lxc_config_value_empty(value
)) {
2568 free(lxc_conf
->rootfs
.path
);
2569 lxc_conf
->rootfs
.path
= NULL
;
2573 dup
= strdup(value
);
2577 /* Split <storage type>:<container path> into <storage type> and
2578 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2579 * "rootfs.path" to <container path>.
2581 tmp
= strchr(dup
, ':');
2585 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2592 container_path
= tmp
;
2594 container_path
= value
;
2597 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2603 static int set_config_rootfs_managed(const char *key
, const char *value
,
2604 struct lxc_conf
*lxc_conf
, void *data
)
2606 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2609 static int set_config_rootfs_mount(const char *key
, const char *value
,
2610 struct lxc_conf
*lxc_conf
, void *data
)
2612 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2615 static int set_config_rootfs_options(const char *key
, const char *value
,
2616 struct lxc_conf
*lxc_conf
, void *data
)
2618 unsigned long mflags
= 0, pflags
= 0;
2619 char *mdata
= NULL
, *opts
= NULL
;
2621 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2623 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2627 ret
= parse_propagationopts(value
, &pflags
);
2633 ret
= set_config_string_item(&opts
, value
);
2639 rootfs
->mountflags
= mflags
| pflags
;
2640 rootfs
->options
= opts
;
2641 rootfs
->data
= mdata
;
2646 static int set_config_uts_name(const char *key
, const char *value
,
2647 struct lxc_conf
*lxc_conf
, void *data
)
2649 struct utsname
*utsname
;
2651 if (lxc_config_value_empty(value
)) {
2652 clr_config_uts_name(key
, lxc_conf
, NULL
);
2656 utsname
= malloc(sizeof(*utsname
));
2660 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2665 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2666 free(lxc_conf
->utsname
);
2667 lxc_conf
->utsname
= utsname
;
2672 static int set_config_namespace_clone(const char *key
, const char *value
,
2673 struct lxc_conf
*lxc_conf
, void *data
)
2678 if (lxc_config_value_empty(value
))
2679 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2681 if (lxc_conf
->ns_keep
!= 0) {
2683 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2684 "\"lxc.namespace.keep\"");
2692 lxc_iterate_parts(token
, ns
, " \t") {
2693 token
+= lxc_char_left_gc(token
, strlen(token
));
2694 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2695 cloneflag
= lxc_namespace_2_cloneflag(token
);
2696 if (cloneflag
< 0) {
2700 lxc_conf
->ns_clone
|= cloneflag
;
2707 static int set_config_namespace_keep(const char *key
, const char *value
,
2708 struct lxc_conf
*lxc_conf
, void *data
)
2713 if (lxc_config_value_empty(value
))
2714 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2716 if (lxc_conf
->ns_clone
!= 0) {
2718 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2719 "\"lxc.namespace.keep\"");
2727 lxc_iterate_parts(token
, ns
, " \t") {
2728 token
+= lxc_char_left_gc(token
, strlen(token
));
2729 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2730 cloneflag
= lxc_namespace_2_cloneflag(token
);
2731 if (cloneflag
< 0) {
2735 lxc_conf
->ns_keep
|= cloneflag
;
2742 static int set_config_time_offset_boot(const char *key
, const char *value
,
2743 struct lxc_conf
*lxc_conf
, void *data
)
2748 char buf
[STRLITERALLEN("ms") + 1];
2750 if (lxc_config_value_empty(value
))
2751 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2753 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2757 unit
= lxc_trim_whitespace_in_place(buf
);
2758 if (strcmp(unit
, "h") == 0) {
2759 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2761 } else if (strcmp(unit
, "m") == 0) {
2762 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2764 } else if (strcmp(unit
, "s") == 0) {
2765 lxc_conf
->timens
.s_boot
= offset
;
2766 } else if (strcmp(unit
, "ms") == 0) {
2767 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2769 } else if (strcmp(unit
, "us") == 0) {
2770 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2772 } else if (strcmp(unit
, "ns") == 0) {
2773 lxc_conf
->timens
.ns_boot
= offset
;
2775 return ret_errno(EINVAL
);
2781 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2782 struct lxc_conf
*lxc_conf
, void *data
)
2787 char buf
[STRLITERALLEN("ms") + 1];
2789 if (lxc_config_value_empty(value
))
2790 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2792 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2796 unit
= lxc_trim_whitespace_in_place(buf
);
2797 if (strcmp(unit
, "h") == 0) {
2798 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2800 } else if (strcmp(unit
, "m") == 0) {
2801 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2803 } else if (strcmp(unit
, "s") == 0) {
2804 lxc_conf
->timens
.s_monotonic
= offset
;
2805 } else if (strcmp(unit
, "ms") == 0) {
2806 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2808 } else if (strcmp(unit
, "us") == 0) {
2809 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2811 } else if (strcmp(unit
, "ns") == 0) {
2812 lxc_conf
->timens
.ns_monotonic
= offset
;
2814 return ret_errno(EINVAL
);
2820 static int set_config_namespace_share(const char *key
, const char *value
,
2821 struct lxc_conf
*lxc_conf
, void *data
)
2824 const char *namespace;
2826 if (lxc_config_value_empty(value
))
2827 return clr_config_namespace_share(key
, lxc_conf
, data
);
2829 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2830 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2834 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2837 struct parse_line_conf
{
2838 struct lxc_conf
*conf
;
2842 static int parse_line(char *buffer
, void *data
)
2844 char *dot
, *key
, *line
, *linep
, *value
;
2846 struct lxc_config_t
*config
;
2849 struct parse_line_conf
*plc
= data
;
2851 /* If there are newlines in the config file we should keep them. */
2852 empty_line
= lxc_is_line_empty(dup
);
2856 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2857 * modified the original string on the stack by replacing '=' by '\0'
2860 linep
= line
= strdup(dup
);
2864 if (!plc
->from_include
) {
2865 ret
= append_unexp_config_line(line
, plc
->conf
);
2873 line
+= lxc_char_left_gc(line
, strlen(line
));
2875 /* ignore comments */
2879 /* martian option - don't add it to the config itself */
2880 if (strncmp(line
, "lxc.", 4))
2885 dot
= strchr(line
, '=');
2887 ERROR("Invalid configuration line: %s", line
);
2895 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2897 value
+= lxc_char_left_gc(value
, strlen(value
));
2898 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2900 if (*value
== '\'' || *value
== '\"') {
2903 len
= strlen(value
);
2904 if (len
> 1 && value
[len
- 1] == *value
) {
2905 value
[len
- 1] = '\0';
2910 config
= lxc_get_config(key
);
2912 ERROR("Unknown configuration key \"%s\"", key
);
2916 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2924 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2926 char *dot
, *key
, *line
, *linep
, *value
;
2929 struct new_config_item
*new = NULL
;
2931 linep
= line
= strdup(dup
);
2935 line
+= lxc_char_left_gc(line
, strlen(line
));
2937 /* martian option - don't add it to the config itself */
2938 if (strncmp(line
, "lxc.", 4))
2942 dot
= strchr(line
, '=');
2944 ERROR("Invalid configuration item: %s", line
);
2952 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2954 value
+= lxc_char_left_gc(value
, strlen(value
));
2955 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2957 if (*value
== '\'' || *value
== '\"') {
2960 len
= strlen(value
);
2961 if (len
> 1 && value
[len
- 1] == *value
) {
2962 value
[len
- 1] = '\0';
2968 new = malloc(sizeof(struct new_config_item
));
2972 new->key
= strdup(key
);
2973 new->val
= strdup(value
);
2974 if (!new->val
|| !new->key
)
2982 if (ret
< 0 && new) {
2992 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2994 struct parse_line_conf c
;
2997 c
.from_include
= from_include
;
2999 /* Catch only the top level config file name in the structure. */
3001 conf
->rcfile
= strdup(file
);
3003 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
3006 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3008 struct lxc_list
*dent
;
3010 dent
= malloc(sizeof(struct lxc_list
));
3014 dent
->elem
= parse_new_conf_line(arg
);
3020 lxc_list_add_tail(defines
, dent
);
3025 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3027 struct lxc_list
*it
;
3030 lxc_list_for_each(it
, defines
) {
3031 struct new_config_item
*new_item
= it
->elem
;
3032 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3037 lxc_config_define_free(defines
);
3042 void lxc_config_define_free(struct lxc_list
*defines
)
3044 struct lxc_list
*it
, *next
;
3046 lxc_list_for_each_safe(it
, defines
, next
) {
3047 struct new_config_item
*new_item
= it
->elem
;
3048 free(new_item
->key
);
3049 free(new_item
->val
);
3055 signed long lxc_config_parse_arch(const char *arch
)
3057 #if HAVE_SYS_PERSONALITY_H
3063 { "arm", PER_LINUX32
},
3064 { "armel", PER_LINUX32
},
3065 { "armhf", PER_LINUX32
},
3066 { "armv7l", PER_LINUX32
},
3067 { "athlon", PER_LINUX32
},
3068 { "i386", PER_LINUX32
},
3069 { "i486", PER_LINUX32
},
3070 { "i586", PER_LINUX32
},
3071 { "i686", PER_LINUX32
},
3072 { "linux32", PER_LINUX32
},
3073 { "mips", PER_LINUX32
},
3074 { "mipsel", PER_LINUX32
},
3075 { "ppc", PER_LINUX32
},
3076 { "powerpc", PER_LINUX32
},
3077 { "x86", PER_LINUX32
},
3078 { "amd64", PER_LINUX
},
3079 { "arm64", PER_LINUX
},
3080 { "linux64", PER_LINUX
},
3081 { "mips64", PER_LINUX
},
3082 { "mips64el", PER_LINUX
},
3083 { "ppc64", PER_LINUX
},
3084 { "ppc64el", PER_LINUX
},
3085 { "ppc64le", PER_LINUX
},
3086 { "powerpc64", PER_LINUX
},
3087 { "s390x", PER_LINUX
},
3088 { "x86_64", PER_LINUX
},
3090 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
3092 for (i
= 0; i
< len
; i
++)
3093 if (!strcmp(pername
[i
].name
, arch
))
3094 return pername
[i
].per
;
3100 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
3108 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3109 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3110 { "LSM", LXC_ATTACH_LSM_EXEC
},
3115 /* For the sake of backward compatibility, drop all privileges
3116 * if none is specified.
3118 for (i
= 0; all_privs
[i
].token
; i
++)
3119 *flags
|= all_privs
[i
].flag
;
3124 lxc_iterate_parts(token
, flaglist
, "|") {
3127 for (i
= 0; all_privs
[i
].token
; i
++)
3128 if (!strcmp(all_privs
[i
].token
, token
))
3129 aflag
= all_privs
[i
].flag
;
3140 /* Write out a configuration file. */
3141 int write_config(int fd
, const struct lxc_conf
*conf
)
3144 size_t len
= conf
->unexpanded_len
;
3149 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3151 SYSERROR("Failed to write configuration file");
3158 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3161 __do_free
char *tmp
= NULL
;
3165 len
= strlen(key
) + strlen(v
) + 4;
3166 tmp
= must_realloc(NULL
, len
);
3168 if (lxc_config_value_empty(v
))
3169 ret
= snprintf(tmp
, len
, "%s =", key
);
3171 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
3172 if (ret
< 0 || ret
>= len
)
3175 /* Save the line verbatim into unexpanded_conf */
3176 if (append_unexp_config_line(tmp
, conf
))
3182 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3186 char *lstart
= conf
->unexpanded_config
;
3188 if (!conf
->unexpanded_config
)
3192 lend
= strchr(lstart
, '\n');
3196 lend
= lstart
+ strlen(lstart
);
3200 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
3206 v
= lstart
[strlen(key
)];
3207 if (!isspace(v
) && v
!= '=') {
3213 conf
->unexpanded_len
-= (lend
- lstart
);
3215 if (*lend
== '\0') {
3220 memmove(lstart
, lend
, strlen(lend
) + 1);
3224 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3225 const char *newpath
, const char *oldname
,
3226 const char *newname
, const char *ovldir
)
3228 __do_free
char *newdir
= NULL
,
3232 size_t newdirlen
, olddirlen
;
3233 char *lstart
= conf
->unexpanded_config
;
3234 const char *key
= "lxc.mount.entry";
3236 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3237 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3238 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
3240 if (ret
< 0 || ret
>= olddirlen
+ 1)
3243 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3244 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3245 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
3247 if (ret
< 0 || ret
>= newdirlen
+ 1)
3250 if (!conf
->unexpanded_config
)
3254 lend
= strchr(lstart
, '\n');
3256 lend
= lstart
+ strlen(lstart
);
3260 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3263 p
= strchr(lstart
+ strlen(key
), '=');
3274 /* Whenever a lxc.mount.entry entry is found in a line we check
3275 * if the substring "overlay" is present before doing any
3276 * further work. We check for "overlay" because substrings need
3277 * to have at least one space before them in a valid overlay
3278 * lxc.mount.entry (/A B overlay). When the space before is
3279 * missing it is very likely that these substrings are part of a
3280 * path or something else. (Checking q >= lend ensures that we
3281 * only count matches in the current line.) */
3282 q
= strstr(p
, " overlay");
3283 if (!q
|| q
>= lend
)
3286 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3289 /* replace the olddir with newdir */
3290 if (olddirlen
>= newdirlen
) {
3291 size_t diff
= olddirlen
- newdirlen
;
3292 memcpy(q
, newdir
, newdirlen
);
3294 if (olddirlen
!= newdirlen
) {
3295 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3296 strlen(q
) - newdirlen
- diff
+ 1);
3298 conf
->unexpanded_len
-= diff
;
3302 size_t diff
= newdirlen
- olddirlen
;
3303 size_t oldlen
= conf
->unexpanded_len
;
3304 size_t newlen
= oldlen
+ diff
;
3305 size_t poffset
= q
- conf
->unexpanded_config
;
3307 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3311 conf
->unexpanded_len
= newlen
;
3312 conf
->unexpanded_alloced
= newlen
+ 1;
3313 new[newlen
- 1] = '\0';
3314 lend
= new + (lend
- conf
->unexpanded_config
);
3316 /* Move over the remainder to make room for the newdir.
3318 memmove(new + poffset
+ newdirlen
,
3319 new + poffset
+ olddirlen
,
3320 oldlen
- poffset
- olddirlen
+ 1);
3321 conf
->unexpanded_config
= new;
3323 memcpy(new + poffset
, newdir
, newdirlen
);
3334 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3335 const char *newpath
, const char *oldname
,
3336 const char *newname
)
3338 __do_free
char *newdir
= NULL
,
3342 char *lstart
= conf
->unexpanded_config
;
3343 size_t newdirlen
, olddirlen
;
3344 const char *key
= "lxc.hook";
3346 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3347 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3348 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3349 if (ret
< 0 || ret
>= olddirlen
+ 1)
3352 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3353 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3354 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3355 if (ret
< 0 || ret
>= newdirlen
+ 1)
3358 if (!conf
->unexpanded_config
)
3362 lend
= strchr(lstart
, '\n');
3364 lend
= lstart
+ strlen(lstart
);
3368 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3371 p
= strchr(lstart
+ strlen(key
), '=');
3382 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
3385 /* replace the olddir with newdir */
3386 if (olddirlen
>= newdirlen
) {
3387 size_t diff
= olddirlen
- newdirlen
;
3388 memcpy(p
, newdir
, newdirlen
);
3390 if (olddirlen
!= newdirlen
) {
3391 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3392 strlen(p
) - newdirlen
- diff
+ 1);
3394 conf
->unexpanded_len
-= diff
;
3398 size_t diff
= newdirlen
- olddirlen
;
3399 size_t oldlen
= conf
->unexpanded_len
;
3400 size_t newlen
= oldlen
+ diff
;
3401 size_t poffset
= p
- conf
->unexpanded_config
;
3403 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3407 conf
->unexpanded_len
= newlen
;
3408 conf
->unexpanded_alloced
= newlen
+ 1;
3409 new[newlen
- 1] = '\0';
3410 lend
= new + (lend
- conf
->unexpanded_config
);
3412 /* Move over the remainder to make room for the newdir.
3414 memmove(new + poffset
+ newdirlen
,
3415 new + poffset
+ olddirlen
,
3416 oldlen
- poffset
- olddirlen
+ 1);
3417 conf
->unexpanded_config
= new;
3419 memcpy(new + poffset
, newdir
, newdirlen
);
3433 ERROR("Error writing to new config"); \
3438 /* This is called only from clone. We wish to update all hwaddrs in the
3439 * unexpanded config file. We can't/don't want to update any which come from
3440 * lxc.includes (there shouldn't be any).
3441 * We can't just walk the c->lxc-conf->network list because that includes netifs
3442 * from the include files. So we update the ones which we find in the unexp
3443 * config file, then find the original macaddr in the conf->network, and update
3444 * that to the same value.
3446 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3448 char *lend
, *p
, *p2
;
3449 struct lxc_list
*it
;
3450 char *lstart
= conf
->unexpanded_config
;
3452 if (!conf
->unexpanded_config
)
3456 char newhwaddr
[18], oldhwaddr
[17];
3458 lend
= strchr(lstart
, '\n');
3460 lend
= lstart
+ strlen(lstart
);
3464 if (!lxc_config_net_is_hwaddr(lstart
)) {
3469 p
= strchr(lstart
, '=');
3482 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3485 if ((p2
- p
) != 17) {
3486 WARN("Bad hwaddr entry");
3491 memcpy(oldhwaddr
, p
, 17);
3493 if (!new_hwaddr(newhwaddr
))
3496 memcpy(p
, newhwaddr
, 17);
3497 lxc_list_for_each(it
, &conf
->network
) {
3498 struct lxc_netdev
*n
= it
->elem
;
3500 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3501 memcpy(n
->hwaddr
, newhwaddr
, 17);
3510 static int set_config_ephemeral(const char *key
, const char *value
,
3511 struct lxc_conf
*lxc_conf
, void *data
)
3513 if (lxc_config_value_empty(value
)) {
3514 lxc_conf
->ephemeral
= 0;
3518 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3521 if (lxc_conf
->ephemeral
> 1)
3527 static int set_config_log_syslog(const char *key
, const char *value
,
3528 struct lxc_conf
*lxc_conf
, void *data
)
3532 if (lxc_conf
->syslog
) {
3533 free(lxc_conf
->syslog
);
3534 lxc_conf
->syslog
= NULL
;
3537 if (lxc_config_value_empty(value
))
3540 facility
= lxc_syslog_priority_to_int(value
);
3541 if (facility
== -EINVAL
)
3544 lxc_log_syslog(facility
);
3546 return set_config_string_item(&lxc_conf
->syslog
, value
);
3549 static int set_config_no_new_privs(const char *key
, const char *value
,
3550 struct lxc_conf
*lxc_conf
, void *data
)
3554 if (lxc_config_value_empty(value
)) {
3555 lxc_conf
->no_new_privs
= false;
3559 if (lxc_safe_uint(value
, &v
) < 0)
3565 lxc_conf
->no_new_privs
= v
? true : false;
3570 /* Callbacks to get configuration items. */
3571 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3572 struct lxc_conf
*c
, void *data
)
3579 memset(retv
, 0, inlen
);
3581 #if HAVE_SYS_PERSONALITY_H
3584 switch (c
->personality
) {
3586 strprint(retv
, inlen
, "i686");
3589 strprint(retv
, inlen
, "x86_64");
3599 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3600 struct lxc_conf
*c
, void *data
)
3602 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3605 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3606 struct lxc_conf
*c
, void *data
)
3608 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3611 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3612 struct lxc_conf
*c
, void *data
)
3614 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3617 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3618 struct lxc_conf
*c
, void *data
)
3620 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3623 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3624 int inlen
, struct lxc_conf
*c
,
3627 return lxc_get_conf_int(c
, retv
, inlen
,
3628 c
->lsm_aa_allow_incomplete
);
3631 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3632 int inlen
, struct lxc_conf
*c
,
3635 return lxc_get_conf_int(c
, retv
, inlen
,
3636 c
->lsm_aa_allow_nesting
);
3639 static int get_config_apparmor_raw(const char *key
, char *retv
,
3640 int inlen
, struct lxc_conf
*c
,
3644 struct lxc_list
*it
;
3650 memset(retv
, 0, inlen
);
3652 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3653 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3659 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3660 struct lxc_conf
*c
, void *data
)
3662 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3665 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3666 struct lxc_conf
*c
, void *data
)
3668 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3671 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3672 struct lxc_conf
*c
, void *data
)
3674 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3678 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3679 * just the value(s) will be printed. Since there still could be more than one,
3680 * it is newline-separated.
3681 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3683 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3684 * 'lxc.cgroup.subsystem.key = value' format.
3686 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3687 int inlen
, struct lxc_conf
*c
,
3691 size_t namespaced_token_len
;
3692 char *global_token
, *namespaced_token
;
3693 struct lxc_list
*it
;
3695 bool get_all
= false;
3700 memset(retv
, 0, inlen
);
3702 if (version
== CGROUP2_SUPER_MAGIC
) {
3703 global_token
= "lxc.cgroup2";
3704 namespaced_token
= "lxc.cgroup2.";
3705 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3706 } else if (version
== CGROUP_SUPER_MAGIC
) {
3707 global_token
= "lxc.cgroup";
3708 namespaced_token
= "lxc.cgroup.";
3709 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3714 if (strcmp(key
, global_token
) == 0)
3716 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3717 key
+= namespaced_token_len
;
3721 lxc_list_for_each(it
, &c
->cgroup
) {
3722 struct lxc_cgroup
*cg
= it
->elem
;
3725 if (version
!= cg
->version
)
3728 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3729 cg
->subsystem
, cg
->value
);
3730 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3731 strprint(retv
, inlen
, "%s\n", cg
->value
);
3738 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3739 struct lxc_conf
*c
, void *data
)
3741 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3742 CGROUP_SUPER_MAGIC
);
3745 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3746 struct lxc_conf
*c
, void *data
)
3748 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3749 CGROUP2_SUPER_MAGIC
);
3752 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3753 struct lxc_conf
*lxc_conf
, void *data
)
3761 memset(retv
, 0, inlen
);
3763 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3768 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3769 struct lxc_conf
*lxc_conf
, void *data
)
3777 memset(retv
, 0, inlen
);
3779 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3784 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3785 struct lxc_conf
*lxc_conf
, void *data
)
3793 memset(retv
, 0, inlen
);
3795 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3800 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3802 struct lxc_conf
*lxc_conf
,
3811 memset(retv
, 0, inlen
);
3813 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3818 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3820 struct lxc_conf
*lxc_conf
,
3829 memset(retv
, 0, inlen
);
3831 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3836 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3837 int inlen
, struct lxc_conf
*lxc_conf
,
3840 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3841 lxc_conf
->cgroup_meta
.relative
);
3844 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3845 struct lxc_conf
*c
, void *data
)
3847 struct lxc_list
*it
;
3848 int len
, listlen
, ret
;
3850 /* "u 1000 1000000 65536"
3852 * let's render this as
3870 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3871 char buf
[__LXC_IDMAP_STR_BUF
];
3876 memset(retv
, 0, inlen
);
3878 listlen
= lxc_list_len(&c
->id_map
);
3879 lxc_list_for_each(it
, &c
->id_map
) {
3880 struct id_map
*map
= it
->elem
;
3881 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3882 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3883 map
->nsid
, map
->hostid
, map
->range
);
3884 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3887 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3893 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3894 struct lxc_conf
*c
, void *data
)
3897 v
= lxc_log_priority_to_string(c
->loglevel
);
3898 return lxc_get_conf_str(retv
, inlen
, v
);
3901 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3902 struct lxc_conf
*c
, void *data
)
3904 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3907 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3908 struct lxc_conf
*c
, void *data
)
3910 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3913 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3914 struct lxc_conf
*c
, void *data
)
3916 int len
, fulllen
= 0;
3917 const char *sep
= "";
3922 memset(retv
, 0, inlen
);
3924 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3927 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3928 case LXC_AUTO_PROC_MIXED
:
3929 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3932 case LXC_AUTO_PROC_RW
:
3933 strprint(retv
, inlen
, "%sproc:rw", sep
);
3940 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3941 case LXC_AUTO_SYS_RO
:
3942 strprint(retv
, inlen
, "%ssys:ro", sep
);
3945 case LXC_AUTO_SYS_RW
:
3946 strprint(retv
, inlen
, "%ssys:rw", sep
);
3949 case LXC_AUTO_SYS_MIXED
:
3950 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3957 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3958 case LXC_AUTO_CGROUP_NOSPEC
:
3959 strprint(retv
, inlen
, "%scgroup", sep
);
3961 case LXC_AUTO_CGROUP_MIXED
:
3962 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3964 case LXC_AUTO_CGROUP_RO
:
3965 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3967 case LXC_AUTO_CGROUP_RW
:
3968 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3970 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3971 strprint(retv
, inlen
, "%scgroup-full", sep
);
3973 case LXC_AUTO_CGROUP_FULL_MIXED
:
3974 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3976 case LXC_AUTO_CGROUP_FULL_RO
:
3977 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3979 case LXC_AUTO_CGROUP_FULL_RW
:
3980 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3989 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3990 struct lxc_conf
*c
, void *data
)
3992 int len
, fulllen
= 0;
3993 struct lxc_list
*it
;
3998 memset(retv
, 0, inlen
);
4000 lxc_list_for_each(it
, &c
->mount_list
) {
4001 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4007 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4008 struct lxc_conf
*c
, void *data
)
4010 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4013 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4014 struct lxc_conf
*c
, void *data
)
4016 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4019 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4020 struct lxc_conf
*c
, void *data
)
4022 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4025 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4026 struct lxc_conf
*c
, void *data
)
4028 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
4031 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4032 struct lxc_conf
*c
, void *data
)
4034 return lxc_get_conf_str(
4036 c
->utsname
? c
->utsname
->nodename
: NULL
);
4039 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4040 struct lxc_conf
*c
, void *data
)
4043 int len
, fulllen
= 0, found
= -1;
4044 struct lxc_list
*it
;
4047 subkey
= strchr(key
, '.');
4051 subkey
= strchr(subkey
+ 1, '.');
4055 if (*subkey
== '\0')
4058 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4059 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
4071 memset(retv
, 0, inlen
);
4073 lxc_list_for_each(it
, &c
->hooks
[found
]) {
4074 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4080 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4081 struct lxc_conf
*c
, void *data
)
4083 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4086 static int get_config_net(const char *key
, char *retv
, int inlen
,
4087 struct lxc_conf
*c
, void *data
)
4089 int len
, fulllen
= 0;
4090 struct lxc_list
*it
;
4095 memset(retv
, 0, inlen
);
4097 lxc_list_for_each(it
, &c
->network
) {
4098 struct lxc_netdev
*n
= it
->elem
;
4099 const char *t
= lxc_net_type_to_str(n
->type
);
4100 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4106 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4107 struct lxc_conf
*c
, void *data
)
4109 int len
, fulllen
= 0;
4110 struct lxc_list
*it
;
4115 memset(retv
, 0, inlen
);
4117 lxc_list_for_each(it
, &c
->caps
) {
4118 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4124 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4125 struct lxc_conf
*c
, void *data
)
4127 int len
, fulllen
= 0;
4128 struct lxc_list
*it
;
4133 memset(retv
, 0, inlen
);
4135 lxc_list_for_each(it
, &c
->keepcaps
) {
4136 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4142 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4143 struct lxc_conf
*c
, void *data
)
4145 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4148 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4149 struct lxc_conf
*c
, void *data
)
4151 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4154 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4155 struct lxc_conf
*c
, void *data
)
4157 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4161 static int get_config_console_buffer_size(const char *key
, char *retv
,
4162 int inlen
, struct lxc_conf
*c
,
4165 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4168 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4169 struct lxc_conf
*c
, void *data
)
4171 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4174 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4175 int inlen
, struct lxc_conf
*c
,
4179 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4186 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4187 struct lxc_conf
*c
, void *data
)
4189 #ifdef HAVE_SECCOMP_NOTIFY
4190 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4192 return ret_set_errno(-1, ENOSYS
);
4196 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4197 struct lxc_conf
*c
, void *data
)
4199 #ifdef HAVE_SECCOMP_NOTIFY
4200 return lxc_get_conf_str(retv
, inlen
,
4201 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4202 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4203 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4205 return ret_set_errno(-1, ENOSYS
);
4209 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4210 struct lxc_conf
*c
, void *data
)
4212 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4215 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4216 struct lxc_conf
*c
, void *data
)
4218 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4221 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4222 struct lxc_conf
*c
, void *data
)
4224 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4227 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4228 struct lxc_conf
*c
, void *data
)
4230 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4233 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4234 struct lxc_conf
*c
, void *data
)
4236 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4239 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4240 struct lxc_conf
*c
, void *data
)
4242 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4245 static int get_config_start(const char *key
, char *retv
, int inlen
,
4246 struct lxc_conf
*c
, void *data
)
4248 if (strcmp(key
+ 10, "auto") == 0)
4249 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4250 else if (strcmp(key
+ 10, "delay") == 0)
4251 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4252 else if (strcmp(key
+ 10, "order") == 0)
4253 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4258 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4259 struct lxc_conf
*c
, void *data
)
4261 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4264 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4265 struct lxc_conf
*c
, void *data
)
4267 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4270 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4271 int inlen
, struct lxc_conf
*c
,
4274 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4277 static int get_config_group(const char *key
, char *retv
, int inlen
,
4278 struct lxc_conf
*c
, void *data
)
4280 int len
, fulllen
= 0;
4281 struct lxc_list
*it
;
4286 memset(retv
, 0, inlen
);
4288 lxc_list_for_each(it
, &c
->groups
) {
4289 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4295 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4296 struct lxc_conf
*c
, void *data
)
4298 int len
, fulllen
= 0;
4299 struct lxc_list
*it
;
4304 memset(retv
, 0, inlen
);
4306 lxc_list_for_each(it
, &c
->environment
) {
4307 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4313 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4314 struct lxc_conf
*c
, void *data
)
4316 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4319 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4320 struct lxc_conf
*c
, void *data
)
4322 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4325 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4326 struct lxc_conf
*c
, void *data
)
4328 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4331 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4332 struct lxc_conf
*c
, void *data
)
4334 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4337 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4338 struct lxc_conf
*c
, void *data
)
4340 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4343 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4344 struct lxc_conf
*c
, void *data
)
4346 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4349 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4350 struct lxc_conf
*c
, void *data
)
4352 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4355 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4356 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4357 * printed, in 'lxc.prlimit.resource = value' format.
4359 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4360 struct lxc_conf
*c
, void *data
)
4362 int fulllen
= 0, len
;
4363 bool get_all
= false;
4364 struct lxc_list
*it
;
4369 memset(retv
, 0, inlen
);
4371 if (!strcmp(key
, "lxc.prlimit"))
4373 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
4378 lxc_list_for_each(it
, &c
->limits
) {
4379 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4380 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4382 struct lxc_limit
*lim
= it
->elem
;
4384 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4385 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4386 partlen
= STRLITERALLEN("unlimited");
4388 partlen
= sprintf(buf
, "%" PRIu64
,
4389 (uint64_t)lim
->limit
.rlim_cur
);
4392 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4393 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4394 memcpy(buf
+ partlen
, ":unlimited",
4395 STRLITERALLEN(":unlimited") + 1);
4397 sprintf(buf
+ partlen
, ":%" PRIu64
,
4398 (uint64_t)lim
->limit
.rlim_max
);
4402 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4403 lim
->resource
, buf
);
4404 } else if (strcmp(lim
->resource
, key
) == 0) {
4405 strprint(retv
, inlen
, "%s", buf
);
4412 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4413 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4414 * entries will be printed, in 'lxc.sysctl.key = value' format.
4416 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4417 struct lxc_conf
*c
, void *data
)
4420 struct lxc_list
*it
;
4422 bool get_all
= false;
4427 memset(retv
, 0, inlen
);
4429 if (strcmp(key
, "lxc.sysctl") == 0)
4431 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4432 key
+= STRLITERALLEN("lxc.sysctl.");
4436 lxc_list_for_each(it
, &c
->sysctls
) {
4437 struct lxc_sysctl
*elem
= it
->elem
;
4439 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
4441 } else if (strcmp(elem
->key
, key
) == 0) {
4442 strprint(retv
, inlen
, "%s", elem
->value
);
4449 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4450 struct lxc_conf
*c
, void *data
)
4452 struct lxc_list
*it
;
4455 bool get_all
= false;
4460 memset(retv
, 0, inlen
);
4462 if (strcmp(key
, "lxc.proc") == 0)
4464 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4465 key
+= STRLITERALLEN("lxc.proc.");
4469 lxc_list_for_each(it
, &c
->procs
) {
4470 struct lxc_proc
*proc
= it
->elem
;
4473 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4474 proc
->filename
, proc
->value
);
4475 } else if (strcmp(proc
->filename
, key
) == 0) {
4476 strprint(retv
, inlen
, "%s", proc
->value
);
4483 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4484 struct lxc_conf
*c
, void *data
)
4492 memset(retv
, 0, inlen
);
4494 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4495 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4496 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4502 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4503 struct lxc_conf
*c
, void *data
)
4511 memset(retv
, 0, inlen
);
4513 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4514 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4515 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4521 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4530 memset(retv
, 0, inlen
);
4532 if (c
->timens
.s_boot
) {
4533 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4535 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4541 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4542 struct lxc_conf
*c
, void *data
)
4550 memset(retv
, 0, inlen
);
4552 if (c
->timens
.s_monotonic
) {
4553 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4555 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4561 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4562 struct lxc_conf
*c
, void *data
)
4565 const char *namespace;
4571 memset(retv
, 0, inlen
);
4573 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4574 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4578 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4583 /* Callbacks to clear config items. */
4584 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4587 c
->personality
= -1;
4591 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4598 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4605 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4613 static inline int clr_config_apparmor_profile(const char *key
,
4614 struct lxc_conf
*c
, void *data
)
4616 free(c
->lsm_aa_profile
);
4617 c
->lsm_aa_profile
= NULL
;
4621 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4625 c
->lsm_aa_allow_incomplete
= 0;
4629 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4633 c
->lsm_aa_allow_nesting
= 0;
4637 static inline int clr_config_apparmor_raw(const char *key
,
4641 return lxc_clear_apparmor_raw(c
);
4644 static inline int clr_config_selinux_context(const char *key
,
4645 struct lxc_conf
*c
, void *data
)
4647 free(c
->lsm_se_context
);
4648 c
->lsm_se_context
= NULL
;
4652 static inline int clr_config_selinux_context_keyring(const char *key
,
4653 struct lxc_conf
*c
, void *data
)
4655 free(c
->lsm_se_keyring_context
);
4656 c
->lsm_se_keyring_context
= NULL
;
4660 static inline int clr_config_keyring_session(const char *key
,
4661 struct lxc_conf
*c
, void *data
)
4663 c
->keyring_disable_session
= false;
4667 static inline int clr_config_cgroup_controller(const char *key
,
4668 struct lxc_conf
*c
, void *data
)
4670 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4673 static inline int clr_config_cgroup2_controller(const char *key
,
4674 struct lxc_conf
*c
, void *data
)
4676 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4679 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4682 if (lxc_conf
->cgroup_meta
.dir
) {
4683 free(lxc_conf
->cgroup_meta
.dir
);
4684 lxc_conf
->cgroup_meta
.dir
= NULL
;
4690 static int clr_config_cgroup_monitor_dir(const char *key
,
4691 struct lxc_conf
*lxc_conf
,
4694 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4698 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4699 struct lxc_conf
*lxc_conf
,
4702 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4706 static int clr_config_cgroup_container_dir(const char *key
,
4707 struct lxc_conf
*lxc_conf
,
4710 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4714 static int clr_config_cgroup_container_inner_dir(const char *key
,
4715 struct lxc_conf
*lxc_conf
,
4718 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4722 static inline int clr_config_cgroup_relative(const char *key
,
4723 struct lxc_conf
*lxc_conf
,
4726 lxc_conf
->cgroup_meta
.relative
= false;
4730 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4733 return lxc_clear_idmaps(c
);
4736 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4739 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4743 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4751 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4754 return lxc_clear_mount_entries(c
);
4757 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4760 return lxc_clear_automounts(c
);
4763 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4771 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4774 free(c
->rootfs
.path
);
4775 c
->rootfs
.path
= NULL
;
4779 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4782 c
->rootfs
.managed
= true;
4786 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4789 free(c
->rootfs
.mount
);
4790 c
->rootfs
.mount
= NULL
;
4794 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4797 free(c
->rootfs
.options
);
4798 c
->rootfs
.options
= NULL
;
4800 free(c
->rootfs
.data
);
4801 c
->rootfs
.data
= NULL
;
4806 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4814 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4817 return lxc_clear_hooks(c
, key
);
4820 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4823 /* default to legacy hooks version */
4824 c
->hooks_version
= 0;
4828 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4831 lxc_free_networks(&c
->network
);
4836 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4839 return lxc_clear_config_caps(c
);
4842 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4845 return lxc_clear_config_keepcaps(c
);
4848 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4851 free(c
->console
.path
);
4852 c
->console
.path
= NULL
;
4856 static inline int clr_config_console_logfile(const char *key
,
4857 struct lxc_conf
*c
, void *data
)
4859 free(c
->console
.log_path
);
4860 c
->console
.log_path
= NULL
;
4864 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4867 c
->console
.log_rotate
= 0;
4871 static inline int clr_config_console_buffer_size(const char *key
,
4872 struct lxc_conf
*c
, void *data
)
4874 c
->console
.buffer_size
= 0;
4878 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4881 c
->console
.log_size
= 0;
4885 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4886 struct lxc_conf
*c
, void *data
)
4889 c
->seccomp
.allow_nesting
= 0;
4897 static inline int clr_config_seccomp_notify_cookie(const char *key
,
4898 struct lxc_conf
*c
, void *data
)
4900 #ifdef HAVE_SECCOMP_NOTIFY
4901 free(c
->seccomp
.notifier
.cookie
);
4902 c
->seccomp
.notifier
.cookie
= NULL
;
4905 return ret_set_errno(-1, ENOSYS
);
4909 static inline int clr_config_seccomp_notify_proxy(const char *key
,
4910 struct lxc_conf
*c
, void *data
)
4912 #ifdef HAVE_SECCOMP_NOTIFY
4913 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
4914 sizeof(c
->seccomp
.notifier
.proxy_addr
));
4917 return ret_set_errno(-1, ENOSYS
);
4921 static inline int clr_config_seccomp_profile(const char *key
,
4922 struct lxc_conf
*c
, void *data
)
4924 free(c
->seccomp
.seccomp
);
4925 c
->seccomp
.seccomp
= NULL
;
4929 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4936 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
4939 c
->autodevtmpfssize
= 500000;
4943 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4950 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4953 c
->rebootsignal
= 0;
4957 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4964 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4967 if (strcmp(key
+ 10, "auto") == 0)
4969 else if (strcmp(key
+ 10, "delay") == 0)
4971 else if (strcmp(key
+ 10, "order") == 0)
4977 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4985 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4988 c
->monitor_unshare
= 0;
4992 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4993 struct lxc_conf
*c
, void *data
)
4995 c
->monitor_signal_pdeath
= 0;
4999 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
5002 return lxc_clear_groups(c
);
5005 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5008 return lxc_clear_environment(c
);
5011 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5014 free(c
->execute_cmd
);
5015 c
->execute_cmd
= NULL
;
5019 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5027 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5035 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5042 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5049 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5056 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5059 c
->no_new_privs
= false;
5063 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5066 return lxc_clear_limits(c
, key
);
5069 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5072 return lxc_clear_sysctls(c
, key
);
5075 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5078 return lxc_clear_procs(c
, key
);
5081 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5084 lxc_clear_includes(c
);
5088 static int clr_config_namespace_clone(const char *key
,
5089 struct lxc_conf
*lxc_conf
, void *data
)
5091 lxc_conf
->ns_clone
= 0;
5095 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5098 lxc_conf
->ns_keep
= 0;
5102 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5104 lxc_conf
->timens
.s_boot
= 0;
5105 lxc_conf
->timens
.ns_boot
= 0;
5109 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5111 lxc_conf
->timens
.s_monotonic
= 0;
5112 lxc_conf
->timens
.ns_monotonic
= 0;
5116 static int clr_config_namespace_share(const char *key
,
5117 struct lxc_conf
*lxc_conf
, void *data
)
5120 const char *namespace;
5122 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5123 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5127 free(lxc_conf
->ns_share
[ns_idx
]);
5128 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5133 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5134 struct lxc_conf
*c
, void *data
)
5139 static struct lxc_config_t
*get_network_config_ops(const char *key
,
5140 struct lxc_conf
*lxc_conf
,
5142 char **deindexed_key
)
5145 unsigned int tmpidx
;
5147 char *copy
, *idx_start
, *idx_end
;
5148 struct lxc_config_t
*config
= NULL
;
5150 /* check that this is a sensible network key */
5151 if (strncmp("lxc.net.", key
, 8)) {
5152 ERROR("Invalid network configuration key \"%s\"", key
);
5158 ERROR("Failed to duplicate string \"%s\"", key
);
5163 if (!isdigit(*(key
+ 8))) {
5164 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
5168 /* beginning of index string */
5169 idx_start
= (copy
+ 7);
5172 /* end of index string */
5173 idx_end
= strchr((copy
+ 8), '.');
5177 /* parse current index */
5178 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
5181 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
5187 /* This, of course is utterly nonsensical on so many levels, but
5188 * better safe than sorry.
5189 * (Checking for INT_MAX here is intentional.)
5191 if (tmpidx
== INT_MAX
) {
5192 SYSERROR("Number of configured networks would overflow the "
5198 numstrlen
= strlen((idx_start
+ 1));
5200 /* repair configuration key */
5203 /* lxc.net.<idx>.<subkey> */
5206 if (strlen(idx_end
+ 1) == 0) {
5207 ERROR("No subkey in network configuration key \"%s\"", key
);
5211 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
5212 copy
[strlen(key
) - (numstrlen
+ 1)] = '\0';
5214 config
= lxc_get_config(copy
);
5216 ERROR("Unknown network configuration key \"%s\"", key
);
5222 *deindexed_key
= copy
;
5231 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5232 * found. So we make sure next comes an integer, find the right callback (by
5233 * rewriting the key), and call it.
5235 static int set_config_net_nic(const char *key
, const char *value
,
5236 struct lxc_conf
*lxc_conf
, void *data
)
5239 const char *idxstring
;
5240 struct lxc_config_t
*config
;
5241 struct lxc_netdev
*netdev
;
5243 char *deindexed_key
= NULL
;
5245 idxstring
= key
+ 8;
5246 if (!isdigit(*idxstring
))
5249 if (lxc_config_value_empty(value
))
5250 return clr_config_net_nic(key
, lxc_conf
, data
);
5252 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5253 if (!config
|| idx
< 0)
5256 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
5258 free(deindexed_key
);
5262 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
5263 free(deindexed_key
);
5268 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
5272 const char *idxstring
;
5273 struct lxc_config_t
*config
;
5274 struct lxc_netdev
*netdev
;
5276 char *deindexed_key
= NULL
;
5278 idxstring
= key
+ 8;
5279 if (!isdigit(*idxstring
))
5282 /* The left conjunct is pretty self-explanatory. The right conjunct
5283 * checks whether the two pointers are equal. If they are we know that
5284 * this is not a key that is namespaced any further and so we are
5285 * supposed to clear the whole network.
5287 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5288 unsigned int rmnetdevidx
;
5290 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
5293 /* Remove network from network list. */
5294 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5298 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5299 if (!config
|| idx
< 0)
5302 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
5304 free(deindexed_key
);
5308 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
5309 free(deindexed_key
);
5314 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5317 struct lxc_netdev
*netdev
= data
;
5327 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5330 struct lxc_netdev
*netdev
= data
;
5335 netdev
->name
[0] = '\0';
5340 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5343 struct lxc_netdev
*netdev
= data
;
5353 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5356 struct lxc_netdev
*netdev
= data
;
5361 netdev
->link
[0] = '\0';
5366 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5369 struct lxc_netdev
*netdev
= data
;
5372 return ret_set_errno(-1, EINVAL
);
5374 netdev
->l2proxy
= false;
5379 static int clr_config_net_macvlan_mode(const char *key
,
5380 struct lxc_conf
*lxc_conf
, void *data
)
5382 struct lxc_netdev
*netdev
= data
;
5387 if (netdev
->type
!= LXC_NET_MACVLAN
)
5390 netdev
->priv
.macvlan_attr
.mode
= -1;
5395 static int clr_config_net_ipvlan_mode(const char *key
,
5396 struct lxc_conf
*lxc_conf
, void *data
)
5398 struct lxc_netdev
*netdev
= data
;
5401 return ret_set_errno(-1, EINVAL
);
5403 if (netdev
->type
!= LXC_NET_IPVLAN
)
5406 netdev
->priv
.ipvlan_attr
.mode
= -1;
5411 static int clr_config_net_ipvlan_isolation(const char *key
,
5412 struct lxc_conf
*lxc_conf
, void *data
)
5414 struct lxc_netdev
*netdev
= data
;
5417 return ret_set_errno(-1, EINVAL
);
5419 if (netdev
->type
!= LXC_NET_IPVLAN
)
5422 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5427 static int clr_config_net_veth_mode(const char *key
,
5428 struct lxc_conf
*lxc_conf
, void *data
)
5430 struct lxc_netdev
*netdev
= data
;
5433 return ret_set_errno(-1, EINVAL
);
5435 if (netdev
->type
!= LXC_NET_VETH
)
5438 netdev
->priv
.veth_attr
.mode
= -1;
5443 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5446 struct lxc_netdev
*netdev
= data
;
5451 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5456 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5459 struct lxc_netdev
*netdev
= data
;
5462 return ret_errno(EINVAL
);
5464 netdev
->priv
.veth_attr
.vlan_id
= 0;
5465 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5470 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5471 struct lxc_conf
*lxc_conf
, void *data
)
5473 struct lxc_netdev
*netdev
= data
;
5474 struct lxc_list
*cur
, *next
;
5477 return ret_errno(EINVAL
);
5479 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5488 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5491 struct lxc_netdev
*netdev
= data
;
5496 free(netdev
->upscript
);
5497 netdev
->upscript
= NULL
;
5502 static int clr_config_net_script_down(const char *key
,
5503 struct lxc_conf
*lxc_conf
, void *data
)
5505 struct lxc_netdev
*netdev
= data
;
5510 free(netdev
->downscript
);
5511 netdev
->downscript
= NULL
;
5516 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5519 struct lxc_netdev
*netdev
= data
;
5524 free(netdev
->hwaddr
);
5525 netdev
->hwaddr
= NULL
;
5530 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5533 struct lxc_netdev
*netdev
= data
;
5544 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5547 struct lxc_netdev
*netdev
= data
;
5552 netdev
->priv
.vlan_attr
.vid
= 0;
5557 static int clr_config_net_ipv4_gateway(const char *key
,
5558 struct lxc_conf
*lxc_conf
, void *data
)
5560 struct lxc_netdev
*netdev
= data
;
5565 free(netdev
->ipv4_gateway
);
5566 netdev
->ipv4_gateway
= NULL
;
5571 static int clr_config_net_ipv4_address(const char *key
,
5572 struct lxc_conf
*lxc_conf
, void *data
)
5574 struct lxc_netdev
*netdev
= data
;
5575 struct lxc_list
*cur
, *next
;
5580 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
5589 static int clr_config_net_veth_ipv4_route(const char *key
,
5590 struct lxc_conf
*lxc_conf
, void *data
)
5592 struct lxc_netdev
*netdev
= data
;
5593 struct lxc_list
*cur
, *next
;
5598 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv4_routes
, next
) {
5607 static int clr_config_net_ipv6_gateway(const char *key
,
5608 struct lxc_conf
*lxc_conf
, void *data
)
5610 struct lxc_netdev
*netdev
= data
;
5615 free(netdev
->ipv6_gateway
);
5616 netdev
->ipv6_gateway
= NULL
;
5621 static int clr_config_net_ipv6_address(const char *key
,
5622 struct lxc_conf
*lxc_conf
, void *data
)
5624 struct lxc_netdev
*netdev
= data
;
5625 struct lxc_list
*cur
, *next
;
5630 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
5639 static int clr_config_net_veth_ipv6_route(const char *key
,
5640 struct lxc_conf
*lxc_conf
, void *data
)
5642 struct lxc_netdev
*netdev
= data
;
5643 struct lxc_list
*cur
, *next
;
5648 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv6_routes
, next
) {
5657 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
5658 struct lxc_conf
*c
, void *data
)
5661 const char *idxstring
;
5662 struct lxc_config_t
*config
;
5663 struct lxc_netdev
*netdev
;
5665 char *deindexed_key
= NULL
;
5667 idxstring
= key
+ 8;
5668 if (!isdigit(*idxstring
))
5671 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
5672 if (!config
|| idx
< 0)
5675 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5677 free(deindexed_key
);
5681 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
5682 free(deindexed_key
);
5687 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5688 struct lxc_conf
*c
, void *data
)
5692 struct lxc_netdev
*netdev
= data
;
5697 memset(retv
, 0, inlen
);
5702 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5707 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5708 struct lxc_conf
*c
, void *data
)
5712 struct lxc_netdev
*netdev
= data
;
5717 memset(retv
, 0, inlen
);
5722 if (netdev
->flags
& IFF_UP
)
5723 strprint(retv
, inlen
, "up");
5728 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5729 struct lxc_conf
*c
, void *data
)
5733 struct lxc_netdev
*netdev
= data
;
5738 memset(retv
, 0, inlen
);
5743 if (netdev
->link
[0] != '\0')
5744 strprint(retv
, inlen
, "%s", netdev
->link
);
5749 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5750 struct lxc_conf
*c
, void *data
)
5752 struct lxc_netdev
*netdev
= data
;
5753 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5756 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5757 struct lxc_conf
*c
, void *data
)
5761 struct lxc_netdev
*netdev
= data
;
5766 memset(retv
, 0, inlen
);
5771 if (netdev
->name
[0] != '\0')
5772 strprint(retv
, inlen
, "%s", netdev
->name
);
5777 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5778 struct lxc_conf
*c
, void *data
)
5783 struct lxc_netdev
*netdev
= data
;
5788 memset(retv
, 0, inlen
);
5793 if (netdev
->type
!= LXC_NET_MACVLAN
)
5796 switch (netdev
->priv
.macvlan_attr
.mode
) {
5797 case MACVLAN_MODE_PRIVATE
:
5800 case MACVLAN_MODE_VEPA
:
5803 case MACVLAN_MODE_BRIDGE
:
5806 case MACVLAN_MODE_PASSTHRU
:
5814 strprint(retv
, inlen
, "%s", mode
);
5819 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5820 struct lxc_conf
*c
, void *data
)
5825 struct lxc_netdev
*netdev
= data
;
5830 memset(retv
, 0, inlen
);
5833 return ret_set_errno(-1, EINVAL
);
5835 if (netdev
->type
!= LXC_NET_IPVLAN
)
5838 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5839 case IPVLAN_MODE_L3
:
5842 case IPVLAN_MODE_L3S
:
5845 case IPVLAN_MODE_L2
:
5853 strprint(retv
, inlen
, "%s", mode
);
5858 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5859 struct lxc_conf
*c
, void *data
)
5864 struct lxc_netdev
*netdev
= data
;
5869 memset(retv
, 0, inlen
);
5872 return ret_set_errno(-1, EINVAL
);
5874 if (netdev
->type
!= LXC_NET_IPVLAN
)
5877 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
5878 case IPVLAN_ISOLATION_BRIDGE
:
5881 case IPVLAN_ISOLATION_PRIVATE
:
5884 case IPVLAN_ISOLATION_VEPA
:
5892 strprint(retv
, inlen
, "%s", mode
);
5897 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
5898 struct lxc_conf
*c
, void *data
)
5903 struct lxc_netdev
*netdev
= data
;
5908 memset(retv
, 0, inlen
);
5911 return ret_set_errno(-1, EINVAL
);
5913 if (netdev
->type
!= LXC_NET_VETH
)
5916 switch (netdev
->priv
.veth_attr
.mode
) {
5917 case VETH_MODE_BRIDGE
:
5920 case VETH_MODE_ROUTER
:
5928 strprint(retv
, inlen
, "%s", mode
);
5933 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
5934 struct lxc_conf
*c
, void *data
)
5938 struct lxc_netdev
*netdev
= data
;
5943 memset(retv
, 0, inlen
);
5948 if (netdev
->type
!= LXC_NET_VETH
)
5951 strprint(retv
, inlen
, "%s",
5952 netdev
->priv
.veth_attr
.pair
[0] != '\0'
5953 ? netdev
->priv
.veth_attr
.pair
5954 : netdev
->priv
.veth_attr
.veth1
);
5959 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
5960 struct lxc_conf
*c
, void *data
)
5964 struct lxc_netdev
*netdev
= data
;
5967 return ret_errno(EINVAL
);
5969 if (netdev
->type
!= LXC_NET_VETH
)
5975 memset(retv
, 0, inlen
);
5977 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
5982 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
, int inlen
,
5983 struct lxc_conf
*c
, void *data
)
5987 struct lxc_list
*it
;
5989 struct lxc_netdev
*netdev
= data
;
5992 return ret_errno(EINVAL
);
5994 if (netdev
->type
!= LXC_NET_VETH
)
6000 memset(retv
, 0, inlen
);
6002 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
6004 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
6005 unsigned short i
= PTR_TO_USHORT(it
->elem
);
6006 strprint(retv
, inlen
, "%u%s", i
,
6007 (listlen
-- > 1) ? "\n" : "");
6013 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6014 struct lxc_conf
*c
, void *data
)
6018 struct lxc_netdev
*netdev
= data
;
6023 memset(retv
, 0, inlen
);
6028 if (netdev
->upscript
)
6029 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6034 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6035 struct lxc_conf
*c
, void *data
)
6039 struct lxc_netdev
*netdev
= data
;
6044 memset(retv
, 0, inlen
);
6049 if (netdev
->downscript
)
6050 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6055 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6056 struct lxc_conf
*c
, void *data
)
6060 struct lxc_netdev
*netdev
= data
;
6065 memset(retv
, 0, inlen
);
6071 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6076 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6077 struct lxc_conf
*c
, void *data
)
6081 struct lxc_netdev
*netdev
= data
;
6086 memset(retv
, 0, inlen
);
6092 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6097 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6098 struct lxc_conf
*c
, void *data
)
6102 struct lxc_netdev
*netdev
= data
;
6107 memset(retv
, 0, inlen
);
6112 if (netdev
->type
!= LXC_NET_VLAN
)
6115 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6120 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6121 struct lxc_conf
*c
, void *data
)
6124 char buf
[INET_ADDRSTRLEN
];
6126 struct lxc_netdev
*netdev
= data
;
6131 memset(retv
, 0, inlen
);
6136 if (netdev
->ipv4_gateway_auto
) {
6137 strprint(retv
, inlen
, "auto");
6138 } else if (netdev
->ipv4_gateway_dev
) {
6139 strprint(retv
, inlen
, "dev");
6140 } else if (netdev
->ipv4_gateway
) {
6141 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
6142 strprint(retv
, inlen
, "%s", buf
);
6148 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6149 struct lxc_conf
*c
, void *data
)
6153 char buf
[INET_ADDRSTRLEN
];
6154 struct lxc_list
*it
;
6156 struct lxc_netdev
*netdev
= data
;
6161 memset(retv
, 0, inlen
);
6166 listlen
= lxc_list_len(&netdev
->ipv4
);
6168 lxc_list_for_each(it
, &netdev
->ipv4
) {
6169 struct lxc_inetdev
*i
= it
->elem
;
6170 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
6171 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6172 (listlen
-- > 1) ? "\n" : "");
6178 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6179 struct lxc_conf
*c
, void *data
)
6183 char buf
[INET_ADDRSTRLEN
];
6184 struct lxc_list
*it
;
6186 struct lxc_netdev
*netdev
= data
;
6191 memset(retv
, 0, inlen
);
6194 return ret_set_errno(-1, EINVAL
);
6196 if (netdev
->type
!= LXC_NET_VETH
)
6199 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
6201 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv4_routes
) {
6202 struct lxc_inetdev
*i
= it
->elem
;
6203 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
6204 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6205 (listlen
-- > 1) ? "\n" : "");
6211 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6212 struct lxc_conf
*c
, void *data
)
6215 char buf
[INET6_ADDRSTRLEN
];
6217 struct lxc_netdev
*netdev
= data
;
6222 memset(retv
, 0, inlen
);
6227 if (netdev
->ipv6_gateway_auto
) {
6228 strprint(retv
, inlen
, "auto");
6229 } else if (netdev
->ipv6_gateway_dev
) {
6230 strprint(retv
, inlen
, "dev");
6231 } else if (netdev
->ipv6_gateway
) {
6232 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
6233 strprint(retv
, inlen
, "%s", buf
);
6239 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6240 struct lxc_conf
*c
, void *data
)
6244 char buf
[INET6_ADDRSTRLEN
];
6245 struct lxc_list
*it
;
6247 struct lxc_netdev
*netdev
= data
;
6252 memset(retv
, 0, inlen
);
6257 listlen
= lxc_list_len(&netdev
->ipv6
);
6259 lxc_list_for_each(it
, &netdev
->ipv6
) {
6260 struct lxc_inet6dev
*i
= it
->elem
;
6261 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
6262 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6263 (listlen
-- > 1) ? "\n" : "");
6269 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6270 struct lxc_conf
*c
, void *data
)
6274 char buf
[INET6_ADDRSTRLEN
];
6275 struct lxc_list
*it
;
6277 struct lxc_netdev
*netdev
= data
;
6282 memset(retv
, 0, inlen
);
6285 return ret_set_errno(-1, EINVAL
);
6287 if (netdev
->type
!= LXC_NET_VETH
)
6290 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
6292 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv6_routes
) {
6293 struct lxc_inet6dev
*i
= it
->elem
;
6294 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
6295 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6296 (listlen
-- > 1) ? "\n" : "");
6302 int lxc_list_config_items(char *retv
, int inlen
)
6311 memset(retv
, 0, inlen
);
6313 for (i
= 0; i
< config_jump_table_size
; i
++) {
6314 char *s
= config_jump_table
[i
].name
;
6316 if (s
[strlen(s
) - 1] == '.')
6319 strprint(retv
, inlen
, "%s\n", s
);
6325 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6334 memset(retv
, 0, inlen
);
6336 if (!strcmp(key
, "lxc.apparmor")) {
6337 strprint(retv
, inlen
, "allow_incomplete\n");
6338 strprint(retv
, inlen
, "allow_nesting\n");
6339 strprint(retv
, inlen
, "profile\n");
6340 strprint(retv
, inlen
, "raw\n");
6341 } else if (!strcmp(key
, "lxc.cgroup")) {
6342 strprint(retv
, inlen
, "dir\n");
6343 } else if (!strcmp(key
, "lxc.selinux")) {
6344 strprint(retv
, inlen
, "context\n");
6345 strprint(retv
, inlen
, "context.keyring\n");
6346 } else if (!strcmp(key
, "lxc.mount")) {
6347 strprint(retv
, inlen
, "auto\n");
6348 strprint(retv
, inlen
, "entry\n");
6349 strprint(retv
, inlen
, "fstab\n");
6350 } else if (!strcmp(key
, "lxc.rootfs")) {
6351 strprint(retv
, inlen
, "mount\n");
6352 strprint(retv
, inlen
, "options\n");
6353 strprint(retv
, inlen
, "path\n");
6354 } else if (!strcmp(key
, "lxc.uts")) {
6355 strprint(retv
, inlen
, "name\n");
6356 } else if (!strcmp(key
, "lxc.hook")) {
6357 strprint(retv
, inlen
, "autodev\n");
6358 strprint(retv
, inlen
, "autodevtmpfssize\n");
6359 strprint(retv
, inlen
, "clone\n");
6360 strprint(retv
, inlen
, "destroy\n");
6361 strprint(retv
, inlen
, "mount\n");
6362 strprint(retv
, inlen
, "post-stop\n");
6363 strprint(retv
, inlen
, "pre-mount\n");
6364 strprint(retv
, inlen
, "pre-start\n");
6365 strprint(retv
, inlen
, "start-host\n");
6366 strprint(retv
, inlen
, "start\n");
6367 strprint(retv
, inlen
, "stop\n");
6368 } else if (!strcmp(key
, "lxc.cap")) {
6369 strprint(retv
, inlen
, "drop\n");
6370 strprint(retv
, inlen
, "keep\n");
6371 } else if (!strcmp(key
, "lxc.console")) {
6372 strprint(retv
, inlen
, "logfile\n");
6373 strprint(retv
, inlen
, "path\n");
6374 } else if (!strcmp(key
, "lxc.seccomp")) {
6375 strprint(retv
, inlen
, "profile\n");
6376 } else if (!strcmp(key
, "lxc.signal")) {
6377 strprint(retv
, inlen
, "halt\n");
6378 strprint(retv
, inlen
, "reboot\n");
6379 strprint(retv
, inlen
, "stop\n");
6380 } else if (!strcmp(key
, "lxc.start")) {
6381 strprint(retv
, inlen
, "auto\n");
6382 strprint(retv
, inlen
, "delay\n");
6383 strprint(retv
, inlen
, "order\n");
6384 } else if (!strcmp(key
, "lxc.monitor")) {
6385 strprint(retv
, inlen
, "unshare\n");
6386 } else if (!strcmp(key
, "lxc.keyring")) {
6387 strprint(retv
, inlen
, "session\n");
6395 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6398 const char *idxstring
;
6399 struct lxc_netdev
*netdev
;
6403 idxstring
= key
+ 8;
6404 if (!isdigit(*idxstring
))
6407 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
6411 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
6418 memset(retv
, 0, inlen
);
6420 strprint(retv
, inlen
, "type\n");
6421 strprint(retv
, inlen
, "script.up\n");
6422 strprint(retv
, inlen
, "script.down\n");
6424 if (netdev
->type
!= LXC_NET_EMPTY
) {
6425 strprint(retv
, inlen
, "flags\n");
6426 strprint(retv
, inlen
, "link\n");
6427 strprint(retv
, inlen
, "name\n");
6428 strprint(retv
, inlen
, "hwaddr\n");
6429 strprint(retv
, inlen
, "mtu\n");
6430 strprint(retv
, inlen
, "ipv6.address\n");
6431 strprint(retv
, inlen
, "ipv6.gateway\n");
6432 strprint(retv
, inlen
, "ipv4.address\n");
6433 strprint(retv
, inlen
, "ipv4.gateway\n");
6436 switch (netdev
->type
) {
6438 strprint(retv
, inlen
, "veth.pair\n");
6439 strprint(retv
, inlen
, "veth.ipv4.route\n");
6440 strprint(retv
, inlen
, "veth.ipv6.route\n");
6441 strprint(retv
, inlen
, "veth.vlan.id\n");
6443 case LXC_NET_MACVLAN
:
6444 strprint(retv
, inlen
, "macvlan.mode\n");
6446 case LXC_NET_IPVLAN
:
6447 strprint(retv
, inlen
, "ipvlan.mode\n");
6448 strprint(retv
, inlen
, "ipvlan.isolation\n");
6451 strprint(retv
, inlen
, "vlan.id\n");