daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
ff6046
From 37c1da056b63323514d71d2832a01ea916f004cc Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Fri, 5 Oct 2018 22:56:40 +0200
ff6046
Subject: [PATCH] nspawn: rework how we allocate/kill scopes
ff6046
ff6046
Fixes: #6347
ff6046
(cherry picked from commit 1d78fea2d6230e0aafa2603abc8f1f51966ef134)
ff6046
ff6046
Resolves: #1697893
ff6046
---
ff6046
 src/nspawn/nspawn-register.c | 64 +++++++++++++++++++++++++++++++++++-
ff6046
 src/nspawn/nspawn-register.h |  1 +
ff6046
 src/nspawn/nspawn.c          |  8 +++--
ff6046
 3 files changed, 70 insertions(+), 3 deletions(-)
ff6046
ff6046
diff --git a/src/nspawn/nspawn-register.c b/src/nspawn/nspawn-register.c
ff6046
index 0d45cce66e..a7cdfc1c7d 100644
ff6046
--- a/src/nspawn/nspawn-register.c
ff6046
+++ b/src/nspawn/nspawn-register.c
ff6046
@@ -274,10 +274,12 @@ int allocate_scope(
ff6046
 
ff6046
         description = strjoina("Container ", machine_name);
ff6046
 
ff6046
-        r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)",
ff6046
+        r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)(sv)",
ff6046
                                   "PIDs", "au", 1, pid,
ff6046
                                   "Description", "s", description,
ff6046
                                   "Delegate", "b", 1,
ff6046
+                                  "CollectMode", "s", "inactive-or-failed",
ff6046
+                                  "AddRef", "b", 1,
ff6046
                                   "Slice", "s", isempty(slice) ? SPECIAL_MACHINE_SLICE : slice);
ff6046
         if (r < 0)
ff6046
                 return bus_log_create_error(r);
ff6046
@@ -324,3 +326,63 @@ int allocate_scope(
ff6046
 
ff6046
         return 0;
ff6046
 }
ff6046
+
ff6046
+int terminate_scope(
ff6046
+                sd_bus *bus,
ff6046
+                const char *machine_name) {
ff6046
+
ff6046
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
ff6046
+        _cleanup_free_ char *scope = NULL;
ff6046
+        int r;
ff6046
+
ff6046
+        r = unit_name_mangle_with_suffix(machine_name, 0, ".scope", &scope);
ff6046
+        if (r < 0)
ff6046
+                return log_error_errno(r, "Failed to mangle scope name: %m");
ff6046
+
ff6046
+        r = sd_bus_call_method(
ff6046
+                        bus,
ff6046
+                        "org.freedesktop.systemd1",
ff6046
+                        "/org/freedesktop/systemd1",
ff6046
+                        "org.freedesktop.systemd1.Manager",
ff6046
+                        "AbandonScope",
ff6046
+                        &error,
ff6046
+                        NULL,
ff6046
+                        "s",
ff6046
+                        scope);
ff6046
+        if (r < 0) {
ff6046
+                log_debug_errno(r, "Failed to abandon scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
ff6046
+                sd_bus_error_free(&error);
ff6046
+        }
ff6046
+
ff6046
+        r = sd_bus_call_method(
ff6046
+                        bus,
ff6046
+                        "org.freedesktop.systemd1",
ff6046
+                        "/org/freedesktop/systemd1",
ff6046
+                        "org.freedesktop.systemd1.Manager",
ff6046
+                        "KillUnit",
ff6046
+                        &error,
ff6046
+                        NULL,
ff6046
+                        "ssi",
ff6046
+                        scope,
ff6046
+                        "all",
ff6046
+                        (int32_t) SIGKILL);
ff6046
+        if (r < 0) {
ff6046
+                log_debug_errno(r, "Failed to SIGKILL scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
ff6046
+                sd_bus_error_free(&error);
ff6046
+        }
ff6046
+
ff6046
+        r = sd_bus_call_method(
ff6046
+                        bus,
ff6046
+                        "org.freedesktop.systemd1",
ff6046
+                        "/org/freedesktop/systemd1",
ff6046
+                        "org.freedesktop.systemd1.Manager",
ff6046
+                        "UnrefUnit",
ff6046
+                        &error,
ff6046
+                        NULL,
ff6046
+                        "s",
ff6046
+                        scope);
ff6046
+        if (r < 0)
ff6046
+                log_debug_errno(r, "Failed to drop reference to scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
ff6046
+
ff6046
+        return 0;
ff6046
+}
ff6046
diff --git a/src/nspawn/nspawn-register.h b/src/nspawn/nspawn-register.h
ff6046
index ddd8b053a3..05f5776f23 100644
ff6046
--- a/src/nspawn/nspawn-register.h
ff6046
+++ b/src/nspawn/nspawn-register.h
ff6046
@@ -11,3 +11,4 @@ int register_machine(sd_bus *bus, const char *machine_name, pid_t pid, const cha
ff6046
 int terminate_machine(sd_bus *bus, const char *machine_name);
ff6046
 
ff6046
 int allocate_scope(sd_bus *bus, const char *machine_name, pid_t pid, const char *slice, CustomMount *mounts, unsigned n_mounts, int kill_signal, char **properties);
ff6046
+int terminate_scope(sd_bus *bus, const char *machine_name);
ff6046
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
ff6046
index c4943f6eb7..b40411dcd0 100644
ff6046
--- a/src/nspawn/nspawn.c
ff6046
+++ b/src/nspawn/nspawn.c
ff6046
@@ -4065,8 +4065,12 @@ static int run(int master,
ff6046
                 putc('\n', stdout);
ff6046
 
ff6046
         /* Kill if it is not dead yet anyway */
ff6046
-        if (arg_register && !arg_keep_unit && bus)
ff6046
-                terminate_machine(bus, arg_machine);
ff6046
+        if (bus) {
ff6046
+                if (arg_register)
ff6046
+                        terminate_machine(bus, arg_machine);
ff6046
+                else if (!arg_keep_unit)
ff6046
+                        terminate_scope(bus, arg_machine);
ff6046
+        }
ff6046
 
ff6046
         /* Normally redundant, but better safe than sorry */
ff6046
         (void) kill(*pid, SIGKILL);