1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "bus-error.h"
13 #include "cgroup-show.h"
14 #include "cgroup-util.h"
17 #include "format-util.h"
18 #include "locale-util.h"
20 #include "output-mode.h"
21 #include "path-util.h"
22 #include "process-util.h"
23 #include "sort-util.h"
24 #include "string-util.h"
25 #include "terminal-util.h"
26 #include "unit-name.h"
28 static void show_pid_array(
37 unsigned i
, j
, pid_width
;
42 typesafe_qsort(pids
, n_pids
, pid_compare_func
);
44 /* Filter duplicates */
45 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
46 if (pids
[i
] == pids
[j
])
51 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
53 if (flags
& OUTPUT_FULL_WIDTH
)
56 if (n_columns
> pid_width
+2)
57 n_columns
-= pid_width
+2;
61 for (i
= 0; i
< n_pids
; i
++) {
62 _cleanup_free_
char *t
= NULL
;
64 (void) get_process_cmdline(pids
[i
], n_columns
, true, &t
);
67 printf("%s%s ", prefix
, special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
69 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? SPECIAL_GLYPH_TREE_BRANCH
: SPECIAL_GLYPH_TREE_RIGHT
)));
71 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
75 static int show_cgroup_one_by_path(
83 _cleanup_fclose_
FILE *f
= NULL
;
84 size_t n
= 0, n_allocated
= 0;
85 _cleanup_free_ pid_t
*pids
= NULL
;
86 _cleanup_free_
char *p
= NULL
;
90 r
= cg_mangle_path(path
, &p
);
94 fn
= strjoina(p
, "/cgroup.procs");
99 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
101 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
104 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
107 assert(n
< n_allocated
);
114 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
119 int show_cgroup_by_path(
125 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
126 _cleanup_closedir_
DIR *d
= NULL
;
128 bool shown_pids
= false;
134 n_columns
= columns();
136 prefix
= strempty(prefix
);
138 r
= cg_mangle_path(path
, &fn
);
146 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
147 _cleanup_free_
char *k
= NULL
;
149 k
= strjoin(fn
, "/", gn
);
154 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
158 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
163 printf("%s%s%s\n", prefix
, special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), cg_unescape(basename(last
)));
166 p1
= strappend(prefix
, special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
171 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
182 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
185 printf("%s%s%s\n", prefix
, special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), cg_unescape(basename(last
)));
188 p2
= strappend(prefix
, " ");
193 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
199 int show_cgroup(const char *controller
,
204 _cleanup_free_
char *p
= NULL
;
209 r
= cg_get_path(controller
, path
, NULL
, &p
);
213 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
216 static int show_extra_pids(
217 const char *controller
,
225 _cleanup_free_ pid_t
*copy
= NULL
;
235 n_columns
= columns();
237 prefix
= strempty(prefix
);
239 copy
= new(pid_t
, n_pids
);
243 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
244 _cleanup_free_
char *k
= NULL
;
246 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
250 if (path_startswith(k
, path
))
256 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
261 int show_cgroup_and_extra(
262 const char *controller
,
266 const pid_t extra_pids
[],
267 unsigned n_extra_pids
,
274 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
278 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
281 int show_cgroup_get_unit_path_and_warn(
286 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
287 _cleanup_free_
char *path
= NULL
;
290 path
= unit_dbus_path_from_name(unit
);
294 r
= sd_bus_get_property_string(
296 "org.freedesktop.systemd1",
298 unit_dbus_interface_from_name(unit
),
303 return log_error_errno(r
, "Failed to query unit control group path: %s",
304 bus_error_message(&error
, r
));
309 int show_cgroup_get_path_and_warn(
315 _cleanup_free_
char *root
= NULL
;
318 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
319 _cleanup_free_
char *unit
= NULL
;
322 m
= strjoina("/run/systemd/machines/", machine
);
323 r
= parse_env_file(NULL
, m
, "SCOPE", &unit
);
325 return log_error_errno(r
, "Failed to load machine data: %m");
327 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
329 return log_error_errno(r
, "Failed to create bus connection: %m");
331 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &root
);
335 r
= cg_get_root_path(&root
);
337 return log_error_errno(r
, "Failed to get root control group path.\n"
338 "No cgroup filesystem mounted on /sys/fs/cgroup");
340 return log_error_errno(r
, "Failed to get root control group path: %m");
346 t
= strjoin(root
, prefix
);
352 *ret
= TAKE_PTR(root
);