daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff2b41
From 5e68640c5b25b3b682ca8ce92a4a573e8dfba6ae Mon Sep 17 00:00:00 2001
ff2b41
From: Franck Bui <fbui@suse.com>
ff2b41
Date: Thu, 24 Nov 2016 18:52:04 +0100
ff2b41
Subject: [PATCH] core: make sure initrd-switch-root command survives PID1's
ff2b41
 killing spree (#4730)
ff2b41
ff2b41
This is a different way to implement the fix proposed by commit
ff2b41
a4021390fef27f4136497328f suggested by Lennart Poettering.
ff2b41
ff2b41
In this patch we instruct PID1 to not kill "systemctl switch-root" command
ff2b41
started by initrd-switch-root service using the "argv[0][0]='@'" trick.
ff2b41
ff2b41
See: https://www.freedesktop.org/wiki/Software/systemd/RootStorageDaemons/ for
ff2b41
more details.
ff2b41
ff2b41
We had to backup argv[0] because argv is modified by dispatch_verb().
ff2b41
ff2b41
(cherry picked from commit acc28e2e3037d689d6481e4664925cf31d4d087b)
ff2b41
ff2b41
Related: #1754053
ff2b41
---
ff2b41
 src/systemctl/systemctl.c | 10 ++++++++++
ff2b41
 1 file changed, 10 insertions(+)
ff2b41
ff2b41
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
ff2b41
index f31137787a..f84b92ccbe 100644
ff2b41
--- a/src/systemctl/systemctl.c
ff2b41
+++ b/src/systemctl/systemctl.c
ff2b41
@@ -128,6 +128,7 @@ static const char *arg_kill_who = NULL;
ff2b41
 static int arg_signal = SIGTERM;
ff2b41
 static const char *arg_root = NULL;
ff2b41
 static usec_t arg_when = 0;
ff2b41
+static char *argv_cmdline = NULL;
ff2b41
 static enum action {
ff2b41
         _ACTION_INVALID,
ff2b41
         ACTION_SYSTEMCTL,
ff2b41
@@ -5060,6 +5061,13 @@ static int switch_root(sd_bus *bus, char **args) {
ff2b41
                         init = NULL;
ff2b41
         }
ff2b41
 
ff2b41
+        /* Instruct PID1 to exclude us from its killing spree applied during
ff2b41
+         * the transition from the initrd to the main system otherwise we would
ff2b41
+         * exit with a failure status even though the switch to the new root
ff2b41
+         * has succeed. */
ff2b41
+        if (in_initrd())
ff2b41
+                argv_cmdline[0] = '@';
ff2b41
+
ff2b41
         log_debug("Switching root - root: %s; init: %s", root, strna(init));
ff2b41
 
ff2b41
         r = sd_bus_call_method(
ff2b41
@@ -7626,6 +7634,8 @@ int main(int argc, char*argv[]) {
ff2b41
         _cleanup_bus_close_unref_ sd_bus *bus = NULL;
ff2b41
         int r;
ff2b41
 
ff2b41
+        argv_cmdline = argv[0];
ff2b41
+
ff2b41
         setlocale(LC_ALL, "");
ff2b41
         log_parse_environment();
ff2b41
         log_open();