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