From fde1c406a837b849f1182d8943f1f942088b608d Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Sun, 23 Apr 2023 15:01:58 +0800
Subject: [PATCH 47/56] clean isulad shim compile relies

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 src/CMakeLists.txt                  |  12 +-
 src/cmd/isulad-shim/common.c        | 179 ++++++++++++++++++++++++++++
 src/cmd/isulad-shim/common.h        |  49 ++++++++
 src/cmd/isulad-shim/process.c       |   3 -
 test/cmd/isulad-shim/CMakeLists.txt |  13 --
 5 files changed, 238 insertions(+), 18 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f3dd3c19..02d7b13f 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -113,8 +113,16 @@ endif()
 add_executable(isulad-shim
     ${ISULAD_SHIM_SRCS}
     )
-target_include_directories(isulad-shim PUBLIC ${ISULAD_SHIM_INCS} ${SHARED_INCS})
-target_link_libraries(isulad-shim libisulad_tools)
+target_include_directories(isulad-shim PUBLIC 
+    ${ISULAD_SHIM_INCS} 
+    ${CMAKE_CURRENT_SOURCE_DIR}
+    ${COMMON_INCS}
+    ${CMAKE_BINARY_DIR}/conf
+    ${CHECKED_INCLUDE_DIRS}
+    ${SHARED_INCS}
+    ${ISULA_LIBUTILS_INCLUDE_DIR}
+    )
+target_link_libraries(isulad-shim ${ISULA_LIBUTILS_LIBRARY})
 if (ANDROID OR MUSL)
     target_link_libraries(isulad-shim ${LIBSSL_LIBRARY} ${LIBYAJL_LIBRARY})
 else()
diff --git a/src/cmd/isulad-shim/common.c b/src/cmd/isulad-shim/common.c
index f188da1e..e1ca96e1 100644
--- a/src/cmd/isulad-shim/common.c
+++ b/src/cmd/isulad-shim/common.c
@@ -27,6 +27,7 @@
 #include <stdbool.h>
 #include <stdarg.h>
 #include <limits.h>
+#include <time.h>
 
 int set_fd_no_inherited(int fd)
 {
@@ -346,3 +347,181 @@ int shim_util_safe_uint64(const char *numstr, uint64_t *converted)
     return 0;
 }
 
