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