Debian Codegraph @

src/shared/bus-unit-procs.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "ansi-color.h"
6 #include "bus-locator.h"
7 #include "bus-unit-procs.h"
8 #include "format-util.h"
9 #include "glyph-util.h"
10 #include "hashmap.h"
11 #include "list.h"
12 #include "log.h"
13 #include "output-mode.h"
14 #include "path-util.h"
15 #include "process-util.h"
16 #include "sort-util.h"
17 #include "string-util.h"
18 #include "terminal-util.h"
19
20 struct CGroupInfo {
21 char *cgroup_path;
22 bool is_const; /* If false, cgroup_path should be free()'d */
23
24 Hashmap *pids; /* PID → process name */
25 bool done;
26
27 struct CGroupInfo *parent;
28 LIST_FIELDS(struct CGroupInfo, siblings);
29 LIST_HEAD(struct CGroupInfo, children);
30 size_t n_children;
31 };
32
33 static int add_cgroup(Hashmap *cgroups, const char *path, bool is_const, struct CGroupInfo **ret) {
34 struct CGroupInfo *parent = NULL, *cg;
35 int r;
36
37 assert(cgroups);
38 assert(ret);
39
40 path = empty_to_root(path);
41
42 cg = hashmap_get(cgroups, path);
43 if (cg) {
44 *ret = cg;
45 return 0;
46 }
47
48 if (!empty_or_root(path)) {
49 const char *e, *pp;
50
51 e = strrchr(path, '/');
52 if (!e)
53 return -EINVAL;
54
55 pp = strndupa_safe(path, e - path);
56
57 r = add_cgroup(cgroups, pp, false, &parent);
58 if (r < 0)
59 return r;
60 }
61
62 cg = new0(struct CGroupInfo, 1);
63 if (!cg)
64 return -ENOMEM;
65
66 if (is_const)
67 cg->cgroup_path = (char*) path;
68 else {
69 cg->cgroup_path = strdup(path);
70 if (!cg->cgroup_path) {
71 free(cg);
72 return -ENOMEM;
73 }
74 }
75
76 cg->is_const = is_const;
77 cg->parent = parent;
78
79 r = hashmap_put(cgroups, cg->cgroup_path, cg);
80 if (r < 0) {
81 if (!is_const)
82 free(cg->cgroup_path);
83 free(cg);
84 return r;
85 }
86
87 if (parent) {
88 LIST_PREPEND(siblings, parent->children, cg);
89 parent->n_children++;
90 }
91
92 *ret = cg;
93 return 1;
94 }
95
96 static int add_process(
97 Hashmap *cgroups,
98 const char *path,
99 pid_t pid,
100 const char *name) {
101
102 struct CGroupInfo *cg;
103 int r;
104
105 assert(cgroups);
106 assert(name);
107 assert(pid > 0);
108
109 r = add_cgroup(cgroups, path, true, &cg);
110 if (r < 0)
111 return r;
112
113 return hashmap_ensure_put(&cg->pids, &trivial_hash_ops, PID_TO_PTR(pid), (void*) name);
114 }
115
116 static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
117 assert(cgroups);
118 assert(cg);
119
120 while (cg->children)
121 remove_cgroup(cgroups, cg->children);
122
123 hashmap_remove(cgroups, cg->cgroup_path);
124
125 if (!cg->is_const)
126 free(cg->cgroup_path);
127
128 hashmap_free(cg->pids);
129
130 if (cg->parent)
131 LIST_REMOVE(siblings, cg->parent->children, cg);
132
133 free(cg);
134 }
135
136 static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
137 assert(a);
138 assert(b);
139
140 return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
141 }
142
143 static int dump_processes(
144 Hashmap *cgroups,
145 const char *cgroup_path,
146 const char *prefix,
147 unsigned n_columns,
148 OutputFlags flags) {
149
150 struct CGroupInfo *cg;
151 int r;
152
153 assert(prefix);
154
155 cgroup_path = empty_to_root(cgroup_path);
156
157 cg = hashmap_get(cgroups, cgroup_path);
158 if (!cg)
159 return 0;
160
161 if (!hashmap_isempty(cg->pids)) {
162 const char *name;
163 void *pidp;
164 size_t n = 0;
165
166 /* Order processes by their PID */
167 pid_t *pids = newa(pid_t, hashmap_size(cg->pids));
168
169 HASHMAP_FOREACH_KEY(name, pidp, cg->pids)
170 pids[n++] = PTR_TO_PID(pidp);
171
172 assert(n == hashmap_size(cg->pids));
173 assert(n > 0);
174 typesafe_qsort(pids, n, pid_compare_func);
175
176 int width = DECIMAL_STR_WIDTH(pids[n-1]);
177
178 for (size_t i = 0; i < n; i++) {
179 _cleanup_free_ char *e = NULL;
180 const char *special;
181 bool more;
182
183 name = hashmap_get(cg->pids, PID_TO_PTR(pids[i]));
184 assert(name);
185
186 if (n_columns != 0) {
187 unsigned k;
188
189 k = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
190
191 e = ellipsize(name, k, 100);
192 if (e)
193 name = e;
194 }
195
196 more = i+1 < n || cg->children;
197 special = glyph(more ? GLYPH_TREE_BRANCH : GLYPH_TREE_RIGHT);
198
199 fprintf(stdout, "%s%s%s%*"PID_PRI" %s%s\n",
200 prefix,
201 special,
202 ansi_grey(),
203 width, pids[i],
204 name,
205 ansi_normal());
206 }
207 }
208
209 if (cg->children) {
210 struct CGroupInfo **children;
211 size_t n = 0, i;
212
213 /* Order subcgroups by their name */
214 children = newa(struct CGroupInfo*, cg->n_children);
215 LIST_FOREACH(siblings, child, cg->children)
216 children[n++] = child;
217 assert(n == cg->n_children);
218 typesafe_qsort(children, n, cgroup_info_compare_func);
219
220 if (n_columns != 0)
221 n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
222
223 for (i = 0; i < n; i++) {
224 _cleanup_free_ char *pp = NULL;
225 const char *name, *special;
226 bool more;
227
228 name = strrchr(children[i]->cgroup_path, '/');
229 if (!name)
230 return -EINVAL;
231 name++;
232
233 more = i+1 < n;
234 special = glyph(more ? GLYPH_TREE_BRANCH : GLYPH_TREE_RIGHT);
235
236 fputs(prefix, stdout);
237 fputs(special, stdout);
238 fputs(name, stdout);
239 fputc('\n', stdout);
240
241 special = glyph(more ? GLYPH_TREE_VERTICAL : GLYPH_TREE_SPACE);
242
243 pp = strjoin(prefix, special);
244 if (!pp)
245 return -ENOMEM;
246
247 r = dump_processes(cgroups, children[i]->cgroup_path, pp, n_columns, flags);
248 if (r < 0)
249 return r;
250 }
251 }
252
253 cg->done = true;
254 return 0;
255 }
256
257 static int dump_extra_processes(
258 Hashmap *cgroups,
259 const char *prefix,
260 unsigned n_columns,
261 OutputFlags flags) {
262
263 _cleanup_free_ pid_t *pids = NULL;
264 _cleanup_hashmap_free_ Hashmap *names = NULL;
265 struct CGroupInfo *cg;
266 size_t n = 0, k;
267 int width, r;
268
269 /* Prints the extra processes, i.e. those that are in cgroups we haven't displayed yet. We show them as
270 * combined, sorted, linear list. */
271
272 HASHMAP_FOREACH(cg, cgroups) {
273 const char *name;
274 void *pidp;
275
276 if (cg->done)
277 continue;
278
279 if (hashmap_isempty(cg->pids))
280 continue;
281
282 r = hashmap_ensure_allocated(&names, &trivial_hash_ops);
283 if (r < 0)
284 return r;
285
286 if (!GREEDY_REALLOC(pids, n + hashmap_size(cg->pids)))
287 return -ENOMEM;
288
289 HASHMAP_FOREACH_KEY(name, pidp, cg->pids) {
290 pids[n++] = PTR_TO_PID(pidp);
291
292 r = hashmap_put(names, pidp, (void*) name);
293 if (r < 0)
294 return r;
295 }
296 }
297
298 if (n == 0)
299 return 0;
300
301 typesafe_qsort(pids, n, pid_compare_func);
302 width = DECIMAL_STR_WIDTH(pids[n-1]);
303
304 for (k = 0; k < n; k++) {
305 _cleanup_free_ char *e = NULL;
306 const char *name;
307
308 name = hashmap_get(names, PID_TO_PTR(pids[k]));
309 assert(name);
310
311 if (n_columns != 0) {
312 unsigned z;
313
314 z = MAX(LESS_BY(n_columns, 2U + width + 1U), 20U);
315
316 e = ellipsize(name, z, 100);
317 if (e)
318 name = e;
319 }
320
321 fprintf(stdout, "%s%s %*" PID_PRI " %s\n",
322 prefix,
323 glyph(GLYPH_TRIANGULAR_BULLET),
324 width, pids[k],
325 name);
326 }
327
328 return 0;
329 }
330
331 int unit_show_processes(
332 sd_bus *bus,
333 const char *unit,
334 const char *cgroup_path,
335 const char *prefix,
336 unsigned n_columns,
337 OutputFlags flags,
338 sd_bus_error *reterr_error) {
339
340 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
341 Hashmap *cgroups = NULL;
342 struct CGroupInfo *cg;
343 int r;
344
345 assert(bus);
346 assert(unit);
347
348 if (flags & OUTPUT_FULL_WIDTH)
349 n_columns = 0;
350 else if (n_columns <= 0)
351 n_columns = columns();
352
353 prefix = strempty(prefix);
354
355 r = bus_call_method(
356 bus,
357 bus_systemd_mgr,
358 "GetUnitProcesses",
359 reterr_error,
360 &reply,
361 "s",
362 unit);
363 if (r < 0)
364 return r;
365
366 cgroups = hashmap_new(&path_hash_ops);
367 if (!cgroups)
368 return -ENOMEM;
369
370 r = sd_bus_message_enter_container(reply, 'a', "(sus)");
371 if (r < 0)
372 goto finish;
373
374 for (;;) {
375 const char *path = NULL, *name = NULL;
376 uint32_t pid;
377
378 r = sd_bus_message_read(reply, "(sus)", &path, &pid, &name);
379 if (r < 0)
380 goto finish;
381 if (r == 0)
382 break;
383
384 r = add_process(cgroups, path, pid, name);
385 if (r == -ENOMEM)
386 goto finish;
387 if (r < 0)
388 log_warning_errno(r, "Invalid process description in GetUnitProcesses reply: cgroup=\"%s\" pid=%u command=\"%s\", ignoring: %m",
389 path, pid, name);
390 }
391
392 r = sd_bus_message_exit_container(reply);
393 if (r < 0)
394 goto finish;
395
396 r = dump_processes(cgroups, cgroup_path, prefix, n_columns, flags);
397 if (r < 0)
398 goto finish;
399
400 if (!FLAGS_SET(flags, OUTPUT_HIDE_EXTRA)) {
401 r = dump_extra_processes(cgroups, prefix, n_columns, flags);
402 if (r < 0)
403 goto finish;
404 }
405
406 r = 0;
407
408 finish:
409 while ((cg = hashmap_first(cgroups)))
410 remove_cgroup(cgroups, cg);
411
412 hashmap_free(cgroups);
413
414 return r;
415 }