+void util_usleep_nointerupt(unsigned long usec)
+{
+#define SECOND_TO_USECOND_MUTIPLE 1000000
+    int ret = 0;
+    struct timespec request = { 0 };
+    struct timespec remain = { 0 };
+    if (usec == 0) {
+        return;
+    }
+
+    request.tv_sec = (time_t)(usec / SECOND_TO_USECOND_MUTIPLE);
+    request.tv_nsec = (long)((usec % SECOND_TO_USECOND_MUTIPLE) * 1000);
+
+    do {
+        ret = nanosleep(&request, &remain);
+        request = remain;
+    } while (ret == -1 && errno == EINTR);
+}
+
+void *util_smart_calloc_s(size_t unit_size, size_t count)
+{
+    if (unit_size == 0) {
+        return NULL;
+    }
+
+    if (count > (MAX_MEMORY_SIZE / unit_size)) {
+        return NULL;
+    }
+
+    return calloc(count, unit_size);
+}
+
+size_t util_array_len(const char **array)
+{
+    const char **pos;
+    size_t len = 0;
+
+    for (pos = array; pos != NULL && *pos != NULL; pos++) {
+        len++;
+    }
+
+    return len;
+}
+
+void util_free_array(char **array)
+{
+    char **p;
+
+    for (p = array; p != NULL && *p != NULL; p++) {
+        UTIL_FREE_AND_SET_NULL(*p);
+    }
+    free(array);
+}
+
+int util_grow_array(char ***orig_array, size_t *orig_capacity, size_t size, size_t increment)
+{
+    size_t add_capacity;
+    char **add_array = NULL;
+
+    if (orig_array == NULL || orig_capacity == NULL || increment == 0) {
+        return -1;
+    }
+
+    if (((*orig_array) == NULL) || ((*orig_capacity) == 0)) {
+        UTIL_FREE_AND_SET_NULL(*orig_array);
+        *orig_capacity = 0;
+    }
+
+    add_capacity = *orig_capacity;
+    while (size + 1 > add_capacity) {
+        add_capacity += increment;
+    }
+    if (add_capacity != *orig_capacity) {
+        add_array = util_smart_calloc_s(sizeof(void *), add_capacity);
+        if (add_array == NULL) {
+            return -1;
+        }
+        if (*orig_array != NULL) {
+            (void)memcpy(add_array, *orig_array, *orig_capacity * sizeof(void *));
+            UTIL_FREE_AND_SET_NULL(*orig_array);
+        }
+
+        *orig_array = add_array;
+        *orig_capacity = add_capacity;
+    }
+
+    return 0;
+}
+
+char *util_strdup_s(const char *src)
+{
+    char *dst = NULL;
+
+    if (src == NULL) {
+        return NULL;
+    }
+
+    dst = strdup(src);
+    if (dst == NULL) {
+        abort();
+    }
+
+    return dst;
+}
+
+static char **make_empty_array()
+{
+    char **res_array = NULL;
+
+    res_array = calloc(2, sizeof(char *));
+    if (res_array == NULL) {
+        return NULL;
+    }
+    res_array[0] = util_strdup_s("");
+    return res_array;
+}
+
+static char **util_shrink_array(char **orig_array, size_t new_size)
+{
+    char **new_array = NULL;
+    size_t i = 0;
+
+    if (new_size == 0) {
+        return orig_array;
+    }
+    new_array = util_smart_calloc_s(sizeof(char *), new_size);
+    if (new_array == NULL) {
+        return orig_array;
+    }
+
+    for (i = 0; i < new_size; i++) {
+        new_array[i] = orig_array[i];
+    }
+    free(orig_array);
+    return new_array;
+}
+
+char **util_string_split_multi(const char *src_str, char delim)
+{
+    int ret, tmp_errno;
+    char *token = NULL;
+    char *cur = NULL;
+    char **res_array = NULL;
+    char deli[2] = { delim, '\0' };
+    size_t count = 0;
+    size_t capacity = 0;
+    char *tmpstr = NULL;
+
+    if (src_str == NULL) {
+        return NULL;
+    }
+
+    if (src_str[0] == '\0') {
+        return make_empty_array();
+    }
+
+    tmpstr = util_strdup_s(src_str);
+    cur = tmpstr;
+    token = strsep(&cur, deli);
+    while (token != NULL) {
+        ret = util_grow_array(&res_array, &capacity, count + 1, 16);
+        if (ret < 0) {
+            goto err_out;
+        }
+        res_array[count] = util_strdup_s(token);
+        count++;
+        token = strsep(&cur, deli);
+    }
+    free(tmpstr);
+    return util_shrink_array(res_array, count + 1);
+
+err_out:
+    tmp_errno = errno;
+    free(tmpstr);
+    util_free_array(res_array);
+    errno = tmp_errno;
+    return NULL;
+}
\ No newline at end of file
diff --git a/src/cmd/isulad-shim/common.h b/src/cmd/isulad-shim/common.h
index 91808295..3de16ace 100644
--- a/src/cmd/isulad-shim/common.h
+++ b/src/cmd/isulad-shim/common.h
@@ -58,6 +58,43 @@ extern "C" {
 #define CONTAINER_ACTION_REBOOT 129
 #define CONTAINER_ACTION_SHUTDOWN 130
 
+
+void util_usleep_nointerupt(unsigned long usec);
+/**
+ * retry_cnt: max count of call cb;
+ * interval_us: how many us to sleep, after call cb;
+ * cb: retry call function;
+ * return:
+ *  0 is cb successful at least once;
+ *  1 is all cb are failure;
+*/
+#define DO_RETRY_CALL(retry_cnt, interval_us, ret, cb, ...) do {    \
+        size_t i = 0;                                               \
+        for(; i < retry_cnt; i++) {                                 \
+            ret = cb(##__VA_ARGS__);                                  \
+            if (ret == 0) {                                         \
+                break;                                              \
+            }                                                       \
+            util_usleep_nointerupt(interval_us);                    \
+        }                                                           \
+    } while(0)
+
+#define UTIL_FREE_AND_SET_NULL(p) \
+    do {                          \
+        if ((p) != NULL) {        \
+            free((void *)(p));    \
+            (p) = NULL;           \
+        }                         \
+    } while (0)
+
+#if __WORDSIZE == 64
+// current max user memory for 64-machine is 2^47 B
+#define MAX_MEMORY_SIZE ((size_t)1 << 47)
+#else
+// current max user memory for 32-machine is 2^31 B
+#define MAX_MEMORY_SIZE ((size_t)1 << 31)
+#endif
+
 ssize_t read_nointr(int fd, void *buf, size_t count);
 ssize_t write_nointr(int fd, const void *buf, size_t count);
 
@@ -79,6 +116,18 @@ int open_no_inherit(const char *path, int flag, mode_t mode);
 
 int shim_util_safe_uint64(const char *numstr, uint64_t *converted);
 
+void *util_smart_calloc_s(size_t unit_size, size_t count);
+
+size_t util_array_len(const char **array);
+
+void util_free_array(char **array);
+
+int util_grow_array(char ***orig_array, size_t *orig_capacity, size_t size, size_t increment);
+
+char *util_strdup_s(const char *src);
+
+char **util_string_split_multi(const char *src_str, char delim);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index a676e7ce..a5e0bd39 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -37,9 +37,6 @@
 
 #include "common.h"
 #include "terminal.h"
-#include "utils_array.h"
-#include "utils_string.h"
-#include "utils.h"
 
 #define MAX_EVENTS 100
 #define DEFAULT_IO_COPY_BUF (16 * 1024)
diff --git a/test/cmd/isulad-shim/CMakeLists.txt b/test/cmd/isulad-shim/CMakeLists.txt
index dc293f6d..e5c1cd6e 100644
--- a/test/cmd/isulad-shim/CMakeLists.txt
+++ b/test/cmd/isulad-shim/CMakeLists.txt
@@ -6,26 +6,13 @@ add_executable(${EXE}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim/process.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim/common.c
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim/terminal.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_string.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_array.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_file.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_convert.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_verify.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_regex.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256/sha256.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/map.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/rb_tree.c
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/path.c
     isulad-shim_ut.cc)
 
 target_include_directories(${EXE} PUBLIC
     ${GTEST_INCLUDE_DIR}
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/cmd/isulad-shim
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
     ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
-    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
     ${CMAKE_BINARY_DIR}/conf
     )
 target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
-- 
2.25.1

