Using local uci_context variables instead of global one

This commit is contained in:
Omar Kallel 2023-02-02 11:32:52 +01:00
parent e83fb9e3c6
commit d26993f3a8
13 changed files with 208 additions and 188 deletions

View file

@ -278,7 +278,6 @@ void get_firewall_zone_name_by_wan_iface(char *if_wan, char **zone_name)
int get_firewall_restart_state(char **state)
{
cwmp_uci_reinit();
return uci_get_state_value(UCI_CPE_FIREWALL_RESTART_STATE, state);
}

View file

@ -603,12 +603,10 @@ void cwmp_config_load()
{
int ret;
cwmp_uci_reinit();
ret = global_conf_init();
while (ret != CWMP_OK && cwmp_stop != true) {
CWMP_LOG(DEBUG, "Error reading uci ret = %d", ret);
sleep(UCI_OPTION_READ_INTERVAL);
cwmp_uci_reinit();
ret = global_conf_init();
}
}

View file

@ -305,7 +305,6 @@ static void cwmp_free()
bkp_tree_clean();
icwmp_uloop_ubus_exit();
icwmp_cleanmem();
cwmp_uci_exit();
rpc_exit();
clean_cwmp_session_structure();
FREE(cwmp_main);
@ -342,7 +341,6 @@ static void configure_var_state()
if (!file_exists(VARSTATE_CONFIG"/cwmp"))
creat(VARSTATE_CONFIG"/cwmp", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
cwmp_uci_reinit();
cwmp_uci_add_section_with_specific_name("cwmp", "acs", "acs", UCI_VARSTATE_CONFIG);
cwmp_uci_add_section_with_specific_name("cwmp", "cpe", "cpe", UCI_VARSTATE_CONFIG);

View file

@ -320,7 +320,6 @@ char* execute_cwmp_cli_command(char *cmd, char *args[])
char *fault = NULL, *fault_ret = NULL;
size_t i;
size_t commands_array_size = sizeof(icwmp_commands) / sizeof(struct cwmp_cli_command_struct);
cwmp_uci_init();
for (i = 0; i < commands_array_size; i++) {
if (strcmp(icwmp_commands[i].command_name, cmd) == 0) {
fault = icwmp_commands[i].cmd_exec_func(cmd_in, &cmd_out);
@ -337,6 +336,5 @@ cli_help:
cli_end:
icwmp_cleanmem();
cwmp_uci_exit();
return fault_ret;
}

View file

@ -18,8 +18,8 @@
#include "log.h"
struct uci_paths uci_save_conf_paths[] = {
[UCI_STANDARD_CONFIG] = { "/etc/config", "/tmp/.uci", NULL },
[UCI_VARSTATE_CONFIG] = { "/var/state", NULL, NULL }
[UCI_STANDARD_CONFIG] = { "/etc/config", "/tmp/.uci" },
[UCI_VARSTATE_CONFIG] = { "/var/state", NULL }
};
/*
@ -107,50 +107,27 @@ lookup:
* UCI INIT EXIT
*/
void cwmp_uci_init_by_uci_path(uci_config_paths uci_path)
struct uci_context *cwmp_uci_init_by_uci_path(uci_config_paths uci_path)
{
if(uci_save_conf_paths[uci_path].uci_ctx != NULL)
return;
uci_save_conf_paths[uci_path].uci_ctx = uci_alloc_context();
if ( uci_save_conf_paths[uci_path].uci_ctx == NULL)
return;
uci_add_delta_path(uci_save_conf_paths[uci_path].uci_ctx, uci_save_conf_paths[uci_path].uci_ctx->savedir);
uci_set_savedir(uci_save_conf_paths[uci_path].uci_ctx, uci_save_conf_paths[uci_path].save_dir);
uci_set_confdir(uci_save_conf_paths[uci_path].uci_ctx, uci_save_conf_paths[uci_path].conf_dir);
struct uci_context *uci_ctx = uci_alloc_context();
if (uci_ctx == NULL)
return NULL;
uci_add_delta_path(uci_ctx, uci_ctx->savedir);
uci_set_savedir(uci_ctx, uci_save_conf_paths[uci_path].save_dir);
uci_set_confdir(uci_ctx, uci_save_conf_paths[uci_path].conf_dir);
return uci_ctx;
}
void cwmp_uci_exit_by_uci_path(uci_config_paths uci_path)
void cwmp_uci_exit(struct uci_context *uci_ctx)
{
if (uci_save_conf_paths[uci_path].uci_ctx) {
uci_free_context(uci_save_conf_paths[uci_path].uci_ctx);
uci_save_conf_paths[uci_path].uci_ctx = NULL;
if (uci_ctx) {
uci_free_context(uci_ctx);
uci_ctx = NULL;
}
}
int cwmp_uci_init(void)
{
unsigned int i = 0;
for (i = 0; i < ARRAY_SIZE(uci_save_conf_paths); i++) {
cwmp_uci_init_by_uci_path(i);
}
return 0;
}
void cwmp_uci_exit(void)
{
unsigned int i = 0;
for (i = 0; i < ARRAY_SIZE(uci_save_conf_paths); i++) {
cwmp_uci_exit_by_uci_path(i);
}
}
void cwmp_uci_reinit(void)
{
cwmp_uci_exit();
cwmp_uci_init();
}
/*
* UCI GET option value
@ -159,11 +136,12 @@ int cwmp_uci_get_option_value_string(char *package, char *section, char *option,
{
struct uci_ptr ptr = { 0 };
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section == NULL || option == NULL) {
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || section == NULL || option == NULL) {
*value = NULL;
return UCI_ERR_NOTFOUND;
}
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, option, NULL) != UCI_OK) {
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, option, NULL) != UCI_OK) {
*value = NULL;
return UCI_ERR_PARSE;
}
@ -175,6 +153,7 @@ int cwmp_uci_get_option_value_string(char *package, char *section, char *option,
*value = NULL;
return UCI_ERR_NOTFOUND;
}
cwmp_uci_exit(uci_ctx);
return UCI_OK;
}
@ -182,28 +161,34 @@ int cwmp_uci_get_value_by_path(char *path, uci_config_paths uci_path, char **val
{
struct uci_ptr ptr;
char *s;
int ret = UCI_ERR_NOTFOUND;
*value = NULL;
if (path == NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (path == NULL || uci_ctx == NULL)
return UCI_ERR_NOTFOUND;
s = strdup(path);
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, s, true) != UCI_OK) {
if (uci_lookup_ptr(uci_ctx, &ptr, s, true) != UCI_OK) {
CWMP_LOG(ERROR, "Error occurred in uci get %s", path);
free(s);
return UCI_ERR_PARSE;
ret = UCI_ERR_PARSE;
goto end;
}
free(s);
if (ptr.flags & UCI_LOOKUP_COMPLETE) {
if (ptr.o == NULL || ptr.o->v.string == NULL) {
CWMP_LOG(INFO, "%s not found or empty value", path);
return UCI_OK;
ret = UCI_OK;
goto end;
}
*value = strdup(ptr.o->v.string);
return UCI_OK;
ret = UCI_OK;
goto end;
}
return UCI_ERR_NOTFOUND;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
int uci_get_state_value(char *path, char **value)
@ -232,7 +217,7 @@ int cwmp_uci_get_value_by_section_string(struct uci_section *s, char *option, ch
uci_foreach_element(&s->options, e)
{
o = (uci_to_option(e));
if (o && o->e.name && !strcmp(o->e.name, option)) {
if (o && o->e.name && strcmp(o->e.name, option) == 0) {
if (o->type == UCI_TYPE_LIST) {
*value = cwmp_uci_list_to_string(&o->v.list, " ");
} else {
@ -297,15 +282,17 @@ int cwmp_uci_get_value_by_section_list(struct uci_section *s, char *option, stru
int cwmp_uci_set_value_string(char *package, char *section, char *option, char *value, uci_config_paths uci_path)
{
struct uci_ptr ptr = {0};
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section == NULL)
if (uci_ctx == NULL || package == NULL || section == NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, option, value))
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, option, value))
return UCI_ERR_PARSE;
if (uci_set(uci_save_conf_paths[uci_path].uci_ctx, &ptr) != UCI_OK)
if (uci_set(uci_ctx, &ptr) != UCI_OK)
return UCI_ERR_NOTFOUND;
if (ptr.o)
return UCI_OK;
cwmp_uci_exit(uci_ctx);
return UCI_ERR_NOTFOUND;
}
@ -328,18 +315,24 @@ int uci_set_value_by_path(char *path, char *value, uci_config_paths uci_path)
if (path == NULL || value == NULL)
return UCI_ERR_NOTFOUND;
snprintf(cmd, sizeof(cmd), "%s=%s", path, value);
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, cmd, true) != UCI_OK) {
return UCI_ERR_PARSE;
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_lookup_ptr(uci_ctx, &ptr, cmd, true) != UCI_OK) {
ret = UCI_ERR_PARSE;
goto end;
}
ret = uci_set(uci_save_conf_paths[uci_path].uci_ctx, &ptr);
ret = uci_set(uci_ctx, &ptr);
if (ret == UCI_OK) {
ret = uci_save(uci_save_conf_paths[uci_path].uci_ctx, ptr.p);
ret = uci_save(uci_ctx, ptr.p);
} else {
CWMP_LOG(ERROR, "UCI delete not succeed %s", path);
return UCI_ERR_NOTFOUND;
ret = UCI_ERR_NOTFOUND;
}
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
@ -429,13 +422,16 @@ int cwmp_uci_get_option_value_list(char *package, char *section, char *option, u
struct uci_ptr ptr = {0};
struct uci_list *list;
char *pch = NULL, *spch = NULL, *dup = NULL;
int option_type;
int option_type = UCI_OK;
*value = NULL;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, option, NULL))
return UCI_ERR_PARSE;
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, option, NULL)) {
option_type = UCI_ERR_PARSE;
goto end;
}
if (ptr.o) {
switch(ptr.o->type) {
@ -445,7 +441,8 @@ int cwmp_uci_get_option_value_list(char *package, char *section, char *option, u
break;
case UCI_TYPE_STRING:
if (!ptr.o->v.string || (ptr.o->v.string)[0] == '\0') {
return UCI_TYPE_STRING;
option_type = UCI_TYPE_STRING;
goto end;
}
list = calloc(1, sizeof(struct uci_list));
cwmp_uci_list_init(list);
@ -461,11 +458,14 @@ int cwmp_uci_get_option_value_list(char *package, char *section, char *option, u
option_type = UCI_TYPE_STRING;
break;
default:
return UCI_ERR_NOTFOUND;
option_type = UCI_ERR_NOTFOUND;
goto end;
}
} else {
return UCI_ERR_NOTFOUND;
option_type = UCI_ERR_NOTFOUND;
}
end:
cwmp_uci_exit(uci_ctx);
return option_type;
}
@ -482,55 +482,48 @@ int cwmp_uci_get_cwmp_varstate_option_value_list(char *package, char *section, c
int cwmp_uci_add_list_value(char *package, char *section, char *option, char *value, uci_config_paths uci_path)
{
struct uci_ptr ptr = {0};
int error = UCI_OK;
int ret = UCI_OK;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, option, value))
return UCI_ERR_PARSE;
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, option, value)) {
ret = UCI_ERR_PARSE;
goto end;
}
error = uci_add_list(uci_save_conf_paths[uci_path].uci_ctx, &ptr);
if (error != UCI_OK)
return error;
ret = uci_add_list(uci_ctx, &ptr);
if (ret != UCI_OK)
CWMP_LOG(ERROR, "Failed to add option value to a uci list");
return UCI_OK;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
int cwmp_uci_del_list_value(char *package, char *section, char *option, char *value, uci_config_paths uci_path)
{
struct uci_ptr ptr = {0};
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, option, value))
return -1;
if (uci_del_list(uci_save_conf_paths[uci_path].uci_ctx, &ptr) != UCI_OK)
return -1;
return 0;
}
int uci_add_list_value(char *cmd, uci_config_paths uci_path)
{
struct uci_ptr ptr;
int ret = UCI_OK;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || cmd == NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || section ==NULL || option ==NULL)
return UCI_ERR_NOTFOUND;
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, cmd, true) != UCI_OK) {
return UCI_ERR_PARSE;
}
ret = uci_add_list(uci_save_conf_paths[uci_path].uci_ctx, &ptr);
if (ret == UCI_OK) {
ret = uci_save(uci_save_conf_paths[uci_path].uci_ctx, ptr.p);
} else {
CWMP_LOG(ERROR, "UCI delete not succeed %s", cmd);
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, option, value)) {
ret = UCI_ERR_PARSE;
goto end;
}
ret = uci_del_list(uci_ctx, &ptr);
if (ret != UCI_OK)
CWMP_LOG(ERROR, "Failed to add option value to a uci list");
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
@ -542,10 +535,12 @@ int cwmp_uci_add_section(char *package, char *stype, uci_config_paths uci_path ,
{
struct uci_ptr ptr = {0};
char fname[128];
int ret = UCI_OK;
*s = NULL;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || stype == NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || stype == NULL)
return UCI_ERR_NOTFOUND;
snprintf(fname, sizeof(fname), "%s/%s", uci_save_conf_paths[uci_path].conf_dir, package);
@ -553,18 +548,22 @@ int cwmp_uci_add_section(char *package, char *stype, uci_config_paths uci_path ,
FILE *fptr = fopen(fname, "w");
if (fptr)
fclose(fptr);
else
return UCI_ERR_UNKNOWN;
else {
ret = UCI_ERR_UNKNOWN;
goto end;
}
}
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, NULL, NULL, NULL) == 0
&& uci_add_section(uci_save_conf_paths[uci_path].uci_ctx, ptr.p, stype, s) == UCI_OK) {
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, NULL, NULL, NULL) == 0
&& uci_add_section(uci_ctx, ptr.p, stype, s) == UCI_OK) {
CWMP_LOG(INFO, "New uci section %s added successfully", stype);
}
else
return UCI_ERR_NOTFOUND;
ret = UCI_ERR_NOTFOUND;
return UCI_OK;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
struct uci_section* get_section_by_section_name(char *package, char *stype, char* sname, uci_config_paths uci_path)
@ -583,29 +582,43 @@ struct uci_section* get_section_by_section_name(char *package, char *stype, char
int cwmp_uci_rename_section_by_section(struct uci_section *s, char *value, uci_config_paths uci_path)
{
struct uci_ptr up = {0};
int ret = UCI_OK;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || s == NULL || value == NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr_by_section(uci_save_conf_paths[uci_path].uci_ctx, &up, s, NULL, value) == -1)
return UCI_ERR_PARSE;
if (uci_rename(uci_save_conf_paths[uci_path].uci_ctx, &up) != UCI_OK)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || s == NULL || value == NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr_by_section(uci_ctx, &up, s, NULL, value) == -1) {
ret = UCI_ERR_PARSE;
goto end;
}
if (uci_rename(uci_ctx, &up) != UCI_OK)
ret = UCI_ERR_NOTFOUND;
return UCI_OK;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
int cwmp_uci_add_section_with_specific_name(char *package, char *stype, char *section_name, uci_config_paths uci_path)
{
struct uci_section *s = NULL;
int ret = UCI_OK;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || stype == NULL)
if (package == NULL || stype == NULL)
return UCI_ERR_NOTFOUND;
if (get_section_by_section_name(package, stype, section_name, uci_path) != NULL)
return UCI_ERR_DUPLICATE;
if (cwmp_uci_add_section(package, stype, uci_path, &s) != UCI_OK)
return UCI_ERR_UNKNOWN;
if (get_section_by_section_name(package, stype, section_name, uci_path) != NULL) {
ret = UCI_ERR_DUPLICATE;
goto end;
}
if (cwmp_uci_add_section(package, stype, uci_path, &s) != UCI_OK) {
ret = UCI_ERR_UNKNOWN;
goto end;
}
return cwmp_uci_rename_section_by_section(s, section_name, uci_path);
ret = cwmp_uci_rename_section_by_section(s, section_name, uci_path);
end:
return ret;
}
/*
@ -616,37 +629,48 @@ int uci_delete_value(char *path, int uci_path)
struct uci_ptr ptr;
int ret = UCI_OK;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || path == NULL)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || path == NULL)
return UCI_ERR_NOTFOUND;
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, path, true) != UCI_OK)
return CWMP_GEN_ERR;
if (uci_lookup_ptr(uci_ctx, &ptr, path, true) != UCI_OK) {
ret = UCI_ERR_PARSE;
goto end;
}
ret = uci_delete(uci_save_conf_paths[uci_path].uci_ctx, &ptr);
ret = uci_delete(uci_ctx, &ptr);
if (ret == UCI_OK) {
ret = uci_save(uci_save_conf_paths[uci_path].uci_ctx, ptr.p);
ret = uci_save(uci_ctx, ptr.p);
} else {
CWMP_LOG(ERROR, "UCI delete not succeed %s", path);
return CWMP_GEN_ERR;
ret = CWMP_GEN_ERR;
}
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
int cwmp_uci_get_section_type(char *package, char *section, uci_config_paths uci_path, char **value)
{
struct uci_ptr ptr = {0};
if (uci_save_conf_paths[uci_path].uci_ctx == NULL || package == NULL || section == NULL)
int ret = UCI_OK;
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL || package == NULL || section == NULL)
return UCI_ERR_NOTFOUND;
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, section, NULL, NULL)) {
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, section, NULL, NULL)) {
*value = "";
return -1;
ret = -1;
goto end;
}
if (ptr.s) {
*value = icwmp_strdup(ptr.s->type);
} else {
*value = "";
}
return UCI_OK;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
struct uci_section *cwmp_uci_walk_section(char *package, char *stype, void *arg1, void *arg2, int cmp, int (*filter)(struct uci_section *s, void *value), struct uci_section *prev_section, uci_config_paths uci_path, int walk)
@ -656,12 +680,16 @@ struct uci_section *cwmp_uci_walk_section(char *package, char *stype, void *arg1
char *value = NULL, *pch = NULL, *spch = NULL;
char dup[256];
struct uci_list *list_value, *list_section;
struct uci_ptr ptr = { 0 };
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL)
return NULL;
if (package == NULL)
goto end;
if (walk == CWMP_GET_FIRST_SECTION) {
if (cwmp_uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, NULL, NULL, NULL) != UCI_OK)
struct uci_ptr ptr = { 0 };
if (cwmp_uci_lookup_ptr(uci_ctx, &ptr, package, NULL, NULL, NULL) != UCI_OK)
goto end;
list_section = &(ptr.p)->sections;
@ -726,27 +754,36 @@ struct uci_section *cwmp_uci_walk_section(char *package, char *stype, void *arg1
s = NULL;
}
end:
cwmp_uci_exit(uci_ctx);
return s;
}
int cwmp_commit_package(char *package, uci_config_paths uci_path)
{
struct uci_ptr ptr = { 0 };
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, true) != UCI_OK) {
int ret = UCI_OK;
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL)
return -1;
if (uci_lookup_ptr(uci_ctx, &ptr, package, true) != UCI_OK) {
ret = -1;
goto end;
}
if (uci_commit(uci_save_conf_paths[uci_path].uci_ctx, &ptr.p, false) != UCI_OK) {
return -1;
}
return 0;
if (uci_commit(uci_ctx, &ptr.p, false) != UCI_OK)
ret = -1;
end:
cwmp_uci_exit(uci_ctx);
return ret;
}
int cwmp_uci_import(char *package_name, const char *input_path, uci_config_paths uci_path)
{
struct uci_package *package = NULL;
struct uci_element *e = NULL;
int ret = CWMP_OK;
if (input_path == NULL)
return -1;
@ -754,53 +791,48 @@ int cwmp_uci_import(char *package_name, const char *input_path, uci_config_paths
if (!input)
return -1;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL) {
ret = -1;
goto end;
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL) {
fclose(input);
return -1;
}
if (uci_import(uci_save_conf_paths[uci_path].uci_ctx, input, package_name, &package, (package_name != NULL)) != UCI_OK) {
if (uci_import(uci_ctx, input, package_name, &package, (package_name != NULL)) != UCI_OK) {
ret = -1;
goto end;
}
if (uci_save_conf_paths[uci_path].uci_ctx == NULL) {
ret = -1;
goto end;
}
uci_foreach_element(&uci_save_conf_paths[uci_path].uci_ctx->root, e)
uci_foreach_element(&uci_ctx->root, e)
{
struct uci_package *p = uci_to_package(e);
if (uci_commit(uci_save_conf_paths[uci_path].uci_ctx, &p, true) != UCI_OK)
if (uci_commit(uci_ctx, &p, true) != UCI_OK)
ret = CWMP_GEN_ERR;
}
end:
fclose(input);
cwmp_uci_exit(uci_ctx);
return ret;
}
int cwmp_uci_export_package(char *package, const char *output_path, uci_config_paths uci_path)
int cwmp_uci_export_package(char *package, const char *output_path, struct uci_context *uci_ctx)
{
struct uci_ptr ptr = { 0 };
int ret = 0;
if (output_path == NULL)
return -1;
FILE *out = fopen(output_path, "a");
if (!out)
return -1;
if (uci_save_conf_paths[uci_path].uci_ctx == NULL) {
if (uci_lookup_ptr(uci_ctx, &ptr, package, true) != UCI_OK) {
ret = -1;
goto end;
}
if (uci_lookup_ptr(uci_save_conf_paths[uci_path].uci_ctx, &ptr, package, true) != UCI_OK) {
ret = -1;
goto end;
}
if (uci_export(uci_save_conf_paths[uci_path].uci_ctx, out, ptr.p, true) != UCI_OK)
if (uci_export(uci_ctx, out, ptr.p, true) != UCI_OK)
ret = -1;
end:
@ -812,16 +844,27 @@ int cwmp_uci_export(const char *output_path, uci_config_paths uci_path)
{
char **configs = NULL;
char **p;
int ret = UCI_OK;
if (uci_list_configs(uci_save_conf_paths[uci_path].uci_ctx, &configs) != UCI_OK)
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(uci_path);
if (uci_ctx == NULL)
return -1;
if (configs == NULL)
return -1;
if (uci_list_configs(uci_ctx, &configs) != UCI_OK) {
ret = -1;
goto end;
}
if (configs == NULL) {
ret = -1;
goto end;
}
for (p = configs; *p; p++)
cwmp_uci_export_package(*p, output_path, uci_path);
cwmp_uci_export_package(*p, output_path, uci_ctx);
end:
FREE(configs);
return 0;
cwmp_uci_exit(uci_ctx);
return ret;
}

View file

@ -122,13 +122,11 @@ struct config_uci_list {
struct uci_paths {
char *conf_dir;
char *save_dir;
struct uci_context *uci_ctx;
};
extern struct uci_paths uci_save_conf_paths[];
int cwmp_uci_init();
void cwmp_uci_exit(void);
void cwmp_uci_reinit(void);
struct uci_context *cwmp_uci_init_by_uci_path(uci_config_paths uci_path);
void cwmp_uci_exit(struct uci_context *uci_ctx);
int cwmp_uci_lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, char *package, char *section, char *option, char *value);
int cwmp_uci_get_cwmp_standard_option_value_list(char *package, char *section, char *option, struct uci_list **value);
int cwmp_uci_get_cwmp_varstate_option_value_list(char *package, char *section, char *option, struct uci_list **value);
@ -142,7 +140,7 @@ int cwmp_uci_get_value_by_section_string(struct uci_section *s, char *option, ch
int cwmp_uci_get_option_value_string(char *package, char *section, char *option, uci_config_paths uci_type, char **value);
int cwmp_commit_package(char *package, uci_config_paths uci_type);
int cwmp_uci_import(char *package_name, const char *input_path, uci_config_paths uci_type);
int cwmp_uci_export_package(char *package, const char *output_path, uci_config_paths uci_type);
int cwmp_uci_export_package(char *package, const char *output_path, struct uci_context *uci_ctx);
int cwmp_uci_export(const char *output_path, uci_config_paths uci_type);
void cwmp_free_uci_list(struct uci_list *list);
int cwmp_uci_add_list_value(char *package, char *section, char *option, char *value, uci_config_paths uci_type);

View file

@ -500,7 +500,6 @@ int apply_downloaded_file(struct download *pdownload, char *download_file_name,
//TODO Not Supported
error = FAULT_CPE_NO_FAULT;
} else if (strcmp(pdownload->file_type, VENDOR_CONFIG_FILE_TYPE) == 0) {
cwmp_uci_init();
int err = CWMP_OK;
if (download_file_name != NULL) {
char file_path[512];
@ -512,7 +511,6 @@ int apply_downloaded_file(struct download *pdownload, char *download_file_name,
remove(VENDOR_CONFIG_FILE);
}
cwmp_uci_exit();
if (err == CWMP_OK)
error = FAULT_CPE_NO_FAULT;
else if (err == CWMP_GEN_ERR)

View file

@ -475,11 +475,9 @@ void icwmp_http_server_init(void)
char cr_port_str[6];
snprintf(cr_port_str, 6, "%hu", cr_port);
cr_port_str[5] = '\0';
cwmp_uci_init();
cwmp_uci_set_value("cwmp", "cpe", "port", cr_port_str);
cwmp_commit_package("cwmp", UCI_STANDARD_CONFIG);
connection_request_port_value_change(cr_port);
cwmp_uci_exit();
CWMP_LOG(INFO, "Connection Request server initiated with the port: %d", cr_port);
}

View file

@ -332,7 +332,6 @@ void create_list_param_obj_notify()
struct uci_element *e = NULL;
int i;
cwmp_uci_reinit();
for (i = 0; i < 7; i++) {
int option_type;
option_type = cwmp_uci_get_cwmp_varstate_option_value_list("cwmp", "@notifications[0]", notifications[i], &list_notif);

View file

@ -62,7 +62,6 @@ int cwmp_session_init()
cwmp_main->cwmp_cr_event = 0;
cwmp_uci_init();
/*
* Set Required methods as initial value of
*/
@ -99,7 +98,6 @@ int cwmp_session_rpc_destructor(struct rpc *rpc)
int cwmp_session_exit()
{
cwmp_uci_exit();
icwmp_cleanmem();
return CWMP_OK;
}
@ -259,7 +257,6 @@ static void set_cwmp_session_status_state(int status)
if (!file_exists(VARSTATE_CONFIG"/cwmp"))
creat(VARSTATE_CONFIG"/cwmp", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
cwmp_uci_reinit();
cwmp_uci_add_section_with_specific_name("cwmp", "sess_status", "sess_status", UCI_VARSTATE_CONFIG);
switch (status) {
@ -615,7 +612,6 @@ int run_session_end_func(void)
if (end_session_flag & END_SESSION_RELOAD) {
CWMP_LOG(INFO, "Config reload: end session request");
cwmp_uci_reinit();
if (cwmp_apply_acs_changes() != CWMP_OK) {
CWMP_LOG(ERROR, "config reload failed at session end");
}

View file

@ -44,7 +44,6 @@ static int reload_cmd(struct blob_buf *b)
blobmsg_add_string(b, "info", "Session running, reload at the end of the session");
} else {
int error = CWMP_OK;
cwmp_uci_reinit();
error = cwmp_apply_acs_changes();
if (error != CWMP_OK) {
// Failed to load cwmp config

View file

@ -202,9 +202,7 @@ int cwmp_launch_upload(struct upload *pupload, struct transfer_complete **ptrans
if (pupload->file_type[0] == '1') {
snprintf(file_path, sizeof(file_path), "/tmp/all_configs");
cwmp_uci_init();
cwmp_uci_export(file_path, UCI_STANDARD_CONFIG);
cwmp_uci_exit();
} else if (pupload->file_type[0] == '2') {
lookup_vlf_name(1, &name);
if (name && strlen(name) > 0) {
@ -221,9 +219,9 @@ int cwmp_launch_upload(struct upload *pupload, struct transfer_complete **ptrans
lookup_vcf_name(pupload->f_instance, &name);
if (name && strlen(name) > 0) {
snprintf(file_path, sizeof(file_path), "/tmp/%s", name);
cwmp_uci_init();
cwmp_uci_export_package(name, file_path, UCI_STANDARD_CONFIG);
cwmp_uci_exit();
struct uci_context *uci_ctx = cwmp_uci_init_by_uci_path(UCI_STANDARD_CONFIG);
cwmp_uci_export_package(name, file_path, uci_ctx);
cwmp_uci_exit(uci_ctx);
FREE(name);
} else {
error = FAULT_CPE_UPLOAD_FAILURE;

View file

@ -24,14 +24,12 @@ static struct uci_list *list = NULL;
static int cwmp_uci_unit_tests_init(void **state)
{
cwmp_uci_init();
return 0;
}
static int cwmp_uci_unit_tests_clean(void **state)
{
icwmp_cleanmem();
cwmp_uci_exit();
if (list != NULL)
cwmp_free_uci_list(list);
return 0;