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