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 __do_free
char *keepcaps
= NULL
;
2239 __do_free
struct lxc_list
*keeplist
= NULL
;
2242 if (lxc_config_value_empty(value
))
2243 return lxc_clear_config_keepcaps(lxc_conf
);
2245 keepcaps
= strdup(value
);
2247 return ret_errno(ENOMEM
);
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
));
2258 return ret_errno(ENOMEM
);
2260 keeplist
->elem
= strdup(token
);
2261 if (!keeplist
->elem
)
2262 return ret_errno(ENOMEM
);
2264 lxc_list_add_tail(&lxc_conf
->keepcaps
, move_ptr(keeplist
));
2270 static int set_config_cap_drop(const char *key
, const char *value
,
2271 struct lxc_conf
*lxc_conf
, void *data
)
2273 char *dropcaps
, *token
;
2274 struct lxc_list
*droplist
;
2277 if (lxc_config_value_empty(value
))
2278 return lxc_clear_config_caps(lxc_conf
);
2280 dropcaps
= strdup(value
);
2284 /* In case several capability drop is specified in a single line
2285 * split these caps in a single element for the list.
2287 lxc_iterate_parts(token
, dropcaps
, " \t") {
2288 droplist
= malloc(sizeof(*droplist
));
2292 droplist
->elem
= strdup(token
);
2293 if (!droplist
->elem
) {
2298 lxc_list_add_tail(&lxc_conf
->caps
, droplist
);
2309 static int set_config_console_path(const char *key
, const char *value
,
2310 struct lxc_conf
*lxc_conf
, void *data
)
2312 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2315 static int set_config_console_rotate(const char *key
, const char *value
,
2316 struct lxc_conf
*lxc_conf
, void *data
)
2318 if (lxc_config_value_empty(value
)) {
2319 lxc_conf
->console
.log_rotate
= 0;
2323 if (lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
) < 0)
2326 if (lxc_conf
->console
.log_rotate
> 1) {
2327 ERROR("The \"lxc.console.rotate\" config key can only be set "
2335 static int set_config_console_logfile(const char *key
, const char *value
,
2336 struct lxc_conf
*lxc_conf
, void *data
)
2338 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2341 static int set_config_console_buffer_size(const char *key
, const char *value
,
2342 struct lxc_conf
*lxc_conf
, void *data
)
2346 uint64_t buffer_size
, pgsz
;
2348 if (lxc_config_value_empty(value
)) {
2349 lxc_conf
->console
.buffer_size
= 0;
2353 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2354 if (!strcmp(value
, "auto")) {
2355 lxc_conf
->console
.buffer_size
= 1 << 17;
2359 ret
= parse_byte_size_string(value
, &size
);
2366 /* must be at least a page size */
2367 pgsz
= lxc_getpagesize();
2368 if ((uint64_t)size
< pgsz
) {
2369 NOTICE("Requested ringbuffer size for the console is %" PRId64
2370 " but must be at least %" PRId64
2371 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2376 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2377 if (buffer_size
== 0)
2380 if (buffer_size
!= size
)
2381 NOTICE("Passed size was not a power of 2. Rounding log size to "
2382 "next power of two: %" PRIu64
" bytes", buffer_size
);
2384 lxc_conf
->console
.buffer_size
= buffer_size
;
2389 static int set_config_console_size(const char *key
, const char *value
,
2390 struct lxc_conf
*lxc_conf
, void *data
)
2394 uint64_t log_size
, pgsz
;
2396 if (lxc_config_value_empty(value
)) {
2397 lxc_conf
->console
.log_size
= 0;
2401 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2402 if (!strcmp(value
, "auto")) {
2403 lxc_conf
->console
.log_size
= 1 << 17;
2407 ret
= parse_byte_size_string(value
, &size
);
2414 /* must be at least a page size */
2415 pgsz
= lxc_getpagesize();
2416 if ((uint64_t)size
< pgsz
) {
2417 NOTICE("Requested ringbuffer size for the console is %" PRId64
2418 " but must be at least %" PRId64
2419 " bytes. Setting ringbuffer size to %" PRId64
" bytes",
2424 log_size
= lxc_find_next_power2((uint64_t)size
);
2428 if (log_size
!= size
)
2429 NOTICE("Passed size was not a power of 2. Rounding log size to "
2430 "next power of two: %" PRIu64
" bytes", log_size
);
2432 lxc_conf
->console
.log_size
= log_size
;
2438 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2439 * file, we expand it in the unexpanded_config, so that after a save_config we
2440 * store the hwaddr for re-use.
2441 * This is only called when reading the config file, not when executing a
2443 * 'x' and 'X' are substituted in-place.
2445 static void update_hwaddr(const char *line
)
2449 line
+= lxc_char_left_gc(line
, strlen(line
));
2453 if (!lxc_config_net_is_hwaddr(line
))
2456 /* Let config_net_hwaddr raise the error. */
2457 p
= strchr(line
, '=');
2468 rand_complete_hwaddr(p
);
2471 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2474 size_t len
= conf
->unexpanded_len
;
2476 update_hwaddr(line
);
2478 linelen
= strlen(line
);
2479 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2480 char *tmp
= realloc(conf
->unexpanded_config
,
2481 conf
->unexpanded_alloced
+ 1024);
2485 if (!conf
->unexpanded_config
)
2488 conf
->unexpanded_config
= tmp
;
2489 conf
->unexpanded_alloced
+= 1024;
2492 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2493 conf
->unexpanded_len
+= linelen
;
2494 if (line
[linelen
- 1] != '\n')
2495 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2496 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2501 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2503 __do_closedir
DIR *dir
= NULL
;
2504 struct dirent
*direntp
;
2505 char path
[PATH_MAX
];
2509 dir
= opendir(dirp
);
2513 while ((direntp
= readdir(dir
))) {
2516 fnam
= direntp
->d_name
;
2517 if (!strcmp(fnam
, "."))
2520 if (!strcmp(fnam
, ".."))
2524 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2527 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2528 if (len
< 0 || len
>= PATH_MAX
)
2531 ret
= lxc_config_read(path
, lxc_conf
, true);
2539 static int set_config_includefiles(const char *key
, const char *value
,
2540 struct lxc_conf
*lxc_conf
, void *data
)
2542 if (lxc_config_value_empty(value
)) {
2543 clr_config_includefiles(key
, lxc_conf
, NULL
);
2548 return do_includedir(value
, lxc_conf
);
2550 return lxc_config_read(value
, lxc_conf
, true);
2553 static int set_config_rootfs_path(const char *key
, const char *value
,
2554 struct lxc_conf
*lxc_conf
, void *data
)
2558 const char *container_path
;
2560 if (lxc_config_value_empty(value
)) {
2561 free(lxc_conf
->rootfs
.path
);
2562 lxc_conf
->rootfs
.path
= NULL
;
2566 dup
= strdup(value
);
2570 /* Split <storage type>:<container path> into <storage type> and
2571 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2572 * "rootfs.path" to <container path>.
2574 tmp
= strchr(dup
, ':');
2578 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2585 container_path
= tmp
;
2587 container_path
= value
;
2590 ret
= set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2596 static int set_config_rootfs_managed(const char *key
, const char *value
,
2597 struct lxc_conf
*lxc_conf
, void *data
)
2599 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2602 static int set_config_rootfs_mount(const char *key
, const char *value
,
2603 struct lxc_conf
*lxc_conf
, void *data
)
2605 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2608 static int set_config_rootfs_options(const char *key
, const char *value
,
2609 struct lxc_conf
*lxc_conf
, void *data
)
2611 unsigned long mflags
= 0, pflags
= 0;
2612 char *mdata
= NULL
, *opts
= NULL
;
2614 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2616 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2620 ret
= parse_propagationopts(value
, &pflags
);
2626 ret
= set_config_string_item(&opts
, value
);
2632 rootfs
->mountflags
= mflags
| pflags
;
2633 rootfs
->options
= opts
;
2634 rootfs
->data
= mdata
;
2639 static int set_config_uts_name(const char *key
, const char *value
,
2640 struct lxc_conf
*lxc_conf
, void *data
)
2642 struct utsname
*utsname
;
2644 if (lxc_config_value_empty(value
)) {
2645 clr_config_uts_name(key
, lxc_conf
, NULL
);
2649 utsname
= malloc(sizeof(*utsname
));
2653 if (strlen(value
) >= sizeof(utsname
->nodename
)) {
2658 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2659 free(lxc_conf
->utsname
);
2660 lxc_conf
->utsname
= utsname
;
2665 static int set_config_namespace_clone(const char *key
, const char *value
,
2666 struct lxc_conf
*lxc_conf
, void *data
)
2671 if (lxc_config_value_empty(value
))
2672 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2674 if (lxc_conf
->ns_keep
!= 0) {
2676 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2677 "\"lxc.namespace.keep\"");
2685 lxc_iterate_parts(token
, ns
, " \t") {
2686 token
+= lxc_char_left_gc(token
, strlen(token
));
2687 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2688 cloneflag
= lxc_namespace_2_cloneflag(token
);
2689 if (cloneflag
< 0) {
2693 lxc_conf
->ns_clone
|= cloneflag
;
2700 static int set_config_namespace_keep(const char *key
, const char *value
,
2701 struct lxc_conf
*lxc_conf
, void *data
)
2706 if (lxc_config_value_empty(value
))
2707 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2709 if (lxc_conf
->ns_clone
!= 0) {
2711 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2712 "\"lxc.namespace.keep\"");
2720 lxc_iterate_parts(token
, ns
, " \t") {
2721 token
+= lxc_char_left_gc(token
, strlen(token
));
2722 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2723 cloneflag
= lxc_namespace_2_cloneflag(token
);
2724 if (cloneflag
< 0) {
2728 lxc_conf
->ns_keep
|= cloneflag
;
2735 static int set_config_time_offset_boot(const char *key
, const char *value
,
2736 struct lxc_conf
*lxc_conf
, void *data
)
2741 char buf
[STRLITERALLEN("ms") + 1];
2743 if (lxc_config_value_empty(value
))
2744 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2746 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2750 unit
= lxc_trim_whitespace_in_place(buf
);
2751 if (strcmp(unit
, "h") == 0) {
2752 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2754 } else if (strcmp(unit
, "m") == 0) {
2755 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2757 } else if (strcmp(unit
, "s") == 0) {
2758 lxc_conf
->timens
.s_boot
= offset
;
2759 } else if (strcmp(unit
, "ms") == 0) {
2760 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2762 } else if (strcmp(unit
, "us") == 0) {
2763 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2765 } else if (strcmp(unit
, "ns") == 0) {
2766 lxc_conf
->timens
.ns_boot
= offset
;
2768 return ret_errno(EINVAL
);
2774 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2775 struct lxc_conf
*lxc_conf
, void *data
)
2780 char buf
[STRLITERALLEN("ms") + 1];
2782 if (lxc_config_value_empty(value
))
2783 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2785 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2789 unit
= lxc_trim_whitespace_in_place(buf
);
2790 if (strcmp(unit
, "h") == 0) {
2791 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2793 } else if (strcmp(unit
, "m") == 0) {
2794 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2796 } else if (strcmp(unit
, "s") == 0) {
2797 lxc_conf
->timens
.s_monotonic
= offset
;
2798 } else if (strcmp(unit
, "ms") == 0) {
2799 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2801 } else if (strcmp(unit
, "us") == 0) {
2802 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2804 } else if (strcmp(unit
, "ns") == 0) {
2805 lxc_conf
->timens
.ns_monotonic
= offset
;
2807 return ret_errno(EINVAL
);
2813 static int set_config_namespace_share(const char *key
, const char *value
,
2814 struct lxc_conf
*lxc_conf
, void *data
)
2817 const char *namespace;
2819 if (lxc_config_value_empty(value
))
2820 return clr_config_namespace_share(key
, lxc_conf
, data
);
2822 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2823 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2827 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2830 struct parse_line_conf
{
2831 struct lxc_conf
*conf
;
2835 static int parse_line(char *buffer
, void *data
)
2837 char *dot
, *key
, *line
, *linep
, *value
;
2839 struct lxc_config_t
*config
;
2842 struct parse_line_conf
*plc
= data
;
2844 /* If there are newlines in the config file we should keep them. */
2845 empty_line
= lxc_is_line_empty(dup
);
2849 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2850 * modified the original string on the stack by replacing '=' by '\0'
2853 linep
= line
= strdup(dup
);
2857 if (!plc
->from_include
) {
2858 ret
= append_unexp_config_line(line
, plc
->conf
);
2866 line
+= lxc_char_left_gc(line
, strlen(line
));
2868 /* ignore comments */
2872 /* martian option - don't add it to the config itself */
2873 if (strncmp(line
, "lxc.", 4))
2878 dot
= strchr(line
, '=');
2880 ERROR("Invalid configuration line: %s", line
);
2888 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2890 value
+= lxc_char_left_gc(value
, strlen(value
));
2891 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2893 if (*value
== '\'' || *value
== '\"') {
2896 len
= strlen(value
);
2897 if (len
> 1 && value
[len
- 1] == *value
) {
2898 value
[len
- 1] = '\0';
2903 config
= lxc_get_config(key
);
2905 ERROR("Unknown configuration key \"%s\"", key
);
2909 ret
= config
->set(key
, value
, plc
->conf
, NULL
);
2917 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2919 char *dot
, *key
, *line
, *linep
, *value
;
2922 struct new_config_item
*new = NULL
;
2924 linep
= line
= strdup(dup
);
2928 line
+= lxc_char_left_gc(line
, strlen(line
));
2930 /* martian option - don't add it to the config itself */
2931 if (strncmp(line
, "lxc.", 4))
2935 dot
= strchr(line
, '=');
2937 ERROR("Invalid configuration item: %s", line
);
2945 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2947 value
+= lxc_char_left_gc(value
, strlen(value
));
2948 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2950 if (*value
== '\'' || *value
== '\"') {
2953 len
= strlen(value
);
2954 if (len
> 1 && value
[len
- 1] == *value
) {
2955 value
[len
- 1] = '\0';
2961 new = malloc(sizeof(struct new_config_item
));
2965 new->key
= strdup(key
);
2966 new->val
= strdup(value
);
2967 if (!new->val
|| !new->key
)
2975 if (ret
< 0 && new) {
2985 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2987 struct parse_line_conf c
;
2990 c
.from_include
= from_include
;
2992 /* Catch only the top level config file name in the structure. */
2994 conf
->rcfile
= strdup(file
);
2996 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2999 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
3001 struct lxc_list
*dent
;
3003 dent
= malloc(sizeof(struct lxc_list
));
3007 dent
->elem
= parse_new_conf_line(arg
);
3013 lxc_list_add_tail(defines
, dent
);
3018 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3020 struct lxc_list
*it
;
3023 lxc_list_for_each(it
, defines
) {
3024 struct new_config_item
*new_item
= it
->elem
;
3025 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3030 lxc_config_define_free(defines
);
3035 void lxc_config_define_free(struct lxc_list
*defines
)
3037 struct lxc_list
*it
, *next
;
3039 lxc_list_for_each_safe(it
, defines
, next
) {
3040 struct new_config_item
*new_item
= it
->elem
;
3041 free(new_item
->key
);
3042 free(new_item
->val
);
3048 signed long lxc_config_parse_arch(const char *arch
)
3050 #if HAVE_SYS_PERSONALITY_H
3056 { "arm", PER_LINUX32
},
3057 { "armel", PER_LINUX32
},
3058 { "armhf", PER_LINUX32
},
3059 { "armv7l", PER_LINUX32
},
3060 { "athlon", PER_LINUX32
},
3061 { "i386", PER_LINUX32
},
3062 { "i486", PER_LINUX32
},
3063 { "i586", PER_LINUX32
},
3064 { "i686", PER_LINUX32
},
3065 { "linux32", PER_LINUX32
},
3066 { "mips", PER_LINUX32
},
3067 { "mipsel", PER_LINUX32
},
3068 { "ppc", PER_LINUX32
},
3069 { "powerpc", PER_LINUX32
},
3070 { "x86", PER_LINUX32
},
3071 { "amd64", PER_LINUX
},
3072 { "arm64", PER_LINUX
},
3073 { "linux64", PER_LINUX
},
3074 { "mips64", PER_LINUX
},
3075 { "mips64el", PER_LINUX
},
3076 { "ppc64", PER_LINUX
},
3077 { "ppc64el", PER_LINUX
},
3078 { "ppc64le", PER_LINUX
},
3079 { "powerpc64", PER_LINUX
},
3080 { "s390x", PER_LINUX
},
3081 { "x86_64", PER_LINUX
},
3083 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
3085 for (i
= 0; i
< len
; i
++)
3086 if (!strcmp(pername
[i
].name
, arch
))
3087 return pername
[i
].per
;
3093 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
3101 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3102 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3103 { "LSM", LXC_ATTACH_LSM_EXEC
},
3108 /* For the sake of backward compatibility, drop all privileges
3109 * if none is specified.
3111 for (i
= 0; all_privs
[i
].token
; i
++)
3112 *flags
|= all_privs
[i
].flag
;
3117 lxc_iterate_parts(token
, flaglist
, "|") {
3120 for (i
= 0; all_privs
[i
].token
; i
++)
3121 if (!strcmp(all_privs
[i
].token
, token
))
3122 aflag
= all_privs
[i
].flag
;
3133 /* Write out a configuration file. */
3134 int write_config(int fd
, const struct lxc_conf
*conf
)
3137 size_t len
= conf
->unexpanded_len
;
3142 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3144 SYSERROR("Failed to write configuration file");
3151 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3154 __do_free
char *tmp
= NULL
;
3158 len
= strlen(key
) + strlen(v
) + 4;
3159 tmp
= must_realloc(NULL
, len
);
3161 if (lxc_config_value_empty(v
))
3162 ret
= snprintf(tmp
, len
, "%s =", key
);
3164 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
3165 if (ret
< 0 || ret
>= len
)
3168 /* Save the line verbatim into unexpanded_conf */
3169 if (append_unexp_config_line(tmp
, conf
))
3175 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3179 char *lstart
= conf
->unexpanded_config
;
3181 if (!conf
->unexpanded_config
)
3185 lend
= strchr(lstart
, '\n');
3189 lend
= lstart
+ strlen(lstart
);
3193 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
3199 v
= lstart
[strlen(key
)];
3200 if (!isspace(v
) && v
!= '=') {
3206 conf
->unexpanded_len
-= (lend
- lstart
);
3208 if (*lend
== '\0') {
3213 memmove(lstart
, lend
, strlen(lend
) + 1);
3217 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3218 const char *newpath
, const char *oldname
,
3219 const char *newname
, const char *ovldir
)
3221 __do_free
char *newdir
= NULL
,
3225 size_t newdirlen
, olddirlen
;
3226 char *lstart
= conf
->unexpanded_config
;
3227 const char *key
= "lxc.mount.entry";
3229 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3230 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3231 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
3233 if (ret
< 0 || ret
>= olddirlen
+ 1)
3236 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3237 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3238 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
3240 if (ret
< 0 || ret
>= newdirlen
+ 1)
3243 if (!conf
->unexpanded_config
)
3247 lend
= strchr(lstart
, '\n');
3249 lend
= lstart
+ strlen(lstart
);
3253 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3256 p
= strchr(lstart
+ strlen(key
), '=');
3267 /* Whenever a lxc.mount.entry entry is found in a line we check
3268 * if the substring "overlay" is present before doing any
3269 * further work. We check for "overlay" because substrings need
3270 * to have at least one space before them in a valid overlay
3271 * lxc.mount.entry (/A B overlay). When the space before is
3272 * missing it is very likely that these substrings are part of a
3273 * path or something else. (Checking q >= lend ensures that we
3274 * only count matches in the current line.) */
3275 q
= strstr(p
, " overlay");
3276 if (!q
|| q
>= lend
)
3279 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3282 /* replace the olddir with newdir */
3283 if (olddirlen
>= newdirlen
) {
3284 size_t diff
= olddirlen
- newdirlen
;
3285 memcpy(q
, newdir
, newdirlen
);
3287 if (olddirlen
!= newdirlen
) {
3288 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3289 strlen(q
) - newdirlen
- diff
+ 1);
3291 conf
->unexpanded_len
-= diff
;
3295 size_t diff
= newdirlen
- olddirlen
;
3296 size_t oldlen
= conf
->unexpanded_len
;
3297 size_t newlen
= oldlen
+ diff
;
3298 size_t poffset
= q
- conf
->unexpanded_config
;
3300 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3304 conf
->unexpanded_len
= newlen
;
3305 conf
->unexpanded_alloced
= newlen
+ 1;
3306 new[newlen
- 1] = '\0';
3307 lend
= new + (lend
- conf
->unexpanded_config
);
3309 /* Move over the remainder to make room for the newdir.
3311 memmove(new + poffset
+ newdirlen
,
3312 new + poffset
+ olddirlen
,
3313 oldlen
- poffset
- olddirlen
+ 1);
3314 conf
->unexpanded_config
= new;
3316 memcpy(new + poffset
, newdir
, newdirlen
);
3327 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3328 const char *newpath
, const char *oldname
,
3329 const char *newname
)
3331 __do_free
char *newdir
= NULL
,
3335 char *lstart
= conf
->unexpanded_config
;
3336 size_t newdirlen
, olddirlen
;
3337 const char *key
= "lxc.hook";
3339 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3340 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3341 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3342 if (ret
< 0 || ret
>= olddirlen
+ 1)
3345 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3346 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3347 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3348 if (ret
< 0 || ret
>= newdirlen
+ 1)
3351 if (!conf
->unexpanded_config
)
3355 lend
= strchr(lstart
, '\n');
3357 lend
= lstart
+ strlen(lstart
);
3361 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3364 p
= strchr(lstart
+ strlen(key
), '=');
3375 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
3378 /* replace the olddir with newdir */
3379 if (olddirlen
>= newdirlen
) {
3380 size_t diff
= olddirlen
- newdirlen
;
3381 memcpy(p
, newdir
, newdirlen
);
3383 if (olddirlen
!= newdirlen
) {
3384 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3385 strlen(p
) - newdirlen
- diff
+ 1);
3387 conf
->unexpanded_len
-= diff
;
3391 size_t diff
= newdirlen
- olddirlen
;
3392 size_t oldlen
= conf
->unexpanded_len
;
3393 size_t newlen
= oldlen
+ diff
;
3394 size_t poffset
= p
- conf
->unexpanded_config
;
3396 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3400 conf
->unexpanded_len
= newlen
;
3401 conf
->unexpanded_alloced
= newlen
+ 1;
3402 new[newlen
- 1] = '\0';
3403 lend
= new + (lend
- conf
->unexpanded_config
);
3405 /* Move over the remainder to make room for the newdir.
3407 memmove(new + poffset
+ newdirlen
,
3408 new + poffset
+ olddirlen
,
3409 oldlen
- poffset
- olddirlen
+ 1);
3410 conf
->unexpanded_config
= new;
3412 memcpy(new + poffset
, newdir
, newdirlen
);
3426 ERROR("Error writing to new config"); \
3431 /* This is called only from clone. We wish to update all hwaddrs in the
3432 * unexpanded config file. We can't/don't want to update any which come from
3433 * lxc.includes (there shouldn't be any).
3434 * We can't just walk the c->lxc-conf->network list because that includes netifs
3435 * from the include files. So we update the ones which we find in the unexp
3436 * config file, then find the original macaddr in the conf->network, and update
3437 * that to the same value.
3439 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3441 char *lend
, *p
, *p2
;
3442 struct lxc_list
*it
;
3443 char *lstart
= conf
->unexpanded_config
;
3445 if (!conf
->unexpanded_config
)
3449 char newhwaddr
[18], oldhwaddr
[17];
3451 lend
= strchr(lstart
, '\n');
3453 lend
= lstart
+ strlen(lstart
);
3457 if (!lxc_config_net_is_hwaddr(lstart
)) {
3462 p
= strchr(lstart
, '=');
3475 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3478 if ((p2
- p
) != 17) {
3479 WARN("Bad hwaddr entry");
3484 memcpy(oldhwaddr
, p
, 17);
3486 if (!new_hwaddr(newhwaddr
))
3489 memcpy(p
, newhwaddr
, 17);
3490 lxc_list_for_each(it
, &conf
->network
) {
3491 struct lxc_netdev
*n
= it
->elem
;
3493 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3494 memcpy(n
->hwaddr
, newhwaddr
, 17);
3503 static int set_config_ephemeral(const char *key
, const char *value
,
3504 struct lxc_conf
*lxc_conf
, void *data
)
3506 if (lxc_config_value_empty(value
)) {
3507 lxc_conf
->ephemeral
= 0;
3511 if (lxc_safe_uint(value
, &lxc_conf
->ephemeral
) < 0)
3514 if (lxc_conf
->ephemeral
> 1)
3520 static int set_config_log_syslog(const char *key
, const char *value
,
3521 struct lxc_conf
*lxc_conf
, void *data
)
3525 if (lxc_conf
->syslog
) {
3526 free(lxc_conf
->syslog
);
3527 lxc_conf
->syslog
= NULL
;
3530 if (lxc_config_value_empty(value
))
3533 facility
= lxc_syslog_priority_to_int(value
);
3534 if (facility
== -EINVAL
)
3537 lxc_log_syslog(facility
);
3539 return set_config_string_item(&lxc_conf
->syslog
, value
);
3542 static int set_config_no_new_privs(const char *key
, const char *value
,
3543 struct lxc_conf
*lxc_conf
, void *data
)
3547 if (lxc_config_value_empty(value
)) {
3548 lxc_conf
->no_new_privs
= false;
3552 if (lxc_safe_uint(value
, &v
) < 0)
3558 lxc_conf
->no_new_privs
= v
? true : false;
3563 /* Callbacks to get configuration items. */
3564 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3565 struct lxc_conf
*c
, void *data
)
3572 memset(retv
, 0, inlen
);
3574 #if HAVE_SYS_PERSONALITY_H
3577 switch (c
->personality
) {
3579 strprint(retv
, inlen
, "i686");
3582 strprint(retv
, inlen
, "x86_64");
3592 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3593 struct lxc_conf
*c
, void *data
)
3595 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3598 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3599 struct lxc_conf
*c
, void *data
)
3601 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3604 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3605 struct lxc_conf
*c
, void *data
)
3607 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3610 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3611 struct lxc_conf
*c
, void *data
)
3613 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3616 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3617 int inlen
, struct lxc_conf
*c
,
3620 return lxc_get_conf_int(c
, retv
, inlen
,
3621 c
->lsm_aa_allow_incomplete
);
3624 static int get_config_apparmor_allow_nesting(const char *key
, char *retv
,
3625 int inlen
, struct lxc_conf
*c
,
3628 return lxc_get_conf_int(c
, retv
, inlen
,
3629 c
->lsm_aa_allow_nesting
);
3632 static int get_config_apparmor_raw(const char *key
, char *retv
,
3633 int inlen
, struct lxc_conf
*c
,
3637 struct lxc_list
*it
;
3643 memset(retv
, 0, inlen
);
3645 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3646 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3652 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3653 struct lxc_conf
*c
, void *data
)
3655 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3658 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3659 struct lxc_conf
*c
, void *data
)
3661 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3664 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3665 struct lxc_conf
*c
, void *data
)
3667 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3671 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3672 * just the value(s) will be printed. Since there still could be more than one,
3673 * it is newline-separated.
3674 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3676 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3677 * 'lxc.cgroup.subsystem.key = value' format.
3679 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3680 int inlen
, struct lxc_conf
*c
,
3684 size_t namespaced_token_len
;
3685 char *global_token
, *namespaced_token
;
3686 struct lxc_list
*it
;
3688 bool get_all
= false;
3693 memset(retv
, 0, inlen
);
3695 if (version
== CGROUP2_SUPER_MAGIC
) {
3696 global_token
= "lxc.cgroup2";
3697 namespaced_token
= "lxc.cgroup2.";
3698 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3699 } else if (version
== CGROUP_SUPER_MAGIC
) {
3700 global_token
= "lxc.cgroup";
3701 namespaced_token
= "lxc.cgroup.";
3702 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3707 if (strcmp(key
, global_token
) == 0)
3709 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3710 key
+= namespaced_token_len
;
3714 lxc_list_for_each(it
, &c
->cgroup
) {
3715 struct lxc_cgroup
*cg
= it
->elem
;
3718 if (version
!= cg
->version
)
3721 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3722 cg
->subsystem
, cg
->value
);
3723 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3724 strprint(retv
, inlen
, "%s\n", cg
->value
);
3731 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3732 struct lxc_conf
*c
, void *data
)
3734 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3735 CGROUP_SUPER_MAGIC
);
3738 static int get_config_cgroup2_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 CGROUP2_SUPER_MAGIC
);
3745 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3746 struct lxc_conf
*lxc_conf
, void *data
)
3754 memset(retv
, 0, inlen
);
3756 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3761 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3762 struct lxc_conf
*lxc_conf
, void *data
)
3770 memset(retv
, 0, inlen
);
3772 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3777 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3778 struct lxc_conf
*lxc_conf
, void *data
)
3786 memset(retv
, 0, inlen
);
3788 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3793 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3795 struct lxc_conf
*lxc_conf
,
3804 memset(retv
, 0, inlen
);
3806 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3811 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3813 struct lxc_conf
*lxc_conf
,
3822 memset(retv
, 0, inlen
);
3824 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3829 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3830 int inlen
, struct lxc_conf
*lxc_conf
,
3833 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3834 lxc_conf
->cgroup_meta
.relative
);
3837 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3838 struct lxc_conf
*c
, void *data
)
3840 struct lxc_list
*it
;
3841 int len
, listlen
, ret
;
3843 /* "u 1000 1000000 65536"
3845 * let's render this as
3863 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3864 char buf
[__LXC_IDMAP_STR_BUF
];
3869 memset(retv
, 0, inlen
);
3871 listlen
= lxc_list_len(&c
->id_map
);
3872 lxc_list_for_each(it
, &c
->id_map
) {
3873 struct id_map
*map
= it
->elem
;
3874 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3875 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3876 map
->nsid
, map
->hostid
, map
->range
);
3877 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3880 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3886 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3887 struct lxc_conf
*c
, void *data
)
3890 v
= lxc_log_priority_to_string(c
->loglevel
);
3891 return lxc_get_conf_str(retv
, inlen
, v
);
3894 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3895 struct lxc_conf
*c
, void *data
)
3897 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3900 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3901 struct lxc_conf
*c
, void *data
)
3903 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3906 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3907 struct lxc_conf
*c
, void *data
)
3909 int len
, fulllen
= 0;
3910 const char *sep
= "";
3915 memset(retv
, 0, inlen
);
3917 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3920 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3921 case LXC_AUTO_PROC_MIXED
:
3922 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3925 case LXC_AUTO_PROC_RW
:
3926 strprint(retv
, inlen
, "%sproc:rw", sep
);
3933 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3934 case LXC_AUTO_SYS_RO
:
3935 strprint(retv
, inlen
, "%ssys:ro", sep
);
3938 case LXC_AUTO_SYS_RW
:
3939 strprint(retv
, inlen
, "%ssys:rw", sep
);
3942 case LXC_AUTO_SYS_MIXED
:
3943 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3950 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3951 case LXC_AUTO_CGROUP_NOSPEC
:
3952 strprint(retv
, inlen
, "%scgroup", sep
);
3954 case LXC_AUTO_CGROUP_MIXED
:
3955 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3957 case LXC_AUTO_CGROUP_RO
:
3958 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3960 case LXC_AUTO_CGROUP_RW
:
3961 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3963 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3964 strprint(retv
, inlen
, "%scgroup-full", sep
);
3966 case LXC_AUTO_CGROUP_FULL_MIXED
:
3967 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3969 case LXC_AUTO_CGROUP_FULL_RO
:
3970 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3972 case LXC_AUTO_CGROUP_FULL_RW
:
3973 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3982 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3983 struct lxc_conf
*c
, void *data
)
3985 int len
, fulllen
= 0;
3986 struct lxc_list
*it
;
3991 memset(retv
, 0, inlen
);
3993 lxc_list_for_each(it
, &c
->mount_list
) {
3994 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4000 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
4001 struct lxc_conf
*c
, void *data
)
4003 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4006 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4007 struct lxc_conf
*c
, void *data
)
4009 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4012 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4013 struct lxc_conf
*c
, void *data
)
4015 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4018 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4019 struct lxc_conf
*c
, void *data
)
4021 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
4024 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4025 struct lxc_conf
*c
, void *data
)
4027 return lxc_get_conf_str(
4029 c
->utsname
? c
->utsname
->nodename
: NULL
);
4032 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4033 struct lxc_conf
*c
, void *data
)
4036 int len
, fulllen
= 0, found
= -1;
4037 struct lxc_list
*it
;
4040 subkey
= strchr(key
, '.');
4044 subkey
= strchr(subkey
+ 1, '.');
4048 if (*subkey
== '\0')
4051 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4052 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
4064 memset(retv
, 0, inlen
);
4066 lxc_list_for_each(it
, &c
->hooks
[found
]) {
4067 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4073 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4074 struct lxc_conf
*c
, void *data
)
4076 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4079 static int get_config_net(const char *key
, char *retv
, int inlen
,
4080 struct lxc_conf
*c
, void *data
)
4082 int len
, fulllen
= 0;
4083 struct lxc_list
*it
;
4088 memset(retv
, 0, inlen
);
4090 lxc_list_for_each(it
, &c
->network
) {
4091 struct lxc_netdev
*n
= it
->elem
;
4092 const char *t
= lxc_net_type_to_str(n
->type
);
4093 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4099 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4100 struct lxc_conf
*c
, void *data
)
4102 int len
, fulllen
= 0;
4103 struct lxc_list
*it
;
4108 memset(retv
, 0, inlen
);
4110 lxc_list_for_each(it
, &c
->caps
) {
4111 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4117 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4118 struct lxc_conf
*c
, void *data
)
4120 int len
, fulllen
= 0;
4121 struct lxc_list
*it
;
4126 memset(retv
, 0, inlen
);
4128 lxc_list_for_each(it
, &c
->keepcaps
) {
4129 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4135 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4136 struct lxc_conf
*c
, void *data
)
4138 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4141 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4142 struct lxc_conf
*c
, void *data
)
4144 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4147 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4148 struct lxc_conf
*c
, void *data
)
4150 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4154 static int get_config_console_buffer_size(const char *key
, char *retv
,
4155 int inlen
, struct lxc_conf
*c
,
4158 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4161 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4162 struct lxc_conf
*c
, void *data
)
4164 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4167 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4168 int inlen
, struct lxc_conf
*c
,
4172 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4179 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4180 struct lxc_conf
*c
, void *data
)
4182 #ifdef HAVE_SECCOMP_NOTIFY
4183 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4185 return ret_set_errno(-1, ENOSYS
);
4189 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4190 struct lxc_conf
*c
, void *data
)
4192 #ifdef HAVE_SECCOMP_NOTIFY
4193 return lxc_get_conf_str(retv
, inlen
,
4194 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4195 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4196 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4198 return ret_set_errno(-1, ENOSYS
);
4202 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4203 struct lxc_conf
*c
, void *data
)
4205 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4208 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4209 struct lxc_conf
*c
, void *data
)
4211 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4214 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4215 struct lxc_conf
*c
, void *data
)
4217 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4220 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4221 struct lxc_conf
*c
, void *data
)
4223 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4226 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4227 struct lxc_conf
*c
, void *data
)
4229 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4232 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4233 struct lxc_conf
*c
, void *data
)
4235 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4238 static int get_config_start(const char *key
, char *retv
, int inlen
,
4239 struct lxc_conf
*c
, void *data
)
4241 if (strcmp(key
+ 10, "auto") == 0)
4242 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4243 else if (strcmp(key
+ 10, "delay") == 0)
4244 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4245 else if (strcmp(key
+ 10, "order") == 0)
4246 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4251 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4252 struct lxc_conf
*c
, void *data
)
4254 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4257 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4258 struct lxc_conf
*c
, void *data
)
4260 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4263 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4264 int inlen
, struct lxc_conf
*c
,
4267 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4270 static int get_config_group(const char *key
, char *retv
, int inlen
,
4271 struct lxc_conf
*c
, void *data
)
4273 int len
, fulllen
= 0;
4274 struct lxc_list
*it
;
4279 memset(retv
, 0, inlen
);
4281 lxc_list_for_each(it
, &c
->groups
) {
4282 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4288 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4289 struct lxc_conf
*c
, void *data
)
4291 int len
, fulllen
= 0;
4292 struct lxc_list
*it
;
4297 memset(retv
, 0, inlen
);
4299 lxc_list_for_each(it
, &c
->environment
) {
4300 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4306 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4307 struct lxc_conf
*c
, void *data
)
4309 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4312 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4313 struct lxc_conf
*c
, void *data
)
4315 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4318 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4319 struct lxc_conf
*c
, void *data
)
4321 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4324 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4325 struct lxc_conf
*c
, void *data
)
4327 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4330 static int get_config_init_gid(const char *key
, char *retv
, int inlen
,
4331 struct lxc_conf
*c
, void *data
)
4333 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_gid
);
4336 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4337 struct lxc_conf
*c
, void *data
)
4339 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4342 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4343 struct lxc_conf
*c
, void *data
)
4345 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4348 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4349 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4350 * printed, in 'lxc.prlimit.resource = value' format.
4352 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4353 struct lxc_conf
*c
, void *data
)
4355 int fulllen
= 0, len
;
4356 bool get_all
= false;
4357 struct lxc_list
*it
;
4362 memset(retv
, 0, inlen
);
4364 if (!strcmp(key
, "lxc.prlimit"))
4366 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
4371 lxc_list_for_each(it
, &c
->limits
) {
4372 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4373 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4375 struct lxc_limit
*lim
= it
->elem
;
4377 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4378 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4379 partlen
= STRLITERALLEN("unlimited");
4381 partlen
= sprintf(buf
, "%" PRIu64
,
4382 (uint64_t)lim
->limit
.rlim_cur
);
4385 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4386 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4387 memcpy(buf
+ partlen
, ":unlimited",
4388 STRLITERALLEN(":unlimited") + 1);
4390 sprintf(buf
+ partlen
, ":%" PRIu64
,
4391 (uint64_t)lim
->limit
.rlim_max
);
4395 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4396 lim
->resource
, buf
);
4397 } else if (strcmp(lim
->resource
, key
) == 0) {
4398 strprint(retv
, inlen
, "%s", buf
);
4405 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4406 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4407 * entries will be printed, in 'lxc.sysctl.key = value' format.
4409 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4410 struct lxc_conf
*c
, void *data
)
4413 struct lxc_list
*it
;
4415 bool get_all
= false;
4420 memset(retv
, 0, inlen
);
4422 if (strcmp(key
, "lxc.sysctl") == 0)
4424 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4425 key
+= STRLITERALLEN("lxc.sysctl.");
4429 lxc_list_for_each(it
, &c
->sysctls
) {
4430 struct lxc_sysctl
*elem
= it
->elem
;
4432 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
4434 } else if (strcmp(elem
->key
, key
) == 0) {
4435 strprint(retv
, inlen
, "%s", elem
->value
);
4442 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4443 struct lxc_conf
*c
, void *data
)
4445 struct lxc_list
*it
;
4448 bool get_all
= false;
4453 memset(retv
, 0, inlen
);
4455 if (strcmp(key
, "lxc.proc") == 0)
4457 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4458 key
+= STRLITERALLEN("lxc.proc.");
4462 lxc_list_for_each(it
, &c
->procs
) {
4463 struct lxc_proc
*proc
= it
->elem
;
4466 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4467 proc
->filename
, proc
->value
);
4468 } else if (strcmp(proc
->filename
, key
) == 0) {
4469 strprint(retv
, inlen
, "%s", proc
->value
);
4476 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4477 struct lxc_conf
*c
, void *data
)
4485 memset(retv
, 0, inlen
);
4487 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4488 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4489 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4495 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4496 struct lxc_conf
*c
, void *data
)
4504 memset(retv
, 0, inlen
);
4506 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4507 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4508 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4514 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4523 memset(retv
, 0, inlen
);
4525 if (c
->timens
.s_boot
) {
4526 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4528 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4534 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4535 struct lxc_conf
*c
, void *data
)
4543 memset(retv
, 0, inlen
);
4545 if (c
->timens
.s_monotonic
) {
4546 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4548 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4554 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4555 struct lxc_conf
*c
, void *data
)
4558 const char *namespace;
4564 memset(retv
, 0, inlen
);
4566 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4567 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4571 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4576 /* Callbacks to clear config items. */
4577 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4580 c
->personality
= -1;
4584 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4591 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4598 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4606 static inline int clr_config_apparmor_profile(const char *key
,
4607 struct lxc_conf
*c
, void *data
)
4609 free(c
->lsm_aa_profile
);
4610 c
->lsm_aa_profile
= NULL
;
4614 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4618 c
->lsm_aa_allow_incomplete
= 0;
4622 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4626 c
->lsm_aa_allow_nesting
= 0;
4630 static inline int clr_config_apparmor_raw(const char *key
,
4634 return lxc_clear_apparmor_raw(c
);
4637 static inline int clr_config_selinux_context(const char *key
,
4638 struct lxc_conf
*c
, void *data
)
4640 free(c
->lsm_se_context
);
4641 c
->lsm_se_context
= NULL
;
4645 static inline int clr_config_selinux_context_keyring(const char *key
,
4646 struct lxc_conf
*c
, void *data
)
4648 free(c
->lsm_se_keyring_context
);
4649 c
->lsm_se_keyring_context
= NULL
;
4653 static inline int clr_config_keyring_session(const char *key
,
4654 struct lxc_conf
*c
, void *data
)
4656 c
->keyring_disable_session
= false;
4660 static inline int clr_config_cgroup_controller(const char *key
,
4661 struct lxc_conf
*c
, void *data
)
4663 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4666 static inline int clr_config_cgroup2_controller(const char *key
,
4667 struct lxc_conf
*c
, void *data
)
4669 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4672 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4675 if (lxc_conf
->cgroup_meta
.dir
) {
4676 free(lxc_conf
->cgroup_meta
.dir
);
4677 lxc_conf
->cgroup_meta
.dir
= NULL
;
4683 static int clr_config_cgroup_monitor_dir(const char *key
,
4684 struct lxc_conf
*lxc_conf
,
4687 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4691 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4692 struct lxc_conf
*lxc_conf
,
4695 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4699 static int clr_config_cgroup_container_dir(const char *key
,
4700 struct lxc_conf
*lxc_conf
,
4703 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4707 static int clr_config_cgroup_container_inner_dir(const char *key
,
4708 struct lxc_conf
*lxc_conf
,
4711 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4715 static inline int clr_config_cgroup_relative(const char *key
,
4716 struct lxc_conf
*lxc_conf
,
4719 lxc_conf
->cgroup_meta
.relative
= false;
4723 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4726 return lxc_clear_idmaps(c
);
4729 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4732 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4736 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4744 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4747 return lxc_clear_mount_entries(c
);
4750 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4753 return lxc_clear_automounts(c
);
4756 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4764 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4767 free(c
->rootfs
.path
);
4768 c
->rootfs
.path
= NULL
;
4772 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4775 c
->rootfs
.managed
= true;
4779 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4782 free(c
->rootfs
.mount
);
4783 c
->rootfs
.mount
= NULL
;
4787 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4790 free(c
->rootfs
.options
);
4791 c
->rootfs
.options
= NULL
;
4793 free(c
->rootfs
.data
);
4794 c
->rootfs
.data
= NULL
;
4799 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4807 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4810 return lxc_clear_hooks(c
, key
);
4813 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4816 /* default to legacy hooks version */
4817 c
->hooks_version
= 0;
4821 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4824 lxc_free_networks(&c
->network
);
4829 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4832 return lxc_clear_config_caps(c
);
4835 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4838 return lxc_clear_config_keepcaps(c
);
4841 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4844 free(c
->console
.path
);
4845 c
->console
.path
= NULL
;
4849 static inline int clr_config_console_logfile(const char *key
,
4850 struct lxc_conf
*c
, void *data
)
4852 free(c
->console
.log_path
);
4853 c
->console
.log_path
= NULL
;
4857 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4860 c
->console
.log_rotate
= 0;
4864 static inline int clr_config_console_buffer_size(const char *key
,
4865 struct lxc_conf
*c
, void *data
)
4867 c
->console
.buffer_size
= 0;
4871 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4874 c
->console
.log_size
= 0;
4878 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4879 struct lxc_conf
*c
, void *data
)
4882 c
->seccomp
.allow_nesting
= 0;
4890 static inline int clr_config_seccomp_notify_cookie(const char *key
,
4891 struct lxc_conf
*c
, void *data
)
4893 #ifdef HAVE_SECCOMP_NOTIFY
4894 free(c
->seccomp
.notifier
.cookie
);
4895 c
->seccomp
.notifier
.cookie
= NULL
;
4898 return ret_set_errno(-1, ENOSYS
);
4902 static inline int clr_config_seccomp_notify_proxy(const char *key
,
4903 struct lxc_conf
*c
, void *data
)
4905 #ifdef HAVE_SECCOMP_NOTIFY
4906 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
4907 sizeof(c
->seccomp
.notifier
.proxy_addr
));
4910 return ret_set_errno(-1, ENOSYS
);
4914 static inline int clr_config_seccomp_profile(const char *key
,
4915 struct lxc_conf
*c
, void *data
)
4917 free(c
->seccomp
.seccomp
);
4918 c
->seccomp
.seccomp
= NULL
;
4922 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4929 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
4932 c
->autodevtmpfssize
= 500000;
4936 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4943 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4946 c
->rebootsignal
= 0;
4950 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4957 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4960 if (strcmp(key
+ 10, "auto") == 0)
4962 else if (strcmp(key
+ 10, "delay") == 0)
4964 else if (strcmp(key
+ 10, "order") == 0)
4970 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4978 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4981 c
->monitor_unshare
= 0;
4985 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4986 struct lxc_conf
*c
, void *data
)
4988 c
->monitor_signal_pdeath
= 0;
4992 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4995 return lxc_clear_groups(c
);
4998 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
5001 return lxc_clear_environment(c
);
5004 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5007 free(c
->execute_cmd
);
5008 c
->execute_cmd
= NULL
;
5012 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5020 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5028 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5035 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5042 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5049 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5052 c
->no_new_privs
= false;
5056 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5059 return lxc_clear_limits(c
, key
);
5062 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5065 return lxc_clear_sysctls(c
, key
);
5068 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5071 return lxc_clear_procs(c
, key
);
5074 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5077 lxc_clear_includes(c
);
5081 static int clr_config_namespace_clone(const char *key
,
5082 struct lxc_conf
*lxc_conf
, void *data
)
5084 lxc_conf
->ns_clone
= 0;
5088 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5091 lxc_conf
->ns_keep
= 0;
5095 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5097 lxc_conf
->timens
.s_boot
= 0;
5098 lxc_conf
->timens
.ns_boot
= 0;
5102 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5104 lxc_conf
->timens
.s_monotonic
= 0;
5105 lxc_conf
->timens
.ns_monotonic
= 0;
5109 static int clr_config_namespace_share(const char *key
,
5110 struct lxc_conf
*lxc_conf
, void *data
)
5113 const char *namespace;
5115 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5116 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5120 free(lxc_conf
->ns_share
[ns_idx
]);
5121 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5126 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5127 struct lxc_conf
*c
, void *data
)
5132 static struct lxc_config_t
*get_network_config_ops(const char *key
,
5133 struct lxc_conf
*lxc_conf
,
5135 char **deindexed_key
)
5138 unsigned int tmpidx
;
5140 char *copy
, *idx_start
, *idx_end
;
5141 struct lxc_config_t
*config
= NULL
;
5143 /* check that this is a sensible network key */
5144 if (strncmp("lxc.net.", key
, 8)) {
5145 ERROR("Invalid network configuration key \"%s\"", key
);
5151 ERROR("Failed to duplicate string \"%s\"", key
);
5156 if (!isdigit(*(key
+ 8))) {
5157 ERROR("Failed to detect digit in string \"%s\"", key
+ 8);
5161 /* beginning of index string */
5162 idx_start
= (copy
+ 7);
5165 /* end of index string */
5166 idx_end
= strchr((copy
+ 8), '.');
5170 /* parse current index */
5171 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
5174 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
5180 /* This, of course is utterly nonsensical on so many levels, but
5181 * better safe than sorry.
5182 * (Checking for INT_MAX here is intentional.)
5184 if (tmpidx
== INT_MAX
) {
5185 SYSERROR("Number of configured networks would overflow the "
5191 numstrlen
= strlen((idx_start
+ 1));
5193 /* repair configuration key */
5196 /* lxc.net.<idx>.<subkey> */
5199 if (strlen(idx_end
+ 1) == 0) {
5200 ERROR("No subkey in network configuration key \"%s\"", key
);
5204 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
5205 copy
[strlen(key
) - (numstrlen
+ 1)] = '\0';
5207 config
= lxc_get_config(copy
);
5209 ERROR("Unknown network configuration key \"%s\"", key
);
5215 *deindexed_key
= copy
;
5224 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5225 * found. So we make sure next comes an integer, find the right callback (by
5226 * rewriting the key), and call it.
5228 static int set_config_net_nic(const char *key
, const char *value
,
5229 struct lxc_conf
*lxc_conf
, void *data
)
5232 const char *idxstring
;
5233 struct lxc_config_t
*config
;
5234 struct lxc_netdev
*netdev
;
5236 char *deindexed_key
= NULL
;
5238 idxstring
= key
+ 8;
5239 if (!isdigit(*idxstring
))
5242 if (lxc_config_value_empty(value
))
5243 return clr_config_net_nic(key
, lxc_conf
, data
);
5245 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5246 if (!config
|| idx
< 0)
5249 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
5251 free(deindexed_key
);
5255 ret
= config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
5256 free(deindexed_key
);
5261 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
5265 const char *idxstring
;
5266 struct lxc_config_t
*config
;
5267 struct lxc_netdev
*netdev
;
5269 char *deindexed_key
= NULL
;
5271 idxstring
= key
+ 8;
5272 if (!isdigit(*idxstring
))
5275 /* The left conjunct is pretty self-explanatory. The right conjunct
5276 * checks whether the two pointers are equal. If they are we know that
5277 * this is not a key that is namespaced any further and so we are
5278 * supposed to clear the whole network.
5280 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5281 unsigned int rmnetdevidx
;
5283 if (lxc_safe_uint(idxstring
, &rmnetdevidx
) < 0)
5286 /* Remove network from network list. */
5287 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5291 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5292 if (!config
|| idx
< 0)
5295 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
5297 free(deindexed_key
);
5301 ret
= config
->clr(deindexed_key
, lxc_conf
, netdev
);
5302 free(deindexed_key
);
5307 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5310 struct lxc_netdev
*netdev
= data
;
5320 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5323 struct lxc_netdev
*netdev
= data
;
5328 netdev
->name
[0] = '\0';
5333 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5336 struct lxc_netdev
*netdev
= data
;
5346 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5349 struct lxc_netdev
*netdev
= data
;
5354 netdev
->link
[0] = '\0';
5359 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5362 struct lxc_netdev
*netdev
= data
;
5365 return ret_set_errno(-1, EINVAL
);
5367 netdev
->l2proxy
= false;
5372 static int clr_config_net_macvlan_mode(const char *key
,
5373 struct lxc_conf
*lxc_conf
, void *data
)
5375 struct lxc_netdev
*netdev
= data
;
5380 if (netdev
->type
!= LXC_NET_MACVLAN
)
5383 netdev
->priv
.macvlan_attr
.mode
= -1;
5388 static int clr_config_net_ipvlan_mode(const char *key
,
5389 struct lxc_conf
*lxc_conf
, void *data
)
5391 struct lxc_netdev
*netdev
= data
;
5394 return ret_set_errno(-1, EINVAL
);
5396 if (netdev
->type
!= LXC_NET_IPVLAN
)
5399 netdev
->priv
.ipvlan_attr
.mode
= -1;
5404 static int clr_config_net_ipvlan_isolation(const char *key
,
5405 struct lxc_conf
*lxc_conf
, void *data
)
5407 struct lxc_netdev
*netdev
= data
;
5410 return ret_set_errno(-1, EINVAL
);
5412 if (netdev
->type
!= LXC_NET_IPVLAN
)
5415 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5420 static int clr_config_net_veth_mode(const char *key
,
5421 struct lxc_conf
*lxc_conf
, void *data
)
5423 struct lxc_netdev
*netdev
= data
;
5426 return ret_set_errno(-1, EINVAL
);
5428 if (netdev
->type
!= LXC_NET_VETH
)
5431 netdev
->priv
.veth_attr
.mode
= -1;
5436 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5439 struct lxc_netdev
*netdev
= data
;
5444 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5449 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5452 struct lxc_netdev
*netdev
= data
;
5455 return ret_errno(EINVAL
);
5457 netdev
->priv
.veth_attr
.vlan_id
= 0;
5458 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5463 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5464 struct lxc_conf
*lxc_conf
, void *data
)
5466 struct lxc_netdev
*netdev
= data
;
5467 struct lxc_list
*cur
, *next
;
5470 return ret_errno(EINVAL
);
5472 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5481 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5484 struct lxc_netdev
*netdev
= data
;
5489 free(netdev
->upscript
);
5490 netdev
->upscript
= NULL
;
5495 static int clr_config_net_script_down(const char *key
,
5496 struct lxc_conf
*lxc_conf
, void *data
)
5498 struct lxc_netdev
*netdev
= data
;
5503 free(netdev
->downscript
);
5504 netdev
->downscript
= NULL
;
5509 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5512 struct lxc_netdev
*netdev
= data
;
5517 free(netdev
->hwaddr
);
5518 netdev
->hwaddr
= NULL
;
5523 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5526 struct lxc_netdev
*netdev
= data
;
5537 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5540 struct lxc_netdev
*netdev
= data
;
5545 netdev
->priv
.vlan_attr
.vid
= 0;
5550 static int clr_config_net_ipv4_gateway(const char *key
,
5551 struct lxc_conf
*lxc_conf
, void *data
)
5553 struct lxc_netdev
*netdev
= data
;
5558 free(netdev
->ipv4_gateway
);
5559 netdev
->ipv4_gateway
= NULL
;
5564 static int clr_config_net_ipv4_address(const char *key
,
5565 struct lxc_conf
*lxc_conf
, void *data
)
5567 struct lxc_netdev
*netdev
= data
;
5568 struct lxc_list
*cur
, *next
;
5573 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
5582 static int clr_config_net_veth_ipv4_route(const char *key
,
5583 struct lxc_conf
*lxc_conf
, void *data
)
5585 struct lxc_netdev
*netdev
= data
;
5586 struct lxc_list
*cur
, *next
;
5591 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv4_routes
, next
) {
5600 static int clr_config_net_ipv6_gateway(const char *key
,
5601 struct lxc_conf
*lxc_conf
, void *data
)
5603 struct lxc_netdev
*netdev
= data
;
5608 free(netdev
->ipv6_gateway
);
5609 netdev
->ipv6_gateway
= NULL
;
5614 static int clr_config_net_ipv6_address(const char *key
,
5615 struct lxc_conf
*lxc_conf
, void *data
)
5617 struct lxc_netdev
*netdev
= data
;
5618 struct lxc_list
*cur
, *next
;
5623 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
5632 static int clr_config_net_veth_ipv6_route(const char *key
,
5633 struct lxc_conf
*lxc_conf
, void *data
)
5635 struct lxc_netdev
*netdev
= data
;
5636 struct lxc_list
*cur
, *next
;
5641 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv6_routes
, next
) {
5650 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
5651 struct lxc_conf
*c
, void *data
)
5654 const char *idxstring
;
5655 struct lxc_config_t
*config
;
5656 struct lxc_netdev
*netdev
;
5658 char *deindexed_key
= NULL
;
5660 idxstring
= key
+ 8;
5661 if (!isdigit(*idxstring
))
5664 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
5665 if (!config
|| idx
< 0)
5668 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5670 free(deindexed_key
);
5674 ret
= config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
5675 free(deindexed_key
);
5680 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5681 struct lxc_conf
*c
, void *data
)
5685 struct lxc_netdev
*netdev
= data
;
5690 memset(retv
, 0, inlen
);
5695 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5700 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5701 struct lxc_conf
*c
, void *data
)
5705 struct lxc_netdev
*netdev
= data
;
5710 memset(retv
, 0, inlen
);
5715 if (netdev
->flags
& IFF_UP
)
5716 strprint(retv
, inlen
, "up");
5721 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5722 struct lxc_conf
*c
, void *data
)
5726 struct lxc_netdev
*netdev
= data
;
5731 memset(retv
, 0, inlen
);
5736 if (netdev
->link
[0] != '\0')
5737 strprint(retv
, inlen
, "%s", netdev
->link
);
5742 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5743 struct lxc_conf
*c
, void *data
)
5745 struct lxc_netdev
*netdev
= data
;
5746 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5749 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5750 struct lxc_conf
*c
, void *data
)
5754 struct lxc_netdev
*netdev
= data
;
5759 memset(retv
, 0, inlen
);
5764 if (netdev
->name
[0] != '\0')
5765 strprint(retv
, inlen
, "%s", netdev
->name
);
5770 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5771 struct lxc_conf
*c
, void *data
)
5776 struct lxc_netdev
*netdev
= data
;
5781 memset(retv
, 0, inlen
);
5786 if (netdev
->type
!= LXC_NET_MACVLAN
)
5789 switch (netdev
->priv
.macvlan_attr
.mode
) {
5790 case MACVLAN_MODE_PRIVATE
:
5793 case MACVLAN_MODE_VEPA
:
5796 case MACVLAN_MODE_BRIDGE
:
5799 case MACVLAN_MODE_PASSTHRU
:
5807 strprint(retv
, inlen
, "%s", mode
);
5812 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5813 struct lxc_conf
*c
, void *data
)
5818 struct lxc_netdev
*netdev
= data
;
5823 memset(retv
, 0, inlen
);
5826 return ret_set_errno(-1, EINVAL
);
5828 if (netdev
->type
!= LXC_NET_IPVLAN
)
5831 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5832 case IPVLAN_MODE_L3
:
5835 case IPVLAN_MODE_L3S
:
5838 case IPVLAN_MODE_L2
:
5846 strprint(retv
, inlen
, "%s", mode
);
5851 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5852 struct lxc_conf
*c
, void *data
)
5857 struct lxc_netdev
*netdev
= data
;
5862 memset(retv
, 0, inlen
);
5865 return ret_set_errno(-1, EINVAL
);
5867 if (netdev
->type
!= LXC_NET_IPVLAN
)
5870 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
5871 case IPVLAN_ISOLATION_BRIDGE
:
5874 case IPVLAN_ISOLATION_PRIVATE
:
5877 case IPVLAN_ISOLATION_VEPA
:
5885 strprint(retv
, inlen
, "%s", mode
);
5890 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
5891 struct lxc_conf
*c
, void *data
)
5896 struct lxc_netdev
*netdev
= data
;
5901 memset(retv
, 0, inlen
);
5904 return ret_set_errno(-1, EINVAL
);
5906 if (netdev
->type
!= LXC_NET_VETH
)
5909 switch (netdev
->priv
.veth_attr
.mode
) {
5910 case VETH_MODE_BRIDGE
:
5913 case VETH_MODE_ROUTER
:
5921 strprint(retv
, inlen
, "%s", mode
);
5926 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
5927 struct lxc_conf
*c
, void *data
)
5931 struct lxc_netdev
*netdev
= data
;
5936 memset(retv
, 0, inlen
);
5941 if (netdev
->type
!= LXC_NET_VETH
)
5944 strprint(retv
, inlen
, "%s",
5945 netdev
->priv
.veth_attr
.pair
[0] != '\0'
5946 ? netdev
->priv
.veth_attr
.pair
5947 : netdev
->priv
.veth_attr
.veth1
);
5952 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
5953 struct lxc_conf
*c
, void *data
)
5957 struct lxc_netdev
*netdev
= data
;
5960 return ret_errno(EINVAL
);
5962 if (netdev
->type
!= LXC_NET_VETH
)
5968 memset(retv
, 0, inlen
);
5970 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
5975 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
, int inlen
,
5976 struct lxc_conf
*c
, void *data
)
5980 struct lxc_list
*it
;
5982 struct lxc_netdev
*netdev
= data
;
5985 return ret_errno(EINVAL
);
5987 if (netdev
->type
!= LXC_NET_VETH
)
5993 memset(retv
, 0, inlen
);
5995 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
5997 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
5998 unsigned short i
= PTR_TO_USHORT(it
->elem
);
5999 strprint(retv
, inlen
, "%u%s", i
,
6000 (listlen
-- > 1) ? "\n" : "");
6006 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
6007 struct lxc_conf
*c
, void *data
)
6011 struct lxc_netdev
*netdev
= data
;
6016 memset(retv
, 0, inlen
);
6021 if (netdev
->upscript
)
6022 strprint(retv
, inlen
, "%s", netdev
->upscript
);
6027 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
6028 struct lxc_conf
*c
, void *data
)
6032 struct lxc_netdev
*netdev
= data
;
6037 memset(retv
, 0, inlen
);
6042 if (netdev
->downscript
)
6043 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6048 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6049 struct lxc_conf
*c
, void *data
)
6053 struct lxc_netdev
*netdev
= data
;
6058 memset(retv
, 0, inlen
);
6064 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6069 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6070 struct lxc_conf
*c
, void *data
)
6074 struct lxc_netdev
*netdev
= data
;
6079 memset(retv
, 0, inlen
);
6085 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6090 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6091 struct lxc_conf
*c
, void *data
)
6095 struct lxc_netdev
*netdev
= data
;
6100 memset(retv
, 0, inlen
);
6105 if (netdev
->type
!= LXC_NET_VLAN
)
6108 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6113 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6114 struct lxc_conf
*c
, void *data
)
6117 char buf
[INET_ADDRSTRLEN
];
6119 struct lxc_netdev
*netdev
= data
;
6124 memset(retv
, 0, inlen
);
6129 if (netdev
->ipv4_gateway_auto
) {
6130 strprint(retv
, inlen
, "auto");
6131 } else if (netdev
->ipv4_gateway_dev
) {
6132 strprint(retv
, inlen
, "dev");
6133 } else if (netdev
->ipv4_gateway
) {
6134 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
6135 strprint(retv
, inlen
, "%s", buf
);
6141 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6142 struct lxc_conf
*c
, void *data
)
6146 char buf
[INET_ADDRSTRLEN
];
6147 struct lxc_list
*it
;
6149 struct lxc_netdev
*netdev
= data
;
6154 memset(retv
, 0, inlen
);
6159 listlen
= lxc_list_len(&netdev
->ipv4
);
6161 lxc_list_for_each(it
, &netdev
->ipv4
) {
6162 struct lxc_inetdev
*i
= it
->elem
;
6163 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
6164 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6165 (listlen
-- > 1) ? "\n" : "");
6171 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6172 struct lxc_conf
*c
, void *data
)
6176 char buf
[INET_ADDRSTRLEN
];
6177 struct lxc_list
*it
;
6179 struct lxc_netdev
*netdev
= data
;
6184 memset(retv
, 0, inlen
);
6187 return ret_set_errno(-1, EINVAL
);
6189 if (netdev
->type
!= LXC_NET_VETH
)
6192 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
6194 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv4_routes
) {
6195 struct lxc_inetdev
*i
= it
->elem
;
6196 inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
));
6197 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6198 (listlen
-- > 1) ? "\n" : "");
6204 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6205 struct lxc_conf
*c
, void *data
)
6208 char buf
[INET6_ADDRSTRLEN
];
6210 struct lxc_netdev
*netdev
= data
;
6215 memset(retv
, 0, inlen
);
6220 if (netdev
->ipv6_gateway_auto
) {
6221 strprint(retv
, inlen
, "auto");
6222 } else if (netdev
->ipv6_gateway_dev
) {
6223 strprint(retv
, inlen
, "dev");
6224 } else if (netdev
->ipv6_gateway
) {
6225 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
6226 strprint(retv
, inlen
, "%s", buf
);
6232 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6233 struct lxc_conf
*c
, void *data
)
6237 char buf
[INET6_ADDRSTRLEN
];
6238 struct lxc_list
*it
;
6240 struct lxc_netdev
*netdev
= data
;
6245 memset(retv
, 0, inlen
);
6250 listlen
= lxc_list_len(&netdev
->ipv6
);
6252 lxc_list_for_each(it
, &netdev
->ipv6
) {
6253 struct lxc_inet6dev
*i
= it
->elem
;
6254 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
6255 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6256 (listlen
-- > 1) ? "\n" : "");
6262 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6263 struct lxc_conf
*c
, void *data
)
6267 char buf
[INET6_ADDRSTRLEN
];
6268 struct lxc_list
*it
;
6270 struct lxc_netdev
*netdev
= data
;
6275 memset(retv
, 0, inlen
);
6278 return ret_set_errno(-1, EINVAL
);
6280 if (netdev
->type
!= LXC_NET_VETH
)
6283 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
6285 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv6_routes
) {
6286 struct lxc_inet6dev
*i
= it
->elem
;
6287 inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
));
6288 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6289 (listlen
-- > 1) ? "\n" : "");
6295 int lxc_list_config_items(char *retv
, int inlen
)
6304 memset(retv
, 0, inlen
);
6306 for (i
= 0; i
< config_jump_table_size
; i
++) {
6307 char *s
= config_jump_table
[i
].name
;
6309 if (s
[strlen(s
) - 1] == '.')
6312 strprint(retv
, inlen
, "%s\n", s
);
6318 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6327 memset(retv
, 0, inlen
);
6329 if (!strcmp(key
, "lxc.apparmor")) {
6330 strprint(retv
, inlen
, "allow_incomplete\n");
6331 strprint(retv
, inlen
, "allow_nesting\n");
6332 strprint(retv
, inlen
, "profile\n");
6333 strprint(retv
, inlen
, "raw\n");
6334 } else if (!strcmp(key
, "lxc.cgroup")) {
6335 strprint(retv
, inlen
, "dir\n");
6336 } else if (!strcmp(key
, "lxc.selinux")) {
6337 strprint(retv
, inlen
, "context\n");
6338 strprint(retv
, inlen
, "context.keyring\n");
6339 } else if (!strcmp(key
, "lxc.mount")) {
6340 strprint(retv
, inlen
, "auto\n");
6341 strprint(retv
, inlen
, "entry\n");
6342 strprint(retv
, inlen
, "fstab\n");
6343 } else if (!strcmp(key
, "lxc.rootfs")) {
6344 strprint(retv
, inlen
, "mount\n");
6345 strprint(retv
, inlen
, "options\n");
6346 strprint(retv
, inlen
, "path\n");
6347 } else if (!strcmp(key
, "lxc.uts")) {
6348 strprint(retv
, inlen
, "name\n");
6349 } else if (!strcmp(key
, "lxc.hook")) {
6350 strprint(retv
, inlen
, "autodev\n");
6351 strprint(retv
, inlen
, "autodevtmpfssize\n");
6352 strprint(retv
, inlen
, "clone\n");
6353 strprint(retv
, inlen
, "destroy\n");
6354 strprint(retv
, inlen
, "mount\n");
6355 strprint(retv
, inlen
, "post-stop\n");
6356 strprint(retv
, inlen
, "pre-mount\n");
6357 strprint(retv
, inlen
, "pre-start\n");
6358 strprint(retv
, inlen
, "start-host\n");
6359 strprint(retv
, inlen
, "start\n");
6360 strprint(retv
, inlen
, "stop\n");
6361 } else if (!strcmp(key
, "lxc.cap")) {
6362 strprint(retv
, inlen
, "drop\n");
6363 strprint(retv
, inlen
, "keep\n");
6364 } else if (!strcmp(key
, "lxc.console")) {
6365 strprint(retv
, inlen
, "logfile\n");
6366 strprint(retv
, inlen
, "path\n");
6367 } else if (!strcmp(key
, "lxc.seccomp")) {
6368 strprint(retv
, inlen
, "profile\n");
6369 } else if (!strcmp(key
, "lxc.signal")) {
6370 strprint(retv
, inlen
, "halt\n");
6371 strprint(retv
, inlen
, "reboot\n");
6372 strprint(retv
, inlen
, "stop\n");
6373 } else if (!strcmp(key
, "lxc.start")) {
6374 strprint(retv
, inlen
, "auto\n");
6375 strprint(retv
, inlen
, "delay\n");
6376 strprint(retv
, inlen
, "order\n");
6377 } else if (!strcmp(key
, "lxc.monitor")) {
6378 strprint(retv
, inlen
, "unshare\n");
6379 } else if (!strcmp(key
, "lxc.keyring")) {
6380 strprint(retv
, inlen
, "session\n");
6388 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6391 const char *idxstring
;
6392 struct lxc_netdev
*netdev
;
6396 idxstring
= key
+ 8;
6397 if (!isdigit(*idxstring
))
6400 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
6404 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
6411 memset(retv
, 0, inlen
);
6413 strprint(retv
, inlen
, "type\n");
6414 strprint(retv
, inlen
, "script.up\n");
6415 strprint(retv
, inlen
, "script.down\n");
6417 if (netdev
->type
!= LXC_NET_EMPTY
) {
6418 strprint(retv
, inlen
, "flags\n");
6419 strprint(retv
, inlen
, "link\n");
6420 strprint(retv
, inlen
, "name\n");
6421 strprint(retv
, inlen
, "hwaddr\n");
6422 strprint(retv
, inlen
, "mtu\n");
6423 strprint(retv
, inlen
, "ipv6.address\n");
6424 strprint(retv
, inlen
, "ipv6.gateway\n");
6425 strprint(retv
, inlen
, "ipv4.address\n");
6426 strprint(retv
, inlen
, "ipv4.gateway\n");
6429 switch (netdev
->type
) {
6431 strprint(retv
, inlen
, "veth.pair\n");
6432 strprint(retv
, inlen
, "veth.ipv4.route\n");
6433 strprint(retv
, inlen
, "veth.ipv6.route\n");
6434 strprint(retv
, inlen
, "veth.vlan.id\n");
6436 case LXC_NET_MACVLAN
:
6437 strprint(retv
, inlen
, "macvlan.mode\n");
6439 case LXC_NET_IPVLAN
:
6440 strprint(retv
, inlen
, "ipvlan.mode\n");
6441 strprint(retv
, inlen
, "ipvlan.isolation\n");
6444 strprint(retv
, inlen
, "vlan.id\n");