251 lines
10 KiB
Diff
251 lines
10 KiB
Diff
From e782318607aa5f1b2cf2fcf003a5a12066877714 Mon Sep 17 00:00:00 2001
|
|
From: haozi007 <liuhao27@huawei.com>
|
|
Date: Thu, 7 Sep 2023 07:39:18 +0000
|
|
Subject: [PATCH 2/2] !47 remove unnecessary strerror * remove unnecessary
|
|
strerror
|
|
|
|
---
|
|
src/api.c | 36 ++++++++++++++++++++----------------
|
|
src/conf.c | 12 ++++++------
|
|
src/invoke/exec.c | 17 +++++++++--------
|
|
src/invoke/tools.c | 2 +-
|
|
src/types/types.c | 6 ++++--
|
|
src/utils.c | 2 +-
|
|
6 files changed, 41 insertions(+), 34 deletions(-)
|
|
|
|
diff --git a/src/api.c b/src/api.c
|
|
index 460223f..4541496 100644
|
|
--- a/src/api.c
|
|
+++ b/src/api.c
|
|
@@ -299,6 +299,22 @@ out:
|
|
return ret;
|
|
}
|
|
|
|
+static void format_invoke_err_msg(const char *name, int save_errno, char **err)
|
|
+{
|
|
+ const char *invoke_err = get_invoke_err_msg(save_errno);
|
|
+
|
|
+ if (asprintf(err, "find plugin: \"%s\" failed: %s", name, invoke_err != NULL ? invoke_err : "") < 0) {
|
|
+ *err = clibcni_util_strdup_s("Out of memory");
|
|
+ }
|
|
+
|
|
+ if (invoke_err != NULL) {
|
|
+ ERROR("find plugin: \"%s\" failed: %s", name, invoke_err);
|
|
+ return;
|
|
+ }
|
|
+ errno = save_errno;
|
|
+ SYSERROR("find plugin: \"%s\" failed", name);
|
|
+}
|
|
+
|
|
static int run_cni_plugin(const struct network_config_list *list, size_t i, const char *operator,
|
|
const struct runtime_conf *rc, const char * const *paths, size_t paths_len,
|
|
struct result **pret, char **err)
|
|
@@ -320,10 +336,7 @@ static int run_cni_plugin(const struct network_config_list *list, size_t i, cons
|
|
|
|
ret = find_in_path(net.network->type, paths, paths_len, &plugin_path, &save_errno);
|
|
if (ret != 0) {
|
|
- if (asprintf(err, "find plugin: \"%s\" failed: %s", net.network->type, get_invoke_err_msg(save_errno)) < 0) {
|
|
- *err = clibcni_util_strdup_s("Out of memory");
|
|
- }
|
|
- ERROR("find plugin: \"%s\" failed: %s", net.network->type, get_invoke_err_msg(save_errno));
|
|
+ format_invoke_err_msg(net.network->type, save_errno, err);
|
|
goto free_out;
|
|
}
|
|
|
|
@@ -448,10 +461,7 @@ static int add_network(const struct network_config *net, const struct runtime_co
|
|
}
|
|
ret = find_in_path(net->network->type, paths, paths_len, &plugin_path, &save_errno);
|
|
if (ret != 0) {
|
|
- if (asprintf(err, "find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno)) < 0) {
|
|
- *err = clibcni_util_strdup_s("Out of memory");
|
|
- }
|
|
- ERROR("find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno));
|
|
+ format_invoke_err_msg(net->network->type, save_errno, err);
|
|
goto free_out;
|
|
}
|
|
|
|
@@ -496,10 +506,7 @@ static int del_network(const struct network_config *net, const struct runtime_co
|
|
}
|
|
ret = find_in_path(net->network->type, paths, paths_len, &plugin_path, &save_errno);
|
|
if (ret != 0) {
|
|
- if (asprintf(err, "find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno)) < 0) {
|
|
- *err = clibcni_util_strdup_s("Out of memory");
|
|
- }
|
|
- ERROR("find plugin: \"%s\" failed: %s", net->network->type, get_invoke_err_msg(save_errno));
|
|
+ format_invoke_err_msg(net->network->type, save_errno, err);
|
|
goto free_out;
|
|
}
|
|
|
|
@@ -814,10 +821,7 @@ int cni_get_version_info(const char *plugin_type, char **paths, struct plugin_in
|
|
len = clibcni_util_array_len((const char * const *)paths);
|
|
ret = find_in_path(plugin_type, (const char * const *)paths, len, &plugin_path, &save_errno);
|
|
if (ret != 0) {
|
|
- if (asprintf(err, "find plugin: \"%s\" failed: %s", plugin_type, get_invoke_err_msg(save_errno)) < 0) {
|
|
- *err = clibcni_util_strdup_s("Out of memory");
|
|
- }
|
|
- ERROR("find plugin: \"%s\" failed: %s", plugin_type, get_invoke_err_msg(save_errno));
|
|
+ format_invoke_err_msg(plugin_type, save_errno, err);
|
|
return ret;
|
|
}
|
|
|
|
diff --git a/src/conf.c b/src/conf.c
|
|
index a3214b3..4d74b73 100644
|
|
--- a/src/conf.c
|
|
+++ b/src/conf.c
|
|
@@ -107,10 +107,10 @@ static char *do_get_cni_net_confs_json(const char *filename, char **err)
|
|
|
|
content = clibcni_util_read_text_file(filename);
|
|
if (content == NULL) {
|
|
- if (asprintf(err, "Read file %s failed: %s", filename, strerror(errno)) < 0) {
|
|
+ SYSERROR("Read file %s failed", filename);
|
|
+ if (asprintf(err, "Read file %s failed", filename) < 0) {
|
|
*err = clibcni_util_strdup_s("Read file failed");
|
|
}
|
|
- ERROR("Read file %s failed: %s", filename, strerror(errno));
|
|
}
|
|
|
|
return content;
|
|
@@ -303,10 +303,10 @@ static int check_conf_dir(const char *dir, DIR **directory, char **err)
|
|
if (errno == ENOENT) {
|
|
return 0;
|
|
}
|
|
- if (asprintf(err, "Open dir failed: %s", strerror(errno)) < 0) {
|
|
+ SYSERROR("Open dir: %s failed", dir);
|
|
+ if (asprintf(err, "Open dir: %s failed", dir) < 0) {
|
|
*err = clibcni_util_strdup_s("Out of memory");
|
|
}
|
|
- SYSERROR("Open dir failed");
|
|
return -1;
|
|
}
|
|
return 1;
|
|
@@ -319,11 +319,11 @@ static int do_check_file_is_valid(const char *fname, int *result, char **err)
|
|
|
|
nret = lstat(fname, &tmp_fstat);
|
|
if (nret != 0) {
|
|
- nret = asprintf(err, "lstat %s failed: %s", fname, strerror(errno));
|
|
+ SYSERROR("lstat %s failed", fname);
|
|
+ nret = asprintf(err, "check file %s failed", fname);
|
|
if (nret < 0) {
|
|
*err = clibcni_util_strdup_s("Out of memory");
|
|
}
|
|
- SYSERROR("lstat %s failed", fname);
|
|
*result = -1;
|
|
return -1;
|
|
}
|
|
diff --git a/src/invoke/exec.c b/src/invoke/exec.c
|
|
index 16d53ea..4ce4b5d 100644
|
|
--- a/src/invoke/exec.c
|
|
+++ b/src/invoke/exec.c
|
|
@@ -354,7 +354,8 @@ static int prepare_raw_exec(const char *plugin_path, int pipe_stdin[2], int pipe
|
|
|
|
ret = pipe2(pipe_stdin, O_CLOEXEC | O_NONBLOCK);
|
|
if (ret < 0) {
|
|
- ret = snprintf(errmsg, len, "Pipe stdin failed: %s", strerror(errno));
|
|
+ SYSERROR("Pipe stdin failed");
|
|
+ ret = snprintf(errmsg, len, "Pipe stdin failed");
|
|
if (ret < 0 || (size_t)ret >= len) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
@@ -363,7 +364,8 @@ static int prepare_raw_exec(const char *plugin_path, int pipe_stdin[2], int pipe
|
|
|
|
ret = pipe2(pipe_stdout, O_CLOEXEC | O_NONBLOCK);
|
|
if (ret < 0) {
|
|
- ret = snprintf(errmsg, len, "Pipe stdout failed: %s", strerror(errno));
|
|
+ SYSERROR("Pipe stdout failed");
|
|
+ ret = snprintf(errmsg, len, "Pipe stdout failed");
|
|
if (ret < 0 || (size_t)ret >= len) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
@@ -383,7 +385,7 @@ static int write_stdin_data_to_child(int pipe_stdin[2], const char *stdin_data,
|
|
|
|
len = strlen(stdin_data);
|
|
if (clibcni_util_write_nointr(pipe_stdin[1], stdin_data, len) != (ssize_t)len) {
|
|
- ret = snprintf(errmsg, errmsg_len, "Write stdin data failed: %s", strerror(errno));
|
|
+ ret = snprintf(errmsg, errmsg_len, "Write stdin data failed");
|
|
if (ret < 0 || (size_t)ret >= errmsg_len) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
@@ -406,8 +408,7 @@ static int read_child_stdout_msg(const int pipe_stdout[2], char *errmsg, size_t
|
|
char buffer[CLIBCNI_BUFFER_SIZE] = { 0 };
|
|
ssize_t tmp_len = clibcni_util_read_nointr(pipe_stdout[0], buffer, CLIBCNI_BUFFER_SIZE - 1);
|
|
if (tmp_len < 0) {
|
|
- ret = snprintf(errmsg, errmsg_len, "%s; read stdout failed: %s", strlen(errmsg) > 0 ? errmsg : "",
|
|
- strerror(errno));
|
|
+ ret = snprintf(errmsg, errmsg_len, "%s; read stdout failed", strlen(errmsg) > 0 ? errmsg : "");
|
|
if (ret < 0 || (size_t)ret >= errmsg_len) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
@@ -437,8 +438,7 @@ static int wait_pid_for_raw_exec_child(pid_t child_pid, const int pipe_stdout[2]
|
|
ret = read_child_stdout_msg(pipe_stdout, errmsg, errmsg_len, stdout_str);
|
|
|
|
if (wait_pid < 0) {
|
|
- ret = snprintf(errmsg, errmsg_len, "%s; waitpid failed: %s", strlen(errmsg) > 0 ? errmsg : "",
|
|
- strerror(errno));
|
|
+ ret = snprintf(errmsg, errmsg_len, "%s; waitpid failed", strlen(errmsg) > 0 ? errmsg : "");
|
|
if (ret < 0 || (size_t)ret >= errmsg_len) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
@@ -571,7 +571,8 @@ static int raw_exec(const char *plugin_path, const char *stdin_data, char * cons
|
|
|
|
child_pid = fork();
|
|
if (child_pid < 0) {
|
|
- ret = snprintf(errmsg, sizeof(errmsg), "Fork failed: %s", strerror(errno));
|
|
+ SYSERROR("Fork failed");
|
|
+ ret = snprintf(errmsg, sizeof(errmsg), "Fork failed");
|
|
if (ret < 0 || (size_t)ret >= sizeof(errmsg)) {
|
|
ERROR("Sprintf failed");
|
|
}
|
|
diff --git a/src/invoke/tools.c b/src/invoke/tools.c
|
|
index 7660be1..7c497f1 100644
|
|
--- a/src/invoke/tools.c
|
|
+++ b/src/invoke/tools.c
|
|
@@ -52,7 +52,7 @@ const char *get_invoke_err_msg(int errcode)
|
|
if (errcode <= INK_SUCCESS) {
|
|
return g_CNI_INVOKE_ERR_MSGS[errcode - (INK_ERR_MIN)];
|
|
}
|
|
- return strerror(errcode);
|
|
+ return NULL;
|
|
}
|
|
|
|
static int do_check_file(const char *plugin, const char *path, char **find_path, int *save_errno)
|
|
diff --git a/src/types/types.c b/src/types/types.c
|
|
index 24e3f1b..fad396b 100644
|
|
--- a/src/types/types.c
|
|
+++ b/src/types/types.c
|
|
@@ -665,7 +665,8 @@ static int do_parse_ipv6_from_str(const char *addr, struct in6_addr *ipv6, uint8
|
|
}
|
|
nret = inet_pton(AF_INET6, addr, ipv6);
|
|
if (nret < 0) {
|
|
- nret = asprintf(err, "ipv6 inet_pton %s", strerror(errno));
|
|
+ SYSERROR("ipv6 inet_pton for: %s", addr);
|
|
+ nret = asprintf(err, "invalid ipv6 addr %s.", addr);
|
|
if (nret < 0) {
|
|
ERROR("Sprintf failed");
|
|
*ret = 1;
|
|
@@ -706,7 +707,8 @@ int parse_ip_from_str(const char *addr, uint8_t **ips, size_t *len, char **err)
|
|
}
|
|
nret = inet_pton(AF_INET, addr, &ipv4);
|
|
if (nret < 0) {
|
|
- nret = asprintf(err, "ipv4 inet_pton %s", strerror(errno));
|
|
+ SYSERROR("ipv4 inet_pton for: %s", addr);
|
|
+ nret = asprintf(err, "invalid ipv4 addr %s.", addr);
|
|
if (nret < 0) {
|
|
ERROR("Sprintf failed");
|
|
ret = 1;
|
|
diff --git a/src/utils.c b/src/utils.c
|
|
index 8efa330..d4a8ead 100644
|
|
--- a/src/utils.c
|
|
+++ b/src/utils.c
|
|
@@ -697,7 +697,7 @@ char *clibcni_util_read_text_file(const char *path)
|
|
|
|
readlen = fread(buf, 1, (size_t)len, filp);
|
|
if (((readlen < (size_t)len) && (!feof(filp))) || (readlen > (size_t)len)) {
|
|
- ERROR("Failed to read file %s, error: %s\n", path, strerror(errno));
|
|
+ SYSERROR("Failed to read file %s", path);
|
|
free(buf);
|
|
buf = NULL;
|
|
goto err_out;
|
|
--
|
|
2.33.0
|
|
|