Debian Codegraph @

src/systemctl/systemctl-list-dependencies.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4
5 #include "alloc-util.h"
6 #include "ansi-color.h"
7 #include "bus-unit-util.h"
8 #include "glyph-util.h"
9 #include "log.h"
10 #include "pager.h"
11 #include "sort-util.h"
12 #include "special.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "systemctl.h"
16 #include "systemctl-list-dependencies.h"
17 #include "systemctl-util.h"
18 #include "terminal-util.h"
19 #include "unit-def.h"
20 #include "unit-name.h"
21
22 static int list_dependencies_print(const char *name, UnitActiveState state, int level, unsigned branches, bool last) {
23 _cleanup_free_ char *n = NULL;
24 size_t max_len = MAX(columns(),20u);
25 size_t len = 0;
26
27 if (arg_plain || state == _UNIT_ACTIVE_STATE_INVALID)
28 printf(" ");
29 else {
30 const char *on;
31
32 switch (state) {
33 case UNIT_ACTIVE:
34 case UNIT_RELOADING:
35 case UNIT_REFRESHING:
36 case UNIT_ACTIVATING:
37 on = ansi_highlight_green();
38 break;
39
40 case UNIT_INACTIVE:
41 case UNIT_DEACTIVATING:
42 on = ansi_normal();
43 break;
44
45 default:
46 on = ansi_highlight_red();
47 }
48
49 printf("%s%s%s ", on, glyph(unit_active_state_to_glyph(state)), ansi_normal());
50 }
51
52 if (!arg_plain) {
53 for (int i = level - 1; i >= 0; i--) {
54 len += 2;
55 if (len > max_len - 3 && !arg_full) {
56 printf("%s...\n",max_len % 2 ? "" : " ");
57 return 0;
58 }
59 printf("%s", glyph(branches & (1 << i) ? GLYPH_TREE_VERTICAL : GLYPH_TREE_SPACE));
60 }
61 len += 2;
62
63 if (len > max_len - 3 && !arg_full) {
64 printf("%s...\n",max_len % 2 ? "" : " ");
65 return 0;
66 }
67
68 printf("%s", glyph(last ? GLYPH_TREE_RIGHT : GLYPH_TREE_BRANCH));
69 }
70
71 if (arg_full) {
72 printf("%s\n", name);
73 return 0;
74 }
75
76 n = ellipsize(name, max_len-len, 100);
77 if (!n)
78 return log_oom();
79
80 printf("%s\n", n);
81 return 0;
82 }
83
84 static int list_dependencies_compare(char * const *a, char * const *b) {
85 assert(a);
86 assert(b);
87
88 if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
89 return 1;
90 if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
91 return -1;
92
93 return strcasecmp(*a, *b);
94 }
95
96 static int list_dependencies_one(
97 sd_bus *bus,
98 const char *name,
99 int level,
100 char ***units,
101 unsigned branches) {
102
103 _cleanup_strv_free_ char **deps = NULL;
104 int r;
105 bool circular = false;
106
107 assert(bus);
108 assert(name);
109 assert(units);
110
111 r = strv_extend(units, name);
112 if (r < 0)
113 return log_oom();
114
115 r = unit_get_dependencies(bus, name, &deps);
116 if (r < 0)
117 return r;
118
119 typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
120
121 STRV_FOREACH(c, deps) {
122 _cleanup_free_ char *load_state = NULL, *sub_state = NULL;
123 UnitActiveState active_state;
124
125 if (strv_contains(*units, *c)) {
126 circular = true;
127 continue;
128 }
129
130 if (arg_types && !strv_contains(arg_types, unit_type_suffix(*c)))
131 continue;
132
133 r = get_state_one_unit(bus, *c, &active_state);
134 if (r < 0)
135 return r;
136
137 if (arg_states) {
138 r = unit_load_state(bus, *c, &load_state);
139 if (r < 0)
140 return r;
141
142 r = get_sub_state_one_unit(bus, *c, &sub_state);
143 if (r < 0)
144 return r;
145
146 if (!strv_overlap(arg_states, STRV_MAKE(unit_active_state_to_string(active_state), load_state, sub_state)))
147 continue;
148 }
149
150 r = list_dependencies_print(*c, active_state, level, branches, /* last= */ c[1] == NULL && !circular);
151 if (r < 0)
152 return r;
153
154 if (arg_all || unit_name_to_type(*c) == UNIT_TARGET) {
155 r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (c[1] == NULL ? 0 : 1));
156 if (r < 0)
157 return r;
158 }
159 }
160
161 if (circular && !arg_plain) {
162 r = list_dependencies_print("...", _UNIT_ACTIVE_STATE_INVALID, level, branches, /* last= */ true);
163 if (r < 0)
164 return r;
165 }
166
167 if (!arg_plain)
168 strv_remove(*units, name);
169
170 return 0;
171 }
172
173 int verb_list_dependencies(int argc, char *argv[], uintptr_t _data, void *userdata) {
174 _cleanup_strv_free_ char **units = NULL, **done = NULL;
175 char **patterns;
176 sd_bus *bus;
177 int r;
178
179 /* We won't be able to preserve the tree structure if --type= or --state= is used */
180 arg_plain = arg_plain || arg_types || arg_states;
181
182 r = acquire_bus(BUS_MANAGER, &bus);
183 if (r < 0)
184 return r;
185
186 patterns = strv_skip(argv, 1);
187 if (patterns) {
188 r = expand_unit_names(bus, patterns, NULL, &units, NULL);
189 if (r < 0)
190 return log_error_errno(r, "Failed to expand names: %m");
191 } else {
192 units = strv_new(SPECIAL_DEFAULT_TARGET);
193 if (!units)
194 return log_oom();
195 }
196
197 pager_open(arg_pager_flags);
198
199 STRV_FOREACH(u, units) {
200 if (u != units)
201 puts("");
202
203 puts(*u);
204 r = list_dependencies_one(bus, *u, 0, &done, 0);
205 if (r < 0)
206 return r;
207 }
208
209 return 0;
210 }