2aacef
From 1672b8dd340c4d4aa6398a08b15b36368ba442ec Mon Sep 17 00:00:00 2001
2aacef
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny@suse.com>
2aacef
Date: Fri, 25 Nov 2022 17:50:27 +0100
2aacef
Subject: [PATCH] cgtop: Do not rewrite -P or -k options
2aacef
2aacef
--recursive=no will overwrite possible -P or -k option hence making the
2aacef
recursive disabling impossible.
2aacef
2aacef
Check what counting types the system supports (encoded in the ordering
2aacef
of our enum) of and pick whatever user requests but is also supported.
2aacef
2aacef
Fixes: #25248
2aacef
(cherry picked from commit 48600b3524afe05d0faa8a5c88b5aaa53b801199)
2aacef
2aacef
Related: #2138081
2aacef
---
2aacef
 src/cgtop/cgtop.c | 16 ++++++++++------
2aacef
 1 file changed, 10 insertions(+), 6 deletions(-)
2aacef
2aacef
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
2aacef
index 95c3987525..8a51a9371b 100644
2aacef
--- a/src/cgtop/cgtop.c
2aacef
+++ b/src/cgtop/cgtop.c
2aacef
@@ -55,6 +55,12 @@ typedef struct Group {
2aacef
         uint64_t io_input_bps, io_output_bps;
2aacef
 } Group;
2aacef
 
2aacef
+typedef enum PidsCount {
2aacef
+        COUNT_USERSPACE_PROCESSES,
2aacef
+        COUNT_ALL_PROCESSES,
2aacef
+        COUNT_PIDS,
2aacef
+} PidsCount;
2aacef
+
2aacef
 static unsigned arg_depth = 3;
2aacef
 static unsigned arg_iterations = UINT_MAX;
2aacef
 static bool arg_batch = false;
2aacef
@@ -65,11 +71,7 @@ static char* arg_root = NULL;
2aacef
 static bool arg_recursive = true;
2aacef
 static bool arg_recursive_unset = false;
2aacef
 
2aacef
-static enum {
2aacef
-        COUNT_PIDS,
2aacef
-        COUNT_USERSPACE_PROCESSES,
2aacef
-        COUNT_ALL_PROCESSES,
2aacef
-} arg_count = COUNT_PIDS;
2aacef
+static PidsCount arg_count = COUNT_PIDS;
2aacef
 
2aacef
 static enum {
2aacef
         ORDER_PATH,
2aacef
@@ -915,6 +917,7 @@ static int run(int argc, char *argv[]) {
2aacef
         usec_t last_refresh = 0;
2aacef
         bool quit = false, immediate_refresh = false;
2aacef
         _cleanup_free_ char *root = NULL;
2aacef
+        PidsCount possible_count;
2aacef
         CGroupMask mask;
2aacef
         int r;
2aacef
 
2aacef
@@ -928,7 +931,8 @@ static int run(int argc, char *argv[]) {
2aacef
         if (r < 0)
2aacef
                 return log_error_errno(r, "Failed to determine supported controllers: %m");
2aacef
 
2aacef
-        arg_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_USERSPACE_PROCESSES;
2aacef
+        possible_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_ALL_PROCESSES;
2aacef
+        arg_count = MIN(possible_count, arg_count);
2aacef
 
2aacef
         if (arg_recursive_unset && arg_count == COUNT_PIDS)
2aacef
                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),