git.droids-corp.org
/
dpdk.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
net/mlx5: make tunnel hub list thread safe
[dpdk.git]
/
lib
/
librte_cfgfile
/
rte_cfgfile.c
diff --git
a/lib/librte_cfgfile/rte_cfgfile.c
b/lib/librte_cfgfile/rte_cfgfile.c
index
7d8c941
..
0020222
100644
(file)
--- a/
lib/librte_cfgfile/rte_cfgfile.c
+++ b/
lib/librte_cfgfile/rte_cfgfile.c
@@
-7,7
+7,9
@@
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
+#include <rte_string_fns.h>
#include <rte_common.h>
#include <rte_common.h>
+#include <rte_log.h>
#include "rte_cfgfile.h"
#include "rte_cfgfile.h"
@@
-25,6
+27,12
@@
struct rte_cfgfile {
struct rte_cfgfile_section *sections;
};
struct rte_cfgfile_section *sections;
};
+RTE_LOG_REGISTER(cfgfile_logtype, lib.cfgfile, INFO);
+
+#define CFG_LOG(level, fmt, args...) \
+ rte_log(RTE_LOG_ ## level, cfgfile_logtype, "%s(): " fmt "\n", \
+ __func__, ## args)
+
/** when we resize a file structure, how many extra entries
* for new sections do we add in */
#define CFG_ALLOC_SECTION_BATCH 8
/** when we resize a file structure, how many extra entries
* for new sections do we add in */
#define CFG_ALLOC_SECTION_BATCH 8
@@
-113,9
+121,8
@@
_add_entry(struct rte_cfgfile_section *section, const char *entryname,
struct rte_cfgfile_entry *curr_entry =
§ion->entries[section->num_entries];
struct rte_cfgfile_entry *curr_entry =
§ion->entries[section->num_entries];
- snprintf(curr_entry->name, sizeof(curr_entry->name), "%s", entryname);
- snprintf(curr_entry->value,
- sizeof(curr_entry->value), "%s", entryvalue);
+ strlcpy(curr_entry->name, entryname, sizeof(curr_entry->name));
+ strlcpy(curr_entry->value, entryvalue, sizeof(curr_entry->value));
section->num_entries++;
return 0;
section->num_entries++;
return 0;
@@
-128,7
+135,7
@@
rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
unsigned int i;
if (!params) {
unsigned int i;
if (!params) {
-
printf("Error -
missing cfgfile parameters\n");
+
CFG_LOG(ERR, "
missing cfgfile parameters\n");
return -EINVAL;
}
return -EINVAL;
}
@@
-141,7
+148,7
@@
rte_cfgfile_check_params(const struct rte_cfgfile_parameters *params)
}
if (valid_comment == 0) {
}
if (valid_comment == 0) {
-
printf("Error -
invalid comment characters %c\n",
+
CFG_LOG(ERR, "
invalid comment characters %c\n",
params->comment_character);
return -ENOTSUP;
}
params->comment_character);
return -ENOTSUP;
}
@@
-160,9
+167,9
@@
struct rte_cfgfile *
rte_cfgfile_load_with_params(const char *filename, int flags,
const struct rte_cfgfile_parameters *params)
{
rte_cfgfile_load_with_params(const char *filename, int flags,
const struct rte_cfgfile_parameters *params)
{
- char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4]
= {0}
;
+ char buffer[CFG_NAME_LEN + CFG_VALUE_LEN + 4];
int lineno = 0;
int lineno = 0;
- struct rte_cfgfile *cfg
= NULL
;
+ struct rte_cfgfile *cfg;
if (rte_cfgfile_check_params(params))
return NULL;
if (rte_cfgfile_check_params(params))
return NULL;
@@
-174,17
+181,18
@@
rte_cfgfile_load_with_params(const char *filename, int flags,
cfg = rte_cfgfile_create(flags);
while (fgets(buffer, sizeof(buffer), f) != NULL) {
cfg = rte_cfgfile_create(flags);
while (fgets(buffer, sizeof(buffer), f) != NULL) {
- char *pos
= NULL
;
+ char *pos;
size_t len = strnlen(buffer, sizeof(buffer));
lineno++;
if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
size_t len = strnlen(buffer, sizeof(buffer));
lineno++;
if ((len >= sizeof(buffer) - 1) && (buffer[len-1] != '\n')) {
-
printf("Error
line %d - no \\n found on string. "
+
CFG_LOG(ERR, "
line %d - no \\n found on string. "
"Check if line too long\n", lineno);
goto error1;
}
/* skip parsing if comment character found */
pos = memchr(buffer, params->comment_character, len);
"Check if line too long\n", lineno);
goto error1;
}
/* skip parsing if comment character found */
pos = memchr(buffer, params->comment_character, len);
- if (pos != NULL && (*(pos-1) != '\\')) {
+ if (pos != NULL &&
+ (pos == buffer || *(pos-1) != '\\')) {
*pos = '\0';
len = pos - buffer;
}
*pos = '\0';
len = pos - buffer;
}
@@
-198,8
+206,9
@@
rte_cfgfile_load_with_params(const char *filename, int flags,
/* section heading line */
char *end = memchr(buffer, ']', len);
if (end == NULL) {
/* section heading line */
char *end = memchr(buffer, ']', len);
if (end == NULL) {
- printf("Error line %d - no terminating ']'"
- "character found\n", lineno);
+ CFG_LOG(ERR,
+ "line %d - no terminating ']' character found\n",
+ lineno);
goto error1;
}
*end = '\0';
goto error1;
}
*end = '\0';
@@
-213,8
+222,9
@@
rte_cfgfile_load_with_params(const char *filename, int flags,
split[0] = buffer;
split[1] = memchr(buffer, '=', len);
if (split[1] == NULL) {
split[0] = buffer;
split[1] = memchr(buffer, '=', len);
if (split[1] == NULL) {
- printf("Error line %d - no '='"
- "character found\n", lineno);
+ CFG_LOG(ERR,
+ "line %d - no '=' character found\n",
+ lineno);
goto error1;
}
*split[1] = '\0';
goto error1;
}
*split[1] = '\0';
@@
-224,10
+234,11
@@
rte_cfgfile_load_with_params(const char *filename, int flags,
_strip(split[1], strlen(split[1]));
char *end = memchr(split[1], '\\', strlen(split[1]));
_strip(split[1], strlen(split[1]));
char *end = memchr(split[1], '\\', strlen(split[1]));
+ size_t split_len = strlen(split[1]) + 1;
while (end != NULL) {
if (*(end+1) == params->comment_character) {
*end = '\0';
while (end != NULL) {
if (*(end+1) == params->comment_character) {
*end = '\0';
- str
cat(split[1], end+1
);
+ str
lcat(split[1], end+1, split_len
);
} else
end++;
end = memchr(end, '\\', strlen(end));
} else
end++;
end = memchr(end, '\\', strlen(end));
@@
-235,8
+246,9
@@
rte_cfgfile_load_with_params(const char *filename, int flags,
if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
(*split[1] == '\0')) {
if (!(flags & CFG_FLAG_EMPTY_VALUES) &&
(*split[1] == '\0')) {
- printf("Error at line %d - cannot use empty "
- "values\n", lineno);
+ CFG_LOG(ERR,
+ "line %d - cannot use empty values\n",
+ lineno);
goto error1;
}
goto error1;
}
@@
-259,7
+271,11
@@
struct rte_cfgfile *
rte_cfgfile_create(int flags)
{
int i;
rte_cfgfile_create(int flags)
{
int i;
- struct rte_cfgfile *cfg = NULL;
+ struct rte_cfgfile *cfg;
+
+ /* future proof flags usage */
+ if (flags & ~(CFG_FLAG_GLOBAL_SECTION | CFG_FLAG_EMPTY_VALUES))
+ return NULL;
cfg = malloc(sizeof(*cfg));
cfg = malloc(sizeof(*cfg));
@@
-270,17
+286,16
@@
rte_cfgfile_create(int flags)
cfg->num_sections = 0;
/* allocate first batch of sections and entries */
cfg->num_sections = 0;
/* allocate first batch of sections and entries */
- cfg->sections = malloc(sizeof(struct rte_cfgfile_section) *
- CFG_ALLOC_SECTION_BATCH);
-
+ cfg->sections = calloc(CFG_ALLOC_SECTION_BATCH,
+ sizeof(struct rte_cfgfile_section));
if (cfg->sections == NULL)
goto error1;
cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
if (cfg->sections == NULL)
goto error1;
cfg->allocated_sections = CFG_ALLOC_SECTION_BATCH;
for (i = 0; i < CFG_ALLOC_SECTION_BATCH; i++) {
- cfg->sections[i].entries =
malloc(sizeof(
-
struct rte_cfgfile_entry) * CFG_ALLOC_ENTRY_BATCH
);
+ cfg->sections[i].entries =
calloc(CFG_ALLOC_ENTRY_BATCH,
+
sizeof(struct rte_cfgfile_entry)
);
if (cfg->sections[i].entries == NULL)
goto error1;
if (cfg->sections[i].entries == NULL)
goto error1;
@@
-341,8
+356,8
@@
rte_cfgfile_add_section(struct rte_cfgfile *cfg, const char *sectionname)
cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
}
cfg->allocated_sections += CFG_ALLOC_SECTION_BATCH;
}
- s
nprintf(cfg->sections[cfg->num_sections].
name,
-
sizeof(cfg->sections[0].name), "%s", sectionname
);
+ s
trlcpy(cfg->sections[cfg->num_sections].name, section
name,
+
sizeof(cfg->sections[0].name)
);
cfg->sections[cfg->num_sections].num_entries = 0;
cfg->num_sections++;
cfg->sections[cfg->num_sections].num_entries = 0;
cfg->num_sections++;
@@
-392,12
+407,12
@@
int rte_cfgfile_set_entry(struct rte_cfgfile *cfg, const char *sectionname,
for (i = 0; i < curr_section->num_entries; i++)
if (!strcmp(curr_section->entries[i].name, entryname)) {
for (i = 0; i < curr_section->num_entries; i++)
if (!strcmp(curr_section->entries[i].name, entryname)) {
- snprintf(curr_section->entries[i].value,
- sizeof(curr_section->entries[i].value),
- "%s", entryvalue);
+ strlcpy(curr_section->entries[i].value, entryvalue,
+ sizeof(curr_section->entries[i].value));
return 0;
}
return 0;
}
- printf("Error - entry name doesn't exist\n");
+
+ CFG_LOG(ERR, "entry name doesn't exist\n");
return -EINVAL;
}
return -EINVAL;
}
@@
-468,8
+483,7
@@
rte_cfgfile_sections(struct rte_cfgfile *cfg, char *sections[],
int i;
for (i = 0; i < cfg->num_sections && i < max_sections; i++)
int i;
for (i = 0; i < cfg->num_sections && i < max_sections; i++)
- snprintf(sections[i], CFG_NAME_LEN, "%s",
- cfg->sections[i].name);
+ strlcpy(sections[i], cfg->sections[i].name, CFG_NAME_LEN);
return i;
}
return i;
}
@@
-499,7
+513,7
@@
rte_cfgfile_section_num_entries_by_index(struct rte_cfgfile *cfg,
const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
const struct rte_cfgfile_section *sect = &(cfg->sections[index]);
- s
nprintf(sectionname, CFG_NAME_LEN, "%s", sect->name
);
+ s
trlcpy(sectionname, sect->name, CFG_NAME_LEN
);
return sect->num_entries;
}
int
return sect->num_entries;
}
int
@@
-526,7
+540,7
@@
rte_cfgfile_section_entries_by_index(struct rte_cfgfile *cfg, int index,
if (index < 0 || index >= cfg->num_sections)
return -1;
sect = &cfg->sections[index];
if (index < 0 || index >= cfg->num_sections)
return -1;
sect = &cfg->sections[index];
- s
nprintf(sectionname, CFG_NAME_LEN, "%s", sect->name
);
+ s
trlcpy(sectionname, sect->name, CFG_NAME_LEN
);
for (i = 0; i < max_entries && i < sect->num_entries; i++)
entries[i] = sect->entries[i];
return i;
for (i = 0; i < max_entries && i < sect->num_entries; i++)
entries[i] = sect->entries[i];
return i;