1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #include <dlfcn.h>
30 #include <ucx/string.h>
31
32 #include "../daemon/func.h"
33 #include "../daemon/log.h"
34
35 #include "init.h"
36
37 int init_test(pblock *pb, Session *sn, Request *rq) {
38 printf(
"init-test\n");
39 return REQ_PROCEED;
40 }
41
42 int load_modules(pblock *pb, Session *sn, Request *rq) {
43 char *shlib = pblock_findval(
"shlib", pb);
44 char *funcs = pblock_findval(
"funcs", pb);
45
46 if(shlib ==
NULL || funcs ==
NULL) {
47 log_ereport(
LOG_MISCONFIG,
"load-modules: missing parameters");
48 if(!shlib && funcs) {
49 log_ereport(
50 LOG_MISCONFIG,
51 "load-modules: missing shlib parameter");
52 }
else if(!shlib && !funcs) {
53 log_ereport(
54 LOG_MISCONFIG,
55 "load-modules: missing funcs parameter");
56 }
else {
57 log_ereport(
58 LOG_MISCONFIG,
59 "load-modules: missing shlib and funcs parameters");
60 }
61 }
62
63
64 void *lib = dlopen(shlib,
RTLD_GLOBAL |
RTLD_NOW);
65 if(lib ==
NULL) {
66 char *dlerr = dlerror();
67 fprintf(stderr,
"Cannot load library %s %s\n", shlib, dlerr);
68
69 log_ereport(
LOG_FAILURE,
"Cannot load library %s: %s", shlib, dlerr);
70
71 return REQ_ABORTED;
72 }
73
74
75 int b =
0;
76 for(
int i=
0;;i++) {
77 if(funcs[i] ==
'-') {
78 funcs[i] =
'_';
79 }
80 if(funcs[i] ==
',' || funcs[i] ==
0) {
81 if(funcs[i] ==
0) {
82 b =
1;
83 }
84
85 funcs[i] =
0;
86
87
88 void *sym = dlsym(lib, funcs);
89 if(sym ==
NULL) {
90 fprintf(stderr,
"Cannot load symbol %s\n", funcs);
91 dlclose(lib);
92 return REQ_ABORTED;
93 }
94 struct FuncStruct fc;
95 ZERO(&fc,
sizeof(
struct FuncStruct));
96 fc.func = (FuncPtr)sym;
97 fc.name = sstrdup(sstr(funcs)).ptr;
98 add_function(&fc);
99
100 if(b) {
101 break;
102 }
103
104 funcs += i +
1;
105 i =
0;
106 }
107 }
108
109
110 void *sym = dlsym(lib,
"nsapi_module_init");
111 if(sym) {
112
113
114
115
116 pblock_remove(
"shlib", pb);
117 pblock_remove(
"funcs", pb);
118
119 FuncPtr init_func = (FuncPtr)sym;
120 int ret = init_func(pb,
NULL,
NULL);
121 if(ret !=
REQ_PROCEED && ret !=
REQ_NOACTION) {
122 log_ereport(
123 LOG_FAILURE,
124 "nsapi_module_init for module %s failed",
125 shlib);
126 return REQ_ABORTED;
127 }
128 }
129
130 return REQ_PROCEED;
131 }
132