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