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