1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include "alloc-util.h"
18 #include "cgroup-show.h"
19 #include "cgroup-util.h"
22 #include "output-mode.h"
24 #include "path-util.h"
26 #include "unit-name.h"
29 static bool arg_no_pager
= false;
30 static bool arg_kernel_threads
= false;
31 static bool arg_all
= false;
37 } arg_show_unit
= SHOW_UNIT_NONE
;
38 static char **arg_names
= NULL
;
40 static int arg_full
= -1;
41 static char* arg_machine
= NULL
;
43 static void help(void) {
44 printf("%s [OPTIONS...] [CGROUP...]\n\n"
45 "Recursively show control group contents.\n\n"
46 " -h --help Show this help\n"
47 " --version Show package version\n"
48 " --no-pager Do not pipe output into a pager\n"
49 " -a --all Show all groups, including empty\n"
50 " -u --unit Show the subtrees of specifified system units\n"
51 " --user-unit Show the subtrees of specifified user units\n"
52 " -l --full Do not ellipsize output\n"
53 " -k Include kernel threads in output\n"
54 " -M --machine= Show container\n"
55 , program_invocation_short_name
);
58 static int parse_argv(int argc
, char *argv
[]) {
66 static const struct option options
[] = {
67 { "help", no_argument
, NULL
, 'h' },
68 { "version", no_argument
, NULL
, ARG_VERSION
},
69 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
70 { "all", no_argument
, NULL
, 'a' },
71 { "full", no_argument
, NULL
, 'l' },
72 { "machine", required_argument
, NULL
, 'M' },
73 { "unit", optional_argument
, NULL
, 'u' },
74 { "user-unit", optional_argument
, NULL
, ARG_USER_UNIT
},
83 while ((c
= getopt_long(argc
, argv
, "-hkalM:u::", options
, NULL
)) >= 0)
103 arg_show_unit
= SHOW_UNIT_SYSTEM
;
104 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
109 arg_show_unit
= SHOW_UNIT_USER
;
110 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
115 /* positional argument */
116 if (strv_push(&arg_names
, optarg
) < 0)
125 arg_kernel_threads
= true;
129 arg_machine
= optarg
;
136 assert_not_reached("Unhandled option");
139 if (arg_machine
&& arg_show_unit
!= SHOW_UNIT_NONE
) {
140 log_error("Cannot combine --unit or --user-unit with --machine=.");
147 static void show_cg_info(const char *controller
, const char *path
) {
149 if (cg_all_unified() == 0 && controller
&& !streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
150 printf("Controller %s; ", controller
);
152 printf("Control group %s:\n", empty_to_root(path
));
156 int main(int argc
, char *argv
[]) {
159 log_parse_environment();
162 r
= parse_argv(argc
, argv
);
166 r
= pager_open(arg_no_pager
, false);
167 if (r
> 0 && arg_full
< 0)
171 arg_all
* OUTPUT_SHOW_ALL
|
172 (arg_full
> 0) * OUTPUT_FULL_WIDTH
|
173 arg_kernel_threads
* OUTPUT_KERNEL_THREADS
;
176 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
177 _cleanup_free_
char *root
= NULL
;
180 STRV_FOREACH(name
, arg_names
) {
183 if (arg_show_unit
!= SHOW_UNIT_NONE
) {
184 /* Command line arguments are unit names */
185 _cleanup_free_
char *cgroup
= NULL
;
188 /* Connect to the bus only if necessary */
189 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
,
190 arg_show_unit
== SHOW_UNIT_USER
,
193 log_error_errno(r
, "Failed to create bus connection: %m");
198 q
= show_cgroup_get_unit_path_and_warn(bus
, *name
, &cgroup
);
202 if (isempty(cgroup
)) {
203 log_warning("Unit %s not found.", *name
);
208 printf("Unit %s (%s):\n", *name
, cgroup
);
211 q
= show_cgroup_by_path(cgroup
, NULL
, 0, output_flags
);
213 } else if (path_startswith(*name
, "/sys/fs/cgroup")) {
215 printf("Directory %s:\n", *name
);
218 q
= show_cgroup_by_path(*name
, NULL
, 0, output_flags
);
220 _cleanup_free_
char *c
= NULL
, *p
= NULL
, *j
= NULL
;
221 const char *controller
, *path
;
224 /* Query root only if needed, treat error as fatal */
225 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
230 q
= cg_split_spec(*name
, &c
, &p
);
232 log_error_errno(q
, "Failed to split argument %s: %m", *name
);
236 controller
= c
?: SYSTEMD_CGROUP_CONTROLLER
;
238 j
= strjoin(root
, "/", p
);
244 path_simplify(j
, false);
249 show_cg_info(controller
, path
);
251 q
= show_cgroup(controller
, path
, NULL
, 0, output_flags
);
263 _cleanup_free_
char *cwd
= NULL
;
265 r
= safe_getcwd(&cwd
);
267 log_error_errno(r
, "Cannot determine current working directory: %m");
271 if (path_startswith(cwd
, "/sys/fs/cgroup")) {
272 printf("Working directory %s:\n", cwd
);
275 r
= show_cgroup_by_path(cwd
, NULL
, 0, output_flags
);
281 _cleanup_free_
char *root
= NULL
;
283 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
287 show_cg_info(SYSTEMD_CGROUP_CONTROLLER
, root
);
290 r
= show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, root
, NULL
, 0, output_flags
);
295 log_error_errno(r
, "Failed to list cgroup tree: %m");
299 free(arg_names
); /* don't free the strings */
301 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;