ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
65878a
From 5a69011a7d265ec42562fdfdb99d886570a49d7a Mon Sep 17 00:00:00 2001
65878a
From: Lennart Poettering <lennart@poettering.net>
65878a
Date: Wed, 6 Nov 2013 02:05:06 +0100
65878a
Subject: [PATCH] nspawn: explicitly terminate machines when we exit nspawn
65878a
65878a
https://bugs.freedesktop.org/show_bug.cgi?id=68370
65878a
https://bugzilla.redhat.com/show_bug.cgi?id=988883
65878a
65878a
Conflicts:
65878a
	src/nspawn/nspawn.c
65878a
65878a
Conflicts:
65878a
	src/nspawn/nspawn.c
65878a
---
65878a
 src/nspawn/nspawn.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++----
65878a
 1 file changed, 64 insertions(+), 4 deletions(-)
65878a
65878a
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
65878a
index 4bc49a3..f326f99 100644
65878a
--- a/src/nspawn/nspawn.c
65878a
+++ b/src/nspawn/nspawn.c
65878a
@@ -43,9 +43,9 @@
65878a
 #include <sys/socket.h>
65878a
 #include <linux/netlink.h>
65878a
 
65878a
-#include <systemd/sd-daemon.h>
65878a
-#include <systemd/sd-bus.h>
65878a
-
65878a
+#include "sd-daemon.h"
65878a
+#include "sd-bus.h"
65878a
+#include "sd-id128.h"
65878a
 #include "log.h"
65878a
 #include "util.h"
65878a
 #include "mkdir.h"
65878a
@@ -56,13 +56,13 @@
65878a
 #include "strv.h"
65878a
 #include "path-util.h"
65878a
 #include "loopback-setup.h"
65878a
-#include "sd-id128.h"
65878a
 #include "dev-setup.h"
65878a
 #include "fdset.h"
65878a
 #include "build.h"
65878a
 #include "fileio.h"
65878a
 #include "bus-internal.h"
65878a
 #include "bus-message.h"
65878a
+#include "bus-error.h"
65878a
 
65878a
 #ifndef TTY_GID
65878a
 #define TTY_GID 5
65878a
@@ -1229,6 +1229,60 @@ static int register_machine(void) {
65878a
         return 0;
65878a
 }
65878a
 
65878a
+static int terminate_machine(pid_t pid) {
65878a
+        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
65878a
+        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
65878a
+        _cleanup_bus_unref_ sd_bus *bus = NULL;
65878a
+        const char *path;
65878a
+        int r;
65878a
+
65878a
+        r = sd_bus_open_system(&bus;;
65878a
+        if (r < 0) {
65878a
+                log_error("Failed to open system bus: %s", strerror(-r));
65878a
+                return r;
65878a
+        }
65878a
+
65878a
+        r = sd_bus_call_method(
65878a
+                        bus,
65878a
+                        "org.freedesktop.machine1",
65878a
+                        "/org/freedesktop/machine1",
65878a
+                        "org.freedesktop.machine1.Manager",
65878a
+                        "GetMachineByPID",
65878a
+                        &error,
65878a
+                        &reply,
65878a
+                        "u",
65878a
+                        (uint32_t) pid);
65878a
+        if (r < 0) {
65878a
+                /* Note that the machine might already have been
65878a
+                 * cleaned up automatically, hence don't consider it a
65878a
+                 * failure if we cannot get the machine object. */
65878a
+                log_debug("Failed to get machine: %s", bus_error_message(&error, r));
65878a
+                return 0;
65878a
+        }
65878a
+
65878a
+        r = sd_bus_message_read(reply, "o", &path);
65878a
+        if (r < 0) {
65878a
+                log_error("Failed to parse GetMachineByPID() reply: %s", bus_error_message(&error, r));
65878a
+                return r;
65878a
+        }
65878a
+
65878a
+        r = sd_bus_call_method(
65878a
+                        bus,
65878a
+                        "org.freedesktop.machine1",
65878a
+                        path,
65878a
+                        "org.freedesktop.machine1.Machine",
65878a
+                        "Terminate",
65878a
+                        &error,
65878a
+                        NULL,
65878a
+                        NULL);
65878a
+        if (r < 0) {
65878a
+                log_debug("Failed to terminate machine: %s", bus_error_message(&error, r));
65878a
+                return 0;
65878a
+        }
65878a
+
65878a
+        return 0;
65878a
+}
65878a
+
65878a
 static bool audit_enabled(void) {
65878a
         int fd;
65878a
 
65878a
@@ -1704,6 +1758,12 @@ int main(int argc, char *argv[]) {
65878a
                 if (saved_attr_valid)
65878a
                         tcsetattr(STDIN_FILENO, TCSANOW, &saved_attr);
65878a
 
65878a
+                /* Kill if it is not dead yet anyway */
65878a
+                terminate_machine(pid);
65878a
+
65878a
+                /* Redundant, but better safe than sorry */
65878a
+                kill(pid, SIGKILL);
65878a
+
65878a
                 k = wait_for_terminate(pid, &status);
65878a
                 if (k < 0) {
65878a
                         r = EXIT_FAILURE;