]>
jfr.im git - irc/quakenet/newserv.git/blob - core/modules.c
4 * Provides functions for dealing with dynamic modules.
10 #include "../lib/array.h"
11 #include "../lib/sstring.h"
12 #include "../lib/irc_string.h"
13 #include "../lib/splitline.h"
14 #include "../lib/strlfunc.h"
23 #define DEPFILE "modules.dep"
25 #define MAXMODULES 100
27 /* Module dependency stuff.
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.
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.
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.
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
46 * At the end of the second pass we should have a correctly filled in array!
49 sstring
*safereload_str
;
50 schedule
*safereload_sched
;
54 unsigned int numparents
;
55 unsigned int numchildren
;
56 struct module_dep
**parents
;
57 struct module_dep
**children
;
58 unsigned int reloading
;
63 struct module_dep moduledeps
[MAXMODULES
];
64 unsigned int knownmodules
=0;
69 /* Clear out and free the dependencies. */
70 void clearmoduledeps() {
73 for (i
=0;i
<knownmodules
;i
++) {
74 freesstring(moduledeps
[i
].name
);
75 if (moduledeps
[i
].parents
) {
76 free(moduledeps
[i
].parents
);
77 moduledeps
[i
].parents
=NULL
;
79 if (moduledeps
[i
].children
) {
80 free(moduledeps
[i
].children
);
81 moduledeps
[i
].children
=NULL
;
88 /* Get a pointer to the given module's dependency record. */
89 struct module_dep
*getmoduledep(const char *name
) {
92 for (i
=0;i
<knownmodules
;i
++) {
93 if (!strcmp(name
,moduledeps
[i
].name
->content
))
94 return &(moduledeps
[i
]);
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() {
108 struct module_dep
*mdp
, *tmdp
;
111 sprintf(buf
,"%s/%s",moddir
->content
,DEPFILE
);
113 if (!(fp
=fopen(buf
,"r"))) {
114 Error("core",ERR_WARNING
,"Unable to open module dependency file: %s",buf
);
118 fgets(buf
, sizeof(buf
), fp
);
122 /* Chomp off that ruddy newline. */
123 for (ch
=buf
;*ch
;ch
++) {
124 if (*ch
=='\n' || *ch
=='\r') {
130 /* We have a space-delimited list of things. Whatever will we do with that? :) */
131 largc
=splitline(buf
,largv
,100,0);
136 /* Add us to the array */
139 Error("core",ERR_ERROR
,
140 "Too many modules in dependency file; rebuild with higher MAXMODULES. Module dependencies disabled.\n");
145 mdp
=&(moduledeps
[i
]);
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
*));
152 /* Fill in the parents array */
153 for (i
=0;i
<(largc
-1);i
++) {
154 if (!(mdp
->parents
[i
]=getmoduledep(largv
[i
+1]))) {
155 Error("core",ERR_WARNING
,"Couldn't find parent module %s of %s. Module dependencies disabled.",
156 largv
[i
+1],largv
[0]);
160 mdp
->parents
[i
]->numchildren
++; /* break the bad news */
165 for (i
=0;i
<knownmodules
;i
++) {
166 mdp
=&(moduledeps
[i
]);
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 */
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... */
178 tmdp
->children
[tmdp
->numchildren
++]=mdp
; /* ... to give this line a chance at making sense */
184 void modulerehash() {
193 freesstring(modsuffix
);
197 moddir
=getcopyconfigitem("core","moduledir",".",100);
198 modsuffix
=getcopyconfigitem("core","modulesuffix",".so",5);
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
);
213 array_init(&modules
,sizeof(module));
214 array_setlim1(&modules
,5);
215 array_setlim2(&modules
,10);
222 int insmod(char *modulename
) {
226 const char *(*verinfo
)(const char **);
227 struct module_dep
*mdp
;
229 delchars(modulename
,"./\\;");
231 if (isloaded(modulename
)) {
232 Error("core",ERR_DEBUG
,"Tried to load already loaded module: %s",modulename
);
236 if (strlen(modulename
)>100) {
237 Error("core",ERR_WARNING
,"Module name too long: %s",modulename
);
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
);
252 Error("core",ERR_WARNING
,"Loading module %s without dependency information.",modulename
);
255 i
=array_getfreeslot(&modules
);
256 mods
=(module *)(modules
.content
);
258 sprintf(buf
,"%s/%s%s",moddir
->content
,modulename
,modsuffix
->content
);
260 mods
[i
].handle
=dlopen(buf
,RTLD_NOW
|RTLD_GLOBAL
);
262 if(mods
[i
].handle
==NULL
) {
263 Error("core",ERR_ERROR
,"Loading module %s failed: %s",modulename
,dlerror());
264 array_delslot(&modules
,i
);
268 mods
[i
].name
=getsstring(modulename
,MODULENAMELEN
);
270 verinfo
=dlsym(mods
[i
].handle
,"_version");
272 mods
[i
].buildid
=verinfo(&mods
[i
].version
);
274 mods
[i
].version
=NULL
;
275 mods
[i
].buildid
=NULL
;
278 mods
[i
].loadedsince
= time(NULL
);
279 Error("core",ERR_INFO
,"Loaded module %s OK.",modulename
);
284 int getindex(char *modulename
) {
288 mods
=(module *)(modules
.content
);
289 for(i
=0;i
<modules
.cursi
;i
++)
290 if (!strcmp(mods
[i
].name
->content
,modulename
))
296 char *lsmod(int index
, const char **ver
, const char **buildid
, time_t *t
) {
299 if (index
< 0 || index
>= modules
.cursi
)
302 mods
=(module *)(modules
.content
);
304 *ver
=mods
[index
].version
;
306 *buildid
=mods
[index
].buildid
;
308 *t
=mods
[index
].loadedsince
;
310 return mods
[index
].name
->content
;
313 int isloaded(char *modulename
) {
314 if (getindex(modulename
)==-1)
321 int rmmod(char *modulename
) {
324 struct module_dep
*mdp
;
326 delchars(modulename
,"./\\;");
328 i
=getindex(modulename
);
332 if ((mdp
=getmoduledep(modulename
))) {
333 for (j
=0;j
<mdp
->numchildren
;j
++) {
334 if (isloaded(mdp
->children
[j
]->name
->content
)) {
335 if (rmmod(mdp
->children
[j
]->name
->content
)) {
336 Error("core",ERR_WARNING
,"Unable to remove child module %s (depends on %s)",
337 mdp
->children
[j
]->name
->content
, modulename
);
343 /* We may have removed other modules - reaquire the index number in case it has changed. */
344 i
=getindex(modulename
);
348 Error("core",ERR_WARNING
,"Removing module %s without dependency information",modulename
);
351 mods
=(module *)(modules
.content
);
353 #ifdef BROKEN_DLCLOSE
356 fini
= dlsym(mods
[i
].handle
, "__fini");
362 dlclose(mods
[i
].handle
);
363 freesstring(mods
[i
].name
);
364 array_delslot(&modules
,i
);
366 Error("core",ERR_INFO
,"Removed module %s.",modulename
);
371 /* Set the reload mark on the indicated module, if loaded, and all its
373 void setreloadmark(struct module_dep
*mdp
) {
376 if (!isloaded(mdp
->name
->content
))
379 for (i
=0;i
<mdp
->numchildren
;i
++)
380 setreloadmark(mdp
->children
[i
]);
385 /* preparereload: this function marks in the dependency tree all the
386 * modules which will be unloaded if this one is removed. */
387 void preparereload(char *modulename
) {
389 struct module_dep
*mdp
;
391 delchars(modulename
,"./\\;");
393 /* First, clear the mark off all dependant modules */
394 for (i
=0;i
<knownmodules
;i
++)
395 moduledeps
[i
].reloading
=0;
397 /* Do nothing if this module is not loaded */
398 i
=getindex(modulename
);
402 if ((mdp
=getmoduledep(modulename
))) {
407 /* reloadmarked: this function loads all the modules marked for reloading.
408 * The way the array is ordered means we will do this in the "right" order.
409 * This means we won't do an isloaded() check for each one - we shouldn't
410 * get any "already loaded" warnings so let them come out if they happen.
412 void reloadmarked(void) {
415 for (i
=0;i
<knownmodules
;i
++) {
416 if (moduledeps
[i
].reloading
) {
417 insmod(moduledeps
[i
].name
->content
);
422 void safereloadcallback(void *arg
) {
423 safereload_sched
=NULL
;
428 preparereload(safereload_str
->content
);
429 rmmod(safereload_str
->content
);
430 insmod(safereload_str
->content
);
433 freesstring(safereload_str
);
437 void safereload(char *themodule
) {
439 freesstring(safereload_str
);
441 safereload_str
=getsstring(themodule
, 100);
443 if (safereload_sched
)
444 deleteschedule(safereload_sched
, safereloadcallback
, NULL
);
446 scheduleoneshot(1, safereloadcallback
, NULL
);
449 void newserv_shutdown() {
453 while (modules
.cursi
) {
454 mods
=(module *)(modules
.content
);
456 strlcpy(buf
, mods
[0].name
->content
, sizeof(buf
));
466 freesstring(modsuffix
);
468 Error("core",ERR_INFO
,"All modules removed. Exiting.");
471 /* very slow, make sure you cache the pointer! */
472 void *ndlsym(char *modulename
, char *fn
) {
473 module *mods
=(module *)(modules
.content
);
474 int i
=getindex(modulename
);
479 return dlsym(mods
[i
].handle
, fn
);