From 5e1b4929f53063ee1b1a36f04db7dd55ac1ca35b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 16:41:16 +0200 Subject: [PATCH 1/6] systemctl: suppress second argument of ternary op where we can --- src/systemctl/systemctl-show.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index 7e4432d3240..29f1f7fd328 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -2078,7 +2078,7 @@ static int show_system_status(sd_bus *bus) { off = ansi_normal(); } - printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ? arg_host : hn); + printf("%s%s%s %s\n", on, special_glyph(SPECIAL_GLYPH_BLACK_CIRCLE), off, arg_host ?: hn); printf(" State: %s%s%s\n", on, strna(mi.state), off); From 27ba2ad2f6bc96a613a52d976ffc9cace1f4dddc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 16:42:22 +0200 Subject: [PATCH 2/6] systemctl: use empty_to_root() where appropriate --- src/systemctl/systemctl-show.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index 29f1f7fd328..c3a9ba763d6 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -2090,7 +2090,7 @@ static int show_system_status(sd_bus *bus) { FORMAT_TIMESTAMP_STYLE(mi.timestamp, arg_timestamp_style), FORMAT_TIMESTAMP_RELATIVE(mi.timestamp)); - printf(" CGroup: %s\n", mi.control_group ?: "/"); + printf(" CGroup: %s\n", empty_to_root(mi.control_group)); if (IN_SET(arg_transport, BUS_TRANSPORT_LOCAL, BUS_TRANSPORT_MACHINE)) { From a0dde733ac750582242d36b8814b216f30acd5e3 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 16:43:27 +0200 Subject: [PATCH 3/6] systemctl: use LESS_BY where appropriate --- src/systemctl/systemctl-show.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index c3a9ba763d6..ea4ac63c44b 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -710,11 +710,7 @@ static void print_status_info( printf(" CGroup: %s\n", i->control_group); - c = columns(); - if (c > sizeof(prefix) - 1) - c -= sizeof(prefix) - 1; - else - c = 0; + c = LESS_BY(columns(), strlen(prefix)); r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); if (r == -EBADR) { @@ -2097,11 +2093,7 @@ static int show_system_status(sd_bus *bus) { static const char prefix[] = " "; unsigned c; - c = columns(); - if (c > sizeof(prefix) - 1) - c -= sizeof(prefix) - 1; - else - c = 0; + c = LESS_BY(columns(), strlen(prefix)); show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); } From 35ac0260db7b896604d156e9638ad15700083508 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 16:47:40 +0200 Subject: [PATCH 4/6] systemctl: only fall back to local cgroup display if we talk to local systemd Otherwise we likely show rubbish because even in local containers we nowadays have cgroup namespacing, hence we likely can't access the cgroup tree from the host at the same place as inside the container. --- src/systemctl/systemctl-show.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index ea4ac63c44b..af2d14d2c99 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -713,7 +713,7 @@ static void print_status_info( c = LESS_BY(columns(), strlen(prefix)); r = unit_show_processes(bus, i->id, i->control_group, prefix, c, get_output_flags(), &error); - if (r == -EBADR) { + if (r == -EBADR && arg_transport == BUS_TRANSPORT_LOCAL) { unsigned k = 0; pid_t extra[2]; From 038cae098b6d85ad9fb06594d36f49915d32f000 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 16:44:29 +0200 Subject: [PATCH 5/6] systemctl: make sure "systemctl -M status" shows cgroup tree of container not host This shows the cgroup tree of the root slice of the container now, by querying the cgroup pid tree via the bus instead of going directly to the cgroupfs. A fallback is kept for really old systemd versions where querying the PID tree was not available. Fixes: #20958 --- src/systemctl/systemctl-show.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/systemctl/systemctl-show.c b/src/systemctl/systemctl-show.c index af2d14d2c99..86803c5fc7b 100644 --- a/src/systemctl/systemctl-show.c +++ b/src/systemctl/systemctl-show.c @@ -29,6 +29,7 @@ #include "process-util.h" #include "signal-util.h" #include "sort-util.h" +#include "special.h" #include "string-table.h" #include "systemctl-list-machines.h" #include "systemctl-list-units.h" @@ -2043,8 +2044,10 @@ static int show_all( static int show_system_status(sd_bus *bus) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; _cleanup_(machine_info_clear) struct machine_info mi = {}; + static const char prefix[] = " "; _cleanup_free_ char *hn = NULL; const char *on, *off; + unsigned c; int r; hn = gethostname_malloc(); @@ -2087,16 +2090,15 @@ static int show_system_status(sd_bus *bus) { FORMAT_TIMESTAMP_RELATIVE(mi.timestamp)); printf(" CGroup: %s\n", empty_to_root(mi.control_group)); - if (IN_SET(arg_transport, - BUS_TRANSPORT_LOCAL, - BUS_TRANSPORT_MACHINE)) { - static const char prefix[] = " "; - unsigned c; - c = LESS_BY(columns(), strlen(prefix)); + c = LESS_BY(columns(), strlen(prefix)); + r = unit_show_processes(bus, SPECIAL_ROOT_SLICE, mi.control_group, prefix, c, get_output_flags(), &error); + if (r == -EBADR && arg_transport == BUS_TRANSPORT_LOCAL) /* Compatibility for really old systemd versions */ show_cgroup(SYSTEMD_CGROUP_CONTROLLER, strempty(mi.control_group), prefix, c, get_output_flags()); - } + else if (r < 0) + log_warning_errno(r, "Failed to dump process list for '%s', ignoring: %s", + arg_host ?: hn, bus_error_message(&error, r)); return 0; } From 22d0f9a7d8e631cf0ed8f90bc2c45e4b04c4d2cb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 28 Oct 2021 18:54:52 +0200 Subject: [PATCH 6/6] systemctl: make dbus PID cgroup tree output look more like systemd-cgls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let's add the same dash of color (well, grey, …) to the dbus-based pid/cgroup tree output as systemd-cgls has. --- src/shared/bus-unit-procs.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/bus-unit-procs.c b/src/shared/bus-unit-procs.c index 50deb9252e2..87c0334fec5 100644 --- a/src/shared/bus-unit-procs.c +++ b/src/shared/bus-unit-procs.c @@ -188,11 +188,13 @@ static int dump_processes( more = i+1 < n || cg->children; special = special_glyph(more ? SPECIAL_GLYPH_TREE_BRANCH : SPECIAL_GLYPH_TREE_RIGHT); - fprintf(stdout, "%s%s%*"PID_PRI" %s\n", + fprintf(stdout, "%s%s%s%*"PID_PRI" %s%s\n", prefix, special, + ansi_grey(), width, pids[i], - name); + name, + ansi_normal()); } }