]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
tools/cgexec: add support for default systemd delegation slice/scope
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Tue, 31 Jan 2023 06:24:37 +0000 (06:24 +0000)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 10 Feb 2023 21:44:25 +0000 (14:44 -0700)
Enhance the cgexec 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/scope - read from cgconfig.conf
$ sudo cgexec -g cpu:cgrp-d1 sleep 10

will create the cgroup under the cpu controller hirearchy
cgroup_root:systemd_default_cgroup:cgrp-d1

and for creating a cgroup cgrp1, the user can use:
$ sudo cgexec -g cpu:/cgrp1 sleep 10

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

Signed-off-by: Kamalesh Babulal <kamalesh.babulal@oracle.com>
Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/tools/cgexec.c

index efd499b08706cb3d210fcaa1ba031f92f46a7fbb..00627cca706c93d3803566fe430dca748e06256d 100644 (file)
@@ -49,11 +49,15 @@ static void usage(int status, const char *program_name)
        info("  -h, --help                      Display this help\n");
        info("  --sticky                        cgred daemon does not ");
        info("change pidlist and children tasks\n");
+#ifdef WITH_SYSTEMD
+       info("  -b                              Ignore default systemd delegate hierarchy\n");
+#endif
 }
 
 int main(int argc, char *argv[])
 {
        struct cgroup_group_spec *cgroup_list[CG_HIER_MAX];
+       int ignore_default_systemd_delegate_slice = 0;
        int cg_specified = 0;
        int flag_child = 0;
        int i, ret = 0;
@@ -63,9 +67,16 @@ int main(int argc, char *argv[])
        int c;
 
        memset(cgroup_list, 0, sizeof(cgroup_list));
-
+#ifdef WITH_SYSTEMD
+       while ((c = getopt_long(argc, argv, "+g:shb", longopts, NULL)) > 0) {
+               switch (c) {
+               case 'b':
+                       ignore_default_systemd_delegate_slice = 1;
+                       break;
+#else
        while ((c = getopt_long(argc, argv, "+g:sh", longopts, NULL)) > 0) {
                switch (c) {
+#endif
                case 'g':
                        ret = parse_cgroup_spec(cgroup_list, optarg, CG_HIER_MAX);
                        if (ret) {
@@ -99,6 +110,10 @@ int main(int argc, char *argv[])
                return ret;
        }
 
+       /* this is false always for disable-systemd */
+       if (!ignore_default_systemd_delegate_slice)
+               cgroup_set_default_systemd_cgroup();
+
        /* Just for debugging purposes. */
        uid = geteuid();
        gid = getegid();