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 "string-util.h"
24 #include "terminal-util.h"
25 #include "unit-name.h"
27 static void show_pid_array(
36 unsigned i
, j
, pid_width
;
41 typesafe_qsort(pids
, n_pids
, pid_compare_func
);
43 /* Filter duplicates */
44 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
45 if (pids
[i
] == pids
[j
])
50 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
52 if (flags
& OUTPUT_FULL_WIDTH
)
55 if (n_columns
> pid_width
+2)
56 n_columns
-= pid_width
+2;
60 for (i
= 0; i
< n_pids
; i
++) {
61 _cleanup_free_
char *t
= NULL
;
63 (void) get_process_cmdline(pids
[i
], n_columns
, true, &t
);
66 printf("%s%s ", prefix
, special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
68 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? SPECIAL_GLYPH_TREE_BRANCH
: SPECIAL_GLYPH_TREE_RIGHT
)));
70 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
74 static int show_cgroup_one_by_path(
82 _cleanup_fclose_
FILE *f
= NULL
;
83 size_t n
= 0, n_allocated
= 0;
84 _cleanup_free_ pid_t
*pids
= NULL
;
85 _cleanup_free_
char *p
= NULL
;
89 r
= cg_mangle_path(path
, &p
);
93 fn
= strjoina(p
, "/cgroup.procs");
98 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
100 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
103 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
106 assert(n
< n_allocated
);
113 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
118 int show_cgroup_by_path(
124 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
125 _cleanup_closedir_
DIR *d
= NULL
;
127 bool shown_pids
= false;
133 n_columns
= columns();
135 prefix
= strempty(prefix
);
137 r
= cg_mangle_path(path
, &fn
);
145 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
146 _cleanup_free_
char *k
= NULL
;
148 k
= strjoin(fn
, "/", gn
);
153 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
157 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
162 printf("%s%s%s\n", prefix
, special_glyph(SPECIAL_GLYPH_TREE_BRANCH
), cg_unescape(basename(last
)));
165 p1
= strappend(prefix
, special_glyph(SPECIAL_GLYPH_TREE_VERTICAL
));
170 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
181 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
184 printf("%s%s%s\n", prefix
, special_glyph(SPECIAL_GLYPH_TREE_RIGHT
), cg_unescape(basename(last
)));
187 p2
= strappend(prefix
, " ");
192 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
198 int show_cgroup(const char *controller
,
203 _cleanup_free_
char *p
= NULL
;
208 r
= cg_get_path(controller
, path
, NULL
, &p
);
212 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
215 static int show_extra_pids(
216 const char *controller
,
224 _cleanup_free_ pid_t
*copy
= NULL
;
234 n_columns
= columns();
236 prefix
= strempty(prefix
);
238 copy
= new(pid_t
, n_pids
);
242 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
243 _cleanup_free_
char *k
= NULL
;
245 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
249 if (path_startswith(k
, path
))
255 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
260 int show_cgroup_and_extra(
261 const char *controller
,
265 const pid_t extra_pids
[],
266 unsigned n_extra_pids
,
273 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
277 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
280 int show_cgroup_get_unit_path_and_warn(
285 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
286 _cleanup_free_
char *path
= NULL
;
289 path
= unit_dbus_path_from_name(unit
);
293 r
= sd_bus_get_property_string(
295 "org.freedesktop.systemd1",
297 unit_dbus_interface_from_name(unit
),
302 return log_error_errno(r
, "Failed to query unit control group path: %s",
303 bus_error_message(&error
, r
));
308 int show_cgroup_get_path_and_warn(
314 _cleanup_free_
char *root
= NULL
;
317 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
318 _cleanup_free_
char *unit
= NULL
;
321 m
= strjoina("/run/systemd/machines/", machine
);
322 r
= parse_env_file(NULL
, m
, "SCOPE", &unit
);
324 return log_error_errno(r
, "Failed to load machine data: %m");
326 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
328 return log_error_errno(r
, "Failed to create bus connection: %m");
330 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &root
);
334 r
= cg_get_root_path(&root
);
336 return log_error_errno(r
, "Failed to get root control group path.\n"
337 "No cgroup filesystem mounted on /sys/fs/cgroup");
339 return log_error_errno(r
, "Failed to get root control group path: %m");
345 t
= strjoin(root
, prefix
);
351 *ret
= TAKE_PTR(root
);