]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/conf.c
close socket command
[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
33fcb7a0
DL
377static int setup_tty(const struct lxc_rootfs *rootfs,
378 const struct lxc_tty_info *tty_info)
b0a33c1e 379{
380 char path[MAXPATHLEN];
381 int i;
382
383 for (i = 0; i < tty_info->nbtty; i++) {
384
385 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
386
52e35957 387 snprintf(path, sizeof(path), "%s/dev/tty%d",
33fcb7a0 388 rootfs->path ? rootfs->path : "", i + 1);
b0a33c1e 389
13954cce 390 /* At this point I can not use the "access" function
b0a33c1e 391 * to check the file is present or not because it fails
392 * with EACCES errno and I don't know why :( */
13954cce 393
b0a33c1e 394 if (mount(pty_info->name, path, "none", MS_BIND, 0)) {
36eb9bde 395 WARN("failed to mount '%s'->'%s'",
52e35957 396 pty_info->name, path);
b0a33c1e 397 continue;
398 }
399 }
400
cd54d859
DL
401 INFO("%d tty(s) has been setup", tty_info->nbtty);
402
b0a33c1e 403 return 0;
404}
405
7a7ff0c6 406static int setup_rootfs_pivot_root_cb(char *buffer, void *data)
bf601689
MH
407{
408 struct lxc_list *mountlist, *listentry, *iterator;
409 char *pivotdir, *mountpoint, *mountentry;
410 int found;
411 void **cbparm;
412
413 mountentry = buffer;
414 cbparm = (void **)data;
415
416 mountlist = cbparm[0];
417 pivotdir = cbparm[1];
418
419 /* parse entry, first field is mountname, ignore */
420 mountpoint = strtok(mountentry, " ");
421 if (!mountpoint)
422 return -1;
423
424 /* second field is mountpoint */
425 mountpoint = strtok(NULL, " ");
426 if (!mountpoint)
427 return -1;
428
429 /* only consider mountpoints below old root fs */
430 if (strncmp(mountpoint, pivotdir, strlen(pivotdir)))
431 return 0;
432
433 /* filter duplicate mountpoints */
434 found = 0;
435 lxc_list_for_each(iterator, mountlist) {
436 if (!strcmp(iterator->elem, mountpoint)) {
437 found = 1;
438 break;
439 }
440 }
441 if (found)
442 return 0;
443
444 /* add entry to list */
445 listentry = malloc(sizeof(*listentry));
446 if (!listentry) {
447 SYSERROR("malloc for mountpoint listentry failed");
448 return -1;
449 }
450
451 listentry->elem = strdup(mountpoint);
452 if (!listentry->elem) {
453 SYSERROR("strdup failed");
454 return -1;
455 }
456 lxc_list_add_tail(mountlist, listentry);
457
458 return 0;
459}
460
bf601689
MH
461static int setup_rootfs_pivot_root(const char *rootfs, const char *pivotdir)
462{
2382ecff 463 char path[MAXPATHLEN];
bf601689
MH
464 void *cbparm[2];
465 struct lxc_list mountlist, *iterator;
466 int ok, still_mounted, last_still_mounted;
a91d897a 467 int remove_pivotdir = 0;
bf601689
MH
468
469 /* change into new root fs */
470 if (chdir(rootfs)) {
9232212a 471 SYSERROR("can't chdir to new rootfs '%s'", rootfs);
bf601689
MH
472 return -1;
473 }
474
0b7a8353
DL
475 if (!pivotdir)
476 pivotdir = "oldrootfs";
bf601689 477
0b7a8353
DL
478 /* create a default mountpoint if none specified */
479 snprintf(path, sizeof(path), "%s/%s", rootfs, pivotdir);
bf601689 480
0b7a8353
DL
481 if (access(path, F_OK)) {
482
483 if (mkdir_p(path, 0755)) {
484 SYSERROR("failed to create pivotdir '%s'", path);
bf601689
MH
485 return -1;
486 }
487
a91d897a 488 remove_pivotdir = 1;
0b7a8353 489 DEBUG("created '%s' directory", path);
bf601689
MH
490 }
491
1b09f2c0 492 DEBUG("mountpoint for old rootfs is '%s'", path);
bf601689
MH
493
494 /* pivot_root into our new root fs */
495
496 if (pivot_root(".", path)) {
497 SYSERROR("pivot_root syscall failed");
498 return -1;
499 }
500
501 if (chdir("/")) {
9232212a 502 SYSERROR("can't chdir to / after pivot_root");
bf601689
MH
503 return -1;
504 }
505
506 DEBUG("pivot_root syscall to '%s' successful", pivotdir);
507
508 /* read and parse /proc/mounts in old root fs */
509 lxc_list_init(&mountlist);
510
511 snprintf(path, sizeof(path), "%s/", pivotdir);
512 cbparm[0] = &mountlist;
513 cbparm[1] = strdup(path);
514
515 if (!cbparm[1]) {
516 SYSERROR("strdup failed");
517 return -1;
518 }
519
520 snprintf(path, sizeof(path), "/%s/proc/mounts", pivotdir);
2382ecff 521 ok = lxc_file_for_each_line(path, setup_rootfs_pivot_root_cb, &cbparm);
bf601689
MH
522 if (ok < 0) {
523 SYSERROR("failed to read or parse mount list '%s'", path);
524 return -1;
525 }
526
527 /* umount filesystems until none left or list no longer shrinks */
528 still_mounted = 0;
529 do {
530 last_still_mounted = still_mounted;
531 still_mounted = 0;
532
533 lxc_list_for_each(iterator, &mountlist) {
534
c08556c6 535 /* umount normally */
bf601689
MH
536 if (!umount(iterator->elem)) {
537 DEBUG("umounted '%s'", (char *)iterator->elem);
538 lxc_list_del(iterator);
539 continue;
540 }
541
bf601689
MH
542 still_mounted++;
543 }
7df119ee 544
bf601689
MH
545 } while (still_mounted > 0 && still_mounted != last_still_mounted);
546
7df119ee 547
c08556c6
DL
548 lxc_list_for_each(iterator, &mountlist) {
549
550 /* let's try a lazy umount */
551 if (!umount2(iterator->elem, MNT_DETACH)) {
552 INFO("lazy unmount of '%s'", (char *)iterator->elem);
553 continue;
554 }
555
556 /* be more brutal (nfs) */
557 if (!umount2(iterator->elem, MNT_FORCE)) {
558 INFO("forced unmount of '%s'", (char *)iterator->elem);
559 continue;
560 }
561
7df119ee 562 WARN("failed to unmount '%s'", (char *)iterator->elem);
c08556c6 563 }
bf601689 564
c08556c6
DL
565 /* umount old root fs; if some other mount points are still
566 * there, we won't be able to umount it, so we have to do
567 * that in a lazy way otherwise the container will always
568 * fail to start
569 */
570 if (umount2(pivotdir, MNT_DETACH)) {
bf601689
MH
571 SYSERROR("could not unmount old rootfs");
572 return -1;
573 }
574 DEBUG("umounted '%s'", pivotdir);
575
c08556c6
DL
576 /* remove temporary mount point, we don't consider the removing
577 * as fatal */
a91d897a
FW
578 if (remove_pivotdir && rmdir(pivotdir))
579 WARN("can't remove mountpoint '%s': %m", pivotdir);
bf601689
MH
580
581 INFO("pivoted to '%s'", rootfs);
582 return 0;
583}
584
33fcb7a0 585static int setup_rootfs(const struct lxc_rootfs *rootfs)
0ad19a3f 586{
b1789442 587 char *mpath = LXCROOTFSMOUNT;
0ad19a3f 588
33fcb7a0 589 if (!rootfs->path)
c69bd12f 590 return 0;
0ad19a3f 591
b1789442
DL
592 if (rootfs->mount)
593 mpath = rootfs->mount;
594
595 if (access(mpath, F_OK)) {
596 SYSERROR("failed to access to '%s', check it is present",
597 mpath);
598 return -1;
599 }
600
601 if (mount(rootfs->path, mpath, "none", MS_BIND|MS_REC, NULL)) {
602 SYSERROR("failed to mount '%s'->'%s'", rootfs->path, mpath);
c3f0a28c 603 return -1;
604 }
0ad19a3f 605
b1789442 606 DEBUG("mounted '%s' on '%s'", rootfs->path, mpath);
c69bd12f 607
b1789442 608 if (setup_rootfs_pivot_root(mpath, rootfs->pivot)) {
33fcb7a0 609 ERROR("failed to pivot_root to '%s'", rootfs->pivot);
25368b52 610 return -1;
c69bd12f
DL
611 }
612
25368b52 613 return 0;
0ad19a3f 614}
615
d852c78c 616static int setup_pts(int pts)
3c26f34e 617{
d852c78c
DL
618 if (!pts)
619 return 0;
3c26f34e 620
621 if (!access("/dev/pts/ptmx", F_OK) && umount("/dev/pts")) {
36eb9bde 622 SYSERROR("failed to umount 'dev/pts'");
3c26f34e 623 return -1;
624 }
625
d852c78c 626 if (mount("devpts", "/dev/pts", "devpts", MS_MGC_VAL, "newinstance")) {
36eb9bde 627 SYSERROR("failed to mount a new instance of '/dev/pts'");
3c26f34e 628 return -1;
629 }
630
631 if (chmod("/dev/pts/ptmx", 0666)) {
36eb9bde 632 SYSERROR("failed to set permission for '/dev/pts/ptmx'");
3c26f34e 633 return -1;
634 }
635
636 if (access("/dev/ptmx", F_OK)) {
637 if (!symlink("/dev/pts/ptmx", "/dev/ptmx"))
638 goto out;
36eb9bde 639 SYSERROR("failed to symlink '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 640 return -1;
641 }
642
643 /* fallback here, /dev/pts/ptmx exists just mount bind */
644 if (mount("/dev/pts/ptmx", "/dev/ptmx", "none", MS_BIND, 0)) {
36eb9bde 645 SYSERROR("mount failed '/dev/pts/ptmx'->'/dev/ptmx'");
3c26f34e 646 return -1;
647 }
cd54d859
DL
648
649 INFO("created new pts instance");
d852c78c 650
3c26f34e 651out:
652 return 0;
653}
654
33fcb7a0
DL
655static int setup_console(const struct lxc_rootfs *rootfs,
656 const struct lxc_console *console)
6e590161 657{
63376d7d
DL
658 char path[MAXPATHLEN];
659 struct stat s;
52e35957 660
63376d7d 661 /* We don't have a rootfs, /dev/console will be shared */
33fcb7a0 662 if (!rootfs->path)
63376d7d 663 return 0;
52e35957 664
33fcb7a0 665 snprintf(path, sizeof(path), "%s/dev/console", rootfs->path);
52e35957 666
63376d7d
DL
667 if (access(path, F_OK)) {
668 WARN("rootfs specified but no console found");
669 return 0;
52e35957
DL
670 }
671
f78a1f32 672 if (console->peer == -1) {
63376d7d 673 INFO("no console output required");
f78a1f32
DL
674 return 0;
675 }
ed502555 676
63376d7d
DL
677 if (stat(path, &s)) {
678 SYSERROR("failed to stat '%s'", path);
679 return -1;
680 }
681
682 if (chmod(console->name, s.st_mode)) {
683 SYSERROR("failed to set mode '0%o' to '%s'",
684 s.st_mode, console->name);
685 return -1;
686 }
13954cce 687
63376d7d
DL
688 if (mount(console->name, path, "none", MS_BIND, 0)) {
689 ERROR("failed to mount '%s' on '%s'", console->name, path);
6e590161 690 return -1;
691 }
692
63376d7d 693 INFO("console has been setup");
cd54d859 694
6e590161 695 return 0;
696}
697
102a5303 698static int setup_cgroup(const char *name, struct lxc_list *cgroups)
576f946d 699{
102a5303
DL
700 struct lxc_list *iterator;
701 struct lxc_cgroup *cg;
88329c69 702 int ret = -1;
6f4a3756 703
102a5303
DL
704 if (lxc_list_empty(cgroups))
705 return 0;
6f4a3756 706
102a5303 707 lxc_list_for_each(iterator, cgroups) {
13954cce 708
102a5303 709 cg = iterator->elem;
6f4a3756 710
102a5303 711 if (lxc_cgroup_set(name, cg->subsystem, cg->value))
88329c69 712 goto out;
6f4a3756 713
102a5303 714 DEBUG("cgroup '%s' set to '%s'", cg->subsystem, cg->value);
6f4a3756 715 }
13954cce 716
88329c69 717 ret = 0;
cd54d859 718 INFO("cgroup has been setup");
88329c69
MN
719out:
720 return ret;
576f946d 721}
722
998ac676
RT
723static void parse_mntopt(char *opt, unsigned long *flags, char **data)
724{
725 struct mount_opt *mo;
726
727 /* If opt is found in mount_opt, set or clear flags.
728 * Otherwise append it to data. */
729
730 for (mo = &mount_opt[0]; mo->name != NULL; mo++) {
731 if (!strncmp(opt, mo->name, strlen(mo->name))) {
732 if (mo->clear)
733 *flags &= ~mo->flag;
734 else
735 *flags |= mo->flag;
736 return;
737 }
738 }
739
740 if (strlen(*data))
741 strcat(*data, ",");
742 strcat(*data, opt);
743}
744
745static int parse_mntopts(struct mntent *mntent, unsigned long *mntflags,
746 char **mntdata)
747{
748 char *s, *data;
749 char *p, *saveptr = NULL;
750
751 if (!mntent->mnt_opts)
752 return 0;
753
754 s = strdup(mntent->mnt_opts);
755 if (!s) {
36eb9bde 756 SYSERROR("failed to allocate memory");
998ac676
RT
757 return -1;
758 }
759
760 data = malloc(strlen(s) + 1);
761 if (!data) {
36eb9bde 762 SYSERROR("failed to allocate memory");
998ac676
RT
763 free(s);
764 return -1;
765 }
766 *data = 0;
767
768 for (p = strtok_r(s, ",", &saveptr); p != NULL;
769 p = strtok_r(NULL, ",", &saveptr))
770 parse_mntopt(p, mntflags, &data);
771
772 if (*data)
773 *mntdata = data;
774 else
775 free(data);
776 free(s);
777
778 return 0;
779}
780
e7938e9e 781static int mount_file_entries(FILE *file)
0ad19a3f 782{
0ad19a3f 783 struct mntent *mntent;
0ad19a3f 784 int ret = -1;
998ac676
RT
785 unsigned long mntflags;
786 char *mntdata;
0ad19a3f 787
998ac676 788 while ((mntent = getmntent(file))) {
1bc60a65 789
998ac676
RT
790 mntflags = 0;
791 mntdata = NULL;
792 if (parse_mntopts(mntent, &mntflags, &mntdata) < 0) {
36eb9bde 793 ERROR("failed to parse mount option '%s'",
998ac676
RT
794 mntent->mnt_opts);
795 goto out;
796 }
0ad19a3f 797
798 if (mount(mntent->mnt_fsname, mntent->mnt_dir,
998ac676 799 mntent->mnt_type, mntflags, mntdata)) {
36eb9bde 800 SYSERROR("failed to mount '%s' on '%s'",
0ad19a3f 801 mntent->mnt_fsname, mntent->mnt_dir);
802 goto out;
803 }
998ac676 804
cd54d859
DL
805 DEBUG("mounted %s on %s, type %s", mntent->mnt_fsname,
806 mntent->mnt_dir, mntent->mnt_type);
807
998ac676 808 free(mntdata);
0ad19a3f 809 }
cd54d859 810
0ad19a3f 811 ret = 0;
cd54d859
DL
812
813 INFO("mount points have been setup");
0ad19a3f 814out:
e7938e9e
MN
815 return ret;
816}
817
818static int setup_mount(const char *fstab)
819{
820 FILE *file;
821 int ret;
822
823 if (!fstab)
824 return 0;
825
826 file = setmntent(fstab, "r");
827 if (!file) {
828 SYSERROR("failed to use '%s'", fstab);
829 return -1;
830 }
831
832 ret = mount_file_entries(file);
833
0ad19a3f 834 endmntent(file);
835 return ret;
836}
837
e7938e9e
MN
838static int setup_mount_entries(struct lxc_list *mount)
839{
840 FILE *file;
841 struct lxc_list *iterator;
842 char *mount_entry;
843 int ret;
844
845 file = tmpfile();
846 if (!file) {
847 ERROR("tmpfile error: %m");
848 return -1;
849 }
850
851 lxc_list_for_each(iterator, mount) {
852 mount_entry = iterator->elem;
1d6b1976 853 fprintf(file, "%s\n", mount_entry);
e7938e9e
MN
854 }
855
856 rewind(file);
857
858 ret = mount_file_entries(file);
859
860 fclose(file);
861 return ret;
862}
863
81810dd1
DL
864static int setup_caps(struct lxc_list *caps)
865{
866 struct lxc_list *iterator;
867 char *drop_entry;
868 int i, capid;
869
870 lxc_list_for_each(iterator, caps) {
871
872 drop_entry = iterator->elem;
873
874 capid = -1;
875
876 for (i = 0; i < sizeof(caps_opt)/sizeof(caps_opt[0]); i++) {
877
878 if (strcmp(drop_entry, caps_opt[i].name))
879 continue;
880
881 capid = caps_opt[i].value;
882 break;
883 }
884
885 if (capid < 0) {
1e11be34
DL
886 ERROR("unknown capability %s", drop_entry);
887 return -1;
81810dd1
DL
888 }
889
890 DEBUG("drop capability '%s' (%d)", drop_entry, capid);
891
892 if (prctl(PR_CAPBSET_DROP, capid, 0, 0, 0)) {
893 SYSERROR("failed to remove %s capability", drop_entry);
894 return -1;
895 }
896
897 }
898
899 DEBUG("capabilities has been setup");
900
901 return 0;
902}
903
0ad19a3f 904static int setup_hw_addr(char *hwaddr, const char *ifname)
905{
906 struct sockaddr sockaddr;
907 struct ifreq ifr;
908 int ret, fd;
909
3cfc0f3a
MN
910 ret = lxc_convert_mac(hwaddr, &sockaddr);
911 if (ret) {
912 ERROR("mac address '%s' conversion failed : %s",
913 hwaddr, strerror(-ret));
0ad19a3f 914 return -1;
915 }
916
917 memcpy(ifr.ifr_name, ifname, IFNAMSIZ);
918 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sockaddr, sizeof(sockaddr));
919
920 fd = socket(AF_INET, SOCK_DGRAM, 0);
921 if (fd < 0) {
3ab87b66 922 ERROR("socket failure : %s", strerror(errno));
0ad19a3f 923 return -1;
924 }
925
926 ret = ioctl(fd, SIOCSIFHWADDR, &ifr);
927 close(fd);
928 if (ret)
3ab87b66 929 ERROR("ioctl failure : %s", strerror(errno));
0ad19a3f 930
cd54d859
DL
931 DEBUG("mac address '%s' on '%s' has been setup", hwaddr, ifname);
932
0ad19a3f 933 return ret;
934}
935
82d5ae15 936static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 937{
82d5ae15
DL
938 struct lxc_list *iterator;
939 struct lxc_inetdev *inetdev;
3cfc0f3a 940 int err;
0ad19a3f 941
82d5ae15
DL
942 lxc_list_for_each(iterator, ip) {
943
944 inetdev = iterator->elem;
945
0093bb8c
DL
946 err = lxc_ipv4_addr_add(ifindex, &inetdev->addr,
947 &inetdev->bcast, inetdev->prefix);
3cfc0f3a
MN
948 if (err) {
949 ERROR("failed to setup_ipv4_addr ifindex %d : %s",
950 ifindex, strerror(-err));
82d5ae15
DL
951 return -1;
952 }
953 }
954
955 return 0;
0ad19a3f 956}
957
82d5ae15 958static int setup_ipv6_addr(struct lxc_list *ip, int ifindex)
0ad19a3f 959{
82d5ae15 960 struct lxc_list *iterator;
7fa9074f 961 struct lxc_inet6dev *inet6dev;
3cfc0f3a 962 int err;
0ad19a3f 963
82d5ae15
DL
964 lxc_list_for_each(iterator, ip) {
965
966 inet6dev = iterator->elem;
967
0093bb8c
DL
968 err = lxc_ipv6_addr_add(ifindex, &inet6dev->addr,
969 &inet6dev->mcast, &inet6dev->acast,
970 inet6dev->prefix);
3cfc0f3a
MN
971 if (err) {
972 ERROR("failed to setup_ipv6_addr ifindex %d : %s",
973 ifindex, strerror(-err));
82d5ae15 974 return -1;
3cfc0f3a 975 }
82d5ae15
DL
976 }
977
978 return 0;
0ad19a3f 979}
980
82d5ae15 981static int setup_netdev(struct lxc_netdev *netdev)
0ad19a3f 982{
0ad19a3f 983 char ifname[IFNAMSIZ];
0ad19a3f 984 char *current_ifname = ifname;
3cfc0f3a 985 int err;
0ad19a3f 986
82d5ae15
DL
987 /* empty network namespace */
988 if (!netdev->ifindex) {
989 if (netdev->flags | IFF_UP) {
3cfc0f3a
MN
990 err = lxc_device_up("lo");
991 if (err) {
992 ERROR("failed to set the loopback up : %s",
993 strerror(-err));
82d5ae15
DL
994 return -1;
995 }
996 return 0;
997 }
0ad19a3f 998 }
13954cce 999
82d5ae15
DL
1000 /* retrieve the name of the interface */
1001 if (!if_indextoname(netdev->ifindex, current_ifname)) {
36eb9bde 1002 ERROR("no interface corresponding to index '%d'",
82d5ae15 1003 netdev->ifindex);
0ad19a3f 1004 return -1;
1005 }
13954cce 1006
018ef520 1007 /* default: let the system to choose one interface name */
9d083402
MT
1008 if (!netdev->name)
1009 netdev->name = "eth%d";
018ef520 1010
82d5ae15 1011 /* rename the interface name */
3cfc0f3a
MN
1012 err = lxc_device_rename(ifname, netdev->name);
1013 if (err) {
1014 ERROR("failed to rename %s->%s : %s", ifname, netdev->name,
1015 strerror(-err));
018ef520
DL
1016 return -1;
1017 }
1018
1019 /* Re-read the name of the interface because its name has changed
1020 * and would be automatically allocated by the system
1021 */
82d5ae15 1022 if (!if_indextoname(netdev->ifindex, current_ifname)) {
018ef520 1023 ERROR("no interface corresponding to index '%d'",
82d5ae15 1024 netdev->ifindex);
018ef520 1025 return -1;
0ad19a3f 1026 }
1027
82d5ae15
DL
1028 /* set a mac address */
1029 if (netdev->hwaddr) {
1030 if (setup_hw_addr(netdev->hwaddr, current_ifname)) {
36eb9bde 1031 ERROR("failed to setup hw address for '%s'",
82d5ae15 1032 current_ifname);
0ad19a3f 1033 return -1;
1034 }
1035 }
1036
82d5ae15
DL
1037 /* setup ipv4 addresses on the interface */
1038 if (setup_ipv4_addr(&netdev->ipv4, netdev->ifindex)) {
36eb9bde 1039 ERROR("failed to setup ip addresses for '%s'",
0ad19a3f 1040 ifname);
1041 return -1;
1042 }
1043
82d5ae15
DL
1044 /* setup ipv6 addresses on the interface */
1045 if (setup_ipv6_addr(&netdev->ipv6, netdev->ifindex)) {
36eb9bde 1046 ERROR("failed to setup ipv6 addresses for '%s'",
0ad19a3f 1047 ifname);
1048 return -1;
1049 }
1050
82d5ae15
DL
1051 /* set the network device up */
1052 if (netdev->flags | IFF_UP) {
3cfc0f3a
MN
1053 int err;
1054
1055 err = lxc_device_up(current_ifname);
1056 if (err) {
1057 ERROR("failed to set '%s' up : %s", current_ifname,
1058 strerror(-err));
0ad19a3f 1059 return -1;
1060 }
1061
1062 /* the network is up, make the loopback up too */
3cfc0f3a
MN
1063 err = lxc_device_up("lo");
1064 if (err) {
1065 ERROR("failed to set the loopback up : %s",
1066 strerror(-err));
0ad19a3f 1067 return -1;
1068 }
1069 }
1070
cd54d859
DL
1071 DEBUG("'%s' has been setup", current_ifname);
1072
0ad19a3f 1073 return 0;
1074}
1075
5f4535a3 1076static int setup_network(struct lxc_list *network)
0ad19a3f 1077{
82d5ae15 1078 struct lxc_list *iterator;
82d5ae15 1079 struct lxc_netdev *netdev;
0ad19a3f 1080
5f4535a3 1081 lxc_list_for_each(iterator, network) {
cd54d859 1082
5f4535a3 1083 netdev = iterator->elem;
82d5ae15
DL
1084
1085 if (setup_netdev(netdev)) {
1086 ERROR("failed to setup netdev");
1087 return -1;
1088 }
1089 }
cd54d859 1090
5f4535a3
DL
1091 if (!lxc_list_empty(network))
1092 INFO("network has been setup");
cd54d859
DL
1093
1094 return 0;
0ad19a3f 1095}
1096
7b379ab3 1097struct lxc_conf *lxc_conf_init(void)
089cd8b8 1098{
7b379ab3
MN
1099 struct lxc_conf *new;
1100
1101 new = malloc(sizeof(*new));
1102 if (!new) {
1103 ERROR("lxc_conf_init : %m");
1104 return NULL;
1105 }
1106 memset(new, 0, sizeof(*new));
1107
28a4b0e5 1108 new->console.path = NULL;
63376d7d
DL
1109 new->console.peer = -1;
1110 new->console.master = -1;
1111 new->console.slave = -1;
1112 new->console.name[0] = '\0';
7b379ab3
MN
1113 lxc_list_init(&new->cgroup);
1114 lxc_list_init(&new->network);
1115 lxc_list_init(&new->mount_list);
81810dd1 1116 lxc_list_init(&new->caps);
7b379ab3
MN
1117
1118 return new;
089cd8b8
DL
1119}
1120
82d5ae15 1121static int instanciate_veth(struct lxc_netdev *netdev)
0ad19a3f 1122{
8634bc19 1123 char veth1buf[IFNAMSIZ], *veth1;
82d5ae15 1124 char veth2[IFNAMSIZ];
3cfc0f3a 1125 int err;
13954cce 1126
e892973e
DL
1127 if (netdev->priv.veth_attr.pair)
1128 veth1 = netdev->priv.veth_attr.pair;
8634bc19
MT
1129 else {
1130 snprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX");
1131 mktemp(veth1buf);
1132 veth1 = veth1buf;
1133 }
82d5ae15 1134
8634bc19 1135 snprintf(veth2, sizeof(veth2), "vethXXXXXX");
82d5ae15
DL
1136 mktemp(veth2);
1137
1138 if (!strlen(veth1) || !strlen(veth2)) {
1139 ERROR("failed to allocate a temporary name");
1140 return -1;
0ad19a3f 1141 }
1142
3cfc0f3a
MN
1143 err = lxc_veth_create(veth1, veth2);
1144 if (err) {
1145 ERROR("failed to create %s-%s : %s", veth1, veth2,
1146 strerror(-err));
6ab9ab6d 1147 return -1;
0ad19a3f 1148 }
13954cce 1149
82d5ae15 1150 if (netdev->mtu) {
3cfc0f3a
MN
1151 err = lxc_device_set_mtu(veth1, atoi(netdev->mtu));
1152 if (!err)
1153 err = lxc_device_set_mtu(veth2, atoi(netdev->mtu));
1154 if (err) {
1155 ERROR("failed to set mtu '%s' for %s-%s : %s",
1156 netdev->mtu, veth1, veth2, strerror(-err));
eb14c10a 1157 goto out_delete;
75d09f83
DL
1158 }
1159 }
1160
3cfc0f3a
MN
1161 if (netdev->link) {
1162 err = lxc_bridge_attach(netdev->link, veth1);
1163 if (err) {
1164 ERROR("failed to attach '%s' to the bridge '%s' : %s",
1165 veth1, netdev->link, strerror(-err));
1166 goto out_delete;
1167 }
eb14c10a
DL
1168 }
1169
82d5ae15
DL
1170 netdev->ifindex = if_nametoindex(veth2);
1171 if (!netdev->ifindex) {
36eb9bde 1172 ERROR("failed to retrieve the index for %s", veth2);
eb14c10a
DL
1173 goto out_delete;
1174 }
1175
82d5ae15 1176 if (netdev->flags & IFF_UP) {
3cfc0f3a
MN
1177 err = lxc_device_up(veth1);
1178 if (err) {
1179 ERROR("failed to set %s up : %s", veth1,
1180 strerror(-err));
eb14c10a 1181 goto out_delete;
0ad19a3f 1182 }
1183 }
1184
82d5ae15
DL
1185 DEBUG("instanciated veth '%s/%s', index is '%d'",
1186 veth1, veth2, netdev->ifindex);
1187
6ab9ab6d 1188 return 0;
eb14c10a
DL
1189
1190out_delete:
1191 lxc_device_delete(veth1);
6ab9ab6d 1192 return -1;
13954cce 1193}
d957ae2d 1194
82d5ae15 1195static int instanciate_macvlan(struct lxc_netdev *netdev)
0ad19a3f 1196{
82d5ae15 1197 char peer[IFNAMSIZ];
3cfc0f3a 1198 int err;
d957ae2d
MT
1199
1200 if (!netdev->link) {
1201 ERROR("no link specified for macvlan netdev");
1202 return -1;
1203 }
13954cce 1204
82d5ae15 1205 snprintf(peer, sizeof(peer), "mcXXXXXX");
22ebac19 1206
82d5ae15
DL
1207 mktemp(peer);
1208
1209 if (!strlen(peer)) {
1210 ERROR("failed to make a temporary name");
1211 return -1;
0ad19a3f 1212 }
1213
3cfc0f3a
MN
1214 err = lxc_macvlan_create(netdev->link, peer,
1215 netdev->priv.macvlan_attr.mode);
1216 if (err) {
1217 ERROR("failed to create macvlan interface '%s' on '%s' : %s",
1218 peer, netdev->link, strerror(-err));
d957ae2d 1219 return -1;
0ad19a3f 1220 }
1221
82d5ae15
DL
1222 netdev->ifindex = if_nametoindex(peer);
1223 if (!netdev->ifindex) {
36eb9bde 1224 ERROR("failed to retrieve the index for %s", peer);
d957ae2d
MT
1225 lxc_device_delete(peer);
1226 return -1;
22ebac19 1227 }
1228
e892973e
DL
1229 DEBUG("instanciated macvlan '%s', index is '%d' and mode '%d'",
1230 peer, netdev->ifindex, netdev->priv.macvlan_attr.mode);
0ad19a3f 1231
d957ae2d 1232 return 0;
0ad19a3f 1233}
1234
26c39028
JHS
1235/* XXX: merge with instanciate_macvlan */
1236static int instanciate_vlan(struct lxc_netdev *netdev)
1237{
1238 char peer[IFNAMSIZ];
3cfc0f3a 1239 int err;
26c39028
JHS
1240
1241 if (!netdev->link) {
1242 ERROR("no link specified for vlan netdev");
1243 return -1;
1244 }
1245
e892973e 1246 snprintf(peer, sizeof(peer), "vlan%d", netdev->priv.vlan_attr.vid);
26c39028 1247
3cfc0f3a
MN
1248 err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid);
1249 if (err) {
1250 ERROR("failed to create vlan interface '%s' on '%s' : %s",
1251 peer, netdev->link, strerror(-err));
26c39028
JHS
1252 return -1;
1253 }
1254
1255 netdev->ifindex = if_nametoindex(peer);
1256 if (!netdev->ifindex) {
1257 ERROR("failed to retrieve the ifindex for %s", peer);
1258 lxc_device_delete(peer);
1259 return -1;
1260 }
1261
e892973e
DL
1262 DEBUG("instanciated vlan '%s', ifindex is '%d'", " vlan1000",
1263 netdev->ifindex);
1264
26c39028
JHS
1265 return 0;
1266}
1267
82d5ae15 1268static int instanciate_phys(struct lxc_netdev *netdev)
0ad19a3f 1269{
9d083402 1270 netdev->ifindex = if_nametoindex(netdev->link);
82d5ae15 1271 if (!netdev->ifindex) {
9d083402 1272 ERROR("failed to retrieve the index for %s", netdev->link);
0ad19a3f 1273 return -1;
1274 }
1275
82d5ae15 1276 return 0;
0ad19a3f 1277}
1278
82d5ae15 1279static int instanciate_empty(struct lxc_netdev *netdev)
0ad19a3f 1280{
82d5ae15
DL
1281 netdev->ifindex = 0;
1282 return 0;
0ad19a3f 1283}
1284
5f4535a3 1285int lxc_create_network(struct lxc_list *network)
0ad19a3f 1286{
82d5ae15 1287 struct lxc_list *iterator;
82d5ae15 1288 struct lxc_netdev *netdev;
0ad19a3f 1289
5f4535a3 1290 lxc_list_for_each(iterator, network) {
0ad19a3f 1291
5f4535a3 1292 netdev = iterator->elem;
13954cce 1293
24654103 1294 if (netdev->type < 0 || netdev->type > LXC_NET_MAXCONFTYPE) {
82d5ae15 1295 ERROR("invalid network configuration type '%d'",
5f4535a3 1296 netdev->type);
82d5ae15
DL
1297 return -1;
1298 }
0ad19a3f 1299
5f4535a3 1300 if (netdev_conf[netdev->type](netdev)) {
82d5ae15
DL
1301 ERROR("failed to create netdev");
1302 return -1;
1303 }
0ad19a3f 1304 }
1305
1306 return 0;
1307}
1308
7fef7a06
DL
1309void lxc_delete_network(struct lxc_list *network)
1310{
1311 struct lxc_list *iterator;
1312 struct lxc_netdev *netdev;
1313
1314 lxc_list_for_each(iterator, network) {
1315 netdev = iterator->elem;
1316 if (netdev->ifindex > 0)
1317 lxc_device_delete_index(netdev->ifindex);
1318 }
1319}
1320
5f4535a3 1321int lxc_assign_network(struct lxc_list *network, pid_t pid)
0ad19a3f 1322{
82d5ae15 1323 struct lxc_list *iterator;
82d5ae15 1324 struct lxc_netdev *netdev;
3cfc0f3a 1325 int err;
0ad19a3f 1326
5f4535a3 1327 lxc_list_for_each(iterator, network) {
82d5ae15 1328
5f4535a3 1329 netdev = iterator->elem;
82d5ae15 1330
236087a6
DL
1331 /* empty network namespace, nothing to move */
1332 if (!netdev->ifindex)
1333 continue;
1334
3cfc0f3a
MN
1335 err = lxc_device_move(netdev->ifindex, pid);
1336 if (err) {
1337 ERROR("failed to move '%s' to the container : %s",
1338 netdev->link, strerror(-err));
82d5ae15
DL
1339 return -1;
1340 }
1341
9d083402 1342 DEBUG("move '%s' to '%d'", netdev->link, pid);
0ad19a3f 1343 }
1344
1345 return 0;
1346}
1347
5e4a62bf 1348int lxc_create_tty(const char *name, struct lxc_conf *conf)
b0a33c1e 1349{
5e4a62bf 1350 struct lxc_tty_info *tty_info = &conf->tty_info;
985d15b1 1351 int i;
b0a33c1e 1352
5e4a62bf
DL
1353 /* no tty in the configuration */
1354 if (!conf->tty)
b0a33c1e 1355 return 0;
1356
13954cce 1357 tty_info->pty_info =
e4e7d59d 1358 malloc(sizeof(*tty_info->pty_info)*conf->tty);
b0a33c1e 1359 if (!tty_info->pty_info) {
36eb9bde 1360 SYSERROR("failed to allocate pty_info");
985d15b1 1361 return -1;
b0a33c1e 1362 }
1363
985d15b1 1364 for (i = 0; i < conf->tty; i++) {
13954cce 1365
b0a33c1e 1366 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1367
13954cce 1368 if (openpty(&pty_info->master, &pty_info->slave,
b0a33c1e 1369 pty_info->name, NULL, NULL)) {
36eb9bde 1370 SYSERROR("failed to create pty #%d", i);
985d15b1
MT
1371 tty_info->nbtty = i;
1372 lxc_delete_tty(tty_info);
1373 return -1;
b0a33c1e 1374 }
1375
b035ad62
MS
1376 /* Prevent leaking the file descriptors to the container */
1377 fcntl(pty_info->master, F_SETFD, FD_CLOEXEC);
1378 fcntl(pty_info->slave, F_SETFD, FD_CLOEXEC);
1379
b0a33c1e 1380 pty_info->busy = 0;
1381 }
1382
985d15b1 1383 tty_info->nbtty = conf->tty;
1ac470c0
DL
1384
1385 INFO("tty's configured");
1386
985d15b1 1387 return 0;
b0a33c1e 1388}
1389
1390void lxc_delete_tty(struct lxc_tty_info *tty_info)
1391{
1392 int i;
1393
1394 for (i = 0; i < tty_info->nbtty; i++) {
1395 struct lxc_pty_info *pty_info = &tty_info->pty_info[i];
1396
1397 close(pty_info->master);
1398 close(pty_info->slave);
1399 }
1400
1401 free(tty_info->pty_info);
1402 tty_info->nbtty = 0;
1403}
1404
571e6ec8 1405int lxc_setup(const char *name, struct lxc_conf *lxc_conf)
0ad19a3f 1406{
571e6ec8 1407 if (setup_utsname(lxc_conf->utsname)) {
36eb9bde 1408 ERROR("failed to setup the utsname for '%s'", name);
95b5ffaf 1409 return -1;
0ad19a3f 1410 }
1411
5f4535a3 1412 if (setup_network(&lxc_conf->network)) {
36eb9bde 1413 ERROR("failed to setup the network for '%s'", name);
95b5ffaf 1414 return -1;
0ad19a3f 1415 }
1416
571e6ec8 1417 if (setup_cgroup(name, &lxc_conf->cgroup)) {
36eb9bde 1418 ERROR("failed to setup the cgroups for '%s'", name);
95b5ffaf 1419 return -1;
0ad19a3f 1420 }
1421
571e6ec8 1422 if (setup_mount(lxc_conf->fstab)) {
36eb9bde 1423 ERROR("failed to setup the mounts for '%s'", name);
95b5ffaf 1424 return -1;
576f946d 1425 }
1426
e7938e9e
MN
1427 if (setup_mount_entries(&lxc_conf->mount_list)) {
1428 ERROR("failed to setup the mount entries for '%s'", name);
1429 return -1;
1430 }
1431
33fcb7a0 1432 if (setup_console(&lxc_conf->rootfs, &lxc_conf->console)) {
36eb9bde 1433 ERROR("failed to setup the console for '%s'", name);
95b5ffaf 1434 return -1;
6e590161 1435 }
1436
33fcb7a0 1437 if (setup_tty(&lxc_conf->rootfs, &lxc_conf->tty_info)) {
36eb9bde 1438 ERROR("failed to setup the ttys for '%s'", name);
95b5ffaf 1439 return -1;
b0a33c1e 1440 }
1441
33fcb7a0 1442 if (setup_rootfs(&lxc_conf->rootfs)) {
36eb9bde 1443 ERROR("failed to set rootfs for '%s'", name);
95b5ffaf 1444 return -1;
ed502555 1445 }
1446
571e6ec8 1447 if (setup_pts(lxc_conf->pts)) {
36eb9bde 1448 ERROR("failed to setup the new pts instance");
95b5ffaf 1449 return -1;
3c26f34e 1450 }
1451
81810dd1
DL
1452 if (setup_caps(&lxc_conf->caps)) {
1453 ERROR("failed to drop capabilities");
1454 return -1;
1455 }
1456
cd54d859
DL
1457 NOTICE("'%s' is setup.", name);
1458
0ad19a3f 1459 return 0;
1460}