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"
39 static bool arg_no_pager = false;
40 static bool arg_kernel_threads = false;
41 static bool arg_all = false;
42 static int arg_full = -1;
43 static char* arg_machine = NULL;
45 static int help(void) {
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 " -l --full Do not ellipsize output\n"
54 " -k Include kernel threads in output\n"
55 " -M --machine Show container\n",
56 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) {
92 puts(SYSTEMD_FEATURES);
108 arg_kernel_threads = true;
112 arg_machine = optarg;
119 assert_not_reached("Unhandled option");
126 int main(int argc, char *argv[]) {
127 int r = 0, retval = EXIT_FAILURE;
129 char _cleanup_free_ *root = NULL;
131 log_parse_environment();
134 r = parse_argv(argc, argv);
138 retval = EXIT_SUCCESS;
143 r = pager_open(false);
151 arg_all * OUTPUT_SHOW_ALL |
152 (arg_full > 0) * OUTPUT_FULL_WIDTH;
157 for (i = optind; i < argc; i++) {
160 fprintf(stdout, "%s:\n", argv[i]);
164 root = strjoin("machine/", arg_machine, "/", argv[i], NULL);
166 root = strdup(argv[i]);
170 q = show_cgroup_by_path(root, NULL, 0,
171 arg_kernel_threads, output_flags);
177 _cleanup_free_ char *p;
179 p = get_current_dir_name();
181 log_error("Cannot determine current working directory: %m");
185 if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) {
186 printf("Working Directory %s:\n", p);
187 r = show_cgroup_by_path(p, NULL, 0,
188 arg_kernel_threads, output_flags);
192 m = strappenda("/run/systemd/machines/", arg_machine);
193 r = parse_env_file(m, NEWLINE, "CGROUP", &root, NULL);
195 r = cg_get_root_path(&root);
197 log_error("Failed to get %s path: %s",
198 arg_machine ? "machine" : "root", strerror(-r));
202 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0,
203 arg_kernel_threads, output_flags);
208 log_error("Failed to list cgroup tree %s: %s", root, strerror(-r));
209 retval = EXIT_FAILURE;
211 retval = EXIT_SUCCESS;