Blob Blame History Raw
From 5e68640c5b25b3b682ca8ce92a4a573e8dfba6ae Mon Sep 17 00:00:00 2001
From: Franck Bui <fbui@suse.com>
Date: Thu, 24 Nov 2016 18:52:04 +0100
Subject: [PATCH] core: make sure initrd-switch-root command survives PID1's
 killing spree (#4730)

This is a different way to implement the fix proposed by commit
a4021390fef27f4136497328f suggested by Lennart Poettering.

In this patch we instruct PID1 to not kill "systemctl switch-root" command
started by initrd-switch-root service using the "argv[0][0]='@'" trick.

See: https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/ for
more details.

We had to backup argv[0] because argv is modified by dispatch_verb().

(cherry picked from commit acc28e2e3037d689d6481e4664925cf31d4d087b)

Related: #1754053
---
 src/systemctl/systemctl.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index f31137787a..f84b92ccbe 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -128,6 +128,7 @@ static const char *arg_kill_who = NULL;
 static int arg_signal = SIGTERM;
 static const char *arg_root = NULL;
 static usec_t arg_when = 0;
+static char *argv_cmdline = NULL;
 static enum action {
         _ACTION_INVALID,
         ACTION_SYSTEMCTL,
@@ -5060,6 +5061,13 @@ static int switch_root(sd_bus *bus, char **args) {
                         init = NULL;
         }
 
+        /* Instruct PID1 to exclude us from its killing spree applied during
+         * the transition from the initrd to the main system otherwise we would
+         * exit with a failure status even though the switch to the new root
+         * has succeed. */
+        if (in_initrd())
+                argv_cmdline[0] = '@';
+
         log_debug("Switching root - root: %s; init: %s", root, strna(init));
 
         r = sd_bus_call_method(
@@ -7626,6 +7634,8 @@ int main(int argc, char*argv[]) {
         _cleanup_bus_close_unref_ sd_bus *bus = NULL;
         int r;
 
+        argv_cmdline = argv[0];
+
         setlocale(LC_ALL, "");
         log_parse_environment();
         log_open();