]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/lscgroup: add support for default systemd delegation slice/scope
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Fri, 10 Feb 2023 21:50:05 +0000 (14:50 -0700)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 10 Feb 2023 21:55:18 +0000 (14:55 -0700)
Enhance the lscgroup tool to support default systemd delegation
slice/scope, if set in the cgconfig.conf's systemd setting.  Parse the
configuration file and read the systemd::delegate setting, if
any, and set it as the default systemd slice/scope in
systemd_default_cgroup. Setting it appends the slice/scope
name to the constructed default cgroup mount path.

When the user passes the relative cgroup name, its appends to the
sub-tree (delegated slice/scope) by default and if the user wishes to
create a cgroup in another subtree or cgroup root hierarchy, they need
to use the absolute path. For example:

                  cgroup_root
                 /          \
                /            \
         systemd.slice*      cgrp1
|
 systemd.scope
|
            cgrp-d1

* default system delegation slice - read from cgconfig.conf
$ sudo lscgroup -g cpu:cgrp-d1

will list the cgroups under the cpu controller hirearchy
cgroup_root:systemd_default_cgroup:cgrp-d1

and for listing a cgroups cgrp1, the user can use:
$ sudo lscgroup -g cpu:/cgrp1

or use the newly introduced -b switch to ignore the systemd slice/scope:
$ sudo lscgroup -b -g cpu:cgrp1

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
(cherry picked from commit eb7ece5f1e3c6ad0f48d5f7c67726e64d7db1b35)

src/tools/lscgroup.c

index e69853522a95bc6be7095032280871cf68b0512a..280f1d6818d390200f5c78029a1afbd7bd69aad7 100644 (file)
@@ -47,6 +47,9 @@ static void usage(int status, const char *program_name)
        info("  -g <controllers>:<path> Control group to be ");
        info("displayed (-g is optional)\n");
        info("  -h, --help                      Display this help\n");
+#ifdef WITH_SYSTEMD
+       info("  -b                              Ignore default systemd delegate hierarchy\n");
+#endif
        info("(Note: currently supported on cgroups v1 only)\n");
 }
 
@@ -243,6 +246,7 @@ int main(int argc, char *argv[])
        };
 
        struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
+       int ignore_default_systemd_delegate_slice = 0;
        int flags = 0;
        int ret = 0;
        int c;
@@ -251,8 +255,16 @@ int main(int argc, char *argv[])
        memset(cgroup_list, 0, sizeof(cgroup_list));
 
        /* parse arguments */
+#ifdef WITH_SYSTEMD
+       while ((c = getopt_long(argc, argv, "hg:b", options, NULL)) > 0) {
+               switch (c) {
+               case 'b':
+                       ignore_default_systemd_delegate_slice = 1;
+                       break;
+#else
        while ((c = getopt_long(argc, argv, "hg:", options, NULL)) > 0) {
                switch (c) {
+#endif
                case 'h':
                        usage(0, argv[0]);
                        ret = 0;
@@ -272,6 +284,17 @@ int main(int argc, char *argv[])
                }
        }
 
+       /* initialize libcg */
+       ret = cgroup_init();
+       if (ret) {
+               err("%s: libcgroup initialization failed: %s\n", argv[0], cgroup_strerror(ret));
+               goto err;
+       }
+
+       /* this is false always for disable-systemd */
+       if (!ignore_default_systemd_delegate_slice)
+               cgroup_set_default_systemd_cgroup();
+
        /* read the list of controllers */
        while (optind < argc) {
                ret = parse_cgroup_spec(cgroup_list, argv[optind], CG_HIER_MAX);