ecbff1
From 41e91ccdf9fa3097d7b90718cc83e743f4dc8d6b Mon Sep 17 00:00:00 2001
ecbff1
From: Jan Rybar <jrybar@redhat.com>
ecbff1
Date: Thu, 17 Aug 2017 18:01:42 +0200
ecbff1
Subject: [PATCH] nspawn: new option to start as PID2
ecbff1
ecbff1
Cherry-picked from: 7732f92
ecbff1
Resolves: #1417387
ecbff1
---
ecbff1
 Makefile.am                   |   2 +
de8967
 man/systemd-nspawn.xml        |  65 +++++++++--
de8967
 src/nspawn/nspawn-stub-pid1.c | 196 ++++++++++++++++++++++++++++++++++
de8967
 src/nspawn/nspawn-stub-pid1.h |  22 ++++
de8967
 src/nspawn/nspawn.c           |  56 ++++++++--
ecbff1
 5 files changed, 328 insertions(+), 13 deletions(-)
ecbff1
 create mode 100644 src/nspawn/nspawn-stub-pid1.c
ecbff1
 create mode 100644 src/nspawn/nspawn-stub-pid1.h
ecbff1
ecbff1
diff --git a/Makefile.am b/Makefile.am
ecbff1
index 7c58fd050..0e2f8d561 100644
ecbff1
--- a/Makefile.am
ecbff1
+++ b/Makefile.am
ecbff1
@@ -2658,6 +2658,8 @@ systemd_cgtop_LDADD = \
ecbff1
 # ------------------------------------------------------------------------------
ecbff1
 systemd_nspawn_SOURCES = \
ecbff1
 	src/nspawn/nspawn.c \
ecbff1
+	src/nspawn/nspawn-stub-pid1.c \
ecbff1
+	src/nspawn/nspawn-stub-pid1.h \
ecbff1
 	src/core/mount-setup.c \
ecbff1
 	src/core/mount-setup.h \
ecbff1
 	src/core/loopback-setup.c \
ecbff1
diff --git a/man/systemd-nspawn.xml b/man/systemd-nspawn.xml
ecbff1
index cbd44d4ab..d0eddaacc 100644
ecbff1
--- a/man/systemd-nspawn.xml
ecbff1
+++ b/man/systemd-nspawn.xml
ecbff1
@@ -241,16 +241,69 @@
ecbff1
         <option>--ephemeral</option>.</para></listitem>
ecbff1
       </varlistentry>
ecbff1
 
ecbff1
+      <varlistentry>
ecbff1
+      <term><option>-a</option></term>
ecbff1
+        <term><option>--as-pid2</option></term>
ecbff1
+
ecbff1
+        <listitem><para>Invoke the shell or specified program as process ID (PID) 2 instead of PID 1 (init). By
ecbff1
+        default, if neither this option nor <option>--boot</option> is used, the selected binary is run as process with
ecbff1
+        PID 1, a mode only suitable for programs that are aware of the special semantics that the process with PID 1
ecbff1
+        has on UNIX. For example, it needs to reap all processes reparented to it, and should implement
ecbff1
+        <command>sysvinit</command> compatible signal handling (specifically: it needs to reboot on SIGINT, reexecute
ecbff1
+        on SIGTERM, reload configuration on SIGHUP, and so on). With <option>--as-pid2</option> a minimal stub init
ecbff1
+        process is run as PID 1 and the selected binary is executed as PID 2 (and hence does not need to implement any
ecbff1
+        special semantics). The stub init process will reap processes as necessary and react appropriately to
ecbff1
+        signals. It is recommended to use this mode to invoke arbitrary commands in containers, unless they have been
ecbff1
+        modified to run correctly as PID 1. Or in other words: this switch should be used for pretty much all commands,
ecbff1
+        except when the command refers to an init or shell implementation, as these are generally capable of running
ecbff1
+        correctly as PID 1). This option may not be combined with <option>--boot</option> or
ecbff1
+        <option>--share-system</option>.</para>
ecbff1
+        </listitem>
ecbff1
+      </varlistentry>
ecbff1
+
ecbff1
       <varlistentry>
