From: Kamalesh Babulal Date: Tue, 10 Jan 2023 14:13:38 +0000 (+0000) Subject: tools/lscgroup: add support for default systemd delegation slice/scope X-Git-Tag: v3.1.0~205 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb7ece5f1e3c6ad0f48d5f7c67726e64d7db1b35;p=thirdparty%2Flibcgroup.git tools/lscgroup: add support for default systemd delegation slice/scope 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 Signed-off-by: Tom Hromatka --- diff --git a/src/tools/lscgroup.c b/src/tools/lscgroup.c index e6985352..280f1d68 100644 --- a/src/tools/lscgroup.c +++ b/src/tools/lscgroup.c @@ -47,6 +47,9 @@ static void usage(int status, const char *program_name) info(" -g : 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);