From fe11b34a3c2843ea2198b310160b182d63aeb63b Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Tue, 2 Apr 2024 11:22:09 +0800
Subject: [PATCH 41/69] fix cpurt init bug for systemd-cgroup

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/daemon/common/cgroup/cgroup.c            | 13 +++++++------
 src/daemon/executor/container_cb/execution.c | 13 +++++++------
 2 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/src/daemon/common/cgroup/cgroup.c b/src/daemon/common/cgroup/cgroup.c
index d3f1445a..007dbb70 100644
--- a/src/daemon/common/cgroup/cgroup.c
+++ b/src/daemon/common/cgroup/cgroup.c
@@ -146,17 +146,18 @@ char *common_convert_cgroup_path(const char *cgroup_path)
         return NULL;
     }
 
-    // for cgroup fs cgroup path, return directly
-    if (!util_has_suffix(cgroup_path, ".slice")) {
-        return util_strdup_s(cgroup_path);
-    }
-
     // for systemd cgroup, cgroup_path should have the form slice:prefix:id,
     // convert it to a true path, such as from test-a.slice:isulad:id
     // to test.slice/test-a.slice/isulad-id.scope
     arr = util_string_split_n(cgroup_path, ':', 3);
     if (arr == NULL || util_array_len((const char **)arr) != 3) {
-        ERROR("Invalid systemd cgroup parent");
+        // not a systemd cgroup, return cgroup path directly
+        return util_strdup_s(cgroup_path);
+    }
+
+    // for cgroup fs cgroup path, return directly
+    if (!util_has_suffix(arr[0], ".slice")) {
+        ERROR("Invalid systemd cgroup path: %s", cgroup_path);
         return NULL;
     }
 
diff --git a/src/daemon/executor/container_cb/execution.c b/src/daemon/executor/container_cb/execution.c
index 88c6b354..4bf3621d 100644
--- a/src/daemon/executor/container_cb/execution.c
+++ b/src/daemon/executor/container_cb/execution.c
@@ -435,11 +435,12 @@ static int cpurt_controller_init(const char *id, const host_config *host_spec)
     }
 
     if (conf_get_systemd_cgroup()) {
-        // currently it is the same as docker, yet it is unclear that
-        // if systemd cgroup is used and cgroup parent is set to a slice rather than system.slice
-        // should iSulad set cpu.rt_runtime_us and cpu.rt_period_us for the parent path?
-        // in fact, even if system.slice is used,
-        // cpu.rt_runtime_us and cpu.rt_period_us might still needed to be set manually
+        __isula_auto_free char *converted_cgroup = common_convert_cgroup_path(cgroups_path);
+        if (converted_cgroup == NULL) {
+            ERROR("Failed to convert cgroup path");
+            return -1;
+        }
+
         __isula_auto_free char *init_cgroup = common_get_init_cgroup_path("cpu");
         if (init_cgroup == NULL) {
             ERROR("Failed to get init cgroup");
@@ -451,7 +452,7 @@ static int cpurt_controller_init(const char *id, const host_config *host_spec)
             ERROR("Failed to get own cgroup");
             return -1;
         }
-        char *new_cgroups_path = util_path_join(init_cgroup, cgroups_path);
+        char *new_cgroups_path = util_path_join(init_cgroup, converted_cgroup);
         if (new_cgroups_path == NULL) {
             ERROR("Failed to join path");
             return -1;
-- 
2.34.1

