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>
41 static char *arg_directory = NULL;
43 static int help(void) {
45 printf("%s [OPTIONS...] [PATH] [ARGUMENTS...]\n\n"
46 "Spawn a minimal namespace container for debugging, testing and building.\n\n"
47 " -h --help Show this help\n"
48 " -D --directory=NAME Root directory for the container\n",
49 program_invocation_short_name);
54 static int parse_argv(int argc, char *argv[]) {
56 static const struct option options[] = {
57 { "help", no_argument, NULL, 'h' },
58 { "directory", required_argument, NULL, 'D' },
67 while ((c = getopt_long(argc, argv, "+hD:", options, NULL)) >= 0) {
77 if (!(arg_directory = strdup(optarg))) {
78 log_error("Failed to duplicate root directory.");
88 log_error("Unknown option code %c", c);
96 static int mount_all(const char *dest) {
98 typedef struct MountPoint {
107 static const MountPoint mount_table[] = {
108 { "proc", "/proc", "proc", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
109 { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND, true }, /* Bind mount first */
110 { "/proc/sys", "/proc/sys", "bind", NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, true }, /* Then, make it r/o */
111 { "sysfs", "/sys", "sysfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, true },
112 { "tmpfs", "/dev", "tmpfs", "mode=755", MS_NOSUID, true },
113 { "/dev/pts", "/dev/pts", "bind", NULL, MS_BIND, true },
114 { "tmpfs", "/dev/.run", "tmpfs", "mode=755", MS_NOSUID|MS_NOEXEC|MS_NODEV, true },
116 { "selinux", "/selinux", "selinuxfs", NULL, MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY, false },
123 for (k = 0; k < ELEMENTSOF(mount_table); k++) {
127 if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) {
128 log_error("Out of memory");
136 if ((t = path_is_mount_point(where)) < 0) {
137 log_error("Failed to detect whether %s is a mount point: %s", where, strerror(-t));
146 mkdir_p(where, 0755);
148 if (mount(mount_table[k].what,
151 mount_table[k].flags,
152 mount_table[k].options) < 0 &&
153 mount_table[k].fatal) {
155 log_error("mount(%s) failed: %m", where);
167 static int copy_devnodes(const char *dest) {
169 static const char devnodes[] =
188 NULSTR_FOREACH(d, devnodes) {
189 char *from = NULL, *to = NULL;
192 asprintf(&from, "/dev/%s", d);
193 asprintf(&to, "%s/dev/%s", dest, d);
196 log_error("Failed to allocate devnode path");
207 if (stat(from, &st) < 0) {
209 if (errno != ENOENT) {
210 log_error("Failed to stat %s: %m", from);
217 if (mknod(to, st.st_mode, st.st_rdev) < 0) {
218 log_error("mknod(%s) failed: %m", dest);
229 if ((k = get_ctty(&tty, &tty_devnum)) < 0) {
230 log_error("Failed to determine controlling tty: %s", strerror(-k));
235 char *from = NULL, *to = NULL;
237 asprintf(&from, "/dev/%s", tty);
238 asprintf(&to, "%s/dev/console", dest);
241 log_error("Out of memory");
246 /* We need to bind mount our own tty on
247 * /dev/console, since ptys cannot be used
248 * unless on a devpts file system. But to bind
249 * mount it we first have to create a device
250 * node where we can bind mount it on. This is
251 * kinda ugly since the TTY will very likely
252 * be owned by a user/group that does not
253 * exist in the container. */
255 if (mknod(to, S_IFCHR|0600, tty_devnum) < 0) {
256 log_error("mknod for /dev/console failed: %m");
262 if (mount(from, to, "bind", MS_BIND|MS_RDONLY, NULL) < 0) {
263 log_error("bind mount for /dev/console failed: %m");
281 static int drop_capabilities(void) {
282 static const unsigned long retain[] = {
292 CAP_NET_BIND_SERVICE,
308 for (l = 0; l <= MAX(63LU, (unsigned long) CAP_LAST_CAP); l ++) {
311 for (i = 0; i < ELEMENTSOF(retain); i++)
315 if (i < ELEMENTSOF(retain))
318 if (prctl(PR_CAPBSET_DROP, l) < 0) {
320 /* If this capability is not known, EINVAL
321 * will be returned, let's ignore this. */
325 log_error("PR_CAPBSET_DROP failed: %m");
333 static int is_os_tree(const char *path) {
336 /* We use /bin/sh as flag file if something is an OS */
338 if (asprintf(&p, "%s/bin/sh", path) < 0)
344 return r < 0 ? 0 : 1;
348 int main(int argc, char *argv[]) {
350 int r = EXIT_FAILURE;
352 log_parse_environment();
355 if ((r = parse_argv(argc, argv)) <= 0)
361 p = path_make_absolute_cwd(arg_directory);
365 arg_directory = get_current_dir_name();
367 if (!arg_directory) {
368 log_error("Failed to determine path");
372 path_kill_slashes(arg_directory);
374 if (geteuid() != 0) {
375 log_error("Need to be root.");
379 if (path_equal(arg_directory, "/")) {
380 log_error("Spawning container on root directory not supported.");
384 if (is_os_tree(arg_directory) <= 0) {
385 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
389 log_info("Spawning namespace container on %s.", arg_directory);
391 if ((pid = syscall(__NR_clone, SIGCHLD|CLONE_NEWIPC|CLONE_NEWNS|CLONE_NEWPID|CLONE_NEWUTS, NULL)) < 0) {
392 log_error("clone() failed: %m");
398 const char *envp[] = {
400 "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
406 if (mount_all(arg_directory) < 0)
409 if (copy_devnodes(arg_directory) < 0)
412 if (chdir(arg_directory) < 0) {
413 log_error("chdir(%s) failed: %m", arg_directory);
416 if (mount(arg_directory, "/", "bind", MS_BIND|MS_MOVE, NULL) < 0) {
417 log_error("mount(MS_MOVE) failed: %m");
421 if (chroot(".") < 0) {
422 log_error("chroot() failed: %m");
426 if (chdir("/") < 0) {
427 log_error("chdir() failed: %m");
431 if (drop_capabilities() < 0)
434 if ((hn = file_name_from_path(arg_directory)))
435 sethostname(hn, strlen(hn));
438 execvpe(argv[optind], argv + optind, (char**) envp);
441 execle("/bin/bash", "-bash", NULL, (char**) envp);
444 log_error("execv() failed: %m");
450 r = wait_for_terminate_and_warn(argc > optind ? argv[optind] : "bash", pid);