From 38158b920e772ea3a7cc9dfcf705666ce3aa5ce3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 30 Apr 2013 00:35:38 -0400 Subject: [PATCH] cgls: add --machine/-M cg_get_machine_path is modified to include the escaped machine name + ".nspawn" if the machine argument is nonnull. --- man/systemd-cgls.xml | 15 +++++++++++- src/analyze/systemd-analyze.c | 1 - src/cgls/cgls.c | 46 ++++++++++++++++++++++++----------- src/login/sd-login.c | 4 +-- src/nspawn/nspawn.c | 22 ++--------------- src/shared/cgroup-util.c | 17 ++++++++----- src/shared/cgroup-util.h | 2 +- src/test/test-cgroup-util.c | 2 +- 8 files changed, 63 insertions(+), 46 deletions(-) diff --git a/man/systemd-cgls.xml b/man/systemd-cgls.xml index b280b87e6..6249d05a8 100644 --- a/man/systemd-cgls.xml +++ b/man/systemd-cgls.xml @@ -49,7 +49,9 @@ - systemd-cgls OPTIONS CGROUP + systemd-cgls + OPTIONS + CGROUP @@ -125,6 +127,16 @@ threads in output. + + + + + Limit cgroups shown to + the part corresponding to the + container MACHINE. + + + @@ -142,6 +154,7 @@ systemd1, systemctl1, systemd-cgtop1, + systemd-nspawn1, ps1 diff --git a/src/analyze/systemd-analyze.c b/src/analyze/systemd-analyze.c index 7f0b69c5a..bb86ec7da 100644 --- a/src/analyze/systemd-analyze.c +++ b/src/analyze/systemd-analyze.c @@ -780,7 +780,6 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, unsigned } static int list_dependencies(DBusConnection *bus) { - _cleanup_free_ char *unit = NULL; _cleanup_strv_free_ char **units = NULL; char ts[FORMAT_TIMESPAN_MAX]; struct unit_times *times; diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 8ddd733df..ef3e5672a 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -39,6 +39,7 @@ static bool arg_no_pager = false; static bool arg_kernel_threads = false; static bool arg_all = false; static int arg_full = -1; +static char* arg_machine = NULL; static void help(void) { @@ -49,7 +50,8 @@ static void help(void) { " --no-pager Do not pipe output into a pager\n" " -a --all Show all groups, including empty\n" " --full Do not ellipsize output\n" - " -k Include kernel threads in output\n", + " -k Include kernel threads in output\n" + " -M --machine Show container\n", program_invocation_short_name); } @@ -67,6 +69,7 @@ static int parse_argv(int argc, char *argv[]) { { "no-pager", no_argument, NULL, ARG_NO_PAGER }, { "all", no_argument, NULL, 'a' }, { "full", no_argument, NULL, ARG_FULL }, + { "machine", required_argument, NULL, 'M' }, { NULL, 0, NULL, 0 } }; @@ -75,7 +78,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 1); assert(argv); - while ((c = getopt_long(argc, argv, "hka", options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "hkaM:", options, NULL)) >= 0) { switch (c) { @@ -104,6 +107,10 @@ static int parse_argv(int argc, char *argv[]) { arg_kernel_threads = true; break; + case 'M': + arg_machine = optarg; + break; + case '?': return -EINVAL; @@ -119,6 +126,7 @@ static int parse_argv(int argc, char *argv[]) { int main(int argc, char *argv[]) { int r = 0, retval = EXIT_FAILURE; int output_flags; + char _cleanup_free_ *root = NULL; log_parse_environment(); log_open(); @@ -144,13 +152,20 @@ int main(int argc, char *argv[]) { (arg_full > 0) * OUTPUT_FULL_WIDTH; if (optind < argc) { - unsigned i; + int i; - for (i = (unsigned) optind; i < (unsigned) argc; i++) { + for (i = optind; i < argc; i++) { int q; printf("%s:\n", argv[i]); - q = show_cgroup_by_path(argv[i], NULL, 0, + if (arg_machine) + root = strjoin("machine/", arg_machine, "/", argv[i], NULL); + else + root = strdup(argv[i]); + if (!root) + return log_oom(); + + q = show_cgroup_by_path(root, NULL, 0, arg_kernel_threads, output_flags); if (q < 0) r = q; @@ -165,16 +180,18 @@ int main(int argc, char *argv[]) { goto finish; } - if (path_startswith(p, "/sys/fs/cgroup")) { + if (path_startswith(p, "/sys/fs/cgroup") && !arg_machine) { printf("Working Directory %s:\n", p); r = show_cgroup_by_path(p, NULL, 0, arg_kernel_threads, output_flags); } else { - _cleanup_free_ char *root = NULL; - - r = cg_get_root_path(&root); + if (arg_machine) + r = cg_get_machine_path(arg_machine, &root); + else + r = cg_get_root_path(&root); if (r < 0) { - log_error("Failed to get root path: %s", strerror(-r)); + log_error("Failed to get %s path: %s", + arg_machine ? "machine" : "root", strerror(-r)); goto finish; } @@ -183,10 +200,11 @@ int main(int argc, char *argv[]) { } } - if (r < 0) - log_error("Failed to list cgroup tree: %s", strerror(-r)); - - retval = EXIT_SUCCESS; + if (r < 0) { + log_error("Failed to list cgroup tree %s: %s", root, strerror(-r)); + retval = EXIT_FAILURE; + } else + retval = EXIT_SUCCESS; finish: pager_close(); diff --git a/src/login/sd-login.c b/src/login/sd-login.c index bc8cd8ae8..66c4487ff 100644 --- a/src/login/sd-login.c +++ b/src/login/sd-login.c @@ -598,7 +598,7 @@ int sd_get_machine_names(char ***machines) { char *n; int c = 0, r; - r = cg_get_machine_path(&md); + r = cg_get_machine_path(NULL, &md); if (r < 0) return r; @@ -681,7 +681,7 @@ _public_ int sd_login_monitor_new(const char *category, sd_login_monitor **m) { _cleanup_free_ char *md = NULL, *p = NULL; int r; - r = cg_get_machine_path(&md); + r = cg_get_machine_path(NULL, &md); if (r < 0) return r; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index d772b478f..bbb3334ba 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -1217,7 +1217,7 @@ finish: int main(int argc, char *argv[]) { pid_t pid = 0; int r = EXIT_FAILURE, k; - _cleanup_free_ char *machine_root = NULL, *name = NULL, *escaped = NULL, *newcg = NULL; + _cleanup_free_ char *newcg = NULL; _cleanup_close_ int master = -1; int n_fd_passed; const char *console = NULL; @@ -1301,30 +1301,12 @@ int main(int argc, char *argv[]) { fdset_close_others(fds); log_open(); - k = cg_get_machine_path(&machine_root); + k = cg_get_machine_path(arg_machine, &newcg); if (k < 0) { log_error("Failed to determine machine cgroup path: %s", strerror(-k)); goto finish; } - name = strappend(arg_machine, ".nspawn"); - if (!name) { - log_oom(); - goto finish; - } - - escaped = cg_escape(name); - if (!escaped) { - log_oom(); - goto finish; - } - - newcg = strjoin(machine_root, "/", escaped, NULL); - if (!newcg) { - log_oom(); - goto finish; - } - k = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, newcg, true); if (k <= 0 && k != -ENOENT) { log_error("Container already running."); diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 1366f5865..8c2ef4553 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -1146,17 +1146,22 @@ int cg_get_user_path(char **path) { return 0; } -int cg_get_machine_path(char **path) { - _cleanup_free_ char *root = NULL; +int cg_get_machine_path(const char *machine, char **path) { + _cleanup_free_ char *root = NULL, *escaped = NULL; char *p; assert(path); - if (cg_get_root_path(&root) < 0 || streq(root, "/")) - p = strdup("/machine"); - else - p = strappend(root, "/machine"); + if (machine) { + const char *name = strappenda(machine, ".nspawn"); + + escaped = cg_escape(name); + if (!escaped) + return -ENOMEM; + } + p = strjoin(cg_get_root_path(&root) >= 0 && !streq(root, "/") ? root : "", + "/machine", machine ? "/" : "", machine ? escaped : "", NULL); if (!p) return -ENOMEM; diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h index 7bd02c100..92caa0c44 100644 --- a/src/shared/cgroup-util.h +++ b/src/shared/cgroup-util.h @@ -87,7 +87,7 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_ int cg_get_root_path(char **path); int cg_get_system_path(char **path); int cg_get_user_path(char **path); -int cg_get_machine_path(char **path); +int cg_get_machine_path(const char *machine, char **path); int cg_path_get_session(const char *path, char **session); int cg_path_get_owner_uid(const char *path, uid_t *uid); diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 6726f8fb1..cc1a6fd72 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -85,7 +85,7 @@ static void test_get_paths(void) { assert_se(cg_get_user_path(&c) >= 0); log_info("User = %s", c); - assert_se(cg_get_machine_path(&d) >= 0); + assert_se(cg_get_machine_path("harley", &d) >= 0); log_info("Machine = %s", d); } -- 2.30.2