From 69dcd191afbdea5a178fb96a21e28537c2fc6a75 Mon Sep 17 00:00:00 2001 From: zhongtao Date: Sat, 27 Jan 2024 11:16:37 +0800 Subject: [PATCH 05/43] module only deletes the temporary files it creates Signed-off-by: zhongtao --- src/cmd/isulad/main.c | 2 +- src/common/constants.h | 2 +- src/daemon/modules/image/oci/oci_image.c | 44 +++++++++++++++++-- src/daemon/modules/image/oci/oci_image.h | 4 ++ src/daemon/modules/image/oci/oci_load.c | 2 +- .../modules/image/oci/registry/registry.c | 2 +- 6 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/cmd/isulad/main.c b/src/cmd/isulad/main.c index fd0b6e89..7c0c072e 100644 --- a/src/cmd/isulad/main.c +++ b/src/cmd/isulad/main.c @@ -1252,7 +1252,7 @@ static int isulad_tmpdir_security_check(const char *tmp_dir) static int recreate_tmpdir(const char *tmp_dir) { - if (util_recursive_rmdir(tmp_dir, 0) != 0) { + if (util_path_remove(tmp_dir) != 0) { ERROR("Failed to remove directory %s", tmp_dir); return -1; } diff --git a/src/common/constants.h b/src/common/constants.h index 27d4956e..8a6f86d8 100644 --- a/src/common/constants.h +++ b/src/common/constants.h @@ -50,7 +50,7 @@ extern "C" { #define TEMP_DIRECTORY_MODE 0700 -#define ISULAD_TEMP_DIRECTORY_MODE 0600 +#define ISULAD_TEMP_DIRECTORY_MODE 0700 #define CONSOLE_FIFO_DIRECTORY_MODE 0770 diff --git a/src/daemon/modules/image/oci/oci_image.c b/src/daemon/modules/image/oci/oci_image.c index 9cf2cd4f..ce1c8a6b 100644 --- a/src/daemon/modules/image/oci/oci_image.c +++ b/src/daemon/modules/image/oci/oci_image.c @@ -283,10 +283,42 @@ out: return ret; } +// remove dir that image module created +// return false when failed to rmdir +// eg: oci-image-load-XXXXXX && registry-XXXXXX +static bool remove_image_tmpdir_cb(const char *path_name, const struct dirent *sub_dir, void *context) +{ + int nret = 0; + char tmpdir[PATH_MAX] = { 0 }; + + if (sub_dir == NULL) { + return true; + } + + if (!util_has_prefix(sub_dir->d_name, LOAD_TMPDIR_PREFIX) && !util_has_prefix(sub_dir->d_name, REGISTRY_TMPDIR_PREFIX)) { + // only remove directory that image module created + return true; + } + + nret = snprintf(tmpdir, PATH_MAX, "%s/%s", path_name, sub_dir->d_name); + if (nret < 0 || (size_t)nret >= PATH_MAX) { + ERROR("Failed to snprintf for %s", sub_dir->d_name); + return false; + } + + if (util_recursive_rmdir(tmpdir, 0) != 0) { + ERROR("Failed to remove path %s", tmpdir); + return false; + } + + return true; +} + static int recreate_image_tmpdir() { char *image_tmp_path = NULL; int ret = 0; + int nret = 0; image_tmp_path = oci_get_isulad_tmpdir(g_oci_image_module_data.root_dir); if (image_tmp_path == NULL) { @@ -295,10 +327,14 @@ static int recreate_image_tmpdir() goto out; } - if (util_recursive_rmdir(image_tmp_path, 0)) { - ERROR("failed to remove directory %s", image_tmp_path); - ret = -1; - goto out; + // If image_tmp_path exist, cleanup it + if (util_dir_exists(image_tmp_path)) { + nret = util_scan_subdirs(image_tmp_path, remove_image_tmpdir_cb, NULL); + if (nret != 0) { + ERROR("Failed to scan isulad tmp subdirs"); + ret = -1; + goto out; + } } if (util_mkdir_p(image_tmp_path, TEMP_DIRECTORY_MODE)) { diff --git a/src/daemon/modules/image/oci/oci_image.h b/src/daemon/modules/image/oci/oci_image.h index c7304897..482091d6 100644 --- a/src/daemon/modules/image/oci/oci_image.h +++ b/src/daemon/modules/image/oci/oci_image.h @@ -38,6 +38,10 @@ struct oci_image_module_data { char **insecure_registries; size_t insecure_registries_len; }; + +#define LOAD_TMPDIR_PREFIX "oci-image-load-" +#define REGISTRY_TMPDIR_PREFIX "registry-" + struct oci_image_module_data *get_oci_image_data(void); int oci_init(const isulad_daemon_configs *args); diff --git a/src/daemon/modules/image/oci/oci_load.c b/src/daemon/modules/image/oci/oci_load.c index 31ae3849..534e2647 100644 --- a/src/daemon/modules/image/oci/oci_load.c +++ b/src/daemon/modules/image/oci/oci_load.c @@ -1048,7 +1048,7 @@ static char *oci_load_path_create() goto out; } - nret = snprintf(tmp_dir, PATH_MAX, "%s/oci-image-load-XXXXXX", image_tmp_path); + nret = snprintf(tmp_dir, PATH_MAX, "%s/%sXXXXXX", image_tmp_path, LOAD_TMPDIR_PREFIX); if (nret < 0 || (size_t)nret >= sizeof(tmp_dir)) { ERROR("Path is too long"); ret = -1; diff --git a/src/daemon/modules/image/oci/registry/registry.c b/src/daemon/modules/image/oci/registry/registry.c index 751a8727..aed3057a 100644 --- a/src/daemon/modules/image/oci/registry/registry.c +++ b/src/daemon/modules/image/oci/registry/registry.c @@ -1908,7 +1908,7 @@ static int prepare_pull_desc(pull_descriptor *desc, registry_pull_options *optio goto out; } - sret = snprintf(blobpath, PATH_MAX, "%s/registry-XXXXXX", image_tmp_path); + sret = snprintf(blobpath, PATH_MAX, "%s/%sXXXXXX", image_tmp_path, REGISTRY_TMPDIR_PREFIX); if (sret < 0 || (size_t)sret >= PATH_MAX) { ERROR("image tmp work path too long"); ret = -1; -- 2.34.1