]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
API: add cgroup_set_default_systemd_cgroup()
authorKamalesh Babulal <kamalesh.babulal@oracle.com>
Wed, 8 Feb 2023 09:56:17 +0000 (15:26 +0530)
committerTom Hromatka <tom.hromatka@oracle.com>
Fri, 10 Feb 2023 21:44:25 +0000 (14:44 -0700)
Introduce an API cgroup_set_default_systemd_cgroup(), that reads the
systemd_default_cgroup_file, if exists, and only sets the
systemd_default_cgroup path if both of the following conditions are met:
1. The minimum length of the default slice/scope read is 15
   characters, because at the minimum it should be <n>.slice/<n>.scope.
2. Checks if the /sys/fs/cgroup/<systemd_default_cgroup> cgroup exists
   and if it doesn't, it also deletes the systemd_default_cgroup_file
   (/var/run/libcgroup/systemd) file.

this will be used by the tools and can be used by any applications
to set the default cgroup tree to systemd_default_cgroup after loading
the cgconfig.conf file(s).

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

index 1893b1d3660e3f5cf81ebc96853357ea4404f16d..6b4fe06776e30656ad89ee876921fba0ea41558f 100644 (file)
@@ -2077,6 +2077,87 @@ static int config_create_slice_scope(char * const tmp_systemd_default_cgroup)
 err:
        return 0;
 }
+
+/*
+ * This function builds the canonical path for the systemd default cgroup
+ * based on the cgroup setup mode and if the cgroup doesn't exist removes
+ * the systemd default cgroup file. This function expects the caller to be
+ * holding the systemd_default_cgroup_lock.
+ */
+static bool systemd_default_cgroup_exists(void)
+{
+       char path[FILENAME_MAX] = "\0";
+       char cgrp_procs_path[FILENAME_MAX + 13]; /* 13 = strlen(/cgroup.procs) */
+       bool exists = false;
+
+       switch (cgroup_setup_mode()) {
+       case CGROUP_MODE_HYBRID:
+               /*
+                * check for empty cgroup v2, the most common usage in
+                * the hybrid case.
+                */
+               if (cg_build_path("", path, NULL))
+                       break;
+       case CGROUP_MODE_UNIFIED:
+               /* fallthrough */
+       case CGROUP_MODE_LEGACY:
+               cg_build_path("", path, "cpu");
+               /* fallthrough */
+       case CGROUP_MODE_UNK:
+               break;
+       }
+
+       if (!strlen(path)) {
+               cgroup_dbg("Unable to form canonical path for %s", systemd_default_cgroup);
+               goto err;
+       }
+
+       snprintf(cgrp_procs_path, sizeof(cgrp_procs_path), "%s/cgroup.procs", path);
+       if (access(cgrp_procs_path, F_OK)) {
+               /*
+                * systemd has already removed the scope cgroup, hence delete
+                * the file too.
+                */
+               cgroup_dbg("%s doesn't exists, deleting %s", path, systemd_default_cgroup_file);
+               unlink(systemd_default_cgroup_file);
+               goto err;
+       }
+
+       exists = true;
+err:
+       return exists;
+}
+
+void cgroup_set_default_systemd_cgroup(void)
+{
+       FILE *systemd_def_cgrp_f;
+       size_t len;
+
+       pthread_rwlock_wrlock(&systemd_default_cgroup_lock);
+
+       systemd_def_cgrp_f = fopen(systemd_default_cgroup_file, "r");
+       if (!systemd_def_cgrp_f) {
+               cgroup_dbg("Unable to read %s ", systemd_default_cgroup_file);
+               goto err;
+       }
+
+       len = fread(systemd_default_cgroup, sizeof(char), FILENAME_MAX, systemd_def_cgrp_f);
+       fclose(systemd_def_cgrp_f);
+       /* at the minimum it should be <n>.slice/<n>.scope == 15 */
+       if (len < 15) {
+               cgroup_dbg("Malformed systemd default cgroup %s", systemd_default_cgroup);
+               goto err;
+       }
+
+       if (systemd_default_cgroup_exists()) {
+               pthread_rwlock_unlock(&systemd_default_cgroup_lock);
+               return;
+       }
+err:
+       pthread_rwlock_unlock(&systemd_default_cgroup_lock);
+       cgroup_dbg(", continuing without systemd default cgroup.\n", systemd_default_cgroup);
+       systemd_default_cgroup[0] = '\0';
+}
 #else
 int cgroup_add_systemd_opts(const char * const config, const char * const value)
 {
@@ -2089,4 +2170,6 @@ int cgroup_alloc_systemd_opts(const char * const config, const char * const valu
 }
 
 void cgroup_cleanup_systemd_opts(void) { }
+
+void cgroup_set_default_systemd_cgroup(void) { }
 #endif
index 07bd7816105d57dde9a06307108d2a060c6af7a1..28ce7d4065e3606f540ccb44733c79e88935d69a 100644 (file)
@@ -155,4 +155,5 @@ CGROUP_3.0 {
        cgroup_get_controller_by_index;
        cgroup_get_controller_name;
        cgroup_create_scope2;
+       cgroup_set_default_systemd_cgroup;
 } CGROUP_2.0;