Debian Codegraph @

src/analyze/analyze-dump.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "analyze.h"
6 #include "analyze-dump.h"
7 #include "bus-error.h"
8 #include "bus-locator.h"
9 #include "bus-message-util.h"
10 #include "bus-util.h"
11 #include "pager.h"
12 #include "runtime-scope.h"
13 #include "strv.h"
14 #include "unit-name.h"
15
16 static int dump_string(sd_bus *bus) {
17 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
18 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
19 int r;
20
21 assert(bus);
22
23 r = bus_call_method(bus, bus_systemd_mgr, "Dump", &error, &reply, NULL);
24 if (r < 0)
25 return log_error_errno(r, "Failed to call Dump: %s", bus_error_message(&error, r));
26
27 return bus_message_dump_string(reply);
28 }
29
30 static int dump_fd(sd_bus *bus) {
31 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
32 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
33 int r;
34
35 r = bus_call_method(bus, bus_systemd_mgr, "DumpByFileDescriptor", &error, &reply, NULL);
36 if (IN_SET(r, -EACCES, -EBADR))
37 /* Fall back to non-fd method. We need to do this even if the bus supports sending
38 * fds to cater to very old managers which didn't have the fd-based method. */
39 return dump_string(bus);
40 if (r < 0)
41 return log_error_errno(r, "Failed to call DumpByFileDescriptor: %s",
42 bus_error_message(&error, r));
43
44 return bus_message_dump_fd(reply);
45 }
46
47 static int dump_patterns_string(sd_bus *bus, char **patterns) {
48 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
49 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
50 int r;
51
52 if (strv_isempty(patterns))
53 return dump_string(bus);
54
55 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatterns");
56 if (r < 0)
57 return bus_log_create_error(r);
58
59 r = sd_bus_message_append_strv(m, patterns);
60 if (r < 0)
61 return bus_log_create_error(r);
62
63 r = sd_bus_call(bus, m, 0, &error, &reply);
64 if (r < 0)
65 return log_error_errno(r, "Failed to call DumpUnitsMatchingPatterns: %s",
66 bus_error_message(&error, r));
67
68 return bus_message_dump_string(reply);
69 }
70
71 static int dump_patterns_fd(sd_bus *bus, char **patterns) {
72 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
73 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL, *m = NULL;
74 int r;
75
76 if (strv_isempty(patterns))
77 return dump_fd(bus);
78
79 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "DumpUnitsMatchingPatternsByFileDescriptor");
80 if (r < 0)
81 return bus_log_create_error(r);
82
83 r = sd_bus_message_append_strv(m, patterns);
84 if (r < 0)
85 return bus_log_create_error(r);
86
87 r = sd_bus_call(bus, m, 0, &error, &reply);
88 if (r < 0)
89 return log_error_errno(r, "Failed to call DumpUnitsMatchingPatternsByFileDescriptor: %s",
90 bus_error_message(&error, r));
91
92 return bus_message_dump_fd(reply);
93 }
94
95 static int mangle_patterns(char **args, char ***ret) {
96 _cleanup_strv_free_ char **mangled = NULL;
97 int r;
98
99 STRV_FOREACH(arg, args) {
100 char *t;
101
102 r = unit_name_mangle_with_suffix(*arg, NULL, UNIT_NAME_MANGLE_GLOB, ".service", &t);
103 if (r < 0)
104 return log_error_errno(r, "Failed to mangle name '%s': %m", *arg);
105
106 r = strv_consume(&mangled, t);
107 if (r < 0)
108 return log_oom();
109 }
110
111 *ret = TAKE_PTR(mangled);
112 return 0;
113 }
114
115 int verb_dump(int argc, char *argv[], uintptr_t _data, void *userdata) {
116 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
117 _cleanup_strv_free_ char **patterns = NULL;
118 int r;
119
120 r = acquire_bus(&bus, NULL);
121 if (r < 0)
122 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
123
124 r = mangle_patterns(strv_skip(argv, 1), &patterns);
125 if (r < 0)
126 return r;
127
128 r = sd_bus_can_send(bus, SD_BUS_TYPE_UNIX_FD);
129 if (r < 0)
130 return log_error_errno(r, "Unable to determine if bus connection supports fd passing: %m");
131
132 pager_open(arg_pager_flags);
133
134 return r > 0 ? dump_patterns_fd(bus, patterns) : dump_patterns_string(bus, patterns);
135 }