ecbff1
         <term><option>-b</option></term>
ecbff1
         <term><option>--boot</option></term>
ecbff1
 
ecbff1
-        <listitem><para>Automatically search for an init binary and
ecbff1
-        invoke it instead of a shell or a user supplied program. If
ecbff1
-        this option is used, arguments specified on the command line
ecbff1
-        are used as arguments for the init binary. This option may not
ecbff1
-        be combined with <option>--share-system</option>.
ecbff1
-        </para></listitem>
ecbff1
+        <listitem><para>Automatically search for an init binary and invoke it as PID 1, instead of a shell or a user
ecbff1
+        supplied program. If this option is used, arguments specified on the command line are used as arguments for the
ecbff1
+        init binary. This option may not be combined with <option>--as-pid2</option> or
ecbff1
+        <option>--share-system</option>.</para>
ecbff1
+
ecbff1
+        <para>The following table explains the different modes of invocation and relationship to
ecbff1
+        <option>--as-pid2</option> (see above):</para>
ecbff1
+
ecbff1
+        
ecbff1
+          <title>Invocation Mode</title>
ecbff1
+          <tgroup cols='2' align='left' colsep='1' rowsep='1'>
ecbff1
+            <colspec colname="switch" />
ecbff1
+            <colspec colname="explanation" />
ecbff1
+            
ecbff1
+              <row>
ecbff1
+                <entry>Switch</entry>
ecbff1
+                <entry>Explanation</entry>
ecbff1
+              </row>
ecbff1
+            
ecbff1
+            
ecbff1
+              <row>
ecbff1
+                <entry>Neither <option>--as-pid2</option> nor <option>--boot</option> specified</entry>
ecbff1
+                <entry>The passed parameters are interpreted as command line, which is executed as PID 1 in the container.</entry>
ecbff1
+              </row>
ecbff1
+
ecbff1
+              <row>
ecbff1
+                <entry><option>--as-pid2</option> specified</entry>
ecbff1
+                <entry>The passed parameters are interpreted as command line, which are executed as PID 2 in the container. A stub init process is run as PID 1.</entry>
ecbff1
+              </row>
ecbff1
+
ecbff1
+              <row>
ecbff1
+                <entry><option>--boot</option> specified</entry>
ecbff1
+                <entry>An init binary as automatically searched and run as PID 1 in the container. The passed parameters are used as invocation parameters for this process.</entry>
ecbff1
+              </row>
ecbff1
+
ecbff1
+            
ecbff1
+          </tgroup>
ecbff1
+        
ecbff1
+        </listitem>
ecbff1
       </varlistentry>
ecbff1
 
ecbff1
       <varlistentry>
