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