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/>.
28 #include "alloc-util.h"
30 #include "cgroup-show.h"
31 #include "cgroup-util.h"
34 #include "output-mode.h"
36 #include "path-util.h"
38 #include "unit-name.h"
41 static bool arg_no_pager
= false;
42 static bool arg_kernel_threads
= false;
43 static bool arg_all
= false;
49 } arg_show_unit
= SHOW_UNIT_NONE
;
50 static char **arg_names
= NULL
;
52 static int arg_full
= -1;
53 static char* arg_machine
= NULL
;
55 static void help(void) {
56 printf("%s [OPTIONS...] [CGROUP...]\n\n"
57 "Recursively show control group contents.\n\n"
58 " -h --help Show this help\n"
59 " --version Show package version\n"
60 " --no-pager Do not pipe output into a pager\n"
61 " -a --all Show all groups, including empty\n"
62 " -u --unit Show the subtrees of specifified system units\n"
63 " --user-unit Show the subtrees of specifified user units\n"
64 " -l --full Do not ellipsize output\n"
65 " -k Include kernel threads in output\n"
66 " -M --machine= Show container\n"
67 , program_invocation_short_name
);
70 static int parse_argv(int argc
, char *argv
[]) {
78 static const struct option options
[] = {
79 { "help", no_argument
, NULL
, 'h' },
80 { "version", no_argument
, NULL
, ARG_VERSION
},
81 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
82 { "all", no_argument
, NULL
, 'a' },
83 { "full", no_argument
, NULL
, 'l' },
84 { "machine", required_argument
, NULL
, 'M' },
85 { "unit", optional_argument
, NULL
, 'u' },
86 { "user-unit", optional_argument
, NULL
, ARG_USER_UNIT
},
95 while ((c
= getopt_long(argc
, argv
, "-hkalM:u::", options
, NULL
)) >= 0)
115 arg_show_unit
= SHOW_UNIT_SYSTEM
;
116 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
121 arg_show_unit
= SHOW_UNIT_USER
;
122 if (strv_push(&arg_names
, optarg
) < 0) /* push optarg if not empty */
127 /* positional argument */
128 if (strv_push(&arg_names
, optarg
) < 0)
137 arg_kernel_threads
= true;
141 arg_machine
= optarg
;
148 assert_not_reached("Unhandled option");
151 if (arg_machine
&& arg_show_unit
!= SHOW_UNIT_NONE
) {
152 log_error("Cannot combine --unit or --user-unit with --machine=.");
159 static void show_cg_info(const char *controller
, const char *path
) {
161 if (cg_all_unified() <= 0 && controller
&& !streq(controller
, SYSTEMD_CGROUP_CONTROLLER
))
162 printf("Controller %s; ", controller
);
164 printf("Control group %s:\n", isempty(path
) ? "/" : path
);
168 int main(int argc
, char *argv
[]) {
171 log_parse_environment();
174 r
= parse_argv(argc
, argv
);
179 r
= pager_open(arg_no_pager
, false);
180 if (r
> 0 && arg_full
< 0)
185 arg_all
* OUTPUT_SHOW_ALL
|
186 (arg_full
> 0) * OUTPUT_FULL_WIDTH
|
187 arg_kernel_threads
* OUTPUT_KERNEL_THREADS
;
190 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
191 _cleanup_free_
char *root
= NULL
;
194 STRV_FOREACH(name
, arg_names
) {
197 if (arg_show_unit
!= SHOW_UNIT_NONE
) {
198 /* Command line arguments are unit names */
199 _cleanup_free_
char *cgroup
= NULL
;
202 /* Connect to the bus only if necessary */
203 r
= bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL
, NULL
,
204 arg_show_unit
== SHOW_UNIT_USER
,
207 log_error_errno(r
, "Failed to create bus connection: %m");
212 q
= show_cgroup_get_unit_path_and_warn(bus
, *name
, &cgroup
);
216 if (isempty(cgroup
)) {
217 log_warning("Unit %s not found.", *name
);
222 printf("Unit %s (%s):\n", *name
, cgroup
);
225 q
= show_cgroup_by_path(cgroup
, NULL
, 0, output_flags
);
227 } else if (path_startswith(*name
, "/sys/fs/cgroup")) {
229 printf("Directory %s:\n", *name
);
232 q
= show_cgroup_by_path(*name
, NULL
, 0, output_flags
);
234 _cleanup_free_
char *c
= NULL
, *p
= NULL
, *j
= NULL
;
235 const char *controller
, *path
;
238 /* Query root only if needed, treat error as fatal */
239 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
244 q
= cg_split_spec(*name
, &c
, &p
);
246 log_error_errno(q
, "Failed to split argument %s: %m", *name
);
250 controller
= c
?: SYSTEMD_CGROUP_CONTROLLER
;
252 j
= strjoin(root
, "/", p
);
258 path_kill_slashes(j
);
263 show_cg_info(controller
, path
);
265 q
= show_cgroup(controller
, path
, NULL
, 0, output_flags
);
277 _cleanup_free_
char *cwd
= NULL
;
279 cwd
= get_current_dir_name();
281 r
= log_error_errno(errno
, "Cannot determine current working directory: %m");
285 if (path_startswith(cwd
, "/sys/fs/cgroup")) {
286 printf("Working directory %s:\n", cwd
);
289 r
= show_cgroup_by_path(cwd
, NULL
, 0, output_flags
);
295 _cleanup_free_
char *root
= NULL
;
297 r
= show_cgroup_get_path_and_warn(arg_machine
, NULL
, &root
);
301 show_cg_info(SYSTEMD_CGROUP_CONTROLLER
, root
);
304 r
= show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, root
, NULL
, 0, output_flags
);
309 log_error_errno(r
, "Failed to list cgroup tree: %m");
313 free(arg_names
); /* don't free the strings */
315 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;