]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile.c
Merge pull request #2125 from brauner/2018-02-02/add_namespace_configs
[mirror_lxc.git] / src / lxc / confile.c
1 /*
2 * lxc: linux Container library
3 * (C) Copyright IBM Corp. 2007, 2008
4 *
5 * Authors:
6 * Daniel Lezcano <daniel.lezcano at free.fr>
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #define _GNU_SOURCE
26 #define __STDC_FORMAT_MACROS
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <syslog.h>
37 #include <time.h>
38 #include <unistd.h>
39 #include <arpa/inet.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <sys/param.h>
43 #include <sys/stat.h>
44 #include <sys/types.h>
45 #include <sys/utsname.h>
46
47 #include "conf.h"
48 #include "config.h"
49 #include "confile.h"
50 #include "confile_legacy.h"
51 #include "confile_utils.h"
52 #include "log.h"
53 #include "lxcseccomp.h"
54 #include "network.h"
55 #include "parse.h"
56 #include "storage.h"
57 #include "utils.h"
58
59 #if HAVE_IFADDRS_H
60 #include <ifaddrs.h>
61 #else
62 #include <../include/ifaddrs.h>
63 #endif
64
65 #if HAVE_SYS_PERSONALITY_H
66 #include <sys/personality.h>
67 #endif
68
69 lxc_log_define(lxc_confile, lxc);
70
71 #define lxc_config_define(name) \
72 static int set_config_##name(const char *, const char *, \
73 struct lxc_conf *, void *); \
74 static int get_config_##name(const char *, char *, int, \
75 struct lxc_conf *, void *); \
76 static int clr_config_##name(const char *, struct lxc_conf *, void *);
77
78 lxc_config_define(autodev);
79 lxc_config_define(apparmor_allow_incomplete);
80 lxc_config_define(apparmor_profile);
81 lxc_config_define(cap_drop);
82 lxc_config_define(cap_keep);
83 lxc_config_define(cgroup_controller);
84 lxc_config_define(cgroup2_controller);
85 lxc_config_define(cgroup_dir);
86 lxc_config_define(console_logfile);
87 lxc_config_define(console_rotate);
88 lxc_config_define(console_buffer_logfile);
89 lxc_config_define(console_buffer_size);
90 lxc_config_define(console_path);
91 lxc_config_define(environment);
92 lxc_config_define(ephemeral);
93 lxc_config_define(execute_cmd);
94 lxc_config_define(group);
95 lxc_config_define(hooks);
96 lxc_config_define(hooks_version);
97 lxc_config_define(idmaps);
98 lxc_config_define(includefiles);
99 lxc_config_define(init_cmd);
100 lxc_config_define(init_cwd);
101 lxc_config_define(init_gid);
102 lxc_config_define(init_uid);
103 lxc_config_define(log_file);
104 lxc_config_define(log_level);
105 lxc_config_define(log_syslog);
106 lxc_config_define(monitor);
107 lxc_config_define(mount);
108 lxc_config_define(mount_auto);
109 lxc_config_define(mount_fstab);
110 lxc_config_define(namespace_clone);
111 lxc_config_define(namespace_keep);
112 lxc_config_define(namespace_share);
113 lxc_config_define(net);
114 lxc_config_define(net_flags);
115 lxc_config_define(net_hwaddr);
116 lxc_config_define(net_ipv4_address);
117 lxc_config_define(net_ipv4_gateway);
118 lxc_config_define(net_ipv6_address);
119 lxc_config_define(net_ipv6_gateway);
120 lxc_config_define(net_link);
121 lxc_config_define(net_macvlan_mode);
122 lxc_config_define(net_mtu);
123 lxc_config_define(net_name);
124 lxc_config_define(net_nic);
125 lxc_config_define(net_script_down);
126 lxc_config_define(net_script_up);
127 lxc_config_define(net_type);
128 lxc_config_define(net_veth_pair);
129 lxc_config_define(net_vlan_id);
130 lxc_config_define(no_new_privs);
131 lxc_config_define(noop);
132 lxc_config_define(personality);
133 lxc_config_define(prlimit);
134 lxc_config_define(pty_max);
135 lxc_config_define(rootfs_backend);
136 lxc_config_define(rootfs_mount);
137 lxc_config_define(rootfs_options);
138 lxc_config_define(rootfs_path);
139 lxc_config_define(seccomp_profile);
140 lxc_config_define(selinux_context);
141 lxc_config_define(signal_halt);
142 lxc_config_define(signal_reboot);
143 lxc_config_define(signal_stop);
144 lxc_config_define(start);
145 lxc_config_define(tty_max);
146 lxc_config_define(tty_dir);
147 lxc_config_define(uts_name);
148 lxc_config_define(sysctl);
149 lxc_config_define(proc);
150
151 static struct lxc_config_t config[] = {
152 /* REMOVE in LXC 3.0 */
153 { "lxc.arch", false, set_config_personality, get_config_personality, clr_config_personality, },
154 { "lxc.apparmor.profile", false, set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, },
155 { "lxc.apparmor.allow_incomplete", false, set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, },
156 { "lxc.autodev", false, set_config_autodev, get_config_autodev, clr_config_autodev, },
157 { "lxc.cap.drop", false, set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, },
158 { "lxc.cap.keep", false, set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, },
159 { "lxc.cgroup2", false, set_config_cgroup2_controller, get_config_cgroup2_controller, clr_config_cgroup2_controller, },
160 { "lxc.cgroup.dir", false, set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, },
161 { "lxc.cgroup", false, set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, },
162 { "lxc.console.buffer.logfile", false, set_config_console_buffer_logfile, get_config_console_buffer_logfile, clr_config_console_buffer_logfile, },
163 { "lxc.console.buffer.size", false, set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
164 { "lxc.console.logfile", false, set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
165 { "lxc.console.path", false, set_config_console_path, get_config_console_path, clr_config_console_path, },
166 { "lxc.console.rotate", false, set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, },
167 { "lxc.environment", false, set_config_environment, get_config_environment, clr_config_environment, },
168 { "lxc.ephemeral", false, set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, },
169 { "lxc.execute.cmd", false, set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, },
170 { "lxc.group", false, set_config_group, get_config_group, clr_config_group, },
171 { "lxc.hook.autodev", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
172 { "lxc.hook.clone", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
173 { "lxc.hook.destroy", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
174 { "lxc.hook.mount", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
175 { "lxc.hook.post-stop", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
176 { "lxc.hook.pre-mount", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
177 { "lxc.hook.pre-start", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
178 { "lxc.hook.start", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
179 { "lxc.hook.start-host", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
180 { "lxc.hook.stop", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
181 { "lxc.hook.version", false, set_config_hooks_version, get_config_hooks_version, clr_config_hooks_version, },
182 { "lxc.hook", false, set_config_hooks, get_config_hooks, clr_config_hooks, },
183 { "lxc.idmap", false, set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
184 { "lxc.include", false, set_config_includefiles, get_config_includefiles, clr_config_includefiles, },
185 { "lxc.init.cmd", false, set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
186 { "lxc.init.gid", false, set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
187 { "lxc.init.uid", false, set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
188 { "lxc.init.cwd", false, set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, },
189 { "lxc.log.file", false, set_config_log_file, get_config_log_file, clr_config_log_file, },
190 { "lxc.log.level", false, set_config_log_level, get_config_log_level, clr_config_log_level, },
191 { "lxc.log.syslog", false, set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
192 { "lxc.monitor.unshare", false, set_config_monitor, get_config_monitor, clr_config_monitor, },
193 { "lxc.mount.auto", false, set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
194 { "lxc.mount.entry", false, set_config_mount, get_config_mount, clr_config_mount, },
195 { "lxc.mount.fstab", false, set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
196 { "lxc.namespace.clone", false, set_config_namespace_clone, get_config_namespace_clone, clr_config_namespace_clone, },
197 { "lxc.namespace.keep", false, set_config_namespace_keep, get_config_namespace_keep, clr_config_namespace_keep, },
198 { "lxc.namespace.share", false, set_config_namespace_share, get_config_namespace_share, clr_config_namespace_share, },
199
200 /* [START]: REMOVE IN LXC 3.0 */
201 { "lxc.network.type", true, set_config_network_legacy_type, get_config_network_legacy_item, clr_config_network_legacy_item, },
202 { "lxc.network.flags", true, set_config_network_legacy_flags, get_config_network_legacy_item, clr_config_network_legacy_item, },
203 { "lxc.network.link", true, set_config_network_legacy_link, get_config_network_legacy_item, clr_config_network_legacy_item, },
204 { "lxc.network.name", true, set_config_network_legacy_name, get_config_network_legacy_item, clr_config_network_legacy_item, },
205 { "lxc.network.macvlan.mode", true, set_config_network_legacy_macvlan_mode, get_config_network_legacy_item, clr_config_network_legacy_item, },
206 { "lxc.network.veth.pair", true, set_config_network_legacy_veth_pair, get_config_network_legacy_item, clr_config_network_legacy_item, },
207 { "lxc.network.script.up", true, set_config_network_legacy_script_up, get_config_network_legacy_item, clr_config_network_legacy_item, },
208 { "lxc.network.script.down", true, set_config_network_legacy_script_down, get_config_network_legacy_item, clr_config_network_legacy_item, },
209 { "lxc.network.hwaddr", true, set_config_network_legacy_hwaddr, get_config_network_legacy_item, clr_config_network_legacy_item, },
210 { "lxc.network.mtu", true, set_config_network_legacy_mtu, get_config_network_legacy_item, clr_config_network_legacy_item, },
211 { "lxc.network.vlan.id", true, set_config_network_legacy_vlan_id, get_config_network_legacy_item, clr_config_network_legacy_item, },
212 { "lxc.network.ipv4.gateway", true, set_config_network_legacy_ipv4_gateway, get_config_network_legacy_item, clr_config_network_legacy_item, },
213 { "lxc.network.ipv4", true, set_config_network_legacy_ipv4, get_config_network_legacy_item, clr_config_network_legacy_item, },
214 { "lxc.network.ipv6.gateway", true, set_config_network_legacy_ipv6_gateway, get_config_network_legacy_item, clr_config_network_legacy_item, },
215 { "lxc.network.ipv6", true, set_config_network_legacy_ipv6, get_config_network_legacy_item, clr_config_network_legacy_item, },
216 { "lxc.network.", true, set_config_network_legacy_nic, get_config_network_legacy_item, clr_config_network_legacy_item, },
217 { "lxc.network", true, set_config_network_legacy, get_config_network_legacy, clr_config_network_legacy, },
218 /* [END]: REMOVE IN LXC 3.0 */
219
220 { "lxc.net.flags", false, set_config_net_flags, get_config_net_flags, clr_config_net_flags, },
221 { "lxc.net.hwaddr", false, set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, },
222 { "lxc.net.ipv4.address", false, set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, },
223 { "lxc.net.ipv4.gateway", false, set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, },
224 { "lxc.net.ipv6.address", false, set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, },
225 { "lxc.net.ipv6.gateway", false, set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, },
226 { "lxc.net.link", false, set_config_net_link, get_config_net_link, clr_config_net_link, },
227 { "lxc.net.macvlan.mode", false, set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, },
228 { "lxc.net.mtu", false, set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, },
229 { "lxc.net.name", false, set_config_net_name, get_config_net_name, clr_config_net_name, },
230 { "lxc.net.script.down", false, set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, },
231 { "lxc.net.script.up", false, set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, },
232 { "lxc.net.type", false, set_config_net_type, get_config_net_type, clr_config_net_type, },
233 { "lxc.net.vlan.id", false, set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, },
234 { "lxc.net.veth.pair", false, set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, },
235 { "lxc.net.", false, set_config_net_nic, get_config_net_nic, clr_config_net_nic, },
236 { "lxc.net", false, set_config_net, get_config_net, clr_config_net, },
237 { "lxc.no_new_privs", false, set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, },
238 { "lxc.prlimit", false, set_config_prlimit, get_config_prlimit, clr_config_prlimit, },
239 { "lxc.pty.max", false, set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
240 { "lxc.rootfs.mount", false, set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, },
241 { "lxc.rootfs.options", false, set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, },
242 { "lxc.rootfs.path", false, set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
243 { "lxc.seccomp.profile", false, set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
244 { "lxc.selinux.context", false, set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, },
245 { "lxc.signal.halt", false, set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
246 { "lxc.signal.reboot", false, set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
247 { "lxc.signal.stop", false, set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
248 { "lxc.start.auto", false, set_config_start, get_config_start, clr_config_start, },
249 { "lxc.start.delay", false, set_config_start, get_config_start, clr_config_start, },
250 { "lxc.start.order", false, set_config_start, get_config_start, clr_config_start, },
251 { "lxc.tty.dir", false, set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
252 { "lxc.tty.max", false, set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
253 { "lxc.uts.name", false, set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
254 { "lxc.sysctl", false, set_config_sysctl, get_config_sysctl, clr_config_sysctl, },
255 { "lxc.proc", false, set_config_proc, get_config_proc, clr_config_proc, },
256
257 /* [START]: REMOVE IN LXC 3.0 */
258 { "lxc.pts", true, set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
259 { "lxc.devttydir", true, set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
260 { "lxc.tty", true, set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
261 { "lxc.aa_profile", true, set_config_lsm_aa_profile, get_config_lsm_aa_profile, clr_config_lsm_aa_profile, },
262 { "lxc.aa_allow_incomplete", true, set_config_lsm_aa_incomplete, get_config_lsm_aa_incomplete, clr_config_lsm_aa_incomplete, },
263 { "lxc.se_context", true, set_config_lsm_se_context, get_config_lsm_se_context, clr_config_lsm_se_context, },
264 { "lxc.id_map", true, set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
265 { "lxc.mount", true, set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
266 { "lxc.rootfs.backend", true, set_config_rootfs_backend, get_config_rootfs_backend, clr_config_rootfs_backend, },
267 { "lxc.rootfs", true, set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
268 { "lxc.utsname", true, set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
269 { "lxc.seccomp", true, set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
270 { "lxc.console", true, set_config_console_path, get_config_console_path, clr_config_console_path, },
271 { "lxc.haltsignal", true, set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
272 { "lxc.rebootsignal", true, set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
273 { "lxc.stopsignal", true, set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
274 { "lxc.syslog", true, set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
275 { "lxc.kmsg", true, set_config_noop, get_config_noop, clr_config_noop, },
276 { "lxc.loglevel", true, set_config_log_level, get_config_log_level, clr_config_log_level, },
277 { "lxc.logfile", true, set_config_log_file, get_config_log_file, clr_config_log_file, },
278 { "lxc.init_cmd", true, set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
279 { "lxc.init_uid", true, set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
280 { "lxc.init_gid", true, set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
281 { "lxc.limit", true, set_config_limit, get_config_limit, clr_config_limit, },
282 { "lxc.pivotdir", true, set_config_noop, get_config_noop, clr_config_noop, },
283 /* [END]: REMOVE IN LXC 3.0 */
284 };
285
286 struct signame {
287 int num;
288 const char *name;
289 };
290
291 static const struct signame signames[] = {
292 { SIGHUP, "HUP" },
293 { SIGINT, "INT" },
294 { SIGQUIT, "QUIT" },
295 { SIGILL, "ILL" },
296 { SIGABRT, "ABRT" },
297 { SIGFPE, "FPE" },
298 { SIGKILL, "KILL" },
299 { SIGSEGV, "SEGV" },
300 { SIGPIPE, "PIPE" },
301 { SIGALRM, "ALRM" },
302 { SIGTERM, "TERM" },
303 { SIGUSR1, "USR1" },
304 { SIGUSR2, "USR2" },
305 { SIGCHLD, "CHLD" },
306 { SIGCONT, "CONT" },
307 { SIGSTOP, "STOP" },
308 { SIGTSTP, "TSTP" },
309 { SIGTTIN, "TTIN" },
310 { SIGTTOU, "TTOU" },
311 #ifdef SIGTRAP
312 { SIGTRAP, "TRAP" },
313 #endif
314 #ifdef SIGIOT
315 { SIGIOT, "IOT" },
316 #endif
317 #ifdef SIGEMT
318 { SIGEMT, "EMT" },
319 #endif
320 #ifdef SIGBUS
321 { SIGBUS, "BUS" },
322 #endif
323 #ifdef SIGSTKFLT
324 { SIGSTKFLT, "STKFLT" },
325 #endif
326 #ifdef SIGCLD
327 { SIGCLD, "CLD" },
328 #endif
329 #ifdef SIGURG
330 { SIGURG, "URG" },
331 #endif
332 #ifdef SIGXCPU
333 { SIGXCPU, "XCPU" },
334 #endif
335 #ifdef SIGXFSZ
336 { SIGXFSZ, "XFSZ" },
337 #endif
338 #ifdef SIGVTALRM
339 { SIGVTALRM, "VTALRM" },
340 #endif
341 #ifdef SIGPROF
342 { SIGPROF, "PROF" },
343 #endif
344 #ifdef SIGWINCH
345 { SIGWINCH, "WINCH" },
346 #endif
347 #ifdef SIGIO
348 { SIGIO, "IO" },
349 #endif
350 #ifdef SIGPOLL
351 { SIGPOLL, "POLL" },
352 #endif
353 #ifdef SIGINFO
354 { SIGINFO, "INFO" },
355 #endif
356 #ifdef SIGLOST
357 { SIGLOST, "LOST" },
358 #endif
359 #ifdef SIGPWR
360 { SIGPWR, "PWR" },
361 #endif
362 #ifdef SIGUNUSED
363 { SIGUNUSED, "UNUSED" },
364 #endif
365 #ifdef SIGSYS
366 { SIGSYS, "SYS" },
367 #endif
368 };
369
370 static const size_t config_size = sizeof(config) / sizeof(struct lxc_config_t);
371
372 struct lxc_config_t *lxc_get_config(const char *key)
373 {
374 size_t i;
375
376 for (i = 0; i < config_size; i++)
377 if (!strncmp(config[i].name, key, strlen(config[i].name)))
378 return &config[i];
379
380 return NULL;
381 }
382
383 static int set_config_net(const char *key, const char *value,
384 struct lxc_conf *lxc_conf, void *data)
385 {
386 if (!lxc_config_value_empty(value)) {
387 ERROR("lxc.net must not have a value");
388 return -1;
389 }
390
391 return clr_config_net(key, lxc_conf, data);
392 }
393
394 static int set_config_net_type(const char *key, const char *value,
395 struct lxc_conf *lxc_conf, void *data)
396 {
397 struct lxc_netdev *netdev = data;
398
399 if (lxc_config_value_empty(value))
400 return clr_config_net_type(key, lxc_conf, data);
401
402 if (!netdev)
403 return -1;
404
405 if (!strcmp(value, "veth")) {
406 netdev->type = LXC_NET_VETH;
407 } else if (!strcmp(value, "macvlan")) {
408 netdev->type = LXC_NET_MACVLAN;
409 lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode,
410 "private");
411 } else if (!strcmp(value, "vlan")) {
412 netdev->type = LXC_NET_VLAN;
413 } else if (!strcmp(value, "phys")) {
414 netdev->type = LXC_NET_PHYS;
415 } else if (!strcmp(value, "empty")) {
416 netdev->type = LXC_NET_EMPTY;
417 } else if (!strcmp(value, "none")) {
418 netdev->type = LXC_NET_NONE;
419 } else {
420 ERROR("invalid network type %s", value);
421 return -1;
422 }
423
424 return 0;
425 }
426
427 static int set_config_net_flags(const char *key, const char *value,
428 struct lxc_conf *lxc_conf, void *data)
429 {
430 struct lxc_netdev *netdev = data;
431
432 if (lxc_config_value_empty(value))
433 return clr_config_net_flags(key, lxc_conf, data);
434
435 if (!netdev)
436 return -1;
437
438 netdev->flags |= IFF_UP;
439
440 return 0;
441 }
442
443 static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
444 struct lxc_netdev *netdev)
445 {
446 struct ifaddrs *ifaddr, *ifa;
447 int n;
448 int ret = 0;
449 const char *type_key = "lxc.net.type";
450 const char *link_key = "lxc.net.link";
451 const char *tmpvalue = "phys";
452
453 if (getifaddrs(&ifaddr) == -1) {
454 SYSERROR("Get network interfaces failed");
455 return -1;
456 }
457
458 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
459 if (!ifa->ifa_addr)
460 continue;
461 if (ifa->ifa_addr->sa_family != AF_PACKET)
462 continue;
463
464 if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) {
465 ret = set_config_net_type(type_key, tmpvalue, lxc_conf,
466 netdev);
467 if (!ret) {
468 ret = set_config_net_link(
469 link_key, ifa->ifa_name, lxc_conf, netdev);
470 if (ret) {
471 ERROR("failed to create matched ifnames");
472 break;
473 }
474 } else {
475 ERROR("failed to create matched ifnames");
476 break;
477 }
478 }
479 }
480
481 freeifaddrs(ifaddr);
482 ifaddr = NULL;
483
484 return ret;
485 }
486
487 static int set_config_net_link(const char *key, const char *value,
488 struct lxc_conf *lxc_conf, void *data)
489 {
490 struct lxc_netdev *netdev = data;
491 int ret = 0;
492
493 if (lxc_config_value_empty(value))
494 return clr_config_net_link(key, lxc_conf, data);
495
496 if (!netdev)
497 return -1;
498
499 if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
500 ret = create_matched_ifnames(value, lxc_conf, netdev);
501 else
502 ret = network_ifname(netdev->link, value);
503
504 return ret;
505 }
506
507 static int set_config_net_name(const char *key, const char *value,
508 struct lxc_conf *lxc_conf, void *data)
509 {
510 struct lxc_netdev *netdev = data;
511
512 if (lxc_config_value_empty(value))
513 return clr_config_net_name(key, lxc_conf, data);
514
515 if (!netdev)
516 return -1;
517
518 return network_ifname(netdev->name, value);
519 }
520
521 static int set_config_net_veth_pair(const char *key, const char *value,
522 struct lxc_conf *lxc_conf, void *data)
523 {
524 struct lxc_netdev *netdev = data;
525
526 if (lxc_config_value_empty(value))
527 return clr_config_net_veth_pair(key, lxc_conf, data);
528
529 if (!netdev)
530 return -1;
531
532 return network_ifname(netdev->priv.veth_attr.pair, value);
533 }
534
535 static int set_config_net_macvlan_mode(const char *key, const char *value,
536 struct lxc_conf *lxc_conf, void *data)
537 {
538 struct lxc_netdev *netdev = data;
539
540 if (lxc_config_value_empty(value))
541 return clr_config_net_macvlan_mode(key, lxc_conf, data);
542
543 if (!netdev)
544 return -1;
545
546 return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
547 }
548
549 static int set_config_net_hwaddr(const char *key, const char *value,
550 struct lxc_conf *lxc_conf, void *data)
551 {
552 struct lxc_netdev *netdev = data;
553 char *new_value;
554
555 if (lxc_config_value_empty(value))
556 return clr_config_net_hwaddr(key, lxc_conf, data);
557
558 if (!netdev)
559 return -1;
560
561 new_value = strdup(value);
562 if (!new_value)
563 return -1;
564
565 rand_complete_hwaddr(new_value);
566
567 if (lxc_config_value_empty(new_value)) {
568 free(new_value);
569 netdev->hwaddr = NULL;
570 return 0;
571 }
572
573 netdev->hwaddr = new_value;
574 return 0;
575 }
576
577 static int set_config_net_vlan_id(const char *key, const char *value,
578 struct lxc_conf *lxc_conf, void *data)
579 {
580 int ret;
581 struct lxc_netdev *netdev = data;
582
583 if (lxc_config_value_empty(value))
584 return clr_config_net_vlan_id(key, lxc_conf, data);
585
586 if (!netdev)
587 return -1;
588
589 ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
590 if (ret < 0)
591 return -1;
592
593 return 0;
594 }
595
596 static int set_config_net_mtu(const char *key, const char *value,
597 struct lxc_conf *lxc_conf, void *data)
598 {
599 struct lxc_netdev *netdev = data;
600
601 if (lxc_config_value_empty(value))
602 return clr_config_net_mtu(key, lxc_conf, data);
603
604 if (!netdev)
605 return -1;
606
607 return set_config_string_item(&netdev->mtu, value);
608 }
609
610 static int set_config_net_ipv4_address(const char *key, const char *value,
611 struct lxc_conf *lxc_conf, void *data)
612 {
613 int ret;
614 struct lxc_netdev *netdev = data;
615 struct lxc_inetdev *inetdev;
616 struct lxc_list *list;
617 char *cursor, *slash;
618 char *addr = NULL, *bcast = NULL, *prefix = NULL;
619
620 if (lxc_config_value_empty(value))
621 return clr_config_net_ipv4_address(key, lxc_conf, data);
622
623 if (!netdev)
624 return -1;
625
626 inetdev = malloc(sizeof(*inetdev));
627 if (!inetdev)
628 return -1;
629
630 memset(inetdev, 0, sizeof(*inetdev));
631
632 list = malloc(sizeof(*list));
633 if (!list) {
634 free(inetdev);
635 return -1;
636 }
637
638 lxc_list_init(list);
639 list->elem = inetdev;
640
641 addr = strdup(value);
642 if (!addr) {
643 free(inetdev);
644 free(list);
645 return -1;
646 }
647
648 cursor = strstr(addr, " ");
649 if (cursor) {
650 *cursor = '\0';
651 bcast = cursor + 1;
652 }
653
654 slash = strstr(addr, "/");
655 if (slash) {
656 *slash = '\0';
657 prefix = slash + 1;
658 }
659
660 ret = inet_pton(AF_INET, addr, &inetdev->addr);
661 if (!ret || ret < 0) {
662 SYSERROR("Invalid ipv4 address \"%s\"", value);
663 free(inetdev);
664 free(addr);
665 free(list);
666 return -1;
667 }
668
669 if (bcast) {
670 ret = inet_pton(AF_INET, bcast, &inetdev->bcast);
671 if (!ret || ret < 0) {
672 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value);
673 free(inetdev);
674 free(list);
675 free(addr);
676 return -1;
677 }
678
679 }
680
681 /* No prefix specified, determine it from the network class. */
682 if (prefix) {
683 ret = lxc_safe_uint(prefix, &inetdev->prefix);
684 if (ret < 0)
685 return -1;
686 } else {
687 inetdev->prefix = config_ip_prefix(&inetdev->addr);
688 }
689
690 /* If no broadcast address, let compute one from the
691 * prefix and address.
692 */
693 if (!bcast) {
694 inetdev->bcast.s_addr = inetdev->addr.s_addr;
695 inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix);
696 }
697
698 lxc_list_add_tail(&netdev->ipv4, list);
699
700 free(addr);
701 return 0;
702 }
703
704 static int set_config_net_ipv4_gateway(const char *key, const char *value,
705 struct lxc_conf *lxc_conf, void *data)
706 {
707 struct lxc_netdev *netdev = data;
708
709 if (lxc_config_value_empty(value))
710 return clr_config_net_ipv4_gateway(key, lxc_conf, data);
711
712 if (!netdev)
713 return -1;
714
715 free(netdev->ipv4_gateway);
716
717 if (!strcmp(value, "auto")) {
718 netdev->ipv4_gateway = NULL;
719 netdev->ipv4_gateway_auto = true;
720 } else {
721 int ret;
722 struct in_addr *gw;
723
724 gw = malloc(sizeof(*gw));
725 if (!gw)
726 return -1;
727
728 ret = inet_pton(AF_INET, value, gw);
729 if (!ret || ret < 0) {
730 SYSERROR("Invalid ipv4 gateway address \"%s\"", value);
731 free(gw);
732 return -1;
733 }
734
735 netdev->ipv4_gateway = gw;
736 netdev->ipv4_gateway_auto = false;
737 }
738
739 return 0;
740 }
741
742 static int set_config_net_ipv6_address(const char *key, const char *value,
743 struct lxc_conf *lxc_conf, void *data)
744 {
745 int ret;
746 struct lxc_netdev *netdev = data;
747 struct lxc_inet6dev *inet6dev;
748 struct lxc_list *list;
749 char *slash, *valdup, *netmask;
750
751 if (lxc_config_value_empty(value))
752 return clr_config_net_ipv6_address(key, lxc_conf, data);
753
754 if (!netdev)
755 return -1;
756
757 inet6dev = malloc(sizeof(*inet6dev));
758 if (!inet6dev)
759 return -1;
760
761 memset(inet6dev, 0, sizeof(*inet6dev));
762
763 list = malloc(sizeof(*list));
764 if (!list) {
765 free(inet6dev);
766 return -1;
767 }
768
769 lxc_list_init(list);
770 list->elem = inet6dev;
771
772 valdup = strdup(value);
773 if (!valdup) {
774 free(list);
775 free(inet6dev);
776 return -1;
777 }
778
779 inet6dev->prefix = 64;
780 slash = strstr(valdup, "/");
781 if (slash) {
782 *slash = '\0';
783 netmask = slash + 1;
784 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
785 if (ret < 0) {
786 free(list);
787 free(inet6dev);
788 free(valdup);
789 return -1;
790 }
791 }
792
793 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
794 if (!ret || ret < 0) {
795 SYSERROR("Invalid ipv6 address \"%s\"", valdup);
796 free(list);
797 free(inet6dev);
798 free(valdup);
799 return -1;
800 }
801
802 lxc_list_add_tail(&netdev->ipv6, list);
803
804 free(valdup);
805 return 0;
806 }
807
808 static int set_config_net_ipv6_gateway(const char *key, const char *value,
809 struct lxc_conf *lxc_conf, void *data)
810 {
811 struct lxc_netdev *netdev = data;
812
813 if (lxc_config_value_empty(value))
814 return clr_config_net_ipv6_gateway(key, lxc_conf, data);
815
816 if (!netdev)
817 return -1;
818
819 free(netdev->ipv6_gateway);
820
821 if (!strcmp(value, "auto")) {
822 netdev->ipv6_gateway = NULL;
823 netdev->ipv6_gateway_auto = true;
824 } else {
825 int ret;
826 struct in6_addr *gw;
827
828 gw = malloc(sizeof(*gw));
829 if (!gw)
830 return -1;
831
832 ret = inet_pton(AF_INET6, value, gw);
833 if (!ret || ret < 0) {
834 SYSERROR("Invalid ipv6 gateway address \"%s\"", value);
835 free(gw);
836 return -1;
837 }
838
839 netdev->ipv6_gateway = gw;
840 netdev->ipv6_gateway_auto = false;
841 }
842
843 return 0;
844 }
845
846 static int set_config_net_script_up(const char *key, const char *value,
847 struct lxc_conf *lxc_conf, void *data)
848 {
849 struct lxc_netdev *netdev = data;
850
851 if (lxc_config_value_empty(value))
852 return clr_config_net_script_up(key, lxc_conf, data);
853
854 if (!netdev)
855 return -1;
856
857 return set_config_string_item(&netdev->upscript, value);
858 }
859
860 static int set_config_net_script_down(const char *key, const char *value,
861 struct lxc_conf *lxc_conf, void *data)
862 {
863 struct lxc_netdev *netdev = data;
864
865 if (lxc_config_value_empty(value))
866 return clr_config_net_script_down(key, lxc_conf, data);
867
868 if (!netdev)
869 return -1;
870
871 return set_config_string_item(&netdev->downscript, value);
872 }
873
874 static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook)
875 {
876 struct lxc_list *hooklist;
877
878 hooklist = malloc(sizeof(*hooklist));
879 if (!hooklist) {
880 free(hook);
881 return -1;
882 }
883
884 hooklist->elem = hook;
885 lxc_list_add_tail(&lxc_conf->hooks[which], hooklist);
886 return 0;
887 }
888
889 static int set_config_seccomp_profile(const char *key, const char *value,
890 struct lxc_conf *lxc_conf, void *data)
891 {
892 return set_config_path_item(&lxc_conf->seccomp, value);
893 }
894
895 static int set_config_execute_cmd(const char *key, const char *value,
896 struct lxc_conf *lxc_conf, void *data)
897 {
898 return set_config_path_item(&lxc_conf->execute_cmd, value);
899 }
900
901 static int set_config_init_cmd(const char *key, const char *value,
902 struct lxc_conf *lxc_conf, void *data)
903 {
904 return set_config_path_item(&lxc_conf->init_cmd, value);
905 }
906
907 static int set_config_init_cwd(const char *key, const char *value,
908 struct lxc_conf *lxc_conf, void *data)
909 {
910 return set_config_path_item(&lxc_conf->init_cwd, value);
911 }
912
913 static int set_config_init_uid(const char *key, const char *value,
914 struct lxc_conf *lxc_conf, void *data)
915 {
916 unsigned int init_uid;
917
918 if (lxc_config_value_empty(value)) {
919 lxc_conf->init_uid = 0;
920 return 0;
921 }
922
923 if (lxc_safe_uint(value, &init_uid) < 0)
924 return -1;
925
926 lxc_conf->init_uid = init_uid;
927
928 return 0;
929 }
930
931 static int set_config_init_gid(const char *key, const char *value,
932 struct lxc_conf *lxc_conf, void *data)
933 {
934 unsigned int init_gid;
935
936 if (lxc_config_value_empty(value)) {
937 lxc_conf->init_gid = 0;
938 return 0;
939 }
940
941 if (lxc_safe_uint(value, &init_gid) < 0)
942 return -1;
943
944 lxc_conf->init_gid = init_gid;
945
946 return 0;
947 }
948
949 static int set_config_hooks(const char *key, const char *value,
950 struct lxc_conf *lxc_conf, void *data)
951 {
952 char *copy;
953
954 if (lxc_config_value_empty(value))
955 return lxc_clear_hooks(lxc_conf, key);
956
957 if (strcmp(key + 4, "hook") == 0) {
958 ERROR("lxc.hook must not have a value");
959 return -1;
960 }
961
962 copy = strdup(value);
963 if (!copy)
964 return -1;
965
966 if (strcmp(key + 9, "pre-start") == 0)
967 return add_hook(lxc_conf, LXCHOOK_PRESTART, copy);
968 else if (strcmp(key + 9, "start-host") == 0)
969 return add_hook(lxc_conf, LXCHOOK_START_HOST, copy);
970 else if (strcmp(key + 9, "pre-mount") == 0)
971 return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy);
972 else if (strcmp(key + 9, "autodev") == 0)
973 return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy);
974 else if (strcmp(key + 9, "mount") == 0)
975 return add_hook(lxc_conf, LXCHOOK_MOUNT, copy);
976 else if (strcmp(key + 9, "start") == 0)
977 return add_hook(lxc_conf, LXCHOOK_START, copy);
978 else if (strcmp(key + 9, "stop") == 0)
979 return add_hook(lxc_conf, LXCHOOK_STOP, copy);
980 else if (strcmp(key + 9, "post-stop") == 0)
981 return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy);
982 else if (strcmp(key + 9, "clone") == 0)
983 return add_hook(lxc_conf, LXCHOOK_CLONE, copy);
984 else if (strcmp(key + 9, "destroy") == 0)
985 return add_hook(lxc_conf, LXCHOOK_DESTROY, copy);
986
987 free(copy);
988 return -1;
989 }
990
991 static int set_config_hooks_version(const char *key, const char *value,
992 struct lxc_conf *lxc_conf, void *data)
993 {
994 int ret;
995 unsigned int tmp;
996
997 if (lxc_config_value_empty(value))
998 return clr_config_hooks_version(key, lxc_conf, NULL);
999
1000 ret = lxc_safe_uint(value, &tmp);
1001 if (ret < 0)
1002 return -1;
1003
1004 if (tmp > 1) {
1005 ERROR("Invalid hook version specified. Currently only 0 "
1006 "(legacy) and 1 are supported");
1007 return -1;
1008 }
1009
1010 lxc_conf->hooks_version = tmp;
1011 return 0;
1012 }
1013
1014 static int set_config_personality(const char *key, const char *value,
1015 struct lxc_conf *lxc_conf, void *data)
1016 {
1017 signed long personality = lxc_config_parse_arch(value);
1018
1019 if (personality >= 0)
1020 lxc_conf->personality = personality;
1021 else
1022 WARN("Unsupported personality \"%s\"", value);
1023
1024 return 0;
1025 }
1026
1027 static int set_config_pty_max(const char *key, const char *value,
1028 struct lxc_conf *lxc_conf, void *data)
1029 {
1030 if (lxc_config_value_empty(value)) {
1031 lxc_conf->pts = 0;
1032 return 0;
1033 }
1034
1035 if (lxc_safe_uint(value, &lxc_conf->pts) < 0)
1036 return -1;
1037
1038 return 0;
1039 }
1040
1041 /* We only need to check whether the first byte of the key after the lxc.start.
1042 * prefix matches our expectations since they fortunately all start with a
1043 * different letter. If anything was wrong with the key we would have already
1044 * noticed when the callback was called.
1045 */
1046 static int set_config_start(const char *key, const char *value,
1047 struct lxc_conf *lxc_conf, void *data)
1048 {
1049 bool is_empty;
1050
1051 is_empty = lxc_config_value_empty(value);
1052
1053 if (*(key + 10) == 'a') { /* lxc.start.auto */
1054 if (is_empty) {
1055 lxc_conf->start_auto = 0;
1056 return 0;
1057 }
1058
1059 if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0)
1060 return -1;
1061
1062 if (lxc_conf->start_auto > 1)
1063 return -1;
1064
1065 return 0;
1066 } else if (*(key + 10) == 'd') { /* lxc.start.delay */
1067 if (is_empty) {
1068 lxc_conf->start_delay = 0;
1069 return 0;
1070 }
1071
1072 return lxc_safe_uint(value, &lxc_conf->start_delay);
1073 } else if (*(key + 10) == 'o') { /* lxc.start.order */
1074 if (is_empty) {
1075 lxc_conf->start_order = 0;
1076 return 0;
1077 }
1078
1079 return lxc_safe_int(value, &lxc_conf->start_order);
1080 }
1081
1082 return -1;
1083 }
1084
1085 static int set_config_monitor(const char *key, const char *value,
1086 struct lxc_conf *lxc_conf, void *data)
1087 {
1088 if (lxc_config_value_empty(value)) {
1089 lxc_conf->monitor_unshare = 0;
1090 return 0;
1091 }
1092
1093 if (strcmp(key + 12, "unshare") == 0)
1094 return lxc_safe_uint(value, &lxc_conf->monitor_unshare);
1095
1096 return -1;
1097 }
1098
1099 static int set_config_group(const char *key, const char *value,
1100 struct lxc_conf *lxc_conf, void *data)
1101 {
1102 char *groups, *groupptr, *sptr, *token;
1103 struct lxc_list *grouplist;
1104 int ret = -1;
1105
1106 if (lxc_config_value_empty(value))
1107 return lxc_clear_groups(lxc_conf);
1108
1109 groups = strdup(value);
1110 if (!groups)
1111 return -1;
1112
1113 /* In case several groups are specified in a single line split these
1114 * groups in a single element for the list.
1115 */
1116 for (groupptr = groups;; groupptr = NULL) {
1117 token = strtok_r(groupptr, " \t", &sptr);
1118 if (!token) {
1119 ret = 0;
1120 break;
1121 }
1122
1123 grouplist = malloc(sizeof(*grouplist));
1124 if (!grouplist)
1125 break;
1126
1127 grouplist->elem = strdup(token);
1128 if (!grouplist->elem) {
1129 free(grouplist);
1130 break;
1131 }
1132
1133 lxc_list_add_tail(&lxc_conf->groups, grouplist);
1134 }
1135
1136 free(groups);
1137 return ret;
1138 }
1139
1140 static int set_config_environment(const char *key, const char *value,
1141 struct lxc_conf *lxc_conf, void *data)
1142 {
1143 struct lxc_list *list_item = NULL;
1144
1145 if (lxc_config_value_empty(value))
1146 return lxc_clear_environment(lxc_conf);
1147
1148 list_item = malloc(sizeof(*list_item));
1149 if (!list_item)
1150 goto on_error;
1151
1152 list_item->elem = strdup(value);
1153
1154 if (!list_item->elem)
1155 goto on_error;
1156
1157 lxc_list_add_tail(&lxc_conf->environment, list_item);
1158
1159 return 0;
1160
1161 on_error:
1162 free(list_item);
1163 return -1;
1164 }
1165
1166 static int set_config_tty_max(const char *key, const char *value,
1167 struct lxc_conf *lxc_conf, void *data)
1168 {
1169 if (lxc_config_value_empty(value)) {
1170 lxc_conf->tty = 0;
1171 return 0;
1172 }
1173
1174 return lxc_safe_uint(value, &lxc_conf->tty);
1175 }
1176
1177 static int set_config_tty_dir(const char *key, const char *value,
1178 struct lxc_conf *lxc_conf, void *data)
1179 {
1180 return set_config_string_item_max(&lxc_conf->ttydir, value,
1181 NAME_MAX + 1);
1182 }
1183
1184 static int set_config_apparmor_profile(const char *key, const char *value,
1185 struct lxc_conf *lxc_conf, void *data)
1186 {
1187 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
1188 }
1189
1190 static int set_config_apparmor_allow_incomplete(const char *key,
1191 const char *value,
1192 struct lxc_conf *lxc_conf,
1193 void *data)
1194 {
1195 if (lxc_config_value_empty(value)) {
1196 lxc_conf->lsm_aa_allow_incomplete = 0;
1197 return 0;
1198 }
1199
1200 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1201 return -1;
1202
1203 if (lxc_conf->lsm_aa_allow_incomplete > 1)
1204 return -1;
1205
1206 return 0;
1207 }
1208
1209 static int set_config_selinux_context(const char *key, const char *value,
1210 struct lxc_conf *lxc_conf, void *data)
1211 {
1212 return set_config_string_item(&lxc_conf->lsm_se_context, value);
1213 }
1214
1215 static int set_config_log_file(const char *key, const char *value,
1216 struct lxc_conf *c, void *data)
1217 {
1218 int ret;
1219
1220 if (lxc_config_value_empty(value)) {
1221 free(c->logfile);
1222 c->logfile = NULL;
1223 return 0;
1224 }
1225
1226 /* Store these values in the lxc_conf, and then try to set for actual
1227 * current logging.
1228 */
1229 ret = set_config_path_item(&c->logfile, value);
1230 if (ret == 0)
1231 ret = lxc_log_set_file(&c->logfd, c->logfile);
1232
1233 return ret;
1234 }
1235
1236 static int set_config_log_level(const char *key, const char *value,
1237 struct lxc_conf *lxc_conf, void *data)
1238 {
1239 int newlevel;
1240
1241 if (lxc_config_value_empty(value)) {
1242 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
1243 return 0;
1244 }
1245
1246 if (value[0] >= '0' && value[0] <= '9') {
1247 if (lxc_safe_int(value, &newlevel) < 0)
1248 return -1;
1249 } else {
1250 newlevel = lxc_log_priority_to_int(value);
1251 }
1252
1253 /* Store these values in the lxc_conf, and then try to set for actual
1254 * current logging.
1255 */
1256 lxc_conf->loglevel = newlevel;
1257 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
1258 }
1259
1260 static int set_config_autodev(const char *key, const char *value,
1261 struct lxc_conf *lxc_conf, void *data)
1262 {
1263 if (lxc_config_value_empty(value)) {
1264 lxc_conf->autodev = 0;
1265 return 0;
1266 }
1267
1268 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1269 return -1;
1270
1271 if (lxc_conf->autodev > 1)
1272 return -1;
1273
1274 return 0;
1275 }
1276
1277 static int sig_num(const char *sig)
1278 {
1279 unsigned int signum;
1280
1281 if (lxc_safe_uint(sig, &signum) < 0)
1282 return -1;
1283
1284 return signum;
1285 }
1286
1287 static int rt_sig_num(const char *signame)
1288 {
1289 int rtmax = 0, sig_n = 0;
1290
1291 if (strncasecmp(signame, "max-", 4) == 0) {
1292 rtmax = 1;
1293 }
1294
1295 signame += 4;
1296 if (!isdigit(*signame))
1297 return -1;
1298
1299 sig_n = sig_num(signame);
1300 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1301 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1302 return -1;
1303
1304 return sig_n;
1305 }
1306
1307 static int sig_parse(const char *signame)
1308 {
1309 size_t n;
1310
1311 if (isdigit(*signame)) {
1312 return sig_num(signame);
1313 } else if (strncasecmp(signame, "sig", 3) == 0) {
1314 signame += 3;
1315 if (strncasecmp(signame, "rt", 2) == 0)
1316 return rt_sig_num(signame + 2);
1317 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) {
1318 if (strcasecmp(signames[n].name, signame) == 0)
1319 return signames[n].num;
1320 }
1321 }
1322
1323 return -1;
1324 }
1325
1326 static int set_config_signal_halt(const char *key, const char *value,
1327 struct lxc_conf *lxc_conf, void *data)
1328 {
1329 int sig_n;
1330
1331 if (lxc_config_value_empty(value)) {
1332 lxc_conf->haltsignal = 0;
1333 return 0;
1334 }
1335
1336 sig_n = sig_parse(value);
1337 if (sig_n < 0)
1338 return -1;
1339
1340 lxc_conf->haltsignal = sig_n;
1341
1342 return 0;
1343 }
1344
1345 static int set_config_signal_reboot(const char *key, const char *value,
1346 struct lxc_conf *lxc_conf, void *data)
1347 {
1348 int sig_n;
1349
1350 if (lxc_config_value_empty(value)) {
1351 lxc_conf->rebootsignal = 0;
1352 return 0;
1353 }
1354
1355 sig_n = sig_parse(value);
1356 if (sig_n < 0)
1357 return -1;
1358
1359 lxc_conf->rebootsignal = sig_n;
1360
1361 return 0;
1362 }
1363
1364 static int set_config_signal_stop(const char *key, const char *value,
1365 struct lxc_conf *lxc_conf, void *data)
1366 {
1367 int sig_n;
1368
1369 if (lxc_config_value_empty(value)) {
1370 lxc_conf->stopsignal = 0;
1371 return 0;
1372 }
1373
1374 sig_n = sig_parse(value);
1375 if (sig_n < 0)
1376 return -1;
1377
1378 lxc_conf->stopsignal = sig_n;
1379
1380 return 0;
1381 }
1382
1383 static int __set_config_cgroup_controller(const char *key, const char *value,
1384 struct lxc_conf *lxc_conf, int version)
1385 {
1386 const char *subkey, *token;
1387 size_t token_len;
1388 struct lxc_list *cglist = NULL;
1389 struct lxc_cgroup *cgelem = NULL;
1390
1391 if (lxc_config_value_empty(value))
1392 return lxc_clear_cgroups(lxc_conf, key, version);
1393
1394 if (version == CGROUP2_SUPER_MAGIC) {
1395 token = "lxc.cgroup2.";
1396 token_len = 12;
1397 } else if (version == CGROUP_SUPER_MAGIC) {
1398 token = "lxc.cgroup.";
1399 token_len = 11;
1400 } else {
1401 return -EINVAL;
1402 }
1403
1404 if (strncmp(key, token, token_len) != 0)
1405 return -EINVAL;
1406
1407 subkey = key + token_len;
1408 if (*subkey == '\0')
1409 return -EINVAL;
1410
1411 cglist = malloc(sizeof(*cglist));
1412 if (!cglist)
1413 goto out;
1414
1415 cgelem = malloc(sizeof(*cgelem));
1416 if (!cgelem)
1417 goto out;
1418 memset(cgelem, 0, sizeof(*cgelem));
1419
1420 cgelem->subsystem = strdup(subkey);
1421 if (!cgelem->subsystem)
1422 goto out;
1423
1424 cgelem->value = strdup(value);
1425 if (!cgelem->value)
1426 goto out;
1427
1428 cgelem->version = version;
1429
1430 lxc_list_add_elem(cglist, cgelem);
1431
1432 if (version == CGROUP2_SUPER_MAGIC)
1433 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1434 else
1435 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
1436
1437 return 0;
1438
1439 out:
1440 free(cglist);
1441 if (cgelem) {
1442 free(cgelem->subsystem);
1443 free(cgelem->value);
1444 free(cgelem);
1445 }
1446
1447 return -1;
1448 }
1449
1450 static int set_config_cgroup_controller(const char *key, const char *value,
1451 struct lxc_conf *lxc_conf, void *data)
1452 {
1453 return __set_config_cgroup_controller(key, value, lxc_conf,
1454 CGROUP_SUPER_MAGIC);
1455 }
1456
1457 static int set_config_cgroup2_controller(const char *key, const char *value,
1458 struct lxc_conf *lxc_conf, void *data)
1459 {
1460 return __set_config_cgroup_controller(key, value, lxc_conf,
1461 CGROUP2_SUPER_MAGIC);
1462 }
1463
1464
1465 static int set_config_cgroup_dir(const char *key, const char *value,
1466 struct lxc_conf *lxc_conf, void *data)
1467 {
1468 if (lxc_config_value_empty(value))
1469 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1470
1471 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1472 }
1473
1474 static int set_config_prlimit(const char *key, const char *value,
1475 struct lxc_conf *lxc_conf, void *data)
1476 {
1477 struct lxc_list *iter;
1478 struct rlimit limit;
1479 rlim_t limit_value;
1480 struct lxc_list *limlist = NULL;
1481 struct lxc_limit *limelem = NULL;
1482
1483 if (lxc_config_value_empty(value))
1484 return lxc_clear_limits(lxc_conf, key);
1485
1486 if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1487 return -1;
1488
1489 key += sizeof("lxc.prlimit.") - 1;
1490
1491 /* soft limit comes first in the value */
1492 if (!parse_limit_value(&value, &limit_value))
1493 return -1;
1494 limit.rlim_cur = limit_value;
1495
1496 /* skip spaces and a colon */
1497 while (isspace(*value))
1498 ++value;
1499
1500 if (*value == ':')
1501 ++value;
1502 else if (*value) /* any other character is an error here */
1503 return -1;
1504
1505 while (isspace(*value))
1506 ++value;
1507
1508 /* optional hard limit */
1509 if (*value) {
1510 if (!parse_limit_value(&value, &limit_value))
1511 return -1;
1512 limit.rlim_max = limit_value;
1513
1514 /* check for trailing garbage */
1515 while (isspace(*value))
1516 ++value;
1517
1518 if (*value)
1519 return -1;
1520 } else {
1521 /* a single value sets both hard and soft limit */
1522 limit.rlim_max = limit.rlim_cur;
1523 }
1524
1525 /* find existing list element */
1526 lxc_list_for_each(iter, &lxc_conf->limits) {
1527 limelem = iter->elem;
1528 if (!strcmp(key, limelem->resource)) {
1529 limelem->limit = limit;
1530 return 0;
1531 }
1532 }
1533
1534 /* allocate list element */
1535 limlist = malloc(sizeof(*limlist));
1536 if (!limlist)
1537 goto on_error;
1538
1539 limelem = malloc(sizeof(*limelem));
1540 if (!limelem)
1541 goto on_error;
1542 memset(limelem, 0, sizeof(*limelem));
1543
1544 limelem->resource = strdup(key);
1545 if (!limelem->resource)
1546 goto on_error;
1547 limelem->limit = limit;
1548
1549 lxc_list_add_elem(limlist, limelem);;
1550
1551 lxc_list_add_tail(&lxc_conf->limits, limlist);
1552
1553 return 0;
1554
1555 on_error:
1556 free(limlist);
1557 if (limelem) {
1558 free(limelem->resource);
1559 free(limelem);
1560 }
1561 return -1;
1562 }
1563
1564 static int set_config_sysctl(const char *key, const char *value,
1565 struct lxc_conf *lxc_conf, void *data)
1566 {
1567 struct lxc_list *iter;
1568 char *replace_value = NULL;
1569 struct lxc_list *sysctl_list = NULL;
1570 struct lxc_sysctl *sysctl_elem = NULL;
1571
1572 if (lxc_config_value_empty(value))
1573 return clr_config_sysctl(key, lxc_conf, NULL);
1574
1575 if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1576 return -1;
1577
1578 key += sizeof("lxc.sysctl.") - 1;
1579
1580 /* find existing list element */
1581 lxc_list_for_each(iter, &lxc_conf->sysctls) {
1582 sysctl_elem = iter->elem;
1583
1584 if (strcmp(key, sysctl_elem->key) != 0)
1585 continue;
1586
1587 replace_value = strdup(value);
1588 if (!replace_value)
1589 return -1;
1590
1591 free(sysctl_elem->value);
1592 sysctl_elem->value = replace_value;
1593 return 0;
1594 }
1595
1596 /* allocate list element */
1597 sysctl_list = malloc(sizeof(*sysctl_list));
1598 if (!sysctl_list)
1599 goto on_error;
1600
1601 sysctl_elem = malloc(sizeof(*sysctl_elem));
1602 if (!sysctl_elem)
1603 goto on_error;
1604 memset(sysctl_elem, 0, sizeof(*sysctl_elem));
1605
1606 sysctl_elem->key = strdup(key);
1607 if (!sysctl_elem->key)
1608 goto on_error;
1609
1610 sysctl_elem->value = strdup(value);
1611 if (!sysctl_elem->value)
1612 goto on_error;
1613
1614 lxc_list_add_elem(sysctl_list, sysctl_elem);
1615
1616 lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list);
1617
1618 return 0;
1619
1620 on_error:
1621 free(sysctl_list);
1622 if (sysctl_elem) {
1623 free(sysctl_elem->key);
1624 free(sysctl_elem->value);
1625 free(sysctl_elem);
1626 }
1627 return -1;
1628 }
1629
1630 static int set_config_proc(const char *key, const char *value,
1631 struct lxc_conf *lxc_conf, void *data)
1632 {
1633 const char *subkey;
1634 struct lxc_list *proclist = NULL;
1635 struct lxc_proc *procelem = NULL;
1636
1637 if (lxc_config_value_empty(value))
1638 return clr_config_proc(key, lxc_conf, NULL);
1639
1640 if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1641 return -1;
1642
1643 subkey = key + sizeof("lxc.proc.") - 1;
1644 if (*subkey == '\0')
1645 return -EINVAL;
1646
1647 proclist = malloc(sizeof(*proclist));
1648 if (!proclist)
1649 goto on_error;
1650
1651 procelem = malloc(sizeof(*procelem));
1652 if (!procelem)
1653 goto on_error;
1654 memset(procelem, 0, sizeof(*procelem));
1655
1656 procelem->filename = strdup(subkey);
1657 procelem->value = strdup(value);
1658
1659 if (!procelem->filename || !procelem->value)
1660 goto on_error;
1661
1662 proclist->elem = procelem;
1663
1664 lxc_list_add_tail(&lxc_conf->procs, proclist);
1665
1666 return 0;
1667
1668 on_error:
1669 free(proclist);
1670 if (procelem) {
1671 free(procelem->filename);
1672 free(procelem->value);
1673 free(procelem);
1674 }
1675
1676 return -1;
1677 }
1678
1679 static int set_config_idmaps(const char *key, const char *value,
1680 struct lxc_conf *lxc_conf, void *data)
1681 {
1682 unsigned long hostid, nsid, range;
1683 char type;
1684 int ret;
1685 struct lxc_list *idmaplist = NULL;
1686 struct id_map *idmap = NULL;
1687
1688 if (lxc_config_value_empty(value))
1689 return lxc_clear_idmaps(lxc_conf);
1690
1691 idmaplist = malloc(sizeof(*idmaplist));
1692 if (!idmaplist)
1693 goto on_error;
1694
1695 idmap = malloc(sizeof(*idmap));
1696 if (!idmap)
1697 goto on_error;
1698 memset(idmap, 0, sizeof(*idmap));
1699
1700 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
1701 if (ret < 0) {
1702 ERROR("Failed to parse id mappings");
1703 goto on_error;
1704 }
1705
1706 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
1707 if (type == 'u')
1708 idmap->idtype = ID_TYPE_UID;
1709 else if (type == 'g')
1710 idmap->idtype = ID_TYPE_GID;
1711 else
1712 goto on_error;
1713
1714 idmap->hostid = hostid;
1715 idmap->nsid = nsid;
1716 idmap->range = range;
1717 idmaplist->elem = idmap;
1718 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
1719
1720 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
1721 if (idmap->nsid == 0)
1722 lxc_conf->root_nsuid_map = idmap;
1723
1724
1725 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
1726 if (idmap->nsid == 0)
1727 lxc_conf->root_nsgid_map = idmap;
1728
1729 idmap = NULL;
1730
1731 return 0;
1732
1733 on_error:
1734 free(idmaplist);
1735 free(idmap);
1736
1737 return -1;
1738 }
1739
1740 static int set_config_mount_fstab(const char *key, const char *value,
1741 struct lxc_conf *lxc_conf, void *data)
1742 {
1743 if (lxc_config_value_empty(value)) {
1744 clr_config_mount_fstab(key, lxc_conf, NULL);
1745 return -1;
1746 }
1747
1748 return set_config_path_item(&lxc_conf->fstab, value);
1749 }
1750
1751 static int set_config_mount_auto(const char *key, const char *value,
1752 struct lxc_conf *lxc_conf, void *data)
1753 {
1754 char *autos, *autoptr, *sptr, *token;
1755 int i;
1756 int ret = -1;
1757 static struct {
1758 const char *token;
1759 int mask;
1760 int flag;
1761 } allowed_auto_mounts[] = {
1762 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1763 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1764 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
1765 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1766 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
1767 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1768 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
1769 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
1770 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
1771 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
1772 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
1773 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
1774 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
1775 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
1776 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
1777 /* For adding anything that is just a single on/off, but has no
1778 * options: keep mask and flag identical and just define the enum
1779 * value as an unused bit so far
1780 */
1781 { NULL, 0, 0 }
1782 };
1783
1784 if (lxc_config_value_empty(value)) {
1785 lxc_conf->auto_mounts = 0;
1786 return 0;
1787 }
1788
1789 autos = strdup(value);
1790 if (!autos)
1791 return -1;
1792
1793 for (autoptr = autos;; autoptr = NULL) {
1794 token = strtok_r(autoptr, " \t", &sptr);
1795 if (!token) {
1796 ret = 0;
1797 break;
1798 }
1799
1800 for (i = 0; allowed_auto_mounts[i].token; i++) {
1801 if (!strcmp(allowed_auto_mounts[i].token, token))
1802 break;
1803 }
1804
1805 if (!allowed_auto_mounts[i].token) {
1806 ERROR("Invalid filesystem to automount \"%s\"", token);
1807 break;
1808 }
1809
1810 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
1811 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
1812 }
1813
1814 free(autos);
1815 return ret;
1816 }
1817
1818 static int set_config_mount(const char *key, const char *value,
1819 struct lxc_conf *lxc_conf, void *data)
1820 {
1821 char *mntelem;
1822 struct lxc_list *mntlist;
1823
1824 if (lxc_config_value_empty(value))
1825 return lxc_clear_mount_entries(lxc_conf);
1826
1827 mntlist = malloc(sizeof(*mntlist));
1828 if (!mntlist)
1829 return -1;
1830
1831 mntelem = strdup(value);
1832 if (!mntelem) {
1833 free(mntlist);
1834 return -1;
1835 }
1836 mntlist->elem = mntelem;
1837
1838 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1839
1840 return 0;
1841 }
1842
1843 static int set_config_cap_keep(const char *key, const char *value,
1844 struct lxc_conf *lxc_conf, void *data)
1845 {
1846 char *keepcaps, *keepptr, *sptr, *token;
1847 struct lxc_list *keeplist;
1848 int ret = -1;
1849
1850 if (lxc_config_value_empty(value))
1851 return lxc_clear_config_keepcaps(lxc_conf);
1852
1853 keepcaps = strdup(value);
1854 if (!keepcaps)
1855 return -1;
1856
1857 /* In case several capability keep is specified in a single line
1858 * split these caps in a single element for the list.
1859 */
1860 for (keepptr = keepcaps;; keepptr = NULL) {
1861 token = strtok_r(keepptr, " \t", &sptr);
1862 if (!token) {
1863 ret = 0;
1864 break;
1865 }
1866
1867 if (!strcmp(token, "none"))
1868 lxc_clear_config_keepcaps(lxc_conf);
1869
1870 keeplist = malloc(sizeof(*keeplist));
1871 if (!keeplist)
1872 break;
1873
1874 keeplist->elem = strdup(token);
1875 if (!keeplist->elem) {
1876 free(keeplist);
1877 break;
1878 }
1879
1880 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
1881 }
1882
1883 free(keepcaps);
1884
1885 return ret;
1886 }
1887
1888 static int set_config_cap_drop(const char *key, const char *value,
1889 struct lxc_conf *lxc_conf, void *data)
1890 {
1891 char *dropcaps, *dropptr, *sptr, *token;
1892 struct lxc_list *droplist;
1893 int ret = -1;
1894
1895 if (lxc_config_value_empty(value))
1896 return lxc_clear_config_caps(lxc_conf);
1897
1898 dropcaps = strdup(value);
1899 if (!dropcaps)
1900 return -1;
1901
1902 /* In case several capability drop is specified in a single line
1903 * split these caps in a single element for the list.
1904 */
1905 for (dropptr = dropcaps;; dropptr = NULL) {
1906 token = strtok_r(dropptr, " \t", &sptr);
1907 if (!token) {
1908 ret = 0;
1909 break;
1910 }
1911
1912 droplist = malloc(sizeof(*droplist));
1913 if (!droplist)
1914 break;
1915
1916 droplist->elem = strdup(token);
1917 if (!droplist->elem) {
1918 free(droplist);
1919 break;
1920 }
1921
1922 lxc_list_add_tail(&lxc_conf->caps, droplist);
1923 }
1924
1925 free(dropcaps);
1926
1927 return ret;
1928 }
1929
1930 static int set_config_console_path(const char *key, const char *value,
1931 struct lxc_conf *lxc_conf, void *data)
1932 {
1933 return set_config_path_item(&lxc_conf->console.path, value);
1934 }
1935
1936 static int set_config_console_rotate(const char *key, const char *value,
1937 struct lxc_conf *lxc_conf, void *data)
1938 {
1939 if (lxc_config_value_empty(value)) {
1940 lxc_conf->console.log_rotate = 0;
1941 return 0;
1942 }
1943
1944 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
1945 return -1;
1946
1947 if (lxc_conf->console.log_rotate > 1) {
1948 ERROR("The \"lxc.console.rotate\" config key can only be set "
1949 "to 0 or 1");
1950 return -1;
1951 }
1952
1953 return 0;
1954 }
1955
1956 static int set_config_console_logfile(const char *key, const char *value,
1957 struct lxc_conf *lxc_conf, void *data)
1958 {
1959 return set_config_path_item(&lxc_conf->console.log_path, value);
1960 }
1961
1962 static int set_config_console_buffer_size(const char *key, const char *value,
1963 struct lxc_conf *lxc_conf, void *data)
1964 {
1965 int ret;
1966 int64_t size;
1967 uint64_t buffer_size, pgsz;
1968
1969 if (lxc_config_value_empty(value)) {
1970 lxc_conf->console.buffer_size = 0;
1971 return 0;
1972 }
1973
1974 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1975 if (!strcmp(value, "auto")) {
1976 lxc_conf->console.buffer_size = 1 << 17;
1977 return 0;
1978 }
1979
1980 ret = parse_byte_size_string(value, &size);
1981 if (ret < 0)
1982 return -1;
1983
1984 if (size < 0)
1985 return -EINVAL;
1986
1987 /* must be at least a page size */
1988 pgsz = lxc_getpagesize();
1989 if ((uint64_t)size < pgsz) {
1990 NOTICE("Requested ringbuffer size for the console is %" PRId64
1991 " but must be at least %" PRId64
1992 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
1993 size, pgsz, pgsz);
1994 size = pgsz;
1995 }
1996
1997 buffer_size = lxc_find_next_power2((uint64_t)size);
1998 if (buffer_size == 0)
1999 return -EINVAL;
2000
2001 if (buffer_size != size)
2002 NOTICE("Passed size was not a power of 2. Rounding log size to "
2003 "next power of two: %" PRIu64 " bytes", buffer_size);
2004
2005 lxc_conf->console.buffer_size = buffer_size;
2006 return 0;
2007 }
2008
2009 static int set_config_console_buffer_logfile(const char *key, const char *value,
2010 struct lxc_conf *lxc_conf,
2011 void *data)
2012 {
2013 return set_config_path_item(&lxc_conf->console.buffer_log_file, value);
2014 }
2015
2016 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
2017 {
2018 size_t len = conf->unexpanded_len, linelen = strlen(line);
2019
2020 update_hwaddr(line);
2021
2022 while (conf->unexpanded_alloced <= len + linelen + 2) {
2023 char *tmp = realloc(conf->unexpanded_config,
2024 conf->unexpanded_alloced + 1024);
2025 if (!tmp)
2026 return -1;
2027
2028 if (!conf->unexpanded_config)
2029 *tmp = '\0';
2030 conf->unexpanded_config = tmp;
2031 conf->unexpanded_alloced += 1024;
2032 }
2033 strcat(conf->unexpanded_config, line);
2034 conf->unexpanded_len += linelen;
2035 if (line[linelen - 1] != '\n') {
2036 strcat(conf->unexpanded_config, "\n");
2037 conf->unexpanded_len++;
2038 }
2039
2040 return 0;
2041 }
2042
2043 static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2044 {
2045 struct dirent *direntp;
2046 DIR *dir;
2047 char path[MAXPATHLEN];
2048 int len;
2049 int ret = -1;
2050
2051 dir = opendir(dirp);
2052 if (!dir)
2053 return -1;
2054
2055 while ((direntp = readdir(dir))) {
2056 const char *fnam;
2057 if (!direntp)
2058 break;
2059
2060 fnam = direntp->d_name;
2061 if (!strcmp(fnam, "."))
2062 continue;
2063
2064 if (!strcmp(fnam, ".."))
2065 continue;
2066
2067 len = strlen(fnam);
2068 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
2069 continue;
2070
2071 len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam);
2072 if (len < 0 || len >= MAXPATHLEN) {
2073 ret = -1;
2074 goto out;
2075 }
2076
2077 ret = lxc_config_read(path, lxc_conf, true);
2078 if (ret < 0)
2079 goto out;
2080 }
2081 ret = 0;
2082
2083 out:
2084 closedir(dir);
2085
2086 return ret;
2087 }
2088
2089 static int set_config_includefiles(const char *key, const char *value,
2090 struct lxc_conf *lxc_conf, void *data)
2091 {
2092 if (lxc_config_value_empty(value)) {
2093 clr_config_includefiles(key, lxc_conf, NULL);
2094 return 0;
2095 }
2096
2097 if (is_dir(value))
2098 return do_includedir(value, lxc_conf);
2099
2100 return lxc_config_read(value, lxc_conf, true);
2101 }
2102
2103 static int set_config_rootfs_path(const char *key, const char *value,
2104 struct lxc_conf *lxc_conf, void *data)
2105 {
2106 int ret;
2107 char *dup, *tmp;
2108 const char *container_path;
2109
2110 if (lxc_config_value_empty(value)) {
2111 free(lxc_conf->rootfs.path);
2112 lxc_conf->rootfs.path = NULL;
2113 return 0;
2114 }
2115
2116 dup = strdup(value);
2117 if (!dup)
2118 return -1;
2119
2120 /* Split <storage type>:<container path> into <storage type> and
2121 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2122 * "rootfs.path" to <container path>.
2123 */
2124 tmp = strchr(dup, ':');
2125 if (tmp) {
2126 *tmp = '\0';
2127 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2128 if (ret < 0) {
2129 free(dup);
2130 return -1;
2131 }
2132 tmp++;
2133 container_path = tmp;
2134 } else {
2135 container_path = value;
2136 }
2137
2138 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2139 free(dup);
2140 return ret;
2141 }
2142
2143 static int set_config_rootfs_mount(const char *key, const char *value,
2144 struct lxc_conf *lxc_conf, void *data)
2145 {
2146 return set_config_path_item(&lxc_conf->rootfs.mount, value);
2147 }
2148
2149 static int set_config_rootfs_options(const char *key, const char *value,
2150 struct lxc_conf *lxc_conf, void *data)
2151 {
2152 return set_config_string_item(&lxc_conf->rootfs.options, value);
2153 }
2154
2155 static int set_config_rootfs_backend(const char *key, const char *value,
2156 struct lxc_conf *lxc_conf, void *data)
2157 {
2158 return 0;
2159 }
2160
2161 static int set_config_uts_name(const char *key, const char *value,
2162 struct lxc_conf *lxc_conf, void *data)
2163 {
2164 struct utsname *utsname;
2165
2166 if (lxc_config_value_empty(value)) {
2167 clr_config_uts_name(key, lxc_conf, NULL);
2168 return 0;
2169 }
2170
2171 utsname = malloc(sizeof(*utsname));
2172 if (!utsname)
2173 return -1;
2174
2175 if (strlen(value) >= sizeof(utsname->nodename)) {
2176 free(utsname);
2177 return -1;
2178 }
2179
2180 strcpy(utsname->nodename, value);
2181 free(lxc_conf->utsname);
2182 lxc_conf->utsname = utsname;
2183
2184 return 0;
2185 }
2186
2187 static int set_config_namespace_clone(const char *key, const char *value,
2188 struct lxc_conf *lxc_conf, void *data)
2189 {
2190 char *ns, *nsptr, *token;
2191 int cloneflag = 0;
2192 char *saveptr = NULL;
2193
2194 if (lxc_config_value_empty(value))
2195 return clr_config_namespace_clone(key, lxc_conf, data);
2196
2197 if (lxc_conf->ns_keep != 0) {
2198 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2199 "\"lxc.namespace.keep\"", strerror(EINVAL));
2200 return -EINVAL;
2201 }
2202
2203 ns = strdup(value);
2204 if (!ns)
2205 return -1;
2206 nsptr = ns;
2207
2208 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2209 token += lxc_char_left_gc(token, strlen(token));
2210 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2211 cloneflag = lxc_namespace_2_cloneflag(token);
2212 if (cloneflag < 0) {
2213 free(ns);
2214 return -EINVAL;
2215 }
2216 lxc_conf->ns_clone |= cloneflag;
2217 }
2218 free(ns);
2219
2220 return 0;
2221 }
2222
2223 static int set_config_namespace_keep(const char *key, const char *value,
2224 struct lxc_conf *lxc_conf, void *data)
2225 {
2226 char *ns, *nsptr, *token;
2227 int cloneflag = 0;
2228 char *saveptr = NULL;
2229
2230 if (lxc_config_value_empty(value))
2231 return clr_config_namespace_keep(key, lxc_conf, data);
2232
2233 if (lxc_conf->ns_clone != 0) {
2234 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2235 "\"lxc.namespace.keep\"", strerror(EINVAL));
2236 return -EINVAL;
2237 }
2238
2239 ns = strdup(value);
2240 if (!ns)
2241 return -1;
2242 nsptr = ns;
2243
2244 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2245 token += lxc_char_left_gc(token, strlen(token));
2246 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2247 cloneflag = lxc_namespace_2_cloneflag(token);
2248 if (cloneflag < 0) {
2249 free(ns);
2250 return -EINVAL;
2251 }
2252 lxc_conf->ns_keep |= cloneflag;
2253 }
2254 free(ns);
2255
2256 return 0;
2257 }
2258
2259 static int set_config_namespace_share(const char *key, const char *value,
2260 struct lxc_conf *lxc_conf, void *data)
2261 {
2262 int ns_idx;
2263 const char *namespace;
2264
2265 if (lxc_config_value_empty(value))
2266 return clr_config_namespace_share(key, lxc_conf, data);
2267
2268 namespace = key + sizeof("lxc.namespace.share.") - 1;
2269 ns_idx = lxc_namespace_2_ns_idx(namespace);
2270 if (ns_idx < 0)
2271 return ns_idx;
2272
2273 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
2274 }
2275
2276 struct parse_line_conf {
2277 struct lxc_conf *conf;
2278 bool from_include;
2279 };
2280
2281 static int parse_line(char *buffer, void *data)
2282 {
2283 char *dot, *key, *line, *linep, *value;
2284 bool empty_line;
2285 struct lxc_config_t *config;
2286 int ret = 0;
2287 char *dup = buffer;
2288 struct parse_line_conf *plc = data;
2289
2290 /* If there are newlines in the config file we should keep them. */
2291 empty_line = lxc_is_line_empty(dup);
2292 if (empty_line)
2293 dup = "\n";
2294
2295 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2296 * modified the original string on the stack by replacing '=' by '\0'
2297 * below.
2298 */
2299 linep = line = strdup(dup);
2300 if (!line)
2301 return -1;
2302
2303 if (!plc->from_include) {
2304 ret = append_unexp_config_line(line, plc->conf);
2305 if (ret < 0)
2306 goto on_error;
2307 }
2308
2309 if (empty_line)
2310 goto on_error;
2311
2312 line += lxc_char_left_gc(line, strlen(line));
2313
2314 /* ignore comments */
2315 if (line[0] == '#')
2316 goto on_error;
2317
2318 /* martian option - don't add it to the config itself */
2319 if (strncmp(line, "lxc.", 4))
2320 goto on_error;
2321
2322 ret = -1;
2323
2324 dot = strchr(line, '=');
2325 if (!dot) {
2326 ERROR("Invalid configuration line: %s", line);
2327 goto on_error;
2328 }
2329
2330 *dot = '\0';
2331 value = dot + 1;
2332
2333 key = line;
2334 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2335
2336 value += lxc_char_left_gc(value, strlen(value));
2337 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2338
2339 if (*value == '\'' || *value == '\"') {
2340 size_t len;
2341
2342 len = strlen(value);
2343 if (len > 1 && value[len - 1] == *value) {
2344 value[len - 1] = '\0';
2345 value++;
2346 }
2347 }
2348
2349 config = lxc_get_config(key);
2350 if (!config) {
2351 ERROR("Unknown configuration key \"%s\"", key);
2352 goto on_error;
2353 }
2354
2355 /* [START]: REMOVE IN LXC 3.0 */
2356 if (config->is_legacy_key && !plc->conf->contains_legacy_key) {
2357 plc->conf->contains_legacy_key = true;
2358 if (getenv("LXC_UPDATE_CONFIG_FORMAT")) {
2359 /* Warn the user once loud and clear that there is at
2360 * least one legacy configuration item in the
2361 * configuration file and then an update is required.
2362 */
2363 fprintf(stderr, "The configuration file contains "
2364 "legacy configuration keys.\nPlease "
2365 "update your configuration file!\nThe "
2366 "update script lxc-update-config -c "
2367 "<path-to-config> can be used for "
2368 "this.\n");
2369 }
2370 }
2371 /* [END]: REMOVE IN LXC 3.0 */
2372
2373 ret = config->set(key, value, plc->conf, NULL);
2374
2375 on_error:
2376 free(linep);
2377 return ret;
2378 }
2379
2380 static int lxc_config_readline(char *buffer, struct lxc_conf *conf)
2381 {
2382 struct parse_line_conf c;
2383
2384 c.conf = conf;
2385 c.from_include = false;
2386
2387 return parse_line(buffer, &c);
2388 }
2389
2390 int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
2391 {
2392 int ret;
2393 struct parse_line_conf c;
2394
2395 c.conf = conf;
2396 c.from_include = from_include;
2397
2398 ret = access(file, R_OK);
2399 if (ret < 0)
2400 return -1;
2401
2402 /* Catch only the top level config file name in the structure. */
2403 if (!conf->rcfile)
2404 conf->rcfile = strdup(file);
2405
2406 return lxc_file_for_each_line(file, parse_line, &c);
2407 }
2408
2409 int lxc_config_define_add(struct lxc_list *defines, char *arg)
2410 {
2411 struct lxc_list *dent;
2412
2413 dent = malloc(sizeof(struct lxc_list));
2414 if (!dent)
2415 return -1;
2416
2417 dent->elem = arg;
2418 lxc_list_add_tail(defines, dent);
2419 return 0;
2420 }
2421
2422 int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf)
2423 {
2424 struct lxc_list *it, *next;
2425 int ret = 0;
2426
2427 lxc_list_for_each(it, defines) {
2428 ret = lxc_config_readline(it->elem, conf);
2429 if (ret)
2430 break;
2431 }
2432
2433 lxc_list_for_each_safe(it, defines, next) {
2434 lxc_list_del(it);
2435 free(it);
2436 }
2437
2438 return ret;
2439 }
2440
2441 signed long lxc_config_parse_arch(const char *arch)
2442 {
2443 #if HAVE_SYS_PERSONALITY_H
2444 size_t i;
2445 struct per_name {
2446 char *name;
2447 unsigned long per;
2448 } pername[] = {
2449 { "x86", PER_LINUX32 },
2450 { "linux32", PER_LINUX32 },
2451 { "i386", PER_LINUX32 },
2452 { "i486", PER_LINUX32 },
2453 { "i586", PER_LINUX32 },
2454 { "i686", PER_LINUX32 },
2455 { "athlon", PER_LINUX32 },
2456 { "mips", PER_LINUX32 },
2457 { "mipsel", PER_LINUX32 },
2458 { "ppc", PER_LINUX32 },
2459 { "arm", PER_LINUX32 },
2460 { "armv7l", PER_LINUX32 },
2461 { "armhf", PER_LINUX32 },
2462 { "armel", PER_LINUX32 },
2463 { "powerpc", PER_LINUX32 },
2464 { "linux64", PER_LINUX },
2465 { "x86_64", PER_LINUX },
2466 { "amd64", PER_LINUX },
2467 { "mips64", PER_LINUX },
2468 { "mips64el", PER_LINUX },
2469 { "ppc64", PER_LINUX },
2470 { "ppc64le", PER_LINUX },
2471 { "ppc64el", PER_LINUX },
2472 { "powerpc64", PER_LINUX },
2473 { "s390x", PER_LINUX },
2474 { "aarch64", PER_LINUX },
2475 { "arm64", PER_LINUX },
2476 };
2477 size_t len = sizeof(pername) / sizeof(pername[0]);
2478
2479 for (i = 0; i < len; i++) {
2480 if (!strcmp(pername[i].name, arch))
2481 return pername[i].per;
2482 }
2483 #endif
2484
2485 return -1;
2486 }
2487
2488 int lxc_fill_elevated_privileges(char *flaglist, int *flags)
2489 {
2490 char *token, *saveptr = NULL;
2491 int i, aflag;
2492 struct {
2493 const char *token;
2494 int flag;
2495 } all_privs[] = {
2496 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
2497 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
2498 { "LSM", LXC_ATTACH_LSM_EXEC },
2499 { NULL, 0 }
2500 };
2501
2502 if (!flaglist) {
2503 /* For the sake of backward compatibility, drop all privileges
2504 * if none is specified.
2505 */
2506 for (i = 0; all_privs[i].token; i++)
2507 *flags |= all_privs[i].flag;
2508
2509 return 0;
2510 }
2511
2512 token = strtok_r(flaglist, "|", &saveptr);
2513 while (token) {
2514 aflag = -1;
2515 for (i = 0; all_privs[i].token; i++)
2516 if (!strcmp(all_privs[i].token, token))
2517 aflag = all_privs[i].flag;
2518 if (aflag < 0)
2519 return -1;
2520
2521 *flags |= aflag;
2522
2523 token = strtok_r(NULL, "|", &saveptr);
2524 }
2525
2526 return 0;
2527 }
2528
2529 /* Write out a configuration file. */
2530 void write_config(FILE *fout, struct lxc_conf *c)
2531 {
2532 int ret;
2533 size_t len = c->unexpanded_len;
2534
2535 if (!len)
2536 return;
2537
2538 ret = fwrite(c->unexpanded_config, 1, len, fout);
2539 if (ret != len)
2540 SYSERROR("Failed to write configuration file");
2541 }
2542
2543 bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
2544 const char *v)
2545 {
2546 int ret;
2547 size_t len;
2548 char *tmp;
2549
2550 len = strlen(key) + strlen(v) + 4;
2551 tmp = alloca(len);
2552
2553 if (lxc_config_value_empty(v))
2554 ret = snprintf(tmp, len, "%s =", key);
2555 else
2556 ret = snprintf(tmp, len, "%s = %s", key, v);
2557 if (ret < 0 || ret >= len)
2558 return false;
2559
2560 /* Save the line verbatim into unexpanded_conf */
2561 if (append_unexp_config_line(tmp, conf))
2562 return false;
2563
2564 return true;
2565 }
2566
2567 void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
2568 bool rm_subkeys)
2569 {
2570 char *lend;
2571 char *lstart = conf->unexpanded_config;
2572
2573 if (!conf->unexpanded_config)
2574 return;
2575
2576 while (*lstart) {
2577 lend = strchr(lstart, '\n');
2578 char v;
2579 if (!lend)
2580 lend = lstart + strlen(lstart);
2581 else
2582 lend++;
2583 if (strncmp(lstart, key, strlen(key)) != 0) {
2584 lstart = lend;
2585 continue;
2586 }
2587 if (!rm_subkeys) {
2588 v = lstart[strlen(key)];
2589 if (!isspace(v) && v != '=') {
2590 lstart = lend;
2591 continue;
2592 }
2593 }
2594 conf->unexpanded_len -= (lend - lstart);
2595 if (*lend == '\0') {
2596 *lstart = '\0';
2597 return;
2598 }
2599 memmove(lstart, lend, strlen(lend) + 1);
2600 }
2601 }
2602
2603 bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
2604 const char *newpath, const char *oldname,
2605 const char *newname, const char *ovldir)
2606 {
2607 int ret;
2608 char *lend, *newdir, *olddir, *p, *q;
2609 size_t newdirlen, olddirlen;
2610 char *lstart = conf->unexpanded_config;
2611 const char *key = "lxc.mount.entry";
2612
2613 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
2614 olddir = alloca(olddirlen + 1);
2615 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
2616 oldname);
2617 if (ret < 0 || ret >= olddirlen + 1)
2618 return false;
2619
2620 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
2621 newdir = alloca(newdirlen + 1);
2622 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
2623 newname);
2624 if (ret < 0 || ret >= newdirlen + 1)
2625 return false;
2626
2627 if (!conf->unexpanded_config)
2628 return true;
2629
2630 while (*lstart) {
2631 lend = strchr(lstart, '\n');
2632 if (!lend)
2633 lend = lstart + strlen(lstart);
2634 else
2635 lend++;
2636
2637 if (strncmp(lstart, key, strlen(key)) != 0)
2638 goto next;
2639
2640 p = strchr(lstart + strlen(key), '=');
2641 if (!p)
2642 goto next;
2643 p++;
2644
2645 while (isblank(*p))
2646 p++;
2647
2648 if (p >= lend)
2649 goto next;
2650
2651 /* Whenever an lxc.mount.entry entry is found in a line we check
2652 * if the substring " overlay" or the substring " aufs" is
2653 * present before doing any further work. We check for "
2654 * overlay" and " aufs" since both substrings need to have at
2655 * least one space before them in a valid overlay
2656 * lxc.mount.entry (/A B overlay). When the space before is
2657 * missing it is very likely that these substrings are part of a
2658 * path or something else. (Checking q >= lend ensures that we
2659 * only count matches in the current line.) */
2660 if ((!(q = strstr(p, " overlay")) || q >= lend) &&
2661 (!(q = strstr(p, " aufs")) || q >= lend))
2662 goto next;
2663
2664 if (!(q = strstr(p, olddir)) || (q >= lend))
2665 goto next;
2666
2667 /* replace the olddir with newdir */
2668 if (olddirlen >= newdirlen) {
2669 size_t diff = olddirlen - newdirlen;
2670 memcpy(q, newdir, newdirlen);
2671 if (olddirlen != newdirlen) {
2672 memmove(q + newdirlen, q + newdirlen + diff,
2673 strlen(q) - newdirlen - diff + 1);
2674 lend -= diff;
2675 conf->unexpanded_len -= diff;
2676 }
2677 } else {
2678 char *new;
2679 size_t diff = newdirlen - olddirlen;
2680 size_t oldlen = conf->unexpanded_len;
2681 size_t newlen = oldlen + diff;
2682 size_t poffset = q - conf->unexpanded_config;
2683
2684 new = realloc(conf->unexpanded_config, newlen + 1);
2685 if (!new)
2686 return false;
2687
2688 conf->unexpanded_len = newlen;
2689 conf->unexpanded_alloced = newlen + 1;
2690 new[newlen - 1] = '\0';
2691 lend = new + (lend - conf->unexpanded_config);
2692 /* Move over the remainder to make room for the newdir.
2693 */
2694 memmove(new + poffset + newdirlen,
2695 new + poffset + olddirlen,
2696 oldlen - poffset - olddirlen + 1);
2697 conf->unexpanded_config = new;
2698 memcpy(new + poffset, newdir, newdirlen);
2699 lend += diff;
2700 }
2701 next:
2702 lstart = lend;
2703 }
2704
2705 return true;
2706 }
2707
2708 bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
2709 const char *newpath, const char *oldname,
2710 const char *newname)
2711 {
2712 int ret;
2713 char *lend, *newdir, *olddir, *p;
2714 char *lstart = conf->unexpanded_config;
2715 size_t newdirlen, olddirlen;
2716 const char *key = "lxc.hook";
2717
2718 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
2719 olddir = alloca(olddirlen + 1);
2720 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
2721 if (ret < 0 || ret >= olddirlen + 1)
2722 return false;
2723
2724 newdirlen = strlen(newpath) + strlen(newname) + 1;
2725 newdir = alloca(newdirlen + 1);
2726 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
2727 if (ret < 0 || ret >= newdirlen + 1)
2728 return false;
2729
2730 if (!conf->unexpanded_config)
2731 return true;
2732
2733 while (*lstart) {
2734 lend = strchr(lstart, '\n');
2735 if (!lend)
2736 lend = lstart + strlen(lstart);
2737 else
2738 lend++;
2739
2740 if (strncmp(lstart, key, strlen(key)) != 0)
2741 goto next;
2742
2743 p = strchr(lstart + strlen(key), '=');
2744 if (!p)
2745 goto next;
2746 p++;
2747
2748 while (isblank(*p))
2749 p++;
2750
2751 if (p >= lend)
2752 goto next;
2753
2754 if (strncmp(p, olddir, strlen(olddir)) != 0)
2755 goto next;
2756
2757 /* replace the olddir with newdir */
2758 if (olddirlen >= newdirlen) {
2759 size_t diff = olddirlen - newdirlen;
2760 memcpy(p, newdir, newdirlen);
2761 if (olddirlen != newdirlen) {
2762 memmove(p + newdirlen, p + newdirlen + diff,
2763 strlen(p) - newdirlen - diff + 1);
2764 lend -= diff;
2765 conf->unexpanded_len -= diff;
2766 }
2767 } else {
2768 char *new;
2769 size_t diff = newdirlen - olddirlen;
2770 size_t oldlen = conf->unexpanded_len;
2771 size_t newlen = oldlen + diff;
2772 size_t poffset = p - conf->unexpanded_config;
2773
2774 new = realloc(conf->unexpanded_config, newlen + 1);
2775 if (!new)
2776 return false;
2777
2778 conf->unexpanded_len = newlen;
2779 conf->unexpanded_alloced = newlen + 1;
2780 new[newlen - 1] = '\0';
2781 lend = new + (lend - conf->unexpanded_config);
2782 /* Move over the remainder to make room for the newdir.
2783 */
2784 memmove(new + poffset + newdirlen,
2785 new + poffset + olddirlen,
2786 oldlen - poffset - olddirlen + 1);
2787 conf->unexpanded_config = new;
2788 memcpy(new + poffset, newdir, newdirlen);
2789 lend += diff;
2790 }
2791 next:
2792 lstart = lend;
2793 }
2794
2795 return true;
2796 }
2797
2798 #define DO(cmd) \
2799 { \
2800 if (!(cmd)) { \
2801 ERROR("Error writing to new config"); \
2802 return false; \
2803 } \
2804 }
2805
2806 /* This is called only from clone. We wish to update all hwaddrs in the
2807 * unexpanded config file. We can't/don't want to update any which come from
2808 * lxc.includes (there shouldn't be any).
2809 * We can't just walk the c->lxc-conf->network list because that includes netifs
2810 * from the include files. So we update the ones which we find in the unexp
2811 * config file, then find the original macaddr in the conf->network, and update
2812 * that to the same value.
2813 */
2814 bool network_new_hwaddrs(struct lxc_conf *conf)
2815 {
2816 char *lend, *p, *p2;
2817 struct lxc_list *it;
2818 char *lstart = conf->unexpanded_config;
2819
2820 if (!conf->unexpanded_config)
2821 return true;
2822
2823 while (*lstart) {
2824 char newhwaddr[18], oldhwaddr[17];
2825
2826 lend = strchr(lstart, '\n');
2827 if (!lend)
2828 lend = lstart + strlen(lstart);
2829 else
2830 lend++;
2831
2832 if (!lxc_config_net_hwaddr(lstart)) {
2833 lstart = lend;
2834 continue;
2835 }
2836
2837 p = strchr(lstart, '=');
2838 if (!p) {
2839 lstart = lend;
2840 continue;
2841 }
2842
2843 p++;
2844 while (isblank(*p))
2845 p++;
2846 if (!*p)
2847 return true;
2848
2849 p2 = p;
2850 while (*p2 && !isblank(*p2) && *p2 != '\n')
2851 p2++;
2852
2853 if ((p2 - p) != 17) {
2854 WARN("Bad hwaddr entry");
2855 lstart = lend;
2856 continue;
2857 }
2858
2859 memcpy(oldhwaddr, p, 17);
2860
2861 if (!new_hwaddr(newhwaddr))
2862 return false;
2863
2864 memcpy(p, newhwaddr, 17);
2865 lxc_list_for_each(it, &conf->network) {
2866 struct lxc_netdev *n = it->elem;
2867
2868 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
2869 memcpy(n->hwaddr, newhwaddr, 17);
2870 }
2871
2872 lstart = lend;
2873 }
2874
2875 return true;
2876 }
2877
2878 static int set_config_ephemeral(const char *key, const char *value,
2879 struct lxc_conf *lxc_conf, void *data)
2880 {
2881 if (lxc_config_value_empty(value)) {
2882 lxc_conf->ephemeral = 0;
2883 return 0;
2884 }
2885
2886 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
2887 return -1;
2888
2889 if (lxc_conf->ephemeral > 1)
2890 return -1;
2891
2892 return 0;
2893 }
2894
2895 static int set_config_log_syslog(const char *key, const char *value,
2896 struct lxc_conf *lxc_conf, void *data)
2897 {
2898 int facility;
2899
2900 if (lxc_conf->syslog) {
2901 free(lxc_conf->syslog);
2902 lxc_conf->syslog = NULL;
2903 }
2904
2905 if (lxc_config_value_empty(value))
2906 return 0;
2907
2908 facility = lxc_syslog_priority_to_int(value);
2909 if (facility == -EINVAL)
2910 return -1;
2911
2912 lxc_log_syslog(facility);
2913 return set_config_string_item(&lxc_conf->syslog, value);
2914 }
2915
2916 static int set_config_no_new_privs(const char *key, const char *value,
2917 struct lxc_conf *lxc_conf, void *data)
2918 {
2919 unsigned int v;
2920
2921 if (lxc_config_value_empty(value)) {
2922 lxc_conf->no_new_privs = false;
2923 return 0;
2924 }
2925
2926 if (lxc_safe_uint(value, &v) < 0)
2927 return -1;
2928
2929 if (v > 1)
2930 return -1;
2931
2932 lxc_conf->no_new_privs = v ? true : false;
2933
2934 return 0;
2935 }
2936
2937 static int set_config_noop(const char *key, const char *value,
2938 struct lxc_conf *lxc_conf, void *data)
2939 {
2940 return 0;
2941 }
2942
2943 /* Callbacks to get configuration items. */
2944 static int get_config_personality(const char *key, char *retv, int inlen,
2945 struct lxc_conf *c, void *data)
2946 {
2947 int fulllen = 0;
2948
2949 if (!retv)
2950 inlen = 0;
2951 else
2952 memset(retv, 0, inlen);
2953
2954 #if HAVE_SYS_PERSONALITY_H
2955 int len = 0;
2956
2957 switch (c->personality) {
2958 case PER_LINUX32:
2959 strprint(retv, inlen, "i686");
2960 break;
2961 case PER_LINUX:
2962 strprint(retv, inlen, "x86_64");
2963 break;
2964 default:
2965 break;
2966 }
2967 #endif
2968
2969 return fulllen;
2970 }
2971
2972 static int get_config_pty_max(const char *key, char *retv, int inlen,
2973 struct lxc_conf *c, void *data)
2974 {
2975 return lxc_get_conf_int(c, retv, inlen, c->pts);
2976 }
2977
2978 static int get_config_tty_max(const char *key, char *retv, int inlen,
2979 struct lxc_conf *c, void *data)
2980 {
2981 return lxc_get_conf_int(c, retv, inlen, c->tty);
2982 }
2983
2984 static int get_config_tty_dir(const char *key, char *retv, int inlen,
2985 struct lxc_conf *c, void *data)
2986 {
2987 return lxc_get_conf_str(retv, inlen, c->ttydir);
2988 }
2989
2990 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
2991 struct lxc_conf *c, void *data)
2992 {
2993 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
2994 }
2995
2996 static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
2997 int inlen, struct lxc_conf *c,
2998 void *data)
2999 {
3000 return lxc_get_conf_int(c, retv, inlen,
3001 c->lsm_aa_allow_incomplete);
3002 }
3003
3004 static int get_config_selinux_context(const char *key, char *retv, int inlen,
3005 struct lxc_conf *c, void *data)
3006 {
3007 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
3008 }
3009
3010 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3011 * just the value(s) will be printed. Since there still could be more than one,
3012 * it is newline-separated.
3013 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
3014 * have newlines?)
3015 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3016 * 'lxc.cgroup.subsystem.key = value' format.
3017 */
3018 static int __get_config_cgroup_controller(const char *key, char *retv,
3019 int inlen, struct lxc_conf *c,
3020 int version)
3021 {
3022 int len;
3023 size_t namespaced_token_len;
3024 char *global_token, *namespaced_token;
3025 struct lxc_list *it;
3026 int fulllen = 0;
3027 bool get_all = false;
3028
3029 if (!retv)
3030 inlen = 0;
3031 else
3032 memset(retv, 0, inlen);
3033
3034 if (version == CGROUP2_SUPER_MAGIC) {
3035 global_token = "lxc.cgroup2";
3036 namespaced_token = "lxc.cgroup2.";
3037 namespaced_token_len = sizeof("lxc.cgroup2.") - 1;;
3038 } else if (version == CGROUP_SUPER_MAGIC) {
3039 global_token = "lxc.cgroup";
3040 namespaced_token = "lxc.cgroup.";
3041 namespaced_token_len = sizeof("lxc.cgroup.") - 1;;
3042 } else {
3043 return -1;
3044 }
3045
3046 if (strcmp(key, global_token) == 0)
3047 get_all = true;
3048 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3049 key += namespaced_token_len;
3050 else
3051 return -1;
3052
3053 lxc_list_for_each(it, &c->cgroup) {
3054 struct lxc_cgroup *cg = it->elem;
3055
3056 if (get_all) {
3057 if (version != cg->version)
3058 continue;
3059
3060 strprint(retv, inlen, "%s.%s = %s\n",
3061 global_token, cg->subsystem, cg->value);
3062 } else if (!strcmp(cg->subsystem, key)) {
3063 strprint(retv, inlen, "%s\n", cg->value);
3064 }
3065 }
3066
3067 return fulllen;
3068 }
3069
3070 static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3071 struct lxc_conf *c, void *data)
3072 {
3073 return __get_config_cgroup_controller(key, retv, inlen, c,
3074 CGROUP_SUPER_MAGIC);
3075 }
3076
3077 static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3078 struct lxc_conf *c, void *data)
3079 {
3080 return __get_config_cgroup_controller(key, retv, inlen, c,
3081 CGROUP2_SUPER_MAGIC);
3082 }
3083
3084 static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3085 struct lxc_conf *lxc_conf, void *data)
3086 {
3087 int len;
3088 int fulllen = 0;
3089
3090 if (!retv)
3091 inlen = 0;
3092 else
3093 memset(retv, 0, inlen);
3094
3095 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3096
3097 return fulllen;
3098 }
3099
3100 static int get_config_idmaps(const char *key, char *retv, int inlen,
3101 struct lxc_conf *c, void *data)
3102 {
3103 struct lxc_list *it;
3104 int len, listlen, ret;
3105 int fulllen = 0;
3106 /* "u 1000 1000000 65536"
3107 *
3108 * let's render this as
3109 *
3110 * sizeof(char)
3111 * +
3112 * sizeof(" ")
3113 * +
3114 * sizeof(uint64_t)
3115 * +
3116 * sizeof(" ")
3117 * +
3118 * sizeof(uint64_t)
3119 * +
3120 * sizeof(" ")
3121 * +
3122 * sizeof(uint64_t)
3123 * +
3124 * \0
3125 */
3126 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3127 char buf[__LXC_IDMAP_STR_BUF];
3128
3129 if (!retv)
3130 inlen = 0;
3131 else
3132 memset(retv, 0, inlen);
3133
3134 listlen = lxc_list_len(&c->id_map);
3135 lxc_list_for_each(it, &c->id_map)
3136 {
3137 struct id_map *map = it->elem;
3138 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3139 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3140 map->nsid, map->hostid, map->range);
3141 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3142 return -1;
3143
3144 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3145 }
3146 return fulllen;
3147 }
3148
3149 static int get_config_log_level(const char *key, char *retv, int inlen,
3150 struct lxc_conf *c, void *data)
3151 {
3152 const char *v;
3153 v = lxc_log_priority_to_string(c->loglevel);
3154 return lxc_get_conf_str(retv, inlen, v);
3155 }
3156
3157 static int get_config_log_file(const char *key, char *retv, int inlen,
3158 struct lxc_conf *c, void *data)
3159 {
3160 return lxc_get_conf_str(retv, inlen, c->logfile);
3161 }
3162
3163 static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3164 struct lxc_conf *c, void *data)
3165 {
3166 return lxc_get_conf_str(retv, inlen, c->fstab);
3167 }
3168
3169 static int get_config_mount_auto(const char *key, char *retv, int inlen,
3170 struct lxc_conf *c, void *data)
3171 {
3172 int len, fulllen = 0;
3173 const char *sep = "";
3174
3175 if (!retv)
3176 inlen = 0;
3177 else
3178 memset(retv, 0, inlen);
3179
3180 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
3181 return 0;
3182
3183 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
3184 case LXC_AUTO_PROC_MIXED:
3185 strprint(retv, inlen, "%sproc:mixed", sep);
3186 sep = " ";
3187 break;
3188 case LXC_AUTO_PROC_RW:
3189 strprint(retv, inlen, "%sproc:rw", sep);
3190 sep = " ";
3191 break;
3192 default:
3193 break;
3194 }
3195
3196 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
3197 case LXC_AUTO_SYS_RO:
3198 strprint(retv, inlen, "%ssys:ro", sep);
3199 sep = " ";
3200 break;
3201 case LXC_AUTO_SYS_RW:
3202 strprint(retv, inlen, "%ssys:rw", sep);
3203 sep = " ";
3204 break;
3205 case LXC_AUTO_SYS_MIXED:
3206 strprint(retv, inlen, "%ssys:mixed", sep);
3207 sep = " ";
3208 break;
3209 default:
3210 break;
3211 }
3212
3213 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
3214 case LXC_AUTO_CGROUP_NOSPEC:
3215 strprint(retv, inlen, "%scgroup", sep);
3216 break;
3217 case LXC_AUTO_CGROUP_MIXED:
3218 strprint(retv, inlen, "%scgroup:mixed", sep);
3219 break;
3220 case LXC_AUTO_CGROUP_RO:
3221 strprint(retv, inlen, "%scgroup:ro", sep);
3222 break;
3223 case LXC_AUTO_CGROUP_RW:
3224 strprint(retv, inlen, "%scgroup:rw", sep);
3225 break;
3226 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3227 strprint(retv, inlen, "%scgroup-full", sep);
3228 break;
3229 case LXC_AUTO_CGROUP_FULL_MIXED:
3230 strprint(retv, inlen, "%scgroup-full:mixed", sep);
3231 break;
3232 case LXC_AUTO_CGROUP_FULL_RO:
3233 strprint(retv, inlen, "%scgroup-full:ro", sep);
3234 break;
3235 case LXC_AUTO_CGROUP_FULL_RW:
3236 strprint(retv, inlen, "%scgroup-full:rw", sep);
3237 break;
3238 default:
3239 break;
3240 }
3241
3242 return fulllen;
3243 }
3244
3245 static int get_config_mount(const char *key, char *retv, int inlen,
3246 struct lxc_conf *c, void *data)
3247 {
3248 int len, fulllen = 0;
3249 struct lxc_list *it;
3250
3251 if (!retv)
3252 inlen = 0;
3253 else
3254 memset(retv, 0, inlen);
3255
3256 lxc_list_for_each(it, &c->mount_list) {
3257 strprint(retv, inlen, "%s\n", (char *)it->elem);
3258 }
3259
3260 return fulllen;
3261 }
3262
3263 static int get_config_rootfs_path(const char *key, char *retv, int inlen,
3264 struct lxc_conf *c, void *data)
3265 {
3266 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
3267 }
3268
3269 static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
3270 struct lxc_conf *c, void *data)
3271 {
3272 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3273 }
3274
3275 static int get_config_rootfs_options(const char *key, char *retv, int inlen,
3276 struct lxc_conf *c, void *data)
3277 {
3278 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
3279 }
3280
3281 static int get_config_rootfs_backend(const char *key, char *retv, int inlen,
3282 struct lxc_conf *c, void *data)
3283 {
3284 return 0;
3285 }
3286
3287 static int get_config_uts_name(const char *key, char *retv, int inlen,
3288 struct lxc_conf *c, void *data)
3289 {
3290 return lxc_get_conf_str(
3291 retv, inlen,
3292 c->utsname ? c->utsname->nodename : NULL);
3293 }
3294
3295 static int get_config_hooks(const char *key, char *retv, int inlen,
3296 struct lxc_conf *c, void *data)
3297 {
3298 char *subkey;
3299 int len, fulllen = 0, found = -1;
3300 struct lxc_list *it;
3301 int i;
3302
3303 subkey = strchr(key, '.');
3304 if (subkey)
3305 subkey = strchr(subkey + 1, '.');
3306 if (!subkey)
3307 return -1;
3308 subkey++;
3309 if (!*subkey)
3310 return -1;
3311 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3312 if (strcmp(lxchook_names[i], subkey) == 0) {
3313 found = i;
3314 break;
3315 }
3316 }
3317 if (found == -1)
3318 return -1;
3319
3320 if (!retv)
3321 inlen = 0;
3322 else
3323 memset(retv, 0, inlen);
3324
3325 lxc_list_for_each(it, &c->hooks[found]) {
3326 strprint(retv, inlen, "%s\n", (char *)it->elem);
3327 }
3328 return fulllen;
3329 }
3330
3331 static int get_config_hooks_version(const char *key, char *retv, int inlen,
3332 struct lxc_conf *c, void *data)
3333 {
3334 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
3335 }
3336
3337 static int get_config_net(const char *key, char *retv, int inlen,
3338 struct lxc_conf *c, void *data)
3339 {
3340 int len, fulllen = 0;
3341 struct lxc_list *it;
3342
3343 if (!retv)
3344 inlen = 0;
3345 else
3346 memset(retv, 0, inlen);
3347
3348 lxc_list_for_each(it, &c->network) {
3349 struct lxc_netdev *n = it->elem;
3350 const char *t = lxc_net_type_to_str(n->type);
3351 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
3352 }
3353
3354 return fulllen;
3355 }
3356
3357 static int get_config_cap_drop(const char *key, char *retv, int inlen,
3358 struct lxc_conf *c, void *data)
3359 {
3360 int len, fulllen = 0;
3361 struct lxc_list *it;
3362
3363 if (!retv)
3364 inlen = 0;
3365 else
3366 memset(retv, 0, inlen);
3367
3368 lxc_list_for_each(it, &c->caps) {
3369 strprint(retv, inlen, "%s\n", (char *)it->elem);
3370 }
3371
3372 return fulllen;
3373 }
3374
3375 static int get_config_cap_keep(const char *key, char *retv, int inlen,
3376 struct lxc_conf *c, void *data)
3377 {
3378 int len, fulllen = 0;
3379 struct lxc_list *it;
3380
3381 if (!retv)
3382 inlen = 0;
3383 else
3384 memset(retv, 0, inlen);
3385
3386 lxc_list_for_each(it, &c->keepcaps) {
3387 strprint(retv, inlen, "%s\n", (char *)it->elem);
3388 }
3389
3390 return fulllen;
3391 }
3392
3393 static int get_config_console_path(const char *key, char *retv, int inlen,
3394 struct lxc_conf *c, void *data)
3395 {
3396 return lxc_get_conf_str(retv, inlen, c->console.path);
3397 }
3398
3399 static int get_config_console_logfile(const char *key, char *retv, int inlen,
3400 struct lxc_conf *c, void *data)
3401 {
3402 return lxc_get_conf_str(retv, inlen, c->console.log_path);
3403 }
3404
3405 static int get_config_console_rotate(const char *key, char *retv, int inlen,
3406 struct lxc_conf *c, void *data)
3407 {
3408 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
3409 }
3410
3411
3412 static int get_config_console_buffer_size(const char *key, char *retv,
3413 int inlen, struct lxc_conf *c,
3414 void *data)
3415 {
3416 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
3417 }
3418
3419 static int get_config_console_buffer_logfile(const char *key, char *retv,
3420 int inlen, struct lxc_conf *c,
3421 void *data)
3422 {
3423 return lxc_get_conf_str(retv, inlen, c->console.buffer_log_file);
3424 }
3425
3426 static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
3427 struct lxc_conf *c, void *data)
3428 {
3429 return lxc_get_conf_str(retv, inlen, c->seccomp);
3430 }
3431
3432 static int get_config_autodev(const char *key, char *retv, int inlen,
3433 struct lxc_conf *c, void *data)
3434 {
3435 return lxc_get_conf_int(c, retv, inlen, c->autodev);
3436 }
3437
3438 static int get_config_signal_halt(const char *key, char *retv, int inlen,
3439 struct lxc_conf *c, void *data)
3440 {
3441 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
3442 }
3443
3444 static int get_config_signal_reboot(const char *key, char *retv, int inlen,
3445 struct lxc_conf *c, void *data)
3446 {
3447 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3448 }
3449
3450 static int get_config_signal_stop(const char *key, char *retv, int inlen,
3451 struct lxc_conf *c, void *data)
3452 {
3453 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
3454 }
3455
3456 static int get_config_start(const char *key, char *retv, int inlen,
3457 struct lxc_conf *c, void *data)
3458 {
3459 if (strcmp(key + 10, "auto") == 0)
3460 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
3461 else if (strcmp(key + 10, "delay") == 0)
3462 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
3463 else if (strcmp(key + 10, "order") == 0)
3464 return lxc_get_conf_int(c, retv, inlen, c->start_order);
3465
3466 return -1;
3467 }
3468
3469 static int get_config_log_syslog(const char *key, char *retv, int inlen,
3470 struct lxc_conf *c, void *data)
3471 {
3472 return lxc_get_conf_str(retv, inlen, c->syslog);
3473 }
3474
3475 static int get_config_monitor(const char *key, char *retv, int inlen,
3476 struct lxc_conf *c, void *data)
3477 {
3478 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
3479 }
3480
3481 static int get_config_group(const char *key, char *retv, int inlen,
3482 struct lxc_conf *c, void *data)
3483 {
3484 int len, fulllen = 0;
3485 struct lxc_list *it;
3486
3487 if (!retv)
3488 inlen = 0;
3489 else
3490 memset(retv, 0, inlen);
3491
3492 lxc_list_for_each(it, &c->groups) {
3493 strprint(retv, inlen, "%s\n", (char *)it->elem);
3494 }
3495
3496 return fulllen;
3497 }
3498
3499 static int get_config_environment(const char *key, char *retv, int inlen,
3500 struct lxc_conf *c, void *data)
3501 {
3502 int len, fulllen = 0;
3503 struct lxc_list *it;
3504
3505 if (!retv)
3506 inlen = 0;
3507 else
3508 memset(retv, 0, inlen);
3509
3510 lxc_list_for_each(it, &c->environment) {
3511 strprint(retv, inlen, "%s\n", (char *)it->elem);
3512 }
3513
3514 return fulllen;
3515 }
3516
3517 static int get_config_execute_cmd(const char *key, char *retv, int inlen,
3518 struct lxc_conf *c, void *data)
3519 {
3520 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
3521 }
3522
3523 static int get_config_init_cmd(const char *key, char *retv, int inlen,
3524 struct lxc_conf *c, void *data)
3525 {
3526 return lxc_get_conf_str(retv, inlen, c->init_cmd);
3527 }
3528
3529 static int get_config_init_cwd(const char *key, char *retv, int inlen,
3530 struct lxc_conf *c, void *data)
3531 {
3532 return lxc_get_conf_str(retv, inlen, c->init_cwd);
3533 }
3534
3535 static int get_config_init_uid(const char *key, char *retv, int inlen,
3536 struct lxc_conf *c, void *data)
3537 {
3538 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
3539 }
3540
3541 static int get_config_init_gid(const char *key, char *retv, int inlen,
3542 struct lxc_conf *c, void *data)
3543 {
3544 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
3545 }
3546
3547 static int get_config_ephemeral(const char *key, char *retv, int inlen,
3548 struct lxc_conf *c, void *data)
3549 {
3550 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
3551 }
3552
3553 static int get_config_no_new_privs(const char *key, char *retv, int inlen,
3554 struct lxc_conf *c, void *data)
3555 {
3556 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
3557 }
3558
3559 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3560 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3561 * printed, in 'lxc.prlimit.resource = value' format.
3562 */
3563 static int get_config_prlimit(const char *key, char *retv, int inlen,
3564 struct lxc_conf *c, void *data)
3565 {
3566 int fulllen = 0, len;
3567 bool get_all = false;
3568 struct lxc_list *it;
3569
3570 if (!retv)
3571 inlen = 0;
3572 else
3573 memset(retv, 0, inlen);
3574
3575 if (!strcmp(key, "lxc.prlimit"))
3576 get_all = true;
3577 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
3578 key += 12;
3579 else
3580 return -1;
3581
3582 lxc_list_for_each(it, &c->limits) {
3583 char buf[LXC_NUMSTRLEN64 * 2 + 2]; /* 2 colon separated 64 bit
3584 integers or the word
3585 'unlimited' */
3586 int partlen;
3587 struct lxc_limit *lim = it->elem;
3588
3589 if (lim->limit.rlim_cur == RLIM_INFINITY) {
3590 memcpy(buf, "unlimited", sizeof("unlimited"));
3591 partlen = sizeof("unlimited") - 1;
3592 } else {
3593 partlen = sprintf(buf, "%" PRIu64,
3594 (uint64_t)lim->limit.rlim_cur);
3595 }
3596 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
3597 if (lim->limit.rlim_max == RLIM_INFINITY)
3598 memcpy(buf + partlen, ":unlimited",
3599 sizeof(":unlimited"));
3600 else
3601 sprintf(buf + partlen, ":%" PRIu64,
3602 (uint64_t)lim->limit.rlim_max);
3603 }
3604
3605 if (get_all) {
3606 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
3607 lim->resource, buf);
3608 } else if (!strcmp(lim->resource, key)) {
3609 strprint(retv, inlen, "%s", buf);
3610 }
3611 }
3612
3613 return fulllen;
3614 }
3615
3616 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3617 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3618 * entries will be printed, in 'lxc.sysctl.key = value' format.
3619 */
3620 static int get_config_sysctl(const char *key, char *retv, int inlen,
3621 struct lxc_conf *c, void *data)
3622 {
3623 int len;
3624 struct lxc_list *it;
3625 int fulllen = 0;
3626 bool get_all = false;
3627
3628 if (!retv)
3629 inlen = 0;
3630 else
3631 memset(retv, 0, inlen);
3632
3633 if (strcmp(key, "lxc.sysctl") == 0)
3634 get_all = true;
3635 else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3636 key += sizeof("lxc.sysctl.") - 1;
3637 else
3638 return -1;
3639
3640 lxc_list_for_each(it, &c->sysctls) {
3641 struct lxc_sysctl *elem = it->elem;
3642 if (get_all) {
3643 strprint(retv, inlen, "lxc.sysctl.%s = %s\n",
3644 elem->key, elem->value);
3645 } else if (strcmp(elem->key, key) == 0) {
3646 strprint(retv, inlen, "%s", elem->value);
3647 }
3648 }
3649
3650 return fulllen;
3651 }
3652
3653 static int get_config_proc(const char *key, char *retv, int inlen,
3654 struct lxc_conf *c, void *data)
3655 {
3656 struct lxc_list *it;
3657 int len;
3658 int fulllen = 0;
3659 bool get_all = false;
3660
3661 if (!retv)
3662 inlen = 0;
3663 else
3664 memset(retv, 0, inlen);
3665
3666 if (strcmp(key, "lxc.proc") == 0)
3667 get_all = true;
3668 else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3669 key += sizeof("lxc.proc.") - 1;
3670 else
3671 return -1;
3672
3673 lxc_list_for_each(it, &c->procs) {
3674 struct lxc_proc *proc = it->elem;
3675
3676 if (get_all) {
3677 strprint(retv, inlen, "lxc.proc.%s = %s\n",
3678 proc->filename, proc->value);
3679 } else if (strcmp(proc->filename, key) == 0) {
3680 strprint(retv, inlen, "%s", proc->value);
3681 }
3682 }
3683
3684 return fulllen;
3685 }
3686
3687 static int get_config_noop(const char *key, char *retv, int inlen,
3688 struct lxc_conf *c, void *data)
3689 {
3690 return 0;
3691 }
3692
3693 static int get_config_namespace_clone(const char *key, char *retv, int inlen,
3694 struct lxc_conf *c, void *data)
3695 {
3696 int i, len;
3697 int fulllen = 0;
3698
3699 if (!retv)
3700 inlen = 0;
3701 else
3702 memset(retv, 0, inlen);
3703
3704 for (i = 0; i < LXC_NS_MAX; i++) {
3705 if (c->ns_clone & ns_info[i].clone_flag)
3706 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3707 }
3708
3709 return fulllen;
3710 }
3711
3712 static int get_config_namespace_keep(const char *key, char *retv, int inlen,
3713 struct lxc_conf *c, void *data)
3714 {
3715 int i, len;
3716 int fulllen = 0;
3717
3718 if (!retv)
3719 inlen = 0;
3720 else
3721 memset(retv, 0, inlen);
3722
3723 for (i = 0; i < LXC_NS_MAX; i++) {
3724 if (c->ns_keep & ns_info[i].clone_flag)
3725 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3726 }
3727
3728 return fulllen;
3729 }
3730
3731 static int get_config_namespace_share(const char *key, char *retv, int inlen,
3732 struct lxc_conf *c, void *data)
3733 {
3734 int len, ns_idx;
3735 const char *namespace;
3736 int fulllen = 0;
3737
3738 if (!retv)
3739 inlen = 0;
3740 else
3741 memset(retv, 0, inlen);
3742
3743 namespace = key + sizeof("lxc.namespace.share.") - 1;
3744 ns_idx = lxc_namespace_2_ns_idx(namespace);
3745 if (ns_idx < 0)
3746 return ns_idx;
3747
3748 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
3749
3750 return fulllen;
3751 }
3752
3753 /* Callbacks to clear config items. */
3754 static inline int clr_config_personality(const char *key, struct lxc_conf *c,
3755 void *data)
3756 {
3757 c->personality = -1;
3758 return 0;
3759 }
3760
3761 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
3762 void *data)
3763 {
3764 c->pts = 0;
3765 return 0;
3766 }
3767
3768 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
3769 void *data)
3770 {
3771 c->tty = 0;
3772 return 0;
3773 }
3774
3775 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
3776 void *data)
3777 {
3778 free(c->ttydir);
3779 c->ttydir = NULL;
3780 return 0;
3781 }
3782
3783 static inline int clr_config_apparmor_profile(const char *key,
3784 struct lxc_conf *c, void *data)
3785 {
3786 free(c->lsm_aa_profile);
3787 c->lsm_aa_profile = NULL;
3788 return 0;
3789 }
3790
3791 static inline int clr_config_apparmor_allow_incomplete(const char *key,
3792 struct lxc_conf *c,
3793 void *data)
3794 {
3795 c->lsm_aa_allow_incomplete = 0;
3796 return 0;
3797 }
3798
3799 static inline int clr_config_selinux_context(const char *key,
3800 struct lxc_conf *c, void *data)
3801 {
3802 free(c->lsm_se_context);
3803 c->lsm_se_context = NULL;
3804 return 0;
3805 }
3806
3807 static inline int clr_config_cgroup_controller(const char *key,
3808 struct lxc_conf *c, void *data)
3809 {
3810 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
3811 }
3812
3813 static inline int clr_config_cgroup2_controller(const char *key,
3814 struct lxc_conf *c, void *data)
3815 {
3816 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
3817 }
3818
3819 static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
3820 void *data)
3821 {
3822 if (lxc_conf->cgroup_meta.dir) {
3823 free(lxc_conf->cgroup_meta.dir);
3824 lxc_conf->cgroup_meta.dir = NULL;
3825 }
3826
3827 return 0;
3828 }
3829
3830 static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
3831 void *data)
3832 {
3833 return lxc_clear_idmaps(c);
3834 }
3835
3836 static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
3837 void *data)
3838 {
3839 c->loglevel = LXC_LOG_LEVEL_NOTSET;
3840 return 0;
3841 }
3842
3843 static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
3844 void *data)
3845 {
3846 free(c->logfile);
3847 c->logfile = NULL;
3848 return 0;
3849 }
3850
3851 static inline int clr_config_mount(const char *key, struct lxc_conf *c,
3852 void *data)
3853 {
3854 return lxc_clear_mount_entries(c);
3855 }
3856
3857 static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
3858 void *data)
3859 {
3860 return lxc_clear_automounts(c);
3861 }
3862
3863 static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
3864 void *data)
3865 {
3866 free(c->fstab);
3867 c->fstab = NULL;
3868 return 0;
3869 }
3870
3871 static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
3872 void *data)
3873 {
3874 free(c->rootfs.path);
3875 c->rootfs.path = NULL;
3876 return 0;
3877 }
3878
3879 static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
3880 void *data)
3881 {
3882 free(c->rootfs.mount);
3883 c->rootfs.mount = NULL;
3884 return 0;
3885 }
3886
3887 static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
3888 void *data)
3889 {
3890 free(c->rootfs.options);
3891 c->rootfs.options = NULL;
3892 return 0;
3893 }
3894
3895 static inline int clr_config_rootfs_backend(const char *key, struct lxc_conf *c,
3896 void *data)
3897 {
3898 return 0;
3899 }
3900
3901 static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
3902 void *data)
3903 {
3904 free(c->utsname);
3905 c->utsname = NULL;
3906 return 0;
3907 }
3908
3909 static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
3910 void *data)
3911 {
3912 return lxc_clear_hooks(c, key);
3913 }
3914
3915 static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
3916 void *data)
3917 {
3918 /* default to legacy hooks version */
3919 c->hooks_version = 0;
3920 return 0;
3921 }
3922
3923 static inline int clr_config_net(const char *key, struct lxc_conf *c,
3924 void *data)
3925 {
3926 lxc_free_networks(&c->network);
3927
3928 return 0;
3929 }
3930
3931 static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
3932 void *data)
3933 {
3934 return lxc_clear_config_caps(c);
3935 }
3936
3937 static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
3938 void *data)
3939 {
3940 return lxc_clear_config_keepcaps(c);
3941 }
3942
3943 static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
3944 void *data)
3945 {
3946 free(c->console.path);
3947 c->console.path = NULL;
3948 return 0;
3949 }
3950
3951 static inline int clr_config_console_logfile(const char *key,
3952 struct lxc_conf *c, void *data)
3953 {
3954 free(c->console.log_path);
3955 c->console.log_path = NULL;
3956 return 0;
3957 }
3958
3959 static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
3960 void *data)
3961 {
3962 c->console.log_rotate = 0;
3963 return 0;
3964 }
3965
3966 static inline int clr_config_console_buffer_size(const char *key,
3967 struct lxc_conf *c, void *data)
3968 {
3969 c->console.buffer_size = 0;
3970 return 0;
3971 }
3972
3973 static inline int clr_config_console_buffer_logfile(const char *key,
3974 struct lxc_conf *c,
3975 void *data)
3976 {
3977 free(c->console.buffer_log_file);
3978 c->console.buffer_log_file = NULL;
3979 return 0;
3980 }
3981
3982 static inline int clr_config_seccomp_profile(const char *key,
3983 struct lxc_conf *c, void *data)
3984 {
3985 free(c->seccomp);
3986 c->seccomp = NULL;
3987 return 0;
3988 }
3989
3990 static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
3991 void *data)
3992 {
3993 c->autodev = 1;
3994 return 0;
3995 }
3996
3997 static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
3998 void *data)
3999 {
4000 c->haltsignal = 0;
4001 return 0;
4002 }
4003
4004 static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
4005 void *data)
4006 {
4007 c->rebootsignal = 0;
4008 return 0;
4009 }
4010
4011 static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
4012 void *data)
4013 {
4014 c->stopsignal = 0;
4015 return 0;
4016 }
4017
4018 static inline int clr_config_start(const char *key, struct lxc_conf *c,
4019 void *data)
4020 {
4021 if (strcmp(key + 10, "auto") == 0)
4022 c->start_auto = 0;
4023 else if (strcmp(key + 10, "delay") == 0)
4024 c->start_delay = 0;
4025 else if (strcmp(key + 10, "order") == 0)
4026 c->start_order = 0;
4027
4028 return 0;
4029 }
4030
4031 static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
4032 void *data)
4033 {
4034 free(c->syslog);
4035 c->syslog = NULL;
4036 return 0;
4037 }
4038
4039 static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
4040 void *data)
4041 {
4042 c->monitor_unshare = 0;
4043 return 0;
4044 }
4045
4046 static inline int clr_config_group(const char *key, struct lxc_conf *c,
4047 void *data)
4048 {
4049 return lxc_clear_groups(c);
4050 }
4051
4052 static inline int clr_config_environment(const char *key, struct lxc_conf *c,
4053 void *data)
4054 {
4055 return lxc_clear_environment(c);
4056 }
4057
4058 static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
4059 void *data)
4060 {
4061 free(c->execute_cmd);
4062 c->execute_cmd = NULL;
4063 return 0;
4064 }
4065
4066 static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
4067 void *data)
4068 {
4069 free(c->init_cmd);
4070 c->init_cmd = NULL;
4071 return 0;
4072 }
4073
4074 static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4075 void *data)
4076 {
4077 free(c->init_cwd);
4078 c->init_cwd = NULL;
4079 return 0;
4080 }
4081
4082 static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4083 void *data)
4084 {
4085 c->init_uid = 0;
4086 return 0;
4087 }
4088
4089 static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4090 void *data)
4091 {
4092 c->init_gid = 0;
4093 return 0;
4094 }
4095
4096 static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4097 void *data)
4098 {
4099 c->ephemeral = 0;
4100 return 0;
4101 }
4102
4103 static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
4104 void *data)
4105 {
4106 c->no_new_privs = false;
4107 return 0;
4108 }
4109
4110 static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
4111 void *data)
4112 {
4113 return lxc_clear_limits(c, key);
4114 }
4115
4116 static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
4117 void *data)
4118 {
4119 return lxc_clear_sysctls(c, key);
4120 }
4121
4122 static inline int clr_config_proc(const char *key, struct lxc_conf *c,
4123 void *data)
4124 {
4125 return lxc_clear_procs(c, key);
4126 }
4127
4128 static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
4129 void *data)
4130 {
4131 lxc_clear_includes(c);
4132 return 0;
4133 }
4134
4135 static inline int clr_config_noop(const char *key, struct lxc_conf *c,
4136 void *data)
4137 {
4138 return 0;
4139 }
4140
4141 static int clr_config_namespace_clone(const char *key,
4142 struct lxc_conf *lxc_conf, void *data)
4143 {
4144 lxc_conf->ns_clone = 0;
4145 return 0;
4146 }
4147
4148 static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
4149 void *data)
4150 {
4151 lxc_conf->ns_keep = 0;
4152 return 0;
4153 }
4154
4155 static int clr_config_namespace_share(const char *key,
4156 struct lxc_conf *lxc_conf, void *data)
4157 {
4158 int ns_idx;
4159 const char *namespace;
4160
4161 namespace = key + sizeof("lxc.namespace.share.") - 1;
4162 ns_idx = lxc_namespace_2_ns_idx(namespace);
4163 if (ns_idx < 0)
4164 return ns_idx;
4165
4166 free(lxc_conf->ns_share[ns_idx]);
4167 lxc_conf->ns_share[ns_idx] = NULL;
4168
4169 return 0;
4170 }
4171
4172 static int get_config_includefiles(const char *key, char *retv, int inlen,
4173 struct lxc_conf *c, void *data)
4174 {
4175 return -ENOSYS;
4176 }
4177
4178 static struct lxc_config_t *get_network_config_ops(const char *key,
4179 struct lxc_conf *lxc_conf,
4180 ssize_t *idx,
4181 char **deindexed_key)
4182 {
4183 int ret;
4184 unsigned int tmpidx;
4185 size_t numstrlen;
4186 char *copy, *idx_start, *idx_end;
4187 struct lxc_config_t *config = NULL;
4188
4189 /* check that this is a sensible network key */
4190 if (strncmp("lxc.net.", key, 8)) {
4191 ERROR("Invalid network configuration key \"%s\"", key);
4192 return NULL;
4193 }
4194
4195 copy = strdup(key);
4196 if (!copy) {
4197 ERROR("Failed to duplicate string \"%s\"", key);
4198 return NULL;
4199 }
4200
4201 /* lxc.net.<n> */
4202 if (!isdigit(*(key + 8))) {
4203 ERROR("Failed to detect digit in string \"%s\"", key + 8);
4204 goto on_error;
4205 }
4206
4207 /* beginning of index string */
4208 idx_start = (copy + 7);
4209 *idx_start = '\0';
4210
4211 /* end of index string */
4212 idx_end = strchr((copy + 8), '.');
4213 if (idx_end)
4214 *idx_end = '\0';
4215
4216 /* parse current index */
4217 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
4218 if (ret < 0) {
4219 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4220 idx_start + 1, strerror(-ret));
4221 *idx = ret;
4222 goto on_error;
4223 }
4224
4225 /* This, of course is utterly nonsensical on so many levels, but
4226 * better safe than sorry.
4227 * (Checking for INT_MAX here is intentional.)
4228 */
4229 if (tmpidx == INT_MAX) {
4230 SYSERROR("Number of configured networks would overflow the "
4231 "counter");
4232 goto on_error;
4233 }
4234 *idx = tmpidx;
4235
4236 numstrlen = strlen((idx_start + 1));
4237
4238 /* repair configuration key */
4239 *idx_start = '.';
4240
4241 /* lxc.net.<idx>.<subkey> */
4242 if (idx_end) {
4243 *idx_end = '.';
4244 if (strlen(idx_end + 1) == 0) {
4245 ERROR("No subkey in network configuration key \"%s\"", key);
4246 goto on_error;
4247 }
4248
4249 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
4250 copy[strlen(key) - numstrlen + 1] = '\0';
4251
4252 config = lxc_get_config(copy);
4253 if (!config) {
4254 ERROR("Unknown network configuration key \"%s\"", key);
4255 goto on_error;
4256 }
4257 }
4258
4259 if (deindexed_key)
4260 *deindexed_key = copy;
4261
4262 return config;
4263
4264 on_error:
4265 free(copy);
4266 return NULL;
4267 }
4268
4269 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4270 * found. So we make sure next comes an integer, find the right callback (by
4271 * rewriting the key), and call it.
4272 */
4273 static int set_config_net_nic(const char *key, const char *value,
4274 struct lxc_conf *lxc_conf, void *data)
4275 {
4276 int ret;
4277 const char *idxstring;
4278 struct lxc_config_t *config;
4279 struct lxc_netdev *netdev;
4280 ssize_t idx = -1;
4281 char *deindexed_key = NULL;
4282
4283 idxstring = key + 8;
4284 if (!isdigit(*idxstring))
4285 return -1;
4286
4287 if (lxc_config_value_empty(value))
4288 return clr_config_net_nic(key, lxc_conf, data);
4289
4290 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4291 if (!config || idx < 0)
4292 return -1;
4293
4294 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
4295 if (!netdev) {
4296 free(deindexed_key);
4297 return -1;
4298 }
4299
4300 ret = config->set(deindexed_key, value, lxc_conf, netdev);
4301 free(deindexed_key);
4302 return ret;
4303 }
4304
4305 static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
4306 void *data)
4307 {
4308 int ret;
4309 const char *idxstring;
4310 struct lxc_config_t *config;
4311 struct lxc_netdev *netdev;
4312 ssize_t idx = -1;
4313 char *deindexed_key = NULL;
4314
4315 idxstring = key + 8;
4316 if (!isdigit(*idxstring))
4317 return -1;
4318
4319 /* The left conjunct is pretty self-explanatory. The right conjunct
4320 * checks whether the two pointers are equal. If they are we know that
4321 * this is not a key that is namespaced any further and so we are
4322 * supposed to clear the whole network.
4323 */
4324 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
4325 unsigned int rmnetdevidx;
4326
4327 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
4328 return -1;
4329
4330 /* Remove network from network list. */
4331 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
4332 return 0;
4333 }
4334
4335 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4336 if (!config || idx < 0)
4337 return -1;
4338
4339 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
4340 if (!netdev) {
4341 free(deindexed_key);
4342 return -1;
4343 }
4344
4345 ret = config->clr(deindexed_key, lxc_conf, netdev);
4346 free(deindexed_key);
4347 return ret;
4348 }
4349
4350 static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
4351 void *data)
4352 {
4353 struct lxc_netdev *netdev = data;
4354
4355 if (!netdev)
4356 return -1;
4357
4358 netdev->type = -1;
4359
4360 return 0;
4361 }
4362
4363 static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
4364 void *data)
4365 {
4366 struct lxc_netdev *netdev = data;
4367
4368 if (!netdev)
4369 return -1;
4370
4371 netdev->name[0] = '\0';
4372
4373 return 0;
4374 }
4375
4376 static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
4377 void *data)
4378 {
4379 struct lxc_netdev *netdev = data;
4380
4381 if (!netdev)
4382 return -1;
4383
4384 netdev->flags = 0;
4385
4386 return 0;
4387 }
4388
4389 static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
4390 void *data)
4391 {
4392 struct lxc_netdev *netdev = data;
4393
4394 if (!netdev)
4395 return -1;
4396
4397 netdev->link[0] = '\0';
4398
4399 return 0;
4400 }
4401
4402 static int clr_config_net_macvlan_mode(const char *key,
4403 struct lxc_conf *lxc_conf, void *data)
4404 {
4405 struct lxc_netdev *netdev = data;
4406
4407 if (!netdev)
4408 return -1;
4409
4410 if (netdev->type != LXC_NET_MACVLAN)
4411 return 0;
4412
4413 netdev->priv.macvlan_attr.mode = -1;
4414
4415 return 0;
4416 }
4417
4418 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
4419 void *data)
4420 {
4421 struct lxc_netdev *netdev = data;
4422
4423 if (!netdev)
4424 return -1;
4425
4426 netdev->priv.veth_attr.pair[0] = '\0';
4427
4428 return 0;
4429 }
4430
4431 static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
4432 void *data)
4433 {
4434 struct lxc_netdev *netdev = data;
4435
4436 if (!netdev)
4437 return -1;
4438
4439 free(netdev->upscript);
4440 netdev->upscript = NULL;
4441
4442 return 0;
4443 }
4444
4445 static int clr_config_net_script_down(const char *key,
4446 struct lxc_conf *lxc_conf, void *data)
4447 {
4448 struct lxc_netdev *netdev = data;
4449
4450 if (!netdev)
4451 return -1;
4452
4453 free(netdev->downscript);
4454 netdev->downscript = NULL;
4455
4456 return 0;
4457 }
4458
4459 static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
4460 void *data)
4461 {
4462 struct lxc_netdev *netdev = data;
4463
4464 if (!netdev)
4465 return -1;
4466
4467 free(netdev->hwaddr);
4468 netdev->hwaddr = NULL;
4469
4470 return 0;
4471 }
4472
4473 static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
4474 void *data)
4475 {
4476 struct lxc_netdev *netdev = data;
4477
4478 if (!netdev)
4479 return -1;
4480
4481 free(netdev->mtu);
4482 netdev->mtu = NULL;
4483
4484 return 0;
4485 }
4486
4487 static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
4488 void *data)
4489 {
4490 struct lxc_netdev *netdev = data;
4491
4492 if (!netdev)
4493 return -1;
4494
4495 netdev->priv.vlan_attr.vid = 0;
4496
4497 return 0;
4498 }
4499
4500 static int clr_config_net_ipv4_gateway(const char *key,
4501 struct lxc_conf *lxc_conf, void *data)
4502 {
4503 struct lxc_netdev *netdev = data;
4504
4505 if (!netdev)
4506 return -1;
4507
4508 free(netdev->ipv4_gateway);
4509 netdev->ipv4_gateway = NULL;
4510
4511 return 0;
4512 }
4513
4514 static int clr_config_net_ipv4_address(const char *key,
4515 struct lxc_conf *lxc_conf, void *data)
4516 {
4517 struct lxc_netdev *netdev = data;
4518 struct lxc_list *cur, *next;
4519
4520 if (!netdev)
4521 return -1;
4522
4523 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
4524 lxc_list_del(cur);
4525 free(cur->elem);
4526 free(cur);
4527 }
4528
4529 return 0;
4530 }
4531
4532 static int clr_config_net_ipv6_gateway(const char *key,
4533 struct lxc_conf *lxc_conf, void *data)
4534 {
4535 struct lxc_netdev *netdev = data;
4536
4537 if (!netdev)
4538 return -1;
4539
4540 free(netdev->ipv6_gateway);
4541 netdev->ipv6_gateway = NULL;
4542
4543 return 0;
4544 }
4545
4546 static int clr_config_net_ipv6_address(const char *key,
4547 struct lxc_conf *lxc_conf, void *data)
4548 {
4549 struct lxc_netdev *netdev = data;
4550 struct lxc_list *cur, *next;
4551
4552 if (!netdev)
4553 return -1;
4554
4555 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
4556 lxc_list_del(cur);
4557 free(cur->elem);
4558 free(cur);
4559 }
4560
4561 return 0;
4562 }
4563
4564 static int get_config_net_nic(const char *key, char *retv, int inlen,
4565 struct lxc_conf *c, void *data)
4566 {
4567 int ret;
4568 const char *idxstring;
4569 struct lxc_config_t *config;
4570 struct lxc_netdev *netdev;
4571 ssize_t idx = -1;
4572 char *deindexed_key = NULL;
4573
4574 idxstring = key + 8;
4575 if (!isdigit(*idxstring))
4576 return -1;
4577
4578 config = get_network_config_ops(key, c, &idx, &deindexed_key);
4579 if (!config || idx < 0)
4580 return -1;
4581
4582 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4583 if (!netdev) {
4584 free(deindexed_key);
4585 return -1;
4586 }
4587
4588 ret = config->get(deindexed_key, retv, inlen, c, netdev);
4589 free(deindexed_key);
4590 return ret;
4591 }
4592
4593 static int get_config_net_type(const char *key, char *retv, int inlen,
4594 struct lxc_conf *c, void *data)
4595 {
4596 int len;
4597 int fulllen = 0;
4598 struct lxc_netdev *netdev = data;
4599
4600 if (!retv)
4601 inlen = 0;
4602 else
4603 memset(retv, 0, inlen);
4604
4605 if (!netdev)
4606 return -1;
4607
4608 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
4609
4610 return fulllen;
4611 }
4612
4613 static int get_config_net_flags(const char *key, char *retv, int inlen,
4614 struct lxc_conf *c, void *data)
4615 {
4616 int len;
4617 int fulllen = 0;
4618 struct lxc_netdev *netdev = data;
4619
4620 if (!retv)
4621 inlen = 0;
4622 else
4623 memset(retv, 0, inlen);
4624
4625 if (!netdev)
4626 return -1;
4627
4628 if (netdev->flags & IFF_UP)
4629 strprint(retv, inlen, "up");
4630
4631 return fulllen;
4632 }
4633
4634 static int get_config_net_link(const char *key, char *retv, int inlen,
4635 struct lxc_conf *c, void *data)
4636 {
4637 int len;
4638 int fulllen = 0;
4639 struct lxc_netdev *netdev = data;
4640
4641 if (!retv)
4642 inlen = 0;
4643 else
4644 memset(retv, 0, inlen);
4645
4646 if (!netdev)
4647 return -1;
4648
4649 if (netdev->link[0] != '\0')
4650 strprint(retv, inlen, "%s", netdev->link);
4651
4652 return fulllen;
4653 }
4654
4655 static int get_config_net_name(const char *key, char *retv, int inlen,
4656 struct lxc_conf *c, void *data)
4657 {
4658 int len;
4659 int fulllen = 0;
4660 struct lxc_netdev *netdev = data;
4661
4662 if (!retv)
4663 inlen = 0;
4664 else
4665 memset(retv, 0, inlen);
4666
4667 if (!netdev)
4668 return -1;
4669
4670 if (netdev->name[0] != '\0')
4671 strprint(retv, inlen, "%s", netdev->name);
4672
4673 return fulllen;
4674 }
4675
4676 static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
4677 struct lxc_conf *c, void *data)
4678 {
4679 int len;
4680 int fulllen = 0;
4681 const char *mode;
4682 struct lxc_netdev *netdev = data;
4683
4684 if (!retv)
4685 inlen = 0;
4686 else
4687 memset(retv, 0, inlen);
4688
4689 if (!netdev)
4690 return -1;
4691
4692 if (netdev->type != LXC_NET_MACVLAN)
4693 return 0;
4694
4695 switch (netdev->priv.macvlan_attr.mode) {
4696 case MACVLAN_MODE_PRIVATE:
4697 mode = "private";
4698 break;
4699 case MACVLAN_MODE_VEPA:
4700 mode = "vepa";
4701 break;
4702 case MACVLAN_MODE_BRIDGE:
4703 mode = "bridge";
4704 break;
4705 case MACVLAN_MODE_PASSTHRU:
4706 mode = "passthru";
4707 break;
4708 default:
4709 mode = "(invalid)";
4710 break;
4711 }
4712
4713 strprint(retv, inlen, "%s", mode);
4714
4715 return fulllen;
4716 }
4717
4718 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
4719 struct lxc_conf *c, void *data)
4720 {
4721 int len;
4722 int fulllen = 0;
4723 struct lxc_netdev *netdev = data;
4724
4725 if (!retv)
4726 inlen = 0;
4727 else
4728 memset(retv, 0, inlen);
4729
4730 if (!netdev)
4731 return -1;
4732
4733 if (netdev->type != LXC_NET_VETH)
4734 return 0;
4735
4736 strprint(retv, inlen, "%s",
4737 netdev->priv.veth_attr.pair[0] != '\0'
4738 ? netdev->priv.veth_attr.pair
4739 : netdev->priv.veth_attr.veth1);
4740
4741 return fulllen;
4742 }
4743
4744 static int get_config_net_script_up(const char *key, char *retv, int inlen,
4745 struct lxc_conf *c, void *data)
4746 {
4747 int len;
4748 int fulllen = 0;
4749 struct lxc_netdev *netdev = data;
4750
4751 if (!retv)
4752 inlen = 0;
4753 else
4754 memset(retv, 0, inlen);
4755
4756 if (!netdev)
4757 return -1;
4758
4759 if (netdev->upscript)
4760 strprint(retv, inlen, "%s", netdev->upscript);
4761
4762 return fulllen;
4763 }
4764
4765 static int get_config_net_script_down(const char *key, char *retv, int inlen,
4766 struct lxc_conf *c, void *data)
4767 {
4768 int len;
4769 int fulllen = 0;
4770 struct lxc_netdev *netdev = data;
4771
4772 if (!retv)
4773 inlen = 0;
4774 else
4775 memset(retv, 0, inlen);
4776
4777 if (!netdev)
4778 return -1;
4779
4780 if (netdev->downscript)
4781 strprint(retv, inlen, "%s", netdev->downscript);
4782
4783 return fulllen;
4784 }
4785
4786 static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
4787 struct lxc_conf *c, void *data)
4788 {
4789 int len;
4790 int fulllen = 0;
4791 struct lxc_netdev *netdev = data;
4792
4793 if (!retv)
4794 inlen = 0;
4795 else
4796 memset(retv, 0, inlen);
4797
4798 if (!netdev)
4799 return -1;
4800
4801 if (netdev->hwaddr)
4802 strprint(retv, inlen, "%s", netdev->hwaddr);
4803
4804 return fulllen;
4805 }
4806
4807 static int get_config_net_mtu(const char *key, char *retv, int inlen,
4808 struct lxc_conf *c, void *data)
4809 {
4810 int len;
4811 int fulllen = 0;
4812 struct lxc_netdev *netdev = data;
4813
4814 if (!retv)
4815 inlen = 0;
4816 else
4817 memset(retv, 0, inlen);
4818
4819 if (!netdev)
4820 return -1;
4821
4822 if (netdev->mtu)
4823 strprint(retv, inlen, "%s", netdev->mtu);
4824
4825 return fulllen;
4826 }
4827
4828 static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
4829 struct lxc_conf *c, void *data)
4830 {
4831 int len;
4832 int fulllen = 0;
4833 struct lxc_netdev *netdev = data;
4834
4835 if (!retv)
4836 inlen = 0;
4837 else
4838 memset(retv, 0, inlen);
4839
4840 if (!netdev)
4841 return -1;
4842
4843 if (netdev->type != LXC_NET_VLAN)
4844 return 0;
4845
4846 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
4847
4848 return fulllen;
4849 }
4850
4851 static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
4852 struct lxc_conf *c, void *data)
4853 {
4854 int len;
4855 char buf[INET_ADDRSTRLEN];
4856 int fulllen = 0;
4857 struct lxc_netdev *netdev = data;
4858
4859 if (!retv)
4860 inlen = 0;
4861 else
4862 memset(retv, 0, inlen);
4863
4864 if (!netdev)
4865 return -1;
4866
4867 if (netdev->ipv4_gateway_auto) {
4868 strprint(retv, inlen, "auto");
4869 } else if (netdev->ipv4_gateway) {
4870 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
4871 strprint(retv, inlen, "%s", buf);
4872 }
4873
4874 return fulllen;
4875 }
4876
4877 static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
4878 struct lxc_conf *c, void *data)
4879 {
4880 int len;
4881 size_t listlen;
4882 char buf[INET_ADDRSTRLEN];
4883 struct lxc_list *it;
4884 int fulllen = 0;
4885 struct lxc_netdev *netdev = data;
4886
4887 if (!retv)
4888 inlen = 0;
4889 else
4890 memset(retv, 0, inlen);
4891
4892 if (!netdev)
4893 return -1;
4894
4895 listlen = lxc_list_len(&netdev->ipv4);
4896 lxc_list_for_each(it, &netdev->ipv4) {
4897 struct lxc_inetdev *i = it->elem;
4898 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
4899 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4900 (listlen-- > 1) ? "\n" : "");
4901 }
4902
4903 return fulllen;
4904 }
4905
4906 static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
4907 struct lxc_conf *c, void *data)
4908 {
4909 int len;
4910 char buf[INET6_ADDRSTRLEN];
4911 int fulllen = 0;
4912 struct lxc_netdev *netdev = data;
4913
4914 if (!retv)
4915 inlen = 0;
4916 else
4917 memset(retv, 0, inlen);
4918
4919 if (!netdev)
4920 return -1;
4921
4922 if (netdev->ipv6_gateway_auto) {
4923 strprint(retv, inlen, "auto");
4924 } else if (netdev->ipv6_gateway) {
4925 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
4926 strprint(retv, inlen, "%s", buf);
4927 }
4928
4929 return fulllen;
4930 }
4931
4932 static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
4933 struct lxc_conf *c, void *data)
4934 {
4935 int len;
4936 size_t listlen;
4937 char buf[INET6_ADDRSTRLEN];
4938 struct lxc_list *it;
4939 int fulllen = 0;
4940 struct lxc_netdev *netdev = data;
4941
4942 if (!retv)
4943 inlen = 0;
4944 else
4945 memset(retv, 0, inlen);
4946
4947 if (!netdev)
4948 return -1;
4949
4950 listlen = lxc_list_len(&netdev->ipv6);
4951 lxc_list_for_each(it, &netdev->ipv6) {
4952 struct lxc_inet6dev *i = it->elem;
4953 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
4954 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4955 (listlen-- > 1) ? "\n" : "");
4956 }
4957
4958 return fulllen;
4959 }
4960
4961 int lxc_list_config_items(char *retv, int inlen)
4962 {
4963 size_t i;
4964 int len;
4965 int fulllen = 0;
4966
4967 if (!retv)
4968 inlen = 0;
4969 else
4970 memset(retv, 0, inlen);
4971
4972 for (i = 0; i < config_size; i++) {
4973 char *s = config[i].name;
4974
4975 if (s[strlen(s) - 1] == '.')
4976 continue;
4977
4978 strprint(retv, inlen, "%s\n", s);
4979 }
4980
4981 return fulllen;
4982 }
4983
4984 int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
4985 int inlen)
4986 {
4987 int len;
4988 int fulllen = 0;
4989
4990 if (!retv)
4991 inlen = 0;
4992 else
4993 memset(retv, 0, inlen);
4994
4995 if (!strcmp(key, "lxc.apparmor")) {
4996 strprint(retv, inlen, "allow_incomplete\n");
4997 strprint(retv, inlen, "profile\n");
4998 } else if (!strcmp(key, "lxc.cgroup")) {
4999 strprint(retv, inlen, "dir\n");
5000 } else if (!strcmp(key, "lxc.selinux")) {
5001 strprint(retv, inlen, "context\n");
5002 } else if (!strcmp(key, "lxc.mount")) {
5003 strprint(retv, inlen, "auto\n");
5004 strprint(retv, inlen, "entry\n");
5005 strprint(retv, inlen, "fstab\n");
5006 } else if (!strcmp(key, "lxc.rootfs")) {
5007 strprint(retv, inlen, "mount\n");
5008 strprint(retv, inlen, "options\n");
5009 strprint(retv, inlen, "path\n");
5010 } else if (!strcmp(key, "lxc.uts")) {
5011 strprint(retv, inlen, "name\n");
5012 } else if (!strcmp(key, "lxc.hook")) {
5013 strprint(retv, inlen, "autodev\n");
5014 strprint(retv, inlen, "clone\n");
5015 strprint(retv, inlen, "destroy\n");
5016 strprint(retv, inlen, "mount\n");
5017 strprint(retv, inlen, "post-stop\n");
5018 strprint(retv, inlen, "pre-mount\n");
5019 strprint(retv, inlen, "pre-start\n");
5020 strprint(retv, inlen, "start-host\n");
5021 strprint(retv, inlen, "start\n");
5022 strprint(retv, inlen, "stop\n");
5023 } else if (!strcmp(key, "lxc.cap")) {
5024 strprint(retv, inlen, "drop\n");
5025 strprint(retv, inlen, "keep\n");
5026 } else if (!strcmp(key, "lxc.console")) {
5027 strprint(retv, inlen, "logfile\n");
5028 strprint(retv, inlen, "path\n");
5029 } else if (!strcmp(key, "lxc.seccomp")) {
5030 strprint(retv, inlen, "profile\n");
5031 } else if (!strcmp(key, "lxc.signal")) {
5032 strprint(retv, inlen, "halt\n");
5033 strprint(retv, inlen, "reboot\n");
5034 strprint(retv, inlen, "stop\n");
5035 } else if (!strcmp(key, "lxc.start")) {
5036 strprint(retv, inlen, "auto\n");
5037 strprint(retv, inlen, "delay\n");
5038 strprint(retv, inlen, "order\n");
5039 } else if (!strcmp(key, "lxc.monitor")) {
5040 strprint(retv, inlen, "unshare\n");
5041 } else {
5042 fulllen = -1;
5043 }
5044
5045 return fulllen;
5046 }
5047
5048 int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
5049 {
5050 int len;
5051 const char *idxstring;
5052 struct lxc_netdev *netdev;
5053 int fulllen = 0;
5054 ssize_t idx = -1;
5055
5056 idxstring = key + 8;
5057 if (!isdigit(*idxstring))
5058 return -1;
5059
5060 (void)get_network_config_ops(key, c, &idx, NULL);
5061 if (idx < 0)
5062 return -1;
5063
5064 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
5065 if (!netdev)
5066 return -1;
5067
5068 if (!retv)
5069 inlen = 0;
5070 else
5071 memset(retv, 0, inlen);
5072
5073 strprint(retv, inlen, "type\n");
5074 strprint(retv, inlen, "script.up\n");
5075 strprint(retv, inlen, "script.down\n");
5076 if (netdev->type != LXC_NET_EMPTY) {
5077 strprint(retv, inlen, "flags\n");
5078 strprint(retv, inlen, "link\n");
5079 strprint(retv, inlen, "name\n");
5080 strprint(retv, inlen, "hwaddr\n");
5081 strprint(retv, inlen, "mtu\n");
5082 strprint(retv, inlen, "ipv6.address\n");
5083 strprint(retv, inlen, "ipv6.gateway\n");
5084 strprint(retv, inlen, "ipv4.address\n");
5085 strprint(retv, inlen, "ipv4.gateway\n");
5086 }
5087
5088 switch (netdev->type) {
5089 case LXC_NET_VETH:
5090 strprint(retv, inlen, "veth.pair\n");
5091 break;
5092 case LXC_NET_MACVLAN:
5093 strprint(retv, inlen, "macvlan.mode\n");
5094 break;
5095 case LXC_NET_VLAN:
5096 strprint(retv, inlen, "vlan.id\n");
5097 break;
5098 case LXC_NET_PHYS:
5099 break;
5100 }
5101
5102 return fulllen;
5103 }