diff --git a/cfg.y b/cfg.y index b50080e00a2..d6f3ed86e90 100644 --- a/cfg.y +++ b/cfg.y @@ -1505,7 +1505,7 @@ assign_stm: LOGLEVEL EQUAL snumber { IFOR(); } | TOS EQUAL error { yyerror("number expected"); } | MPATH EQUAL STRING {IFOR(); - set_mpath($3); } + add_mpath($3); } | MPATH EQUAL error { yyerror("string value expected"); } | DISABLE_DNS_FAILOVER EQUAL NUMBER { IFOR(); disable_dns_failover=$3; diff --git a/sr_module.c b/sr_module.c index efded7d60b5..3f6ef2257d6 100644 --- a/sr_module.c +++ b/sr_module.c @@ -90,9 +90,12 @@ struct sr_module* modules=0; #endif #define MPATH_LEN 256 -static const char *mpath; -static char mpath_buf[MPATH_LEN + 1]; -static int mpath_len; +struct mpath { + char buf[MPATH_LEN + 1]; + int len; + struct mpath *next; +}; +static struct mpath *mpaths, *last_mpath; /* initializes statically built (compiled in) modules*/ int register_builtin_modules(void) @@ -369,75 +372,95 @@ static int load_static_module(char *path) return -1; } -void set_mpath(const char *new_mpath) +void add_mpath(const char *new_mpath) { + struct mpath *nmpath; int len = strlen(new_mpath); if (len >= MPATH_LEN) { LM_ERR("mpath %s too long!\n", new_mpath); return; } - mpath = new_mpath; - strcpy(mpath_buf, new_mpath); - mpath_len = len; - if (mpath_len == 0 || mpath_buf[mpath_len - 1] != '/') { - mpath_buf[mpath_len] = '/'; - mpath_len++; - mpath_buf[mpath_len] = '\0'; + nmpath = pkg_malloc(sizeof *nmpath); + if (!nmpath) { + LM_ERR("could not allocate space for %s mpath!\n", new_mpath); + return; } + memset(nmpath, 0, sizeof *nmpath); + /* link it in the list */ + if (last_mpath) + last_mpath->next = nmpath; + else + mpaths = nmpath; + last_mpath = nmpath; + memcpy(nmpath->buf, new_mpath, len); + nmpath->len = len; + if (nmpath->len == 0 || nmpath->buf[nmpath->len - 1] != '/') { + nmpath->buf[nmpath->len] = '/'; + nmpath->len++; + } + nmpath->buf[nmpath->len] = '\0'; } /* returns 0 on success , <0 on error */ int load_module(char* name) { - int i_tmp; + int i_tmp, len; struct stat statf; + struct mpath *mp; /* if this is a static module, load it directly */ if (load_static_module(name) == 0) return 0; - if(*name!='/' && mpath!=NULL - && strlen(name)+mpath_lennext) { + len = strlen(name); + if (len + mp->len >= MPATH_LEN) + continue; + memcpy(mp->buf + mp->len, name, len); + mp->buf[mp->len + len] = '\0'; + if (stat(mp->buf, &statf) == -1 || S_ISDIR(statf.st_mode)) { + i_tmp = strlen(mp->buf); if(strchr(name, '/')==NULL && - strncmp(mpath_buf+i_tmp-3, ".so", 3)==0) + strncmp(mp->buf+i_tmp-3, ".so", 3)==0) { - if(i_tmp+strlen(name)buf+i_tmp-3, "/"); + strcpy(mp->buf+i_tmp-2, name); + if (stat(mp->buf, &statf) == -1) { + mp->buf[mp->len]='\0'; + LM_DBG("module '%s' not found in '%s'\n", + name, mp->buf); + goto next; } } else { - LM_ERR("failed to load module - path too long\n"); - return -1; + LM_DBG("failed to load module '%s' from '%s' - path too long\n", name, mp->buf); + goto next; } } else { - LM_ERR("failed to load module - not found\n"); - return -1; + goto next; } } - LM_DBG("loading module %s\n", mpath_buf); - if (sr_load_module(mpath_buf)!=0){ - LM_ERR("failed to load module\n"); - return -1; - } - mpath_buf[mpath_len]='\0'; - } else { - LM_DBG("loading module %s\n", name); - if (sr_load_module(name)!=0){ - LM_ERR("failed to load module\n"); - return -1; + LM_DBG("trying module %s\n", mp->buf); + if (sr_load_module(mp->buf)!=0) { + LM_DBG("failed to load module '%s'\n", mp->buf); + } else { + mp->buf[mp->len]='\0'; + return 0; } +next: + mp->buf[mp->len]='\0'; } - return 0; + LM_ERR("failed to load module '%s' - not found\n", name); + return -1; } diff --git a/sr_module.h b/sr_module.h index 0e3847c807c..aaa4990253c 100644 --- a/sr_module.h +++ b/sr_module.h @@ -201,7 +201,7 @@ struct module_exports{ module agrees with the new script */ }; -void set_mpath(const char *new_mpath); +void add_mpath(const char *new_mpath); extern struct sr_module* modules; /*!< global module list*/ diff --git a/test/unit_tests.c b/test/unit_tests.c index 14890068b4f..2da913c82c9 100644 --- a/test/unit_tests.c +++ b/test/unit_tests.c @@ -42,7 +42,7 @@ void init_unit_tests(void) { if (!strcmp(testing_module, "core")) { - set_mpath("modules/"); + add_mpath("modules/"); solve_module_dependencies(modules); init_cachedb_tests(); //init_malloc_tests();