richardphibel / rpms / systemd

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