#include <dlfcn.h>
#include <cx/string.h>
#include "../daemon/func.h"
#include "../daemon/log.h"
#include "../daemon/config.h"
#include "init.h"
int init_test(pblock *pb, Session *sn, Request *rq) {
printf(
"init-test\n");
return REQ_PROCEED;
}
int load_modules(pblock *pb, Session *sn, Request *rq) {
char *shlib = pblock_findval(
"shlib", pb);
char *funcs = pblock_findval(
"funcs", pb);
if(shlib ==
NULL || funcs ==
NULL) {
log_ereport(
LOG_MISCONFIG,
"load-modules: missing parameters");
if(!shlib && funcs) {
log_ereport(
LOG_MISCONFIG,
"load-modules: missing shlib parameter");
}
else if(!shlib && !funcs) {
log_ereport(
LOG_MISCONFIG,
"load-modules: missing funcs parameter");
}
else {
log_ereport(
LOG_MISCONFIG,
"load-modules: missing shlib and funcs parameters");
}
}
void *lib = dlopen(shlib,
RTLD_GLOBAL |
RTLD_NOW);
if(lib ==
NULL) {
char *dlerr = dlerror();
fprintf(stderr,
"Cannot load library %s %s\n", shlib, dlerr);
log_ereport(
LOG_FAILURE,
"Cannot load library %s: %s", shlib, dlerr);
return REQ_ABORTED;
}
pool_handle_t *init_pool = cfg_get_init_pool();
int b =
0;
for(
int i=
0;;i++) {
if(funcs[i] ==
'-') {
funcs[i] =
'_';
}
if(funcs[i] ==
',' || funcs[i] ==
0) {
if(funcs[i] ==
0) {
b =
1;
}
funcs[i] =
0;
void *sym = dlsym(lib, funcs);
if(sym ==
NULL) {
fprintf(stderr,
"Cannot load symbol %s\n", funcs);
dlclose(lib);
return REQ_ABORTED;
}
struct FuncStruct fc;
ZERO(&fc,
sizeof(
struct FuncStruct));
fc.func = (FuncPtr)sym;
fc.name = pool_strdup(init_pool, funcs);
add_function(&fc);
if(b) {
break;
}
funcs += i +
1;
i =
0;
}
}
void *sym = dlsym(lib,
"nsapi_module_init");
if(sym) {
pblock_remove(
"shlib", pb);
pblock_remove(
"funcs", pb);
FuncPtr init_func = (FuncPtr)sym;
int ret = init_func(pb,
NULL,
NULL);
if(ret !=
REQ_PROCEED && ret !=
REQ_NOACTION) {
log_ereport(
LOG_FAILURE,
"nsapi_module_init for module %s failed",
shlib);
return REQ_ABORTED;
}
}
return REQ_PROCEED;
}