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"
38 static bool arg_no_pager = false;
39 static bool arg_kernel_threads = false;
40 static bool arg_all = false;
41 static int arg_full = -1;
43 static void help(void) {
45 printf("%s [OPTIONS...] [CGROUP...]\n\n"
46 "Recursively show control group contents.\n\n"
47 " -h --help Show this help\n"
48 " --version Show package version\n"
49 " --no-pager Do not pipe output into a pager\n"
50 " -a --all Show all groups, including empty\n"
51 " --full Do not ellipsize output\n"
52 " -k Include kernel threads in output\n",
53 program_invocation_short_name);
56 static int parse_argv(int argc, char *argv[]) {
64 static const struct option options[] = {
65 { "help", no_argument, NULL, 'h' },
66 { "version", no_argument, NULL, ARG_VERSION },
67 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
68 { "all", no_argument, NULL, 'a' },
69 { "full", no_argument, NULL, ARG_FULL },
78 while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) {
88 puts(SYSTEMD_FEATURES);
104 arg_kernel_threads = true;
111 log_error("Unknown option code %c", c);
119 int main(int argc, char *argv[]) {
120 int r = 0, retval = EXIT_FAILURE;
123 log_parse_environment();
126 r = parse_argv(argc, argv);
130 retval = EXIT_SUCCESS;
143 arg_all * OUTPUT_SHOW_ALL |
144 (arg_full > 0) * OUTPUT_FULL_WIDTH;
149 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
151 printf("%s:\n", argv[i]);
153 q = show_cgroup_by_path(argv[i], NULL, 0,
154 arg_kernel_threads, output_flags);
160 char _cleanup_free_ *p;
162 p = get_current_dir_name();
164 log_error("Cannot determine current working directory: %m");
168 if (path_startswith(p, "/sys/fs/cgroup")) {
169 printf("Working Directory %s:\n", p);
170 r = show_cgroup_by_path(p, NULL, 0,
171 arg_kernel_threads, output_flags);
173 char _cleanup_free_ *root = NULL;
174 const char *t = NULL;
176 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &root);
180 if (endswith(root, "/system"))
181 root[strlen(root)-7] = 0;
183 t = root[0] ? root : "/";
186 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, t, NULL, 0,
187 arg_kernel_threads, output_flags);
192 log_error("Failed to list cgroup tree: %s", strerror(-r));
194 retval = EXIT_SUCCESS;