2 * ircd-ratbox: A slightly useful ircd.
3 * modules.c: A module loader.
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
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.
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.
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
24 * $Id: modules.c 3161 2007-01-25 07:23:01Z nenolod $
36 #include "s_newconf.h"
39 #include "ircd_defs.h"
45 struct module **modlist
= NULL
;
47 static const char *core_module_table
[] = {
64 #define MODS_INCREMENT 10
66 int max_mods
= MODS_INCREMENT
;
68 static rb_dlink_list mod_paths
;
70 static int mo_modload(struct Client
*, struct Client
*, int, const char **);
71 static int mo_modlist(struct Client
*, struct Client
*, int, const char **);
72 static int mo_modreload(struct Client
*, struct Client
*, int, const char **);
73 static int mo_modunload(struct Client
*, struct Client
*, int, const char **);
74 static int mo_modrestart(struct Client
*, struct Client
*, int, const char **);
76 struct Message modload_msgtab
= {
77 "MODLOAD", 0, 0, 0, MFLG_SLOW
,
78 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modload
, 2}}
81 struct Message modunload_msgtab
= {
82 "MODUNLOAD", 0, 0, 0, MFLG_SLOW
,
83 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modunload
, 2}}
86 struct Message modreload_msgtab
= {
87 "MODRELOAD", 0, 0, 0, MFLG_SLOW
,
88 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modreload
, 2}}
91 struct Message modlist_msgtab
= {
92 "MODLIST", 0, 0, 0, MFLG_SLOW
,
93 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modlist
, 0}}
96 struct Message modrestart_msgtab
= {
97 "MODRESTART", 0, 0, 0, MFLG_SLOW
,
98 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modrestart
, 0}}
106 ilog(L_MAIN
, "lt_dlinit failed");
110 mod_add_cmd(&modload_msgtab
);
111 mod_add_cmd(&modunload_msgtab
);
112 mod_add_cmd(&modreload_msgtab
);
113 mod_add_cmd(&modlist_msgtab
);
114 mod_add_cmd(&modrestart_msgtab
);
116 /* Add the default paths we look in to the module system --nenolod */
117 mod_add_path(MODPATH
);
118 mod_add_path(AUTOMODPATH
);
125 * side effects - returns a module path from path
128 mod_find_path(const char *path
)
133 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
137 if(!strcmp(path
, mpath
))
148 * side effects - adds path to list
151 mod_add_path(const char *path
)
155 if(mod_find_path(path
))
158 pathst
= rb_strdup(path
);
159 rb_dlinkAddAlloc(pathst
, &mod_paths
);
166 * side effects - clear the lists of paths
169 mod_clear_paths(void)
171 rb_dlink_node
*ptr
, *next_ptr
;
173 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
176 rb_free_rb_dlink_node(ptr
);
179 mod_paths
.head
= mod_paths
.tail
= NULL
;
180 mod_paths
.length
= 0;
191 findmodule_byname(const char *name
)
195 for (i
= 0; i
< num_mods
; i
++)
197 if(!irccmp(modlist
[i
]->name
, name
))
203 /* load_all_modules()
210 load_all_modules(int warn
)
212 DIR *system_module_dir
= NULL
;
213 struct dirent
*ldirent
= NULL
;
214 char module_fq_name
[PATH_MAX
+ 1];
219 modlist
= (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT
));
221 max_mods
= MODS_INCREMENT
;
223 system_module_dir
= opendir(AUTOMODPATH
);
225 if(system_module_dir
== NULL
)
227 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
231 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
233 len
= strlen(ldirent
->d_name
);
234 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, ".la"))
236 (void) rb_snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
237 (void) load_a_module(module_fq_name
, warn
, 0);
241 (void) closedir(system_module_dir
);
244 /* load_core_modules()
248 * side effects - core modules are loaded, if any fail, kill ircd
251 load_core_modules(int warn
)
253 char module_name
[PATH_MAX
];
257 for (i
= 0; core_module_table
[i
]; i
++)
259 rb_snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
260 core_module_table
[i
], ".la");
262 if(load_a_module(module_name
, warn
, 1) == -1)
265 "Error loading core module %s%s: terminating ircd",
266 core_module_table
[i
], ".la");
279 load_one_module(const char *path
, int coremodule
)
281 char modpath
[PATH_MAX
];
282 rb_dlink_node
*pathst
;
287 if (server_state_foreground
== 1)
288 inotice("loading module %s ...", path
);
290 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
292 mpath
= pathst
->data
;
294 rb_snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
, path
);
295 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
297 if(stat(modpath
, &statbuf
) == 0)
299 if(S_ISREG(statbuf
.st_mode
))
301 /* Regular files only please */
303 return load_a_module(modpath
, 1, 1);
305 return load_a_module(modpath
, 1, 0);
312 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
317 /* load a module .. */
319 mo_modload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
323 if(!IsOperAdmin(source_p
))
325 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
326 me
.name
, source_p
->name
, "admin");
330 m_bn
= rb_basename(parv
[1]);
332 if(findmodule_byname(m_bn
) != -1)
334 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
339 load_one_module(parv
[1], 0);
347 /* unload a module .. */
349 mo_modunload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
354 if(!IsOperAdmin(source_p
))
356 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
357 me
.name
, source_p
->name
, "admin");
361 m_bn
= rb_basename(parv
[1]);
363 if((modindex
= findmodule_byname(m_bn
)) == -1)
365 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
370 if(modlist
[modindex
]->core
== 1)
372 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
377 if(unload_one_module(m_bn
, 1) == -1)
379 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
386 /* unload and load in one! */
388 mo_modreload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
394 if(!IsOperAdmin(source_p
))
396 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
397 me
.name
, source_p
->name
, "admin");
401 m_bn
= rb_basename(parv
[1]);
403 if((modindex
= findmodule_byname(m_bn
)) == -1)
405 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
410 check_core
= modlist
[modindex
]->core
;
412 if(unload_one_module(m_bn
, 1) == -1)
414 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
419 if((load_one_module(m_bn
, check_core
) == -1) && check_core
)
421 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
422 "Error reloading core module: %s: terminating ircd", m_bn
);
423 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", m_bn
);
431 /* list modules .. */
433 mo_modlist(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
437 if(!IsOperAdmin(source_p
))
439 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
440 me
.name
, source_p
->name
, "admin");
444 for (i
= 0; i
< num_mods
; i
++)
448 if(match(parv
[1], modlist
[i
]->name
))
450 sendto_one(source_p
, form_str(RPL_MODLIST
),
451 me
.name
, source_p
->name
,
453 (unsigned long)(uintptr_t)modlist
[i
]->address
,
454 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
459 sendto_one(source_p
, form_str(RPL_MODLIST
),
460 me
.name
, source_p
->name
, modlist
[i
]->name
,
461 (unsigned long)(uintptr_t)modlist
[i
]->address
,
463 modlist
[i
]->core
? "(core)" : "");
467 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
471 /* unload and reload all modules */
473 mo_modrestart(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
477 if(!IsOperAdmin(source_p
))
479 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
480 me
.name
, source_p
->name
, "admin");
484 sendto_one_notice(source_p
, ":Reloading all modules");
488 unload_one_module(modlist
[0]->name
, 0);
491 load_core_modules(0);
494 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
495 "Module Restart: %d modules unloaded, %d modules loaded",
497 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
504 #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */
511 #ifdef CHARYBDIS_PROFILE
512 # ifndef RTLD_PROFILE
513 # warning libdl may not support profiling, sucks. :(
514 # define RTLD_PROFILE 0
518 static void increase_modlist(void);
520 #define MODS_INCREMENT 10
522 static char unknown_ver
[] = "<unknown>";
524 /* unload_one_module()
526 * inputs - name of module to unload
527 * - 1 to say modules unloaded, 0 to not
528 * output - 0 if successful, -1 if error
529 * side effects - module is unloaded
532 unload_one_module(const char *name
, int warn
)
536 if((modindex
= findmodule_byname(name
)) == -1)
540 ** XXX - The type system in C does not allow direct conversion between
541 ** data and function pointers, but as it happens, most C compilers will
542 ** safely do this, however it is a theoretical overlow to cast as we
543 ** must do here. I have library functions to take care of this, but
544 ** despite being more "correct" for the C language, this is more
545 ** practical. Removing the abuse of the ability to cast ANY pointer
546 ** to and from an integer value here will break some compilers.
549 /* Left the comment in but the code isn't here any more -larne */
550 switch (modlist
[modindex
]->mapi_version
)
554 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
555 if(mheader
->mapi_command_list
)
558 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
562 /* hook events are never removed, we simply lose the
563 * ability to call them --fl
565 if(mheader
->mapi_hfn_list
)
567 mapi_hfn_list_av1
*m
;
568 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
569 remove_hook(m
->hapi_name
, m
->fn
);
572 if(mheader
->mapi_unregister
)
573 mheader
->mapi_unregister();
577 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
578 "Unknown/unsupported MAPI version %d when unloading %s!",
579 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
580 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
581 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
585 lt_dlclose(modlist
[modindex
]->address
);
587 rb_free(modlist
[modindex
]->name
);
588 memmove(&modlist
[modindex
], &modlist
[modindex
+ 1],
589 sizeof(struct module *) * ((num_mods
- 1) - modindex
));
596 ilog(L_MAIN
, "Module %s unloaded", name
);
597 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
607 * inputs - path name of module, int to notice, int of core
608 * output - -1 if error 0 if success
609 * side effects - loads a module if successful
612 load_a_module(const char *path
, int warn
, int core
)
620 mod_basename
= rb_basename(path
);
622 tmpptr
= lt_dlopenext(path
);
626 const char *err
= lt_dlerror();
628 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
629 "Error loading module %s: %s", mod_basename
, err
);
630 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
631 rb_free(mod_basename
);
637 * _mheader is actually a struct mapi_mheader_*, but mapi_version
638 * is always the first member of this structure, so we treate it
639 * as a single int in order to determine the API version.
642 mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "_mheader");
643 if((mapi_version
== NULL
644 && (mapi_version
= (int *) (uintptr_t) lt_dlsym(tmpptr
, "__mheader")) == NULL
)
645 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
647 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
648 "Data format error: module %s has no MAPI header.",
650 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
651 (void) lt_dlclose(tmpptr
);
652 rb_free(mod_basename
);
656 switch (MAPI_VERSION(*mapi_version
))
660 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*)(void *)mapi_version
; /* see above */
661 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
663 ilog(L_MAIN
, "Module %s indicated failure during load.",
665 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
666 "Module %s indicated failure during load.",
669 rb_free(mod_basename
);
672 if(mheader
->mapi_command_list
)
675 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
679 if(mheader
->mapi_hook_list
)
682 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
683 *m
->hapi_id
= register_hook(m
->hapi_name
);
686 if(mheader
->mapi_hfn_list
)
688 mapi_hfn_list_av1
*m
;
689 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
690 add_hook(m
->hapi_name
, m
->fn
);
693 ver
= mheader
->mapi_module_version
;
698 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
699 mod_basename
, MAPI_VERSION(*mapi_version
));
700 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
701 "Module %s has unknown/unsupported MAPI version %d.",
702 mod_basename
, *mapi_version
);
704 rb_free(mod_basename
);
713 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
714 modlist
[num_mods
]->address
= tmpptr
;
715 modlist
[num_mods
]->version
= ver
;
716 modlist
[num_mods
]->core
= core
;
717 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
718 modlist
[num_mods
]->mapi_header
= mapi_version
;
719 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
724 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
725 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
726 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
727 (unsigned long) tmpptr
);
728 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
729 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
731 rb_free(mod_basename
);
740 * side effects - expand the size of modlist if necessary
743 increase_modlist(void)
745 struct module **new_modlist
= NULL
;
747 if((num_mods
+ 1) < max_mods
)
750 new_modlist
= (struct module **) rb_malloc(sizeof(struct module *) *
751 (max_mods
+ MODS_INCREMENT
));
752 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module *) * num_mods
);
755 modlist
= new_modlist
;
756 max_mods
+= MODS_INCREMENT
;