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