daandemeyer / rpms / systemd

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