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