]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/conf.c
ubuntu: Tweak architecture support
[mirror_lxc.git] / src / lxc / conf.c
CommitLineData
0ad19a3f 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>
0ad19a3f 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
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23#define _GNU_SOURCE
24#include <stdio.h>
25#undef _GNU_SOURCE
26#include <stdlib.h>
e3b4c4c4 27#include <stdarg.h>
0ad19a3f 28#include <errno.h>
29#include <string.h>
30#include <dirent.h>
0ad19a3f 31#include <unistd.h>
e3b4c4c4 32#include <sys/wait.h>
2d76d1d7 33#include <sys/syscall.h>
e827ff7e
SG
34
35#if HAVE_PTY_H
b0a33c1e 36#include <pty.h>
e827ff7e
SG
37#else
38#include <../include/openpty.h>
39#endif
0ad19a3f 40
b3ecde1e
DL
41#include <linux/loop.h>
42
0ad19a3f 43#include <sys/types.h>
44#include <sys/utsname.h>
45#include <sys/param.h>
46#include <sys/stat.h>
47#include <sys/socket.h>
48#include <sys/mount.h>
49#include <sys/mman.h>
81810dd1 50#include <sys/prctl.h>
0ad19a3f 51
52#include <arpa/inet.h>
53#include <fcntl.h>
54#include <netinet/in.h>
55#include <net/if.h>
6f4a3756 56#include <libgen.h>
0ad19a3f 57
e5bda9ee 58#include "network.h"
59#include "error.h"
b2718c72 60#include "parse.h"
881450bb 61#include "config.h"
1b09f2c0
DL
62#include "utils.h"
63#include "conf.h"
64#include "log.h"
65#include "lxc.h" /* for lxc_cgroup_set() */
d55bc1ad 66#include "caps.h" /* for lxc_caps_last_cap() */
36eb9bde 67
d0a36f2c
SG
68#if HAVE_APPARMOR
69#include <apparmor.h>
70#endif
71
495d2046
SG
72#if HAVE_SYS_CAPABILITY_H
73#include <sys/capability.h>
74#endif
75
6ff05e18
SG
76#if HAVE_SYS_PERSONALITY_H
77#include <sys/personality.h>
78#endif
79
edaf8b1b
SG
80#if IS_BIONIC
81#include <../include/lxcmntent.h>
82#else
83#include <mntent.h>
84#endif
85
769872f9
SH
86#include "lxcseccomp.h"
87
36eb9bde 88lxc_log_define(lxc_conf, lxc);
e5bda9ee 89
0ad19a3f 90#define MAXHWLEN 18
91#define MAXINDEXLEN 20
442cbbe6 92#define MAXMTULEN 16
0ad19a3f 93#define MAXLINELEN 128
94
968fbd36
SK
95#ifndef MS_DIRSYNC
96#define MS_DIRSYNC 128
97#endif
98
fdc03323
DL
99#ifndef MS_REC
100#define MS_REC 16384
101#endif
102
c08556c6
DL
103#ifndef MNT_DETACH
104#define MNT_DETACH 2
105#endif
106
859a6da0
NC
107#ifndef MS_SLAVE
108#define MS_SLAVE (1<<19)
109#endif
110
88d413d5
SW
111#ifndef MS_RELATIME
112#define MS_RELATIME (1 << 21)
113#endif
114
115#ifndef MS_STRICTATIME
116#define MS_STRICTATIME (1 << 24)
117#endif
118
495d2046 119#if HAVE_SYS_CAPABILITY_H
b09094da
MN
120#ifndef CAP_SETFCAP
121#define CAP_SETFCAP 31
122#endif
123
124#ifndef CAP_MAC_OVERRIDE
125#define CAP_MAC_OVERRIDE 32
126#endif
127
128#ifndef CAP_MAC_ADMIN
129#define CAP_MAC_ADMIN 33
130#endif
495d2046 131#endif
b09094da
MN
132
133#ifndef PR_CAPBSET_DROP
134#define PR_CAPBSET_DROP 24
135#endif
136
9818cae4
SG
137#ifndef LO_FLAGS_AUTOCLEAR
138#define LO_FLAGS_AUTOCLEAR 4
139#endif
140
2d76d1d7
SG
141/* Define pivot_root() if missing from the C library */
142#ifndef HAVE_PIVOT_ROOT
143static int pivot_root(const char * new_root, const char * put_old)
144{
145#ifdef __NR_pivot_root
146return syscall(__NR_pivot_root, new_root, put_old);
147#else
148errno = ENOSYS;
149return -1;
150#endif
151}
152#else
153extern int pivot_root(const char * new_root, const char * put_old);
154#endif
155
156/* Define sethostname() if missing from the C library */
157#ifndef HAVE_SETHOSTNAME
158static int sethostname(const char * name, size_t len)
159{
160#ifdef __NR_sethostname
161return syscall(__NR_sethostname, name, len);
162#else
163errno = ENOSYS;
164return -1;
165#endif
166}
167#endif
168
72f919c4
SG
169/* Define __S_ISTYPE if missing from the C library */
170#ifndef __S_ISTYPE
171#define __S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
172#endif
173
72d0e1cb 174char *lxchook_names[NUM_LXC_HOOKS] = {
f7bee6c6 175 "pre-start", "pre-mount", "mount", "autodev", "start", "post-stop" };
72d0e1cb 176
e3b4c4c4 177typedef int (*instanciate_cb)(struct lxc_handler *, struct lxc_netdev *);
0ad19a3f 178
998ac676
RT
179struct mount_opt {
180 char *name;
181 int clear;
182 int flag;
183};
184
81810dd1
DL
185struct caps_opt {
186 char *name;
187 int value;
188};
189
e3b4c4c4
ST
190static int instanciate_veth(struct lxc_handler *, struct lxc_netdev *);
191static int instanciate_macvlan(struct lxc_handler *, struct lxc_netdev *);
192static int instanciate_vlan(struct lxc_handler *, struct lxc_netdev *);
193static int instanciate_phys(struct lxc_handler *, struct lxc_netdev *);
194static int instanciate_empty(struct lxc_handler *, struct lxc_netdev *);
82d5ae15 195
24654103
DL
196static instanciate_cb netdev_conf[LXC_NET_MAXCONFTYPE + 1] = {
197 [LXC_NET_VETH] = instanciate_veth,
198 [LXC_NET_MACVLAN] = instanciate_macvlan,
199 [LXC_NET_VLAN] = instanciate_vlan,
200 [LXC_NET_PHYS] = instanciate_phys,
201 [LXC_NET_EMPTY] = instanciate_empty,
0ad19a3f 202};
203
74a2b586
JK
204static int shutdown_veth(struct lxc_handler *, struct lxc_netdev *);
205static int shutdown_macvlan(struct lxc_handler *, struct lxc_netdev *);
206static int shutdown_vlan(struct lxc_handler *, struct lxc_netdev *);
207static int shutdown_phys(struct lxc_handler *, struct lxc_netdev *);
208static int shutdown_empty(struct lxc_handler *, struct lxc_netdev *);
209
210static instanciate_cb netdev_deconf[LXC_NET_MAXCONFTYPE + 1] = {
211 [LXC_NET_VETH] = shutdown_veth,
212 [LXC_NET_MACVLAN] = shutdown_macvlan,
213 [LXC_NET_VLAN] = shutdown_vlan,
214 [LXC_NET_PHYS] = shutdown_phys,
215 [LXC_NET_EMPTY] = shutdown_empty,
216};
217
998ac676 218static struct mount_opt mount_opt[] = {
88d413d5
SW
219 { "defaults", 0, 0 },
220 { "ro", 0, MS_RDONLY },
221 { "rw", 1, MS_RDONLY },
222 { "suid", 1, MS_NOSUID },
223 { "nosuid", 0, MS_NOSUID },
224 { "dev", 1, MS_NODEV },
225 { "nodev", 0, MS_NODEV },
226 { "exec", 1, MS_NOEXEC },
227 { "noexec", 0, MS_NOEXEC },
228 { "sync", 0, MS_SYNCHRONOUS },
229 { "async", 1, MS_SYNCHRONOUS },
230 { "dirsync", 0, MS_DIRSYNC },
231 { "remount", 0, MS_REMOUNT },
232 { "mand", 0, MS_MANDLOCK },
233 { "nomand", 1, MS_MANDLOCK },
234 { "atime", 1, MS_NOATIME },
235 { "noatime", 0, MS_NOATIME },
236 { "diratime", 1, MS_NODIRATIME },
237 { "nodiratime", 0, MS_NODIRATIME },
238 { "bind", 0, MS_BIND },
239 { "rbind", 0, MS_BIND|MS_REC },
240 { "relatime", 0, MS_RELATIME },
241 { "norelatime", 1, MS_RELATIME },
242 { "strictatime", 0, MS_STRICTATIME },
243 { "nostrictatime", 1, MS_STRICTATIME },
244 { NULL, 0, 0 },
998ac676
RT
245};
246
495d2046 247#if HAVE_SYS_CAPABILITY_H
81810dd1 248static struct caps_opt caps_opt[] = {
a6afdde9 249 { "chown", CAP_CHOWN },
1e11be34
DL
250 { "dac_override", CAP_DAC_OVERRIDE },
251 { "dac_read_search", CAP_DAC_READ_SEARCH },
252 { "fowner", CAP_FOWNER },
253 { "fsetid", CAP_FSETID },
81810dd1
DL
254 { "kill", CAP_KILL },
255 { "setgid", CAP_SETGID },
256 { "setuid", CAP_SETUID },
257 { "setpcap", CAP_SETPCAP },
258 { "linux_immutable", CAP_LINUX_IMMUTABLE },
259 { "net_bind_service", CAP_NET_BIND_SERVICE },
260 { "net_broadcast", CAP_NET_BROADCAST },
261 { "net_admin", CAP_NET_ADMIN },
262 { "net_raw", CAP_NET_RAW },
263 { "ipc_lock", CAP_IPC_LOCK },
264 { "ipc_owner", CAP_IPC_OWNER },
265 { "sys_module", CAP_SYS_MODULE },
266 { "sys_rawio", CAP_SYS_RAWIO },
267 { "sys_chroot", CAP_SYS_CHROOT },
268 { "sys_ptrace", CAP_SYS_PTRACE },
269 { "sys_pacct", CAP_SYS_PACCT },
270 { "sys_admin", CAP_SYS_ADMIN },
271 { "sys_boot", CAP_SYS_BOOT },
272 { "sys_nice", CAP_SYS_NICE },
273 { "sys_resource", CAP_SYS_RESOURCE },
274 { "sys_time", CAP_SYS_TIME },
275 { "sys_tty_config", CAP_SYS_TTY_CONFIG },
276 { "mknod", CAP_MKNOD },
277 { "lease", CAP_LEASE },
9527e566 278#ifdef CAP_AUDIT_WRITE
81810dd1 279 { "audit_write", CAP_AUDIT_WRITE },
9527e566
FW
280#endif
281#ifdef CAP_AUDIT_CONTROL
81810dd1 282 { "audit_control", CAP_AUDIT_CONTROL },
9527e566 283#endif
81810dd1
DL
284 { "setfcap", CAP_SETFCAP },
285 { "mac_override", CAP_MAC_OVERRIDE },
286 { "mac_admin", CAP_MAC_ADMIN },
5170c716
CS
287#ifdef CAP_SYSLOG
288 { "syslog", CAP_SYSLOG },
289#endif
290#ifdef CAP_WAKE_ALARM
291 { "wake_alarm", CAP_WAKE_ALARM },
292#endif
81810dd1 293};
495d2046
SG
294#else
295static struct caps_opt caps_opt[] = {};
296#endif
81810dd1 297
91c3830e
SH
298static int run_buffer(char *buffer)
299{
300 FILE *f;
301 char *output;
302
303 f = popen(buffer, "r");
304 if (!f) {
305 SYSERROR("popen failed");
306 return -1;
307 }
308
309 output = malloc(LXC_LOG_BUFFER_SIZE);
310 if (!output) {
311 ERROR("failed to allocate memory for script output");
312 return -1;
313 }
314
315 while(fgets(output, LXC_LOG_BUFFER_SIZE, f))
316 DEBUG("script output: %s", output);
317
318 free(output);
319
320 if (pclose(f) == -1) {
321 SYSERROR("Script exited on error");
322 return -1;
323 }
324
325 return 0;
326}
327
751d9dcd
DL
328static int run_script(const char *name, const char *section,
329 const char *script, ...)
e3b4c4c4 330{
abbfd20b 331 int ret;
91c3830e 332 char *buffer, *p;
abbfd20b
DL
333 size_t size = 0;
334 va_list ap;
751d9dcd
DL
335
336 INFO("Executing script '%s' for container '%s', config section '%s'",
337 script, name, section);
e3b4c4c4 338
abbfd20b
DL
339 va_start(ap, script);
340 while ((p = va_arg(ap, char *)))
95642a10 341 size += strlen(p) + 1;
abbfd20b
DL
342 va_end(ap);
343
344 size += strlen(script);
345 size += strlen(name);
346 size += strlen(section);
95642a10 347 size += 3;
abbfd20b 348
95642a10
MS
349 if (size > INT_MAX)
350 return -1;
351
352 buffer = alloca(size);
abbfd20b
DL
353 if (!buffer) {
354 ERROR("failed to allocate memory");
751d9dcd
DL
355 return -1;
356 }
357
9ba8130c
SH
358 ret = snprintf(buffer, size, "%s %s %s", script, name, section);
359 if (ret < 0 || ret >= size) {
360 ERROR("Script name too long");
361 free(buffer);
362 return -1;
363 }
751d9dcd 364
abbfd20b 365 va_start(ap, script);
9ba8130c
SH
366 while ((p = va_arg(ap, char *))) {
367 int len = size-ret;
368 int rc;
369 rc = snprintf(buffer + ret, len, " %s", p);
370 if (rc < 0 || rc >= len) {
371 free(buffer);
372 ERROR("Script args too long");
373 return -1;
374 }
375 ret += rc;
376 }
abbfd20b 377 va_end(ap);
751d9dcd 378
91c3830e 379 return run_buffer(buffer);
e3b4c4c4
ST
380}
381
a6afdde9 382static int find_fstype_cb(char* buffer, void *data)
78ae2fcc 383{
384 struct cbarg {
385 const char *rootfs;
a6afdde9 386 const char *target;
78ae2fcc 387 int mntopt;
388 } *cbarg = data;
389
390 char *fstype;
391
392 /* we don't try 'nodev' entries */
393 if (strstr(buffer, "nodev"))
394 return 0;
395
396 fstype = buffer;
b2718c72 397 fstype += lxc_char_left_gc(fstype, strlen(fstype));
398 fstype[lxc_char_right_gc(fstype, strlen(fstype))] = '\0';
78ae2fcc 399
a6afdde9
DL
400 DEBUG("trying to mount '%s'->'%s' with fstype '%s'",
401 cbarg->rootfs, cbarg->target, fstype);
402
403 if (mount(cbarg->rootfs, cbarg->target, fstype, cbarg->mntopt, NULL)) {
404 DEBUG("mount failed with error: %s", strerror(errno));
78ae2fcc 405 return 0;
a6afdde9 406 }
78ae2fcc 407
a6afdde9
DL
408 INFO("mounted '%s' on '%s', with fstype '%s'",
409 cbarg->rootfs, cbarg->target, fstype);
78ae2fcc 410
411 return 1;
412}
413
2656d231 414static int mount_unknow_fs(const char *rootfs, const char *target, int mntopt)
78ae2fcc 415{
a6afdde9 416 int i;
78ae2fcc 417
418 struct cbarg {
419 const char *rootfs;
a6afdde9 420 const char *target;
78ae2fcc 421 int mntopt;
422 } cbarg = {
423 .rootfs = rootfs,
a6afdde9 424 .target = target,
78ae2fcc 425 .mntopt = mntopt,
426 };
427
a6afdde9
DL
428 /*
429 * find the filesystem type with brute force:
430 * first we check with /etc/filesystems, in case the modules
78ae2fcc 431 * are auto-loaded and fall back to the supported kernel fs
432 */
433 char *fsfile[] = {
434 "/etc/filesystems",
435 "/proc/filesystems",
436 };
437
a6afdde9
DL
438 for (i = 0; i < sizeof(fsfile)/sizeof(fsfile[0]); i++) {
439
440 int ret;
441
442 if (access(fsfile[i], F_OK))
443 continue;
444
445 ret = lxc_file_for_each_line(fsfile[i], find_fstype_cb, &cbarg);
446 if (ret < 0) {
447 ERROR("failed to parse '%s'", fsfile[i]);
448 return -1;
449 }
450
451 if (ret)
452 return 0;
78ae2fcc 453 }
454
a6afdde9
DL
455 ERROR("failed to determine fs type for '%s'", rootfs);
456 return -1;
457}
458
2656d231 459static int mount_rootfs_dir(const char *rootfs, const char *target)
a6afdde9
DL
460{
461 return mount(rootfs, target, "none", MS_BIND | MS_REC, NULL);
462}
463
464static int setup_lodev(const char *rootfs, int fd, struct loop_info64 *loinfo)
465{
466 int rfd;
467 int ret = -1;
468
469 rfd = open(rootfs, O_RDWR);
470 if (rfd < 0) {
471 SYSERROR("failed to open '%s'", rootfs);
78ae2fcc 472 return -1;
473 }
474
a6afdde9 475 memset(loinfo, 0, sizeof(*loinfo));
78ae2fcc 476
a6afdde9 477 loinfo->lo_flags = LO_FLAGS_AUTOCLEAR;
78ae2fcc 478
a6afdde9
DL
479 if (ioctl(fd, LOOP_SET_FD, rfd)) {
480 SYSERROR("failed to LOOP_SET_FD");
481 goto out;
78ae2fcc 482 }
483
a6afdde9
DL
484 if (ioctl(fd, LOOP_SET_STATUS64, loinfo)) {
485 SYSERROR("failed to LOOP_SET_STATUS64");
78ae2fcc 486 goto out;
487 }
488
a6afdde9 489 ret = 0;
78ae2fcc 490out:
a6afdde9 491 close(rfd);
78ae2fcc 492
a6afdde9 493 return ret;
78ae2fcc 494}
495
2656d231 496static int mount_rootfs_file(const char *rootfs, const char *target)
78ae2fcc 497{
a6afdde9
DL
498 struct dirent dirent, *direntp;
499 struct loop_info64 loinfo;
9ba8130c 500 int ret = -1, fd = -1, rc;
a6afdde9
DL
501 DIR *dir;
502 char path[MAXPATHLEN];
78ae2fcc 503
a6afdde9
DL
504 dir = opendir("/dev");
505 if (!dir) {
506 SYSERROR("failed to open '/dev'");
78ae2fcc 507 return -1;
508 }
509
a6afdde9
DL
510 while (!readdir_r(dir, &dirent, &direntp)) {
511
512 if (!direntp)
513 break;
514
515 if (!strcmp(direntp->d_name, "."))
516 continue;
517
518 if (!strcmp(direntp->d_name, ".."))
519 continue;
520
521 if (strncmp(direntp->d_name, "loop", 4))
522 continue;
523
9ba8130c
SH
524 rc = snprintf(path, MAXPATHLEN, "/dev/%s", direntp->d_name);
525 if (rc < 0 || rc >= MAXPATHLEN)
526 continue;
527
a6afdde9
DL
528 fd = open(path, O_RDWR);
529 if (fd < 0)
530 continue;
531
532 if (ioctl(fd, LOOP_GET_STATUS64, &loinfo) == 0) {
533 close(fd);
534 continue;
535 }
536
537 if (errno != ENXIO) {
538 WARN("unexpected error for ioctl on '%s': %m",
539 direntp->d_name);
540 continue;
541 }
542
543 DEBUG("found '%s' free lodev", path);
544
545 ret = setup_lodev(rootfs, fd, &loinfo);
546 if (!ret)
2656d231 547 ret = mount_unknow_fs(path, target, 0);
a6afdde9
DL
548 close(fd);
549
550 break;
551 }
552
553 if (closedir(dir))
554 WARN("failed to close directory");
555
556 return ret;
78ae2fcc 557}
558
2656d231 559static int mount_rootfs_block(const char *rootfs, const char *target)
a6afdde9 560{
2656d231 561 return mount_unknow_fs(rootfs, target, 0);
a6afdde9
DL
562}
563
0c547523
SH
564/*
565 * pin_rootfs
566 * if rootfs is a directory, then open ${rootfs}.hold for writing for the
567 * duration of the container run, to prevent the container from marking the
568 * underlying fs readonly on shutdown.
569 * return -1 on error.
570 * return -2 if nothing needed to be pinned.
571 * return an open fd (>=0) if we pinned it.
572 */
573int pin_rootfs(const char *rootfs)
574{
575 char absrootfs[MAXPATHLEN];
576 char absrootfspin[MAXPATHLEN];
577 struct stat s;
578 int ret, fd;
579
e99ee0de 580 if (rootfs == NULL || strlen(rootfs) == 0)
0d03360a 581 return -2;
e99ee0de 582
0c547523
SH
583 if (!realpath(rootfs, absrootfs)) {
584 SYSERROR("failed to get real path for '%s'", rootfs);
585 return -1;
586 }
587
588 if (access(absrootfs, F_OK)) {
589 SYSERROR("'%s' is not accessible", absrootfs);
590 return -1;
591 }
592
593 if (stat(absrootfs, &s)) {
594 SYSERROR("failed to stat '%s'", absrootfs);
595 return -1;
596 }
597
72f919c4 598 if (!S_ISDIR(s.st_mode))
0c547523
SH
599 return -2;
600
601 ret = snprintf(absrootfspin, MAXPATHLEN, "%s%s", absrootfs, ".hold");
602 if (ret >= MAXPATHLEN) {
603 SYSERROR("pathname too long for rootfs hold file");
604 return -1;
605 }
606
607 fd = open(absrootfspin, O_CREAT | O_RDWR, S_IWUSR|S_IRUSR);
608 INFO("opened %s as fd %d\n", absrootfspin, fd);
609 return fd;
610}
611
2656d231 612static int mount_rootfs(const char *rootfs, const char *target)
0ad19a3f 613{
b09ef133 614 char absrootfs[MAXPATHLEN];
78ae2fcc 615 struct stat s;
a6afdde9 616 int i;
78ae2fcc 617
a6afdde9 618 typedef int (*rootfs_cb)(const char *, const char *);
78ae2fcc 619
620 struct rootfs_type {
621 int type;
622 rootfs_cb cb;
623 } rtfs_type[] = {
2656d231
DL
624 { S_IFDIR, mount_rootfs_dir },
625 { S_IFBLK, mount_rootfs_block },
626 { S_IFREG, mount_rootfs_file },
78ae2fcc 627 };
0ad19a3f 628
4c8ab83b 629 if (!realpath(rootfs, absrootfs)) {
36eb9bde 630 SYSERROR("failed to get real path for '%s'", rootfs);
4c8ab83b 631 return -1;
632 }
b09ef133 633
b09ef133 634 if (access(absrootfs, F_OK)) {
36eb9bde 635 SYSERROR("'%s' is not accessible", absrootfs);
b09ef133 636 return -1;
637 }
638
78ae2fcc 639 if (stat(absrootfs, &s)) {
36eb9bde 640 SYSERROR("failed to stat '%s'", absrootfs);
9b0f0477 641 return -1;
642 }
643
78ae2fcc 644 for (i = 0; i < sizeof(rtfs_type)/sizeof(rtfs_type[0]); i++) {
9b0f0477 645
78ae2fcc 646 if (!__S_ISTYPE(s.st_mode, rtfs_type[i].type))
647 continue;
9b0f0477 648
a6afdde9 649 return rtfs_type[i].cb(absrootfs, target);
78ae2fcc 650 }
9b0f0477 651
36eb9bde 652 ERROR("unsupported rootfs type for '%s'", absrootfs);
78ae2fcc 653 return -1;
0ad19a3f 654}
655
4e5440c6 656static int setup_utsname(struct utsname *utsname)
0ad19a3f 657{
4e5440c6
DL
658 if (!utsname)
659 return 0;
0ad19a3f 660
4e5440c6
DL
661 if (sethostname(utsname->nodename, strlen(utsname->nodename))) {
662 SYSERROR("failed to set the hostname to '%s'", utsname->nodename);
0ad19a3f 663 return -1;
664 }
665
4e5440c6 666 INFO("'%s' hostname has been setup", utsname->nodename);
cd54d859 667
0ad19a3f 668 return 0;
669}
670
33fcb7a0 671static int setup_tty(const struct lxc_rootfs *rootfs,
7c6ef2a2 672 const struct lxc_tty_info *tty_info, char *ttydir)
b0a33c1e 673{
7c6ef2a2
SH
674 char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
675 int i, ret;
b0a33c1e 676
bc9bd0e3
DL
677 if (!rootfs->path)
678 return 0;
679
b0a33c1e 680 for (i = 0; i < tty_info->nbtty; i++) {
681
682 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
683
7c6ef2a2 684 ret = snprintf(path, sizeof(path), "%s/dev/tty%d",
12297168 685 rootfs->mount, i + 1);
7c6ef2a2
SH
686 if (ret >= sizeof(path)) {
687 ERROR("pathname too long for ttys");
688 return -1;
689 }
690 if (ttydir) {
691 /* create dev/lxc/tty%d" */
9ba8130c 692 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/tty%d",
7c6ef2a2
SH
693 rootfs->mount, ttydir, i + 1);
694 if (ret >= sizeof(lxcpath)) {
695 ERROR("pathname too long for ttys");
696 return -1;
697 }
698 ret = creat(lxcpath, 0660);
699 if (ret==-1 && errno != EEXIST) {
700 SYSERROR("error creating %s\n", lxcpath);
701 return -1;
702 }
703 close(ret);
704 ret = unlink(path);
705 if (ret && errno != ENOENT) {
706 SYSERROR("error unlinking %s\n", path);
707 return -1;
708 }
b0a33c1e 709
7c6ef2a2
SH
710 if (mount(pty_info->name, lxcpath, "none", MS_BIND, 0)) {
711 WARN("failed to mount '%s'->'%s'",
712 pty_info->name, path);
713 continue;
714 }
13954cce 715
9ba8130c
SH
716 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/tty%d", ttydir, i+1);
717 if (ret >= sizeof(lxcpath)) {
718 ERROR("tty pathname too long");
719 return -1;
720 }
7c6ef2a2
SH
721 ret = symlink(lxcpath, path);
722 if (ret) {
723 SYSERROR("failed to create symlink for tty %d\n", i+1);
724 return -1;
725 }
726 } else {
c6883f38
SH
727 /* If we populated /dev, then we need to create /dev/ttyN */
728 if (access(path, F_OK)) {
729 ret = creat(path, 0660);
730 if (ret==-1) {
731 SYSERROR("error creating %s\n", path);
732 /* this isn't fatal, continue */
733 } else
734 close(ret);
735 }
7c6ef2a2
SH
736 if (mount(pty_info->name, path, "none", MS_BIND, 0)) {
737 WARN("failed to mount '%s'->'%s'",
738 pty_info->name, path);
739 continue;
740 }
b0a33c1e 741 }
742 }
743
cd54d859
DL
744 INFO("%d tty(s) has been setup", tty_info->nbtty);
745
b0a33c1e 746 return 0;
747}
748
7a7ff0c6 749static int setup_rootfs_pivot_root_cb(char *buffer, void *data)
bf601689
MH
750{
751 struct lxc_list *mountlist, *listentry, *iterator;
752 char *pivotdir, *mountpoint, *mountentry;
753 int found;
754 void **cbparm;
755
756 mountentry = buffer;
757 cbparm = (void **)data;
758
759 mountlist = cbparm[0];
760 pivotdir = cbparm[1];
761
762 /* parse entry, first field is mountname, ignore */
763 mountpoint = strtok(mountentry, " ");
764 if (!mountpoint)
765 return -1;
766
767 /* second field is mountpoint */
768 mountpoint = strtok(NULL, " ");
769 if (!mountpoint)
770 return -1;
771
772 /* only consider mountpoints below old root fs */
773 if (strncmp(mountpoint, pivotdir, strlen(pivotdir)))
774 return 0;
775
776 /* filter duplicate mountpoints */
777 found = 0;
778 lxc_list_for_each(iterator, mountlist) {
779 if (!strcmp(iterator->elem, mountpoint)) {
780 found = 1;
781 break;
782 }
783 }
784 if (found)
785 return 0;
786
787 /* add entry to list */
788 listentry = malloc(sizeof(*listentry));
789 if (!listentry) {
790 SYSERROR("malloc for mountpoint listentry failed");
791 return -1;
792 }
793
794 listentry->elem = strdup(mountpoint);
795 if (!listentry->elem) {
796 SYSERROR("strdup failed");
797 return -1;
798 }
799 lxc_list_add_tail(mountlist, listentry);
800
801 return 0;
802}
803
cc6f6dd7 804static int umount_oldrootfs(const char *oldrootfs)
bf601689 805{
2382ecff 806 char path[MAXPATHLEN];
bf601689 807 void *cbparm[2];
9ebb03ad 808 struct lxc_list mountlist, *iterator, *next;
bf601689 809 int ok, still_mounted, last_still_mounted;
9ba8130c 810 int rc;
bf601689
MH
811
812 /* read and parse /proc/mounts in old root fs */
813 lxc_list_init(&mountlist);
814
cc6f6dd7 815 /* oldrootfs is on the top tree directory now */
9ba8130c
SH
816 rc = snprintf(path, sizeof(path), "/%s", oldrootfs);
817 if (rc >= sizeof(path)) {
818 ERROR("rootfs name too long");
819 return -1;
820 }
bf601689 821 cbparm[0] = &mountlist;
bf601689 822
cc6f6dd7 823 cbparm[1] = strdup(path);
bf601689
MH
824 if (!cbparm[1]) {
825 SYSERROR("strdup failed");
826 return -1;
827 }
828
9ba8130c
SH
829 rc = snprintf(path, sizeof(path), "%s/proc/mounts", oldrootfs);
830 if (rc >= sizeof(path)) {
831 ERROR("container proc/mounts name too long");
832 return -1;
833 }
cc6f6dd7
DL
834
835 ok = lxc_file_for_each_line(path,
836 setup_rootfs_pivot_root_cb, &cbparm);
bf601689
MH
837 if (ok < 0) {
838 SYSERROR("failed to read or parse mount list '%s'", path);
839 return -1;
840 }
841
842 /* umount filesystems until none left or list no longer shrinks */
843 still_mounted = 0;
844 do {
845 last_still_mounted = still_mounted;
846 still_mounted = 0;
847
9ebb03ad 848 lxc_list_for_each_safe(iterator, &mountlist, next) {
bf601689 849
c08556c6 850 /* umount normally */
bf601689
MH
851 if (!umount(iterator->elem)) {
852 DEBUG("umounted '%s'", (char *)iterator->elem);
853 lxc_list_del(iterator);
854 continue;
855 }
856
bf601689
MH
857 still_mounted++;
858 }
7df119ee 859
bf601689
MH
860 } while (still_mounted > 0 && still_mounted != last_still_mounted);
861
7df119ee 862
c08556c6
DL
863 lxc_list_for_each(iterator, &mountlist) {
864
865 /* let's try a lazy umount */
866 if (!umount2(iterator->elem, MNT_DETACH)) {
867 INFO("lazy unmount of '%s'", (char *)iterator->elem);
868 continue;
869 }
870
871 /* be more brutal (nfs) */
872 if (!umount2(iterator->elem, MNT_FORCE)) {
873 INFO("forced unmount of '%s'", (char *)iterator->elem);
874 continue;
875 }
876
7df119ee 877 WARN("failed to unmount '%s'", (char *)iterator->elem);
c08556c6 878 }
bf601689 879
cc6f6dd7
DL
880 return 0;
881}
882
883static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
884{
885 char path[MAXPATHLEN];
886 int remove_pivotdir = 0;
9ba8130c 887 int rc;
cc6f6dd7
DL
888
889 /* change into new root fs */
890 if (chdir(rootfs)) {
891 SYSERROR("can't chdir to new rootfs '%s'", rootfs);
892 return -1;
893 }
894
895 if (!pivotdir)
30c5d292 896 pivotdir = "lxc_putold";
cc6f6dd7 897
4f9293b1 898 /* compute the full path to pivotdir under rootfs */
9ba8130c
SH
899 rc = snprintf(path, sizeof(path), "%s/%s", rootfs, pivotdir);
900 if (rc >= sizeof(path)) {
901 ERROR("pivot dir name too long");
902 return -1;
903 }
cc6f6dd7
DL
904
905 if (access(path, F_OK)) {
906
907 if (mkdir_p(path, 0755)) {
908 SYSERROR("failed to create pivotdir '%s'", path);
909 return -1;
910 }
911
912 remove_pivotdir = 1;
913 DEBUG("created '%s' directory", path);
914 }
915
916 DEBUG("mountpoint for old rootfs is '%s'", path);
917
918 /* pivot_root into our new root fs */
919 if (pivot_root(".", path)) {
920 SYSERROR("pivot_root syscall failed");
bf601689
MH
921 return -1;
922 }
cc6f6dd7
DL
923
924 if (chdir("/")) {
925 SYSERROR("can't chdir to / after pivot_root");
926 return -1;
927 }
928
929 DEBUG("pivot_root syscall to '%s' successful", rootfs);
930
931 /* we switch from absolute path to relative path */
932 if (umount_oldrootfs(pivotdir))
933 return -1;
bf601689 934
c08556c6
DL
935 /* remove temporary mount point, we don't consider the removing
936 * as fatal */
a91d897a
FW
937 if (remove_pivotdir && rmdir(pivotdir))
938 WARN("can't remove mountpoint '%s': %m", pivotdir);
bf601689 939
bf601689
MH
940 return 0;
941}
942
91c3830e
SH
943/*
944 * Do we want to add options for max size of /dev and a file to
945 * specify which devices to create?
946 */
947static int mount_autodev(char *root)
948{
949 int ret;
950 char path[MAXPATHLEN];
951
952 INFO("Mounting /dev under %s\n", root);
953 ret = snprintf(path, MAXPATHLEN, "%s/dev", root);
954 if (ret < 0 || ret > MAXPATHLEN)
955 return -1;
956 ret = mount("none", path, "tmpfs", 0, "size=100000");
957 if (ret) {
958 SYSERROR("Failed to mount /dev at %s\n", root);
959 return -1;
960 }
961 ret = snprintf(path, MAXPATHLEN, "%s/dev/pts", root);
962 if (ret < 0 || ret >= MAXPATHLEN)
963 return -1;
964 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
965 if (ret) {
966 SYSERROR("Failed to create /dev/pts in container");
967 return -1;
968 }
969
970 INFO("Mounted /dev under %s\n", root);
971 return 0;
972}
973
c6883f38
SH
974struct lxc_devs {
975 char *name;
976 mode_t mode;
977 int maj;
978 int min;
979};
980
981struct lxc_devs lxc_devs[] = {
982 { "null", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 3 },
983 { "zero", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 5 },
984 { "full", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 7 },
985 { "urandom", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 9 },
986 { "random", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 1, 8 },
987 { "tty", S_IFCHR | S_IRWXU | S_IRWXG | S_IRWXO, 5, 0 },
988 { "console", S_IFCHR | S_IRUSR | S_IWUSR, 5, 1 },
989};
990
c6883f38
SH
991static int setup_autodev(char *root)
992{
993 int ret;
994 struct lxc_devs *d;
995 char path[MAXPATHLEN];
996 int i;
3a32201c 997 mode_t cmask;
c6883f38 998
91c3830e
SH
999 INFO("Creating initial consoles under %s/dev\n", root);
1000
c6883f38 1001 ret = snprintf(path, MAXPATHLEN, "%s/dev", root);
91c3830e
SH
1002 if (ret < 0 || ret >= MAXPATHLEN) {
1003 ERROR("Error calculating container /dev location");
c6883f38 1004 return -1;
f7bee6c6 1005 }
91c3830e
SH
1006
1007 INFO("Populating /dev under %s\n", root);
3a32201c 1008 cmask = umask(S_IXUSR | S_IXGRP | S_IXOTH);
c6883f38
SH
1009 for (i = 0; i < sizeof(lxc_devs) / sizeof(lxc_devs[0]); i++) {
1010 d = &lxc_devs[i];
1011 ret = snprintf(path, MAXPATHLEN, "%s/dev/%s", root, d->name);
1012 if (ret < 0 || ret >= MAXPATHLEN)
1013 return -1;
1014 ret = mknod(path, d->mode, makedev(d->maj, d->min));
91c3830e 1015 if (ret && errno != EEXIST) {
c6883f38
SH
1016 SYSERROR("Error creating %s\n", d->name);
1017 return -1;
1018 }
1019 }
3a32201c 1020 umask(cmask);
c6883f38
SH
1021
1022 INFO("Populated /dev under %s\n", root);
1023 return 0;
1024}
1025
cc28d0b0
SH
1026/*
1027 * Detect whether / is mounted MS_SHARED. The only way I know of to
1028 * check that is through /proc/self/mountinfo.
1029 * I'm only checking for /. If the container rootfs or mount location
1030 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1031 * out would be too much work to be worth it.
1032 */
1033#define LINELEN 4096
1034int detect_shared_rootfs(void)
1035{
1036 char buf[LINELEN], *p;
1037 FILE *f;
1038 int i;
1039 char *p2;
1040
1041 f = fopen("/proc/self/mountinfo", "r");
1042 if (!f)
1043 return 0;
1044 while ((p = fgets(buf, LINELEN, f))) {
1045 INFO("looking at .%s.", p);
1046 for (p = buf, i=0; p && i < 4; i++)
1047 p = index(p+1, ' ');
1048 if (!p)
1049 continue;
1050 p2 = index(p+1, ' ');
1051 if (!p2)
1052 continue;
1053 *p2 = '\0';
1054 INFO("now p is .%s.", p);
1055 if (strcmp(p+1, "/") == 0) {
1056 // this is '/'. is it shared?
1057 p = index(p2+1, ' ');
1058 if (strstr(p, "shared:"))
1059 return 1;
1060 }
1061 }
1062 fclose(f);
1063 return 0;
1064}
1065
1066/*
1067 * I'll forgive you for asking whether all of this is needed :) The
1068 * answer is yes.
1069 * pivot_root will fail if the new root, the put_old dir, or the parent
1070 * of current->fs->root are MS_SHARED. (parent of current->fs_root may
1071 * or may not be current->fs_root - if we assumed it always was, we could
1072 * just mount --make-rslave /). So,
1073 * 1. mount a tiny tmpfs to be parent of current->fs->root.
1074 * 2. make that MS_SLAVE
1075 * 3. make a 'root' directory under that
1076 * 4. mount --rbind / under the $tinyroot/root.
1077 * 5. make that rslave
1078 * 6. chdir and chroot into $tinyroot/root
1079 * 7. $tinyroot will be unmounted by our parent in start.c
1080 */
1081static int chroot_into_slave(struct lxc_conf *conf)
1082{
1083 char path[MAXPATHLEN];
1084 const char *destpath = conf->rootfs.mount;
1085 int ret;
1086
1087 if (mount(destpath, destpath, NULL, MS_BIND, 0)) {
1088 SYSERROR("failed to mount %s bind", destpath);
1089 return -1;
1090 }
1091 if (mount("", destpath, NULL, MS_SLAVE, 0)) {
1092 SYSERROR("failed to make %s slave", destpath);
1093 return -1;
1094 }
1095 if (mount("none", destpath, "tmpfs", 0, "size=10000")) {
1096 SYSERROR("Failed to mount tmpfs / at %s", destpath);
1097 return -1;
1098 }
1099 ret = snprintf(path, MAXPATHLEN, "%s/root", destpath);
1100 if (ret < 0 || ret >= MAXPATHLEN) {
1101 ERROR("out of memory making root path");
1102 return -1;
1103 }
1104 if (mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) {
1105 SYSERROR("Failed to create /dev/pts in container");
1106 return -1;
1107 }
1108 if (mount("/", path, NULL, MS_BIND|MS_REC, 0)) {
1109 SYSERROR("Failed to rbind mount / to %s", path);
1110 return -1;
1111 }
1112 if (mount("", destpath, NULL, MS_SLAVE|MS_REC, 0)) {
1113 SYSERROR("Failed to make tmp-/ at %s rslave", path);
1114 return -1;
1115 }
1116 if (chdir(path)) {
1117 SYSERROR("Failed to chdir into tmp-/");
1118 return -1;
1119 }
1120 if (chroot(path)) {
1121 SYSERROR("Failed to chroot into tmp-/");
1122 return -1;
1123 }
1124 INFO("Chrooted into tmp-/ at %s\n", path);
1125 return 0;
1126}
1127
1128static int setup_rootfs(struct lxc_conf *conf)
0ad19a3f 1129{
cc28d0b0
SH
1130 const struct lxc_rootfs *rootfs = &conf->rootfs;
1131
33fcb7a0 1132 if (!rootfs->path)
c69bd12f 1133 return 0;
0ad19a3f 1134
12297168 1135 if (access(rootfs->mount, F_OK)) {
b1789442 1136 SYSERROR("failed to access to '%s', check it is present",
12297168 1137 rootfs->mount);
b1789442
DL
1138 return -1;
1139 }
1140
cc28d0b0
SH
1141 if (detect_shared_rootfs()) {
1142 if (chroot_into_slave(conf)) {
1143 ERROR("Failed to chroot into slave /");
1144 return -1;
1145 }
1146 }
1147
2656d231 1148 if (mount_rootfs(rootfs->path, rootfs->mount)) {
a6afdde9 1149 ERROR("failed to mount rootfs");
c3f0a28c 1150 return -1;
1151 }
0ad19a3f 1152
12297168 1153 DEBUG("mounted '%s' on '%s'", rootfs->path, rootfs->mount);
c69bd12f 1154
ac778708
DL
1155 return 0;
1156}
1157
1158int setup_pivot_root(const struct lxc_rootfs *rootfs)
1159{
ac778708
DL
1160 if (!rootfs->path)
1161 return 0;
1162
12297168 1163 if (setup_rootfs_pivot_root(rootfs->mount, rootfs->pivot)) {
cc6f6dd7 1164 ERROR("failed to setup pivot root");
25368b52 1165 return -1;
c69bd12f
DL
1166 }
1167
25368b52 1168 return 0;
0ad19a3f 1169}
1170
d852c78c 1171static int setup_pts(int pts)
3c26f34e 1172{
77890c6d
SW
1173 char target[PATH_MAX];
1174
d852c78c
DL
1175 if (!pts)
1176 return 0;
3c26f34e 1177
1178 if (!access("/dev/pts/ptmx", F_OK) && umount("/dev/pts")) {
36eb9bde 1179 SYSERROR("failed to umount 'dev/pts'");
3c26f34e 1180 return -1;
1181 }
1182
a6afdde9
DL
1183 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL,
1184 "newinstance,ptmxmode=0666")) {
36eb9bde 1185 SYSERROR("failed to mount a new instance of '/dev/pts'");
3c26f34e 1186 return -1;
1187 }
1188
3c26f34e 1189 if (access("/dev/ptmx", F_OK)) {
1190 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
1191 goto out;
36eb9bde 1192 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 1193 return -1;
1194 }
1195
77890c6d
SW
1196 if (realpath("/dev/ptmx", target) && !strcmp(target, "/dev/pts/ptmx"))
1197 goto out;
1198
3c26f34e 1199 /* fallback here, /dev/pts/ptmx exists just mount bind */
1200 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND, 0)) {
36eb9bde 1201 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 1202 return -1;
1203 }
cd54d859
DL
1204
1205 INFO("created new pts instance");
d852c78c 1206
3c26f34e 1207out:
1208 return 0;
1209}
1210
cccc74b5
DL
1211static int setup_personality(int persona)
1212{
6ff05e18 1213 #if HAVE_SYS_PERSONALITY_H
cccc74b5
DL
1214 if (persona == -1)
1215 return 0;
1216
1217 if (personality(persona) < 0) {
1218 SYSERROR("failed to set personality to '0x%x'", persona);
1219 return -1;
1220 }
1221
1222 INFO("set personality to '0x%x'", persona);
6ff05e18 1223 #endif
cccc74b5
DL
1224
1225 return 0;
1226}
1227
7c6ef2a2 1228static int setup_dev_console(const struct lxc_rootfs *rootfs,
33fcb7a0 1229 const struct lxc_console *console)
6e590161 1230{
63376d7d
DL
1231 char path[MAXPATHLEN];
1232 struct stat s;
7c6ef2a2 1233 int ret;
52e35957 1234
7c6ef2a2
SH
1235 ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs->mount);
1236 if (ret >= sizeof(path)) {
1237 ERROR("console path too long\n");
1238 return -1;
1239 }
52e35957 1240
63376d7d 1241 if (access(path, F_OK)) {
466978b0 1242 WARN("rootfs specified but no console found at '%s'", path);
63376d7d 1243 return 0;
52e35957
DL
1244 }
1245
f78a1f32 1246 if (console->peer == -1) {
63376d7d 1247 INFO("no console output required");
f78a1f32
DL
1248 return 0;
1249 }
ed502555 1250
63376d7d
DL
1251 if (stat(path, &s)) {
1252 SYSERROR("failed to stat '%s'", path);
1253 return -1;
1254 }
1255
1256 if (chmod(console->name, s.st_mode)) {
1257 SYSERROR("failed to set mode '0%o' to '%s'",
1258 s.st_mode, console->name);
1259 return -1;
1260 }
13954cce 1261
63376d7d
DL
1262 if (mount(console->name, path, "none", MS_BIND, 0)) {
1263 ERROR("failed to mount '%s' on '%s'", console->name, path);
6e590161 1264 return -1;
1265 }
1266
63376d7d 1267 INFO("console has been setup");
7c6ef2a2
SH
1268 return 0;
1269}
1270
1271static int setup_ttydir_console(const struct lxc_rootfs *rootfs,
1272 const struct lxc_console *console,
1273 char *ttydir)
1274{
1275 char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
1276 int ret;
1277
1278 /* create rootfs/dev/<ttydir> directory */
1279 ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs->mount,
1280 ttydir);
1281 if (ret >= sizeof(path))
1282 return -1;
1283 ret = mkdir(path, 0755);
1284 if (ret && errno != EEXIST) {
1285 SYSERROR("failed with errno %d to create %s\n", errno, path);
1286 return -1;
1287 }
1288 INFO("created %s\n", path);
1289
1290 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console",
1291 rootfs->mount, ttydir);
1292 if (ret >= sizeof(lxcpath)) {
1293 ERROR("console path too long\n");
1294 return -1;
1295 }
1296
1297 snprintf(path, sizeof(path), "%s/dev/console", rootfs->mount);
1298 ret = unlink(path);
1299 if (ret && errno != ENOENT) {
1300 SYSERROR("error unlinking %s\n", path);
1301 return -1;
1302 }
1303
1304 ret = creat(lxcpath, 0660);
1305 if (ret==-1 && errno != EEXIST) {
1306 SYSERROR("error %d creating %s\n", errno, lxcpath);
1307 return -1;
1308 }
1309 close(ret);
1310
1311 if (console->peer == -1) {
1312 INFO("no console output required");
1313 return 0;
1314 }
1315
1316 if (mount(console->name, lxcpath, "none", MS_BIND, 0)) {
1317 ERROR("failed to mount '%s' on '%s'", console->name, lxcpath);
1318 return -1;
1319 }
1320
1321 /* create symlink from rootfs/dev/console to 'lxc/console' */
9ba8130c
SH
1322 ret = snprintf(lxcpath, sizeof(lxcpath), "%s/console", ttydir);
1323 if (ret >= sizeof(lxcpath)) {
1324 ERROR("lxc/console path too long");
1325 return -1;
1326 }
7c6ef2a2
SH
1327 ret = symlink(lxcpath, path);
1328 if (ret) {
1329 SYSERROR("failed to create symlink for console");
1330 return -1;
1331 }
1332
1333 INFO("console has been setup on %s", lxcpath);
cd54d859 1334
6e590161 1335 return 0;
1336}
1337
7c6ef2a2
SH
1338static int setup_console(const struct lxc_rootfs *rootfs,
1339 const struct lxc_console *console,
1340 char *ttydir)
1341{
1342 /* We don't have a rootfs, /dev/console will be shared */
1343 if (!rootfs->path)
1344 return 0;
1345 if (!ttydir)
1346 return setup_dev_console(rootfs, console);
1347
1348 return setup_ttydir_console(rootfs, console, ttydir);
1349}
1350
1bd051a6
SH
1351static int setup_kmsg(const struct lxc_rootfs *rootfs,
1352 const struct lxc_console *console)
1353{
1354 char kpath[MAXPATHLEN];
1355 int ret;
1356
222fea5a
DE
1357 if (!rootfs->path)
1358 return 0;
1bd051a6
SH
1359 ret = snprintf(kpath, sizeof(kpath), "%s/dev/kmsg", rootfs->mount);
1360 if (ret < 0 || ret >= sizeof(kpath))
1361 return -1;
1362
1363 ret = unlink(kpath);
1364 if (ret && errno != ENOENT) {
1365 SYSERROR("error unlinking %s\n", kpath);
1366 return -1;
1367 }
1368
1369 ret = symlink("console", kpath);
1370 if (ret) {
1371 SYSERROR("failed to create symlink for kmsg");
1372 return -1;
1373 }
1374
1375 return 0;
1376}
1377
ae5c8b8e 1378int setup_cgroup(const char *cgpath, struct lxc_list *cgroups)
576f946d 1379{
102a5303
DL
1380 struct lxc_list *iterator;
1381 struct lxc_cgroup *cg;
88329c69 1382 int ret = -1;
6f4a3756 1383
102a5303
DL
1384 if (lxc_list_empty(cgroups))
1385 return 0;
6f4a3756 1386
102a5303 1387 lxc_list_for_each(iterator, cgroups) {
13954cce 1388
102a5303 1389 cg = iterator->elem;
6f4a3756 1390
ae5c8b8e
SH
1391 if (lxc_cgroup_set_bypath(cgpath, cg->subsystem, cg->value)) {
1392 ERROR("Error setting %s to %s for %s\n", cg->subsystem,
1393 cg->value, cgpath);
88329c69 1394 goto out;
ae5c8b8e 1395 }
6f4a3756 1396
102a5303 1397 DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value);
6f4a3756 1398 }
13954cce 1399
88329c69 1400 ret = 0;
cd54d859 1401 INFO("cgroup has been setup");
88329c69
MN
1402out:
1403 return ret;
576f946d 1404}
1405
998ac676
RT
1406static void parse_mntopt(char *opt, unsigned long *flags, char **data)
1407{
1408 struct mount_opt *mo;
1409
1410 /* If opt is found in mount_opt, set or clear flags.
1411 * Otherwise append it to data. */
1412
1413 for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
1414 if (!strncmp(opt, mo->name, strlen(mo->name))) {
1415 if (mo->clear)
1416 *flags &= ~mo->flag;
1417 else
1418 *flags |= mo->flag;
1419 return;
1420 }
1421 }
1422
1423 if (strlen(*data))
1424 strcat(*data, ",");
1425 strcat(*data, opt);
1426}
1427
911324ef 1428static int parse_mntopts(const char *mntopts, unsigned long *mntflags,
998ac676
RT
1429 char **mntdata)
1430{
1431 char *s, *data;
1432 char *p, *saveptr = NULL;
1433
911324ef 1434 *mntdata = NULL;
91656ce5 1435 *mntflags = 0L;
911324ef
DL
1436
1437 if (!mntopts)
998ac676
RT
1438 return 0;
1439
911324ef 1440 s = strdup(mntopts);
998ac676 1441 if (!s) {
36eb9bde 1442 SYSERROR("failed to allocate memory");
998ac676
RT
1443 return -1;
1444 }
1445
1446 data = malloc(strlen(s) + 1);
1447 if (!data) {
36eb9bde 1448 SYSERROR("failed to allocate memory");
998ac676
RT
1449 free(s);
1450 return -1;
1451 }
1452 *data = 0;
1453
1454 for (p = strtok_r(s, ",", &saveptr); p != NULL;
1455 p = strtok_r(NULL, ",", &saveptr))
1456 parse_mntopt(p, mntflags, &data);
1457
1458 if (*data)
1459 *mntdata = data;
1460 else
1461 free(data);
1462 free(s);
1463
1464 return 0;
1465}
1466
911324ef
DL
1467static int mount_entry(const char *fsname, const char *target,
1468 const char *fstype, unsigned long mountflags,
1469 const char *data)
1470{
1471 if (mount(fsname, target, fstype, mountflags & ~MS_REMOUNT, data)) {
1472 SYSERROR("failed to mount '%s' on '%s'", fsname, target);
1473 return -1;
1474 }
1475
1476 if ((mountflags & MS_REMOUNT) || (mountflags & MS_BIND)) {
1477
1478 DEBUG("remounting %s on %s to respect bind or remount options",
1479 fsname, target);
1480
1481 if (mount(fsname, target, fstype,
1482 mountflags | MS_REMOUNT, data)) {
1483 SYSERROR("failed to mount '%s' on '%s'",
1484 fsname, target);
1485 return -1;
1486 }
1487 }
1488
1489 DEBUG("mounted '%s' on '%s', type '%s'", fsname, target, fstype);
1490
1491 return 0;
1492}
1493
1494static inline int mount_entry_on_systemfs(struct mntent *mntent)
0ad19a3f 1495{
998ac676
RT
1496 unsigned long mntflags;
1497 char *mntdata;
911324ef
DL
1498 int ret;
1499
1500 if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
1501 ERROR("failed to parse mount option '%s'", mntent->mnt_opts);
1502 return -1;
1503 }
1504
1505 ret = mount_entry(mntent->mnt_fsname, mntent->mnt_dir,
1506 mntent->mnt_type, mntflags, mntdata);
1507
68c152ef
SH
1508 if (hasmntopt(mntent, "optional") != NULL)
1509 ret = 0;
1510
911324ef
DL
1511 free(mntdata);
1512
1513 return ret;
1514}
1515
1516static int mount_entry_on_absolute_rootfs(struct mntent *mntent,
80a881b2
SH
1517 const struct lxc_rootfs *rootfs,
1518 const char *lxc_name)
911324ef 1519{
013bd428 1520 char *aux;
59760f5d 1521 char path[MAXPATHLEN];
911324ef
DL
1522 unsigned long mntflags;
1523 char *mntdata;
80a881b2 1524 int r, ret = 0, offset;
67e571de 1525 const char *lxcpath;
0ad19a3f 1526
911324ef
DL
1527 if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
1528 ERROR("failed to parse mount option '%s'", mntent->mnt_opts);
1529 return -1;
1530 }
1bc60a65 1531
2a59a681
SH
1532 lxcpath = default_lxc_path();
1533 if (!lxcpath) {
1534 ERROR("Out of memory");
1535 return -1;
1536 }
1537
80a881b2 1538 /* if rootfs->path is a blockdev path, allow container fstab to
2a59a681
SH
1539 * use $lxcpath/CN/rootfs as the target prefix */
1540 r = snprintf(path, MAXPATHLEN, "%s/%s/rootfs", lxcpath, lxc_name);
80a881b2
SH
1541 if (r < 0 || r >= MAXPATHLEN)
1542 goto skipvarlib;
1543
1544 aux = strstr(mntent->mnt_dir, path);
1545 if (aux) {
1546 offset = strlen(path);
1547 goto skipabs;
1548 }
1549
1550skipvarlib:
013bd428
DL
1551 aux = strstr(mntent->mnt_dir, rootfs->path);
1552 if (!aux) {
1553 WARN("ignoring mount point '%s'", mntent->mnt_dir);
1554 goto out;
1555 }
80a881b2
SH
1556 offset = strlen(rootfs->path);
1557
1558skipabs:
013bd428 1559
9ba8130c 1560 r = snprintf(path, MAXPATHLEN, "%s/%s", rootfs->mount,
80a881b2
SH
1561 aux + offset);
1562 if (r < 0 || r >= MAXPATHLEN) {
1563 WARN("pathnme too long for '%s'", mntent->mnt_dir);
1564 ret = -1;
1565 goto out;
1566 }
1567
d330fe7b 1568
013bd428 1569 ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type,
911324ef 1570 mntflags, mntdata);
0ad19a3f 1571
68c152ef
SH
1572 if (hasmntopt(mntent, "optional") != NULL)
1573 ret = 0;
1574
013bd428 1575out:
911324ef
DL
1576 free(mntdata);
1577 return ret;
1578}
d330fe7b 1579
911324ef
DL
1580static int mount_entry_on_relative_rootfs(struct mntent *mntent,
1581 const char *rootfs)
1582{
1583 char path[MAXPATHLEN];
1584 unsigned long mntflags;
1585 char *mntdata;
1586 int ret;
d330fe7b 1587
911324ef
DL
1588 if (parse_mntopts(mntent->mnt_opts, &mntflags, &mntdata) < 0) {
1589 ERROR("failed to parse mount option '%s'", mntent->mnt_opts);
1590 return -1;
1591 }
d330fe7b 1592
911324ef 1593 /* relative to root mount point */
9ba8130c
SH
1594 ret = snprintf(path, sizeof(path), "%s/%s", rootfs, mntent->mnt_dir);
1595 if (ret >= sizeof(path)) {
1596 ERROR("path name too long");
1597 return -1;
1598 }
911324ef
DL
1599
1600 ret = mount_entry(mntent->mnt_fsname, path, mntent->mnt_type,
1601 mntflags, mntdata);
1602
68c152ef
SH
1603 if (hasmntopt(mntent, "optional") != NULL)
1604 ret = 0;
1605
911324ef 1606 free(mntdata);
998ac676 1607
911324ef
DL
1608 return ret;
1609}
1610
80a881b2
SH
1611static int mount_file_entries(const struct lxc_rootfs *rootfs, FILE *file,
1612 const char *lxc_name)
911324ef
DL
1613{
1614 struct mntent *mntent;
1615 int ret = -1;
e76b8764 1616
911324ef 1617 while ((mntent = getmntent(file))) {
e76b8764 1618
911324ef
DL
1619 if (!rootfs->path) {
1620 if (mount_entry_on_systemfs(mntent))
e76b8764 1621 goto out;
911324ef 1622 continue;
e76b8764
CDC
1623 }
1624
911324ef
DL
1625 /* We have a separate root, mounts are relative to it */
1626 if (mntent->mnt_dir[0] != '/') {
1627 if (mount_entry_on_relative_rootfs(mntent,
1628 rootfs->mount))
1629 goto out;
1630 continue;
1631 }
cd54d859 1632
80a881b2 1633 if (mount_entry_on_absolute_rootfs(mntent, rootfs, lxc_name))
911324ef 1634 goto out;
0ad19a3f 1635 }
cd54d859 1636
0ad19a3f 1637 ret = 0;
cd54d859
DL
1638
1639 INFO("mount points have been setup");
0ad19a3f 1640out:
e7938e9e
MN
1641 return ret;
1642}
1643
80a881b2
SH
1644static int setup_mount(const struct lxc_rootfs *rootfs, const char *fstab,
1645 const char *lxc_name)
e7938e9e
MN
1646{
1647 FILE *file;
1648 int ret;
1649
1650 if (!fstab)
1651 return 0;
1652
1653 file = setmntent(fstab, "r");
1654 if (!file) {
1655 SYSERROR("failed to use '%s'", fstab);
1656 return -1;
1657 }
1658
80a881b2 1659 ret = mount_file_entries(rootfs, file, lxc_name);
e7938e9e 1660
0ad19a3f 1661 endmntent(file);
1662 return ret;
1663}
1664
80a881b2
SH
1665static int setup_mount_entries(const struct lxc_rootfs *rootfs, struct lxc_list *mount,
1666 const char *lxc_name)
e7938e9e
MN
1667{
1668 FILE *file;
1669 struct lxc_list *iterator;
1670 char *mount_entry;
1671 int ret;
1672
1673 file = tmpfile();
1674 if (!file) {
1675 ERROR("tmpfile error: %m");
1676 return -1;
1677 }
1678
1679 lxc_list_for_each(iterator, mount) {
1680 mount_entry = iterator->elem;
1d6b1976 1681 fprintf(file, "%s\n", mount_entry);
e7938e9e
MN
1682 }
1683
1684 rewind(file);
1685
80a881b2 1686 ret = mount_file_entries(rootfs, file, lxc_name);
e7938e9e
MN
1687
1688 fclose(file);
1689 return ret;
1690}
1691
81810dd1
DL
1692static int setup_caps(struct lxc_list *caps)
1693{
1694 struct lxc_list *iterator;
1695 char *drop_entry;
d55bc1ad 1696 char *ptr;
81810dd1
DL
1697 int i, capid;
1698
1699 lxc_list_for_each(iterator, caps) {
1700
1701 drop_entry = iterator->elem;
1702
1703 capid = -1;
1704
1705 for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) {
1706
1707 if (strcmp(drop_entry, caps_opt[i].name))
1708 continue;
1709
1710 capid = caps_opt[i].value;
1711 break;
1712 }
1713
d55bc1ad
CS
1714 if (capid < 0) {
1715 /* try to see if it's numeric, so the user may specify
1716 * capabilities that the running kernel knows about but
1717 * we don't */
1718 capid = strtol(drop_entry, &ptr, 10);
1719 if (!ptr || *ptr != '\0' ||
1720 capid == LONG_MIN || capid == LONG_MAX)
1721 /* not a valid number */
1722 capid = -1;
1723 else if (capid > lxc_caps_last_cap())
1724 /* we have a number but it's not a valid
1725 * capability */
1726 capid = -1;
1727 }
1728
81810dd1 1729 if (capid < 0) {
1e11be34
DL
1730 ERROR("unknown capability %s", drop_entry);
1731 return -1;
81810dd1
DL
1732 }
1733
1734 DEBUG("drop capability '%s' (%d)", drop_entry, capid);
1735
1736 if (prctl(PR_CAPBSET_DROP, capid, 0, 0, 0)) {
1737 SYSERROR("failed to remove %s capability", drop_entry);
1738 return -1;
1739 }
1740
1741 }
1742
1743 DEBUG("capabilities has been setup");
1744
1745 return 0;
1746}
1747
0ad19a3f 1748static int setup_hw_addr(char *hwaddr, const char *ifname)
1749{
1750 struct sockaddr sockaddr;
1751 struct ifreq ifr;
1752 int ret, fd;
1753
3cfc0f3a
MN
1754 ret = lxc_convert_mac(hwaddr, &sockaddr);
1755 if (ret) {
1756 ERROR("mac address '%s' conversion failed : %s",
1757 hwaddr, strerror(-ret));
0ad19a3f 1758 return -1;
1759 }
1760
1761 memcpy(ifr.ifr_name, ifname, IFNAMSIZ);
1762 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sockaddr, sizeof(sockaddr));
1763
1764 fd = socket(AF_INET, SOCK_DGRAM, 0);
1765 if (fd < 0) {
3ab87b66 1766 ERROR("socket failure : %s", strerror(errno));
0ad19a3f 1767 return -1;
1768 }
1769
1770 ret = ioctl(fd, SIOCSIFHWADDR, &ifr);
1771 close(fd);
1772 if (ret)
3ab87b66 1773 ERROR("ioctl failure : %s", strerror(errno));
0ad19a3f 1774
cd54d859
DL
1775 DEBUG("mac address '%s' on '%s' has been setup", hwaddr, ifname);
1776
0ad19a3f 1777 return ret;
1778}
1779
82d5ae15 1780static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 1781{
82d5ae15
DL
1782 struct lxc_list *iterator;
1783 struct lxc_inetdev *inetdev;
3cfc0f3a 1784 int err;
0ad19a3f 1785
82d5ae15
DL
1786 lxc_list_for_each(iterator, ip) {
1787
1788 inetdev = iterator->elem;
1789
0093bb8c
DL
1790 err = lxc_ipv4_addr_add(ifindex, &inetdev->addr,
1791 &inetdev->bcast, inetdev->prefix);
3cfc0f3a
MN
1792 if (err) {
1793 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
1794 ifindex, strerror(-err));
82d5ae15
DL
1795 return -1;
1796 }
1797 }
1798
1799 return 0;
0ad19a3f 1800}
1801
82d5ae15 1802static int setup_ipv6_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 1803{
82d5ae15 1804 struct lxc_list *iterator;
7fa9074f 1805 struct lxc_inet6dev *inet6dev;
3cfc0f3a 1806 int err;
0ad19a3f 1807
82d5ae15
DL
1808 lxc_list_for_each(iterator, ip) {
1809
1810 inet6dev = iterator->elem;
1811
b3df193c 1812 err = lxc_ipv6_addr_add(ifindex, &inet6dev->addr,
0093bb8c
DL
1813 &inet6dev->mcast, &inet6dev->acast,
1814 inet6dev->prefix);
3cfc0f3a
MN
1815 if (err) {
1816 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
1817 ifindex, strerror(-err));
82d5ae15 1818 return -1;
3cfc0f3a 1819 }
82d5ae15
DL
1820 }
1821
1822 return 0;
0ad19a3f 1823}
1824
82d5ae15 1825static int setup_netdev(struct lxc_netdev *netdev)
0ad19a3f 1826{
0ad19a3f 1827 char ifname[IFNAMSIZ];
0ad19a3f 1828 char *current_ifname = ifname;
3cfc0f3a 1829 int err;
0ad19a3f 1830
82d5ae15
DL
1831 /* empty network namespace */
1832 if (!netdev->ifindex) {
b0efbac4 1833 if (netdev->flags & IFF_UP) {
d472214b 1834 err = lxc_netdev_up("lo");
3cfc0f3a
MN
1835 if (err) {
1836 ERROR("failed to set the loopback up : %s",
1837 strerror(-err));
82d5ae15
DL
1838 return -1;
1839 }
82d5ae15 1840 }
7b57e8b6 1841 return 0;
0ad19a3f 1842 }
13954cce 1843
82d5ae15
DL
1844 /* retrieve the name of the interface */
1845 if (!if_indextoname(netdev->ifindex, current_ifname)) {
36eb9bde 1846 ERROR("no interface corresponding to index '%d'",
82d5ae15 1847 netdev->ifindex);
0ad19a3f 1848 return -1;
1849 }
13954cce 1850
018ef520 1851 /* default: let the system to choose one interface name */
9d083402 1852 if (!netdev->name)
fb6d9b2f
DL
1853 netdev->name = netdev->type == LXC_NET_PHYS ?
1854 netdev->link : "eth%d";
018ef520 1855
82d5ae15 1856 /* rename the interface name */
b84f58b9 1857 err = lxc_netdev_rename_by_name(ifname, netdev->name);
3cfc0f3a
MN
1858 if (err) {
1859 ERROR("failed to rename %s->%s : %s", ifname, netdev->name,
1860 strerror(-err));
018ef520
DL
1861 return -1;
1862 }
1863
1864 /* Re-read the name of the interface because its name has changed
1865 * and would be automatically allocated by the system
1866 */
82d5ae15 1867 if (!if_indextoname(netdev->ifindex, current_ifname)) {
018ef520 1868 ERROR("no interface corresponding to index '%d'",
82d5ae15 1869 netdev->ifindex);
018ef520 1870 return -1;
0ad19a3f 1871 }
1872
82d5ae15
DL
1873 /* set a mac address */
1874 if (netdev->hwaddr) {
1875 if (setup_hw_addr(netdev->hwaddr, current_ifname)) {
36eb9bde 1876 ERROR("failed to setup hw address for '%s'",
82d5ae15 1877 current_ifname);
0ad19a3f 1878 return -1;
1879 }
1880 }
1881
82d5ae15
DL
1882 /* setup ipv4 addresses on the interface */
1883 if (setup_ipv4_addr(&netdev->ipv4, netdev->ifindex)) {
36eb9bde 1884 ERROR("failed to setup ip addresses for '%s'",
0ad19a3f 1885 ifname);
1886 return -1;
1887 }
1888
82d5ae15
DL
1889 /* setup ipv6 addresses on the interface */
1890 if (setup_ipv6_addr(&netdev->ipv6, netdev->ifindex)) {
36eb9bde 1891 ERROR("failed to setup ipv6 addresses for '%s'",
0ad19a3f 1892 ifname);
1893 return -1;
1894 }
1895
82d5ae15 1896 /* set the network device up */
b0efbac4 1897 if (netdev->flags & IFF_UP) {
3cfc0f3a
MN
1898 int err;
1899
d472214b 1900 err = lxc_netdev_up(current_ifname);
3cfc0f3a
MN
1901 if (err) {
1902 ERROR("failed to set '%s' up : %s", current_ifname,
1903 strerror(-err));
0ad19a3f 1904 return -1;
1905 }
1906
1907 /* the network is up, make the loopback up too */
d472214b 1908 err = lxc_netdev_up("lo");
3cfc0f3a
MN
1909 if (err) {
1910 ERROR("failed to set the loopback up : %s",
1911 strerror(-err));
0ad19a3f 1912 return -1;
1913 }
1914 }
1915
f8fee0e2
MK
1916 /* We can only set up the default routes after bringing
1917 * up the interface, sine bringing up the interface adds
1918 * the link-local routes and we can't add a default
1919 * route if the gateway is not reachable. */
1920
1921 /* setup ipv4 gateway on the interface */
1922 if (netdev->ipv4_gateway) {
1923 if (!(netdev->flags & IFF_UP)) {
1924 ERROR("Cannot add ipv4 gateway for %s when not bringing up the interface", ifname);
1925 return -1;
1926 }
1927
1928 if (lxc_list_empty(&netdev->ipv4)) {
1929 ERROR("Cannot add ipv4 gateway for %s when not assigning an address", ifname);
1930 return -1;
1931 }
1932
1933 err = lxc_ipv4_gateway_add(netdev->ifindex, netdev->ipv4_gateway);
1934 if (err) {
1935 ERROR("failed to setup ipv4 gateway for '%s': %s",
1936 ifname, strerror(-err));
19a26f82
MK
1937 if (netdev->ipv4_gateway_auto) {
1938 char buf[INET_ADDRSTRLEN];
1939 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
1940 ERROR("tried to set autodetected ipv4 gateway '%s'", buf);
1941 }
f8fee0e2
MK
1942 return -1;
1943 }
1944 }
1945
1946 /* setup ipv6 gateway on the interface */
1947 if (netdev->ipv6_gateway) {
1948 if (!(netdev->flags & IFF_UP)) {
1949 ERROR("Cannot add ipv6 gateway for %s when not bringing up the interface", ifname);
1950 return -1;
1951 }
1952
1953 if (lxc_list_empty(&netdev->ipv6) && !IN6_IS_ADDR_LINKLOCAL(netdev->ipv6_gateway)) {
1954 ERROR("Cannot add ipv6 gateway for %s when not assigning an address", ifname);
1955 return -1;
1956 }
1957
1958 err = lxc_ipv6_gateway_add(netdev->ifindex, netdev->ipv6_gateway);
1959 if (err) {
1960 ERROR("failed to setup ipv6 gateway for '%s': %s",
1961 ifname, strerror(-err));
19a26f82
MK
1962 if (netdev->ipv6_gateway_auto) {
1963 char buf[INET6_ADDRSTRLEN];
72d0e1cb 1964 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
19a26f82
MK
1965 ERROR("tried to set autodetected ipv6 gateway '%s'", buf);
1966 }
f8fee0e2
MK
1967 return -1;
1968 }
1969 }
1970
cd54d859
DL
1971 DEBUG("'%s' has been setup", current_ifname);
1972
0ad19a3f 1973 return 0;
1974}
1975
5f4535a3 1976static int setup_network(struct lxc_list *network)
0ad19a3f 1977{
82d5ae15 1978 struct lxc_list *iterator;
82d5ae15 1979 struct lxc_netdev *netdev;
0ad19a3f 1980
5f4535a3 1981 lxc_list_for_each(iterator, network) {
cd54d859 1982
5f4535a3 1983 netdev = iterator->elem;
82d5ae15
DL
1984
1985 if (setup_netdev(netdev)) {
1986 ERROR("failed to setup netdev");
1987 return -1;
1988 }
1989 }
cd54d859 1990
5f4535a3
DL
1991 if (!lxc_list_empty(network))
1992 INFO("network has been setup");
cd54d859
DL
1993
1994 return 0;
0ad19a3f 1995}
1996
7b35f3d6
SH
1997void lxc_rename_phys_nics_on_shutdown(struct lxc_conf *conf)
1998{
1999 int i;
2000
2001 INFO("running to reset %d nic names", conf->num_savednics);
2002 for (i=0; i<conf->num_savednics; i++) {
2003 struct saved_nic *s = &conf->saved_nics[i];
2004 INFO("resetting nic %d to %s\n", s->ifindex, s->orig_name);
2005 lxc_netdev_rename_by_index(s->ifindex, s->orig_name);
2006 free(s->orig_name);
2007 }
2008 conf->num_savednics = 0;
2009 free(conf->saved_nics);
2010}
2011
49684c0b
CS
2012static int setup_private_host_hw_addr(char *veth1)
2013{
2014 struct ifreq ifr;
2015 int err;
2016 int sockfd;
2017
2018 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
2019 if (sockfd < 0)
2020 return -errno;
2021
2022 snprintf((char *)ifr.ifr_name, IFNAMSIZ, "%s", veth1);
2023 err = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
2024 if (err < 0) {
2025 close(sockfd);
2026 return -errno;
2027 }
2028
2029 ifr.ifr_hwaddr.sa_data[0] = 0xfe;
2030 err = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
2031 close(sockfd);
2032 if (err < 0)
2033 return -errno;
2034
7ad84da7
DL
2035 DEBUG("mac address of host interface '%s' changed to private "
2036 "%02x:%02x:%02x:%02x:%02x:%02x", veth1,
2037 ifr.ifr_hwaddr.sa_data[0] & 0xff,
2038 ifr.ifr_hwaddr.sa_data[1] & 0xff,
2039 ifr.ifr_hwaddr.sa_data[2] & 0xff,
2040 ifr.ifr_hwaddr.sa_data[3] & 0xff,
2041 ifr.ifr_hwaddr.sa_data[4] & 0xff,
2042 ifr.ifr_hwaddr.sa_data[5] & 0xff);
49684c0b
CS
2043
2044 return 0;
2045}
2046
ae9242c8
SH
2047static char *default_rootfs_mount = LXCROOTFSMOUNT;
2048
7b379ab3 2049struct lxc_conf *lxc_conf_init(void)
089cd8b8 2050{
7b379ab3 2051 struct lxc_conf *new;
26ddeedd 2052 int i;
7b379ab3
MN
2053
2054 new = malloc(sizeof(*new));
2055 if (!new) {
2056 ERROR("lxc_conf_init : %m");
2057 return NULL;
2058 }
2059 memset(new, 0, sizeof(*new));
2060
cccc74b5 2061 new->personality = -1;
596a818d
DE
2062 new->console.log_path = NULL;
2063 new->console.log_fd = -1;
28a4b0e5 2064 new->console.path = NULL;
63376d7d
DL
2065 new->console.peer = -1;
2066 new->console.master = -1;
2067 new->console.slave = -1;
2068 new->console.name[0] = '\0';
d2e30e99 2069 new->maincmd_fd = -1;
ae9242c8 2070 new->rootfs.mount = default_rootfs_mount;
2f3f41d0 2071 new->kmsg = 1;
7b379ab3
MN
2072 lxc_list_init(&new->cgroup);
2073 lxc_list_init(&new->network);
2074 lxc_list_init(&new->mount_list);
81810dd1 2075 lxc_list_init(&new->caps);
f6d3e3e4 2076 lxc_list_init(&new->id_map);
26ddeedd
SH
2077 for (i=0; i<NUM_LXC_HOOKS; i++)
2078 lxc_list_init(&new->hooks[i]);
e075f5d9
SH
2079#if HAVE_APPARMOR
2080 new->aa_profile = NULL;
2081#endif
2082#if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2083 new->lsm_umount_proc = 0;
2084#endif
7b379ab3
MN
2085
2086 return new;
089cd8b8
DL
2087}
2088
e3b4c4c4 2089static int instanciate_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
0ad19a3f 2090{
8634bc19 2091 char veth1buf[IFNAMSIZ], *veth1;
0e391e57 2092 char veth2buf[IFNAMSIZ], *veth2;
3cfc0f3a 2093 int err;
13954cce 2094
e892973e
DL
2095 if (netdev->priv.veth_attr.pair)
2096 veth1 = netdev->priv.veth_attr.pair;
8634bc19 2097 else {
9ba8130c
SH
2098 err = snprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX");
2099 if (err >= sizeof(veth1buf)) { /* can't *really* happen, but... */
2100 ERROR("veth1 name too long");
2101 return -1;
2102 }
0e391e57 2103 veth1 = mktemp(veth1buf);
74a2b586
JK
2104 /* store away for deconf */
2105 memcpy(netdev->priv.veth_attr.veth1, veth1, IFNAMSIZ);
8634bc19 2106 }
82d5ae15 2107
0e391e57
DL
2108 snprintf(veth2buf, sizeof(veth2buf), "vethXXXXXX");
2109 veth2 = mktemp(veth2buf);
82d5ae15
DL
2110
2111 if (!strlen(veth1) || !strlen(veth2)) {
2112 ERROR("failed to allocate a temporary name");
2113 return -1;
0ad19a3f 2114 }
2115
3cfc0f3a
MN
2116 err = lxc_veth_create(veth1, veth2);
2117 if (err) {
2118 ERROR("failed to create %s-%s : %s", veth1, veth2,
2119 strerror(-err));
6ab9ab6d 2120 return -1;
0ad19a3f 2121 }
13954cce 2122
49684c0b
CS
2123 /* changing the high byte of the mac address to 0xfe, the bridge interface
2124 * will always keep the host's mac address and not take the mac address
2125 * of a container */
2126 err = setup_private_host_hw_addr(veth1);
2127 if (err) {
2128 ERROR("failed to change mac address of host interface '%s' : %s",
2129 veth1, strerror(-err));
2130 goto out_delete;
2131 }
2132
82d5ae15 2133 if (netdev->mtu) {
d472214b 2134 err = lxc_netdev_set_mtu(veth1, atoi(netdev->mtu));
3cfc0f3a 2135 if (!err)
d472214b 2136 err = lxc_netdev_set_mtu(veth2, atoi(netdev->mtu));
3cfc0f3a
MN
2137 if (err) {
2138 ERROR("failed to set mtu '%s' for %s-%s : %s",
2139 netdev->mtu, veth1, veth2, strerror(-err));
eb14c10a 2140 goto out_delete;
75d09f83
DL
2141 }
2142 }
2143
3cfc0f3a
MN
2144 if (netdev->link) {
2145 err = lxc_bridge_attach(netdev->link, veth1);
2146 if (err) {
2147 ERROR("failed to attach '%s' to the bridge '%s' : %s",
2148 veth1, netdev->link, strerror(-err));
2149 goto out_delete;
2150 }
eb14c10a
DL
2151 }
2152
82d5ae15
DL
2153 netdev->ifindex = if_nametoindex(veth2);
2154 if (!netdev->ifindex) {
36eb9bde 2155 ERROR("failed to retrieve the index for %s", veth2);
eb14c10a
DL
2156 goto out_delete;
2157 }
2158
d472214b 2159 err = lxc_netdev_up(veth1);
6e35af2e
DL
2160 if (err) {
2161 ERROR("failed to set %s up : %s", veth1, strerror(-err));
2162 goto out_delete;
0ad19a3f 2163 }
2164
e3b4c4c4 2165 if (netdev->upscript) {
751d9dcd
DL
2166 err = run_script(handler->name, "net", netdev->upscript, "up",
2167 "veth", veth1, (char*) NULL);
2168 if (err)
e3b4c4c4 2169 goto out_delete;
e3b4c4c4
ST
2170 }
2171
82d5ae15
DL
2172 DEBUG("instanciated veth '%s/%s', index is '%d'",
2173 veth1, veth2, netdev->ifindex);
2174
6ab9ab6d 2175 return 0;
eb14c10a
DL
2176
2177out_delete:
b84f58b9 2178 lxc_netdev_delete_by_name(veth1);
6ab9ab6d 2179 return -1;
13954cce 2180}
d957ae2d 2181
74a2b586
JK
2182static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
2183{
2184 char *veth1;
2185 int err;
2186
2187 if (netdev->priv.veth_attr.pair)
2188 veth1 = netdev->priv.veth_attr.pair;
2189 else
2190 veth1 = netdev->priv.veth_attr.veth1;
2191
2192 if (netdev->downscript) {
2193 err = run_script(handler->name, "net", netdev->downscript,
2194 "down", "veth", veth1, (char*) NULL);
2195 if (err)
2196 return -1;
2197 }
2198 return 0;
2199}
2200
e3b4c4c4 2201static int instanciate_macvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
0ad19a3f 2202{
0e391e57 2203 char peerbuf[IFNAMSIZ], *peer;
3cfc0f3a 2204 int err;
d957ae2d
MT
2205
2206 if (!netdev->link) {
2207 ERROR("no link specified for macvlan netdev");
2208 return -1;
2209 }
13954cce 2210
9ba8130c
SH
2211 err = snprintf(peerbuf, sizeof(peerbuf), "mcXXXXXX");
2212 if (err >= sizeof(peerbuf))
2213 return -1;
82d5ae15 2214
0e391e57 2215 peer = mktemp(peerbuf);
82d5ae15
DL
2216 if (!strlen(peer)) {
2217 ERROR("failed to make a temporary name");
2218 return -1;
0ad19a3f 2219 }
2220
3cfc0f3a
MN
2221 err = lxc_macvlan_create(netdev->link, peer,
2222 netdev->priv.macvlan_attr.mode);
2223 if (err) {
2224 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
2225 peer, netdev->link, strerror(-err));
d957ae2d 2226 return -1;
0ad19a3f 2227 }
2228
82d5ae15
DL
2229 netdev->ifindex = if_nametoindex(peer);
2230 if (!netdev->ifindex) {
36eb9bde 2231 ERROR("failed to retrieve the index for %s", peer);
b84f58b9 2232 lxc_netdev_delete_by_name(peer);
d957ae2d 2233 return -1;
22ebac19 2234 }
2235
e3b4c4c4 2236 if (netdev->upscript) {
751d9dcd
DL
2237 err = run_script(handler->name, "net", netdev->upscript, "up",
2238 "macvlan", netdev->link, (char*) NULL);
2239 if (err)
e3b4c4c4 2240 return -1;
e3b4c4c4
ST
2241 }
2242
e892973e
DL
2243 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
2244 peer, netdev->ifindex, netdev->priv.macvlan_attr.mode);
0ad19a3f 2245
d957ae2d 2246 return 0;
0ad19a3f 2247}
2248
74a2b586
JK
2249static int shutdown_macvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
2250{
2251 int err;
2252
2253 if (netdev->downscript) {
2254 err = run_script(handler->name, "net", netdev->downscript,
2255 "down", "macvlan", netdev->link,
2256 (char*) NULL);
2257 if (err)
2258 return -1;
2259 }
2260 return 0;
2261}
2262
26c39028 2263/* XXX: merge with instanciate_macvlan */
e3b4c4c4 2264static int instanciate_vlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
26c39028
JHS
2265{
2266 char peer[IFNAMSIZ];
3cfc0f3a 2267 int err;
26c39028
JHS
2268
2269 if (!netdev->link) {
2270 ERROR("no link specified for vlan netdev");
2271 return -1;
2272 }
2273
9ba8130c
SH
2274 err = snprintf(peer, sizeof(peer), "vlan%d", netdev->priv.vlan_attr.vid);
2275 if (err >= sizeof(peer)) {
2276 ERROR("peer name too long");
2277 return -1;
2278 }
26c39028 2279
3cfc0f3a
MN
2280 err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid);
2281 if (err) {
2282 ERROR("failed to create vlan interface '%s' on '%s' : %s",
2283 peer, netdev->link, strerror(-err));
26c39028
JHS
2284 return -1;
2285 }
2286
2287 netdev->ifindex = if_nametoindex(peer);
2288 if (!netdev->ifindex) {
2289 ERROR("failed to retrieve the ifindex for %s", peer);
b84f58b9 2290 lxc_netdev_delete_by_name(peer);
26c39028
JHS
2291 return -1;
2292 }
2293
e892973e
DL
2294 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
2295 netdev->ifindex);
2296
26c39028
JHS
2297 return 0;
2298}
2299
74a2b586
JK
2300static int shutdown_vlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
2301{
2302 return 0;
2303}
2304
e3b4c4c4 2305static int instanciate_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
0ad19a3f 2306{
6168e99f
DL
2307 if (!netdev->link) {
2308 ERROR("no link specified for the physical interface");
2309 return -1;
2310 }
2311
9d083402 2312 netdev->ifindex = if_nametoindex(netdev->link);
82d5ae15 2313 if (!netdev->ifindex) {
9d083402 2314 ERROR("failed to retrieve the index for %s", netdev->link);
0ad19a3f 2315 return -1;
2316 }
2317
e3b4c4c4
ST
2318 if (netdev->upscript) {
2319 int err;
751d9dcd
DL
2320 err = run_script(handler->name, "net", netdev->upscript,
2321 "up", "phys", netdev->link, (char*) NULL);
2322 if (err)
e3b4c4c4 2323 return -1;
e3b4c4c4
ST
2324 }
2325
82d5ae15 2326 return 0;
0ad19a3f 2327}
2328
74a2b586
JK
2329static int shutdown_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
2330{
2331 int err;
2332
2333 if (netdev->downscript) {
2334 err = run_script(handler->name, "net", netdev->downscript,
2335 "down", "phys", netdev->link, (char*) NULL);
2336 if (err)
2337 return -1;
2338 }
2339 return 0;
2340}
2341
e3b4c4c4 2342static int instanciate_empty(struct lxc_handler *handler, struct lxc_netdev *netdev)
0ad19a3f 2343{
82d5ae15 2344 netdev->ifindex = 0;
e3b4c4c4
ST
2345 if (netdev->upscript) {
2346 int err;
751d9dcd
DL
2347 err = run_script(handler->name, "net", netdev->upscript,
2348 "up", "empty", (char*) NULL);
2349 if (err)
e3b4c4c4 2350 return -1;
e3b4c4c4 2351 }
82d5ae15 2352 return 0;
0ad19a3f 2353}
2354
74a2b586
JK
2355static int shutdown_empty(struct lxc_handler *handler, struct lxc_netdev *netdev)
2356{
2357 int err;
2358
2359 if (netdev->downscript) {
2360 err = run_script(handler->name, "net", netdev->downscript,
2361 "down", "empty", (char*) NULL);
2362 if (err)
2363 return -1;
2364 }
2365 return 0;
2366}
2367
e3b4c4c4 2368int lxc_create_network(struct lxc_handler *handler)
0ad19a3f 2369{
e3b4c4c4 2370 struct lxc_list *network = &handler->conf->network;
82d5ae15 2371 struct lxc_list *iterator;
82d5ae15 2372 struct lxc_netdev *netdev;
0ad19a3f 2373
5f4535a3 2374 lxc_list_for_each(iterator, network) {
0ad19a3f 2375
5f4535a3 2376 netdev = iterator->elem;
13954cce 2377
24654103 2378 if (netdev->type < 0 || netdev->type > LXC_NET_MAXCONFTYPE) {
82d5ae15 2379 ERROR("invalid network configuration type '%d'",
5f4535a3 2380 netdev->type);
82d5ae15
DL
2381 return -1;
2382 }
0ad19a3f 2383
e3b4c4c4 2384 if (netdev_conf[netdev->type](handler, netdev)) {
82d5ae15
DL
2385 ERROR("failed to create netdev");
2386 return -1;
2387 }
e3b4c4c4 2388
0ad19a3f 2389 }
2390
2391 return 0;
2392}
2393
74a2b586 2394void lxc_delete_network(struct lxc_handler *handler)
7fef7a06 2395{
74a2b586 2396 struct lxc_list *network = &handler->conf->network;
7fef7a06
DL
2397 struct lxc_list *iterator;
2398 struct lxc_netdev *netdev;
2399
2400 lxc_list_for_each(iterator, network) {
2401 netdev = iterator->elem;
d472214b 2402
74a2b586 2403 if (netdev->ifindex != 0 && netdev->type == LXC_NET_PHYS) {
d8f8e352
DL
2404 if (lxc_netdev_rename_by_index(netdev->ifindex, netdev->link))
2405 WARN("failed to rename to the initial name the " \
2406 "netdev '%s'", netdev->link);
d472214b 2407 continue;
d8f8e352 2408 }
d472214b 2409
74a2b586
JK
2410 if (netdev_deconf[netdev->type](handler, netdev)) {
2411 WARN("failed to destroy netdev");
2412 }
2413
d8f8e352
DL
2414 /* Recent kernel remove the virtual interfaces when the network
2415 * namespace is destroyed but in case we did not moved the
2416 * interface to the network namespace, we have to destroy it
2417 */
74a2b586
JK
2418 if (netdev->ifindex != 0 &&
2419 lxc_netdev_delete_by_index(netdev->ifindex))
d8f8e352 2420 WARN("failed to remove interface '%s'", netdev->name);
7fef7a06
DL
2421 }
2422}
2423
5f4535a3 2424int lxc_assign_network(struct lxc_list *network, pid_t pid)
0ad19a3f 2425{
82d5ae15 2426 struct lxc_list *iterator;
82d5ae15 2427 struct lxc_netdev *netdev;
3cfc0f3a 2428 int err;
0ad19a3f 2429
5f4535a3 2430 lxc_list_for_each(iterator, network) {
82d5ae15 2431
5f4535a3 2432 netdev = iterator->elem;
82d5ae15 2433
236087a6
DL
2434 /* empty network namespace, nothing to move */
2435 if (!netdev->ifindex)
2436 continue;
2437
d472214b 2438 err = lxc_netdev_move_by_index(netdev->ifindex, pid);
3cfc0f3a
MN
2439 if (err) {
2440 ERROR("failed to move '%s' to the container : %s",
2441 netdev->link, strerror(-err));
82d5ae15
DL
2442 return -1;
2443 }
2444
c1c75c04 2445 DEBUG("move '%s' to '%d'", netdev->name, pid);
0ad19a3f 2446 }
2447
2448 return 0;
2449}
2450
251d0d2a
DE
2451static int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
2452 size_t buf_size)
f6d3e3e4
SH
2453{
2454 char path[PATH_MAX];
e4ccd113 2455 int ret, closeret;
f6d3e3e4
SH
2456 FILE *f;
2457
2458 ret = snprintf(path, PATH_MAX, "/proc/%d/%cid_map", pid, idtype == ID_TYPE_UID ? 'u' : 'g');
2459 if (ret < 0 || ret >= PATH_MAX) {
2460 fprintf(stderr, "%s: path name too long", __func__);
2461 return -E2BIG;
2462 }
2463 f = fopen(path, "w");
2464 if (!f) {
2465 perror("open");
2466 return -EINVAL;
2467 }
251d0d2a 2468 ret = fwrite(buf, buf_size, 1, f);
f6d3e3e4 2469 if (ret < 0)
e4ccd113
SH
2470 SYSERROR("writing id mapping");
2471 closeret = fclose(f);
2472 if (closeret)
2473 SYSERROR("writing id mapping");
2474 return ret < 0 ? ret : closeret;
f6d3e3e4
SH
2475}
2476
2477int lxc_map_ids(struct lxc_list *idmap, pid_t pid)
2478{
2479 struct lxc_list *iterator;
2480 struct id_map *map;
2481 int ret = 0;
251d0d2a 2482 enum idtype type;
4f7521b4 2483 char *buf = NULL, *pos;
251d0d2a
DE
2484
2485 for(type = ID_TYPE_UID; type <= ID_TYPE_GID; type++) {
4f7521b4
SH
2486 int left, fill;
2487
2488 pos = buf;
251d0d2a 2489 lxc_list_for_each(iterator, idmap) {
4f7521b4
SH
2490 /* The kernel only takes <= 4k for writes to /proc/<nr>/[ug]id_map */
2491 if (!buf)
2492 buf = pos = malloc(4096);
2493 if (!buf)
2494 return -ENOMEM;
2495
251d0d2a
DE
2496 map = iterator->elem;
2497 if (map->idtype == type) {
2498 left = 4096 - (pos - buf);
2499 fill = snprintf(pos, left, "%lu %lu %lu\n",
2500 map->nsid, map->hostid, map->range);
2501 if (fill <= 0 || fill >= left)
2502 SYSERROR("snprintf failed, too many mappings");
2503 pos += fill;
2504 }
2505 }
4f7521b4
SH
2506 if (pos == buf) // no mappings were found
2507 continue;
251d0d2a 2508 ret = write_id_mapping(type, pid, buf, pos-buf);
f6d3e3e4
SH
2509 if (ret)
2510 break;
2511 }
251d0d2a 2512
4f7521b4
SH
2513 if (buf)
2514 free(buf);
f6d3e3e4
SH
2515 return ret;
2516}
2517
19a26f82
MK
2518int lxc_find_gateway_addresses(struct lxc_handler *handler)
2519{
2520 struct lxc_list *network = &handler->conf->network;
2521 struct lxc_list *iterator;
2522 struct lxc_netdev *netdev;
2523 int link_index;
2524
2525 lxc_list_for_each(iterator, network) {
2526 netdev = iterator->elem;
2527
2528 if (!netdev->ipv4_gateway_auto && !netdev->ipv6_gateway_auto)
2529 continue;
2530
2531 if (netdev->type != LXC_NET_VETH && netdev->type != LXC_NET_MACVLAN) {
2532 ERROR("gateway = auto only supported for "
2533 "veth and macvlan");
2534 return -1;
2535 }
2536
2537 if (!netdev->link) {
2538 ERROR("gateway = auto needs a link interface");
2539 return -1;
2540 }
2541
2542 link_index = if_nametoindex(netdev->link);
2543 if (!link_index)
2544 return -EINVAL;
2545
2546 if (netdev->ipv4_gateway_auto) {
2547 if (lxc_ipv4_addr_get(link_index, &netdev->ipv4_gateway)) {
2548 ERROR("failed to automatically find ipv4 gateway "
2549 "address from link interface '%s'", netdev->link);
2550 return -1;
2551 }
2552 }
2553
2554 if (netdev->ipv6_gateway_auto) {
2555 if (lxc_ipv6_addr_get(link_index, &netdev->ipv6_gateway)) {
2556 ERROR("failed to automatically find ipv6 gateway "
2557 "address from link interface '%s'", netdev->link);
2558 return -1;
2559 }
2560 }
2561 }
2562
2563 return 0;
2564}
2565
5e4a62bf 2566int lxc_create_tty(const char *name, struct lxc_conf *conf)
b0a33c1e 2567{
5e4a62bf 2568 struct lxc_tty_info *tty_info = &conf->tty_info;
985d15b1 2569 int i;
b0a33c1e 2570
5e4a62bf
DL
2571 /* no tty in the configuration */
2572 if (!conf->tty)
b0a33c1e 2573 return 0;
2574
13954cce 2575 tty_info->pty_info =
e4e7d59d 2576 malloc(sizeof(*tty_info->pty_info)*conf->tty);
b0a33c1e 2577 if (!tty_info->pty_info) {
36eb9bde 2578 SYSERROR("failed to allocate pty_info");
985d15b1 2579 return -1;
b0a33c1e 2580 }
2581
985d15b1 2582 for (i = 0; i < conf->tty; i++) {
13954cce 2583
b0a33c1e 2584 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
2585
13954cce 2586 if (openpty(&pty_info->master, &pty_info->slave,
b0a33c1e 2587 pty_info->name, NULL, NULL)) {
36eb9bde 2588 SYSERROR("failed to create pty #%d", i);
985d15b1
MT
2589 tty_info->nbtty = i;
2590 lxc_delete_tty(tty_info);
2591 return -1;
b0a33c1e 2592 }
2593
5332bb84
DL
2594 DEBUG("allocated pty '%s' (%d/%d)",
2595 pty_info->name, pty_info->master, pty_info->slave);
2596
b035ad62
MS
2597 /* Prevent leaking the file descriptors to the container */
2598 fcntl(pty_info->master, F_SETFD, FD_CLOEXEC);
2599 fcntl(pty_info->slave, F_SETFD, FD_CLOEXEC);
2600
b0a33c1e 2601 pty_info->busy = 0;
2602 }
2603
985d15b1 2604 tty_info->nbtty = conf->tty;
1ac470c0
DL
2605
2606 INFO("tty's configured");
2607
985d15b1 2608 return 0;
b0a33c1e 2609}
2610
2611void lxc_delete_tty(struct lxc_tty_info *tty_info)
2612{
2613 int i;
2614
2615 for (i = 0; i < tty_info->nbtty; i++) {
2616 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
2617
2618 close(pty_info->master);
2619 close(pty_info->slave);
2620 }
2621
2622 free(tty_info->pty_info);
2623 tty_info->nbtty = 0;
2624}
2625
f6d3e3e4
SH
2626/*
2627 * given a host uid, return the ns uid if it is mapped.
2628 * if it is not mapped, return the original host id.
2629 */
2630static int shiftid(struct lxc_conf *c, int uid, enum idtype w)
2631{
2632 struct lxc_list *iterator;
2633 struct id_map *map;
2634 int low, high;
2635
2636 lxc_list_for_each(iterator, &c->id_map) {
2637 map = iterator->elem;
2638 if (map->idtype != w)
2639 continue;
2640
2641 low = map->nsid;
2642 high = map->nsid + map->range;
2643 if (uid < low || uid >= high)
2644 continue;
2645
2646 return uid - low + map->hostid;
2647 }
2648
2649 return uid;
2650}
2651
2652/*
2653 * Take a pathname for a file created on the host, and map the uid and gid
2654 * into the container if needed. (Used for ttys)
2655 */
2656static int uid_shift_file(char *path, struct lxc_conf *c)
2657{
2658 struct stat statbuf;
2659 int newuid, newgid;
2660
2661 if (stat(path, &statbuf)) {
2662 SYSERROR("stat(%s)", path);
2663 return -1;
2664 }
2665
2666 newuid = shiftid(c, statbuf.st_uid, ID_TYPE_UID);
2667 newgid = shiftid(c, statbuf.st_gid, ID_TYPE_GID);
2668 if (newuid != statbuf.st_uid || newgid != statbuf.st_gid) {
20087962 2669 DEBUG("chowning %s from %d:%d to %d:%d\n", path, (int)statbuf.st_uid, (int)statbuf.st_gid, newuid, newgid);
f6d3e3e4
SH
2670 if (chown(path, newuid, newgid)) {
2671 SYSERROR("chown(%s)", path);
2672 return -1;
2673 }
2674 }
2675 return 0;
2676}
2677
2678int uid_shift_ttys(int pid, struct lxc_conf *conf)
2679{
2680 int i, ret;
2681 struct lxc_tty_info *tty_info = &conf->tty_info;
2682 char path[MAXPATHLEN];
2683 char *ttydir = conf->ttydir;
2684
2685 if (!conf->rootfs.path)
2686 return 0;
2687 /* first the console */
2688 ret = snprintf(path, sizeof(path), "/proc/%d/root/dev/%s/console", pid, ttydir ? ttydir : "");
2689 if (ret < 0 || ret >= sizeof(path)) {
2690 ERROR("console path too long\n");
2691 return -1;
2692 }
2693 if (uid_shift_file(path, conf)) {
2694 DEBUG("Failed to chown the console %s.\n", path);
2695 return -1;
2696 }
2697 for (i=0; i< tty_info->nbtty; i++) {
2698 ret = snprintf(path, sizeof(path), "/proc/%d/root/dev/%s/tty%d",
2699 pid, ttydir ? ttydir : "", i + 1);
2700 if (ret < 0 || ret >= sizeof(path)) {
2701 ERROR("pathname too long for ttys");
2702 return -1;
2703 }
2704 if (uid_shift_file(path, conf)) {
2705 DEBUG("Failed to chown pty %s.\n", path);
2706 return -1;
2707 }
2708 }
2709
2710 return 0;
2711}
2712
571e6ec8 2713int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
0ad19a3f 2714{
e075f5d9
SH
2715#if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
2716 int mounted;
2717#endif
2718
571e6ec8 2719 if (setup_utsname(lxc_conf->utsname)) {
36eb9bde 2720 ERROR("failed to setup the utsname for '%s'", name);
95b5ffaf 2721 return -1;
0ad19a3f 2722 }
2723
5f4535a3 2724 if (setup_network(&lxc_conf->network)) {
36eb9bde 2725 ERROR("failed to setup the network for '%s'", name);
95b5ffaf 2726 return -1;
0ad19a3f 2727 }
2728
89eaa05e
SH
2729 if (run_lxc_hooks(name, "pre-mount", lxc_conf)) {
2730 ERROR("failed to run pre-mount hooks for container '%s'.", name);
2731 return -1;
2732 }
5ea6163a 2733
cc28d0b0 2734 if (setup_rootfs(lxc_conf)) {
ac778708 2735 ERROR("failed to setup rootfs for '%s'", name);
95b5ffaf 2736 return -1;
0ad19a3f 2737 }
2738
c6883f38 2739 if (lxc_conf->autodev) {
91c3830e
SH
2740 if (mount_autodev(lxc_conf->rootfs.mount)) {
2741 ERROR("failed to mount /dev in the container");
c6883f38
SH
2742 return -1;
2743 }
2744 }
2745
80a881b2 2746 if (setup_mount(&lxc_conf->rootfs, lxc_conf->fstab, name)) {
36eb9bde 2747 ERROR("failed to setup the mounts for '%s'", name);
95b5ffaf 2748 return -1;
576f946d 2749 }
2750
c1dc38c2 2751 if (!lxc_list_empty(&lxc_conf->mount_list) && setup_mount_entries(&lxc_conf->rootfs, &lxc_conf->mount_list, name)) {
e7938e9e
MN
2752 ERROR("failed to setup the mount entries for '%s'", name);
2753 return -1;
2754 }
2755
773fb9ca
SH
2756 if (run_lxc_hooks(name, "mount", lxc_conf)) {
2757 ERROR("failed to run mount hooks for container '%s'.", name);
2758 return -1;
2759 }
2760
91c3830e 2761 if (lxc_conf->autodev) {
f7bee6c6
MW
2762 if (run_lxc_hooks(name, "autodev", lxc_conf)) {
2763 ERROR("failed to run autodev hooks for container '%s'.", name);
2764 return -1;
2765 }
91c3830e
SH
2766 if (setup_autodev(lxc_conf->rootfs.mount)) {
2767 ERROR("failed to populate /dev in the container");
2768 return -1;
2769 }
2770 }
2771
7c6ef2a2 2772 if (setup_console(&lxc_conf->rootfs, &lxc_conf->console, lxc_conf->ttydir)) {
36eb9bde 2773 ERROR("failed to setup the console for '%s'", name);
95b5ffaf 2774 return -1;
6e590161 2775 }
2776
7e0e1d94
AV
2777 if (lxc_conf->kmsg) {
2778 if (setup_kmsg(&lxc_conf->rootfs, &lxc_conf->console)) // don't fail
2779 ERROR("failed to setup kmsg for '%s'", name);
2780 }
1bd051a6 2781
7c6ef2a2 2782 if (setup_tty(&lxc_conf->rootfs, &lxc_conf->tty_info, lxc_conf->ttydir)) {
36eb9bde 2783 ERROR("failed to setup the ttys for '%s'", name);
95b5ffaf 2784 return -1;
b0a33c1e 2785 }
2786
e075f5d9 2787#if HAVE_APPARMOR /* || HAVE_SMACK || HAVE_SELINUX */
9ac3ffb5
SG
2788 INFO("rootfs path is .%s., mount is .%s.", lxc_conf->rootfs.path,
2789 lxc_conf->rootfs.mount);
2790 if (lxc_conf->rootfs.path == NULL || strlen(lxc_conf->rootfs.path) == 0)
2791 mounted = 0;
2792 else
2793 mounted = lsm_mount_proc_if_needed(lxc_conf->rootfs.path, lxc_conf->rootfs.mount);
e075f5d9
SH
2794 if (mounted == -1) {
2795 SYSERROR("failed to mount /proc in the container.");
2796 return -1;
2797 } else if (mounted == 1) {
2798 lxc_conf->lsm_umount_proc = 1;
2799 }
2800#endif
2801
ac778708 2802 if (setup_pivot_root(&lxc_conf->rootfs)) {
36eb9bde 2803 ERROR("failed to set rootfs for '%s'", name);
95b5ffaf 2804 return -1;
ed502555 2805 }
2806
571e6ec8 2807 if (setup_pts(lxc_conf->pts)) {
36eb9bde 2808 ERROR("failed to setup the new pts instance");
95b5ffaf 2809 return -1;
3c26f34e 2810 }
2811
cccc74b5
DL
2812 if (setup_personality(lxc_conf->personality)) {
2813 ERROR("failed to setup personality");
2814 return -1;
2815 }
2816
f6d3e3e4
SH
2817 if (lxc_list_empty(&lxc_conf->id_map)) {
2818 if (setup_caps(&lxc_conf->caps)) {
2819 ERROR("failed to drop capabilities");
2820 return -1;
2821 }
81810dd1
DL
2822 }
2823
cd54d859
DL
2824 NOTICE("'%s' is setup.", name);
2825
0ad19a3f 2826 return 0;
2827}
26ddeedd
SH
2828
2829int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf)
2830{
2831 int which = -1;
2832 struct lxc_list *it;
2833
2834 if (strcmp(hook, "pre-start") == 0)
2835 which = LXCHOOK_PRESTART;
5ea6163a
SH
2836 else if (strcmp(hook, "pre-mount") == 0)
2837 which = LXCHOOK_PREMOUNT;
26ddeedd
SH
2838 else if (strcmp(hook, "mount") == 0)
2839 which = LXCHOOK_MOUNT;
f7bee6c6
MW
2840 else if (strcmp(hook, "autodev") == 0)
2841 which = LXCHOOK_AUTODEV;
26ddeedd
SH
2842 else if (strcmp(hook, "start") == 0)
2843 which = LXCHOOK_START;
2844 else if (strcmp(hook, "post-stop") == 0)
2845 which = LXCHOOK_POSTSTOP;
2846 else
2847 return -1;
2848 lxc_list_for_each(it, &conf->hooks[which]) {
2849 int ret;
2850 char *hookname = it->elem;
2851 ret = run_script(name, "lxc", hookname, hook, NULL);
2852 if (ret)
2853 return ret;
2854 }
2855 return 0;
2856}
72d0e1cb 2857
427b3a21 2858static void lxc_remove_nic(struct lxc_list *it)
72d0e1cb
SG
2859{
2860 struct lxc_netdev *netdev = it->elem;
9ebb03ad 2861 struct lxc_list *it2,*next;
72d0e1cb
SG
2862
2863 lxc_list_del(it);
2864
2865 if (netdev->link)
2866 free(netdev->link);
2867 if (netdev->name)
2868 free(netdev->name);
2869 if (netdev->upscript)
2870 free(netdev->upscript);
2871 if (netdev->hwaddr)
2872 free(netdev->hwaddr);
2873 if (netdev->mtu)
2874 free(netdev->mtu);
2875 if (netdev->ipv4_gateway)
2876 free(netdev->ipv4_gateway);
2877 if (netdev->ipv6_gateway)
2878 free(netdev->ipv6_gateway);
9ebb03ad 2879 lxc_list_for_each_safe(it2, &netdev->ipv4, next) {
72d0e1cb
SG
2880 lxc_list_del(it2);
2881 free(it2->elem);
2882 free(it2);
2883 }
9ebb03ad 2884 lxc_list_for_each_safe(it2, &netdev->ipv6, next) {
72d0e1cb
SG
2885 lxc_list_del(it2);
2886 free(it2->elem);
2887 free(it2);
2888 }
d95db067 2889 free(netdev);
72d0e1cb
SG
2890 free(it);
2891}
2892
2893/* we get passed in something like '0', '0.ipv4' or '1.ipv6' */
12a50cc6 2894int lxc_clear_nic(struct lxc_conf *c, const char *key)
72d0e1cb
SG
2895{
2896 char *p1;
2897 int ret, idx, i;
2898 struct lxc_list *it;
2899 struct lxc_netdev *netdev;
2900
2901 p1 = index(key, '.');
2902 if (!p1 || *(p1+1) == '\0')
2903 p1 = NULL;
2904
2905 ret = sscanf(key, "%d", &idx);
2906 if (ret != 1) return -1;
2907 if (idx < 0)
2908 return -1;
2909
2910 i = 0;
2911 lxc_list_for_each(it, &c->network) {
2912 if (i == idx)
2913 break;
2914 i++;
2915 }
2916 if (i < idx) // we don't have that many nics defined
2917 return -1;
2918
2919 if (!it || !it->elem)
2920 return -1;
2921
2922 netdev = it->elem;
2923
2924 if (!p1) {
2925 lxc_remove_nic(it);
2926 } else if (strcmp(p1, "ipv4") == 0) {
9ebb03ad
DE
2927 struct lxc_list *it2,*next;
2928 lxc_list_for_each_safe(it2, &netdev->ipv4, next) {
72d0e1cb
SG
2929 lxc_list_del(it2);
2930 free(it2->elem);
2931 free(it2);
2932 }
2933 } else if (strcmp(p1, "ipv6") == 0) {
9ebb03ad
DE
2934 struct lxc_list *it2,*next;
2935 lxc_list_for_each_safe(it2, &netdev->ipv6, next) {
72d0e1cb
SG
2936 lxc_list_del(it2);
2937 free(it2->elem);
2938 free(it2);
2939 }
2940 } else if (strcmp(p1, "link") == 0) {
2941 if (netdev->link) {
2942 free(netdev->link);
2943 netdev->link = NULL;
2944 }
2945 } else if (strcmp(p1, "name") == 0) {
2946 if (netdev->name) {
2947 free(netdev->name);
2948 netdev->name = NULL;
2949 }
2950 } else if (strcmp(p1, "script.up") == 0) {
2951 if (netdev->upscript) {
2952 free(netdev->upscript);
2953 netdev->upscript = NULL;
2954 }
2955 } else if (strcmp(p1, "hwaddr") == 0) {
2956 if (netdev->hwaddr) {
2957 free(netdev->hwaddr);
2958 netdev->hwaddr = NULL;
2959 }
2960 } else if (strcmp(p1, "mtu") == 0) {
2961 if (netdev->mtu) {
2962 free(netdev->mtu);
2963 netdev->mtu = NULL;
2964 }
2965 } else if (strcmp(p1, "ipv4_gateway") == 0) {
2966 if (netdev->ipv4_gateway) {
2967 free(netdev->ipv4_gateway);
2968 netdev->ipv4_gateway = NULL;
2969 }
2970 } else if (strcmp(p1, "ipv6_gateway") == 0) {
2971 if (netdev->ipv6_gateway) {
2972 free(netdev->ipv6_gateway);
2973 netdev->ipv6_gateway = NULL;
2974 }
2975 }
2976 else return -1;
2977
2978 return 0;
2979}
2980
2981int lxc_clear_config_network(struct lxc_conf *c)
2982{
9ebb03ad
DE
2983 struct lxc_list *it,*next;
2984 lxc_list_for_each_safe(it, &c->network, next) {
72d0e1cb
SG
2985 lxc_remove_nic(it);
2986 }
2987 return 0;
2988}
2989
2990int lxc_clear_config_caps(struct lxc_conf *c)
2991{
9ebb03ad 2992 struct lxc_list *it,*next;
72d0e1cb 2993
9ebb03ad 2994 lxc_list_for_each_safe(it, &c->caps, next) {
72d0e1cb
SG
2995 lxc_list_del(it);
2996 free(it->elem);
2997 free(it);
2998 }
2999 return 0;
3000}
3001
12a50cc6 3002int lxc_clear_cgroups(struct lxc_conf *c, const char *key)
72d0e1cb 3003{
9ebb03ad 3004 struct lxc_list *it,*next;
72d0e1cb 3005 bool all = false;
12a50cc6 3006 const char *k = key + 11;
72d0e1cb
SG
3007
3008 if (strcmp(key, "lxc.cgroup") == 0)
3009 all = true;
3010
9ebb03ad 3011 lxc_list_for_each_safe(it, &c->cgroup, next) {
72d0e1cb
SG
3012 struct lxc_cgroup *cg = it->elem;
3013 if (!all && strcmp(cg->subsystem, k) != 0)
3014 continue;
3015 lxc_list_del(it);
3016 free(cg->subsystem);
3017 free(cg->value);
3018 free(cg);
3019 free(it);
3020 }
3021 return 0;
3022}
3023
3024int lxc_clear_mount_entries(struct lxc_conf *c)
3025{
9ebb03ad 3026 struct lxc_list *it,*next;
72d0e1cb 3027
9ebb03ad 3028 lxc_list_for_each_safe(it, &c->mount_list, next) {
72d0e1cb
SG
3029 lxc_list_del(it);
3030 free(it->elem);
3031 free(it);
3032 }
3033 return 0;
3034}
3035
12a50cc6 3036int lxc_clear_hooks(struct lxc_conf *c, const char *key)
72d0e1cb 3037{
9ebb03ad 3038 struct lxc_list *it,*next;
17ed13a3 3039 bool all = false, done = false;
12a50cc6 3040 const char *k = key + 9;
72d0e1cb
SG
3041 int i;
3042
17ed13a3
SH
3043 if (strcmp(key, "lxc.hook") == 0)
3044 all = true;
3045
72d0e1cb 3046 for (i=0; i<NUM_LXC_HOOKS; i++) {
17ed13a3 3047 if (all || strcmp(k, lxchook_names[i]) == 0) {
9ebb03ad 3048 lxc_list_for_each_safe(it, &c->hooks[i], next) {
17ed13a3
SH
3049 lxc_list_del(it);
3050 free(it->elem);
3051 free(it);
3052 }
3053 done = true;
72d0e1cb
SG
3054 }
3055 }
17ed13a3
SH
3056
3057 if (!done) {
3058 ERROR("Invalid hook key: %s", key);
3059 return -1;
3060 }
72d0e1cb
SG
3061 return 0;
3062}
8eb5694b 3063
7b35f3d6
SH
3064void lxc_clear_saved_nics(struct lxc_conf *conf)
3065{
3066 int i;
3067
3068 if (!conf->num_savednics)
3069 return;
3070 for (i=0; i < conf->num_savednics; i++)
3071 free(conf->saved_nics[i].orig_name);
3072 conf->saved_nics = 0;
3073 free(conf->saved_nics);
3074}
3075
8eb5694b
SH
3076void lxc_conf_free(struct lxc_conf *conf)
3077{
3078 if (!conf)
3079 return;
3080 if (conf->console.path)
3081 free(conf->console.path);
ae9242c8 3082 if (conf->rootfs.mount != default_rootfs_mount)
8eb5694b 3083 free(conf->rootfs.mount);
d95db067
DE
3084 if (conf->rootfs.path)
3085 free(conf->rootfs.path);
3086 if (conf->utsname)
3087 free(conf->utsname);
3088 if (conf->ttydir)
3089 free(conf->ttydir);
3090 if (conf->fstab)
3091 free(conf->fstab);
8eb5694b 3092 lxc_clear_config_network(conf);
1f530df6 3093#if HAVE_APPARMOR
8eb5694b
SH
3094 if (conf->aa_profile)
3095 free(conf->aa_profile);
1f530df6 3096#endif
769872f9 3097 lxc_seccomp_free(conf);
8eb5694b
SH
3098 lxc_clear_config_caps(conf);
3099 lxc_clear_cgroups(conf, "lxc.cgroup");
17ed13a3 3100 lxc_clear_hooks(conf, "lxc.hook");
8eb5694b 3101 lxc_clear_mount_entries(conf);
7b35f3d6 3102 lxc_clear_saved_nics(conf);
8eb5694b
SH
3103 free(conf);
3104}