nspawn.c revision 88213476187cafc86bea2276199891873000588d
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <signal.h>
#include <sched.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/capability.h>
#include <getopt.h>
#include "log.h"
#include "util.h"
static char *arg_directory = NULL;
static int help(void) {
printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
"Spawn a minimal namespace container for debugging, testing and building.\n\n"
" -h --help Show this help\n"
" -D --directory=NAME Root directory for the container\n",
return 0;
}
};
int c;
switch (c) {
case 'h':
help();
return 0;
case 'D':
log_error("Failed to duplicate root directory.");
return -ENOMEM;
}
break;
case '?':
return -EINVAL;
default:
log_error("Unknown option code %c", c);
return -EINVAL;
}
}
return 1;
}
typedef struct MountPoint {
const char *what;
const char *where;
const char *type;
const char *options;
unsigned long flags;
} MountPoint;
static const MountPoint mount_table[] = {
};
unsigned k;
int r = 0;
for (k = 0; k < ELEMENTSOF(mount_table); k++) {
char *where;
int t;
log_error("Out of memory");
if (r == 0)
r = -ENOMEM;
break;
}
if ((t = path_is_mount_point(where)) < 0) {
if (r == 0)
r = t;
continue;
}
mount_table[k].type,
mount_table[k].flags,
mount_table[k].options) < 0) {
if (r == 0)
r = -errno;
}
}
return r;
}
static int copy_devnodes(const char *dest) {
static const char devnodes[] =
"null\0"
"zero\0"
"full\0"
"random\0"
"urandom\0"
"tty\0"
"ptmx\0"
"kmsg\0"
"rtc0\0";
const char *d;
int r = 0, k;
NULSTR_FOREACH(d, devnodes) {
log_error("Failed to allocate devnode path");
if (r == 0)
r = -ENOMEM;
break;
}
if (r == 0)
r = -errno;
}
} else {
if (r == 0)
r = -errno;
}
}
}
if (r == 0)
r = k;
} else {
log_error("Out of memory");
if (r == 0)
r = k;
} else {
/* We need to bind mount our own tty on
* unless on a devpts file system. But to bind
* mount it we first have to create a device
* node where we can bind mount it on. This is
* kinda ugly since the TTY will very likely
* exist in the container. */
if (r == 0)
r = -errno;
}
if (r == 0)
r = -errno;
}
}
}
return r;
}
static int drop_capabilities(void) {
static const unsigned long retain[] = {
};
unsigned long l;
unsigned i;
for (i = 0; i < ELEMENTSOF(retain); i++)
if (retain[i] == l)
break;
if (i < ELEMENTSOF(retain))
continue;
if (prctl(PR_CAPBSET_DROP, l) < 0) {
/* If this capability is not known, EINVAL
* will be returned, let's ignore this. */
continue;
log_error("PR_CAPBSET_DROP failed: %m");
return -errno;
}
}
return 0;
}
static int is_os_tree(const char *path) {
int r;
char *p;
return -ENOMEM;
free(p);
return r < 0 ? 0 : 1;
}
int r = EXIT_FAILURE;
log_open();
goto finish;
if (arg_directory) {
char *p;
arg_directory = p;
} else
if (!arg_directory) {
log_error("Failed to determine path");
goto finish;
}
if (geteuid() != 0) {
log_error("Need to be root.");
goto finish;
}
log_error("Spawning constainer on root directory not supported.");
goto finish;
}
if (is_os_tree(arg_directory) <= 0) {
goto finish;
}
if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS|CLONE_NEWNET, NULL)) < 0) {
log_error("clone() failed: %m");
goto finish;
}
if (pid == 0) {
const char *hn;
/* child */
if (mount_all(arg_directory) < 0)
goto child_fail;
if (copy_devnodes(arg_directory) < 0)
goto child_fail;
if (chdir(arg_directory) < 0) {
goto child_fail;
}
log_error("mount(MS_MOVE) failed: %m");
goto child_fail;
}
if (chroot(".") < 0) {
log_error("chroot() failed: %m");
goto child_fail;
}
if (chdir("/") < 0) {
log_error("chdir() failed: %m");
goto child_fail;
}
if (drop_capabilities() < 0)
goto child_fail;
else
log_error("execv() failed: %m");
}
if (r < 0)
r = EXIT_FAILURE;
if (pid > 0)
return r;
}