diff --git a/src/common.c b/src/common.c index 547bd6c..0090e9d 100755 --- a/src/common.c +++ b/src/common.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "common.h" #include "cwmp_cli.h" @@ -96,6 +97,11 @@ int global_env_init(int argc, char **argv, struct env *env) { int c, option_index = 0; + /* This is to initialize the global context in mxml, + * with out init mxml sometimes segfaults, when calling the destructor. + */ + mxml_error(NULL); + while ((c = getopt_long(argc, argv, "bgchv", cwmp_long_options, &option_index)) != -1) { switch (c) { case 'b': @@ -105,7 +111,9 @@ int global_env_init(int argc, char **argv, struct env *env) env->periodic = CWMP_START_PERIODIC; break; case 'c': + cwmp_main = (struct cwmp*)calloc(1, sizeof(struct cwmp)); execute_cwmp_cli_command(argv[2], argv + 3); + FREE(cwmp_main); exit(0); case 'h': show_help(); diff --git a/src/cwmp.c b/src/cwmp.c index 76f2215..be70274 100644 --- a/src/cwmp.c +++ b/src/cwmp.c @@ -201,10 +201,6 @@ static int cwmp_init() openlog("cwmp", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); CWMP_LOG(INFO, "STARTING ICWMP with PID :%d", getpid()); - /* This is to initialize the global context in mxml, - * with out init mxml sometimes segfaults, when calling the destructor. - */ - mxml_error(NULL); cwmp_main = (struct cwmp*)calloc(1, sizeof(struct cwmp)); cwmp_main->init_complete = false; @@ -343,13 +339,13 @@ int main(int argc, char **argv) if (error) return error; - if ((error = cwmp_init())) - return error; - memset(&env, 0, sizeof(struct env)); if ((error = global_env_init(argc, argv, &env))) return error; + if ((error = cwmp_init())) + return error; + memcpy(&(cwmp_main->env), &env, sizeof(struct env)); if ((error = cwmp_init_backup_session(NULL, ALL)))