From 13bc364cb5d8c03b701dde2b2811be84ee608e92 Mon Sep 17 00:00:00 2001
From: xuxuepeng <xuxuepeng1@huawei.com>
Date: Fri, 24 Nov 2023 14:18:32 +0800
Subject: [PATCH 41/65] Acquire system info in on demand

Signed-off-by: xuxuepeng <xuxuepeng1@huawei.com>
---
 src/cmd/isulad/main.c                         |  2 --
 src/daemon/common/sysinfo.c                   | 10 +-----
 src/daemon/common/sysinfo.h                   |  6 ++++
 .../executor/container_cb/execution_create.c  | 32 +++++++++----------
 .../executor/container_cb/execution_extend.c  |  9 +++++-
 src/daemon/modules/spec/verify.c              | 32 ++++++++-----------
 src/daemon/modules/spec/verify.h              |  5 +--
 test/mocks/sysinfo_mock.cc                    |  8 +++++
 test/mocks/sysinfo_mock.h                     |  1 +
 test/mocks/verify_mock.cc                     |  4 +--
 test/mocks/verify_mock.h                      |  2 +-
 11 files changed, 60 insertions(+), 51 deletions(-)

diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index 5b971a72..95454e2a 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -1765,8 +1765,6 @@ int main(int argc, char **argv)
 
     update_isulad_rlimits();
 