ecbff1
diff --git a/src/nspawn/nspawn-stub-pid1.c b/src/nspawn/nspawn-stub-pid1.c
ecbff1
new file mode 100644
ecbff1
index 000000000..11c11560c
ecbff1
--- /dev/null
ecbff1
+++ b/src/nspawn/nspawn-stub-pid1.c
ecbff1
@@ -0,0 +1,196 @@
ecbff1
+/***
ecbff1
+  This file is part of systemd.
ecbff1
+
ecbff1
+  Copyright 2016 Lennart Poettering
ecbff1
+
ecbff1
+  systemd is free software; you can redistribute it and/or modify it
ecbff1
+  under the terms of the GNU Lesser General Public License as published by
ecbff1
+  the Free Software Foundation; either version 2.1 of the License, or
ecbff1
+  (at your option) any later version.
ecbff1
+
ecbff1
+  systemd is distributed in the hope that it will be useful, but
ecbff1
+  WITHOUT ANY WARRANTY; without even the implied warranty of
ecbff1
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ecbff1
+  Lesser General Public License for more details.
ecbff1
+
ecbff1
+  You should have received a copy of the GNU Lesser General Public License
ecbff1
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
ecbff1
+***/
ecbff1
+
ecbff1
+#include <sys/reboot.h>
ecbff1
+#include <sys/unistd.h>
ecbff1
+#include <sys/wait.h>
ecbff1
+#include <sys/prctl.h>
ecbff1
+
ecbff1
+#include "log.h"
ecbff1
+#include "nspawn-stub-pid1.h"
ecbff1
+#include "util.h"
ecbff1
+#include "time-util.h"
ecbff1
+#include "def.h"
ecbff1
+
ecbff1
+static int reset_environ(const char *new_environment, size_t length) {
ecbff1
+        unsigned long start, end;
ecbff1
+
ecbff1
+        start = (unsigned long) new_environment;
ecbff1
+        end = start + length;
ecbff1
+
ecbff1
+        if (prctl(PR_SET_MM, PR_SET_MM_ENV_START, start, 0, 0) < 0)
ecbff1
+                return -errno;
ecbff1
+
ecbff1
+        if (prctl(PR_SET_MM, PR_SET_MM_ENV_END, end, 0, 0) < 0)
ecbff1
+                return -errno;
ecbff1
+
ecbff1
+        return 0;
ecbff1
+}
ecbff1
+
ecbff1
+int stub_pid1(sd_id128_t uuid) {
ecbff1
+        enum {
ecbff1
+                STATE_RUNNING,
ecbff1
+                STATE_REBOOT,
ecbff1
+                STATE_POWEROFF,
ecbff1
+        } state = STATE_RUNNING;
ecbff1
+
ecbff1
+        sigset_t fullmask, oldmask, waitmask;
ecbff1
+        usec_t quit_usec = USEC_INFINITY;
ecbff1
+        pid_t pid;
ecbff1
+        int r;
ecbff1
+
ecbff1
+        /* The new environment we set up, on the stack. */
ecbff1
+        char new_environment[] =
ecbff1
+                "container=systemd-nspawn\0"
ecbff1
+                "container_uuid=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
ecbff1
+
ecbff1
+        /* Implements a stub PID 1, that reaps all processes and processes a couple of standard signals. This is useful
ecbff1
+         * for allowing arbitrary processes run in a container, and still have all zombies reaped. */
ecbff1
+
ecbff1
+        assert_se(sigfillset(&fullmask) >= 0);
ecbff1
+        assert_se(sigprocmask(SIG_BLOCK, &fullmask, &oldmask) >= 0);
ecbff1
+
ecbff1
+        pid = fork();
ecbff1
+        if (pid < 0)
ecbff1
+                return log_error_errno(errno, "Failed to fork child pid: %m");
ecbff1
+
ecbff1
+        if (pid == 0) {
ecbff1
+                /* Return in the child */
ecbff1
+                assert_se(sigprocmask(SIG_SETMASK, &oldmask, NULL) >= 0);
ecbff1
+                setsid();
ecbff1
+                return 0;
ecbff1
+        }
ecbff1
+
ecbff1
+        reset_all_signal_handlers();
ecbff1
+
ecbff1
+        log_close();
ecbff1
+        close_all_fds(NULL, 0);
ecbff1
+        log_open();
ecbff1
+
ecbff1
+        /* Flush out /proc/self/environ, so that we don't leak the environment from the host into the container. Also,
ecbff1
+         * set $container= and $container_uuid= so that clients in the container that query it from /proc/1/environ
ecbff1
+         * find them set. */
ecbff1
+        sd_id128_to_string(uuid, new_environment + sizeof(new_environment) - SD_ID128_STRING_MAX);
ecbff1
+        reset_environ(new_environment, sizeof(new_environment));
ecbff1
+
ecbff1
+        rename_process("STUBINIT");
ecbff1
+
ecbff1
+        assert_se(sigemptyset(&waitmask) >= 0);
ecbff1
+
ecbff1
+        sigset_add_many(&waitmask,
ecbff1
+                        SIGCHLD,          /* posix: process died */
ecbff1
+                        SIGINT,           /* sysv: ctrl-alt-del */
ecbff1
+                        SIGRTMIN+3,       /* systemd: halt */
ecbff1
+                        SIGRTMIN+4,       /* systemd: poweroff */
ecbff1
+                        SIGRTMIN+5,       /* systemd: reboot */
ecbff1
+                        SIGRTMIN+6,       /* systemd: kexec */
ecbff1
+                        SIGRTMIN+13,      /* systemd: halt */
ecbff1
+                        SIGRTMIN+14,      /* systemd: poweroff */
ecbff1
+                        SIGRTMIN+15,      /* systemd: reboot */
ecbff1
+                        SIGRTMIN+16,      /* systemd: kexec */
ecbff1
+                        -1);
ecbff1
+
ecbff1
+        /* Note that we ignore SIGTERM (sysv's reexec), SIGHUP (reload), and all other signals here, since we don't
ecbff1
+         * support reexec/reloading in this stub process. */
ecbff1
+
ecbff1
+        for (;;) {
ecbff1
+                siginfo_t si;
ecbff1
+                usec_t current_usec;
ecbff1
+
ecbff1
+                si.si_pid = 0;
ecbff1
+                r = waitid(P_ALL, 0, &si, WEXITED|WNOHANG);
ecbff1
+                if (r < 0) {
ecbff1
+                        r = log_error_errno(errno, "Failed to reap children: %m");
ecbff1
+                        goto finish;
ecbff1
+                }
ecbff1
+
ecbff1
+                current_usec = now(CLOCK_MONOTONIC);
ecbff1
+
ecbff1
+                if (si.si_pid == pid || current_usec >= quit_usec) {
ecbff1
+
ecbff1
+                        /* The child we started ourselves died or we reached a timeout. */
ecbff1
+
ecbff1
+                        if (state == STATE_REBOOT) { /* dispatch a queued reboot */
ecbff1
+                                (void) reboot(RB_AUTOBOOT);
ecbff1
+                                r = log_error_errno(errno, "Failed to reboot: %m");
ecbff1
+                                goto finish;
ecbff1
+
ecbff1
+                        } else if (state == STATE_POWEROFF)
ecbff1
+                                (void) reboot(RB_POWER_OFF); /* if this fails, fall back to normal exit. */
ecbff1
+
ecbff1
+                        if (si.si_pid == pid && si.si_code == CLD_EXITED)
ecbff1
+                                r = si.si_status; /* pass on exit code */
ecbff1
+                        else
ecbff1
+                                r = 255; /* signal, coredump, timeout, … */
ecbff1
+
ecbff1
+                        goto finish;
ecbff1
+                }
ecbff1
+                if (si.si_pid != 0)
ecbff1
+                        /* We reaped something. Retry until there's nothing more to reap. */
ecbff1
+                        continue;
ecbff1
+
ecbff1
+                if (quit_usec == USEC_INFINITY)
ecbff1
+                        r = sigwaitinfo(&waitmask, &si);
ecbff1
+                else {
ecbff1
+                        struct timespec ts;
ecbff1
+                        r = sigtimedwait(&waitmask, &si, timespec_store(&ts, quit_usec - current_usec));
ecbff1
+                }
ecbff1
+                if (r < 0) {
ecbff1
+                        if (errno == EINTR) /* strace -p attach can result in EINTR, let's handle this nicely. */
ecbff1
+                                continue;
ecbff1
+                        if (errno == EAGAIN) /* timeout reached */
ecbff1
+                                continue;
ecbff1
+
ecbff1
+                        r = log_error_errno(errno, "Failed to wait for signal: %m");
ecbff1
+                        goto finish;
ecbff1
+                }
ecbff1
+
ecbff1
+                if (si.si_signo == SIGCHLD)
ecbff1
+                        continue; /* Let's reap this */
ecbff1
+
ecbff1
+                if (state != STATE_RUNNING)
ecbff1
+                        continue;
ecbff1
+
ecbff1
+                /* Would love to use a switch() statement here, but SIGRTMIN is actually a function call, not a
ecbff1
+                 * constant… */
ecbff1
+
ecbff1
+                if (si.si_signo == SIGRTMIN+3 ||
ecbff1
+                    si.si_signo == SIGRTMIN+4 ||
ecbff1
+                    si.si_signo == SIGRTMIN+13 ||
ecbff1
+                    si.si_signo == SIGRTMIN+14)
ecbff1
+
ecbff1
+                        state = STATE_POWEROFF;
ecbff1
+
ecbff1
+                else if (si.si_signo == SIGINT ||
ecbff1
+                         si.si_signo == SIGRTMIN+5 ||
ecbff1
+                         si.si_signo == SIGRTMIN+6 ||
ecbff1
+                         si.si_signo == SIGRTMIN+15 ||
ecbff1
+                         si.si_signo == SIGRTMIN+16)
ecbff1
+
ecbff1
+                        state = STATE_REBOOT;
ecbff1
+                else
ecbff1
+                        assert_not_reached("Got unexpected signal");
ecbff1
+
ecbff1
+                /* (void) kill_and_sigcont(pid, SIGTERM); */
ecbff1
+                quit_usec = now(CLOCK_MONOTONIC) + DEFAULT_TIMEOUT_USEC;
ecbff1
+        }
ecbff1
+
ecbff1
+finish:
ecbff1
+        _exit(r < 0 ? EXIT_FAILURE : r);
ecbff1
+}
ecbff1
diff --git a/src/nspawn/nspawn-stub-pid1.h b/src/nspawn/nspawn-stub-pid1.h
ecbff1
new file mode 100644
ecbff1
index 000000000..be0f1af4c
ecbff1
--- /dev/null
ecbff1
+++ b/src/nspawn/nspawn-stub-pid1.h
ecbff1
@@ -0,0 +1,22 @@
ecbff1
+#pragma once
ecbff1
+
ecbff1
+/***
ecbff1
+  This file is part of systemd.
ecbff1
+
ecbff1
+  Copyright 2016 Lennart Poettering
ecbff1
+
ecbff1
+  systemd is free software; you can redistribute it and/or modify it
ecbff1
+  under the terms of the GNU Lesser General Public License as published by
ecbff1
+  the Free Software Foundation; either version 2.1 of the License, or
ecbff1
+  (at your option) any later version.
ecbff1
+
ecbff1
+  systemd is distributed in the hope that it will be useful, but
ecbff1
+  WITHOUT ANY WARRANTY; without even the implied warranty of
ecbff1
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ecbff1
+  Lesser General Public License for more details.
ecbff1
+
ecbff1
+  You should have received a copy of the GNU Lesser General Public License
ecbff1
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
ecbff1
+***/
ecbff1
+
ecbff1
+int stub_pid1(sd_id128_t uuid);
ecbff1
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
ecbff1
index d0003d379..ea365b3f9 100644
ecbff1
--- a/src/nspawn/nspawn.c
ecbff1
+++ b/src/nspawn/nspawn.c
ecbff1
@@ -99,6 +99,7 @@
ecbff1
 #include "in-addr-util.h"
