Debian Codegraph @

src/systemctl/systemctl-list-unit-files.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fnmatch.h>
4
5 #include "sd-bus.h"
6
7 #include "alloc-util.h"
8 #include "ansi-color.h"
9 #include "bus-error.h"
10 #include "bus-locator.h"
11 #include "bus-util.h"
12 #include "format-table.h"
13 #include "hashmap.h"
14 #include "install.h"
15 #include "path-util.h"
16 #include "sort-util.h"
17 #include "string-util.h"
18 #include "strv.h"
19 #include "systemctl.h"
20 #include "systemctl-list-unit-files.h"
21 #include "systemctl-util.h"
22
23 static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) {
24 const char *d1, *d2;
25 int r, s;
26
27 assert(a);
28 assert(a->path);
29 assert(b);
30 assert(b->path);
31
32 d1 = strrchr(a->path, '.');
33 d2 = strrchr(b->path, '.');
34
35 if (d1 && d2) {
36 r = strcasecmp(d1, d2);
37 if (r != 0)
38 return r;
39 }
40
41 _cleanup_free_ char *f1 = NULL, *f2 = NULL;
42 r = path_extract_filename(a->path, &f1);
43 s = path_extract_filename(b->path, &f2);
44 if (r < 0 || s < 0)
45 return CMP(r, s);
46
47 return strcasecmp(f1, f2);
48 }
49
50 static int output_show_unit_file(const UnitFileList *u, char **states, char **patterns) {
51 int r;
52
53 assert(u);
54 assert(u->path);
55
56 _cleanup_free_ char *id = NULL;
57 r = path_extract_filename(u->path, &id);
58 if (r < 0)
59 return log_debug_errno(r, "Failed to extract unit name from path %s: %m", u->path);
60
61 if (!strv_fnmatch_or_empty(patterns, id, FNM_NOESCAPE))
62 return false;
63
64 if (!strv_isempty(arg_types)) {
65 const char *dot;
66
67 dot = strrchr(u->path, '.');
68 if (!dot)
69 return false;
70
71 if (!strv_contains(arg_types, dot+1))
72 return false;
73 }
74
75 if (!strv_isempty(states) &&
76 !strv_contains(states, unit_file_state_to_string(u->state)))
77 return false;
78
79 return true;
80 }
81
82 static const char* preset_action_to_color(PresetAction action, bool underline) {
83 assert(action >= 0);
84
85 switch (action) {
86 case PRESET_ENABLE:
87 return underline ? ansi_highlight_green_underline() : ansi_highlight_green();
88 case PRESET_DISABLE:
89 return underline ? ansi_highlight_red_underline() : ansi_highlight_red();
90 case PRESET_IGNORE:
91 return underline ? ansi_highlight_yellow_underline() : ansi_highlight_yellow();
92 default:
93 return NULL;
94 }
95 }
96
97 static int output_unit_file_list(const UnitFileList *units, unsigned c) {
98 _cleanup_(table_unrefp) Table *table = NULL;
99 _cleanup_(unit_file_presets_done) UnitFilePresets presets = {};
100 int r;
101
102 table = table_new("unit file", "state", "preset");
103 if (!table)
104 return log_oom();
105
106 table_set_header(table, arg_legend != 0);
107 if (arg_full)
108 table_set_width(table, 0);
109
110 table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
111
112 FOREACH_ARRAY(u, units, c) {
113 const char *on_underline = NULL, *on_unit_color = NULL;
114 bool underline;
115
116 underline = u + 1 < units + c &&
117 !streq(unit_type_suffix(u->path), unit_type_suffix((u + 1)->path));
118
119 if (underline)
120 on_underline = ansi_underline();
121
122 if (IN_SET(u->state,
123 UNIT_FILE_MASKED,
124 UNIT_FILE_MASKED_RUNTIME,
125 UNIT_FILE_DISABLED,
126 UNIT_FILE_BAD))
127 on_unit_color = underline ? ansi_highlight_red_underline() : ansi_highlight_red();
128 else if (IN_SET(u->state,
129 UNIT_FILE_ENABLED,
130 UNIT_FILE_ALIAS))
131 on_unit_color = underline ? ansi_highlight_green_underline() : ansi_highlight_green();
132 else
133 on_unit_color = on_underline;
134
135 _cleanup_free_ char *id = NULL;
136 r = path_extract_filename(u->path, &id);
137 if (r < 0)
138 return log_error_errno(r, "Failed to extract unit name from path %s: %m", u->path);
139
140 r = table_add_many(table,
141 TABLE_STRING, id,
142 TABLE_SET_BOTH_COLORS, strempty(on_underline),
143 TABLE_STRING, unit_file_state_to_string(u->state),
144 TABLE_SET_BOTH_COLORS, strempty(on_unit_color));
145 if (r < 0)
146 return table_log_add_error(r);
147
148 if (show_preset_for_state(u->state)) {
149 const char *on_preset_color = underline ? on_underline : ansi_normal();
150
151 r = unit_file_query_preset(arg_runtime_scope, arg_root, id, &presets);
152 if (r >= 0)
153 on_preset_color = preset_action_to_color(r, underline);
154
155 r = table_add_many(table,
156 TABLE_STRING, strna(preset_action_past_tense_to_string(r)),
157 TABLE_SET_BOTH_COLORS, strempty(on_preset_color));
158 } else
159 r = table_add_many(table,
160 TABLE_EMPTY,
161 TABLE_SET_BOTH_COLORS, underline ? ansi_grey_underline() : ansi_grey());
162 if (r < 0)
163 return table_log_add_error(r);
164 }
165
166 r = output_table(table);
167 if (r < 0)
168 return r;
169
170 if (arg_legend != 0)
171 printf("\n%u unit files listed.\n", c);
172
173 return 0;
174 }
175
176 int verb_list_unit_files(int argc, char *argv[], uintptr_t _data, void *userdata) {
177 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
178 _cleanup_hashmap_free_ Hashmap *h = NULL;
179 _cleanup_free_ UnitFileList *units = NULL;
180 unsigned c = 0;
181 int r;
182
183 if (install_client_side() != INSTALL_CLIENT_SIDE_NO) {
184 unsigned n_units;
185
186 r = unit_file_get_list(arg_runtime_scope, arg_root, arg_states, strv_skip(argv, 1), &h);
187 if (r < 0)
188 return log_error_errno(r, "Failed to get unit file list: %m");
189
190 n_units = hashmap_size(h);
191
192 units = new(UnitFileList, n_units);
193 if (!units)
194 return log_oom();
195
196 UnitFileList *u;
197 HASHMAP_FOREACH(u, h) {
198 if (output_show_unit_file(u, NULL, NULL) <= 0)
199 continue;
200
201 units[c++] = *u;
202 }
203
204 assert(c <= n_units);
205 } else {
206 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
207 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
208 const char *path, *state;
209 bool fallback = false;
210 sd_bus *bus;
211
212 r = acquire_bus(BUS_MANAGER, &bus);
213 if (r < 0)
214 return r;
215
216 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFilesByPatterns");
217 if (r < 0)
218 return bus_log_create_error(r);
219
220 r = sd_bus_message_append_strv(m, arg_states);
221 if (r < 0)
222 return bus_log_create_error(r);
223
224 if (arg_with_dependencies) {
225 _cleanup_strv_free_ char **names_with_deps = NULL;
226
227 r = append_unit_dependencies(bus, strv_skip(argv, 1), &names_with_deps);
228 if (r < 0)
229 return log_error_errno(r, "Failed to append unit dependencies: %m");
230
231 r = sd_bus_message_append_strv(m, names_with_deps);
232 } else
233 r = sd_bus_message_append_strv(m, strv_skip(argv, 1));
234 if (r < 0)
235 return bus_log_create_error(r);
236
237 r = sd_bus_call(bus, m, 0, &error, &reply);
238 if (r < 0 && sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_METHOD)) {
239 /* Fallback to legacy ListUnitFiles method */
240 log_debug_errno(r, "Unable to list unit files through ListUnitFilesByPatterns, falling back to ListUnitsFiles method.");
241
242 fallback = true;
243 m = sd_bus_message_unref(m);
244 sd_bus_error_free(&error);
245
246 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "ListUnitFiles");
247 if (r < 0)
248 return bus_log_create_error(r);
249
250 r = sd_bus_call(bus, m, 0, &error, &reply);
251 }
252 if (r < 0)
253 return log_error_errno(r, "Failed to list unit files: %s", bus_error_message(&error, r));
254
255 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ss)");
256 if (r < 0)
257 return bus_log_parse_error(r);
258
259 while ((r = sd_bus_message_read(reply, "(ss)", &path, &state)) > 0) {
260
261 if (!GREEDY_REALLOC(units, c + 1))
262 return log_oom();
263
264 units[c] = (UnitFileList) {
265 .path = (char*) path,
266 .state = unit_file_state_from_string(state),
267 };
268
269 if (output_show_unit_file(&units[c],
270 fallback ? arg_states : NULL,
271 fallback ? strv_skip(argv, 1) : NULL) > 0)
272 c++;
273 }
274 if (r < 0)
275 return bus_log_parse_error(r);
276
277 r = sd_bus_message_exit_container(reply);
278 if (r < 0)
279 return bus_log_parse_error(r);
280 }
281
282 pager_open(arg_pager_flags);
283
284 typesafe_qsort(units, c, compare_unit_file_list);
285 r = output_unit_file_list(units, c);
286 if (r < 0)
287 return r;
288
289 if (c == 0)
290 return -ENOENT;
291
292 return 0;
293 }