1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "cgroup-show.h"
29 #include "cgroup-util.h"
31 #include "path-util.h"
35 #include "output-mode.h"
39 #include "bus-error.h"
40 #include "unit-name.h"
42 static bool arg_no_pager
= false;
43 static bool arg_kernel_threads
= false;
44 static bool arg_all
= false;
45 static int arg_full
= -1;
46 static char* arg_machine
= NULL
;
48 static void help(void) {
49 printf("%s [OPTIONS...] [CGROUP...]\n\n"
50 "Recursively show control group contents.\n\n"
51 " -h --help Show this help\n"
52 " --version Show package version\n"
53 " --no-pager Do not pipe output into a pager\n"
54 " -a --all Show all groups, including empty\n"
55 " -l --full Do not ellipsize output\n"
56 " -k Include kernel threads in output\n"
57 " -M --machine= Show container\n"
58 , program_invocation_short_name
);
61 static int parse_argv(int argc
, char *argv
[]) {
68 static const struct option options
[] = {
69 { "help", no_argument
, NULL
, 'h' },
70 { "version", no_argument
, NULL
, ARG_VERSION
},
71 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
72 { "all", no_argument
, NULL
, 'a' },
73 { "full", no_argument
, NULL
, 'l' },
74 { "machine", required_argument
, NULL
, 'M' },
83 while ((c
= getopt_long(argc
, argv
, "hkalM:", options
, NULL
)) >= 0)
93 puts(SYSTEMD_FEATURES
);
109 arg_kernel_threads
= true;
113 arg_machine
= optarg
;
120 assert_not_reached("Unhandled option");
126 static int get_cgroup_root(char **ret
) {
127 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
128 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
129 _cleanup_free_
char *unit
= NULL
, *path
= NULL
;
134 r
= cg_get_root_path(ret
);
136 return log_error_errno(r
, "Failed to get root control group path: %m");
141 m
= strjoina("/run/systemd/machines/", arg_machine
);
142 r
= parse_env_file(m
, NEWLINE
, "SCOPE", &unit
, NULL
);
144 return log_error_errno(r
, "Failed to load machine data: %m");
146 path
= unit_dbus_path_from_name(unit
);
150 r
= bus_open_transport(BUS_TRANSPORT_LOCAL
, NULL
, false, &bus
);
152 return log_error_errno(r
, "Failed to create bus connection: %m");
154 r
= sd_bus_get_property_string(
156 "org.freedesktop.systemd1",
158 unit_dbus_interface_from_name(unit
),
163 return log_error_errno(r
, "Failed to query unit control group path: %s", bus_error_message(&error
, r
));
168 int main(int argc
, char *argv
[]) {
171 log_parse_environment();
174 r
= parse_argv(argc
, argv
);
179 r
= pager_open(false);
180 if (r
> 0 && arg_full
< 0)
185 arg_all
* OUTPUT_SHOW_ALL
|
186 (arg_full
> 0) * OUTPUT_FULL_WIDTH
;
189 _cleanup_free_
char *root
= NULL
;
192 r
= get_cgroup_root(&root
);
196 for (i
= optind
; i
< argc
; i
++) {
199 if (path_startswith(argv
[i
], "/sys/fs/cgroup")) {
201 printf("Directory %s:\n", argv
[i
]);
204 q
= show_cgroup_by_path(argv
[i
], NULL
, 0, arg_kernel_threads
, output_flags
);
206 _cleanup_free_
char *c
= NULL
, *p
= NULL
, *j
= NULL
;
207 const char *controller
, *path
;
209 r
= cg_split_spec(argv
[i
], &c
, &p
);
211 log_error_errno(r
, "Failed to split argument %s: %m", argv
[i
]);
215 controller
= c
?: SYSTEMD_CGROUP_CONTROLLER
;
217 j
= strjoin(root
, "/", p
, NULL
);
223 path_kill_slashes(j
);
228 if (cg_unified() > 0)
229 printf("Control group %s:\n", path
);
231 printf("Controller %s; control group %s:\n", controller
, path
);
234 q
= show_cgroup(controller
, path
, NULL
, 0, arg_kernel_threads
, output_flags
);
245 _cleanup_free_
char *cwd
= NULL
;
247 cwd
= get_current_dir_name();
249 r
= log_error_errno(errno
, "Cannot determine current working directory: %m");
253 if (path_startswith(cwd
, "/sys/fs/cgroup")) {
254 printf("Working directory %s:\n", cwd
);
257 r
= show_cgroup_by_path(cwd
, NULL
, 0, arg_kernel_threads
, output_flags
);
263 _cleanup_free_
char *root
= NULL
;
265 r
= get_cgroup_root(&root
);
269 printf("Control group %s:\n", isempty(root
) ? "/" : root
);
272 r
= show_cgroup(SYSTEMD_CGROUP_CONTROLLER
, root
, NULL
, 0, arg_kernel_threads
, output_flags
);
277 log_error_errno(r
, "Failed to list cgroup tree: %m");
282 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;