ecbff1
 #include "fw-util.h"
ecbff1
 #include "local-addresses.h"
ecbff1
+#include "nspawn-stub-pid1.h"
ecbff1
 
ecbff1
 #ifdef HAVE_SECCOMP
ecbff1
 #include "seccomp-util.h"
ecbff1
@@ -129,6 +130,14 @@ typedef enum Volatile {
ecbff1
         VOLATILE_STATE,
ecbff1
 } Volatile;
ecbff1
 
ecbff1
+typedef enum StartMode {
ecbff1
+        START_PID1, /* Run parameters as command line as process 1 */
ecbff1
+        START_PID2, /* Use stub init process as PID 1, run parameters as command line as process 2 */
ecbff1
+        START_BOOT, /* Search for init system, pass arguments as parameters */
ecbff1
+        _START_MODE_MAX,
ecbff1
+        _START_MODE_INVALID = -1
ecbff1
+} StartMode;
ecbff1
+
ecbff1
 static char *arg_directory = NULL;
ecbff1
 static char *arg_template = NULL;
ecbff1
 static char *arg_user = NULL;
ecbff1
@@ -139,7 +148,7 @@ static const char *arg_selinux_apifs_context = NULL;
ecbff1
 static const char *arg_slice = NULL;
ecbff1
 static bool arg_private_network = false;
