[systemd-commits] 7 commits - .gitignore Makefile.am man/systemd.xml src/cgroups-agent.c src/dbus.c src/dbus-common.c src/execute.c src/execute.h src/.gitignore src/manager.c src/nspawn.c src/quotacheck.c src/service.c src/systemctl.c src/util.c src/util.h TODO units/systemd-logger.socket units/systemd-shutdownd.socket

Lennart Poettering lennart at kemper.freedesktop.org
Sun Mar 13 19:12:36 PDT 2011


 .gitignore                     |    1 
 Makefile.am                    |   12 +
 TODO                           |    2 
 man/systemd.xml                |    8 
 src/.gitignore                 |    1 
 src/cgroups-agent.c            |   15 +
 src/dbus-common.c              |   12 -
 src/dbus.c                     |    2 
 src/execute.c                  |    4 
 src/execute.h                  |    2 
 src/manager.c                  |   15 +
 src/nspawn.c                   |  444 +++++++++++++++++++++++++++++++++++++++++
 src/quotacheck.c               |    2 
 src/service.c                  |    2 
 src/systemctl.c                |    4 
 src/util.c                     |   90 +++++++-
 src/util.h                     |    2 
 units/systemd-logger.socket    |    2 
 units/systemd-shutdownd.socket |    2 
 19 files changed, 587 insertions(+), 35 deletions(-)

New commits:
commit 6df6b93910da6cf501325d861fcf2d7f8b8bf556
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 03:12:04 2011 +0100

    nspawn: improve exit warning

diff --git a/src/nspawn.c b/src/nspawn.c
index 4e4d40e..fa70e86 100644
--- a/src/nspawn.c
+++ b/src/nspawn.c
@@ -366,7 +366,7 @@ int main(int argc, char *argv[]) {
         }
 
         if (path_equal(arg_directory, "/")) {
-                log_error("Spawning constainer on root directory not supported.");
+                log_error("Spawning container on root directory not supported.");
                 goto finish;
         }
 
@@ -429,7 +429,7 @@ int main(int argc, char *argv[]) {
                 _exit(EXIT_FAILURE);
         }
 
-        r = wait_for_terminate_and_warn("container", pid);
+        r = wait_for_terminate_and_warn(argc > optind ? argv[optind] : "bash", pid);
 
         if (r < 0)
                 r = EXIT_FAILURE;

commit 91b22f21f3824c1766d34f622c5bbb70cbe881a8
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 03:10:09 2011 +0100

    core: move abstract namespace sockets to /dev/.run
    
    Now that we have /dev/.run there's no need to use abstract namespace
    sockets. So, let's move things to /dev/.run, to make things more easily
    discoverable and improve compat with chroot() and fs namespacing.

diff --git a/man/systemd.xml b/man/systemd.xml
index 6b1a4c9..5a4c4ab 100644
--- a/man/systemd.xml
+++ b/man/systemd.xml
@@ -1024,7 +1024,7 @@
 
                 <variablelist>
                         <varlistentry>
-                                <term><filename>@/org/freedesktop/systemd1/notify</filename></term>
+                                <term><filename>/var/run/systemd/notify</filename></term>
 
                                 <listitem><para>Daemon status
                                 notification socket. This is an AF_UNIX
@@ -1037,7 +1037,7 @@
                         </varlistentry>
 
                         <varlistentry>
-                                <term><filename>@/org/freedesktop/systemd1/logger</filename></term>
+                                <term><filename>/var/run/systemd/logger</filename></term>
 
                                 <listitem><para>Used internally by the
                                 <filename>systemd-logger.service</filename>
@@ -1050,7 +1050,7 @@
                         </varlistentry>
 
                         <varlistentry>
-                                <term><filename>@/org/freedesktop/systemd1/shutdown</filename></term>
+                                <term><filename>/var/run/systemd/shutdownd</filename></term>
 
                                 <listitem><para>Used internally by the
                                 <citerefentry><refentrytitle>shutdown</refentrytitle><manvolnum>8</manvolnum></citerefentry>
@@ -1061,7 +1061,7 @@
                         </varlistentry>
 
                         <varlistentry>
