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