!525 update to v2.0.18

From: @zh_xiaoyu 
Reviewed-by: @duguhaotian, @taotao-sauce 
Signed-off-by: @duguhaotian
This commit is contained in:
openeuler-ci-bot 2023-01-03 11:32:35 +00:00 committed by Gitee
commit 1e0c9a90b3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
69 changed files with 8 additions and 11119 deletions

View File

@ -1,158 +0,0 @@
From f9cb1b86511fac1c3a7f11fdaa4c9c20dc889068 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sun, 9 Oct 2022 19:00:38 +0800
Subject: [PATCH 01/43] use epoll instead of select for wait_exit_fifo
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../modules/service/service_container.c | 73 ++++++++++++++-----
src/utils/cutils/mainloop.c | 5 ++
src/utils/cutils/mainloop.h | 4 +
3 files changed, 62 insertions(+), 20 deletions(-)
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 2f688f57..2b3c8794 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -52,6 +52,7 @@
#include "runtime_api.h"
#include "error.h"
#include "io_handler.h"
+#include "mainloop.h"
#include "constants.h"
#include "event_type.h"
#include "utils_array.h"
@@ -662,37 +663,69 @@ static int verify_mounts(const container_t *cont)
return 0;
}
-static void wait_exit_fifo(const char *id, const int exit_fifo_fd)
+static int wait_exit_fifo_epoll_callback(int fd, uint32_t events, void *cbdata, struct epoll_descr *descr)
{
- int nret = 0;
+ int ret = EPOLL_LOOP_HANDLE_CLOSE;
int exit_code = 0;
- const int WAIT_TIMEOUT = 3;
- fd_set set;
- struct timeval timeout;
+ char *container_id = cbdata;
- FD_ZERO(&set);
- FD_SET(exit_fifo_fd, &set);
+ if (util_read_nointr(fd, &exit_code, sizeof(int)) <= 0) {
+ ERROR("Failed to read exit fifo fd for container %s", container_id);
+ return ret;
+ }
- timeout.tv_sec = WAIT_TIMEOUT;
- timeout.tv_usec = 0;
+ ERROR("The container %s 's monitor on fd %d has exited: %d", container_id, fd, exit_code);
+ return ret;
+}
- nret = select(exit_fifo_fd + 1, &set, NULL, NULL, &timeout);
- if (nret < 0) {
- ERROR("Wait containers %s 's monitor on fd %d error: %s", id, exit_fifo_fd,
- errno ? strerror(errno) : "");
+static void wait_exit_fifo_timeout_callback(void *cbdata)
+{
+ char *container_id = NULL;
+
+ if (cbdata == NULL) {
+ ERROR("Invalid cbdata");
return;
}
+ container_id = (char *)cbdata;
- if (nret == 0) {
- // timeout
- // maybe monitor still cleanup cgroup and processes,
- // or monitor doesn't step in cleanup at all
- ERROR("Wait containers %s 's monitor on fd %d timeout", id, exit_fifo_fd);
+ // timeout
+ // maybe monitor still cleanup cgroup and processes,
+ // or monitor doesn't step in cleanup at all
+ ERROR("Wait container %s 's monitor timeout", container_id);
+}
+
+static void wait_exit_fifo(const char *id, const int exit_fifo_fd)
+{
+ int nret = 0;
+ const int WAIT_TIMEOUT = 3000;
+ char *container_id = NULL;
+ struct epoll_descr descr = { 0 };
+
+ nret = epoll_loop_open(&descr);
+ if (nret != 0) {
+ ERROR("Failed to create epoll for container %s", id);
return;
}
- (void)util_read_nointr(exit_fifo_fd, &exit_code, sizeof(int));
- ERROR("The container %s 's monitor on fd %d has exited: %d", id, exit_fifo_fd, exit_code);
+ container_id = util_strdup_s(id);
+ nret = epoll_loop_add_handler(&descr, exit_fifo_fd, wait_exit_fifo_epoll_callback, container_id);
+ if (nret != 0) {
+ ERROR("Failed to add epoll handler for container %s", id);
+ goto out;
+ }
+
+ descr.timeout_cb = wait_exit_fifo_timeout_callback;
+ descr.timeout_cbdata = container_id;
+ nret = epoll_loop(&descr, WAIT_TIMEOUT);
+ if (nret != 0) {
+ ERROR("Wait container %s 's monitor on fd %d error: %s", id, exit_fifo_fd,
+ errno ? strerror(errno) : "");
+ goto out;
+ }
+
+out:
+ free(container_id);
+ epoll_loop_close(&descr);
}
static int do_start_container(container_t *cont, const char *console_fifos[], bool reset_rm, pid_ppid_info_t *pid_info)
diff --git a/src/utils/cutils/mainloop.c b/src/utils/cutils/mainloop.c
index cf0b41a3..1028087a 100644
--- a/src/utils/cutils/mainloop.c
+++ b/src/utils/cutils/mainloop.c
@@ -56,6 +56,9 @@ int epoll_loop(struct epoll_descr *descr, int t)
}
if (ep_fds == 0 && t != 0) {
+ if (descr->timeout_cb != NULL) {
+ descr->timeout_cb(descr->timeout_cbdata);
+ }
goto out;
}
@@ -139,6 +142,8 @@ int epoll_loop_open(struct epoll_descr *descr)
}
linked_list_init(&(descr->handler_list));
+ descr->timeout_cb = NULL;
+ descr->timeout_cbdata = NULL;
return 0;
}
diff --git a/src/utils/cutils/mainloop.h b/src/utils/cutils/mainloop.h
index b9099a74..5124b33a 100644
--- a/src/utils/cutils/mainloop.h
+++ b/src/utils/cutils/mainloop.h
@@ -18,9 +18,13 @@
#include <stdint.h>
#include "linked_list.h"
+typedef void (*epoll_timeout_callback_t)(void *data);
+
struct epoll_descr {
int fd;
struct linked_list handler_list;
+ epoll_timeout_callback_t timeout_cb;
+ void *timeout_cbdata;
};
#define EPOLL_LOOP_HANDLE_CONTINUE 0
--
2.25.1

View File

@ -1,155 +0,0 @@
From 864a500f18d56aebeaa71960f10791386212b18b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Thu, 13 Oct 2022 15:55:08 +0800
Subject: [PATCH 02/43] add namespace util UT
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/CMakeLists.txt | 15 ++++
test/cutils/utils_namespace/CMakeLists.txt | 16 ++++
.../utils_namespace/utils_namespace_ut.cc | 82 +++++++++++++++++++
3 files changed, 113 insertions(+)
create mode 100644 test/cutils/utils_namespace/CMakeLists.txt
create mode 100644 test/cutils/utils_namespace/utils_namespace_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index b549f844..31408f18 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -1,7 +1,22 @@
project(iSulad_UT)
+aux_source_directory(${CMAKE_SOURCE_DIR}/src/utils/cutils local_cutils_srcs)
+aux_source_directory(${CMAKE_SOURCE_DIR}/src/utils/cutils/map local_cutils_map_srcs)
+
+add_library(libutils_ut STATIC
+ ${local_cutils_srcs}
+ ${local_cutils_map_srcs}
+ )
+set_target_properties(libutils_ut PROPERTIES PREFIX "")
+target_include_directories(libutils_ut
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/common
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils
+ PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils/map
+)
+
add_subdirectory(utils_string)
add_subdirectory(utils_convert)
add_subdirectory(utils_array)
add_subdirectory(utils_base64)
add_subdirectory(utils_pwgr)
+add_subdirectory(utils_namespace)
diff --git a/test/cutils/utils_namespace/CMakeLists.txt b/test/cutils/utils_namespace/CMakeLists.txt
new file mode 100644
index 00000000..8add4a71
--- /dev/null
+++ b/test/cutils/utils_namespace/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_namespace_ut)
+
+add_executable(${EXE}
+ utils_namespace_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_namespace/utils_namespace_ut.cc b/test/cutils/utils_namespace/utils_namespace_ut.cc
new file mode 100644
index 00000000..da50d503
--- /dev/null
+++ b/test/cutils/utils_namespace/utils_namespace_ut.cc
@@ -0,0 +1,82 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: hejunjie
+ * Create: 2022-04-08
+ * Description: utils_pwgr unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "namespace.h"
+
+TEST(utils_namespace, test_namespace_is_host)
+{
+ ASSERT_EQ(namespace_is_host(SHARE_NAMESPACE_HOST), true);
+ ASSERT_EQ(namespace_is_host(SHARE_NAMESPACE_NONE), false);
+ ASSERT_EQ(namespace_is_host(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_none)
+{
+ ASSERT_EQ(namespace_is_none(SHARE_NAMESPACE_HOST), false);
+ ASSERT_EQ(namespace_is_none(SHARE_NAMESPACE_NONE), true);
+ ASSERT_EQ(namespace_is_none(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_container)
+{
+ std::string con = "container:test";
+ ASSERT_EQ(namespace_is_container(SHARE_NAMESPACE_HOST), false);
+ ASSERT_EQ(namespace_is_container(con.c_str()), true);
+ ASSERT_EQ(namespace_is_container(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_bridge)
+{
+ ASSERT_EQ(namespace_is_bridge(SHARE_NAMESPACE_HOST), false);
+ ASSERT_EQ(namespace_is_bridge(SHARE_NAMESPACE_BRIDGE), true);
+ ASSERT_EQ(namespace_is_bridge(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_file)
+{
+ ASSERT_EQ(namespace_is_file(SHARE_NAMESPACE_HOST), false);
+ ASSERT_EQ(namespace_is_file(SHARE_NAMESPACE_FILE), true);
+ ASSERT_EQ(namespace_is_file(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_shareable)
+{
+ ASSERT_EQ(namespace_is_shareable(SHARE_NAMESPACE_HOST), false);
+ ASSERT_EQ(namespace_is_shareable(SHARE_NAMESPACE_SHAREABLE), true);
+ ASSERT_EQ(namespace_is_shareable(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_get_connected_container)
+{
+ std::string con = "container:test";
+ char *ret = nullptr;
+ ret = namespace_get_connected_container(con.c_str());
+ ASSERT_STREQ(ret, "test");
+ ASSERT_EQ(namespace_get_connected_container(SHARE_NAMESPACE_SHAREABLE), nullptr);
+ ASSERT_EQ(namespace_get_connected_container(nullptr), nullptr);
+}
+
+TEST(utils_namespace, test_namespace_get_host_namespace_path)
+{
+ ASSERT_EQ(namespace_get_host_namespace_path(nullptr), nullptr);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_PID), SHARE_NAMESPACE_PID_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_NETWORK), SHARE_NAMESPACE_NET_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_IPC), SHARE_NAMESPACE_IPC_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_UTS), SHARE_NAMESPACE_UTS_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_MOUNT), SHARE_NAMESPACE_MNT_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_USER), SHARE_NAMESPACE_USER_HOST_PATH);
+ ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_CGROUP), SHARE_NAMESPACE_CGROUP_HOST_PATH);
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,182 +0,0 @@
From d1061efc9a83df659f5aeab57352b9247380217a Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Thu, 13 Oct 2022 16:56:10 +0800
Subject: [PATCH 03/43] refactor build system of cutils ut
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/utils_array/CMakeLists.txt | 13 +------------
test/cutils/utils_base64/CMakeLists.txt | 14 +-------------
test/cutils/utils_convert/CMakeLists.txt | 13 +------------
test/cutils/utils_namespace/utils_namespace_ut.cc | 6 +++---
test/cutils/utils_pwgr/CMakeLists.txt | 14 +-------------
test/cutils/utils_string/CMakeLists.txt | 13 +------------
6 files changed, 8 insertions(+), 65 deletions(-)
diff --git a/test/cutils/utils_array/CMakeLists.txt b/test/cutils/utils_array/CMakeLists.txt
index 427e588d..71733e31 100644
--- a/test/cutils/utils_array/CMakeLists.txt
+++ b/test/cutils/utils_array/CMakeLists.txt
@@ -3,17 +3,6 @@ project(iSulad_UT)
SET(EXE utils_array_ut)
add_executable(${EXE}
- ${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
utils_array_ut.cc)
target_include_directories(${EXE} PUBLIC
@@ -25,5 +14,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
)
set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,calloc")
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_base64/CMakeLists.txt b/test/cutils/utils_base64/CMakeLists.txt
index 3c7b72dc..d5b99361 100644
--- a/test/cutils/utils_base64/CMakeLists.txt
+++ b/test/cutils/utils_base64/CMakeLists.txt
@@ -3,18 +3,6 @@ project(iSulad_UT)
SET(EXE utils_base64_ut)
add_executable(${EXE}
- ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/utils_base64.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_string.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/path.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/map.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/rb_tree.c
utils_base64_ut.cc)
target_include_directories(${EXE} PUBLIC
@@ -25,5 +13,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
)
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_convert/CMakeLists.txt b/test/cutils/utils_convert/CMakeLists.txt
index 809a798e..30068208 100644
--- a/test/cutils/utils_convert/CMakeLists.txt
+++ b/test/cutils/utils_convert/CMakeLists.txt
@@ -3,17 +3,6 @@ project(iSulad_UT)
SET(EXE utils_convert_ut)
add_executable(${EXE}
- ${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
utils_convert_ut.cc)
target_include_directories(${EXE} PUBLIC
@@ -24,5 +13,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
)
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_namespace/utils_namespace_ut.cc b/test/cutils/utils_namespace/utils_namespace_ut.cc
index da50d503..1c652e9f 100644
--- a/test/cutils/utils_namespace/utils_namespace_ut.cc
+++ b/test/cutils/utils_namespace/utils_namespace_ut.cc
@@ -8,9 +8,9 @@
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
- * Author: hejunjie
- * Create: 2022-04-08
- * Description: utils_pwgr unit test
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils namespace unit test
*******************************************************************************/
#include <gtest/gtest.h>
diff --git a/test/cutils/utils_pwgr/CMakeLists.txt b/test/cutils/utils_pwgr/CMakeLists.txt
index 548718da..5938991e 100644
--- a/test/cutils/utils_pwgr/CMakeLists.txt
+++ b/test/cutils/utils_pwgr/CMakeLists.txt
@@ -3,18 +3,6 @@ project(iSulad_UT)
SET(EXE utils_pwgr_ut)
add_executable(${EXE}
- ${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/cutils/utils_pwgr.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
utils_pwgr_ut.cc)
target_include_directories(${EXE} PUBLIC
@@ -25,5 +13,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
)
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_string/CMakeLists.txt b/test/cutils/utils_string/CMakeLists.txt
index c86141ef..1343f4e6 100644
--- a/test/cutils/utils_string/CMakeLists.txt
+++ b/test/cutils/utils_string/CMakeLists.txt
@@ -3,17 +3,6 @@ project(iSulad_UT)
SET(EXE utils_string_ut)
add_executable(${EXE}
- ${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/path.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/map.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map/rb_tree.c
utils_string_ut.cc)
target_include_directories(${EXE} PUBLIC
@@ -25,5 +14,5 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
)
set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,util_strdup_s -Wl,--wrap,calloc -Wl,--wrap,strcat_s")
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
--
2.25.1

View File

@ -1,68 +0,0 @@
From 8c33633d26ec5d5eb9b5ad415afc114cf6c232f3 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 11 Oct 2022 20:34:18 +0800
Subject: [PATCH 04/43] run storage layers ut with non-root
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/CMakeLists.txt | 2 ++
test/image/oci/storage/layers/storage_layers_ut.cc | 10 ++++++++--
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 940de5ce..a36f68c5 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,5 +1,7 @@
project(iSulad_UT)
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized")
+
function(gmock_find_library _name)
find_library(${_name}
NAMES ${ARGN}
diff --git a/test/image/oci/storage/layers/storage_layers_ut.cc b/test/image/oci/storage/layers/storage_layers_ut.cc
index 0b481a95..87dfb4a1 100644
--- a/test/image/oci/storage/layers/storage_layers_ut.cc
+++ b/test/image/oci/storage/layers/storage_layers_ut.cc
@@ -153,7 +153,8 @@ protected:
MockDriverQuota_SetMock(&m_driver_quota_mock);
struct storage_module_init_options opts = {0};
- std::string isulad_dir = "/var/lib/isulad/";
+ std::string isulad_dir = "/tmp/isulad/";
+ mkdir(isulad_dir.c_str(), 0755);
std::string root_dir = isulad_dir + "data";
std::string run_dir = isulad_dir + "data/run";
std::string data_dir = GetDirectory() + "/data";
@@ -167,6 +168,9 @@ protected:
ASSERT_STRNE(util_clean_path(run_dir.c_str(), real_run_path, sizeof(real_run_path)), nullptr);
opts.storage_run_root = strdup(real_run_path);
opts.driver_name = strdup("overlay");
+ opts.driver_opts = static_cast<char **>(util_smart_calloc_s(sizeof(char *), 1));
+ opts.driver_opts[0] = strdup("overlay2.skip_mount_home=true");
+ opts.driver_opts_len = 1;
EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl));
ASSERT_EQ(layer_store_init(&opts), 0);
@@ -174,6 +178,8 @@ protected:
free(opts.storage_root);
free(opts.storage_run_root);
free(opts.driver_name);
+ free(opts.driver_opts[0]);
+ free(opts.driver_opts);
}
void TearDown() override
@@ -183,7 +189,7 @@ protected:
layer_store_exit();
layer_store_cleanup();
- std::string rm_command = "rm -rf /var/lib/isulad/data";
+ std::string rm_command = "rm -rf /tmp/isulad/";
ASSERT_EQ(system(rm_command.c_str()), 0);
}
--
2.25.1

View File

@ -1,37 +0,0 @@
From 0ddc58b78b0ca7d6c1cb52b10e3fa03f0da69326 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 15 Oct 2022 16:51:38 +0800
Subject: [PATCH 05/43] add extern C for mainloop header
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/utils/cutils/mainloop.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/src/utils/cutils/mainloop.h b/src/utils/cutils/mainloop.h
index 5124b33a..7a4f1cfd 100644
--- a/src/utils/cutils/mainloop.h
+++ b/src/utils/cutils/mainloop.h
@@ -18,6 +18,10 @@
#include <stdint.h>
#include "linked_list.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef void (*epoll_timeout_callback_t)(void *data);
struct epoll_descr {
@@ -42,4 +46,8 @@ extern int epoll_loop_open(struct epoll_descr *descr);
extern int epoll_loop_close(struct epoll_descr *descr);
+#ifdef __cplusplus
+}
+#endif
+
#endif
--
2.25.1

View File

@ -1,517 +0,0 @@
From f7089859d339cfe2b33ab701c02e1f424e4bd248 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 15 Oct 2022 16:52:32 +0800
Subject: [PATCH 06/43] add UT for mainloop and network
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
test/cutils/CMakeLists.txt | 3 +
test/cutils/mainloop/CMakeLists.txt | 27 ++++++
test/cutils/mainloop/mainloop_ut.cc | 55 ++++++++++++
test/cutils/utils_network/CMakeLists.txt | 27 ++++++
test/cutils/utils_network/utils_network_ut.cc | 89 +++++++++++++++++++
test/cutils/utils_string/utils_string_ut.cc | 24 +++++
test/mocks/mainloop_mock.cc | 58 ++++++++++++
test/mocks/mainloop_mock.h | 33 +++++++
test/mocks/utils_network_mock.cc | 60 +++++++++++++
test/mocks/utils_network_mock.h | 34 +++++++
10 files changed, 410 insertions(+)
create mode 100644 test/cutils/mainloop/CMakeLists.txt
create mode 100644 test/cutils/mainloop/mainloop_ut.cc
create mode 100644 test/cutils/utils_network/CMakeLists.txt
create mode 100644 test/cutils/utils_network/utils_network_ut.cc
create mode 100644 test/mocks/mainloop_mock.cc
create mode 100644 test/mocks/mainloop_mock.h
create mode 100644 test/mocks/utils_network_mock.cc
create mode 100644 test/mocks/utils_network_mock.h
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 31408f18..724f2188 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -8,15 +8,18 @@ add_library(libutils_ut STATIC
${local_cutils_map_srcs}
)
set_target_properties(libutils_ut PROPERTIES PREFIX "")
+target_link_libraries(libutils_ut ${ISULA_LIBUTILS_LIBRARY})
target_include_directories(libutils_ut
PUBLIC ${CMAKE_SOURCE_DIR}/src/common
PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils
PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils/map
)
+add_subdirectory(mainloop)
add_subdirectory(utils_string)
add_subdirectory(utils_convert)
add_subdirectory(utils_array)
add_subdirectory(utils_base64)
add_subdirectory(utils_pwgr)
add_subdirectory(utils_namespace)
+add_subdirectory(utils_network)
diff --git a/test/cutils/mainloop/CMakeLists.txt b/test/cutils/mainloop/CMakeLists.txt
new file mode 100644
index 00000000..78e3f18d
--- /dev/null
+++ b/test/cutils/mainloop/CMakeLists.txt
@@ -0,0 +1,27 @@
+project(iSulad_UT)
+
+SET(EXE mainloop_ut)
+
+add_executable(${EXE}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/mainloop_mock.cc
+ mainloop_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${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_CURRENT_SOURCE_DIR}/../../../test/mocks
+ )
+
+target_link_libraries(${EXE}
+ ${GTEST_BOTH_LIBRARIES}
+ ${GMOCK_LIBRARY}
+ ${GMOCK_MAIN_LIBRARY}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${ISULA_LIBUTILS_LIBRARY}
+ libutils_ut -lcrypto -lyajl -lz)
+
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/mainloop/mainloop_ut.cc b/test/cutils/mainloop/mainloop_ut.cc
new file mode 100644
index 00000000..adba9ea1
--- /dev/null
+++ b/test/cutils/mainloop/mainloop_ut.cc
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: mainloop unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-11
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "mainloop.h"
+#include "mainloop_mock.h"
+
+using ::testing::NiceMock;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::_;
+
+class MainloopUnitTest : public testing::Test {
+protected:
+ void SetUp() override
+ {
+ Mainloop_SetMock(&m_mainloop_mock);
+ EXPECT_CALL(m_mainloop_mock, Close(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(m_mainloop_mock, EpollCreate1(_)).WillRepeatedly(Return(0));
+ EXPECT_CALL(m_mainloop_mock, EpollCtl(_, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(m_mainloop_mock, EpollWait(_, _, _, _)).WillRepeatedly(Return(0));
+ }
+
+ void TearDown() override
+ {
+ Mainloop_SetMock(nullptr);
+ }
+
+ NiceMock<MockMainloop> m_mainloop_mock;
+};
+
+TEST_F(MainloopUnitTest, test_mainloop)
+{
+ struct epoll_descr descr = { 0 };
+
+ ASSERT_EQ(epoll_loop_open(&descr), 0);
+ ASSERT_EQ(epoll_loop_add_handler(&descr, 111, nullptr, nullptr), 0);
+ ASSERT_EQ(epoll_loop(&descr, -1), 0);
+ ASSERT_EQ(epoll_loop_del_handler(&descr, 111), 0);
+ ASSERT_EQ(epoll_loop_close(&descr), 0);
+}
diff --git a/test/cutils/utils_network/CMakeLists.txt b/test/cutils/utils_network/CMakeLists.txt
new file mode 100644
index 00000000..7e2c84e7
--- /dev/null
+++ b/test/cutils/utils_network/CMakeLists.txt
@@ -0,0 +1,27 @@
+project(iSulad_UT)
+
+SET(EXE utils_network_ut)
+
+add_executable(${EXE}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/utils_network_mock.cc
+ utils_network_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${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_CURRENT_SOURCE_DIR}/../../../test/mocks
+ )
+
+target_link_libraries(${EXE}
+ ${GTEST_BOTH_LIBRARIES}
+ ${GMOCK_LIBRARY}
+ ${GMOCK_MAIN_LIBRARY}
+ ${CMAKE_THREAD_LIBS_INIT}
+ ${ISULA_LIBUTILS_LIBRARY}
+ libutils_ut -lcrypto -lyajl -lz)
+
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_network/utils_network_ut.cc b/test/cutils/utils_network/utils_network_ut.cc
new file mode 100644
index 00000000..532fd780
--- /dev/null
+++ b/test/cutils/utils_network/utils_network_ut.cc
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: utils_network unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-11
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "utils.h"
+#include "utils_network.h"
+#include "utils_network_mock.h"
+
+using ::testing::NiceMock;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::_;
+
+std::string GetLocalPath()
+{
+ char abs_path[PATH_MAX] { 0x00 };
+ int ret = readlink("/proc/self/exe", abs_path, sizeof(abs_path));
+ if (ret < 0 || static_cast<size_t>(ret) >= sizeof(abs_path)) {
+ return "";
+ }
+
+ for (int i { ret }; i >= 0; --i) {
+ if (abs_path[i] == '/') {
+ abs_path[i + 1] = '\0';
+ break;
+ }
+ }
+
+ return static_cast<std::string>(abs_path);
+}
+
+class UtilsNetworkUnitTest : public testing::Test {
+protected:
+ void SetUp() override
+ {
+ UtilsNetwork_SetMock(&m_utils_network_mock);
+ EXPECT_CALL(m_utils_network_mock, Mount(_, _, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(m_utils_network_mock, Umount2(_, _)).WillRepeatedly(Invoke(invokeUmont2));
+
+ EXPECT_CALL(m_utils_network_mock, PthreadCreate(_, _, _, _)).WillRepeatedly(Return(0));
+ EXPECT_CALL(m_utils_network_mock, PthreadJoin(_, _)).WillRepeatedly(Invoke(invokePthreadJoin));
+ }
+
+ void TearDown() override
+ {
+ UtilsNetwork_SetMock(nullptr);
+ }
+
+ NiceMock<MockUtilsNetwork> m_utils_network_mock;
+
+ static int invokeUmont2(const char *target, int flags)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ static int invokePthreadJoin(pthread_t thread, void **retval)
+ {
+ void *status = (void *)calloc(1, sizeof(int));
+ *retval = status;
+
+ return 0;
+ }
+};
+
+TEST_F(UtilsNetworkUnitTest, test_network_namespace)
+{
+ int err = 0;
+ std::string netNS = GetLocalPath() + "test_namespace";
+
+ ASSERT_EQ(util_create_netns_file(netNS.c_str()), 0);
+ ASSERT_EQ(util_mount_namespace(netNS.c_str()), 0);
+ ASSERT_EQ(util_umount_namespace(netNS.c_str()), 0);
+ ASSERT_EQ(util_force_remove_file(netNS.c_str(), &err), true);
+}
diff --git a/test/cutils/utils_string/utils_string_ut.cc b/test/cutils/utils_string/utils_string_ut.cc
index 0cac212f..b488a09f 100644
--- a/test/cutils/utils_string/utils_string_ut.cc
+++ b/test/cutils/utils_string/utils_string_ut.cc
@@ -807,3 +807,27 @@ TEST(utils_string_ut, test_parse_percent_string)
ret = util_parse_percent_string(wrong6, &converted);
ASSERT_NE(ret, 0);
}
+
+TEST(utils_string_ut, test_str_token)
+{
+ char *token = nullptr;
+ char *string = (char *)"abc:def:gh";
+ char *tmp = string;
+
+ token = util_str_token(&tmp, nullptr);
+ ASSERT_STREQ(token, nullptr);
+ token = util_str_token(nullptr, ":");
+ ASSERT_STREQ(token, nullptr);
+
+ token = util_str_token(&tmp, ",");
+ ASSERT_STREQ(tmp, nullptr);
+ ASSERT_STREQ(token, string);
+ tmp = string;
+ free(token);
+ token = nullptr;
+
+ token = util_str_token(&tmp, ":");
+ ASSERT_STREQ(tmp, "def:gh");
+ ASSERT_STREQ(token, "abc");
+ free(token);
+}
diff --git a/test/mocks/mainloop_mock.cc b/test/mocks/mainloop_mock.cc
new file mode 100644
index 00000000..508c957b
--- /dev/null
+++ b/test/mocks/mainloop_mock.cc
@@ -0,0 +1,58 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-13
+ * Description: provide mainloop mock
+ ******************************************************************************/
+
+#include "mainloop_mock.h"
+
+namespace {
+MockMainloop *g_mainloop_mock = nullptr;
+}
+
+void Mainloop_SetMock(MockMainloop* mock)
+{
+ g_mainloop_mock = mock;
+}
+
+int close(int fd)
+{
+ if (g_mainloop_mock != nullptr) {
+ return g_mainloop_mock->Close(fd);
+ }
+ return 0;
+}
+
+int epoll_create1(int flags)
+{
+ std::cout << "epoll_create1" << std::endl;
+ if (g_mainloop_mock != nullptr) {
+ return g_mainloop_mock->EpollCreate1(flags);
+ }
+ return 0;
+}
+
+int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
+{
+ if (g_mainloop_mock != nullptr) {
+ return g_mainloop_mock->EpollCtl(epfd, op, fd, event);
+ }
+ return 0;
+}
+
+int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
+{
+ if (g_mainloop_mock != nullptr) {
+ return g_mainloop_mock->EpollWait(epfd, events, maxevents, timeout);
+ }
+ return 0;
+}
diff --git a/test/mocks/mainloop_mock.h b/test/mocks/mainloop_mock.h
new file mode 100644
index 00000000..aab16d12
--- /dev/null
+++ b/test/mocks/mainloop_mock.h
@@ -0,0 +1,33 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-13
+ * Description: mainloop mock
+ ******************************************************************************/
+
+#ifndef _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
+#define _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
+
+#include <gmock/gmock.h>
+#include <sys/epoll.h>
+
+class MockMainloop {
+public:
+ virtual ~MockMainloop() = default;
+ MOCK_METHOD1(Close, int(int));
+ MOCK_METHOD1(EpollCreate1, int(int));
+ MOCK_METHOD4(EpollCtl, int(int, int, int, struct epoll_event *));
+ MOCK_METHOD4(EpollWait, int(int, struct epoll_event *, int, int));
+};
+
+void Mainloop_SetMock(MockMainloop* mock);
+
+#endif // _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
diff --git a/test/mocks/utils_network_mock.cc b/test/mocks/utils_network_mock.cc
new file mode 100644
index 00000000..afa346b5
--- /dev/null
+++ b/test/mocks/utils_network_mock.cc
@@ -0,0 +1,60 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-15
+ * Description: provide utils_network mock
+ ******************************************************************************/
+
+#include "utils_network_mock.h"
+
+namespace {
+MockUtilsNetwork *g_utils_network_mock = nullptr;
+}
+
+void UtilsNetwork_SetMock(MockUtilsNetwork* mock)
+{
+ g_utils_network_mock = mock;
+}
+
+int mount(const char *source, const char *target, const char *filesystemtype,
+ unsigned long mountflags, const void *data)
+{
+ if (g_utils_network_mock != nullptr) {
+ return g_utils_network_mock->Mount(source, target, filesystemtype, mountflags, data);
+ }
+ return 0;
+}
+
+int umount2(const char *target, int flags)
+{
+ if (g_utils_network_mock != nullptr) {
+ return g_utils_network_mock->Umount2(target, flags);
+ }
+ return 0;
+}
+
+int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),
+ void *arg)
+{
+ if (g_utils_network_mock != nullptr) {
+ return g_utils_network_mock->PthreadCreate(thread, attr, start_routine, arg);
+ }
+ return 0;
+}
+
+
+int pthread_join(pthread_t thread, void **retval)
+{
+ if (g_utils_network_mock != nullptr) {
+ return g_utils_network_mock->PthreadJoin(thread, retval);
+ }
+ return 0;
+}
\ No newline at end of file
diff --git a/test/mocks/utils_network_mock.h b/test/mocks/utils_network_mock.h
new file mode 100644
index 00000000..fcae5664
--- /dev/null
+++ b/test/mocks/utils_network_mock.h
@@ -0,0 +1,34 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-15
+ * Description: utils_network mock
+ ******************************************************************************/
+
+#ifndef _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
+#define _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
+
+#include <gmock/gmock.h>
+#include <sys/mount.h>
+#include <pthread.h>
+
+class MockUtilsNetwork {
+public:
+ virtual ~MockUtilsNetwork() = default;
+ MOCK_METHOD5(Mount, int(const char *, const char *, const char *, unsigned long, const void *));
+ MOCK_METHOD2(Umount2, int(const char *, int));
+ MOCK_METHOD4(PthreadCreate, int(pthread_t *, const pthread_attr_t *, void *(*start_routine)(void *), void *));
+ MOCK_METHOD2(PthreadJoin, int(pthread_t, void **));
+};
+
+void UtilsNetwork_SetMock(MockUtilsNetwork* mock);
+
+#endif // _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
--
2.25.1

View File

@ -1,159 +0,0 @@
From 59b3f0832626fecebef66326b5316dbd10e482e0 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 15 Oct 2022 14:53:50 +0800
Subject: [PATCH 07/43] add check for aes apis
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/utils/cutils/namespace.c | 3 +--
src/utils/cutils/utils_aes.c | 48 ++++++++++++++++++++++++++++++------
src/utils/cutils/utils_aes.h | 2 +-
3 files changed, 43 insertions(+), 10 deletions(-)
diff --git a/src/utils/cutils/namespace.c b/src/utils/cutils/namespace.c
index 2916c8bb..dc2fe810 100644
--- a/src/utils/cutils/namespace.c
+++ b/src/utils/cutils/namespace.c
@@ -20,9 +20,8 @@
char *namespace_get_connected_container(const char *mode)
{
- const char *p = mode != NULL ? (mode + strlen(SHARE_NAMESPACE_PREFIX)) : NULL;
-
if (namespace_is_container(mode)) {
+ const char *p = mode + strlen(SHARE_NAMESPACE_PREFIX);
return util_strdup_s(p);
}
diff --git a/src/utils/cutils/utils_aes.c b/src/utils/cutils/utils_aes.c
index 9e318b5b..1e25ecd3 100644
--- a/src/utils/cutils/utils_aes.c
+++ b/src/utils/cutils/utils_aes.c
@@ -28,12 +28,17 @@
#include "utils.h"
#include "utils_file.h"
-int util_aes_key(char *key_file, bool create, unsigned char *aeskey)
+int util_aes_key(const char *key_file, bool create, unsigned char *aeskey)
{
char *key_dir = NULL;
int fd = 0;
int ret = 0;
+ if (key_file == NULL || aeskey == NULL) {
+ ERROR("Invalid arguments");
+ return -1;
+ }
+
if (!util_file_exists(key_file)) {
if (!create) {
ERROR("init aes failed, file %s not exist", key_file);
@@ -102,6 +107,11 @@ size_t util_aes_encode_buf_len(size_t len)
return AES_256_CFB_IV_LEN + util_aes_decode_buf_len(len);
}
+static bool invalid_ase_args(unsigned char *aeskey, unsigned char *bytes, size_t len, unsigned char **out)
+{
+ return aeskey == NULL || out == NULL || bytes == NULL || len == 0;
+}
+
int util_aes_encode(unsigned char *aeskey, unsigned char *bytes, size_t len, unsigned char **out)
{
int ret = 0;
@@ -110,22 +120,34 @@ int util_aes_encode(unsigned char *aeskey, unsigned char *bytes, size_t len, uns
int size = 0;
int expected_size = len;
unsigned char *iv = NULL;
+ EVP_CIPHER_CTX *ctx = NULL;
#ifdef OPENSSL_IS_BORINGSSL
const EVP_CIPHER *cipher = EVP_aes_256_ofb();
#else
const EVP_CIPHER *cipher = EVP_aes_256_cfb();
#endif
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (ctx == NULL || cipher == NULL) {
+ if (cipher == NULL) {
ERROR("EVP init failed");
return -1;
}
+ if (invalid_ase_args(aeskey, bytes, len, out)) {
+ ERROR("Invalid arguments");
+ return -1;
+ }
+
+ ctx = EVP_CIPHER_CTX_new();
+ if (ctx == NULL) {
+ ERROR("Ctx create failed");
+ return -1;
+ }
+
*out = util_common_calloc_s(util_aes_encode_buf_len(len) + 1);
if (*out == NULL) {
ERROR("out of memory");
- return -1;
+ ret = -1;
+ goto out;
}
iv = *out;
@@ -192,27 +214,39 @@ int util_aes_decode(unsigned char *aeskey, unsigned char *bytes, size_t len, uns
int size = 0;
int expected_size = 0;
unsigned char *iv = NULL;
+ EVP_CIPHER_CTX *ctx = NULL;
#ifdef OPENSSL_IS_BORINGSSL
const EVP_CIPHER *cipher = EVP_aes_256_ofb();
#else
const EVP_CIPHER *cipher = EVP_aes_256_cfb();
#endif
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- if (ctx == NULL || cipher == NULL) {
+ if (cipher == NULL) {
ERROR("EVP init failed");
return -1;
}
+ if (invalid_ase_args(aeskey, bytes, len, out)) {
+ ERROR("Invalid arguments");
+ return -1;
+ }
+
if (len <= AES_256_CFB_IV_LEN) {
ERROR("Invalid aes length, it must be larger than %d", AES_256_CFB_IV_LEN);
return -1;
}
+ ctx = EVP_CIPHER_CTX_new();
+ if (ctx == NULL) {
+ ERROR("Ctx create failed");
+ return -1;
+ }
+
*out = util_common_calloc_s(util_aes_decode_buf_len(len) + 1);
if (*out == NULL) {
ERROR("out of memory");
- return -1;
+ ret = -1;
+ goto out;
}
iv = bytes;
diff --git a/src/utils/cutils/utils_aes.h b/src/utils/cutils/utils_aes.h
index d429c9e0..476fea65 100644
--- a/src/utils/cutils/utils_aes.h
+++ b/src/utils/cutils/utils_aes.h
@@ -27,7 +27,7 @@ extern "C" {
#define AES_256_CFB_KEY_LEN 32
#define AES_256_CFB_IV_LEN 16
-int util_aes_key(char *key_path, bool create, unsigned char *aeskey);
+int util_aes_key(const char *key_path, bool create, unsigned char *aeskey);
// note: Input bytes is "IV+data", "bytes+AES_256_CFB_IV_LEN" is the real data to be encoded.
// The output length is the input "len" and add the '\0' after end of the length.
--
2.25.1

View File

@ -1,142 +0,0 @@
From 3909ddbc369c69202308e77beda6553b7d95d79b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 15 Oct 2022 15:04:56 +0800
Subject: [PATCH 08/43] add ut for cutils aes
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/CMakeLists.txt | 1 +
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_aes/CMakeLists.txt | 16 ++++++
test/cutils/utils_aes/utils_aes_ut.cc | 74 +++++++++++++++++++++++++++
4 files changed, 92 insertions(+)
create mode 100644 test/cutils/utils_aes/CMakeLists.txt
create mode 100644 test/cutils/utils_aes/utils_aes_ut.cc
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a36f68c5..034aaf97 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -62,6 +62,7 @@ IF(ENABLE_COVERAGE)
COMMAND lcov --directory . --capture --output-file coverage.info
COMMAND lcov -a coverage.base -a coverage.info --output-file coverage.total
+ COMMAND lcov --remove coverage.total '/usr/include/*' --output-file coverage.total
COMMAND lcov --remove coverage.total ${COVERAGE_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/coverage.info.cleaned
COMMAND genhtml -o coverage ${PROJECT_BINARY_DIR}/coverage.info.cleaned
COMMAND ${CMAKE_COMMAND} -E remove coverage.base coverage.total ${PROJECT_BINARY_DIR}/coverage.info.cleaned
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 724f2188..93c73fb8 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -23,3 +23,4 @@ add_subdirectory(utils_base64)
add_subdirectory(utils_pwgr)
add_subdirectory(utils_namespace)
add_subdirectory(utils_network)
+add_subdirectory(utils_aes)
diff --git a/test/cutils/utils_aes/CMakeLists.txt b/test/cutils/utils_aes/CMakeLists.txt
new file mode 100644
index 00000000..f7535bb3
--- /dev/null
+++ b/test/cutils/utils_aes/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_aes_ut)
+
+add_executable(${EXE}
+ utils_aes_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_aes/utils_aes_ut.cc b/test/cutils/utils_aes/utils_aes_ut.cc
new file mode 100644
index 00000000..e564428d
--- /dev/null
+++ b/test/cutils/utils_aes/utils_aes_ut.cc
@@ -0,0 +1,74 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils namespace unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_aes.h"
+
+TEST(utils_aes, test_util_aes_key)
+{
+ std::string key_file = "./aes_key";
+ unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+
+ unlink(key_file.c_str());
+ ASSERT_NE(util_aes_key(key_file.c_str(), false, key_val), 0);
+ ASSERT_NE(util_aes_key(nullptr, true, key_val), 0);
+ ASSERT_NE(util_aes_key(nullptr, false, key_val), 0);
+ ASSERT_NE(util_aes_key(key_file.c_str(), true, nullptr), 0);
+ ASSERT_NE(util_aes_key(key_file.c_str(), false, nullptr), 0);
+ ASSERT_NE(util_aes_key(nullptr, true, nullptr), 0);
+ ASSERT_NE(util_aes_key(nullptr, false, nullptr), 0);
+}
+
+TEST(utils_aes, test_util_aes_encode)
+{
+ std::string key_file = "./aes_key";
+ unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+ std::string test_data = "hello world";
+ unsigned char *out = nullptr;
+
+ ASSERT_EQ(util_aes_key(key_file.c_str(), true, key_val), 0);
+ ASSERT_EQ(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), &out), 0);
+
+ ASSERT_NE(util_aes_encode(nullptr, (unsigned char *)test_data.c_str(), test_data.size(), &out), 0);
+ ASSERT_NE(util_aes_encode(key_val, nullptr, 0, &out), 0);
+ ASSERT_NE(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), 0, &out), 0);
+ ASSERT_NE(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), nullptr), 0);
+
+ unlink(key_file.c_str());
+}
+
+TEST(utils_aes, test_util_aes_decode)
+{
+ std::string key_file = "./aes_key";
+ unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+ std::string test_data = "hello world";
+ unsigned char *encode_data = nullptr;
+ unsigned char *decode_data = nullptr;
+ size_t aes_len = AES_256_CFB_IV_LEN;
+
+ ASSERT_EQ(util_aes_key(key_file.c_str(), true, key_val), 0);
+ ASSERT_EQ(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), &encode_data), 0);
+ aes_len += test_data.size();
+ ASSERT_EQ(util_aes_decode(key_val, encode_data, aes_len, &decode_data), 0);
+ printf("get decode value = %s\n", (const char *)decode_data);
+ ASSERT_EQ(strcmp(test_data.c_str(), (const char *)decode_data), 0);
+
+ ASSERT_NE(util_aes_decode(nullptr, encode_data, aes_len, &decode_data), 0);
+ ASSERT_NE(util_aes_decode(key_val, nullptr, 0, &decode_data), 0);
+ ASSERT_NE(util_aes_decode(key_val, encode_data, 0, &decode_data), 0);
+ ASSERT_NE(util_aes_decode(key_val, encode_data, aes_len, nullptr), 0);
+
+ unlink(key_file.c_str());
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,100 +0,0 @@
From b2f293d1777ba3846ea4bcab5754b66ebfe4094c Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 15 Oct 2022 17:01:02 +0800
Subject: [PATCH 09/43] add ut for cutils error
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_error/CMakeLists.txt | 16 ++++++++
test/cutils/utils_error/utils_error_ut.cc | 45 +++++++++++++++++++++++
3 files changed, 62 insertions(+)
create mode 100644 test/cutils/utils_error/CMakeLists.txt
create mode 100644 test/cutils/utils_error/utils_error_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 93c73fb8..d3e1038a 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -24,3 +24,4 @@ add_subdirectory(utils_pwgr)
add_subdirectory(utils_namespace)
add_subdirectory(utils_network)
add_subdirectory(utils_aes)
+add_subdirectory(utils_error)
diff --git a/test/cutils/utils_error/CMakeLists.txt b/test/cutils/utils_error/CMakeLists.txt
new file mode 100644
index 00000000..28016605
--- /dev/null
+++ b/test/cutils/utils_error/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_error_ut)
+
+add_executable(${EXE}
+ utils_error_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_error/utils_error_ut.cc b/test/cutils/utils_error/utils_error_ut.cc
new file mode 100644
index 00000000..d4585d70
--- /dev/null
+++ b/test/cutils/utils_error/utils_error_ut.cc
@@ -0,0 +1,45 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils namespace unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "error.h"
+
+TEST(utils_error, test_errno_to_error_message)
+{
+ const char *ret = nullptr;
+ std::string internal_err = "Server internal error";
+ std::string unknow_err = "Unknown error";
+
+ ret = errno_to_error_message(ISULAD_SUCCESS);
+ ASSERT_EQ(strcmp(ret, DEF_SUCCESS_STR), 0);
+
+ ret = errno_to_error_message(ISULAD_ERR_INTERNAL);
+ ASSERT_EQ(strcmp(ret, internal_err.c_str()), 0);
+
+ ret = errno_to_error_message(ISULAD_ERR_UNKNOWN);
+ ASSERT_EQ(strcmp(ret, unknow_err.c_str()), 0);
+}
+
+TEST(utils_error, test_format_errorf)
+{
+ char *out = nullptr;
+ std::string target = "hello world";
+
+ format_errorf(&out, "hello %s", "world");
+ ASSERT_EQ(strcmp(out, target.c_str()), 0);
+
+ format_errorf(nullptr, "hello %s", "world");
+ format_errorf(&out, nullptr);
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,28 +0,0 @@
From 99caa82b9406ef344707401c0b8c548296924466 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 15 Oct 2022 17:18:28 +0800
Subject: [PATCH 10/43] ensure argument is not null
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/utils/cutils/error.h | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/src/utils/cutils/error.h b/src/utils/cutils/error.h
index 5a8dca99..e3946cf2 100644
--- a/src/utils/cutils/error.h
+++ b/src/utils/cutils/error.h
@@ -62,6 +62,10 @@ static inline void format_errorf(char **err, const char *format, ...)
va_list argp;
va_start(argp, format);
+ if (err == NULL) {
+ return;
+ }
+
ret = vsnprintf(errbuf, BUFSIZ, format, argp);
va_end(argp);
if (ret < 0) {
--
2.25.1

View File

@ -1,96 +0,0 @@
From 8020e1e076e597a82962fe03990f89314f5419fa Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 17 Oct 2022 11:10:05 +0800
Subject: [PATCH 11/43] add ut for utils_fs
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_fs/CMakeLists.txt | 16 +++++++++++
test/cutils/utils_fs/utils_fs_ut.cc | 42 +++++++++++++++++++++++++++++
3 files changed, 59 insertions(+)
create mode 100644 test/cutils/utils_fs/CMakeLists.txt
create mode 100644 test/cutils/utils_fs/utils_fs_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index d3e1038a..9073a68d 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -25,3 +25,4 @@ add_subdirectory(utils_namespace)
add_subdirectory(utils_network)
add_subdirectory(utils_aes)
add_subdirectory(utils_error)
+add_subdirectory(utils_fs)
diff --git a/test/cutils/utils_fs/CMakeLists.txt b/test/cutils/utils_fs/CMakeLists.txt
new file mode 100644
index 00000000..7ff3176a
--- /dev/null
+++ b/test/cutils/utils_fs/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_fs_ut)
+
+add_executable(${EXE}
+ utils_fs_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_fs/utils_fs_ut.cc b/test/cutils/utils_fs/utils_fs_ut.cc
new file mode 100644
index 00000000..e1c4fd4b
--- /dev/null
+++ b/test/cutils/utils_fs/utils_fs_ut.cc
@@ -0,0 +1,42 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils namespace unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_fs.h"
+
+int good_cb(const char *mp, const char *pattern)
+{
+ return 0;
+}
+
+int good_check_cb(const char *mp, const char *pattern)
+{
+ return pattern != nullptr ? 0 : -1;
+}
+
+int bad_cb(const char *mp, const char *pattern)
+{
+ return -1;
+}
+
+TEST(utils_fs, test_util_deal_with_mount_info)
+{
+ std::string spattern = "[0-9]*";
+
+ ASSERT_EQ(util_deal_with_mount_info(good_cb, spattern.c_str()), true);
+ ASSERT_EQ(util_deal_with_mount_info(bad_cb, spattern.c_str()), false);
+ ASSERT_EQ(util_deal_with_mount_info(good_check_cb, spattern.c_str()), true);
+ ASSERT_EQ(util_deal_with_mount_info(good_check_cb, nullptr), false);
+}
--
2.25.1

View File

@ -1,55 +0,0 @@
From f1313b04f60672a1aae1a3d36df5f0b8abcdd06f Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 15:04:01 +0800
Subject: [PATCH 12/43] Add adaptation code for filters
Signed-off-by: zhongtao <taozh97@163.com>
---
src/utils/cutils/filters.c | 3 ++-
src/utils/cutils/filters.h | 8 ++++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/src/utils/cutils/filters.c b/src/utils/cutils/filters.c
index 1885e9cc..21f5db7c 100644
--- a/src/utils/cutils/filters.c
+++ b/src/utils/cutils/filters.c
@@ -315,9 +315,10 @@ bool filters_args_valid_key(const char **accepted, size_t len, const char *field
{
size_t i;
- if (field == NULL) {
+ if (accepted == NULL || field == NULL) {
return false;
}
+
for (i = 0; i < len; i++) {
if (accepted[i] != NULL && strcmp(accepted[i], field) == 0) {
return true;
diff --git a/src/utils/cutils/filters.h b/src/utils/cutils/filters.h
index 021704d1..c6e5a618 100644
--- a/src/utils/cutils/filters.h
+++ b/src/utils/cutils/filters.h
@@ -20,6 +20,10 @@
#include "map.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct filters_args {
// A map of map[string][map[string][bool]]
map_t *fields;
@@ -48,5 +52,9 @@ bool filters_args_exact_match(const struct filters_args *filters, const char *fi
bool filters_args_match(const struct filters_args *filters, const char *field, const char *source);
+#ifdef __cplusplus
+}
+#endif
+
#endif
--
2.25.1

View File

@ -1,30 +0,0 @@
From 45824723bf3968a1106089fb0572ad6a593ac62e Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 15:15:49 +0800
Subject: [PATCH 13/43] Add parameter check to path
Signed-off-by: zhongtao <taozh97@163.com>
---
src/utils/cutils/path.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/src/utils/cutils/path.c b/src/utils/cutils/path.c
index 79cd7af6..5a743632 100644
--- a/src/utils/cutils/path.c
+++ b/src/utils/cutils/path.c
@@ -733,6 +733,12 @@ int util_realpath_in_scope(const char *rootfs, const char *path, char **real_pat
char cleaned[PATH_MAX] = { 0 };
char *tmp = NULL;
+ if (rootfs == NULL || path == NULL || real_path == NULL) {
+ ERROR("Null parameter");
+ ret = -1;
+ goto out;
+ }
+
int nret = snprintf(full_path, sizeof(full_path), "%s%s", rootfs, path);
if (nret < 0 || (size_t)nret >= sizeof(full_path)) {
ERROR("sprintf error: %s", strerror(errno));
--
2.25.1

View File

@ -1,134 +0,0 @@
From 8cd75df646c836745b5a1325e68be67046a84f1a Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 18:23:19 +0800
Subject: [PATCH 14/43] Add ut for utils_convert
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/utils_convert/utils_convert_ut.cc | 112 ++++++++++++++++++
1 file changed, 112 insertions(+)
diff --git a/test/cutils/utils_convert/utils_convert_ut.cc b/test/cutils/utils_convert/utils_convert_ut.cc
index 7e46fd84..bee9c3c4 100644
--- a/test/cutils/utils_convert/utils_convert_ut.cc
+++ b/test/cutils/utils_convert/utils_convert_ut.cc
@@ -239,3 +239,115 @@ TEST(utils_convert, test_util_str_to_bool)
ret = util_str_to_bool("nullptr", &converted);
ASSERT_NE(ret, 0);
}
+
+TEST(utils_convert, test_util_safe_uint64)
+{
+ int ret;
+ uint64_t converted;
+ ret = util_safe_uint64("255", &converted);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(converted, 255);
+
+ ret = util_safe_uint64("255", nullptr);
+ ASSERT_NE(ret, 0);
+
+ ret = util_safe_uint64("-1", &converted);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(converted, UINT64_MAX);
+
+ ret = util_safe_uint64("0", &converted);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(converted, 0);
+
+ ret = util_safe_uint64("1.23", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_safe_uint64("1x", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_safe_uint64("18446744073709551616", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_safe_uint64("nullptr", &converted);
+ ASSERT_NE(ret, 0);
+}
+
+TEST(utils_convert, test_util_parse_octal_uint32)
+{
+ int ret;
+ uint32_t converted;
+ ret = util_parse_octal_uint32("50", &converted);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(converted, 40);
+
+ ret = util_parse_octal_uint32("50", nullptr);
+ ASSERT_NE(ret, 0);
+
+ ret = util_parse_octal_uint32("-1", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_parse_octal_uint32("0", &converted);
+ ASSERT_EQ(ret, 0);
+ ASSERT_EQ(converted, 0);
+
+ ret = util_parse_octal_uint32("1.23", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_parse_octal_uint32("1x", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_parse_octal_uint32("40000000000", &converted);
+ ASSERT_NE(ret, 0);
+
+ ret = util_parse_octal_uint32("nullptr", &converted);
+ ASSERT_NE(ret, 0);
+}
+
+TEST(utils_convert, test_util_uint_to_string)
+{
+ char *converted;
+ long long unsigned int ret;
+
+ ret = 123456;
+ converted = util_uint_to_string(ret);
+ ASSERT_STREQ(converted, "123456");
+
+ ret = 0;
+ converted = util_uint_to_string(ret);
+ ASSERT_STREQ(converted, "0");
+
+ ret = ULLONG_MAX;
+ converted = util_uint_to_string(ret);
+ ASSERT_NE(converted, nullptr);
+ ASSERT_STREQ(converted, std::to_string((long long unsigned int)ULLONG_MAX).c_str());
+
+ ret = -1;
+ converted = util_uint_to_string(ret);
+ ASSERT_NE(converted, nullptr);
+ ASSERT_STREQ(converted, std::to_string((long long unsigned int)ULLONG_MAX).c_str());
+}
+
+TEST(utils_convert, test_util_int_to_string)
+{
+ char *converted;
+ long long int ret;
+
+ ret = 123456;
+ converted = util_int_to_string(ret);
+ ASSERT_STREQ(converted, "123456");
+
+ ret = 0;
+ converted = util_int_to_string(ret);
+ ASSERT_STREQ(converted, "0");
+
+ ret = LLONG_MAX;
+ converted = util_int_to_string(ret);
+ ASSERT_NE(converted, nullptr);
+ ASSERT_STREQ(converted, std::to_string((long long)LLONG_MAX).c_str());
+
+ ret = LLONG_MIN;
+ converted = util_int_to_string(ret);
+ ASSERT_NE(converted, nullptr);
+ ASSERT_STREQ(converted, std::to_string((long long)LLONG_MIN).c_str());
+
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,187 +0,0 @@
From eb122520ccdcd71dee4e93d3bf9e6c296e3af315 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 11:43:18 +0800
Subject: [PATCH 15/43] Add ut for path
Signed-off-by: zhongtao <taozh97@163.com>
---
test/CMakeLists.txt | 1 -
test/cutils/CMakeLists.txt | 1 +
test/cutils/path/CMakeLists.txt | 17 ++++++++
test/{ => cutils}/path/path_ut.cc | 70 ++++++++++++++++++++++++++++++-
test/path/CMakeLists.txt | 28 -------------
5 files changed, 87 insertions(+), 30 deletions(-)
create mode 100644 test/cutils/path/CMakeLists.txt
rename test/{ => cutils}/path/path_ut.cc (82%)
delete mode 100644 test/path/CMakeLists.txt
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 034aaf97..92a4e969 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -42,7 +42,6 @@ include_directories(${GMOCK_INCLUDE_DIRS})
IF(ENABLE_UT)
add_subdirectory(cutils)
add_subdirectory(image)
- add_subdirectory(path)
add_subdirectory(cmd)
add_subdirectory(runtime)
add_subdirectory(specs)
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 9073a68d..00753e64 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -16,6 +16,7 @@ target_include_directories(libutils_ut
)
add_subdirectory(mainloop)
+add_subdirectory(path)
add_subdirectory(utils_string)
add_subdirectory(utils_convert)
add_subdirectory(utils_array)
diff --git a/test/cutils/path/CMakeLists.txt b/test/cutils/path/CMakeLists.txt
new file mode 100644
index 00000000..745258a1
--- /dev/null
+++ b/test/cutils/path/CMakeLists.txt
@@ -0,0 +1,17 @@
+project(iSulad_UT)
+
+SET(EXE path_ut)
+
+add_executable(${EXE}
+ path_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
+ )
+set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,getcwd -Wl,--wrap,readlink")
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/path/path_ut.cc b/test/cutils/path/path_ut.cc
similarity index 82%
rename from test/path/path_ut.cc
rename to test/cutils/path/path_ut.cc
index 0068ecb4..8601fa07 100644
--- a/test/path/path_ut.cc
+++ b/test/cutils/path/path_ut.cc
@@ -1,5 +1,5 @@
/*
- * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved.
+ * Copyright (c) Huawei Technologies Co., Ltd. 2019-2022. All rights reserved.
* iSulad licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
@@ -376,3 +376,71 @@ TEST(path_ut, test_preserve_trailing_dot_or_separator)
free(res);
res = nullptr;
}
+
+TEST(path_ut, test_split_path_dir_entry)
+{
+ std::string str;
+ char *dir = nullptr;
+ char *base = nullptr;
+ char **null_dir = nullptr;
+
+ str = "/home/dir/file";
+ ASSERT_EQ(util_split_path_dir_entry(nullptr, &dir, &base), -1);
+ ASSERT_EQ(util_split_path_dir_entry(str.c_str(), null_dir, &base), 0);
+ ASSERT_EQ(null_dir, nullptr);
+
+ str = "/home/dir/../file";
+ ASSERT_EQ(util_split_path_dir_entry(str.c_str(), &dir, &base), 0);
+ ASSERT_NE(dir, nullptr);
+ ASSERT_STREQ(dir, "/home");
+ ASSERT_NE(base, nullptr);
+ ASSERT_STREQ(base, "file");
+
+ str = "/home/dir/./file";
+ ASSERT_EQ(util_split_path_dir_entry(str.c_str(), &dir, &base), 0);
+ ASSERT_NE(dir, nullptr);
+ ASSERT_STREQ(dir, "/home/dir");
+ ASSERT_NE(base, nullptr);
+ ASSERT_STREQ(base, "file");
+
+ str = "./dir/file";
+ MOCK_SET_V(getcwd, getcwd_specify);
+ ASSERT_EQ(util_split_path_dir_entry(str.c_str(), &dir, &base), 0);
+ ASSERT_NE(dir, nullptr);
+ ASSERT_STREQ(dir, "/home/dir");
+ ASSERT_NE(base, nullptr);
+ ASSERT_STREQ(base, "file");
+}
+
+TEST(path_ut, test_realpath_in_scope)
+{
+ std::string rootfs;
+ std::string path;
+ char *realpath = nullptr;
+ char *null_roofs = nullptr;
+
+ ASSERT_EQ(util_realpath_in_scope(null_roofs, path.c_str(), &realpath), -1);
+
+ rootfs = "/home/dir";
+ path = "/file";
+ ASSERT_EQ(util_realpath_in_scope(rootfs.c_str(), path.c_str(), &realpath), 0);
+ ASSERT_NE(realpath, nullptr);
+ ASSERT_STREQ(realpath, "/home/dir/file");
+
+ rootfs = "/home/dir";
+ path = "/../file";
+ ASSERT_EQ(util_realpath_in_scope(rootfs.c_str(), path.c_str(), &realpath), -1);
+
+ rootfs = "/home/dir";
+ path = "/./file";
+ ASSERT_EQ(util_realpath_in_scope(rootfs.c_str(), path.c_str(), &realpath), 0);
+ ASSERT_NE(realpath, nullptr);
+ ASSERT_STREQ(realpath, "/home/dir/file");
+
+ rootfs = "";
+ path = "./dir/file";
+ MOCK_SET_V(getcwd, getcwd_specify);
+ ASSERT_EQ(util_realpath_in_scope(rootfs.c_str(), path.c_str(), &realpath), 0);
+ ASSERT_NE(realpath, nullptr);
+ ASSERT_STREQ(realpath, "/home/dir/file");
+}
diff --git a/test/path/CMakeLists.txt b/test/path/CMakeLists.txt
deleted file mode 100644
index dcb69130..00000000
--- a/test/path/CMakeLists.txt
+++ /dev/null
@@ -1,28 +0,0 @@
-project(iSulad_UT)
-
-SET(EXE path_ut)
-
-add_executable(${EXE}
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/cutils/utils.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/cutils/path.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/utils_string.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_regex.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/cutils/utils_verify.c
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256/sha256.c
- path_ut.cc)
-
-target_include_directories(${EXE} PUBLIC
- ${CMAKE_CURRENT_SOURCE_DIR}/../include
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/common
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/cutils/map
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/cutils
- ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256
- )
-set_target_properties(${EXE} PROPERTIES LINK_FLAGS "-Wl,--wrap,getcwd -Wl,--wrap,readlink")
-target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} -lcrypto -lyajl -lz)
-add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
--
2.25.1

View File

@ -1,160 +0,0 @@
From e8b1afc8241200a51b1eab8884fca4bfda286126 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 11:25:58 +0800
Subject: [PATCH 16/43] Add ut for filters
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_filters/CMakeLists.txt | 18 +++
test/cutils/utils_filters/utils_filters_ut.cc | 103 ++++++++++++++++++
3 files changed, 122 insertions(+)
create mode 100644 test/cutils/utils_filters/CMakeLists.txt
create mode 100644 test/cutils/utils_filters/utils_filters_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 00753e64..4e67d9ed 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -27,3 +27,4 @@ add_subdirectory(utils_network)
add_subdirectory(utils_aes)
add_subdirectory(utils_error)
add_subdirectory(utils_fs)
+add_subdirectory(utils_filters)
diff --git a/test/cutils/utils_filters/CMakeLists.txt b/test/cutils/utils_filters/CMakeLists.txt
new file mode 100644
index 00000000..31d3ac25
--- /dev/null
+++ b/test/cutils/utils_filters/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE utils_filters_ut)
+
+add_executable(${EXE}
+ utils_filters_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${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
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_filters/utils_filters_ut.cc b/test/cutils/utils_filters/utils_filters_ut.cc
new file mode 100644
index 00000000..81a928f9
--- /dev/null
+++ b/test/cutils/utils_filters/utils_filters_ut.cc
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: filters unit test
+ * Author: zhongtao
+ * Create: 2022-10-17
+ */
+
+#include <gtest/gtest.h>
+#include "filters.h"
+#include "utils.h"
+
+TEST(utils_filters, test_filters)
+{
+ struct filters_args *filters = filters_args_new();
+ ASSERT_NE(filters, nullptr);
+
+ const char *key1_outside = "lable";
+ const char *key1_inside = "lable1";
+ const char *value1_inside = "1";
+ const char *value1_outside = "lable1=1";
+ const char *exact_value1_outside = "lable2=2";
+ const char *key2_outside = "id";
+ const char *value2_outside = "id1=123";
+ const char *key3_outside = "patten";
+ const char *value3_outside = "^/?[a-zA-Z0-9][a-zA-Z0-9_.-]+$";
+ const char *match_patten = "c8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+ const char *unmatch_patten = "#c8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+
+ //test filters_args_add
+ ASSERT_EQ(filters_args_add(filters, key1_outside, value1_outside), true);
+ ASSERT_EQ(filters_args_add(filters, key2_outside, value2_outside), true);
+ ASSERT_EQ(filters_args_add(filters, key3_outside, value3_outside), true);
+
+ ASSERT_EQ(filters_args_add(nullptr, key1_outside, value1_outside), false);
+
+ //test filters_args_get
+ char **value = NULL;
+ value = filters_args_get(filters, key2_outside);
+ ASSERT_NE(value, nullptr);
+ ASSERT_STREQ(*value, value2_outside);
+
+ value = filters_args_get(nullptr, key1_outside);
+ ASSERT_EQ(value, nullptr);
+
+ // test filters_args_len
+ ASSERT_EQ(filters_args_len(nullptr), 0);
+ ASSERT_EQ(filters_args_len(filters), 3);
+
+ //test filters_args_del
+ ASSERT_EQ(filters_args_del(filters, key2_outside, value2_outside), true);
+ value = filters_args_get(filters, key2_outside);
+ ASSERT_EQ(value, nullptr);
+
+ ASSERT_EQ(filters_args_del(nullptr, key1_outside, value1_outside), false);
+
+ //test filters_args_match_kv_list
+ map_t *source = map_new(MAP_STR_STR, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
+ ASSERT_EQ(map_replace(source, (void *)key1_inside, (void *)value1_inside), true);
+
+ ASSERT_EQ(filters_args_match_kv_list(filters, key1_outside, source), true);
+ ASSERT_EQ(filters_args_match_kv_list(filters, key1_outside, nullptr), false);
+ ASSERT_EQ(filters_args_match_kv_list(nullptr, key1_outside, source), true);
+
+ //test filters_args_exact_match
+ ASSERT_EQ(filters_args_exact_match(filters, key1_outside, value1_outside), true);
+ ASSERT_EQ(filters_args_exact_match(filters, key1_outside, exact_value1_outside), false);
+ ASSERT_EQ(filters_args_exact_match(nullptr, key1_outside, exact_value1_outside), true);
+ ASSERT_EQ(filters_args_exact_match(filters, key1_outside, nullptr), false);
+
+ //test filters_args_match
+ ASSERT_EQ(filters_args_match(filters, key3_outside, match_patten), true);
+ ASSERT_EQ(filters_args_match(filters, key3_outside, unmatch_patten), false);
+ ASSERT_EQ(filters_args_match(nullptr, key3_outside, match_patten), true);
+ ASSERT_EQ(filters_args_match(filters, key3_outside, nullptr), false);
+
+ //test filters_args_free
+ filters_args_free(nullptr);
+ filters_args_free(filters);
+}
+
+TEST(utils_filters, test_filters_args_valid_key)
+{
+ const char *accepted_filters[] = { "id", "label", "name", NULL };
+ const char *valid = "name";
+ const char *invalid = "description";
+
+ ASSERT_EQ(filters_args_valid_key(accepted_filters, sizeof(accepted_filters) / sizeof(char *), valid), true);
+ ASSERT_EQ(filters_args_valid_key(accepted_filters, sizeof(accepted_filters) / sizeof(char *), invalid), false);
+
+ ASSERT_EQ(filters_args_valid_key(accepted_filters, sizeof(accepted_filters) / sizeof(char *), nullptr), false);
+ ASSERT_EQ(filters_args_valid_key(nullptr, 3, valid), false);
+ ASSERT_EQ(filters_args_valid_key(accepted_filters, 1, valid), false);
+ ASSERT_EQ(filters_args_valid_key(accepted_filters, 5, valid), true);
+
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,91 +0,0 @@
From 5b7c5fd4d856ad222dc51d79f8cf972730e48a54 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 17 Oct 2022 14:31:39 +0800
Subject: [PATCH 17/43] add static for unexport function
1. add static for inner function;
2. add check for aguments;
3. remove same code;
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/utils/cutils/utils_timestamp.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/src/utils/cutils/utils_timestamp.c b/src/utils/cutils/utils_timestamp.c
index c490c00e..85551d51 100644
--- a/src/utils/cutils/utils_timestamp.c
+++ b/src/utils/cutils/utils_timestamp.c
@@ -39,7 +39,7 @@ bool unix_nanos_to_timestamp(int64_t nanos, types_timestamp_t *timestamp)
return true;
}
-int types_timestamp_cmp_check(const types_timestamp_t *t1, const types_timestamp_t *t2)
+static int types_timestamp_cmp_check(const types_timestamp_t *t1, const types_timestamp_t *t2)
{
if (t1 == NULL && t2 == NULL) {
return 0;
@@ -54,7 +54,7 @@ int types_timestamp_cmp_check(const types_timestamp_t *t1, const types_timestamp
return 2;
}
-int types_timestamp_cmp_nanos(const types_timestamp_t *t1, const types_timestamp_t *t2)
+static int types_timestamp_cmp_nanos(const types_timestamp_t *t1, const types_timestamp_t *t2)
{
if (t1->has_nanos && t2->has_nanos) {
if (t1->nanos > t2->nanos) {
@@ -147,7 +147,7 @@ bool util_get_timestamp(const char *str_time, types_timestamp_t *timestamp)
return true;
}
-bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timebuffer, size_t maxsize, bool local_utc)
+static bool get_time_buffer_help(const types_timestamp_t *timestamp, char *timebuffer, size_t maxsize, bool local_utc)
{
int nret = 0;
int tm_zone_hour = 0;
@@ -222,6 +222,11 @@ bool util_get_now_time_stamp(types_timestamp_t *timestamp)
int err = 0;
struct timespec ts;
+ if (timestamp == NULL) {
+ ERROR("Invalid arguments");
+ return false;
+ }
+
err = clock_gettime(CLOCK_REALTIME, &ts);
if (err != 0) {
ERROR("failed to get time");
@@ -586,10 +591,11 @@ static bool get_tm_zone_from_str(const char *str, struct tm *tm, int32_t *nanos,
char *tmstr = NULL;
char *zp = NULL;
char *zonestr = NULL;
+ bool ret = false;
if (hasnil(str, tm, nanos, tz)) {
ERROR("Get tm and timezone from str input error");
- goto err_out;
+ return false;
}
tmstr = util_strdup_s(str);
@@ -610,15 +616,12 @@ static bool get_tm_zone_from_str(const char *str, struct tm *tm, int32_t *nanos,
ERROR("init tz failed");
goto err_out;
}
-
- free(tmstr);
- free(zonestr);
- return true;
+ ret = true;
err_out:
free(tmstr);
free(zonestr);
- return false;
+ return ret;
}
static int64_t get_minmus_time(struct tm *tm1, struct tm *tm2)
--
2.25.1

View File

@ -1,296 +0,0 @@
From 725f5813ee3125ad3c55dfbe3aeb5d8155e93e8f Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Mon, 17 Oct 2022 20:16:58 +0800
Subject: [PATCH 18/43] add ut for cutils timestamp
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_timestamp/CMakeLists.txt | 16 ++
.../utils_timestamp/utils_timestamp_ut.cc | 241 ++++++++++++++++++
3 files changed, 258 insertions(+)
create mode 100644 test/cutils/utils_timestamp/CMakeLists.txt
create mode 100644 test/cutils/utils_timestamp/utils_timestamp_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 4e67d9ed..7f454f75 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -28,3 +28,4 @@ add_subdirectory(utils_aes)
add_subdirectory(utils_error)
add_subdirectory(utils_fs)
add_subdirectory(utils_filters)
+add_subdirectory(utils_timestamp)
diff --git a/test/cutils/utils_timestamp/CMakeLists.txt b/test/cutils/utils_timestamp/CMakeLists.txt
new file mode 100644
index 00000000..38aec640
--- /dev/null
+++ b/test/cutils/utils_timestamp/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_timestamp_ut)
+
+add_executable(${EXE}
+ utils_timestamp_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_timestamp/utils_timestamp_ut.cc b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
new file mode 100644
index 00000000..d6756e3a
--- /dev/null
+++ b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
@@ -0,0 +1,241 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-13
+ * Description: utils timestamp unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_timestamp.h"
+
+TEST(utils_timestamp, test_util_types_timestamp_cmp)
+{
+ types_timestamp_t t1 = { 0 };
+ types_timestamp_t t2 = { 0 };
+
+ // t1 == t2
+ ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+
+ t1.has_seconds = true;
+ t1.seconds = 2;
+ t1.has_nanos = false;
+ t1.nanos = 0;
+ t2.has_seconds = false;
+ t2.seconds = 0;
+ t2.has_nanos = false;
+ t2.nanos = 100;
+ // t1 > t2
+ ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 1);
+
+ t1.has_seconds = false;
+ t1.seconds = 2;
+ t1.has_nanos = false;
+ t1.nanos = 0;
+ t2.has_seconds = true;
+ t2.seconds = 1;
+ t2.has_nanos = true;
+ t2.nanos = 100;
+ // t1 < t2
+ ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), -1);
+
+ t1.has_seconds = true;
+ t1.seconds = 2;
+ t1.has_nanos = false;
+ t1.nanos = 0;
+ t2.has_seconds = true;
+ t2.seconds = 2;
+ t2.has_nanos = false;
+ t2.nanos = 0;
+ // t1 == t2
+ ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+
+ t1.has_nanos = true;
+ t1.nanos = 88;
+ t2.has_nanos = true;
+ t2.nanos = 88;
+ // t1 == t2
+ ASSERT_EQ(util_types_timestamp_cmp(&t1, &t2), 0);
+}
+
+TEST(utils_timestamp, test_util_get_timestamp)
+{
+ types_timestamp_t t = { 0 };
+ std::string invalid_str = "1970-01-02X:00:xx";
+ std::string dstr1 = "1970-01-01T00:00:01.000000800";
+ struct tm tm_local = { 0 };
+ const time_t now_time = time(NULL);
+ long int tm_gmtoff = 0;
+
+ (void)localtime_r(&now_time, &tm_local);
+#ifdef __USE_MISC
+ tm_gmtoff = tm_local.tm_gmtoff;
+#else
+ tm_gmtoff = tm_local.__tm_gmtoff;
+#endif
+
+ ASSERT_EQ(util_get_timestamp(dstr1.c_str(), &t), true);
+ t.seconds += tm_gmtoff;
+ ASSERT_EQ(t.has_seconds, true);
+ ASSERT_EQ(t.seconds, 1);
+ ASSERT_EQ(t.has_nanos, true);
+ ASSERT_EQ(t.nanos, 800);
+
+ // invalid agruments check
+ ASSERT_EQ(util_get_timestamp(nullptr, &t), false);
+ ASSERT_EQ(util_get_timestamp(dstr1.c_str(), nullptr), false);
+ ASSERT_EQ(util_get_timestamp(invalid_str.c_str(), nullptr), false);
+}
+
+TEST(utils_timestamp, test_util_get_now_local_utc_time_buffer)
+{
+ char local_time[128] = { 0 };
+
+ ASSERT_EQ(util_get_now_local_utc_time_buffer(local_time, 128), true);
+ ASSERT_EQ(util_get_now_local_utc_time_buffer(nullptr, 0), false);
+}
+
+TEST(utils_timestamp, test_util_get_time_interval)
+{
+ types_timestamp_t t1 = { 0 };
+ types_timestamp_t t2 = { 0 };
+ int64_t ret = 0;
+
+ ASSERT_EQ(util_get_time_interval(t1, t2, &ret), 0);
+ ASSERT_EQ(ret, 0);
+
+ t2.has_seconds = true;
+ t2.seconds = 8;
+ t2.has_nanos = true;
+ t2.nanos = 8;
+ ASSERT_EQ(util_get_time_interval(t1, t2, &ret), 0);
+ ASSERT_EQ(ret, 8000000008);
+
+ t2.has_seconds = true;
+ t2.seconds = INT64_MAX;
+ t2.has_nanos = false;
+ t2.nanos = 0;
+ ASSERT_NE(util_get_time_interval(t1, t2, &ret), 0);
+
+ t2.seconds = INT64_MAX - 1;
+ t2.has_nanos = true;
+ t2.nanos = 100;
+ ASSERT_NE(util_get_time_interval(t1, t2, &ret), 0);
+}
+
+TEST(utils_timestamp, test_util_get_tm_from_str)
+{
+ std::string invalid_str = "2016-01-02T15:04:01:03";
+
+ std::vector<std::tuple<std::string, int, int, int, int, int, int, int>> cases = {
+ std::make_tuple("1970-01-01T01", 0, 0, 0, 1, 1, 0, 70),
+ std::make_tuple("1980-02-02T02:02", 0, 0, 2, 2, 2, 1, 80),
+ std::make_tuple("1990-03-03T03:03:03", 0, 3, 3, 3, 3, 2, 90),
+ };
+
+ for (const auto &cs : cases) {
+ struct tm got = { 0 };
+ int32_t nano = 0;
+ ASSERT_EQ(util_get_tm_from_str(std::get<0>(elem).c_str(), &got, &nano), true);
+ ASSERT_EQ(nano, std::get<1>(elem));
+ ASSERT_EQ(got.tm_sec, std::get<2>(elem));
+ ASSERT_EQ(got.tm_min, std::get<3>(elem));
+ ASSERT_EQ(got.tm_hour, std::get<4>(elem));
+ ASSERT_EQ(got.tm_mday, std::get<5>(elem));
+ ASSERT_EQ(got.tm_mon, std::get<6>(elem));
+ ASSERT_EQ(got.tm_year, std::get<7>(elem));
+ }
+
+ // check invalid cases
+ ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), &got, &nano), true);
+ ASSERT_NE(util_get_tm_from_str(nullptr, &got, &nano), true);
+ ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), nullptr, &nano), true);
+ ASSERT_NE(util_get_tm_from_str(invalid_str.c_str(), &got, nullptr), true);
+}
+
+TEST(utils_timestamp, test_util_time_seconds_since)
+{
+ std::string defaultstr = "-";
+ std::string invalid_str = "2016-01-02T15:04:01:03";
+ std::string dstr1 = "1990-03-03T03:03:03";
+ types_timestamp_t currt = { 0 };
+ char tbuf[128] = { 0 };
+ int64_t ret;
+
+ ASSERT_EQ(util_get_now_time_stamp(&currt), true);
+ currt.seconds -= 10;
+ ASSERT_EQ(util_get_time_buffer(&currt, tbuf, 128), true);
+ ret = util_time_seconds_since(tbuf);
+ ASSERT_GE(ret, 9);
+ ASSERT_LE(ret, 11);
+
+ ASSERT_EQ(util_time_seconds_since(dstr1.c_str()), 0);
+
+ // invalid cases
+ ASSERT_EQ(util_time_seconds_since(invalid_str.c_str()), 0);
+ ASSERT_EQ(util_time_seconds_since(nullptr), 0);
+ ASSERT_EQ(util_time_seconds_since(defaultContainerTime), 0);
+ ASSERT_EQ(util_time_seconds_since(defaultstr.c_str()), 0);
+}
+
+TEST(utils_timestamp, test_util_time_format_duration)
+{
+ std::string invalid_str = "2016-01-02T15:04:01:03";
+ std::string dstr3 = "1990-03-03T03:03:03.000000000+08:00";
+ std::string defaultstr = "-";
+ char out[128] = { 0 };
+
+ ASSERT_EQ(util_time_format_duration(dstr3.c_str(), out, 128), 0);
+
+ // invalid cases
+ ASSERT_EQ(util_time_format_duration(invalid_str.c_str(), out, 128), 1);
+ ASSERT_EQ(util_time_format_duration(nullptr, out, 128), 1);
+ ASSERT_EQ(util_time_format_duration(defaultContainerTime, out, 128), 1);
+ ASSERT_EQ(util_time_format_duration(defaultstr.c_str(), out, 128), 1);
+ ASSERT_EQ(util_time_format_duration(invalid_str.c_str(), out, 0), -1);
+}
+
+TEST(utils_timestamp, test_util_to_unix_nanos_from_str)
+{
+ std::string invalid_str = "2016-01-02T15:04:01:03";
+ std::string dstr3 = "1970-01-01T00:00:01.0+00:00";
+ int64_t ret = 0;
+
+ ASSERT_EQ(util_to_unix_nanos_from_str(dstr3.c_str(), &ret), 0);
+ ASSERT_EQ(ret, 1000000000);
+
+ // invalid cases
+ ASSERT_NE(util_to_unix_nanos_from_str(invalid_str.c_str(), &ret), 0);
+ ASSERT_EQ(util_to_unix_nanos_from_str(nullptr, &ret), 0);
+}
+
+TEST(utils_timestamp, test_util_time_str_to_nanoseconds)
+{
+ int64_t ret = 0;
+ std::string invalid_str = "xxxxxxx";
+ std::string dstr2 = "1ms";
+ std::string dstr3 = "2s";
+ std::string dstr4 = "1m";
+ std::string dstr5 = "1h";
+
+ ASSERT_EQ(util_time_str_to_nanoseconds(dstr2.c_str(), &ret), 0);
+ ASSERT_EQ(ret, Time_Milli);
+ ASSERT_EQ(util_time_str_to_nanoseconds(dstr3.c_str(), &ret), 0);
+ ASSERT_EQ(ret, 2 * Time_Second);
+ ASSERT_EQ(util_time_str_to_nanoseconds(dstr4.c_str(), &ret), 0);
+ ASSERT_EQ(ret, 60 * Time_Second);
+ ASSERT_EQ(util_time_str_to_nanoseconds(dstr5.c_str(), &ret), 0);
+ ASSERT_EQ(ret, 3600 * Time_Second);
+
+ // invalid cases
+ ASSERT_NE(util_time_str_to_nanoseconds(invalid_str.c_str(), &ret), 0);
+ ASSERT_NE(util_time_str_to_nanoseconds(nullptr, &ret), 0);
+ ASSERT_NE(util_time_str_to_nanoseconds(dstr3.c_str(), nullptr), 0);
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,37 +0,0 @@
From e437396ff5bc90bfa3736e0ef06be117bca4b174 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 18 Oct 2022 10:23:34 +0800
Subject: [PATCH 19/43] fix timestamp ut error
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/utils_timestamp/utils_timestamp_ut.cc | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/test/cutils/utils_timestamp/utils_timestamp_ut.cc b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
index d6756e3a..0f6b80f6 100644
--- a/test/cutils/utils_timestamp/utils_timestamp_ut.cc
+++ b/test/cutils/utils_timestamp/utils_timestamp_ut.cc
@@ -133,6 +133,8 @@ TEST(utils_timestamp, test_util_get_time_interval)
TEST(utils_timestamp, test_util_get_tm_from_str)
{
std::string invalid_str = "2016-01-02T15:04:01:03";
+ struct tm got = { 0 };
+ int32_t nano = 0;
std::vector<std::tuple<std::string, int, int, int, int, int, int, int>> cases = {
std::make_tuple("1970-01-01T01", 0, 0, 0, 1, 1, 0, 70),
@@ -140,9 +142,7 @@ TEST(utils_timestamp, test_util_get_tm_from_str)
std::make_tuple("1990-03-03T03:03:03", 0, 3, 3, 3, 3, 2, 90),
};
- for (const auto &cs : cases) {
- struct tm got = { 0 };
- int32_t nano = 0;
+ for (const auto &elem : cases) {
ASSERT_EQ(util_get_tm_from_str(std::get<0>(elem).c_str(), &got, &nano), true);
ASSERT_EQ(nano, std::get<1>(elem));
ASSERT_EQ(got.tm_sec, std::get<2>(elem));
--
2.25.1

View File

@ -1,45 +0,0 @@
From 99df201139e1afbc719f78bae047eaf826676b7f Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 17 Oct 2022 20:20:00 +0800
Subject: [PATCH 20/43] improve code in utils_mount_spec
Signed-off-by: zhongtao <taozh97@163.com>
---
src/utils/cutils/utils_mount_spec.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/src/utils/cutils/utils_mount_spec.c b/src/utils/cutils/utils_mount_spec.c
index d8f64c81..e9b23cf2 100644
--- a/src/utils/cutils/utils_mount_spec.c
+++ b/src/utils/cutils/utils_mount_spec.c
@@ -458,15 +458,23 @@ int util_parse_mount_spec(char *mount_str, mount_spec **spec, char **errmsg_out)
if (mount_str == NULL) {
CACHE_ERRMSG(errmsg, "Invalid mount specification: can't be empty");
- ret = -1;
- goto out;
+ return -1;
}
if (!mount_str[0]) {
CACHE_ERRMSG(errmsg, "Invalid mount specification: can't be empty");
- ret = -1;
- goto out;
+ return -1;
}
+ if(spec == NULL){
+ CACHE_ERRMSG(errmsg, "Invalid spec: can't be NULL");
+ return -1;
+ }
+
+ if(errmsg_out == NULL){
+ CACHE_ERRMSG(errmsg, "Invalid errmsg_out: can't be NULL");
+ return -1;
+ }
+
m = util_common_calloc_s(sizeof(mount_spec));
if (m == NULL) {
CACHE_ERRMSG(errmsg, "out of memory");
--
2.25.1

View File

@ -1,118 +0,0 @@
From 7e4921d01576f180c3624195a0edff4b4f6807f8 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Tue, 18 Oct 2022 10:46:14 +0800
Subject: [PATCH 21/43] Add ut for utils_mount_spec
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_mount_spec/CMakeLists.txt | 16 +++++
.../utils_mount_spec/utils_mount_spec_ut.cc | 64 +++++++++++++++++++
3 files changed, 81 insertions(+)
create mode 100644 test/cutils/utils_mount_spec/CMakeLists.txt
create mode 100644 test/cutils/utils_mount_spec/utils_mount_spec_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 7f454f75..f159aacb 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -29,3 +29,4 @@ add_subdirectory(utils_error)
add_subdirectory(utils_fs)
add_subdirectory(utils_filters)
add_subdirectory(utils_timestamp)
+add_subdirectory(utils_mount_spec)
diff --git a/test/cutils/utils_mount_spec/CMakeLists.txt b/test/cutils/utils_mount_spec/CMakeLists.txt
new file mode 100644
index 00000000..24fb5add
--- /dev/null
+++ b/test/cutils/utils_mount_spec/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_mount_spec_ut)
+
+add_executable(${EXE}
+ utils_mount_spec_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc b/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc
new file mode 100644
index 00000000..0f60d397
--- /dev/null
+++ b/test/cutils/utils_mount_spec/utils_mount_spec_ut.cc
@@ -0,0 +1,64 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2022-10-18
+ * Description: utils mount spec unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_mount_spec.h"
+
+TEST(utils_mount_spec, test_util_valid_mount_spec)
+{
+ char *base_valid = (char *)"type=bind,source=/home,target=/vol3,readonly=true,bind-selinux-opts=z,bind-propagation=rprivate";
+ char *oci_valid = (char *)"type=tmpfs,dst=/tmpfs,tmpfs-size=1m,tmpfs-mode=1700";
+ char *invalid1 = (char *)"type=volume,src=vol,dst=/vol,ro=true,red=false";
+ char *invalid2 = (char *)"type,src,dst";
+ char *errmsg = NULL;
+
+ ASSERT_EQ(util_valid_mount_spec(base_valid, &errmsg), true);
+ ASSERT_EQ(util_valid_mount_spec(oci_valid, &errmsg), true);
+
+ ASSERT_EQ(util_valid_mount_spec(invalid1, &errmsg), false);
+ ASSERT_EQ(util_valid_mount_spec(invalid2, &errmsg), false);
+ ASSERT_EQ(util_valid_mount_spec(nullptr, &errmsg), false);
+ ASSERT_EQ(util_valid_mount_spec(base_valid, nullptr), false);
+}
+
+TEST(utils_mount_spec, test_util_parse_mount_spec)
+{
+ char *base_valid = (char *)"type=bind,source=/home,target=/vol3,readonly=true,bind-selinux-opts=z,bind-propagation=rprivate";
+ char *oci_valid = (char *)"type=tmpfs,dst=/tmpfs,tmpfs-size=1m,tmpfs-mode=1700";
+ char *invalid1 = (char *)"type=volume,src=vol,dst=/vol,ro=true,red=false";
+ char *invalid2 = (char *)"type,src,dst";
+ mount_spec *m = NULL;
+ char *errmsg = NULL;
+
+ ASSERT_EQ(util_parse_mount_spec(base_valid, &m, &errmsg), 0);
+ ASSERT_STREQ(m->type, "bind");
+ ASSERT_STREQ(m->source, "/home");
+ ASSERT_STREQ(m->target, "/vol3");
+ ASSERT_EQ(m->readonly, true);
+ ASSERT_STREQ(m->bind_options->propagation, "rprivate");
+ ASSERT_STREQ(m->bind_options->selinux_opts, "z");
+
+ ASSERT_EQ(util_parse_mount_spec(oci_valid, &m, &errmsg), 0);
+ ASSERT_STREQ(m->type, "tmpfs");
+ ASSERT_STREQ(m->target, "/tmpfs");
+ ASSERT_EQ(m->tmpfs_options->size_bytes, 1048576);
+ ASSERT_EQ(m->tmpfs_options->mode, 960);
+
+ ASSERT_NE(util_parse_mount_spec(invalid1, &m, &errmsg), 0);
+ ASSERT_NE(util_parse_mount_spec(invalid2, &m, &errmsg), 0);
+ ASSERT_NE(util_parse_mount_spec(nullptr, &m, &errmsg), 0);
+ ASSERT_NE(util_parse_mount_spec(base_valid, nullptr, &errmsg), 0);
+ ASSERT_NE(util_parse_mount_spec(base_valid, &m, nullptr), 0);
+}
--
2.25.1

View File

@ -1,100 +0,0 @@
From e43af2dc017a63a772c7dea2583d7d58506d7608 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Tue, 18 Oct 2022 11:50:37 +0800
Subject: [PATCH 22/43] Add ut for utils_regex
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_regex/CMakeLists.txt | 16 ++++++++
test/cutils/utils_regex/utils_regex_ut.cc | 46 +++++++++++++++++++++++
3 files changed, 63 insertions(+)
create mode 100644 test/cutils/utils_regex/CMakeLists.txt
create mode 100644 test/cutils/utils_regex/utils_regex_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index f159aacb..4b235a09 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -30,3 +30,4 @@ add_subdirectory(utils_fs)
add_subdirectory(utils_filters)
add_subdirectory(utils_timestamp)
add_subdirectory(utils_mount_spec)
+add_subdirectory(utils_regex)
diff --git a/test/cutils/utils_regex/CMakeLists.txt b/test/cutils/utils_regex/CMakeLists.txt
new file mode 100644
index 00000000..3f6410b2
--- /dev/null
+++ b/test/cutils/utils_regex/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_regex_ut)
+
+add_executable(${EXE}
+ utils_regex_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_regex/utils_regex_ut.cc b/test/cutils/utils_regex/utils_regex_ut.cc
new file mode 100644
index 00000000..1b4414de
--- /dev/null
+++ b/test/cutils/utils_regex/utils_regex_ut.cc
@@ -0,0 +1,46 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2022-10-18
+ * Description: utils regex unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_regex.h"
+
+TEST(utils_regex, test_util_reg_match)
+{
+ const char *pattern = "^[a-f0-9]{64}$";
+ const char *valid = "c8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+ const char *invalid = "g8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+
+ ASSERT_EQ(util_reg_match(pattern, valid), 0);
+ ASSERT_EQ(util_reg_match(pattern, invalid), 1);
+
+ ASSERT_EQ(util_reg_match(pattern, nullptr), -1);
+ ASSERT_EQ(util_reg_match(nullptr, pattern), -1);
+}
+
+TEST(utils_regex, test_util_wildcard_to_regex)
+{
+ std::string wildcard = "*hello?";
+ char *value = NULL;
+
+ ASSERT_EQ(util_wildcard_to_regex(wildcard.c_str(), &value), 0);
+ ASSERT_STREQ(value, "^.*hello.$");
+
+ wildcard = "file{1,2,3}";
+ ASSERT_EQ(util_wildcard_to_regex(wildcard.c_str(), &value), 0);
+ ASSERT_STREQ(value, "^file\\{1,2,3\\}$");
+
+ ASSERT_EQ(util_wildcard_to_regex(nullptr, &value), -1);
+ ASSERT_EQ(util_wildcard_to_regex(wildcard.c_str(), nullptr), -1);
+}
--
2.25.1

View File

@ -1,164 +0,0 @@
From 60ef94806076e728b6f76d5b9b874e375182233c Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 18 Oct 2022 15:47:36 +0800
Subject: [PATCH 23/43] improve code in utils.c
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/utils/cutils/utils.c | 60 ++++++++++++++++++++++------------------
1 file changed, 33 insertions(+), 27 deletions(-)
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index e362581b..eddfda5c 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -64,13 +64,13 @@ int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize
void *tmp = NULL;
if (newptr == NULL || newsize == 0) {
- goto err_out;
+ return -1;
}
tmp = util_common_calloc_s(newsize);
if (tmp == NULL) {
ERROR("Failed to malloc memory");
- goto err_out;
+ return -1;
}
if (oldptr != NULL) {
@@ -82,9 +82,6 @@ int util_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize
*newptr = tmp;
return 0;
-
-err_out:
- return -1;
}
static int util_read_pipe(int pipe_fd, char **out_buf, size_t *out_buf_size, size_t *out_real_size)
@@ -353,8 +350,6 @@ void util_contain_errmsg(const char *errmsg, int *exit_code)
} else if (strcasestr(errmsg, "not a directory")) {
*exit_code = 127;
}
-
- return;
}
char *util_short_digest(const char *digest)
@@ -818,6 +813,9 @@ bool util_exec_cmd(exec_func_t cb_func, void *args, const char *stdin_msg, char
close(in_fd[1]);
in_fd[1] = -1;
+ if (stdout_msg == NULL) {
+ stdout_close_flag = 1;
+ }
for (;;) {
if (stdout_close_flag == 0) {
stdout_close_flag = util_read_pipe(out_fd[0], &stdout_buffer, &stdout_buf_size, &stdout_real_size);
@@ -840,8 +838,14 @@ bool util_exec_cmd(exec_func_t cb_func, void *args, const char *stdin_msg, char
close(err_fd[0]);
close(out_fd[0]);
out:
- *stdout_msg = stdout_buffer;
- *stderr_msg = stderr_buffer;
+ if (stdout_msg != NULL) {
+ *stdout_msg = stdout_buffer;
+ }
+ if (stderr_msg != NULL) {
+ *stderr_msg = stderr_buffer;
+ } else {
+ free(stderr_buffer);
+ }
return ret;
}
@@ -947,8 +951,8 @@ int util_env_insert(char ***penv, size_t *penv_len, const char *key, size_t key_
env = *penv;
env_len = *penv_len;
- if (env_len > (SIZE_MAX / sizeof(char *)) - 1) {
- ERROR("Failed to realloc memory for envionment variables");
+ if (env_len > (MAX_MEMORY_SIZE / sizeof(char *)) - 1) {
+ ERROR("Too large envionment variables");
return -1;
}
@@ -1108,7 +1112,7 @@ static int set_echo_back(bool echo_back)
return 0;
}
-int util_input_notty(char *buf, size_t maxlen)
+static int util_input_notty(char *buf, size_t maxlen)
{
size_t i = 0;
int ret = 0;
@@ -1424,9 +1428,9 @@ int util_read_pid_ppid_info(uint32_t pid, pid_ppid_info_t *pid_info)
proc_t *proc = NULL;
proc_t *p_proc = NULL;
- if (pid == 0) {
- ret = -1;
- goto out;
+ if (pid == 0 || pid_info == NULL) {
+ ERROR("Invalid arguments");
+ return -1;
}
proc = util_get_process_proc_info((pid_t)pid);
@@ -1506,8 +1510,8 @@ defs_map_string_object *dup_map_string_empty_object(defs_map_string_object *src)
return NULL;
}
- dst->keys = util_common_calloc_s(src->len * sizeof(char *));
- dst->values = util_common_calloc_s(src->len * sizeof(defs_map_string_object_element *));
+ dst->keys = util_smart_calloc_s(sizeof(char *), src->len);
+ dst->values = util_smart_calloc_s(sizeof(defs_map_string_object_element *), src->len);
if (dst->keys == NULL || dst->values == NULL) {
ERROR("Out of memory");
ret = -1;
@@ -1538,12 +1542,16 @@ int convert_v2_runtime(const char *runtime, char *binary)
size_t parts_len = 0;
char buf[PATH_MAX] = {0};
int ret = 0;
+ int nret;
+
+ if (binary == NULL) {
+ return -1;
+ }
parts = util_string_split_multi(runtime, '.');
if (parts == NULL) {
ERROR("split failed: %s", runtime);
- ret = -1;
- goto out;
+ return -1;
}
parts_len = util_array_len((const char **)parts);
@@ -1553,15 +1561,13 @@ int convert_v2_runtime(const char *runtime, char *binary)
goto out;
}
- if (binary != NULL) {
- int nret = snprintf(buf, sizeof(buf), "%s-%s-%s-%s", "containerd", "shim", parts[2], parts[3]);
- if (nret < 0 || (size_t)nret >= sizeof(buf)) {
- ERROR("Failed to snprintf string");
- ret = -1;
- goto out;
- }
- strcpy(binary, buf);
+ nret = snprintf(buf, sizeof(buf), "%s-%s-%s-%s", "containerd", "shim", parts[2], parts[3]);
+ if (nret < 0 || (size_t)nret >= sizeof(buf)) {
+ ERROR("Failed to snprintf string");
+ ret = -1;
+ goto out;
}
+ (void)strcpy(binary, buf);
out:
util_free_array(parts);
--
2.25.1

View File

@ -1,327 +0,0 @@
From fef883b9716bf8b71dd10152d9dea1b4e6952530 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Tue, 18 Oct 2022 17:11:04 +0800
Subject: [PATCH 24/43] add ut for cutils utils
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_utils/CMakeLists.txt | 16 ++
test/cutils/utils_utils/utils_utils_ut.cc | 272 ++++++++++++++++++++++
3 files changed, 289 insertions(+)
create mode 100644 test/cutils/utils_utils/CMakeLists.txt
create mode 100644 test/cutils/utils_utils/utils_utils_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 4b235a09..23426015 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -31,3 +31,4 @@ add_subdirectory(utils_filters)
add_subdirectory(utils_timestamp)
add_subdirectory(utils_mount_spec)
add_subdirectory(utils_regex)
+add_subdirectory(utils_utils)
diff --git a/test/cutils/utils_utils/CMakeLists.txt b/test/cutils/utils_utils/CMakeLists.txt
new file mode 100644
index 00000000..7b3bd546
--- /dev/null
+++ b/test/cutils/utils_utils/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_utils_ut)
+
+add_executable(${EXE}
+ utils_utils_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_utils/utils_utils_ut.cc b/test/cutils/utils_utils/utils_utils_ut.cc
new file mode 100644
index 00000000..531947d2
--- /dev/null
+++ b/test/cutils/utils_utils/utils_utils_ut.cc
@@ -0,0 +1,272 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: haozi007
+ * Create: 2022-10-18
+ * Description: utils unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils.h"
+
+TEST(utils_utils, test_util_mem_realloc)
+{
+ char *old = nullptr;
+ ASSERT_EQ(util_mem_realloc(nullptr, 0, old, 0), -1);
+}
+
+TEST(utils_utils, test_util_sig_parse)
+{
+ std::string num_str = "9";
+ std::string sig_str = "SIGSEGV";
+ std::string sig_only = "SEGV";
+
+ ASSERT_EQ(util_sig_parse(nullptr), -1);
+ ASSERT_EQ(util_sig_parse(num_str.c_str()), 9);
+ ASSERT_EQ(util_sig_parse(sig_str.c_str()), 11);
+ ASSERT_EQ(util_sig_parse(sig_str.c_str()), 11);
+}
+
+TEST(utils_utils, test_util_contain_errmsg)
+{
+ int exit_code = 0;
+ std::string teststr = "hello world";
+ std::vector<std::tuple<std::string, int>> cases = {
+ std::make_tuple("executable file not found", 127),
+ std::make_tuple("no such file or directory", 127),
+ std::make_tuple("system cannot find the file specified", 127),
+ std::make_tuple("permission denied", 126),
+ std::make_tuple("not a directory", 127),
+ };
+
+ for (const auto &elem : cases) {
+ util_contain_errmsg(std::get<0>(elem).c_str(), &exit_code);
+ ASSERT_EQ(exit_code, std::get<1>(elem));
+ }
+
+ // invalid cases
+ util_contain_errmsg(nullptr, &exit_code);
+ util_contain_errmsg(teststr.c_str(), &exit_code);
+ util_contain_errmsg(teststr.c_str(), nullptr);
+}
+
+TEST(utils_utils, test_util_digest)
+{
+ std::string valid_dg = "sha256:729ce43e2c915c3463b620f3fba201a4a641ca5a282387e233db799208342a08";
+ std::string invalid_dg = "xxxx";
+
+ ASSERT_STREQ(util_short_digest(valid_dg.c_str()), "729ce43e2c91");
+ ASSERT_STREQ(util_short_digest(invalid_dg.c_str()), nullptr);
+ ASSERT_STREQ(util_short_digest(nullptr), nullptr);
+ ASSERT_STREQ(util_full_digest(nullptr), nullptr);
+}
+
+TEST(utils_utils, test_util_proc_info)
+{
+ char buf[1024] = {0};
+ pid_t cpid = getpid();
+ proc_t *pt = nullptr;
+
+ ASSERT_EQ(util_stat2proc(nullptr, 10), nullptr);
+ ASSERT_EQ(util_stat2proc(buf, 0), nullptr);
+
+ ASSERT_EQ(util_process_alive(0, 10000), false);
+ // maybe return true
+ ASSERT_EQ(util_process_alive(10000000, 10000), false);
+
+ pt = util_get_process_proc_info(cpid);
+ ASSERT_NE(pt, nullptr);
+ ASSERT_EQ(util_process_alive(cpid, pt->start_time), true);
+ ASSERT_EQ(util_process_alive(cpid, 11), false);
+
+}
+
+void top_cb(char **args, const char *pid_args, size_t args_len)
+{
+ printf("this is stdout\n");
+ fprintf(stderr, "this is stderr\n");
+ exit(0);
+}
+
+TEST(utils_utils, test_util_exec_top_cmd)
+{
+ char *out_str = nullptr;
+ char *err_str = nullptr;
+
+ ASSERT_EQ(util_exec_top_cmd(top_cb, nullptr, nullptr, 0, &out_str, &err_str), true);
+ ASSERT_NE(out_str, nullptr);
+ free(out_str);
+ ASSERT_NE(err_str, nullptr);
+ free(err_str);
+}
+
+TEST(utils_utils, test_util_get_backtrace)
+{
+ char **ret = util_get_backtrace();
+
+ ASSERT_NE(ret, nullptr);
+ free(ret);
+}
+
+TEST(utils_utils, test_util_env_ops)
+{
+ char **ret = nullptr;
+ size_t ret_len = 0;
+ std::string first_val = "hello=world";
+ std::string second_val = "todo=test";
+ std::string new_val = "hello=test";
+ std::string key1 = "hello";
+ std::string key2 = "todo";
+ char *got = nullptr;
+
+ ASSERT_EQ(util_env_insert(&ret, &ret_len, key1.c_str(), key1.size(), first_val.c_str()), 0);
+ ASSERT_EQ(ret_len, 1);
+ ASSERT_STREQ(ret[0], first_val.c_str());
+
+ ASSERT_EQ(util_env_insert(&ret, &ret_len, key2.c_str(), key2.size(), second_val.c_str()), 0);
+ ASSERT_EQ(ret_len, 2);
+ ASSERT_STREQ(ret[1], second_val.c_str());
+
+
+ got = util_env_get_val(ret, ret_len, key1.c_str(), key1.size());
+ ASSERT_STREQ(got, "world");
+ free(got);
+
+ ASSERT_EQ(util_env_insert(&ret, &ret_len, key1.c_str(), key1.size(), new_val.c_str()), 0);
+ ASSERT_EQ(ret_len, 2);
+ ASSERT_STREQ(ret[0], new_val.c_str());
+
+ got = util_env_get_val(ret, ret_len, key2.c_str(), key2.size());
+ ASSERT_STREQ(got, "test");
+ free(got);
+
+ util_free_array_by_len(ret, ret_len);
+}
+
+TEST(utils_utils, test_util_parse_user_remap)
+{
+ unsigned int uid, gid, offset;
+ std::string valid_str = "1000:1000:65535";
+ std::string invalid_str = "1000:1000:65536";
+
+ ASSERT_EQ(util_parse_user_remap(valid_str.c_str(), &uid, &gid, &offset), 0);
+ ASSERT_EQ(uid, 1000);
+ ASSERT_EQ(gid, 1000);
+ ASSERT_EQ(offset, 65535);
+
+ ASSERT_EQ(util_parse_user_remap(nullptr, &uid, &gid, &offset), -1);
+ ASSERT_EQ(util_parse_user_remap(invalid_str.c_str(), &uid, &gid, &offset), -1);
+}
+
+TEST(utils_utils, test_util_check_pid_max_kernel_namespaced)
+{
+ int ret = system("cat /proc/kallsyms | grep proc_dointvec_pidmax");
+ ASSERT_EQ(util_check_pid_max_kernel_namespaced(), ret == 0 ? true : false);
+}
+
+TEST(utils_utils, test_util_memset_sensitive_string)
+{
+ char buff[32] = "hello";
+
+ util_memset_sensitive_string(buff);
+ ASSERT_EQ(strlen(buff), 0);
+ util_memset_sensitive_string(nullptr);
+}
+
+void exec_cb(void *args)
+{
+ char buff[8] = { 0 };
+ int ret;
+
+ ret = util_input_readall(buff, 7);
+
+ if (ret < 0) {
+ exit(-1);
+ }
+ exit(0);
+}
+
+void exec_echo_cb(void *args)
+{
+ char buff[8] = { 0 };
+ int ret;
+ bool *is_echo = (bool *)args;
+
+ if (*is_echo) {
+ ret = util_input_noecho(buff, 7);
+ } else {
+ ret = util_input_echo(buff, 7);
+ }
+
+ if (ret < 0) {
+ exit(-1);
+ }
+ exit(0);
+}
+
+TEST(utils_utils, test_util_input)
+{
+ std::string test = "hello";
+ bool is_echo = true;
+
+ ASSERT_EQ(util_exec_cmd(exec_cb, nullptr, test.c_str(), nullptr, nullptr), true);
+
+ ASSERT_EQ(util_exec_cmd(exec_echo_cb, &is_echo, test.c_str(), nullptr, nullptr), false);
+ is_echo = false;
+ ASSERT_EQ(util_exec_cmd(exec_echo_cb, &is_echo, test.c_str(), nullptr, nullptr), false);
+}
+
+TEST(utils_utils, test_util_normalized_host_os_arch)
+{
+ ASSERT_EQ(util_normalized_host_os_arch(nullptr, nullptr, nullptr), -1);
+}
+
+TEST(utils_utils, test_util_read_pid_ppid_info)
+{
+ pid_t pid = getpid();
+ pid_t ppid = getppid();
+ pid_ppid_info_t pid_info = { 0 };
+
+ ASSERT_EQ(util_read_pid_ppid_info((uint32_t)pid, &pid_info), 0);
+ ASSERT_EQ(pid_info.ppid, (int)ppid);
+
+ ASSERT_EQ(util_read_pid_ppid_info(0, nullptr), -1);
+}
+
+TEST(utils_utils, test_util_parse_user_group)
+{
+ std::string uandg = "user:group";
+ char *user = nullptr;
+ char *group = nullptr;
+ char *tmp = nullptr;
+
+ util_parse_user_group(uandg.c_str(), &user, &group, &tmp);
+ ASSERT_STREQ(user, "user");
+ ASSERT_STREQ(group, "group");
+ free(tmp);
+}
+
+TEST(utils_utils, test_dup_map_string_empty_object)
+{
+ ASSERT_EQ(dup_map_string_empty_object(nullptr), nullptr);
+}
+
+TEST(utils_utils, test_convert_v2_runtime)
+{
+ std::string valid_str = "io.containerd.runc.v1";
+ std::string valid_ret = "containerd-shim-runc-v1";
+ std::string invalid_str = "xxx.xxx.xxx.xxx";
+ char buff[32] = { 0 };
+
+ ASSERT_EQ(convert_v2_runtime(invalid_str.c_str(), buff), -1);
+ ASSERT_EQ(convert_v2_runtime(nullptr, buff), -1);
+ ASSERT_EQ(convert_v2_runtime(valid_str.c_str(), nullptr), -1);
+ ASSERT_EQ(convert_v2_runtime(valid_str.c_str(), buff), 0);
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,63 +0,0 @@
From 979b21ba298edc64c88e2534b013076aa92ec975 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 19 Oct 2022 14:38:49 +0800
Subject: [PATCH 25/43] make sure kill pid not negative
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/cmd/isulad-shim/common.c | 2 +-
src/daemon/modules/runtime/isula/isula_rt_ops.c | 4 ++--
src/utils/cutils/utils.c | 2 +-
3 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/cmd/isulad-shim/common.c b/src/cmd/isulad-shim/common.c
index 20e0963c..bb8464bb 100644
--- a/src/cmd/isulad-shim/common.c
+++ b/src/cmd/isulad-shim/common.c
@@ -175,7 +175,7 @@ int cmd_combined_output(const char *binary, const char *params[], void *output,
ret = SHIM_OK;
out:
- if (ret != SHIM_OK && pid != 0) {
+ if (ret != SHIM_OK) {
kill(pid, 9);
}
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index 1e4cb5aa..c9667ee5 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -361,7 +361,7 @@ static bool shim_alive(const char *workdir)
file_read_int(fpid, &pid);
- if (pid == 0) {
+ if (pid <= 0) {
ERROR("failed read shim-pid file %s", fpid);
return false;
}
@@ -798,7 +798,7 @@ static void shim_kill_force(const char *workdir)
file_read_int(fpid, &pid);
- if (pid == 0) {
+ if (pid <= 0) {
goto out;
}
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index eddfda5c..9f5deaf9 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -461,7 +461,7 @@ bool util_process_alive(pid_t pid, unsigned long long start_time)
bool alive = true;
proc_t *pid_info = NULL;
- if (pid == 0) {
+ if (pid <= 0) {
return false;
}
--
2.25.1

View File

@ -1,240 +0,0 @@
From d9eb77bbe430fa74ca1c55c0c6907afaeb559499 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 18 Oct 2022 19:19:23 +0800
Subject: [PATCH 26/43] add UT for atomic and map
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
test/cutils/CMakeLists.txt | 2 +
test/cutils/map/CMakeLists.txt | 16 ++++++
test/cutils/map/map_ut.cc | 39 ++++++++++++++
test/cutils/util_atomic/CMakeLists.txt | 18 +++++++
test/cutils/util_atomic/util_atomic_ut.cc | 56 +++++++++++++++++++++
test/cutils/utils_string/utils_string_ut.cc | 25 +++++++++
test/mocks/utils_network_mock.cc | 2 +-
7 files changed, 157 insertions(+), 1 deletion(-)
create mode 100644 test/cutils/map/CMakeLists.txt
create mode 100644 test/cutils/map/map_ut.cc
create mode 100644 test/cutils/util_atomic/CMakeLists.txt
create mode 100644 test/cutils/util_atomic/util_atomic_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 23426015..28e37b27 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -17,6 +17,8 @@ target_include_directories(libutils_ut
add_subdirectory(mainloop)
add_subdirectory(path)
+add_subdirectory(map)
+add_subdirectory(util_atomic)
add_subdirectory(utils_string)
add_subdirectory(utils_convert)
add_subdirectory(utils_array)
diff --git a/test/cutils/map/CMakeLists.txt b/test/cutils/map/CMakeLists.txt
new file mode 100644
index 00000000..4059559f
--- /dev/null
+++ b/test/cutils/map/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE map_ut)
+
+add_executable(${EXE}
+ map_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/map/map_ut.cc b/test/cutils/map/map_ut.cc
new file mode 100644
index 00000000..fd75da28
--- /dev/null
+++ b/test/cutils/map/map_ut.cc
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: map unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-19
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "map.h"
+
+TEST(map_map_ut, test_map)
+{
+ // map[string][bool]
+ map_t *map_test = nullptr;
+ bool exist = true;
+
+ map_test = map_new(MAP_STR_BOOL, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
+ ASSERT_NE(map_test, nullptr);
+ ASSERT_EQ(map_insert(map_test, (void *)"key", &exist), true);
+
+ map_itor *itor = map_itor_new(map_test);
+ ASSERT_NE(itor, nullptr);
+ ASSERT_EQ(map_itor_first(itor), true);
+ ASSERT_EQ(map_itor_last(itor), true);
+ ASSERT_EQ(map_itor_prev(itor), false);
+
+ map_itor_free(itor);
+ map_clear(map_test);
+}
diff --git a/test/cutils/util_atomic/CMakeLists.txt b/test/cutils/util_atomic/CMakeLists.txt
new file mode 100644
index 00000000..071b2a04
--- /dev/null
+++ b/test/cutils/util_atomic/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE util_atomic_ut)
+
+add_executable(${EXE}
+ util_atomic_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${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
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/util_atomic/util_atomic_ut.cc b/test/cutils/util_atomic/util_atomic_ut.cc
new file mode 100644
index 00000000..29772a1e
--- /dev/null
+++ b/test/cutils/util_atomic/util_atomic_ut.cc
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: util_atomic unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-15
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "mock.h"
+#include "util_atomic.h"
+
+TEST(utils_atomic_ut, test_atomic_inc_dec)
+{
+ uint64_t atomic = 0;
+ uint64_t atomic_image = 0;
+
+ atomic_int_set(&atomic, 10);
+ ASSERT_EQ(atomic_int_get(&atomic), 10);
+ ASSERT_EQ(atomic_int_inc(&atomic), 11);
+ ASSERT_EQ(atomic_int_dec_test(&atomic), false);
+
+ atomic_int_set_image(&atomic_image, 1);
+ ASSERT_EQ(atomic_int_inc_image(&atomic_image), 2);
+ ASSERT_EQ(atomic_int_dec_test_image(&atomic_image), false);
+ ASSERT_EQ(atomic_int_dec_test_image(&atomic_image), true);
+}
+
+TEST(utils_atomic_ut, test_atomic_calculate)
+{
+ uint64_t atomic = 0;
+
+ ASSERT_EQ(atomic_int_compare_exchange(&atomic, 1, 2), false);
+
+
+ ASSERT_EQ(atomic_int_compare_exchange(&atomic, 0, 2), true);
+ // atomic = 2
+ ASSERT_EQ(atomic_int_add(&atomic, 3), 2);
+ // atomic = 5
+ ASSERT_EQ(atomic_int_and(&atomic, 4), 5);
+ // atomic = 4
+ ASSERT_EQ(atomic_int_or(&atomic, 8), 4);
+ // atomic = 12
+ ASSERT_EQ(atomic_int_xor(&atomic, 3), 12);
+ // atomic = 15
+ ASSERT_EQ(atomic_int_get(&atomic), 15);
+}
diff --git a/test/cutils/utils_string/utils_string_ut.cc b/test/cutils/utils_string/utils_string_ut.cc
index b488a09f..8b6c61a6 100644
--- a/test/cutils/utils_string/utils_string_ut.cc
+++ b/test/cutils/utils_string/utils_string_ut.cc
@@ -18,6 +18,7 @@
#include <gtest/gtest.h>
#include "mock.h"
#include "utils_string.h"
+#include "utils_array.h"
extern "C" {
DECLARE_WRAPPER(util_strdup_s, char *, (const char *str));
@@ -831,3 +832,27 @@ TEST(utils_string_ut, test_str_token)
ASSERT_STREQ(token, "abc");
free(token);
}
+
+TEST(utils_string_ut, test_string_split_multi)
+{
+ char **result = nullptr;
+
+ ASSERT_EQ(util_string_split_multi(nullptr, ':'), nullptr);
+
+ result = util_string_split_multi("", ':');
+ ASSERT_STREQ(result[0], "");
+ ASSERT_EQ(result[1], nullptr);
+ util_free_array(result);
+
+ result = util_string_split_multi("abcd;", ':');
+ ASSERT_STREQ(result[0], "abcd;");
+ ASSERT_EQ(result[1], nullptr);
+ util_free_array(result);
+
+ result = util_string_split_multi("abc:dd:e", ':');
+ ASSERT_STREQ(result[0], "abc");
+ ASSERT_STREQ(result[1], "dd");
+ ASSERT_STREQ(result[2], "e");
+ ASSERT_EQ(result[3], nullptr);
+ util_free_array(result);
+}
diff --git a/test/mocks/utils_network_mock.cc b/test/mocks/utils_network_mock.cc
index afa346b5..01027a62 100644
--- a/test/mocks/utils_network_mock.cc
+++ b/test/mocks/utils_network_mock.cc
@@ -57,4 +57,4 @@ int pthread_join(pthread_t thread, void **retval)
return g_utils_network_mock->PthreadJoin(thread, retval);
}
return 0;
-}
\ No newline at end of file
+}
--
2.25.1

View File

@ -1,125 +0,0 @@
From d9f2f58a5c8952fe0f03c8f6fbbed7f741a41e68 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Wed, 19 Oct 2022 15:12:27 +0800
Subject: [PATCH 27/43] remove unnecessary goto and add parameter check for
verify
Signed-off-by: zhongtao <taozh97@163.com>
---
src/utils/cutils/utils_verify.c | 41 ++++++++++++++++++++-------------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/src/utils/cutils/utils_verify.c b/src/utils/cutils/utils_verify.c
index 40b153f0..9ed33bf3 100644
--- a/src/utils/cutils/utils_verify.c
+++ b/src/utils/cutils/utils_verify.c
@@ -143,24 +143,21 @@ bool util_validate_unix_socket(const char *socket)
}
if (strncmp("unix://", socket, strlen("unix://"))) {
- nret = -1;
- goto err_out;
+ return false;
}
name = socket + strlen("unix://");
if (name[0] == '\0') {
- nret = -1;
- goto err_out;
+ return false;
}
nret = util_validate_absolute_path(name);
if (nret != 0) {
- nret = -1;
- goto err_out;
+ return false;
}
-err_out:
- return nret == 0;
+
+ return true;
}
bool util_validate_socket(const char *socket)
@@ -219,7 +216,6 @@ size_t util_get_all_caps_len()
bool util_valid_cap(const char *cap)
{
- bool cret = true;
int nret = 0;
char tmpcap[32] = { 0 };
size_t all_caps_len = util_get_all_caps_len();
@@ -231,16 +227,13 @@ bool util_valid_cap(const char *cap)
nret = snprintf(tmpcap, sizeof(tmpcap), "CAP_%s", cap);
if (nret < 0 || nret >= sizeof(tmpcap)) {
ERROR("Failed to print string");
- cret = false;
- goto err_out;
+ return false;
}
if (!util_strings_in_slice(g_all_caps, all_caps_len, tmpcap)) {
- cret = false;
- goto err_out;
+ return false;
}
-err_out:
- return cret;
+ return true;
}
bool util_valid_container_id(const char *id)
@@ -570,16 +563,27 @@ bool util_valid_value_false(const char *value)
bool util_valid_rw_mode(const char *mode)
{
+ if (mode == NULL){
+ return false;
+ }
+
return !strcmp(mode, "rw") || !strcmp(mode, "ro");
}
bool util_valid_label_mode(const char *mode)
{
+ if (mode == NULL){
+ return false;
+ }
+
return !strcmp(mode, "z") || !strcmp(mode, "Z");
}
bool util_valid_copy_mode(const char *mode)
{
+ if (mode == NULL){
+ return false;
+ }
return !strcmp(mode, "nocopy");
}
@@ -681,7 +685,7 @@ bool util_valid_positive_interger(const char *value)
{
const char *patten = "^[0-9]*$";
- if (value == NULL) {
+ if (value == NULL || strcmp(value, "") == 0) {
return false;
}
@@ -704,6 +708,11 @@ int util_valid_env(const char *env, char **dst)
int ret = 0;
char *value = NULL;
+ if (dst == NULL){
+ ERROR("NULL dst");
+ return -1;
+ }
+
char **arr = util_string_split_multi(env, '=');
if (arr == NULL) {
ERROR("Failed to split env string");
--
2.25.1

View File

@ -1,487 +0,0 @@
From c31c29a9ea4f4818036ee52cb85f82b5a02e43f9 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Wed, 19 Oct 2022 15:09:34 +0800
Subject: [PATCH 28/43] Add ut for verify
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_verify/CMakeLists.txt | 16 +
test/cutils/utils_verify/utils_verify_ut.cc | 432 ++++++++++++++++++++
3 files changed, 449 insertions(+)
create mode 100644 test/cutils/utils_verify/CMakeLists.txt
create mode 100644 test/cutils/utils_verify/utils_verify_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 28e37b27..2447b781 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -34,3 +34,4 @@ add_subdirectory(utils_timestamp)
add_subdirectory(utils_mount_spec)
add_subdirectory(utils_regex)
add_subdirectory(utils_utils)
+add_subdirectory(utils_verify)
diff --git a/test/cutils/utils_verify/CMakeLists.txt b/test/cutils/utils_verify/CMakeLists.txt
new file mode 100644
index 00000000..abf9596f
--- /dev/null
+++ b/test/cutils/utils_verify/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_verify_ut)
+
+add_executable(${EXE}
+ utils_verify_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_verify/utils_verify_ut.cc b/test/cutils/utils_verify/utils_verify_ut.cc
new file mode 100644
index 00000000..d1a9b6bf
--- /dev/null
+++ b/test/cutils/utils_verify/utils_verify_ut.cc
@@ -0,0 +1,432 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhongtao
+ * Create: 2022-10-19
+ * Description: utils verify unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_verify.h"
+#include "utils_file.h"
+
+TEST(utils_verify, test_util_valid_cmd_arg)
+{
+ ASSERT_EQ(util_valid_cmd_arg("isula ps"), true);
+
+ ASSERT_EQ(util_valid_cmd_arg(nullptr), false);
+ ASSERT_EQ(util_valid_cmd_arg("isula ps | grep"), false);
+ ASSERT_EQ(util_valid_cmd_arg("isula`"), false);
+ ASSERT_EQ(util_valid_cmd_arg("isula ps & grep`"), false);
+ ASSERT_EQ(util_valid_cmd_arg("isula ps ; grep`"), false);
+}
+
+TEST(utils_verify, test_util_valid_signal)
+{
+ ASSERT_EQ(util_valid_signal(2), true);
+
+ ASSERT_EQ(util_valid_signal(0), false);
+ ASSERT_EQ(util_valid_signal(-1), false);
+
+ ASSERT_EQ(util_valid_signal(64), true);
+ ASSERT_EQ(util_valid_signal(65), false);
+}
+
+TEST(utils_verify, test_util_validate_absolute_path)
+{
+ ASSERT_EQ(util_validate_absolute_path("/etc/isulad"), 0);
+ ASSERT_EQ(util_validate_absolute_path("/isulad/"), 0);
+
+ ASSERT_EQ(util_validate_absolute_path(nullptr), -1);
+ ASSERT_EQ(util_validate_absolute_path("./isulad"), -1);
+ ASSERT_EQ(util_validate_absolute_path("isulad"), -1);
+}
+
+TEST(utils_verify, test_util_validate_unix_socket)
+{
+ ASSERT_EQ(util_validate_unix_socket("unix:///etc/isulad"), true);
+ ASSERT_EQ(util_validate_unix_socket("unix:///isulad/"), true);
+
+ ASSERT_EQ(util_validate_unix_socket(nullptr), false);
+ ASSERT_EQ(util_validate_unix_socket("unix://"), false);
+ ASSERT_EQ(util_validate_unix_socket("unix://./isulad"), false);
+ ASSERT_EQ(util_validate_unix_socket("unix://isulad"), false);
+}
+
+TEST(utils_verify, test_util_validate_socket)
+{
+ ASSERT_EQ(util_validate_socket("unix:///etc/isulad"), true);
+ ASSERT_EQ(util_validate_socket("unix:///isulad/"), true);
+
+ ASSERT_EQ(util_validate_socket(nullptr), false);
+ ASSERT_EQ(util_validate_socket("unix://"), false);
+ ASSERT_EQ(util_validate_socket("unix://./isulad"), false);
+ ASSERT_EQ(util_validate_socket("unix://isulad"), false);
+
+ ASSERT_EQ(util_validate_socket("tcp://localhost:2375"), true);
+ ASSERT_EQ(util_validate_socket("tcp://127.0.0.1:2375"), true);
+
+ ASSERT_EQ(util_validate_socket("tcp://"), false);
+ ASSERT_EQ(util_validate_socket("tcp://127.0.0.1"), false);
+ ASSERT_EQ(util_validate_socket("tcp://127.0.0.1,2375"), false);
+}
+
+TEST(utils_verify, test_util_valid_device_mode)
+{
+ ASSERT_EQ(util_valid_device_mode("rwm"), true);
+
+ ASSERT_EQ(util_valid_device_mode(nullptr), false);
+ ASSERT_EQ(util_valid_device_mode(""), false);
+ ASSERT_EQ(util_valid_device_mode("rrwm"), false);
+ ASSERT_EQ(util_valid_device_mode("rwwm"), false);
+ ASSERT_EQ(util_valid_device_mode("rwmm"), false);
+ ASSERT_EQ(util_valid_device_mode("awm"), false);
+}
+
+TEST(utils_verify, test_util_valid_str)
+{
+ ASSERT_EQ(util_valid_str("str"), true);
+
+ ASSERT_EQ(util_valid_str(""), false);
+ ASSERT_EQ(util_valid_str(nullptr), false);
+}
+
+TEST(utils_verify, test_util_get_all_caps_len)
+{
+ ASSERT_EQ(util_get_all_caps_len(), 38);
+}
+
+TEST(utils_verify, test_util_valid_cap)
+{
+ ASSERT_EQ(util_valid_cap("DAC_READ_SEARCH"), true);
+
+ ASSERT_EQ(util_valid_cap(nullptr), false);
+ ASSERT_EQ(util_valid_cap(""), false);
+ ASSERT_EQ(util_valid_cap("DA_READ_SEARCH"), false);
+}
+
+TEST(utils_verify, test_util_valid_time_tz)
+{
+ ASSERT_EQ(util_valid_time_tz("2022-10-04T18:22:45.289257759Z"), true);
+
+ ASSERT_EQ(util_valid_time_tz(nullptr), false);
+ ASSERT_EQ(util_valid_time_tz("2016-01-02T15:04:01:03"), false);
+}
+
+TEST(utils_verify, test_util_valid_embedded_image_name)
+{
+ ASSERT_EQ(util_valid_embedded_image_name("busybox:latest"), true);
+
+ ASSERT_EQ(util_valid_embedded_image_name(nullptr), false);
+ ASSERT_EQ(util_valid_embedded_image_name("busybox:/latest"), false);
+ ASSERT_EQ(util_valid_embedded_image_name("busybox"), false);
+ ASSERT_EQ(util_valid_embedded_image_name("busybox:#latest"), false);
+}
+
+TEST(utils_verify, test_util_valid_image_name)
+{
+ ASSERT_EQ(util_valid_image_name("busybox:latest"), true);
+ ASSERT_EQ(util_valid_image_name("busybox"), true);
+
+ ASSERT_EQ(util_valid_image_name(nullptr), false);
+ ASSERT_EQ(util_valid_image_name("busybox:/latest"), false);
+ ASSERT_EQ(util_valid_image_name("busybox:#latest"), false);
+}
+
+TEST(utils_verify, test_util_tag_pos)
+{
+ ASSERT_STREQ(util_tag_pos("busybox:latest"), ":latest");
+
+ ASSERT_EQ(util_tag_pos("busybox:/latest"), nullptr);
+ ASSERT_EQ(util_tag_pos("busybox"), nullptr);
+}
+
+TEST(utils_verify, test_util_valid_file)
+{
+ std::string isulad_dir = "/tmp/test";
+ ASSERT_EQ(util_mkdir_p(isulad_dir.c_str(), 0700), 0);
+
+ ASSERT_EQ(util_valid_file(isulad_dir.c_str(), S_IFDIR), true);
+ ASSERT_EQ(util_valid_file(isulad_dir.c_str(), S_IFBLK), false);
+ ASSERT_EQ(util_valid_file(isulad_dir.c_str(), 0), false);
+
+ ASSERT_EQ(util_path_remove(isulad_dir.c_str()), 0);
+
+ ASSERT_EQ(util_valid_file(nullptr, S_IFDIR),false);
+}
+
+TEST(utils_verify, test_util_valid_digest)
+{
+ ASSERT_EQ(util_valid_digest("sha256:7bd0c945d7e4cc2ce5c21d449ba07eb89c8e6c28085edbcf6f5fa4bf90e7eedc"), true);
+
+ ASSERT_EQ(util_valid_digest(nullptr), false);
+ ASSERT_EQ(util_valid_digest("ha256:7bd0c945d7e4cc2ce5c21d449ba07eb89c8e6c28085edbcf6f5fa4bf90e7eedc"), false);
+}
+
+TEST(utils_verify, test_util_valid_tag)
+{
+ ASSERT_EQ(util_valid_tag("busybox:latest"), true);
+
+ ASSERT_EQ(util_valid_tag(nullptr), false);
+ ASSERT_EQ(util_valid_tag("sha256:latest"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_type)
+{
+ ASSERT_EQ(util_valid_key_type("type"), true);
+
+ ASSERT_EQ(util_valid_key_type(nullptr), false);
+ ASSERT_EQ(util_valid_key_type("type:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_src)
+{
+ ASSERT_EQ(util_valid_key_src("src"), true);
+ ASSERT_EQ(util_valid_key_src("source"), true);
+
+ ASSERT_EQ(util_valid_key_src(nullptr), false);
+ ASSERT_EQ(util_valid_key_src("source:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_dst)
+{
+ ASSERT_EQ(util_valid_key_dst("dst"), true);
+ ASSERT_EQ(util_valid_key_dst("destination"), true);
+ ASSERT_EQ(util_valid_key_dst("target"), true);
+
+ ASSERT_EQ(util_valid_key_dst(nullptr), false);
+ ASSERT_EQ(util_valid_key_dst("target:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_ro)
+{
+ ASSERT_EQ(util_valid_key_ro("ro"), true);
+ ASSERT_EQ(util_valid_key_ro("readonly"), true);
+
+ ASSERT_EQ(util_valid_key_ro(nullptr), false);
+ ASSERT_EQ(util_valid_key_ro("readonly:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_propagation)
+{
+ ASSERT_EQ(util_valid_key_propagation("bind-propagation"), true);
+
+ ASSERT_EQ(util_valid_key_propagation(nullptr), false);
+ ASSERT_EQ(util_valid_key_propagation("bind-propagation:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_selinux)
+{
+ ASSERT_EQ(util_valid_key_selinux("bind-selinux-opts"), true);
+ ASSERT_EQ(util_valid_key_selinux("selinux-opts"), true);
+
+ ASSERT_EQ(util_valid_key_selinux(nullptr), false);
+ ASSERT_EQ(util_valid_key_selinux("bind-selinux-opts:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_tmpfs_size)
+{
+ ASSERT_EQ(util_valid_key_tmpfs_size("tmpfs-size"), true);
+
+ ASSERT_EQ(util_valid_key_tmpfs_size(nullptr), false);
+ ASSERT_EQ(util_valid_key_tmpfs_size("tmpfs-size:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_tmpfs_mode)
+{
+ ASSERT_EQ(util_valid_key_tmpfs_mode("tmpfs-mode"), true);
+
+ ASSERT_EQ(util_valid_key_tmpfs_mode(nullptr), false);
+ ASSERT_EQ(util_valid_key_tmpfs_mode("tmpfs-mode:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_key_nocopy)
+{
+ ASSERT_EQ(util_valid_key_nocopy("volume-nocopy"), true);
+
+ ASSERT_EQ(util_valid_key_nocopy(nullptr), false);
+ ASSERT_EQ(util_valid_key_nocopy("volume-nocopy:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_value_true)
+{
+ ASSERT_EQ(util_valid_value_true("1"), true);
+ ASSERT_EQ(util_valid_value_true("true"), true);
+
+ ASSERT_EQ(util_valid_value_true(nullptr), false);
+ ASSERT_EQ(util_valid_value_true("0"), false);
+ ASSERT_EQ(util_valid_value_true("false"), false);
+}
+
+TEST(utils_verify, test_util_valid_value_false)
+{
+ ASSERT_EQ(util_valid_value_false("0"), true);
+ ASSERT_EQ(util_valid_value_false("false"), true);
+
+ ASSERT_EQ(util_valid_value_false(nullptr), false);
+ ASSERT_EQ(util_valid_value_false("1"), false);
+ ASSERT_EQ(util_valid_value_false("true"), false);
+}
+
+TEST(utils_verify, test_util_valid_rw_mode)
+{
+ ASSERT_EQ(util_valid_rw_mode("ro"), true);
+ ASSERT_EQ(util_valid_rw_mode("rw"), true);
+
+ ASSERT_EQ(util_valid_rw_mode(nullptr), false);
+ ASSERT_EQ(util_valid_rw_mode("rwro"), false);
+}
+
+TEST(utils_verify, test_util_valid_label_mode)
+{
+ ASSERT_EQ(util_valid_label_mode("z"), true);
+ ASSERT_EQ(util_valid_label_mode("Z"), true);
+
+ ASSERT_EQ(util_valid_label_mode(nullptr), false);
+ ASSERT_EQ(util_valid_label_mode("zZ"), false);
+}
+
+TEST(utils_verify, test_util_valid_copy_mode)
+{
+ ASSERT_EQ(util_valid_copy_mode("nocopy"), true);
+
+ ASSERT_EQ(util_valid_copy_mode(nullptr), false);
+ ASSERT_EQ(util_valid_copy_mode("nocopy:123"), false);
+}
+
+TEST(utils_verify, test_util_valid_propagation_mode)
+{
+ ASSERT_EQ(util_valid_propagation_mode("private"), true);
+ ASSERT_EQ(util_valid_propagation_mode("rprivate"), true);
+ ASSERT_EQ(util_valid_propagation_mode("slave"), true);
+ ASSERT_EQ(util_valid_propagation_mode("rslave"), true);
+ ASSERT_EQ(util_valid_propagation_mode("shared"), true);
+ ASSERT_EQ(util_valid_propagation_mode("rshared"), true);
+
+ ASSERT_EQ(util_valid_propagation_mode(nullptr), false);
+ ASSERT_EQ(util_valid_propagation_mode("rrslave"), false);
+}
+
+TEST(utils_verify, test_util_valid_mount_mode)
+{
+ ASSERT_EQ(util_valid_mount_mode("ro,private,z,nocopy"), true);
+
+ ASSERT_EQ(util_valid_mount_mode(nullptr), false);
+ ASSERT_EQ(util_valid_mount_mode("ro,rw,private,z,nocopy"), false);
+ ASSERT_EQ(util_valid_mount_mode("ri,private,z,nocopy"), false);
+}
+
+TEST(utils_verify, test_util_valid_container_id)
+{
+ ASSERT_EQ(util_valid_container_id("451f587884b04ef2a81a6d410f65083c906a865044ef5bef8af833aaab8c63aa"), true);
+
+ ASSERT_EQ(util_valid_container_id(nullptr), false);
+ ASSERT_EQ(util_valid_container_id("g51f587884b04ef2a81a6d410f65083c906a865044ef5bef8af833aaab8c63aa"), false);
+ ASSERT_EQ(util_valid_container_id(""), false);
+}
+
+TEST(utils_verify, test_util_valid_container_name)
+{
+ ASSERT_EQ(util_valid_container_name("test"), true);
+
+ ASSERT_EQ(util_valid_container_name(nullptr), false);
+ ASSERT_EQ(util_valid_container_name(".test"), false);
+}
+
+TEST(utils_verify, test_util_valid_container_id_or_name)
+{
+ ASSERT_EQ(util_valid_container_id_or_name("test"), true);
+ ASSERT_EQ(util_valid_container_id_or_name("451f587884b04ef2a81a6d410f65083c906a865044ef5bef8af833aaab8c63aa"), true);
+
+ ASSERT_EQ(util_valid_container_id_or_name(nullptr), false);
+ ASSERT_EQ(util_valid_container_id_or_name(".test"), false);
+ ASSERT_EQ(util_valid_container_id_or_name(""), false);
+}
+
+TEST(utils_verify, test_util_valid_host_name)
+{
+ ASSERT_EQ(util_valid_host_name("LAPTOP-6O44CJ3O"), true);
+
+ ASSERT_EQ(util_valid_host_name(nullptr), false);
+ ASSERT_EQ(util_valid_host_name(".LAPTOP-6O44CJ3O"), false);
+}
+
+TEST(utils_verify, test_util_valid_runtime_name)
+{
+ ASSERT_EQ(util_valid_runtime_name("runc"), true);
+
+ ASSERT_EQ(util_valid_runtime_name(nullptr), false);
+}
+
+TEST(utils_verify, test_util_valid_short_sha256_id)
+{
+ ASSERT_EQ(util_valid_short_sha256_id("ff4a8eb070e12018233797e865841d877a7835c4c6d5cfc52e5481995da6b2f7"), true);
+ ASSERT_EQ(util_valid_short_sha256_id("ff4"), true);
+
+ ASSERT_EQ(util_valid_short_sha256_id(nullptr), false);
+ ASSERT_EQ(util_valid_short_sha256_id("ff"), false);
+}
+
+TEST(utils_verify, test_util_valid_exec_suffix)
+{
+ ASSERT_EQ(util_valid_exec_suffix("ff4a8eb070e12018233797e865841d877a7835c4c6d5cfc52e5481995da6b2f7"), true);
+
+ ASSERT_EQ(util_valid_exec_suffix(nullptr), false);
+ ASSERT_EQ(util_valid_exec_suffix("gf4a8eb070e12018233797e865841d877a7835c4c6d5cfc52e5481995da6b2f7"), false);
+}
+
+TEST(utils_verify, test_util_valid_positive_interger)
+{
+ ASSERT_EQ(util_valid_positive_interger("123456789"), true);
+ ASSERT_EQ(util_valid_positive_interger("0"), true);
+
+ ASSERT_EQ(util_valid_positive_interger(nullptr), false);
+ ASSERT_EQ(util_valid_positive_interger("-123456789"), false);
+ ASSERT_EQ(util_valid_positive_interger(""), false);
+}
+
+TEST(utils_verify, test_util_valid_device_cgroup_rule)
+{
+ ASSERT_EQ(util_valid_device_cgroup_rule("b 8:* rmw"), true);
+
+ ASSERT_EQ(util_valid_device_cgroup_rule(nullptr), false);
+ ASSERT_EQ(util_valid_device_cgroup_rule("d 8:* rmw"), false);
+}
+
+TEST(utils_verify, test_util_valid_env)
+{
+ char *env = (char *)"USER=root";
+ char *dst = nullptr;
+
+ ASSERT_EQ(util_valid_env(env, &dst), 0);
+ ASSERT_STREQ(dst, "USER=root");
+
+ ASSERT_EQ(util_valid_env(nullptr, &dst), -1);
+ ASSERT_EQ(util_valid_env(env, nullptr), -1);
+}
+
+TEST(utils_verify, test_util_valid_sysctl)
+{
+ ASSERT_EQ(util_valid_sysctl("kernel.msgmax"), true);
+ ASSERT_EQ(util_valid_sysctl("net.abc"), true);
+
+ ASSERT_EQ(util_valid_sysctl(nullptr), false);
+ ASSERT_EQ(util_valid_sysctl("kernel.shmal"), false);
+}
+
+TEST(utils_verify, test_util_valid_volume_name)
+{
+ ASSERT_EQ(util_valid_volume_name("f6391b735a917ffbaff138970dc45290508574e6ab92e06a1e9dd290f31592ca"), true);
+ ASSERT_EQ(util_valid_volume_name("aa"), true);
+
+ ASSERT_EQ(util_valid_volume_name(nullptr), false);
+ ASSERT_EQ(util_valid_volume_name(""), false);
+ ASSERT_EQ(util_valid_volume_name("a"), false);
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,26 +0,0 @@
From f342d18d5eb26baabad2749002bac71b412ed8b6 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Thu, 20 Oct 2022 19:03:57 +0800
Subject: [PATCH 29/43] fix error in utils_verify_ut
Signed-off-by: zhongtao <taozh97@163.com>
---
test/cutils/utils_verify/utils_verify_ut.cc | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/test/cutils/utils_verify/utils_verify_ut.cc b/test/cutils/utils_verify/utils_verify_ut.cc
index d1a9b6bf..9c4bf944 100644
--- a/test/cutils/utils_verify/utils_verify_ut.cc
+++ b/test/cutils/utils_verify/utils_verify_ut.cc
@@ -100,7 +100,7 @@ TEST(utils_verify, test_util_valid_str)
TEST(utils_verify, test_util_get_all_caps_len)
{
- ASSERT_EQ(util_get_all_caps_len(), 38);
+ ASSERT_NE(util_get_all_caps_len(), 0);
}
TEST(utils_verify, test_util_valid_cap)
--
2.25.1

View File

@ -1,122 +0,0 @@
From 5e4fc62e36631d8d2e444ac3d60ce99ec7396ca0 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Fri, 21 Oct 2022 09:58:40 +0800
Subject: [PATCH 30/43] add more test for string and map
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
test/cutils/map/map_ut.cc | 50 ++++++++++++++++++++-
test/cutils/utils_string/utils_string_ut.cc | 17 ++++---
2 files changed, 61 insertions(+), 6 deletions(-)
diff --git a/test/cutils/map/map_ut.cc b/test/cutils/map/map_ut.cc
index fd75da28..7e4c97e4 100644
--- a/test/cutils/map/map_ut.cc
+++ b/test/cutils/map/map_ut.cc
@@ -18,7 +18,12 @@
#include <gtest/gtest.h>
#include "map.h"
-TEST(map_map_ut, test_map)
+static void ptr_ptr_map_kefree(void *key, void *value)
+{
+ return;
+}
+
+TEST(map_map_ut, test_map_string)
{
// map[string][bool]
map_t *map_test = nullptr;
@@ -37,3 +42,46 @@ TEST(map_map_ut, test_map)
map_itor_free(itor);
map_clear(map_test);
}
+
+TEST(map_map_ut, test_map_int)
+{
+ int key = 3;
+ int value = 5;
+ int *value_ptr = nullptr;
+ // map[int][int]
+ map_t *map_test = nullptr;
+
+ map_test = map_new(MAP_INT_INT, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
+ ASSERT_NE(map_test, nullptr);
+ ASSERT_EQ(map_insert(map_test, &key, &value), true);
+
+ key = 22;
+ value = 33;
+ ASSERT_EQ(map_insert(map_test, &key, &value), true);
+
+ value_ptr = (int *)map_search(map_test, &key);
+ ASSERT_EQ(*value_ptr, 33);
+
+ key = 44;
+ ASSERT_EQ(map_search(map_test, &key), nullptr);
+
+ map_clear(map_test);
+}
+
+TEST(map_map_ut, test_map_ptr)
+{
+ int *key_ptr = new int(3);
+ int *value_ptr = new int(5);
+ // map[ptr][ptr]
+ map_t *map_test = nullptr;
+
+ map_test = map_new(MAP_PTR_PTR, MAP_DEFAULT_CMP_FUNC, ptr_ptr_map_kefree);
+ ASSERT_NE(map_test, nullptr);
+ ASSERT_EQ(map_insert(map_test, key_ptr, value_ptr), true);
+ ASSERT_EQ(map_search(map_test, key_ptr), value_ptr);
+ ASSERT_EQ(map_search(map_test, nullptr), nullptr);
+
+ map_clear(map_test);
+ delete key_ptr;
+ delete value_ptr;
+}
diff --git a/test/cutils/utils_string/utils_string_ut.cc b/test/cutils/utils_string/utils_string_ut.cc
index 8b6c61a6..0f8f0011 100644
--- a/test/cutils/utils_string/utils_string_ut.cc
+++ b/test/cutils/utils_string/utils_string_ut.cc
@@ -833,26 +833,33 @@ TEST(utils_string_ut, test_str_token)
free(token);
}
-TEST(utils_string_ut, test_string_split_multi)
+TEST(utils_string_ut, test_string_split_n)
{
char **result = nullptr;
- ASSERT_EQ(util_string_split_multi(nullptr, ':'), nullptr);
+ ASSERT_EQ(util_string_split_n(nullptr, ':', 3), nullptr);
+ ASSERT_EQ(util_string_split_n("aa:bb", ':', 0), nullptr);
- result = util_string_split_multi("", ':');
+ result = util_string_split_n("", ':', 3);
ASSERT_STREQ(result[0], "");
ASSERT_EQ(result[1], nullptr);
util_free_array(result);
- result = util_string_split_multi("abcd;", ':');
+ result = util_string_split_n("abcd;", ':', 3);
ASSERT_STREQ(result[0], "abcd;");
ASSERT_EQ(result[1], nullptr);
util_free_array(result);
- result = util_string_split_multi("abc:dd:e", ':');
+ result = util_string_split_n("abc:dd:e", ':', 3);
ASSERT_STREQ(result[0], "abc");
ASSERT_STREQ(result[1], "dd");
ASSERT_STREQ(result[2], "e");
ASSERT_EQ(result[3], nullptr);
util_free_array(result);
+
+ result = util_string_split_n("abc:dd:e", ':', 2);
+ ASSERT_STREQ(result[0], "abc");
+ ASSERT_STREQ(result[1], "dd:e");
+ ASSERT_EQ(result[2], nullptr);
+ util_free_array(result);
}
--
2.25.1

View File

@ -1,32 +0,0 @@
From 81cf4edd6aa0c4170eb0ec48a6934ef3262a920d Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 22 Oct 2022 11:48:48 +0800
Subject: [PATCH 31/43] remove mnt point if add device mapper device failed
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../layer_store/graphdriver/devmapper/driver_devmapper.c | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
index f4d54963..0a4e5e57 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
@@ -106,7 +106,13 @@ static int do_create(const char *id, const char *parent, const struct graphdrive
goto out;
}
- ret = add_device(id, parent, driver->devset, create_opts->storage_opt);
+ if (add_device(id, parent, driver->devset, create_opts->storage_opt) != 0) {
+ ERROR("Failed to add device");
+ ret = -1;
+ if (util_path_remove(mnt_point_dir) != 0) {
+ ERROR("Remove path:%s failed", mnt_point_dir);
+ }
+ }
out:
free(mnt_parent_dir);
--
2.25.1

View File

@ -1,59 +0,0 @@
From 5ded258d2e9a331a1fa5e3f76757fdb1e868cc8f Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Thu, 20 Oct 2022 16:17:26 +0800
Subject: [PATCH 32/43] dec device info ref in grow device fs
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../graphdriver/devmapper/deviceset.c | 26 +++++++++----------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
index 78d8737d..4dadc336 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
@@ -2483,6 +2483,7 @@ static void cleanup_deleted_devices(struct graphdriver *driver)
device_info = lookup_device(driver->devset, idsarray[i]);
if (device_info == NULL || device_info->info == NULL) {
DEBUG("devmapper: no such device with hash(%s), just skip cleanup", idsarray[i]);
+ devmapper_device_info_ref_dec(device_info);
continue;
}
@@ -2900,21 +2901,20 @@ static int grow_device_fs(struct device_set *devset, const char *hash, uint64_t
if (size <= base_size) {
return 0;
- } else {
- DEBUG("devmapper: new fs size is larger than old basesize, start to grow fs");
- device_info = lookup_device(devset, hash);
- if (device_info == NULL) {
- ERROR("devmapper: lookup device %s failed", hash);
- ret = -1;
- goto out;
- }
+ }
- if (grow_fs(devset, device_info->info) != 0) {
- ret = -1;
- goto out;
- }
+ DEBUG("devmapper: new fs size is larger than old basesize, start to grow fs");
+ device_info = lookup_device(devset, hash);
+ if (device_info == NULL) {
+ ERROR("devmapper: lookup device %s failed", hash);
+ return -1;
}
-out:
+
+ if (grow_fs(devset, device_info->info) != 0) {
+ ret = -1;
+ }
+
+ devmapper_device_info_ref_dec(device_info);
return ret;
}
--
2.25.1

View File

@ -1,72 +0,0 @@
From 4b59b86a191a43d88423a1f2659392b8bd9ff849 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 26 Oct 2022 16:57:41 +0800
Subject: [PATCH 33/43] device mapper bugfix
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../layer_store/graphdriver/devmapper/driver_devmapper.c | 9 +++++++--
.../graphdriver/devmapper/wrapper_devmapper.c | 8 ++++----
2 files changed, 11 insertions(+), 6 deletions(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
index 0a4e5e57..dd231bd6 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
@@ -231,6 +231,9 @@ char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
if (rootfs == NULL) {
ERROR("Failed to join devmapper rootfs %s", mnt_point_dir);
ret = -1;
+ if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
+ DEBUG("devmapper: unmount %s failed", mnt_point_dir);
+ }
goto out;
}
@@ -246,6 +249,7 @@ char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
id_file = util_path_join(mnt_point_dir, "id");
if (!util_file_exists(id_file)) {
if (util_atomic_write_file(id_file, id, strlen(id), SECURE_CONFIG_FILE_MODE, true) != 0) {
+ ret = -1;
if (unmount_device(id, mnt_point_dir, driver->devset) != 0) {
DEBUG("devmapper: unmount %s failed", mnt_point_dir);
}
@@ -350,13 +354,14 @@ int devmapper_apply_diff(const char *id, const struct graphdriver *driver, const
if (archive_unpack(content, layer_fs, &options, &err) != 0) {
ERROR("devmapper: failed to unpack to %s: %s", layer_fs, err);
ret = -1;
- goto out;
+ goto umount_out;
}
+umount_out:
+ // umount layer if devmapper_mount_layer success
if (devmapper_umount_layer(id, driver) != 0) {
ERROR("devmapper: failed to umount layer %s", id);
ret = -1;
- goto out;
}
out:
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
index 7ae1fd40..c8f3d6d8 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/wrapper_devmapper.c
@@ -28,10 +28,10 @@
#include "utils.h"
#include "err_msg.h"
-static bool g_dm_saw_busy = false;
-static bool g_dm_saw_exist = false;
-static bool g_dm_saw_enxio = false; // no such device or address
-static bool g_dm_saw_eno_data = false; // no data available
+static __thread bool g_dm_saw_busy = false;
+static __thread bool g_dm_saw_exist = false;
+static __thread bool g_dm_saw_enxio = false; // no such device or address
+static __thread bool g_dm_saw_eno_data = false; // no data available
static int64_t dm_udev_wait_timeout = 0;
char *dev_strerror(int errnum)
--
2.25.1

View File

@ -1,25 +0,0 @@
From 1111a16537bddf5d272f58bd1b67969f105a426d Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Fri, 28 Oct 2022 11:21:19 +0800
Subject: [PATCH 34/43] delete syncCloseSem when close all wssession
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/daemon/entry/cri/websocket/service/ws_server.cc | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/daemon/entry/cri/websocket/service/ws_server.cc b/src/daemon/entry/cri/websocket/service/ws_server.cc
index 6438c4d7..a9b58685 100644
--- a/src/daemon/entry/cri/websocket/service/ws_server.cc
+++ b/src/daemon/entry/cri/websocket/service/ws_server.cc
@@ -250,6 +250,7 @@ void WebsocketServer::CloseAllWsSession()
close(it->second->pipes.at(0));
close(it->second->pipes.at(1));
(void)sem_destroy(it->second->syncCloseSem);
+ delete it->second->syncCloseSem;
delete it->second->sessionMutex;
delete it->second;
}
--
2.25.1

View File

@ -1,187 +0,0 @@
From b0022ff6d820d17428aa04e6be148e84dade0855 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Fri, 28 Oct 2022 16:32:03 +0800
Subject: [PATCH 35/43] [improve] debug information for console io
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
.../entry/cri/websocket/service/ws_server.cc | 22 +++++++++++++------
src/daemon/modules/events/collector.c | 12 +++++-----
src/utils/console/console.c | 8 ++++++-
src/utils/cutils/io_wrapper.h | 11 ++++++++++
4 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/src/daemon/entry/cri/websocket/service/ws_server.cc b/src/daemon/entry/cri/websocket/service/ws_server.cc
index a9b58685..41bb3fe8 100644
--- a/src/daemon/entry/cri/websocket/service/ws_server.cc
+++ b/src/daemon/entry/cri/websocket/service/ws_server.cc
@@ -79,17 +79,25 @@ int SessionData::PushMessage(unsigned char *message)
sessionMutex->lock();
- // In extreme scenarios, websocket data cannot be processed,
- // ignore the data coming in later to prevent iSulad from getting stuck
- if (close || buffer.size() >= FIFO_LIST_BUFFER_MAX_LEN) {
- free(message);
+ if (!close && buffer.size() < FIFO_LIST_BUFFER_MAX_LEN) {
+ buffer.push_back(message);
sessionMutex->unlock();
- return -1;
+ return 0;
}
- buffer.push_back(message);
+ // In extreme scenarios, websocket data cannot be processed,
+ // ignore the data coming in later to prevent iSulad from getting stuck
+ free(message);
sessionMutex->unlock();
- return 0;
+
+ if (close) {
+ DEBUG("Closed session");
+ }
+ if (buffer.size() >= FIFO_LIST_BUFFER_MAX_LEN) {
+ ERROR("Too large: %zu message!", buffer.size());
+ }
+
+ return -1;
}
bool SessionData::IsClosed()
diff --git a/src/daemon/modules/events/collector.c b/src/daemon/modules/events/collector.c
index 9dc4dbe7..433cb88b 100644
--- a/src/daemon/modules/events/collector.c
+++ b/src/daemon/modules/events/collector.c
@@ -339,28 +339,28 @@ static void supplement_annotations_for_container_msg(const container_t *cont, co
struct isulad_events_format *format_msg)
{
if (supplement_pid_for_container_msg(cont, msg, format_msg) != 0) {
- ERROR("Failed to supplement pid info");
+ WARN("Failed to supplement pid info");
}
if (supplement_exitcode_for_container_msg(cont, msg, format_msg) != 0) {
- ERROR("Failed to supplement exitCode info");
+ WARN("Failed to supplement exitCode info");
}
if (supplement_image_for_container_msg(cont, msg, format_msg) != 0) {
- ERROR("Failed to supplement image info");
+ WARN("Failed to supplement image info");
}
if (supplement_name_for_container_msg(cont, msg, format_msg) != 0) {
- ERROR("Failed to supplement name info");
+ WARN("Failed to supplement name info");
}
if (supplement_labels_for_container_msg(cont, msg, format_msg) != 0) {
- ERROR("Failed to supplement label info");
+ WARN("Failed to supplement label info");
}
if (strlen(msg->extra_annations) != 0) {
if (util_array_append(&format_msg->annotations, msg->extra_annations) != 0) {
- ERROR("Failed to supplement extra annations info");
+ WARN("Failed to supplement extra annations info");
}
}
diff --git a/src/utils/console/console.c b/src/utils/console/console.c
index 3565eef3..d5e5d9af 100644
--- a/src/utils/console/console.c
+++ b/src/utils/console/console.c
@@ -100,7 +100,7 @@ static int console_writer_write_data(const struct io_write_wrapper *writer, cons
}
ret = writer->write_func(writer->context, buf, (size_t)len);
if (ret <= 0 || ret != len) {
- ERROR("failed to write, error:%s", strerror(errno));
+ ERROR("Failed to write, type: %d, expect: %zd, wrote: %zd, error: %s!", writer->io_type, len, ret, strerror(errno));
return -1;
}
return 0;
@@ -401,6 +401,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
if (fifoinfd >= 0) {
ts.stdin_reader = stdinfd;
ts.stdin_writer.context = &fifoinfd;
+ ts.stdin_writer.io_type = FIFO_IN_IO;
ts.stdin_writer.write_func = fd_write_function;
if (tty) {
ret = epoll_loop_add_handler(&descr, ts.stdin_reader, console_cb_tty_stdin_with_escape, &ts);
@@ -418,6 +419,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
if (fifooutfd >= 0) {
ts.stdout_reader = fifooutfd;
ts.stdout_writer.context = &stdoutfd;
+ ts.stdin_writer.io_type = FIFO_OUT_IO;
ts.stdout_writer.write_func = fd_write_function;
ret = epoll_loop_add_handler(&descr, ts.stdout_reader, console_cb_stdio_copy, &ts);
if (ret) {
@@ -429,6 +431,7 @@ int console_loop_with_std_fd(int stdinfd, int stdoutfd, int stderrfd, int fifoin
if (fifoerrfd >= 0) {
ts.stderr_reader = fifoerrfd;
ts.stderr_writer.context = &stderrfd;
+ ts.stdin_writer.io_type = FIFO_ERR_IO;
ts.stderr_writer.write_func = fd_write_function;
ret = epoll_loop_add_handler(&descr, ts.stderr_reader, console_cb_stdio_copy, &ts);
if (ret) {
@@ -477,18 +480,21 @@ int console_loop_io_copy(int sync_fd, const int *srcfds, struct io_write_wrapper
ts[i].stdin_reader = srcfds[i];
ts[i].stdin_writer.context = writers[i].context;
ts[i].stdin_writer.write_func = writers[i].write_func;
+ ts[i].stdin_writer.io_type = FUNC_IN_IO;
ret = epoll_loop_add_handler(&descr, ts[i].stdin_reader, console_cb_stdio_copy, &ts[i]);
} else if (channels[i] == STDOUT_CHANNEL) {
// Reusing ts.stdout_reader and ts.stdout_writer for coping io
ts[i].stdout_reader = srcfds[i];
ts[i].stdout_writer.context = writers[i].context;
ts[i].stdout_writer.write_func = writers[i].write_func;
+ ts[i].stdin_writer.io_type = FUNC_OUT_IO;
ret = epoll_loop_add_handler(&descr, ts[i].stdout_reader, console_cb_stdio_copy, &ts[i]);
} else {
// Reusing ts.stderr_reader and ts.stderr_writer for coping io
ts[i].stderr_reader = srcfds[i];
ts[i].stderr_writer.context = writers[i].context;
ts[i].stderr_writer.write_func = writers[i].write_func;
+ ts[i].stdin_writer.io_type = FUNC_ERR_IO;
ret = epoll_loop_add_handler(&descr, ts[i].stderr_reader, console_cb_stdio_copy, &ts[i]);
}
if (ret != 0) {
diff --git a/src/utils/cutils/io_wrapper.h b/src/utils/cutils/io_wrapper.h
index 0134a822..a9340d01 100644
--- a/src/utils/cutils/io_wrapper.h
+++ b/src/utils/cutils/io_wrapper.h
@@ -21,6 +21,15 @@
extern "C" {
#endif
+enum {
+ FIFO_IN_IO = 0,
+ FIFO_OUT_IO,
+ FIFO_ERR_IO,
+ FUNC_IN_IO,
+ FUNC_OUT_IO,
+ FUNC_ERR_IO,
+};
+
typedef ssize_t (*io_write_func_t)(void *context, const void *data, size_t len);
typedef int (*io_close_func_t)(void *context, char **err);
@@ -28,6 +37,7 @@ struct io_write_wrapper {
void *context;
io_write_func_t write_func;
io_close_func_t close_func;
+ int io_type;
};
typedef ssize_t (*io_read_func_t)(void *context, void *buf, size_t len);
@@ -36,6 +46,7 @@ struct io_read_wrapper {
void *context;
io_read_func_t read;
io_close_func_t close;
+ int io_type;
};
#ifdef __cplusplus
--
2.25.1

View File

@ -1,288 +0,0 @@
From afe09041af292ae529ba8071e0737be778223853 Mon Sep 17 00:00:00 2001
From: songbuhuang <544824346@qq.com>
Date: Sat, 29 Oct 2022 16:35:21 +0800
Subject: [PATCH 36/43] add ut for file
Signed-off-by: songbuhuang <544824346@qq.com>
---
test/cutils/CMakeLists.txt | 1 +
test/cutils/utils_file/CMakeLists.txt | 18 ++
test/cutils/utils_file/utils_file_ut.cc | 229 ++++++++++++++++++++++++
3 files changed, 248 insertions(+)
create mode 100644 test/cutils/utils_file/CMakeLists.txt
create mode 100644 test/cutils/utils_file/utils_file_ut.cc
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 2447b781..11e0ee90 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -29,6 +29,7 @@ add_subdirectory(utils_network)
add_subdirectory(utils_aes)
add_subdirectory(utils_error)
add_subdirectory(utils_fs)
+add_subdirectory(utils_file)
add_subdirectory(utils_filters)
add_subdirectory(utils_timestamp)
add_subdirectory(utils_mount_spec)
diff --git a/test/cutils/utils_file/CMakeLists.txt b/test/cutils/utils_file/CMakeLists.txt
new file mode 100644
index 00000000..20317e15
--- /dev/null
+++ b/test/cutils/utils_file/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE utils_file_ut)
+
+add_executable(${EXE}
+ utils_file_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${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
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/cutils/utils_file/utils_file_ut.cc b/test/cutils/utils_file/utils_file_ut.cc
new file mode 100644
index 00000000..ccd60ba4
--- /dev/null
+++ b/test/cutils/utils_file/utils_file_ut.cc
@@ -0,0 +1,229 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: utils_file unit test
+ * Author: huangsong
+ * Create: 2022-10-26
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <gtest/gtest.h>
+#include "mock.h"
+#include "utils_file.h"
+#include "constants.h"
+#include "map.h"
+
+#define FILE_PERMISSION_TEST 0755
+
+TEST(utils_file, test_util_dir_exists)
+{
+ ASSERT_EQ(util_dir_exists(nullptr), false);
+
+ const char *path = "/tmp/test";
+ ASSERT_EQ(util_dir_exists(path), false);
+ ASSERT_EQ(util_mkdir_p(path, FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_dir_exists(path), true);
+ ASSERT_EQ(util_path_remove(path), 0);
+
+}
+
+TEST(utils_file, test_util_fileself_exists)
+{
+ ASSERT_EQ(util_fileself_exists(nullptr), false);
+
+ std::string path = "/tmp/test";
+ std::string path_link = "/tmp/test/link";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_fileself_exists(path_link.c_str()), false);
+ ASSERT_EQ(symlink(path.c_str(),path_link.c_str()), 0);
+ ASSERT_EQ(util_fileself_exists(path_link.c_str()), true);
+ ASSERT_EQ(util_path_remove(path_link.c_str()), 0);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_util_file_exists)
+{
+ ASSERT_EQ(util_file_exists(nullptr), false);
+
+ std::string path = "/tmp/test";
+ ASSERT_EQ(util_file_exists(path.c_str()), false);
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_file_exists(path.c_str()), true);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+
+TEST(utils_file, test_util_recursive_rmdir)
+{
+ ASSERT_EQ(util_recursive_rmdir(nullptr,0), -1);
+
+ std::string path = "/tmp/test";
+ std::string path_link = "/tmp/test/link";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_mkdir_p(path_link.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_recursive_rmdir(path.c_str(),1), 0);
+ ASSERT_EQ(util_file_exists(path.c_str()), false);
+ ASSERT_EQ(util_file_exists(path_link.c_str()), false);
+}
+
+TEST(utils_file, test_util_ensure_path)
+{
+ char *rpath = NULL;
+ std::string path = "/tmp/test";
+ ASSERT_EQ(util_ensure_path(nullptr,path.c_str()), -1);
+ ASSERT_EQ(util_ensure_path(&rpath,nullptr), -1);
+
+ ASSERT_EQ(util_file_exists(path.c_str()), false);
+ ASSERT_EQ(util_ensure_path(&rpath,path.c_str()), 0);
+ ASSERT_EQ(util_file_exists(rpath), true);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_util_build_dir)
+{
+ ASSERT_EQ(util_build_dir(nullptr), -1);
+
+ std::string path = "/tmp/test/file";
+ ASSERT_EQ(util_build_dir(path.c_str()), 0);
+ ASSERT_EQ(util_file_exists("/tmp"), true);
+ ASSERT_EQ(util_file_exists("/tmp/test"), true);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_util_human_size)
+{
+ uint64_t b = 1;
+ uint64_t kb = 1024;
+ uint64_t mb = 1024 * 1024;
+ uint64_t gb = 1024 * 1024 * 1024;
+ ASSERT_STREQ(util_human_size(b), "1B");
+ ASSERT_STREQ(util_human_size(kb), "1KB");
+ ASSERT_STREQ(util_human_size(mb), "1MB");
+ ASSERT_STREQ(util_human_size(gb), "1GB");
+
+ ASSERT_STREQ(util_human_size_decimal(b), "1B");
+ ASSERT_STREQ(util_human_size_decimal(kb), "1.000KB");
+ ASSERT_STREQ(util_human_size_decimal(mb), "1.000MB");
+ ASSERT_STREQ(util_human_size_decimal(gb), "1.000GB");
+}
+
+TEST(utils_file, test_util_open)
+{
+ std::string path = "/tmp/test";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_NE(util_open(path.c_str(), O_RDONLY, 0), -1);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_util_add_path)
+{
+ std::string path = "/tmp/test/";
+ std::string add_path = "add";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_STREQ(util_add_path(path.c_str(),add_path.c_str()), "/tmp/test/add");
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+
+ std::string path1 = "/tmp/test";
+ ASSERT_EQ(util_mkdir_p(path1.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_STREQ(util_add_path(path1.c_str(),add_path.c_str()), "/tmp/add");
+ ASSERT_EQ(util_path_remove(path1.c_str()), 0);
+}
+
+TEST(utils_file, test_verify_file_and_get_real_path)
+{
+ std::string path = "/tmp/test";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_STREQ(verify_file_and_get_real_path(path.c_str()),"/tmp/test");
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_look_path)
+{
+ std::string path = "/usr/bin/nsenter";
+ char *err = NULL;
+ ASSERT_STREQ(look_path("nsenter",&err),path.c_str());
+}
+
+TEST(utils_file, test_util_copy_file)
+{
+ std::string path = "/tmp/test";
+ ASSERT_EQ(util_copy_file("/etc/hosts",path.c_str(),NETWORK_MOUNT_FILE_MODE), 0);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_copy_file("/etc/hosts",path.c_str(),NETWORK_MOUNT_FILE_MODE), -1);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_utils_calculate_dir_size_without_hardlink)
+{
+ std::string path = "/tmp/test";
+ std::string hosts = "/etc/hosts";
+ ASSERT_EQ(util_copy_file(hosts.c_str(),path.c_str(),NETWORK_MOUNT_FILE_MODE), 0);
+ int64_t total_size = 0;
+ int64_t total_inodes = 0;
+ utils_calculate_dir_size_without_hardlink("/tmp/", &total_size,&total_inodes);
+ ASSERT_NE(total_size, 0);
+ ASSERT_NE(total_inodes, 0);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+static bool parse_checked_layer_cb(const char *line, void *context)
+{
+ printf("this is stdout\n");
+ fprintf(stderr, "this is stderr\n");
+ return true;
+}
+
+TEST(utils_file, test_util_proc_file_line_by_line)
+{
+ std::string path = "/tmp/test";
+ std::string content = "hello world";
+ int fd;
+ fd = util_open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_EQ(util_write_nointr_in_total(fd, content.c_str(), strlen(content.c_str())),11);
+ FILE *fp = NULL;
+ map_t *checked_layers = NULL;
+ fp = util_fopen(path.c_str(), "r");
+ checked_layers = map_new(MAP_STR_BOOL, MAP_DEFAULT_CMP_FUNC, MAP_DEFAULT_FREE_FUNC);
+ ASSERT_EQ(util_proc_file_line_by_line(fp, parse_checked_layer_cb, (void *)checked_layers),0);
+ fclose(fp);
+ ASSERT_EQ(util_path_remove(path.c_str()), 0);
+}
+
+TEST(utils_file, test_util_recursive_remove_path)
+{
+ ASSERT_EQ(util_recursive_remove_path(nullptr), -1);
+
+ std::string path = "/tmp/test";
+ std::string path_link = "/tmp/test/link";
+ ASSERT_EQ(util_mkdir_p(path.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_mkdir_p(path_link.c_str(), FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_recursive_remove_path(path.c_str()), 0);
+ ASSERT_EQ(util_file_exists(path.c_str()), false);
+ ASSERT_EQ(util_file_exists(path_link.c_str()), false);
+
+}
+
+TEST(utils_file, test_util_copy_dir_recursive)
+{
+ char *path = (char*)"/tmp/test1/";
+ char *src = (char*)"/tmp/test/";
+ ASSERT_EQ(util_mkdir_p(path, FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_mkdir_p(src, FILE_PERMISSION_TEST), 0);
+ ASSERT_EQ(util_copy_dir_recursive(path,src), 0);
+ ASSERT_EQ(util_recursive_remove_path(path), 0);
+ ASSERT_EQ(util_recursive_remove_path(src), 0);
+}
+
+
--
2.25.1

View File

@ -1,688 +0,0 @@
From d7b324c04b4b353270216887b05e245337942b97 Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Mon, 24 Oct 2022 20:46:47 +0800
Subject: [PATCH 37/43] Add extend C for header files
Signed-off-by: zhongtao <taozh97@163.com>
---
src/cmd/isula/stream/attach.h | 9 +++++++++
src/cmd/isula/stream/cp.h | 8 ++++++++
src/cmd/isula/stream/exec.h | 8 ++++++++
src/daemon/modules/api/log_gather_api.h | 8 ++++++++
src/daemon/modules/api/plugin_api.h | 8 ++++++++
src/daemon/modules/container/container_events_handler.h | 8 ++++++++
.../modules/container/restart_manager/restartmanager.h | 8 ++++++++
src/daemon/modules/container/supervisor/supervisor.h | 8 ++++++++
src/daemon/modules/events/monitord.h | 8 ++++++++
src/daemon/modules/image/embedded/db/db_all.h | 8 ++++++++
src/daemon/modules/image/embedded/db/db_common.h | 8 ++++++++
src/daemon/modules/image/embedded/db/db_images_common.h | 8 ++++++++
src/daemon/modules/image/embedded/db/sqlite_common.h | 8 ++++++++
src/daemon/modules/image/embedded/embedded_image.h | 8 ++++++++
src/daemon/modules/image/embedded/lim.h | 8 ++++++++
src/daemon/modules/image/embedded/snapshot/embedded.h | 8 ++++++++
src/daemon/modules/image/embedded/snapshot/snapshot.h | 8 ++++++++
.../modules/image/embedded/snapshot/snapshot_def.h | 7 +++++++
src/daemon/modules/image/external/ext_image.h | 8 ++++++++
src/daemon/modules/image/oci/registry_type.h | 8 ++++++++
.../graphdriver/devmapper/devices_constants.h | 8 ++++++++
src/daemon/modules/plugin/pspec.h | 8 ++++++++
src/daemon/modules/spec/specs_mount.h | 8 ++++++++
src/daemon/modules/spec/specs_security.h | 8 ++++++++
src/utils/buffer/buffer.h | 8 ++++++++
src/utils/cutils/linked_list.h | 8 ++++++++
src/utils/http/parser.h | 8 ++++++++
27 files changed, 216 insertions(+)
diff --git a/src/cmd/isula/stream/attach.h b/src/cmd/isula/stream/attach.h
index 2e71852b..9792da9d 100644
--- a/src/cmd/isula/stream/attach.h
+++ b/src/cmd/isula/stream/attach.h
@@ -18,9 +18,18 @@
#include "client_arguments.h"
#include "isula_libutils/container_inspect.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern const char g_cmd_attach_desc[];
extern const char g_cmd_attach_usage[];
extern struct client_arguments g_cmd_attach_args;
int inspect_container(const struct client_arguments *args, container_inspect **inspect_data);
int cmd_attach_main(int argc, const char **argv);
+
+#ifdef __cplusplus
+}
+#endif
+
#endif // CMD_ISULA_STREAM_ATTACH_H
diff --git a/src/cmd/isula/stream/cp.h b/src/cmd/isula/stream/cp.h
index 821a3887..491d5421 100644
--- a/src/cmd/isula/stream/cp.h
+++ b/src/cmd/isula/stream/cp.h
@@ -17,9 +17,17 @@
#include "client_arguments.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern const char g_cmd_cp_desc[];
extern const char g_cmd_cp_usage[];
extern struct client_arguments g_cmd_cp_args;
int cmd_cp_main(int argc, const char **argv);
+#ifdef __cplusplus
+}
+#endif
+
#endif // CMD_ISULA_STREAM_CP_H
diff --git a/src/cmd/isula/stream/exec.h b/src/cmd/isula/stream/exec.h
index 6161c62f..d507d71e 100644
--- a/src/cmd/isula/stream/exec.h
+++ b/src/cmd/isula/stream/exec.h
@@ -22,6 +22,10 @@
#include "attach.h"
#include "command_parser.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
#define EXEC_OPTIONS(cmdargs) \
{ \
CMD_OPT_TYPE_CALLBACK, false, "env", 'e', &(cmdargs).extra_env, "Set environment variables", \
@@ -51,4 +55,8 @@ extern const char g_cmd_exec_usage[];
extern struct client_arguments g_cmd_exec_args;
int cmd_exec_main(int argc, const char **argv);
+#ifdef __cplusplus
+}
+#endif
+
#endif // CMD_ISULA_STREAM_EXEC_H
diff --git a/src/daemon/modules/api/log_gather_api.h b/src/daemon/modules/api/log_gather_api.h
index b72a5800..2c98ff11 100644
--- a/src/daemon/modules/api/log_gather_api.h
+++ b/src/daemon/modules/api/log_gather_api.h
@@ -22,6 +22,10 @@
#define REV_BUF_SIZE 4096
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct log_gather_conf {
const char *fifo_path;
int *exitcode;
@@ -40,5 +44,9 @@ enum log_gather_driver {
void *log_gather(void *arg);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/api/plugin_api.h b/src/daemon/modules/api/plugin_api.h
index 303ba6bf..bf138ec1 100644
--- a/src/daemon/modules/api/plugin_api.h
+++ b/src/daemon/modules/api/plugin_api.h
@@ -41,6 +41,10 @@
#define PLUGIN_EVENT_CONTAINER_POST_STOP (1UL << 2)
#define PLUGIN_EVENT_CONTAINER_POST_REMOVE (1UL << 3)
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef struct plugin_manifest {
uint64_t init_type;
uint64_t watch_event;
@@ -109,4 +113,8 @@ int plugin_event_container_post_stop(const container_t *cont);
int plugin_event_container_post_remove(const container_t *cont);
int plugin_event_container_post_remove2(const char *cid, const oci_runtime_spec *oci);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_API_PLUGIN_API_H
diff --git a/src/daemon/modules/container/container_events_handler.h b/src/daemon/modules/container/container_events_handler.h
index f38f293d..77cf5d06 100644
--- a/src/daemon/modules/container/container_events_handler.h
+++ b/src/daemon/modules/container/container_events_handler.h
@@ -22,10 +22,18 @@
#include "container_api.h"
#include "events_format.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
container_events_handler_t *container_events_handler_new();
void container_events_handler_free(container_events_handler_t *handler);
int container_events_handler_post_events(const struct isulad_events_format *event);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_CONTAINER_CONTAINER_EVENTS_HANDLER_H
diff --git a/src/daemon/modules/container/restart_manager/restartmanager.h b/src/daemon/modules/container/restart_manager/restartmanager.h
index 1f5f13b3..106343b2 100644
--- a/src/daemon/modules/container/restart_manager/restartmanager.h
+++ b/src/daemon/modules/container/restart_manager/restartmanager.h
@@ -22,6 +22,10 @@
#include "isula_libutils/host_config.h"
#include "container_api.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
void restart_policy_free(host_config_restart_policy *policy);
restart_manager_t *restart_manager_new(const host_config_restart_policy *policy, int failure_count);
@@ -43,4 +47,8 @@ int restart_manager_wait_cancel(restart_manager_t *rm, uint64_t timeout);
int container_restart_in_thread(const char *id, uint64_t timeout, int exit_code);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_CONTAINER_RESTART_MANAGER_RESTARTMANAGER_H
diff --git a/src/daemon/modules/container/supervisor/supervisor.h b/src/daemon/modules/container/supervisor/supervisor.h
index e5b5a2e7..2b208c5a 100644
--- a/src/daemon/modules/container/supervisor/supervisor.h
+++ b/src/daemon/modules/container/supervisor/supervisor.h
@@ -19,8 +19,16 @@
#include "container_unix.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern char *exit_fifo_name(const char *cont_state_path);
extern int new_supervisor();
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/events/monitord.h b/src/daemon/modules/events/monitord.h
index beace80e..1741983d 100644
--- a/src/daemon/modules/events/monitord.h
+++ b/src/daemon/modules/events/monitord.h
@@ -20,6 +20,10 @@
#include "utils.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct monitord_sync_data {
sem_t *monitord_sem;
int *exit_code;
@@ -27,4 +31,8 @@ struct monitord_sync_data {
int new_monitord(struct monitord_sync_data *msync);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/db/db_all.h b/src/daemon/modules/image/embedded/db/db_all.h
index 9fbe2729..e72777c2 100644
--- a/src/daemon/modules/image/embedded/db/db_all.h
+++ b/src/daemon/modules/image/embedded/db/db_all.h
@@ -20,6 +20,10 @@
#include <stdbool.h>
#include "db_common.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct db_sninfo {
char *snid;
char *parent_snid;
@@ -73,5 +77,9 @@ int db_read_all_images_info(struct db_all_images **image_info);
void db_all_imginfo_free(struct db_all_images *images_info);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/db/db_common.h b/src/daemon/modules/image/embedded/db/db_common.h
index ab83599f..8ed1138c 100644
--- a/src/daemon/modules/image/embedded/db/db_common.h
+++ b/src/daemon/modules/image/embedded/db/db_common.h
@@ -25,9 +25,17 @@
#define DB_DEREF_ONLY 4
#define DB_NOT_EXIST 5
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int db_common_init(const char *rootpath);
void db_common_finish(void);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_IMAGE_EMBEDDED_DB_DB_COMMON_H
diff --git a/src/daemon/modules/image/embedded/db/db_images_common.h b/src/daemon/modules/image/embedded/db/db_images_common.h
index 1fde16cc..a35ea49f 100644
--- a/src/daemon/modules/image/embedded/db/db_images_common.h
+++ b/src/daemon/modules/image/embedded/db/db_images_common.h
@@ -17,6 +17,10 @@
#include <stdint.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/* common interface definition for database */
struct db_single_image_info {
@@ -32,5 +36,9 @@ struct db_all_images_info {
struct db_single_image_info **images_info;
};
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/db/sqlite_common.h b/src/daemon/modules/image/embedded/db/sqlite_common.h
index 7562932c..8c5db83a 100644
--- a/src/daemon/modules/image/embedded/db/sqlite_common.h
+++ b/src/daemon/modules/image/embedded/db/sqlite_common.h
@@ -19,6 +19,10 @@
#define DBNAME "sqlite.db"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef int(*sqlite_callback_t)(void *, int, char **, char **);
sqlite3 *get_global_db();
@@ -32,5 +36,9 @@ int db_sqlite_request(const char *stmt);
int db_sqlite_request_callback(const char *stmt,
sqlite_callback_t callback, void *data);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/embedded_image.h b/src/daemon/modules/image/embedded/embedded_image.h
index 2177a40b..b1fafacf 100644
--- a/src/daemon/modules/image/embedded/embedded_image.h
+++ b/src/daemon/modules/image/embedded/embedded_image.h
@@ -18,6 +18,10 @@
#include <stdint.h>
#include "image_api.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
bool embedded_detect(const char *image_name);
int embedded_prepare_rf(const im_prepare_request *request, char **real_rootfs);
@@ -48,4 +52,8 @@ int embedded_init(const isulad_daemon_configs *args);
void embedded_exit();
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/lim.h b/src/daemon/modules/image/embedded/lim.h
index cb21884e..fb5bb414 100644
--- a/src/daemon/modules/image/embedded/lim.h
+++ b/src/daemon/modules/image/embedded/lim.h
@@ -22,6 +22,10 @@
#define IMAGE_DATA_TYPE_CONFIG "config"
#define IMAGE_DATA_TYPE_CONFIG_PATH "config_path"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct image_creator {
char *name;
char *type;
@@ -56,5 +60,9 @@ int lim_query_image_data(const char *name, const char *type,
int lim_create_rw_layer(char *name, const char *id, char **options,
char **mount_string);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/snapshot/embedded.h b/src/daemon/modules/image/embedded/snapshot/embedded.h
index eaf4b588..e0cf7bab 100644
--- a/src/daemon/modules/image/embedded/snapshot/embedded.h
+++ b/src/daemon/modules/image/embedded/snapshot/embedded.h
@@ -19,6 +19,10 @@
#include "linked_list.h"
#include "snapshot_def.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct snapshot_plugin ebd_plugin();
int ebd_create_layer(char *id, char *parent, uint32_t layer_attribute,
@@ -31,5 +35,9 @@ int ebd_apply_diff(char *id, char *parent, char *archive, char *metadata);
int ebd_generate_mount_string(struct db_image *imginfo,
struct db_sninfo **sninfos, char **mount_string);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/snapshot/snapshot.h b/src/daemon/modules/image/embedded/snapshot/snapshot.h
index dd2e42e7..fae6c8da 100644
--- a/src/daemon/modules/image/embedded/snapshot/snapshot.h
+++ b/src/daemon/modules/image/embedded/snapshot/snapshot.h
@@ -22,11 +22,19 @@
#include "db_all.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int snapshot_init(uint32_t driver_type);
int snapshot_generate_mount_string(uint32_t driver_type,
struct db_image *imginfo,
struct db_sninfo **sninfos, char **mount_string);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/embedded/snapshot/snapshot_def.h b/src/daemon/modules/image/embedded/snapshot/snapshot_def.h
index fbc2c791..eaf397a6 100644
--- a/src/daemon/modules/image/embedded/snapshot/snapshot_def.h
+++ b/src/daemon/modules/image/embedded/snapshot/snapshot_def.h
@@ -26,6 +26,10 @@
#define LAYER_NUM_MAX 125
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef int(*create_layer_cb)(char *id, char *parent, uint32_t layer_attribute,
char **options, char **mount_string);
@@ -44,6 +48,9 @@ struct snapshot_plugin {
generate_mount_string_cb gms;
};
+#ifdef __cplusplus
+}
+#endif
#endif
diff --git a/src/daemon/modules/image/external/ext_image.h b/src/daemon/modules/image/external/ext_image.h
index ded2b8ca..17ef4d98 100644
--- a/src/daemon/modules/image/external/ext_image.h
+++ b/src/daemon/modules/image/external/ext_image.h
@@ -26,6 +26,10 @@
#include "image_api.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
bool ext_detect(const char *image_name);
int ext_filesystem_usage(const im_container_fs_usage_request *request, imagetool_fs_info **fs_usage);
@@ -46,4 +50,8 @@ int ext_logout(const im_logout_request *request);
int ext_init(const isulad_daemon_configs *args);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/image/oci/registry_type.h b/src/daemon/modules/image/oci/registry_type.h
index 11135250..bfc77f4a 100644
--- a/src/daemon/modules/image/oci/registry_type.h
+++ b/src/daemon/modules/image/oci/registry_type.h
@@ -29,6 +29,10 @@
#define MAX_LAYER_NUM 125
#define ROOTFS_TYPE "layers"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef struct {
char *schema;
char *realm;
@@ -132,4 +136,8 @@ void free_challenge(challenge *c);
void free_layer_blob(layer_blob *layer);
void free_pull_desc(pull_descriptor *desc);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_IMAGE_OCI_REGISTRY_TYPE_H
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/devices_constants.h b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/devices_constants.h
index 5b3db2bd..01bbfef7 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/devices_constants.h
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/devices_constants.h
@@ -35,6 +35,10 @@
#define DEFAULT_DEVICE_SET_MODE 0700
+#ifdef __cplusplus
+extern "C" {
+#endif
+
typedef struct {
map_t *map; // map string image_devmapper_device_info* key string will be strdup value ptr will not
} metadata_store_t;
@@ -73,4 +77,8 @@ struct device_set {
bool user_base_size;
};
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/plugin/pspec.h b/src/daemon/modules/plugin/pspec.h
index 618159b1..0ad26848 100644
--- a/src/daemon/modules/plugin/pspec.h
+++ b/src/daemon/modules/plugin/pspec.h
@@ -19,6 +19,10 @@
#include "isula_libutils/oci_runtime_spec.h"
#include "isula_libutils/oci_runtime_pspec.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
/*
* extract pspec from oci.
* return NULL when failed. oci not modified.
@@ -39,5 +43,9 @@ int set_pspec(oci_runtime_spec *oci, const char *data);
*/
char *merge_pspec(const char *base, const char *data);
+#ifdef __cplusplus
+}
+#endif
+
#endif // DAEMON_MODULES_PLUGIN_PSPEC_H
diff --git a/src/daemon/modules/spec/specs_mount.h b/src/daemon/modules/spec/specs_mount.h
index 07c07a37..3283d92b 100644
--- a/src/daemon/modules/spec/specs_mount.h
+++ b/src/daemon/modules/spec/specs_mount.h
@@ -26,6 +26,10 @@
#include "isula_libutils/oci_runtime_hooks.h"
#include "isula_libutils/oci_runtime_spec.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int adapt_settings_for_mounts(oci_runtime_spec *oci_spec, container_config *container_spec);
int merge_conf_mounts(oci_runtime_spec *oci_spec, host_config *host_spec,
@@ -43,4 +47,8 @@ int merge_conf_device(oci_runtime_spec *oci_spec, host_config *host_spec);
int setup_ipc_dirs(host_config *host_spec, container_config_v2_common_config *v2_spec);
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/daemon/modules/spec/specs_security.h b/src/daemon/modules/spec/specs_security.h
index f33814d2..37315a95 100644
--- a/src/daemon/modules/spec/specs_security.h
+++ b/src/daemon/modules/spec/specs_security.h
@@ -25,6 +25,10 @@
#include "isula_libutils/container_config_v2.h"
#include "isula_libutils/oci_runtime_spec.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
int merge_default_seccomp_spec(oci_runtime_spec *oci_spec, const defs_process_capabilities *capabilities);
int merge_caps(oci_runtime_spec *oci_spec, const char **adds, size_t adds_len, const char **drops, size_t drops_len);
int refill_oci_process_capabilities(defs_process_capabilities **caps, const char **src_caps, size_t src_caps_len);
@@ -36,4 +40,8 @@ int merge_seccomp(oci_runtime_spec *oci_spec, const char *seccomp_profile);
int merge_selinux(oci_runtime_spec *oci_spec, container_config_v2_common_config *v2_spec);
#endif
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/utils/buffer/buffer.h b/src/utils/buffer/buffer.h
index 168a61f6..83276100 100644
--- a/src/utils/buffer/buffer.h
+++ b/src/utils/buffer/buffer.h
@@ -19,6 +19,10 @@
#include <strings.h>
#include <stdarg.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct Buffer {
char *contents;
size_t bytes_used;
@@ -32,4 +36,8 @@ size_t buffer_strlen(const Buffer *buf);
void buffer_free(Buffer *buf);
int buffer_append(Buffer *buf, const char *append, size_t len);
void buffer_empty(Buffer *buf);
+
+#ifdef __cplusplus
+}
+#endif
#endif
diff --git a/src/utils/cutils/linked_list.h b/src/utils/cutils/linked_list.h
index 59acb2a8..3a87990a 100644
--- a/src/utils/cutils/linked_list.h
+++ b/src/utils/cutils/linked_list.h
@@ -17,6 +17,10 @@
#include <stddef.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct linked_list {
void *elem;
struct linked_list *next;
@@ -122,5 +126,9 @@ static inline size_t linked_list_len(struct linked_list *list)
return i;
}
+#ifdef __cplusplus
+}
+#endif
+
#endif
diff --git a/src/utils/http/parser.h b/src/utils/http/parser.h
index 9a37bd7f..ce5fe5e7 100644
--- a/src/utils/http/parser.h
+++ b/src/utils/http/parser.h
@@ -55,6 +55,10 @@
#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#ifdef __cplusplus
+extern "C" {
+#endif
+
struct parsed_http_message {
enum http_method method;
int status_code;
@@ -88,5 +92,9 @@ int parse_http(const char *buf, size_t len, struct parsed_http_message *m,
enum http_parser_type type);
char *get_header_value(const struct parsed_http_message *m, const char *header);
+#ifdef __cplusplus
+}
+#endif
+
#endif
--
2.25.1

View File

@ -1,119 +0,0 @@
From a27582c9e626d01c47ff29d4b2480813a69fa3a6 Mon Sep 17 00:00:00 2001
From: songbuhuang <544824346@qq.com>
Date: Tue, 18 Oct 2022 17:19:00 +0800
Subject: [PATCH 38/43] add isula create --rm option
Signed-off-by: songbuhuang <544824346@qq.com>
---
src/cmd/isula/base/create.c | 14 ++++++++++++++
src/cmd/isula/base/create.h | 7 +++++++
src/cmd/isula/base/run.c | 17 +++--------------
src/cmd/isula/base/run.h | 9 +--------
4 files changed, 25 insertions(+), 22 deletions(-)
diff --git a/src/cmd/isula/base/create.c b/src/cmd/isula/base/create.c
index 97da80ac..8cef5d17 100644
--- a/src/cmd/isula/base/create.c
+++ b/src/cmd/isula/base/create.c
@@ -2149,6 +2149,15 @@ out:
return ret;
}
+static int create_check_auto_remove(const struct client_arguments *args)
+{
+ if (args->custom_conf.auto_remove && ((args->restart != NULL) && (strcmp("no", args->restart) != 0))) {
+ COMMAND_ERROR("Conflicting options: --restart and --rm");
+ return -1;
+ }
+ return 0;
+}
+
int create_checker(struct client_arguments *args)
{
int ret = 0;
@@ -2236,6 +2245,11 @@ int create_checker(struct client_arguments *args)
goto out;
}
+ if (create_check_auto_remove(args)) {
+ ret = -1;
+ goto out;
+ }
+
out:
return ret;
}
diff --git a/src/cmd/isula/base/create.h b/src/cmd/isula/base/create.h
index 467fefe8..4f63d92f 100644
--- a/src/cmd/isula/base/create.h
+++ b/src/cmd/isula/base/create.h
@@ -306,6 +306,13 @@ extern "C" {
&(cmdargs).restart, \
"Restart policy to apply when a container exits(no, always, on-reboot, on-failure[:max-retries])", \
NULL }, \
+ { CMD_OPT_TYPE_BOOL, \
+ false, \
+ "rm", \
+ 0, \
+ &(cmdargs).custom_conf.auto_remove, \
+ "Automatically remove the container when it exits", \
+ NULL }, \
{ CMD_OPT_TYPE_STRING, \
false, \
"host-channel", \
diff --git a/src/cmd/isula/base/run.c b/src/cmd/isula/base/run.c
index 53e89c3d..73486c95 100644
--- a/src/cmd/isula/base/run.c
+++ b/src/cmd/isula/base/run.c
@@ -195,11 +195,8 @@ free_out:
static int run_checker(struct client_arguments *args)
{
- int ret = 0;
-
- ret = create_checker(args);
- if (ret) {
- goto out;
+ if (create_checker(args)) {
+ return -1;
}
/* Make detach option a high priority than terminal */
@@ -208,13 +205,5 @@ static int run_checker(struct client_arguments *args)
args->custom_conf.attach_stdout = false;
args->custom_conf.attach_stderr = false;
}
-
- if (args->custom_conf.auto_remove && ((args->restart != NULL) && (strcmp("no", args->restart) != 0))) {
- COMMAND_ERROR("Conflicting options: --restart and --rm");
- ret = -1;
- goto out;
- }
-
-out:
- return ret;
+ return 0;
}
diff --git a/src/cmd/isula/base/run.h b/src/cmd/isula/base/run.h
index 2658c33d..efca28a9 100644
--- a/src/cmd/isula/base/run.h
+++ b/src/cmd/isula/base/run.h
@@ -34,14 +34,7 @@ extern "C" {
'd', \
&(cmdargs).detach, \
"Run container in background and print container ID", \
- NULL }, \
- { CMD_OPT_TYPE_BOOL, \
- false, \
- "rm", \
- 0, \
- &(cmdargs).custom_conf.auto_remove, \
- "Automatically remove the container when it exits", \
- NULL },
+ NULL },
extern const char g_cmd_run_desc[];
extern const char g_cmd_run_usage[];
--
2.25.1

View File

@ -1,589 +0,0 @@
From 587aa25d7f44f79c28c3331b81bbc77efb7e1eb1 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Thu, 27 Oct 2022 00:45:53 -0700
Subject: [PATCH 39/43] feat add container cleanup module
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
src/daemon/modules/api/image_api.h | 6 +
src/daemon/modules/container/CMakeLists.txt | 3 +
src/daemon/modules/container/container_unix.c | 2 +
.../container/leftover_cleanup/CMakeLists.txt | 19 +++
.../container/leftover_cleanup/cleanup.c | 135 ++++++++++++++++++
.../container/leftover_cleanup/cleanup.h | 49 +++++++
.../leftover_cleanup/oci_rootfs_clean.c | 71 +++++++++
.../leftover_cleanup/oci_rootfs_clean.h | 30 ++++
src/daemon/modules/image/image.c | 26 ++++
src/daemon/modules/image/oci/oci_image.c | 5 +
src/daemon/modules/image/oci/oci_image.h | 1 +
.../oci/storage/rootfs_store/rootfs_store.c | 5 +
.../oci/storage/rootfs_store/rootfs_store.h | 3 +
.../modules/image/oci/storage/storage.c | 5 +
.../modules/image/oci/storage/storage.h | 2 +
15 files changed, 362 insertions(+)
create mode 100644 src/daemon/modules/container/leftover_cleanup/CMakeLists.txt
create mode 100644 src/daemon/modules/container/leftover_cleanup/cleanup.c
create mode 100644 src/daemon/modules/container/leftover_cleanup/cleanup.h
create mode 100644 src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
create mode 100644 src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
diff --git a/src/daemon/modules/api/image_api.h b/src/daemon/modules/api/image_api.h
index 10fa7b5b..a1c6084a 100644
--- a/src/daemon/modules/api/image_api.h
+++ b/src/daemon/modules/api/image_api.h
@@ -184,6 +184,10 @@ typedef struct {
char *name_id;
} im_export_request;
+typedef struct {
+ char *type;
+} im_get_rf_dir_request;
+
typedef struct {
char *name_id;
bool force;
@@ -325,6 +329,8 @@ int im_container_export(const im_export_request *request);
void free_im_export_request(im_export_request *ptr);
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request);
+
int im_resolv_image_name(const char *image_type, const char *image_name, char **resolved_name);
container_inspect_graph_driver *im_graphdriver_get_metadata_by_container_id(const char *id);
diff --git a/src/daemon/modules/container/CMakeLists.txt b/src/daemon/modules/container/CMakeLists.txt
index fd2a0b10..def602c7 100644
--- a/src/daemon/modules/container/CMakeLists.txt
+++ b/src/daemon/modules/container/CMakeLists.txt
@@ -5,6 +5,7 @@ add_subdirectory(supervisor)
add_subdirectory(health_check)
add_subdirectory(container_gc)
add_subdirectory(restart_manager)
+add_subdirectory(leftover_cleanup)
set(MANAGER_SRCS
${local_manager_srcs}
@@ -13,6 +14,7 @@ set(MANAGER_SRCS
${HEALTH_CHECK_SRCS}
${GC_SRCS}
${RESTART_MANAGER_SRCS}
+ ${LEFTOVER_CLEANUP_SRCS}
PARENT_SCOPE
)
@@ -23,5 +25,6 @@ set(MANAGER_INCS
${HEALTH_CHECK_INCS}
${GC_INCS}
${RESTART_MANAGER_INCS}
+ ${LEFTOVER_CLEANUP_INCS}
PARENT_SCOPE
)
diff --git a/src/daemon/modules/container/container_unix.c b/src/daemon/modules/container/container_unix.c
index 9910b3c8..88c4bf51 100644
--- a/src/daemon/modules/container/container_unix.c
+++ b/src/daemon/modules/container/container_unix.c
@@ -46,6 +46,7 @@
#include "utils_string.h"
#include "volume_api.h"
#include "namespace.h"
+#include "cleanup.h"
static int parse_container_log_configs(container_t *cont);
@@ -1278,6 +1279,7 @@ int container_module_init(char **msg)
}
containers_restore();
+ clean_leftover();
if (start_gchandler()) {
*msg = "Failed to start garbage collecotor handler";
diff --git a/src/daemon/modules/container/leftover_cleanup/CMakeLists.txt b/src/daemon/modules/container/leftover_cleanup/CMakeLists.txt
new file mode 100644
index 00000000..225ac38f
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/CMakeLists.txt
@@ -0,0 +1,19 @@
+# get current directory sources files
+aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} local_leftover_cleanup_srcs)
+
+set(LEFTOVER_CLEANUP_SRCS
+ ${local_leftover_cleanup_srcs}
+ PARENT_SCOPE
+ )
+
+set(LEFTOVER_CLEANUP_INCS
+ ${CMAKE_CURRENT_SOURCE_DIR}
+ PARENT_SCOPE
+ )
+
+if (DISABLE_OCI)
+ list(REMOVE_ITEM
+ local_leftover_cleanup_srcs
+ ${CMAKE_CURRENT_SOURCE_DIR}/oci_rootfs_clean.c
+ )
+endif()
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.c b/src/daemon/modules/container/leftover_cleanup/cleanup.c
new file mode 100644
index 00000000..29fa4bfa
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.c
@@ -0,0 +1,135 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide cleanup functions
+ *********************************************************************************/
+#include "utils.h"
+#include "cleanup.h"
+#include "oci_rootfs_clean.h"
+
+static struct cleaners *create_cleaners()
+{
+ struct cleaners *ret = NULL;
+
+ ret = util_common_calloc_s(sizeof(struct cleaners));
+ if (ret == NULL) {
+ ERROR("Out of memory");
+ return NULL;
+ }
+
+ linked_list_init(&(ret->cleaner_list));
+
+ return ret;
+}
+
+static void destroy_cleaners(struct cleaners *clns)
+{
+ struct linked_list *it = NULL;
+ struct linked_list *next = NULL;
+ struct clean_node *c_node = NULL;
+
+ linked_list_for_each_safe(it, &(clns->cleaner_list), next) {
+ c_node = (struct clean_node *)it->elem;
+ linked_list_del(it);
+ free(c_node);
+ free(it);
+ it = NULL;
+ }
+
+ free(clns);
+}
+
+static int add_clean_node(struct cleaners * clns, clean_func_t f, char * desc)
+{
+ struct linked_list *new_node = NULL;
+ struct clean_node *c_node = NULL;
+
+ new_node = util_common_calloc_s(sizeof(struct linked_list));
+ if (new_node == NULL) {
+ ERROR("Out of memory");
+ return -1;
+ }
+
+ c_node = util_common_calloc_s(sizeof(struct clean_node));
+ if (c_node == NULL) {
+ ERROR("Out of memory");
+ free(new_node);
+ return -1;
+ }
+ c_node->cleaner = f;
+ c_node->desc = desc;
+
+ linked_list_add_elem(new_node, c_node);
+ linked_list_add_tail(&(clns->cleaner_list), new_node);
+ clns->count++;
+
+ return 0;
+}
+
+static int default_cleaner()
+{
+ return 0;
+}
+
+static struct cleaners *cleaner_init()
+{
+ struct cleaners *clns = create_cleaners();
+
+ if (clns == NULL) {
+ return NULL;
+ }
+
+ add_clean_node(clns, default_cleaner, "default clean");
+#ifdef ENABLE_OCI_IMAGE
+ add_clean_node(clns, oci_rootfs_cleaner, "clean rootfs");
+#endif
+
+ return clns;
+}
+
+static void do_clean(struct cleaners * clns)
+{
+ struct linked_list *it = NULL;
+ struct linked_list *next = NULL;
+ struct clean_node *c_node = NULL;
+ int ret = 0;
+
+ linked_list_for_each_safe(it, &(clns->cleaner_list), next) {
+ c_node = (struct clean_node *)it->elem;
+ if ((ret = c_node->cleaner()) != 0) {
+ ERROR("failed to clean for: %s", c_node->desc);
+ } else {
+ DEBUG("do clean success for: %s", c_node->desc);
+ clns->done_clean++;
+ }
+ }
+}
+
+void clean_leftover()
+{
+ struct cleaners *clns = cleaner_init();
+
+ if (clns == NULL) {
+ ERROR("failed to clean leftovers, because cleaner init error");
+ return;
+ }
+
+ do_clean(clns);
+
+ if (clns->count == clns->done_clean) {
+ DEBUG("all clean up success");
+ } else {
+ ERROR("Aim to do %d clean, %d clean sucess\n", clns->count, clns->done_clean);
+ }
+
+ destroy_cleaners(clns);
+}
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.h b/src/daemon/modules/container/leftover_cleanup/cleanup.h
new file mode 100644
index 00000000..26fc1b0b
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.h
@@ -0,0 +1,49 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide cleanup definition
+ *********************************************************************************/
+#ifndef DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_H
+#define DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_H
+
+#include <stdlib.h>
+
+#include "linked_list.h"
+#include "isula_libutils/log.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+typedef int clean_func_t(void);
+
+struct clean_node {
+ char *desc;
+ clean_func_t *cleaner;
+ int error_code;
+};
+
+struct cleaners {
+ int count;
+ int done_clean;
+ struct linked_list cleaner_list;
+};
+
+
+void clean_leftover();
+
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
new file mode 100644
index 00000000..db56870b
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
@@ -0,0 +1,71 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide rootfs cleaner functions
+ *********************************************************************************/
+#include <sys/stat.h>
+#include <string.h>
+#include "oci_rootfs_clean.h"
+#include "container_api.h"
+#include "image_api.h"
+#include "utils_file.h"
+#include "utils.h"
+
+struct cb_result {
+ int clean_err_cnt;
+};
+
+static bool walk_dir_cb(const char *path_name, const struct dirent *sub_dir, void *context)
+{
+ struct cb_result *result = (struct cb_result *)context;
+ container_t *cont = containers_store_get(sub_dir->d_name);
+ int rm_rootfs_ret = 0;
+
+ if (cont != NULL) {
+ return true;
+ }
+
+ INFO("cleaning leftover dir: %s", sub_dir->d_name);
+ rm_rootfs_ret = im_remove_container_rootfs(IMAGE_TYPE_OCI, sub_dir->d_name);
+ if (rm_rootfs_ret != 0) {
+ result->clean_err_cnt++;
+ }
+
+ return true;
+}
+
+
+int oci_rootfs_cleaner(void)
+{
+ struct cb_result res = { 0 };
+ im_get_rf_dir_request request = { 0 };
+ char *rf_dir = NULL;
+ int ret = 0;
+
+ request.type = IMAGE_TYPE_OCI;
+ rf_dir = im_get_rootfs_dir(&request);
+ if (rf_dir == NULL) {
+ return 0;
+ }
+
+ ret = util_scan_subdirs(rf_dir, walk_dir_cb, &res);
+ if (ret != 0) {
+ ERROR("failed to scan subdirs");
+ return -1;
+ }
+
+ if (res.clean_err_cnt == 0) {
+ return 0;
+ }
+
+ return -1;
+}
diff --git a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
new file mode 100644
index 00000000..8dff351f
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
@@ -0,0 +1,30 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide rootfs cleaner definition
+ *********************************************************************************/
+#ifndef DAEMON_MODULES_CONTAINER_ROOTFS_CLEAN_H
+#define DAEMON_MODULES_CONTAINER_ROOTFS_CLEAN_H
+
+#include "cleanup.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+int oci_rootfs_cleaner(void);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif
\ No newline at end of file
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index f487f831..3c395c1f 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -49,6 +49,7 @@ struct bim_ops {
int (*delete_rf)(const im_delete_rootfs_request *request);
int (*export_rf)(const im_export_request *request);
char *(*resolve_image_name)(const char *image_name);
+ char *(*get_dir_rf)(void);
/* merge image config ops */
int (*merge_conf)(const char *img_name, container_config *container_spec);
@@ -132,6 +133,7 @@ static const struct bim_ops g_embedded_ops = {
.umount_rf = embedded_umount_rf,
.delete_rf = embedded_delete_rf,
.export_rf = NULL,
+ .get_dir_rf = NULL,
.merge_conf = embedded_merge_conf,
.get_user_conf = embedded_get_user_conf,
@@ -166,6 +168,7 @@ static const struct bim_ops g_oci_ops = {
.umount_rf = oci_umount_rf,
.delete_rf = oci_delete_rf,
.export_rf = oci_export_rf,
+ .get_dir_rf = oci_get_dir_rf,
.merge_conf = oci_merge_conf_rf,
.get_user_conf = oci_get_user_conf,
@@ -199,6 +202,7 @@ static const struct bim_ops g_ext_ops = {
.umount_rf = ext_umount_rf,
.delete_rf = ext_delete_rf,
.export_rf = NULL,
+ .get_dir_rf = NULL,
.merge_conf = ext_merge_conf,
.get_user_conf = ext_get_user_conf,
@@ -1768,6 +1772,28 @@ int im_container_export(const im_export_request *request)
}
#endif
+#ifdef ENABLE_OCI_IMAGE
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+ if (request->type == NULL) {
+ ERROR("Missing image type");
+ return NULL;
+ }
+
+ struct bim *bim = NULL;
+ bim = bim_get(request->type, NULL, NULL, NULL);
+ if (bim->ops->get_dir_rf == NULL) {
+ ERROR("Unimplemnts get rootfs dir in %s", bim->type);
+ return NULL;
+ }
+
+ return bim->ops->get_dir_rf();
+}
+#else
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+ return NULL;
+}
+#endif
+
void free_im_export_request(im_export_request *ptr)
{
if (ptr == NULL) {
diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c
index 86828f50..e951adb4 100644
--- a/src/daemon/modules/image/oci/oci_image.c
+++ b/src/daemon/modules/image/oci/oci_image.c
@@ -686,6 +686,11 @@ int oci_export_rf(const im_export_request *request)
return ret;
}
+char *oci_get_dir_rf(void)
+{
+ return storage_rootfs_get_dir();
+}
+
int oci_login(const im_login_request *request)
{
int ret = 0;
diff --git a/src/daemon/modules/image/oci/oci_image.h b/src/daemon/modules/image/oci/oci_image.h
index 64a4d8e8..aeeb3b65 100644
--- a/src/daemon/modules/image/oci/oci_image.h
+++ b/src/daemon/modules/image/oci/oci_image.h
@@ -54,6 +54,7 @@ int oci_mount_rf(const im_mount_request *request);
int oci_umount_rf(const im_umount_request *request);
int oci_delete_rf(const im_delete_rootfs_request *request);
int oci_export_rf(const im_export_request *request);
+char *oci_get_dir_rf(void);
int oci_container_filesystem_usage(const im_container_fs_usage_request *request, imagetool_fs_info **fs_usage);
int oci_login(const im_login_request *request);
int oci_logout(const im_logout_request *request);
diff --git a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
index 378d1a96..0270f6a7 100644
--- a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
+++ b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
@@ -1319,3 +1319,8 @@ out:
rootfs_store_unlock();
return ret;
}
+
+char *rootfs_store_get_data_dir()
+{
+ return g_rootfs_store->dir;
+}
\ No newline at end of file
diff --git a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.h b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.h
index e13f97bc..c23af091 100644
--- a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.h
+++ b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.h
@@ -65,6 +65,9 @@ storage_rootfs *rootfs_store_get_rootfs(const char *id);
// Return a slice enumerating the known containers.
int rootfs_store_get_all_rootfs(struct rootfs_list *all_rootfs);
+// Return rootfs store data dir
+char *rootfs_store_get_data_dir();
+
// Free memory of container store, but will not delete the persisted files
void rootfs_store_free();
diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c
index 57b5fb80..6cb4a51b 100644
--- a/src/daemon/modules/image/oci/storage/storage.c
+++ b/src/daemon/modules/image/oci/storage/storage.c
@@ -1868,3 +1868,8 @@ int storage_module_init(struct storage_module_init_options *opts)
out:
return ret;
}
+
+char *storage_rootfs_get_dir()
+{
+ return rootfs_store_get_data_dir();
+}
\ No newline at end of file
diff --git a/src/daemon/modules/image/oci/storage/storage.h b/src/daemon/modules/image/oci/storage/storage.h
index 1fe29b45..5914adec 100644
--- a/src/daemon/modules/image/oci/storage/storage.h
+++ b/src/daemon/modules/image/oci/storage/storage.h
@@ -181,6 +181,8 @@ char *storage_rootfs_mount(const char *container_id);
int storage_rootfs_umount(const char *container_id, bool force);
+char *storage_rootfs_get_dir();
+
container_inspect_graph_driver *storage_get_metadata_by_container_id(const char *id);
#ifdef __cplusplus
--
2.25.1

View File

@ -1,154 +0,0 @@
From 3942fb5667077200017a1a7c72672e482e798df6 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 1 Nov 2022 14:36:02 +0800
Subject: [PATCH 40/43] bugfix for websocket receive data too long
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../entry/cri/websocket/service/ws_server.cc | 65 ++++++++++++++++---
.../entry/cri/websocket/service/ws_server.h | 5 +-
2 files changed, 59 insertions(+), 11 deletions(-)
diff --git a/src/daemon/entry/cri/websocket/service/ws_server.cc b/src/daemon/entry/cri/websocket/service/ws_server.cc
index 41bb3fe8..ea320ff4 100644
--- a/src/daemon/entry/cri/websocket/service/ws_server.cc
+++ b/src/daemon/entry/cri/websocket/service/ws_server.cc
@@ -126,6 +126,32 @@ void SessionData::CloseSession()
sessionMutex->unlock();
}
+bool SessionData::IsStdinComplete()
+{
+ bool c = true;
+
+ if (sessionMutex == nullptr) {
+ return true;
+ }
+
+ sessionMutex->lock();
+ c = completeStdin;
+ sessionMutex->unlock();
+
+ return c;
+}
+
+void SessionData::SetStdinComplete(bool complete)
+{
+ if (sessionMutex == nullptr) {
+ return;
+ }
+
+ sessionMutex->lock();
+ completeStdin = complete;
+ sessionMutex->unlock();
+}
+
void SessionData::EraseAllMessage()
{
if (sessionMutex == nullptr) {
@@ -330,6 +356,7 @@ int WebsocketServer::GenerateSessionData(SessionData *session, const std::string
session->sessionMutex = bufMutex;
session->syncCloseSem = syncCloseSem;
session->close = false;
+ session->completeStdin = true;
session->containerID = containerID;
session->suffix = std::string(suffix);
@@ -524,28 +551,44 @@ int WebsocketServer::ResizeTerminal(int socketID, const char *jsonData, size_t l
return ret;
}
-void WebsocketServer::Receive(int socketID, void *in, size_t len)
+void WebsocketServer::Receive(int socketID, void *in, size_t len, bool complete)
{
auto it = m_wsis.find(socketID);
if (it == m_wsis.end()) {
- ERROR("invailed websocket session!");
+ ERROR("Invailed websocket session!");
return;
}
+ if (!it->second->IsStdinComplete()) {
+ DEBUG("Receive remaning stdin data with length %zu", len);
+ // Too much data may cause error 'resource temporarily unavaliable' by using 'write'
+ if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), (char *)in, len) < 0) {
+ ERROR("Sub write over! err msg: %s", strerror(errno));
+ }
+ goto out;
+ }
+
if (*static_cast<char *>(in) == WebsocketChannel::RESIZECHANNEL) {
if (ResizeTerminal(socketID, (char *)in + 1, len, it->second->containerID, it->second->suffix) != 0) {
ERROR("Failed to resize terminal tty");
- return;
}
- } else if (*static_cast<char *>(in) == WebsocketChannel::STDINCHANNEL) {
- if (write(m_wsis[socketID]->pipes.at(1), (void *)((char *)in + 1), len - 1) < 0) {
- ERROR("sub write over!");
- return;
+ if (!complete) {
+ ERROR("Resize data too long");
}
- } else {
- ERROR("invalid data: %s", (char *)in);
return;
}
+
+ if (*static_cast<char *>(in) == WebsocketChannel::STDINCHANNEL) {
+ if (util_write_nointr_in_total(m_wsis[socketID]->pipes.at(1), (char *)in + 1, len - 1) < 0) {
+ ERROR("Sub write over! err msg: %s", strerror(errno));
+ }
+ goto out;
+ }
+
+ ERROR("Invalid data: %s", (char *)in);
+
+out:
+ it->second->SetStdinComplete(complete);
}
int WebsocketServer::Callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len)
@@ -605,7 +648,9 @@ int WebsocketServer::Callback(struct lws *wsi, enum lws_callback_reasons reason,
break;
case LWS_CALLBACK_RECEIVE: {
ReadGuard<RWMutex> lock(m_mutex);
- WebsocketServer::GetInstance()->Receive(lws_get_socket_fd(wsi), static_cast<char *>(in), len);
+ size_t bytesLen = lws_remaining_packet_payload(wsi);
+ WebsocketServer::GetInstance()->Receive(lws_get_socket_fd(wsi), static_cast<char *>(in),
+ len, bytesLen == 0);
}
break;
case LWS_CALLBACK_CLOSED: {
diff --git a/src/daemon/entry/cri/websocket/service/ws_server.h b/src/daemon/entry/cri/websocket/service/ws_server.h
index a2a180ec..7da56818 100644
--- a/src/daemon/entry/cri/websocket/service/ws_server.h
+++ b/src/daemon/entry/cri/websocket/service/ws_server.h
@@ -44,6 +44,7 @@ struct SessionData {
std::list<unsigned char *> buffer;
std::string containerID;
std::string suffix;
+ volatile bool completeStdin;
unsigned char *FrontMessage();
void PopMessage();
@@ -51,6 +52,8 @@ struct SessionData {
bool IsClosed();
void CloseSession();
void EraseAllMessage();
+ bool IsStdinComplete();
+ void SetStdinComplete(bool complete);
};
class WebsocketServer {
@@ -72,7 +75,7 @@ private:
std::vector<std::string> split(std::string str, char r);
int CreateContext();
- inline void Receive(int socketID, void *in, size_t len);
+ inline void Receive(int socketID, void *in, size_t len, bool complete);
int Wswrite(struct lws *wsi, const unsigned char *message);
inline void DumpHandshakeInfo(struct lws *wsi) noexcept;
int RegisterStreamTask(struct lws *wsi) noexcept;
--
2.25.1

View File

@ -1,38 +0,0 @@
From 53372ea2cf848e0352bdd4fba4a664b4955cdd6e Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Tue, 1 Nov 2022 18:50:48 -0700
Subject: [PATCH 41/43] fix call bim_put in im_get_rootfs_dir
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
src/daemon/modules/image/image.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index 3c395c1f..ed7d968a 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -1774,6 +1774,8 @@ int im_container_export(const im_export_request *request)
#ifdef ENABLE_OCI_IMAGE
char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+ char *dir = NULL;
+
if (request->type == NULL) {
ERROR("Missing image type");
return NULL;
@@ -1785,8 +1787,10 @@ char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
ERROR("Unimplemnts get rootfs dir in %s", bim->type);
return NULL;
}
+ dir = bim->ops->get_dir_rf();
+ bim_put(bim);
- return bim->ops->get_dir_rf();
+ return dir;
}
#else
char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
--
2.25.1

View File

@ -1,152 +0,0 @@
From c47dc7d4a038dd9b2e2ec3bb27938e03502724d9 Mon Sep 17 00:00:00 2001
From: songbuhuang <544824346@qq.com>
Date: Tue, 18 Oct 2022 21:25:02 +0800
Subject: [PATCH 42/43] isula usage consistency optimization
Signed-off-by: songbuhuang <544824346@qq.com>
---
src/cmd/isula/extend/events.c | 2 +-
src/cmd/isula/extend/export.c | 2 +-
src/cmd/isula/images/images.c | 2 +-
src/cmd/isula/images/logout.c | 2 +-
src/cmd/isula/images/pull.c | 2 +-
src/cmd/isula/information/inspect.c | 2 +-
src/cmd/isula/information/ps.c | 2 +-
src/cmd/isula/information/version.c | 2 +-
src/cmd/isula/stream/cp.c | 2 +-
9 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/cmd/isula/extend/events.c b/src/cmd/isula/extend/events.c
index 7094ac0d..b35f246a 100644
--- a/src/cmd/isula/extend/events.c
+++ b/src/cmd/isula/extend/events.c
@@ -27,7 +27,7 @@
#include "utils_timestamp.h"
const char g_cmd_events_desc[] = "Get real time events from the server";
-const char g_cmd_events_usage[] = "events [command options]";
+const char g_cmd_events_usage[] = "events [OPTIONS]";
struct client_arguments g_cmd_events_args = {
.since = NULL,
diff --git a/src/cmd/isula/extend/export.c b/src/cmd/isula/extend/export.c
index ea9b9c11..68d17c82 100644
--- a/src/cmd/isula/extend/export.c
+++ b/src/cmd/isula/extend/export.c
@@ -28,7 +28,7 @@
#include "connect.h"
const char g_cmd_export_desc[] = "export container";
-const char g_cmd_export_usage[] = "export [command options] [ID|NAME]";
+const char g_cmd_export_usage[] = "export [OPTIONS] [ID|NAME]";
struct client_arguments g_cmd_export_args = {};
diff --git a/src/cmd/isula/images/images.c b/src/cmd/isula/images/images.c
index 3d538aa5..e4b28f5a 100644
--- a/src/cmd/isula/images/images.c
+++ b/src/cmd/isula/images/images.c
@@ -44,7 +44,7 @@
#define SHORT_DIGEST_LEN 12
const char g_cmd_images_desc[] = "List images";
-const char g_cmd_images_usage[] = "images";
+const char g_cmd_images_usage[] = "images [OPTIONS]";
struct client_arguments g_cmd_images_args = {};
/* keep track of field widths for printing. */
diff --git a/src/cmd/isula/images/logout.c b/src/cmd/isula/images/logout.c
index a5b99073..18c9b061 100644
--- a/src/cmd/isula/images/logout.c
+++ b/src/cmd/isula/images/logout.c
@@ -64,7 +64,6 @@ int client_logout(const struct client_arguments *args)
ret = ESERVERERROR;
goto out;
}
-
COMMAND_ERROR("Logout Succeeded");
out:
@@ -79,6 +78,7 @@ int cmd_logout_main(int argc, const char **argv)
int exit_code = 1; /* exit 1 if failed to logout */
command_t cmd;
struct command_option options[] = {
+ LOG_OPTIONS(lconf)
COMMON_OPTIONS(g_cmd_logout_args)
};
diff --git a/src/cmd/isula/images/pull.c b/src/cmd/isula/images/pull.c
index fbe7458b..548e8d90 100644
--- a/src/cmd/isula/images/pull.c
+++ b/src/cmd/isula/images/pull.c
@@ -63,7 +63,6 @@ int client_pull(const struct client_arguments *args)
ret = ESERVERERROR;
goto out;
}
-
COMMAND_ERROR("Image \"%s\" pulled", response->image_ref);
out:
@@ -78,6 +77,7 @@ int cmd_pull_main(int argc, const char **argv)
int exit_code = 1; /* exit 1 if failed to pull */
command_t cmd;
struct command_option options[] = {
+ LOG_OPTIONS(lconf)
COMMON_OPTIONS(g_cmd_pull_args)
};
diff --git a/src/cmd/isula/information/inspect.c b/src/cmd/isula/information/inspect.c
index bb52b42a..b86f931f 100644
--- a/src/cmd/isula/information/inspect.c
+++ b/src/cmd/isula/information/inspect.c
@@ -27,7 +27,7 @@
#include "connect.h"
const char g_cmd_inspect_desc[] = "Return low-level information on a container or image";
-const char g_cmd_inspect_usage[] = "inspect [options] CONTAINER|IMAGE [CONTAINER|IMAGE...]";
+const char g_cmd_inspect_usage[] = "inspect [OPTIONS] CONTAINER|IMAGE [CONTAINER|IMAGE...]";
struct client_arguments g_cmd_inspect_args = {
.format = NULL,
diff --git a/src/cmd/isula/information/ps.c b/src/cmd/isula/information/ps.c
index 71c01acb..57d34aa3 100644
--- a/src/cmd/isula/information/ps.c
+++ b/src/cmd/isula/information/ps.c
@@ -32,7 +32,7 @@
#include "utils_timestamp.h"
const char g_cmd_list_desc[] = "List containers";
-const char g_cmd_list_usage[] = "ps [command options]";
+const char g_cmd_list_usage[] = "ps [OPTIONS]";
#define COMMAND_LENGTH_MAX 22
#define TIME_DURATION_MAX_LEN 32
diff --git a/src/cmd/isula/information/version.c b/src/cmd/isula/information/version.c
index 1e94f08a..037a8e1f 100644
--- a/src/cmd/isula/information/version.c
+++ b/src/cmd/isula/information/version.c
@@ -27,7 +27,7 @@
#include "constants.h"
const char g_cmd_version_desc[] = "Display information about isula";
-const char g_cmd_version_usage[] = "version";
+const char g_cmd_version_usage[] = "version [OPTIONS]";
struct client_arguments g_cmd_version_args = {};
diff --git a/src/cmd/isula/stream/cp.c b/src/cmd/isula/stream/cp.c
index 450a7990..f85602ed 100644
--- a/src/cmd/isula/stream/cp.c
+++ b/src/cmd/isula/stream/cp.c
@@ -41,7 +41,7 @@
const char g_cmd_cp_desc[] = "Copy files/folders between a container and the local filesystem";
const char g_cmd_cp_usage[] = "cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH\n"
- " cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH";
+ " cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH";
struct client_arguments g_cmd_cp_args = {};
--
2.25.1

View File

@ -1,74 +0,0 @@
From a04bf6abc54f6516d5181fbfa7e08cc1095bc72e Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Wed, 2 Nov 2022 00:01:43 -0700
Subject: [PATCH 43/43] fix do container_unref in oci_rootfs_clean
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
src/daemon/modules/container/leftover_cleanup/cleanup.c | 2 +-
src/daemon/modules/container/leftover_cleanup/cleanup.h | 2 +-
.../modules/container/leftover_cleanup/oci_rootfs_clean.c | 2 ++
.../modules/image/oci/storage/rootfs_store/rootfs_store.c | 2 +-
4 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.c b/src/daemon/modules/container/leftover_cleanup/cleanup.c
index 29fa4bfa..ec9517cf 100644
--- a/src/daemon/modules/container/leftover_cleanup/cleanup.c
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.c
@@ -48,7 +48,7 @@ static void destroy_cleaners(struct cleaners *clns)
free(clns);
}
-static int add_clean_node(struct cleaners * clns, clean_func_t f, char * desc)
+static int add_clean_node(struct cleaners *clns, clean_func_t f, const char *desc)
{
struct linked_list *new_node = NULL;
struct clean_node *c_node = NULL;
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.h b/src/daemon/modules/container/leftover_cleanup/cleanup.h
index 26fc1b0b..efae99d0 100644
--- a/src/daemon/modules/container/leftover_cleanup/cleanup.h
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.h
@@ -27,7 +27,7 @@ extern "C" {
typedef int clean_func_t(void);
struct clean_node {
- char *desc;
+ const char *desc;
clean_func_t *cleaner;
int error_code;
};
diff --git a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
index db56870b..fbef4ce0 100644
--- a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
+++ b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
@@ -31,6 +31,7 @@ static bool walk_dir_cb(const char *path_name, const struct dirent *sub_dir, voi
int rm_rootfs_ret = 0;
if (cont != NULL) {
+ container_unref(cont);
return true;
}
@@ -58,6 +59,7 @@ int oci_rootfs_cleaner(void)
}
ret = util_scan_subdirs(rf_dir, walk_dir_cb, &res);
+ free(rf_dir);
if (ret != 0) {
ERROR("failed to scan subdirs");
return -1;
diff --git a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
index 0270f6a7..97cc39e8 100644
--- a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
+++ b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
@@ -1322,5 +1322,5 @@ out:
char *rootfs_store_get_data_dir()
{
- return g_rootfs_store->dir;
+ return util_strdup_s(g_rootfs_store->dir);
}
\ No newline at end of file
--
2.25.1

View File

@ -1,32 +0,0 @@
From 1bf460aa845611fbf20136eaa147b6c75f9419f1 Mon Sep 17 00:00:00 2001
From: Mig Yang <418109103@qq.com>
Date: Wed, 16 Nov 2022 17:32:18 +0800
Subject: [PATCH 44/45] fix can not install isulad rpm because of spec
---
iSulad.spec | 2 ++
1 file changed, 2 insertions(+)
diff --git a/iSulad.spec b/iSulad.spec
index e9826a29..97c6bcb3 100644
--- a/iSulad.spec
+++ b/iSulad.spec
@@ -18,6 +18,7 @@ ExclusiveArch: x86_64 aarch64
Provides: libhttpclient.so()(64bit)
Provides: libisula.so()(64bit)
Provides: libisulad_img.so()(64bit)
+Provides: libisulad_tools.so()(64bit)
%endif
%if 0%{?is_systemd}
@@ -79,6 +80,7 @@ cd build
install -d $RPM_BUILD_ROOT/%{_libdir}
install -m 0644 ./src/libisula.so %{buildroot}/%{_libdir}/libisula.so
install -m 0644 ./src/utils/http/libhttpclient.so %{buildroot}/%{_libdir}/libhttpclient.so
+install -m 0644 ./src/libisulad_tools.so %{buildroot}/%{_libdir}/libisulad_tools.so
install -m 0644 ./src/daemon/modules/image/libisulad_img.so %{buildroot}/%{_libdir}/libisulad_img.so
chmod +x %{buildroot}/%{_libdir}/libisula.so
chmod +x %{buildroot}/%{_libdir}/libhttpclient.so
--
2.25.1

View File

@ -1,25 +0,0 @@
From a81cc3350766a352ab29c2d884b5542c5207f93f Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Mon, 21 Nov 2022 16:16:41 +0800
Subject: [PATCH 45/45] remove unknown option wno-maybe-uninitialized
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
test/CMakeLists.txt | 2 --
1 file changed, 2 deletions(-)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 92a4e969..6b6cd5de 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,7 +1,5 @@
project(iSulad_UT)
-set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-maybe-uninitialized")
-
function(gmock_find_library _name)
find_library(${_name}
NAMES ${ARGN}
--
2.25.1

View File

@ -1,267 +0,0 @@
From 6cf9f48c2339f85fa233c4e557da08884f666704 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 22 Nov 2022 16:52:51 +0800
Subject: [PATCH 46/54] fix storage layer and driver ut failed in container
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../oci/storage/layers/storage_driver_ut.cc | 61 ++++++++++++++++---
.../oci/storage/layers/storage_layers_ut.cc | 54 +++++++++++++++-
2 files changed, 106 insertions(+), 9 deletions(-)
diff --git a/test/image/oci/storage/layers/storage_driver_ut.cc b/test/image/oci/storage/layers/storage_driver_ut.cc
index 735526f1..650368d8 100644
--- a/test/image/oci/storage/layers/storage_driver_ut.cc
+++ b/test/image/oci/storage/layers/storage_driver_ut.cc
@@ -58,6 +58,28 @@ std::string GetDirectory()
return static_cast<std::string>(abs_path) + "../../../../../../test/image/oci/storage/layers";
}
+bool check_support_overlay(std::string root_dir)
+{
+ if (!util_support_overlay()) {
+ std::cout << "Cannot support overlay, skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ char *backing_fs = util_get_fs_name(root_dir.c_str());
+ if (backing_fs == NULL) {
+ std::cout << "Failed to get fs name for " << root_dir << ", skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ if (strcmp(backing_fs, "aufs") == 0 || strcmp(backing_fs, "zfs") == 0 || strcmp(backing_fs, "overlayfs") == 0 ||
+ strcmp(backing_fs, "ecryptfs") == 0) {
+ std::cout << "Backing fs cannot support overlay, skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ return true;
+}
+
bool dirExists(const char *path)
{
DIR *dp = nullptr;
@@ -99,11 +121,16 @@ protected:
void SetUp() override
{
MockDriverQuota_SetMock(&m_driver_quota_mock);
- std::string isulad_dir { "/var/lib/isulad/" };
+ std::string isulad_dir { "/tmp/isulad/" };
+ mkdir(isulad_dir.c_str(), 0755);
std::string root_dir = isulad_dir + "data";
std::string run_dir = isulad_dir + "data/run";
std::string data_dir = GetDirectory() + "/data";
- struct storage_module_init_options *opts;
+
+ support_overlay = check_support_overlay(root_dir);
+ if (!support_overlay) {
+ return;
+ }
ASSERT_STRNE(util_clean_path(data_dir.c_str(), data_path, sizeof(data_path)), nullptr);
std::string cp_command = "cp -r " + std::string(data_path) + " " + isulad_dir;
@@ -117,15 +144,16 @@ protected:
+ root_dir + "/overlay/9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63/work ";
ASSERT_EQ(system(mkdir.c_str()), 0);
- opts = (struct storage_module_init_options *)util_common_calloc_s(sizeof(struct storage_module_init_options));
+ struct storage_module_init_options *opts = (struct storage_module_init_options *)util_common_calloc_s(sizeof(struct storage_module_init_options));
opts->storage_root = strdup(root_dir.c_str());
opts->storage_run_root = strdup(run_dir.c_str());
opts->driver_name = strdup("overlay");
- opts->driver_opts = (char **)util_common_calloc_s(4 * sizeof(char *));
+ opts->driver_opts = (char **)util_common_calloc_s(5 * sizeof(char *));
opts->driver_opts[0] = strdup("overlay2.basesize=128M");
opts->driver_opts[1] = strdup("overlay2.override_kernel_check=true");
opts->driver_opts[2] = strdup("overlay2.skip_mount_home=false");
opts->driver_opts[3] = strdup("overlay2.mountopt=rw");
+ opts->driver_opts[4] = strdup("overlay2.skip_mount_home=true");
opts->driver_opts_len = 4;
EXPECT_CALL(m_driver_quota_mock, QuotaCtl(_, _, _, _)).WillRepeatedly(Invoke(invokeQuotaCtl));
@@ -141,18 +169,25 @@ protected:
void TearDown() override
{
MockDriverQuota_SetMock(nullptr);
- ASSERT_EQ(graphdriver_cleanup(), 0);
- std::string rm_command = "rm -rf /var/lib/isulad/data";
+ if (support_overlay) {
+ ASSERT_EQ(graphdriver_cleanup(), 0);
+ }
+ std::string rm_command = "rm -rf /tmp/isulad/";
ASSERT_EQ(system(rm_command.c_str()), 0);
}
NiceMock<MockDriverQuota> m_driver_quota_mock;
char data_path[PATH_MAX] = { 0x00 };
+ bool support_overlay;
};
TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
std::string incorrectId { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" };
ASSERT_TRUE(graphdriver_layer_exists(id.c_str()));
@@ -161,6 +196,10 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_layer_exists)
TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string id { "eb29745b8228e1e97c01b1d5c2554a319c00a94d8dd5746a3904222ad65a13f8" };
struct driver_create_opts *create_opts;
@@ -186,8 +225,12 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_create_rw)
TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
- std::string merged_dir = "/var/lib/isulad/data/overlay/" + id + "/merged";
+ std::string merged_dir = "/tmp/isulad/data/overlay/" + id + "/merged";
struct driver_mount_opts *mount_opts = nullptr;
char* mount_dir = nullptr;
@@ -219,6 +262,10 @@ TEST_F(StorageDriverUnitTest, test_graphdriver_mount_layer)
TEST_F(StorageDriverUnitTest, test_graphdriver_try_repair_lowers)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string id { "1be74353c3d0fd55fb5638a52953e6f1bc441e5b1710921db9ec2aa202725569" };
ASSERT_EQ(graphdriver_try_repair_lowers(id.c_str(), nullptr), 0);
}
diff --git a/test/image/oci/storage/layers/storage_layers_ut.cc b/test/image/oci/storage/layers/storage_layers_ut.cc
index 87dfb4a1..fca37e83 100644
--- a/test/image/oci/storage/layers/storage_layers_ut.cc
+++ b/test/image/oci/storage/layers/storage_layers_ut.cc
@@ -59,6 +59,28 @@ std::string GetDirectory()
return static_cast<std::string>(abs_path) + "../../../../../../test/image/oci/storage/layers";
}
+bool check_support_overlay(std::string root_dir)
+{
+ if (!util_support_overlay()) {
+ std::cout << "Cannot support overlay, skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ char *backing_fs = util_get_fs_name(root_dir.c_str());
+ if (backing_fs == NULL) {
+ std::cout << "Failed to get fs name for " << root_dir << ", skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ if (strcmp(backing_fs, "aufs") == 0 || strcmp(backing_fs, "zfs") == 0 || strcmp(backing_fs, "overlayfs") == 0 ||
+ strcmp(backing_fs, "ecryptfs") == 0) {
+ std::cout << "Backing fs cannot support overlay, skip storage driver ut test." << std::endl;
+ return false;
+ }
+
+ return true;
+}
+
bool dirExists(const char *path)
{
DIR *dp = nullptr;
@@ -159,6 +181,11 @@ protected:
std::string run_dir = isulad_dir + "data/run";
std::string data_dir = GetDirectory() + "/data";
+ support_overlay = check_support_overlay(root_dir);
+ if (!support_overlay) {
+ return;
+ }
+
ASSERT_STRNE(util_clean_path(data_dir.c_str(), data_path, sizeof(data_path)), nullptr);
std::string cp_command = "cp -r " + std::string(data_path) + " " + isulad_dir;
ASSERT_EQ(system(cp_command.c_str()), 0);
@@ -186,8 +213,10 @@ protected:
{
MockDriverQuota_SetMock(nullptr);
- layer_store_exit();
- layer_store_cleanup();
+ if (support_overlay) {
+ layer_store_exit();
+ layer_store_cleanup();
+ }
std::string rm_command = "rm -rf /tmp/isulad/";
ASSERT_EQ(system(rm_command.c_str()), 0);
@@ -197,10 +226,15 @@ protected:
char real_path[PATH_MAX] = { 0x00 };
char real_run_path[PATH_MAX] = { 0x00 };
char data_path[PATH_MAX] = { 0x00 };
+ bool support_overlay;
};
TEST_F(StorageLayersUnitTest, test_layers_load)
{
+ if (!support_overlay) {
+ return;
+ }
+
struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
ASSERT_NE(layer_list, nullptr);
@@ -246,6 +280,10 @@ TEST_F(StorageLayersUnitTest, test_layers_load)
TEST_F(StorageLayersUnitTest, test_layer_store_exists)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string id { "7db8f44a0a8e12ea4283e3180e98880007efbd5de2e7c98b67de9cdd4dfffb0b" };
std::string incorrectId { "50551ff67da98ab8540d7132" };
@@ -255,6 +293,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_exists)
TEST_F(StorageLayersUnitTest, test_layer_store_create)
{
+ if (!support_overlay) {
+ return;
+ }
+
struct layer_opts *layer_opt = (struct layer_opts *)util_common_calloc_s(sizeof(struct layer_opts));
layer_opt->parent = strdup("9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63");
layer_opt->writable = true;
@@ -278,6 +320,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_create)
TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string compress { "sha256:0e03bdcc26d7a9a57ef3b6f1bf1a210cff6239bff7c8cac72435984032851689" };
std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
@@ -294,6 +340,10 @@ TEST_F(StorageLayersUnitTest, test_layer_store_by_compress_digest)
TEST_F(StorageLayersUnitTest, test_layer_store_by_uncompress_digest)
{
+ if (!support_overlay) {
+ return;
+ }
+
std::string uncompress { "sha256:9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
std::string id { "9c27e219663c25e0f28493790cc0b88bc973ba3b1686355f221c38a36978ac63" };
struct layer_list *layer_list = (struct layer_list *)util_common_calloc_s(sizeof(struct layer_list));
--
2.25.1

View File

@ -1,96 +0,0 @@
From 9c056dc6d696d3eabd192ad6b396e27bb5846362 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Thu, 17 Nov 2022 19:25:26 -0800
Subject: [PATCH 47/54] handle security warning for cleanup module
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
.../container/leftover_cleanup/cleanup.c | 17 +++++++++++++----
src/daemon/modules/image/image.c | 14 +++++++++++---
2 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.c b/src/daemon/modules/container/leftover_cleanup/cleanup.c
index ec9517cf..9ce1dd0c 100644
--- a/src/daemon/modules/container/leftover_cleanup/cleanup.c
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.c
@@ -82,15 +82,25 @@ static int default_cleaner()
static struct cleaners *cleaner_init()
{
+ int ret = 0;
struct cleaners *clns = create_cleaners();
if (clns == NULL) {
return NULL;
}
- add_clean_node(clns, default_cleaner, "default clean");
+ ret = add_clean_node(clns, default_cleaner, "default clean");
+ if (ret != 0) {
+ ERROR("add default_cleaner error");
+ return clns;
+ }
+
#ifdef ENABLE_OCI_IMAGE
- add_clean_node(clns, oci_rootfs_cleaner, "clean rootfs");
+ ret = add_clean_node(clns, oci_rootfs_cleaner, "clean rootfs");
+ if (ret != 0) {
+ ERROR("add oci_rootfs_cleaner error");
+ return clns;
+ }
#endif
return clns;
@@ -101,11 +111,10 @@ static void do_clean(struct cleaners * clns)
struct linked_list *it = NULL;
struct linked_list *next = NULL;
struct clean_node *c_node = NULL;
- int ret = 0;
linked_list_for_each_safe(it, &(clns->cleaner_list), next) {
c_node = (struct clean_node *)it->elem;
- if ((ret = c_node->cleaner()) != 0) {
+ if (c_node->cleaner() != 0) {
ERROR("failed to clean for: %s", c_node->desc);
} else {
DEBUG("do clean success for: %s", c_node->desc);
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index ed7d968a..fb0db361 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -1775,21 +1775,29 @@ int im_container_export(const im_export_request *request)
#ifdef ENABLE_OCI_IMAGE
char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
char *dir = NULL;
+ struct bim *bim = NULL;
if (request->type == NULL) {
ERROR("Missing image type");
return NULL;
}
- struct bim *bim = NULL;
bim = bim_get(request->type, NULL, NULL, NULL);
+
+ if (bim == NULL) {
+ ERROR("Failed to init bim, image type:%s", request->type);
+ return NULL;
+ }
+
if (bim->ops->get_dir_rf == NULL) {
ERROR("Unimplemnts get rootfs dir in %s", bim->type);
- return NULL;
+ goto out;
}
+
dir = bim->ops->get_dir_rf();
- bim_put(bim);
+out:
+ bim_put(bim);
return dir;
}
#else
--
2.25.1

View File

@ -1,222 +0,0 @@
From 4029481a18ba302e4842b40f479dac63381570f3 Mon Sep 17 00:00:00 2001
From: chengzrz <czrzrichard@gmail.com>
Date: Fri, 25 Nov 2022 10:13:43 +0800
Subject: [PATCH 48/54] add unit test for util/sha256
Signed-off-by: chengzrz <czrzrichard@gmail.com>
---
test/CMakeLists.txt | 1 +
test/sha256/CMakeLists.txt | 18 +++++
test/sha256/sha256_ut.cc | 162 +++++++++++++++++++++++++++++++++++++
3 files changed, 181 insertions(+)
create mode 100644 test/sha256/CMakeLists.txt
create mode 100644 test/sha256/sha256_ut.cc
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 6b6cd5de..27201100 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -44,6 +44,7 @@ IF(ENABLE_UT)
add_subdirectory(runtime)
add_subdirectory(specs)
add_subdirectory(services)
+ add_subdirectory(sha256)
ENDIF(ENABLE_UT)
IF(ENABLE_FUZZ)
diff --git a/test/sha256/CMakeLists.txt b/test/sha256/CMakeLists.txt
new file mode 100644
index 00000000..10779f4c
--- /dev/null
+++ b/test/sha256/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE sha256_ut)
+
+add_executable(${EXE}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/tar/util_gzip.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256/sha256.c
+ sha256_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/tar
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/sha256/sha256_ut.cc b/test/sha256/sha256_ut.cc
new file mode 100644
index 00000000..746220d7
--- /dev/null
+++ b/test/sha256/sha256_ut.cc
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: sha256 unit test
+ * Author: chengzeruizhi
+ * Create: 2022-11-22
+ */
+
+#include <gtest/gtest.h>
+
+#include "constants.h"
+#include "util_gzip.h"
+#include "utils.h"
+#include "utils_file.h"
+#include "sha256.h"
+
+TEST(sha256, test_sha256_digest_file)
+{
+ int get_err;
+ char *digest = sha256_digest_file(NULL, false);
+ EXPECT_EQ(digest, nullptr);
+
+ digest = sha256_digest_file(NULL, true);
+ EXPECT_EQ(digest, nullptr);
+
+ int fd = util_open("/tmp/sha256_empty_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd, 0);
+ digest = sha256_digest_file("/tmp/sha256_empty_file", false);
+ EXPECT_STREQ(digest, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ int emptyfile_ret = util_gzip_z("/tmp/sha256_empty_file", "/tmp/sha256_empty_file.gz", DEFAULT_SECURE_FILE_MODE);
+ digest = sha256_digest_file("/tmp/sha256_empty_file.gz", true);
+ EXPECT_STREQ(digest, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ close(fd);
+ util_force_remove_file("/tmp/sha256_empty_file", &get_err);
+ if (emptyfile_ret == 0) {
+ util_force_remove_file("/tmp/sha256_empty_file.gz", &get_err);
+ }
+
+ int fd2 = util_open("/tmp/sha256_test_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd2, 0);
+ util_write_nointr(fd2, "asdjfljsad", 10);
+ digest = sha256_digest_file("/tmp/sha256_test_file", false);
+ EXPECT_STREQ(digest, "fe2d2648f9221659cf67068096ba561211d06d37dbfaf2d61b0b3bc34f43d3e1");
+ int testfile_ret = util_gzip_z("/tmp/sha256_test_file", "/tmp/sha256_test_file.gz", DEFAULT_SECURE_FILE_MODE);
+ digest = sha256_digest_file("/tmp/sha256_test_file.gz", true);
+ EXPECT_STREQ(digest, "fe2d2648f9221659cf67068096ba561211d06d37dbfaf2d61b0b3bc34f43d3e1");
+ close(fd2);
+ util_force_remove_file("/tmp/sha256_test_file", &get_err);
+ if (testfile_ret == 0) {
+ util_force_remove_file("/tmp/sha256_test_file.gz", &get_err);
+ }
+}
+
+TEST(sha256, test_sha256_digest_str)
+{
+ char *digest = sha256_digest_str(NULL);
+ EXPECT_EQ(digest, nullptr);
+
+ digest = sha256_digest_str("");
+ EXPECT_STREQ(digest, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+
+ digest = sha256_digest_str(
+ "^cvdgfdgghaswere3575676y&*`~cx,xfdgdvcvdfd][';./?.,<>|\\!@#$%^&*()_+=-090wvvs3sdfel33cxvdf***$");
+ EXPECT_STREQ(digest, "899a57a99c14c047eab26f8d6719da256a0737f6c28728ba5777b4fc5398c657");
+}
+
+TEST(sha256, test_sha256_full_gzip_digest)
+{
+ int get_err;
+ char *digest = sha256_full_gzip_digest(NULL);
+ EXPECT_EQ(digest, nullptr);
+
+ int fd = util_open("/tmp/sha256_empty_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd, 0);
+ digest = sha256_full_gzip_digest("/tmp/sha256_empty_file");
+ EXPECT_EQ(digest, nullptr);
+
+ int emptyfile_ret = util_gzip_z("/tmp/sha256_empty_file", "/tmp/sha256_empty_file.gz", DEFAULT_SECURE_FILE_MODE);
+ digest = sha256_full_gzip_digest("/tmp/sha256_empty_file.gz");
+ EXPECT_STREQ(digest, "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ close(fd);
+ util_force_remove_file("/tmp/sha256_empty_file", &get_err);
+ if (emptyfile_ret == 0) {
+ util_force_remove_file("/tmp/sha256_empty_file.gz", &get_err);
+ }
+
+ int fd2 = util_open("/tmp/sha256_test_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd2, 0);
+ util_write_nointr(fd2, "asdjfljsad", 10);
+ digest = sha256_full_gzip_digest("/tmp/sha256_test_file");
+ EXPECT_EQ(digest, nullptr);
+ int testfile_ret = util_gzip_z("/tmp/sha256_test_file", "/tmp/sha256_test_file.gz", DEFAULT_SECURE_FILE_MODE);
+ digest = sha256_full_gzip_digest("/tmp/sha256_test_file.gz");
+ EXPECT_STREQ(digest, "sha256:fe2d2648f9221659cf67068096ba561211d06d37dbfaf2d61b0b3bc34f43d3e1");
+ close(fd2);
+ util_force_remove_file("/tmp/sha256_test_file", &get_err);
+ if (testfile_ret == 0) {
+ util_force_remove_file("/tmp/sha256_test_file.gz", &get_err);
+ }
+}
+
+TEST(sha256, test_sha256_full_file_digest)
+{
+ int get_err;
+ char *digest = sha256_full_file_digest(NULL);
+ EXPECT_EQ(digest, nullptr);
+
+ int fd = util_open("/tmp/sha256_empty_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd, 0);
+ digest = sha256_full_file_digest("/tmp/sha256_empty_file");
+ EXPECT_STREQ(digest, "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+ close(fd);
+ util_force_remove_file("/tmp/sha256_empty_file", &get_err);
+
+ int fd2 = util_open("/tmp/sha256_test_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd2, 0);
+ util_write_nointr(fd2, "asdjfljsad", 10);
+ digest = sha256_full_file_digest("/tmp/sha256_test_file");
+ EXPECT_STREQ(digest, "sha256:fe2d2648f9221659cf67068096ba561211d06d37dbfaf2d61b0b3bc34f43d3e1");
+ close(fd2);
+ util_force_remove_file("/tmp/sha256_test_file", &get_err);
+}
+
+TEST(sha256, test_sha256_valid_digest_file)
+{
+ int get_err;
+
+ ASSERT_FALSE(sha256_valid_digest_file(NULL, NULL));
+ int fd = util_open("/tmp/sha256_test_file", O_RDWR | O_CREAT, DEFAULT_SECURE_FILE_MODE);
+ ASSERT_GE(fd, 0);
+ util_write_nointr(fd, "asdjfljsad", 10);
+ EXPECT_TRUE(sha256_valid_digest_file("/tmp/sha256_test_file",
+ "sha256:fe2d2648f9221659cf67068096ba561211d06d37dbfaf2d61b0b3bc34f43d3e1"));
+ util_force_remove_file("/tmp/sha256_test_file", &get_err);
+}
+
+TEST(sha256, test_sha256_full_digest_str)
+{
+ char *full_digest = sha256_full_digest_str(NULL);
+ EXPECT_EQ(full_digest, nullptr);
+ full_digest = sha256_full_digest_str(util_strdup_s(""));
+ EXPECT_STREQ(full_digest, "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+}
+
+TEST(sha256, test_util_without_sha256_prefix)
+{
+ char *digest = util_without_sha256_prefix(NULL);
+ EXPECT_EQ(digest, nullptr);
+ digest = util_without_sha256_prefix(util_strdup_s("sha246:"));
+ EXPECT_EQ(digest, nullptr);
+ digest = util_without_sha256_prefix(util_strdup_s("sha256:"));
+ EXPECT_STREQ(digest, "");
+ digest = util_without_sha256_prefix(util_strdup_s("sha256:asdfawf2q3rqrg234rewfd]\a]sd;v.z/xc"));
+ EXPECT_STREQ(digest, "asdfawf2q3rqrg234rewfd]\a]sd;v.z/xc");
+}
\ No newline at end of file
--
2.25.1

View File

@ -1,31 +0,0 @@
From d1527a3b8405d92f638c46c8250f2636ba18c644 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Fri, 25 Nov 2022 16:22:47 +0800
Subject: [PATCH 49/54] add primary group to additional groups
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/daemon/modules/image/image_rootfs_handler.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/src/daemon/modules/image/image_rootfs_handler.c b/src/daemon/modules/image/image_rootfs_handler.c
index 842c1dd3..a76363d0 100644
--- a/src/daemon/modules/image/image_rootfs_handler.c
+++ b/src/daemon/modules/image/image_rootfs_handler.c
@@ -546,6 +546,13 @@ int get_user_from_image_roofs(const char *basefs, const host_config *hc, const c
}
}
+ // CVE-2022-36109
+ // add primary group to additional groups
+ ret = append_additional_gids(puser->gid, &puser->additional_gids, &puser->additional_gids_len);
+ if (ret != 0) {
+ goto cleanup;
+ }
+
cleanup:
if (f_passwd != NULL) {
fclose(f_passwd);
--
2.25.1

View File

@ -1,178 +0,0 @@
From afad1f4da9a5411280e094e121cba18180d60958 Mon Sep 17 00:00:00 2001
From: chengzrz <czrzrichard@gmail.com>
Date: Fri, 25 Nov 2022 17:15:22 +0800
Subject: [PATCH 50/54] add unit test for buffer
Signed-off-by: chengzrz <czrzrichard@gmail.com>
---
test/CMakeLists.txt | 1 +
test/buffer/CMakeLists.txt | 17 ++++++++
test/buffer/buffer_ut.cc | 89 ++++++++++++++++++++++++++++++++++++++
test/tar/CMakeLists.txt | 18 ++++++++
test/tar/tar_ut.cc | 0
5 files changed, 125 insertions(+)
create mode 100644 test/buffer/CMakeLists.txt
create mode 100644 test/buffer/buffer_ut.cc
create mode 100644 test/tar/CMakeLists.txt
create mode 100644 test/tar/tar_ut.cc
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 27201100..8b927f91 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -45,6 +45,7 @@ IF(ENABLE_UT)
add_subdirectory(specs)
add_subdirectory(services)
add_subdirectory(sha256)
+ add_subdirectory(buffer)
ENDIF(ENABLE_UT)
IF(ENABLE_FUZZ)
diff --git a/test/buffer/CMakeLists.txt b/test/buffer/CMakeLists.txt
new file mode 100644
index 00000000..f900b592
--- /dev/null
+++ b/test/buffer/CMakeLists.txt
@@ -0,0 +1,17 @@
+project(iSulad_UT)
+
+SET(EXE buffer_ut)
+
+add_executable(${EXE}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/buffer/buffer.c
+ buffer_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/buffer
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/buffer/buffer_ut.cc b/test/buffer/buffer_ut.cc
new file mode 100644
index 00000000..9c5630e0
--- /dev/null
+++ b/test/buffer/buffer_ut.cc
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: buffer unit test
+ * Author: chengzeruizhi
+ * Create: 2022-11-29
+ */
+
+#include <gtest/gtest.h>
+
+#include "buffer.h"
+
+TEST(buffer, test_buffer_alloc)
+{
+ Buffer *buffer = buffer_alloc(0);
+ EXPECT_EQ(buffer, nullptr);
+
+ buffer = buffer_alloc(-1);
+ EXPECT_EQ(buffer, nullptr);
+
+ buffer = buffer_alloc(SIZE_MAX + 1);
+ EXPECT_EQ(buffer, nullptr);
+
+ buffer = buffer_alloc(10);
+ ASSERT_NE(buffer, nullptr);
+ EXPECT_EQ(buffer->total_size, 10);
+ EXPECT_EQ(buffer->bytes_used, 0);
+ EXPECT_NE(buffer->contents, nullptr);
+ buffer_free(buffer);
+}
+
+TEST(buffer, test_buffer_strlen)
+{
+ Buffer *buffer = buffer_alloc(0);
+ EXPECT_EQ(buffer_strlen(buffer), 0);
+ buffer = buffer_alloc(-1);
+ EXPECT_EQ(buffer_strlen(buffer), 0);
+ buffer = buffer_alloc(SIZE_MAX + 1);
+ EXPECT_EQ(buffer_strlen(buffer), 0);
+ buffer = buffer_alloc(10);
+ ASSERT_NE(buffer, nullptr);
+ EXPECT_EQ(buffer_strlen(buffer), 0);
+ ASSERT_EQ(buffer_append(buffer, "append", 6), 0);
+ EXPECT_EQ(buffer_strlen(buffer), 6);
+ buffer_free(buffer);
+}
+
+TEST(buffer, test_buffer_free)
+{
+ Buffer *buffer = nullptr;
+ buffer_free(buffer);
+ EXPECT_EQ(buffer, nullptr);
+}
+
+TEST(buffer, test_buffer_append)
+{
+ EXPECT_EQ(buffer_append(nullptr, "append", 6), -1);
+ Buffer *buffer = buffer_alloc(5);
+ EXPECT_EQ(buffer_append(buffer, "buffer needs to grow", 20), 0);
+ EXPECT_STREQ(buffer->contents, "buffer needs to grow");
+ EXPECT_EQ(buffer->bytes_used, 20);
+ EXPECT_EQ(buffer->total_size, 42);
+ buffer_free(buffer);
+
+ buffer = buffer_alloc(20);
+ EXPECT_EQ(buffer_append(buffer, "first", 5), 0);
+ EXPECT_EQ(buffer->bytes_used, 5);
+ EXPECT_STREQ(buffer->contents, "first");
+ EXPECT_EQ(buffer_append(buffer, "second", 6), 0);
+ EXPECT_EQ(buffer->bytes_used, 11);
+ EXPECT_EQ(buffer->total_size, 20);
+ EXPECT_STREQ(buffer->contents, "firstsecond");
+}
+
+TEST(buffer, test_buffer_empty)
+{
+ Buffer *buffer = buffer_alloc(10);
+ buffer_append(buffer, "content", 7);
+ buffer_empty(buffer);
+ EXPECT_EQ(buffer->total_size, 10);
+ EXPECT_EQ(buffer->bytes_used, 0);
+}
diff --git a/test/tar/CMakeLists.txt b/test/tar/CMakeLists.txt
new file mode 100644
index 00000000..10779f4c
--- /dev/null
+++ b/test/tar/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE sha256_ut)
+
+add_executable(${EXE}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/tar/util_gzip.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256/sha256.c
+ sha256_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/common
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/tar
+ ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256
+ )
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
diff --git a/test/tar/tar_ut.cc b/test/tar/tar_ut.cc
new file mode 100644
index 00000000..e69de29b
--
2.25.1

View File

@ -1,86 +0,0 @@
From 7c1c3107fffbfd208acccce8f3c077e10babed3d Mon Sep 17 00:00:00 2001
From: yangjiaqi <yangjiaqi16@huawei.com>
Date: Mon, 28 Nov 2022 21:03:19 +0800
Subject: [PATCH 51/54] remove chmod 751 permission for dirs by engine when
user-remap enabled
---
.../modules/service/service_container.c | 55 -------------------
1 file changed, 55 deletions(-)
diff --git a/src/daemon/modules/service/service_container.c b/src/daemon/modules/service/service_container.c
index 2b3c8794..85a8ab52 100644
--- a/src/daemon/modules/service/service_container.c
+++ b/src/daemon/modules/service/service_container.c
@@ -413,54 +413,6 @@ static int mount_host_channel(const host_config_host_channel *host_channel, cons
return 0;
}
-static int chmod_runtime_bundle_permission(const char *runtime)
-{
- int ret = 0;
- char *bundle_dir = NULL;
- char *engine_dir = NULL;
- char *root_dir = NULL;
-
- bundle_dir = conf_get_routine_rootdir(runtime);
- if (bundle_dir == NULL) {
- ret = -1;
- goto error_out;
- }
-
- engine_dir = conf_get_engine_rootpath();
- if (engine_dir == NULL) {
- ret = -1;
- goto error_out;
- }
-
- root_dir = conf_get_isulad_rootdir();
- if (root_dir == NULL) {
- ret = -1;
- goto error_out;
- }
-
- ret = chmod(bundle_dir, USER_REMAP_DIRECTORY_MODE);
- if (ret != 0) {
- ERROR("Failed to chmod bundle dir '%s' for user remap", bundle_dir);
- goto error_out;
- }
- ret = chmod(engine_dir, USER_REMAP_DIRECTORY_MODE);
- if (ret != 0) {
- ERROR("Failed to chmod engine dir '%s' for user remap", engine_dir);
- goto error_out;
- }
- ret = chmod(root_dir, USER_REMAP_DIRECTORY_MODE);
- if (ret != 0) {
- ERROR("Failed to chmod root dir '%s' for user remap", root_dir);
- goto error_out;
- }
-
-error_out:
- free(bundle_dir);
- free(engine_dir);
- free(root_dir);
- return ret;
-}
-
static int prepare_user_remap_config(const container_t *cont)
{
if (cont == NULL) {
@@ -471,13 +423,6 @@ static int prepare_user_remap_config(const container_t *cont)
return 0;
}
- if (cont->hostconfig->user_remap != NULL) {
- if (chmod_runtime_bundle_permission(cont->runtime)) {
- ERROR("Failed to chmod bundle permission for user remap");
- return -1;
- }
- }
-
if (cont->hostconfig->host_channel != NULL) {
if (mount_host_channel(cont->hostconfig->host_channel, cont->hostconfig->user_remap)) {
ERROR("Failed to mount host channel");
--
2.25.1

View File

@ -1,168 +0,0 @@
From 04c7beb8788826063c19715b58e11c4eea7efbe6 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Wed, 30 Nov 2022 23:54:47 -0800
Subject: [PATCH 52/54] add console ut
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
test/CMakeLists.txt | 1 +
test/console/CMakeLists.txt | 20 +++++++
test/console/console_ut.cc | 107 ++++++++++++++++++++++++++++++++++++
3 files changed, 128 insertions(+)
create mode 100644 test/console/CMakeLists.txt
create mode 100644 test/console/console_ut.cc
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 8b927f91..06adb602 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -46,6 +46,7 @@ IF(ENABLE_UT)
add_subdirectory(services)
add_subdirectory(sha256)
add_subdirectory(buffer)
+ add_subdirectory(console)
ENDIF(ENABLE_UT)
IF(ENABLE_FUZZ)
diff --git a/test/console/CMakeLists.txt b/test/console/CMakeLists.txt
new file mode 100644
index 00000000..acadc620
--- /dev/null
+++ b/test/console/CMakeLists.txt
@@ -0,0 +1,20 @@
+project(iSulad_UT)
+
+SET(EXE console_ut)
+
+add_executable(${EXE}
+ ${CMAKE_SOURCE_DIR}/src/utils/console/console.c
+ console_ut.cc)
+
+
+target_include_directories(${EXE} PUBLIC
+ ${GTEST_INCLUDE_DIR}
+ ${CMAKE_SOURCE_DIR}/src/utils/console
+ ${CMAKE_SOURCE_DIR}/src/utils/cutils
+ ${CMAKE_SOURCE_DIR}/src/common
+ )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
+
+
diff --git a/test/console/console_ut.cc b/test/console/console_ut.cc
new file mode 100644
index 00000000..73479000
--- /dev/null
+++ b/test/console/console_ut.cc
@@ -0,0 +1,107 @@
+#include <sys/stat.h>
+#include <gtest/gtest.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>
+
+#include "console.h"
+
+#define FIFO_NAME "fifo1"
+#define PATH_NOT_EXIST "./path_not_found/"
+#define LONGER_PATH_MAX 4098
+
+TEST(utils_console, test_console_fifo_create)
+{
+ int ret = 0;
+ struct stat buf;
+
+ ret = console_fifo_create(FIFO_NAME);
+ if (ret != 0) {
+ return;
+ }
+
+ if (stat(FIFO_NAME, &buf) < 0) {
+ return;
+ }
+
+ ASSERT_EQ(S_ISFIFO(buf.st_mode), true);
+
+ ret = access(FIFO_NAME, R_OK|W_OK);
+ ASSERT_EQ(ret, 0);
+
+ remove(FIFO_NAME);
+}
+
+TEST(utils_console, test_console_fifo_create_failed)
+{
+ int ret = 0;
+
+ ret = console_fifo_create(PATH_NOT_EXIST FIFO_NAME);
+ ASSERT_EQ(ret, -1);
+}
+
+TEST(utils_console, test_console_fifo_delete)
+{
+ int ret = 0;
+ char path_buf[LONGER_PATH_MAX] = { 0x00 };
+
+ memset(path_buf, 'a', LONGER_PATH_MAX);
+ path_buf[LONGER_PATH_MAX - 1] = 0;
+ ASSERT_EQ(strlen(path_buf), LONGER_PATH_MAX-1)<< "strlen is " << strlen(path_buf);
+
+ ret = console_fifo_create(FIFO_NAME);
+ if (ret != 0) {
+ return;
+ }
+
+ // PATH TOO LONG
+ ret = console_fifo_delete(path_buf);
+ ASSERT_EQ(ret, -1) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+ // PATH NULL
+ ret = console_fifo_delete(NULL);
+ ASSERT_EQ(ret, -1) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+ // PATH LEN IS ZERO
+ ret = console_fifo_delete("");
+ ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+ // PATH NOT FOUND
+ ret = console_fifo_delete(PATH_NOT_EXIST FIFO_NAME);
+ ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+ ret = console_fifo_delete(FIFO_NAME);
+ ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+}
+
+TEST(utils_console, test_console_fifo_open)
+{
+ int ret = 0;
+ int fifooutfd = -1;
+
+ ret = console_fifo_create(FIFO_NAME);
+ if (ret != 0) {
+ return;
+ }
+
+ ret = console_fifo_open(FIFO_NAME, &fifooutfd, O_RDWR | O_NONBLOCK);
+ ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+ console_fifo_close(fifooutfd);
+ remove(FIFO_NAME);
+}
+
+TEST(utils_console, test_console_fifo_open_withlock)
+{
+ int ret = 0;
+ int fifooutfd = -1;
+
+ ret = console_fifo_create(FIFO_NAME);
+ if (ret != 0) {
+ return;
+ }
+
+ ret = console_fifo_open_withlock(FIFO_NAME, &fifooutfd, O_RDWR | O_NONBLOCK);
+ ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+ console_fifo_close(fifooutfd);
+ remove(FIFO_NAME);
+}
--
2.25.1

View File

@ -1,128 +0,0 @@
From 51a57b584eed06e0d857963f2e2750114e26ef52 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 3 Dec 2022 15:43:43 +0800
Subject: [PATCH 53/54] fix additional gids for exec user
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/daemon/modules/runtime/engines/engine.h | 1 +
.../modules/runtime/engines/lcr/lcr_rt_ops.c | 71 +++++++++++++++++++
2 files changed, 72 insertions(+)
diff --git a/src/daemon/modules/runtime/engines/engine.h b/src/daemon/modules/runtime/engines/engine.h
index 95428e0f..04c4a670 100644
--- a/src/daemon/modules/runtime/engines/engine.h
+++ b/src/daemon/modules/runtime/engines/engine.h
@@ -71,6 +71,7 @@ typedef struct _engine_exec_request_t {
const char **console_fifos;
const char *user;
+ const char *add_gids;
const char **env;
size_t env_len;
diff --git a/src/daemon/modules/runtime/engines/lcr/lcr_rt_ops.c b/src/daemon/modules/runtime/engines/lcr/lcr_rt_ops.c
index a2b93b72..f2eec6d2 100644
--- a/src/daemon/modules/runtime/engines/lcr/lcr_rt_ops.c
+++ b/src/daemon/modules/runtime/engines/lcr/lcr_rt_ops.c
@@ -352,12 +352,76 @@ static int generate_user_string_by_uid_gid(const defs_process_user *puser, char
return 0;
}
+static char **covert_gids_to_string(const gid_t *gids, const size_t gids_len)
+{
+ int nret = 0;
+ size_t i = 0;
+ size_t len = 0;
+ char **result = NULL;
+
+ result = util_smart_calloc_s(sizeof(char *), gids_len);
+ if (result == NULL) {
+ ERROR("Out of memory");
+ return NULL;
+ }
+
+ for (i = 0; i < gids_len; i++) {
+ char gid_str[ISULAD_NUMSTRLEN32] = { 0 };
+
+ nret = snprintf(gid_str, ISULAD_NUMSTRLEN32, "%u", (unsigned int)gids[i]);
+ if (nret < 0 || nret >= ISULAD_NUMSTRLEN32) {
+ ERROR("Invalid gid :%u", (unsigned int)gids[i]);
+ util_free_array_by_len(result, len);
+ return NULL;
+ }
+
+ result[i] = util_strdup_s(gid_str);
+ len++;
+ }
+
+ return result;
+}
+
+// additional gids string(GID[,GID])
+static int generate_add_gids_string(const defs_process_user *puser, char **add_gids)
+{
+ const size_t max_gids = 100;
+ char **gids = NULL;
+
+ if (puser->additional_gids == NULL || puser->additional_gids_len == 0) {
+ INFO("None attach additional gids");
+ return 0;
+ }
+
+ if (puser->additional_gids_len > max_gids) {
+ ERROR("Too many additional gids");
+ return -1;
+ }
+
+ gids = covert_gids_to_string(puser->additional_gids, puser->additional_gids_len);
+ if (gids == NULL) {
+ ERROR("Failed to covert gids to string");
+ return -1;
+ }
+
+ *add_gids = util_string_join(",", (const char **)gids, puser->additional_gids_len);
+ if (*add_gids == NULL) {
+ ERROR("Failed to string join");
+ util_free_array_by_len(gids, puser->additional_gids_len);
+ return -1;
+ }
+
+ util_free_array_by_len(gids, puser->additional_gids_len);
+ return 0;
+}
+
int rt_lcr_exec(const char *id, const char *runtime, const rt_exec_params_t *params, int *exit_code)
{
int ret = 0;
struct engine_operation *engine_ops = NULL;
engine_exec_request_t request = { 0 };
char *user = NULL;
+ char *add_gids = NULL;
engine_ops = engines_get_handler(runtime);
if (engine_ops == NULL || engine_ops->engine_exec_op == NULL) {
@@ -385,6 +449,12 @@ int rt_lcr_exec(const char *id, const char *runtime, const rt_exec_params_t *par
goto out;
}
request.user = user;
+
+ if (generate_add_gids_string(params->spec->user, &add_gids) != 0) {
+ ret = -1;
+ goto out;
+ }
+ request.add_gids = add_gids;
}
request.open_stdin = params->attach_stdin;
@@ -412,6 +482,7 @@ out:
engine_ops->engine_clear_errmsg_op();
}
free(user);
+ free(add_gids);
return ret;
}
--
2.25.1

View File

@ -1,112 +0,0 @@
From 31ed5d907341363408c8d90aa72a6eee12ad7ccb Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 3 Dec 2022 17:10:38 +0800
Subject: [PATCH 54/54] add CI for additional gid
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../container_cases/exec_additional_gids.sh | 92 +++++++++++++++++++
1 file changed, 92 insertions(+)
create mode 100755 CI/test_cases/container_cases/exec_additional_gids.sh
diff --git a/CI/test_cases/container_cases/exec_additional_gids.sh b/CI/test_cases/container_cases/exec_additional_gids.sh
new file mode 100755
index 00000000..f24678d3
--- /dev/null
+++ b/CI/test_cases/container_cases/exec_additional_gids.sh
@@ -0,0 +1,92 @@
+#!/bin/bash
+#
+# attributes: isulad exec check additional gids
+# concurrent: YES
+# spend time: 1
+
+#######################################################################
+##- Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+# - iSulad licensed under the Mulan PSL v2.
+# - You can use this software according to the terms and conditions of the Mulan PSL v2.
+# - You may obtain a copy of Mulan PSL v2 at:
+# - http://license.coscl.org.cn/MulanPSL2
+# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# - PURPOSE.
+# - See the Mulan PSL v2 for more details.
+##- @Description:CI
+##- @Author: zhangxiaoyu
+##- @Create: 2022-12-03
+#######################################################################
+
+curr_path=$(dirname $(readlink -f "$0"))
+data_path=$(realpath $curr_path/../data)
+source ../helpers.sh
+test="exec additional gids test => test_exec_additional_gids"
+test_log=$(mktemp /tmp/additional_gids_test_XXX)
+
+USERNAME="user"
+USER_UID="1000"
+USER_GID="$USER_UID"
+ADDITIONAL_GID="1001"
+ADDITIONAL_GROUP="additional"
+
+cont_name=add_gids_test
+file_info="Keep it secret, keep it safe"
+
+function additional_gids_test()
+{
+ local ret=0
+
+ isula rm -f `isula ps -a -q`
+
+ isula run -tid -n $cont_name ubuntu bash
+ [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to run container" && ((ret++))
+
+ isula exec $cont_name bash -c "groupadd --gid $USER_GID $USERNAME \
+ && groupadd --gid $ADDITIONAL_GID $ADDITIONAL_GROUP \
+ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -G $ADDITIONAL_GROUP \
+ && mkdir /app && chown ${USERNAME}:${USERNAME} /app \
+ && echo $file_info > /app/sekrit.txt \
+ && chown 0:${USER_GID} /app/sekrit.txt \
+ && chmod 606 /app/sekrit.txt"
+ [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - create user and group failed" && ((ret++))
+
+ /usr/bin/expect <<- EOF > ${test_log} 2>&1
+set timeout 10
+spawn isula exec -it --workdir /app -u $USERNAME $cont_name bash
+expect "${USERNAME}*"
+send "newgrp ${ADDITIONAL_GROUP}\n"
+expect "*"
+send "groups\n"
+expect "$"
+send "cat sekrit.txt\n"
+expect "*"
+send "exit\n"
+expect "${USERNAME}*"
+send "exit\n"
+expect eof
+EOF
+
+ cat $test_log | grep "$file_info"
+ [[ $? -eq 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - read file success, but should fail" && ((ret++))
+
+ cat $test_log | grep "Permission denied"
+ [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - read error message failed" && ((ret++))
+
+ isula rm -f `isula ps -a -q`
+
+ return ${ret}
+}
+
+declare -i ans=0
+
+msg_info "${test} starting..."
+
+additional_gids_test || ((ans++))
+
+rm -rf ${test_log}
+
+msg_info "${test} finished with return ${ret}..."
+
+show_result ${ans} "${curr_path}/${0}"
--
2.25.1

View File

@ -1,139 +0,0 @@
From 3ffaa7ae39d1af1d7c5aae976f1d6017ad76dee9 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Wed, 30 Nov 2022 17:33:16 +0800
Subject: [PATCH 55/65] retry call runtime ops
1. add retry macro;
2. retry call runtime cleanup ops;
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
src/daemon/modules/plugin/plugin.c | 21 +++----------------
.../modules/runtime/isula/isula_rt_ops.c | 14 +++++++++++--
src/utils/cutils/utils.c | 1 +
src/utils/cutils/utils.h | 19 +++++++++++++++++
4 files changed, 35 insertions(+), 20 deletions(-)
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 725bca5b..18035518 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -71,7 +71,7 @@
// suffix is '.sock'
#define PLUGIN_SOCKET_FILE_SUFFIX_LEN 5
-#define PLUGIN_ACTIVATE_MAX_RETRY 3
+#define PLUGIN_ACTIVATE_MAX_RETRY 5
#ifndef RestHttpHead
#define RestHttpHead "http://localhost"
@@ -403,22 +403,6 @@ out:
return -1;
}
-static int pm_activate_plugin_with_retry(plugin_t *plugin, size_t retry)
-{
- size_t i = 0;
- int err = 0;
-
- for (i = 0; i < retry; i++) {
- err = pm_activate_plugin(plugin);
- if (!err) {
- return 0;
- }
- sleep((unsigned int)i + 1);
- }
-
- return err;
-}
-
static void pm_rdlock(void)
{
int errcode;
@@ -500,7 +484,8 @@ static int pm_register_plugin(const char *name, const char *addr)
ERROR("alloc plugin failed");
goto failed;
}
- err = pm_activate_plugin_with_retry(plugin, PLUGIN_ACTIVATE_MAX_RETRY);
+
+ DO_RETYR_CALL(PLUGIN_ACTIVATE_MAX_RETRY, 1000000, err, pm_activate_plugin, plugin);
if (err != 0) {
ERROR("active plugin failed");
goto failed;
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index c9667ee5..bfe7de08 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -946,6 +946,7 @@ int rt_isula_restart(const char *name, const char *runtime, const rt_restart_par
int rt_isula_clean_resource(const char *id, const char *runtime, const rt_clean_params_t *params)
{
char workdir[PATH_MAX] = { 0 };
+ int nret;
if (id == NULL || runtime == NULL || params == NULL) {
ERROR("nullptr arguments not allowed");
@@ -966,8 +967,17 @@ int rt_isula_clean_resource(const char *id, const char *runtime, const rt_clean_
shim_kill_force(workdir);
}
- (void)runtime_call_kill_force(workdir, runtime, id);
- (void)runtime_call_delete_force(workdir, runtime, id);
+ // retry 10 count call runtime kill, every call sleep 1s
+ DO_RETYR_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
+ if (nret != 0) {
+ WARN("call runtime force kill failed");
+ }
+
+ // retry 10 count call runtime delete, every call sleep 1s
+ DO_RETYR_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
+ if (nret != 0) {
+ WARN("call runtime force delete failed");
+ }
if (util_recursive_rmdir(workdir, 0) != 0) {
ERROR("failed rmdir -r shim workdir");
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index 9f5deaf9..a154c52a 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -1216,6 +1216,7 @@ void util_usleep_nointerupt(unsigned long usec)
request = remain;
} while (ret == -1 && errno == EINTR);
}
+
int util_generate_random_str(char *id, size_t len)
{
int fd = -1;
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 27cfc902..0a9535a1 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -381,6 +381,25 @@ defs_map_string_object * dup_map_string_empty_object(defs_map_string_object *src
int convert_v2_runtime(const char *runtime, char *binary);
+/**
+ * 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_RETYR_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)
+
#ifdef __cplusplus
}
#endif
--
2.25.1

View File

@ -1,50 +0,0 @@
From 61a06b548e05edb3892eb08e1028ef71b41ee332 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Fri, 2 Dec 2022 15:35:52 +0800
Subject: [PATCH 56/65] add ut test for retry macro
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
test/cutils/utils_utils/utils_utils_ut.cc | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/test/cutils/utils_utils/utils_utils_ut.cc b/test/cutils/utils_utils/utils_utils_ut.cc
index 531947d2..5bd98d47 100644
--- a/test/cutils/utils_utils/utils_utils_ut.cc
+++ b/test/cutils/utils_utils/utils_utils_ut.cc
@@ -269,4 +269,31 @@ TEST(utils_utils, test_convert_v2_runtime)
ASSERT_EQ(convert_v2_runtime(nullptr, buff), -1);
ASSERT_EQ(convert_v2_runtime(valid_str.c_str(), nullptr), -1);
ASSERT_EQ(convert_v2_runtime(valid_str.c_str(), buff), 0);
+}
+
+int global_total = 0;
+int retry_call_test(int success_idx) {
+ if (global_total == success_idx) {
+ return 0;
+ }
+ global_total++;
+ return -1;
+}
+
+TEST(utils_utils, test_do_retry_call)
+{
+ int nret;
+
+ global_total = 0;
+ DO_RETYR_CALL(10, 100, nret, retry_call_test, 0);
+ ASSERT_EQ(nret, 0);
+ ASSERT_EQ(global_total, 0);
+ global_total = 0;
+ DO_RETYR_CALL(10, 100, nret, retry_call_test, 5);
+ ASSERT_EQ(nret, 0);
+ ASSERT_EQ(global_total, 5);
+ global_total = 0;
+ DO_RETYR_CALL(10, 100, nret, retry_call_test, 11);
+ ASSERT_EQ(global_total, 10);
+ ASSERT_EQ(nret, -1);
}
\ No newline at end of file
--
2.25.1

View File

@ -1,98 +0,0 @@
From 3e51dc0746c5e0692f54cf54d7aa6beb3b13d799 Mon Sep 17 00:00:00 2001
From: ger202 <huangsong14@huawei.com>
Date: Wed, 7 Dec 2022 03:21:17 +0000
Subject: [PATCH 57/65] !1749 set inspect_container timeout * update function
inspect_container timeout
---
src/cmd/isula/information/top.c | 4 +++-
src/cmd/isula/stream/attach.c | 5 ++++-
src/cmd/isula/stream/exec.c | 4 +++-
src/daemon/entry/cri/cri_helpers.cc | 3 +--
src/utils/cutils/utils.h | 3 +++
5 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/cmd/isula/information/top.c b/src/cmd/isula/information/top.c
index 603f97ba..e55ec43b 100644
--- a/src/cmd/isula/information/top.c
+++ b/src/cmd/isula/information/top.c
@@ -29,7 +29,9 @@
const char g_cmd_top_desc[] = "Display the running processes of a container";
const char g_cmd_top_usage[] = "top [OPTIONS] CONTAINER [ps OPTIONS]";
-struct client_arguments g_cmd_top_args = {};
+struct client_arguments g_cmd_top_args = {
+ .time = INSPECT_TIMEOUT_SEC,
+};
static void client_top_info_server(const struct isula_top_response *response)
{
size_t i;
diff --git a/src/cmd/isula/stream/attach.c b/src/cmd/isula/stream/attach.c
index f260a76e..7fb1046a 100644
--- a/src/cmd/isula/stream/attach.c
+++ b/src/cmd/isula/stream/attach.c
@@ -48,7 +48,9 @@ sem_t g_attach_waitopen_sem;
sem_t g_attach_waitexit_sem;
#endif
-struct client_arguments g_cmd_attach_args = { 0 };
+struct client_arguments g_cmd_attach_args = {
+ .time = INSPECT_TIMEOUT_SEC,
+};
static int check_tty(bool tty, struct termios *oldtios, bool *reset_tty)
@@ -116,6 +118,7 @@ int inspect_container(const struct client_arguments *args, container_inspect **i
}
inspect_request.name = args->name;
+ inspect_request.timeout = args->time;
ops = get_connect_client_ops();
if (ops == NULL || !ops->container.inspect) {
COMMAND_ERROR("Unimplemented ops");
diff --git a/src/cmd/isula/stream/exec.c b/src/cmd/isula/stream/exec.c
index 2d0d37da..bd8bd49a 100644
--- a/src/cmd/isula/stream/exec.c
+++ b/src/cmd/isula/stream/exec.c
@@ -42,7 +42,9 @@ const char g_cmd_exec_usage[] = "exec [OPTIONS] CONTAINER COMMAND [ARG...]";
sem_t g_command_waitopen_sem;
sem_t g_command_waitexit_sem;
-struct client_arguments g_cmd_exec_args = {};
+struct client_arguments g_cmd_exec_args = {
+ .time = INSPECT_TIMEOUT_SEC,
+};
static int fill_exec_request(const struct client_arguments *args, const struct command_fifo_config *fifos,
struct isula_exec_request *request)
diff --git a/src/daemon/entry/cri/cri_helpers.cc b/src/daemon/entry/cri/cri_helpers.cc
index 7df759e1..64cea7ba 100644
--- a/src/daemon/entry/cri/cri_helpers.cc
+++ b/src/daemon/entry/cri/cri_helpers.cc
@@ -720,8 +720,7 @@ out:
auto InspectContainer(const std::string &Id, Errors &err, bool with_host_config) -> container_inspect *
{
container_inspect *inspect_data { nullptr };
-
- inspect_data = inspect_container((const char *)Id.c_str(), 0, with_host_config);
+ inspect_data = inspect_container((const char *)Id.c_str(), INSPECT_TIMEOUT_SEC, with_host_config);
if (inspect_data == nullptr) {
err.Errorf("Failed to call inspect service %s", Id.c_str());
}
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 0a9535a1..4518e3ac 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -123,6 +123,9 @@ int malloc_trim(size_t pad);
#define TIME_STR_SIZE 512
+// client inspect container timeout
+#define INSPECT_TIMEOUT_SEC 120
+
// native umask value
#define ANNOTATION_UMAKE_KEY "native.umask"
#define UMASK_NORMAL "normal"
--
2.25.1

View File

@ -1,364 +0,0 @@
From 484852b127dab5f5548ed34d5bb668b18e4dc99e Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Fri, 9 Dec 2022 07:01:31 +0000
Subject: [PATCH 58/65] !1757 add adaption code for musl. * add adaption code
for musl.
---
cmake/checker.cmake | 2 +-
cmake/options.cmake | 12 ++++++++++++
cmake/set_build_flags.cmake | 5 +++++
src/CMakeLists.txt | 6 +++---
src/daemon/common/selinux_label.c | 2 +-
src/daemon/common/sysinfo.c | 4 ++--
src/daemon/executor/container_cb/execution_stream.c | 2 +-
src/daemon/modules/image/CMakeLists.txt | 2 +-
src/daemon/modules/image/image.c | 6 ++++--
src/daemon/modules/image/image_rootfs_handler.c | 12 ++++++------
src/utils/cutils/CMakeLists.txt | 2 +-
src/utils/cutils/utils.c | 13 +++++++++----
src/utils/cutils/utils.h | 2 +-
src/utils/cutils/utils_verify.c | 8 ++++----
14 files changed, 51 insertions(+), 27 deletions(-)
diff --git a/cmake/checker.cmake b/cmake/checker.cmake
index cbcfc929..fea4f925 100644
--- a/cmake/checker.cmake
+++ b/cmake/checker.cmake
@@ -54,7 +54,7 @@ find_library(CRYPTO_LIBRARY crypto
HINTS ${PC_CRYPTO_LIBDIR} ${PC_LIBCRYPTO_LIBRARY_DIRS})
_CHECK(CRYPTO_LIBRARY "CRYPTO_LIBRARY-NOTFOUND" "libcrypto.so")
-if (ANDROID)
+if (ANDROID OR MUSL)
# check libssl
find_library(LIBSSL_LIBRARY ssl)
_CHECK(CRYPTO_LIBRARY "LIBSSL_LIBRARY-NOTFOUND" "libssl.so")
diff --git a/cmake/options.cmake b/cmake/options.cmake
index 7a141fef..0d44868a 100644
--- a/cmake/options.cmake
+++ b/cmake/options.cmake
@@ -115,6 +115,18 @@ if (ENABLE_SUP_GROUPS)
message("${Green}-- Enable sup groups${ColourReset}")
endif()
+option(MUSL "available for musl" OFF)
+if (MUSL)
+ add_definitions(-D__MUSL__)
+ message("${Green}-- Available for MUSL${ColourReset}")
+endif()
+
+option(ANDROID "available for android" OFF)
+if (ANDROID)
+ add_definitions(-D__ANDROID__)
+ message("${Green}-- Available for ANDROID${ColourReset}")
+endif()
+
if (NOT RUNPATH)
set(RUNPATH "/var/run")
endif()
diff --git a/cmake/set_build_flags.cmake b/cmake/set_build_flags.cmake
index fa6f38c0..89c9468c 100644
--- a/cmake/set_build_flags.cmake
+++ b/cmake/set_build_flags.cmake
@@ -9,6 +9,11 @@ endif()
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -shared -pthread")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -pie -rdynamic")
+if (NOT DISABLE_WERROR)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")
+endif()
+
if (ISULAD_GCOV)
set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 65bcb978..f3dd3c19 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -102,7 +102,7 @@ add_executable(isula
)
target_include_directories(isula PUBLIC ${ISULA_INCS} ${SHARED_INCS})
target_link_libraries(isula libisula ${LIBYAJL_LIBRARY})
-if (ANDROID)
+if (ANDROID OR MUSL)
target_link_libraries(isula ${LIBSSL_LIBRARY})
else()
target_link_libraries(isula -lpthread)
@@ -115,7 +115,7 @@ add_executable(isulad-shim
)
target_include_directories(isulad-shim PUBLIC ${ISULAD_SHIM_INCS} ${SHARED_INCS})
target_link_libraries(isulad-shim libisulad_tools)
-if (ANDROID)
+if (ANDROID OR MUSL)
target_link_libraries(isulad-shim ${LIBSSL_LIBRARY} ${LIBYAJL_LIBRARY})
else()
target_link_libraries(isulad-shim -lpthread)
@@ -181,7 +181,7 @@ else()
endif()
target_link_libraries(isulad libisulad_tools libhttpclient -ldl)
-if (ANDROID)
+if (ANDROID OR MUSL)
target_link_libraries(isulad ${LIBSSL_LIBRARY} ${LIBYAJL_LIBRARY})
else()
target_link_libraries(isulad -lpthread)
diff --git a/src/daemon/common/selinux_label.c b/src/daemon/common/selinux_label.c
index 145e4b6e..24294780 100644
--- a/src/daemon/common/selinux_label.c
+++ b/src/daemon/common/selinux_label.c
@@ -592,7 +592,7 @@ static int container_label(char **process_label, char **file_label)
return 0;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
lxc_path = ISULAD_DAEMON_CONTAINER_CONTEXTS;
#else
lxc_path = selinux_lxc_contexts_path();
diff --git a/src/daemon/common/sysinfo.c b/src/daemon/common/sysinfo.c
index d52f8767..e7b3807b 100644
--- a/src/daemon/common/sysinfo.c
+++ b/src/daemon/common/sysinfo.c
@@ -1308,7 +1308,7 @@ out:
return ret;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
static bool cgroup2_no_controller()
{
char *controllers_str = NULL;
@@ -1336,7 +1336,7 @@ static int make_sure_cgroup2_isulad_path_exist()
return -1;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
if (cgroup2_no_controller()) {
DEBUG("no cgroup controller found");
return 0;
diff --git a/src/daemon/executor/container_cb/execution_stream.c b/src/daemon/executor/container_cb/execution_stream.c
index 9af6fb5c..ebb9ee2b 100644
--- a/src/daemon/executor/container_cb/execution_stream.c
+++ b/src/daemon/executor/container_cb/execution_stream.c
@@ -63,7 +63,7 @@
#include "utils_file.h"
#include "utils_verify.h"
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
#define SIG_CANCEL_SIGNAL SIGUSR1
#define PTHREAD_CANCEL_ENABLE 1
#define PTHREAD_CANCEL_DISABLE 0
diff --git a/src/daemon/modules/image/CMakeLists.txt b/src/daemon/modules/image/CMakeLists.txt
index 329d2937..6f9e9936 100644
--- a/src/daemon/modules/image/CMakeLists.txt
+++ b/src/daemon/modules/image/CMakeLists.txt
@@ -117,7 +117,7 @@ target_link_libraries(${LIB_ISULAD_IMG}
${ZLIB_LIBRARY}
libhttpclient)
-if (NOT ANDROID)
+if ((NOT ANDROID) AND (NOT MUSL))
target_link_libraries(${LIB_ISULAD_IMG} -lpthread)
endif()
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index fb0db361..8fb226aa 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -1773,7 +1773,8 @@ int im_container_export(const im_export_request *request)
#endif
#ifdef ENABLE_OCI_IMAGE
-char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request)
+{
char *dir = NULL;
struct bim *bim = NULL;
@@ -1801,7 +1802,8 @@ out:
return dir;
}
#else
-char *im_get_rootfs_dir(const im_get_rf_dir_request *request) {
+char *im_get_rootfs_dir(const im_get_rf_dir_request *request)
+{
return NULL;
}
#endif
diff --git a/src/daemon/modules/image/image_rootfs_handler.c b/src/daemon/modules/image/image_rootfs_handler.c
index a76363d0..c3964b2c 100644
--- a/src/daemon/modules/image/image_rootfs_handler.c
+++ b/src/daemon/modules/image/image_rootfs_handler.c
@@ -87,7 +87,7 @@ static int proc_by_fpasswd(FILE *f_passwd, const char *user, defs_process_user *
struct passwd *pwbufp = NULL;
if (f_passwd != NULL) {
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
errval = util_getpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
#else
errval = fgetpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
@@ -105,7 +105,7 @@ static int proc_by_fpasswd(FILE *f_passwd, const char *user, defs_process_user *
*matched_username = util_strdup_s(pwbufp->pw_name);
break;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
errval = util_getpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
#else
errval = fgetpwent_r(f_passwd, &pw, buf, sizeof(buf), &pwbufp);
@@ -215,7 +215,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
return 0;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
#else
errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -226,7 +226,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
if (search_group_list(gbufp, matched_username, puser) != 0) {
return -1;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
#else
errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -240,7 +240,7 @@ static int do_proc_by_froup(FILE *f_group, const char *group, defs_process_user
puser->gid = gbufp->gr_gid;
*groupcnt = 1;
}
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
errval = util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
#else
errval = fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp);
@@ -378,7 +378,7 @@ static int get_additional_groups(char **additional_groups, size_t additional_gro
struct group *gbufp = NULL;
struct group *groups = NULL;
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
while (f_group != NULL && util_getgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp) == 0) {
#else
while (f_group != NULL && fgetgrent_r(f_group, &grp, buf, sizeof(buf), &gbufp) == 0) {
diff --git a/src/utils/cutils/CMakeLists.txt b/src/utils/cutils/CMakeLists.txt
index 30414d91..50a17f60 100644
--- a/src/utils/cutils/CMakeLists.txt
+++ b/src/utils/cutils/CMakeLists.txt
@@ -2,7 +2,7 @@
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} local_cutils_srcs)
add_subdirectory(map)
-if (NOT ANDROID)
+if ((NOT ANDROID) AND (NOT MUSL))
list(REMOVE_ITEM local_cutils_srcs "${CMAKE_CURRENT_SOURCE_DIR}/utils_pwgr.c")
endif()
diff --git a/src/utils/cutils/utils.c b/src/utils/cutils/utils.c
index a154c52a..b1db045a 100644
--- a/src/utils/cutils/utils.c
+++ b/src/utils/cutils/utils.c
@@ -16,7 +16,7 @@
#define _GNU_SOURCE
#include "utils.h"
#include <errno.h>
-#ifndef __ANDROID__
+#if !defined (__ANDROID__) && !defined(__MUSL__)
#include <execinfo.h>
#endif
#include <stdint.h>
@@ -47,7 +47,7 @@
#include "utils_string.h"
#include "utils_verify.h"
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
int mallopt(int param, int value)
{
return 1;
@@ -427,7 +427,12 @@ proc_t *util_stat2proc(const char *s, size_t len)
/* parse these two strings separately, skipping the leading "(". */
/* comm[16] in kernel */
+ /* https://www.openwall.com/lists/musl/2013/11/15/5: musl's sscanf("%15c",cmd) requires exactly 15 characters; anything shorter is a matching failure. */
+#ifdef __MUSL__
+ num = sscanf(s, "%d (%15s", &p->pid, p->cmd);
+#else
num = sscanf(s, "%d (%15c", &p->pid, p->cmd);
+#endif
if (num != 2) {
ERROR("Call sscanf error: %s", errno ? strerror(errno) : "");
free(p);
@@ -851,8 +856,8 @@ out:
char **util_get_backtrace(void)
{
-#ifdef __ANDROID__
- /* android has no backtrace */
+#if defined (__ANDROID__) || defined(__MUSL__)
+ /* android and musl has no backtrace */
return NULL;
#else
#define BACKTRACE_SIZE 16
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 4518e3ac..72cab9f2 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -47,7 +47,7 @@
extern "C" {
#endif
-#ifdef __ANDROID__
+#if defined (__ANDROID__) || defined(__MUSL__)
#define M_TRIM_THRESHOLD -1
#define M_TOP_PAD -2
#define M_MMAP_THRESHOLD -3
diff --git a/src/utils/cutils/utils_verify.c b/src/utils/cutils/utils_verify.c
index 9ed33bf3..713e72c3 100644
--- a/src/utils/cutils/utils_verify.c
+++ b/src/utils/cutils/utils_verify.c
@@ -563,7 +563,7 @@ bool util_valid_value_false(const char *value)
bool util_valid_rw_mode(const char *mode)
{
- if (mode == NULL){
+ if (mode == NULL) {
return false;
}
@@ -572,7 +572,7 @@ bool util_valid_rw_mode(const char *mode)
bool util_valid_label_mode(const char *mode)
{
- if (mode == NULL){
+ if (mode == NULL) {
return false;
}
@@ -581,7 +581,7 @@ bool util_valid_label_mode(const char *mode)
bool util_valid_copy_mode(const char *mode)
{
- if (mode == NULL){
+ if (mode == NULL) {
return false;
}
return !strcmp(mode, "nocopy");
@@ -708,7 +708,7 @@ int util_valid_env(const char *env, char **dst)
int ret = 0;
char *value = NULL;
- if (dst == NULL){
+ if (dst == NULL) {
ERROR("NULL dst");
return -1;
}
--
2.25.1

View File

@ -1,880 +0,0 @@
From edb570b8720aed234bf2c17642f5e6caba2c726a Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Tue, 6 Dec 2022 19:23:33 -0800
Subject: [PATCH 59/65] deleting broken rootfs
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
cmake/options.cmake | 6 ++
src/cmd/isulad/main.c | 15 ++++
src/daemon/modules/api/image_api.h | 2 +
src/daemon/modules/api/leftover_cleanup_api.h | 36 +++++++++
src/daemon/modules/container/CMakeLists.txt | 2 +
src/daemon/modules/container/container_unix.c | 2 -
.../leftover_cleanup/clean_context.c | 78 +++++++++++++++++++
.../leftover_cleanup/clean_context.h | 41 ++++++++++
.../container/leftover_cleanup/cleanup.c | 46 +++++------
.../container/leftover_cleanup/cleanup.h | 12 ++-
.../leftover_cleanup/leftover_cleanup_api.c | 71 +++++++++++++++++
.../leftover_cleanup/oci_rootfs_clean.c | 34 +++++++-
.../leftover_cleanup/oci_rootfs_clean.h | 4 +-
src/daemon/modules/image/image.c | 48 ++++++++++++
src/daemon/modules/image/oci/oci_image.c | 10 +++
src/daemon/modules/image/oci/oci_image.h | 1 +
.../oci/storage/layer_store/layer_store.c | 2 +
.../oci/storage/rootfs_store/rootfs_store.c | 13 +++-
.../modules/image/oci/storage/storage.c | 32 ++++++++
.../modules/image/oci/storage/storage.h | 3 +
test/image/oci/storage/rootfs/CMakeLists.txt | 2 +
21 files changed, 422 insertions(+), 38 deletions(-)
create mode 100644 src/daemon/modules/api/leftover_cleanup_api.h
create mode 100644 src/daemon/modules/container/leftover_cleanup/clean_context.c
create mode 100644 src/daemon/modules/container/leftover_cleanup/clean_context.h
create mode 100644 src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
diff --git a/cmake/options.cmake b/cmake/options.cmake
index 0d44868a..1e63a485 100644
--- a/cmake/options.cmake
+++ b/cmake/options.cmake
@@ -115,6 +115,12 @@ if (ENABLE_SUP_GROUPS)
message("${Green}-- Enable sup groups${ColourReset}")
endif()
+option(DISABLE_CLEANUP "disable cleanup module" OFF)
+if (DISABLE_CLEANUP STREQUAL "ON")
+ add_definitions(-DDISABLE_CLEANUP)
+ message("${Green}-- Disable cleanup module")
+endif()
+
option(MUSL "available for musl" OFF)
if (MUSL)
add_definitions(-D__MUSL__)
diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c
index d4f984d5..9b664bee 100644
--- a/src/cmd/isulad/main.c
+++ b/src/cmd/isulad/main.c
@@ -73,6 +73,9 @@
#include "utils_string.h"
#include "utils_verify.h"
#include "volume_api.h"
+#ifndef DISABLE_CLEANUP
+#include "leftover_cleanup_api.h"
+#endif
#include "opt_log.h"
#ifdef GRPC_CONNECTOR
@@ -1235,6 +1238,14 @@ static int isulad_server_init_common()
goto out;
}
+#ifndef DISABLE_CLEANUP
+ // to cleanup leftover, init clean module before other modules.
+ if (clean_module_init() != 0) {
+ ERROR("Failed to init clean module");
+ goto out;
+ }
+#endif
+
if (volume_init(args->json_confs->graph) != 0) {
ERROR("Failed to init volume");
goto out;
@@ -1451,6 +1462,10 @@ static int start_daemon_threads(char **msg)
goto out;
}
+#ifndef DISABLE_CLEANUP
+ clean_module_do_clean();
+#endif
+
ret = 0;
out:
return ret;
diff --git a/src/daemon/modules/api/image_api.h b/src/daemon/modules/api/image_api.h
index a1c6084a..b8d7fd5b 100644
--- a/src/daemon/modules/api/image_api.h
+++ b/src/daemon/modules/api/image_api.h
@@ -244,6 +244,8 @@ int im_umount_container_rootfs(const char *image_type, const char *image_name, c
int im_remove_container_rootfs(const char *image_type, const char *container_id);
+int im_remove_broken_rootfs(const char *image_type, const char *container_id);
+
int im_merge_image_config(const char *image_type, const char *image_name, container_config *container_spec);
int im_get_user_conf(const char *image_type, const char *basefs, host_config *hc, const char *userstr,
diff --git a/src/daemon/modules/api/leftover_cleanup_api.h b/src/daemon/modules/api/leftover_cleanup_api.h
new file mode 100644
index 00000000..32a41258
--- /dev/null
+++ b/src/daemon/modules/api/leftover_cleanup_api.h
@@ -0,0 +1,36 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-12-7
+ * Description: provide cleanup functions
+ *********************************************************************************/
+#ifndef DAEMON_MODULES_API_LEFTOVER_CLEANUP_API_H
+#define DAEMON_MODULES_API_LEFTOVER_CLEANUP_API_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+typedef enum {
+ BROKEN_ROOTFS = 1
+} cleanup_ctx_data_t;
+
+int clean_module_init();
+
+void clean_module_fill_ctx(cleanup_ctx_data_t data_type, void *data);
+
+void clean_module_do_clean();
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif
diff --git a/src/daemon/modules/container/CMakeLists.txt b/src/daemon/modules/container/CMakeLists.txt
index def602c7..38c3d88b 100644
--- a/src/daemon/modules/container/CMakeLists.txt
+++ b/src/daemon/modules/container/CMakeLists.txt
@@ -5,7 +5,9 @@ add_subdirectory(supervisor)
add_subdirectory(health_check)
add_subdirectory(container_gc)
add_subdirectory(restart_manager)
+IF (NOT DISABLE_CLEANUP)
add_subdirectory(leftover_cleanup)
+ENDIF()
set(MANAGER_SRCS
${local_manager_srcs}
diff --git a/src/daemon/modules/container/container_unix.c b/src/daemon/modules/container/container_unix.c
index 88c4bf51..9910b3c8 100644
--- a/src/daemon/modules/container/container_unix.c
+++ b/src/daemon/modules/container/container_unix.c
@@ -46,7 +46,6 @@
#include "utils_string.h"
#include "volume_api.h"
#include "namespace.h"
-#include "cleanup.h"
static int parse_container_log_configs(container_t *cont);
@@ -1279,7 +1278,6 @@ int container_module_init(char **msg)
}
containers_restore();
- clean_leftover();
if (start_gchandler()) {
*msg = "Failed to start garbage collecotor handler";
diff --git a/src/daemon/modules/container/leftover_cleanup/clean_context.c b/src/daemon/modules/container/leftover_cleanup/clean_context.c
new file mode 100644
index 00000000..6ccc39ed
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/clean_context.c
@@ -0,0 +1,78 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide cleanup definition
+ *********************************************************************************/
+#include "clean_context.h"
+#include "linked_list.h"
+#include "utils.h"
+#include "isula_libutils/log.h"
+
+struct clean_ctx *clean_ctx_init()
+{
+ struct clean_ctx *ctx = util_common_calloc_s(sizeof(struct clean_ctx));
+ if (ctx == NULL) {
+ ERROR("Out of memory");
+ return NULL;
+ }
+
+ linked_list_init(&(ctx->broken_rootfs_list));
+ ctx->inited = true;
+
+ return ctx;
+}
+
+void clean_ctx_destroy(struct clean_ctx *ctx)
+{
+ struct linked_list *it = NULL;
+ struct linked_list *next = NULL;
+ char *id = NULL;
+
+ if (ctx == NULL) {
+ return;
+ }
+
+ if (!ctx->inited) {
+ free(ctx);
+ return;
+ }
+
+ linked_list_for_each_safe(it, &(ctx->broken_rootfs_list), next) {
+ id = (char *)it->elem;
+ linked_list_del(it);
+ free(id);
+ free(it);
+ it = NULL;
+ }
+
+ free(ctx);
+}
+
+void clean_ctx_fill_broken_rootfs(struct clean_ctx *ctx, const char *id)
+{
+ struct linked_list *new_node = NULL;
+ char *broken_id = NULL;
+
+ if (!ctx->inited) {
+ return;
+ }
+
+ new_node = util_common_calloc_s(sizeof(struct linked_list));
+ if (new_node == NULL) {
+ ERROR("Out of memory, broken rootfs %s not added", id);
+ return;
+ }
+
+ broken_id = util_strdup_s(id);
+ linked_list_add_elem(new_node, broken_id);
+ linked_list_add_tail(&ctx->broken_rootfs_list, new_node);
+}
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/clean_context.h b/src/daemon/modules/container/leftover_cleanup/clean_context.h
new file mode 100644
index 00000000..fcac8df8
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/clean_context.h
@@ -0,0 +1,41 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide cleanup definition
+ *********************************************************************************/
+#ifndef DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_CLEAN_CONTEXT_H
+#define DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_CLEAN_CONTEXT_H
+
+#include "linked_list.h"
+#include "utils.h"
+#include "isula_libutils/log.h"
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+struct clean_ctx {
+ bool inited;
+ struct linked_list broken_rootfs_list;
+};
+
+struct clean_ctx *clean_ctx_init();
+
+void clean_ctx_destroy(struct clean_ctx *ctx);
+
+void clean_ctx_fill_broken_rootfs(struct clean_ctx *ctx, const char *id);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.c b/src/daemon/modules/container/leftover_cleanup/cleanup.c
index 9ce1dd0c..664988b5 100644
--- a/src/daemon/modules/container/leftover_cleanup/cleanup.c
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.c
@@ -31,12 +31,16 @@ static struct cleaners *create_cleaners()
return ret;
}
-static void destroy_cleaners(struct cleaners *clns)
+void destroy_cleaners(struct cleaners *clns)
{
struct linked_list *it = NULL;
struct linked_list *next = NULL;
struct clean_node *c_node = NULL;
+ if (clns == NULL) {
+ return;
+ }
+
linked_list_for_each_safe(it, &(clns->cleaner_list), next) {
c_node = (struct clean_node *)it->elem;
linked_list_del(it);
@@ -80,25 +84,31 @@ static int default_cleaner()
return 0;
}
-static struct cleaners *cleaner_init()
+struct cleaners *cleaners_init()
{
int ret = 0;
struct cleaners *clns = create_cleaners();
-
+
if (clns == NULL) {
return NULL;
}
ret = add_clean_node(clns, default_cleaner, "default clean");
if (ret != 0) {
- ERROR("add default_cleaner error");
+ ERROR("Add default_cleaner error");
return clns;
}
#ifdef ENABLE_OCI_IMAGE
+ ret = add_clean_node(clns, oci_broken_rootfs_cleaner, "clean broken rootfs");
+ if (ret != 0) {
+ ERROR("Clean broken rootfs failed");
+ return clns;
+ }
+
ret = add_clean_node(clns, oci_rootfs_cleaner, "clean rootfs");
if (ret != 0) {
- ERROR("add oci_rootfs_cleaner error");
+ ERROR("Add oci_rootfs_cleaner error");
return clns;
}
#endif
@@ -106,7 +116,7 @@ static struct cleaners *cleaner_init()
return clns;
}
-static void do_clean(struct cleaners * clns)
+void cleaners_do_clean(struct cleaners *clns, struct clean_ctx *ctx)
{
struct linked_list *it = NULL;
struct linked_list *next = NULL;
@@ -114,31 +124,11 @@ static void do_clean(struct cleaners * clns)
linked_list_for_each_safe(it, &(clns->cleaner_list), next) {
c_node = (struct clean_node *)it->elem;
- if (c_node->cleaner() != 0) {
- ERROR("failed to clean for: %s", c_node->desc);
+ if (c_node->cleaner(ctx) != 0) {
+ ERROR("Failed to clean for: %s", c_node->desc);
} else {
DEBUG("do clean success for: %s", c_node->desc);
clns->done_clean++;
}
}
}
-
-void clean_leftover()
-{
- struct cleaners *clns = cleaner_init();
-
- if (clns == NULL) {
- ERROR("failed to clean leftovers, because cleaner init error");
- return;
- }
-
- do_clean(clns);
-
- if (clns->count == clns->done_clean) {
- DEBUG("all clean up success");
- } else {
- ERROR("Aim to do %d clean, %d clean sucess\n", clns->count, clns->done_clean);
- }
-
- destroy_cleaners(clns);
-}
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/cleanup.h b/src/daemon/modules/container/leftover_cleanup/cleanup.h
index efae99d0..8dd5e9bd 100644
--- a/src/daemon/modules/container/leftover_cleanup/cleanup.h
+++ b/src/daemon/modules/container/leftover_cleanup/cleanup.h
@@ -12,19 +12,20 @@
* Create: 2022-10-31
* Description: provide cleanup definition
*********************************************************************************/
-#ifndef DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_H
-#define DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_H
+#ifndef DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_CLEANERS_H
+#define DAEMON_MODULES_CONTAINER_LEFTOVER_CLEANUP_CLEANERS_H
#include <stdlib.h>
#include "linked_list.h"
#include "isula_libutils/log.h"
+#include "clean_context.h"
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
-typedef int clean_func_t(void);
+typedef int clean_func_t(struct clean_ctx *ctx);
struct clean_node {
const char *desc;
@@ -38,8 +39,11 @@ struct cleaners {
struct linked_list cleaner_list;
};
+struct cleaners *cleaners_init();
-void clean_leftover();
+void destroy_cleaners(struct cleaners *clns);
+
+void cleaners_do_clean(struct cleaners *clns, struct clean_ctx *ctx);
#if defined(__cplusplus) || defined(c_plusplus)
diff --git a/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
new file mode 100644
index 00000000..7bdaef22
--- /dev/null
+++ b/src/daemon/modules/container/leftover_cleanup/leftover_cleanup_api.c
@@ -0,0 +1,71 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2018-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ * http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: wangrunze
+ * Create: 2022-10-31
+ * Description: provide cleanup functions
+ *********************************************************************************/
+#include "utils.h"
+#include "leftover_cleanup_api.h"
+#include "cleanup.h"
+#include "clean_context.h"
+
+struct clean_ctx *g_clean_ctx = NULL;
+struct cleaners *g_clns = NULL;
+
+int clean_module_init()
+{
+ // create cleaners and clean_ctx
+ g_clns = cleaners_init();
+ if (g_clns == NULL) {
+ ERROR("Failed to init clean module");
+ return -1;
+ }
+
+ g_clean_ctx = clean_ctx_init();
+ if (g_clean_ctx == NULL) {
+ ERROR("Failed to init clean module");
+ destroy_cleaners(g_clns);
+ return -1;
+ }
+
+ return 0;
+}
+
+void clean_module_fill_ctx(cleanup_ctx_data_t data_type, void *data)
+{
+ switch (data_type) {
+ case BROKEN_ROOTFS:
+ clean_ctx_fill_broken_rootfs(g_clean_ctx, data);
+ break;
+ }
+}
+
+void clean_module_do_clean()
+{
+ if (g_clns == NULL || g_clean_ctx == NULL) {
+ return;
+ }
+
+ cleaners_do_clean(g_clns, g_clean_ctx);
+
+ if (g_clns->count == g_clns->done_clean) {
+ DEBUG("all clean up success");
+ } else {
+ ERROR("Aim to do %d clean, %d clean sucess\n", g_clns->count, g_clns->done_clean);
+ }
+
+ destroy_cleaners(g_clns);
+ clean_ctx_destroy(g_clean_ctx);
+
+ g_clns = NULL;
+ g_clean_ctx = NULL;
+}
+
diff --git a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
index fbef4ce0..b2205569 100644
--- a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
+++ b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.c
@@ -19,6 +19,7 @@
#include "image_api.h"
#include "utils_file.h"
#include "utils.h"
+#include "linked_list.h"
struct cb_result {
int clean_err_cnt;
@@ -45,7 +46,7 @@ static bool walk_dir_cb(const char *path_name, const struct dirent *sub_dir, voi
}
-int oci_rootfs_cleaner(void)
+int oci_rootfs_cleaner(struct clean_ctx *ctx)
{
struct cb_result res = { 0 };
im_get_rf_dir_request request = { 0 };
@@ -67,7 +68,36 @@ int oci_rootfs_cleaner(void)
if (res.clean_err_cnt == 0) {
return 0;
- }
+ }
return -1;
}
+
+int oci_broken_rootfs_cleaner(struct clean_ctx *ctx)
+{
+ int rm_fail_cnt = 0;
+ struct linked_list *it = NULL;
+ struct linked_list *next = NULL;
+ char *id = NULL;
+
+ if (ctx == NULL) {
+ return -1;
+ }
+
+ linked_list_for_each_safe(it, &(ctx->broken_rootfs_list), next) {
+ id = (char *)it->elem;
+ if (im_remove_broken_rootfs(IMAGE_TYPE_OCI, id) != 0) {
+ ERROR("Failed to clean broken rootfs %s", id);
+ rm_fail_cnt++;
+ } else {
+ EVENT("clean broken rootfs succeed %s", id);
+ }
+ }
+
+ if (rm_fail_cnt != 0) {
+ DEBUG("can't clean some broken rootfs, %d left", rm_fail_cnt);
+ return -1;
+ }
+
+ return 0;
+}
\ No newline at end of file
diff --git a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
index 8dff351f..88920fa9 100644
--- a/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
+++ b/src/daemon/modules/container/leftover_cleanup/oci_rootfs_clean.h
@@ -21,7 +21,9 @@
extern "C" {
#endif
-int oci_rootfs_cleaner(void);
+int oci_rootfs_cleaner(struct clean_ctx *ctx);
+
+int oci_broken_rootfs_cleaner(struct clean_ctx *ctx);
#if defined(__cplusplus) || defined(c_plusplus)
}
diff --git a/src/daemon/modules/image/image.c b/src/daemon/modules/image/image.c
index 8fb226aa..6fac7cc4 100644
--- a/src/daemon/modules/image/image.c
+++ b/src/daemon/modules/image/image.c
@@ -50,6 +50,7 @@ struct bim_ops {
int (*export_rf)(const im_export_request *request);
char *(*resolve_image_name)(const char *image_name);
char *(*get_dir_rf)(void);
+ int (*delete_broken_rf)(const im_delete_rootfs_request *request);
/* merge image config ops */
int (*merge_conf)(const char *img_name, container_config *container_spec);
@@ -132,6 +133,7 @@ static const struct bim_ops g_embedded_ops = {
.mount_rf = embedded_mount_rf,
.umount_rf = embedded_umount_rf,
.delete_rf = embedded_delete_rf,
+ .delete_broken_rf = NULL,
.export_rf = NULL,
.get_dir_rf = NULL,
@@ -167,6 +169,7 @@ static const struct bim_ops g_oci_ops = {
.mount_rf = oci_mount_rf,
.umount_rf = oci_umount_rf,
.delete_rf = oci_delete_rf,
+ .delete_broken_rf = oci_delete_broken_rf,
.export_rf = oci_export_rf,
.get_dir_rf = oci_get_dir_rf,
@@ -201,6 +204,7 @@ static const struct bim_ops g_ext_ops = {
.mount_rf = ext_mount_rf,
.umount_rf = ext_umount_rf,
.delete_rf = ext_delete_rf,
+ .delete_broken_rf = NULL,
.export_rf = NULL,
.get_dir_rf = NULL,
@@ -464,6 +468,50 @@ out:
return ret;
}
+int im_remove_broken_rootfs(const char *image_type, const char *container_id)
+{
+ int ret = 0;
+ im_delete_rootfs_request *request = NULL;
+ struct bim *bim = NULL;
+
+ if (container_id == NULL || image_type == NULL) {
+ ERROR("Invalid input arguments");
+ return -1;
+ }
+
+ bim = bim_get(image_type, NULL, NULL, container_id);
+ if (bim == NULL) {
+ ERROR("Failed to init bim when deleting broken rootfs %s", container_id);
+ return -1;
+ }
+
+ if (bim->ops->delete_broken_rf == NULL) {
+ ERROR("Unimplements delete in %s", bim->type);
+ ret = -1;
+ goto out;
+ }
+
+ request = util_common_calloc_s(sizeof(im_delete_rootfs_request));
+ if (request == NULL) {
+ ERROR("Out of memory");
+ ret = -1;
+ goto out;
+ }
+ request->name_id = util_strdup_s(container_id);
+
+ ret = bim->ops->delete_broken_rf(request);
+ if (ret != 0) {
+ ERROR("Failed to delete rootfs for container %s", container_id);
+ ret = -1;
+ goto out;
+ }
+
+out:
+ bim_put(bim);
+ free_im_delete_request(request);
+ return ret;
+}
+
int im_remove_container_rootfs(const char *image_type, const char *container_id)
{
int ret = 0;
diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c
index e951adb4..0fc0b7fe 100644
--- a/src/daemon/modules/image/oci/oci_image.c
+++ b/src/daemon/modules/image/oci/oci_image.c
@@ -368,6 +368,16 @@ out:
return ret;
}
+int oci_delete_broken_rf(const im_delete_rootfs_request *request)
+{
+ if (request == NULL) {
+ ERROR("Request is NULL");
+ return -1;
+ }
+
+ return storage_broken_rw_layer_delete(request->name_id);
+}
+
int oci_delete_rf(const im_delete_rootfs_request *request)
{
if (request == NULL) {
diff --git a/src/daemon/modules/image/oci/oci_image.h b/src/daemon/modules/image/oci/oci_image.h
index aeeb3b65..cd7da336 100644
--- a/src/daemon/modules/image/oci/oci_image.h
+++ b/src/daemon/modules/image/oci/oci_image.h
@@ -53,6 +53,7 @@ int oci_merge_conf_rf(const char *img_name, container_config *container_spec);
int oci_mount_rf(const im_mount_request *request);
int oci_umount_rf(const im_umount_request *request);
int oci_delete_rf(const im_delete_rootfs_request *request);
+int oci_delete_broken_rf(const im_delete_rootfs_request *request);
int oci_export_rf(const im_export_request *request);
char *oci_get_dir_rf(void);
int oci_container_filesystem_usage(const im_container_fs_usage_request *request, imagetool_fs_info **fs_usage);
diff --git a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
index e563a8ef..b9ab0d65 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
@@ -557,6 +557,7 @@ static int update_layer_datas(const char *id, const struct layer_opts *opts, lay
slayer->id = util_strdup_s(id);
slayer->parent = util_strdup_s(opts->parent);
+ slayer->writable = opts->writable;
if (opts->opts != NULL) {
slayer->mountlabel = util_strdup_s(opts->opts->mount_label);
}
@@ -1434,6 +1435,7 @@ static void copy_json_to_layer(const layer_t *jl, struct layer *l)
l->mount_point = util_strdup_s(jl->smount_point->path);
l->mount_count = jl->smount_point->count;
}
+ l->writable = jl->slayer->writable;
}
int layer_store_list(struct layer_list *resp)
diff --git a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
index 97cc39e8..c6e305a0 100644
--- a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
+++ b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
@@ -38,6 +38,10 @@
#include "utils_string.h"
#include "utils_timestamp.h"
+#ifndef DISABLE_CLEANUP
+#include "leftover_cleanup_api.h"
+#endif
+
#define CONTAINER_JSON "container.json"
typedef struct rootfs_store {
@@ -170,6 +174,7 @@ static int append_container_by_directory(const char *container_dir)
nret = snprintf(container_path, sizeof(container_path), "%s/%s", container_dir, CONTAINER_JSON);
if (nret < 0 || (size_t)nret >= sizeof(container_path)) {
+ // snprintf error, not append, but outside should not delete the rootfs
ERROR("Failed to get container path");
return -1;
}
@@ -182,6 +187,7 @@ static int append_container_by_directory(const char *container_dir)
}
if (do_append_container(c) != 0) {
+ // append error should not return -1, outside should not remove rootfs
ERROR("Failed to append container");
ret = -1;
goto out;
@@ -199,6 +205,7 @@ static int get_containers_from_json()
{
int ret = 0;
int nret;
+ int append_ret = 0;
char **container_dirs = NULL;
size_t container_dirs_num = 0;
size_t i;
@@ -231,7 +238,11 @@ static int get_containers_from_json()
continue;
}
- if (append_container_by_directory(container_path) != 0) {
+ append_ret = append_container_by_directory(container_path);
+ if (append_ret != 0) {
+#ifndef DISABLE_CLEANUP
+ clean_module_fill_ctx(BROKEN_ROOTFS, (void *)container_dirs[i]);
+#endif
ERROR("Found container path but load json failed: %s, deleting...", container_path);
if (util_recursive_rmdir(container_path, 0) != 0) {
ERROR("Failed to delete rootfs directory : %s", container_path);
diff --git a/src/daemon/modules/image/oci/storage/storage.c b/src/daemon/modules/image/oci/storage/storage.c
index 6cb4a51b..829ea8d0 100644
--- a/src/daemon/modules/image/oci/storage/storage.c
+++ b/src/daemon/modules/image/oci/storage/storage.c
@@ -306,6 +306,38 @@ struct layer *storage_layer_get(const char *layer_id)
return layer_store_lookup(layer_id);
}
+int storage_broken_rw_layer_delete(const char *layer_id)
+{
+ int ret = 0;
+ struct layer *layer_info = NULL;
+
+ if (layer_id == NULL) {
+ return -1;
+ }
+
+ layer_info = layer_store_lookup(layer_id);
+ if (layer_info == NULL) {
+ ERROR("Failed to get layer info for layer %s", layer_id);
+ return -1;
+ }
+
+ if (!layer_info->writable) {
+ ERROR("Broken rootfs should only delete rw layer, layer %s is ro layer", layer_id);
+ ret = -1;
+ goto out;
+ }
+
+ // delete rootfs and rw layer, rw layer has the same name as rootfs
+ if (layer_store_delete(layer_info->id) != 0) {
+ ERROR("Can't delete layer of broken rootfs");
+ ret = -1;
+ }
+
+out:
+ free_layer(layer_info);
+ return ret;
+}
+
void free_layer(struct layer *ptr)
{
if (ptr == NULL) {
diff --git a/src/daemon/modules/image/oci/storage/storage.h b/src/daemon/modules/image/oci/storage/storage.h
index 5914adec..3ec47959 100644
--- a/src/daemon/modules/image/oci/storage/storage.h
+++ b/src/daemon/modules/image/oci/storage/storage.h
@@ -42,6 +42,7 @@ struct layer {
int64_t compress_size;
char *uncompressed_digest;
int64_t uncompress_size;
+ bool writable;
};
struct layer_list {
@@ -163,6 +164,8 @@ struct layer_list *storage_layers_get_by_compress_digest(const char *digest);
struct layer *storage_layer_get(const char *layer_id);
+int storage_broken_rw_layer_delete(const char *layer_id);
+
int storage_layer_try_repair_lowers(const char *layer_id, const char *last_layer_id);
void free_layer(struct layer *l);
diff --git a/test/image/oci/storage/rootfs/CMakeLists.txt b/test/image/oci/storage/rootfs/CMakeLists.txt
index 8da8196b..5bf568f9 100644
--- a/test/image/oci/storage/rootfs/CMakeLists.txt
+++ b/test/image/oci/storage/rootfs/CMakeLists.txt
@@ -2,6 +2,8 @@ project(iSulad_UT)
SET(EXE storage_rootfs_ut)
+add_definitions(-DDISABLE_CLEANUP)
+
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/utils/cutils/utils_regex.c
--
2.25.1

View File

@ -1,175 +0,0 @@
From 9b41a8d28d3ffbe33eb84d8e254b603012b22b34 Mon Sep 17 00:00:00 2001
From: DriedYellowPeach <wangrunze13@huawei.com>
Date: Sun, 11 Dec 2022 11:12:55 +0000
Subject: [PATCH 60/65] !1761 fix leftover devicemapper mnt dir * fix leftover
devicemapper mnt dir
---
.../graphdriver/devmapper/deviceset.c | 37 +++++++++++++
.../graphdriver/devmapper/deviceset.h | 2 +
.../graphdriver/devmapper/driver_devmapper.c | 55 ++++++++++++-------
.../oci/storage/layer_store/layer_store.c | 11 +++-
4 files changed, 84 insertions(+), 21 deletions(-)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
index 4dadc336..b157510a 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.c
@@ -486,6 +486,43 @@ out:
return exist;
}
+// return true if find the metadata
+// or the argument is wrong
+// or can't decide
+bool has_metadata(const char *hash, struct device_set *devset)
+{
+ char metadata_file[PATH_MAX] = { 0 };
+ char *metadata_path = NULL;
+ bool ret = true;
+ int nret = 0;
+
+ if (hash == NULL) {
+ return true;
+ }
+
+ metadata_path = metadata_dir(devset);
+ if (metadata_path == NULL) {
+ ERROR("Failed to get meta data directory");
+ goto out;
+ }
+
+ nret = snprintf(metadata_file, sizeof(metadata_file), "%s/%s", metadata_path, util_valid_str(hash) ? hash : "base");
+ if (nret < 0 || (size_t)nret >= sizeof(metadata_file)) {
+ ERROR("Failed to snprintf metadata file path with hash:%s, path is too long", hash);
+ goto out;
+ }
+
+ if (!util_file_exists(metadata_file)) {
+ WARN("No such file:%s, need not to load", metadata_file);
+ ret = false;
+ goto out;
+ }
+
+out:
+ free(metadata_path);
+ return ret;
+}
+
static image_devmapper_device_info *load_metadata(const struct device_set *devset, const char *hash)
{
image_devmapper_device_info *info = NULL;
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.h b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.h
index c11eece6..ec985e40 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.h
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/deviceset.h
@@ -83,6 +83,8 @@ int delete_device(const char *hash, bool sync_delete, struct device_set *devset)
int export_device_metadata(struct device_metadata *dev_metadata, const char *hash, struct device_set *devset);
+bool has_metadata(const char *hash, struct device_set *devset);
+
struct status *device_set_status(struct device_set *devset);
void free_devmapper_status(struct status *st);
diff --git a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
index dd231bd6..c83d3e54 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/graphdriver/devmapper/driver_devmapper.c
@@ -144,28 +144,11 @@ int devmapper_create_ro(const char *id, const char *parent, const struct graphdr
return do_create(id, parent, driver, create_opts);
}
-// Remove removes a device with a given id, unmounts the filesystem.
-int devmapper_rm_layer(const char *id, const struct graphdriver *driver)
+static int devmapper_try_rm_layer_mnt(const char *id, const struct graphdriver *driver)
{
+ int ret = 0;
char *mnt_parent_dir = NULL;
char *mnt_point_dir = NULL;
- int ret = 0;
-
- if (!util_valid_str(id) || driver == NULL) {
- ERROR("invalid argument");
- return -1;
- }
-
- if (!has_device(id, driver->devset)) {
- DEBUG("Device with id:%s is not exist", id);
- goto out;
- }
-
- if (delete_device(id, false, driver->devset) != 0) {
- ERROR("failed to remove device %s", id);
- ret = -1;
- goto out;
- }
mnt_parent_dir = util_path_join(driver->home, "mnt");
if (mnt_parent_dir == NULL) {
@@ -193,6 +176,40 @@ out:
return ret;
}
+// Remove removes a device with a given id, unmounts the filesystem.
+int devmapper_rm_layer(const char *id, const struct graphdriver *driver)
+{
+ if (!util_valid_str(id) || driver == NULL) {
+ ERROR("invalid argument");
+ return -1;
+ }
+
+ if (!has_device(id, driver->devset)) {
+ DEBUG("Device with id:%s is not exist", id);
+ if (!has_metadata(id, driver->devset)) {
+ // this means metadata is lost
+ // if we can rm mnt, then the layer is removed
+ EVENT("try clean lost metadata and its mnt: %s", id);
+ return devmapper_try_rm_layer_mnt(id, driver);
+ }
+ // if has_metadata and not rm successfully, return -1
+ // so next start up of isulad will retry delete the layer.
+ return -1;
+ }
+
+ if (delete_device(id, false, driver->devset) != 0) {
+ ERROR("failed to remove device %s", id);
+ return -1;
+ }
+
+ if (devmapper_try_rm_layer_mnt(id, driver) != 0) {
+ ERROR("failed to remove mnt dir of Device: %s", id);
+ return -1;
+ }
+
+ return 0;
+}
+
// devmapper_mount_layer mounts a device with given id into the root filesystem
char *devmapper_mount_layer(const char *id, const struct graphdriver *driver,
const struct driver_mount_opts *mount_opts)
diff --git a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
index b9ab0d65..60aaff22 100644
--- a/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
+++ b/src/daemon/modules/image/oci/storage/layer_store/layer_store.c
@@ -1813,8 +1813,15 @@ static bool load_layer_json_cb(const char *path_name, const struct dirent *sub_d
remove_invalid_dir:
(void)graphdriver_umount_layer(sub_dir->d_name);
- (void)graphdriver_rm_layer(sub_dir->d_name);
- (void)util_recursive_rmdir(tmpdir, 0);
+ // layer not removed successfully, we can't remove layer.json
+ if (graphdriver_rm_layer(sub_dir->d_name) != 0) {
+ ERROR("failed to rm layer: %s when handing invalid rootfs", sub_dir->d_name);
+ goto free_out;
+ }
+ ERROR("tmpdir is %s", tmpdir);
+ if (util_recursive_rmdir(tmpdir, 0) != 0) {
+ ERROR("failed to rm rootfs dir: %s when handing invalid rootfs", tmpdir);
+ }
free_out:
free(rpath);
--
2.25.1

View File

@ -1,113 +0,0 @@
From fa7428d0baf2c310c852b1ece41736b21ea441f9 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Fri, 9 Dec 2022 17:26:10 +0800
Subject: [PATCH 61/65] check file system ro before merge network for
syscontainer
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../executor/container_cb/execution_network.c | 29 +++++++++++++++++++
src/utils/cutils/utils_fs.c | 27 +++++++++++++++++
src/utils/cutils/utils_fs.h | 1 +
3 files changed, 57 insertions(+)
diff --git a/src/daemon/executor/container_cb/execution_network.c b/src/daemon/executor/container_cb/execution_network.c
index bbc35e80..b738d02f 100644
--- a/src/daemon/executor/container_cb/execution_network.c
+++ b/src/daemon/executor/container_cb/execution_network.c
@@ -761,9 +761,38 @@ out:
return ret;
}
+static int check_readonly_fs_for_etc(const char *rootfs, bool *ro)
+{
+ char *path = NULL;
+
+ if (util_realpath_in_scope(rootfs, "/etc", &path) < 0) {
+ SYSERROR("Failed to get real path '/etc' under rootfs '%s'", rootfs);
+ isulad_set_error_message("Failed to get real path '/etc' under rootfs '%s'", rootfs);
+ return -1;
+ }
+
+ *ro = util_check_readonly_fs(path);
+
+ free(path);
+ return 0;
+}
+
+// modify network file in rootfs
+// make sure network file saved if rootfs migrate to another host
static int merge_network_for_syscontainer(const host_config *host_spec, const char *rootfs, const char *hostname)
{
int ret = 0;
+ bool ro = false;
+
+ if (check_readonly_fs_for_etc(rootfs, &ro) != 0) {
+ ERROR("Failed to check network path");
+ return -1;
+ }
+
+ if (ro) {
+ WARN("Readonly filesystem for etc under %s. Skip merge network for syscontainer", rootfs);
+ return 0;
+ }
ret = write_hostname_to_file(rootfs, hostname);
if (ret) {
diff --git a/src/utils/cutils/utils_fs.c b/src/utils/cutils/utils_fs.c
index 218e2fe8..e7165f26 100644
--- a/src/utils/cutils/utils_fs.c
+++ b/src/utils/cutils/utils_fs.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
+#include <sys/statvfs.h>
#include <linux/magic.h>
#include <sys/statfs.h>
#include <dirent.h>
@@ -620,3 +621,29 @@ child_out:
cleanup:
return ret;
}
+
+bool util_check_readonly_fs(const char *path)
+{
+ int i;
+ const int max_retry = 10;
+ struct statfs fsbuf;
+
+ for (i = 0; i < max_retry; i++) {
+ if (statfs(path, &fsbuf) == 0) {
+ break;
+ }
+ if (errno == EINTR) {
+ continue;
+ }
+
+ ERROR("Stat fs failed: %s", strerror(errno));
+ return false;
+ }
+
+ if (i >= max_retry) {
+ ERROR("Too much interrupted");
+ return false;
+ }
+
+ return (fsbuf.f_flags & ST_RDONLY) != 0;
+}
diff --git a/src/utils/cutils/utils_fs.h b/src/utils/cutils/utils_fs.h
index 6ab6b78e..6ad64a1a 100644
--- a/src/utils/cutils/utils_fs.h
+++ b/src/utils/cutils/utils_fs.h
@@ -38,6 +38,7 @@ int util_ensure_mounted_as(const char *dst, const char *mntopts);
int util_mount_from(const char *base, const char *src, const char *dst, const char *mtype, const char *mntopts);
typedef int (*mount_info_call_back_t)(const char *, const char *);
bool util_deal_with_mount_info(mount_info_call_back_t cb, const char *);
+bool util_check_readonly_fs(const char *path);
#ifdef __cplusplus
}
#endif
--
2.25.1

View File

@ -1,134 +0,0 @@
From a2ed1886a091d1edfb54fdec38db277e0d0e29f5 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Tue, 13 Dec 2022 01:34:12 -0800
Subject: [PATCH 62/65] isulad shim wait for all child process
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
src/cmd/isulad-shim/process.c | 18 ++++++++++++++++++
src/daemon/modules/plugin/plugin.c | 2 +-
.../modules/runtime/isula/isula_rt_ops.c | 4 ++--
src/utils/cutils/utils.h | 2 +-
test/cutils/utils_utils/utils_utils_ut.cc | 6 +++---
5 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/src/cmd/isulad-shim/process.c b/src/cmd/isulad-shim/process.c
index c8ce7a44..1fc95525 100644
--- a/src/cmd/isulad-shim/process.c
+++ b/src/cmd/isulad-shim/process.c
@@ -39,6 +39,7 @@
#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)
@@ -1206,10 +1207,20 @@ out:
return ret;
}
+static int try_wait_all_child() {
+ if (waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD) {
+ // all child handled
+ return 0;
+ }
+
+ return 1;
+}
+
int process_signal_handle_routine(process_t *p)
{
int ret = SHIM_ERR;
bool exit_shim = false;
+ int nret = 0;
int i;
for (;;) {
@@ -1235,6 +1246,13 @@ int process_signal_handle_routine(process_t *p)
}
if (exit_shim) {
process_kill_all(p);
+
+ // wait atmost 120 seconds
+ DO_RETRY_CALL(120, 1000000, nret, try_wait_all_child, 0);
+ if (nret != 0) {
+ write_message(g_log_fd, ERR_MSG, "Failed to wait all child after 120 seconds");
+ }
+
process_delete(p);
if (p->exit_fd > 0) {
(void)write_nointr(p->exit_fd, &status, sizeof(int));
diff --git a/src/daemon/modules/plugin/plugin.c b/src/daemon/modules/plugin/plugin.c
index 18035518..53afeeaf 100644
--- a/src/daemon/modules/plugin/plugin.c
+++ b/src/daemon/modules/plugin/plugin.c
@@ -485,7 +485,7 @@ static int pm_register_plugin(const char *name, const char *addr)
goto failed;
}
- DO_RETYR_CALL(PLUGIN_ACTIVATE_MAX_RETRY, 1000000, err, pm_activate_plugin, plugin);
+ DO_RETRY_CALL(PLUGIN_ACTIVATE_MAX_RETRY, 1000000, err, pm_activate_plugin, plugin);
if (err != 0) {
ERROR("active plugin failed");
goto failed;
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index bfe7de08..dd1bb4e8 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -968,13 +968,13 @@ int rt_isula_clean_resource(const char *id, const char *runtime, const rt_clean_
}
// retry 10 count call runtime kill, every call sleep 1s
- DO_RETYR_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
+ DO_RETRY_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
if (nret != 0) {
WARN("call runtime force kill failed");
}
// retry 10 count call runtime delete, every call sleep 1s
- DO_RETYR_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
+ DO_RETRY_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
if (nret != 0) {
WARN("call runtime force delete failed");
}
diff --git a/src/utils/cutils/utils.h b/src/utils/cutils/utils.h
index 72cab9f2..fec6d879 100644
--- a/src/utils/cutils/utils.h
+++ b/src/utils/cutils/utils.h
@@ -392,7 +392,7 @@ int convert_v2_runtime(const char *runtime, char *binary);
* 0 is cb successful at least once;
* 1 is all cb are failure;
*/
-#define DO_RETYR_CALL(retry_cnt, interval_us, ret, cb, ...) do { \
+#define DO_RETRY_CALL(retry_cnt, interval_us, ret, cb, ...) do { \
size_t i = 0; \
for(; i < retry_cnt; i++) { \
ret = cb(__VA_ARGS__); \
diff --git a/test/cutils/utils_utils/utils_utils_ut.cc b/test/cutils/utils_utils/utils_utils_ut.cc
index 5bd98d47..c8f38717 100644
--- a/test/cutils/utils_utils/utils_utils_ut.cc
+++ b/test/cutils/utils_utils/utils_utils_ut.cc
@@ -285,15 +285,15 @@ TEST(utils_utils, test_do_retry_call)
int nret;
global_total = 0;
- DO_RETYR_CALL(10, 100, nret, retry_call_test, 0);
+ DO_RETRY_CALL(10, 100, nret, retry_call_test, 0);
ASSERT_EQ(nret, 0);
ASSERT_EQ(global_total, 0);
global_total = 0;
- DO_RETYR_CALL(10, 100, nret, retry_call_test, 5);
+ DO_RETRY_CALL(10, 100, nret, retry_call_test, 5);
ASSERT_EQ(nret, 0);
ASSERT_EQ(global_total, 5);
global_total = 0;
- DO_RETYR_CALL(10, 100, nret, retry_call_test, 11);
+ DO_RETRY_CALL(10, 100, nret, retry_call_test, 11);
ASSERT_EQ(global_total, 10);
ASSERT_EQ(nret, -1);
}
\ No newline at end of file
--
2.25.1

View File

@ -1,59 +0,0 @@
From 33fb34f3c864161fb24de77b72e157327e1e620c Mon Sep 17 00:00:00 2001
From: zhongtao <taozh97@163.com>
Date: Thu, 15 Dec 2022 15:55:34 +0800
Subject: [PATCH 63/65] When run options rm is set, delete the stoped
container's fifo directory.
Signed-off-by: zhongtao <taozh97@163.com>
---
src/cmd/isula/base/run.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/src/cmd/isula/base/run.c b/src/cmd/isula/base/run.c
index 73486c95..24863120 100644
--- a/src/cmd/isula/base/run.c
+++ b/src/cmd/isula/base/run.c
@@ -42,6 +42,29 @@ struct client_arguments g_cmd_run_args = {
.pull = "missing"
};
+/*
+ * --rm option will result in no time to delete the directory created by the client, resulting in residue.
+ * This function is used to delete the client's fifo file and home directory.
+ */
+static void delete_client_fifo_and_home_dir(const char *rundir, const char *name,
+ struct command_fifo_config *console_fifos)
+{
+ char client_fifo_home_dir[PATH_MAX] = { 0 };
+ int nret = 0;
+
+ nret = snprintf(client_fifo_home_dir, sizeof(client_fifo_home_dir), "%s/%s/", rundir, name);
+ if (nret < 0 || (size_t)nret >= sizeof(client_fifo_home_dir)) {
+ ERROR("Client fifo home path:%s/%s/ is too long.", rundir, name);
+ return;
+ }
+
+ delete_command_fifo(console_fifos);
+
+ if (util_recursive_rmdir(client_fifo_home_dir, 0)) {
+ WARN("Failed to delete client fifo home path:%s", client_fifo_home_dir);
+ }
+}
+
static int local_cmd_start(const struct client_arguments *args)
{
int ret = 0;
@@ -66,6 +89,10 @@ static int local_cmd_start(const struct client_arguments *args)
client_wait_fifo_exit(args);
free_out:
+ if (args->custom_conf.auto_remove && !args->detach) {
+ delete_client_fifo_and_home_dir(CLIENT_RUNDIR, args->name, console_fifos);
+ console_fifos = NULL;
+ }
client_restore_console(reset_tty, &oldtios, console_fifos);
return ret;
}
--
2.25.1

View File

@ -1,153 +0,0 @@
From a8c344ad4400ed876aaa4f53f7c992a7ad1c1580 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Wed, 14 Dec 2022 20:49:34 -0800
Subject: [PATCH 64/65] recheck kill command exit status
Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
.../modules/runtime/isula/isula_rt_ops.c | 70 +++++++++++++++----
1 file changed, 57 insertions(+), 13 deletions(-)
diff --git a/src/daemon/modules/runtime/isula/isula_rt_ops.c b/src/daemon/modules/runtime/isula/isula_rt_ops.c
index dd1bb4e8..dee57263 100644
--- a/src/daemon/modules/runtime/isula/isula_rt_ops.c
+++ b/src/daemon/modules/runtime/isula/isula_rt_ops.c
@@ -54,6 +54,9 @@
#define RESIZE_DATA_SIZE 100
#define PID_WAIT_TIME 120
+// handle string from stderr output.
+typedef int(*handle_output_callback_t)(const char *output);
+
static void copy_process(shim_client_process_state *p, defs_process *dp)
{
p->args = dp->args;
@@ -584,7 +587,7 @@ out:
}
static int runtime_call_simple(const char *workdir, const char *runtime, const char *subcmd, const char **opts,
- size_t opts_len, const char *id)
+ size_t opts_len, const char *id, handle_output_callback_t cb)
{
runtime_exec_info rei = { 0 };
char *stdout = NULL;
@@ -596,24 +599,65 @@ static int runtime_call_simple(const char *workdir, const char *runtime, const c
if (!util_exec_cmd(runtime_exec_func, &rei, NULL, &stdout, &stderr)) {
ERROR("call runtime %s failed stderr %s", subcmd, stderr);
ret = -1;
- goto out;
+ // additional handler for the stderr,
+ // this intend to change the ret val of this function
+ // for example, if output string contains some specific content,
+ // we consider the runtime call simple succeeded,
+ // even if the process exit with failure.
+ if (stderr != NULL && cb != NULL) {
+ ret = cb(stderr);
+ }
}
-out:
UTIL_FREE_AND_SET_NULL(stdout);
UTIL_FREE_AND_SET_NULL(stderr);
return ret;
}
-static int runtime_call_kill_force(const char *workdir, const char *runtime, const char *id)
+// oci runtime return -1 if the container 'does not exist'
+// if output contains 'does not exist', means nothing to kill, return 0
+// this will change the exit status of kill command
+static int kill_output_check(const char *output)
{
- return runtime_call_simple(workdir, runtime, "kill", NULL, 0, id);
+ char *pattern = "does not exist";
+
+ if (output == NULL) {
+ return -1;
+ }
+
+ // container not exist, kill success, return 0
+ if (util_strings_contains_word(output, pattern)) {
+ return 0;
+ }
+
+ // kill failed, return -1
+ return -1;
+}
+
+// kill success or kill_output_check succeed return 0, DO_RETRY_CALL will break;
+// if kill failed, recheck on shim alive, if not alive, kill succeed, still return 0;
+// else, return -1, DO_RETRY_CALL will call this again;
+static int runtime_call_kill_and_check(const char *workdir, const char *runtime, const char *id)
+{
+ int ret = -1;
+
+ // kill succeed, return 0; kill_output_check succeed, return 0;
+ ret = runtime_call_simple(workdir, runtime, "kill", NULL, 0, id, kill_output_check);
+ if (ret == 0) {
+ return 0;
+ }
+
+ if (!shim_alive(workdir)) {
+ ret = 0;
+ }
+
+ return ret;
}
static int runtime_call_delete_force(const char *workdir, const char *runtime, const char *id)
{
const char *opts[1] = { "--force" };
- return runtime_call_simple(workdir, runtime, "delete", opts, 1, id);
+ return runtime_call_simple(workdir, runtime, "delete", opts, 1, id, NULL);
}
#define ExitSignalOffset 128
@@ -919,7 +963,7 @@ int rt_isula_start(const char *id, const char *runtime, const rt_start_params_t
pid_info->ppid = shim_pid;
pid_info->pstart_time = p_proc->start_time;
- if (runtime_call_simple(workdir, runtime, "start", NULL, 0, id) != 0) {
+ if (runtime_call_simple(workdir, runtime, "start", NULL, 0, id, NULL) != 0) {
ERROR("call runtime start id failed");
ret = -1;
goto out;
@@ -967,14 +1011,14 @@ int rt_isula_clean_resource(const char *id, const char *runtime, const rt_clean_
shim_kill_force(workdir);
}
- // retry 10 count call runtime kill, every call sleep 1s
- DO_RETRY_CALL(10, 1000000, nret, runtime_call_kill_force, workdir, runtime, id);
+ // retry 10 count call runtime kill, every call sleep 0.5s
+ DO_RETRY_CALL(10, 500000, nret, runtime_call_kill_and_check, workdir, runtime, id);
if (nret != 0) {
WARN("call runtime force kill failed");
}
- // retry 10 count call runtime delete, every call sleep 1s
- DO_RETRY_CALL(10, 1000000, nret, runtime_call_delete_force, workdir, runtime, id);
+ // retry 10 count call runtime delete, every call sleep 0.1s
+ DO_RETRY_CALL(10, 100000, nret, runtime_call_delete_force, workdir, runtime, id);
if (nret != 0) {
WARN("call runtime force delete failed");
}
@@ -1204,7 +1248,7 @@ int rt_isula_pause(const char *id, const char *runtime, const rt_pause_params_t
return -1;
}
- return runtime_call_simple(workdir, runtime, "pause", NULL, 0, id);
+ return runtime_call_simple(workdir, runtime, "pause", NULL, 0, id, NULL);
}
int rt_isula_resume(const char *id, const char *runtime, const rt_resume_params_t *params)
@@ -1221,7 +1265,7 @@ int rt_isula_resume(const char *id, const char *runtime, const rt_resume_params_
return -1;
}
- return runtime_call_simple(workdir, runtime, "resume", NULL, 0, id);
+ return runtime_call_simple(workdir, runtime, "resume", NULL, 0, id, NULL);
}
int rt_isula_listpids(const char *name, const char *runtime, const rt_listpids_params_t *params, rt_listpids_out_t *out)
--
2.25.1

View File

@ -1,430 +0,0 @@
From a13e021620c62f32dfb1fd5242a3cf43c1d163b8 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Wed, 14 Dec 2022 10:50:49 +0800
Subject: [PATCH 65/65] start sandbox before setup network by default
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
src/daemon/entry/cri/cni_network_plugin.cc | 43 +++---
src/daemon/entry/cri/cni_network_plugin.h | 2 +
src/daemon/entry/cri/cri_helpers.cc | 12 ++
src/daemon/entry/cri/cri_helpers.h | 3 +
.../cri_pod_sandbox_manager_service_impl.cc | 135 +++++++++++-------
.../cri_pod_sandbox_manager_service_impl.h | 2 +-
src/daemon/entry/cri/cri_security_context.cc | 3 +-
src/daemon/modules/spec/specs.c | 2 +-
src/utils/cutils/utils_file.c | 2 +-
9 files changed, 135 insertions(+), 69 deletions(-)
diff --git a/src/daemon/entry/cri/cni_network_plugin.cc b/src/daemon/entry/cri/cni_network_plugin.cc
index c850bc32..976a21a4 100644
--- a/src/daemon/entry/cri/cni_network_plugin.cc
+++ b/src/daemon/entry/cri/cni_network_plugin.cc
@@ -479,6 +479,27 @@ cleanup:
return result;
}
+auto CniNetworkPlugin::GetNetNSPath(const std::string &id, const std::map<std::string, std::string> &annotations,
+ Errors &err) -> std::string
+{
+ std::string netnsPath;
+
+ if (CRIHelpers::SetupNetworkFront(annotations)) {
+ auto iter = annotations.find(CRIHelpers::Constants::POD_SANDBOX_KEY);
+ if (iter == annotations.end()) {
+ ERROR("Failed to find sandbox key from annotations");
+ return netnsPath;
+ }
+ return iter->second;
+ }
+
+ netnsPath = GetNetNS(id, err);
+ if (err.NotEmpty()) {
+ ERROR("CNI failed to retrieve network namespace path: %s", err.GetCMessage());
+ }
+
+ return netnsPath;
+}
void CniNetworkPlugin::SetUpPod(const std::string &ns, const std::string &name, const std::string &interfaceName,
const std::string &id, const std::map<std::string, std::string> &annotations,
@@ -489,12 +510,7 @@ void CniNetworkPlugin::SetUpPod(const std::string &ns, const std::string &name,
return;
}
- auto iter = annotations.find(CRIHelpers::Constants::POD_SANDBOX_KEY);
- if (iter == annotations.end()) {
- ERROR("Failed to find sandbox key from annotations");
- return;
- }
- const std::string netnsPath = iter->second;
+ std::string netnsPath = GetNetNSPath(id, annotations, err);
if (netnsPath.length() == 0) {
ERROR("Failed to get network namespace path");
return;
@@ -600,21 +616,14 @@ void CniNetworkPlugin::TearDownPod(const std::string &ns, const std::string &nam
}
Errors tmpErr;
- auto iter = annotations.find(CRIHelpers::Constants::POD_SANDBOX_KEY);
- if (iter == annotations.end()) {
- ERROR("Failed to find sandbox key from annotations");
- return;
- }
- std::string netnsPath = iter->second;
- if (netnsPath.length() == 0) {
- ERROR("Failed to get network namespace path");
- return;
- }
+ std::string netnsPath = GetNetNSPath(id, annotations, err);
// When netns file does not exist, netnsPath is assigned to an
// empty string so that lxc can handle the path properly
- if (!util_file_exists(netnsPath.c_str())) {
+ if (!util_file_exists(netnsPath.c_str()) || err.NotEmpty()) {
+ ERROR("Failed to get network namespace path, maybe podsandbox '%s' has been stopped", id.c_str());
netnsPath = "";
+ err.Clear();
}
RLockNetworkMap(err);
diff --git a/src/daemon/entry/cri/cni_network_plugin.h b/src/daemon/entry/cri/cni_network_plugin.h
index 8d51a94d..434222b5 100644
--- a/src/daemon/entry/cri/cni_network_plugin.h
+++ b/src/daemon/entry/cri/cni_network_plugin.h
@@ -118,6 +118,8 @@ public:
private:
auto GetNetNS(const std::string &podSandboxID, Errors &err) -> std::string;
+ auto GetNetNSPath(const std::string &id, const std::map<std::string, std::string> &annotations,
+ Errors &err) -> std::string;
private:
virtual void PlatformInit(Errors &error);
diff --git a/src/daemon/entry/cri/cri_helpers.cc b/src/daemon/entry/cri/cri_helpers.cc
index 64cea7ba..711196ba 100644
--- a/src/daemon/entry/cri/cri_helpers.cc
+++ b/src/daemon/entry/cri/cri_helpers.cc
@@ -49,6 +49,8 @@ const std::string Constants::DOCKER_IMAGEID_PREFIX { "docker://" };
const std::string Constants::DOCKER_PULLABLE_IMAGEID_PREFIX { "docker-pullable://" };
const std::string Constants::RUNTIME_READY { "RuntimeReady" };
const std::string Constants::NETWORK_READY { "NetworkReady" };
+// Kata 2.x need create network namespace and setup network befoce run podsandbox
+const std::string Constants::NETWORK_SETUP_ANNOTATION_KEY { "cri.sandbox.network.setup.v2" };
const std::string Constants::POD_CHECKPOINT_KEY { "cri.sandbox.isulad.checkpoint" };
const std::string Constants::CONTAINER_TYPE_ANNOTATION_KEY { "io.kubernetes.cri.container-type" };
const std::string Constants::CONTAINER_NAME_ANNOTATION_KEY { "io.kubernetes.cri.container-name" };
@@ -1009,4 +1011,14 @@ out:
return runtime_val;
}
+bool SetupNetworkFront(const std::map<std::string, std::string> &annotations)
+{
+ auto iter = annotations.find(CRIHelpers::Constants::NETWORK_SETUP_ANNOTATION_KEY);
+ if (iter == annotations.end()) {
+ return false;
+ }
+
+ return iter->second == std::string("true");
+}
+
} // namespace CRIHelpers
diff --git a/src/daemon/entry/cri/cri_helpers.h b/src/daemon/entry/cri/cri_helpers.h
index b3bfafe4..d50759ad 100644
--- a/src/daemon/entry/cri/cri_helpers.h
+++ b/src/daemon/entry/cri/cri_helpers.h
@@ -48,6 +48,7 @@ public:
static const std::string DOCKER_PULLABLE_IMAGEID_PREFIX;
static const std::string RUNTIME_READY;
static const std::string NETWORK_READY;
+ static const std::string NETWORK_SETUP_ANNOTATION_KEY;
static const std::string POD_CHECKPOINT_KEY;
static const size_t MAX_CHECKPOINT_KEY_LEN { 250 };
static const std::string CONTAINER_TYPE_ANNOTATION_KEY;
@@ -151,6 +152,8 @@ void StopContainer(service_executor_t *cb, const std::string &containerID, int64
char *GenerateExecSuffix();
char *cri_runtime_convert(const char *runtime);
+
+bool SetupNetworkFront(const std::map<std::string, std::string> &annotations);
}; // namespace CRIHelpers
#endif // DAEMON_ENTRY_CRI_CRI_HELPERS_H
diff --git a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
index f7b3119d..4c245763 100644
--- a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
+++ b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
@@ -99,10 +99,20 @@ void PodSandboxManagerServiceImpl::ApplySandboxResources(const runtime::v1alpha2
}
-void PodSandboxManagerServiceImpl::SetHostConfigDefaultValue(host_config *hc)
+void PodSandboxManagerServiceImpl::SetHostConfigDefaultValue(const google::protobuf::Map<std::string, std::string> &annotations,
+ host_config *hc)
{
free(hc->network_mode);
- hc->network_mode = util_strdup_s(CRI::Constants::namespaceModeFile.c_str());
+
+ auto iter = annotations.find(CRIHelpers::Constants::NETWORK_SETUP_ANNOTATION_KEY);
+ // set network mode file when kata 2.x
+ if (iter != annotations.end() && iter->second == std::string("true")) {
+ hc->network_mode = util_strdup_s(CRI::Constants::namespaceModeFile.c_str());
+ return;
+ }
+
+ // default network mode is empty string
+ hc->network_mode = util_strdup_s("");
}
void PodSandboxManagerServiceImpl::MakeSandboxIsuladConfig(const runtime::v1alpha2::PodSandboxConfig &c,
@@ -168,7 +178,7 @@ void PodSandboxManagerServiceImpl::MakeSandboxIsuladConfig(const runtime::v1alph
custom_config->hostname = util_strdup_s(c.hostname().c_str());
}
- SetHostConfigDefaultValue(hc);
+ SetHostConfigDefaultValue(c.annotations(), hc);
if (c.has_linux()) {
ApplySandboxLinuxOptions(c.linux(), hc, custom_config, error);
@@ -487,7 +497,6 @@ void PodSandboxManagerServiceImpl::SetupSandboxNetwork(const runtime::v1alpha2::
{
std::map<std::string, std::string> stdAnnos;
std::map<std::string, std::string> networkOptions;
- char* sandbox_key = get_sandbox_key(inspect_data);
// Setup sandbox files
if (config.has_dns_config() && inspect_data->resolv_conf_path != nullptr) {
@@ -495,32 +504,36 @@ void PodSandboxManagerServiceImpl::SetupSandboxNetwork(const runtime::v1alpha2::
SetupSandboxFiles(inspect_data->resolv_conf_path, config, error);
if (error.NotEmpty()) {
ERROR("failed to setup sandbox files");
- goto cleanup;
+ return;
}
}
// Do not invoke network plugins if in hostNetwork mode.
if (config.linux().security_context().namespace_options().network() == runtime::v1alpha2::NamespaceMode::NODE) {
- goto cleanup;
+ return;
}
// Setup networking for the sandbox.
CRIHelpers::ProtobufAnnoMapToStd(config.annotations(), stdAnnos);
stdAnnos[CRIHelpers::Constants::POD_CHECKPOINT_KEY] = jsonCheckpoint;
networkOptions["UID"] = config.metadata().uid();
- if (sandbox_key == NULL) {
- goto cleanup;
+
+ if (namespace_is_file(inspect_data->host_config->network_mode)) {
+ char *sandbox_key = get_sandbox_key(inspect_data);
+ if (sandbox_key == nullptr) {
+ ERROR("Empty sandbox key");
+ error.SetError("Empty sandbox key");
+ return;
+ }
+ stdAnnos.insert(std::pair<std::string, std::string>(CRIHelpers::Constants::POD_SANDBOX_KEY,
+ std::string(sandbox_key)));
+ free(sandbox_key);
}
- stdAnnos.insert(std::pair<std::string, std::string>(CRIHelpers::Constants::POD_SANDBOX_KEY, sandbox_key));
m_pluginManager->SetUpPod(config.metadata().namespace_(), config.metadata().name(),
Network::DEFAULT_NETWORK_INTERFACE_NAME, response_id, stdAnnos, networkOptions, error);
if (error.NotEmpty()) {
ERROR("SetupPod failed: %s", error.GetCMessage());
- StopContainerHelper(response_id, error);
- goto cleanup;
}
-cleanup:
- free(sandbox_key);
return;
}
@@ -568,8 +581,9 @@ auto PodSandboxManagerServiceImpl::RunPodSandbox(const runtime::v1alpha2::PodSan
ERROR("Failed to retrieve inspect data");
goto cleanup;
}
- netnsPath = get_sandbox_key(inspect_data);
+
if (namespace_is_file(inspect_data->host_config->network_mode)) {
+ netnsPath = get_sandbox_key(inspect_data);
if (!util_file_exists(netnsPath) || util_mount_namespace(netnsPath) != 0) {
error.Errorf("Failed to mount network namespace");
ERROR("Failed to mount network namespace");
@@ -578,9 +592,11 @@ auto PodSandboxManagerServiceImpl::RunPodSandbox(const runtime::v1alpha2::PodSan
}
// Step 5: Setup networking for the sandbox.
- SetupSandboxNetwork(config, response_id, jsonCheckpoint, inspect_data, error);
- if (error.NotEmpty()) {
- goto cleanup;
+ if (namespace_is_file(inspect_data->host_config->network_mode)) {
+ SetupSandboxNetwork(config, response_id, jsonCheckpoint, inspect_data, error);
+ if (error.NotEmpty()) {
+ goto cleanup;
+ }
}
// Step 6: Start the sandbox container.
@@ -589,6 +605,15 @@ auto PodSandboxManagerServiceImpl::RunPodSandbox(const runtime::v1alpha2::PodSan
goto cleanup;
}
+ // If netns mode is not file, setup network after start sandbox container
+ if (!namespace_is_file(inspect_data->host_config->network_mode)) {
+ SetupSandboxNetwork(config, response_id, jsonCheckpoint, inspect_data, error);
+ if (error.NotEmpty()) {
+ StopContainerHelper(response_id, error);
+ goto cleanup;
+ }
+ }
+
cleanup:
if (error.Empty()) {
SetNetworkReady(response_id, true, error);
@@ -723,44 +748,58 @@ auto PodSandboxManagerServiceImpl::ClearCniNetwork(const std::string &realSandbo
/*error*/) -> int
{
Errors networkErr;
- container_inspect* inspect_data = nullptr;
+ container_inspect *inspect_data = nullptr;
+ char *netnsPath = nullptr;
+
+ if (hostNetwork) {
+ return 0;
+ }
bool ready = GetNetworkReady(realSandboxID, networkErr);
- if (!hostNetwork && (ready || networkErr.NotEmpty())) {
- Errors pluginErr;
-
- // hostNetwork has indicated network mode which render host config unnecessary
- // so that with_host_config is set to be false.
- inspect_data = CRIHelpers::InspectContainer(realSandboxID, pluginErr, false);
- if (pluginErr.NotEmpty()) {
- ERROR("Failed to inspect container");
- }
+ if (!ready && networkErr.Empty()) {
+ WARN("Network not ready");
+ return 0;
+ }
- char* netnsPath = get_sandbox_key(inspect_data);
- if (netnsPath == nullptr) {
- ERROR("Failed to get network namespace path");
- return 0;
- }
+ Errors pluginErr;
+ inspect_data = CRIHelpers::InspectContainer(realSandboxID, pluginErr, true);
+ if (pluginErr.NotEmpty()) {
+ ERROR("Failed to inspect container");
+ // not return and make sure teardown network
+ }
- stdAnnos.insert(std::pair<std::string, std::string>(CRIHelpers::Constants::POD_SANDBOX_KEY, netnsPath));
- m_pluginManager->TearDownPod(ns, name, Network::DEFAULT_NETWORK_INTERFACE_NAME, realSandboxID, stdAnnos,
- pluginErr);
- if (pluginErr.NotEmpty()) {
- WARN("TearDownPod cni network failed: %s", pluginErr.GetCMessage());
- errlist.push_back(pluginErr.GetMessage());
+ if (inspect_data != nullptr && namespace_is_file(inspect_data->host_config->network_mode)) {
+ netnsPath = get_sandbox_key(inspect_data);
+ if (netnsPath == nullptr) {
+ ERROR("Get sandbox key failed");
+ // not return and make sure teardown network
} else {
- INFO("TearDownPod cni network: success");
- SetNetworkReady(realSandboxID, false, pluginErr);
- if (pluginErr.NotEmpty()) {
- WARN("set network ready: %s", pluginErr.GetCMessage());
- }
- // umount netns when cni removed network successfully
- if (util_umount_namespace(netnsPath) != 0) {
- ERROR("Failed to umount directory %s:%s", netnsPath, strerror(errno));
- }
+ stdAnnos.insert(std::pair<std::string, std::string>(CRIHelpers::Constants::POD_SANDBOX_KEY,
+ std::string(netnsPath)));
}
- free(netnsPath);
}
+
+ m_pluginManager->TearDownPod(ns, name, Network::DEFAULT_NETWORK_INTERFACE_NAME, realSandboxID, stdAnnos,
+ pluginErr);
+ if (pluginErr.NotEmpty()) {
+ WARN("TearDownPod cni network failed: %s", pluginErr.GetCMessage());
+ errlist.push_back(pluginErr.GetMessage());
+ goto out;
+ }
+
+ INFO("TearDownPod cni network: success");
+ SetNetworkReady(realSandboxID, false, pluginErr);
+ if (pluginErr.NotEmpty()) {
+ WARN("set network ready: %s", pluginErr.GetCMessage());
+ }
+ // umount netns when cni removed network successfully
+ if (inspect_data != nullptr && namespace_is_file(inspect_data->host_config->network_mode) &&
+ util_umount_namespace(netnsPath) != 0) {
+ ERROR("Failed to umount directory %s:%s", netnsPath, strerror(errno));
+ }
+
+out:
+ free(netnsPath);
free_container_inspect(inspect_data);
return 0;
}
diff --git a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.h b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.h
index 68a9d919..f7c0aa00 100644
--- a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.h
+++ b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.h
@@ -66,7 +66,7 @@ private:
const std::string &image, std::string &jsonCheckpoint,
const std::string &runtimeHandler,
Errors &error) -> container_create_request *;
- void SetHostConfigDefaultValue(host_config *hc);
+ void SetHostConfigDefaultValue(const google::protobuf::Map<std::string, std::string> &annotations, host_config *hc);
void MakeSandboxIsuladConfig(const runtime::v1alpha2::PodSandboxConfig &c, host_config *hc,
container_config *custom_config, Errors &error);
void ApplySandboxLinuxOptions(const runtime::v1alpha2::LinuxPodSandboxConfig &lc, host_config *hc,
diff --git a/src/daemon/entry/cri/cri_security_context.cc b/src/daemon/entry/cri/cri_security_context.cc
index 8484108c..1cd9287d 100644
--- a/src/daemon/entry/cri/cri_security_context.cc
+++ b/src/daemon/entry/cri/cri_security_context.cc
@@ -162,7 +162,8 @@ static void ModifyHostNetworkOptionForSandbox(const runtime::v1alpha2::Namespace
free(hostConfig->uts_mode);
hostConfig->uts_mode = util_strdup_s(CRI::Constants::namespaceModeHost.c_str());
}
- // Note: default networkMode is file
+
+ // Set networkMode to file or "" in SetHostConfigDefaultValue
}
static void ModifyContainerNamespaceOptions(const runtime::v1alpha2::NamespaceOption &nsOpts,
diff --git a/src/daemon/modules/spec/specs.c b/src/daemon/modules/spec/specs.c
index ec6385a6..138a0989 100644
--- a/src/daemon/modules/spec/specs.c
+++ b/src/daemon/modules/spec/specs.c
@@ -1460,7 +1460,7 @@ static int merge_share_network_namespace(oci_runtime_spec *oci_spec, const host_
int ret = 0;
char *ns_path = NULL;
- if (host_spec->network_mode == NULL) {
+ if (host_spec->network_mode == NULL || strlen(host_spec->network_mode) == 0) {
return 0;
}
diff --git a/src/utils/cutils/utils_file.c b/src/utils/cutils/utils_file.c
index f06f4d49..7b1d5cb6 100644
--- a/src/utils/cutils/utils_file.c
+++ b/src/utils/cutils/utils_file.c
@@ -85,7 +85,7 @@ bool util_file_exists(const char *f)
struct stat buf;
int nret;
- if (f == NULL) {
+ if (f == NULL || strlen(f) == 0) {
return false;
}
--
2.25.1

View File

@ -1,157 +0,0 @@
From 4ad92d9524915bc83294627afe6954f43587e450 Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Thu, 15 Dec 2022 20:42:59 +0800
Subject: [PATCH 66/66] compatibility for manage pods which created by old
iSulad
Signed-off-by: haozi007 <liuhao27@huawei.com>
---
.../cri_pod_sandbox_manager_service_impl.cc | 12 +++-
src/daemon/entry/cri/naming.cc | 66 +++++++++++++++----
src/daemon/entry/cri/naming.h | 2 +-
3 files changed, 66 insertions(+), 14 deletions(-)
diff --git a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
index 4c245763..3e89008a 100644
--- a/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
+++ b/src/daemon/entry/cri/cri_pod_sandbox_manager_service_impl.cc
@@ -1188,7 +1188,11 @@ void PodSandboxManagerServiceImpl::PodSandboxStatusToGRPC(
CRIHelpers::ExtractLabels(inspect->config->labels, *podStatus->mutable_labels());
CRIHelpers::ExtractAnnotations(inspect->config->annotations, *podStatus->mutable_annotations());
- CRINaming::ParseSandboxName(podStatus->annotations(), *podStatus->mutable_metadata(), error);
+ std::string name;
+ if (inspect->name != nullptr) {
+ name = std::string(inspect->name);
+ }
+ CRINaming::ParseSandboxName(name, podStatus->annotations(), *podStatus->mutable_metadata(), error);
if (error.NotEmpty()) {
return;
}
@@ -1301,7 +1305,11 @@ void PodSandboxManagerServiceImpl::ListPodSandboxToGRPC(
CRIHelpers::ExtractAnnotations(response->containers[i]->annotations, *pod->mutable_annotations());
- CRINaming::ParseSandboxName(pod->annotations(), *pod->mutable_metadata(), error);
+ std::string name;
+ if (response->containers[i]->name != nullptr) {
+ name = std::string(response->containers[i]->name);
+ }
+ CRINaming::ParseSandboxName(name, pod->annotations(), *pod->mutable_metadata(), error);
if (filterOutReadySandboxes && pod->state() == runtime::v1alpha2::SANDBOX_READY) {
continue;
diff --git a/src/daemon/entry/cri/naming.cc b/src/daemon/entry/cri/naming.cc
index 682f2e52..54a14a81 100644
--- a/src/daemon/entry/cri/naming.cc
+++ b/src/daemon/entry/cri/naming.cc
@@ -26,6 +26,33 @@
#include "utils.h"
namespace CRINaming {
+static int parseName(const std::string &name, std::vector<std::string> &items, unsigned int &attempt, Errors &err)
+{
+ std::istringstream f(name);
+ std::string part;
+
+ while (getline(f, part, CRI::Constants::nameDelimiterChar)) {
+ items.push_back(part);
+ }
+
+ if (items.size() != 6) {
+ err.Errorf("failed to parse the sandbox name: %s", name.c_str());
+ return -1;
+ }
+
+ if (items[0] != CRI::Constants::kubePrefix) {
+ err.Errorf("container is not managed by kubernetes: %s", name.c_str());
+ return -1;
+ }
+
+ if (util_safe_uint(items[5].c_str(), &attempt)) {
+ err.Errorf("failed to parse the sandbox name %s: %s", name.c_str(), strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadata)
{
std::string sname;
@@ -44,9 +71,12 @@ std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadat
return sname;
}
-void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &annotations,
+void ParseSandboxName(const std::string &name, const google::protobuf::Map<std::string, std::string> &annotations,
runtime::v1alpha2::PodSandboxMetadata &metadata, Errors &err)
{
+ // need check uid and attemp 2 items
+ int needSetUidOrAttemp = 2;
+
if (annotations.count(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY) == 0) {
err.Errorf("annotation don't contains the sandbox name, failed to parse it");
return;
@@ -57,21 +87,35 @@ void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &ann
return;
}
- if (annotations.count(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY) == 0) {
- err.Errorf("annotation don't contains the sandbox uid, failed to parse it");
- return;
+ metadata.set_name(annotations.at(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY));
+ metadata.set_namespace_(annotations.at(CRIHelpers::Constants::SANDBOX_NAMESPACE_ANNOTATION_KEY));
+
+ if (annotations.count(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY) != 0) {
+ metadata.set_uid(annotations.at(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY));
+ needSetUidOrAttemp--;
+ }
+
+ if (annotations.count(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY) != 0) {
+ auto sandboxAttempt = annotations.at(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY);
+ metadata.set_attempt(static_cast<google::protobuf::uint32>(std::stoul(sandboxAttempt)));
+ needSetUidOrAttemp--;
}
- if (annotations.count(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY) == 0) {
- err.Errorf("annotation don't contains the sandbox attempt, failed to parse it");
+ if (needSetUidOrAttemp == 0) {
return;
}
- metadata.set_name(annotations.at(CRIHelpers::Constants::SANDBOX_NAME_ANNOTATION_KEY));
- metadata.set_namespace_(annotations.at(CRIHelpers::Constants::SANDBOX_NAMESPACE_ANNOTATION_KEY));
- metadata.set_uid(annotations.at(CRIHelpers::Constants::SANDBOX_UID_ANNOTATION_KEY));
- auto sandboxAttempt = annotations.at(CRIHelpers::Constants::SANDBOX_ATTEMPT_ANNOTATION_KEY);
- metadata.set_attempt(static_cast<google::protobuf::uint32>(std::stoul(sandboxAttempt)));
+ // get uid and attempt from name,
+ // compatibility to new iSulad manage pods created by old version iSulad
+ // maybe should remove in next version of iSulad
+ std::vector<std::string> items;
+ unsigned int attempt;
+
+ if (parseName(name, items, attempt, err) != 0) {
+ return;
+ }
+ metadata.set_uid(items[4]);
+ metadata.set_attempt(static_cast<google::protobuf::uint32>(attempt));
}
std::string MakeContainerName(const runtime::v1alpha2::PodSandboxConfig &s, const runtime::v1alpha2::ContainerConfig &c)
diff --git a/src/daemon/entry/cri/naming.h b/src/daemon/entry/cri/naming.h
index 7eab41d3..f2d51a98 100644
--- a/src/daemon/entry/cri/naming.h
+++ b/src/daemon/entry/cri/naming.h
@@ -26,7 +26,7 @@ std::string MakeSandboxName(const runtime::v1alpha2::PodSandboxMetadata &metadat
std::string MakeContainerName(const runtime::v1alpha2::PodSandboxConfig &s,
const runtime::v1alpha2::ContainerConfig &c);
-void ParseSandboxName(const google::protobuf::Map<std::string, std::string> &annotations,
+void ParseSandboxName(const std::string &name, const google::protobuf::Map<std::string, std::string> &annotations,
runtime::v1alpha2::PodSandboxMetadata &metadata, Errors &err);
void ParseContainerName(const google::protobuf::Map<std::string, std::string> &annotations,
--
2.25.1

View File

@ -1,26 +0,0 @@
From 1fb9cb0f0e894a2a3b6565b9c69ce4694214910e Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Thu, 22 Dec 2022 17:08:20 +0800
Subject: [PATCH] remove clean_module_fill_ctx for libisulad_img.so
Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
.../modules/image/oci/storage/rootfs_store/rootfs_store.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
index dc1be6c7..1c5d2d84 100644
--- a/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
+++ b/src/daemon/modules/image/oci/storage/rootfs_store/rootfs_store.c
@@ -238,7 +238,7 @@ static int get_containers_from_json()
append_ret = append_container_by_directory(container_path);
if (append_ret != 0) {
-#ifndef DISABLE_CLEANUP
+#if !defined (DISABLE_CLEANUP) && !defined(LIB_ISULAD_IMG_SO)
clean_module_fill_ctx(BROKEN_ROOTFS, (void *)container_dirs[i]);
#endif
ERROR("Found container path but load json failed: %s, deleting...", container_path);
--
2.25.1

View File

@ -1,5 +1,5 @@
%global _version 2.0.17 %global _version 2.0.18
%global _release 14 %global _release 1
%global is_systemd 1 %global is_systemd 1
%global enable_shimv2 1 %global enable_shimv2 1
%global is_embedded 1 %global is_embedded 1
@ -13,74 +13,6 @@ URL: https://gitee.com/openeuler/iSulad
Source: https://gitee.com/openeuler/iSulad/repository/archive/v%{version}.tar.gz Source: https://gitee.com/openeuler/iSulad/repository/archive/v%{version}.tar.gz
BuildRoot: {_tmppath}/iSulad-%{version} BuildRoot: {_tmppath}/iSulad-%{version}
Patch0001: 0001-use-epoll-instead-of-select-for-wait_exit_fifo.patch
Patch0002: 0002-add-namespace-util-UT.patch
Patch0003: 0003-refactor-build-system-of-cutils-ut.patch
Patch0004: 0004-run-storage-layers-ut-with-non-root.patch
Patch0005: 0005-add-extern-C-for-mainloop-header.patch
Patch0006: 0006-add-UT-for-mainloop-and-network.patch
Patch0007: 0007-add-check-for-aes-apis.patch
Patch0008: 0008-add-ut-for-cutils-aes.patch
Patch0009: 0009-add-ut-for-cutils-error.patch
Patch0010: 0010-ensure-argument-is-not-null.patch
Patch0011: 0011-add-ut-for-utils_fs.patch
Patch0012: 0012-Add-adaptation-code-for-filters.patch
Patch0013: 0013-Add-parameter-check-to-path.patch
Patch0014: 0014-Add-ut-for-utils_convert.patch
Patch0015: 0015-Add-ut-for-path.patch
Patch0016: 0016-Add-ut-for-filters.patch
Patch0017: 0017-add-static-for-unexport-function.patch
Patch0018: 0018-add-ut-for-cutils-timestamp.patch
Patch0019: 0019-fix-timestamp-ut-error.patch
Patch0020: 0020-improve-code-in-utils_mount_spec.patch
Patch0021: 0021-Add-ut-for-utils_mount_spec.patch
Patch0022: 0022-Add-ut-for-utils_regex.patch
Patch0023: 0023-improve-code-in-utils.c.patch
Patch0024: 0024-add-ut-for-cutils-utils.patch
Patch0025: 0025-make-sure-kill-pid-not-negative.patch
Patch0026: 0026-add-UT-for-atomic-and-map.patch
Patch0027: 0027-remove-unnecessary-goto-and-add-parameter-check-for-.patch
Patch0028: 0028-Add-ut-for-verify.patch
Patch0029: 0029-fix-error-in-utils_verify_ut.patch
Patch0030: 0030-add-more-test-for-string-and-map.patch
Patch0031: 0031-remove-mnt-point-if-add-device-mapper-device-failed.patch
Patch0032: 0032-dec-device-info-ref-in-grow-device-fs.patch
Patch0033: 0033-device-mapper-bugfix.patch
Patch0034: 0034-delete-syncCloseSem-when-close-all-wssession.patch
Patch0035: 0035-improve-debug-information-for-console-io.patch
Patch0036: 0036-add-ut-for-file.patch
Patch0037: 0037-Add-extend-C-for-header-files.patch
Patch0038: 0038-add-isula-create-rm-option.patch
Patch0039: 0039-feat-add-container-cleanup-module.patch
Patch0040: 0040-bugfix-for-websocket-receive-data-too-long.patch
Patch0041: 0041-fix-call-bim_put-in-im_get_rootfs_dir.patch
Patch0042: 0042-isula-usage-consistency-optimization.patch
Patch0043: 0043-fix-do-container_unref-in-oci_rootfs_clean.patch
Patch0044: 0044-fix-can-not-install-isulad-rpm-because-of-spec.patch
Patch0045: 0045-remove-unknown-option-wno-maybe-uninitialized.patch
Patch0046: 0046-fix-storage-layer-and-driver-ut-failed-in-container.patch
Patch0047: 0047-handle-security-warning-for-cleanup-module.patch
Patch0048: 0048-add-unit-test-for-util-sha256.patch
Patch0049: 0049-add-primary-group-to-additional-groups.patch
Patch0050: 0050-add-unit-test-for-buffer.patch
Patch0051: 0051-remove-chmod-751-permission-for-dirs-by-engine-when-.patch
Patch0052: 0052-add-console-ut.patch
Patch0053: 0053-fix-additional-gids-for-exec-user.patch
Patch0054: 0054-add-CI-for-additional-gid.patch
Patch0055: 0055-retry-call-runtime-ops.patch
Patch0056: 0056-add-ut-test-for-retry-macro.patch
Patch0057: 0057-1749-set-inspect_container-timeout.patch
Patch0058: 0058-1757-add-adaption-code-for-musl.patch
Patch0059: 0059-deleting-broken-rootfs.patch
Patch0060: 0060-1761-fix-leftover-devicemapper-mnt-dir.patch
Patch0061: 0061-check-file-system-ro-before-merge-network-for-syscon.patch
Patch0062: 0062-isulad-shim-wait-for-all-child-process.patch
Patch0063: 0063-When-run-options-rm-is-set-delete-the-stoped-contain.patch
Patch0064: 0064-recheck-kill-command-exit-status.patch
Patch0065: 0065-start-sandbox-before-setup-network-by-default.patch
Patch0066: 0066-compatibility-for-manage-pods-which-created-by-old-i.patch
Patch0067: 0067-remove-clean_module_fill_ctx-for-libisulad_img.so.patch
%ifarch x86_64 aarch64 %ifarch x86_64 aarch64
Provides: libhttpclient.so()(64bit) Provides: libhttpclient.so()(64bit)
Provides: libisula.so()(64bit) Provides: libisula.so()(64bit)
@ -306,6 +238,12 @@ fi
%endif %endif
%changelog %changelog
* Tue Jan 03 2023 zhangxiaoyu <zhangxiaoyu58@huawei.com> - 2.0.18-1
- Type: update
- ID: NA
- SUG: NA
- DESC: update to v2.0.18
* Thu Dec 22 2022 zhangxiaoyu <zhangxiaoyu58@huawei.com> - 2.0.17-14 * Thu Dec 22 2022 zhangxiaoyu <zhangxiaoyu58@huawei.com> - 2.0.17-14
- Type: bugfix - Type: bugfix
- ID: NA - ID: NA

Binary file not shown.