]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/cgls/cgls.c
nspawn: introduce the new /machine/ tree in the cgroup tree and move containers there
[thirdparty/systemd.git] / src / cgls / cgls.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
fa776d8e
LP
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
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
fa776d8e
LP
11 (at your option) any later version.
12
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
5430f7f2 16 Lesser General Public License for more details.
fa776d8e 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
fa776d8e
LP
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <limits.h>
23#include <stdio.h>
24#include <unistd.h>
25#include <errno.h>
26#include <getopt.h>
27#include <string.h>
28
29#include "cgroup-show.h"
c6c18be3 30#include "cgroup-util.h"
fa776d8e 31#include "log.h"
9eb977db 32#include "path-util.h"
fa776d8e 33#include "util.h"
1968a360 34#include "pager.h"
c3175a7f 35#include "build.h"
9bdbc2e2 36#include "output-mode.h"
1968a360
LP
37
38static bool arg_no_pager = false;
1e5678d0 39static bool arg_kernel_threads = false;
c3175a7f 40static bool arg_all = false;
9bdbc2e2 41static int arg_full = -1;
fa776d8e
LP
42
43static void help(void) {
44
45 printf("%s [OPTIONS...] [CGROUP...]\n\n"
46 "Recursively show control group contents.\n\n"
69fc152f 47 " -h --help Show this help\n"
c3175a7f 48 " --version Show package version\n"
1e5678d0 49 " --no-pager Do not pipe output into a pager\n"
c3175a7f 50 " -a --all Show all groups, including empty\n"
9bdbc2e2 51 " --full Do not ellipsize output\n"
1e5678d0 52 " -k Include kernel threads in output\n",
fa776d8e
LP
53 program_invocation_short_name);
54}
55
56static int parse_argv(int argc, char *argv[]) {
57
1968a360 58 enum {
c3175a7f 59 ARG_NO_PAGER = 0x100,
9bdbc2e2
LN
60 ARG_VERSION,
61 ARG_FULL,
1968a360
LP
62 };
63
fa776d8e 64 static const struct option options[] = {
1968a360 65 { "help", no_argument, NULL, 'h' },
c3175a7f 66 { "version", no_argument, NULL, ARG_VERSION },
1968a360 67 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
c3175a7f 68 { "all", no_argument, NULL, 'a' },
9bdbc2e2 69 { "full", no_argument, NULL, ARG_FULL },
1968a360 70 { NULL, 0, NULL, 0 }
fa776d8e
LP
71 };
72
73 int c;
74
75 assert(argc >= 1);
76 assert(argv);
77
c3175a7f 78 while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) {
fa776d8e
LP
79
80 switch (c) {
81
82 case 'h':
83 help();
84 return 0;
85
c3175a7f
LP
86 case ARG_VERSION:
87 puts(PACKAGE_STRING);
c3175a7f
LP
88 puts(SYSTEMD_FEATURES);
89 return 0;
90
1968a360
LP
91 case ARG_NO_PAGER:
92 arg_no_pager = true;
93 break;
94
c3175a7f
LP
95 case 'a':
96 arg_all = true;
97 break;
98
9bdbc2e2
LN
99 case ARG_FULL:
100 arg_full = true;
101 break;
102
1e5678d0
LP
103 case 'k':
104 arg_kernel_threads = true;
105 break;
106
fa776d8e
LP
107 case '?':
108 return -EINVAL;
109
110 default:
111 log_error("Unknown option code %c", c);
112 return -EINVAL;
113 }
114 }
115
116 return 1;
117}
118
119int main(int argc, char *argv[]) {
22f4096c 120 int r = 0, retval = EXIT_FAILURE;
9bdbc2e2 121 int output_flags;
fa776d8e
LP
122
123 log_parse_environment();
2396fb04 124 log_open();
fa776d8e 125
1e5678d0
LP
126 r = parse_argv(argc, argv);
127 if (r < 0)
fa776d8e
LP
128 goto finish;
129 else if (r == 0) {
22f4096c 130 retval = EXIT_SUCCESS;
fa776d8e
LP
131 goto finish;
132 }
133
9bdbc2e2 134 if (!arg_no_pager) {
1b12a7b5 135 r = pager_open(false);
9bdbc2e2
LN
136 if (r > 0) {
137 if (arg_full == -1)
138 arg_full = true;
139 }
140 }
141
142 output_flags =
143 arg_all * OUTPUT_SHOW_ALL |
144 (arg_full > 0) * OUTPUT_FULL_WIDTH;
1968a360 145
fa776d8e
LP
146 if (optind < argc) {
147 unsigned i;
148
149 for (i = (unsigned) optind; i < (unsigned) argc; i++) {
150 int q;
151 printf("%s:\n", argv[i]);
152
9bdbc2e2
LN
153 q = show_cgroup_by_path(argv[i], NULL, 0,
154 arg_kernel_threads, output_flags);
1e5678d0 155 if (q < 0)
fa776d8e
LP
156 r = q;
157 }
158
159 } else {
6f862a69 160 char _cleanup_free_ *p;
fa776d8e 161
1e5678d0
LP
162 p = get_current_dir_name();
163 if (!p) {
fa776d8e
LP
164 log_error("Cannot determine current working directory: %m");
165 goto finish;
166 }
167
77d5f105 168 if (path_startswith(p, "/sys/fs/cgroup")) {
fa776d8e 169 printf("Working Directory %s:\n", p);
9bdbc2e2
LN
170 r = show_cgroup_by_path(p, NULL, 0,
171 arg_kernel_threads, output_flags);
1f16b4a6 172 } else {
6f862a69 173 char _cleanup_free_ *root = NULL;
1f16b4a6 174
7027ff61
LP
175 r = cg_get_root_path(&root);
176 if (r < 0) {
177 log_error("Failed to get root path: %s", strerror(-r));
178 goto finish;
b9a8e638 179 }
1f16b4a6 180
7027ff61 181 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0,
9bdbc2e2 182 arg_kernel_threads, output_flags);
1f16b4a6 183 }
fa776d8e
LP
184 }
185
186 if (r < 0)
187 log_error("Failed to list cgroup tree: %s", strerror(-r));
188
22f4096c 189 retval = EXIT_SUCCESS;
fa776d8e
LP
190
191finish:
1968a360 192 pager_close();
fa776d8e
LP
193
194 return retval;
195}