]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
cgtop: correctly order root cgroup always to the top
authorLennart Poettering <lennart@poettering.net>
Fri, 9 Feb 2018 15:51:03 +0000 (16:51 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 9 Feb 2018 15:53:09 +0000 (16:53 +0100)
Internally, we encode the root cgroup as empty string. However,
path_compare() is allergic to comparing absolute and relative paths.

Let's clean this up, by always uses "/" as path for the root cgroup when
comparing.

src/cgtop/cgtop.c

index 1a73fb099df875375a5bc972eee915acfd9214ce..c68b56568ba5c1b39b67694457f826da8bdd94ba 100644 (file)
@@ -506,6 +506,10 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
         return 0;
 }
 
+static const char *empty_to_slash(const char *p) {
+        return isempty(p) ? "/" : p;
+}
+
 static int group_compare(const void*a, const void *b) {
         const Group *x = *(Group**)a, *y = *(Group**)b;
 
@@ -515,9 +519,9 @@ static int group_compare(const void*a, const void *b) {
                  * recursive summing is off, since that is actually
                  * not accumulative for all children. */
 
-                if (path_startswith(y->path, x->path))
+                if (path_startswith(empty_to_slash(y->path), empty_to_slash(x->path)))
                         return -1;
-                if (path_startswith(x->path, y->path))
+                if (path_startswith(empty_to_slash(x->path), empty_to_slash(y->path)))
                         return 1;
         }
 
@@ -666,7 +670,7 @@ static void display(Hashmap *a) {
 
                 g = array[j];
 
-                path = isempty(g->path) ? "/" : g->path;
+                path = empty_to_slash(g->path);
                 ellipsized = ellipsize(path, path_columns, 33);
                 printf("%-*s", path_columns, ellipsized ?: path);