]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/conf.c
add a configure option to set a rootfs mount point
[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:
7 * Daniel Lezcano <dlezcano at fr.ibm.com>
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>
27#include <errno.h>
28#include <string.h>
29#include <dirent.h>
30#include <mntent.h>
31#include <unistd.h>
b0a33c1e 32#include <pty.h>
0ad19a3f 33
34#include <sys/types.h>
35#include <sys/utsname.h>
36#include <sys/param.h>
37#include <sys/stat.h>
38#include <sys/socket.h>
39#include <sys/mount.h>
40#include <sys/mman.h>
81810dd1
DL
41#include <sys/prctl.h>
42#include <sys/capability.h>
0ad19a3f 43
44#include <arpa/inet.h>
45#include <fcntl.h>
46#include <netinet/in.h>
47#include <net/if.h>
6f4a3756 48#include <libgen.h>
0ad19a3f 49
e5bda9ee 50#include "network.h"
51#include "error.h"
b2718c72 52#include "parse.h"
881450bb 53#include "config.h"
1b09f2c0
DL
54#include "utils.h"
55#include "conf.h"
56#include "log.h"
57#include "lxc.h" /* for lxc_cgroup_set() */
36eb9bde
CLG
58
59lxc_log_define(lxc_conf, lxc);
e5bda9ee 60
0ad19a3f 61#define MAXHWLEN 18
62#define MAXINDEXLEN 20
442cbbe6 63#define MAXMTULEN 16
0ad19a3f 64#define MAXLINELEN 128
65
fdc03323
DL
66#ifndef MS_REC
67#define MS_REC 16384
68#endif
69
c08556c6
DL
70#ifndef MNT_DETACH
71#define MNT_DETACH 2
72#endif
73
b09094da
MN
74#ifndef CAP_SETFCAP
75#define CAP_SETFCAP 31
76#endif
77
78#ifndef CAP_MAC_OVERRIDE
79#define CAP_MAC_OVERRIDE 32
80#endif
81
82#ifndef CAP_MAC_ADMIN
83#define CAP_MAC_ADMIN 33
84#endif
85
86#ifndef PR_CAPBSET_DROP
87#define PR_CAPBSET_DROP 24
88#endif
89
bf601689
MH
90extern int pivot_root(const char * new_root, const char * put_old);
91
82d5ae15 92typedef int (*instanciate_cb)(struct lxc_netdev *);
0ad19a3f 93
998ac676
RT
94struct mount_opt {
95 char *name;
96 int clear;
97 int flag;
98};
99
81810dd1
DL
100struct caps_opt {
101 char *name;
102 int value;
103};
104
82d5ae15
DL
105static int instanciate_veth(struct lxc_netdev *);
106static int instanciate_macvlan(struct lxc_netdev *);
26c39028 107static int instanciate_vlan(struct lxc_netdev *);
82d5ae15
DL
108static int instanciate_phys(struct lxc_netdev *);
109static int instanciate_empty(struct lxc_netdev *);
110
24654103
DL
111static instanciate_cb netdev_conf[LXC_NET_MAXCONFTYPE + 1] = {
112 [LXC_NET_VETH] = instanciate_veth,
113 [LXC_NET_MACVLAN] = instanciate_macvlan,
114 [LXC_NET_VLAN] = instanciate_vlan,
115 [LXC_NET_PHYS] = instanciate_phys,
116 [LXC_NET_EMPTY] = instanciate_empty,
0ad19a3f 117};
118
998ac676
RT
119static struct mount_opt mount_opt[] = {
120 { "defaults", 0, 0 },
121 { "ro", 0, MS_RDONLY },
122 { "rw", 1, MS_RDONLY },
123 { "suid", 1, MS_NOSUID },
124 { "nosuid", 0, MS_NOSUID },
125 { "dev", 1, MS_NODEV },
126 { "nodev", 0, MS_NODEV },
127 { "exec", 1, MS_NOEXEC },
128 { "noexec", 0, MS_NOEXEC },
129 { "sync", 0, MS_SYNCHRONOUS },
130 { "async", 1, MS_SYNCHRONOUS },
131 { "remount", 0, MS_REMOUNT },
132 { "mand", 0, MS_MANDLOCK },
133 { "nomand", 1, MS_MANDLOCK },
134 { "atime", 1, MS_NOATIME },
135 { "noatime", 0, MS_NOATIME },
136 { "diratime", 1, MS_NODIRATIME },
137 { "nodiratime", 0, MS_NODIRATIME },
138 { "bind", 0, MS_BIND },
139 { "rbind", 0, MS_BIND|MS_REC },
140 { NULL, 0, 0 },
141};
142
81810dd1 143static struct caps_opt caps_opt[] = {
1e11be34
DL
144 { "chown", CAP_CHOWN },
145 { "dac_override", CAP_DAC_OVERRIDE },
146 { "dac_read_search", CAP_DAC_READ_SEARCH },
147 { "fowner", CAP_FOWNER },
148 { "fsetid", CAP_FSETID },
81810dd1
DL
149 { "kill", CAP_KILL },
150 { "setgid", CAP_SETGID },
151 { "setuid", CAP_SETUID },
152 { "setpcap", CAP_SETPCAP },
153 { "linux_immutable", CAP_LINUX_IMMUTABLE },
154 { "net_bind_service", CAP_NET_BIND_SERVICE },
155 { "net_broadcast", CAP_NET_BROADCAST },
156 { "net_admin", CAP_NET_ADMIN },
157 { "net_raw", CAP_NET_RAW },
158 { "ipc_lock", CAP_IPC_LOCK },
159 { "ipc_owner", CAP_IPC_OWNER },
160 { "sys_module", CAP_SYS_MODULE },
161 { "sys_rawio", CAP_SYS_RAWIO },
162 { "sys_chroot", CAP_SYS_CHROOT },
163 { "sys_ptrace", CAP_SYS_PTRACE },
164 { "sys_pacct", CAP_SYS_PACCT },
165 { "sys_admin", CAP_SYS_ADMIN },
166 { "sys_boot", CAP_SYS_BOOT },
167 { "sys_nice", CAP_SYS_NICE },
168 { "sys_resource", CAP_SYS_RESOURCE },
169 { "sys_time", CAP_SYS_TIME },
170 { "sys_tty_config", CAP_SYS_TTY_CONFIG },
171 { "mknod", CAP_MKNOD },
172 { "lease", CAP_LEASE },
173 { "audit_write", CAP_AUDIT_WRITE },
174 { "audit_control", CAP_AUDIT_CONTROL },
175 { "setfcap", CAP_SETFCAP },
176 { "mac_override", CAP_MAC_OVERRIDE },
177 { "mac_admin", CAP_MAC_ADMIN },
81810dd1
DL
178};
179
180
7a7ff0c6 181static int configure_find_fstype_cb(char* buffer, void *data)
78ae2fcc 182{
183 struct cbarg {
184 const char *rootfs;
185 const char *testdir;
186 char *fstype;
187 int mntopt;
188 } *cbarg = data;
189
190 char *fstype;
191
192 /* we don't try 'nodev' entries */
193 if (strstr(buffer, "nodev"))
194 return 0;
195
196 fstype = buffer;
b2718c72 197 fstype += lxc_char_left_gc(fstype, strlen(fstype));
198 fstype[lxc_char_right_gc(fstype, strlen(fstype))] = '\0';
78ae2fcc 199
200 if (mount(cbarg->rootfs, cbarg->testdir, fstype, cbarg->mntopt, NULL))
201 return 0;
202
203 /* found ! */
204 umount(cbarg->testdir);
205 strcpy(cbarg->fstype, fstype);
206
207 return 1;
208}
209
210/* find the filesystem type with brute force */
211static int configure_find_fstype(const char *rootfs, char *fstype, int mntopt)
212{
213 int i, found;
78ae2fcc 214
215 struct cbarg {
216 const char *rootfs;
217 const char *testdir;
218 char *fstype;
219 int mntopt;
220 } cbarg = {
221 .rootfs = rootfs,
222 .fstype = fstype,
223 .mntopt = mntopt,
224 };
225
226 /* first we check with /etc/filesystems, in case the modules
227 * are auto-loaded and fall back to the supported kernel fs
228 */
229 char *fsfile[] = {
230 "/etc/filesystems",
231 "/proc/filesystems",
232 };
233
234 cbarg.testdir = tempnam("/tmp", "lxc-");
235 if (!cbarg.testdir) {
36eb9bde 236 SYSERROR("failed to build a temp name");
78ae2fcc 237 return -1;
238 }
239
240 if (mkdir(cbarg.testdir, 0755)) {
36eb9bde 241 SYSERROR("failed to create temporary directory");
78ae2fcc 242 return -1;
243 }
244
245 for (i = 0; i < sizeof(fsfile)/sizeof(fsfile[0]); i++) {
246
b2718c72 247 found = lxc_file_for_each_line(fsfile[i],
248 configure_find_fstype_cb,
2382ecff 249 &cbarg);
78ae2fcc 250
251 if (found < 0) {
36eb9bde 252 SYSERROR("failed to read '%s'", fsfile[i]);
78ae2fcc 253 goto out;
254 }
255
256 if (found)
257 break;
258 }
259
260 if (!found) {
36eb9bde 261 ERROR("failed to determine fs type for '%s'", rootfs);
78ae2fcc 262 goto out;
263 }
264
265out:
266 rmdir(cbarg.testdir);
267 return found - 1;
268}
269
270static int configure_rootfs_dir_cb(const char *rootfs, const char *absrootfs,
271 FILE *f)
272{
fdc03323 273 return fprintf(f, "%s %s none rbind 0 0\n", absrootfs, rootfs);
78ae2fcc 274}
275
276static int configure_rootfs_blk_cb(const char *rootfs, const char *absrootfs,
277 FILE *f)
278{
279 char fstype[MAXPATHLEN];
280
281 if (configure_find_fstype(absrootfs, fstype, 0)) {
36eb9bde 282 ERROR("failed to configure mount for block device '%s'",
78ae2fcc 283 absrootfs);
284 return -1;
285 }
286
287 return fprintf(f, "%s %s %s defaults 0 0\n", absrootfs, rootfs, fstype);
288}
289
eae6543d 290static int configure_rootfs(const char *name, const char *rootfs)
0ad19a3f 291{
292 char path[MAXPATHLEN];
b09ef133 293 char absrootfs[MAXPATHLEN];
9b0f0477 294 char fstab[MAXPATHLEN];
78ae2fcc 295 struct stat s;
9b0f0477 296 FILE *f;
78ae2fcc 297 int i, ret;
298
299 typedef int (*rootfs_cb)(const char *, const char *, FILE *);
300
301 struct rootfs_type {
302 int type;
303 rootfs_cb cb;
304 } rtfs_type[] = {
305 { __S_IFDIR, configure_rootfs_dir_cb },
306 { __S_IFBLK, configure_rootfs_blk_cb },
307 };
0ad19a3f 308
4c8ab83b 309 if (!realpath(rootfs, absrootfs)) {
36eb9bde 310 SYSERROR("failed to get real path for '%s'", rootfs);
4c8ab83b 311 return -1;
312 }
b09ef133 313
4c8ab83b 314 snprintf(path, MAXPATHLEN, LXCPATH "/%s/rootfs", name);
b09ef133 315
78ae2fcc 316 if (mkdir(path, 0755)) {
36eb9bde 317 SYSERROR("failed to create the '%s' directory", path);
78ae2fcc 318 return -1;
319 }
320
b09ef133 321 if (access(absrootfs, F_OK)) {
36eb9bde 322 SYSERROR("'%s' is not accessible", absrootfs);
b09ef133 323 return -1;
324 }
325
78ae2fcc 326 if (stat(absrootfs, &s)) {
36eb9bde 327 SYSERROR("failed to stat '%s'", absrootfs);
9b0f0477 328 return -1;
329 }
330
78ae2fcc 331 for (i = 0; i < sizeof(rtfs_type)/sizeof(rtfs_type[0]); i++) {
9b0f0477 332
78ae2fcc 333 if (!__S_ISTYPE(s.st_mode, rtfs_type[i].type))
334 continue;
9b0f0477 335
78ae2fcc 336 snprintf(fstab, MAXPATHLEN, LXCPATH "/%s/fstab", name);
4c8ab83b 337
78ae2fcc 338 f = fopen(fstab, "a+");
339 if (!f) {
36eb9bde 340 SYSERROR("failed to open fstab file");
78ae2fcc 341 return -1;
342 }
9b0f0477 343
78ae2fcc 344 ret = rtfs_type[i].cb(path, absrootfs, f);
9b0f0477 345
78ae2fcc 346 fclose(f);
347
348 if (ret < 0) {
36eb9bde 349 ERROR("failed to add rootfs mount in fstab");
78ae2fcc 350 return -1;
351 }
352
353 snprintf(path, MAXPATHLEN, LXCPATH "/%s/rootfs/rootfs", name);
354
355 return symlink(absrootfs, path);
356 }
9b0f0477 357
36eb9bde 358 ERROR("unsupported rootfs type for '%s'", absrootfs);
78ae2fcc 359 return -1;
0ad19a3f 360}
361
4e5440c6 362static int setup_utsname(struct utsname *utsname)
0ad19a3f 363{
4e5440c6
DL
364 if (!utsname)
365 return 0;
0ad19a3f 366
4e5440c6
DL
367 if (sethostname(utsname->nodename, strlen(utsname->nodename))) {
368 SYSERROR("failed to set the hostname to '%s'", utsname->nodename);
0ad19a3f 369 return -1;
370 }
371
4e5440c6 372 INFO("'%s' hostname has been setup", utsname->nodename);
cd54d859 373
0ad19a3f 374 return 0;
375}
376
52e35957 377static int setup_tty(const char *rootfs, const struct lxc_tty_info *tty_info)
b0a33c1e 378{
379 char path[MAXPATHLEN];
380 int i;
381
382 for (i = 0; i < tty_info->nbtty; i++) {
383
384 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
385
52e35957
DL
386 snprintf(path, sizeof(path), "%s/dev/tty%d",
387 rootfs ? rootfs : "", i + 1);
b0a33c1e 388
13954cce 389 /* At this point I can not use the "access" function
b0a33c1e 390 * to check the file is present or not because it fails
391 * with EACCES errno and I don't know why :( */
13954cce 392
b0a33c1e 393 if (mount(pty_info->name, path, "none", MS_BIND, 0)) {
36eb9bde 394 WARN("failed to mount '%s'->'%s'",
52e35957 395 pty_info->name, path);
b0a33c1e 396 continue;
397 }
398 }
399
cd54d859
DL
400 INFO("%d tty(s) has been setup", tty_info->nbtty);
401
b0a33c1e 402 return 0;
403}
404
7a7ff0c6 405static int setup_rootfs_pivot_root_cb(char *buffer, void *data)
bf601689
MH
406{
407 struct lxc_list *mountlist, *listentry, *iterator;
408 char *pivotdir, *mountpoint, *mountentry;
409 int found;
410 void **cbparm;
411
412 mountentry = buffer;
413 cbparm = (void **)data;
414
415 mountlist = cbparm[0];
416 pivotdir = cbparm[1];
417
418 /* parse entry, first field is mountname, ignore */
419 mountpoint = strtok(mountentry, " ");
420 if (!mountpoint)
421 return -1;
422
423 /* second field is mountpoint */
424 mountpoint = strtok(NULL, " ");
425 if (!mountpoint)
426 return -1;
427
428 /* only consider mountpoints below old root fs */
429 if (strncmp(mountpoint, pivotdir, strlen(pivotdir)))
430 return 0;
431
432 /* filter duplicate mountpoints */
433 found = 0;
434 lxc_list_for_each(iterator, mountlist) {
435 if (!strcmp(iterator->elem, mountpoint)) {
436 found = 1;
437 break;
438 }
439 }
440 if (found)
441 return 0;
442
443 /* add entry to list */
444 listentry = malloc(sizeof(*listentry));
445 if (!listentry) {
446 SYSERROR("malloc for mountpoint listentry failed");
447 return -1;
448 }
449
450 listentry->elem = strdup(mountpoint);
451 if (!listentry->elem) {
452 SYSERROR("strdup failed");
453 return -1;
454 }
455 lxc_list_add_tail(mountlist, listentry);
456
457 return 0;
458}
459
bf601689
MH
460static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
461{
2382ecff 462 char path[MAXPATHLEN];
bf601689
MH
463 void *cbparm[2];
464 struct lxc_list mountlist, *iterator;
465 int ok, still_mounted, last_still_mounted;
bf601689
MH
466
467 /* change into new root fs */
468 if (chdir(rootfs)) {
469 SYSERROR("can't chroot to new rootfs '%s'", rootfs);
470 return -1;
471 }
472
0b7a8353
DL
473 if (!pivotdir)
474 pivotdir = "oldrootfs";
bf601689 475
0b7a8353
DL
476 /* create a default mountpoint if none specified */
477 snprintf(path, sizeof(path), "%s/%s", rootfs, pivotdir);
bf601689 478
0b7a8353
DL
479 if (access(path, F_OK)) {
480
481 if (mkdir_p(path, 0755)) {
482 SYSERROR("failed to create pivotdir '%s'", path);
bf601689
MH
483 return -1;
484 }
485
0b7a8353 486 DEBUG("created '%s' directory", path);
bf601689
MH
487 }
488
1b09f2c0 489 DEBUG("mountpoint for old rootfs is '%s'", path);
bf601689
MH
490
491 /* pivot_root into our new root fs */
492
493 if (pivot_root(".", path)) {
494 SYSERROR("pivot_root syscall failed");
495 return -1;
496 }
497
498 if (chdir("/")) {
499 SYSERROR("can't chroot to / after pivot_root");
500 return -1;
501 }
502
503 DEBUG("pivot_root syscall to '%s' successful", pivotdir);
504
505 /* read and parse /proc/mounts in old root fs */
506 lxc_list_init(&mountlist);
507
508 snprintf(path, sizeof(path), "%s/", pivotdir);
509 cbparm[0] = &mountlist;
510 cbparm[1] = strdup(path);
511
512 if (!cbparm[1]) {
513 SYSERROR("strdup failed");
514 return -1;
515 }
516
517 snprintf(path, sizeof(path), "/%s/proc/mounts", pivotdir);
2382ecff 518 ok = lxc_file_for_each_line(path, setup_rootfs_pivot_root_cb, &cbparm);
bf601689
MH
519 if (ok < 0) {
520 SYSERROR("failed to read or parse mount list '%s'", path);
521 return -1;
522 }
523
524 /* umount filesystems until none left or list no longer shrinks */
525 still_mounted = 0;
526 do {
527 last_still_mounted = still_mounted;
528 still_mounted = 0;
529
530 lxc_list_for_each(iterator, &mountlist) {
531
c08556c6 532 /* umount normally */
bf601689
MH
533 if (!umount(iterator->elem)) {
534 DEBUG("umounted '%s'", (char *)iterator->elem);
535 lxc_list_del(iterator);
536 continue;
537 }
538
bf601689
MH
539 still_mounted++;
540 }
7df119ee 541
bf601689
MH
542 } while (still_mounted > 0 && still_mounted != last_still_mounted);
543
7df119ee 544
c08556c6
DL
545 lxc_list_for_each(iterator, &mountlist) {
546
547 /* let's try a lazy umount */
548 if (!umount2(iterator->elem, MNT_DETACH)) {
549 INFO("lazy unmount of '%s'", (char *)iterator->elem);
550 continue;
551 }
552
553 /* be more brutal (nfs) */
554 if (!umount2(iterator->elem, MNT_FORCE)) {
555 INFO("forced unmount of '%s'", (char *)iterator->elem);
556 continue;
557 }
558
7df119ee 559 WARN("failed to unmount '%s'", (char *)iterator->elem);
c08556c6 560 }
bf601689 561
c08556c6
DL
562 /* umount old root fs; if some other mount points are still
563 * there, we won't be able to umount it, so we have to do
564 * that in a lazy way otherwise the container will always
565 * fail to start
566 */
567 if (umount2(pivotdir, MNT_DETACH)) {
bf601689
MH
568 SYSERROR("could not unmount old rootfs");
569 return -1;
570 }
571 DEBUG("umounted '%s'", pivotdir);
572
c08556c6
DL
573 /* remove temporary mount point, we don't consider the removing
574 * as fatal */
0b7a8353
DL
575 if (rmdir(pivotdir))
576 WARN("can't remove mountpoint: %m");
bf601689
MH
577
578 INFO("pivoted to '%s'", rootfs);
579 return 0;
580}
581
582static int setup_rootfs(const char *rootfs, const char *pivotdir)
0ad19a3f 583{
25368b52 584 const char *tmpfs = "/tmp";
0ad19a3f 585
c69bd12f
DL
586 if (!rootfs)
587 return 0;
0ad19a3f 588
25368b52
FW
589 if (mount(rootfs, tmpfs, "none", MS_BIND|MS_REC, NULL)) {
590 SYSERROR("failed to mount '%s'->'%s'", rootfs, "/tmp");
c3f0a28c 591 return -1;
592 }
0ad19a3f 593
25368b52 594 DEBUG("mounted '%s' on '%s'", rootfs, tmpfs);
c69bd12f 595
25368b52 596 if (setup_rootfs_pivot_root(tmpfs, pivotdir)) {
bf601689 597 ERROR("failed to pivot_root to '%s'", rootfs);
25368b52 598 return -1;
c69bd12f
DL
599 }
600
25368b52 601 return 0;
0ad19a3f 602}
603
d852c78c 604static int setup_pts(int pts)
3c26f34e 605{
d852c78c
DL
606 if (!pts)
607 return 0;
3c26f34e 608
609 if (!access("/dev/pts/ptmx", F_OK) && umount("/dev/pts")) {
36eb9bde 610 SYSERROR("failed to umount 'dev/pts'");
3c26f34e 611 return -1;
612 }
613
d852c78c 614 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, "newinstance")) {
36eb9bde 615 SYSERROR("failed to mount a new instance of '/dev/pts'");
3c26f34e 616 return -1;
617 }
618
619 if (chmod("/dev/pts/ptmx", 0666)) {
36eb9bde 620 SYSERROR("failed to set permission for '/dev/pts/ptmx'");
3c26f34e 621 return -1;
622 }
623
624 if (access("/dev/ptmx", F_OK)) {
625 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
626 goto out;
36eb9bde 627 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 628 return -1;
629 }
630
631 /* fallback here, /dev/pts/ptmx exists just mount bind */
632 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND, 0)) {
36eb9bde 633 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 634 return -1;
635 }
cd54d859
DL
636
637 INFO("created new pts instance");
d852c78c 638
3c26f34e 639out:
640 return 0;
641}
642
63376d7d 643static int setup_console(const char *rootfs, const struct lxc_console *console)
6e590161 644{
63376d7d
DL
645 char path[MAXPATHLEN];
646 struct stat s;
52e35957 647
63376d7d
DL
648 /* We don't have a rootfs, /dev/console will be shared */
649 if (!rootfs)
650 return 0;
52e35957 651
63376d7d 652 snprintf(path, sizeof(path), "%s/dev/console", rootfs);
52e35957 653
63376d7d
DL
654 if (access(path, F_OK)) {
655 WARN("rootfs specified but no console found");
656 return 0;
52e35957
DL
657 }
658
f78a1f32 659 if (console->peer == -1) {
63376d7d 660 INFO("no console output required");
f78a1f32
DL
661 return 0;
662 }
ed502555 663
63376d7d
DL
664 if (stat(path, &s)) {
665 SYSERROR("failed to stat '%s'", path);
666 return -1;
667 }
668
669 if (chmod(console->name, s.st_mode)) {
670 SYSERROR("failed to set mode '0%o' to '%s'",
671 s.st_mode, console->name);
672 return -1;
673 }
13954cce 674
63376d7d
DL
675 if (mount(console->name, path, "none", MS_BIND, 0)) {
676 ERROR("failed to mount '%s' on '%s'", console->name, path);
6e590161 677 return -1;
678 }
679
63376d7d 680 INFO("console has been setup");
cd54d859 681
6e590161 682 return 0;
683}
684
102a5303 685static int setup_cgroup(const char *name, struct lxc_list *cgroups)
576f946d 686{
102a5303
DL
687 struct lxc_list *iterator;
688 struct lxc_cgroup *cg;
88329c69 689 int ret = -1;
6f4a3756 690
102a5303
DL
691 if (lxc_list_empty(cgroups))
692 return 0;
6f4a3756 693
102a5303 694 lxc_list_for_each(iterator, cgroups) {
13954cce 695
102a5303 696 cg = iterator->elem;
6f4a3756 697
102a5303 698 if (lxc_cgroup_set(name, cg->subsystem, cg->value))
88329c69 699 goto out;
6f4a3756 700
102a5303 701 DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value);
6f4a3756 702 }
13954cce 703
88329c69 704 ret = 0;
cd54d859 705 INFO("cgroup has been setup");
88329c69
MN
706out:
707 return ret;
576f946d 708}
709
998ac676
RT
710static void parse_mntopt(char *opt, unsigned long *flags, char **data)
711{
712 struct mount_opt *mo;
713
714 /* If opt is found in mount_opt, set or clear flags.
715 * Otherwise append it to data. */
716
717 for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
718 if (!strncmp(opt, mo->name, strlen(mo->name))) {
719 if (mo->clear)
720 *flags &= ~mo->flag;
721 else
722 *flags |= mo->flag;
723 return;
724 }
725 }
726
727 if (strlen(*data))
728 strcat(*data, ",");
729 strcat(*data, opt);
730}
731
732static int parse_mntopts(struct mntent *mntent, unsigned long *mntflags,
733 char **mntdata)
734{
735 char *s, *data;
736 char *p, *saveptr = NULL;
737
738 if (!mntent->mnt_opts)
739 return 0;
740
741 s = strdup(mntent->mnt_opts);
742 if (!s) {
36eb9bde 743 SYSERROR("failed to allocate memory");
998ac676
RT
744 return -1;
745 }
746
747 data = malloc(strlen(s) + 1);
748 if (!data) {
36eb9bde 749 SYSERROR("failed to allocate memory");
998ac676
RT
750 free(s);
751 return -1;
752 }
753 *data = 0;
754
755 for (p = strtok_r(s, ",", &saveptr); p != NULL;
756 p = strtok_r(NULL, ",", &saveptr))
757 parse_mntopt(p, mntflags, &data);
758
759 if (*data)
760 *mntdata = data;
761 else
762 free(data);
763 free(s);
764
765 return 0;
766}
767
e7938e9e 768static int mount_file_entries(FILE *file)
0ad19a3f 769{
0ad19a3f 770 struct mntent *mntent;
0ad19a3f 771 int ret = -1;
998ac676
RT
772 unsigned long mntflags;
773 char *mntdata;
0ad19a3f 774
998ac676 775 while ((mntent = getmntent(file))) {
1bc60a65 776
998ac676
RT
777 mntflags = 0;
778 mntdata = NULL;
779 if (parse_mntopts(mntent, &mntflags, &mntdata) < 0) {
36eb9bde 780 ERROR("failed to parse mount option '%s'",
998ac676
RT
781 mntent->mnt_opts);
782 goto out;
783 }
0ad19a3f 784
785 if (mount(mntent->mnt_fsname, mntent->mnt_dir,
998ac676 786 mntent->mnt_type, mntflags, mntdata)) {
36eb9bde 787 SYSERROR("failed to mount '%s' on '%s'",
0ad19a3f 788 mntent->mnt_fsname, mntent->mnt_dir);
789 goto out;
790 }
998ac676 791
cd54d859
DL
792 DEBUG("mounted %s on %s, type %s", mntent->mnt_fsname,
793 mntent->mnt_dir, mntent->mnt_type);
794
998ac676 795 free(mntdata);
0ad19a3f 796 }
cd54d859 797
0ad19a3f 798 ret = 0;
cd54d859
DL
799
800 INFO("mount points have been setup");
0ad19a3f 801out:
e7938e9e
MN
802 return ret;
803}
804
805static int setup_mount(const char *fstab)
806{
807 FILE *file;
808 int ret;
809
810 if (!fstab)
811 return 0;
812
813 file = setmntent(fstab, "r");
814 if (!file) {
815 SYSERROR("failed to use '%s'", fstab);
816 return -1;
817 }
818
819 ret = mount_file_entries(file);
820
0ad19a3f 821 endmntent(file);
822 return ret;
823}
824
e7938e9e
MN
825static int setup_mount_entries(struct lxc_list *mount)
826{
827 FILE *file;
828 struct lxc_list *iterator;
829 char *mount_entry;
830 int ret;
831
832 file = tmpfile();
833 if (!file) {
834 ERROR("tmpfile error: %m");
835 return -1;
836 }
837
838 lxc_list_for_each(iterator, mount) {
839 mount_entry = iterator->elem;
1d6b1976 840 fprintf(file, "%s\n", mount_entry);
e7938e9e
MN
841 }
842
843 rewind(file);
844
845 ret = mount_file_entries(file);
846
847 fclose(file);
848 return ret;
849}
850
81810dd1
DL
851static int setup_caps(struct lxc_list *caps)
852{
853 struct lxc_list *iterator;
854 char *drop_entry;
855 int i, capid;
856
857 lxc_list_for_each(iterator, caps) {
858
859 drop_entry = iterator->elem;
860
861 capid = -1;
862
863 for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) {
864
865 if (strcmp(drop_entry, caps_opt[i].name))
866 continue;
867
868 capid = caps_opt[i].value;
869 break;
870 }
871
872 if (capid < 0) {
1e11be34
DL
873 ERROR("unknown capability %s", drop_entry);
874 return -1;
81810dd1
DL
875 }
876
877 DEBUG("drop capability '%s' (%d)", drop_entry, capid);
878
879 if (prctl(PR_CAPBSET_DROP, capid, 0, 0, 0)) {
880 SYSERROR("failed to remove %s capability", drop_entry);
881 return -1;
882 }
883
884 }
885
886 DEBUG("capabilities has been setup");
887
888 return 0;
889}
890
0ad19a3f 891static int setup_hw_addr(char *hwaddr, const char *ifname)
892{
893 struct sockaddr sockaddr;
894 struct ifreq ifr;
895 int ret, fd;
896
3cfc0f3a
MN
897 ret = lxc_convert_mac(hwaddr, &sockaddr);
898 if (ret) {
899 ERROR("mac address '%s' conversion failed : %s",
900 hwaddr, strerror(-ret));
0ad19a3f 901 return -1;
902 }
903
904 memcpy(ifr.ifr_name, ifname, IFNAMSIZ);
905 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sockaddr, sizeof(sockaddr));
906
907 fd = socket(AF_INET, SOCK_DGRAM, 0);
908 if (fd < 0) {
3ab87b66 909 ERROR("socket failure : %s", strerror(errno));
0ad19a3f 910 return -1;
911 }
912
913 ret = ioctl(fd, SIOCSIFHWADDR, &ifr);
914 close(fd);
915 if (ret)
3ab87b66 916 ERROR("ioctl failure : %s", strerror(errno));
0ad19a3f 917
cd54d859
DL
918 DEBUG("mac address '%s' on '%s' has been setup", hwaddr, ifname);
919
0ad19a3f 920 return ret;
921}
922
82d5ae15 923static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 924{
82d5ae15
DL
925 struct lxc_list *iterator;
926 struct lxc_inetdev *inetdev;
3cfc0f3a 927 int err;
0ad19a3f 928
82d5ae15
DL
929 lxc_list_for_each(iterator, ip) {
930
931 inetdev = iterator->elem;
932
3cfc0f3a
MN
933 err = lxc_ip_addr_add(AF_INET, ifindex,
934 &inetdev->addr, inetdev->prefix);
935 if (err) {
936 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
937 ifindex, strerror(-err));
82d5ae15
DL
938 return -1;
939 }
940 }
941
942 return 0;
0ad19a3f 943}
944
82d5ae15 945static int setup_ipv6_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 946{
82d5ae15 947 struct lxc_list *iterator;
7fa9074f 948 struct lxc_inet6dev *inet6dev;
3cfc0f3a 949 int err;
0ad19a3f 950
82d5ae15
DL
951 lxc_list_for_each(iterator, ip) {
952
953 inet6dev = iterator->elem;
954
3cfc0f3a
MN
955 err = lxc_ip_addr_add(AF_INET6, ifindex,
956 &inet6dev->addr, inet6dev->prefix);
957 if (err) {
958 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
959 ifindex, strerror(-err));
82d5ae15 960 return -1;
3cfc0f3a 961 }
82d5ae15
DL
962 }
963
964 return 0;
0ad19a3f 965}
966
82d5ae15 967static int setup_netdev(struct lxc_netdev *netdev)
0ad19a3f 968{
0ad19a3f 969 char ifname[IFNAMSIZ];
0ad19a3f 970 char *current_ifname = ifname;
3cfc0f3a 971 int err;
0ad19a3f 972
82d5ae15
DL
973 /* empty network namespace */
974 if (!netdev->ifindex) {
975 if (netdev->flags | IFF_UP) {
3cfc0f3a
MN
976 err = lxc_device_up("lo");
977 if (err) {
978 ERROR("failed to set the loopback up : %s",
979 strerror(-err));
82d5ae15
DL
980 return -1;
981 }
982 return 0;
983 }
0ad19a3f 984 }
13954cce 985
82d5ae15
DL
986 /* retrieve the name of the interface */
987 if (!if_indextoname(netdev->ifindex, current_ifname)) {
36eb9bde 988 ERROR("no interface corresponding to index '%d'",
82d5ae15 989 netdev->ifindex);
0ad19a3f 990 return -1;
991 }
13954cce 992
018ef520 993 /* default: let the system to choose one interface name */
9d083402
MT
994 if (!netdev->name)
995 netdev->name = "eth%d";
018ef520 996
82d5ae15 997 /* rename the interface name */
3cfc0f3a
MN
998 err = lxc_device_rename(ifname, netdev->name);
999 if (err) {
1000 ERROR("failed to rename %s->%s : %s", ifname, netdev->name,
1001 strerror(-err));
018ef520
DL
1002 return -1;
1003 }
1004
1005 /* Re-read the name of the interface because its name has changed
1006 * and would be automatically allocated by the system
1007 */
82d5ae15 1008 if (!if_indextoname(netdev->ifindex, current_ifname)) {
018ef520 1009 ERROR("no interface corresponding to index '%d'",
82d5ae15 1010 netdev->ifindex);
018ef520 1011 return -1;
0ad19a3f 1012 }
1013
82d5ae15
DL
1014 /* set a mac address */
1015 if (netdev->hwaddr) {
1016 if (setup_hw_addr(netdev->hwaddr, current_ifname)) {
36eb9bde 1017 ERROR("failed to setup hw address for '%s'",
82d5ae15 1018 current_ifname);
0ad19a3f 1019 return -1;
1020 }
1021 }
1022
82d5ae15
DL
1023 /* setup ipv4 addresses on the interface */
1024 if (setup_ipv4_addr(&netdev->ipv4, netdev->ifindex)) {
36eb9bde 1025 ERROR("failed to setup ip addresses for '%s'",
0ad19a3f 1026 ifname);
1027 return -1;
1028 }
1029
82d5ae15
DL
1030 /* setup ipv6 addresses on the interface */
1031 if (setup_ipv6_addr(&netdev->ipv6, netdev->ifindex)) {
36eb9bde 1032 ERROR("failed to setup ipv6 addresses for '%s'",
0ad19a3f 1033 ifname);
1034 return -1;
1035 }
1036
82d5ae15
DL
1037 /* set the network device up */
1038 if (netdev->flags | IFF_UP) {
3cfc0f3a
MN
1039 int err;
1040
1041 err = lxc_device_up(current_ifname);
1042 if (err) {
1043 ERROR("failed to set '%s' up : %s", current_ifname,
1044 strerror(-err));
0ad19a3f 1045 return -1;
1046 }
1047
1048 /* the network is up, make the loopback up too */
3cfc0f3a
MN
1049 err = lxc_device_up("lo");
1050 if (err) {
1051 ERROR("failed to set the loopback up : %s",
1052 strerror(-err));
0ad19a3f 1053 return -1;
1054 }
1055 }
1056
cd54d859
DL
1057 DEBUG("'%s' has been setup", current_ifname);
1058
0ad19a3f 1059 return 0;
1060}
1061
5f4535a3 1062static int setup_network(struct lxc_list *network)
0ad19a3f 1063{
82d5ae15 1064 struct lxc_list *iterator;
82d5ae15 1065 struct lxc_netdev *netdev;
0ad19a3f 1066
5f4535a3 1067 lxc_list_for_each(iterator, network) {
cd54d859 1068
5f4535a3 1069 netdev = iterator->elem;
82d5ae15
DL
1070
1071 if (setup_netdev(netdev)) {
1072 ERROR("failed to setup netdev");
1073 return -1;
1074 }
1075 }
cd54d859 1076
5f4535a3
DL
1077 if (!lxc_list_empty(network))
1078 INFO("network has been setup");
cd54d859
DL
1079
1080 return 0;
0ad19a3f 1081}
1082
7b379ab3 1083struct lxc_conf *lxc_conf_init(void)
089cd8b8 1084{
7b379ab3
MN
1085 struct lxc_conf *new;
1086
1087 new = malloc(sizeof(*new));
1088 if (!new) {
1089 ERROR("lxc_conf_init : %m");
1090 return NULL;
1091 }
1092 memset(new, 0, sizeof(*new));
1093
28a4b0e5 1094 new->console.path = NULL;
63376d7d
DL
1095 new->console.peer = -1;
1096 new->console.master = -1;
1097 new->console.slave = -1;
1098 new->console.name[0] = '\0';
7b379ab3
MN
1099 lxc_list_init(&new->cgroup);
1100 lxc_list_init(&new->network);
1101 lxc_list_init(&new->mount_list);
81810dd1 1102 lxc_list_init(&new->caps);
7b379ab3
MN
1103
1104 return new;
089cd8b8
DL
1105}
1106
82d5ae15 1107static int instanciate_veth(struct lxc_netdev *netdev)
0ad19a3f 1108{
8634bc19 1109 char veth1buf[IFNAMSIZ], *veth1;
82d5ae15 1110 char veth2[IFNAMSIZ];
3cfc0f3a 1111 int err;
13954cce 1112
e892973e
DL
1113 if (netdev->priv.veth_attr.pair)
1114 veth1 = netdev->priv.veth_attr.pair;
8634bc19
MT
1115 else {
1116 snprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX");
1117 mktemp(veth1buf);
1118 veth1 = veth1buf;
1119 }
82d5ae15 1120
8634bc19 1121 snprintf(veth2, sizeof(veth2), "vethXXXXXX");
82d5ae15
DL
1122 mktemp(veth2);
1123
1124 if (!strlen(veth1) || !strlen(veth2)) {
1125 ERROR("failed to allocate a temporary name");
1126 return -1;
0ad19a3f 1127 }
1128
3cfc0f3a
MN
1129 err = lxc_veth_create(veth1, veth2);
1130 if (err) {
1131 ERROR("failed to create %s-%s : %s", veth1, veth2,
1132 strerror(-err));
6ab9ab6d 1133 return -1;
0ad19a3f 1134 }
13954cce 1135
82d5ae15 1136 if (netdev->mtu) {
3cfc0f3a
MN
1137 err = lxc_device_set_mtu(veth1, atoi(netdev->mtu));
1138 if (!err)
1139 err = lxc_device_set_mtu(veth2, atoi(netdev->mtu));
1140 if (err) {
1141 ERROR("failed to set mtu '%s' for %s-%s : %s",
1142 netdev->mtu, veth1, veth2, strerror(-err));
eb14c10a 1143 goto out_delete;
75d09f83
DL
1144 }
1145 }
1146
3cfc0f3a
MN
1147 if (netdev->link) {
1148 err = lxc_bridge_attach(netdev->link, veth1);
1149 if (err) {
1150 ERROR("failed to attach '%s' to the bridge '%s' : %s",
1151 veth1, netdev->link, strerror(-err));
1152 goto out_delete;
1153 }
eb14c10a
DL
1154 }
1155
82d5ae15
DL
1156 netdev->ifindex = if_nametoindex(veth2);
1157 if (!netdev->ifindex) {
36eb9bde 1158 ERROR("failed to retrieve the index for %s", veth2);
eb14c10a
DL
1159 goto out_delete;
1160 }
1161
82d5ae15 1162 if (netdev->flags & IFF_UP) {
3cfc0f3a
MN
1163 err = lxc_device_up(veth1);
1164 if (err) {
1165 ERROR("failed to set %s up : %s", veth1,
1166 strerror(-err));
eb14c10a 1167 goto out_delete;
0ad19a3f 1168 }
1169 }
1170
82d5ae15
DL
1171 DEBUG("instanciated veth '%s/%s', index is '%d'",
1172 veth1, veth2, netdev->ifindex);
1173
6ab9ab6d 1174 return 0;
eb14c10a
DL
1175
1176out_delete:
1177 lxc_device_delete(veth1);
6ab9ab6d 1178 return -1;
13954cce 1179}
d957ae2d 1180
82d5ae15 1181static int instanciate_macvlan(struct lxc_netdev *netdev)
0ad19a3f 1182{
82d5ae15 1183 char peer[IFNAMSIZ];
3cfc0f3a 1184 int err;
d957ae2d
MT
1185
1186 if (!netdev->link) {
1187 ERROR("no link specified for macvlan netdev");
1188 return -1;
1189 }
13954cce 1190
82d5ae15 1191 snprintf(peer, sizeof(peer), "mcXXXXXX");
22ebac19 1192
82d5ae15
DL
1193 mktemp(peer);
1194
1195 if (!strlen(peer)) {
1196 ERROR("failed to make a temporary name");
1197 return -1;
0ad19a3f 1198 }
1199
3cfc0f3a
MN
1200 err = lxc_macvlan_create(netdev->link, peer,
1201 netdev->priv.macvlan_attr.mode);
1202 if (err) {
1203 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
1204 peer, netdev->link, strerror(-err));
d957ae2d 1205 return -1;
0ad19a3f 1206 }
1207
82d5ae15
DL
1208 netdev->ifindex = if_nametoindex(peer);
1209 if (!netdev->ifindex) {
36eb9bde 1210 ERROR("failed to retrieve the index for %s", peer);
d957ae2d
MT
1211 lxc_device_delete(peer);
1212 return -1;
22ebac19 1213 }
1214
e892973e
DL
1215 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
1216 peer, netdev->ifindex, netdev->priv.macvlan_attr.mode);
0ad19a3f 1217
d957ae2d 1218 return 0;
0ad19a3f 1219}
1220
26c39028
JHS
1221/* XXX: merge with instanciate_macvlan */
1222static int instanciate_vlan(struct lxc_netdev *netdev)
1223{
1224 char peer[IFNAMSIZ];
3cfc0f3a 1225 int err;
26c39028
JHS
1226
1227 if (!netdev->link) {
1228 ERROR("no link specified for vlan netdev");
1229 return -1;
1230 }
1231
e892973e 1232 snprintf(peer, sizeof(peer), "vlan%d", netdev->priv.vlan_attr.vid);
26c39028 1233
3cfc0f3a
MN
1234 err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid);
1235 if (err) {
1236 ERROR("failed to create vlan interface '%s' on '%s' : %s",
1237 peer, netdev->link, strerror(-err));
26c39028
JHS
1238 return -1;
1239 }
1240
1241 netdev->ifindex = if_nametoindex(peer);
1242 if (!netdev->ifindex) {
1243 ERROR("failed to retrieve the ifindex for %s", peer);
1244 lxc_device_delete(peer);
1245 return -1;
1246 }
1247
e892973e
DL
1248 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
1249 netdev->ifindex);
1250
26c39028
JHS
1251 return 0;
1252}
1253
82d5ae15 1254static int instanciate_phys(struct lxc_netdev *netdev)
0ad19a3f 1255{
9d083402 1256 netdev->ifindex = if_nametoindex(netdev->link);
82d5ae15 1257 if (!netdev->ifindex) {
9d083402 1258 ERROR("failed to retrieve the index for %s", netdev->link);
0ad19a3f 1259 return -1;
1260 }
1261
82d5ae15 1262 return 0;
0ad19a3f 1263}
1264
82d5ae15 1265static int instanciate_empty(struct lxc_netdev *netdev)
0ad19a3f 1266{
82d5ae15
DL
1267 netdev->ifindex = 0;
1268 return 0;
0ad19a3f 1269}
1270
5f4535a3 1271int lxc_create_network(struct lxc_list *network)
0ad19a3f 1272{
82d5ae15 1273 struct lxc_list *iterator;
82d5ae15 1274 struct lxc_netdev *netdev;
0ad19a3f 1275
5f4535a3 1276 lxc_list_for_each(iterator, network) {
0ad19a3f 1277
5f4535a3 1278 netdev = iterator->elem;
13954cce 1279
24654103 1280 if (netdev->type < 0 || netdev->type > LXC_NET_MAXCONFTYPE) {
82d5ae15 1281 ERROR("invalid network configuration type '%d'",
5f4535a3 1282 netdev->type);
82d5ae15
DL
1283 return -1;
1284 }
0ad19a3f 1285
5f4535a3 1286 if (netdev_conf[netdev->type](netdev)) {
82d5ae15
DL
1287 ERROR("failed to create netdev");
1288 return -1;
1289 }
0ad19a3f 1290 }
1291
1292 return 0;
1293}
1294
7fef7a06
DL
1295void lxc_delete_network(struct lxc_list *network)
1296{
1297 struct lxc_list *iterator;
1298 struct lxc_netdev *netdev;
1299
1300 lxc_list_for_each(iterator, network) {
1301 netdev = iterator->elem;
1302 if (netdev->ifindex > 0)
1303 lxc_device_delete_index(netdev->ifindex);
1304 }
1305}
1306
5f4535a3 1307int lxc_assign_network(struct lxc_list *network, pid_t pid)
0ad19a3f 1308{
82d5ae15 1309 struct lxc_list *iterator;
82d5ae15 1310 struct lxc_netdev *netdev;
3cfc0f3a 1311 int err;
0ad19a3f 1312
5f4535a3 1313 lxc_list_for_each(iterator, network) {
82d5ae15 1314
5f4535a3 1315 netdev = iterator->elem;
82d5ae15 1316
236087a6
DL
1317 /* empty network namespace, nothing to move */
1318 if (!netdev->ifindex)
1319 continue;
1320
3cfc0f3a
MN
1321 err = lxc_device_move(netdev->ifindex, pid);
1322 if (err) {
1323 ERROR("failed to move '%s' to the container : %s",
1324 netdev->link, strerror(-err));
82d5ae15
DL
1325 return -1;
1326 }
1327
9d083402 1328 DEBUG("move '%s' to '%d'", netdev->link, pid);
0ad19a3f 1329 }
1330
1331 return 0;
1332}
1333
5e4a62bf 1334int lxc_create_tty(const char *name, struct lxc_conf *conf)
b0a33c1e 1335{
5e4a62bf 1336 struct lxc_tty_info *tty_info = &conf->tty_info;
985d15b1 1337 int i;
b0a33c1e 1338
5e4a62bf
DL
1339 /* no tty in the configuration */
1340 if (!conf->tty)
b0a33c1e 1341 return 0;
1342
13954cce 1343 tty_info->pty_info =
e4e7d59d 1344 malloc(sizeof(*tty_info->pty_info)*conf->tty);
b0a33c1e 1345 if (!tty_info->pty_info) {
36eb9bde 1346 SYSERROR("failed to allocate pty_info");
985d15b1 1347 return -1;
b0a33c1e 1348 }
1349
985d15b1 1350 for (i = 0; i < conf->tty; i++) {
13954cce 1351
b0a33c1e 1352 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1353
13954cce 1354 if (openpty(&pty_info->master, &pty_info->slave,
b0a33c1e 1355 pty_info->name, NULL, NULL)) {
36eb9bde 1356 SYSERROR("failed to create pty #%d", i);
985d15b1
MT
1357 tty_info->nbtty = i;
1358 lxc_delete_tty(tty_info);
1359 return -1;
b0a33c1e 1360 }
1361
b035ad62
MS
1362 /* Prevent leaking the file descriptors to the container */
1363 fcntl(pty_info->master, F_SETFD, FD_CLOEXEC);
1364 fcntl(pty_info->slave, F_SETFD, FD_CLOEXEC);
1365
b0a33c1e 1366 pty_info->busy = 0;
1367 }
1368
985d15b1 1369 tty_info->nbtty = conf->tty;
1ac470c0
DL
1370
1371 INFO("tty's configured");
1372
985d15b1 1373 return 0;
b0a33c1e 1374}
1375
1376void lxc_delete_tty(struct lxc_tty_info *tty_info)
1377{
1378 int i;
1379
1380 for (i = 0; i < tty_info->nbtty; i++) {
1381 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1382
1383 close(pty_info->master);
1384 close(pty_info->slave);
1385 }
1386
1387 free(tty_info->pty_info);
1388 tty_info->nbtty = 0;
1389}
1390
571e6ec8 1391int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
0ad19a3f 1392{
571e6ec8 1393 if (setup_utsname(lxc_conf->utsname)) {
36eb9bde 1394 ERROR("failed to setup the utsname for '%s'", name);
95b5ffaf 1395 return -1;
0ad19a3f 1396 }
1397
5f4535a3 1398 if (setup_network(&lxc_conf->network)) {
36eb9bde 1399 ERROR("failed to setup the network for '%s'", name);
95b5ffaf 1400 return -1;
0ad19a3f 1401 }
1402
571e6ec8 1403 if (setup_cgroup(name, &lxc_conf->cgroup)) {
36eb9bde 1404 ERROR("failed to setup the cgroups for '%s'", name);
95b5ffaf 1405 return -1;
0ad19a3f 1406 }
1407
571e6ec8 1408 if (setup_mount(lxc_conf->fstab)) {
36eb9bde 1409 ERROR("failed to setup the mounts for '%s'", name);
95b5ffaf 1410 return -1;
576f946d 1411 }
1412
e7938e9e
MN
1413 if (setup_mount_entries(&lxc_conf->mount_list)) {
1414 ERROR("failed to setup the mount entries for '%s'", name);
1415 return -1;
1416 }
1417
63376d7d 1418 if (setup_console(lxc_conf->rootfs, &lxc_conf->console)) {
36eb9bde 1419 ERROR("failed to setup the console for '%s'", name);
95b5ffaf 1420 return -1;
6e590161 1421 }
1422
571e6ec8 1423 if (setup_tty(lxc_conf->rootfs, &lxc_conf->tty_info)) {
36eb9bde 1424 ERROR("failed to setup the ttys for '%s'", name);
95b5ffaf 1425 return -1;
b0a33c1e 1426 }
1427
bf601689 1428 if (setup_rootfs(lxc_conf->rootfs, lxc_conf->pivotdir)) {
36eb9bde 1429 ERROR("failed to set rootfs for '%s'", name);
95b5ffaf 1430 return -1;
ed502555 1431 }
1432
571e6ec8 1433 if (setup_pts(lxc_conf->pts)) {
36eb9bde 1434 ERROR("failed to setup the new pts instance");
95b5ffaf 1435 return -1;
3c26f34e 1436 }
1437
81810dd1
DL
1438 if (setup_caps(&lxc_conf->caps)) {
1439 ERROR("failed to drop capabilities");
1440 return -1;
1441 }
1442
cd54d859
DL
1443 NOTICE("'%s' is setup.", name);
1444
0ad19a3f 1445 return 0;
1446}