1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
13 #include "cgroup-show.h"
14 #include "cgroup-util.h"
17 #include "output-mode.h"
19 #include "path-util.h"
21 #include "terminal-util.h"
22 #include "unit-name.h"
25 static PagerFlags arg_pager_flags
= 0;
26 static bool arg_kernel_threads
= false;
27 static bool arg_all
= false;
33 } arg_show_unit
= SHOW_UNIT_NONE
;
34 static char **arg_names
= NULL
;
36 static int arg_full
= -1;
37 static char* arg_machine
= NULL
;
39 static int help(void) {
40 _cleanup_free_
char *link
= NULL
;
43 r
= terminal_urlify_man("systemd-cgls", "1", &link
);
47 printf("%s [OPTIONS...] [CGROUP...]\n\n"
48 "Recursively show control group contents.\n\n"
49 " -h --help Show this help\n"
50 " --version Show package version\n"
51 " --no-pager Do not pipe output into a pager\n"
52 " -a --all Show all groups, including empty\n"
53 " -u --unit Show the subtrees of specifified system units\n"
54 " --user-unit Show the subtrees of specifified user units\n"
55 " -l --full Do not ellipsize output\n"
56 " -k Include kernel threads in output\n"
57 " -M --machine= Show container\n"
58 "\nSee the %s for details.\n"
59 , program_invocation_short_name
66 static int parse_argv(int argc
, char *argv
[]) {
74 static const struct option options
[] = {
75 { "help", no_argument
, NULL
, 'h' },
76 { "version", no_argument
, NULL
, ARG_VERSION
},
77 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
78 { "all", no_argument
, NULL
, 'a' },
79 { "full", no_argument
, NULL
, 'l' },
80 { "machine", required_argument
, NULL
, 'M' },
81 { "unit", optional_argument
, NULL
, 'u' },
82 { "user-unit", optional_argument
, NULL
, ARG_USER_UNIT
},
91 while ((c
= getopt_long(argc
, argv
, "-hkalM:u::", options
, NULL
)) >= 0)
102 arg_pager_flags
|= PAGER_DISABLE
;
110 arg_show_unit
= SHOW_UNIT_SYSTEM
;
111 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
116 arg_show_unit
= SHOW_UNIT_USER
;
117 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
122 /* positional argument */
123 if (strv_push(&arg_names
, optarg
) < 0)
132 arg_kernel_threads
= true;
136 arg_machine
= optarg
;
143 assert_not_reached("Unhandled option");
146 if (arg_machine
&& arg_show_unit
!= SHOW_UNIT_NONE
) {
147 log_error("Cannot combine --unit or --user-unit with --machine=.");
154 static void show_cg_info(const char *controller
, const char *path
) {
156 if (cg_all_unified() == 0 && controller
&& !streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
157 printf("Controller %s; ", controller
);
159 printf("Control group %s:\n", empty_to_root(path
));
163 int main(int argc
, char *argv
[]) {
166 log_parse_environment();
169 r
= parse_argv(argc
, argv
);
173 r
= pager_open(arg_pager_flags
);
174 if (r
> 0 && arg_full
< 0)
178 arg_all
* OUTPUT_SHOW_ALL
|
179 (arg_full
> 0) * OUTPUT_FULL_WIDTH
|
180 arg_kernel_threads
* OUTPUT_KERNEL_THREADS
;
183 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
184 _cleanup_free_
char *root
= NULL
;
187 STRV_FOREACH(name
, arg_names
) {
190 if (arg_show_unit
!= SHOW_UNIT_NONE
) {
191 /* Command line arguments are unit names */
192 _cleanup_free_
char *cgroup
= NULL
;
195 /* Connect to the bus only if necessary */
196 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
,
197 arg_show_unit
== SHOW_UNIT_USER
,
200 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
);
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
);
251 path_simplify(j
, false);
256 show_cg_info(controller
, path
);
258 q
= show_cgroup(controller
, path
, NULL
, 0, output_flags
);
270 _cleanup_free_
char *cwd
= NULL
;
272 r
= safe_getcwd(&cwd
);
274 log_error_errno(r
, "Cannot determine current working directory: %m");
278 if (path_startswith(cwd
, "/sys/fs/cgroup")) {
279 printf("Working directory %s:\n", cwd
);
282 r
= show_cgroup_by_path(cwd
, NULL
, 0, output_flags
);
288 _cleanup_free_
char *root
= NULL
;
290 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
294 show_cg_info(SYSTEMD_CGROUP_CONTROLLER
, root
);
297 r
= show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, root
, NULL
, 0, output_flags
);
302 log_error_errno(r
, "Failed to list cgroup tree: %m");
306 free(arg_names
); /* don't free the strings */
308 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;