2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <systemd/sd-bus.h>
29 #include "alloc-util.h"
30 #include "bus-error.h"
32 #include "cgroup-show.h"
33 #include "cgroup-util.h"
36 #include "format-util.h"
37 #include "locale-util.h"
39 #include "output-mode.h"
40 #include "path-util.h"
41 #include "process-util.h"
42 #include "string-util.h"
43 #include "terminal-util.h"
44 #include "unit-name.h"
46 static void show_pid_array(
55 unsigned i
, j
, pid_width
;
60 qsort(pids
, n_pids
, sizeof(pid_t
), pid_compare_func
);
62 /* Filter duplicates */
63 for (j
= 0, i
= 1; i
< n_pids
; i
++) {
64 if (pids
[i
] == pids
[j
])
69 pid_width
= DECIMAL_STR_WIDTH(pids
[j
]);
71 if (flags
& OUTPUT_FULL_WIDTH
)
74 if (n_columns
> pid_width
+2)
75 n_columns
-= pid_width
+2;
79 for (i
= 0; i
< n_pids
; i
++) {
80 _cleanup_free_
char *t
= NULL
;
82 (void) get_process_cmdline(pids
[i
], n_columns
, true, &t
);
85 printf("%s%s ", prefix
, special_glyph(TRIANGULAR_BULLET
));
87 printf("%s%s", prefix
, special_glyph(((more
|| i
< n_pids
-1) ? TREE_BRANCH
: TREE_RIGHT
)));
89 printf("%*"PID_PRI
" %s\n", pid_width
, pids
[i
], strna(t
));
93 static int show_cgroup_one_by_path(
101 _cleanup_fclose_
FILE *f
= NULL
;
102 size_t n
= 0, n_allocated
= 0;
103 _cleanup_free_ pid_t
*pids
= NULL
;
104 _cleanup_free_
char *p
= NULL
;
108 r
= cg_mangle_path(path
, &p
);
112 fn
= strjoina(p
, "/cgroup.procs");
117 while ((r
= cg_read_pid(f
, &pid
)) > 0) {
119 if (!(flags
& OUTPUT_KERNEL_THREADS
) && is_kernel_thread(pid
) > 0)
122 if (!GREEDY_REALLOC(pids
, n_allocated
, n
+ 1))
125 assert(n
< n_allocated
);
132 show_pid_array(pids
, n
, prefix
, n_columns
, false, more
, flags
);
137 int show_cgroup_by_path(
143 _cleanup_free_
char *fn
= NULL
, *p1
= NULL
, *last
= NULL
, *p2
= NULL
;
144 _cleanup_closedir_
DIR *d
= NULL
;
146 bool shown_pids
= false;
152 n_columns
= columns();
154 prefix
= strempty(prefix
);
156 r
= cg_mangle_path(path
, &fn
);
164 while ((r
= cg_read_subgroup(d
, &gn
)) > 0) {
165 _cleanup_free_
char *k
= NULL
;
167 k
= strjoin(fn
, "/", gn
);
172 if (!(flags
& OUTPUT_SHOW_ALL
) && cg_is_empty_recursive(NULL
, k
) > 0)
176 show_cgroup_one_by_path(path
, prefix
, n_columns
, true, flags
);
181 printf("%s%s%s\n", prefix
, special_glyph(TREE_BRANCH
), cg_unescape(basename(last
)));
184 p1
= strappend(prefix
, special_glyph(TREE_VERTICAL
));
189 show_cgroup_by_path(last
, p1
, n_columns
-2, flags
);
201 show_cgroup_one_by_path(path
, prefix
, n_columns
, !!last
, flags
);
204 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), cg_unescape(basename(last
)));
207 p2
= strappend(prefix
, " ");
212 show_cgroup_by_path(last
, p2
, n_columns
-2, flags
);
218 int show_cgroup(const char *controller
,
223 _cleanup_free_
char *p
= NULL
;
228 r
= cg_get_path(controller
, path
, NULL
, &p
);
232 return show_cgroup_by_path(p
, prefix
, n_columns
, flags
);
235 static int show_extra_pids(
236 const char *controller
,
244 _cleanup_free_ pid_t
*copy
= NULL
;
254 n_columns
= columns();
256 prefix
= strempty(prefix
);
258 copy
= new(pid_t
, n_pids
);
262 for (i
= 0, j
= 0; i
< n_pids
; i
++) {
263 _cleanup_free_
char *k
= NULL
;
265 r
= cg_pid_get_path(controller
, pids
[i
], &k
);
269 if (path_startswith(k
, path
))
275 show_pid_array(copy
, j
, prefix
, n_columns
, true, false, flags
);
280 int show_cgroup_and_extra(
281 const char *controller
,
285 const pid_t extra_pids
[],
286 unsigned n_extra_pids
,
293 r
= show_cgroup(controller
, path
, prefix
, n_columns
, flags
);
297 return show_extra_pids(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
300 int show_cgroup_and_extra_by_spec(
304 const pid_t extra_pids
[],
305 unsigned n_extra_pids
,
308 _cleanup_free_
char *controller
= NULL
, *path
= NULL
;
313 r
= cg_split_spec(spec
, &controller
, &path
);
317 return show_cgroup_and_extra(controller
, path
, prefix
, n_columns
, extra_pids
, n_extra_pids
, flags
);
320 int show_cgroup_get_unit_path_and_warn(
325 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
326 _cleanup_free_
char *path
= NULL
;
329 path
= unit_dbus_path_from_name(unit
);
333 r
= sd_bus_get_property_string(
335 "org.freedesktop.systemd1",
337 unit_dbus_interface_from_name(unit
),
342 return log_error_errno(r
, "Failed to query unit control group path: %s",
343 bus_error_message(&error
, r
));
348 int show_cgroup_get_path_and_warn(
354 _cleanup_free_
char *root
= NULL
;
357 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
358 _cleanup_free_
char *unit
= NULL
;
361 m
= strjoina("/run/systemd/machines/", machine
);
362 r
= parse_env_file(m
, NEWLINE
, "SCOPE", &unit
, NULL
);
364 return log_error_errno(r
, "Failed to load machine data: %m");
366 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
368 return log_error_errno(r
, "Failed to create bus connection: %m");
370 r
= show_cgroup_get_unit_path_and_warn(bus
, unit
, &root
);
374 r
= cg_get_root_path(&root
);
376 return log_error_errno(r
, "Failed to get root control group path.\n"
377 "No cgroup filesystem mounted on /sys/fs/cgroup");
379 return log_error_errno(r
, "Failed to get root control group path: %m");
385 t
= strjoin(root
, prefix
);