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