-                                <term><filename>@/org/freedesktop/systemd1/private</filename></term>
+                                <term><filename>/var/run/systemd/private</filename></term>
 
                                 <listitem><para>Used internally as
                                 communication channel between
diff --git a/src/cgroups-agent.c b/src/cgroups-agent.c
index 7b4fca2..18612ec 100644
--- a/src/cgroups-agent.c
+++ b/src/cgroups-agent.c
@@ -49,10 +49,19 @@ int main(int argc, char *argv[]) {
          * this to avoid an activation loop when we start dbus when we
          * are called when the dbus service is shut down. */
 
-        if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
-                log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
-                goto finish;
+        if (!(bus = dbus_connection_open_private("unix:path=/dev/.run/systemd/private", &error))) {
+#ifndef LEGACY
+                dbus_error_free(&error);
+
+                /* Retry with the pre v21 socket name, to ease upgrades */
+                if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
+#endif
+                        log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
+                        goto finish;
+                }
+#ifndef LEGACY
         }
+#endif
 
         if (bus_check_peercred(bus) < 0) {
                 log_error("Bus owner not root.");
diff --git a/src/dbus-common.c b/src/dbus-common.c
index 25b718e..bb9cf2e 100644
--- a/src/dbus-common.c
+++ b/src/dbus-common.c
@@ -104,8 +104,16 @@ int bus_connect(DBusBusType t, DBusConnection **_bus, bool *private, DBusError *
 
         /* If we are root, then let's not go via the bus */
         if (geteuid() == 0 && t == DBUS_BUS_SYSTEM) {
-                if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
-                        return -EIO;
+
+                if (!(bus = dbus_connection_open_private("unix:path=/dev/.run/systemd/private", error))) {
+#ifndef LEGACY
+                        dbus_error_free(error);
+
+                        /* Retry with the pre v21 socket name, to ease upgrades */
+                        if (!(bus = dbus_connection_open_private("unix:abstract=/org/freedesktop/systemd1/private", error)))
+#endif
+                                return -EIO;
+                }
 
                 dbus_connection_set_exit_on_disconnect(bus, FALSE);
 
diff --git a/src/dbus.c b/src/dbus.c
index af03c57..31e776f 100644
--- a/src/dbus.c
+++ b/src/dbus.c
@@ -955,7 +955,7 @@ static int bus_init_private(Manager *m) {
         if (getpid() != 1)
                 return 0;
 
-        if (!(m->private_bus = dbus_server_listen("unix:abstract=/org/freedesktop/systemd1/private", &error))) {
+        if (!(m->private_bus = dbus_server_listen("unix:path=/dev/.run/systemd/private", &error))) {
                 log_error("Failed to create private D-Bus server: %s", error.message);
                 r = -EIO;
                 goto fail;
diff --git a/src/execute.c b/src/execute.c
index ee05e99..556ff9b 100644
--- a/src/execute.c
+++ b/src/execute.c
@@ -173,9 +173,9 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
 
         zero(sa);
         sa.sa.sa_family = AF_UNIX;
-        strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
+        strncpy(sa.un.sun_path, LOGGER_SOCKET, sizeof(sa.un.sun_path));
 
-        if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + sizeof(LOGGER_SOCKET) - 1) < 0) {
+        if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + sizeof(LOGGER_SOCKET) - 1) < 0) {
                 close_nointr_nofail(fd);
                 return -errno;
         }
diff --git a/src/execute.h b/src/execute.h
index 2856d2f..e77cdcf 100644
--- a/src/execute.h
+++ b/src/execute.h
@@ -40,7 +40,7 @@ struct CGroupBonding;
 #include "util.h"
 
 /* Abstract namespace! */
-#define LOGGER_SOCKET "/org/freedesktop/systemd1/logger"
+#define LOGGER_SOCKET "/dev/.run/systemd/logger"
 
 /* This doesn't really belong here, but I couldn't find a better place to put this. */
 #define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT
diff --git a/src/manager.c b/src/manager.c
index 8bbde7c..6ccb03f 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -66,7 +66,8 @@
 #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC)
 
 /* Where clients shall send notification messages to */
-#define NOTIFY_SOCKET "/org/freedesktop/systemd1/notify"
+#define NOTIFY_SOCKET_SYSTEM "/dev/.run/systemd/notify"
+#define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify"
 
 static int manager_setup_notify(Manager *m) {
         union {
@@ -88,9 +89,12 @@ static int manager_setup_notify(Manager *m) {
         sa.sa.sa_family = AF_UNIX;
 
         if (getpid() != 1)
-                snprintf(sa.un.sun_path+1, sizeof(sa.un.sun_path)-1, NOTIFY_SOCKET "/%llu", random_ull());
+                snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull());
         else
-                strncpy(sa.un.sun_path+1, NOTIFY_SOCKET, sizeof(sa.un.sun_path)-1);
+                strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path));
+
+        if (sa.un.sun_path[0] == '@')
+                sa.un.sun_path[0] = 0;
 
         if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) {
                 log_error("bind() failed: %m");
@@ -109,7 +113,10 @@ static int manager_setup_notify(Manager *m) {
         if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0)
                 return -errno;
 
-        if (!(m->notify_socket = strdup(sa.un.sun_path+1)))
+        if (sa.un.sun_path[0] == 0)
+                sa.un.sun_path[0] = '@';
+
+        if (!(m->notify_socket = strdup(sa.un.sun_path)))
                 return -ENOMEM;
 
         log_debug("Using notification socket %s", m->notify_socket);
diff --git a/src/service.c b/src/service.c
index 70999f3..e7a9e7c 100644
--- a/src/service.c
+++ b/src/service.c
@@ -1659,7 +1659,7 @@ static int service_spawn(
         }
 
         if (set_notify_socket)
-                if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
+                if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", s->meta.manager->notify_socket) < 0) {
                         r = -ENOMEM;
                         goto fail;
                 }
diff --git a/src/systemctl.c b/src/systemctl.c
index b8af654..5db094f 100644
--- a/src/systemctl.c
+++ b/src/systemctl.c
@@ -5364,7 +5364,7 @@ static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
         zero(sockaddr);
         sockaddr.sa.sa_family = AF_UNIX;
         sockaddr.un.sun_path[0] = 0;
-        strncpy(sockaddr.un.sun_path+1, "/org/freedesktop/systemd1/shutdownd", sizeof(sockaddr.un.sun_path)-1);
+        strncpy(sockaddr.un.sun_path, "/dev/.run/systemd/shutdownd", sizeof(sockaddr.un.sun_path));
 
         zero(iovec);
         iovec.iov_base = (char*) &c;
@@ -5372,7 +5372,7 @@ static int send_shutdownd(usec_t t, char mode, bool warn, const char *message) {
 
         zero(msghdr);
         msghdr.msg_name = &sockaddr;
-        msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + 1 + sizeof("/org/freedesktop/systemd1/shutdownd") - 1;
+        msghdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + sizeof("/dev/.run/systemd/shutdownd") - 1;
 
         msghdr.msg_iov = &iovec;
         msghdr.msg_iovlen = 1;
diff --git a/units/systemd-logger.socket b/units/systemd-logger.socket
index 57244a2..5cf6a9b 100644
--- a/units/systemd-logger.socket
+++ b/units/systemd-logger.socket
@@ -13,4 +13,4 @@ DefaultDependencies=no
 Before=sockets.target
 
 [Socket]
-ListenStream=@/org/freedesktop/systemd1/logger
+ListenStream=/dev/.run/systemd/logger
diff --git a/units/systemd-shutdownd.socket b/units/systemd-shutdownd.socket
index 0df24cf..6faf36f 100644
--- a/units/systemd-shutdownd.socket
+++ b/units/systemd-shutdownd.socket
@@ -13,4 +13,4 @@ DefaultDependencies=no
 Before=sockets.target
 
 [Socket]
-ListenDatagram=@/org/freedesktop/systemd1/shutdownd
+ListenDatagram=/dev/.run/systemd/shutdownd

commit 88213476187cafc86bea2276199891873000588d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 02:40:36 2011 +0100

    nspawn: add simple chroot(1) like tool to execute commands in a namespace container

diff --git a/.gitignore b/.gitignore
index ffc602a..d679f79 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
+systemd-nspawn
 systemd-stdio-bridge
 systemd-machine-id-setup
 systemd-detect-virt
diff --git a/Makefile.am b/Makefile.am
index 872bcc2..7d6cfd1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -115,6 +115,9 @@ bin_PROGRAMS = \
 	systemd-cgls \
 	systemd-stdio-bridge
 
+sbin_PROGRAMS = \
+        systemd-nspawn
+
 if HAVE_GTK
 bin_PROGRAMS += \
 	systemadm \
@@ -983,6 +986,15 @@ systemd_cgls_CFLAGS = \
 systemd_cgls_LDADD = \
 	libsystemd-basic.la
 
+systemd_nspawn_SOURCES = \
+	src/nspawn.c
+
+systemd_nspawn_CFLAGS = \
+	$(AM_CFLAGS)
+
+systemd_nspawn_LDADD = \
+	libsystemd-basic.la
+
 systemd_stdio_bridge_SOURCES = \
 	src/bridge.c
 
diff --git a/src/nspawn.c b/src/nspawn.c
new file mode 100644
index 0000000..4e4d40e
--- /dev/null
+++ b/src/nspawn.c
@@ -0,0 +1,444 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  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 <sys/types.h>
+#include <sys/syscall.h>
+#include <sys/mount.h>
+#include <sys/wait.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/prctl.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",
+               program_invocation_short_name);
+
+        return 0;
+}
+
+static int parse_argv(int argc, char *argv[]) {
+
+        static const struct option options[] = {
+                { "help",      no_argument,       NULL, 'h' },
+                { "directory", required_argument, NULL, 'D' },
+                { NULL,        0,                 NULL, 0   }
+        };
+
+        int c;
+
+        assert(argc >= 0);
+        assert(argv);
+
+        while ((c = getopt_long(argc, argv, "+hD:", options, NULL)) >= 0) {
+
+                switch (c) {
+
+                case 'h':
+                        help();
+                        return 0;
+
+                case 'D':
+                        free(arg_directory);
+                        if (!(arg_directory = strdup(optarg))) {
+                                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;
+}
+
+static int mount_all(const char *dest) {
+
+        typedef struct MountPoint {
+                const char *what;
+                const char *where;
+                const char *type;
+                const char *options;
+                unsigned long flags;
+        } MountPoint;
+
+        static const MountPoint mount_table[] = {
+                { "proc",      "/proc",     "proc",   NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV },
+                { "/proc/sys", "/proc/sys", "bind",   NULL,        MS_BIND },                      /* Bind mount first */
+                { "/proc/sys", "/proc/sys", "bind",   NULL,        MS_BIND|MS_RDONLY|MS_REMOUNT }, /* Then, make it r/o */
+                { "sysfs",     "/sys",      "sysfs",  NULL,        MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_RDONLY },
+                { "tmpfs",     "/dev",      "tmpfs",  "mode=755",  MS_NOSUID },
+                { "/dev/pts",  "/dev/pts",  "bind",   NULL,        MS_BIND },
+                { "tmpfs",     "/dev/.run", "tmpfs",  "mode=755",  MS_NOSUID|MS_NOEXEC|MS_NODEV },
+        };
+
+        unsigned k;
+        int r = 0;
+
+        for (k = 0; k < ELEMENTSOF(mount_table); k++) {
+                char *where;
+                int t;
+
+                if (asprintf(&where, "%s/%s", dest, mount_table[k].where) < 0) {
+                        log_error("Out of memory");
+
+                        if (r == 0)
+                                r = -ENOMEM;
+
+                        break;
+                }
+
+                if ((t = path_is_mount_point(where)) < 0) {
+                        log_error("Failed to detect whether %s is a mount point: %s", where, strerror(-t));
+                        free(where);
+
+                        if (r == 0)
+                                r = t;
+
+                        continue;
+                }
+
+                mkdir_p(where, 0755);
+
+                if (mount(mount_table[k].what,
+                          where,
+                          mount_table[k].type,
+                          mount_table[k].flags,
+                          mount_table[k].options) < 0) {
+
+                        log_error("mount(%s) failed: %m", where);
+
+                        if (r == 0)
+                                r = -errno;
+                }
+
+                free(where);
+        }
+
+        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;
+        char *tty = NULL;
+        dev_t tty_devnum;
+
+        NULSTR_FOREACH(d, devnodes) {
+                char *from = NULL, *to = NULL;
+                struct stat st;
+
+                asprintf(&from, "/dev/%s", d);
+                asprintf(&to, "%s/dev/%s", dest, d);
+
+                if (!from || !to) {
+                        log_error("Failed to allocate devnode path");
+
+                        free(from);
+                        free(to);
+
+                        if (r == 0)
+                                r = -ENOMEM;
+
+                        break;
+                }
+
+                if (stat(from, &st) < 0) {
+
+                        if (errno != ENOENT) {
+                                log_error("Failed to stat %s: %m", from);
+
+                                if (r == 0)
+                                        r = -errno;
+                        }
+
+                } else {
+                        if (mknod(to, st.st_mode, st.st_rdev) < 0) {
+                                log_error("mknod(%s) failed: %m", dest);
+
+                                if (r == 0)
+                                        r = -errno;
+                        }
+                }
+
+                free(from);
+                free(to);
+        }
+
+        if ((k = get_ctty(&tty, &tty_devnum)) < 0) {
+                log_error("Failed to determine controlling tty: %s", strerror(-k));
+
+                if (r == 0)
+                        r = k;
+        } else {
+                char *from = NULL, *to = NULL;
+
+                asprintf(&from, "/dev/%s", tty);
+                asprintf(&to, "%s/dev/console", dest);
+
+                if (!from || !to) {
+                        log_error("Out of memory");
+
+                        if (r == 0)
+                                r = k;
+                } else {
+                        /* We need to bind mount our own tty on
+                         * /dev/console, since ptys cannot be used
+                         * 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
+                         * be owned by a user/group that does not
+                         * exist in the container. */
+
+                        if (mknod(to, S_IFCHR|0600, tty_devnum) < 0) {
+                                log_error("mknod for /dev/console failed: %m");
+
+                                if (r == 0)
+                                        r = -errno;
+                        }
+
+                        if (mount(from, to, "bind", MS_BIND, NULL) < 0) {
+                                log_error("bind mount for /dev/console failed: %m");
+
+                                if (r == 0)
+                                        r = -errno;
+                        }
+                }
+
+                free(from);
+                free(to);
+        }
+
+        free(tty);
+
+        return r;
+}
+
+static int drop_capabilities(void) {
+        static const unsigned long retain[] = {
+                CAP_CHOWN,
+                CAP_DAC_OVERRIDE,
+                CAP_DAC_READ_SEARCH,
+                CAP_FOWNER,
+                CAP_FSETID,
+                CAP_IPC_OWNER,
+                CAP_KILL,
+                CAP_LEASE,
+                CAP_LINUX_IMMUTABLE,
+                CAP_NET_BIND_SERVICE,
+                CAP_NET_BROADCAST,
+                CAP_NET_RAW,
+                CAP_SETGID,
+                CAP_SETFCAP,
+                CAP_SETPCAP,
+                CAP_SETUID,
+                CAP_SYS_ADMIN,
+                CAP_SYS_CHROOT,
+                CAP_SYS_NICE,
+                CAP_SYS_PTRACE,
+                CAP_SYS_TTY_CONFIG
+        };
+
+        unsigned long l;
+
+        for (l = 0; l <= MAX(63LU, (unsigned long) CAP_LAST_CAP); 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. */
+                        if (errno == EINVAL)
+                                continue;
+
+                        log_error("PR_CAPBSET_DROP failed: %m");
+                        return -errno;
+                }
+        }
+
+        return 0;
+}
+
+static int is_os_tree(const char *path) {
+        int r;
+        char *p;
+        /* We use /bin/sh as flag file if something is an OS */
+
+        if (asprintf(&p, "%s/bin/sh", path) < 0)
+                return -ENOMEM;
+
+        r = access(p, F_OK);
+        free(p);
+
+        return r < 0 ? 0 : 1;
+}
+
+
+int main(int argc, char *argv[]) {
+        pid_t pid = 0;
+        int r = EXIT_FAILURE;
+
+        log_parse_environment();
+        log_open();
+
+        if ((r = parse_argv(argc, argv)) <= 0)
+                goto finish;
+
+        if (arg_directory) {
+                char *p;
+
+                p = path_make_absolute_cwd(arg_directory);
+                free(arg_directory);
+                arg_directory = p;
+        } else
+                arg_directory = get_current_dir_name();
+
+        if (!arg_directory) {
+                log_error("Failed to determine path");
+                goto finish;
+        }
+
+        path_kill_slashes(arg_directory);
+
+        if (geteuid() != 0) {
+                log_error("Need to be root.");
+                goto finish;
+        }
+
+        if (path_equal(arg_directory, "/")) {
+                log_error("Spawning constainer on root directory not supported.");
+                goto finish;
+        }
+
+        if (is_os_tree(arg_directory) <= 0) {
+                log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
+                goto finish;
+        }
+
+        log_info("Spawning namespace container on %s.", arg_directory);
+
+        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) {
+                        log_error("chdir(%s) failed: %m", arg_directory);
+                        goto child_fail;
+                }
+                if (mount(arg_directory, "/", "bind", MS_BIND|MS_MOVE, NULL) < 0) {
+                        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;
+
+                if ((hn = file_name_from_path(arg_directory)))
+                        sethostname(hn, strlen(hn));
+
+                if (argc > optind)
+                        execvp(argv[optind], argv + optind);
+                else
+                        execl("/bin/bash", "/bin/bash", NULL);
+
+                log_error("execv() failed: %m");
+
+        child_fail:
+                _exit(EXIT_FAILURE);
+        }
+
+        r = wait_for_terminate_and_warn("container", pid);
+
+        if (r < 0)
+                r = EXIT_FAILURE;
+
+finish:
+        free(arg_directory);
+
+        if (pid > 0)
+                kill(pid, SIGTERM);
+
+        return r;
+}

commit f9b9232be9db82cc729a56a2e99ecb27be546aac
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 02:36:00 2011 +0100

    util: detect CLONE_NEWPID namespaces, and cache results

diff --git a/TODO b/TODO
index a36ca5e..986e06a 100644
--- a/TODO
+++ b/TODO
@@ -22,6 +22,8 @@ F15:
 
 * 0595f9a1c182a84581749823ef47c5f292e545f9 is borked, freezes shutdown
 
+* capability_bounding_set_drop not used.
+
 Features:
 
 * optionally create watched directories in .path units
diff --git a/src/util.c b/src/util.c
index b2baa1b..38d630e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -3948,6 +3948,20 @@ int detect_vm(const char **id) {
 /* Returns a short identifier for the various VM/container implementations */
 int detect_virtualization(const char **id) {
         int r;
+        static __thread const char *cached_id = NULL;
+        const char *_id;
+        FILE *f;
+
+        if (cached_id) {
+
+                if (cached_id == (const char*) -1)
+                        return 0;
+
+                if (id)
+                        *id = cached_id;
+
+                return 1;
+        }
 
         /* Unfortunately most of these operations require root access
          * in one way or another */
@@ -3955,24 +3969,60 @@ int detect_virtualization(const char **id) {
                 return -EPERM;
 
         if ((r = running_in_chroot()) > 0) {
-                if (id)
-                        *id = "chroot";
+                _id = "chroot";
+                r = 1;
+                goto finish;
+        }
 
-                return r;
+        if ((f = fopen("/proc/self/cgroup", "r"))) {
+
+                for (;;) {
+                        char line[LINE_MAX], *p;
+
+                        if (!fgets(line, sizeof(line), f))
+                                break;
+
+                        if (!(p = strchr(strstrip(line), ':')))
+                                continue;
+
+                        if (strncmp(p, ":ns:", 4))
+                                continue;
+
+                        if (!streq(p, ":ns:/")) {
+                                fclose(f);
+
+                                r = 1;
+                                _id = "ns";
+                                goto finish;
+                        }
+                }
+
+                fclose(f);
         }
 
         /* /proc/vz exists in container and outside of the container,
          * /proc/bc only outside of the container. */
         if (access("/proc/vz", F_OK) >= 0 &&
             access("/proc/bc", F_OK) < 0) {
+                _id = "openvz";
+                r = 1;
+                goto finish;
+        }
 
-                if (id)
-                        *id = "openvz";
+        r = detect_vm(&_id);
 
-                return 1;
-        }
+finish:
+        if (r < 0)
+                return r;
+        else if (r > 0)
+                cached_id = _id;
+        else
+                cached_id = (const char*) -1;
 
-        return detect_vm(id);
+        if (id)
+                *id = _id;
+
+        return r;
 }
 
 void execute_directory(const char *directory, DIR *d, char *argv[]) {

commit 224170db0a2215284964fd9cc218681651713271
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 02:34:18 2011 +0100

    git: ignore generated policy file

diff --git a/src/.gitignore b/src/.gitignore
index 9c01ae1..4c7d3c8 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,3 +1,4 @@
+org.freedesktop.systemd1.policy
 gnome-ask-password-agent.c
 systemd-interfaces.c
 systemadm.c

commit 0a27cf3f32403f48059396cb43ad25d0a12ef64b
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 02:33:51 2011 +0100

    util: return exit status in wait_for_terminate_and_warn()

diff --git a/src/quotacheck.c b/src/quotacheck.c
index 55c2f0c..da2da3b 100644
--- a/src/quotacheck.c
+++ b/src/quotacheck.c
@@ -107,7 +107,7 @@ int main(int argc, char *argv[]) {
                 _exit(1); /* Operational error */
         }
 
-        r = wait_for_terminate_and_warn("quotacheck", pid) >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+        r = wait_for_terminate_and_warn("quotacheck", pid) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 
 finish:
         return r;
diff --git a/src/util.c b/src/util.c
index ee6217d..b2baa1b 100644
--- a/src/util.c
+++ b/src/util.c
@@ -3616,7 +3616,7 @@ int wait_for_terminate_and_warn(const char *name, pid_t pid) {
         if (status.si_code == CLD_EXITED) {
                 if (status.si_status != 0) {
                         log_warning("%s failed with error code %i.", name, status.si_status);
-                        return -EPROTO;
+                        return status.si_status;
                 }
 
                 log_debug("%s succeeded.", name);

commit 46824d0e6b2aae8f503464368d02c1da992f56f1
Author: Lennart Poettering <lennart at poettering.net>
Date:   Mon Mar 14 02:33:23 2011 +0100

    util: properly identify pty devices by their major

diff --git a/src/util.c b/src/util.c
index c9366c4..ee6217d 100644
--- a/src/util.c
+++ b/src/util.c
@@ -2865,7 +2865,7 @@ int getttyname_harder(int fd, char **r) {
 
         if (streq(s, "tty")) {
                 free(s);
-                return get_ctty(r);
+                return get_ctty(r, NULL);
         }
 
         *r = s;
@@ -2907,7 +2907,7 @@ int get_ctty_devnr(dev_t *d) {
         return 0;
 }
 
-int get_ctty(char **r) {
+int get_ctty(char **r, dev_t *_devnr) {
         int k;
         char fn[128], *s, *b, *p;
         dev_t devnr;
@@ -2925,6 +2925,18 @@ int get_ctty(char **r) {
                 if (k != -ENOENT)
                         return k;
 
+                /* This is an ugly hack */
+                if (major(devnr) == 136) {
+                        if (asprintf(&b, "pts/%lu", (unsigned long) minor(devnr)) < 0)
+                                return -ENOMEM;
+
+                        *r = b;
+                        if (_devnr)
+                                *_devnr = devnr;
+
+                        return 0;
+                }
+
                 /* Probably something like the ptys which have no
                  * symlink in /dev/char. Let's return something
                  * vaguely useful. */
@@ -2933,6 +2945,9 @@ int get_ctty(char **r) {
                         return -ENOMEM;
 
                 *r = b;
+                if (_devnr)
+                        *_devnr = devnr;
+
                 return 0;
         }
 
@@ -2950,6 +2965,9 @@ int get_ctty(char **r) {
                 return -ENOMEM;
 
         *r = b;
+        if (_devnr)
+                *_devnr = devnr;
+
         return 0;
 }
 
diff --git a/src/util.h b/src/util.h
index 13e5f62..320bcd7 100644
--- a/src/util.h
+++ b/src/util.h
@@ -337,7 +337,7 @@ int getttyname_malloc(int fd, char **r);
 int getttyname_harder(int fd, char **r);
 
 int get_ctty_devnr(dev_t *d);
-int get_ctty(char **r);
+int get_ctty(char **r, dev_t *_devnr);
 
 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
 



More information about the systemd-commits mailing list