1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #define __STDC_FORMAT_MACROS
14 #include <netinet/in.h>
19 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/utsname.h>
31 #include "confile_utils.h"
32 #include "../include/netns_ifaddrs.h"
34 #include "lxcseccomp.h"
36 #include "memory_utils.h"
39 #include "storage/storage.h"
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(init_groups
);
98 lxc_config_define(keyring_session
);
99 lxc_config_define(log_file
);
100 lxc_config_define(log_level
);
101 lxc_config_define(log_syslog
);
102 lxc_config_define(monitor
);
103 lxc_config_define(monitor_signal_pdeath
);
104 lxc_config_define(mount
);
105 lxc_config_define(mount_auto
);
106 lxc_config_define(mount_fstab
);
107 lxc_config_define(namespace_clone
);
108 lxc_config_define(namespace_keep
);
109 lxc_config_define(time_offset_boot
);
110 lxc_config_define(time_offset_monotonic
);
111 lxc_config_define(namespace_share
);
112 lxc_config_define(net
);
113 lxc_config_define(net_flags
);
114 lxc_config_define(net_hwaddr
);
115 lxc_config_define(net_ipv4_address
);
116 lxc_config_define(net_ipv4_gateway
);
117 lxc_config_define(net_ipv6_address
);
118 lxc_config_define(net_ipv6_gateway
);
119 lxc_config_define(net_link
);
120 lxc_config_define(net_l2proxy
);
121 lxc_config_define(net_macvlan_mode
);
122 lxc_config_define(net_ipvlan_mode
);
123 lxc_config_define(net_ipvlan_isolation
);
124 lxc_config_define(net_mtu
);
125 lxc_config_define(net_name
);
126 lxc_config_define(net_nic
);
127 lxc_config_define(net_script_down
);
128 lxc_config_define(net_script_up
);
129 lxc_config_define(net_type
);
130 lxc_config_define(net_veth_mode
);
131 lxc_config_define(net_veth_pair
);
132 lxc_config_define(net_veth_ipv4_route
);
133 lxc_config_define(net_veth_ipv6_route
);
134 lxc_config_define(net_veth_vlan_id
);
135 lxc_config_define(net_veth_vlan_tagged_id
);
136 lxc_config_define(net_vlan_id
);
137 lxc_config_define(no_new_privs
);
138 lxc_config_define(personality
);
139 lxc_config_define(prlimit
);
140 lxc_config_define(pty_max
);
141 lxc_config_define(rootfs_managed
);
142 lxc_config_define(rootfs_mount
);
143 lxc_config_define(rootfs_options
);
144 lxc_config_define(rootfs_path
);
145 lxc_config_define(seccomp_profile
);
146 lxc_config_define(seccomp_allow_nesting
);
147 lxc_config_define(seccomp_notify_cookie
);
148 lxc_config_define(seccomp_notify_proxy
);
149 lxc_config_define(selinux_context
);
150 lxc_config_define(selinux_context_keyring
);
151 lxc_config_define(signal_halt
);
152 lxc_config_define(signal_reboot
);
153 lxc_config_define(signal_stop
);
154 lxc_config_define(start
);
155 lxc_config_define(tty_max
);
156 lxc_config_define(tty_dir
);
157 lxc_config_define(uts_name
);
158 lxc_config_define(sysctl
);
159 lxc_config_define(proc
);
163 * If a new config option is added to this table, be aware that
164 * the order in which the options are places into the table matters.
165 * That means that more specific options of a namespace have to be
166 * placed above more generic ones.
168 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
169 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
170 * has to be placed above lxc.ab.
172 static struct lxc_config_t config_jump_table
[] = {
173 { "lxc.arch", set_config_personality
, get_config_personality
, clr_config_personality
, },
174 { "lxc.apparmor.profile", set_config_apparmor_profile
, get_config_apparmor_profile
, clr_config_apparmor_profile
, },
175 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete
, get_config_apparmor_allow_incomplete
, clr_config_apparmor_allow_incomplete
, },
176 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting
, get_config_apparmor_allow_nesting
, clr_config_apparmor_allow_nesting
, },
177 { "lxc.apparmor.raw", set_config_apparmor_raw
, get_config_apparmor_raw
, clr_config_apparmor_raw
, },
178 { "lxc.autodev.tmpfs.size", set_config_autodev_tmpfs_size
, get_config_autodev_tmpfs_size
, clr_config_autodev_tmpfs_size
, },
179 { "lxc.autodev", set_config_autodev
, get_config_autodev
, clr_config_autodev
, },
180 { "lxc.cap.drop", set_config_cap_drop
, get_config_cap_drop
, clr_config_cap_drop
, },
181 { "lxc.cap.keep", set_config_cap_keep
, get_config_cap_keep
, clr_config_cap_keep
, },
182 { "lxc.cgroup2", set_config_cgroup2_controller
, get_config_cgroup2_controller
, clr_config_cgroup2_controller
, },
183 { "lxc.cgroup.dir.monitor.pivot", set_config_cgroup_monitor_pivot_dir
, get_config_cgroup_monitor_pivot_dir
, clr_config_cgroup_monitor_pivot_dir
, },
184 { "lxc.cgroup.dir.monitor", set_config_cgroup_monitor_dir
, get_config_cgroup_monitor_dir
, clr_config_cgroup_monitor_dir
, },
185 { "lxc.cgroup.dir.container.inner", set_config_cgroup_container_inner_dir
, get_config_cgroup_container_inner_dir
, clr_config_cgroup_container_inner_dir
, },
186 { "lxc.cgroup.dir.container", set_config_cgroup_container_dir
, get_config_cgroup_container_dir
, clr_config_cgroup_container_dir
, },
187 { "lxc.cgroup.dir", set_config_cgroup_dir
, get_config_cgroup_dir
, clr_config_cgroup_dir
, },
188 { "lxc.cgroup.relative", set_config_cgroup_relative
, get_config_cgroup_relative
, clr_config_cgroup_relative
, },
189 { "lxc.cgroup", set_config_cgroup_controller
, get_config_cgroup_controller
, clr_config_cgroup_controller
, },
190 { "lxc.console.buffer.size", set_config_console_buffer_size
, get_config_console_buffer_size
, clr_config_console_buffer_size
, },
191 { "lxc.console.logfile", set_config_console_logfile
, get_config_console_logfile
, clr_config_console_logfile
, },
192 { "lxc.console.path", set_config_console_path
, get_config_console_path
, clr_config_console_path
, },
193 { "lxc.console.rotate", set_config_console_rotate
, get_config_console_rotate
, clr_config_console_rotate
, },
194 { "lxc.console.size", set_config_console_size
, get_config_console_size
, clr_config_console_size
, },
195 { "lxc.environment", set_config_environment
, get_config_environment
, clr_config_environment
, },
196 { "lxc.ephemeral", set_config_ephemeral
, get_config_ephemeral
, clr_config_ephemeral
, },
197 { "lxc.execute.cmd", set_config_execute_cmd
, get_config_execute_cmd
, clr_config_execute_cmd
, },
198 { "lxc.group", set_config_group
, get_config_group
, clr_config_group
, },
199 { "lxc.hook.autodev", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
200 { "lxc.hook.clone", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
201 { "lxc.hook.destroy", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
202 { "lxc.hook.mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
203 { "lxc.hook.post-stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
204 { "lxc.hook.pre-mount", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
205 { "lxc.hook.pre-start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
206 { "lxc.hook.start", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
207 { "lxc.hook.start-host", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
208 { "lxc.hook.stop", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
209 { "lxc.hook.version", set_config_hooks_version
, get_config_hooks_version
, clr_config_hooks_version
, },
210 { "lxc.hook", set_config_hooks
, get_config_hooks
, clr_config_hooks
, },
211 { "lxc.idmap", set_config_idmaps
, get_config_idmaps
, clr_config_idmaps
, },
212 { "lxc.include", set_config_includefiles
, get_config_includefiles
, clr_config_includefiles
, },
213 { "lxc.init.cmd", set_config_init_cmd
, get_config_init_cmd
, clr_config_init_cmd
, },
214 { "lxc.init.gid", set_config_init_gid
, get_config_init_gid
, clr_config_init_gid
, },
215 { "lxc.init.groups", set_config_init_groups
, get_config_init_groups
, clr_config_init_groups
, },
216 { "lxc.init.uid", set_config_init_uid
, get_config_init_uid
, clr_config_init_uid
, },
217 { "lxc.init.cwd", set_config_init_cwd
, get_config_init_cwd
, clr_config_init_cwd
, },
218 { "lxc.keyring.session", set_config_keyring_session
, get_config_keyring_session
, clr_config_keyring_session
},
219 { "lxc.log.file", set_config_log_file
, get_config_log_file
, clr_config_log_file
, },
220 { "lxc.log.level", set_config_log_level
, get_config_log_level
, clr_config_log_level
, },
221 { "lxc.log.syslog", set_config_log_syslog
, get_config_log_syslog
, clr_config_log_syslog
, },
222 { "lxc.monitor.unshare", set_config_monitor
, get_config_monitor
, clr_config_monitor
, },
223 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath
, get_config_monitor_signal_pdeath
, clr_config_monitor_signal_pdeath
, },
224 { "lxc.mount.auto", set_config_mount_auto
, get_config_mount_auto
, clr_config_mount_auto
, },
225 { "lxc.mount.entry", set_config_mount
, get_config_mount
, clr_config_mount
, },
226 { "lxc.mount.fstab", set_config_mount_fstab
, get_config_mount_fstab
, clr_config_mount_fstab
, },
227 { "lxc.namespace.clone", set_config_namespace_clone
, get_config_namespace_clone
, clr_config_namespace_clone
, },
228 { "lxc.namespace.keep", set_config_namespace_keep
, get_config_namespace_keep
, clr_config_namespace_keep
, },
229 { "lxc.namespace.share", set_config_namespace_share
, get_config_namespace_share
, clr_config_namespace_share
, },
230 { "lxc.time.offset.boot", set_config_time_offset_boot
, get_config_time_offset_boot
, clr_config_time_offset_boot
, },
231 { "lxc.time.offset.monotonic", set_config_time_offset_monotonic
, get_config_time_offset_monotonic
, clr_config_time_offset_monotonic
, },
232 { "lxc.net.flags", set_config_net_flags
, get_config_net_flags
, clr_config_net_flags
, },
233 { "lxc.net.hwaddr", set_config_net_hwaddr
, get_config_net_hwaddr
, clr_config_net_hwaddr
, },
234 { "lxc.net.ipv4.address", set_config_net_ipv4_address
, get_config_net_ipv4_address
, clr_config_net_ipv4_address
, },
235 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway
, get_config_net_ipv4_gateway
, clr_config_net_ipv4_gateway
, },
236 { "lxc.net.ipv6.address", set_config_net_ipv6_address
, get_config_net_ipv6_address
, clr_config_net_ipv6_address
, },
237 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway
, get_config_net_ipv6_gateway
, clr_config_net_ipv6_gateway
, },
238 { "lxc.net.link", set_config_net_link
, get_config_net_link
, clr_config_net_link
, },
239 { "lxc.net.l2proxy", set_config_net_l2proxy
, get_config_net_l2proxy
, clr_config_net_l2proxy
, },
240 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode
, get_config_net_macvlan_mode
, clr_config_net_macvlan_mode
, },
241 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode
, get_config_net_ipvlan_mode
, clr_config_net_ipvlan_mode
, },
242 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation
, get_config_net_ipvlan_isolation
, clr_config_net_ipvlan_isolation
, },
243 { "lxc.net.mtu", set_config_net_mtu
, get_config_net_mtu
, clr_config_net_mtu
, },
244 { "lxc.net.name", set_config_net_name
, get_config_net_name
, clr_config_net_name
, },
245 { "lxc.net.script.down", set_config_net_script_down
, get_config_net_script_down
, clr_config_net_script_down
, },
246 { "lxc.net.script.up", set_config_net_script_up
, get_config_net_script_up
, clr_config_net_script_up
, },
247 { "lxc.net.type", set_config_net_type
, get_config_net_type
, clr_config_net_type
, },
248 { "lxc.net.vlan.id", set_config_net_vlan_id
, get_config_net_vlan_id
, clr_config_net_vlan_id
, },
249 { "lxc.net.veth.mode", set_config_net_veth_mode
, get_config_net_veth_mode
, clr_config_net_veth_mode
, },
250 { "lxc.net.veth.pair", set_config_net_veth_pair
, get_config_net_veth_pair
, clr_config_net_veth_pair
, },
251 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route
, get_config_net_veth_ipv4_route
, clr_config_net_veth_ipv4_route
, },
252 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route
, get_config_net_veth_ipv6_route
, clr_config_net_veth_ipv6_route
, },
253 { "lxc.net.veth.vlan.id", set_config_net_veth_vlan_id
, get_config_net_veth_vlan_id
, clr_config_net_veth_vlan_id
, },
254 { "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
, },
255 { "lxc.net.", set_config_net_nic
, get_config_net_nic
, clr_config_net_nic
, },
256 { "lxc.net", set_config_net
, get_config_net
, clr_config_net
, },
257 { "lxc.no_new_privs", set_config_no_new_privs
, get_config_no_new_privs
, clr_config_no_new_privs
, },
258 { "lxc.prlimit", set_config_prlimit
, get_config_prlimit
, clr_config_prlimit
, },
259 { "lxc.pty.max", set_config_pty_max
, get_config_pty_max
, clr_config_pty_max
, },
260 { "lxc.rootfs.managed", set_config_rootfs_managed
, get_config_rootfs_managed
, clr_config_rootfs_managed
, },
261 { "lxc.rootfs.mount", set_config_rootfs_mount
, get_config_rootfs_mount
, clr_config_rootfs_mount
, },
262 { "lxc.rootfs.options", set_config_rootfs_options
, get_config_rootfs_options
, clr_config_rootfs_options
, },
263 { "lxc.rootfs.path", set_config_rootfs_path
, get_config_rootfs_path
, clr_config_rootfs_path
, },
264 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting
, get_config_seccomp_allow_nesting
, clr_config_seccomp_allow_nesting
, },
265 { "lxc.seccomp.notify.cookie", set_config_seccomp_notify_cookie
, get_config_seccomp_notify_cookie
, clr_config_seccomp_notify_cookie
, },
266 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy
, get_config_seccomp_notify_proxy
, clr_config_seccomp_notify_proxy
, },
267 { "lxc.seccomp.profile", set_config_seccomp_profile
, get_config_seccomp_profile
, clr_config_seccomp_profile
, },
268 { "lxc.selinux.context.keyring", set_config_selinux_context_keyring
, get_config_selinux_context_keyring
, clr_config_selinux_context_keyring
},
269 { "lxc.selinux.context", set_config_selinux_context
, get_config_selinux_context
, clr_config_selinux_context
, },
270 { "lxc.signal.halt", set_config_signal_halt
, get_config_signal_halt
, clr_config_signal_halt
, },
271 { "lxc.signal.reboot", set_config_signal_reboot
, get_config_signal_reboot
, clr_config_signal_reboot
, },
272 { "lxc.signal.stop", set_config_signal_stop
, get_config_signal_stop
, clr_config_signal_stop
, },
273 { "lxc.start.auto", set_config_start
, get_config_start
, clr_config_start
, },
274 { "lxc.start.delay", set_config_start
, get_config_start
, clr_config_start
, },
275 { "lxc.start.order", set_config_start
, get_config_start
, clr_config_start
, },
276 { "lxc.tty.dir", set_config_tty_dir
, get_config_tty_dir
, clr_config_tty_dir
, },
277 { "lxc.tty.max", set_config_tty_max
, get_config_tty_max
, clr_config_tty_max
, },
278 { "lxc.uts.name", set_config_uts_name
, get_config_uts_name
, clr_config_uts_name
, },
279 { "lxc.sysctl", set_config_sysctl
, get_config_sysctl
, clr_config_sysctl
, },
280 { "lxc.proc", set_config_proc
, get_config_proc
, clr_config_proc
, },
283 static const size_t config_jump_table_size
= sizeof(config_jump_table
) / sizeof(struct lxc_config_t
);
285 struct lxc_config_t
*lxc_get_config_exact(const char *key
)
289 for (i
= 0; i
< config_jump_table_size
; i
++)
290 if (!strcmp(config_jump_table
[i
].name
, key
))
291 return &config_jump_table
[i
];
297 struct lxc_config_t
*lxc_get_config(const char *key
)
301 for (i
= 0; i
< config_jump_table_size
; i
++)
302 if (!strncmp(config_jump_table
[i
].name
, key
, strlen(config_jump_table
[i
].name
)))
303 return &config_jump_table
[i
];
308 static int set_config_net(const char *key
, const char *value
,
309 struct lxc_conf
*lxc_conf
, void *data
)
311 if (!lxc_config_value_empty(value
)) {
312 ERROR("lxc.net must not have a value");
316 return clr_config_net(key
, lxc_conf
, data
);
319 static int set_config_net_type(const char *key
, const char *value
,
320 struct lxc_conf
*lxc_conf
, void *data
)
322 struct lxc_netdev
*netdev
= data
;
324 if (lxc_config_value_empty(value
))
325 return clr_config_net_type(key
, lxc_conf
, data
);
328 return ret_errno(EINVAL
);
330 if (strcmp(value
, "veth") == 0) {
331 netdev
->type
= LXC_NET_VETH
;
332 lxc_list_init(&netdev
->priv
.veth_attr
.ipv4_routes
);
333 lxc_list_init(&netdev
->priv
.veth_attr
.ipv6_routes
);
334 lxc_list_init(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
335 if (!lxc_veth_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
336 lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, "bridge");
337 } else if (strcmp(value
, "macvlan") == 0) {
338 netdev
->type
= LXC_NET_MACVLAN
;
339 if (!lxc_macvlan_flag_to_mode(netdev
->priv
.veth_attr
.mode
))
340 lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, "private");
341 } else if (strcmp(value
, "ipvlan") == 0) {
342 netdev
->type
= LXC_NET_IPVLAN
;
343 if (!lxc_ipvlan_flag_to_mode(netdev
->priv
.ipvlan_attr
.mode
))
344 lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, "l3");
345 if (!lxc_ipvlan_flag_to_isolation(netdev
->priv
.ipvlan_attr
.isolation
))
346 lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, "bridge");
347 } else if (strcmp(value
, "vlan") == 0) {
348 netdev
->type
= LXC_NET_VLAN
;
349 } else if (strcmp(value
, "phys") == 0) {
350 netdev
->type
= LXC_NET_PHYS
;
351 } else if (strcmp(value
, "empty") == 0) {
352 netdev
->type
= LXC_NET_EMPTY
;
353 } else if (strcmp(value
, "none") == 0) {
354 netdev
->type
= LXC_NET_NONE
;
356 return log_error(-1, "Invalid network type %s", value
);
362 static int set_config_net_flags(const char *key
, const char *value
,
363 struct lxc_conf
*lxc_conf
, void *data
)
365 struct lxc_netdev
*netdev
= data
;
367 if (lxc_config_value_empty(value
))
368 return clr_config_net_flags(key
, lxc_conf
, data
);
371 return ret_errno(EINVAL
);
373 netdev
->flags
|= IFF_UP
;
378 static int create_matched_ifnames(const char *value
, struct lxc_conf
*lxc_conf
,
379 struct lxc_netdev
*netdev
)
381 call_cleaner(netns_freeifaddrs
) struct netns_ifaddrs
*ifaddr
= NULL
;
382 struct netns_ifaddrs
*ifa
;
385 const char *type_key
= "lxc.net.type";
386 const char *link_key
= "lxc.net.link";
387 const char *tmpvalue
= "phys";
389 if (netns_getifaddrs(&ifaddr
, -1, &(bool){false}) < 0)
390 return log_error_errno(-1, errno
, "Failed to get network interfaces");
392 for (ifa
= ifaddr
, n
= 0; ifa
!= NULL
; ifa
= ifa
->ifa_next
, n
++) {
396 if (ifa
->ifa_addr
->sa_family
!= AF_PACKET
)
399 if (!strncmp(value
, ifa
->ifa_name
, strlen(value
) - 1)) {
400 ret
= set_config_net_type(type_key
, tmpvalue
, lxc_conf
,
403 ret
= set_config_net_link(
404 link_key
, ifa
->ifa_name
, lxc_conf
, netdev
);
406 ERROR("Failed to create matched ifnames");
410 ERROR("Failed to create matched ifnames");
419 static int set_config_net_link(const char *key
, const char *value
,
420 struct lxc_conf
*lxc_conf
, void *data
)
422 struct lxc_netdev
*netdev
= data
;
425 if (lxc_config_value_empty(value
))
426 return clr_config_net_link(key
, lxc_conf
, data
);
429 return ret_errno(EINVAL
);
431 if (value
[strlen(value
) - 1] == '+' && netdev
->type
== LXC_NET_PHYS
)
432 ret
= create_matched_ifnames(value
, lxc_conf
, netdev
);
434 ret
= network_ifname(netdev
->link
, value
, sizeof(netdev
->link
));
439 static int set_config_net_l2proxy(const char *key
, const char *value
,
440 struct lxc_conf
*lxc_conf
, void *data
)
442 struct lxc_netdev
*netdev
= data
;
443 unsigned int val
= 0;
446 if (lxc_config_value_empty(value
))
447 return clr_config_net_l2proxy(key
, lxc_conf
, data
);
450 return ret_errno(EINVAL
);
452 ret
= lxc_safe_uint(value
, &val
);
454 return ret_errno(ret
);
458 netdev
->l2proxy
= false;
461 netdev
->l2proxy
= true;
465 return ret_errno(EINVAL
);
468 static int set_config_net_name(const char *key
, const char *value
,
469 struct lxc_conf
*lxc_conf
, void *data
)
471 struct lxc_netdev
*netdev
= data
;
473 if (lxc_config_value_empty(value
))
474 return clr_config_net_name(key
, lxc_conf
, data
);
477 return ret_errno(EINVAL
);
479 return network_ifname(netdev
->name
, value
, sizeof(netdev
->name
));
483 static int set_config_net_veth_mode(const char *key
, const char *value
,
484 struct lxc_conf
*lxc_conf
, void *data
)
486 struct lxc_netdev
*netdev
= data
;
488 if (lxc_config_value_empty(value
))
489 return clr_config_net_veth_mode(key
, lxc_conf
, data
);
492 return ret_errno(EINVAL
);
494 return lxc_veth_mode_to_flag(&netdev
->priv
.veth_attr
.mode
, value
);
497 static int set_config_net_veth_pair(const char *key
, const char *value
,
498 struct lxc_conf
*lxc_conf
, void *data
)
500 struct lxc_netdev
*netdev
= data
;
502 if (lxc_config_value_empty(value
))
503 return clr_config_net_veth_pair(key
, lxc_conf
, data
);
506 return ret_errno(EINVAL
);
508 return network_ifname(netdev
->priv
.veth_attr
.pair
, value
,
509 sizeof(netdev
->priv
.veth_attr
.pair
));
512 static int set_config_net_veth_vlan_id(const char *key
, const char *value
,
513 struct lxc_conf
*lxc_conf
, void *data
)
516 struct lxc_netdev
*netdev
= data
;
519 return ret_errno(EINVAL
);
521 if (lxc_config_value_empty(value
))
522 return clr_config_net_veth_vlan_id(key
, lxc_conf
, data
);
524 if (strcmp(value
, "none") == 0) {
525 netdev
->priv
.veth_attr
.vlan_id
= BRIDGE_VLAN_NONE
;
527 unsigned short vlan_id
;
528 ret
= get_u16(&vlan_id
, value
, 0);
530 return ret_errno(EINVAL
);
532 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
533 return ret_errno(EINVAL
);
535 netdev
->priv
.veth_attr
.vlan_id
= vlan_id
;
538 netdev
->priv
.veth_attr
.vlan_id_set
= true;
542 static int set_config_net_veth_vlan_tagged_id(const char *key
, const char *value
,
543 struct lxc_conf
*lxc_conf
, void *data
)
545 __do_free
struct lxc_list
*list
= NULL
;
547 unsigned short vlan_id
;
548 struct lxc_netdev
*netdev
= data
;
551 return ret_errno(EINVAL
);
553 if (lxc_config_value_empty(value
))
554 return clr_config_net_veth_vlan_tagged_id(key
, lxc_conf
, data
);
556 ret
= get_u16(&vlan_id
, value
, 0);
560 if (vlan_id
> BRIDGE_VLAN_ID_MAX
)
563 list
= malloc(sizeof(*list
));
565 return ret_errno(ENOMEM
);
568 list
->elem
= UINT_TO_PTR(vlan_id
);
570 lxc_list_add_tail(&netdev
->priv
.veth_attr
.vlan_tagged_ids
, move_ptr(list
));
575 static int set_config_net_macvlan_mode(const char *key
, const char *value
,
576 struct lxc_conf
*lxc_conf
, void *data
)
578 struct lxc_netdev
*netdev
= data
;
580 if (lxc_config_value_empty(value
))
581 return clr_config_net_macvlan_mode(key
, lxc_conf
, data
);
584 return ret_errno(EINVAL
);
586 return lxc_macvlan_mode_to_flag(&netdev
->priv
.macvlan_attr
.mode
, value
);
589 static int set_config_net_ipvlan_mode(const char *key
, const char *value
,
590 struct lxc_conf
*lxc_conf
, void *data
)
592 struct lxc_netdev
*netdev
= data
;
594 if (lxc_config_value_empty(value
))
595 return clr_config_net_ipvlan_mode(key
, lxc_conf
, data
);
598 return ret_errno(EINVAL
);
600 if (netdev
->type
!= LXC_NET_IPVLAN
)
601 return log_error_errno(-EINVAL
,
602 EINVAL
, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network",
605 return lxc_ipvlan_mode_to_flag(&netdev
->priv
.ipvlan_attr
.mode
, value
);
608 static int set_config_net_ipvlan_isolation(const char *key
, const char *value
,
609 struct lxc_conf
*lxc_conf
, void *data
)
611 struct lxc_netdev
*netdev
= data
;
613 if (lxc_config_value_empty(value
))
614 return clr_config_net_ipvlan_isolation(key
, lxc_conf
, data
);
617 return ret_errno(EINVAL
);
619 if (netdev
->type
!= LXC_NET_IPVLAN
)
620 return log_error_errno(-EINVAL
,
621 EINVAL
, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network",
624 return lxc_ipvlan_isolation_to_flag(&netdev
->priv
.ipvlan_attr
.isolation
, value
);
627 static int set_config_net_hwaddr(const char *key
, const char *value
,
628 struct lxc_conf
*lxc_conf
, void *data
)
630 struct lxc_netdev
*netdev
= data
;
633 if (lxc_config_value_empty(value
))
634 return clr_config_net_hwaddr(key
, lxc_conf
, data
);
637 return ret_errno(EINVAL
);
639 new_value
= strdup(value
);
641 return ret_errno(ENOMEM
);
643 rand_complete_hwaddr(new_value
);
645 if (lxc_config_value_empty(new_value
)) {
647 netdev
->hwaddr
= NULL
;
651 netdev
->hwaddr
= new_value
;
656 static int set_config_net_vlan_id(const char *key
, const char *value
,
657 struct lxc_conf
*lxc_conf
, void *data
)
660 struct lxc_netdev
*netdev
= data
;
662 if (lxc_config_value_empty(value
))
663 return clr_config_net_vlan_id(key
, lxc_conf
, data
);
666 return ret_errno(EINVAL
);
668 ret
= get_u16(&netdev
->priv
.vlan_attr
.vid
, value
, 0);
675 static int set_config_net_mtu(const char *key
, const char *value
,
676 struct lxc_conf
*lxc_conf
, void *data
)
678 struct lxc_netdev
*netdev
= data
;
680 if (lxc_config_value_empty(value
))
681 return clr_config_net_mtu(key
, lxc_conf
, data
);
684 return ret_errno(EINVAL
);
686 return set_config_string_item(&netdev
->mtu
, value
);
689 static int set_config_net_ipv4_address(const char *key
, const char *value
,
690 struct lxc_conf
*lxc_conf
, void *data
)
692 __do_free
char *addr
= NULL
;
693 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
694 __do_free
struct lxc_list
*list
= NULL
;
696 struct lxc_netdev
*netdev
= data
;
697 char *cursor
, *slash
;
698 char *bcast
= NULL
, *prefix
= NULL
;
700 if (lxc_config_value_empty(value
))
701 return clr_config_net_ipv4_address(key
, lxc_conf
, data
);
704 return ret_errno(EINVAL
);
706 inetdev
= malloc(sizeof(*inetdev
));
708 return ret_errno(ENOMEM
);
709 memset(inetdev
, 0, sizeof(*inetdev
));
711 list
= malloc(sizeof(*list
));
713 return ret_errno(ENOMEM
);
717 addr
= strdup(value
);
719 return ret_errno(ENOMEM
);
721 cursor
= strstr(addr
, " ");
727 slash
= strstr(addr
, "/");
733 ret
= inet_pton(AF_INET
, addr
, &inetdev
->addr
);
735 return log_error_errno(-1, errno
, "Invalid ipv4 address \"%s\"", value
);
738 ret
= inet_pton(AF_INET
, bcast
, &inetdev
->bcast
);
740 return log_error_errno(-1, errno
, "Invalid ipv4 broadcast address \"%s\"", value
);
744 /* No prefix specified, determine it from the network class. */
746 ret
= lxc_safe_uint(prefix
, &inetdev
->prefix
);
750 inetdev
->prefix
= config_ip_prefix(&inetdev
->addr
);
753 /* If no broadcast address, let compute one from the
754 * prefix and address.
757 inetdev
->bcast
.s_addr
= inetdev
->addr
.s_addr
;
758 inetdev
->bcast
.s_addr
|= htonl(INADDR_BROADCAST
>> inetdev
->prefix
);
761 list
->elem
= inetdev
;
762 lxc_list_add_tail(&netdev
->ipv4
, list
);
769 static int set_config_net_ipv4_gateway(const char *key
, const char *value
,
770 struct lxc_conf
*lxc_conf
, void *data
)
772 struct lxc_netdev
*netdev
= data
;
774 if (lxc_config_value_empty(value
))
775 return clr_config_net_ipv4_gateway(key
, lxc_conf
, data
);
780 free(netdev
->ipv4_gateway
);
782 if (strcmp(value
, "auto") == 0) {
783 netdev
->ipv4_gateway
= NULL
;
784 netdev
->ipv4_gateway_auto
= true;
785 } else if (strcmp(value
, "dev") == 0) {
786 netdev
->ipv4_gateway
= NULL
;
787 netdev
->ipv4_gateway_auto
= false;
788 netdev
->ipv4_gateway_dev
= true;
790 __do_free
struct in_addr
*gw
= NULL
;
793 gw
= malloc(sizeof(*gw
));
795 return ret_errno(ENOMEM
);
797 ret
= inet_pton(AF_INET
, value
, gw
);
799 return log_error_errno(-1, errno
, "Invalid ipv4 gateway address \"%s\"", value
);
801 netdev
->ipv4_gateway
= move_ptr(gw
);
802 netdev
->ipv4_gateway_auto
= false;
808 static int set_config_net_veth_ipv4_route(const char *key
, const char *value
,
809 struct lxc_conf
*lxc_conf
, void *data
)
811 __do_free
char *valdup
= NULL
;
812 __do_free
struct lxc_inetdev
*inetdev
= NULL
;
813 __do_free
struct lxc_list
*list
= NULL
;
815 char *netmask
, *slash
;
816 struct lxc_netdev
*netdev
= data
;
818 if (lxc_config_value_empty(value
))
819 return clr_config_net_veth_ipv4_route(key
, lxc_conf
, data
);
822 return ret_errno(EINVAL
);
824 if (netdev
->type
!= LXC_NET_VETH
)
825 return log_error_errno(-EINVAL
,
826 EINVAL
, "Invalid ipv4 route \"%s\", can only be used with veth network",
829 inetdev
= malloc(sizeof(*inetdev
));
831 return ret_errno(ENOMEM
);
832 memset(inetdev
, 0, sizeof(*inetdev
));
834 list
= malloc(sizeof(*list
));
836 return ret_errno(ENOMEM
);
839 list
->elem
= inetdev
;
841 valdup
= strdup(value
);
843 return ret_errno(ENOMEM
);
845 slash
= strchr(valdup
, '/');
847 return ret_errno(EINVAL
);
852 return ret_errno(EINVAL
);
856 ret
= lxc_safe_uint(netmask
, &inetdev
->prefix
);
857 if (ret
< 0 || inetdev
->prefix
> 32)
858 return ret_errno(EINVAL
);
860 ret
= inet_pton(AF_INET
, valdup
, &inetdev
->addr
);
862 return ret_errno(EINVAL
);
864 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv4_routes
, list
);
871 static int set_config_net_ipv6_address(const char *key
, const char *value
,
872 struct lxc_conf
*lxc_conf
, void *data
)
874 __do_free
char *valdup
= NULL
;
875 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
876 __do_free
struct lxc_list
*list
= NULL
;
878 struct lxc_netdev
*netdev
= data
;
879 char *slash
, *netmask
;
881 if (lxc_config_value_empty(value
))
882 return clr_config_net_ipv6_address(key
, lxc_conf
, data
);
885 return ret_errno(EINVAL
);
887 inet6dev
= malloc(sizeof(*inet6dev
));
889 return ret_errno(ENOMEM
);
890 memset(inet6dev
, 0, sizeof(*inet6dev
));
892 list
= malloc(sizeof(*list
));
894 return ret_errno(ENOMEM
);
898 valdup
= strdup(value
);
900 return ret_errno(ENOMEM
);
902 inet6dev
->prefix
= 64;
903 slash
= strstr(valdup
, "/");
908 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
913 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
915 return log_error_errno(-EINVAL
, EINVAL
, "Invalid ipv6 address \"%s\"", valdup
);
917 list
->elem
= inet6dev
;
918 lxc_list_add_tail(&netdev
->ipv6
, list
);
925 static int set_config_net_ipv6_gateway(const char *key
, const char *value
,
926 struct lxc_conf
*lxc_conf
, void *data
)
928 struct lxc_netdev
*netdev
= data
;
930 if (lxc_config_value_empty(value
))
931 return clr_config_net_ipv6_gateway(key
, lxc_conf
, data
);
934 return ret_errno(EINVAL
);
936 free(netdev
->ipv6_gateway
);
938 if (strcmp(value
, "auto") == 0) {
939 netdev
->ipv6_gateway
= NULL
;
940 netdev
->ipv6_gateway_auto
= true;
941 } else if (strcmp(value
, "dev") == 0) {
942 netdev
->ipv6_gateway
= NULL
;
943 netdev
->ipv6_gateway_auto
= false;
944 netdev
->ipv6_gateway_dev
= true;
947 __do_free
struct in6_addr
*gw
= NULL
;
949 gw
= malloc(sizeof(*gw
));
951 return ret_errno(ENOMEM
);
953 ret
= inet_pton(AF_INET6
, value
, gw
);
955 return log_error_errno(-EINVAL
, EINVAL
,
956 "Invalid ipv6 gateway address \"%s\"", value
);
958 netdev
->ipv6_gateway
= move_ptr(gw
);
959 netdev
->ipv6_gateway_auto
= false;
965 static int set_config_net_veth_ipv6_route(const char *key
, const char *value
,
966 struct lxc_conf
*lxc_conf
, void *data
)
968 __do_free
char *valdup
= NULL
;
969 __do_free
struct lxc_inet6dev
*inet6dev
= NULL
;
970 __do_free
struct lxc_list
*list
= NULL
;
972 char *netmask
, *slash
;
973 struct lxc_netdev
*netdev
= data
;
975 if (lxc_config_value_empty(value
))
976 return clr_config_net_veth_ipv6_route(key
, lxc_conf
, data
);
979 return ret_errno(EINVAL
);
981 if (netdev
->type
!= LXC_NET_VETH
)
982 return log_error_errno(-EINVAL
,
983 EINVAL
, "Invalid ipv6 route \"%s\", can only be used with veth network",
986 inet6dev
= malloc(sizeof(*inet6dev
));
988 return ret_errno(ENOMEM
);
989 memset(inet6dev
, 0, sizeof(*inet6dev
));
991 list
= malloc(sizeof(*list
));
993 return ret_errno(ENOMEM
);
997 valdup
= strdup(value
);
1001 slash
= strchr(valdup
, '/');
1003 return ret_errno(EINVAL
);
1008 return ret_errno(EINVAL
);
1012 ret
= lxc_safe_uint(netmask
, &inet6dev
->prefix
);
1013 if (ret
< 0 || inet6dev
->prefix
> 128)
1014 return ret_errno(EINVAL
);
1016 ret
= inet_pton(AF_INET6
, valdup
, &inet6dev
->addr
);
1017 if (!ret
|| ret
< 0)
1018 return ret_errno(EINVAL
);
1020 list
->elem
= inet6dev
;
1021 lxc_list_add_tail(&netdev
->priv
.veth_attr
.ipv6_routes
, list
);
1028 static int set_config_net_script_up(const char *key
, const char *value
,
1029 struct lxc_conf
*lxc_conf
, void *data
)
1031 struct lxc_netdev
*netdev
= data
;
1033 if (lxc_config_value_empty(value
))
1034 return clr_config_net_script_up(key
, lxc_conf
, data
);
1037 return ret_errno(EINVAL
);
1039 return set_config_string_item(&netdev
->upscript
, value
);
1042 static int set_config_net_script_down(const char *key
, const char *value
,
1043 struct lxc_conf
*lxc_conf
, void *data
)
1045 struct lxc_netdev
*netdev
= data
;
1047 if (lxc_config_value_empty(value
))
1048 return clr_config_net_script_down(key
, lxc_conf
, data
);
1051 return ret_errno(EINVAL
);
1053 return set_config_string_item(&netdev
->downscript
, value
);
1056 static int add_hook(struct lxc_conf
*lxc_conf
, int which
, __owns
char *hook
)
1058 __do_free
char *val
= hook
;
1059 struct lxc_list
*hooklist
;
1061 hooklist
= malloc(sizeof(*hooklist
));
1063 return ret_errno(ENOMEM
);
1065 hooklist
->elem
= move_ptr(val
);
1066 lxc_list_add_tail(&lxc_conf
->hooks
[which
], hooklist
);
1071 static int set_config_seccomp_allow_nesting(const char *key
, const char *value
,
1072 struct lxc_conf
*lxc_conf
, void *data
)
1075 if (lxc_config_value_empty(value
))
1076 return clr_config_seccomp_allow_nesting(key
, lxc_conf
, NULL
);
1078 if (lxc_safe_uint(value
, &lxc_conf
->seccomp
.allow_nesting
) < 0)
1081 if (lxc_conf
->seccomp
.allow_nesting
> 1)
1082 return ret_set_errno(-1, EINVAL
);
1091 static int set_config_seccomp_notify_cookie(const char *key
, const char *value
,
1092 struct lxc_conf
*lxc_conf
, void *data
)
1094 #ifdef HAVE_SECCOMP_NOTIFY
1095 return set_config_string_item(&lxc_conf
->seccomp
.notifier
.cookie
, value
);
1097 return ret_set_errno(-1, ENOSYS
);
1101 static int set_config_seccomp_notify_proxy(const char *key
, const char *value
,
1102 struct lxc_conf
*lxc_conf
, void *data
)
1104 #ifdef HAVE_SECCOMP_NOTIFY
1107 if (lxc_config_value_empty(value
))
1108 return clr_config_seccomp_notify_proxy(key
, lxc_conf
, NULL
);
1110 if (strncmp(value
, "unix:", 5) != 0)
1111 return ret_set_errno(-1, EINVAL
);
1114 if (lxc_unix_sockaddr(&lxc_conf
->seccomp
.notifier
.proxy_addr
, offset
) < 0)
1119 return ret_set_errno(-1, ENOSYS
);
1123 static int set_config_seccomp_profile(const char *key
, const char *value
,
1124 struct lxc_conf
*lxc_conf
, void *data
)
1126 return set_config_path_item(&lxc_conf
->seccomp
.seccomp
, value
);
1129 static int set_config_execute_cmd(const char *key
, const char *value
,
1130 struct lxc_conf
*lxc_conf
, void *data
)
1132 return set_config_path_item(&lxc_conf
->execute_cmd
, value
);
1135 static int set_config_init_cmd(const char *key
, const char *value
,
1136 struct lxc_conf
*lxc_conf
, void *data
)
1138 return set_config_path_item(&lxc_conf
->init_cmd
, value
);
1141 static int set_config_init_cwd(const char *key
, const char *value
,
1142 struct lxc_conf
*lxc_conf
, void *data
)
1144 return set_config_path_item(&lxc_conf
->init_cwd
, value
);
1147 static int set_config_init_uid(const char *key
, const char *value
,
1148 struct lxc_conf
*lxc_conf
, void *data
)
1150 unsigned int init_uid
;
1152 if (lxc_config_value_empty(value
)) {
1153 lxc_conf
->init_uid
= 0;
1157 if (lxc_safe_uint(value
, &init_uid
) < 0)
1160 lxc_conf
->init_uid
= init_uid
;
1165 static int set_config_init_gid(const char *key
, const char *value
,
1166 struct lxc_conf
*lxc_conf
, void *data
)
1168 unsigned int init_gid
;
1170 if (lxc_config_value_empty(value
)) {
1171 lxc_conf
->init_gid
= 0;
1175 if (lxc_safe_uint(value
, &init_gid
) < 0)
1178 lxc_conf
->init_gid
= init_gid
;
1183 static int set_config_init_groups(const char *key
, const char *value
,
1184 struct lxc_conf
*lxc_conf
, void *data
)
1186 __do_free
char *value_dup
= NULL
;
1187 __do_free gid_t
*init_groups
= NULL
;
1192 if (lxc_config_value_empty(value
))
1193 return clr_config_init_groups(key
, lxc_conf
, NULL
);
1195 value_dup
= strdup(value
);
1199 lxc_iterate_parts(token
, value_dup
, ",")
1202 if (num_groups
== 0)
1203 return clr_config_init_groups(key
, lxc_conf
, NULL
);
1205 init_groups
= zalloc(sizeof(gid_t
) * num_groups
);
1207 return ret_errno(ENOMEM
);
1209 /* Restore duplicated value so we can call lxc_iterate_parts() again. */
1210 strcpy(value_dup
, value
);
1212 lxc_iterate_parts(token
, value_dup
, ",") {
1217 ret
= lxc_safe_uint(token
, &group
);
1221 init_groups
[iter
++] = group
;
1224 lxc_conf
->init_groups
.size
= num_groups
;
1225 lxc_conf
->init_groups
.list
= move_ptr(init_groups
);
1230 static int set_config_hooks(const char *key
, const char *value
,
1231 struct lxc_conf
*lxc_conf
, void *data
)
1233 __do_free
char *copy
= NULL
;
1235 if (lxc_config_value_empty(value
))
1236 return lxc_clear_hooks(lxc_conf
, key
);
1238 if (strcmp(key
+ 4, "hook") == 0)
1239 return log_error_errno(-EINVAL
, EINVAL
, "lxc.hook must not have a value");
1241 copy
= strdup(value
);
1243 return ret_errno(ENOMEM
);
1245 if (strcmp(key
+ 9, "pre-start") == 0)
1246 return add_hook(lxc_conf
, LXCHOOK_PRESTART
, move_ptr(copy
));
1247 else if (strcmp(key
+ 9, "start-host") == 0)
1248 return add_hook(lxc_conf
, LXCHOOK_START_HOST
, move_ptr(copy
));
1249 else if (strcmp(key
+ 9, "pre-mount") == 0)
1250 return add_hook(lxc_conf
, LXCHOOK_PREMOUNT
, move_ptr(copy
));
1251 else if (strcmp(key
+ 9, "autodev") == 0)
1252 return add_hook(lxc_conf
, LXCHOOK_AUTODEV
, move_ptr(copy
));
1253 else if (strcmp(key
+ 9, "mount") == 0)
1254 return add_hook(lxc_conf
, LXCHOOK_MOUNT
, move_ptr(copy
));
1255 else if (strcmp(key
+ 9, "start") == 0)
1256 return add_hook(lxc_conf
, LXCHOOK_START
, move_ptr(copy
));
1257 else if (strcmp(key
+ 9, "stop") == 0)
1258 return add_hook(lxc_conf
, LXCHOOK_STOP
, move_ptr(copy
));
1259 else if (strcmp(key
+ 9, "post-stop") == 0)
1260 return add_hook(lxc_conf
, LXCHOOK_POSTSTOP
, move_ptr(copy
));
1261 else if (strcmp(key
+ 9, "clone") == 0)
1262 return add_hook(lxc_conf
, LXCHOOK_CLONE
, move_ptr(copy
));
1263 else if (strcmp(key
+ 9, "destroy") == 0)
1264 return add_hook(lxc_conf
, LXCHOOK_DESTROY
, move_ptr(copy
));
1266 return ret_errno(EINVAL
);
1269 static int set_config_hooks_version(const char *key
, const char *value
,
1270 struct lxc_conf
*lxc_conf
, void *data
)
1275 if (lxc_config_value_empty(value
))
1276 return clr_config_hooks_version(key
, lxc_conf
, NULL
);
1278 ret
= lxc_safe_uint(value
, &tmp
);
1283 return log_error_errno(-EINVAL
,
1284 EINVAL
, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
1286 lxc_conf
->hooks_version
= tmp
;
1291 static int set_config_personality(const char *key
, const char *value
,
1292 struct lxc_conf
*lxc_conf
, void *data
)
1294 signed long personality
;
1296 personality
= lxc_config_parse_arch(value
);
1297 if (personality
>= 0)
1298 lxc_conf
->personality
= personality
;
1300 WARN("Unsupported personality \"%s\"", value
);
1305 static int set_config_pty_max(const char *key
, const char *value
,
1306 struct lxc_conf
*lxc_conf
, void *data
)
1309 unsigned int max
= 0;
1311 if (lxc_config_value_empty(value
)) {
1312 lxc_conf
->pty_max
= 0;
1316 ret
= lxc_safe_uint(value
, &max
);
1318 return ret_errno(EINVAL
);
1320 lxc_conf
->pty_max
= max
;
1325 /* We only need to check whether the first byte of the key after the lxc.start.
1326 * prefix matches our expectations since they fortunately all start with a
1327 * different letter. If anything was wrong with the key we would have already
1328 * noticed when the callback was called.
1330 static int set_config_start(const char *key
, const char *value
,
1331 struct lxc_conf
*lxc_conf
, void *data
)
1336 is_empty
= lxc_config_value_empty(value
);
1338 if (*(key
+ 10) == 'a') { /* lxc.start.auto */
1340 lxc_conf
->start_auto
= 0;
1344 ret
= lxc_safe_uint(value
, &lxc_conf
->start_auto
);
1348 if (lxc_conf
->start_auto
> 1)
1349 return ret_errno(EINVAL
);
1352 } else if (*(key
+ 10) == 'd') { /* lxc.start.delay */
1354 lxc_conf
->start_delay
= 0;
1358 return lxc_safe_uint(value
, &lxc_conf
->start_delay
);
1359 } else if (*(key
+ 10) == 'o') { /* lxc.start.order */
1361 lxc_conf
->start_order
= 0;
1365 return lxc_safe_int(value
, &lxc_conf
->start_order
);
1368 return ret_errno(EINVAL
);
1371 static int set_config_monitor(const char *key
, const char *value
,
1372 struct lxc_conf
*lxc_conf
, void *data
)
1374 if (lxc_config_value_empty(value
)) {
1375 lxc_conf
->monitor_unshare
= 0;
1379 if (strcmp(key
+ 12, "unshare") == 0)
1380 return lxc_safe_uint(value
, &lxc_conf
->monitor_unshare
);
1382 return ret_errno(EINVAL
);
1385 static int set_config_monitor_signal_pdeath(const char *key
, const char *value
,
1386 struct lxc_conf
*lxc_conf
, void *data
)
1388 if (lxc_config_value_empty(value
)) {
1389 lxc_conf
->monitor_signal_pdeath
= 0;
1393 if (strcmp(key
+ 12, "signal.pdeath") == 0) {
1396 sig_n
= sig_parse(value
);
1398 return ret_errno(EINVAL
);
1400 lxc_conf
->monitor_signal_pdeath
= sig_n
;
1404 return ret_errno(EINVAL
);
1407 static int set_config_group(const char *key
, const char *value
,
1408 struct lxc_conf
*lxc_conf
, void *data
)
1410 __do_free
char *groups
= NULL
;
1413 if (lxc_config_value_empty(value
))
1414 return lxc_clear_groups(lxc_conf
);
1416 groups
= strdup(value
);
1418 return ret_errno(ENOMEM
);
1420 /* In case several groups are specified in a single line split these
1421 * groups in a single element for the list.
1423 lxc_iterate_parts(token
, groups
, " \t") {
1424 __do_free
struct lxc_list
*grouplist
= NULL
;
1426 grouplist
= malloc(sizeof(*grouplist
));
1428 return ret_errno(ENOMEM
);
1430 grouplist
->elem
= strdup(token
);
1431 if (!grouplist
->elem
)
1432 return ret_errno(ENOMEM
);
1434 lxc_list_add_tail(&lxc_conf
->groups
, move_ptr(grouplist
));
1440 static int set_config_environment(const char *key
, const char *value
,
1441 struct lxc_conf
*lxc_conf
, void *data
)
1443 __do_free
struct lxc_list
*list_item
= NULL
;
1445 if (lxc_config_value_empty(value
))
1446 return lxc_clear_environment(lxc_conf
);
1448 list_item
= malloc(sizeof(*list_item
));
1450 return ret_errno(ENOMEM
);
1452 if (!strchr(value
, '=')) {
1453 const char *env_val
;
1454 const char *env_key
= value
;
1455 const char *env_var
[3] = {0};
1457 env_val
= getenv(env_key
);
1459 return ret_errno(ENOENT
);
1461 env_var
[0] = env_key
;
1462 env_var
[1] = env_val
;
1463 list_item
->elem
= lxc_string_join("=", env_var
, false);
1465 list_item
->elem
= strdup(value
);
1468 if (!list_item
->elem
)
1469 return ret_errno(ENOMEM
);
1471 lxc_list_add_tail(&lxc_conf
->environment
, move_ptr(list_item
));
1476 static int set_config_tty_max(const char *key
, const char *value
,
1477 struct lxc_conf
*lxc_conf
, void *data
)
1480 unsigned int nbtty
= 0;
1482 if (lxc_config_value_empty(value
)) {
1483 lxc_conf
->ttys
.max
= 0;
1487 ret
= lxc_safe_uint(value
, &nbtty
);
1491 lxc_conf
->ttys
.max
= nbtty
;
1496 static int set_config_tty_dir(const char *key
, const char *value
,
1497 struct lxc_conf
*lxc_conf
, void *data
)
1499 return set_config_string_item_max(&lxc_conf
->ttys
.dir
, value
,
1503 static int set_config_apparmor_profile(const char *key
, const char *value
,
1504 struct lxc_conf
*lxc_conf
, void *data
)
1506 return set_config_string_item(&lxc_conf
->lsm_aa_profile
, value
);
1509 static int set_config_apparmor_allow_incomplete(const char *key
,
1511 struct lxc_conf
*lxc_conf
,
1516 if (lxc_config_value_empty(value
)) {
1517 lxc_conf
->lsm_aa_allow_incomplete
= 0;
1521 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_incomplete
);
1525 if (lxc_conf
->lsm_aa_allow_incomplete
> 1)
1526 return ret_errno(EINVAL
);
1531 static int set_config_apparmor_allow_nesting(const char *key
,
1533 struct lxc_conf
*lxc_conf
,
1538 if (lxc_config_value_empty(value
))
1539 return clr_config_apparmor_allow_nesting(key
, lxc_conf
, NULL
);
1541 ret
= lxc_safe_uint(value
, &lxc_conf
->lsm_aa_allow_nesting
);
1545 if (lxc_conf
->lsm_aa_allow_nesting
> 1)
1546 return ret_errno(EINVAL
);
1551 static int set_config_apparmor_raw(const char *key
,
1553 struct lxc_conf
*lxc_conf
,
1556 __do_free
char *elem
= NULL
;
1557 __do_free
struct lxc_list
*list
= NULL
;
1559 if (lxc_config_value_empty(value
))
1560 return lxc_clear_apparmor_raw(lxc_conf
);
1562 list
= malloc(sizeof(*list
));
1564 return ret_errno(ENOMEM
);
1566 elem
= strdup(value
);
1568 return ret_errno(ENOMEM
);
1570 list
->elem
= move_ptr(elem
);
1571 lxc_list_add_tail(&lxc_conf
->lsm_aa_raw
, move_ptr(list
));
1576 static int set_config_selinux_context(const char *key
, const char *value
,
1577 struct lxc_conf
*lxc_conf
, void *data
)
1579 return set_config_string_item(&lxc_conf
->lsm_se_context
, value
);
1582 static int set_config_selinux_context_keyring(const char *key
, const char *value
,
1583 struct lxc_conf
*lxc_conf
, void *data
)
1585 return set_config_string_item(&lxc_conf
->lsm_se_keyring_context
, value
);
1588 static int set_config_keyring_session(const char *key
, const char *value
,
1589 struct lxc_conf
*lxc_conf
, void *data
)
1591 return set_config_bool_item(&lxc_conf
->keyring_disable_session
, value
, false);
1594 static int set_config_log_file(const char *key
, const char *value
,
1595 struct lxc_conf
*c
, void *data
)
1599 if (lxc_config_value_empty(value
)) {
1600 free_disarm(c
->logfile
);
1605 * Store these values in the lxc_conf, and then try to set for actual
1608 ret
= set_config_path_item(&c
->logfile
, value
);
1610 ret
= lxc_log_set_file(&c
->logfd
, c
->logfile
);
1615 static int set_config_log_level(const char *key
, const char *value
,
1616 struct lxc_conf
*lxc_conf
, void *data
)
1620 if (lxc_config_value_empty(value
)) {
1621 lxc_conf
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
1625 if (value
[0] >= '0' && value
[0] <= '9') {
1628 ret
= lxc_safe_int(value
, &newlevel
);
1630 return ret_errno(EINVAL
);
1632 newlevel
= lxc_log_priority_to_int(value
);
1636 * Store these values in the lxc_conf, and then try to set for actual
1639 lxc_conf
->loglevel
= newlevel
;
1641 return lxc_log_set_level(&lxc_conf
->loglevel
, newlevel
);
1644 static int set_config_autodev(const char *key
, const char *value
,
1645 struct lxc_conf
*lxc_conf
, void *data
)
1649 if (lxc_config_value_empty(value
)) {
1650 lxc_conf
->autodev
= 0;
1654 ret
= lxc_safe_uint(value
, &lxc_conf
->autodev
);
1656 return ret_errno(EINVAL
);
1658 if (lxc_conf
->autodev
> 1)
1659 return ret_errno(EINVAL
);
1664 static int set_config_autodev_tmpfs_size(const char *key
, const char *value
,
1665 struct lxc_conf
*lxc_conf
, void *data
)
1667 if (lxc_config_value_empty(value
)) {
1668 lxc_conf
->autodevtmpfssize
= 500000;
1672 if (lxc_safe_int(value
, &lxc_conf
->autodevtmpfssize
) < 0)
1673 lxc_conf
->autodevtmpfssize
= 500000;
1678 static int set_config_signal_halt(const char *key
, const char *value
,
1679 struct lxc_conf
*lxc_conf
, void *data
)
1683 if (lxc_config_value_empty(value
)) {
1684 lxc_conf
->haltsignal
= 0;
1688 sig_n
= sig_parse(value
);
1690 return ret_errno(EINVAL
);
1692 lxc_conf
->haltsignal
= sig_n
;
1697 static int set_config_signal_reboot(const char *key
, const char *value
,
1698 struct lxc_conf
*lxc_conf
, void *data
)
1702 if (lxc_config_value_empty(value
)) {
1703 lxc_conf
->rebootsignal
= 0;
1707 sig_n
= sig_parse(value
);
1709 return ret_errno(EINVAL
);
1711 lxc_conf
->rebootsignal
= sig_n
;
1716 static int set_config_signal_stop(const char *key
, const char *value
,
1717 struct lxc_conf
*lxc_conf
, void *data
)
1721 if (lxc_config_value_empty(value
)) {
1722 lxc_conf
->stopsignal
= 0;
1726 sig_n
= sig_parse(value
);
1728 return ret_errno(EINVAL
);
1730 lxc_conf
->stopsignal
= sig_n
;
1735 static int __set_config_cgroup_controller(const char *key
, const char *value
,
1736 struct lxc_conf
*lxc_conf
, int version
)
1738 __do_free
struct lxc_list
*cglist
= NULL
;
1739 call_cleaner(free_lxc_cgroup
) struct lxc_cgroup
*cgelem
= NULL
;
1740 const char *subkey
, *token
;
1743 if (lxc_config_value_empty(value
))
1744 return lxc_clear_cgroups(lxc_conf
, key
, version
);
1746 if (version
== CGROUP2_SUPER_MAGIC
) {
1747 token
= "lxc.cgroup2.";
1749 } else if (version
== CGROUP_SUPER_MAGIC
) {
1750 token
= "lxc.cgroup.";
1753 return ret_errno(EINVAL
);
1756 if (strncmp(key
, token
, token_len
) != 0)
1757 return ret_errno(EINVAL
);
1759 subkey
= key
+ token_len
;
1760 if (*subkey
== '\0')
1761 return ret_errno(EINVAL
);
1763 cglist
= malloc(sizeof(*cglist
));
1765 return ret_errno(ENOMEM
);
1767 cgelem
= malloc(sizeof(*cgelem
));
1769 return ret_errno(ENOMEM
);
1770 memset(cgelem
, 0, sizeof(*cgelem
));
1772 cgelem
->subsystem
= strdup(subkey
);
1773 if (!cgelem
->subsystem
)
1774 return ret_errno(ENOMEM
);
1776 cgelem
->value
= strdup(value
);
1778 return ret_errno(ENOMEM
);
1780 cgelem
->version
= version
;
1782 lxc_list_add_elem(cglist
, move_ptr(cgelem
));
1784 if (version
== CGROUP2_SUPER_MAGIC
)
1785 lxc_list_add_tail(&lxc_conf
->cgroup2
, cglist
);
1787 lxc_list_add_tail(&lxc_conf
->cgroup
, cglist
);
1793 static int set_config_cgroup_controller(const char *key
, const char *value
,
1794 struct lxc_conf
*lxc_conf
, void *data
)
1796 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1797 CGROUP_SUPER_MAGIC
);
1800 static int set_config_cgroup2_controller(const char *key
, const char *value
,
1801 struct lxc_conf
*lxc_conf
, void *data
)
1803 return __set_config_cgroup_controller(key
, value
, lxc_conf
,
1804 CGROUP2_SUPER_MAGIC
);
1808 static int set_config_cgroup_dir(const char *key
, const char *value
,
1809 struct lxc_conf
*lxc_conf
, void *data
)
1811 if (strcmp(key
, "lxc.cgroup.dir") != 0)
1812 return ret_errno(EINVAL
);
1814 if (lxc_config_value_empty(value
))
1815 return clr_config_cgroup_dir(key
, lxc_conf
, NULL
);
1817 return set_config_string_item(&lxc_conf
->cgroup_meta
.dir
, value
);
1820 static int set_config_cgroup_monitor_dir(const char *key
, const char *value
,
1821 struct lxc_conf
*lxc_conf
, void *data
)
1823 if (lxc_config_value_empty(value
))
1824 return clr_config_cgroup_monitor_dir(key
, lxc_conf
, NULL
);
1826 return set_config_string_item(&lxc_conf
->cgroup_meta
.monitor_dir
,
1830 static int set_config_cgroup_monitor_pivot_dir(const char *key
, const char *value
,
1831 struct lxc_conf
*lxc_conf
, void *data
)
1833 if (lxc_config_value_empty(value
))
1834 return clr_config_cgroup_monitor_pivot_dir(key
, lxc_conf
, NULL
);
1836 return set_config_string_item(&lxc_conf
->cgroup_meta
.monitor_pivot_dir
,
1840 static int set_config_cgroup_container_dir(const char *key
, const char *value
,
1841 struct lxc_conf
*lxc_conf
,
1844 if (lxc_config_value_empty(value
))
1845 return clr_config_cgroup_container_dir(key
, lxc_conf
, NULL
);
1847 return set_config_string_item(&lxc_conf
->cgroup_meta
.container_dir
,
1851 static int set_config_cgroup_container_inner_dir(const char *key
,
1853 struct lxc_conf
*lxc_conf
,
1856 if (lxc_config_value_empty(value
))
1857 return clr_config_cgroup_container_inner_dir(key
, lxc_conf
, NULL
);
1859 if (strchr(value
, '/') ||
1860 strcmp(value
, ".") == 0 ||
1861 strcmp(value
, "..") == 0)
1862 return log_error_errno(-EINVAL
, EINVAL
, "lxc.cgroup.dir.container.inner must be a single directory name");
1864 return set_config_string_item(&lxc_conf
->cgroup_meta
.namespace_dir
, value
);
1867 static int set_config_cgroup_relative(const char *key
, const char *value
,
1868 struct lxc_conf
*lxc_conf
, void *data
)
1870 unsigned int converted
;
1873 if (lxc_config_value_empty(value
))
1874 return clr_config_cgroup_relative(key
, lxc_conf
, NULL
);
1876 ret
= lxc_safe_uint(value
, &converted
);
1880 if (converted
== 1) {
1881 lxc_conf
->cgroup_meta
.relative
= true;
1885 if (converted
== 0) {
1886 lxc_conf
->cgroup_meta
.relative
= false;
1890 return ret_errno(EINVAL
);
1893 static bool parse_limit_value(const char **value
, rlim_t
*res
)
1895 char *endptr
= NULL
;
1897 if (strncmp(*value
, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1898 *res
= RLIM_INFINITY
;
1899 *value
+= STRLITERALLEN("unlimited");
1904 *res
= strtoull(*value
, &endptr
, 10);
1905 if (errno
|| !endptr
)
1913 static int set_config_prlimit(const char *key
, const char *value
,
1914 struct lxc_conf
*lxc_conf
, void *data
)
1916 __do_free
struct lxc_list
*limlist
= NULL
;
1917 call_cleaner(free_lxc_limit
) struct lxc_limit
*limelem
= NULL
;
1918 struct lxc_list
*iter
;
1919 struct rlimit limit
;
1922 if (lxc_config_value_empty(value
))
1923 return lxc_clear_limits(lxc_conf
, key
);
1925 if (strncmp(key
, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
1926 return ret_errno(EINVAL
);
1928 key
+= STRLITERALLEN("lxc.prlimit.");
1930 /* soft limit comes first in the value */
1931 if (!parse_limit_value(&value
, &limit_value
))
1932 return ret_errno(EINVAL
);
1934 limit
.rlim_cur
= limit_value
;
1936 /* skip spaces and a colon */
1937 while (isspace(*value
))
1942 else if (*value
) /* any other character is an error here */
1943 return ret_errno(EINVAL
);
1945 while (isspace(*value
))
1948 /* optional hard limit */
1950 if (!parse_limit_value(&value
, &limit_value
))
1951 return ret_errno(EINVAL
);
1953 limit
.rlim_max
= limit_value
;
1955 /* check for trailing garbage */
1956 while (isspace(*value
))
1960 return ret_errno(EINVAL
);
1962 /* a single value sets both hard and soft limit */
1963 limit
.rlim_max
= limit
.rlim_cur
;
1966 /* find existing list element */
1967 lxc_list_for_each(iter
, &lxc_conf
->limits
) {
1968 limelem
= iter
->elem
;
1969 if (!strcmp(key
, limelem
->resource
)) {
1970 limelem
->limit
= limit
;
1975 /* allocate list element */
1976 limlist
= malloc(sizeof(*limlist
));
1978 return ret_errno(ENOMEM
);
1980 limelem
= malloc(sizeof(*limelem
));
1982 return ret_errno(ENOMEM
);
1983 memset(limelem
, 0, sizeof(*limelem
));
1985 limelem
->resource
= strdup(key
);
1986 if (!limelem
->resource
)
1987 return ret_errno(ENOMEM
);
1989 limelem
->limit
= limit
;
1990 lxc_list_add_elem(limlist
, move_ptr(limelem
));;
1991 lxc_list_add_tail(&lxc_conf
->limits
, move_ptr(limlist
));
1996 static int set_config_sysctl(const char *key
, const char *value
,
1997 struct lxc_conf
*lxc_conf
, void *data
)
1999 __do_free
struct lxc_list
*sysctl_list
= NULL
;
2000 call_cleaner(free_lxc_sysctl
) struct lxc_sysctl
*sysctl_elem
= NULL
;
2001 struct lxc_list
*iter
;
2003 if (lxc_config_value_empty(value
))
2004 return clr_config_sysctl(key
, lxc_conf
, NULL
);
2006 if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
2009 key
+= STRLITERALLEN("lxc.sysctl.");
2011 /* find existing list element */
2012 lxc_list_for_each(iter
, &lxc_conf
->sysctls
) {
2013 __do_free
char *replace_value
= NULL
;
2015 sysctl_elem
= iter
->elem
;
2017 if (strcmp(key
, sysctl_elem
->key
) != 0)
2020 replace_value
= strdup(value
);
2022 return ret_errno(EINVAL
);
2024 free(sysctl_elem
->value
);
2025 sysctl_elem
->value
= move_ptr(replace_value
);
2030 /* allocate list element */
2031 sysctl_list
= malloc(sizeof(*sysctl_list
));
2033 return ret_errno(ENOMEM
);
2035 sysctl_elem
= malloc(sizeof(*sysctl_elem
));
2037 return ret_errno(ENOMEM
);
2038 memset(sysctl_elem
, 0, sizeof(*sysctl_elem
));
2040 sysctl_elem
->key
= strdup(key
);
2041 if (!sysctl_elem
->key
)
2042 return ret_errno(ENOMEM
);
2044 sysctl_elem
->value
= strdup(value
);
2045 if (!sysctl_elem
->value
)
2046 return ret_errno(ENOMEM
);
2048 lxc_list_add_elem(sysctl_list
, move_ptr(sysctl_elem
));
2049 lxc_list_add_tail(&lxc_conf
->sysctls
, move_ptr(sysctl_list
));
2054 static int set_config_proc(const char *key
, const char *value
,
2055 struct lxc_conf
*lxc_conf
, void *data
)
2057 __do_free
struct lxc_list
*proclist
= NULL
;
2058 call_cleaner(free_lxc_proc
) struct lxc_proc
*procelem
= NULL
;
2061 if (lxc_config_value_empty(value
))
2062 return clr_config_proc(key
, lxc_conf
, NULL
);
2064 if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
2067 subkey
= key
+ STRLITERALLEN("lxc.proc.");
2068 if (*subkey
== '\0')
2069 return ret_errno(EINVAL
);
2071 proclist
= malloc(sizeof(*proclist
));
2073 return ret_errno(ENOMEM
);
2075 procelem
= malloc(sizeof(*procelem
));
2077 return ret_errno(ENOMEM
);
2078 memset(procelem
, 0, sizeof(*procelem
));
2080 procelem
->filename
= strdup(subkey
);
2081 if (!procelem
->filename
)
2082 return ret_errno(ENOMEM
);
2084 procelem
->value
= strdup(value
);
2085 if (!procelem
->value
)
2086 return ret_errno(ENOMEM
);
2088 proclist
->elem
= move_ptr(procelem
);
2089 lxc_list_add_tail(&lxc_conf
->procs
, move_ptr(proclist
));
2094 static int set_config_idmaps(const char *key
, const char *value
,
2095 struct lxc_conf
*lxc_conf
, void *data
)
2097 __do_free
struct lxc_list
*idmaplist
= NULL
;
2098 __do_free
struct id_map
*idmap
= NULL
;
2099 unsigned long hostid
, nsid
, range
;
2103 if (lxc_config_value_empty(value
))
2104 return lxc_clear_idmaps(lxc_conf
);
2106 idmaplist
= malloc(sizeof(*idmaplist
));
2108 return ret_errno(ENOMEM
);
2110 idmap
= malloc(sizeof(*idmap
));
2112 return ret_errno(ENOMEM
);
2113 memset(idmap
, 0, sizeof(*idmap
));
2115 ret
= parse_idmaps(value
, &type
, &nsid
, &hostid
, &range
);
2117 return log_error_errno(-EINVAL
, EINVAL
, "Failed to parse id mappings");
2119 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type
, nsid
, hostid
, range
);
2121 idmap
->idtype
= ID_TYPE_UID
;
2122 else if (type
== 'g')
2123 idmap
->idtype
= ID_TYPE_GID
;
2125 return ret_errno(EINVAL
);
2127 idmap
->hostid
= hostid
;
2129 idmap
->range
= range
;
2130 idmaplist
->elem
= idmap
;
2131 lxc_list_add_tail(&lxc_conf
->id_map
, idmaplist
);
2133 if (!lxc_conf
->root_nsuid_map
&& idmap
->idtype
== ID_TYPE_UID
)
2134 if (idmap
->nsid
== 0)
2135 lxc_conf
->root_nsuid_map
= idmap
;
2137 if (!lxc_conf
->root_nsgid_map
&& idmap
->idtype
== ID_TYPE_GID
)
2138 if (idmap
->nsid
== 0)
2139 lxc_conf
->root_nsgid_map
= idmap
;
2142 move_ptr(idmaplist
);
2147 static int set_config_mount_fstab(const char *key
, const char *value
,
2148 struct lxc_conf
*lxc_conf
, void *data
)
2150 if (lxc_config_value_empty(value
)) {
2151 clr_config_mount_fstab(key
, lxc_conf
, NULL
);
2152 return ret_errno(EINVAL
);
2155 return set_config_path_item(&lxc_conf
->fstab
, value
);
2158 static int set_config_mount_auto(const char *key
, const char *value
,
2159 struct lxc_conf
*lxc_conf
, void *data
)
2161 __do_free
char *autos
= NULL
;
2168 } allowed_auto_mounts
[] = {
2169 { "proc", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2170 { "proc:mixed", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_MIXED
},
2171 { "proc:rw", LXC_AUTO_PROC_MASK
, LXC_AUTO_PROC_RW
},
2172 { "sys", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2173 { "sys:ro", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RO
},
2174 { "sys:mixed", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_MIXED
},
2175 { "sys:rw", LXC_AUTO_SYS_MASK
, LXC_AUTO_SYS_RW
},
2176 { "cgroup", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
},
2177 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
},
2178 { "cgroup:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
},
2179 { "cgroup:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
},
2180 { "cgroup:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2181 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2182 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RO
| LXC_AUTO_CGROUP_FORCE
},
2183 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_RW
| LXC_AUTO_CGROUP_FORCE
},
2184 { "cgroup-full", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
},
2185 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
},
2186 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
},
2187 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
},
2188 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_NOSPEC
| LXC_AUTO_CGROUP_FORCE
},
2189 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_MIXED
| LXC_AUTO_CGROUP_FORCE
},
2190 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RO
| LXC_AUTO_CGROUP_FORCE
},
2191 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK
, LXC_AUTO_CGROUP_FULL_RW
| LXC_AUTO_CGROUP_FORCE
},
2192 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK
, LXC_AUTO_SHMOUNTS
},
2194 * For adding anything that is just a single on/off, but has no
2195 * options: keep mask and flag identical and just define the
2196 * enum value as an unused bit so far
2201 if (lxc_config_value_empty(value
)) {
2202 lxc_conf
->auto_mounts
= 0;
2206 autos
= strdup(value
);
2208 return ret_errno(ENOMEM
);
2210 lxc_iterate_parts(token
, autos
, " \t") {
2211 bool is_shmounts
= false;
2213 for (i
= 0; allowed_auto_mounts
[i
].token
; i
++) {
2214 if (!strcmp(allowed_auto_mounts
[i
].token
, token
))
2217 if (strcmp("shmounts:", allowed_auto_mounts
[i
].token
) == 0 &&
2218 strncmp("shmounts:", token
, STRLITERALLEN("shmounts:")) == 0) {
2224 if (!allowed_auto_mounts
[i
].token
)
2225 return log_error_errno(-EINVAL
, EINVAL
, "Invalid filesystem to automount \"%s\"", token
);
2227 lxc_conf
->auto_mounts
&= ~allowed_auto_mounts
[i
].mask
;
2228 lxc_conf
->auto_mounts
|= allowed_auto_mounts
[i
].flag
;
2231 __do_free
char *container_path
= NULL
, *host_path
= NULL
;
2234 val
= token
+ STRLITERALLEN("shmounts:");
2236 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2238 host_path
= strdup(val
);
2240 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts host path");
2242 val
= strchr(host_path
, ':');
2243 if (!val
|| *(val
+ 1) == '\0')
2244 val
= "/dev/.lxc-mounts";
2248 container_path
= strdup(val
);
2250 return log_error_errno(-EINVAL
, EINVAL
, "Failed to copy shmounts container path");
2252 lxc_conf
->shmount
.path_host
= move_ptr(host_path
);
2253 lxc_conf
->shmount
.path_cont
= move_ptr(container_path
);
2260 static int set_config_mount(const char *key
, const char *value
,
2261 struct lxc_conf
*lxc_conf
, void *data
)
2263 __do_free
char *mntelem
= NULL
;
2264 __do_free
struct lxc_list
*mntlist
= NULL
;
2266 if (lxc_config_value_empty(value
))
2267 return lxc_clear_mount_entries(lxc_conf
);
2269 mntlist
= malloc(sizeof(*mntlist
));
2271 return ret_errno(ENOMEM
);
2273 mntelem
= strdup(value
);
2275 return ret_errno(ENOMEM
);
2277 mntlist
->elem
= move_ptr(mntelem
);
2278 lxc_list_add_tail(&lxc_conf
->mount_list
, move_ptr(mntlist
));
2283 int add_elem_to_mount_list(const char *value
, struct lxc_conf
*lxc_conf
) {
2284 return set_config_mount(NULL
, value
, lxc_conf
, NULL
);
2287 static int set_config_cap_keep(const char *key
, const char *value
,
2288 struct lxc_conf
*lxc_conf
, void *data
)
2290 __do_free
char *keepcaps
= NULL
;
2291 __do_free
struct lxc_list
*keeplist
= NULL
;
2294 if (lxc_config_value_empty(value
))
2295 return lxc_clear_config_keepcaps(lxc_conf
);
2297 keepcaps
= strdup(value
);
2299 return ret_errno(ENOMEM
);
2301 /* In case several capability keep is specified in a single line
2302 * split these caps in a single element for the list.
2304 lxc_iterate_parts(token
, keepcaps
, " \t") {
2305 if (!strcmp(token
, "none"))
2306 lxc_clear_config_keepcaps(lxc_conf
);
2308 keeplist
= malloc(sizeof(*keeplist
));
2310 return ret_errno(ENOMEM
);
2312 keeplist
->elem
= strdup(token
);
2313 if (!keeplist
->elem
)
2314 return ret_errno(ENOMEM
);
2316 lxc_list_add_tail(&lxc_conf
->keepcaps
, move_ptr(keeplist
));
2322 static int set_config_cap_drop(const char *key
, const char *value
,
2323 struct lxc_conf
*lxc_conf
, void *data
)
2325 __do_free
char *dropcaps
= NULL
;
2326 __do_free
struct lxc_list
*droplist
= NULL
;
2329 if (lxc_config_value_empty(value
))
2330 return lxc_clear_config_caps(lxc_conf
);
2332 dropcaps
= strdup(value
);
2334 return ret_errno(ENOMEM
);
2336 /* In case several capability drop is specified in a single line
2337 * split these caps in a single element for the list.
2339 lxc_iterate_parts(token
, dropcaps
, " \t") {
2340 droplist
= malloc(sizeof(*droplist
));
2342 return ret_errno(ENOMEM
);
2344 droplist
->elem
= strdup(token
);
2345 if (!droplist
->elem
)
2346 return ret_errno(ENOMEM
);
2348 lxc_list_add_tail(&lxc_conf
->caps
, move_ptr(droplist
));
2354 static int set_config_console_path(const char *key
, const char *value
,
2355 struct lxc_conf
*lxc_conf
, void *data
)
2357 return set_config_path_item(&lxc_conf
->console
.path
, value
);
2360 static int set_config_console_rotate(const char *key
, const char *value
,
2361 struct lxc_conf
*lxc_conf
, void *data
)
2365 if (lxc_config_value_empty(value
)) {
2366 lxc_conf
->console
.log_rotate
= 0;
2370 ret
= lxc_safe_uint(value
, &lxc_conf
->console
.log_rotate
);
2372 return ret_errno(EINVAL
);
2374 if (lxc_conf
->console
.log_rotate
> 1)
2375 return log_error_errno(-EINVAL
, EINVAL
, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
2380 static int set_config_console_logfile(const char *key
, const char *value
,
2381 struct lxc_conf
*lxc_conf
, void *data
)
2383 return set_config_path_item(&lxc_conf
->console
.log_path
, value
);
2386 static int set_config_console_buffer_size(const char *key
, const char *value
,
2387 struct lxc_conf
*lxc_conf
, void *data
)
2391 uint64_t buffer_size
, pgsz
;
2393 if (lxc_config_value_empty(value
)) {
2394 lxc_conf
->console
.buffer_size
= 0;
2398 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2399 if (!strcmp(value
, "auto")) {
2400 lxc_conf
->console
.buffer_size
= 1 << 17;
2404 ret
= parse_byte_size_string(value
, &size
);
2409 return ret_errno(EINVAL
);
2411 /* must be at least a page size */
2412 pgsz
= lxc_getpagesize();
2413 if ((uint64_t)size
< pgsz
) {
2414 NOTICE("Requested ringbuffer size for the console is %" PRId64
" but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2419 buffer_size
= lxc_find_next_power2((uint64_t)size
);
2420 if (buffer_size
== 0)
2421 return ret_errno(EINVAL
);
2423 if (buffer_size
!= size
)
2424 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", buffer_size
);
2426 lxc_conf
->console
.buffer_size
= buffer_size
;
2431 static int set_config_console_size(const char *key
, const char *value
,
2432 struct lxc_conf
*lxc_conf
, void *data
)
2436 uint64_t log_size
, pgsz
;
2438 if (lxc_config_value_empty(value
)) {
2439 lxc_conf
->console
.log_size
= 0;
2443 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2444 if (!strcmp(value
, "auto")) {
2445 lxc_conf
->console
.log_size
= 1 << 17;
2449 ret
= parse_byte_size_string(value
, &size
);
2451 return ret_errno(EINVAL
);
2454 return ret_errno(EINVAL
);
2456 /* must be at least a page size */
2457 pgsz
= lxc_getpagesize();
2458 if ((uint64_t)size
< pgsz
) {
2459 NOTICE("Requested ringbuffer size for the console is %" PRId64
" but must be at least %" PRId64
" bytes. Setting ringbuffer size to %" PRId64
" bytes",
2464 log_size
= lxc_find_next_power2((uint64_t)size
);
2466 return ret_errno(EINVAL
);
2468 if (log_size
!= size
)
2469 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64
" bytes", log_size
);
2471 lxc_conf
->console
.log_size
= log_size
;
2477 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2478 * file, we expand it in the unexpanded_config, so that after a save_config we
2479 * store the hwaddr for re-use.
2480 * This is only called when reading the config file, not when executing a
2482 * 'x' and 'X' are substituted in-place.
2484 static void update_hwaddr(const char *line
)
2488 line
+= lxc_char_left_gc(line
, strlen(line
));
2492 if (!lxc_config_net_is_hwaddr(line
))
2495 /* Let config_net_hwaddr raise the error. */
2496 p
= strchr(line
, '=');
2507 rand_complete_hwaddr(p
);
2510 int append_unexp_config_line(const char *line
, struct lxc_conf
*conf
)
2513 size_t len
= conf
->unexpanded_len
;
2515 update_hwaddr(line
);
2517 linelen
= strlen(line
);
2518 while (conf
->unexpanded_alloced
<= len
+ linelen
+ 2) {
2521 tmp
= realloc(conf
->unexpanded_config
, conf
->unexpanded_alloced
+ 1024);
2523 return ret_errno(EINVAL
);
2525 if (!conf
->unexpanded_config
)
2528 conf
->unexpanded_config
= tmp
;
2529 conf
->unexpanded_alloced
+= 1024;
2532 memcpy(conf
->unexpanded_config
+ conf
->unexpanded_len
, line
, linelen
);
2533 conf
->unexpanded_len
+= linelen
;
2534 if (line
[linelen
- 1] != '\n')
2535 conf
->unexpanded_config
[conf
->unexpanded_len
++] = '\n';
2536 conf
->unexpanded_config
[conf
->unexpanded_len
] = '\0';
2541 static int do_includedir(const char *dirp
, struct lxc_conf
*lxc_conf
)
2543 __do_closedir
DIR *dir
= NULL
;
2544 struct dirent
*direntp
;
2547 dir
= opendir(dirp
);
2551 while ((direntp
= readdir(dir
))) {
2553 char path
[PATH_MAX
];
2555 fnam
= direntp
->d_name
;
2556 if (!strcmp(fnam
, "."))
2559 if (!strcmp(fnam
, ".."))
2563 if (len
< 6 || strncmp(fnam
+ len
- 5, ".conf", 5) != 0)
2566 len
= snprintf(path
, PATH_MAX
, "%s/%s", dirp
, fnam
);
2567 if (len
< 0 || len
>= PATH_MAX
)
2568 return ret_errno(EIO
);
2570 ret
= lxc_config_read(path
, lxc_conf
, true);
2578 static int set_config_includefiles(const char *key
, const char *value
,
2579 struct lxc_conf
*lxc_conf
, void *data
)
2581 if (lxc_config_value_empty(value
)) {
2582 clr_config_includefiles(key
, lxc_conf
, NULL
);
2587 return do_includedir(value
, lxc_conf
);
2589 return lxc_config_read(value
, lxc_conf
, true);
2592 static int set_config_rootfs_path(const char *key
, const char *value
,
2593 struct lxc_conf
*lxc_conf
, void *data
)
2595 __do_free
char *dup
= NULL
;
2598 const char *container_path
;
2600 if (lxc_config_value_empty(value
)) {
2601 free(lxc_conf
->rootfs
.path
);
2602 lxc_conf
->rootfs
.path
= NULL
;
2606 dup
= strdup(value
);
2608 return ret_errno(ENOMEM
);
2610 /* Split <storage type>:<container path> into <storage type> and
2611 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2612 * "rootfs.path" to <container path>.
2614 tmp
= strchr(dup
, ':');
2618 ret
= set_config_path_item(&lxc_conf
->rootfs
.bdev_type
, dup
);
2620 return ret_errno(ENOMEM
);
2623 container_path
= tmp
;
2625 container_path
= value
;
2628 return set_config_path_item(&lxc_conf
->rootfs
.path
, container_path
);
2631 static int set_config_rootfs_managed(const char *key
, const char *value
,
2632 struct lxc_conf
*lxc_conf
, void *data
)
2634 return set_config_bool_item(&lxc_conf
->rootfs
.managed
, value
, true);
2637 static int set_config_rootfs_mount(const char *key
, const char *value
,
2638 struct lxc_conf
*lxc_conf
, void *data
)
2640 return set_config_path_item(&lxc_conf
->rootfs
.mount
, value
);
2643 static int set_config_rootfs_options(const char *key
, const char *value
,
2644 struct lxc_conf
*lxc_conf
, void *data
)
2646 __do_free
char *mdata
= NULL
, *opts
= NULL
;
2647 unsigned long mflags
= 0, pflags
= 0;
2648 struct lxc_rootfs
*rootfs
= &lxc_conf
->rootfs
;
2651 ret
= parse_mntopts(value
, &mflags
, &mdata
);
2653 return ret_errno(EINVAL
);
2655 ret
= parse_propagationopts(value
, &pflags
);
2657 return ret_errno(EINVAL
);
2659 ret
= set_config_string_item(&opts
, value
);
2661 return ret_errno(ENOMEM
);
2663 rootfs
->mountflags
= mflags
| pflags
;
2664 rootfs
->options
= move_ptr(opts
);
2665 rootfs
->data
= move_ptr(mdata
);
2670 static int set_config_uts_name(const char *key
, const char *value
,
2671 struct lxc_conf
*lxc_conf
, void *data
)
2673 __do_free
struct utsname
*utsname
= NULL
;
2675 if (lxc_config_value_empty(value
)) {
2676 clr_config_uts_name(key
, lxc_conf
, NULL
);
2680 utsname
= malloc(sizeof(*utsname
));
2682 return ret_errno(ENOMEM
);
2684 if (strlen(value
) >= sizeof(utsname
->nodename
))
2685 return ret_errno(EINVAL
);
2687 (void)strlcpy(utsname
->nodename
, value
, sizeof(utsname
->nodename
));
2688 free(lxc_conf
->utsname
);
2689 lxc_conf
->utsname
= move_ptr(utsname
);
2694 static int set_config_namespace_clone(const char *key
, const char *value
,
2695 struct lxc_conf
*lxc_conf
, void *data
)
2697 __do_free
char *ns
= NULL
;
2701 if (lxc_config_value_empty(value
))
2702 return clr_config_namespace_clone(key
, lxc_conf
, data
);
2704 if (lxc_conf
->ns_keep
!= 0)
2705 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2709 return ret_errno(ENOMEM
);
2711 lxc_iterate_parts(token
, ns
, " \t") {
2712 token
+= lxc_char_left_gc(token
, strlen(token
));
2713 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2714 cloneflag
= lxc_namespace_2_cloneflag(token
);
2716 return ret_errno(EINVAL
);
2717 lxc_conf
->ns_clone
|= cloneflag
;
2723 static int set_config_namespace_keep(const char *key
, const char *value
,
2724 struct lxc_conf
*lxc_conf
, void *data
)
2726 __do_free
char *ns
= NULL
;
2730 if (lxc_config_value_empty(value
))
2731 return clr_config_namespace_keep(key
, lxc_conf
, data
);
2733 if (lxc_conf
->ns_clone
!= 0)
2734 return log_error_errno(-EINVAL
, EINVAL
, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
2738 return ret_errno(ENOMEM
);
2740 lxc_iterate_parts(token
, ns
, " \t") {
2741 token
+= lxc_char_left_gc(token
, strlen(token
));
2742 token
[lxc_char_right_gc(token
, strlen(token
))] = '\0';
2743 cloneflag
= lxc_namespace_2_cloneflag(token
);
2745 return ret_errno(EINVAL
);
2746 lxc_conf
->ns_keep
|= cloneflag
;
2752 static int set_config_time_offset_boot(const char *key
, const char *value
,
2753 struct lxc_conf
*lxc_conf
, void *data
)
2758 char buf
[STRLITERALLEN("ms") + 1];
2760 if (lxc_config_value_empty(value
))
2761 return clr_config_time_offset_boot(key
, lxc_conf
, data
);
2763 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2767 unit
= lxc_trim_whitespace_in_place(buf
);
2768 if (strcmp(unit
, "h") == 0) {
2769 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_boot
))
2770 return ret_errno(EOVERFLOW
);
2771 } else if (strcmp(unit
, "m") == 0) {
2772 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_boot
))
2773 return ret_errno(EOVERFLOW
);
2774 } else if (strcmp(unit
, "s") == 0) {
2775 lxc_conf
->timens
.s_boot
= offset
;
2776 } else if (strcmp(unit
, "ms") == 0) {
2777 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_boot
))
2778 return ret_errno(EOVERFLOW
);
2779 } else if (strcmp(unit
, "us") == 0) {
2780 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_boot
))
2781 return ret_errno(EOVERFLOW
);
2782 } else if (strcmp(unit
, "ns") == 0) {
2783 lxc_conf
->timens
.ns_boot
= offset
;
2785 return ret_errno(EINVAL
);
2791 static int set_config_time_offset_monotonic(const char *key
, const char *value
,
2792 struct lxc_conf
*lxc_conf
, void *data
)
2797 char buf
[STRLITERALLEN("ms") + 1];
2799 if (lxc_config_value_empty(value
))
2800 return clr_config_time_offset_monotonic(key
, lxc_conf
, data
);
2802 ret
= lxc_safe_int64_residual(value
, &offset
, 10, buf
, sizeof(buf
));
2806 unit
= lxc_trim_whitespace_in_place(buf
);
2807 if (strcmp(unit
, "h") == 0) {
2808 if (!multiply_overflow(offset
, 3600, &lxc_conf
->timens
.s_monotonic
))
2809 return ret_errno(EOVERFLOW
);
2810 } else if (strcmp(unit
, "m") == 0) {
2811 if (!multiply_overflow(offset
, 60, &lxc_conf
->timens
.s_monotonic
))
2812 return ret_errno(EOVERFLOW
);
2813 } else if (strcmp(unit
, "s") == 0) {
2814 lxc_conf
->timens
.s_monotonic
= offset
;
2815 } else if (strcmp(unit
, "ms") == 0) {
2816 if (!multiply_overflow(offset
, 1000000, &lxc_conf
->timens
.ns_monotonic
))
2817 return ret_errno(EOVERFLOW
);
2818 } else if (strcmp(unit
, "us") == 0) {
2819 if (!multiply_overflow(offset
, 1000, &lxc_conf
->timens
.ns_monotonic
))
2820 return ret_errno(EOVERFLOW
);
2821 } else if (strcmp(unit
, "ns") == 0) {
2822 lxc_conf
->timens
.ns_monotonic
= offset
;
2824 return ret_errno(EINVAL
);
2830 static int set_config_namespace_share(const char *key
, const char *value
,
2831 struct lxc_conf
*lxc_conf
, void *data
)
2834 const char *namespace;
2836 if (lxc_config_value_empty(value
))
2837 return clr_config_namespace_share(key
, lxc_conf
, data
);
2839 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
2840 ns_idx
= lxc_namespace_2_ns_idx(namespace);
2844 return set_config_string_item(&lxc_conf
->ns_share
[ns_idx
], value
);
2847 struct parse_line_conf
{
2848 struct lxc_conf
*conf
;
2852 static int parse_line(char *buffer
, void *data
)
2854 __do_free
char *linep
= NULL
;
2855 char *dot
, *key
, *line
, *value
;
2857 struct lxc_config_t
*config
;
2860 struct parse_line_conf
*plc
= data
;
2862 /* If there are newlines in the config file we should keep them. */
2863 empty_line
= lxc_is_line_empty(dup
);
2867 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2868 * modified the original string on the stack by replacing '=' by '\0'
2871 linep
= line
= strdup(dup
);
2873 return ret_errno(ENOMEM
);
2875 if (!plc
->from_include
) {
2876 ret
= append_unexp_config_line(line
, plc
->conf
);
2884 line
+= lxc_char_left_gc(line
, strlen(line
));
2886 /* ignore comments */
2890 /* martian option - don't add it to the config itself */
2891 if (strncmp(line
, "lxc.", 4))
2894 dot
= strchr(line
, '=');
2896 return log_error_errno(-EINVAL
, EINVAL
, "Invalid configuration line: %s", line
);
2902 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2904 value
+= lxc_char_left_gc(value
, strlen(value
));
2905 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2907 if (*value
== '\'' || *value
== '\"') {
2910 len
= strlen(value
);
2911 if (len
> 1 && value
[len
- 1] == *value
) {
2912 value
[len
- 1] = '\0';
2917 config
= lxc_get_config(key
);
2919 return log_error_errno(-EINVAL
, EINVAL
, "Unknown configuration key \"%s\"", key
);
2921 return config
->set(key
, value
, plc
->conf
, NULL
);
2924 static struct new_config_item
*parse_new_conf_line(char *buffer
)
2926 __do_free
char *k
= NULL
, *linep
= NULL
, *v
= NULL
;
2927 __do_free
struct new_config_item
*new = NULL
;
2929 char *dot
, *key
, *line
, *value
;
2931 linep
= line
= strdup(dup
);
2935 line
+= lxc_char_left_gc(line
, strlen(line
));
2937 /* martian option - don't add it to the config itself */
2938 if (strncmp(line
, "lxc.", 4))
2941 dot
= strchr(line
, '=');
2943 return log_error_errno(NULL
, EINVAL
, "Invalid configuration line: %s", line
);
2949 key
[lxc_char_right_gc(key
, strlen(key
))] = '\0';
2951 value
+= lxc_char_left_gc(value
, strlen(value
));
2952 value
[lxc_char_right_gc(value
, strlen(value
))] = '\0';
2954 if (*value
== '\'' || *value
== '\"') {
2957 len
= strlen(value
);
2958 if (len
> 1 && value
[len
- 1] == *value
) {
2959 value
[len
- 1] = '\0';
2964 new = malloc(sizeof(struct new_config_item
));
2976 new->key
= move_ptr(k
);
2977 new->val
= move_ptr(v
);
2978 return move_ptr(new);
2981 int lxc_config_read(const char *file
, struct lxc_conf
*conf
, bool from_include
)
2983 struct parse_line_conf c
;
2986 c
.from_include
= from_include
;
2988 /* Catch only the top level config file name in the structure. */
2990 conf
->rcfile
= strdup(file
);
2992 return lxc_file_for_each_line_mmap(file
, parse_line
, &c
);
2995 int lxc_config_define_add(struct lxc_list
*defines
, char *arg
)
2997 __do_free
struct lxc_list
*dent
= NULL
;
2999 dent
= malloc(sizeof(struct lxc_list
));
3001 return ret_errno(ENOMEM
);
3003 dent
->elem
= parse_new_conf_line(arg
);
3005 return ret_errno(ENOMEM
);
3007 lxc_list_add_tail(defines
, move_ptr(dent
));
3012 bool lxc_config_define_load(struct lxc_list
*defines
, struct lxc_container
*c
)
3014 struct lxc_list
*it
;
3017 lxc_list_for_each(it
, defines
) {
3018 struct new_config_item
*new_item
= it
->elem
;
3019 bret
= c
->set_config_item(c
, new_item
->key
, new_item
->val
);
3024 lxc_config_define_free(defines
);
3029 void lxc_config_define_free(struct lxc_list
*defines
)
3031 struct lxc_list
*it
, *next
;
3033 lxc_list_for_each_safe(it
, defines
, next
) {
3034 struct new_config_item
*new_item
= it
->elem
;
3035 free(new_item
->key
);
3036 free(new_item
->val
);
3042 signed long lxc_config_parse_arch(const char *arch
)
3044 #if HAVE_SYS_PERSONALITY_H
3049 { "arm", PER_LINUX32
},
3050 { "armel", PER_LINUX32
},
3051 { "armhf", PER_LINUX32
},
3052 { "armv7l", PER_LINUX32
},
3053 { "athlon", PER_LINUX32
},
3054 { "i386", PER_LINUX32
},
3055 { "i486", PER_LINUX32
},
3056 { "i586", PER_LINUX32
},
3057 { "i686", PER_LINUX32
},
3058 { "linux32", PER_LINUX32
},
3059 { "mips", PER_LINUX32
},
3060 { "mipsel", PER_LINUX32
},
3061 { "ppc", PER_LINUX32
},
3062 { "powerpc", PER_LINUX32
},
3063 { "x86", PER_LINUX32
},
3064 { "amd64", PER_LINUX
},
3065 { "arm64", PER_LINUX
},
3066 { "linux64", PER_LINUX
},
3067 { "mips64", PER_LINUX
},
3068 { "mips64el", PER_LINUX
},
3069 { "ppc64", PER_LINUX
},
3070 { "ppc64el", PER_LINUX
},
3071 { "ppc64le", PER_LINUX
},
3072 { "powerpc64", PER_LINUX
},
3073 { "s390x", PER_LINUX
},
3074 { "x86_64", PER_LINUX
},
3076 size_t len
= sizeof(pername
) / sizeof(pername
[0]);
3078 for (int i
= 0; i
< len
; i
++)
3079 if (!strcmp(pername
[i
].name
, arch
))
3080 return pername
[i
].per
;
3083 return LXC_ARCH_UNCHANGED
;
3086 int lxc_fill_elevated_privileges(char *flaglist
, int *flags
)
3094 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP
},
3095 { "CAP", LXC_ATTACH_DROP_CAPABILITIES
},
3096 { "LSM", LXC_ATTACH_LSM_EXEC
},
3101 /* For the sake of backward compatibility, drop all privileges
3102 * if none is specified.
3104 for (i
= 0; all_privs
[i
].token
; i
++)
3105 *flags
|= all_privs
[i
].flag
;
3110 lxc_iterate_parts(token
, flaglist
, "|") {
3113 for (i
= 0; all_privs
[i
].token
; i
++)
3114 if (!strcmp(all_privs
[i
].token
, token
))
3115 aflag
= all_privs
[i
].flag
;
3118 return ret_errno(EINVAL
);
3126 /* Write out a configuration file. */
3127 int write_config(int fd
, const struct lxc_conf
*conf
)
3130 size_t len
= conf
->unexpanded_len
;
3135 ret
= lxc_write_nointr(fd
, conf
->unexpanded_config
, len
);
3137 return log_error_errno(-errno
, errno
, "Failed to write configuration file");
3142 bool do_append_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3145 __do_free
char *tmp
= NULL
;
3149 len
= strlen(key
) + strlen(v
) + 4;
3150 tmp
= must_realloc(NULL
, len
);
3152 if (lxc_config_value_empty(v
))
3153 ret
= snprintf(tmp
, len
, "%s =", key
);
3155 ret
= snprintf(tmp
, len
, "%s = %s", key
, v
);
3156 if (ret
< 0 || ret
>= len
)
3159 /* Save the line verbatim into unexpanded_conf */
3160 if (append_unexp_config_line(tmp
, conf
))
3166 void clear_unexp_config_line(struct lxc_conf
*conf
, const char *key
,
3170 char *lstart
= conf
->unexpanded_config
;
3172 if (!conf
->unexpanded_config
)
3176 lend
= strchr(lstart
, '\n');
3180 lend
= lstart
+ strlen(lstart
);
3184 if (strncmp(lstart
, key
, strlen(key
)) != 0) {
3190 v
= lstart
[strlen(key
)];
3191 if (!isspace(v
) && v
!= '=') {
3197 conf
->unexpanded_len
-= (lend
- lstart
);
3199 if (*lend
== '\0') {
3204 memmove(lstart
, lend
, strlen(lend
) + 1);
3208 bool clone_update_unexp_ovl_paths(struct lxc_conf
*conf
, const char *oldpath
,
3209 const char *newpath
, const char *oldname
,
3210 const char *newname
, const char *ovldir
)
3212 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3213 char *lstart
= conf
->unexpanded_config
;
3214 const char *key
= "lxc.mount.entry";
3217 size_t newdirlen
, olddirlen
;
3219 olddirlen
= strlen(ovldir
) + strlen(oldpath
) + strlen(oldname
) + 2;
3220 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3221 ret
= snprintf(olddir
, olddirlen
+ 1, "%s=%s/%s", ovldir
, oldpath
,
3223 if (ret
< 0 || ret
>= olddirlen
+ 1)
3226 newdirlen
= strlen(ovldir
) + strlen(newpath
) + strlen(newname
) + 2;
3227 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3228 ret
= snprintf(newdir
, newdirlen
+ 1, "%s=%s/%s", ovldir
, newpath
,
3230 if (ret
< 0 || ret
>= newdirlen
+ 1)
3233 if (!conf
->unexpanded_config
)
3237 lend
= strchr(lstart
, '\n');
3239 lend
= lstart
+ strlen(lstart
);
3243 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3246 p
= strchr(lstart
+ strlen(key
), '=');
3257 /* Whenever a lxc.mount.entry entry is found in a line we check
3258 * if the substring "overlay" is present before doing any
3259 * further work. We check for "overlay" because substrings need
3260 * to have at least one space before them in a valid overlay
3261 * lxc.mount.entry (/A B overlay). When the space before is
3262 * missing it is very likely that these substrings are part of a
3263 * path or something else. (Checking q >= lend ensures that we
3264 * only count matches in the current line.) */
3265 q
= strstr(p
, " overlay");
3266 if (!q
|| q
>= lend
)
3269 if (!(q
= strstr(p
, olddir
)) || (q
>= lend
))
3272 /* replace the olddir with newdir */
3273 if (olddirlen
>= newdirlen
) {
3274 size_t diff
= olddirlen
- newdirlen
;
3275 memcpy(q
, newdir
, newdirlen
);
3277 if (olddirlen
!= newdirlen
) {
3278 memmove(q
+ newdirlen
, q
+ newdirlen
+ diff
,
3279 strlen(q
) - newdirlen
- diff
+ 1);
3281 conf
->unexpanded_len
-= diff
;
3285 size_t diff
= newdirlen
- olddirlen
;
3286 size_t oldlen
= conf
->unexpanded_len
;
3287 size_t newlen
= oldlen
+ diff
;
3288 size_t poffset
= q
- conf
->unexpanded_config
;
3290 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3294 conf
->unexpanded_len
= newlen
;
3295 conf
->unexpanded_alloced
= newlen
+ 1;
3296 new[newlen
- 1] = '\0';
3297 lend
= new + (lend
- conf
->unexpanded_config
);
3299 /* Move over the remainder to make room for the newdir.
3301 memmove(new + poffset
+ newdirlen
,
3302 new + poffset
+ olddirlen
,
3303 oldlen
- poffset
- olddirlen
+ 1);
3304 conf
->unexpanded_config
= new;
3306 memcpy(new + poffset
, newdir
, newdirlen
);
3317 bool clone_update_unexp_hooks(struct lxc_conf
*conf
, const char *oldpath
,
3318 const char *newpath
, const char *oldname
,
3319 const char *newname
)
3321 __do_free
char *newdir
= NULL
, *olddir
= NULL
;
3322 char *lstart
= conf
->unexpanded_config
;
3323 const char *key
= "lxc.hook";
3326 size_t newdirlen
, olddirlen
;
3328 olddirlen
= strlen(oldpath
) + strlen(oldname
) + 1;
3329 olddir
= must_realloc(NULL
, olddirlen
+ 1);
3330 ret
= snprintf(olddir
, olddirlen
+ 1, "%s/%s", oldpath
, oldname
);
3331 if (ret
< 0 || ret
>= olddirlen
+ 1)
3334 newdirlen
= strlen(newpath
) + strlen(newname
) + 1;
3335 newdir
= must_realloc(NULL
, newdirlen
+ 1);
3336 ret
= snprintf(newdir
, newdirlen
+ 1, "%s/%s", newpath
, newname
);
3337 if (ret
< 0 || ret
>= newdirlen
+ 1)
3340 if (!conf
->unexpanded_config
)
3344 lend
= strchr(lstart
, '\n');
3346 lend
= lstart
+ strlen(lstart
);
3350 if (strncmp(lstart
, key
, strlen(key
)) != 0)
3353 p
= strchr(lstart
+ strlen(key
), '=');
3364 if (strncmp(p
, olddir
, strlen(olddir
)) != 0)
3367 /* replace the olddir with newdir */
3368 if (olddirlen
>= newdirlen
) {
3369 size_t diff
= olddirlen
- newdirlen
;
3370 memcpy(p
, newdir
, newdirlen
);
3372 if (olddirlen
!= newdirlen
) {
3373 memmove(p
+ newdirlen
, p
+ newdirlen
+ diff
,
3374 strlen(p
) - newdirlen
- diff
+ 1);
3376 conf
->unexpanded_len
-= diff
;
3380 size_t diff
= newdirlen
- olddirlen
;
3381 size_t oldlen
= conf
->unexpanded_len
;
3382 size_t newlen
= oldlen
+ diff
;
3383 size_t poffset
= p
- conf
->unexpanded_config
;
3385 new = realloc(conf
->unexpanded_config
, newlen
+ 1);
3389 conf
->unexpanded_len
= newlen
;
3390 conf
->unexpanded_alloced
= newlen
+ 1;
3391 new[newlen
- 1] = '\0';
3392 lend
= new + (lend
- conf
->unexpanded_config
);
3394 /* Move over the remainder to make room for the newdir.
3396 memmove(new + poffset
+ newdirlen
,
3397 new + poffset
+ olddirlen
,
3398 oldlen
- poffset
- olddirlen
+ 1);
3399 conf
->unexpanded_config
= new;
3401 memcpy(new + poffset
, newdir
, newdirlen
);
3415 ERROR("Error writing to new config"); \
3420 /* This is called only from clone. We wish to update all hwaddrs in the
3421 * unexpanded config file. We can't/don't want to update any which come from
3422 * lxc.includes (there shouldn't be any).
3423 * We can't just walk the c->lxc-conf->network list because that includes netifs
3424 * from the include files. So we update the ones which we find in the unexp
3425 * config file, then find the original macaddr in the conf->network, and update
3426 * that to the same value.
3428 bool network_new_hwaddrs(struct lxc_conf
*conf
)
3430 char *lend
, *p
, *p2
;
3431 struct lxc_list
*it
;
3432 char *lstart
= conf
->unexpanded_config
;
3434 if (!conf
->unexpanded_config
)
3438 char newhwaddr
[18], oldhwaddr
[17];
3440 lend
= strchr(lstart
, '\n');
3442 lend
= lstart
+ strlen(lstart
);
3446 if (!lxc_config_net_is_hwaddr(lstart
)) {
3451 p
= strchr(lstart
, '=');
3464 while (*p2
&& !isblank(*p2
) && *p2
!= '\n')
3467 if ((p2
- p
) != 17) {
3468 WARN("Bad hwaddr entry");
3473 memcpy(oldhwaddr
, p
, 17);
3475 if (!new_hwaddr(newhwaddr
))
3478 memcpy(p
, newhwaddr
, 17);
3479 lxc_list_for_each(it
, &conf
->network
) {
3480 struct lxc_netdev
*n
= it
->elem
;
3482 if (n
->hwaddr
&& memcmp(oldhwaddr
, n
->hwaddr
, 17) == 0)
3483 memcpy(n
->hwaddr
, newhwaddr
, 17);
3492 static int set_config_ephemeral(const char *key
, const char *value
,
3493 struct lxc_conf
*lxc_conf
, void *data
)
3497 if (lxc_config_value_empty(value
)) {
3498 lxc_conf
->ephemeral
= 0;
3502 ret
= lxc_safe_uint(value
, &lxc_conf
->ephemeral
);
3506 if (lxc_conf
->ephemeral
> 1)
3507 return ret_errno(EINVAL
);
3512 static int set_config_log_syslog(const char *key
, const char *value
,
3513 struct lxc_conf
*lxc_conf
, void *data
)
3517 if (lxc_conf
->syslog
)
3518 free_disarm(lxc_conf
->syslog
);
3520 if (lxc_config_value_empty(value
))
3523 facility
= lxc_syslog_priority_to_int(value
);
3524 if (facility
== -EINVAL
)
3525 return ret_errno(EINVAL
);
3527 lxc_log_syslog(facility
);
3529 return set_config_string_item(&lxc_conf
->syslog
, value
);
3532 static int set_config_no_new_privs(const char *key
, const char *value
,
3533 struct lxc_conf
*lxc_conf
, void *data
)
3538 if (lxc_config_value_empty(value
)) {
3539 lxc_conf
->no_new_privs
= false;
3543 ret
= lxc_safe_uint(value
, &v
);
3548 return ret_errno(EINVAL
);
3550 lxc_conf
->no_new_privs
= v
? true : false;
3555 /* Callbacks to get configuration items. */
3556 static int get_config_personality(const char *key
, char *retv
, int inlen
,
3557 struct lxc_conf
*c
, void *data
)
3564 memset(retv
, 0, inlen
);
3566 #if HAVE_SYS_PERSONALITY_H
3569 switch (c
->personality
) {
3571 strprint(retv
, inlen
, "i686");
3574 strprint(retv
, inlen
, "x86_64");
3584 static int get_config_pty_max(const char *key
, char *retv
, int inlen
,
3585 struct lxc_conf
*c
, void *data
)
3587 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->pty_max
);
3590 static int get_config_tty_max(const char *key
, char *retv
, int inlen
,
3591 struct lxc_conf
*c
, void *data
)
3593 return lxc_get_conf_size_t(c
, retv
, inlen
, c
->ttys
.max
);
3596 static int get_config_tty_dir(const char *key
, char *retv
, int inlen
,
3597 struct lxc_conf
*c
, void *data
)
3599 return lxc_get_conf_str(retv
, inlen
, c
->ttys
.dir
);
3602 static int get_config_apparmor_profile(const char *key
, char *retv
, int inlen
,
3603 struct lxc_conf
*c
, void *data
)
3605 return lxc_get_conf_str(retv
, inlen
, c
->lsm_aa_profile
);
3608 static int get_config_apparmor_allow_incomplete(const char *key
, char *retv
,
3609 int inlen
, struct lxc_conf
*c
,
3612 return lxc_get_conf_int(c
, retv
, inlen
,
3613 c
->lsm_aa_allow_incomplete
);
3616 static int get_config_apparmor_allow_nesting(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_nesting
);
3624 static int get_config_apparmor_raw(const char *key
, char *retv
,
3625 int inlen
, struct lxc_conf
*c
,
3629 struct lxc_list
*it
;
3635 memset(retv
, 0, inlen
);
3637 lxc_list_for_each(it
, &c
->lsm_aa_raw
) {
3638 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3644 static int get_config_selinux_context(const char *key
, char *retv
, int inlen
,
3645 struct lxc_conf
*c
, void *data
)
3647 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_context
);
3650 static int get_config_selinux_context_keyring(const char *key
, char *retv
, int inlen
,
3651 struct lxc_conf
*c
, void *data
)
3653 return lxc_get_conf_str(retv
, inlen
, c
->lsm_se_keyring_context
);
3656 static int get_config_keyring_session(const char *key
, char *retv
, int inlen
,
3657 struct lxc_conf
*c
, void *data
)
3659 return lxc_get_conf_bool(c
, retv
, inlen
, c
->keyring_disable_session
);
3663 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3664 * just the value(s) will be printed. Since there still could be more than one,
3665 * it is newline-separated.
3666 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3668 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3669 * 'lxc.cgroup.subsystem.key = value' format.
3671 static int __get_config_cgroup_controller(const char *key
, char *retv
,
3672 int inlen
, struct lxc_conf
*c
,
3676 size_t namespaced_token_len
;
3677 char *global_token
, *namespaced_token
;
3678 struct lxc_list
*it
;
3680 bool get_all
= false;
3685 memset(retv
, 0, inlen
);
3687 if (version
== CGROUP2_SUPER_MAGIC
) {
3688 global_token
= "lxc.cgroup2";
3689 namespaced_token
= "lxc.cgroup2.";
3690 namespaced_token_len
= STRLITERALLEN("lxc.cgroup2.");
3691 } else if (version
== CGROUP_SUPER_MAGIC
) {
3692 global_token
= "lxc.cgroup";
3693 namespaced_token
= "lxc.cgroup.";
3694 namespaced_token_len
= STRLITERALLEN("lxc.cgroup.");
3696 return ret_errno(EINVAL
);
3699 if (strcmp(key
, global_token
) == 0)
3701 else if (strncmp(key
, namespaced_token
, namespaced_token_len
) == 0)
3702 key
+= namespaced_token_len
;
3704 return ret_errno(EINVAL
);
3706 lxc_list_for_each(it
, &c
->cgroup
) {
3707 struct lxc_cgroup
*cg
= it
->elem
;
3710 if (version
!= cg
->version
)
3713 strprint(retv
, inlen
, "%s.%s = %s\n", global_token
,
3714 cg
->subsystem
, cg
->value
);
3715 } else if (strcmp(cg
->subsystem
, key
) == 0) {
3716 strprint(retv
, inlen
, "%s\n", cg
->value
);
3723 static int get_config_cgroup_controller(const char *key
, char *retv
, int inlen
,
3724 struct lxc_conf
*c
, void *data
)
3726 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3727 CGROUP_SUPER_MAGIC
);
3730 static int get_config_cgroup2_controller(const char *key
, char *retv
, int inlen
,
3731 struct lxc_conf
*c
, void *data
)
3733 return __get_config_cgroup_controller(key
, retv
, inlen
, c
,
3734 CGROUP2_SUPER_MAGIC
);
3737 static int get_config_cgroup_dir(const char *key
, char *retv
, int inlen
,
3738 struct lxc_conf
*lxc_conf
, void *data
)
3743 if (strcmp(key
, "lxc.cgroup.dir") != 0)
3744 return ret_errno(EINVAL
);
3749 memset(retv
, 0, inlen
);
3751 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.dir
);
3756 static int get_config_cgroup_monitor_dir(const char *key
, char *retv
, int inlen
,
3757 struct lxc_conf
*lxc_conf
, void *data
)
3765 memset(retv
, 0, inlen
);
3767 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_dir
);
3772 static int get_config_cgroup_monitor_pivot_dir(const char *key
, char *retv
, int inlen
,
3773 struct lxc_conf
*lxc_conf
, void *data
)
3781 memset(retv
, 0, inlen
);
3783 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
3788 static int get_config_cgroup_container_dir(const char *key
, char *retv
,
3790 struct lxc_conf
*lxc_conf
,
3799 memset(retv
, 0, inlen
);
3801 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.container_dir
);
3806 static int get_config_cgroup_container_inner_dir(const char *key
, char *retv
,
3808 struct lxc_conf
*lxc_conf
,
3817 memset(retv
, 0, inlen
);
3819 strprint(retv
, inlen
, "%s", lxc_conf
->cgroup_meta
.namespace_dir
);
3824 static inline int get_config_cgroup_relative(const char *key
, char *retv
,
3825 int inlen
, struct lxc_conf
*lxc_conf
,
3828 return lxc_get_conf_int(lxc_conf
, retv
, inlen
,
3829 lxc_conf
->cgroup_meta
.relative
);
3832 static int get_config_idmaps(const char *key
, char *retv
, int inlen
,
3833 struct lxc_conf
*c
, void *data
)
3835 struct lxc_list
*it
;
3836 int len
, listlen
, ret
;
3838 /* "u 1000 1000000 65536"
3840 * let's render this as
3858 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3859 char buf
[__LXC_IDMAP_STR_BUF
];
3864 memset(retv
, 0, inlen
);
3866 listlen
= lxc_list_len(&c
->id_map
);
3867 lxc_list_for_each(it
, &c
->id_map
) {
3868 struct id_map
*map
= it
->elem
;
3869 ret
= snprintf(buf
, __LXC_IDMAP_STR_BUF
, "%c %lu %lu %lu",
3870 (map
->idtype
== ID_TYPE_UID
) ? 'u' : 'g',
3871 map
->nsid
, map
->hostid
, map
->range
);
3872 if (ret
< 0 || ret
>= __LXC_IDMAP_STR_BUF
)
3873 return ret_errno(EIO
);
3875 strprint(retv
, inlen
, "%s%s", buf
, (listlen
-- > 1) ? "\n" : "");
3881 static int get_config_log_level(const char *key
, char *retv
, int inlen
,
3882 struct lxc_conf
*c
, void *data
)
3885 v
= lxc_log_priority_to_string(c
->loglevel
);
3886 return lxc_get_conf_str(retv
, inlen
, v
);
3889 static int get_config_log_file(const char *key
, char *retv
, int inlen
,
3890 struct lxc_conf
*c
, void *data
)
3892 return lxc_get_conf_str(retv
, inlen
, c
->logfile
);
3895 static int get_config_mount_fstab(const char *key
, char *retv
, int inlen
,
3896 struct lxc_conf
*c
, void *data
)
3898 return lxc_get_conf_str(retv
, inlen
, c
->fstab
);
3901 static int get_config_mount_auto(const char *key
, char *retv
, int inlen
,
3902 struct lxc_conf
*c
, void *data
)
3904 int len
, fulllen
= 0;
3905 const char *sep
= "";
3910 memset(retv
, 0, inlen
);
3912 if (!(c
->auto_mounts
& LXC_AUTO_ALL_MASK
))
3915 switch (c
->auto_mounts
& LXC_AUTO_PROC_MASK
) {
3916 case LXC_AUTO_PROC_MIXED
:
3917 strprint(retv
, inlen
, "%sproc:mixed", sep
);
3920 case LXC_AUTO_PROC_RW
:
3921 strprint(retv
, inlen
, "%sproc:rw", sep
);
3928 switch (c
->auto_mounts
& LXC_AUTO_SYS_MASK
) {
3929 case LXC_AUTO_SYS_RO
:
3930 strprint(retv
, inlen
, "%ssys:ro", sep
);
3933 case LXC_AUTO_SYS_RW
:
3934 strprint(retv
, inlen
, "%ssys:rw", sep
);
3937 case LXC_AUTO_SYS_MIXED
:
3938 strprint(retv
, inlen
, "%ssys:mixed", sep
);
3945 switch (c
->auto_mounts
& LXC_AUTO_CGROUP_MASK
) {
3946 case LXC_AUTO_CGROUP_NOSPEC
:
3947 strprint(retv
, inlen
, "%scgroup", sep
);
3949 case LXC_AUTO_CGROUP_MIXED
:
3950 strprint(retv
, inlen
, "%scgroup:mixed", sep
);
3952 case LXC_AUTO_CGROUP_RO
:
3953 strprint(retv
, inlen
, "%scgroup:ro", sep
);
3955 case LXC_AUTO_CGROUP_RW
:
3956 strprint(retv
, inlen
, "%scgroup:rw", sep
);
3958 case LXC_AUTO_CGROUP_FULL_NOSPEC
:
3959 strprint(retv
, inlen
, "%scgroup-full", sep
);
3961 case LXC_AUTO_CGROUP_FULL_MIXED
:
3962 strprint(retv
, inlen
, "%scgroup-full:mixed", sep
);
3964 case LXC_AUTO_CGROUP_FULL_RO
:
3965 strprint(retv
, inlen
, "%scgroup-full:ro", sep
);
3967 case LXC_AUTO_CGROUP_FULL_RW
:
3968 strprint(retv
, inlen
, "%scgroup-full:rw", sep
);
3977 static int get_config_mount(const char *key
, char *retv
, int inlen
,
3978 struct lxc_conf
*c
, void *data
)
3980 int len
, fulllen
= 0;
3981 struct lxc_list
*it
;
3986 memset(retv
, 0, inlen
);
3988 lxc_list_for_each(it
, &c
->mount_list
) {
3989 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
3995 static int get_config_rootfs_path(const char *key
, char *retv
, int inlen
,
3996 struct lxc_conf
*c
, void *data
)
3998 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.path
);
4001 static int get_config_rootfs_managed(const char *key
, char *retv
, int inlen
,
4002 struct lxc_conf
*c
, void *data
)
4004 return lxc_get_conf_bool(c
, retv
, inlen
, c
->rootfs
.managed
);
4007 static int get_config_rootfs_mount(const char *key
, char *retv
, int inlen
,
4008 struct lxc_conf
*c
, void *data
)
4010 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.mount
);
4013 static int get_config_rootfs_options(const char *key
, char *retv
, int inlen
,
4014 struct lxc_conf
*c
, void *data
)
4016 return lxc_get_conf_str(retv
, inlen
, c
->rootfs
.options
);
4019 static int get_config_uts_name(const char *key
, char *retv
, int inlen
,
4020 struct lxc_conf
*c
, void *data
)
4022 return lxc_get_conf_str(
4024 c
->utsname
? c
->utsname
->nodename
: NULL
);
4027 static int get_config_hooks(const char *key
, char *retv
, int inlen
,
4028 struct lxc_conf
*c
, void *data
)
4031 int len
, fulllen
= 0, found
= -1;
4032 struct lxc_list
*it
;
4035 subkey
= strchr(key
, '.');
4037 return ret_errno(EINVAL
);
4039 subkey
= strchr(subkey
+ 1, '.');
4041 return ret_errno(EINVAL
);
4043 if (*subkey
== '\0')
4044 return ret_errno(EINVAL
);
4046 for (i
= 0; i
< NUM_LXC_HOOKS
; i
++) {
4047 if (strcmp(lxchook_names
[i
], subkey
) == 0) {
4054 return ret_errno(EINVAL
);
4059 memset(retv
, 0, inlen
);
4061 lxc_list_for_each(it
, &c
->hooks
[found
]) {
4062 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4068 static int get_config_hooks_version(const char *key
, char *retv
, int inlen
,
4069 struct lxc_conf
*c
, void *data
)
4071 return lxc_get_conf_int(c
, retv
, inlen
, c
->hooks_version
);
4074 static int get_config_net(const char *key
, char *retv
, int inlen
,
4075 struct lxc_conf
*c
, void *data
)
4077 int len
, fulllen
= 0;
4078 struct lxc_list
*it
;
4083 memset(retv
, 0, inlen
);
4085 lxc_list_for_each(it
, &c
->network
) {
4086 struct lxc_netdev
*n
= it
->elem
;
4087 const char *t
= lxc_net_type_to_str(n
->type
);
4088 strprint(retv
, inlen
, "%s\n", t
? t
: "(invalid)");
4094 static int get_config_cap_drop(const char *key
, char *retv
, int inlen
,
4095 struct lxc_conf
*c
, void *data
)
4097 int len
, fulllen
= 0;
4098 struct lxc_list
*it
;
4103 memset(retv
, 0, inlen
);
4105 lxc_list_for_each(it
, &c
->caps
) {
4106 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4112 static int get_config_cap_keep(const char *key
, char *retv
, int inlen
,
4113 struct lxc_conf
*c
, void *data
)
4115 int len
, fulllen
= 0;
4116 struct lxc_list
*it
;
4121 memset(retv
, 0, inlen
);
4123 lxc_list_for_each(it
, &c
->keepcaps
) {
4124 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4130 static int get_config_console_path(const char *key
, char *retv
, int inlen
,
4131 struct lxc_conf
*c
, void *data
)
4133 return lxc_get_conf_str(retv
, inlen
, c
->console
.path
);
4136 static int get_config_console_logfile(const char *key
, char *retv
, int inlen
,
4137 struct lxc_conf
*c
, void *data
)
4139 return lxc_get_conf_str(retv
, inlen
, c
->console
.log_path
);
4142 static int get_config_console_rotate(const char *key
, char *retv
, int inlen
,
4143 struct lxc_conf
*c
, void *data
)
4145 return lxc_get_conf_int(c
, retv
, inlen
, c
->console
.log_rotate
);
4149 static int get_config_console_buffer_size(const char *key
, char *retv
,
4150 int inlen
, struct lxc_conf
*c
,
4153 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.buffer_size
);
4156 static int get_config_console_size(const char *key
, char *retv
, int inlen
,
4157 struct lxc_conf
*c
, void *data
)
4159 return lxc_get_conf_uint64(c
, retv
, inlen
, c
->console
.log_size
);
4162 static int get_config_seccomp_allow_nesting(const char *key
, char *retv
,
4163 int inlen
, struct lxc_conf
*c
,
4167 return lxc_get_conf_int(c
, retv
, inlen
, c
->seccomp
.allow_nesting
);
4169 return ret_errno(ENOSYS
);
4173 static int get_config_seccomp_notify_cookie(const char *key
, char *retv
, int inlen
,
4174 struct lxc_conf
*c
, void *data
)
4176 #ifdef HAVE_SECCOMP_NOTIFY
4177 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.notifier
.cookie
);
4179 return ret_errno(ENOSYS
);
4183 static int get_config_seccomp_notify_proxy(const char *key
, char *retv
, int inlen
,
4184 struct lxc_conf
*c
, void *data
)
4186 #ifdef HAVE_SECCOMP_NOTIFY
4187 return lxc_get_conf_str(retv
, inlen
,
4188 (c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]) == '/'
4189 ? &c
->seccomp
.notifier
.proxy_addr
.sun_path
[0]
4190 : &c
->seccomp
.notifier
.proxy_addr
.sun_path
[1]);
4192 return ret_errno(ENOSYS
);
4196 static int get_config_seccomp_profile(const char *key
, char *retv
, int inlen
,
4197 struct lxc_conf
*c
, void *data
)
4199 return lxc_get_conf_str(retv
, inlen
, c
->seccomp
.seccomp
);
4202 static int get_config_autodev(const char *key
, char *retv
, int inlen
,
4203 struct lxc_conf
*c
, void *data
)
4205 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodev
);
4208 static int get_config_autodev_tmpfs_size(const char *key
, char *retv
, int inlen
,
4209 struct lxc_conf
*c
, void *data
)
4211 return lxc_get_conf_int(c
, retv
, inlen
, c
->autodevtmpfssize
);
4214 static int get_config_signal_halt(const char *key
, char *retv
, int inlen
,
4215 struct lxc_conf
*c
, void *data
)
4217 return lxc_get_conf_int(c
, retv
, inlen
, c
->haltsignal
);
4220 static int get_config_signal_reboot(const char *key
, char *retv
, int inlen
,
4221 struct lxc_conf
*c
, void *data
)
4223 return lxc_get_conf_int(c
, retv
, inlen
, c
->rebootsignal
);
4226 static int get_config_signal_stop(const char *key
, char *retv
, int inlen
,
4227 struct lxc_conf
*c
, void *data
)
4229 return lxc_get_conf_int(c
, retv
, inlen
, c
->stopsignal
);
4232 static int get_config_start(const char *key
, char *retv
, int inlen
,
4233 struct lxc_conf
*c
, void *data
)
4235 if (strcmp(key
+ 10, "auto") == 0)
4236 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_auto
);
4237 else if (strcmp(key
+ 10, "delay") == 0)
4238 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_delay
);
4239 else if (strcmp(key
+ 10, "order") == 0)
4240 return lxc_get_conf_int(c
, retv
, inlen
, c
->start_order
);
4245 static int get_config_log_syslog(const char *key
, char *retv
, int inlen
,
4246 struct lxc_conf
*c
, void *data
)
4248 return lxc_get_conf_str(retv
, inlen
, c
->syslog
);
4251 static int get_config_monitor(const char *key
, char *retv
, int inlen
,
4252 struct lxc_conf
*c
, void *data
)
4254 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_unshare
);
4257 static int get_config_monitor_signal_pdeath(const char *key
, char *retv
,
4258 int inlen
, struct lxc_conf
*c
,
4261 return lxc_get_conf_int(c
, retv
, inlen
, c
->monitor_signal_pdeath
);
4264 static int get_config_group(const char *key
, char *retv
, int inlen
,
4265 struct lxc_conf
*c
, void *data
)
4267 int len
, fulllen
= 0;
4268 struct lxc_list
*it
;
4273 memset(retv
, 0, inlen
);
4275 lxc_list_for_each(it
, &c
->groups
) {
4276 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4282 static int get_config_environment(const char *key
, char *retv
, int inlen
,
4283 struct lxc_conf
*c
, void *data
)
4285 int len
, fulllen
= 0;
4286 struct lxc_list
*it
;
4291 memset(retv
, 0, inlen
);
4293 lxc_list_for_each(it
, &c
->environment
) {
4294 strprint(retv
, inlen
, "%s\n", (char *)it
->elem
);
4300 static int get_config_execute_cmd(const char *key
, char *retv
, int inlen
,
4301 struct lxc_conf
*c
, void *data
)
4303 return lxc_get_conf_str(retv
, inlen
, c
->execute_cmd
);
4306 static int get_config_init_cmd(const char *key
, char *retv
, int inlen
,
4307 struct lxc_conf
*c
, void *data
)
4309 return lxc_get_conf_str(retv
, inlen
, c
->init_cmd
);
4312 static int get_config_init_cwd(const char *key
, char *retv
, int inlen
,
4313 struct lxc_conf
*c
, void *data
)
4315 return lxc_get_conf_str(retv
, inlen
, c
->init_cwd
);
4318 static int get_config_init_uid(const char *key
, char *retv
, int inlen
,
4319 struct lxc_conf
*c
, void *data
)
4321 return lxc_get_conf_int(c
, retv
, inlen
, c
->init_uid
);
4324 static int get_config_init_gid(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_gid
);
4330 static int get_config_init_groups(const char *key
, char *retv
, int inlen
,
4331 struct lxc_conf
*c
, void *data
)
4333 int fulllen
= 0, len
;
4338 memset(retv
, 0, inlen
);
4340 if (c
->init_groups
.size
== 0)
4343 for (int i
= 0; i
< c
->init_groups
.size
; i
++)
4344 strprint(retv
, inlen
, "%s%d", (i
> 0) ? "," : "",
4345 c
->init_groups
.list
[i
]);
4350 static int get_config_ephemeral(const char *key
, char *retv
, int inlen
,
4351 struct lxc_conf
*c
, void *data
)
4353 return lxc_get_conf_int(c
, retv
, inlen
, c
->ephemeral
);
4356 static int get_config_no_new_privs(const char *key
, char *retv
, int inlen
,
4357 struct lxc_conf
*c
, void *data
)
4359 return lxc_get_conf_int(c
, retv
, inlen
, c
->no_new_privs
);
4362 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4363 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4364 * printed, in 'lxc.prlimit.resource = value' format.
4366 static int get_config_prlimit(const char *key
, char *retv
, int inlen
,
4367 struct lxc_conf
*c
, void *data
)
4369 int fulllen
= 0, len
;
4370 bool get_all
= false;
4371 struct lxc_list
*it
;
4376 memset(retv
, 0, inlen
);
4378 if (!strcmp(key
, "lxc.prlimit"))
4380 else if (strncmp(key
, "lxc.prlimit.", 12) == 0)
4383 return ret_errno(EINVAL
);
4385 lxc_list_for_each(it
, &c
->limits
) {
4386 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4387 char buf
[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4389 struct lxc_limit
*lim
= it
->elem
;
4391 if (lim
->limit
.rlim_cur
== RLIM_INFINITY
) {
4392 memcpy(buf
, "unlimited", STRLITERALLEN("unlimited") + 1);
4393 partlen
= STRLITERALLEN("unlimited");
4395 partlen
= sprintf(buf
, "%" PRIu64
,
4396 (uint64_t)lim
->limit
.rlim_cur
);
4399 if (lim
->limit
.rlim_cur
!= lim
->limit
.rlim_max
) {
4400 if (lim
->limit
.rlim_max
== RLIM_INFINITY
)
4401 memcpy(buf
+ partlen
, ":unlimited",
4402 STRLITERALLEN(":unlimited") + 1);
4404 sprintf(buf
+ partlen
, ":%" PRIu64
,
4405 (uint64_t)lim
->limit
.rlim_max
);
4409 strprint(retv
, inlen
, "lxc.prlimit.%s = %s\n",
4410 lim
->resource
, buf
);
4411 } else if (strcmp(lim
->resource
, key
) == 0) {
4412 strprint(retv
, inlen
, "%s", buf
);
4419 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4420 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4421 * entries will be printed, in 'lxc.sysctl.key = value' format.
4423 static int get_config_sysctl(const char *key
, char *retv
, int inlen
,
4424 struct lxc_conf
*c
, void *data
)
4427 struct lxc_list
*it
;
4429 bool get_all
= false;
4434 memset(retv
, 0, inlen
);
4436 if (strcmp(key
, "lxc.sysctl") == 0)
4438 else if (strncmp(key
, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4439 key
+= STRLITERALLEN("lxc.sysctl.");
4441 return ret_errno(EINVAL
);
4443 lxc_list_for_each(it
, &c
->sysctls
) {
4444 struct lxc_sysctl
*elem
= it
->elem
;
4446 strprint(retv
, inlen
, "lxc.sysctl.%s = %s\n", elem
->key
,
4448 } else if (strcmp(elem
->key
, key
) == 0) {
4449 strprint(retv
, inlen
, "%s", elem
->value
);
4456 static int get_config_proc(const char *key
, char *retv
, int inlen
,
4457 struct lxc_conf
*c
, void *data
)
4459 struct lxc_list
*it
;
4462 bool get_all
= false;
4467 memset(retv
, 0, inlen
);
4469 if (strcmp(key
, "lxc.proc") == 0)
4471 else if (strncmp(key
, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4472 key
+= STRLITERALLEN("lxc.proc.");
4474 return ret_errno(EINVAL
);
4476 lxc_list_for_each(it
, &c
->procs
) {
4477 struct lxc_proc
*proc
= it
->elem
;
4480 strprint(retv
, inlen
, "lxc.proc.%s = %s\n",
4481 proc
->filename
, proc
->value
);
4482 } else if (strcmp(proc
->filename
, key
) == 0) {
4483 strprint(retv
, inlen
, "%s", proc
->value
);
4490 static int get_config_namespace_clone(const char *key
, char *retv
, int inlen
,
4491 struct lxc_conf
*c
, void *data
)
4499 memset(retv
, 0, inlen
);
4501 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4502 if (c
->ns_clone
& ns_info
[i
].clone_flag
)
4503 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4509 static int get_config_namespace_keep(const char *key
, char *retv
, int inlen
,
4510 struct lxc_conf
*c
, void *data
)
4518 memset(retv
, 0, inlen
);
4520 for (i
= 0; i
< LXC_NS_MAX
; i
++) {
4521 if (c
->ns_keep
& ns_info
[i
].clone_flag
)
4522 strprint(retv
, inlen
, "%s\n", ns_info
[i
].proc_name
);
4528 static int get_config_time_offset_boot(const char *key
, char *retv
, int inlen
, struct lxc_conf
*c
,
4537 memset(retv
, 0, inlen
);
4539 if (c
->timens
.s_boot
) {
4540 strprint(retv
, inlen
, "%" PRId64
" s\n", c
->timens
.s_boot
);
4542 strprint(retv
, inlen
, "%" PRId64
" ns\n", c
->timens
.ns_boot
);
4548 static int get_config_time_offset_monotonic(const char *key
, char *retv
, int inlen
,
4549 struct lxc_conf
*c
, void *data
)
4557 memset(retv
, 0, inlen
);
4559 if (c
->timens
.s_monotonic
) {
4560 strprint(retv
, inlen
, "%" PRId64
"s\n", c
->timens
.s_monotonic
);
4562 strprint(retv
, inlen
, "%" PRId64
"ns\n", c
->timens
.ns_monotonic
);
4568 static int get_config_namespace_share(const char *key
, char *retv
, int inlen
,
4569 struct lxc_conf
*c
, void *data
)
4572 const char *namespace;
4578 memset(retv
, 0, inlen
);
4580 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
4581 ns_idx
= lxc_namespace_2_ns_idx(namespace);
4585 strprint(retv
, inlen
, "%s", c
->ns_share
[ns_idx
]);
4590 /* Callbacks to clear config items. */
4591 static inline int clr_config_personality(const char *key
, struct lxc_conf
*c
,
4594 c
->personality
= -1;
4598 static inline int clr_config_pty_max(const char *key
, struct lxc_conf
*c
,
4605 static inline int clr_config_tty_max(const char *key
, struct lxc_conf
*c
,
4612 static inline int clr_config_tty_dir(const char *key
, struct lxc_conf
*c
,
4615 free_disarm(c
->ttys
.dir
);
4619 static inline int clr_config_apparmor_profile(const char *key
,
4620 struct lxc_conf
*c
, void *data
)
4622 free_disarm(c
->lsm_aa_profile
);
4626 static inline int clr_config_apparmor_allow_incomplete(const char *key
,
4630 c
->lsm_aa_allow_incomplete
= 0;
4634 static inline int clr_config_apparmor_allow_nesting(const char *key
,
4638 c
->lsm_aa_allow_nesting
= 0;
4642 static inline int clr_config_apparmor_raw(const char *key
,
4646 return lxc_clear_apparmor_raw(c
);
4649 static inline int clr_config_selinux_context(const char *key
,
4650 struct lxc_conf
*c
, void *data
)
4652 free_disarm(c
->lsm_se_context
);
4656 static inline int clr_config_selinux_context_keyring(const char *key
,
4657 struct lxc_conf
*c
, void *data
)
4659 free_disarm(c
->lsm_se_keyring_context
);
4663 static inline int clr_config_keyring_session(const char *key
,
4664 struct lxc_conf
*c
, void *data
)
4666 c
->keyring_disable_session
= false;
4670 static inline int clr_config_cgroup_controller(const char *key
,
4671 struct lxc_conf
*c
, void *data
)
4673 return lxc_clear_cgroups(c
, key
, CGROUP_SUPER_MAGIC
);
4676 static inline int clr_config_cgroup2_controller(const char *key
,
4677 struct lxc_conf
*c
, void *data
)
4679 return lxc_clear_cgroups(c
, key
, CGROUP2_SUPER_MAGIC
);
4682 static int clr_config_cgroup_dir(const char *key
, struct lxc_conf
*lxc_conf
,
4685 if (strcmp(key
, "lxc.cgroup.dir") != 0)
4686 return ret_errno(EINVAL
);
4688 if (lxc_conf
->cgroup_meta
.dir
)
4689 free_disarm(lxc_conf
->cgroup_meta
.dir
);
4694 static int clr_config_cgroup_monitor_dir(const char *key
,
4695 struct lxc_conf
*lxc_conf
,
4698 free_disarm(lxc_conf
->cgroup_meta
.monitor_dir
);
4702 static int clr_config_cgroup_monitor_pivot_dir(const char *key
,
4703 struct lxc_conf
*lxc_conf
,
4706 free_disarm(lxc_conf
->cgroup_meta
.monitor_pivot_dir
);
4710 static int clr_config_cgroup_container_dir(const char *key
,
4711 struct lxc_conf
*lxc_conf
,
4714 free_disarm(lxc_conf
->cgroup_meta
.container_dir
);
4718 static int clr_config_cgroup_container_inner_dir(const char *key
,
4719 struct lxc_conf
*lxc_conf
,
4722 free_disarm(lxc_conf
->cgroup_meta
.namespace_dir
);
4726 static inline int clr_config_cgroup_relative(const char *key
,
4727 struct lxc_conf
*lxc_conf
,
4730 lxc_conf
->cgroup_meta
.relative
= false;
4734 static inline int clr_config_idmaps(const char *key
, struct lxc_conf
*c
,
4737 return lxc_clear_idmaps(c
);
4740 static inline int clr_config_log_level(const char *key
, struct lxc_conf
*c
,
4743 c
->loglevel
= LXC_LOG_LEVEL_NOTSET
;
4747 static inline int clr_config_log_file(const char *key
, struct lxc_conf
*c
,
4750 free_disarm(c
->logfile
);
4754 static inline int clr_config_mount(const char *key
, struct lxc_conf
*c
,
4757 return lxc_clear_mount_entries(c
);
4760 static inline int clr_config_mount_auto(const char *key
, struct lxc_conf
*c
,
4763 return lxc_clear_automounts(c
);
4766 static inline int clr_config_mount_fstab(const char *key
, struct lxc_conf
*c
,
4769 free_disarm(c
->fstab
);
4773 static inline int clr_config_rootfs_path(const char *key
, struct lxc_conf
*c
,
4776 free_disarm(c
->rootfs
.path
);
4780 static inline int clr_config_rootfs_managed(const char *key
, struct lxc_conf
*c
,
4783 c
->rootfs
.managed
= true;
4787 static inline int clr_config_rootfs_mount(const char *key
, struct lxc_conf
*c
,
4790 free_disarm(c
->rootfs
.mount
);
4794 static inline int clr_config_rootfs_options(const char *key
, struct lxc_conf
*c
,
4797 free_disarm(c
->rootfs
.options
);
4798 free_disarm(c
->rootfs
.data
);
4803 static inline int clr_config_uts_name(const char *key
, struct lxc_conf
*c
,
4806 free_disarm(c
->utsname
);
4810 static inline int clr_config_hooks(const char *key
, struct lxc_conf
*c
,
4813 return lxc_clear_hooks(c
, key
);
4816 static inline int clr_config_hooks_version(const char *key
, struct lxc_conf
*c
,
4819 /* default to legacy hooks version */
4820 c
->hooks_version
= 0;
4824 static inline int clr_config_net(const char *key
, struct lxc_conf
*c
,
4827 lxc_free_networks(&c
->network
);
4832 static inline int clr_config_cap_drop(const char *key
, struct lxc_conf
*c
,
4835 return lxc_clear_config_caps(c
);
4838 static inline int clr_config_cap_keep(const char *key
, struct lxc_conf
*c
,
4841 return lxc_clear_config_keepcaps(c
);
4844 static inline int clr_config_console_path(const char *key
, struct lxc_conf
*c
,
4847 free_disarm(c
->console
.path
);
4851 static inline int clr_config_console_logfile(const char *key
,
4852 struct lxc_conf
*c
, void *data
)
4854 free_disarm(c
->console
.log_path
);
4858 static inline int clr_config_console_rotate(const char *key
, struct lxc_conf
*c
,
4861 c
->console
.log_rotate
= 0;
4865 static inline int clr_config_console_buffer_size(const char *key
,
4866 struct lxc_conf
*c
, void *data
)
4868 c
->console
.buffer_size
= 0;
4872 static inline int clr_config_console_size(const char *key
, struct lxc_conf
*c
,
4875 c
->console
.log_size
= 0;
4879 static inline int clr_config_seccomp_allow_nesting(const char *key
,
4880 struct lxc_conf
*c
, void *data
)
4883 c
->seccomp
.allow_nesting
= 0;
4886 return ret_errno(ENOSYS
);
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_disarm(c
->seccomp
.notifier
.cookie
);
4897 return ret_errno(ENOSYS
);
4901 static inline int clr_config_seccomp_notify_proxy(const char *key
,
4902 struct lxc_conf
*c
, void *data
)
4904 #ifdef HAVE_SECCOMP_NOTIFY
4905 memset(&c
->seccomp
.notifier
.proxy_addr
, 0,
4906 sizeof(c
->seccomp
.notifier
.proxy_addr
));
4909 return ret_errno(ENOSYS
);
4913 static inline int clr_config_seccomp_profile(const char *key
,
4914 struct lxc_conf
*c
, void *data
)
4916 free_disarm(c
->seccomp
.seccomp
);
4920 static inline int clr_config_autodev(const char *key
, struct lxc_conf
*c
,
4927 static inline int clr_config_autodev_tmpfs_size(const char *key
, struct lxc_conf
*c
,
4930 c
->autodevtmpfssize
= 500000;
4934 static inline int clr_config_signal_halt(const char *key
, struct lxc_conf
*c
,
4941 static inline int clr_config_signal_reboot(const char *key
, struct lxc_conf
*c
,
4944 c
->rebootsignal
= 0;
4948 static inline int clr_config_signal_stop(const char *key
, struct lxc_conf
*c
,
4955 static inline int clr_config_start(const char *key
, struct lxc_conf
*c
,
4958 if (strcmp(key
+ 10, "auto") == 0)
4960 else if (strcmp(key
+ 10, "delay") == 0)
4962 else if (strcmp(key
+ 10, "order") == 0)
4968 static inline int clr_config_log_syslog(const char *key
, struct lxc_conf
*c
,
4971 free_disarm(c
->syslog
);
4975 static inline int clr_config_monitor(const char *key
, struct lxc_conf
*c
,
4978 c
->monitor_unshare
= 0;
4982 static inline int clr_config_monitor_signal_pdeath(const char *key
,
4983 struct lxc_conf
*c
, void *data
)
4985 c
->monitor_signal_pdeath
= 0;
4989 static inline int clr_config_group(const char *key
, struct lxc_conf
*c
,
4992 return lxc_clear_groups(c
);
4995 static inline int clr_config_environment(const char *key
, struct lxc_conf
*c
,
4998 return lxc_clear_environment(c
);
5001 static inline int clr_config_execute_cmd(const char *key
, struct lxc_conf
*c
,
5004 free_disarm(c
->execute_cmd
);
5008 static inline int clr_config_init_cmd(const char *key
, struct lxc_conf
*c
,
5011 free_disarm(c
->init_cmd
);
5015 static inline int clr_config_init_cwd(const char *key
, struct lxc_conf
*c
,
5018 free_disarm(c
->init_cwd
);
5022 static inline int clr_config_init_uid(const char *key
, struct lxc_conf
*c
,
5029 static inline int clr_config_init_gid(const char *key
, struct lxc_conf
*c
,
5036 static inline int clr_config_init_groups(const char *key
, struct lxc_conf
*c
,
5039 free_disarm(c
->init_groups
.list
);
5040 c
->init_groups
.size
= 0;
5044 static inline int clr_config_ephemeral(const char *key
, struct lxc_conf
*c
,
5051 static inline int clr_config_no_new_privs(const char *key
, struct lxc_conf
*c
,
5054 c
->no_new_privs
= false;
5058 static inline int clr_config_prlimit(const char *key
, struct lxc_conf
*c
,
5061 return lxc_clear_limits(c
, key
);
5064 static inline int clr_config_sysctl(const char *key
, struct lxc_conf
*c
,
5067 return lxc_clear_sysctls(c
, key
);
5070 static inline int clr_config_proc(const char *key
, struct lxc_conf
*c
,
5073 return lxc_clear_procs(c
, key
);
5076 static inline int clr_config_includefiles(const char *key
, struct lxc_conf
*c
,
5079 lxc_clear_includes(c
);
5083 static int clr_config_namespace_clone(const char *key
,
5084 struct lxc_conf
*lxc_conf
, void *data
)
5086 lxc_conf
->ns_clone
= 0;
5090 static int clr_config_namespace_keep(const char *key
, struct lxc_conf
*lxc_conf
,
5093 lxc_conf
->ns_keep
= 0;
5097 static int clr_config_time_offset_boot(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5099 lxc_conf
->timens
.s_boot
= 0;
5100 lxc_conf
->timens
.ns_boot
= 0;
5104 static int clr_config_time_offset_monotonic(const char *key
, struct lxc_conf
*lxc_conf
, void *data
)
5106 lxc_conf
->timens
.s_monotonic
= 0;
5107 lxc_conf
->timens
.ns_monotonic
= 0;
5111 static int clr_config_namespace_share(const char *key
,
5112 struct lxc_conf
*lxc_conf
, void *data
)
5115 const char *namespace;
5117 namespace = key
+ STRLITERALLEN("lxc.namespace.share.");
5118 ns_idx
= lxc_namespace_2_ns_idx(namespace);
5122 free(lxc_conf
->ns_share
[ns_idx
]);
5123 lxc_conf
->ns_share
[ns_idx
] = NULL
;
5128 static int get_config_includefiles(const char *key
, char *retv
, int inlen
,
5129 struct lxc_conf
*c
, void *data
)
5131 return ret_errno(ENOSYS
);
5134 static struct lxc_config_t
*get_network_config_ops(const char *key
,
5135 struct lxc_conf
*lxc_conf
,
5137 char **deindexed_key
)
5139 __do_free
char *copy
= NULL
;
5140 struct lxc_config_t
*config
= NULL
;
5142 unsigned int tmpidx
;
5144 char *idx_start
, *idx_end
;
5146 /* check that this is a sensible network key */
5147 if (strncmp("lxc.net.", key
, 8))
5148 return log_error_errno(NULL
, EINVAL
, "Invalid network configuration key \"%s\"", key
);
5152 return log_error_errno(NULL
, ENOMEM
, "Failed to duplicate string \"%s\"", key
);
5155 if (!isdigit(*(key
+ 8)))
5156 return log_error_errno(NULL
, EINVAL
, "Failed to detect digit in string \"%s\"", key
+ 8);
5158 /* beginning of index string */
5159 idx_start
= (copy
+ 7);
5162 /* end of index string */
5163 idx_end
= strchr((copy
+ 8), '.');
5167 /* parse current index */
5168 ret
= lxc_safe_uint((idx_start
+ 1), &tmpidx
);
5171 return log_error_errno(NULL
, -ret
, "Failed to parse unsigned integer from string \"%s\"", idx_start
+ 1);
5174 /* This, of course is utterly nonsensical on so many levels, but
5175 * better safe than sorry.
5176 * (Checking for INT_MAX here is intentional.)
5178 if (tmpidx
== INT_MAX
)
5179 return log_error_errno(NULL
, ERANGE
, "Number of configured networks would overflow the counter");
5182 numstrlen
= strlen((idx_start
+ 1));
5184 /* repair configuration key */
5187 /* lxc.net.<idx>.<subkey> */
5190 if (strlen(idx_end
+ 1) == 0)
5191 return log_error_errno(NULL
, EINVAL
, "No subkey in network configuration key \"%s\"", key
);
5193 memmove(copy
+ 8, idx_end
+ 1, strlen(idx_end
+ 1));
5194 copy
[strlen(key
) - (numstrlen
+ 1)] = '\0';
5196 config
= lxc_get_config(copy
);
5198 return log_error_errno(NULL
, ENOENT
, "Unknown network configuration key \"%s\"", key
);
5202 *deindexed_key
= move_ptr(copy
);
5207 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5208 * found. So we make sure next comes an integer, find the right callback (by
5209 * rewriting the key), and call it.
5211 static int set_config_net_nic(const char *key
, const char *value
,
5212 struct lxc_conf
*lxc_conf
, void *data
)
5214 __do_free
char *deindexed_key
= NULL
;
5216 const char *idxstring
;
5217 struct lxc_config_t
*config
;
5218 struct lxc_netdev
*netdev
;
5220 idxstring
= key
+ 8;
5221 if (!isdigit(*idxstring
))
5222 return ret_errno(EINVAL
);
5224 if (lxc_config_value_empty(value
))
5225 return clr_config_net_nic(key
, lxc_conf
, data
);
5227 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5228 if (!config
|| idx
< 0)
5231 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, true);
5233 return ret_errno(EINVAL
);
5235 return config
->set(deindexed_key
, value
, lxc_conf
, netdev
);
5238 static int clr_config_net_nic(const char *key
, struct lxc_conf
*lxc_conf
,
5241 __do_free
char *deindexed_key
= NULL
;
5244 const char *idxstring
;
5245 struct lxc_config_t
*config
;
5246 struct lxc_netdev
*netdev
;
5248 idxstring
= key
+ 8;
5249 if (!isdigit(*idxstring
))
5250 return ret_errno(EINVAL
);
5252 /* The left conjunct is pretty self-explanatory. The right conjunct
5253 * checks whether the two pointers are equal. If they are we know that
5254 * this is not a key that is namespaced any further and so we are
5255 * supposed to clear the whole network.
5257 if (isdigit(*idxstring
) && (strrchr(key
, '.') == (idxstring
- 1))) {
5258 unsigned int rmnetdevidx
;
5260 ret
= lxc_safe_uint(idxstring
, &rmnetdevidx
);
5264 /* Remove network from network list. */
5265 lxc_remove_nic_by_idx(lxc_conf
, rmnetdevidx
);
5269 config
= get_network_config_ops(key
, lxc_conf
, &idx
, &deindexed_key
);
5270 if (!config
|| idx
< 0)
5273 netdev
= lxc_get_netdev_by_idx(lxc_conf
, (unsigned int)idx
, false);
5275 return ret_errno(EINVAL
);
5277 return config
->clr(deindexed_key
, lxc_conf
, netdev
);
5280 static int clr_config_net_type(const char *key
, struct lxc_conf
*lxc_conf
,
5283 struct lxc_netdev
*netdev
= data
;
5286 return ret_errno(EINVAL
);
5293 static int clr_config_net_name(const char *key
, struct lxc_conf
*lxc_conf
,
5296 struct lxc_netdev
*netdev
= data
;
5299 return ret_errno(EINVAL
);
5301 netdev
->name
[0] = '\0';
5306 static int clr_config_net_flags(const char *key
, struct lxc_conf
*lxc_conf
,
5309 struct lxc_netdev
*netdev
= data
;
5312 return ret_errno(EINVAL
);
5319 static int clr_config_net_link(const char *key
, struct lxc_conf
*lxc_conf
,
5322 struct lxc_netdev
*netdev
= data
;
5325 return ret_errno(EINVAL
);
5327 netdev
->link
[0] = '\0';
5332 static int clr_config_net_l2proxy(const char *key
, struct lxc_conf
*lxc_conf
,
5335 struct lxc_netdev
*netdev
= data
;
5338 return ret_errno(EINVAL
);
5340 netdev
->l2proxy
= false;
5345 static int clr_config_net_macvlan_mode(const char *key
,
5346 struct lxc_conf
*lxc_conf
, void *data
)
5348 struct lxc_netdev
*netdev
= data
;
5351 return ret_errno(EINVAL
);
5353 if (netdev
->type
!= LXC_NET_MACVLAN
)
5356 netdev
->priv
.macvlan_attr
.mode
= -1;
5361 static int clr_config_net_ipvlan_mode(const char *key
,
5362 struct lxc_conf
*lxc_conf
, void *data
)
5364 struct lxc_netdev
*netdev
= data
;
5367 return ret_errno(EINVAL
);
5369 if (netdev
->type
!= LXC_NET_IPVLAN
)
5372 netdev
->priv
.ipvlan_attr
.mode
= -1;
5377 static int clr_config_net_ipvlan_isolation(const char *key
,
5378 struct lxc_conf
*lxc_conf
, void *data
)
5380 struct lxc_netdev
*netdev
= data
;
5383 return ret_errno(EINVAL
);
5385 if (netdev
->type
!= LXC_NET_IPVLAN
)
5388 netdev
->priv
.ipvlan_attr
.isolation
= -1;
5393 static int clr_config_net_veth_mode(const char *key
,
5394 struct lxc_conf
*lxc_conf
, void *data
)
5396 struct lxc_netdev
*netdev
= data
;
5399 return ret_errno(EINVAL
);
5401 if (netdev
->type
!= LXC_NET_VETH
)
5404 netdev
->priv
.veth_attr
.mode
= -1;
5409 static int clr_config_net_veth_pair(const char *key
, struct lxc_conf
*lxc_conf
,
5412 struct lxc_netdev
*netdev
= data
;
5415 return ret_errno(EINVAL
);
5417 netdev
->priv
.veth_attr
.pair
[0] = '\0';
5422 static int clr_config_net_veth_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5425 struct lxc_netdev
*netdev
= data
;
5428 return ret_errno(EINVAL
);
5430 netdev
->priv
.veth_attr
.vlan_id
= 0;
5431 netdev
->priv
.veth_attr
.vlan_id_set
= false;
5436 static int clr_config_net_veth_vlan_tagged_id(const char *key
,
5437 struct lxc_conf
*lxc_conf
, void *data
)
5439 struct lxc_netdev
*netdev
= data
;
5440 struct lxc_list
*cur
, *next
;
5443 return ret_errno(EINVAL
);
5445 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
, next
) {
5454 static int clr_config_net_script_up(const char *key
, struct lxc_conf
*lxc_conf
,
5457 struct lxc_netdev
*netdev
= data
;
5460 return ret_errno(EINVAL
);
5462 free_disarm(netdev
->upscript
);
5467 static int clr_config_net_script_down(const char *key
,
5468 struct lxc_conf
*lxc_conf
, void *data
)
5470 struct lxc_netdev
*netdev
= data
;
5473 return ret_errno(EINVAL
);
5475 free_disarm(netdev
->downscript
);
5480 static int clr_config_net_hwaddr(const char *key
, struct lxc_conf
*lxc_conf
,
5483 struct lxc_netdev
*netdev
= data
;
5486 return ret_errno(EINVAL
);
5488 free_disarm(netdev
->hwaddr
);
5493 static int clr_config_net_mtu(const char *key
, struct lxc_conf
*lxc_conf
,
5496 struct lxc_netdev
*netdev
= data
;
5499 return ret_errno(EINVAL
);
5501 free_disarm(netdev
->mtu
);
5506 static int clr_config_net_vlan_id(const char *key
, struct lxc_conf
*lxc_conf
,
5509 struct lxc_netdev
*netdev
= data
;
5512 return ret_errno(EINVAL
);
5514 netdev
->priv
.vlan_attr
.vid
= 0;
5519 static int clr_config_net_ipv4_gateway(const char *key
,
5520 struct lxc_conf
*lxc_conf
, void *data
)
5522 struct lxc_netdev
*netdev
= data
;
5525 return ret_errno(EINVAL
);
5527 free_disarm(netdev
->ipv4_gateway
);
5532 static int clr_config_net_ipv4_address(const char *key
,
5533 struct lxc_conf
*lxc_conf
, void *data
)
5535 struct lxc_netdev
*netdev
= data
;
5536 struct lxc_list
*cur
, *next
;
5539 return ret_errno(EINVAL
);
5541 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
5550 static int clr_config_net_veth_ipv4_route(const char *key
,
5551 struct lxc_conf
*lxc_conf
, void *data
)
5553 struct lxc_netdev
*netdev
= data
;
5554 struct lxc_list
*cur
, *next
;
5557 return ret_errno(EINVAL
);
5559 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv4_routes
, next
) {
5568 static int clr_config_net_ipv6_gateway(const char *key
,
5569 struct lxc_conf
*lxc_conf
, void *data
)
5571 struct lxc_netdev
*netdev
= data
;
5574 return ret_errno(EINVAL
);
5576 free_disarm(netdev
->ipv6_gateway
);
5581 static int clr_config_net_ipv6_address(const char *key
,
5582 struct lxc_conf
*lxc_conf
, void *data
)
5584 struct lxc_netdev
*netdev
= data
;
5585 struct lxc_list
*cur
, *next
;
5588 return ret_errno(EINVAL
);
5590 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
5599 static int clr_config_net_veth_ipv6_route(const char *key
,
5600 struct lxc_conf
*lxc_conf
, void *data
)
5602 struct lxc_netdev
*netdev
= data
;
5603 struct lxc_list
*cur
, *next
;
5606 return ret_errno(EINVAL
);
5608 lxc_list_for_each_safe(cur
, &netdev
->priv
.veth_attr
.ipv6_routes
, next
) {
5617 static int get_config_net_nic(const char *key
, char *retv
, int inlen
,
5618 struct lxc_conf
*c
, void *data
)
5620 __do_free
char *deindexed_key
= NULL
;
5622 const char *idxstring
;
5623 struct lxc_config_t
*config
;
5624 struct lxc_netdev
*netdev
;
5626 idxstring
= key
+ 8;
5627 if (!isdigit(*idxstring
))
5628 return ret_errno(EINVAL
);
5630 config
= get_network_config_ops(key
, c
, &idx
, &deindexed_key
);
5631 if (!config
|| idx
< 0)
5634 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
5636 return ret_errno(EINVAL
);
5638 return config
->get(deindexed_key
, retv
, inlen
, c
, netdev
);
5641 static int get_config_net_type(const char *key
, char *retv
, int inlen
,
5642 struct lxc_conf
*c
, void *data
)
5646 struct lxc_netdev
*netdev
= data
;
5651 memset(retv
, 0, inlen
);
5654 return ret_errno(EINVAL
);
5656 strprint(retv
, inlen
, "%s", lxc_net_type_to_str(netdev
->type
));
5661 static int get_config_net_flags(const char *key
, char *retv
, int inlen
,
5662 struct lxc_conf
*c
, void *data
)
5666 struct lxc_netdev
*netdev
= data
;
5671 memset(retv
, 0, inlen
);
5674 return ret_errno(EINVAL
);
5676 if (netdev
->flags
& IFF_UP
)
5677 strprint(retv
, inlen
, "up");
5682 static int get_config_net_link(const char *key
, char *retv
, int inlen
,
5683 struct lxc_conf
*c
, void *data
)
5687 struct lxc_netdev
*netdev
= data
;
5692 memset(retv
, 0, inlen
);
5695 return ret_errno(EINVAL
);
5697 if (netdev
->link
[0] != '\0')
5698 strprint(retv
, inlen
, "%s", netdev
->link
);
5703 static int get_config_net_l2proxy(const char *key
, char *retv
, int inlen
,
5704 struct lxc_conf
*c
, void *data
)
5706 struct lxc_netdev
*netdev
= data
;
5707 return lxc_get_conf_bool(c
, retv
, inlen
, netdev
->l2proxy
);
5710 static int get_config_net_name(const char *key
, char *retv
, int inlen
,
5711 struct lxc_conf
*c
, void *data
)
5715 struct lxc_netdev
*netdev
= data
;
5720 memset(retv
, 0, inlen
);
5723 return ret_errno(EINVAL
);
5725 if (netdev
->name
[0] != '\0')
5726 strprint(retv
, inlen
, "%s", netdev
->name
);
5731 static int get_config_net_macvlan_mode(const char *key
, char *retv
, int inlen
,
5732 struct lxc_conf
*c
, void *data
)
5737 struct lxc_netdev
*netdev
= data
;
5742 memset(retv
, 0, inlen
);
5745 return ret_errno(EINVAL
);
5747 if (netdev
->type
!= LXC_NET_MACVLAN
)
5750 switch (netdev
->priv
.macvlan_attr
.mode
) {
5751 case MACVLAN_MODE_PRIVATE
:
5754 case MACVLAN_MODE_VEPA
:
5757 case MACVLAN_MODE_BRIDGE
:
5760 case MACVLAN_MODE_PASSTHRU
:
5768 strprint(retv
, inlen
, "%s", mode
);
5773 static int get_config_net_ipvlan_mode(const char *key
, char *retv
, int inlen
,
5774 struct lxc_conf
*c
, void *data
)
5777 struct lxc_netdev
*netdev
= data
;
5784 memset(retv
, 0, inlen
);
5787 return ret_errno(EINVAL
);
5789 if (netdev
->type
!= LXC_NET_IPVLAN
)
5792 switch (netdev
->priv
.ipvlan_attr
.mode
) {
5793 case IPVLAN_MODE_L3
:
5796 case IPVLAN_MODE_L3S
:
5799 case IPVLAN_MODE_L2
:
5807 strprint(retv
, inlen
, "%s", mode
);
5812 static int get_config_net_ipvlan_isolation(const char *key
, char *retv
, int inlen
,
5813 struct lxc_conf
*c
, void *data
)
5816 struct lxc_netdev
*netdev
= data
;
5823 memset(retv
, 0, inlen
);
5826 return ret_errno(EINVAL
);
5828 if (netdev
->type
!= LXC_NET_IPVLAN
)
5831 switch (netdev
->priv
.ipvlan_attr
.isolation
) {
5832 case IPVLAN_ISOLATION_BRIDGE
:
5835 case IPVLAN_ISOLATION_PRIVATE
:
5838 case IPVLAN_ISOLATION_VEPA
:
5846 strprint(retv
, inlen
, "%s", mode
);
5851 static int get_config_net_veth_mode(const char *key
, char *retv
, int inlen
,
5852 struct lxc_conf
*c
, void *data
)
5855 struct lxc_netdev
*netdev
= data
;
5862 memset(retv
, 0, inlen
);
5865 return ret_errno(EINVAL
);
5867 if (netdev
->type
!= LXC_NET_VETH
)
5870 switch (netdev
->priv
.veth_attr
.mode
) {
5871 case VETH_MODE_BRIDGE
:
5874 case VETH_MODE_ROUTER
:
5882 strprint(retv
, inlen
, "%s", mode
);
5887 static int get_config_net_veth_pair(const char *key
, char *retv
, int inlen
,
5888 struct lxc_conf
*c
, void *data
)
5892 struct lxc_netdev
*netdev
= data
;
5897 memset(retv
, 0, inlen
);
5900 return ret_errno(EINVAL
);
5902 if (netdev
->type
!= LXC_NET_VETH
)
5905 strprint(retv
, inlen
, "%s",
5906 netdev
->priv
.veth_attr
.pair
[0] != '\0'
5907 ? netdev
->priv
.veth_attr
.pair
5908 : netdev
->priv
.veth_attr
.veth1
);
5913 static int get_config_net_veth_vlan_id(const char *key
, char *retv
, int inlen
,
5914 struct lxc_conf
*c
, void *data
)
5918 struct lxc_netdev
*netdev
= data
;
5921 return ret_errno(EINVAL
);
5923 if (netdev
->type
!= LXC_NET_VETH
)
5929 memset(retv
, 0, inlen
);
5931 strprint(retv
, inlen
, "%d", netdev
->priv
.veth_attr
.vlan_id
);
5936 static int get_config_net_veth_vlan_tagged_id(const char *key
, char *retv
, int inlen
,
5937 struct lxc_conf
*c
, void *data
)
5941 struct lxc_list
*it
;
5943 struct lxc_netdev
*netdev
= data
;
5946 return ret_errno(EINVAL
);
5948 if (netdev
->type
!= LXC_NET_VETH
)
5954 memset(retv
, 0, inlen
);
5956 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.vlan_tagged_ids
);
5958 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.vlan_tagged_ids
) {
5959 unsigned short i
= PTR_TO_USHORT(it
->elem
);
5960 strprint(retv
, inlen
, "%u%s", i
,
5961 (listlen
-- > 1) ? "\n" : "");
5967 static int get_config_net_script_up(const char *key
, char *retv
, int inlen
,
5968 struct lxc_conf
*c
, void *data
)
5972 struct lxc_netdev
*netdev
= data
;
5977 memset(retv
, 0, inlen
);
5980 return ret_errno(EINVAL
);
5982 if (netdev
->upscript
)
5983 strprint(retv
, inlen
, "%s", netdev
->upscript
);
5988 static int get_config_net_script_down(const char *key
, char *retv
, int inlen
,
5989 struct lxc_conf
*c
, void *data
)
5993 struct lxc_netdev
*netdev
= data
;
5998 memset(retv
, 0, inlen
);
6001 return ret_errno(EINVAL
);
6003 if (netdev
->downscript
)
6004 strprint(retv
, inlen
, "%s", netdev
->downscript
);
6009 static int get_config_net_hwaddr(const char *key
, char *retv
, int inlen
,
6010 struct lxc_conf
*c
, void *data
)
6014 struct lxc_netdev
*netdev
= data
;
6019 memset(retv
, 0, inlen
);
6022 return ret_errno(EINVAL
);
6025 strprint(retv
, inlen
, "%s", netdev
->hwaddr
);
6030 static int get_config_net_mtu(const char *key
, char *retv
, int inlen
,
6031 struct lxc_conf
*c
, void *data
)
6035 struct lxc_netdev
*netdev
= data
;
6040 memset(retv
, 0, inlen
);
6043 return ret_errno(EINVAL
);
6046 strprint(retv
, inlen
, "%s", netdev
->mtu
);
6051 static int get_config_net_vlan_id(const char *key
, char *retv
, int inlen
,
6052 struct lxc_conf
*c
, void *data
)
6056 struct lxc_netdev
*netdev
= data
;
6061 memset(retv
, 0, inlen
);
6064 return ret_errno(EINVAL
);
6066 if (netdev
->type
!= LXC_NET_VLAN
)
6069 strprint(retv
, inlen
, "%d", netdev
->priv
.vlan_attr
.vid
);
6074 static int get_config_net_ipv4_gateway(const char *key
, char *retv
, int inlen
,
6075 struct lxc_conf
*c
, void *data
)
6078 char buf
[INET_ADDRSTRLEN
];
6080 struct lxc_netdev
*netdev
= data
;
6085 memset(retv
, 0, inlen
);
6088 return ret_errno(EINVAL
);
6090 if (netdev
->ipv4_gateway_auto
) {
6091 strprint(retv
, inlen
, "auto");
6092 } else if (netdev
->ipv4_gateway_dev
) {
6093 strprint(retv
, inlen
, "dev");
6094 } else if (netdev
->ipv4_gateway
) {
6095 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
)))
6097 strprint(retv
, inlen
, "%s", buf
);
6103 static int get_config_net_ipv4_address(const char *key
, char *retv
, int inlen
,
6104 struct lxc_conf
*c
, void *data
)
6108 char buf
[INET_ADDRSTRLEN
];
6109 struct lxc_list
*it
;
6111 struct lxc_netdev
*netdev
= data
;
6116 memset(retv
, 0, inlen
);
6119 return ret_errno(EINVAL
);
6121 listlen
= lxc_list_len(&netdev
->ipv4
);
6123 lxc_list_for_each(it
, &netdev
->ipv4
) {
6124 struct lxc_inetdev
*i
= it
->elem
;
6125 if (!inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
)))
6127 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6128 (listlen
-- > 1) ? "\n" : "");
6134 static int get_config_net_veth_ipv4_route(const char *key
, char *retv
, int inlen
,
6135 struct lxc_conf
*c
, void *data
)
6139 char buf
[INET_ADDRSTRLEN
];
6140 struct lxc_list
*it
;
6142 struct lxc_netdev
*netdev
= data
;
6147 memset(retv
, 0, inlen
);
6150 return ret_errno(EINVAL
);
6152 if (netdev
->type
!= LXC_NET_VETH
)
6155 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv4_routes
);
6157 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv4_routes
) {
6158 struct lxc_inetdev
*i
= it
->elem
;
6159 if (!inet_ntop(AF_INET
, &i
->addr
, buf
, sizeof(buf
)))
6161 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6162 (listlen
-- > 1) ? "\n" : "");
6168 static int get_config_net_ipv6_gateway(const char *key
, char *retv
, int inlen
,
6169 struct lxc_conf
*c
, void *data
)
6172 char buf
[INET6_ADDRSTRLEN
];
6174 struct lxc_netdev
*netdev
= data
;
6179 memset(retv
, 0, inlen
);
6182 return ret_errno(EINVAL
);
6184 if (netdev
->ipv6_gateway_auto
) {
6185 strprint(retv
, inlen
, "auto");
6186 } else if (netdev
->ipv6_gateway_dev
) {
6187 strprint(retv
, inlen
, "dev");
6188 } else if (netdev
->ipv6_gateway
) {
6189 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
)))
6191 strprint(retv
, inlen
, "%s", buf
);
6197 static int get_config_net_ipv6_address(const char *key
, char *retv
, int inlen
,
6198 struct lxc_conf
*c
, void *data
)
6202 char buf
[INET6_ADDRSTRLEN
];
6203 struct lxc_list
*it
;
6205 struct lxc_netdev
*netdev
= data
;
6210 memset(retv
, 0, inlen
);
6213 return ret_errno(EINVAL
);
6215 listlen
= lxc_list_len(&netdev
->ipv6
);
6217 lxc_list_for_each(it
, &netdev
->ipv6
) {
6218 struct lxc_inet6dev
*i
= it
->elem
;
6219 if (!inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
)))
6221 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6222 (listlen
-- > 1) ? "\n" : "");
6228 static int get_config_net_veth_ipv6_route(const char *key
, char *retv
, int inlen
,
6229 struct lxc_conf
*c
, void *data
)
6233 char buf
[INET6_ADDRSTRLEN
];
6234 struct lxc_list
*it
;
6236 struct lxc_netdev
*netdev
= data
;
6241 memset(retv
, 0, inlen
);
6244 return ret_errno(EINVAL
);
6246 if (netdev
->type
!= LXC_NET_VETH
)
6249 listlen
= lxc_list_len(&netdev
->priv
.veth_attr
.ipv6_routes
);
6251 lxc_list_for_each(it
, &netdev
->priv
.veth_attr
.ipv6_routes
) {
6252 struct lxc_inet6dev
*i
= it
->elem
;
6253 if (!inet_ntop(AF_INET6
, &i
->addr
, buf
, sizeof(buf
)))
6255 strprint(retv
, inlen
, "%s/%u%s", buf
, i
->prefix
,
6256 (listlen
-- > 1) ? "\n" : "");
6262 int lxc_list_config_items(char *retv
, int inlen
)
6271 memset(retv
, 0, inlen
);
6273 for (i
= 0; i
< config_jump_table_size
; i
++) {
6274 char *s
= config_jump_table
[i
].name
;
6276 if (s
[strlen(s
) - 1] == '.')
6279 strprint(retv
, inlen
, "%s\n", s
);
6285 int lxc_list_subkeys(struct lxc_conf
*conf
, const char *key
, char *retv
,
6294 memset(retv
, 0, inlen
);
6296 if (!strcmp(key
, "lxc.apparmor")) {
6297 strprint(retv
, inlen
, "allow_incomplete\n");
6298 strprint(retv
, inlen
, "allow_nesting\n");
6299 strprint(retv
, inlen
, "profile\n");
6300 strprint(retv
, inlen
, "raw\n");
6301 } else if (!strcmp(key
, "lxc.cgroup")) {
6302 strprint(retv
, inlen
, "dir\n");
6303 } else if (!strcmp(key
, "lxc.selinux")) {
6304 strprint(retv
, inlen
, "context\n");
6305 strprint(retv
, inlen
, "context.keyring\n");
6306 } else if (!strcmp(key
, "lxc.mount")) {
6307 strprint(retv
, inlen
, "auto\n");
6308 strprint(retv
, inlen
, "entry\n");
6309 strprint(retv
, inlen
, "fstab\n");
6310 } else if (!strcmp(key
, "lxc.rootfs")) {
6311 strprint(retv
, inlen
, "mount\n");
6312 strprint(retv
, inlen
, "options\n");
6313 strprint(retv
, inlen
, "path\n");
6314 } else if (!strcmp(key
, "lxc.uts")) {
6315 strprint(retv
, inlen
, "name\n");
6316 } else if (!strcmp(key
, "lxc.hook")) {
6317 strprint(retv
, inlen
, "autodev\n");
6318 strprint(retv
, inlen
, "autodevtmpfssize\n");
6319 strprint(retv
, inlen
, "clone\n");
6320 strprint(retv
, inlen
, "destroy\n");
6321 strprint(retv
, inlen
, "mount\n");
6322 strprint(retv
, inlen
, "post-stop\n");
6323 strprint(retv
, inlen
, "pre-mount\n");
6324 strprint(retv
, inlen
, "pre-start\n");
6325 strprint(retv
, inlen
, "start-host\n");
6326 strprint(retv
, inlen
, "start\n");
6327 strprint(retv
, inlen
, "stop\n");
6328 } else if (!strcmp(key
, "lxc.cap")) {
6329 strprint(retv
, inlen
, "drop\n");
6330 strprint(retv
, inlen
, "keep\n");
6331 } else if (!strcmp(key
, "lxc.console")) {
6332 strprint(retv
, inlen
, "logfile\n");
6333 strprint(retv
, inlen
, "path\n");
6334 } else if (!strcmp(key
, "lxc.seccomp")) {
6335 strprint(retv
, inlen
, "profile\n");
6336 } else if (!strcmp(key
, "lxc.signal")) {
6337 strprint(retv
, inlen
, "halt\n");
6338 strprint(retv
, inlen
, "reboot\n");
6339 strprint(retv
, inlen
, "stop\n");
6340 } else if (!strcmp(key
, "lxc.start")) {
6341 strprint(retv
, inlen
, "auto\n");
6342 strprint(retv
, inlen
, "delay\n");
6343 strprint(retv
, inlen
, "order\n");
6344 } else if (!strcmp(key
, "lxc.monitor")) {
6345 strprint(retv
, inlen
, "unshare\n");
6346 } else if (!strcmp(key
, "lxc.keyring")) {
6347 strprint(retv
, inlen
, "session\n");
6349 fulllen
= ret_errno(EINVAL
);
6355 int lxc_list_net(struct lxc_conf
*c
, const char *key
, char *retv
, int inlen
)
6358 const char *idxstring
;
6359 struct lxc_netdev
*netdev
;
6363 idxstring
= key
+ 8;
6364 if (!isdigit(*idxstring
))
6367 (void)get_network_config_ops(key
, c
, &idx
, NULL
);
6369 return ret_errno(EINVAL
);
6371 netdev
= lxc_get_netdev_by_idx(c
, (unsigned int)idx
, false);
6373 return ret_errno(EINVAL
);
6378 memset(retv
, 0, inlen
);
6380 strprint(retv
, inlen
, "type\n");
6381 strprint(retv
, inlen
, "script.up\n");
6382 strprint(retv
, inlen
, "script.down\n");
6384 if (netdev
->type
!= LXC_NET_EMPTY
) {
6385 strprint(retv
, inlen
, "flags\n");
6386 strprint(retv
, inlen
, "link\n");
6387 strprint(retv
, inlen
, "name\n");
6388 strprint(retv
, inlen
, "hwaddr\n");
6389 strprint(retv
, inlen
, "mtu\n");
6390 strprint(retv
, inlen
, "ipv6.address\n");
6391 strprint(retv
, inlen
, "ipv6.gateway\n");
6392 strprint(retv
, inlen
, "ipv4.address\n");
6393 strprint(retv
, inlen
, "ipv4.gateway\n");
6396 switch (netdev
->type
) {
6398 strprint(retv
, inlen
, "veth.pair\n");
6399 strprint(retv
, inlen
, "veth.ipv4.route\n");
6400 strprint(retv
, inlen
, "veth.ipv6.route\n");
6401 strprint(retv
, inlen
, "veth.vlan.id\n");
6403 case LXC_NET_MACVLAN
:
6404 strprint(retv
, inlen
, "macvlan.mode\n");
6406 case LXC_NET_IPVLAN
:
6407 strprint(retv
, inlen
, "ipvlan.mode\n");
6408 strprint(retv
, inlen
, "ipvlan.isolation\n");
6411 strprint(retv
, inlen
, "vlan.id\n");