1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
26 #include <sys/syscall.h>
27 #include <sys/mount.h>
33 #include <sys/prctl.h>
34 #include <sys/capability.h>
40 static char *arg_directory = NULL;
42 static int help(void) {
44 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
45 "Spawn a minimal namespace container for debugging, testing and building.\n\n"
46 " -h --help Show this help\n"
47 " -D --directory=NAME Root directory for the container\n",
48 program_invocation_short_name);
53 static int parse_argv(int argc, char *argv[]) {
55 static const struct option options[] = {
56 { "help", no_argument, NULL, 'h' },
57 { "directory", required_argument, NULL, 'D' },
66 while ((c = getopt_long(argc, argv, "+hD:", options, NULL)) >= 0) {
76 if (!(arg_directory = strdup(optarg))) {
77 log_error("Failed to duplicate root directory.");
87 log_error("Unknown option code %c", c);
95 static int mount_all(const char *dest) {
97 typedef struct MountPoint {
105 static const MountPoint mount_table[] = {
106 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV },
107 { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND }, /* Bind mount first */
108 { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT }, /* Then, make it r/o */
109 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY },
110 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID },
111 { "/dev/pts", "/dev/pts", "bind", NULL, MS_BIND },
112 { "tmpfs", "/dev/.run", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV },
118 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
122 if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) {
123 log_error("Out of memory");
131 if ((t = path_is_mount_point(where)) < 0) {
132 log_error("Failed to detect whether %s is a mount point: %s", where, strerror(-t));
141 mkdir_p(where, 0755);
143 if (mount(mount_table[k].what,
146 mount_table[k].flags,
147 mount_table[k].options) < 0) {
149 log_error("mount(%s) failed: %m", where);
161 static int copy_devnodes(const char *dest) {
163 static const char devnodes[] =
179 NULSTR_FOREACH(d, devnodes) {
180 char *from = NULL, *to = NULL;
183 asprintf(&from, "/dev/%s", d);
184 asprintf(&to, "%s/dev/%s", dest, d);
187 log_error("Failed to allocate devnode path");
198 if (stat(from, &st) < 0) {
200 if (errno != ENOENT) {
201 log_error("Failed to stat %s: %m", from);
208 if (mknod(to, st.st_mode, st.st_rdev) < 0) {
209 log_error("mknod(%s) failed: %m", dest);
220 if ((k = get_ctty(&tty, &tty_devnum)) < 0) {
221 log_error("Failed to determine controlling tty: %s", strerror(-k));
226 char *from = NULL, *to = NULL;
228 asprintf(&from, "/dev/%s", tty);
229 asprintf(&to, "%s/dev/console", dest);
232 log_error("Out of memory");
237 /* We need to bind mount our own tty on
238 * /dev/console, since ptys cannot be used
239 * unless on a devpts file system. But to bind
240 * mount it we first have to create a device
241 * node where we can bind mount it on. This is
242 * kinda ugly since the TTY will very likely
243 * be owned by a user/group that does not
244 * exist in the container. */
246 if (mknod(to, S_IFCHR|0600, tty_devnum) < 0) {
247 log_error("mknod for /dev/console failed: %m");
253 if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
254 log_error("bind mount for /dev/console failed: %m");
270 static int drop_capabilities(void) {
271 static const unsigned long retain[] = {
281 CAP_NET_BIND_SERVICE,
297 for (l = 0; l <= MAX(63LU, (unsigned long) CAP_LAST_CAP); l ++) {
300 for (i = 0; i < ELEMENTSOF(retain); i++)
304 if (i < ELEMENTSOF(retain))
307 if (prctl(PR_CAPBSET_DROP, l) < 0) {
309 /* If this capability is not known, EINVAL
310 * will be returned, let's ignore this. */
314 log_error("PR_CAPBSET_DROP failed: %m");
322 static int is_os_tree(const char *path) {
325 /* We use /bin/sh as flag file if something is an OS */
327 if (asprintf(&p, "%s/bin/sh", path) < 0)
333 return r < 0 ? 0 : 1;
337 int main(int argc, char *argv[]) {
339 int r = EXIT_FAILURE;
341 log_parse_environment();
344 if ((r = parse_argv(argc, argv)) <= 0)
350 p = path_make_absolute_cwd(arg_directory);
354 arg_directory = get_current_dir_name();
356 if (!arg_directory) {
357 log_error("Failed to determine path");
361 path_kill_slashes(arg_directory);
363 if (geteuid() != 0) {
364 log_error("Need to be root.");
368 if (path_equal(arg_directory, "/")) {
369 log_error("Spawning constainer on root directory not supported.");
373 if (is_os_tree(arg_directory) <= 0) {
374 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
378 log_info("Spawning namespace container on %s.", arg_directory);
380 if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS|CLONE_NEWNET, NULL)) < 0) {
381 log_error("clone() failed: %m");
390 if (mount_all(arg_directory) < 0)
393 if (copy_devnodes(arg_directory) < 0)
396 if (chdir(arg_directory) < 0) {
397 log_error("chdir(%s) failed: %m", arg_directory);
400 if (mount(arg_directory, "/", "bind", MS_BIND|MS_MOVE, NULL) < 0) {
401 log_error("mount(MS_MOVE) failed: %m");
405 if (chroot(".") < 0) {
406 log_error("chroot() failed: %m");
410 if (chdir("/") < 0) {
411 log_error("chdir() failed: %m");
415 if (drop_capabilities() < 0)
418 if ((hn = file_name_from_path(arg_directory)))
419 sethostname(hn, strlen(hn));
422 execvp(argv[optind], argv + optind);
424 execl("/bin/bash", "/bin/bash", NULL);
426 log_error("execv() failed: %m");
432 r = wait_for_terminate_and_warn("container", pid);