]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * modules.c: A module loader. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
23 | */ | |
24 | ||
25 | #include "stdinc.h" | |
26 | #include "modules.h" | |
27 | #include "logger.h" | |
28 | #include "ircd.h" | |
29 | #include "client.h" | |
30 | #include "send.h" | |
31 | #include "s_conf.h" | |
32 | #include "s_newconf.h" | |
33 | #include "numeric.h" | |
34 | #include "parse.h" | |
35 | #include "ircd_defs.h" | |
36 | #include "match.h" | |
37 | #include "s_serv.h" | |
38 | #include "capability.h" | |
39 | #include "hash.h" | |
40 | ||
41 | #include <ltdl.h> | |
42 | ||
43 | #ifndef LT_MODULE_EXT | |
44 | # error "Solanum requires loadable module support." | |
45 | #endif | |
46 | ||
47 | rb_dlink_list module_list; | |
48 | rb_dlink_list mod_paths; | |
49 | ||
50 | static const char *core_module_table[] = { | |
51 | "m_ban", | |
52 | "m_die", | |
53 | "m_error", | |
54 | "m_identified", | |
55 | "m_join", | |
56 | "m_kick", | |
57 | "m_kill", | |
58 | "m_message", | |
59 | "m_mode", | |
60 | "m_modules", | |
61 | "m_nick", | |
62 | "m_part", | |
63 | "m_quit", | |
64 | "m_server", | |
65 | "m_squit", | |
66 | NULL | |
67 | }; | |
68 | ||
69 | #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */ | |
70 | ||
71 | void | |
72 | init_modules(void) | |
73 | { | |
74 | if(lt_dlinit()) | |
75 | { | |
76 | ilog(L_MAIN, "lt_dlinit failed"); | |
77 | exit(EXIT_FAILURE); | |
78 | } | |
79 | ||
80 | memset(&module_list, 0, sizeof(module_list)); | |
81 | memset(&mod_paths, 0, sizeof(mod_paths)); | |
82 | ||
83 | /* Add the default paths we look in to the module system --nenolod */ | |
84 | mod_add_path(ircd_paths[IRCD_PATH_MODULES]); | |
85 | mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); | |
86 | } | |
87 | ||
88 | static unsigned int prev_caps; | |
89 | ||
90 | void | |
91 | mod_remember_clicaps(void) | |
92 | { | |
93 | prev_caps = capability_index_mask(cli_capindex); | |
94 | } | |
95 | ||
96 | void | |
97 | mod_notify_clicaps(void) | |
98 | { | |
99 | unsigned int cur_caps = capability_index_mask(cli_capindex); | |
100 | unsigned int del = prev_caps & ~cur_caps; | |
101 | unsigned int new = cur_caps & ~prev_caps; | |
102 | ||
103 | if (del) | |
104 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", | |
105 | me.name, capability_index_list(cli_capindex, del)); | |
106 | if (new) | |
107 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * NEW :%s", | |
108 | me.name, capability_index_list(cli_capindex, new)); | |
109 | } | |
110 | ||
111 | /* mod_find_path() | |
112 | * | |
113 | * input - path | |
114 | * output - none | |
115 | * side effects - returns a module path from path | |
116 | */ | |
117 | static char * | |
118 | mod_find_path(const char *path) | |
119 | { | |
120 | rb_dlink_node *ptr; | |
121 | char *mpath; | |
122 | ||
123 | RB_DLINK_FOREACH(ptr, mod_paths.head) | |
124 | { | |
125 | mpath = ptr->data; | |
126 | ||
127 | if(!strcmp(path, mpath)) | |
128 | return mpath; | |
129 | } | |
130 | ||
131 | return NULL; | |
132 | } | |
133 | ||
134 | /* mod_add_path | |
135 | * | |
136 | * input - path | |
137 | * ouput - | |
138 | * side effects - adds path to list | |
139 | */ | |
140 | void | |
141 | mod_add_path(const char *path) | |
142 | { | |
143 | char *pathst; | |
144 | ||
145 | if(mod_find_path(path)) | |
146 | return; | |
147 | ||
148 | pathst = rb_strdup(path); | |
149 | rb_dlinkAddAlloc(pathst, &mod_paths); | |
150 | } | |
151 | ||
152 | /* mod_clear_paths() | |
153 | * | |
154 | * input - | |
155 | * output - | |
156 | * side effects - clear the lists of paths | |
157 | */ | |
158 | void | |
159 | mod_clear_paths(void) | |
160 | { | |
161 | rb_dlink_node *ptr, *next_ptr; | |
162 | ||
163 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) | |
164 | { | |
165 | rb_free(ptr->data); | |
166 | rb_free_rb_dlink_node(ptr); | |
167 | } | |
168 | ||
169 | mod_paths.head = mod_paths.tail = NULL; | |
170 | mod_paths.length = 0; | |
171 | } | |
172 | ||
173 | /* findmodule_byname | |
174 | * | |
175 | * input - module to load | |
176 | * output - index of module on success, -1 on failure | |
177 | * side effects - none | |
178 | */ | |
179 | struct module * | |
180 | findmodule_byname(const char *name) | |
181 | { | |
182 | rb_dlink_node *ptr; | |
183 | char name_ext[PATH_MAX + 1]; | |
184 | ||
185 | rb_strlcpy(name_ext, name, sizeof name_ext); | |
186 | rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext); | |
187 | ||
188 | RB_DLINK_FOREACH(ptr, module_list.head) | |
189 | { | |
190 | struct module *mod = ptr->data; | |
191 | ||
192 | if(!irccmp(mod->name, name)) | |
193 | return mod; | |
194 | ||
195 | if(!irccmp(mod->name, name_ext)) | |
196 | return mod; | |
197 | } | |
198 | ||
199 | return NULL; | |
200 | } | |
201 | ||
202 | /* load_all_modules() | |
203 | * | |
204 | * input - | |
205 | * output - | |
206 | * side effects - | |
207 | */ | |
208 | void | |
209 | load_all_modules(bool warn) | |
210 | { | |
211 | DIR *system_module_dir = NULL; | |
212 | struct dirent *ldirent = NULL; | |
213 | char module_fq_name[PATH_MAX + 1]; | |
214 | size_t module_ext_len = strlen(LT_MODULE_EXT); | |
215 | ||
216 | system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); | |
217 | ||
218 | if(system_module_dir == NULL) | |
219 | { | |
220 | ilog(L_MAIN, "Could not load modules from %s: %s", ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], strerror(errno)); | |
221 | return; | |
222 | } | |
223 | ||
224 | while ((ldirent = readdir(system_module_dir)) != NULL) | |
225 | { | |
226 | size_t len = strlen(ldirent->d_name); | |
227 | ||
228 | if(len > module_ext_len && | |
229 | rb_strncasecmp(ldirent->d_name + (len - module_ext_len), LT_MODULE_EXT, module_ext_len) == 0) | |
230 | { | |
231 | (void) snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", | |
232 | ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], ldirent->d_name); | |
233 | (void) load_a_module(module_fq_name, warn, MAPI_ORIGIN_CORE, false); | |
234 | } | |
235 | ||
236 | } | |
237 | (void) closedir(system_module_dir); | |
238 | } | |
239 | ||
240 | /* load_core_modules() | |
241 | * | |
242 | * input - | |
243 | * output - | |
244 | * side effects - core modules are loaded, if any fail, kill ircd | |
245 | */ | |
246 | void | |
247 | load_core_modules(bool warn) | |
248 | { | |
249 | char module_name[PATH_MAX]; | |
250 | int i; | |
251 | ||
252 | ||
253 | for (i = 0; core_module_table[i]; i++) | |
254 | { | |
255 | snprintf(module_name, sizeof(module_name), "%s/%s", ircd_paths[IRCD_PATH_MODULES], core_module_table[i]); | |
256 | ||
257 | if(load_a_module(module_name, warn, MAPI_ORIGIN_CORE, true) == false) | |
258 | { | |
259 | ilog(L_MAIN, | |
260 | "Error loading core module %s: terminating ircd", | |
261 | core_module_table[i]); | |
262 | exit(EXIT_FAILURE); | |
263 | } | |
264 | } | |
265 | } | |
266 | ||
267 | /* load_one_module() | |
268 | * | |
269 | * input - | |
270 | * output - | |
271 | * side effects - | |
272 | */ | |
273 | bool | |
274 | load_one_module(const char *path, int origin, bool coremodule) | |
275 | { | |
276 | char modpath[PATH_MAX]; | |
277 | rb_dlink_node *pathst; | |
278 | ||
279 | if (server_state_foreground) | |
280 | inotice("loading module %s ...", path); | |
281 | ||
282 | if(coremodule) | |
283 | origin = MAPI_ORIGIN_CORE; | |
284 | ||
285 | RB_DLINK_FOREACH(pathst, mod_paths.head) | |
286 | { | |
287 | struct stat statbuf; | |
288 | const char *mpath = pathst->data; | |
289 | ||
290 | snprintf(modpath, sizeof(modpath), "%s/%s%s", mpath, path, LT_MODULE_EXT); | |
291 | if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) | |
292 | { | |
293 | if(stat(modpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) | |
294 | { | |
295 | /* Regular files only please */ | |
296 | return load_a_module(modpath, true, origin, coremodule); | |
297 | } | |
298 | ||
299 | } | |
300 | } | |
301 | ||
302 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Cannot locate module %s", path); | |
303 | ||
304 | if (server_state_foreground) | |
305 | ierror("cannot locate module %s", path); | |
306 | ||
307 | return false; | |
308 | } | |
309 | ||
310 | static char unknown_ver[] = "<unknown>"; | |
311 | static char unknown_description[] = "<none>"; | |
312 | ||
313 | /* unload_one_module() | |
314 | * | |
315 | * inputs - name of module to unload | |
316 | * - true to say modules unloaded, false to not | |
317 | * output - true if successful, false if error | |
318 | * side effects - module is unloaded | |
319 | */ | |
320 | bool | |
321 | unload_one_module(const char *name, bool warn) | |
322 | { | |
323 | struct module *mod; | |
324 | ||
325 | if((mod = findmodule_byname(name)) == NULL) | |
326 | return false; | |
327 | ||
328 | /* | |
329 | ** XXX - The type system in C does not allow direct conversion between | |
330 | ** data and function pointers, but as it happens, most C compilers will | |
331 | ** safely do this, however it is a theoretical overlow to cast as we | |
332 | ** must do here. I have library functions to take care of this, but | |
333 | ** despite being more "correct" for the C language, this is more | |
334 | ** practical. Removing the abuse of the ability to cast ANY pointer | |
335 | ** to and from an integer value here will break some compilers. | |
336 | ** -jmallett | |
337 | */ | |
338 | /* Left the comment in but the code isn't here any more -larne */ | |
339 | switch (mod->mapi_version) | |
340 | { | |
341 | case 1: | |
342 | { | |
343 | struct mapi_mheader_av1 *mheader = mod->mapi_header; | |
344 | if(mheader->mapi_command_list) | |
345 | { | |
346 | struct Message **m; | |
347 | for (m = mheader->mapi_command_list; *m; ++m) | |
348 | mod_del_cmd(*m); | |
349 | } | |
350 | ||
351 | /* hook events are never removed, we simply lose the | |
352 | * ability to call them --fl | |
353 | */ | |
354 | if(mheader->mapi_hfn_list) | |
355 | { | |
356 | mapi_hfn_list_av1 *m; | |
357 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
358 | remove_hook(m->hapi_name, m->fn); | |
359 | } | |
360 | ||
361 | if(mheader->mapi_unregister) | |
362 | mheader->mapi_unregister(); | |
363 | break; | |
364 | } | |
365 | case 2: | |
366 | { | |
367 | struct mapi_mheader_av2 *mheader = mod->mapi_header; | |
368 | ||
369 | /* XXX duplicate code :( */ | |
370 | if(mheader->mapi_command_list) | |
371 | { | |
372 | struct Message **m; | |
373 | for (m = mheader->mapi_command_list; *m; ++m) | |
374 | mod_del_cmd(*m); | |
375 | } | |
376 | ||
377 | /* hook events are never removed, we simply lose the | |
378 | * ability to call them --fl | |
379 | */ | |
380 | if(mheader->mapi_hfn_list) | |
381 | { | |
382 | mapi_hfn_list_av1 *m; | |
383 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
384 | remove_hook(m->hapi_name, m->fn); | |
385 | } | |
386 | ||
387 | if(mheader->mapi_unregister) | |
388 | mheader->mapi_unregister(); | |
389 | ||
390 | if(mheader->mapi_cap_list) | |
391 | { | |
392 | mapi_cap_list_av2 *m; | |
393 | for (m = mheader->mapi_cap_list; m->cap_name; ++m) | |
394 | { | |
395 | struct CapabilityIndex *idx; | |
396 | ||
397 | switch (m->cap_index) | |
398 | { | |
399 | case MAPI_CAP_CLIENT: | |
400 | idx = cli_capindex; | |
401 | break; | |
402 | case MAPI_CAP_SERVER: | |
403 | idx = serv_capindex; | |
404 | break; | |
405 | default: | |
406 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
407 | "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", | |
408 | m->cap_index, m->cap_name, mod->name); | |
409 | ilog(L_MAIN, | |
410 | "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", | |
411 | m->cap_index, m->cap_name, mod->name); | |
412 | continue; | |
413 | } | |
414 | ||
415 | capability_orphan(idx, m->cap_name); | |
416 | } | |
417 | } | |
418 | break; | |
419 | } | |
420 | default: | |
421 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
422 | "Unknown/unsupported MAPI version %d when unloading %s!", | |
423 | mod->mapi_version, mod->name); | |
424 | ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!", | |
425 | mod->mapi_version, mod->name); | |
426 | break; | |
427 | } | |
428 | ||
429 | lt_dlclose(mod->address); | |
430 | ||
431 | rb_dlinkDelete(&mod->node, &module_list); | |
432 | rb_free(mod->name); | |
433 | rb_free(mod->path); | |
434 | rb_free(mod); | |
435 | ||
436 | if(warn) | |
437 | { | |
438 | ilog(L_MAIN, "Module %s unloaded", name); | |
439 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, "Module %s unloaded", name); | |
440 | } | |
441 | ||
442 | return true; | |
443 | } | |
444 | ||
445 | /* | |
446 | * load_a_module() | |
447 | * | |
448 | * inputs - path name of module, bool to notice, int of origin, bool if core | |
449 | * output - false if error true if success | |
450 | * side effects - loads a module if successful | |
451 | */ | |
452 | bool | |
453 | load_a_module(const char *path, bool warn, int origin, bool core) | |
454 | { | |
455 | struct module *mod; | |
456 | lt_dlhandle tmpptr; | |
457 | char *mod_displayname, *c; | |
458 | const char *ver, *description = NULL; | |
459 | ||
460 | int *mapi_version; | |
461 | ||
462 | mod_displayname = rb_basename(path); | |
463 | ||
464 | /* Trim off the ending for the display name if we have to */ | |
465 | if((c = rb_strcasestr(mod_displayname, LT_MODULE_EXT)) != NULL) | |
466 | *c = '\0'; | |
467 | ||
468 | /* Quietly succeed if the module is already loaded */ | |
469 | if(findmodule_byname(mod_displayname) != NULL) | |
470 | return true; | |
471 | ||
472 | tmpptr = lt_dlopenext(path); | |
473 | ||
474 | if(tmpptr == NULL) | |
475 | { | |
476 | const char *err = lt_dlerror(); | |
477 | ||
478 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
479 | "Error loading module %s: %s", mod_displayname, err); | |
480 | ilog(L_MAIN, "Error loading module %s: %s", mod_displayname, err); | |
481 | rb_free(mod_displayname); | |
482 | return false; | |
483 | } | |
484 | ||
485 | /* | |
486 | * _mheader is actually a struct mapi_mheader_*, but mapi_version | |
487 | * is always the first member of this structure, so we treate it | |
488 | * as a single int in order to determine the API version. | |
489 | * -larne. | |
490 | */ | |
491 | mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader"); | |
492 | if((mapi_version == NULL | |
493 | && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL) | |
494 | || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR) | |
495 | { | |
496 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
497 | "Data format error: module %s has no MAPI header.", | |
498 | mod_displayname); | |
499 | ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_displayname); | |
500 | (void) lt_dlclose(tmpptr); | |
501 | rb_free(mod_displayname); | |
502 | return false; | |
503 | } | |
504 | ||
505 | switch (MAPI_VERSION(*mapi_version)) | |
506 | { | |
507 | case 1: | |
508 | { | |
509 | struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */ | |
510 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) | |
511 | { | |
512 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
513 | mod_displayname); | |
514 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
515 | "Module %s indicated failure during load.", | |
516 | mod_displayname); | |
517 | lt_dlclose(tmpptr); | |
518 | rb_free(mod_displayname); | |
519 | return false; | |
520 | } | |
521 | if(mheader->mapi_command_list) | |
522 | { | |
523 | struct Message **m; | |
524 | for (m = mheader->mapi_command_list; *m; ++m) | |
525 | mod_add_cmd(*m); | |
526 | } | |
527 | ||
528 | if(mheader->mapi_hook_list) | |
529 | { | |
530 | mapi_hlist_av1 *m; | |
531 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
532 | *m->hapi_id = register_hook(m->hapi_name); | |
533 | } | |
534 | ||
535 | if(mheader->mapi_hfn_list) | |
536 | { | |
537 | mapi_hfn_list_av1 *m; | |
538 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
539 | add_hook(m->hapi_name, m->fn); | |
540 | } | |
541 | ||
542 | ver = mheader->mapi_module_version; | |
543 | break; | |
544 | } | |
545 | case 2: | |
546 | { | |
547 | struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version; /* see above */ | |
548 | ||
549 | if(mheader->mapi_cap_list) | |
550 | { | |
551 | mapi_cap_list_av2 *m; | |
552 | for (m = mheader->mapi_cap_list; m->cap_name; ++m) | |
553 | { | |
554 | struct CapabilityIndex *idx; | |
555 | int result; | |
556 | ||
557 | switch (m->cap_index) | |
558 | { | |
559 | case MAPI_CAP_CLIENT: | |
560 | idx = cli_capindex; | |
561 | break; | |
562 | case MAPI_CAP_SERVER: | |
563 | idx = serv_capindex; | |
564 | break; | |
565 | default: | |
566 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
567 | "Unknown/unsupported CAP index found of type %d on capability %s when loading %s", | |
568 | m->cap_index, m->cap_name, mod_displayname); | |
569 | ilog(L_MAIN, | |
570 | "Unknown/unsupported CAP index found of type %d on capability %s when loading %s", | |
571 | m->cap_index, m->cap_name, mod_displayname); | |
572 | continue; | |
573 | } | |
574 | ||
575 | result = capability_put(idx, m->cap_name, m->cap_ownerdata); | |
576 | if (m->cap_id != NULL) | |
577 | *(m->cap_id) = result; | |
578 | } | |
579 | } | |
580 | ||
581 | /* XXX duplicated code :( */ | |
582 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) | |
583 | { | |
584 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
585 | mod_displayname); | |
586 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
587 | "Module %s indicated failure during load.", | |
588 | mod_displayname); | |
589 | if(mheader->mapi_cap_list) | |
590 | { | |
591 | mapi_cap_list_av2 *m; | |
592 | for (m = mheader->mapi_cap_list; m->cap_name; ++m) | |
593 | { | |
594 | struct CapabilityIndex *idx; | |
595 | switch (m->cap_index) | |
596 | { | |
597 | case MAPI_CAP_CLIENT: | |
598 | idx = cli_capindex; | |
599 | break; | |
600 | case MAPI_CAP_SERVER: | |
601 | idx = serv_capindex; | |
602 | break; | |
603 | default: | |
604 | continue; | |
605 | } | |
606 | capability_orphan(idx, m->cap_name); | |
607 | } | |
608 | } | |
609 | lt_dlclose(tmpptr); | |
610 | rb_free(mod_displayname); | |
611 | return false; | |
612 | } | |
613 | ||
614 | /* Basic date code checks | |
615 | * | |
616 | * Don't make them fatal, but do complain about differences within a certain time frame. | |
617 | * Later on if there are major API changes we can add fatal checks. | |
618 | * -- Elizafox | |
619 | */ | |
620 | if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0) | |
621 | { | |
622 | long int delta = datecode - mheader->mapi_datecode; | |
623 | if (delta > MOD_WARN_DELTA) | |
624 | { | |
625 | delta /= 86400; | |
626 | iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems", | |
627 | mod_displayname, delta); | |
628 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
629 | "Module %s build date is out of sync with ircd build date by %ld days, expect problems", | |
630 | mod_displayname, delta); | |
631 | } | |
632 | } | |
633 | ||
634 | if(mheader->mapi_command_list) | |
635 | { | |
636 | struct Message **m; | |
637 | for (m = mheader->mapi_command_list; *m; ++m) | |
638 | mod_add_cmd(*m); | |
639 | } | |
640 | ||
641 | if(mheader->mapi_hook_list) | |
642 | { | |
643 | mapi_hlist_av1 *m; | |
644 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
645 | *m->hapi_id = register_hook(m->hapi_name); | |
646 | } | |
647 | ||
648 | if(mheader->mapi_hfn_list) | |
649 | { | |
650 | mapi_hfn_list_av1 *m; | |
651 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
652 | { | |
653 | int priority = m->priority; | |
654 | if (priority == 0) | |
655 | priority = HOOK_NORMAL; | |
656 | add_hook_prio(m->hapi_name, m->fn, priority); | |
657 | } | |
658 | } | |
659 | ||
660 | /* New in MAPI v2 - version replacement */ | |
661 | ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version; | |
662 | description = mheader->mapi_module_description; | |
663 | } | |
664 | ||
665 | break; | |
666 | default: | |
667 | ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.", | |
668 | mod_displayname, MAPI_VERSION(*mapi_version)); | |
669 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
670 | "Module %s has unknown/unsupported MAPI version %d.", | |
671 | mod_displayname, *mapi_version); | |
672 | lt_dlclose(tmpptr); | |
673 | rb_free(mod_displayname); | |
674 | return false; | |
675 | } | |
676 | ||
677 | if(ver == NULL) | |
678 | ver = unknown_ver; | |
679 | ||
680 | if(description == NULL) | |
681 | description = unknown_description; | |
682 | ||
683 | mod = rb_malloc(sizeof(struct module)); | |
684 | mod->address = tmpptr; | |
685 | mod->version = ver; | |
686 | mod->description = description; | |
687 | mod->core = core; | |
688 | mod->name = rb_strdup(mod_displayname); | |
689 | mod->mapi_header = mapi_version; | |
690 | mod->mapi_version = MAPI_VERSION(*mapi_version); | |
691 | mod->origin = origin; | |
692 | mod->path = rb_strdup(path); | |
693 | rb_dlinkAdd(mod, &mod->node, &module_list); | |
694 | ||
695 | if(warn) | |
696 | { | |
697 | const char *o; | |
698 | ||
699 | switch (origin) | |
700 | { | |
701 | case MAPI_ORIGIN_EXTENSION: | |
702 | o = "extension"; | |
703 | break; | |
704 | case MAPI_ORIGIN_CORE: | |
705 | o = "core"; | |
706 | break; | |
707 | default: | |
708 | o = "unknown"; | |
709 | break; | |
710 | } | |
711 | ||
712 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
713 | "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p", | |
714 | mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, | |
715 | (void *) tmpptr); | |
716 | ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p", | |
717 | mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, (void *) tmpptr); | |
718 | } | |
719 | rb_free(mod_displayname); | |
720 | return true; | |
721 | } | |
722 | ||
723 | void | |
724 | modules_do_reload(void *info_) | |
725 | { | |
726 | struct modreload *info = info_; | |
727 | struct module *mod; | |
728 | int check_core; | |
729 | int origin; | |
730 | char *m_bn = rb_basename(info->module); | |
731 | char *path; | |
732 | struct Client *source_p = find_id(info->id); | |
733 | ||
734 | if((mod = findmodule_byname(m_bn)) == NULL) | |
735 | { | |
736 | if (source_p) sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); | |
737 | rb_free(info); | |
738 | rb_free(m_bn); | |
739 | return; | |
740 | } | |
741 | ||
742 | origin = mod->origin; | |
743 | check_core = mod->core; | |
744 | path = rb_strdup(mod->path); | |
745 | ||
746 | mod_remember_clicaps(); | |
747 | ||
748 | if(unload_one_module(m_bn, true) == false) | |
749 | { | |
750 | if (source_p) sendto_one_notice(source_p, ":Module %s is not loaded", m_bn); | |
751 | rb_free(info); | |
752 | rb_free(m_bn); | |
753 | rb_free(path); | |
754 | return; | |
755 | } | |
756 | ||
757 | if((load_a_module(path, true, origin, check_core) == false) && check_core) | |
758 | { | |
759 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
760 | "Error reloading core module: %s: terminating ircd", m_bn); | |
761 | ilog(L_MAIN, "Error loading core module %s: terminating ircd", m_bn); | |
762 | exit(0); | |
763 | } | |
764 | ||
765 | mod_notify_clicaps(); | |
766 | ||
767 | rb_free(info); | |
768 | rb_free(m_bn); | |
769 | rb_free(path); | |
770 | } | |
771 | ||
772 | void | |
773 | modules_do_restart(void *unused) | |
774 | { | |
775 | unsigned int modnum = 0; | |
776 | rb_dlink_node *ptr, *nptr; | |
777 | ||
778 | mod_remember_clicaps(); | |
779 | ||
780 | RB_DLINK_FOREACH_SAFE(ptr, nptr, module_list.head) | |
781 | { | |
782 | struct module *mod = ptr->data; | |
783 | if(!unload_one_module(mod->name, false)) | |
784 | { | |
785 | ilog(L_MAIN, "Module Restart: %s was not unloaded %s", | |
786 | mod->name, | |
787 | mod->core? "(core module)" : ""); | |
788 | ||
789 | if(!mod->core) | |
790 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
791 | "Module Restart: %s failed to unload", | |
792 | mod->name); | |
793 | continue; | |
794 | } | |
795 | ||
796 | modnum++; | |
797 | } | |
798 | ||
799 | load_all_modules(false); | |
800 | load_core_modules(false); | |
801 | rehash(false); | |
802 | ||
803 | mod_notify_clicaps(); | |
804 | ||
805 | sendto_realops_snomask(SNO_GENERAL, L_NETWIDE, | |
806 | "Module Restart: %u modules unloaded, %lu modules loaded", | |
807 | modnum, rb_dlink_list_length(&module_list)); | |
808 | ilog(L_MAIN, "Module Restart: %u modules unloaded, %lu modules loaded", modnum, rb_dlink_list_length(&module_list)); | |
809 | } |