ff6046
From 74640adc3e79064ab34f7ced59e231603c58f07c Mon Sep 17 00:00:00 2001
ff6046
From: Lennart Poettering <lennart@poettering.net>
ff6046
Date: Fri, 5 Oct 2018 22:54:57 +0200
ff6046
Subject: [PATCH] nspawn: simplify machine terminate bus call
ff6046
ff6046
We have the machine name anyway, let's use TerminateMachine() on
ff6046
machined's Manager object directly with it. That way it's a single
ff6046
method call only, instead of two, to terminate the machine.
ff6046
ff6046
(cherry picked from commit 11d81e506ed68c6c5cebe319dc57a9a2fc4319c5)
ff6046
ff6046
Resolves: #1697893
ff6046
---
ff6046
 src/nspawn/nspawn-register.c | 34 +++++++---------------------------
ff6046
 src/nspawn/nspawn-register.h |  2 +-
ff6046
 src/nspawn/nspawn.c          |  2 +-
ff6046
 3 files changed, 9 insertions(+), 29 deletions(-)
ff6046
ff6046
diff --git a/src/nspawn/nspawn-register.c b/src/nspawn/nspawn-register.c
ff6046
index 85f3cf1c01..e459cb63ec 100644
ff6046
--- a/src/nspawn/nspawn-register.c
ff6046
+++ b/src/nspawn/nspawn-register.c
ff6046
@@ -201,10 +201,11 @@ int register_machine(
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-int terminate_machine(sd_bus *bus, pid_t pid) {
ff6046
+int terminate_machine(
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_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
ff6046
-        const char *path;
ff6046
         int r;
ff6046
 
ff6046
         assert(bus);
ff6046
@@ -214,32 +215,11 @@ int terminate_machine(sd_bus *bus, pid_t pid) {
ff6046
                         "org.freedesktop.machine1",
ff6046
                         "/org/freedesktop/machine1",
ff6046
                         "org.freedesktop.machine1.Manager",
ff6046
-                        "GetMachineByPID",
ff6046
-                        &error,
ff6046
-                        &reply,
ff6046
-                        "u",
ff6046
-                        (uint32_t) pid);
ff6046
-        if (r < 0) {
ff6046
-                /* Note that the machine might already have been
ff6046
-                 * cleaned up automatically, hence don't consider it a
ff6046
-                 * failure if we cannot get the machine object. */
ff6046
-                log_debug("Failed to get machine: %s", bus_error_message(&error, r));
ff6046
-                return 0;
ff6046
-        }
ff6046
-
ff6046
-        r = sd_bus_message_read(reply, "o", &path);
ff6046
-        if (r < 0)
ff6046
-                return bus_log_parse_error(r);
ff6046
-
ff6046
-        r = sd_bus_call_method(
ff6046
-                        bus,
ff6046
-                        "org.freedesktop.machine1",
ff6046
-                        path,
ff6046
-                        "org.freedesktop.machine1.Machine",
ff6046
-                        "Terminate",
ff6046
+                        "TerminateMachine",
ff6046
                         &error,
ff6046
                         NULL,
ff6046
-                        NULL);
ff6046
+                        "s",
ff6046
+                        machine_name);
ff6046
         if (r < 0)
ff6046
                 log_debug("Failed to terminate machine: %s", bus_error_message(&error, r));
ff6046
 
ff6046
diff --git a/src/nspawn/nspawn-register.h b/src/nspawn/nspawn-register.h
ff6046
index 30807b9687..ddd8b053a3 100644
ff6046
--- a/src/nspawn/nspawn-register.h
ff6046
+++ b/src/nspawn/nspawn-register.h
ff6046
@@ -8,6 +8,6 @@
ff6046
 #include "nspawn-mount.h"
ff6046
 
ff6046
 int register_machine(sd_bus *bus, const char *machine_name, pid_t pid, const char *directory, sd_id128_t uuid, int local_ifindex, const char *slice, CustomMount *mounts, unsigned n_mounts, int kill_signal, char **properties, bool keep_unit, const char *service);
ff6046
-int terminate_machine(sd_bus *bus, pid_t pid);
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
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
ff6046
index 8aec893a69..c4943f6eb7 100644
ff6046
--- a/src/nspawn/nspawn.c
ff6046
+++ b/src/nspawn/nspawn.c
ff6046
@@ -4066,7 +4066,7 @@ static int run(int master,
ff6046
 
ff6046
         /* Kill if it is not dead yet anyway */
ff6046
         if (arg_register && !arg_keep_unit && bus)
ff6046
-                terminate_machine(bus, *pid);
ff6046
+                terminate_machine(bus, arg_machine);
ff6046
 
ff6046
         /* Normally redundant, but better safe than sorry */
ff6046
         (void) kill(*pid, SIGKILL);