ecbff1
 static bool arg_read_only = false;
ecbff1
-static bool arg_boot = false;
ecbff1
+static StartMode arg_start_mode = START_PID1;
ecbff1
 static bool arg_ephemeral = false;
ecbff1
 static LinkJournal arg_link_journal = LINK_AUTO;
ecbff1
 static bool arg_link_journal_try = false;
ecbff1
@@ -200,6 +209,7 @@ static void help(void) {
ecbff1
                "  -x --ephemeral            Run container with snapshot of root directory, and\n"
ecbff1
                "                            remove it after exit\n"
ecbff1
                "  -i --image=PATH           File system device or disk image for the container\n"
ecbff1
+               "  -a --as-pid2              Maintain a stub init as PID1, invoke binary as PID2\n"
ecbff1
                "  -b --boot                 Boot up full system (i.e. invoke init)\n"
ecbff1
                "  -u --user=USER            Run the command under specified user or uid\n"
ecbff1
                "  -M --machine=NAME         Set the machine name for the container\n"
ecbff1
@@ -304,6 +314,7 @@ static int parse_argv(int argc, char *argv[]) {
ecbff1
                 { "ephemeral",             no_argument,       NULL, 'x'                   },
ecbff1
                 { "user",                  required_argument, NULL, 'u'                   },
ecbff1
                 { "private-network",       no_argument,       NULL, ARG_PRIVATE_NETWORK   },
ecbff1
+                { "as-pid2",               no_argument,       NULL, 'a'                   },
ecbff1
                 { "boot",                  no_argument,       NULL, 'b'                   },
ecbff1
                 { "uuid",                  required_argument, NULL, ARG_UUID              },
ecbff1
                 { "read-only",             no_argument,       NULL, ARG_READ_ONLY         },
ecbff1
@@ -340,7 +351,7 @@ static int parse_argv(int argc, char *argv[]) {
ecbff1
         assert(argc >= 0);
ecbff1
         assert(argv);
ecbff1
 
ecbff1
-        while ((c = getopt_long(argc, argv, "+hD:u:bL:M:jS:Z:qi:xp:n", options, NULL)) >= 0)
ecbff1
+        while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:n", options, NULL)) >= 0)
ecbff1
 
ecbff1
                 switch (c) {
ecbff1
 
ecbff1
@@ -421,7 +432,21 @@ static int parse_argv(int argc, char *argv[]) {
ecbff1
                         break;
ecbff1
 
ecbff1
                 case 'b':
ecbff1
-                        arg_boot = true;
ecbff1
+                        if (arg_start_mode == START_PID2) {
ecbff1
+                                log_error("--boot and --as-pid2 may not be combined.");
ecbff1
+                                return -EINVAL;
ecbff1
+                        }
ecbff1
+
ecbff1
+                        arg_start_mode = START_BOOT;
ecbff1
+                        break;
ecbff1
+
ecbff1
+                case 'a':
ecbff1
+                        if (arg_start_mode == START_BOOT) {
ecbff1
+                                log_error("--boot and --as-pid2 may not be combined.");
ecbff1
+                                return -EINVAL;
ecbff1
+                        }
ecbff1
+
ecbff1
+                        arg_start_mode = START_PID2;
ecbff1
                         break;
ecbff1
 
ecbff1
                 case ARG_UUID:
ecbff1
@@ -741,7 +766,7 @@ static int parse_argv(int argc, char *argv[]) {
ecbff1
         if (arg_share_system)
ecbff1
                 arg_register = false;
ecbff1
 
ecbff1
-        if (arg_boot && arg_share_system) {
ecbff1
+        if (arg_start_mode != START_PID1 && arg_share_system) {
ecbff1
                 log_error("--boot and --share-system may not be combined.");
ecbff1
                 return -EINVAL;
ecbff1
         }
ecbff1
@@ -3586,6 +3611,10 @@ int main(int argc, char *argv[]) {
ecbff1
         log_parse_environment();
ecbff1
         log_open();
ecbff1
 
ecbff1
+        /* Make sure rename_process() in the stub init process can work */
ecbff1
+        saved_argv = argv;
ecbff1
+        saved_argc = argc;
ecbff1
+
ecbff1
         r = parse_argv(argc, argv);
ecbff1
         if (r <= 0)
ecbff1
                 goto finish;
ecbff1
@@ -3694,7 +3723,7 @@ int main(int argc, char *argv[]) {
ecbff1
                         }
ecbff1
                 }
ecbff1
 
ecbff1
-                if (arg_boot) {
ecbff1
+                if (arg_start_mode == START_BOOT) {
ecbff1
                         if (path_is_os_tree(arg_directory) <= 0) {
ecbff1
                                 log_error("Directory %s doesn't look like an OS root directory (os-release file is missing). Refusing.", arg_directory);
ecbff1
                                 r = -EINVAL;
ecbff1
@@ -4109,7 +4138,19 @@ int main(int argc, char *argv[]) {
ecbff1
                         if (!barrier_place_and_sync(&barrier))
ecbff1
                                 _exit(EXIT_FAILURE);
ecbff1
 
ecbff1
-                        if (arg_boot) {
ecbff1
+                        if (arg_start_mode == START_PID2) {
ecbff1
+                                r = stub_pid1(arg_uuid);
ecbff1
+                                if (r < 0)
ecbff1
+                                {
ecbff1
+                                        log_error_errno(r, "Failed to start as PID2: %m");
ecbff1
+                                        _exit(EXIT_FAILURE);
ecbff1
+                                }
ecbff1
+                        }
ecbff1
+
ecbff1
+                        log_close();
ecbff1
+                        (void) fdset_close_others(fds);
ecbff1
+
ecbff1
+                        if (arg_start_mode == START_BOOT) {
ecbff1
                                 char **a;
ecbff1
                                 size_t l;
ecbff1
 
ecbff1
@@ -4135,6 +4176,7 @@ int main(int argc, char *argv[]) {
ecbff1
                                 execle("/bin/sh", "-sh", NULL, env_use);
ecbff1
                         }
ecbff1
 
ecbff1
+                        log_open();
ecbff1
                         log_error_errno(errno, "execv() failed: %m");
ecbff1
                         _exit(EXIT_FAILURE);
ecbff1
                 }
ecbff1
@@ -4210,7 +4252,7 @@ int main(int argc, char *argv[]) {
ecbff1
                                         goto finish;
ecbff1
                                 }
ecbff1
 
ecbff1
-                                if (arg_boot) {
ecbff1
+                                if (arg_start_mode == START_BOOT) {
ecbff1
                                         /* Try to kill the init system on SIGINT or SIGTERM */
ecbff1
                                         sd_event_add_signal(event, NULL, SIGINT, on_orderly_shutdown, UINT32_TO_PTR(pid));
ecbff1
                                         sd_event_add_signal(event, NULL, SIGTERM, on_orderly_shutdown, UINT32_TO_PTR(pid));