naccyde / rpms / systemd

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