提交 32001591 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!35 utils: add prefix for utils function

Merge pull request !35 from lifeng_isula/master_fix_util
......@@ -31,7 +31,7 @@ Buffer *buffer_alloc(size_t initial_size)
return NULL;
}
buf = util_common_calloc_s(sizeof(Buffer));
buf = lcr_util_common_calloc_s(sizeof(Buffer));
if (buf == NULL) {
return NULL;
}
......@@ -104,7 +104,7 @@ static int buffer_grow(Buffer *buf, size_t minimum_size)
return -1;
}
tmp = util_common_calloc_s(new_size);
tmp = lcr_util_common_calloc_s(new_size);
if (tmp == NULL) {
ERROR("Out of memory");
return -1;
......
......@@ -40,7 +40,7 @@ static int files_limit_checker(const char *value)
long long limit = 0;
int ret = 0;
ret = util_safe_llong(value, &limit);
ret = lcr_util_safe_llong(value, &limit);
if (ret) {
ret = -1;
}
......@@ -75,7 +75,7 @@ static int check_console_log_filesize(const char *value)
return ret;
}
if (parse_byte_size_string(value, &tmp) == 0 && tmp >= min) {
if (lcr_parse_byte_size_string(value, &tmp) == 0 && tmp >= min) {
ret = 0;
}
......@@ -94,7 +94,7 @@ static int check_oom_score_adj(const char *value)
return ret;
}
if (util_safe_int(value, &tmp) == 0 && tmp >= min && tmp <= max) {
if (lcr_util_safe_int(value, &tmp) == 0 && tmp >= min && tmp <= max) {
ret = 0;
}
lcr_set_error_message(LCR_ERR_RUNTIME, "Invalid value %s, range for oom score adj is [%d, %d]", value, min, max);
......@@ -110,7 +110,7 @@ static int check_console_log_filerotate(const char *value)
return ret;
}
if (util_safe_uint(value, &tmp) == 0) {
if (lcr_util_safe_uint(value, &tmp) == 0) {
ret = 0;
}
......@@ -124,7 +124,7 @@ static int check_rootfs_mount(const char *value)
return -1;
}
if (!dir_exists(value)) {
if (!lcr_util_dir_exists(value)) {
lcr_set_error_message(LCR_ERR_RUNTIME, "Container rootfs mount path '%s' is not exist", value);
return -1;
}
......@@ -273,18 +273,18 @@ struct lcr_list *create_lcr_list_node(const char *key, const char *value)
struct lcr_list *node = NULL;
lcr_config_item_t *entry = NULL;
node = util_common_calloc_s(sizeof(*node));
node = lcr_util_common_calloc_s(sizeof(*node));
if (node == NULL) {
return NULL;
}
entry = util_common_calloc_s(sizeof(*entry));
entry = lcr_util_common_calloc_s(sizeof(*entry));
if (entry == NULL) {
free(node);
return NULL;
}
entry->name = util_strdup_s(key);
entry->name = lcr_util_strdup_s(key);
entry->value = util_strdup_s(value);
entry->value = lcr_util_strdup_s(value);
node->elem = entry;
return node;
......@@ -434,7 +434,7 @@ static int trans_oci_process_init_groups(const oci_runtime_spec_process *proc, s
}
size_t total_len = (LCR_NUMSTRLEN64 + 1) * proc->user->additional_gids_len;
char *gids = util_common_calloc_s(total_len);
char *gids = lcr_util_common_calloc_s(total_len);
if (gids == NULL) {
goto out;
}
......@@ -539,7 +539,7 @@ static int trans_oci_process_env_and_cap(const oci_runtime_spec_process *proc, s
size_t i;
for (i = 0; i < proc->env_len; i++) {
char *replaced = util_string_replace(" ", SPACE_MAGIC_STR, proc->env[i]);
char *replaced = lcr_util_string_replace(" ", SPACE_MAGIC_STR, proc->env[i]);
if (replaced == NULL) {
ERROR("memory allocation error");
goto out;
......@@ -590,7 +590,7 @@ static int trans_oci_process_prlimit(const oci_runtime_spec_process *proc, struc
char buf_key[30] = { 0 };
char buf_value[60] = { 0 };
size_t j;
char *type = util_strdup_s(lr->type);
char *type = lcr_util_strdup_s(lr->type);
// Lower case type,eg. RLIMIT_NOFILE -> RLIMIT_nofile
for (j = strlen("RLIMIT_"); j < strlen(type); j++) {
......@@ -699,7 +699,7 @@ struct lcr_list *trans_oci_process(const oci_runtime_spec_process *proc)
{
struct lcr_list *conf = NULL;
conf = util_common_calloc_s(sizeof(struct lcr_list));
conf = lcr_util_common_calloc_s(sizeof(struct lcr_list));
if (conf == NULL) {
return NULL;
}
......@@ -770,7 +770,7 @@ static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, stru
int nret;
if (is_root_readonly(root)) {
value = util_strdup_s("ro");
value = lcr_util_strdup_s("ro");
}
if ((linux != NULL) && linux->rootfs_propagation != NULL) {
......@@ -781,7 +781,7 @@ static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, stru
goto out;
}
newsize = strlen(linux->rootfs_propagation) + strlen(value) + APPEND_COMMA_END_SIZE;
nret = mem_realloc((void **)&tmpvalue, newsize, value, strlen(value));
nret = lcr_mem_realloc((void **)&tmpvalue, newsize, value, strlen(value));
if (nret < 0) {
ERROR("Out of memory");
goto out;
......@@ -793,7 +793,7 @@ static int trans_oci_root_rootfs_options(const oci_runtime_spec_root *root, stru
goto out;
}
} else {
value = util_strdup_s(linux->rootfs_propagation);
value = lcr_util_strdup_s(linux->rootfs_propagation);
}
}
......@@ -815,7 +815,7 @@ struct lcr_list *trans_oci_root(const oci_runtime_spec_root *root, const oci_run
{
struct lcr_list *conf = NULL;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -887,12 +887,12 @@ static char *trans_mount_to_lxc_options(const defs_mount *mount)
lxc_options = isdir ? ",create=dir" : ",create=file";
prefix = util_string_join(",", (const char **)mount->options, mount->options_len);
prefix = lcr_util_string_join(",", (const char **)mount->options, mount->options_len);
if (prefix == NULL) {
prefix = util_strdup_s("defaults");
prefix = lcr_util_strdup_s("defaults");
}
result = util_string_append(lxc_options, prefix);
result = lcr_util_string_append(lxc_options, prefix);
free(prefix);
return result;
......@@ -926,15 +926,15 @@ static char *get_mount_readmode_options(const defs_mount *mount, const char *typ
if (is_mount_type_cgroup(type)) {
if (readonly) {
options = util_strdup_s("ro:force");
options = lcr_util_strdup_s("ro:force");
} else {
options = util_strdup_s("rw:force");
options = lcr_util_strdup_s("rw:force");
}
} else {
if (readonly) {
options = util_strdup_s("ro");
options = lcr_util_strdup_s("ro");
} else {
options = util_strdup_s("rw");
options = lcr_util_strdup_s("rw");
}
}
......@@ -996,12 +996,12 @@ static struct lcr_list *trans_mount_entry_to_lxc(const defs_mount *mount)
char *replaced_dest = NULL;
int ret;
char *replaced_source = util_string_replace(" ", SPACE_MAGIC_STR, mount->source);
char *replaced_source = lcr_util_string_replace(" ", SPACE_MAGIC_STR, mount->source);
if (replaced_source == NULL) {
ERROR("memory allocation error");
goto err_out;
}
replaced_dest = util_string_replace(" ", SPACE_MAGIC_STR, mount->destination);
replaced_dest = lcr_util_string_replace(" ", SPACE_MAGIC_STR, mount->destination);
if (replaced_dest == NULL) {
ERROR("memory allocation error");
free(replaced_source);
......@@ -1117,7 +1117,7 @@ struct lcr_list *trans_oci_mounts(const oci_runtime_spec *c)
bool system_container = is_system_container(c);
bool external_rootfs = is_external_rootfs(c);
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -1169,7 +1169,7 @@ static int trans_one_oci_id_mapping(struct lcr_list *conf, const char *typ, cons
if (nret < 0 || (size_t)nret >= sizeof(subid)) {
return -1;
}
nret = util_atomic_write_file(path, subid);
nret = lcr_util_atomic_write_file(path, subid);
if (nret < 0) {
return -1;
}
......@@ -1208,7 +1208,7 @@ static struct lcr_list *trans_oci_id_mapping(const oci_runtime_config_linux *l)
struct lcr_list *conf = NULL;
int nret = 0;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -1852,7 +1852,7 @@ static struct lcr_list *trans_oci_resources(const oci_runtime_config_linux_resou
{
struct lcr_list *conf = NULL;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -1912,7 +1912,7 @@ static char *trans_oci_namespace_to_lxc(const char *typ)
for (p = namespaces_map; p != NULL && p->ns_name != NULL; p++) {
if (strcmp(typ, p->ns_name) == 0) {
return util_strdup_s(p->lxc_name);
return lcr_util_strdup_s(p->lxc_name);
}
}
return NULL;
......@@ -1926,7 +1926,7 @@ static struct lcr_list *trans_oci_namespaces(const oci_runtime_config_linux *l)
size_t i;
oci_runtime_defs_linux_namespace_reference *ns = NULL;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -1970,7 +1970,7 @@ static struct lcr_list *trans_oci_mask_ro_paths(const oci_runtime_config_linux *
size_t i;
char *path = NULL;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -2020,7 +2020,7 @@ static struct lcr_list *trans_oci_linux_devices(const oci_runtime_config_linux *
oci_runtime_defs_linux_device *device = NULL;
char buf_value[POPULATE_DEVICE_SIZE] = { 0 };
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -2089,15 +2089,15 @@ static inline bool is_seccomp_action_errno(const char *value)
static char *seccomp_trans_action(const char *action)
{
if (is_seccomp_action_kill(action)) {
return util_strdup_s("kill");
return lcr_util_strdup_s("kill");
} else if (is_seccomp_action_trap(action)) {
return util_strdup_s("trap");
return lcr_util_strdup_s("trap");
} else if (is_seccomp_action_allow(action)) {
return util_strdup_s("allow");
return lcr_util_strdup_s("allow");
} else if (is_seccomp_action_trace(action)) {
return util_strdup_s("trace 1");
return lcr_util_strdup_s("trace 1");
} else if (is_seccomp_action_errno(action)) {
return util_strdup_s("errno 1");
return lcr_util_strdup_s("errno 1");
}
return NULL;
......@@ -2164,11 +2164,11 @@ static char *get_hostarch(void)
for (i = 0; i < len; i++) {
if (i == 0 || i == 1 || i == 2) {
if (strcmp(uts.machine, arch_type[i].arch) == 0) {
return util_strdup_s(arch_type[i].value);
return lcr_util_strdup_s(arch_type[i].value);
}
} else {
if (strncmp(uts.machine, arch_type[i].arch, (size_t)(arch_type[i].num)) == 0) {
return util_strdup_s(arch_type[i].value);
return lcr_util_strdup_s(arch_type[i].value);
}
}
}
......@@ -2209,7 +2209,7 @@ static char *seccomp_trans_arch(const char *arch)
if (strcmp(arch_type[i].arch, "SCMP_ARCH_AUTO") == 0) {
return get_hostarch();
} else if (strcmp(arch, arch_type[i].arch) == 0) {
return util_strdup_s(arch_type[i].value);
return lcr_util_strdup_s(arch_type[i].value);
}
}
return NULL;
......@@ -2320,7 +2320,7 @@ static struct lcr_list *trans_oci_linux_sysctl(const json_map_string_string *sys
struct lcr_list *node = NULL;
size_t i;
conf = util_common_calloc_s(sizeof(*conf));
conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -2457,7 +2457,7 @@ struct lcr_list *trans_oci_linux(const oci_runtime_config_linux *l, char **secco
int ret = 0;
struct lcr_list *tmp = NULL;
struct lcr_list *conf = util_common_calloc_s(sizeof(*conf));
struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -2532,7 +2532,7 @@ struct lcr_list *trans_annotations(const json_map_string_string *anno)
size_t len;
int ret = 0;
struct lcr_list *conf = util_common_calloc_s(sizeof(*conf));
struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......@@ -2606,7 +2606,7 @@ static int add_needed_net_conf(struct lcr_list *conf)
/* get needed lxc conf */
struct lcr_list *get_needed_lxc_conf()
{
struct lcr_list *conf = util_common_calloc_s(sizeof(*conf));
struct lcr_list *conf = lcr_util_common_calloc_s(sizeof(*conf));
if (conf == NULL) {
return NULL;
}
......
......@@ -67,7 +67,7 @@ void lcr_set_error_message(lcr_errno_t errcode, const char *format, ...)
return;
}
g_lcr_error.errcode = errcode;
g_lcr_error.errmsg = util_strdup_s(errbuf);
g_lcr_error.errmsg = lcr_util_strdup_s(errbuf);
}
void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...)
......@@ -88,7 +88,7 @@ void lcr_try_set_error_message(lcr_errno_t errcode, const char *format, ...)
return;
}
g_lcr_error.errcode = errcode;
g_lcr_error.errmsg = util_strdup_s(errbuf);
g_lcr_error.errmsg = lcr_util_strdup_s(errbuf);
}
void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...)
......@@ -107,7 +107,7 @@ void lcr_append_error_message(lcr_errno_t errcode, const char *format, ...)
return;
}
g_lcr_error.errcode = errcode;
result = util_string_append(g_lcr_error.errmsg, errbuf);
result = lcr_util_string_append(g_lcr_error.errmsg, errbuf);
if (result == NULL) {
g_lcr_error.errcode = LCR_ERR_MEMOUT;
return;
......
......@@ -118,15 +118,15 @@ struct lcr_container_info *lcr_container_info_get(const char *name, const char *
run_flag = (strcmp(st, "STOPPED") != 0);
/* Now it makes sense to allocate memory */
info = util_common_calloc_s(sizeof(*info));
info = lcr_util_common_calloc_s(sizeof(*info));
if (info == NULL) {
nret = -1;
goto put_and_finish;
}
info->init = -1;
info->running = run_flag;
info->name = util_strdup_s(name);
info->state = util_strdup_s(st);
info->name = lcr_util_strdup_s(name);
info->state = lcr_util_strdup_s(st);
if (run_flag) {
info->init = c->init_pid(c);
}
......@@ -204,7 +204,7 @@ static int create_partial(const struct lxc_container *c)
// $lxcpath + '/' + $name + '/partial' + \0
len = strlen(c->config_path) + strlen(c->name) + 10;
char *path = util_common_calloc_s(len);
char *path = lcr_util_common_calloc_s(len);
if (path == NULL) {
ERROR("Out of memory in create_partial");
return -1;
......@@ -216,7 +216,7 @@ static int create_partial(const struct lxc_container *c)
goto out_free;
}
fd = util_open(path, O_RDWR | O_CREAT | O_EXCL, DEFAULT_SECURE_FILE_MODE);
fd = lcr_util_open(path, O_RDWR | O_CREAT | O_EXCL, DEFAULT_SECURE_FILE_MODE);
if (fd < 0) {
SYSERROR("Error creating partial file: %s", path);
goto out_free;
......@@ -251,7 +251,7 @@ static void remove_partial(const struct lxc_container *c)
// $lxcpath + '/' + $name + '/partial' + \0
len = strlen(c->config_path) + strlen(c->name) + 10;
char *path = util_common_calloc_s(len);
char *path = lcr_util_common_calloc_s(len);
if (path == NULL) {
ERROR("Out of memory in remove_partial");
return;
......@@ -371,7 +371,7 @@ static bool wait_start_pid(pid_t pid, int rfd, const char *name, const char *pat
ssize_t size_read = 0;
char buffer[BUFSIZ] = { 0 };
ret = wait_for_pid(pid);
ret = lcr_wait_for_pid(pid);
if (ret == 0) {
return true;
}
......@@ -1036,7 +1036,7 @@ static char *lcr_get_config_item(struct lxc_container *c, const char *key, bool
goto out;
}
cret = util_common_calloc_s((len + 1) * sizeof(char));
cret = lcr_util_common_calloc_s((len + 1) * sizeof(char));
if (cret == NULL) {
ERROR("Out of memory");
goto out;
......@@ -1079,7 +1079,7 @@ static bool lcr_get_console_config_items(struct lxc_container *c, struct lcr_con
if (item == NULL) {
DEBUG("Log rotate is NULL");
} else {
if (util_safe_uint(item, &trotate) == 0) {
if (lcr_util_safe_uint(item, &trotate) == 0) {
config->log_rotate = trotate;
} else {
ERROR("trans to uint failed");
......@@ -1226,7 +1226,7 @@ int lcr_log_init(const char *name, const char *file, const char *priority, const
lconf.priority = priority ? priority : "ERROR";
} else {
/* File has prefix "fifo:", */
full_path = util_string_split_prefix(pre_len, file);
full_path = lcr_util_string_split_prefix(pre_len, file);
lconf.file = full_path;
lconf.driver = "fifo";
lconf.priority = priority;
......
......@@ -55,7 +55,7 @@ static inline void add_array_elem(char **array, size_t total, size_t *pos, const
if (*pos + 1 >= total - 1) {
return;
}
array[*pos] = util_strdup_s(elem);
array[*pos] = lcr_util_strdup_s(elem);
*pos += 1;
}
......@@ -507,10 +507,10 @@ void do_lcr_state(struct lxc_container *c, struct lcr_container_state *lcs)
clear_error_message(&g_lcr_error);
(void)memset(lcs, 0x00, sizeof(struct lcr_container_state));
lcs->name = util_strdup_s(c->name);
lcs->name = lcr_util_strdup_s(c->name);
state = c->state(c);
lcs->state = state ? util_strdup_s(state) : util_strdup_s("-");
lcs->state = state ? lcr_util_strdup_s(state) : lcr_util_strdup_s("-");
if (c->is_running(c)) {
lcs->init = c->init_pid(c);
......@@ -549,7 +549,7 @@ static void execute_lxc_attach(const char *name, const char *path, const struct
size_t j = 0;
size_t args_len = PARAM_NUM;
if (util_check_inherited(true, -1) != 0) {
if (lcr_util_check_inherited(true, -1) != 0) {
COMMAND_ERROR("Close inherited fds failed");
exit(EXIT_FAILURE);
}
......@@ -560,7 +560,7 @@ static void execute_lxc_attach(const char *name, const char *path, const struct
exit(EXIT_FAILURE);
}
params = util_common_calloc_s(args_len * sizeof(char *));
params = lcr_util_common_calloc_s(args_len * sizeof(char *));
if (params == NULL) {
COMMAND_ERROR("Out of memory");
exit(EXIT_FAILURE);
......@@ -661,7 +661,7 @@ bool do_attach(const char *name, const char *path, const struct lcr_exec_request
}
if (pid == (pid_t)0) {
if (util_null_stdfds() < 0) {
if (lcr_util_null_stdfds() < 0) {
COMMAND_ERROR("Failed to close fds");
exit(EXIT_FAILURE);
}
......@@ -676,7 +676,7 @@ bool do_attach(const char *name, const char *path, const struct lcr_exec_request
close(pipefd[1]);
status = wait_for_pid_status(pid);
status = lcr_wait_for_pid_status(pid);
if (status < 0) {
ERROR("Failed to wait lxc-attach");
goto close_out;
......@@ -707,7 +707,7 @@ void execute_lxc_start(const char *name, const char *path, const struct lcr_star
char *params[PARAM_NUM] = { NULL };
size_t i = 0;
if (util_check_inherited(true, -1) != 0) {
if (lcr_util_check_inherited(true, -1) != 0) {
COMMAND_ERROR("Close inherited fds failed");
}
......
......@@ -73,7 +73,7 @@ static int realloc_annotations(oci_runtime_spec *oci_spec, size_t new_lens)
}
if (!oci_spec->annotations) {
oci_spec->annotations = util_common_calloc_s(sizeof(json_map_string_string));
oci_spec->annotations = lcr_util_common_calloc_s(sizeof(json_map_string_string));
if (!oci_spec->annotations) {
ERROR("Out of memory");
nret = -1;
......@@ -88,7 +88,7 @@ static int realloc_annotations(oci_runtime_spec *oci_spec, size_t new_lens)
}
new_size = (oci_spec->annotations->len + new_lens) * sizeof(char *);
old_size = oci_spec->annotations->len * sizeof(char *);
nret = mem_realloc((void **)&fkey, new_size, oci_spec->annotations->keys, old_size);
nret = lcr_mem_realloc((void **)&fkey, new_size, oci_spec->annotations->keys, old_size);
if (nret) {
ERROR("Failed to realloc memory for files limit variables");
nret = -1;
......@@ -96,7 +96,7 @@ static int realloc_annotations(oci_runtime_spec *oci_spec, size_t new_lens)
}
oci_spec->annotations->keys = fkey;
nret = mem_realloc((void **)&fval, new_size, oci_spec->annotations->values, old_size);
nret = lcr_mem_realloc((void **)&fval, new_size, oci_spec->annotations->values, old_size);
if (nret) {
ERROR("Failed to realloc memory for files limit variables");
nret = -1;
......@@ -123,7 +123,7 @@ static int make_annotations(oci_runtime_spec *container, const struct lxc_contai
goto out;
}
fpos = (int)(anno->len - 1);
anno->keys[fpos] = util_strdup_s("log.console.file");
anno->keys[fpos] = lcr_util_strdup_s("log.console.file");
anno->values[fpos] = NULL;
}
......@@ -136,14 +136,14 @@ static int make_annotations(oci_runtime_spec *container, const struct lxc_contai
if (anno->values[fpos]) {
free(anno->values[fpos]);
}
anno->values[fpos] = util_strdup_s(default_path);
anno->values[fpos] = lcr_util_strdup_s(default_path);
}
if (strcmp("none", anno->values[fpos]) == 0) {
DEBUG("Disable console log.");
ret = 0;
goto out;
}
if (util_ensure_path(&realpath, anno->values[fpos])) {
if (lcr_util_ensure_path(&realpath, anno->values[fpos])) {
ERROR("Invalid log path: %s, error: %s.", anno->values[fpos], strerror(errno));
goto out;
}
......@@ -351,7 +351,7 @@ static int lcr_spec_write_seccomp_line(int fd, const char *seccomp)
len = strlen("lxc.seccomp.profile") + 3 + strlen(seccomp) + 1;
line = util_common_calloc_s(len * sizeof(char));
line = lcr_util_common_calloc_s(len * sizeof(char));
if (line == NULL) {
ERROR("Out of memory");
goto cleanup;
......@@ -389,13 +389,13 @@ static char *lcr_save_seccomp_file(const char *bundle, const char *seccomp_conf)
goto cleanup;
}
nret = util_ensure_path(&real_seccomp, seccomp);
nret = lcr_util_ensure_path(&real_seccomp, seccomp);
if (nret < 0) {
ERROR("Failed to ensure path %s", seccomp);
goto cleanup;
}
fd = util_open(real_seccomp, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
fd = lcr_util_open(real_seccomp, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
if (fd == -1) {
SYSERROR("Create file %s failed", real_seccomp);
goto cleanup;
......@@ -426,7 +426,7 @@ static struct lcr_container_info *info_new(struct lcr_container_info **info, siz
length = (*size + 1) * sizeof(struct lcr_container_info);
nret = mem_realloc((void **)&n, length, (void *)(*info), (*size) * sizeof(struct lcr_container_info));
nret = lcr_mem_realloc((void **)&n, length, (void *)(*info), (*size) * sizeof(struct lcr_container_info));
if (nret < 0) {
return NULL;
}
......@@ -592,8 +592,8 @@ int lcr_containers_info_get(const char *lcrpath, struct lcr_container_info **inf
goto put_container;
}
in->running = run_flag;
in->name = util_strdup_s(name);
in->state = util_strdup_s(st);
in->name = lcr_util_strdup_s(name);
in->state = lcr_util_strdup_s(st);
if (run_flag) {
in->init = c->init_pid(c);
}
......@@ -671,7 +671,7 @@ struct lcr_list *lcr_oci2lcr(const struct lxc_container *c, oci_runtime_spec *co
{
struct lcr_list *lcr_conf = NULL;
lcr_conf = util_common_calloc_s(sizeof(*lcr_conf));
lcr_conf = lcr_util_common_calloc_s(sizeof(*lcr_conf));
if (lcr_conf == NULL) {
goto out_free;
}
......@@ -722,13 +722,13 @@ static int lcr_open_config_file(const char *bundle)
goto out;
}
nret = util_ensure_path(&real_config, config);
nret = lcr_util_ensure_path(&real_config, config);
if (nret < 0) {
ERROR("Failed to ensure path %s", config);
goto out;
}
fd = util_open(real_config, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
fd = lcr_util_open(real_config, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
if (fd == -1) {
ERROR("Create file %s failed, %s", real_config, strerror(errno));
lcr_set_error_message(LCR_ERR_RUNTIME, "Create file %s failed, %s", real_config, strerror(errno));
......@@ -756,7 +756,7 @@ static char *escape_string_encode(const char *src)
return NULL;
}
dst = util_common_calloc_s(2 * len + 1);
dst = lcr_util_common_calloc_s(2 * len + 1);
if (dst == NULL) {
ERROR("Out of memory");
return NULL;
......@@ -814,7 +814,7 @@ static int lcr_spec_write_config(int fd, const struct lcr_list *lcr_conf)
goto cleanup;
}
len = strlen(item->name) + 3 + strlen(item->value) + 1;
line = util_common_calloc_s(len);
line = lcr_util_common_calloc_s(len);
if (line == NULL) {
ERROR("Out of memory");
goto cleanup;
......@@ -866,7 +866,7 @@ char *lcr_get_bundle(const char *lcrpath, const char *name)
/* bundle = lcrpath + '/' + name + '\0' */
len = strlen(lcrpath) + strlen(name) + 2;
bundle = util_common_calloc_s(len);
bundle = lcr_util_common_calloc_s(len);
if (bundle == NULL) {
ERROR("Out of memory");
goto cleanup;
......@@ -961,12 +961,12 @@ static int lcr_write_file(const char *path, const char *data, size_t len)
return -1;
}
if (util_ensure_path(&real_path, path) < 0) {
if (lcr_util_ensure_path(&real_path, path) < 0) {
ERROR("Failed to ensure path %s", path);
goto out_free;
}
fd = util_open(real_path, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
fd = lcr_util_open(real_path, O_CREAT | O_TRUNC | O_CLOEXEC | O_WRONLY, CONFIG_FILE_MODE);
if (fd == -1) {
ERROR("Create file %s failed", real_path);
lcr_set_error_message(LCR_ERR_RUNTIME, "Create file %s failed", real_path);
......
......@@ -28,7 +28,7 @@ struct lcr_list;
#define SAFE_MALLOC(P, size, ret) \
do { \
(P) = util_common_calloc_s((size)); \
(P) = lcr_util_common_calloc_s((size)); \
if ((P) == NULL) { \
ERROR("Out of memory"); \
(ret) = false; \
......
......@@ -52,7 +52,7 @@ void engine_set_log_prefix(const char *prefix)
}
free(g_engine_log_prefix);
g_engine_log_prefix = util_strdup_s(prefix);
g_engine_log_prefix = lcr_util_strdup_s(prefix);
}
/* engine free log prefix */
......@@ -100,7 +100,7 @@ static int open_fifo(const char *fifo_path)
return nret;
}
fifo_fd = util_open(fifo_path, O_RDWR | O_NONBLOCK, 0);
fifo_fd = lcr_util_open(fifo_path, O_RDWR | O_NONBLOCK, 0);
if (fifo_fd == -1) {
COMMAND_ERROR("Open fifo %s failed: %s", fifo_path, strerror(errno));
return -1;
......@@ -185,11 +185,11 @@ int engine_log_enable(const struct engine_log_config *log)
}
free(g_engine_log_module);
g_engine_log_module = util_strdup_s(log->name);
g_engine_log_module = lcr_util_strdup_s(log->name);
full_path = util_strdup_s(log->file);
full_path = lcr_util_strdup_s(log->file);
nret = util_build_dir(full_path);
nret = lcr_util_build_dir(full_path);
if (nret != 0) {
COMMAND_ERROR("failed to create dir for log file");
goto out;
......@@ -262,7 +262,7 @@ static char *parse_timespec_to_human()
return NULL;
}
return util_strdup_s(date_time);
return lcr_util_strdup_s(date_time);
}
/* use to add log to driver */
......
......@@ -144,37 +144,8 @@ error:
return NULL;
}
bool file_exists(const char *path)
{
struct stat s;
if (path == NULL) {
return false;
}
return stat(path, &s) == 0;
}
/* dir exists */
bool dir_exists(const char *path)
{
struct stat s;
int nret;
if (path == NULL) {
return false;
}
nret = stat(path, &s);
if (nret < 0) {
return false;
}
return S_ISDIR(s.st_mode);
}
/* wait for pid */
int wait_for_pid(pid_t pid)
int lcr_wait_for_pid(pid_t pid)
{
int st;
int nret = 0;
......@@ -197,7 +168,7 @@ again:
}
/* wait for pid status */
int wait_for_pid_status(pid_t pid)
int lcr_wait_for_pid_status(pid_t pid)
{
int st;
int nret = 0;
......@@ -232,7 +203,7 @@ static char *do_string_join(const char *sep, const char **parts, size_t parts_le
return res_string;
}
char *util_string_join(const char *sep, const char **parts, size_t len)
char *lcr_util_string_join(const char *sep, const char **parts, size_t len)
{
size_t sep_len;
size_t result_len;
......@@ -258,47 +229,6 @@ char *util_string_join(const char *sep, const char **parts, size_t len)
return do_string_join(sep, parts, len, result_len);
}
int util_mkdir_p(const char *dir, mode_t mode)
{
const char *tmp_pos = NULL;
const char *base = NULL;
char *cur_dir = NULL;
ssize_t len = 0;
if (dir == NULL || strlen(dir) > PATH_MAX) {
goto err_out;
}
tmp_pos = dir;
base = dir;
do {
dir = tmp_pos + strspn(tmp_pos, "/");
tmp_pos = dir + strcspn(dir, "/");
len = dir - base;
if (len <= 0) {
break;
}
cur_dir = strndup(base, (size_t)len);
if (cur_dir == NULL) {
ERROR("strndup failed");
goto err_out;
}
if (*cur_dir) {
if (mkdir(cur_dir, mode) && (errno != EEXIST || !util_dir_exists(cur_dir))) {
ERROR("failed to create directory '%s': %s", cur_dir, strerror(errno));
goto err_out;
}
}
free(cur_dir);
} while (tmp_pos != dir);
return 0;
err_out:
free(cur_dir);
return -1;
}
/* lcr shrink array */
static char **lcr_shrink_array(char **orig_array, size_t new_size)
{
......@@ -312,7 +242,7 @@ static char **lcr_shrink_array(char **orig_array, size_t new_size)
if (new_size > SIZE_MAX / sizeof(char *)) {
return orig_array;
}
res_array = (char **)util_common_calloc_s(new_size * sizeof(char *));
res_array = (char **)lcr_util_common_calloc_s(new_size * sizeof(char *));
if (res_array == NULL) {
return orig_array;
}
......@@ -340,7 +270,7 @@ char **lcr_string_split_and_trim(const char *orig_str, char _sep)
return calloc(1, sizeof(char *));
}
str = util_strdup_s(orig_str);
str = lcr_util_strdup_s(orig_str);
token = strtok_r(str, deli, &reserve_ptr);
while (token != NULL) {
......@@ -356,7 +286,7 @@ char **lcr_string_split_and_trim(const char *orig_str, char _sep)
if (r < 0) {
goto error_out;
}
res_array[count] = util_strdup_s(token);
res_array[count] = lcr_util_strdup_s(token);
count++;
token = strtok_r(NULL, deli, &reserve_ptr);
}
......@@ -410,7 +340,7 @@ int lcr_grow_array(void ***orig_array, size_t *orig_capacity, size_t size, size_
if (add_capacity > SIZE_MAX / sizeof(void *)) {
return -1;
}
add_array = util_common_calloc_s(add_capacity * sizeof(void *));
add_array = lcr_util_common_calloc_s(add_capacity * sizeof(void *));
if (add_array == NULL) {
return -1;
}
......@@ -440,7 +370,7 @@ size_t lcr_array_len(void **orig_array)
}
/* util common malloc s */
void *util_common_calloc_s(size_t size)
void *lcr_util_common_calloc_s(size_t size)
{
if (size == 0) {
return NULL;
......@@ -449,7 +379,7 @@ void *util_common_calloc_s(size_t size)
return calloc(1, size);
}
int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
int lcr_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
{
void *addr = NULL;
......@@ -461,7 +391,7 @@ int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize)
goto err_out;
}
addr = util_common_calloc_s(newsize);
addr = lcr_util_common_calloc_s(newsize);
if (addr == NULL) {
goto err_out;
}
......@@ -478,18 +408,13 @@ err_out:
return -1;
}
bool util_valid_cmd_arg(const char *arg)
{
return arg && strchr(arg, '|') == NULL && strchr(arg, '`') == NULL && strchr(arg, '&') == NULL &&
strchr(arg, ';') == NULL;
}
static inline bool is_invalid_error_str(const char *err_str, const char *numstr)
{
return err_str == NULL || err_str == numstr || *err_str != '\0';
}
int util_safe_strtod(const char *numstr, double *converted)
int lcr_util_safe_strtod(const char *numstr, double *converted)
{
char *err_str = NULL;
double ld;
......@@ -512,32 +437,8 @@ int util_safe_strtod(const char *numstr, double *converted)
return 0;
}
/* util safe ullong */
int util_safe_ullong(const char *numstr, unsigned long long *converted)
{
char *err_str = NULL;
unsigned long long sli;
if (numstr == NULL || converted == NULL) {
return -EINVAL;
}
errno = 0;
sli = strtoull(numstr, &err_str, 0);
if (errno > 0) {
return -errno;
}
if (is_invalid_error_str(err_str, numstr)) {
return -EINVAL;
}
*converted = (unsigned long long)sli;
return 0;
}
/* util safe uint */
int util_safe_uint(const char *numstr, unsigned int *converted)
int lcr_util_safe_uint(const char *numstr, unsigned int *converted)
{
char *err_str = NULL;
unsigned long int ui;
......@@ -619,7 +520,7 @@ static int util_parse_size_int_and_float(const char *numstr, int64_t mlt, int64_
tmp = *(dot - 1);
*(dot - 1) = '0';
// parsing 0.456
nret = util_safe_strtod(dot - 1, &float_size);
nret = lcr_util_safe_strtod(dot - 1, &float_size);
// recover 120.456 to 123.456
*(dot - 1) = tmp;
if (nret < 0) {
......@@ -634,7 +535,7 @@ static int util_parse_size_int_and_float(const char *numstr, int64_t mlt, int64_
}
*dot = '\0';
}
nret = util_safe_llong(numstr, &int_size);
nret = lcr_util_safe_llong(numstr, &int_size);
if (nret < 0) {
return nret;
}
......@@ -654,7 +555,7 @@ static int util_parse_size_int_and_float(const char *numstr, int64_t mlt, int64_
}
/* parse byte size string */
int parse_byte_size_string(const char *s, int64_t *converted)
int lcr_parse_byte_size_string(const char *s, int64_t *converted)
{
int ret;
int64_t mltpl = 0;
......@@ -665,7 +566,7 @@ int parse_byte_size_string(const char *s, int64_t *converted)
return -EINVAL;
}
dup = util_strdup_s(s);
dup = lcr_util_strdup_s(s);
pmlt = dup;
while (*pmlt != '\0' && (isdigit(*pmlt) || *pmlt == '.')) {
......@@ -688,7 +589,7 @@ int parse_byte_size_string(const char *s, int64_t *converted)
/*
* if path do not exist, this function will create it.
*/
int util_ensure_path(char **confpath, const char *path)
int lcr_util_ensure_path(char **confpath, const char *path)
{
int err = -1;
int fd;
......@@ -698,7 +599,7 @@ int util_ensure_path(char **confpath, const char *path)
return -1;
}
fd = util_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, DEFAULT_SECURE_FILE_MODE);
fd = lcr_util_open(path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, DEFAULT_SECURE_FILE_MODE);
if (fd < 0 && errno != EEXIST) {
ERROR("failed to open '%s'", path);
goto err;
......@@ -712,7 +613,7 @@ int util_ensure_path(char **confpath, const char *path)
goto err;
}
*confpath = util_strdup_s(real_path);
*confpath = lcr_util_strdup_s(real_path);
err = EXIT_SUCCESS;
......@@ -721,7 +622,7 @@ err:
}
/* util dir exists */
bool util_dir_exists(const char *path)
bool lcr_util_dir_exists(const char *path)
{
struct stat s;
int nret;
......@@ -775,7 +676,7 @@ static void util_rmdir_one(const char *dirpath, const struct dirent *pdirent, in
}
if (S_ISDIR(fstat.st_mode)) {
if (util_recursive_rmdir(fname, (recursive_depth + 1)) < 0) {
if (lcr_util_recursive_rmdir(fname, (recursive_depth + 1)) < 0) {
*failure = 1;
}
} else {
......@@ -787,7 +688,7 @@ static void util_rmdir_one(const char *dirpath, const struct dirent *pdirent, in
}
/* util recursive rmdir */
int util_recursive_rmdir(const char *dirpath, int recursive_depth)
int lcr_util_recursive_rmdir(const char *dirpath, int recursive_depth)
{
struct dirent *pdirent = NULL;
DIR *directory = NULL;
......@@ -800,7 +701,7 @@ int util_recursive_rmdir(const char *dirpath, int recursive_depth)
goto err_out;
}
if (!util_dir_exists(dirpath)) { /* dir not exists */
if (!lcr_util_dir_exists(dirpath)) { /* dir not exists */
goto err_out;
}
......@@ -833,7 +734,7 @@ err_out:
}
/* util string replace one */
static ssize_t util_string_replace_one(const char *needle, const char *replace, const char *haystack, char **result)
static ssize_t lcr_util_string_replace_one(const char *needle, const char *replace, const char *haystack, char **result)
{
char *res_string = *result;
char *p = NULL;
......@@ -865,7 +766,7 @@ static ssize_t util_string_replace_one(const char *needle, const char *replace,
}
/* util string replace */
char *util_string_replace(const char *needle, const char *replace, const char *haystack)
char *lcr_util_string_replace(const char *needle, const char *replace, const char *haystack)
{
ssize_t length = -1;
ssize_t reserve_len = -1;
......@@ -884,7 +785,7 @@ char *util_string_replace(const char *needle, const char *replace, const char *h
}
reserve_len = length;
}
length = util_string_replace_one(needle, replace, haystack, &res_string);
length = lcr_util_string_replace_one(needle, replace, haystack, &res_string);
if (length < 0) {
free(res_string);
return NULL;
......@@ -903,7 +804,7 @@ char *util_string_replace(const char *needle, const char *replace, const char *h
return res_string;
}
int util_open(const char *filename, int flags, mode_t mode)
int lcr_util_open(const char *filename, int flags, mode_t mode)
{
char rpath[PATH_MAX] = { 0x00 };
......@@ -917,53 +818,7 @@ int util_open(const char *filename, int flags, mode_t mode)
}
}
FILE *util_fopen(const char *filename, const char *mode)
{
unsigned int fdmode = 0;
int f_fd = -1;
int tmperrno;
FILE *fp = NULL;
char rpath[PATH_MAX] = { 0x00 };
if (mode == NULL) {
return NULL;
}
if (cleanpath(filename, rpath, sizeof(rpath)) == NULL) {
ERROR("cleanpath failed");
return NULL;
}
if (!strncmp(mode, "a+", 2)) {
fdmode = O_RDWR | O_CREAT | O_APPEND;
} else if (!strncmp(mode, "a", 1)) {
fdmode = O_WRONLY | O_CREAT | O_APPEND;
} else if (!strncmp(mode, "w+", 2)) {
fdmode = O_RDWR | O_TRUNC | O_CREAT;
} else if (!strncmp(mode, "w", 1)) {
fdmode = O_WRONLY | O_TRUNC | O_CREAT;
} else if (!strncmp(mode, "r+", 2)) {
fdmode = O_RDWR;
} else if (!strncmp(mode, "r", 1)) {
fdmode = O_RDONLY;
}
fdmode |= O_CLOEXEC;
f_fd = open(rpath, (int)fdmode, 0666);
if (f_fd < 0) {
return NULL;
}
fp = fdopen(f_fd, mode);
tmperrno = errno;
if (fp == NULL) {
close(f_fd);
}
errno = tmperrno;
return fp;
}
int util_safe_int(const char *num_str, int *converted)
int lcr_util_safe_int(const char *num_str, int *converted)
{
char *err_str = NULL;
signed long int li;
......@@ -1008,7 +863,7 @@ static bool util_is_std_fileno(int fd)
return fd == STDIN_FILENO || fd == STDOUT_FILENO || fd == STDERR_FILENO;
}
int util_check_inherited(bool closeall, int fd_to_ignore)
int lcr_util_check_inherited(bool closeall, int fd_to_ignore)
{
struct dirent *pdirent = NULL;
int fd = -1;
......@@ -1032,7 +887,7 @@ restart:
continue;
}
if (util_safe_int(pdirent->d_name, &fd) < 0) {
if (lcr_util_safe_int(pdirent->d_name, &fd) < 0) {
continue;
}
......@@ -1054,7 +909,7 @@ restart:
}
/* util string append */
char *util_string_append(const char *post, const char *pre)
char *lcr_util_string_append(const char *post, const char *pre)
{
char *res_string = NULL;
size_t length = 0;
......@@ -1063,17 +918,17 @@ char *util_string_append(const char *post, const char *pre)
return NULL;
}
if (pre == NULL) {
return util_strdup_s(post);
return lcr_util_strdup_s(post);
}
if (post == NULL) {
return util_strdup_s(pre);
return lcr_util_strdup_s(pre);
}
if (strlen(post) > ((SIZE_MAX - strlen(pre)) - 1)) {
ERROR("String is too long to be appended");
return NULL;
}
length = strlen(post) + strlen(pre) + 1;
res_string = util_common_calloc_s(length);
res_string = lcr_util_common_calloc_s(length);
if (res_string == NULL) {
return NULL;
}
......@@ -1084,7 +939,7 @@ char *util_string_append(const char *post, const char *pre)
}
/* util string split prefix */
char *util_string_split_prefix(size_t prefix_len, const char *file)
char *lcr_util_string_split_prefix(size_t prefix_len, const char *file)
{
size_t file_len = 0;
size_t len = 0;
......@@ -1102,7 +957,7 @@ char *util_string_split_prefix(size_t prefix_len, const char *file)
if (len > SIZE_MAX / sizeof(char) - 1) {
return NULL;
}
path = util_common_calloc_s((len + 1) * sizeof(char));
path = lcr_util_common_calloc_s((len + 1) * sizeof(char));
if (path == NULL) {
return NULL;
}
......@@ -1120,7 +975,7 @@ static void set_char_to_terminator(char *p)
* @name is absolute path of this file.
* make all directory in this absolute path.
* */
int util_build_dir(const char *name)
int lcr_util_build_dir(const char *name)
{
char *n = NULL; // because we'll be modifying it
char *p = NULL;
......@@ -1131,7 +986,7 @@ int util_build_dir(const char *name)
return -1;
}
n = util_strdup_s(name);
n = lcr_util_strdup_s(name);
e = &(n[strlen(n)]);
for (p = n + 1; p < e; p++) {
if (*p != '/') {
......@@ -1140,7 +995,7 @@ int util_build_dir(const char *name)
set_char_to_terminator(p);
if (access(n, F_OK)) {
nret = mkdir(n, DEFAULT_SECURE_DIRECTORY_MODE);
if (nret && (errno != EEXIST || !dir_exists(n))) {
if (nret && (errno != EEXIST || !lcr_util_dir_exists(n))) {
ERROR("failed to create directory '%s'.", n);
free(n);
return -1;
......@@ -1153,7 +1008,7 @@ int util_build_dir(const char *name)
}
/* util write nointr */
ssize_t util_write_nointr(int fd, const void *buf, size_t count)
ssize_t lcr_util_write_nointr(int fd, const void *buf, size_t count)
{
ssize_t nret;
......@@ -1175,7 +1030,7 @@ ssize_t util_write_nointr(int fd, const void *buf, size_t count)
}
/* util read nointr */
ssize_t util_read_nointr(int fd, void *buf, size_t count)
ssize_t lcr_util_read_nointr(int fd, void *buf, size_t count)
{
ssize_t nret;
......@@ -1195,102 +1050,8 @@ ssize_t util_read_nointr(int fd, void *buf, size_t count)
return nret;
}
/* util free array */
void util_free_array(char **array)
{
char **p = NULL;
if (array == NULL) {
return;
}
for (p = array; p && *p; p++) {
free(*p);
*p = NULL;
}
free((void *)array);
}
/* sig num */
static int sig_num(const char *sig)
{
int n;
if (util_safe_int(sig, &n) < 0) {
return -1;
}
return n;
}
struct signame {
int num;
const char *name;
};
/* util sig parse */
int util_sig_parse(const char *signame)
{
size_t n;
const struct signame signames[] = SIGNAL_MAP_DEFAULT;
if (signame == NULL) {
return -1;
}
if (isdigit(*signame)) {
return sig_num(signame);
} else if (strncasecmp(signame, "sig", 3) == 0) {
signame += 3;
for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) {
if (strcasecmp(signames[n].name, signame) == 0) {
return signames[n].num;
}
}
} else {
for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) {
if (strcasecmp(signames[n].name, signame) == 0) {
return signames[n].num;
}
}
}
return -1;
}
/* util valid signal */
bool util_valid_signal(int sig)
{
size_t n = 0;
const struct signame signames[] = SIGNAL_MAP_DEFAULT;
for (n = 0; n < sizeof(signames) / sizeof(signames[0]); n++) {
if (signames[n].num == sig) {
return true;
}
}
return false;
}
/* str skip str */
const char *str_skip_str(const char *str, const char *skip)
{
if (str == NULL || skip == NULL) {
return NULL;
}
for (;; str++, skip++) {
if (!*skip) {
return str;
} else if (*str != *skip) {
return NULL;
}
}
}
/* util array len */
size_t util_array_len(char **array)
size_t lcr_util_array_len(char **array)
{
char **pos = NULL;
size_t len = 0;
......@@ -1302,40 +1063,8 @@ size_t util_array_len(char **array)
return len;
}
/* util array append */
int util_array_append(char ***array, const char *element)
{
size_t len;
char **new_array = NULL;
if (array == NULL || element == NULL) {
return -1;
}
// let newlen to len + 2 for element and null
len = util_array_len(*array);
if (len > SIZE_MAX / sizeof(char *) - 2) {
ERROR("Array size is too big!");
return -1;
}
new_array = util_common_calloc_s((len + 2) * sizeof(char *));
if (new_array == NULL) {
ERROR("Out of memory");
return -1;
}
if (*array) {
(void)memcpy(new_array, *array, len * sizeof(char *));
free((void *)*array);
}
*array = new_array;
new_array[len] = util_strdup_s(element);
return 0;
}
/* util safe llong */
int util_safe_llong(const char *numstr, long long *converted)
int lcr_util_safe_llong(const char *numstr, long long *converted)
{
char *err_str = NULL;
long long ll;
......@@ -1358,7 +1087,7 @@ int util_safe_llong(const char *numstr, long long *converted)
return 0;
}
char *util_strdup_s(const char *src)
char *lcr_util_strdup_s(const char *src)
{
char *dst = NULL;
......@@ -1410,7 +1139,7 @@ static int set_stdfds(int fd)
return 0;
}
int util_null_stdfds(void)
int lcr_util_null_stdfds(void)
{
int ret = -1;
int fd;
......@@ -1424,88 +1153,6 @@ int util_null_stdfds(void)
return ret;
}
bool util_copy_file(const char *src_file, const char *dst_file, mode_t mode)
{
bool ret = false;
char *nret = NULL;
char real_src_file[PATH_MAX + 1] = { 0 };
int src_fd = -1;
int dst_fd = -1;
char buf[BUFSIZE + 1] = { 0 };
if (src_file == NULL || dst_file == NULL) {
return ret;
}
nret = realpath(src_file, real_src_file);
if (nret == NULL) {
ERROR("real path: %s, return: %s", src_file, strerror(errno));
return ret;
}
src_fd = util_open(real_src_file, O_RDONLY, CONFIG_FILE_MODE);
if (src_fd < 0) {
ERROR("Open src file: %s, failed: %s", real_src_file, strerror(errno));
goto free_out;
}
dst_fd = util_open(dst_file, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (dst_fd < 0) {
ERROR("Creat file: %s, failed: %s", dst_file, strerror(errno));
goto free_out;
}
while (true) {
ssize_t len = util_read_nointr(src_fd, buf, BUFSIZE);
if (len < 0) {
ERROR("Read src file failed: %s", strerror(errno));
goto free_out;
} else if (len == 0) {
break;
}
if (util_write_nointr(dst_fd, buf, (size_t)len) != len) {
ERROR("Write file failed: %s", strerror(errno));
goto free_out;
}
}
ret = true;
free_out:
if (src_fd >= 0) {
close(src_fd);
}
if (dst_fd >= 0) {
close(dst_fd);
}
return ret;
}
bool util_write_file(const char *filepath, const char *content, size_t len, bool add_newline, mode_t mode)
{
bool ret = true;
int rfd = -1;
if (filepath == NULL || content == NULL) {
return false;
}
rfd = util_open(filepath, O_CREAT | O_TRUNC | O_WRONLY, mode);
if (rfd == -1) {
ERROR("Create file %s failed: %s", filepath, strerror(errno));
return false;
}
if (write(rfd, content, len) == -1) {
ERROR("Write hostname failed: %s", strerror(errno));
ret = false;
goto out_free;
}
if (add_newline && write(rfd, "\n", 1) == -1) {
ERROR("Write new line failed: %s", strerror(errno));
ret = false;
goto out_free;
}
out_free:
close(rfd);
return ret;
}
static void util_trim_newline(char *s)
{
if (s == NULL) {
......@@ -1543,7 +1190,7 @@ static int append_new_content_to_file(FILE *fp, const char *content)
goto out;
}
content_len = strlen(content) + strlen("\n") + 1;
tmp_str = util_common_calloc_s(content_len);
tmp_str = lcr_util_common_calloc_s(content_len);
if (tmp_str == NULL) {
ERROR("Out of memory");
ret = -1;
......@@ -1568,7 +1215,7 @@ out:
return ret;
}
int util_atomic_write_file(const char *filepath, const char *content)
int lcr_util_atomic_write_file(const char *filepath, const char *content)
{
int fd;
int ret = 0;
......@@ -1578,7 +1225,7 @@ int util_atomic_write_file(const char *filepath, const char *content)
if (filepath == NULL || content == NULL) {
return -1;
}
fd = util_open(filepath, O_RDWR | O_CREAT | O_APPEND, DEFAULT_SECURE_FILE_MODE);
fd = lcr_util_open(filepath, O_RDWR | O_CREAT | O_APPEND, DEFAULT_SECURE_FILE_MODE);
if (fd < 0) {
ERROR("Failed to open: %s", filepath);
return -1;
......
......@@ -154,55 +154,40 @@ extern "C" {
{ SIGRTMAX - 1, "RTMAX-1" }, { SIGRTMAX, "RTMAX" }, \
}
bool file_exists(const char *path);
bool dir_exists(const char *path);
int wait_for_pid(pid_t pid);
int wait_for_pid_status(pid_t pid);
char *util_string_join(const char *sep, const char **parts, size_t len);
int util_mkdir_p(const char *dir, mode_t mode);
int lcr_wait_for_pid(pid_t pid);
int lcr_wait_for_pid_status(pid_t pid);
char *lcr_util_string_join(const char *sep, const char **parts, size_t len);
char **lcr_string_split_and_trim(const char *str, char _sep);
void lcr_free_array(void **array);
int lcr_grow_array(void ***array, size_t *capacity, size_t new_size, size_t capacity_increment);
size_t lcr_array_len(void **array);
int mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize);
bool util_valid_cmd_arg(const char *arg);
int util_safe_ullong(const char *numstr, unsigned long long *converted);
int util_safe_strtod(const char *numstr, double *converted);
int util_safe_uint(const char *numstr, unsigned int *converted);
int parse_byte_size_string(const char *s, int64_t *converted);
bool util_dir_exists(const char *path);
int util_ensure_path(char **confpath, const char *path);
int util_recursive_rmdir(const char *dirpath, int recursive_depth);
char *util_string_replace(const char *needle, const char *replacement, const char *haystack);
int util_open(const char *filename, int flags, mode_t mode);
FILE *util_fopen(const char *filename, const char *mode);
void *util_common_calloc_s(size_t size);
int util_safe_int(const char *numstr, int *converted);
int util_check_inherited(bool closeall, int fd_to_ignore);
char *util_string_append(const char *post, const char *pre);
char *util_string_split_prefix(size_t prefix_len, const char *file);
int util_build_dir(const char *name);
ssize_t util_write_nointr(int fd, const void *buf, size_t count);
ssize_t util_read_nointr(int fd, void *buf, size_t count);
void util_free_array(char **array);
int util_sig_parse(const char *signame);
bool util_valid_signal(int sig);
size_t util_array_len(char **array);
const char *str_skip_str(const char *str, const char *skip);
int util_array_append(char ***array, const char *element);
int util_safe_llong(const char *numstr, long long *converted);
char *util_strdup_s(const char *src);
int util_null_stdfds(void);
bool util_copy_file(const char *src_file, const char *dst_file, mode_t mode);
bool util_write_file(const char *filepath, const char *content, size_t len, bool add_newline, mode_t mode);
int util_atomic_write_file(const char *filepath, const char *content);
int lcr_mem_realloc(void **newptr, size_t newsize, void *oldptr, size_t oldsize);
int lcr_util_safe_strtod(const char *numstr, double *converted);
int lcr_util_safe_uint(const char *numstr, unsigned int *converted);
int lcr_parse_byte_size_string(const char *s, int64_t *converted);
bool lcr_util_dir_exists(const char *path);
int lcr_util_ensure_path(char **confpath, const char *path);
int lcr_util_recursive_rmdir(const char *dirpath, int recursive_depth);
char *lcr_util_string_replace(const char *needle, const char *replacement, const char *haystack);
int lcr_util_open(const char *filename, int flags, mode_t mode);
void *lcr_util_common_calloc_s(size_t size);
int lcr_util_safe_int(const char *numstr, int *converted);
int lcr_util_check_inherited(bool closeall, int fd_to_ignore);
char *lcr_util_string_append(const char *post, const char *pre);
char *lcr_util_string_split_prefix(size_t prefix_len, const char *file);
int lcr_util_build_dir(const char *name);
ssize_t lcr_util_write_nointr(int fd, const void *buf, size_t count);
ssize_t lcr_util_read_nointr(int fd, void *buf, size_t count);
size_t lcr_util_array_len(char **array);
int lcr_util_safe_llong(const char *numstr, long long *converted);
char *lcr_util_strdup_s(const char *src);
int lcr_util_null_stdfds(void);
int lcr_util_atomic_write_file(const char *filepath, const char *content);
#ifdef __cplusplus
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册