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