Debian Codegraph @

src/analyze/analyze-dot.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "analyze.h"
7 #include "analyze-dot.h"
8 #include "bus-error.h"
9 #include "bus-locator.h"
10 #include "bus-unit-util.h"
11 #include "bus-util.h"
12 #include "glob-util.h"
13 #include "log.h"
14 #include "runtime-scope.h"
15 #include "string-util.h"
16 #include "strv.h"
17 #include "terminal-util.h"
18 #include "unit-def.h"
19
20 static int graph_one_property(
21 sd_bus *bus,
22 const UnitInfo *u,
23 const char *prop,
24 const char *color,
25 char **patterns,
26 char **from_patterns,
27 char **to_patterns) {
28
29 _cleanup_strv_free_ char **units = NULL;
30 bool match_patterns;
31 int r;
32
33 assert(bus);
34 assert(u);
35 assert(prop);
36 assert(color);
37
38 match_patterns = strv_fnmatch(patterns, u->id);
39
40 if (!strv_isempty(from_patterns) && !match_patterns && !strv_fnmatch(from_patterns, u->id))
41 return 0;
42
43 r = bus_get_unit_property_strv(bus, u->unit_path, prop, &units);
44 if (r < 0)
45 return r;
46
47 STRV_FOREACH(unit, units) {
48 bool match_patterns2;
49
50 match_patterns2 = strv_fnmatch(patterns, *unit);
51
52 if (!strv_isempty(to_patterns) && !match_patterns2 && !strv_fnmatch(to_patterns, *unit))
53 continue;
54
55 if (!strv_isempty(patterns) && !match_patterns && !match_patterns2)
56 continue;
57
58 printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u->id, *unit, color);
59 }
60
61 return 0;
62 }
63
64 static int graph_one(
65 sd_bus *bus,
66 const UnitInfo *u,
67 char **patterns,
68 char **from_patterns,
69 char **to_patterns) {
70
71 int r;
72
73 assert(bus);
74 assert(u);
75
76 if (IN_SET(arg_dot, DEP_ORDER, DEP_ALL)) {
77 r = graph_one_property(bus, u, "After", "green", patterns, from_patterns, to_patterns);
78 if (r < 0)
79 return r;
80 }
81
82 if (IN_SET(arg_dot, DEP_REQUIRE, DEP_ALL)) {
83 r = graph_one_property(bus, u, "Requires", "black", patterns, from_patterns, to_patterns);
84 if (r < 0)
85 return r;
86
87 r = graph_one_property(bus, u, "Requisite", "darkblue", patterns, from_patterns, to_patterns);
88 if (r < 0)
89 return r;
90
91 r = graph_one_property(bus, u, "BindsTo", "gold", patterns, from_patterns, to_patterns);
92 if (r < 0)
93 return r;
94
95 r = graph_one_property(bus, u, "Wants", "grey66", patterns, from_patterns, to_patterns);
96 if (r < 0)
97 return r;
98
99 r = graph_one_property(bus, u, "Conflicts", "red", patterns, from_patterns, to_patterns);
100 if (r < 0)
101 return r;
102 }
103
104 return 0;
105 }
106
107 static int expand_patterns(sd_bus *bus, char **patterns, char ***ret) {
108 _cleanup_strv_free_ char **expanded_patterns = NULL;
109 int r;
110
111 assert(bus);
112 assert(ret);
113
114 STRV_FOREACH(pattern, patterns) {
115 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
116 _cleanup_free_ char *unit = NULL, *unit_id = NULL;
117
118 if (strv_extend(&expanded_patterns, *pattern) < 0)
119 return log_oom();
120
121 if (string_is_glob(*pattern))
122 continue;
123
124 unit = unit_dbus_path_from_name(*pattern);
125 if (!unit)
126 return log_oom();
127
128 r = sd_bus_get_property_string(
129 bus,
130 "org.freedesktop.systemd1",
131 unit,
132 "org.freedesktop.systemd1.Unit",
133 "Id",
134 &error,
135 &unit_id);
136 if (r < 0)
137 return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
138
139 if (!streq(*pattern, unit_id))
140 if (strv_extend(&expanded_patterns, unit_id) < 0)
141 return log_oom();
142 }
143
144 *ret = TAKE_PTR(expanded_patterns); /* do not free */
145
146 return 0;
147 }
148
149 int verb_dot(int argc, char *argv[], uintptr_t _data, void *userdata) {
150 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
151 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
152 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
153 _cleanup_strv_free_ char **expanded_patterns = NULL;
154 _cleanup_strv_free_ char **expanded_from_patterns = NULL;
155 _cleanup_strv_free_ char **expanded_to_patterns = NULL;
156 UnitInfo u;
157 int r;
158
159 r = acquire_bus(&bus, NULL);
160 if (r < 0)
161 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
162
163 r = expand_patterns(bus, strv_skip(argv, 1), &expanded_patterns);
164 if (r < 0)
165 return r;
166
167 r = expand_patterns(bus, arg_dot_from_patterns, &expanded_from_patterns);
168 if (r < 0)
169 return r;
170
171 r = expand_patterns(bus, arg_dot_to_patterns, &expanded_to_patterns);
172 if (r < 0)
173 return r;
174
175 r = bus_call_method(bus, bus_systemd_mgr, "ListUnits", &error, &reply, NULL);
176 if (r < 0)
177 return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
178
179 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
180 if (r < 0)
181 return bus_log_parse_error(r);
182
183 printf("digraph systemd {\n");
184
185 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
186
187 r = graph_one(bus, &u, expanded_patterns, expanded_from_patterns, expanded_to_patterns);
188 if (r < 0)
189 return r;
190 }
191 if (r < 0)
192 return bus_log_parse_error(r);
193
194 printf("}\n");
195
196 log_info(" Color legend: black = Requires\n"
197 " dark blue = Requisite\n"
198 " gold = BindsTo\n"
199 " dark grey = Wants\n"
200 " red = Conflicts\n"
201 " green = After\n");
202
203 if (on_tty() && !arg_quiet)
204 log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
205 "-- Try a shell pipeline like 'systemd-analyze dot | dot -Tsvg > systemd.svg'!\n");
206
207 return 0;
208 }