1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
13 #include "alloc-util.h"
14 #include "bus-error.h"
16 #include "cgroup-show.h"
17 #include "cgroup-util.h"
20 #include "format-util.h"
21 #include "locale-util.h"
23 #include "output-mode.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "string-util.h"
27 #include "terminal-util.h"
28 #include "unit-name.h"
30 static void show_pid_array(
39 unsigned i
, j
, pid_width
;
44 qsort(pids
, n_pids
, sizeof(pid_t
), pid_compare_func
);
46 /* Filter duplicates */
47 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
48 if (pids
[i
] == pids
[j
])
53 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
55 if (flags
& OUTPUT_FULL_WIDTH
)
58 if (n_columns
> pid_width
+2)
59 n_columns
-= pid_width
+2;
63 for (i
= 0; i
< n_pids
; i
++) {
64 _cleanup_free_
char *t
= NULL
;
66 (void) get_process_cmdline(pids
[i
], n_columns
, true, &t
);
69 printf("%s%s ", prefix
, special_glyph(TRIANGULAR_BULLET
));
71 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? TREE_BRANCH
: TREE_RIGHT
)));
73 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
77 static int show_cgroup_one_by_path(
85 _cleanup_fclose_
FILE *f
= NULL
;
86 size_t n
= 0, n_allocated
= 0;
87 _cleanup_free_ pid_t
*pids
= NULL
;
88 _cleanup_free_
char *p
= NULL
;
92 r
= cg_mangle_path(path
, &p
);
96 fn
= strjoina(p
, "/cgroup.procs");
101 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
103 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
106 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
109 assert(n
< n_allocated
);
116 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
121 int show_cgroup_by_path(
127 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
128 _cleanup_closedir_
DIR *d
= NULL
;
130 bool shown_pids
= false;
136 n_columns
= columns();
138 prefix
= strempty(prefix
);
140 r
= cg_mangle_path(path
, &fn
);
148 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
149 _cleanup_free_
char *k
= NULL
;
151 k
= strjoin(fn
, "/", gn
);
156 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
160 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
165 printf("%s%s%s\n", prefix
, special_glyph(TREE_BRANCH
), cg_unescape(basename(last
)));
168 p1
= strappend(prefix
, special_glyph(TREE_VERTICAL
));
173 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
184 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
187 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), cg_unescape(basename(last
)));
190 p2
= strappend(prefix
, " ");
195 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
201 int show_cgroup(const char *controller
,
206 _cleanup_free_
char *p
= NULL
;
211 r
= cg_get_path(controller
, path
, NULL
, &p
);
215 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
218 static int show_extra_pids(
219 const char *controller
,
227 _cleanup_free_ pid_t
*copy
= NULL
;
237 n_columns
= columns();
239 prefix
= strempty(prefix
);
241 copy
= new(pid_t
, n_pids
);
245 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
246 _cleanup_free_
char *k
= NULL
;
248 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
252 if (path_startswith(k
, path
))
258 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
263 int show_cgroup_and_extra(
264 const char *controller
,
268 const pid_t extra_pids
[],
269 unsigned n_extra_pids
,
276 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
280 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
283 int show_cgroup_and_extra_by_spec(
287 const pid_t extra_pids
[],
288 unsigned n_extra_pids
,
291 _cleanup_free_
char *controller
= NULL
, *path
= NULL
;
296 r
= cg_split_spec(spec
, &controller
, &path
);
300 return show_cgroup_and_extra(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
303 int show_cgroup_get_unit_path_and_warn(
308 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
309 _cleanup_free_
char *path
= NULL
;
312 path
= unit_dbus_path_from_name(unit
);
316 r
= sd_bus_get_property_string(
318 "org.freedesktop.systemd1",
320 unit_dbus_interface_from_name(unit
),
325 return log_error_errno(r
, "Failed to query unit control group path: %s",
326 bus_error_message(&error
, r
));
331 int show_cgroup_get_path_and_warn(
337 _cleanup_free_
char *root
= NULL
;
340 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
341 _cleanup_free_
char *unit
= NULL
;
344 m
= strjoina("/run/systemd/machines/", machine
);
345 r
= parse_env_file(NULL
, m
, NEWLINE
, "SCOPE", &unit
, NULL
);
347 return log_error_errno(r
, "Failed to load machine data: %m");
349 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
351 return log_error_errno(r
, "Failed to create bus connection: %m");
353 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &root
);
357 r
= cg_get_root_path(&root
);
359 return log_error_errno(r
, "Failed to get root control group path.\n"
360 "No cgroup filesystem mounted on /sys/fs/cgroup");
362 return log_error_errno(r
, "Failed to get root control group path: %m");
368 t
= strjoin(root
, prefix
);
374 *ret
= TAKE_PTR(root
);