From 4c18c29522fc35f94cae6f1e34e28bbbedef2520 Mon Sep 17 00:00:00 2001 From: jikai Date: Tue, 28 Nov 2023 15:59:48 +0800 Subject: [PATCH 18/22] use fixed tmp file to write config etc Signed-off-by: jikai --- src/lcrcontainer_extend.c | 8 +-- src/utils.c | 127 +++----------------------------------- src/utils.h | 4 +- tests/utils_ut.cpp | 19 +++--- 4 files changed, 21 insertions(+), 137 deletions(-) diff --git a/src/lcrcontainer_extend.c b/src/lcrcontainer_extend.c index e3c081a..0e1d926 100644 --- a/src/lcrcontainer_extend.c +++ b/src/lcrcontainer_extend.c @@ -406,7 +406,7 @@ static char *lcr_save_seccomp_file(const char *bundle, const char *seccomp_conf) } if (lcr_util_atomic_write_file(real_seccomp, seccomp_conf, strlen(seccomp_conf), - CONFIG_FILE_MODE, false) == -1) { + CONFIG_FILE_MODE, ".tmp_seccomp_save") == -1) { ERROR("write seccomp_conf failed"); goto cleanup; } @@ -617,7 +617,7 @@ static FILE *lcr_open_tmp_config_file(const char *bundle, char **config_file, ch goto out; } - *tmp_file = lcr_util_get_random_tmp_file(*config_file); + *tmp_file = lcr_util_get_tmp_file(*config_file, ".tmp_config_save"); if (*tmp_file == NULL) { ERROR("Failed to get random tmp file for %s", *config_file); goto out; @@ -874,7 +874,7 @@ out_free: if (fp != NULL) { fclose(fp); } - if (!bret && unlink(tmp_file) != 0 && errno != ENOENT) { + if (!bret && tmp_file != NULL && unlink(tmp_file) != 0 && errno != ENOENT) { SYSERROR("Failed to remove temp file:%s", tmp_file); } free(config_file); @@ -898,7 +898,7 @@ static bool lcr_write_ocihooks(const char *path, const oci_runtime_spec_hooks *h } if (lcr_util_atomic_write_file(path, json_hooks, strlen(json_hooks), - CONFIG_FILE_MODE, false) == -1) { + CONFIG_FILE_MODE, ".tmp_ocihooks_save") == -1) { ERROR("write json hooks failed"); goto out_free; } diff --git a/src/utils.c b/src/utils.c index 68e9bc4..85874a7 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1284,53 +1284,6 @@ out: return ret; } -static char *lcr_util_path_base(const char *path) -{ - char *dir = NULL; - int len = 0; - int i = 0; - - if (path == NULL) { - ERROR("invalid NULL param"); - return NULL; - } - - len = (int)strlen(path); - if (len == 0) { - return lcr_util_strdup_s("."); - } - - dir = lcr_util_strdup_s(path); - - // strip last slashes - for (i = len - 1; i >= 0; i--) { - if (dir[i] != '/') { - break; - } - dir[i] = '\0'; - } - - len = (int)strlen(dir); - if (len == 0) { - free(dir); - return lcr_util_strdup_s("/"); - } - - for (i = len - 1; i >= 0; i--) { - if (dir[i] == '/') { - break; - } - } - - if (i < 0) { - return dir; - } - - char *result = lcr_util_strdup_s(&dir[i + 1]); - free(dir); - return result; -} - static char *lcr_util_path_dir(const char *path) { char *dir = NULL; @@ -1364,43 +1317,6 @@ static char *lcr_util_path_dir(const char *path) return dir; } -static int lcr_util_generate_random_str(char *id, size_t len) -{ - int fd = -1; - int num = 0; - size_t i; - const int m = 256; - - if (id == NULL) { - return -1; - } - - len = len / 2; - fd = open("/dev/urandom", O_RDONLY); - if (fd == -1) { - ERROR("Failed to open /dev/urandom"); - return -1; - } - for (i = 0; i < len; i++) { - int nret; - if (lcr_util_read_nointr(fd, &num, sizeof(int)) < 0) { - ERROR("Failed to read urandom value"); - close(fd); - return -1; - } - unsigned char rs = (unsigned char)(num % m); - nret = snprintf((id + i * 2), ((len - i) * 2 + 1), "%02x", (unsigned int)rs); - if (nret < 0 || (size_t)nret >= ((len - i) * 2 + 1)) { - ERROR("Failed to snprintf random string"); - close(fd); - return -1; - } - } - close(fd); - id[i * 2] = '\0'; - return 0; -} - static char *lcr_util_path_join(const char *dir, const char *file) { int nret = 0; @@ -1426,53 +1342,30 @@ static char *lcr_util_path_join(const char *dir, const char *file) return lcr_util_strdup_s(cleaned); } -char *lcr_util_get_random_tmp_file(const char *fname) +char *lcr_util_get_tmp_file(const char *fname, const char *tmp_name) { -#define RANDOM_TMP_PATH 10 - int nret = 0; char *result = NULL; - char *base = NULL; char *dir = NULL; - char rpath[PATH_MAX] = { 0x00 }; - char random_tmp[RANDOM_TMP_PATH + 1] = { 0x00 }; - if (fname == NULL) { + if (fname == NULL || tmp_name == NULL) { ERROR("Invalid NULL param"); return NULL; } - base = lcr_util_path_base(fname); - if (base == NULL) { - ERROR("Failed to get base of %s", fname); - goto out; - } - dir = lcr_util_path_dir(fname); if (dir == NULL) { ERROR("Failed to get dir of %s", fname); goto out; } - if (lcr_util_generate_random_str(random_tmp, (size_t)RANDOM_TMP_PATH)) { - ERROR("Failed to generate random str for random path"); - goto out; - } - - nret = snprintf(rpath, PATH_MAX, ".tmp-%s-%s", base, random_tmp); - if (nret < 0 || (size_t)nret >= PATH_MAX) { - ERROR("Failed to generate tmp base file"); - goto out; - } - - result = lcr_util_path_join(dir, rpath); + result = lcr_util_path_join(dir, tmp_name); out: - free(base); free(dir); return result; } -static int do_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode, bool sync) +static int do_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode) { int ret = 0; int dst_fd = -1; @@ -1492,12 +1385,6 @@ static int do_atomic_write_file(const char *fname, const char *content, size_t c goto free_out; } - if (sync && (fdatasync(dst_fd) != 0)) { - ret = -1; - SYSERROR("Failed to sync data of file:%s", fname); - goto free_out; - } - free_out: if (dst_fd >= 0) { close(dst_fd); @@ -1505,7 +1392,7 @@ free_out: return ret; } -int lcr_util_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode, bool sync) +int lcr_util_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode, const char *tmp_name) { int ret = 0; char *tmp_file = NULL; @@ -1522,13 +1409,13 @@ int lcr_util_atomic_write_file(const char *fname, const char *content, size_t co return -1; } - tmp_file = lcr_util_get_random_tmp_file(fname); + tmp_file = lcr_util_get_tmp_file(fname, tmp_name); if (tmp_file == NULL) { ERROR("Failed to get tmp file for %s", fname); return -1; } - ret = do_atomic_write_file(tmp_file, content, content_len, mode, sync); + ret = do_atomic_write_file(tmp_file, content, content_len, mode); if (ret != 0) { ERROR("Failed to write content to tmp file for %s", tmp_file); ret = -1; diff --git a/src/utils.h b/src/utils.h index 51e0dea..69e0c28 100644 --- a/src/utils.h +++ b/src/utils.h @@ -217,9 +217,9 @@ int lcr_util_null_stdfds(void); int lcr_util_flock_append_file(const char *filepath, const char *content); -char *lcr_util_get_random_tmp_file(const char *fname); +char *lcr_util_get_tmp_file(const char *fname, const char *tmp_name); -int lcr_util_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode, bool sync); +int lcr_util_atomic_write_file(const char *fname, const char *content, size_t content_len, mode_t mode, const char *tmp_name); int lcr_util_get_real_swap(int64_t memory, int64_t memory_swap, int64_t *swap); int lcr_util_trans_cpushare_to_cpuweight(int64_t cpu_share); diff --git a/tests/utils_ut.cpp b/tests/utils_ut.cpp index 8acba29..17f60ed 100644 --- a/tests/utils_ut.cpp +++ b/tests/utils_ut.cpp @@ -95,19 +95,16 @@ err_out: return buf; } -TEST(utils_testcase, test_get_random_tmp_file) +TEST(utils_testcase, test_get_tmp_file) { -#define RANDOM_TMP_PATH 10 const char *fname = "/tmp/lcr-test/test"; - char *tmp_file = lcr_util_get_random_tmp_file(nullptr); - const char *prefix = "/tmp/lcr-test/.tmp-test-"; + char *tmp_file = lcr_util_get_tmp_file(nullptr, ".tmp_test_file"); ASSERT_EQ(tmp_file, nullptr); - tmp_file = lcr_util_get_random_tmp_file(fname); + tmp_file = lcr_util_get_tmp_file(fname, ".tmp_test_file"); ASSERT_NE(tmp_file, nullptr); - ASSERT_EQ(strlen(tmp_file), strlen("/tmp/lcr-test/.tmp-test-") + RANDOM_TMP_PATH); - ASSERT_EQ(memcmp(tmp_file, prefix, strlen(prefix)), 0); + ASSERT_STREQ(tmp_file, "/tmp/lcr-test/.tmp_test_file"); free(tmp_file); } @@ -118,19 +115,19 @@ TEST(utils_testcase, test_atomic_write_file) const char *new_content = "line1\nline2\nline3\n"; char *readcontent = nullptr; - ASSERT_EQ(lcr_util_atomic_write_file(NULL, content, strlen(content), 0644, false), -1); - ASSERT_EQ(lcr_util_atomic_write_file(fname, NULL, 0, 0644, false), 0); + ASSERT_EQ(lcr_util_atomic_write_file(NULL, content, strlen(content), 0644, ".tmp_test"), -1); + ASSERT_EQ(lcr_util_atomic_write_file(fname, NULL, 0, 0644, ".tmp_test"), 0); ASSERT_EQ(lcr_util_build_dir(fname), 0); - ASSERT_EQ(lcr_util_atomic_write_file(fname, content, strlen(content), 0644, false), 0); + ASSERT_EQ(lcr_util_atomic_write_file(fname, content, strlen(content), 0644, ".tmp_test"), 0); readcontent = test_read_text_file(fname); ASSERT_NE(readcontent, nullptr); ASSERT_STREQ(readcontent, content); free(readcontent); - ASSERT_EQ(lcr_util_atomic_write_file(fname, new_content, strlen(new_content), 0644, false), 0); + ASSERT_EQ(lcr_util_atomic_write_file(fname, new_content, strlen(new_content), 0644, ".tmp_test"), 0); readcontent = test_read_text_file(fname); ASSERT_NE(readcontent, nullptr); ASSERT_STREQ(readcontent, new_content); -- 2.34.1