daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
aed857
From 6772555b226a116bff07b7d8af28b16032273866 Mon Sep 17 00:00:00 2001
aed857
From: David Tardon <dtardon@redhat.com>
aed857
Date: Wed, 9 May 2018 09:35:52 +0200
aed857
Subject: [PATCH] systemd-analyze: make dump work for large # of units
aed857
aed857
If there is a large number of units, the size of the generated dump
aed857
string can overstep DBus message size limit. So let's pass that string
aed857
via a fd.
aed857
aed857
(cherry picked from commit c0a1bfacfea9c65ea79fd07682a5b60b5d711a33)
aed857
aed857
Resolves: #1446095
aed857
---
23b3cf
 src/analyze/analyze.c                  | 57 ++++++++++++++++++++++----
23b3cf
 src/core/dbus-manager.c                | 26 +++++++++++-
23b3cf
 src/core/org.freedesktop.systemd1.conf |  4 ++
aed857
 3 files changed, 76 insertions(+), 11 deletions(-)
aed857
aed857
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
c62b8e
index ff84f6894f..7116aaa88d 100644
aed857
--- a/src/analyze/analyze.c
aed857
+++ b/src/analyze/analyze.c
aed857
@@ -29,6 +29,7 @@
aed857
 #include "sd-bus.h"
aed857
 #include "bus-util.h"
aed857
 #include "bus-error.h"
aed857
+#include "copy.h"
aed857
 #include "install.h"
aed857
 #include "log.h"
aed857
 #include "build.h"
aed857
@@ -1096,12 +1097,42 @@ static int dot(sd_bus *bus, char* patterns[]) {
aed857
         return 0;
aed857
 }
aed857
 
