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