]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out "dump" verb
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 09:39:09 +0000 (10:39 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 16:22:23 +0000 (17:22 +0100)
src/analyze/analyze-dump.c [new file with mode: 0644]
src/analyze/analyze-dump.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/analyze.h
src/analyze/meson.build

diff --git a/src/analyze/analyze-dump.c b/src/analyze/analyze-dump.c
new file mode 100644 (file)
index 0000000..549e2cd
--- /dev/null
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "sd-bus.h"
+
+#include "analyze-dump.h"
+#include "analyze.h"
+#include "bus-error.h"
+#include "bus-locator.h"
+#include "bus-util.h"
+#include "copy.h"
+
+static int dump_fallback(sd_bus *bus) {
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+        const char *text = NULL;
+        int r;
+
+        assert(bus);
+
+        r = bus_call_method(bus, bus_systemd_mgr, "Dump", &error, &reply, NULL);
+        if (r < 0)
+                return log_error_errno(r, "Failed to issue method call Dump: %s", bus_error_message(&error, r));
+
+        r = sd_bus_message_read(reply, "s", &text);
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        fputs(text, stdout);
+        return 0;
+}
+
+int dump(int argc, char *argv[], void *userdata) {
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
+        int fd = -1;
+        int r;
+
+        r = acquire_bus(&bus, NULL);
+        if (r < 0)
+                return bus_log_connect_error(r, arg_transport);
+
+        pager_open(arg_pager_flags);
+
+        if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
+                return dump_fallback(bus);
+
+        r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
+        if (r < 0) {
+                /* fall back to Dump if DumpByFileDescriptor is not supported */
+                if (!IN_SET(r, -EACCES, -EBADR))
+                        return log_error_errno(r, "Failed to issue method call DumpByFileDescriptor: %s",
+                                               bus_error_message(&error, r));
+
+                return dump_fallback(bus);
+        }
+
+        r = sd_bus_message_read(reply, "h", &fd);
+        if (r < 0)
+                return bus_log_parse_error(r);
+
+        fflush(stdout);
+        return copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
+}
diff --git a/src/analyze/analyze-dump.h b/src/analyze/analyze-dump.h
new file mode 100644 (file)
index 0000000..2fd35c4
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int dump(int argc, char *argv[], void *userdata);
index a9288c336c3877a4f52a1e649c5c31296d33cefa..f9790d99ee8154371b13f2c1b076873b435e01de 100644 (file)
@@ -15,6 +15,7 @@
 #include "analyze.h"
 #include "analyze-calendar.h"
 #include "analyze-condition.h"
+#include "analyze-dump.h"
 #include "analyze-elf.h"
 #include "analyze-filesystems.h"
 #include "analyze-security.h"
@@ -96,7 +97,7 @@ static char **arg_dot_from_patterns = NULL;
 static char **arg_dot_to_patterns = NULL;
 static usec_t arg_fuzz = 0;
 PagerFlags arg_pager_flags = 0;
-static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
+BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
 static const char *arg_host = NULL;
 static UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
 static RecursiveErrors arg_recursive_errors = RECURSIVE_ERRORS_YES;
@@ -176,7 +177,7 @@ typedef struct HostInfo {
         char *architecture;
 } HostInfo;
 
-static int acquire_bus(sd_bus **bus, bool *use_full_bus) {
+int acquire_bus(sd_bus **bus, bool *use_full_bus) {
         bool user = arg_scope != UNIT_FILE_SYSTEM;
         int r;
 
@@ -1378,60 +1379,6 @@ static int dot(int argc, char *argv[], void *userdata) {
         return 0;
 }
 
-static int dump_fallback(sd_bus *bus) {
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
-        const char *text = NULL;
-        int r;
-
-        assert(bus);
-
-        r = bus_call_method(bus, bus_systemd_mgr, "Dump", &error, &reply, NULL);
-        if (r < 0)
-                return log_error_errno(r, "Failed to issue method call Dump: %s", bus_error_message(&error, r));
-
-        r = sd_bus_message_read(reply, "s", &text);
-        if (r < 0)
-                return bus_log_parse_error(r);
-
-        fputs(text, stdout);
-        return 0;
-}
-
-static int dump(int argc, char *argv[], void *userdata) {
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        int fd = -1;
-        int r;
-
-        r = acquire_bus(&bus, NULL);
-        if (r < 0)
-                return bus_log_connect_error(r, arg_transport);
-
-        pager_open(arg_pager_flags);
-
-        if (!sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD))
-                return dump_fallback(bus);
-
-        r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
-        if (r < 0) {
-                /* fall back to Dump if DumpByFileDescriptor is not supported */
-                if (!IN_SET(r, -EACCES, -EBADR))
-                        return log_error_errno(r, "Failed to issue method call DumpByFileDescriptor: %s",
-                                               bus_error_message(&error, r));
-
-                return dump_fallback(bus);
-        }
-
-        r = sd_bus_message_read(reply, "h", &fd);
-        if (r < 0)
-                return bus_log_parse_error(r);
-
-        fflush(stdout);
-        return copy_bytes(fd, STDOUT_FILENO, UINT64_MAX, 0);
-}
-
 static int cat_config(int argc, char *argv[], void *userdata) {
         char **arg, **list;
         int r;
index 062314a38967169d3b41c0f72c22105a01b04038..243e3544588883a51ddb64d1da458d1a5ccdea7f 100644 (file)
@@ -3,12 +3,16 @@
 
 #include <stdbool.h>
 
+#include "bus-util.h"
 #include "pager.h"
 #include "time-util.h"
 
 extern PagerFlags arg_pager_flags;
+extern BusTransport arg_transport;
 extern unsigned arg_iterations;
 extern usec_t arg_base_time;
 extern bool arg_quiet;
 
+int acquire_bus(sd_bus **bus, bool *use_full_bus);
+
 void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan);
index 00197d312928b6658c1d47cdd466e44997e3ed11..2d62412641193b5859a56048bc728d36bd8e01dd 100644 (file)
@@ -5,6 +5,8 @@ systemd_analyze_sources = files('''
         analyze-calendar.h
         analyze-condition.c
         analyze-condition.h
+        analyze-dump.c
+        analyze-dump.h
         analyze-elf.c
         analyze-elf.h
         analyze-filesystems.c