39 |
|
#include "memory.h" |
40 |
|
|
41 |
|
|
42 |
< |
static dlink_list modules_list = { NULL, NULL, 0 }; |
42 |
> |
dlink_list modules_list = { NULL, NULL, 0 }; |
43 |
|
|
44 |
|
static const char *unknown_ver = "<unknown>"; |
45 |
|
|
64 |
|
static dlink_list mod_paths = { NULL, NULL, 0 }; |
65 |
|
static dlink_list conf_modules = { NULL, NULL, 0 }; |
66 |
|
|
67 |
– |
static void mo_modload(struct Client *, struct Client *, int, char *[]); |
68 |
– |
static void mo_modlist(struct Client *, struct Client *, int, char *[]); |
69 |
– |
static void mo_modreload(struct Client *, struct Client *, int, char *[]); |
70 |
– |
static void mo_modunload(struct Client *, struct Client *, int, char *[]); |
71 |
– |
static void mo_modrestart(struct Client *, struct Client *, int, char *[]); |
72 |
– |
|
73 |
– |
struct Message modload_msgtab = { |
74 |
– |
"MODLOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
75 |
– |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modload, m_ignore} |
76 |
– |
}; |
77 |
– |
|
78 |
– |
struct Message modunload_msgtab = { |
79 |
– |
"MODUNLOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
80 |
– |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modunload, m_ignore} |
81 |
– |
}; |
82 |
– |
|
83 |
– |
struct Message modreload_msgtab = { |
84 |
– |
"MODRELOAD", 0, 0, 2, 0, MFLG_SLOW, 0, |
85 |
– |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modreload, m_ignore} |
86 |
– |
}; |
87 |
– |
|
88 |
– |
struct Message modlist_msgtab = { |
89 |
– |
"MODLIST", 0, 0, 0, 0, MFLG_SLOW, 0, |
90 |
– |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modlist, m_ignore} |
91 |
– |
}; |
92 |
– |
|
93 |
– |
struct Message modrestart_msgtab = { |
94 |
– |
"MODRESTART", 0, 0, 0, 0, MFLG_SLOW, 0, |
95 |
– |
{m_unregistered, m_not_oper, m_ignore, m_ignore, mo_modrestart, m_ignore} |
96 |
– |
}; |
97 |
– |
|
98 |
– |
|
67 |
|
int |
68 |
|
modules_valid_suffix(const char *name) |
69 |
|
{ |
97 |
|
if (warn == 1) |
98 |
|
{ |
99 |
|
ilog(LOG_TYPE_IRCD, "Module %s unloaded", name); |
100 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s unloaded", name); |
100 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
101 |
> |
"Module %s unloaded", name); |
102 |
|
} |
103 |
|
|
104 |
|
return 0; |
111 |
|
* side effects - loads a module if successful |
112 |
|
*/ |
113 |
|
int |
114 |
< |
load_a_module(const char *path, int warn, int core) |
114 |
> |
load_a_module(const char *path, int warn) |
115 |
|
{ |
116 |
|
lt_dlhandle tmpptr = NULL; |
117 |
|
const char *mod_basename = NULL; |
123 |
|
if (!(tmpptr = lt_dlopen(path))) { |
124 |
|
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
125 |
|
|
126 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error loading module %s: %s", |
126 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
127 |
> |
"Error loading module %s: %s", |
128 |
|
mod_basename, err); |
129 |
|
ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err); |
130 |
|
return -1; |
132 |
|
|
133 |
|
if ((modp = lt_dlsym(tmpptr, "module_entry")) == NULL) |
134 |
|
{ |
135 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, "Module %s has no module_entry export", |
136 |
< |
mod_basename); |
137 |
< |
ilog(LOG_TYPE_IRCD, "Module %s has no module_entry export", mod_basename); |
135 |
> |
const char *err = ((err = lt_dlerror())) ? err : "<unknown>"; |
136 |
> |
|
137 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
138 |
> |
"Error loading module %s: %s", |
139 |
> |
mod_basename, err); |
140 |
> |
ilog(LOG_TYPE_IRCD, "Error loading module %s: %s", mod_basename, err); |
141 |
|
lt_dlclose(tmpptr); |
142 |
|
return -1; |
143 |
|
} |
147 |
|
if (EmptyString(modp->version)) |
148 |
|
modp->version = unknown_ver; |
149 |
|
|
150 |
< |
if (core) |
178 |
< |
modp->flags |= MODULE_FLAG_CORE; |
179 |
< |
|
180 |
< |
DupString(modp->name, mod_basename); |
150 |
> |
modp->name = xstrdup(mod_basename); |
151 |
|
dlinkAdd(modp, &modp->node, &modules_list); |
152 |
|
|
153 |
|
if (modp->modinit) |
155 |
|
|
156 |
|
if (warn == 1) |
157 |
|
{ |
158 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, |
158 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
159 |
|
"Module %s [version: %s handle: %p] loaded.", |
160 |
|
modp->name, modp->version, tmpptr); |
161 |
|
ilog(LOG_TYPE_IRCD, "Module %s [version: %s handle: %p] loaded.", |
181 |
|
" link library. Exiting."); |
182 |
|
exit(0); |
183 |
|
} |
214 |
– |
|
215 |
– |
mod_add_cmd(&modload_msgtab); |
216 |
– |
mod_add_cmd(&modunload_msgtab); |
217 |
– |
mod_add_cmd(&modreload_msgtab); |
218 |
– |
mod_add_cmd(&modlist_msgtab); |
219 |
– |
mod_add_cmd(&modrestart_msgtab); |
184 |
|
} |
185 |
|
|
186 |
|
/* mod_find_path() |
193 |
|
mod_find_path(const char *path) |
194 |
|
{ |
195 |
|
dlink_node *ptr; |
232 |
– |
struct module_path *mpath; |
196 |
|
|
197 |
|
DLINK_FOREACH(ptr, mod_paths.head) |
198 |
|
{ |
199 |
< |
mpath = ptr->data; |
199 |
> |
struct module_path *mpath = ptr->data; |
200 |
|
|
201 |
|
if (!strcmp(path, mpath->path)) |
202 |
|
return mpath; |
251 |
|
void |
252 |
|
mod_clear_paths(void) |
253 |
|
{ |
291 |
– |
struct module_path *pathst = NULL; |
254 |
|
dlink_node *ptr = NULL, *next_ptr = NULL; |
255 |
|
|
256 |
|
DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
257 |
|
{ |
258 |
< |
pathst = ptr->data; |
259 |
< |
|
298 |
< |
dlinkDelete(&pathst->node, &mod_paths); |
299 |
< |
MyFree(pathst); |
258 |
> |
dlinkDelete(ptr, &mod_paths); |
259 |
> |
MyFree(ptr->data); |
260 |
|
} |
261 |
|
|
262 |
|
DLINK_FOREACH_SAFE(ptr, next_ptr, conf_modules.head) |
263 |
|
{ |
264 |
< |
pathst = ptr->data; |
265 |
< |
|
306 |
< |
dlinkDelete(&pathst->node, &conf_modules); |
307 |
< |
MyFree(pathst); |
264 |
> |
dlinkDelete(ptr, &conf_modules); |
265 |
> |
MyFree(ptr->data); |
266 |
|
} |
267 |
|
} |
268 |
|
|
299 |
|
{ |
300 |
|
DIR *system_module_dir = NULL; |
301 |
|
struct dirent *ldirent = NULL; |
302 |
< |
char module_fq_name[PATH_MAX + 1]; |
345 |
< |
|
346 |
< |
modules_init(); |
302 |
> |
char module_fq_name[HYB_PATH_MAX + 1]; |
303 |
|
|
304 |
|
if ((system_module_dir = opendir(AUTOMODPATH)) == NULL) |
305 |
|
{ |
314 |
|
{ |
315 |
|
snprintf(module_fq_name, sizeof(module_fq_name), "%s/%s", |
316 |
|
AUTOMODPATH, ldirent->d_name); |
317 |
< |
load_a_module(module_fq_name, warn, 0); |
317 |
> |
load_a_module(module_fq_name, warn); |
318 |
|
} |
319 |
|
} |
320 |
|
|
331 |
|
load_conf_modules(void) |
332 |
|
{ |
333 |
|
dlink_node *ptr = NULL; |
378 |
– |
struct module_path *mpath = NULL; |
334 |
|
|
335 |
|
DLINK_FOREACH(ptr, conf_modules.head) |
336 |
|
{ |
337 |
< |
mpath = ptr->data; |
337 |
> |
struct module_path *mpath = ptr->data; |
338 |
|
|
339 |
|
if (findmodule_byname(mpath->path) == NULL) |
340 |
< |
load_one_module(mpath->path, 0); |
340 |
> |
load_one_module(mpath->path); |
341 |
|
} |
342 |
|
} |
343 |
|
|
350 |
|
void |
351 |
|
load_core_modules(int warn) |
352 |
|
{ |
353 |
< |
char module_name[PATH_MAX + 1]; |
353 |
> |
char module_name[HYB_PATH_MAX + 1]; |
354 |
|
int i = 0; |
355 |
|
|
356 |
|
for (; core_module_table[i]; ++i) |
358 |
|
snprintf(module_name, sizeof(module_name), "%s%s", |
359 |
|
MODPATH, core_module_table[i]); |
360 |
|
|
361 |
< |
if (load_a_module(module_name, warn, 1) == -1) |
361 |
> |
if (load_a_module(module_name, warn) == -1) |
362 |
|
{ |
363 |
|
ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd", |
364 |
|
core_module_table[i]); |
375 |
|
* side effects - module is loaded if found. |
376 |
|
*/ |
377 |
|
int |
378 |
< |
load_one_module(const char *path, int coremodule) |
378 |
> |
load_one_module(const char *path) |
379 |
|
{ |
380 |
|
dlink_node *ptr = NULL; |
381 |
< |
char modpath[PATH_MAX + 1]; |
381 |
> |
char modpath[HYB_PATH_MAX + 1]; |
382 |
|
struct stat statbuf; |
383 |
|
|
384 |
|
DLINK_FOREACH(ptr, mod_paths.head) |
392 |
|
|
393 |
|
if (strstr(modpath, "../") == NULL && |
394 |
|
strstr(modpath, "/..") == NULL) |
440 |
– |
{ |
395 |
|
if (!stat(modpath, &statbuf)) |
396 |
< |
{ |
397 |
< |
if (S_ISREG(statbuf.st_mode)) |
444 |
< |
{ |
445 |
< |
/* Regular files only please */ |
446 |
< |
return load_a_module(modpath, 1, coremodule); |
447 |
< |
} |
448 |
< |
} |
449 |
< |
} |
396 |
> |
if (S_ISREG(statbuf.st_mode)) /* Regular files only please */ |
397 |
> |
return load_a_module(modpath, 1); |
398 |
|
} |
399 |
|
|
400 |
< |
sendto_realops_flags(UMODE_ALL, L_ALL, |
400 |
> |
sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE, |
401 |
|
"Cannot locate module %s", path); |
402 |
|
ilog(LOG_TYPE_IRCD, "Cannot locate module %s", path); |
403 |
|
return -1; |
404 |
|
} |
457 |
– |
|
458 |
– |
/* load a module .. */ |
459 |
– |
static void |
460 |
– |
mo_modload(struct Client *client_p, struct Client *source_p, |
461 |
– |
int parc, char *parv[]) |
462 |
– |
{ |
463 |
– |
const char *m_bn = NULL; |
464 |
– |
|
465 |
– |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
466 |
– |
{ |
467 |
– |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
468 |
– |
me.name, source_p->name); |
469 |
– |
return; |
470 |
– |
} |
471 |
– |
|
472 |
– |
m_bn = libio_basename(parv[1]); |
473 |
– |
|
474 |
– |
if (findmodule_byname(m_bn) != NULL) |
475 |
– |
{ |
476 |
– |
sendto_one(source_p, ":%s NOTICE %s :Module %s is already loaded", |
477 |
– |
me.name, source_p->name, m_bn); |
478 |
– |
return; |
479 |
– |
} |
480 |
– |
|
481 |
– |
load_one_module(parv[1], 0); |
482 |
– |
} |
483 |
– |
|
484 |
– |
/* unload a module .. */ |
485 |
– |
static void |
486 |
– |
mo_modunload(struct Client *client_p, struct Client *source_p, |
487 |
– |
int parc, char *parv[]) |
488 |
– |
{ |
489 |
– |
const char *m_bn = NULL; |
490 |
– |
struct module *modp = NULL; |
491 |
– |
|
492 |
– |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
493 |
– |
{ |
494 |
– |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
495 |
– |
me.name, source_p->name); |
496 |
– |
return; |
497 |
– |
} |
498 |
– |
|
499 |
– |
m_bn = libio_basename(parv[1]); |
500 |
– |
|
501 |
– |
if ((modp = findmodule_byname(m_bn)) == NULL) |
502 |
– |
{ |
503 |
– |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
504 |
– |
me.name, source_p->name, m_bn); |
505 |
– |
return; |
506 |
– |
} |
507 |
– |
|
508 |
– |
if (modp->flags & MODULE_FLAG_CORE) |
509 |
– |
{ |
510 |
– |
sendto_one(source_p, |
511 |
– |
":%s NOTICE %s :Module %s is a core module and may not be unloaded", |
512 |
– |
me.name, source_p->name, m_bn); |
513 |
– |
return; |
514 |
– |
} |
515 |
– |
|
516 |
– |
if (unload_one_module(m_bn, 1) == -1) |
517 |
– |
{ |
518 |
– |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
519 |
– |
me.name, source_p->name, m_bn); |
520 |
– |
} |
521 |
– |
} |
522 |
– |
|
523 |
– |
/* unload and load in one! */ |
524 |
– |
static void |
525 |
– |
mo_modreload(struct Client *client_p, struct Client *source_p, |
526 |
– |
int parc, char *parv[]) |
527 |
– |
{ |
528 |
– |
const char *m_bn = NULL; |
529 |
– |
struct module *modp = NULL; |
530 |
– |
int check_core; |
531 |
– |
|
532 |
– |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
533 |
– |
{ |
534 |
– |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
535 |
– |
me.name, source_p->name); |
536 |
– |
return; |
537 |
– |
} |
538 |
– |
|
539 |
– |
m_bn = libio_basename(parv[1]); |
540 |
– |
|
541 |
– |
if ((modp = findmodule_byname(m_bn)) == NULL) |
542 |
– |
{ |
543 |
– |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
544 |
– |
me.name, source_p->name, m_bn); |
545 |
– |
return; |
546 |
– |
} |
547 |
– |
|
548 |
– |
check_core = (modp->flags & MODULE_FLAG_CORE) != 0; |
549 |
– |
|
550 |
– |
if (unload_one_module(m_bn, 1) == -1) |
551 |
– |
{ |
552 |
– |
sendto_one(source_p, ":%s NOTICE %s :Module %s is not loaded", |
553 |
– |
me.name, source_p->name, m_bn); |
554 |
– |
return; |
555 |
– |
} |
556 |
– |
|
557 |
– |
if ((load_one_module(parv[1], check_core) == -1) && check_core) |
558 |
– |
{ |
559 |
– |
sendto_realops_flags(UMODE_ALL, L_ALL, "Error reloading core " |
560 |
– |
"module: %s: terminating ircd", parv[1]); |
561 |
– |
ilog(LOG_TYPE_IRCD, "Error loading core module %s: terminating ircd", parv[1]); |
562 |
– |
exit(0); |
563 |
– |
} |
564 |
– |
} |
565 |
– |
|
566 |
– |
/* list modules .. */ |
567 |
– |
static void |
568 |
– |
mo_modlist(struct Client *client_p, struct Client *source_p, |
569 |
– |
int parc, char *parv[]) |
570 |
– |
{ |
571 |
– |
const dlink_node *ptr = NULL; |
572 |
– |
|
573 |
– |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
574 |
– |
{ |
575 |
– |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
576 |
– |
me.name, source_p->name); |
577 |
– |
return; |
578 |
– |
} |
579 |
– |
|
580 |
– |
DLINK_FOREACH(ptr, modules_list.head) |
581 |
– |
{ |
582 |
– |
const struct module *modp = ptr->data; |
583 |
– |
|
584 |
– |
if (parc > 1 && !match(parv[1], modp->name)) |
585 |
– |
continue; |
586 |
– |
|
587 |
– |
sendto_one(source_p, form_str(RPL_MODLIST), me.name, source_p->name, |
588 |
– |
modp->name, modp->handle, |
589 |
– |
modp->version, (modp->flags & MODULE_FLAG_CORE) ?"(core)":""); |
590 |
– |
} |
591 |
– |
|
592 |
– |
sendto_one(source_p, form_str(RPL_ENDOFMODLIST), |
593 |
– |
me.name, source_p->name); |
594 |
– |
} |
595 |
– |
|
596 |
– |
/* unload and reload all modules */ |
597 |
– |
static void |
598 |
– |
mo_modrestart(struct Client *client_p, struct Client *source_p, |
599 |
– |
int parc, char *parv[]) |
600 |
– |
{ |
601 |
– |
unsigned int modnum = 0; |
602 |
– |
dlink_node *ptr = NULL, *ptr_next = NULL; |
603 |
– |
|
604 |
– |
if (!HasOFlag(source_p, OPER_FLAG_MODULE)) |
605 |
– |
{ |
606 |
– |
sendto_one(source_p, form_str(ERR_NOPRIVILEGES), |
607 |
– |
me.name, source_p->name); |
608 |
– |
return; |
609 |
– |
} |
610 |
– |
|
611 |
– |
sendto_one(source_p, ":%s NOTICE %s :Reloading all modules", |
612 |
– |
me.name, source_p->name); |
613 |
– |
|
614 |
– |
modnum = dlink_list_length(&modules_list); |
615 |
– |
|
616 |
– |
DLINK_FOREACH_SAFE(ptr, ptr_next, modules_list.head) |
617 |
– |
{ |
618 |
– |
struct module *modp = ptr->data; |
619 |
– |
unload_one_module(modp->name, 0); |
620 |
– |
} |
621 |
– |
|
622 |
– |
load_all_modules(0); |
623 |
– |
load_conf_modules(); |
624 |
– |
load_core_modules(0); |
625 |
– |
|
626 |
– |
sendto_realops_flags(UMODE_ALL, L_ALL, |
627 |
– |
"Module Restart: %u modules unloaded, %u modules loaded", |
628 |
– |
modnum, dlink_list_length(&modules_list)); |
629 |
– |
ilog(LOG_TYPE_IRCD, "Module Restart: %u modules unloaded, %u modules loaded", |
630 |
– |
modnum, dlink_list_length(&modules_list)); |
631 |
– |
} |