Debian Codegraph @

src/shared/cgroup-show.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <dirent.h>
4 #include <stdio.h>
5
6 #include "sd-bus.h"
7
8 #include "alloc-util.h"
9 #include "ansi-color.h"
10 #include "bus-error.h"
11 #include "bus-util.h"
12 #include "cgroup-show.h"
13 #include "cgroup-util.h"
14 #include "env-file.h"
15 #include "escape.h"
16 #include "fd-util.h"
17 #include "format-util.h"
18 #include "glyph-util.h"
19 #include "hostname-util.h"
20 #include "log.h"
21 #include "mountpoint-util.h"
22 #include "nulstr-util.h"
23 #include "output-mode.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "runtime-scope.h"
27 #include "sort-util.h"
28 #include "string-util.h"
29 #include "terminal-util.h"
30 #include "unit-def.h"
31 #include "xattr-util.h"
32
33 static void show_pid_array(
34 pid_t pids[],
35 size_t n_pids,
36 const char *prefix,
37 size_t n_columns,
38 bool extra,
39 bool more,
40 OutputFlags flags) {
41
42 size_t i, j, pid_width;
43
44 if (n_pids == 0)
45 return;
46
47 typesafe_qsort(pids, n_pids, pid_compare_func);
48
49 /* Filter duplicates */
50 for (j = 0, i = 1; i < n_pids; i++) {
51 if (pids[i] == pids[j])
52 continue;
53 pids[++j] = pids[i];
54 }
55 n_pids = j + 1;
56 pid_width = DECIMAL_STR_WIDTH(pids[j]);
57
58 if (flags & OUTPUT_FULL_WIDTH)
59 n_columns = SIZE_MAX;
60 else {
61 if (n_columns > pid_width + 3) /* something like "├─1114784 " */
62 n_columns -= pid_width + 3;
63 else
64 n_columns = 20;
65 }
66 for (i = 0; i < n_pids; i++) {
67 _cleanup_free_ char *t = NULL;
68
69 (void) pid_get_cmdline(pids[i], n_columns,
70 PROCESS_CMDLINE_COMM_FALLBACK | PROCESS_CMDLINE_USE_LOCALE,
71 &t);
72
73 if (extra)
74 printf("%s%s ", prefix, glyph(GLYPH_TRIANGULAR_BULLET));
75 else
76 printf("%s%s", prefix, glyph(((more || i < n_pids-1) ? GLYPH_TREE_BRANCH : GLYPH_TREE_RIGHT)));
77
78 printf("%s%*"PID_PRI" %s%s\n", ansi_grey(), (int) pid_width, pids[i], strna(t), ansi_normal());
79 }
80 }
81
82 static int show_cgroup_one_by_path(
83 const char *path,
84 const char *prefix,
85 size_t n_columns,
86 bool more,
87 OutputFlags flags) {
88
89 _cleanup_free_ pid_t *pids = NULL;
90 _cleanup_fclose_ FILE *f = NULL;
91 size_t n = 0;
92 char *fn;
93 int r;
94
95 assert(path);
96
97 fn = strjoina(path, "/cgroup.procs");
98 f = fopen(fn, "re");
99 if (!f)
100 return -errno;
101
102 for (;;) {
103 pid_t pid;
104
105 /* libvirt / qemu uses threaded mode and cgroup.procs cannot be read at the lower levels.
106 * From https://docs.kernel.org/admin-guide/cgroup-v2.html#threads,
107 * “cgroup.procs” in a threaded domain cgroup contains the PIDs of all processes in
108 * the subtree and is not readable in the subtree proper.
109 *
110 * ENODEV is generated when we enumerate processes from a cgroup and the cgroup is removed
111 * concurrently. */
112 r = cg_read_pid(f, &pid, /* flags= */ 0);
113 if (IN_SET(r, 0, -EOPNOTSUPP, -ENODEV))
114 break;
115 if (r < 0)
116 return r;
117
118 if (!(flags & OUTPUT_KERNEL_THREADS) && pid_is_kernel_thread(pid) > 0)
119 continue;
120
121 if (!GREEDY_REALLOC(pids, n + 1))
122 return -ENOMEM;
123
124 pids[n++] = pid;
125 }
126
127 show_pid_array(pids, n, prefix, n_columns, false, more, flags);
128
129 return 0;
130 }
131
132 static int show_cgroup_name(
133 const char *path,
134 const char *prefix,
135 Glyph tree,
136 OutputFlags flags) {
137
138 uint64_t cgroupid = UINT64_MAX;
139 _cleanup_free_ char *b = NULL;
140 _cleanup_close_ int fd = -EBADF;
141 bool delegate;
142 int r;
143
144 fd = open(path, O_PATH|O_CLOEXEC|O_NOFOLLOW|O_DIRECTORY, 0);
145 if (fd < 0)
146 return log_debug_errno(errno, "Failed to open cgroup '%s', ignoring: %m", path);
147
148 r = cg_is_delegated_fd(fd);
149 if (r < 0)
150 log_debug_errno(r, "Failed to check if cgroup is delegated, ignoring: %m");
151 delegate = r > 0;
152
153 if (FLAGS_SET(flags, OUTPUT_CGROUP_ID)) {
154 r = fd_to_handle_u64(fd, &cgroupid);
155 if (r < 0)
156 log_debug_errno(r, "Failed to determine cgroup ID of %s, ignoring: %m", path);
157 }
158
159 r = path_extract_filename(path, &b);
160 if (r < 0)
161 return log_error_errno(r, "Failed to extract filename from cgroup path: %m");
162
163 printf("%s%s%s%s%s",
164 prefix, glyph(tree),
165 delegate ? ansi_underline() : "",
166 cg_unescape(b),
167 delegate ? ansi_normal() : "");
168
169 if (delegate)
170 printf(" %s%s%s",
171 ansi_highlight(),
172 glyph(GLYPH_ELLIPSIS),
173 ansi_normal());
174
175 if (cgroupid != UINT64_MAX)
176 printf(" %s(#%" PRIu64 ")%s", ansi_grey(), cgroupid, ansi_normal());
177
178 printf("\n");
179
180 if (FLAGS_SET(flags, OUTPUT_CGROUP_XATTRS)) {
181 _cleanup_free_ char *nl = NULL;
182
183 r = flistxattr_malloc(fd, &nl);
184 if (r < 0)
185 log_debug_errno(r, "Failed to enumerate xattrs on '%s', ignoring: %m", path);
186
187 NULSTR_FOREACH(xa, nl) {
188 _cleanup_free_ char *x = NULL, *y = NULL, *buf = NULL;
189
190 if (!STARTSWITH_SET(xa, "user.", "trusted."))
191 continue;
192
193 size_t buf_size;
194 r = fgetxattr_malloc(fd, xa, &buf, &buf_size);
195 if (r < 0) {
196 log_debug_errno(r, "Failed to read xattr '%s' off '%s', ignoring: %m", xa, path);
197 continue;
198 }
199
200 x = cescape(xa);
201 if (!x)
202 return -ENOMEM;
203
204 y = cescape_length(buf, buf_size);
205 if (!y)
206 return -ENOMEM;
207
208 printf("%s%s%s %s%s%s: %s\n",
209 prefix,
210 tree == GLYPH_TREE_BRANCH ? glyph(GLYPH_TREE_VERTICAL) : " ",
211 glyph(GLYPH_ARROW_RIGHT),
212 ansi_blue(), x, ansi_normal(),
213 y);
214 }
215 }
216
217 return 0;
218 }
219
220 int show_cgroup_by_path(
221 const char *path,
222 const char *prefix,
223 size_t n_columns,
224 OutputFlags flags) {
225
226 _cleanup_free_ char *p1 = NULL, *last = NULL, *p2 = NULL;
227 _cleanup_closedir_ DIR *d = NULL;
228 bool shown_pids = false;
229 char *gn = NULL;
230 int r;
231
232 assert(path);
233
234 if (n_columns <= 0)
235 n_columns = columns();
236
237 prefix = strempty(prefix);
238
239 d = opendir(path);
240 if (!d)
241 return -errno;
242
243 while ((r = cg_read_subgroup(d, &gn)) > 0) {
244 _cleanup_free_ char *k = NULL;
245
246 k = path_join(path, gn);
247 free(gn);
248 if (!k)
249 return -ENOMEM;
250
251 if (!(flags & OUTPUT_SHOW_ALL) && cg_is_empty(path_startswith(k, "/sys/fs/cgroup/")) > 0)
252 continue;
253
254 if (!shown_pids) {
255 (void) show_cgroup_one_by_path(path, prefix, n_columns, true, flags);
256 shown_pids = true;
257 }
258
259 if (last) {
260 r = show_cgroup_name(last, prefix, GLYPH_TREE_BRANCH, flags);
261 if (r < 0)
262 return r;
263
264 if (!p1) {
265 p1 = strjoin(prefix, glyph(GLYPH_TREE_VERTICAL));
266 if (!p1)
267 return -ENOMEM;
268 }
269
270 show_cgroup_by_path(last, p1, n_columns-2, flags);
271 free(last);
272 }
273
274 last = TAKE_PTR(k);
275 }
276
277 if (r < 0)
278 return r;
279
280 if (!shown_pids)
281 (void) show_cgroup_one_by_path(path, prefix, n_columns, !!last, flags);
282
283 if (last) {
284 r = show_cgroup_name(last, prefix, GLYPH_TREE_RIGHT, flags);
285 if (r < 0)
286 return r;
287
288 if (!p2) {
289 p2 = strjoin(prefix, " ");
290 if (!p2)
291 return -ENOMEM;
292 }
293
294 show_cgroup_by_path(last, p2, n_columns-2, flags);
295 }
296
297 return 0;
298 }
299
300 int show_cgroup(const char *path,
301 const char *prefix,
302 size_t n_columns,
303 OutputFlags flags) {
304
305 _cleanup_free_ char *p = NULL;
306 int r;
307
308 assert(path);
309
310 r = cg_get_path(path, /* suffix= */ NULL, &p);
311 if (r < 0)
312 return r;
313
314 return show_cgroup_by_path(p, prefix, n_columns, flags);
315 }
316
317 static int show_extra_pids(
318 const char *path,
319 const char *prefix,
320 size_t n_columns,
321 const pid_t pids[],
322 size_t n_pids,
323 OutputFlags flags) {
324
325 _cleanup_free_ pid_t *copy = NULL;
326 size_t i, j;
327 int r;
328
329 assert(path);
330
331 if (n_pids <= 0)
332 return 0;
333
334 if (n_columns <= 0)
335 n_columns = columns();
336
337 prefix = strempty(prefix);
338
339 copy = new(pid_t, n_pids);
340 if (!copy)
341 return -ENOMEM;
342
343 for (i = 0, j = 0; i < n_pids; i++) {
344 _cleanup_free_ char *k = NULL;
345
346 r = cg_pid_get_path(pids[i], &k);
347 if (r < 0)
348 return r;
349
350 if (path_startswith(k, path))
351 continue;
352
353 copy[j++] = pids[i];
354 }
355
356 show_pid_array(copy, j, prefix, n_columns, true, false, flags);
357
358 return 0;
359 }
360
361 int show_cgroup_and_extra(
362 const char *path,
363 const char *prefix,
364 size_t n_columns,
365 const pid_t extra_pids[],
366 size_t n_extra_pids,
367 OutputFlags flags) {
368
369 int r;
370
371 assert(path);
372
373 r = show_cgroup(path, prefix, n_columns, flags);
374 if (r < 0)
375 return r;
376
377 return show_extra_pids(path, prefix, n_columns, extra_pids, n_extra_pids, flags);
378 }
379
380 int show_cgroup_get_unit_path_and_warn(
381 sd_bus *bus,
382 const char *unit,
383 char **ret) {
384
385 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
386 _cleanup_free_ char *path = NULL;
387 int r;
388
389 path = unit_dbus_path_from_name(unit);
390 if (!path)
391 return log_oom();
392
393 r = sd_bus_get_property_string(
394 bus,
395 "org.freedesktop.systemd1",
396 path,
397 unit_dbus_interface_from_name(unit),
398 "ControlGroup",
399 &error,
400 ret);
401 if (r < 0)
402 return log_error_errno(r, "Failed to query unit control group path: %s",
403 bus_error_message(&error, r));
404
405 return 0;
406 }
407
408 int show_cgroup_get_path_and_warn(
409 const char *machine,
410 const char *prefix,
411 char **ret) {
412
413 _cleanup_free_ char *root = NULL;
414 int r;
415
416 assert(ret);
417
418 if (machine) {
419 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
420 _cleanup_free_ char *unit = NULL;
421 const char *m;
422
423 if (!hostname_is_valid(machine, 0))
424 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Machine name is not valid: %s", machine);
425
426 m = strjoina("/run/systemd/machines/", machine);
427 r = parse_env_file(NULL, m, "SCOPE", &unit);
428 if (r < 0)
429 return log_error_errno(r, "Failed to load machine data: %m");
430
431 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, &bus);
432 if (r < 0)
433 return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
434
435 r = show_cgroup_get_unit_path_and_warn(bus, unit, &root);
436 if (r < 0)
437 return r;
438 } else {
439 r = cg_get_root_path(&root);
440 if (r == -ENOMEDIUM)
441 return log_error_errno(r, "Failed to get root control group path.\n"
442 "No cgroup filesystem mounted on /sys/fs/cgroup");
443 if (r < 0)
444 return log_error_errno(r, "Failed to get root control group path: %m");
445 }
446
447 if (prefix) {
448 char *t;
449
450 t = path_join(root, prefix);
451 if (!t)
452 return log_oom();
453
454 *ret = t;
455 } else
456 *ret = TAKE_PTR(root);
457
458 return 0;
459 }