]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cgls/cgls.c
path-util: introduce path_simplify()
[thirdparty/systemd.git] / src / cgls / cgls.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <getopt.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "sd-bus.h"
15
16 #include "alloc-util.h"
17 #include "bus-util.h"
18 #include "cgroup-show.h"
19 #include "cgroup-util.h"
20 #include "fileio.h"
21 #include "log.h"
22 #include "output-mode.h"
23 #include "pager.h"
24 #include "path-util.h"
25 #include "strv.h"
26 #include "unit-name.h"
27 #include "util.h"
28
29 static bool arg_no_pager = false;
30 static bool arg_kernel_threads = false;
31 static bool arg_all = false;
32
33 static enum {
34 SHOW_UNIT_NONE,
35 SHOW_UNIT_SYSTEM,
36 SHOW_UNIT_USER,
37 } arg_show_unit = SHOW_UNIT_NONE;
38 static char **arg_names = NULL;
39
40 static int arg_full = -1;
41 static char* arg_machine = NULL;
42
43 static void help(void) {
44 printf("%s [OPTIONS...] [CGROUP...]\n\n"
45 "Recursively show control group contents.\n\n"
46 " -h --help Show this help\n"
47 " --version Show package version\n"
48 " --no-pager Do not pipe output into a pager\n"
49 " -a --all Show all groups, including empty\n"
50 " -u --unit Show the subtrees of specifified system units\n"
51 " --user-unit Show the subtrees of specifified user units\n"
52 " -l --full Do not ellipsize output\n"
53 " -k Include kernel threads in output\n"
54 " -M --machine= Show container\n"
55 , program_invocation_short_name);
56 }
57
58 static int parse_argv(int argc, char *argv[]) {
59
60 enum {
61 ARG_NO_PAGER = 0x100,
62 ARG_VERSION,
63 ARG_USER_UNIT,
64 };
65
66 static const struct option options[] = {
67 { "help", no_argument, NULL, 'h' },
68 { "version", no_argument, NULL, ARG_VERSION },
69 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
70 { "all", no_argument, NULL, 'a' },
71 { "full", no_argument, NULL, 'l' },
72 { "machine", required_argument, NULL, 'M' },
73 { "unit", optional_argument, NULL, 'u' },
74 { "user-unit", optional_argument, NULL, ARG_USER_UNIT },
75 {}
76 };
77
78 int c;
79
80 assert(argc >= 1);
81 assert(argv);
82
83 while ((c = getopt_long(argc, argv, "-hkalM:u::", options, NULL)) >= 0)
84
85 switch (c) {
86
87 case 'h':
88 help();
89 return 0;
90
91 case ARG_VERSION:
92 return version();
93
94 case ARG_NO_PAGER:
95 arg_no_pager = true;
96 break;
97
98 case 'a':
99 arg_all = true;
100 break;
101
102 case 'u':
103 arg_show_unit = SHOW_UNIT_SYSTEM;
104 if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
105 return log_oom();
106 break;
107
108 case ARG_USER_UNIT:
109 arg_show_unit = SHOW_UNIT_USER;
110 if (strv_push(&arg_names, optarg) < 0) /* push optarg if not empty */
111 return log_oom();
112 break;
113
114 case 1:
115 /* positional argument */
116 if (strv_push(&arg_names, optarg) < 0)
117 return log_oom();
118 break;
119
120 case 'l':
121 arg_full = true;
122 break;
123
124 case 'k':
125 arg_kernel_threads = true;
126 break;
127
128 case 'M':
129 arg_machine = optarg;
130 break;
131
132 case '?':
133 return -EINVAL;
134
135 default:
136 assert_not_reached("Unhandled option");
137 }
138
139 if (arg_machine && arg_show_unit != SHOW_UNIT_NONE) {
140 log_error("Cannot combine --unit or --user-unit with --machine=.");
141 return -EINVAL;
142 }
143
144 return 1;
145 }
146
147 static void show_cg_info(const char *controller, const char *path) {
148
149 if (cg_all_unified() == 0 && controller && !streq(controller, SYSTEMD_CGROUP_CONTROLLER))
150 printf("Controller %s; ", controller);
151
152 printf("Control group %s:\n", empty_to_root(path));
153 fflush(stdout);
154 }
155
156 int main(int argc, char *argv[]) {
157 int r, output_flags;
158
159 log_parse_environment();
160 log_open();
161
162 r = parse_argv(argc, argv);
163 if (r <= 0)
164 goto finish;
165
166 r = pager_open(arg_no_pager, false);
167 if (r > 0 && arg_full < 0)
168 arg_full = true;
169
170 output_flags =
171 arg_all * OUTPUT_SHOW_ALL |
172 (arg_full > 0) * OUTPUT_FULL_WIDTH |
173 arg_kernel_threads * OUTPUT_KERNEL_THREADS;
174
175 if (arg_names) {
176 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
177 _cleanup_free_ char *root = NULL;
178 char **name;
179
180 STRV_FOREACH(name, arg_names) {
181 int q;
182
183 if (arg_show_unit != SHOW_UNIT_NONE) {
184 /* Command line arguments are unit names */
185 _cleanup_free_ char *cgroup = NULL;
186
187 if (!bus) {
188 /* Connect to the bus only if necessary */
189 r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL,
190 arg_show_unit == SHOW_UNIT_USER,
191 &bus);
192 if (r < 0) {
193 log_error_errno(r, "Failed to create bus connection: %m");
194 goto finish;
195 }
196 }
197
198 q = show_cgroup_get_unit_path_and_warn(bus, *name, &cgroup);
199 if (q < 0)
200 goto failed;
201
202 if (isempty(cgroup)) {
203 log_warning("Unit %s not found.", *name);
204 q = -ENOENT;
205 goto failed;
206 }
207
208 printf("Unit %s (%s):\n", *name, cgroup);
209 fflush(stdout);
210
211 q = show_cgroup_by_path(cgroup, NULL, 0, output_flags);
212
213 } else if (path_startswith(*name, "/sys/fs/cgroup")) {
214
215 printf("Directory %s:\n", *name);
216 fflush(stdout);
217
218 q = show_cgroup_by_path(*name, NULL, 0, output_flags);
219 } else {
220 _cleanup_free_ char *c = NULL, *p = NULL, *j = NULL;
221 const char *controller, *path;
222
223 if (!root) {
224 /* Query root only if needed, treat error as fatal */
225 r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
226 if (r < 0)
227 goto finish;
228 }
229
230 q = cg_split_spec(*name, &c, &p);
231 if (q < 0) {
232 log_error_errno(q, "Failed to split argument %s: %m", *name);
233 goto failed;
234 }
235
236 controller = c ?: SYSTEMD_CGROUP_CONTROLLER;
237 if (p) {
238 j = strjoin(root, "/", p);
239 if (!j) {
240 r = log_oom();
241 goto finish;
242 }
243
244 path_simplify(j, false);
245 path = j;
246 } else
247 path = root;
248
249 show_cg_info(controller, path);
250
251 q = show_cgroup(controller, path, NULL, 0, output_flags);
252 }
253
254 failed:
255 if (q < 0 && r >= 0)
256 r = q;
257 }
258
259 } else {
260 bool done = false;
261
262 if (!arg_machine) {
263 _cleanup_free_ char *cwd = NULL;
264
265 r = safe_getcwd(&cwd);
266 if (r < 0) {
267 log_error_errno(r, "Cannot determine current working directory: %m");
268 goto finish;
269 }
270
271 if (path_startswith(cwd, "/sys/fs/cgroup")) {
272 printf("Working directory %s:\n", cwd);
273 fflush(stdout);
274
275 r = show_cgroup_by_path(cwd, NULL, 0, output_flags);
276 done = true;
277 }
278 }
279
280 if (!done) {
281 _cleanup_free_ char *root = NULL;
282
283 r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root);
284 if (r < 0)
285 goto finish;
286
287 show_cg_info(SYSTEMD_CGROUP_CONTROLLER, root);
288
289 printf("-.slice\n");
290 r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, root, NULL, 0, output_flags);
291 }
292 }
293
294 if (r < 0)
295 log_error_errno(r, "Failed to list cgroup tree: %m");
296
297 finish:
298 pager_close();
299 free(arg_names); /* don't free the strings */
300
301 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
302 }