teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0621-systemd-analyze-make-dump-work-for-large-of-units.patch

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