[systemd-commits] 3 commits - Makefile.am TODO man/systemd-nspawn.xml src/journal src/nspawn

Lennart Poettering lennart at kemper.freedesktop.org
Wed Jul 18 17:04:06 PDT 2012


 Makefile.am              |    3 
 TODO                     |    4 
 man/systemd-nspawn.xml   |   48 ++++++++
 src/journal/sd-journal.c |    4 
 src/nspawn/nspawn.c      |  267 +++++++++++++++++++++++++++++++++++++++++------
 5 files changed, 293 insertions(+), 33 deletions(-)

New commits:
commit db7feb7e9c436ec3ad3b90cf21bd43d8036aad0d
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 19 02:03:42 2012 +0200

    nspawn: generate proper error messages in the child

diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 14de7f8..1d7511e 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -808,13 +808,15 @@ static int process_pty(int master, sigset_t *mask) {
         fd_nonblock(STDOUT_FILENO, 1);
         fd_nonblock(master, 1);
 
-        if ((signal_fd = signalfd(-1, mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
+        signal_fd = signalfd(-1, mask, SFD_NONBLOCK|SFD_CLOEXEC);
+        if (signal_fd < 0) {
                 log_error("signalfd(): %m");
                 r = -errno;
                 goto finish;
         }
 
-        if ((ep = epoll_create1(EPOLL_CLOEXEC)) < 0) {
+        ep = epoll_create1(EPOLL_CLOEXEC);
+        if (ep < 0) {
                 log_error("Failed to create epoll: %m");
                 r = -errno;
                 goto finish;
@@ -850,7 +852,8 @@ static int process_pty(int master, sigset_t *mask) {
                 ssize_t k;
                 int i, nfds;
 
-                if ((nfds = epoll_wait(ep, ev, ELEMENTSOF(ev), -1)) < 0) {
+                nfds = epoll_wait(ep, ev, ELEMENTSOF(ev), -1);
+                if (nfds < 0) {
 
                         if (errno == EINTR || errno == EAGAIN)
                                 continue;
@@ -885,7 +888,8 @@ static int process_pty(int master, sigset_t *mask) {
                                 struct signalfd_siginfo sfsi;
                                 ssize_t n;
 
-                                if ((n = read(signal_fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) {
+                                n = read(signal_fd, &sfsi, sizeof(sfsi));
+                                if (n != sizeof(sfsi)) {
 
                                         if (n >= 0) {
                                                 log_error("Failed to read from signalfd: invalid block size");
@@ -921,7 +925,8 @@ static int process_pty(int master, sigset_t *mask) {
 
                         if (stdin_readable && in_buffer_full < LINE_MAX) {
 
-                                if ((k = read(STDIN_FILENO, in_buffer + in_buffer_full, LINE_MAX - in_buffer_full)) < 0) {
+                                k = read(STDIN_FILENO, in_buffer + in_buffer_full, LINE_MAX - in_buffer_full);
+                                if (k < 0) {
 
                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
                                                 stdin_readable = false;
@@ -936,7 +941,8 @@ static int process_pty(int master, sigset_t *mask) {
 
                         if (master_writable && in_buffer_full > 0) {
 
-                                if ((k = write(master, in_buffer, in_buffer_full)) < 0) {
+                                k = write(master, in_buffer, in_buffer_full);
+                                if (k < 0) {
 
                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
                                                 master_writable = false;
@@ -955,7 +961,8 @@ static int process_pty(int master, sigset_t *mask) {
 
                         if (master_readable && out_buffer_full < LINE_MAX) {
 
-                                if ((k = read(master, out_buffer + out_buffer_full, LINE_MAX - out_buffer_full)) < 0) {
+                                k = read(master, out_buffer + out_buffer_full, LINE_MAX - out_buffer_full);
+                                if (k < 0) {
 
                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
                                                 master_readable = false;
@@ -970,7 +977,8 @@ static int process_pty(int master, sigset_t *mask) {
 
                         if (stdout_writable && out_buffer_full > 0) {
 
-                                if ((k = write(STDOUT_FILENO, out_buffer, out_buffer_full)) < 0) {
+                                k = write(STDOUT_FILENO, out_buffer, out_buffer_full);
+                                if (k < 0) {
 
                                         if (errno == EAGAIN || errno == EPIPE || errno == ECONNRESET || errno == EIO)
                                                 stdout_writable = false;
@@ -1015,7 +1023,8 @@ int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
 
-        if ((r = parse_argv(argc, argv)) <= 0)
+        r = parse_argv(argc, argv);
+        if (r <= 0)
                 goto finish;
 
         if (arg_directory) {
@@ -1054,7 +1063,8 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        if ((k = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 0, &oldcg)) < 0) {
+        k = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 0, &oldcg);
+        if (k < 0) {
                 log_error("Failed to determine current cgroup: %s", strerror(-k));
                 goto finish;
         }
@@ -1070,18 +1080,20 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
-        STRV_FOREACH(controller,arg_controllers) {
+        STRV_FOREACH(controller, arg_controllers) {
                 k = cg_create_and_attach(*controller, newcg, 0);
                 if (k < 0)
                         log_warning("Failed to create cgroup in controller %s: %s", *controller, strerror(-k));
         }
 
-        if ((master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY)) < 0) {
+        master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NDELAY);
+        if (master < 0) {
                 log_error("Failed to acquire pseudo tty: %m");
                 goto finish;
         }
 
-        if (!(console = ptsname(master))) {
+        console = ptsname(master);
+        if (!console) {
                 log_error("Failed to determine tty name: %m");
                 goto finish;
         }
@@ -1163,15 +1175,26 @@ int main(int argc, char *argv[]) {
                 assert_se(sigemptyset(&mask) == 0);
                 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
 
-                if (setsid() < 0)
+                if (open_terminal(console, O_RDWR) != STDIN_FILENO ||
+                    dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
+                    dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
+                        goto child_fail;
+
+                if (setsid() < 0) {
+                        log_error("setsid() failed: %m");
                         goto child_fail;
+                }
 
-                if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0)
+                if (prctl(PR_SET_PDEATHSIG, SIGKILL) < 0) {
+                        log_error("PR_SET_PDEATHSIG failed: %m");
                         goto child_fail;
+                }
 
                 /* Mark / as private, in case somebody marked it shared */
-                if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0)
+                if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) {
+                        log_error("MS_PRIVATE|MS_REC failed: %m");
                         goto child_fail;
+                }
 
                 /* Turn directory into bind mount */
                 if (mount(arg_directory, arg_directory, "bind", MS_BIND, NULL) < 0) {
@@ -1213,11 +1236,6 @@ int main(int argc, char *argv[]) {
                         goto child_fail;
                 }
 
-                if (open_terminal("dev/console", O_RDWR) != STDIN_FILENO ||
-                    dup2(STDIN_FILENO, STDOUT_FILENO) != STDOUT_FILENO ||
-                    dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO)
-                        goto child_fail;
-
                 if (mount(arg_directory, "/", "bind", MS_MOVE, NULL) < 0) {
                         log_error("mount(MS_BIND) failed: %m");
                         goto child_fail;

commit 9f8d29834ba97052403e50ec9b358c0470fa4ceb
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 19 02:03:22 2012 +0200

    journald: don't choke on journal files with no cutoff date

diff --git a/TODO b/TODO
index 27fc5a3..9d09916 100644
--- a/TODO
+++ b/TODO
@@ -40,6 +40,10 @@ Features:
 
 * syscall filter: add knowledge about compat syscalls
 
+* syscall filter: don't enforce no new privs?
+
+* syscall filter: option to return EPERM rather than SIGSYS?
+
 * logind: wakelock/opportunistic suspend support
 
 * switch-root: sockets need relabelling
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 497f79c..c3f19ca 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1950,6 +1950,8 @@ _public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
                 usec_t fr, t;
 
                 r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
+                if (r == -ENOENT)
+                        continue;
                 if (r < 0)
                         return r;
                 if (r == 0)
@@ -1987,6 +1989,8 @@ _public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot
                 usec_t fr, t;
 
                 r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
+                if (r == -ENOENT)
+                        continue;
                 if (r < 0)
                         return r;
                 if (r == 0)

commit 57fb9fb56db0584581ce33ee842dcbf5f1136856
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Jul 19 02:02:39 2012 +0200

    nspawn: introduce new --link-journal= switch to link container journals into host

diff --git a/Makefile.am b/Makefile.am
index 9575842..985e8ef 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1468,7 +1468,8 @@ systemd_nspawn_LDADD = \
 	libsystemd-label.la \
 	libsystemd-capability.la \
 	libsystemd-shared.la \
-	libsystemd-daemon.la
+	libsystemd-daemon.la \
+	libsystemd-id128-internal.la
 
 # ------------------------------------------------------------------------------
 systemd_stdio_bridge_SOURCES = \
diff --git a/man/systemd-nspawn.xml b/man/systemd-nspawn.xml
index b6f2c27..9f8b8e2 100644
--- a/man/systemd-nspawn.xml
+++ b/man/systemd-nspawn.xml
@@ -235,6 +235,54 @@
                                 CAP_SYS_RESOURCE.</para></listitem>
                         </varlistentry>
 
+                        <varlistentry>
+                                <term><option>--link-journal=</option></term>
+
+                                <listitem><para>Control whether the
+                                container's journal shall be made
+                                visible to the host system. If enabled
+                                allows viewing the container's journal
+                                files from the host (but not vice
+                                versa). Takes one of
+                                <literal>no</literal>,
+                                <literal>host</literal>,
+                                <literal>guest</literal>,
+                                <literal>auto</literal>. If
+                                <literal>no</literal> the journal is
+                                not linked. If <literal>host</literal>
+                                the journal files are stored on the
+                                host file system (beneath the host's
+                                <filename>/var/log/journal</filename>)
+                                and a per-machine subdirectory of this
+                                directory is created and bind mounted
+                                into the container at the same
+                                location. If <literal>guest</literal>
+                                the journal files are stored on the
+                                guest file system (beneath the guest's
+                                <filename>/var/log/journal</filename>)
+                                and a per-machine subdirectory of this
+                                directory is symlinked into the host
+                                at the same location. If
+                                <literal>auto</literal> (the default)
+                                and the subdirectory of
+                                <filename>/var/log/journal</filename>
+                                exists as directory it is bind mounted
+                                into the container, but nothing is
+                                done otherwise. Effectively, booting a
+                                container once with
+                                <literal>guest</literal> or
+                                <literal>host</literal> will link the
+                                journal persistantly if further one
+                                the default of <literal>auto</literal>
+                                is used.</para></listitem>
+                        </varlistentry>
+
+                        <varlistentry>
+                                <term><option>-j</option></term>
+
+                                <listitem><para>Equivalent to
+                                <option>--link-journal=guest</option>.</para></listitem>
+                        </varlistentry>
                 </variablelist>
 
         </refsect1>
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 2879db1..14de7f8 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -52,6 +52,14 @@
 #include "strv.h"
 #include "path-util.h"
 #include "loopback-setup.h"
+#include "sd-id128.h"
+
+typedef enum LinkJournal {
+        LINK_NO,
+        LINK_AUTO,
+        LINK_HOST,
+        LINK_GUEST
+} LinkJournal;
 
 static char *arg_directory = NULL;
 static char *arg_user = NULL;
@@ -60,6 +68,7 @@ static char *arg_uuid = NULL;
 static bool arg_private_network = false;
 static bool arg_read_only = false;
 static bool arg_boot = false;
+static LinkJournal arg_link_journal = LINK_AUTO;
 static uint64_t arg_retain =
         (1ULL << CAP_CHOWN) |
         (1ULL << CAP_DAC_OVERRIDE) |
@@ -88,15 +97,17 @@ 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"
-               "  -b --boot             Boot up full system (i.e. invoke init)\n"
-               "  -u --user=USER        Run the command under specified user or uid\n"
-               "  -C --controllers=LIST Put the container in specified comma-separated cgroup hierarchies\n"
-               "     --uuid=UUID        Set a specific machine UUID for the container\n"
-               "     --private-network  Disable network in container\n"
-               "     --read-only        Mount the root directory read-only\n"
-               "     --capability=CAP   In addition to the default, retain specified capability\n",
+               "  -h --help               Show this help\n"
+               "  -D --directory=NAME     Root directory for the container\n"
+               "  -b --boot               Boot up full system (i.e. invoke init)\n"
+               "  -u --user=USER          Run the command under specified user or uid\n"
+               "  -C --controllers=LIST   Put the container in specified comma-separated cgroup hierarchies\n"
+               "     --uuid=UUID          Set a specific machine UUID for the container\n"
+               "     --private-network    Disable network in container\n"
+               "     --read-only          Mount the root directory read-only\n"
+               "     --capability=CAP     In addition to the default, retain specified capability\n"
+               "     --link-journal=MODE  Link up guest journal, one of no, auto, guest, host\n"
+               "  -j                      Equivalent to --link-journal=host\n",
                program_invocation_short_name);
 
         return 0;
@@ -108,7 +119,8 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_PRIVATE_NETWORK = 0x100,
                 ARG_UUID,
                 ARG_READ_ONLY,
-                ARG_CAPABILITY
+                ARG_CAPABILITY,
+                ARG_LINK_JOURNAL
         };
 
         static const struct option options[] = {
@@ -121,6 +133,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "uuid",            required_argument, NULL, ARG_UUID            },
                 { "read-only",       no_argument,       NULL, ARG_READ_ONLY       },
                 { "capability",      required_argument, NULL, ARG_CAPABILITY      },
+                { "link-journal",    required_argument, NULL, ARG_LINK_JOURNAL    },
                 { NULL,              0,                 NULL, 0                   }
         };
 
@@ -129,7 +142,7 @@ static int parse_argv(int argc, char *argv[]) {
         assert(argc >= 0);
         assert(argv);
 
-        while ((c = getopt_long(argc, argv, "+hD:u:C:b", options, NULL)) >= 0) {
+        while ((c = getopt_long(argc, argv, "+hD:u:C:bj", options, NULL)) >= 0) {
 
                 switch (c) {
 
@@ -210,6 +223,26 @@ static int parse_argv(int argc, char *argv[]) {
                         break;
                 }
 
+                case 'j':
+                        arg_link_journal = LINK_GUEST;
+                        break;
+
+                case ARG_LINK_JOURNAL:
+                        if (streq(optarg, "auto"))
+                                arg_link_journal = LINK_AUTO;
+                        else if (streq(optarg, "no"))
+                                arg_link_journal = LINK_NO;
+                        else if (streq(optarg, "guest"))
+                                arg_link_journal = LINK_GUEST;
+                        else if (streq(optarg, "host"))
+                                arg_link_journal = LINK_HOST;
+                        else {
+                                log_error("Failed to parse link journal mode %s", optarg);
+                                return -EINVAL;
+                        }
+
+                        break;
+
                 case '?':
                         return -EINVAL;
 
@@ -596,6 +629,155 @@ static int setup_hostname(void) {
         return r;
 }
 
+static int setup_journal(const char *directory) {
+        sd_id128_t machine_id;
+        char *p = NULL, *b = NULL, *l, *q = NULL, *d = NULL;
+        int r;
+
+        if (arg_link_journal == LINK_NO)
+                return 0;
+
+        p = strappend(directory, "/etc/machine-id");
+        if (!p) {
+                log_error("Out of memory");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        r = read_one_line_file(p, &b);
+        if (r == -ENOENT && arg_link_journal == LINK_AUTO) {
+                r = 0;
+                goto finish;
+        } else if (r < 0) {
+                log_error("Failed to read machine ID: %s", strerror(-r));
+                return r;
+        }
+
+        l = strstrip(b);
+        if (isempty(l) && arg_link_journal == LINK_AUTO) {
+                r = 0;
+                goto finish;
+        }
+
+        /* Verify validaty */
+        r = sd_id128_from_string(l, &machine_id);
+        if (r < 0) {
+                log_error("Failed to parse machine ID: %s", strerror(-r));
+                goto finish;
+        }
+
+        free(p);
+        p = strappend("/var/log/journal/", l);
+        q = strjoin(directory, "/var/log/journal/", l, NULL);
+        if (!p || !q) {
+                log_error("Out of memory");
+                r = -ENOMEM;
+                goto finish;
+        }
+
+        if (path_is_mount_point(p, false) > 0 ||
+            path_is_mount_point(q, false) > 0) {
+                if (arg_link_journal != LINK_AUTO) {
+                        log_error("Journal already a mount point, refusing.");
+                        r = -EEXIST;
+                        goto finish;
+                }
+
+                r = 0;
+                goto finish;
+        }
+
+        r = readlink_and_make_absolute(p, &d);
+        if (r >= 0) {
+                if ((arg_link_journal == LINK_GUEST ||
+                     arg_link_journal == LINK_AUTO) &&
+                    path_equal(d, q)) {
+
+                        mkdir_p(q, 0755);
+
+                        r = 0;
+                        goto finish;
+                }
+
+                if (unlink(p) < 0) {
+                        log_error("Failed to remove symlink %s: %m", p);
+                        r = -errno;
+                        goto finish;
+                }
+        } else if (r == -EINVAL) {
+
+                if (arg_link_journal == LINK_GUEST &&
+                    rmdir(p) < 0) {
+
+                        if (errno == ENOTDIR)
+                                log_error("%s already exists and is neither symlink nor directory.", p);
+                        else {
+                                log_error("Failed to remove %s: %m", p);
+                                r = -errno;
+                        }
+
+                        goto finish;
+                }
+        } else if (r != -ENOENT) {
+                log_error("readlink(%s) failed: %m", p);
+                goto finish;
+        }
+
+        if (arg_link_journal == LINK_GUEST) {
+
+                if (symlink(q, p) < 0) {
+                        log_error("Failed to symlink %s to %s: %m", q, p);
+                        r = -errno;
+                        goto finish;
+                }
+
+                mkdir_p(q, 0755);
+
+                r = 0;
+                goto finish;
+        }
+
+        if (arg_link_journal == LINK_HOST) {
+                r = mkdir_p(p, 0755);
+                if (r < 0) {
+                        log_error("Failed to create %s: %m", p);
+                        goto finish;
+                }
+
+        } else if (access(p, F_OK) < 0) {
+                r = 0;
+                goto finish;
+        }
+
+        if (dir_is_empty(q) == 0) {
+                log_error("%s not empty.", q);
+                r = -ENOTEMPTY;
+                goto finish;
+        }
+
+        r = mkdir_p(q, 0755);
+        if (r < 0) {
+                log_error("Failed to create %s: %m", q);
+                goto finish;
+        }
+
+        if (mount(p, q, "bind", MS_BIND, NULL) < 0) {
+                log_error("Failed to bind mount journal from host into guest: %m");
+                r = -errno;
+                goto finish;
+        }
+
+        r = 0;
+
+finish:
+        free(p);
+        free(q);
+        free(d);
+        free(b);
+        return r;
+
+}
+
 static int drop_capabilities(void) {
         return capability_bounding_set_drop(~arg_retain, false);
 }
@@ -1023,6 +1205,9 @@ int main(int argc, char *argv[]) {
                 if (setup_resolv_conf(arg_directory) < 0)
                         goto child_fail;
 
+                if (setup_journal(arg_directory) < 0)
+                        goto child_fail;
+
                 if (chdir(arg_directory) < 0) {
                         log_error("chdir(%s) failed: %m", arg_directory);
                         goto child_fail;



More information about the systemd-commits mailing list