dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
65878a
From 2b7fc0e2a0a680fe61041e569eda50f06c3f8768 Mon Sep 17 00:00:00 2001
65878a
From: Lukas Nykryn <lnykryn@redhat.com>
65878a
Date: Mon, 28 Jul 2014 18:18:58 +0200
65878a
Subject: [PATCH] cgls: fix running with -M option
65878a
65878a
systemd-machined doesn't store cgroup path in a state file anymore.
65878a
Let's figure it out from the scope.
65878a
65878a
Resolves: #1085455
65878a
---
65878a
 Makefile.am     |  8 +++++-
65878a
 src/cgls/cgls.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
65878a
 2 files changed, 91 insertions(+), 2 deletions(-)
65878a
65878a
diff --git a/Makefile.am b/Makefile.am
65878a
index fa2fba6..a9fb792 100644
65878a
--- a/Makefile.am
65878a
+++ b/Makefile.am
65878a
@@ -1830,8 +1830,14 @@ systemd_reply_password_LDADD = \
65878a
 systemd_cgls_SOURCES = \
65878a
 	src/cgls/cgls.c
65878a
 
65878a
+systemd_cgls_CFLAGS = \
65878a
+	$(AM_CFLAGS) \
65878a
+	$(DBUS_CFLAGS)
65878a
+
65878a
 systemd_cgls_LDADD = \
65878a
-	libsystemd-shared.la
65878a
+	libsystemd-shared.la \
65878a
+	libsystemd-dbus.la \
65878a
+	libudev.la
65878a
 
65878a
 # ------------------------------------------------------------------------------
65878a
 systemd_cgtop_SOURCES = \
65878a
diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c
65878a
index c689b5c..f678657 100644
65878a
--- a/src/cgls/cgls.c
65878a
+++ b/src/cgls/cgls.c
65878a
@@ -19,6 +19,7 @@
65878a
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
65878a
 ***/
65878a
 
65878a
+#include <dbus/dbus.h>
65878a
 #include <limits.h>
65878a
 #include <stdio.h>
65878a
 #include <unistd.h>
65878a
@@ -35,6 +36,8 @@
65878a
 #include "build.h"
65878a
 #include "output-mode.h"
65878a
 #include "fileio.h"
65878a
+#include "dbus-common.h"
65878a
+#include "unit-name.h"
65878a
 
65878a
 static bool arg_no_pager = false;
65878a
 static bool arg_kernel_threads = false;
65878a
@@ -127,6 +130,10 @@ int main(int argc, char *argv[]) {
65878a
         int r = 0, retval = EXIT_FAILURE;
65878a
         int output_flags;
65878a
         char _cleanup_free_ *root = NULL;
65878a
+        DBusConnection *bus = NULL;
65878a
+        DBusError error;
65878a
+
65878a
+        dbus_error_init(&error);
65878a
 
65878a
         log_parse_environment();
65878a
         log_open();
65878a
@@ -147,6 +154,14 @@ int main(int argc, char *argv[]) {
65878a
                 }
65878a
         }
65878a
 
65878a
+        bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
65878a
+
65878a
+        if (!bus) {
65878a
+                printf("Failed to get D-Bus connection: %s", error.message);
65878a
+                retval = EXIT_FAILURE;
65878a
+                goto finish;
65878a
+        }
65878a
+
65878a
         output_flags =
65878a
                 arg_all * OUTPUT_SHOW_ALL |
65878a
                 (arg_full > 0) * OUTPUT_FULL_WIDTH;
65878a
@@ -189,8 +204,67 @@ int main(int argc, char *argv[]) {
65878a
                 } else {
65878a
                         if (arg_machine) {
65878a
                                 char *m;
65878a
+                                const char *cgroup;
65878a
+                                const char *property = "ControlGroup";
65878a
+                                const char *interface = "org.freedesktop.systemd1.Scope";
65878a
+                                _cleanup_free_ char *scope = NULL;
65878a
+                                _cleanup_free_ char *path = NULL;
65878a
+                                _cleanup_dbus_message_unref_ DBusMessage *reply = NULL;
65878a
+                                DBusMessageIter iter, sub;
65878a
+
65878a
                                 m = strappenda("/run/systemd/machines/", arg_machine);
65878a
-                                r = parse_env_file(m, NEWLINE, "CGROUP", &root, NULL);
65878a
+                                r = parse_env_file(m, NEWLINE, "SCOPE", &scope, NULL);
65878a
+
65878a
+                                if (r < 0) {
65878a
+                                        log_error("Failed to get machine path: %s", strerror(-r));
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
+                                path = unit_dbus_path_from_name(scope);
65878a
+                                if (!path) {
65878a
+                                        r = log_oom();
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
+                                r = bus_method_call_with_reply(
65878a
+                                                bus,
65878a
+                                                "org.freedesktop.systemd1",
65878a
+                                                path,
65878a
+                                                "org.freedesktop.DBus.Properties",
65878a
+                                                "Get",
65878a
+                                                &reply,
65878a
+                                                &error,
65878a
+                                                DBUS_TYPE_STRING, &interface,
65878a
+                                                DBUS_TYPE_STRING, &property,
65878a
+                                                DBUS_TYPE_INVALID);
65878a
+                                if (r < 0) {
65878a
+                                        log_error("Failed to query ControlGroup: %s", bus_error(&error, r));
65878a
+                                        dbus_error_free(&error);
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
+                                if (!dbus_message_iter_init(reply, &iter) ||
65878a
+                                    dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT) {
65878a
+                                        log_error("Failed to parse reply.");
65878a
+                                        r = -EINVAL;
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
+                                dbus_message_iter_recurse(&iter, &sub);
65878a
+                                if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING) {
65878a
+                                        log_error("Failed to parse reply.");
65878a
+                                        r = -EINVAL;
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
+                                dbus_message_iter_get_basic(&sub, &cgroup);
65878a
+
65878a
+                                root = strdup(cgroup);
65878a
+                                if (!root) {
65878a
+                                        r = log_oom();
65878a
+                                        goto finish;
65878a
+                                }
65878a
+
65878a
                         } else
65878a
                                 r = cg_get_root_path(&root);
65878a
                         if (r < 0) {
65878a
@@ -211,6 +285,15 @@ int main(int argc, char *argv[]) {
65878a
                 retval = EXIT_SUCCESS;
65878a
 
65878a
 finish:
65878a
+
65878a
+        if (bus) {
65878a
+                dbus_connection_flush(bus);
65878a
+                dbus_connection_close(bus);
65878a
+                dbus_connection_unref(bus);
65878a
+        }
65878a
+
65878a
+        dbus_error_free(&error);
65878a
+        dbus_shutdown();
65878a
         pager_close();
65878a
 
65878a
         return retval;