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