From 0c19d0c481428fc91d832bd47846eff203005052 Mon Sep 17 00:00:00 2001
From: jikai <jikai11@huawei.com>
Date: Tue, 30 Jan 2024 13:16:35 +0800
Subject: [PATCH 197/198] bugfix for cont restart when iSulad drops original
 default-ulimit

Signed-off-by: jikai <jikai11@huawei.com>
---
 src/daemon/modules/api/specs_api.h            |  4 ++
 .../modules/service/service_container.c       | 14 ++----
 src/daemon/modules/spec/specs.c               | 46 +++++++++++++++++++
 3 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/src/daemon/modules/api/specs_api.h b/src/daemon/modules/api/specs_api.h
index 1a5a6ecc..458e3806 100644
--- a/src/daemon/modules/api/specs_api.h
+++ b/src/daemon/modules/api/specs_api.h
@@ -42,6 +42,10 @@ int merge_share_namespace(oci_runtime_spec *oci_spec, const host_config *host_sp
 
 int update_spec_annotations(oci_runtime_spec *oci_spec, container_config *container_spec, host_config *host_spec);
 
+int update_oci_container_cgroups_path(const char *id, oci_runtime_spec *oci_spec, const host_config *host_spec);
+
+int update_oci_ulimit(oci_runtime_spec *oci_spec, const host_config *host_spec);
+
 const oci_runtime_spec *get_readonly_default_oci_spec(bool system_container);
 
 int spec_module_init(void);
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 534a0a1d..bad2a9e1 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -685,7 +685,6 @@ out:
 
 static int do_oci_spec_update(const char *id, oci_runtime_spec *oci_spec, container_config *container_spec, host_config *hostconfig)
 {
-    char *cgroup_parent = NULL;
     int ret;
 
     // Renew annotations for oci spec, cgroup path only,
@@ -697,16 +696,10 @@ static int do_oci_spec_update(const char *id, oci_runtime_spec *oci_spec, contai
     }
 
     // If isulad daemon cgroup parent updated, we should update this config into oci spec
-    cgroup_parent = merge_container_cgroups_path(id, hostconfig);
-    if (cgroup_parent == NULL) {
+    ret = update_oci_container_cgroups_path(id, oci_spec, hostconfig);
+    if (ret < 0) {
         return -1;
     }
-    if (oci_spec->linux->cgroups_path != NULL && strcmp(oci_spec->linux->cgroups_path, cgroup_parent) != 0) {
-        free(oci_spec->linux->cgroups_path);
-        oci_spec->linux->cgroups_path = cgroup_parent;
-        cgroup_parent = NULL;
-    }
-    free(cgroup_parent);
 
     // For Linux.Resources, isula update will save changes into oci spec;
     // so we just skip it;
@@ -719,7 +712,8 @@ static int do_oci_spec_update(const char *id, oci_runtime_spec *oci_spec, contai
     }
 
     // If isulad daemon ulimit updated, we should update this config into oci spec.
-    if (merge_global_ulimit(oci_spec) != 0) {
+    ret = update_oci_ulimit(oci_spec, hostconfig);
+    if (ret < 0) {
         return -1;
     }
 
diff --git a/src/daemon/modules/spec/specs.c b/src/daemon/modules/spec/specs.c
index dd6e413b..d2088a8e 100644
--- a/src/daemon/modules/spec/specs.c
+++ b/src/daemon/modules/spec/specs.c
@@ -2165,6 +2165,28 @@ char *merge_container_cgroups_path(const char *id, const host_config *host_spec)
     return ret;
 }
 
+int update_oci_container_cgroups_path(const char *id, oci_runtime_spec *oci_spec, const host_config *hostconfig)
+{
+    if (oci_spec == NULL || oci_spec->linux == NULL) {
+        ERROR("Invalid arguments");
+        return -1;
+    }
+
+    char *cgroup_parent = merge_container_cgroups_path(id, hostconfig);
+    if (cgroup_parent == NULL) {
+        return -1;
+    }
+
+    if (oci_spec->linux->cgroups_path != NULL && strcmp(oci_spec->linux->cgroups_path, cgroup_parent) != 0) {
+        free(oci_spec->linux->cgroups_path);
+        oci_spec->linux->cgroups_path = cgroup_parent;
+        cgroup_parent = NULL;
+    }
+    free(cgroup_parent);
+
+    return 0;
+}
+
 static int merge_oci_cgroups_path(const char *id, oci_runtime_spec *oci_spec, const host_config *host_spec)
 {
     if (id == NULL || oci_spec == NULL || host_spec == NULL) {
@@ -2309,6 +2331,30 @@ out:
     return ret;
 }
 
+int update_oci_ulimit(oci_runtime_spec *oci_spec, const host_config *hostconfig) {
+    if (oci_spec == NULL || hostconfig == NULL) {
+        ERROR("Invalid arguments");
+        return -1;
+    }
+
+    size_t i = 0;
+    if (oci_spec->process != NULL) {
+        for (i = 0; i < oci_spec->process->rlimits_len; i++) {
+            free_defs_process_rlimits_element(oci_spec->process->rlimits[i]);
+            oci_spec->process->rlimits[i] = NULL;
+        }
+        free(oci_spec->process->rlimits);
+        oci_spec->process->rlimits = NULL;
+        oci_spec->process->rlimits_len = 0;
+    }
+
+    if (merge_conf_ulimits(oci_spec, hostconfig) != 0 || merge_global_ulimit(oci_spec) != 0) {
+        return -1;
+    }
+
+    return 0;
+}
+
 /* read oci config */
 oci_runtime_spec *load_oci_config(const char *rootpath, const char *name)
 {
-- 
2.25.1

