1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
13 #include "cgroup-show.h"
14 #include "cgroup-util.h"
17 #include "main-func.h"
18 #include "output-mode.h"
20 #include "path-util.h"
21 #include "pretty-print.h"
23 #include "unit-name.h"
26 static PagerFlags arg_pager_flags
= 0;
27 static bool arg_kernel_threads
= false;
28 static bool arg_all
= false;
34 } arg_show_unit
= SHOW_UNIT_NONE
;
35 static char **arg_names
= NULL
;
37 static int arg_full
= -1;
38 static const char* arg_machine
= NULL
;
40 STATIC_DESTRUCTOR_REGISTER(arg_names
, freep
); /* don't free the strings */
42 static int help(void) {
43 _cleanup_free_
char *link
= NULL
;
46 r
= terminal_urlify_man("systemd-cgls", "1", &link
);
50 printf("%s [OPTIONS...] [CGROUP...]\n\n"
51 "Recursively show control group contents.\n\n"
52 " -h --help Show this help\n"
53 " --version Show package version\n"
54 " --no-pager Do not pipe output into a pager\n"
55 " -a --all Show all groups, including empty\n"
56 " -u --unit Show the subtrees of specifified system units\n"
57 " --user-unit Show the subtrees of specifified user units\n"
58 " -l --full Do not ellipsize output\n"
59 " -k Include kernel threads in output\n"
60 " -M --machine= Show container\n"
61 "\nSee the %s for details.\n"
62 , program_invocation_short_name
69 static int parse_argv(int argc
, char *argv
[]) {
77 static const struct option options
[] = {
78 { "help", no_argument
, NULL
, 'h' },
79 { "version", no_argument
, NULL
, ARG_VERSION
},
80 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
81 { "all", no_argument
, NULL
, 'a' },
82 { "full", no_argument
, NULL
, 'l' },
83 { "machine", required_argument
, NULL
, 'M' },
84 { "unit", optional_argument
, NULL
, 'u' },
85 { "user-unit", optional_argument
, NULL
, ARG_USER_UNIT
},
94 while ((c
= getopt_long(argc
, argv
, "-hkalM:u::", options
, NULL
)) >= 0)
105 arg_pager_flags
|= PAGER_DISABLE
;
113 arg_show_unit
= SHOW_UNIT_SYSTEM
;
114 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
119 arg_show_unit
= SHOW_UNIT_USER
;
120 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
125 /* positional argument */
126 if (strv_push(&arg_names
, optarg
) < 0)
135 arg_kernel_threads
= true;
139 arg_machine
= optarg
;
146 assert_not_reached("Unhandled option");
149 if (arg_machine
&& arg_show_unit
!= SHOW_UNIT_NONE
)
150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
151 "Cannot combine --unit or --user-unit with --machine=.");
156 static void show_cg_info(const char *controller
, const char *path
) {
158 if (cg_all_unified() == 0 && controller
&& !streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
159 printf("Controller %s; ", controller
);
161 printf("Control group %s:\n", empty_to_root(path
));
165 static int run(int argc
, char *argv
[]) {
168 log_parse_environment();
171 r
= parse_argv(argc
, argv
);
175 r
= pager_open(arg_pager_flags
);
176 if (r
> 0 && arg_full
< 0)
180 arg_all
* OUTPUT_SHOW_ALL
|
181 (arg_full
> 0) * OUTPUT_FULL_WIDTH
|
182 arg_kernel_threads
* OUTPUT_KERNEL_THREADS
;
185 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
186 _cleanup_free_
char *root
= NULL
;
189 STRV_FOREACH(name
, arg_names
) {
192 if (arg_show_unit
!= SHOW_UNIT_NONE
) {
193 /* Command line arguments are unit names */
194 _cleanup_free_
char *cgroup
= NULL
;
197 /* Connect to the bus only if necessary */
198 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
,
199 arg_show_unit
== SHOW_UNIT_USER
,
202 return log_error_errno(r
, "Failed to create bus connection: %m");
205 q
= show_cgroup_get_unit_path_and_warn(bus
, *name
, &cgroup
);
209 if (isempty(cgroup
)) {
210 log_warning("Unit %s not found.", *name
);
215 printf("Unit %s (%s):\n", *name
, cgroup
);
218 q
= show_cgroup_by_path(cgroup
, NULL
, 0, output_flags
);
220 } else if (path_startswith(*name
, "/sys/fs/cgroup")) {
222 printf("Directory %s:\n", *name
);
225 q
= show_cgroup_by_path(*name
, NULL
, 0, output_flags
);
227 _cleanup_free_
char *c
= NULL
, *p
= NULL
, *j
= NULL
;
228 const char *controller
, *path
;
231 /* Query root only if needed, treat error as fatal */
232 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
234 return log_error_errno(r
, "Failed to list cgroup tree: %m");
237 q
= cg_split_spec(*name
, &c
, &p
);
239 log_error_errno(q
, "Failed to split argument %s: %m", *name
);
243 controller
= c
?: SYSTEMD_CGROUP_CONTROLLER
;
245 j
= strjoin(root
, "/", p
);
249 path_simplify(j
, false);
254 show_cg_info(controller
, path
);
256 q
= show_cgroup(controller
, path
, NULL
, 0, output_flags
);
268 _cleanup_free_
char *cwd
= NULL
;
270 r
= safe_getcwd(&cwd
);
272 return log_error_errno(r
, "Cannot determine current working directory: %m");
274 if (path_startswith(cwd
, "/sys/fs/cgroup")) {
275 printf("Working directory %s:\n", cwd
);
278 r
= show_cgroup_by_path(cwd
, NULL
, 0, output_flags
);
284 _cleanup_free_
char *root
= NULL
;
286 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
288 return log_error_errno(r
, "Failed to list cgroup tree: %m");
290 show_cg_info(SYSTEMD_CGROUP_CONTROLLER
, root
);
293 r
= show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, root
, NULL
, 0, output_flags
);
297 return log_error_errno(r
, "Failed to list cgroup tree: %m");
302 DEFINE_MAIN_FUNCTION(run
);