-    (void)get_sys_info(true);
-
     clock_gettime(CLOCK_MONOTONIC, &t_start);
 
     if (pre_init_daemon(argc, argv) != 0) {
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index 39338925..28665834 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -19,7 +19,6 @@
 #include <errno.h>
 #include <sys/sysinfo.h>
 
-#include <isula_libutils/auto_cleanup.h>
 #include <isula_libutils/log.h>
 
 #include "err_msg.h"
@@ -30,8 +29,6 @@
 #define etcOsRelease "/etc/os-release"
 #define altOsRelease "/usr/lib/os-release"
 
-static sysinfo_t *g_sysinfo = NULL;
-
 static char *get_pagesize(const char *pline)
 {
     size_t headlen;
@@ -382,10 +379,6 @@ sysinfo_t *get_sys_info(bool quiet)
     sysinfo_t *sysinfo = NULL;
     int ret = 0;
 
-    if (g_sysinfo != NULL) {
-        return g_sysinfo;
-    }
-
     sysinfo = util_common_calloc_s(sizeof(sysinfo_t));
     if (sysinfo == NULL) {
         ERROR("Out of memory");
@@ -413,7 +406,6 @@ sysinfo_t *get_sys_info(bool quiet)
     if (ret != 0) {
         goto out;
     }
-    g_sysinfo = sysinfo;
 out:
     if (ret != 0) {
         free_sysinfo(sysinfo);
@@ -577,7 +569,7 @@ char *sysinfo_cgroup_controller_cpurt_mnt_path(void)
     __isula_auto_free char *mnt = NULL;
     __isula_auto_free char *root = NULL;
     char fpath[PATH_MAX] = { 0 };
-    sysinfo_t *sysinfo = NULL;
+    __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
 
     sysinfo = get_sys_info(true);
     if (sysinfo == NULL) {
diff --git a/src/daemon/common/sysinfo.h b/src/daemon/common/sysinfo.h
index 363576a9..cb44d1c5 100644
--- a/src/daemon/common/sysinfo.h
+++ b/src/daemon/common/sysinfo.h
@@ -21,6 +21,7 @@ extern "C" {
 
 #include <stdbool.h>
 #include <stdint.h>
+#include <isula_libutils/auto_cleanup.h>
 
 #include "cgroup.h"
 
@@ -96,6 +97,11 @@ void free_mounts_info(mountinfo_t **minfos);
 
 char *sysinfo_cgroup_controller_cpurt_mnt_path(void);
 
+// define auto free function callback for sysinfo_t
+define_auto_cleanup_callback(free_sysinfo, sysinfo_t)
+// define auto free macro for sysinfo_t
+#define __isula_auto_sysinfo_t auto_cleanup_tag(free_sysinfo)
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/daemon/executor/container_cb/execution_create.c b/src/daemon/executor/container_cb/execution_create.c
index 6b6c3b75..ca2a9163 100644
--- a/src/daemon/executor/container_cb/execution_create.c
+++ b/src/daemon/executor/container_cb/execution_create.c
@@ -145,7 +145,7 @@ static int merge_external_rootfs_to_host_config(host_config *host_spec, const ch
     return 0;
 }
 
-static host_config *get_host_spec(const container_create_request *request)
+static host_config *get_host_spec(const container_create_request *request, const sysinfo_t *sysinfo)
 {
     host_config *host_spec = NULL;
 
@@ -158,7 +158,7 @@ static host_config *get_host_spec(const container_create_request *request)
         goto error_out;
     }
 
-    if (verify_host_config_settings(host_spec, false)) {
+    if (verify_host_config_settings(host_spec, sysinfo, false)) {
         ERROR("Failed to verify host config settings");
         goto error_out;
     }
@@ -1109,17 +1109,9 @@ static int preparate_runtime_environment(const container_create_request *request
     return 0;
 }
 
-static int adapt_host_spec(host_config *host_spec)
+static int adapt_host_spec(host_config *host_spec, const sysinfo_t *sysinfo)
 {
     int ret = 0;
-    sysinfo_t *sysinfo = NULL;
-
-    sysinfo = get_sys_info(true);
-    if (sysinfo == NULL) {
-        ERROR("Can not get system info");
-        ret = -1;
-        goto out;
-    }
 
     if (host_spec->memory > 0 && host_spec->memory_swap == 0 && sysinfo->cgmeminfo.swap) {
         if (host_spec->memory > (INT64_MAX / 2)) {
@@ -1136,14 +1128,14 @@ out:
 }
 
 static int get_basic_spec(const container_create_request *request, host_config **host_spec,
-                          container_config **container_spec)
+                          container_config **container_spec, const sysinfo_t *sysinfo)
 {
-    *host_spec = get_host_spec(request);
+    *host_spec = get_host_spec(request, sysinfo);
     if (*host_spec == NULL) {
         return -1;
     }
 
-    if (adapt_host_spec(*host_spec) != 0) {
+    if (adapt_host_spec(*host_spec, sysinfo) != 0) {
         return -1;
     }
 
@@ -1393,6 +1385,7 @@ int container_create_cb(const container_create_request *request, container_creat
     int ret = 0;
     bool skip_id_name_manage = false;
     bool skip_sandbox_key_manage = false;
+    __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
 
     DAEMON_CLEAR_ERRMSG();
 
@@ -1413,7 +1406,14 @@ int container_create_cb(const container_create_request *request, container_creat
         goto clean_nameindex;
     }
 
-    if (get_basic_spec(request, &host_spec, &container_spec) != 0) {
+    sysinfo = get_sys_info(true);
+    if (sysinfo == NULL) {
+        ERROR("Failed to get system info");
+        cc = ISULAD_ERR_EXEC;
+        goto clean_nameindex;
+    }
+
+    if (get_basic_spec(request, &host_spec, &container_spec, sysinfo) != 0) {
         cc = ISULAD_ERR_INPUT;
         goto clean_container_root_dir;
     }
@@ -1540,7 +1540,7 @@ int container_create_cb(const container_create_request *request, container_creat
         goto clean_netns;
     }
 
-    if (verify_container_settings(oci_spec) != 0) {
+    if (verify_container_settings(oci_spec, sysinfo) != 0) {
         ERROR("Failed to verify container settings");
         cc = ISULAD_ERR_EXEC;
         goto umount_channel;
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index de017b4e..25ec5d3b 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -1110,8 +1110,15 @@ static int update_host_config_check(container_t *cont, host_config *hostconfig)
 {
     int ret = 0;
     const char *id = cont->common_config->id;
+    __isula_auto_sysinfo_t sysinfo_t *sysinfo = NULL;
 
-    ret = verify_host_config_settings(hostconfig, true);
+    sysinfo = get_sys_info(true);
+    if (sysinfo == NULL) {
+        ERROR("Failed to get system info for updating container %s", id);
+        return -1;
+    }
+
+    ret = verify_host_config_settings(hostconfig, sysinfo, true);
     if (ret != 0) {
         return -1;
     }
diff --git a/src/daemon/modules/spec/verify.c b/src/daemon/modules/spec/verify.c
index 2a8b3259..b9e3c606 100644
--- a/src/daemon/modules/spec/verify.c
+++ b/src/daemon/modules/spec/verify.c
@@ -41,7 +41,6 @@
 #include "constants.h"
 #include "err_msg.h"
 #include "isula_libutils/log.h"
-#include "sysinfo.h"
 #include "selinux_label.h"
 #include "image_api.h"
 #include "utils.h"
@@ -1614,16 +1613,13 @@ out:
 }
 
 /* verify container settings */
-int verify_container_settings(const oci_runtime_spec *container)
+int verify_container_settings(const oci_runtime_spec *container, const sysinfo_t *sysinfo)
 {
     int ret = 0;
-    sysinfo_t *sysinfo = NULL;
 
-    sysinfo = get_sys_info(true);
-    if (sysinfo == NULL) {
-        ERROR("Can not get system info");
-        ret = -1;
-        goto out;
+    if (container == NULL || sysinfo == NULL) {
+        ERROR("Invalid input arguments for verifying container settings");
+        return -1;
     }
 
     if (!util_valid_host_name(container->hostname)) {
@@ -1987,16 +1983,9 @@ static int host_config_settings_restart_policy(const host_config *hostconfig)
     return verify_restart_policy_name(rp, hostconfig);
 }
 
-static int host_config_settings_with_sysinfo(host_config *hostconfig, bool update)
+static int host_config_settings_with_sysinfo(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
 {
     int ret = 0;
-    sysinfo_t *sysinfo = NULL;
-
-    sysinfo = get_sys_info(true);
-    if (sysinfo == NULL) {
-        ERROR("Can not get system info");
-        return -1;
-    }
 
     ret = verify_host_config_hugetlbs(sysinfo, &(hostconfig->hugetlbs), &(hostconfig->hugetlbs_len));
     if (ret != 0) {
@@ -2055,7 +2044,7 @@ out:
 }
 
 /* verify host config settings */
-int verify_host_config_settings(host_config *hostconfig, bool update)
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
 {
     int ret = 0;
 #ifdef ENABLE_USERNS_REMAP
@@ -2066,6 +2055,13 @@ int verify_host_config_settings(host_config *hostconfig, bool update)
         goto out;
     }
 
+    if (sysinfo == NULL) {
+        ERROR("Invalid sysinfo for verifying host config settings");
+        isulad_set_error_message("Invalid sysinfo for verifying host config settings");
+        ret = -1;
+        goto out;
+    }
+
 #ifdef ENABLE_USERNS_REMAP
     if (userns_remap != NULL && hostconfig->user_remap != NULL) {
         ERROR("invalid --user-remap command option, daemon already configed --userns-remap");
@@ -2081,7 +2077,7 @@ int verify_host_config_settings(host_config *hostconfig, bool update)
         goto out;
     }
 
-    ret = host_config_settings_with_sysinfo(hostconfig, update);
+    ret = host_config_settings_with_sysinfo(hostconfig, sysinfo, update);
     if (ret != 0) {
         goto out;
     }
diff --git a/src/daemon/modules/spec/verify.h b/src/daemon/modules/spec/verify.h
index 21e8fba8..0224f9fb 100644
--- a/src/daemon/modules/spec/verify.h
+++ b/src/daemon/modules/spec/verify.h
@@ -20,18 +20,19 @@
 #include "isula_libutils/oci_runtime_spec.h"
 #include "isula_libutils/host_config.h"
 #include "isula_libutils/container_config.h"
+#include "sysinfo.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-int verify_container_settings(const oci_runtime_spec *container);
+int verify_container_settings(const oci_runtime_spec *container, const sysinfo_t *sysinfo);
 
 int verify_oci_hook(const oci_runtime_spec_hooks *h);
 
 int verify_container_settings_start(const oci_runtime_spec *oci_spec);
 
-int verify_host_config_settings(host_config *hostconfig, bool update);
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update);
 
 int verify_container_config(const container_config *container_spec, const char *runtime);
 
diff --git a/test/mocks/sysinfo_mock.cc b/test/mocks/sysinfo_mock.cc
index f9abc786..d8f33f84 100644
--- a/test/mocks/sysinfo_mock.cc
+++ b/test/mocks/sysinfo_mock.cc
@@ -63,6 +63,14 @@ char *validate_hugetlb(const char *pagesize, uint64_t limit)
     return nullptr;
 }
 
+sysinfo_t *get_sys_info(bool quiet)
+{
+    if (g_sysinfo_mock != nullptr) {
+        return g_sysinfo_mock->GetSysInfo(quiet);
+    }
+    return nullptr;
+}
+
 void free_sysinfo(sysinfo_t *sysinfo)
 {
     if (g_sysinfo_mock != nullptr) {
diff --git a/test/mocks/sysinfo_mock.h b/test/mocks/sysinfo_mock.h
index 45208b0f..2b8e926d 100644
--- a/test/mocks/sysinfo_mock.h
+++ b/test/mocks/sysinfo_mock.h
@@ -26,6 +26,7 @@ public:
     MOCK_METHOD1(FreeMountsInfo, void(mountinfo_t **minfos));
     MOCK_METHOD0(GetDefaultHugePageSize, char *(void));
     MOCK_METHOD2(ValidateHugetlb, char*(const char *pagesize, uint64_t limit));
+    MOCK_METHOD1(GetSysInfo, sysinfo_t *(bool quiet));
     MOCK_METHOD1(FreeSysinfo, void(sysinfo_t *sysinfo));
 };
 
diff --git a/test/mocks/verify_mock.cc b/test/mocks/verify_mock.cc
index 0e7e7461..4c481676 100644
--- a/test/mocks/verify_mock.cc
+++ b/test/mocks/verify_mock.cc
@@ -24,10 +24,10 @@ void MockVerify_SetMock(MockVerify *mock)
     g_verify_mock = mock;
 }
 
-int verify_host_config_settings(host_config *hostconfig, bool update)
+int verify_host_config_settings(host_config *hostconfig, const sysinfo_t *sysinfo, bool update)
 {
     if (g_verify_mock != nullptr) {
-        return g_verify_mock->VerifyHostConfigSettings(hostconfig, update);
+        return g_verify_mock->VerifyHostConfigSettings(hostconfig, sysinfo, update);
     }
     return 0;
 }
diff --git a/test/mocks/verify_mock.h b/test/mocks/verify_mock.h
index 7890159f..b9ad8627 100644
--- a/test/mocks/verify_mock.h
+++ b/test/mocks/verify_mock.h
@@ -21,7 +21,7 @@
 
 class MockVerify {
 public:
-    MOCK_METHOD2(VerifyHostConfigSettings, int(host_config *hostconfig, bool update));
+    MOCK_METHOD3(VerifyHostConfigSettings, int(host_config *hostconfig, const sysinfo_t *sysinfo, bool update));
 };
 
 void MockVerify_SetMock(MockVerify* mock);
-- 
2.42.0

