1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "alloc-util.h"
29 #include "bus-error.h"
31 #include "cgroup-show.h"
32 #include "cgroup-util.h"
35 #include "format-util.h"
36 #include "locale-util.h"
38 #include "output-mode.h"
39 #include "path-util.h"
40 #include "process-util.h"
41 #include "string-util.h"
42 #include "terminal-util.h"
43 #include "unit-name.h"
45 static void show_pid_array(
54 unsigned i
, j
, pid_width
;
59 qsort(pids
, n_pids
, sizeof(pid_t
), pid_compare_func
);
61 /* Filter duplicates */
62 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
63 if (pids
[i
] == pids
[j
])
68 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
70 if (flags
& OUTPUT_FULL_WIDTH
)
73 if (n_columns
> pid_width
+2)
74 n_columns
-= pid_width
+2;
78 for (i
= 0; i
< n_pids
; i
++) {
79 _cleanup_free_
char *t
= NULL
;
81 (void) get_process_cmdline(pids
[i
], n_columns
, true, &t
);
84 printf("%s%s ", prefix
, special_glyph(TRIANGULAR_BULLET
));
86 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? TREE_BRANCH
: TREE_RIGHT
)));
88 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
92 static int show_cgroup_one_by_path(
100 _cleanup_fclose_
FILE *f
= NULL
;
101 size_t n
= 0, n_allocated
= 0;
102 _cleanup_free_ pid_t
*pids
= NULL
;
103 _cleanup_free_
char *p
= NULL
;
107 r
= cg_mangle_path(path
, &p
);
111 fn
= strjoina(p
, "/cgroup.procs");
116 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
118 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
121 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
124 assert(n
< n_allocated
);
131 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
136 int show_cgroup_by_path(
142 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
143 _cleanup_closedir_
DIR *d
= NULL
;
145 bool shown_pids
= false;
151 n_columns
= columns();
153 prefix
= strempty(prefix
);
155 r
= cg_mangle_path(path
, &fn
);
163 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
164 _cleanup_free_
char *k
= NULL
;
166 k
= strjoin(fn
, "/", gn
);
171 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
175 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
180 printf("%s%s%s\n", prefix
, special_glyph(TREE_BRANCH
), cg_unescape(basename(last
)));
183 p1
= strappend(prefix
, special_glyph(TREE_VERTICAL
));
188 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
199 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
202 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), cg_unescape(basename(last
)));
205 p2
= strappend(prefix
, " ");
210 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
216 int show_cgroup(const char *controller
,
221 _cleanup_free_
char *p
= NULL
;
226 r
= cg_get_path(controller
, path
, NULL
, &p
);
230 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
233 static int show_extra_pids(
234 const char *controller
,
242 _cleanup_free_ pid_t
*copy
= NULL
;
252 n_columns
= columns();
254 prefix
= strempty(prefix
);
256 copy
= new(pid_t
, n_pids
);
260 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
261 _cleanup_free_
char *k
= NULL
;
263 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
267 if (path_startswith(k
, path
))
273 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
278 int show_cgroup_and_extra(
279 const char *controller
,
283 const pid_t extra_pids
[],
284 unsigned n_extra_pids
,
291 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
295 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
298 int show_cgroup_and_extra_by_spec(
302 const pid_t extra_pids
[],
303 unsigned n_extra_pids
,
306 _cleanup_free_
char *controller
= NULL
, *path
= NULL
;
311 r
= cg_split_spec(spec
, &controller
, &path
);
315 return show_cgroup_and_extra(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
318 int show_cgroup_get_unit_path_and_warn(
323 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
324 _cleanup_free_
char *path
= NULL
;
327 path
= unit_dbus_path_from_name(unit
);
331 r
= sd_bus_get_property_string(
333 "org.freedesktop.systemd1",
335 unit_dbus_interface_from_name(unit
),
340 return log_error_errno(r
, "Failed to query unit control group path: %s",
341 bus_error_message(&error
, r
));
346 int show_cgroup_get_path_and_warn(
352 _cleanup_free_
char *root
= NULL
;
355 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
356 _cleanup_free_
char *unit
= NULL
;
359 m
= strjoina("/run/systemd/machines/", machine
);
360 r
= parse_env_file(m
, NEWLINE
, "SCOPE", &unit
, NULL
);
362 return log_error_errno(r
, "Failed to load machine data: %m");
364 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
366 return log_error_errno(r
, "Failed to create bus connection: %m");
368 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &root
);
372 r
= cg_get_root_path(&root
);
374 return log_error_errno(r
, "Failed to get root control group path.\n"
375 "No cgroup filesystem mounted on /sys/fs/cgroup");
377 return log_error_errno(r
, "Failed to get root control group path: %m");
383 t
= strjoin(root
, prefix
);