aed857
-static int dump(sd_bus *bus, char **args) {
aed857
-        _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
aed857
-        _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
aed857
+static int dump_fallback(sd_bus *bus) {
aed857
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
aed857
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
aed857
         const char *text = NULL;
aed857
         int r;
aed857
 
aed857
+        assert(bus);
aed857
+
aed857
+        r = sd_bus_call_method(
aed857
+                        bus,
aed857
+                        "org.freedesktop.systemd1",
aed857
+                        "/org/freedesktop/systemd1",
aed857
+                        "org.freedesktop.systemd1.Manager",
aed857
+                        "Dump",
aed857
+                        &error,
aed857
+                        &reply,
aed857
+                        "");
aed857
+        if (r < 0) {
aed857
+                log_error("Failed to issue method call Dump: %s", bus_error_message(&error, -r));
aed857
+                return r;
aed857
+        }
aed857
+
aed857
+        r = sd_bus_message_read(reply, "s", &text);
aed857
+        if (r < 0)
aed857
+                return bus_log_parse_error(r);
aed857
+
aed857
+        fputs(text, stdout);
aed857
+        return 0;
aed857
+}
aed857
+
aed857
+static int dump(sd_bus *bus, char **args) {
aed857
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
aed857
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
aed857
+        int fd = -1;
aed857
+        int r;
aed857
+
aed857
         if (!strv_isempty(args)) {
aed857
                 log_error("Too many arguments.");
aed857
                 return -E2BIG;
aed857
@@ -1109,26 +1140,34 @@ static int dump(sd_bus *bus, char **args) {
aed857
 
aed857
         pager_open_if_enabled();
aed857
 
aed857
+        if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
aed857
+                return dump_fallback(bus);
aed857
+
aed857
         r = sd_bus_call_method(
aed857
                         bus,
aed857
                        "org.freedesktop.systemd1",
aed857
                        "/org/freedesktop/systemd1",
aed857
                        "org.freedesktop.systemd1.Manager",
aed857
-                       "Dump",
aed857
+                       "DumpByFileDescriptor",
aed857
                        &error,
aed857
                        &reply,
aed857
                        "");
aed857
         if (r < 0) {
aed857
-                log_error("Failed issue method call: %s", bus_error_message(&error, -r));
aed857
-                return r;
aed857
+                /* fall back to Dump if DumpByFileDescriptor is not supported */
aed857
+                if (!IN_SET(r, -EACCES, -EBADR)) {
aed857
+                        log_error("Failed to issue method call DumpByFileDescriptor: %s", bus_error_message(&error, -r));
aed857
+                        return r;
aed857
+                }
aed857
+
aed857
+                return dump_fallback(bus);
aed857
         }
aed857
 
aed857
-        r = sd_bus_message_read(reply, "s", &text);
aed857
+        r = sd_bus_message_read(reply, "h", &fd;;
aed857
         if (r < 0)
aed857
                 return bus_log_parse_error(r);
aed857
 
aed857
-        fputs(text, stdout);
aed857
-        return 0;
aed857
+        fflush(stdout);
aed857
+        return copy_bytes(fd, STDOUT_FILENO, (uint64_t) -1, 0);
aed857
 }
aed857
 
aed857
 static int set_log_level(sd_bus *bus, char **args) {
aed857
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
c62b8e
index d34ed042f6..1766163b33 100644
aed857
--- a/src/core/dbus-manager.c
aed857
+++ b/src/core/dbus-manager.c
aed857
@@ -1064,7 +1064,7 @@ static int method_unsubscribe(sd_bus *bus, sd_bus_message *message, void *userda
aed857
         return sd_bus_reply_method_return(message, NULL);
aed857
 }
aed857
 
aed857
-static int method_dump(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
aed857
+static int dump_impl(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error, int (*reply)(sd_bus_message *, char *)) {
aed857
         _cleanup_free_ char *dump = NULL;
aed857
         _cleanup_fclose_ FILE *f = NULL;
aed857
         Manager *m = userdata;
aed857
@@ -1089,13 +1089,34 @@ static int method_dump(sd_bus *bus, sd_bus_message *message, void *userdata, sd_
aed857
         manager_dump_jobs(m, f, NULL);
aed857
 
aed857
         fflush(f);
aed857
-
aed857
         if (ferror(f))
aed857
                 return -ENOMEM;
aed857
 
aed857
+        return reply(message, dump);
aed857
+}
aed857
+
aed857
+static int reply_dump(sd_bus_message *message, char *dump) {
aed857
         return sd_bus_reply_method_return(message, "s", dump);
aed857
 }
aed857
 
aed857
+static int method_dump(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
aed857
+        return dump_impl(bus, message, userdata, error, reply_dump);
aed857
+}
aed857
+
aed857
+static int reply_dump_by_fd(sd_bus_message *message, char *dump) {
aed857
+        _cleanup_close_ int fd = -1;
aed857
+
aed857
+        fd = acquire_data_fd(dump, strlen(dump), 0);
aed857
+        if (fd < 0)
aed857
+                return fd;
aed857
+
aed857
+        return sd_bus_reply_method_return(message, "h", fd);
aed857
+}
aed857
+
aed857
+static int method_dump_by_fd(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
aed857
+        return dump_impl(bus, message, userdata, error, reply_dump_by_fd);
aed857
+}
aed857
+
aed857
 static int method_create_snapshot(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) {
aed857
         _cleanup_free_ char *path = NULL;
aed857
         Manager *m = userdata;
aed857
@@ -2092,6 +2113,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
aed857
         SD_BUS_METHOD("Subscribe", NULL, NULL, method_subscribe, SD_BUS_VTABLE_UNPRIVILEGED),
aed857
         SD_BUS_METHOD("Unsubscribe", NULL, NULL, method_unsubscribe, SD_BUS_VTABLE_UNPRIVILEGED),
aed857
         SD_BUS_METHOD("Dump", NULL, "s", method_dump, SD_BUS_VTABLE_UNPRIVILEGED),
aed857
+        SD_BUS_METHOD("DumpByFileDescriptor", NULL, "h", method_dump_by_fd, SD_BUS_VTABLE_UNPRIVILEGED),
aed857
         SD_BUS_METHOD("CreateSnapshot", "sb", "o", method_create_snapshot, 0),
aed857
         SD_BUS_METHOD("RemoveSnapshot", "s", NULL, method_remove_snapshot, 0),
aed857
         SD_BUS_METHOD("Reload", NULL, NULL, method_reload, SD_BUS_VTABLE_UNPRIVILEGED),
aed857
diff --git a/src/core/org.freedesktop.systemd1.conf b/src/core/org.freedesktop.systemd1.conf
c62b8e
index 3997dd0b4e..8187cf1731 100644
aed857
--- a/src/core/org.freedesktop.systemd1.conf
aed857
+++ b/src/core/org.freedesktop.systemd1.conf
aed857
@@ -96,6 +96,10 @@
aed857
                        send_interface="org.freedesktop.systemd1.Manager"
aed857
                        send_member="Dump"/>
aed857
 
aed857
+                
aed857
+                       send_interface="org.freedesktop.systemd1.Manager"
aed857
+                       send_member="DumpByFileDescriptor"/>
aed857
+
aed857
                 
aed857
                        send_interface="org.freedesktop.systemd1.Manager"
aed857
                        send_member="GetDefaultTarget"/>