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/>.
29 #include "cgroup-show.h"
30 #include "cgroup-util.h"
32 #include "path-util.h"
36 #include "output-mode.h"
40 #include "bus-error.h"
41 #include "unit-name.h"
43 static bool arg_no_pager = false;
44 static bool arg_kernel_threads = false;
45 static bool arg_all = false;
46 static int arg_full = -1;
47 static char* arg_machine = NULL;
49 static void help(void) {
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 " -l --full Do not ellipsize output\n"
57 " -k Include kernel threads in output\n"
58 " -M --machine Show container\n"
59 , program_invocation_short_name);
62 static int parse_argv(int argc, char *argv[]) {
69 static const struct option options[] = {
70 { "help", no_argument, NULL, 'h' },
71 { "version", no_argument, NULL, ARG_VERSION },
72 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
73 { "all", no_argument, NULL, 'a' },
74 { "full", no_argument, NULL, 'l' },
75 { "machine", required_argument, NULL, 'M' },
84 while ((c = getopt_long(argc, argv, "hkalM:", options, NULL)) >= 0)
94 puts(SYSTEMD_FEATURES);
110 arg_kernel_threads = true;
114 arg_machine = optarg;
121 assert_not_reached("Unhandled option");
127 int main(int argc, char *argv[]) {
128 int r = 0, retval = EXIT_FAILURE;
130 _cleanup_free_ char *root = NULL;
131 _cleanup_bus_close_unref_ sd_bus *bus = NULL;
133 log_parse_environment();
136 r = parse_argv(argc, argv);
140 retval = EXIT_SUCCESS;
145 r = pager_open(false);
153 arg_all * OUTPUT_SHOW_ALL |
154 (arg_full > 0) * OUTPUT_FULL_WIDTH;
156 r = bus_open_transport(BUS_TRANSPORT_LOCAL, NULL, false, &bus);
158 log_error("Failed to create bus connection: %s", strerror(-r));
165 for (i = optind; i < argc; i++) {
168 fprintf(stdout, "%s:\n", argv[i]);
172 root = strjoin("machine/", arg_machine, "/", argv[i], NULL);
174 root = strdup(argv[i]);
178 q = show_cgroup_by_path(root, NULL, 0,
179 arg_kernel_threads, output_flags);
185 _cleanup_free_ char *p;
187 p = get_current_dir_name();
189 log_error("Cannot determine current working directory: %m");
193 if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) {
194 printf("Working Directory %s:\n", p);
195 r = show_cgroup_by_path(p, NULL, 0,
196 arg_kernel_threads, output_flags);
201 _cleanup_free_ char *scope = NULL;
202 _cleanup_free_ char *path = NULL;
203 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
204 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
206 m = strappenda("/run/systemd/machines/", arg_machine);
207 r = parse_env_file(m, NEWLINE, "SCOPE", &scope, NULL);
209 log_error("Failed to get machine path: %s", strerror(-r));
213 path = unit_dbus_path_from_name(scope);
219 r = sd_bus_get_property(
221 "org.freedesktop.systemd1",
223 "org.freedesktop.systemd1.Scope",
230 log_error("Failed to query ControlGroup: %s", bus_error_message(&error, -r));
234 r = sd_bus_message_read(reply, "s", &cgroup);
236 bus_log_parse_error(r);
240 root = strdup(cgroup);
247 r = cg_get_root_path(&root);
249 log_error("Failed to get %s path: %s",
250 arg_machine ? "machine" : "root", strerror(-r));
254 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0,
255 arg_kernel_threads, output_flags);
260 log_error("Failed to list cgroup tree %s: %s", root, strerror(-r));
261 retval = EXIT_FAILURE;
263 retval = EXIT_SUCCESS;