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