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