| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "build.h" |
| 9 |
#include "bus-util.h" |
| 10 |
#include "cgroup-show.h" |
| 11 |
#include "cgroup-util.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "options.h" |
| 16 |
#include "output-mode.h" |
| 17 |
#include "pager.h" |
| 18 |
#include "parse-util.h" |
| 19 |
#include "path-util.h" |
| 20 |
#include "pretty-print.h" |
| 21 |
#include "runtime-scope.h" |
| 22 |
#include "string-util.h" |
| 23 |
#include "strv.h" |
| 24 |
#include "unit-name.h" |
| 25 |
|
| 26 |
static PagerFlags arg_pager_flags = 0; |
| 27 |
static OutputFlags arg_output_flags = 0; |
| 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, strv_freep); |
| 40 |
|
| 41 |
static int help(void) { |
| 42 |
_cleanup_free_ char *link = NULL; |
| 43 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 44 |
int r; |
| 45 |
|
| 46 |
r = terminal_urlify_man("systemd-cgls", "1", &link); |
| 47 |
if (r < 0) |
| 48 |
return log_oom(); |
| 49 |
|
| 50 |
r = option_parser_get_help_table(&options); |
| 51 |
if (r < 0) |
| 52 |
return r; |
| 53 |
|
| 54 |
printf("%s [OPTIONS...] [CGROUP...]\n\n" |
| 55 |
"%sRecursively show control group contents.%s\n\n", |
| 56 |
program_invocation_short_name, |
| 57 |
ansi_highlight(), |
| 58 |
ansi_normal()); |
| 59 |
|
| 60 |
r = table_print_or_warn(options); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
|
| 64 |
printf("\nSee the %s for details.\n", link); |
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
static int parse_argv(int argc, char *argv[]) { |
| 69 |
assert(argc >= 1); |
| 70 |
assert(argv); |
| 71 |
|
| 72 |
OptionParser opts = { argc, argv, OPTION_PARSER_RETURN_POSITIONAL_ARGS }; |
| 73 |
int r; |
| 74 |
|
| 75 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 76 |
switch (c) { |
| 77 |
|
| 78 |
OPTION_COMMON_HELP: |
| 79 |
return help(); |
| 80 |
|
| 81 |
OPTION_COMMON_VERSION: |
| 82 |
return version(); |
| 83 |
|
| 84 |
OPTION_COMMON_NO_PAGER: |
| 85 |
arg_pager_flags |= PAGER_DISABLE; |
| 86 |
break; |
| 87 |
|
| 88 |
OPTION('a', "all", NULL, "Show all groups, including empty"): |
| 89 |
arg_output_flags |= OUTPUT_SHOW_ALL; |
| 90 |
break; |
| 91 |
|
| 92 |
OPTION_FULL(OPTION_OPTIONAL_ARG, 'u', "unit", "UNIT", |
| 93 |
"Show the subtrees of specified system units"): |
| 94 |
if (arg_show_unit == SHOW_UNIT_USER) |
| 95 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 96 |
"Cannot combine --unit with --user-unit."); |
| 97 |
|
| 98 |
arg_show_unit = SHOW_UNIT_SYSTEM; |
| 99 |
if (strv_extend(&arg_names, opts.arg) < 0) /* push arg if not empty */ |
| 100 |
return log_oom(); |
| 101 |
break; |
| 102 |
|
| 103 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "user-unit", "UNIT", |
| 104 |
"Show the subtrees of specified user units"): |
| 105 |
if (arg_show_unit == SHOW_UNIT_SYSTEM) |
| 106 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 107 |
"Cannot combine --user-unit with --unit."); |
| 108 |
|
| 109 |
arg_show_unit = SHOW_UNIT_USER; |
| 110 |
if (strv_extend(&arg_names, opts.arg) < 0) /* push arg if not empty */ |
| 111 |
return log_oom(); |
| 112 |
break; |
| 113 |
|
| 114 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "xattr", "BOOL", |
| 115 |
"Show cgroup extended attributes"): {} |
| 116 |
OPTION_SHORT('x', NULL, "Same as --xattr=true"): |
| 117 |
if (opts.arg) { |
| 118 |
r = parse_boolean(opts.arg); |
| 119 |
if (r < 0) |
| 120 |
return log_error_errno(r, "Failed to parse --xattr= value: %s", opts.arg); |
| 121 |
} else |
| 122 |
r = true; |
| 123 |
|
| 124 |
SET_FLAG(arg_output_flags, OUTPUT_CGROUP_XATTRS, r); |
| 125 |
break; |
| 126 |
|
| 127 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "cgroup-id", "BOOL", |
| 128 |
"Show cgroup ID"): {} |
| 129 |
OPTION_SHORT('c', NULL, "Same as --cgroup-id=true"): |
| 130 |
if (opts.arg) { |
| 131 |
r = parse_boolean(opts.arg); |
| 132 |
if (r < 0) |
| 133 |
return log_error_errno(r, "Failed to parse --cgroup-id= value: %s", opts.arg); |
| 134 |
} else |
| 135 |
r = true; |
| 136 |
|
| 137 |
SET_FLAG(arg_output_flags, OUTPUT_CGROUP_ID, r); |
| 138 |
break; |
| 139 |
|
| 140 |
OPTION('l', "full", NULL, "Do not ellipsize output"): |
| 141 |
arg_full = true; |
| 142 |
break; |
| 143 |
|
| 144 |
OPTION_SHORT('k', NULL, "Include kernel threads in output"): |
| 145 |
arg_output_flags |= OUTPUT_KERNEL_THREADS; |
| 146 |
break; |
| 147 |
|
| 148 |
OPTION_COMMON_MACHINE: |
| 149 |
arg_machine = opts.arg; |
| 150 |
break; |
| 151 |
|
| 152 |
OPTION_POSITIONAL: |
| 153 |
if (strv_extend(&arg_names, opts.arg) < 0) /* push arg */ |
| 154 |
return log_oom(); |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
if (arg_machine && arg_show_unit != SHOW_UNIT_NONE) |
| 159 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 160 |
"Cannot combine --unit or --user-unit with --machine=."); |
| 161 |
|
| 162 |
assert(option_parser_get_n_args(&opts) == 0); |
| 163 |
|
| 164 |
return 1; |
| 165 |
} |
| 166 |
|
| 167 |
static void show_cg_info(const char *path) { |
| 168 |
printf("CGroup %s:\n", empty_to_root(path)); |
| 169 |
fflush(stdout); |
| 170 |
} |
| 171 |
|
| 172 |
static int run(int argc, char *argv[]) { |
| 173 |
int r; |
| 174 |
|
| 175 |
log_setup(); |
| 176 |
|
| 177 |
r = parse_argv(argc, argv); |
| 178 |
if (r <= 0) |
| 179 |
return r; |
| 180 |
|
| 181 |
pager_open(arg_pager_flags); |
| 182 |
if (arg_full < 0 && pager_have()) |
| 183 |
arg_full = true; |
| 184 |
|
| 185 |
if (arg_full > 0) |
| 186 |
arg_output_flags |= OUTPUT_FULL_WIDTH; |
| 187 |
|
| 188 |
if (arg_names) { |
| 189 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 190 |
_cleanup_free_ char *root = NULL; |
| 191 |
|
| 192 |
STRV_FOREACH(name, arg_names) { |
| 193 |
int q; |
| 194 |
|
| 195 |
if (arg_show_unit != SHOW_UNIT_NONE) { |
| 196 |
/* Command line arguments are unit names */ |
| 197 |
_cleanup_free_ char *cgroup = NULL, *unit_name = NULL; |
| 198 |
|
| 199 |
r = unit_name_mangle(*name, UNIT_NAME_MANGLE_WARN, &unit_name); |
| 200 |
if (r < 0) |
| 201 |
return log_error_errno(r, "Failed to mangle unit name: %m"); |
| 202 |
|
| 203 |
if (!bus) { |
| 204 |
RuntimeScope scope = arg_show_unit == SHOW_UNIT_USER ? RUNTIME_SCOPE_USER : RUNTIME_SCOPE_SYSTEM; |
| 205 |
|
| 206 |
/* Connect to the bus only if necessary */ |
| 207 |
r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, scope, &bus); |
| 208 |
if (r < 0) |
| 209 |
return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, scope); |
| 210 |
} |
| 211 |
|
| 212 |
q = show_cgroup_get_unit_path_and_warn(bus, unit_name, &cgroup); |
| 213 |
if (q < 0) |
| 214 |
goto failed; |
| 215 |
|
| 216 |
if (isempty(cgroup)) { |
| 217 |
q = log_warning_errno(SYNTHETIC_ERRNO(ENOENT), "Unit %s not found.", unit_name); |
| 218 |
goto failed; |
| 219 |
} |
| 220 |
|
| 221 |
printf("Unit %s (%s):\n", unit_name, cgroup); |
| 222 |
fflush(stdout); |
| 223 |
|
| 224 |
q = show_cgroup(cgroup, NULL, 0, arg_output_flags); |
| 225 |
|
| 226 |
} else if (path_startswith(*name, "/sys/fs/cgroup")) { |
| 227 |
|
| 228 |
printf("Directory %s:\n", *name); |
| 229 |
fflush(stdout); |
| 230 |
|
| 231 |
q = show_cgroup_by_path(*name, NULL, 0, arg_output_flags); |
| 232 |
} else { |
| 233 |
_cleanup_free_ char *c = NULL, *p = NULL, *j = NULL; |
| 234 |
const char *path; |
| 235 |
|
| 236 |
if (!root) { |
| 237 |
/* Query root only if needed, treat error as fatal */ |
| 238 |
r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root); |
| 239 |
if (r < 0) |
| 240 |
return log_error_errno(r, "Failed to list cgroup tree: %m"); |
| 241 |
} |
| 242 |
|
| 243 |
q = cg_split_spec(*name, &c, &p); |
| 244 |
if (q < 0) { |
| 245 |
log_error_errno(q, "Failed to split argument %s: %m", *name); |
| 246 |
goto failed; |
| 247 |
} |
| 248 |
|
| 249 |
if (c && !streq(c, SYSTEMD_CGROUP_CONTROLLER)) |
| 250 |
log_warning("Legacy cgroup v1 controller '%s' was specified, ignoring.", c); |
| 251 |
|
| 252 |
if (p) { |
| 253 |
j = path_join(root, p); |
| 254 |
if (!j) |
| 255 |
return log_oom(); |
| 256 |
|
| 257 |
path_simplify(j); |
| 258 |
path = j; |
| 259 |
} else |
| 260 |
path = root; |
| 261 |
|
| 262 |
show_cg_info(path); |
| 263 |
|
| 264 |
q = show_cgroup(path, NULL, 0, arg_output_flags); |
| 265 |
} |
| 266 |
|
| 267 |
failed: |
| 268 |
if (q < 0 && r >= 0) |
| 269 |
r = q; |
| 270 |
} |
| 271 |
|
| 272 |
} else { |
| 273 |
bool done = false; |
| 274 |
|
| 275 |
if (!arg_machine) { |
| 276 |
_cleanup_free_ char *cwd = NULL; |
| 277 |
|
| 278 |
r = safe_getcwd(&cwd); |
| 279 |
if (r < 0) |
| 280 |
return log_error_errno(r, "Cannot determine current working directory: %m"); |
| 281 |
|
| 282 |
if (path_startswith(cwd, "/sys/fs/cgroup")) { |
| 283 |
printf("Working directory %s:\n", cwd); |
| 284 |
fflush(stdout); |
| 285 |
|
| 286 |
r = show_cgroup_by_path(cwd, NULL, 0, arg_output_flags); |
| 287 |
done = true; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
if (!done) { |
| 292 |
_cleanup_free_ char *root = NULL; |
| 293 |
|
| 294 |
r = show_cgroup_get_path_and_warn(arg_machine, NULL, &root); |
| 295 |
if (r < 0) |
| 296 |
return log_error_errno(r, "Failed to list cgroup tree: %m"); |
| 297 |
|
| 298 |
show_cg_info(root); |
| 299 |
|
| 300 |
printf("-.slice\n"); |
| 301 |
r = show_cgroup(root, NULL, 0, arg_output_flags); |
| 302 |
} |
| 303 |
} |
| 304 |
if (r < 0) |
| 305 |
return log_error_errno(r, "Failed to list cgroup tree: %m"); |
| 306 |
|
| 307 |
return 0; |
| 308 |
} |
| 309 |
|
| 310 |
DEFINE_MAIN_FUNCTION(run); |