]>
Commit | Line | Data |
---|---|---|
2c5db955 CP |
1 | /* |
2 | * modules.c: | |
3 | * | |
4 | * Provides functions for dealing with dynamic modules. | |
5 | */ | |
6 | ||
7 | #include <stdlib.h> | |
8 | #include <dlfcn.h> | |
9 | #include "modules.h" | |
10 | #include "../lib/array.h" | |
11 | #include "../lib/sstring.h" | |
12 | #include "../lib/irc_string.h" | |
3fa581ac | 13 | #include "../lib/splitline.h" |
707c5824 | 14 | #include "../lib/strlfunc.h" |
2c5db955 CP |
15 | #include "config.h" |
16 | #include "error.h" | |
17 | #include <stdio.h> | |
18 | #include <string.h> | |
3f1c5f43 CP |
19 | #include <errno.h> |
20 | #include <unistd.h> | |
65f2c6a3 | 21 | #include "schedule.h" |
2c5db955 | 22 | |
3fa581ac | 23 | #define DEPFILE "modules.dep" |
24 | ||
25 | #define MAXMODULES 100 | |
26 | ||
27 | /* Module dependency stuff. | |
28 | * | |
29 | * "parents" are modules we rely on in order to work. | |
30 | * Whenever we load a module we must ensure all parents are present first. | |
31 | * | |
32 | * "children" are modules which rely on us. | |
33 | * Whenever we remove a module, we must ensure all children are removed. | |
34 | * If it's a reload, we will reload them afterwards. | |
35 | * | |
36 | * Now, depmod.pl cunningly arranges the file so that each modules' parents are | |
37 | * always defined earlier in the file. So we can allocate and fill in the parents | |
38 | * array as we read the file. Note that we can also arrange for numchildren to be | |
39 | * correct on each node when we've finished. | |
40 | * | |
41 | * We then do a second pass. At each node, we allocate the space for the | |
42 | * children array and reset numchildren to 0. We can also then populate our | |
43 | * parents' children array with ourselves, using "numchildren" to get the | |
44 | * list in order. | |
45 | * | |
46 | * At the end of the second pass we should have a correctly filled in array! | |
47 | */ | |
48 | ||
65f2c6a3 | 49 | sstring *safereload_str; |
50 | schedule *safereload_sched; | |
51 | ||
3fa581ac | 52 | struct module_dep { |
53 | sstring *name; | |
54 | unsigned int numparents; | |
55 | unsigned int numchildren; | |
56 | struct module_dep **parents; | |
57 | struct module_dep **children; | |
58 | unsigned int reloading; | |
59 | }; | |
60 | ||
2c5db955 CP |
61 | array modules; |
62 | ||
3fa581ac | 63 | struct module_dep moduledeps[MAXMODULES]; |
64 | unsigned int knownmodules=0; | |
65 | ||
2c5db955 CP |
66 | sstring *moddir; |
67 | sstring *modsuffix; | |
68 | ||
3fa581ac | 69 | /* Clear out and free the dependencies. */ |
70 | void clearmoduledeps() { | |
71 | unsigned int i; | |
72 | ||
73 | for (i=0;i<knownmodules;i++) { | |
707c5824 | 74 | freesstring(moduledeps[i].name); |
b46e727d | 75 | if (moduledeps[i].parents) { |
76 | free(moduledeps[i].parents); | |
77 | moduledeps[i].parents=NULL; | |
78 | } | |
79 | if (moduledeps[i].children) { | |
80 | free(moduledeps[i].children); | |
81 | moduledeps[i].children=NULL; | |
82 | } | |
3fa581ac | 83 | } |
84 | ||
85 | knownmodules=0; | |
86 | } | |
87 | ||
88 | /* Get a pointer to the given module's dependency record. */ | |
89 | struct module_dep *getmoduledep(const char *name) { | |
90 | unsigned int i; | |
91 | ||
92 | for (i=0;i<knownmodules;i++) { | |
93 | if (!strcmp(name,moduledeps[i].name->content)) | |
94 | return &(moduledeps[i]); | |
95 | } | |
96 | ||
97 | return NULL; | |
98 | } | |
99 | ||
100 | /* Populate the dependency array. Read the monster description above for details. | |
101 | * This relies on moddir being set, so call this after setting it up. */ | |
102 | void initmoduledeps() { | |
103 | FILE *fp; | |
104 | char buf[1024]; | |
105 | char *largv[100]; | |
106 | char largc; | |
107 | char *ch; | |
b46e727d | 108 | struct module_dep *mdp, *tmdp; |
3fa581ac | 109 | unsigned int i,j; |
110 | ||
111 | sprintf(buf,"%s/%s",moddir->content,DEPFILE); | |
112 | ||
113 | if (!(fp=fopen(buf,"r"))) { | |
114 | Error("core",ERR_WARNING,"Unable to open module dependency file: %s",buf); | |
115 | } else { | |
116 | /* First pass */ | |
117 | while (!feof(fp)) { | |
118 | fgets(buf, sizeof(buf), fp); | |
119 | if (feof(fp)) | |
120 | break; | |
121 | ||
122 | /* Chomp off that ruddy newline. */ | |
123 | for (ch=buf;*ch;ch++) { | |
124 | if (*ch=='\n' || *ch=='\r') { | |
125 | *ch='\0'; | |
126 | break; | |
127 | } | |
128 | } | |
129 | ||
130 | /* We have a space-delimited list of things. Whatever will we do with that? :) */ | |
131 | largc=splitline(buf,largv,100,0); | |
132 | ||
133 | if (largc<1) | |
134 | continue; | |
135 | ||
136 | /* Add us to the array */ | |
137 | i=knownmodules++; | |
138 | if (i>=MAXMODULES) { | |
b46e727d | 139 | Error("core",ERR_ERROR, |
140 | "Too many modules in dependency file; rebuild with higher MAXMODULES. Module dependencies disabled.\n"); | |
141 | clearmoduledeps(); | |
3fa581ac | 142 | return; |
143 | } | |
144 | ||
145 | mdp=&(moduledeps[i]); | |
146 | ||
147 | mdp->name=getsstring(largv[0],100); | |
148 | mdp->numparents=largc-1; | |
149 | mdp->numchildren=0; /* peace, for now */ | |
150 | mdp->parents=malloc(mdp->numparents * sizeof(struct module_dep *)); | |
151 | ||
152 | /* Fill in the parents array */ | |
153 | for (i=0;i<(largc-1);i++) { | |
154 | if (!(mdp->parents[i]=getmoduledep(largv[i+1]))) { | |
b46e727d | 155 | Error("core",ERR_WARNING,"Couldn't find parent module %s of %s. Module dependencies disabled.", |
156 | largv[i+1],largv[0]); | |
157 | clearmoduledeps(); | |
158 | return; | |
3fa581ac | 159 | } |
160 | mdp->parents[i]->numchildren++; /* break the bad news */ | |
161 | } | |
162 | } | |
163 | ||
164 | /* Second pass */ | |
165 | for (i=0;i<knownmodules;i++) { | |
166 | mdp=&(moduledeps[i]); | |
167 | ||
168 | /* Allocate child array */ | |
169 | if (mdp->numchildren) { | |
170 | mdp->children=malloc(mdp->numchildren * sizeof(struct module_dep *)); | |
171 | mdp->numchildren=0; /* if only real life were this simple */ | |
172 | } | |
173 | ||
174 | /* Now fill in the children arrays of our parents (bear with me on this) */ | |
175 | for (j=0;j<mdp->numparents;j++) { | |
176 | tmdp=mdp->parents[j]; /* This is just... */ | |
177 | ||
178 | tmdp->children[tmdp->numchildren++]=mdp; /* ... to give this line a chance at making sense */ | |
179 | } | |
180 | } | |
181 | } | |
182 | } | |
183 | ||
2c5db955 CP |
184 | void modulerehash() { |
185 | int i; | |
186 | sstring **mods; | |
187 | array *autoloads; | |
188 | ||
189 | if (moddir!=NULL) | |
190 | freesstring(moddir); | |
191 | ||
192 | if (modsuffix!=NULL) | |
193 | freesstring(modsuffix); | |
3fa581ac | 194 | |
195 | clearmoduledeps(); | |
2c5db955 CP |
196 | |
197 | moddir=getcopyconfigitem("core","moduledir",".",100); | |
198 | modsuffix=getcopyconfigitem("core","modulesuffix",".so",5); | |
199 | ||
3fa581ac | 200 | initmoduledeps(); |
201 | ||
2c5db955 CP |
202 | /* Check for auto-load modules */ |
203 | autoloads=getconfigitems("core","loadmodule"); | |
204 | if (autoloads!=NULL) { | |
205 | mods=(sstring **)(autoloads->content); | |
206 | for (i=0;i<autoloads->cursi;i++) { | |
207 | insmod(mods[i]->content); | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | void initmodules() { | |
213 | array_init(&modules,sizeof(module)); | |
214 | array_setlim1(&modules,5); | |
215 | array_setlim2(&modules,10); | |
216 | ||
217 | moddir=NULL; | |
218 | modsuffix=NULL; | |
219 | modulerehash(); | |
220 | } | |
221 | ||
222 | int insmod(char *modulename) { | |
b46e727d | 223 | int i; |
2c5db955 | 224 | module *mods; |
73292a32 | 225 | char buf[1024]; |
87698d77 | 226 | const char *(*verinfo)(void); |
3fa581ac | 227 | struct module_dep *mdp; |
2c5db955 CP |
228 | |
229 | delchars(modulename,"./\\;"); | |
230 | ||
231 | if (isloaded(modulename)) { | |
232 | Error("core",ERR_DEBUG,"Tried to load already loaded module: %s",modulename); | |
233 | return 1; | |
234 | } | |
235 | ||
236 | if (strlen(modulename)>100) { | |
237 | Error("core",ERR_WARNING,"Module name too long: %s",modulename); | |
238 | return 1; | |
239 | } | |
3fa581ac | 240 | |
241 | if ((mdp=getmoduledep(modulename))) { | |
242 | for (i=0;i<mdp->numparents;i++) { | |
243 | if (!isloaded(mdp->parents[i]->name->content)) { | |
244 | if (insmod(mdp->parents[i]->name->content)) { | |
245 | Error("core",ERR_WARNING,"Error loading dependant module %s (needed by %s)", | |
246 | mdp->parents[i]->name->content,modulename); | |
247 | return 1; | |
248 | } | |
249 | } | |
250 | } | |
251 | } else { | |
252 | Error("core",ERR_WARNING,"Loading module %s without dependency information.",modulename); | |
253 | } | |
254 | ||
2c5db955 CP |
255 | i=array_getfreeslot(&modules); |
256 | mods=(module *)(modules.content); | |
3fa581ac | 257 | |
2c5db955 | 258 | sprintf(buf,"%s/%s%s",moddir->content,modulename,modsuffix->content); |
73292a32 | 259 | |
2c5db955 CP |
260 | mods[i].handle=dlopen(buf,RTLD_NOW|RTLD_GLOBAL); |
261 | ||
262 | if(mods[i].handle==NULL) { | |
263 | Error("core",ERR_ERROR,"Loading module %s failed: %s",modulename,dlerror()); | |
264 | array_delslot(&modules,i); | |
265 | return -1; | |
266 | } | |
3fa581ac | 267 | |
2c5db955 CP |
268 | mods[i].name=getsstring(modulename,MODULENAMELEN); |
269 | ||
87698d77 CP |
270 | verinfo=dlsym(mods[i].handle,"_version"); |
271 | if(verinfo) { | |
272 | mods[i].version=verinfo(); | |
273 | } else { | |
274 | mods[i].version=NULL; | |
275 | } | |
276 | ||
2c5db955 CP |
277 | Error("core",ERR_INFO,"Loaded module %s OK.",modulename); |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
282 | int getindex(char *modulename) { | |
283 | int i; | |
284 | module *mods; | |
285 | ||
286 | mods=(module *)(modules.content); | |
287 | for(i=0;i<modules.cursi;i++) | |
288 | if (!strcmp(mods[i].name->content,modulename)) | |
289 | return i; | |
290 | ||
291 | return -1; | |
292 | } | |
293 | ||
1fab6211 | 294 | char *lsmod(int index) { |
1fab6211 | 295 | module *mods; |
296 | ||
297 | if (index < 0 || index >= modules.cursi) | |
298 | return NULL; | |
299 | ||
300 | mods=(module *)(modules.content); | |
301 | return mods[index].name->content; | |
302 | } | |
303 | ||
87698d77 CP |
304 | const char *lsmodver(int index) { |
305 | module *mods; | |
306 | ||
307 | if (index < 0 || index >= modules.cursi) | |
308 | return NULL; | |
309 | ||
310 | mods=(module *)(modules.content); | |
311 | return mods[index].version; | |
312 | } | |
313 | ||
2c5db955 CP |
314 | int isloaded(char *modulename) { |
315 | if (getindex(modulename)==-1) | |
316 | return 0; | |
317 | else | |
318 | return 1; | |
319 | } | |
320 | ||
3fa581ac | 321 | |
2c5db955 | 322 | int rmmod(char *modulename) { |
3fa581ac | 323 | int i,j; |
2c5db955 | 324 | module *mods; |
3fa581ac | 325 | struct module_dep *mdp; |
2c5db955 CP |
326 | |
327 | delchars(modulename,"./\\;"); | |
328 | ||
329 | i=getindex(modulename); | |
330 | if (i<0) | |
331 | return 1; | |
3fa581ac | 332 | |
333 | if ((mdp=getmoduledep(modulename))) { | |
334 | for (j=0;j<mdp->numchildren;j++) { | |
335 | if (isloaded(mdp->children[j]->name->content)) { | |
336 | if (rmmod(mdp->children[j]->name->content)) { | |
337 | Error("core",ERR_WARNING,"Unable to remove child module %s (depends on %s)", | |
338 | mdp->children[j]->name->content, modulename); | |
339 | return 1; | |
340 | } | |
341 | } | |
342 | } | |
343 | ||
344 | /* We may have removed other modules - reaquire the index number in case it has changed. */ | |
345 | i=getindex(modulename); | |
346 | if (i<0) | |
347 | return 1; | |
348 | } else { | |
349 | Error("core",ERR_WARNING,"Removing module %s without dependency information",modulename); | |
350 | } | |
2c5db955 CP |
351 | |
352 | mods=(module *)(modules.content); | |
353 | ||
c5f1f827 CP |
354 | #ifdef BROKEN_DLCLOSE |
355 | { | |
356 | void (*fini)(); | |
357 | fini = dlsym(mods[i].handle, "__fini"); | |
358 | if(!dlerror()) | |
359 | fini(); | |
360 | } | |
361 | #endif | |
362 | ||
2c5db955 CP |
363 | dlclose(mods[i].handle); |
364 | freesstring(mods[i].name); | |
365 | array_delslot(&modules,i); | |
366 | ||
367 | Error("core",ERR_INFO,"Removed module %s.",modulename); | |
368 | ||
369 | return 0; | |
370 | } | |
3fa581ac | 371 | |
372 | /* Set the reload mark on the indicated module, if loaded, and all its | |
373 | * children */ | |
374 | void setreloadmark(struct module_dep *mdp) { | |
375 | unsigned int i; | |
376 | ||
377 | if (!isloaded(mdp->name->content)) | |
378 | return; | |
379 | ||
380 | for (i=0;i<mdp->numchildren;i++) | |
381 | setreloadmark(mdp->children[i]); | |
382 | ||
383 | mdp->reloading=1; | |
384 | } | |
385 | ||
386 | /* preparereload: this function marks in the dependency tree all the | |
387 | * modules which will be unloaded if this one is removed. */ | |
388 | void preparereload(char *modulename) { | |
389 | unsigned int i; | |
390 | struct module_dep *mdp; | |
391 | ||
392 | delchars(modulename,"./\\;"); | |
393 | ||
394 | /* First, clear the mark off all dependant modules */ | |
395 | for (i=0;i<knownmodules;i++) | |
396 | moduledeps[i].reloading=0; | |
397 | ||
398 | /* Do nothing if this module is not loaded */ | |
399 | i=getindex(modulename); | |
400 | if (i<0) | |
401 | return; | |
402 | ||
403 | if ((mdp=getmoduledep(modulename))) { | |
404 | setreloadmark(mdp); | |
405 | } | |
406 | } | |
407 | ||
408 | /* reloadmarked: this function loads all the modules marked for reloading. | |
409 | * The way the array is ordered means we will do this in the "right" order. | |
410 | * This means we won't do an isloaded() check for each one - we shouldn't | |
411 | * get any "already loaded" warnings so let them come out if they happen. | |
412 | */ | |
413 | void reloadmarked(void) { | |
414 | unsigned int i; | |
415 | ||
416 | for (i=0;i<knownmodules;i++) { | |
417 | if (moduledeps[i].reloading) { | |
418 | insmod(moduledeps[i].name->content); | |
419 | } | |
420 | } | |
421 | } | |
65f2c6a3 | 422 | |
423 | void safereloadcallback(void *arg) { | |
424 | safereload_sched=NULL; | |
425 | ||
426 | if (!safereload_str) | |
427 | return; | |
428 | ||
429 | preparereload(safereload_str->content); | |
430 | rmmod(safereload_str->content); | |
431 | insmod(safereload_str->content); | |
432 | reloadmarked(); | |
433 | ||
434 | freesstring(safereload_str); | |
435 | safereload_str=NULL; | |
436 | } | |
437 | ||
438 | void safereload(char *themodule) { | |
439 | if (safereload_str) | |
440 | freesstring(safereload_str); | |
441 | ||
442 | safereload_str=getsstring(themodule, 100); | |
443 | ||
444 | if (safereload_sched) | |
445 | deleteschedule(safereload_sched, safereloadcallback, NULL); | |
446 | ||
447 | scheduleoneshot(1, safereloadcallback, NULL); | |
448 | } | |
83951d54 | 449 | |
450 | void newserv_shutdown() { | |
451 | module *mods; | |
707c5824 CP |
452 | char buf[1024]; |
453 | ||
83951d54 | 454 | while (modules.cursi) { |
455 | mods=(module *)(modules.content); | |
707c5824 CP |
456 | |
457 | strlcpy(buf, mods[0].name->content, sizeof(buf)); | |
458 | rmmod(buf); | |
83951d54 | 459 | } |
460 | ||
707c5824 CP |
461 | clearmoduledeps(); |
462 | ||
463 | if (moddir!=NULL) | |
464 | freesstring(moddir); | |
465 | ||
466 | if (modsuffix!=NULL) | |
467 | freesstring(modsuffix); | |
468 | ||
83951d54 | 469 | Error("core",ERR_INFO,"All modules removed. Exiting."); |
470 | } | |
4e4920a4 CP |
471 | |
472 | /* very slow, make sure you cache the pointer! */ | |
473 | void *ndlsym(char *modulename, char *fn) { | |
474 | module *mods=(module *)(modules.content); | |
475 | int i=getindex(modulename); | |
476 | ||
477 | if (i<0) | |
478 | return NULL; | |
479 | ||
480 | return dlsym(mods[i].handle, fn); | |
481 | } |