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"
43 #ifndef STATIC_MODULES
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}}
104 mod_add_cmd(&modload_msgtab
);
105 mod_add_cmd(&modunload_msgtab
);
106 mod_add_cmd(&modreload_msgtab
);
107 mod_add_cmd(&modlist_msgtab
);
108 mod_add_cmd(&modrestart_msgtab
);
110 /* Add the default paths we look in to the module system --nenolod */
111 mod_add_path(MODPATH
);
112 mod_add_path(AUTOMODPATH
);
119 * side effects - returns a module path from path
122 mod_find_path(const char *path
)
127 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
131 if(!strcmp(path
, mpath
))
142 * side effects - adds path to list
145 mod_add_path(const char *path
)
149 if(mod_find_path(path
))
152 pathst
= rb_strdup(path
);
153 rb_dlinkAddAlloc(pathst
, &mod_paths
);
160 * side effects - clear the lists of paths
163 mod_clear_paths(void)
165 rb_dlink_node
*ptr
, *next_ptr
;
167 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
170 rb_free_rb_dlink_node(ptr
);
173 mod_paths
.head
= mod_paths
.tail
= NULL
;
174 mod_paths
.length
= 0;
185 findmodule_byname(const char *name
)
189 for (i
= 0; i
< num_mods
; i
++)
191 if(!irccmp(modlist
[i
]->name
, name
))
197 /* load_all_modules()
204 load_all_modules(int warn
)
206 DIR *system_module_dir
= NULL
;
207 struct dirent
*ldirent
= NULL
;
208 char module_fq_name
[PATH_MAX
+ 1];
213 modlist
= (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT
));
215 max_mods
= MODS_INCREMENT
;
217 system_module_dir
= opendir(AUTOMODPATH
);
219 if(system_module_dir
== NULL
)
221 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
225 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
227 len
= strlen(ldirent
->d_name
);
228 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, SHARED_SUFFIX
))
230 (void) rb_snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
231 (void) load_a_module(module_fq_name
, warn
, 0);
235 (void) closedir(system_module_dir
);
238 /* load_core_modules()
242 * side effects - core modules are loaded, if any fail, kill ircd
245 load_core_modules(int warn
)
247 char module_name
[PATH_MAX
];
251 for (i
= 0; core_module_table
[i
]; i
++)
253 rb_snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
254 core_module_table
[i
], SHARED_SUFFIX
);
256 if(load_a_module(module_name
, warn
, 1) == -1)
259 "Error loading core module %s%s: terminating ircd",
260 core_module_table
[i
], SHARED_SUFFIX
);
273 load_one_module(const char *path
, int coremodule
)
275 char modpath
[PATH_MAX
];
276 rb_dlink_node
*pathst
;
281 if (server_state_foreground
== 1)
282 inotice("loading module %s ...", path
);
284 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
286 mpath
= pathst
->data
;
288 rb_snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
, path
);
289 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
291 if(stat(modpath
, &statbuf
) == 0)
293 if(S_ISREG(statbuf
.st_mode
))
295 /* Regular files only please */
297 return load_a_module(modpath
, 1, 1);
299 return load_a_module(modpath
, 1, 0);
306 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
311 /* load a module .. */
313 mo_modload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
317 if(!IsOperAdmin(source_p
))
319 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
320 me
.name
, source_p
->name
, "admin");
324 m_bn
= rb_basename(parv
[1]);
326 if(findmodule_byname(m_bn
) != -1)
328 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
333 load_one_module(parv
[1], 0);
341 /* unload a module .. */
343 mo_modunload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
348 if(!IsOperAdmin(source_p
))
350 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
351 me
.name
, source_p
->name
, "admin");
355 m_bn
= rb_basename(parv
[1]);
357 if((modindex
= findmodule_byname(m_bn
)) == -1)
359 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
364 if(modlist
[modindex
]->core
== 1)
366 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
371 if(unload_one_module(m_bn
, 1) == -1)
373 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
380 /* unload and load in one! */
382 mo_modreload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
388 if(!IsOperAdmin(source_p
))
390 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
391 me
.name
, source_p
->name
, "admin");
395 m_bn
= rb_basename(parv
[1]);
397 if((modindex
= findmodule_byname(m_bn
)) == -1)
399 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
404 check_core
= modlist
[modindex
]->core
;
406 if(unload_one_module(m_bn
, 1) == -1)
408 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
413 if((load_one_module(m_bn
, check_core
) == -1) && check_core
)
415 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
416 "Error reloading core module: %s: terminating ircd", m_bn
);
417 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", m_bn
);
425 /* list modules .. */
427 mo_modlist(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
431 if(!IsOperAdmin(source_p
))
433 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
434 me
.name
, source_p
->name
, "admin");
438 for (i
= 0; i
< num_mods
; i
++)
442 if(match(parv
[1], modlist
[i
]->name
))
444 sendto_one(source_p
, form_str(RPL_MODLIST
),
445 me
.name
, source_p
->name
,
447 (unsigned long)(uintptr_t)modlist
[i
]->address
,
448 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
453 sendto_one(source_p
, form_str(RPL_MODLIST
),
454 me
.name
, source_p
->name
, modlist
[i
]->name
,
455 (unsigned long)(uintptr_t)modlist
[i
]->address
,
457 modlist
[i
]->core
? "(core)" : "");
461 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
465 /* unload and reload all modules */
467 mo_modrestart(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
471 if(!IsOperAdmin(source_p
))
473 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
474 me
.name
, source_p
->name
, "admin");
478 sendto_one_notice(source_p
, ":Reloading all modules");
482 unload_one_module(modlist
[0]->name
, 0);
485 load_core_modules(0);
488 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
489 "Module Restart: %d modules unloaded, %d modules loaded",
491 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
498 #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */
505 #ifdef CHARYBDIS_PROFILE
506 # ifndef RTLD_PROFILE
507 # warning libdl may not support profiling, sucks. :(
508 # define RTLD_PROFILE 0
512 static void increase_modlist(void);
514 #define MODS_INCREMENT 10
516 static char unknown_ver
[] = "<unknown>";
518 /* This file contains the core functions to use dynamic libraries.
523 #ifdef HAVE_MACH_O_DYLD_H
525 ** jmallett's dl*(3) shims for NSModule(3) systems.
527 #include <mach-o/dyld.h>
531 #define RTLD_LAZY 2185 /* built-in dl*(3) don't care */
534 void undefinedErrorHandler(const char *);
535 NSModule
multipleErrorHandler(NSSymbol
, NSModule
, NSModule
);
536 void linkEditErrorHandler(NSLinkEditErrors
, int, const char *, const char *);
538 void *dlopen(char *, int);
540 void *dlsym(void *, char *);
542 static int firstLoad
= TRUE
;
543 static int myDlError
;
544 static char *myErrorTable
[] = { "Loading file as object failed\n",
545 "Loading file as object succeeded\n",
546 "Not a valid shared object\n",
547 "Architecture of object invalid on this architecture\n",
548 "Invalid or corrupt image\n",
549 "Could not access object\n",
550 "NSCreateObjectFileImageFromFile failed\n",
555 undefinedErrorHandler(const char *symbolName
)
557 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Undefined symbol: %s", symbolName
);
558 ilog(L_MAIN
, "Undefined symbol: %s", symbolName
);
563 multipleErrorHandler(NSSymbol s
, NSModule old
, NSModule
new)
566 ** This results in substantial leaking of memory... Should free one
569 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
570 "Symbol `%s' found in `%s' and `%s'",
571 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
572 ilog(L_MAIN
, "Symbol `%s' found in `%s' and `%s'",
573 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
574 /* We return which module should be considered valid, I believe */
579 linkEditErrorHandler(NSLinkEditErrors errorClass
, int errnum
,
580 const char *fileName
, const char *errorString
)
582 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
583 "Link editor error: %s for %s", errorString
, fileName
);
584 ilog(L_MAIN
, "Link editor error: %s for %s", errorString
, fileName
);
591 return myDlError
== NSObjectFileImageSuccess
? NULL
: myErrorTable
[myDlError
% 7];
595 dlopen(char *filename
, int unused
)
597 NSObjectFileImage myImage
;
603 ** If we are loading our first symbol (huzzah!) we should go ahead
604 ** and install link editor error handling!
606 NSLinkEditErrorHandlers linkEditorErrorHandlers
;
608 linkEditorErrorHandlers
.undefined
= undefinedErrorHandler
;
609 linkEditorErrorHandlers
.multiple
= multipleErrorHandler
;
610 linkEditorErrorHandlers
.linkEdit
= linkEditErrorHandler
;
611 NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers
);
614 myDlError
= NSCreateObjectFileImageFromFile(filename
, &myImage
);
615 if(myDlError
!= NSObjectFileImageSuccess
)
619 myModule
= NSLinkModule(myImage
, filename
, NSLINKMODULE_OPTION_PRIVATE
);
620 return (void *) myModule
;
624 dlclose(void *myModule
)
626 NSUnLinkModule(myModule
, FALSE
);
631 dlsym(void *myModule
, char *mySymbolName
)
635 mySymbol
= NSLookupSymbolInModule((NSModule
) myModule
, mySymbolName
);
636 return NSAddressOfSymbol(mySymbol
);
643 * HPUX dl compat functions
645 #if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN)
646 #define RTLD_LAZY BIND_DEFERRED
647 #define RTLD_GLOBAL DYNAMIC_PATH
648 #define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0)
649 #define dlclose(handle) shl_unload((shl_t)(handle))
650 #define dlsym(handle,name) hpux_dlsym(handle,name)
651 #define dlerror() strerror(errno)
654 hpux_dlsym(void *handle
, char *name
)
657 if(!shl_findsym((shl_t
*) & handle
, name
, TYPE_UNDEFINED
, &sym_addr
))
664 /* unload_one_module()
666 * inputs - name of module to unload
667 * - 1 to say modules unloaded, 0 to not
668 * output - 0 if successful, -1 if error
669 * side effects - module is unloaded
672 unload_one_module(const char *name
, int warn
)
676 if((modindex
= findmodule_byname(name
)) == -1)
680 ** XXX - The type system in C does not allow direct conversion between
681 ** data and function pointers, but as it happens, most C compilers will
682 ** safely do this, however it is a theoretical overlow to cast as we
683 ** must do here. I have library functions to take care of this, but
684 ** despite being more "correct" for the C language, this is more
685 ** practical. Removing the abuse of the ability to cast ANY pointer
686 ** to and from an integer value here will break some compilers.
689 /* Left the comment in but the code isn't here any more -larne */
690 switch (modlist
[modindex
]->mapi_version
)
694 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
695 if(mheader
->mapi_command_list
)
698 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
702 /* hook events are never removed, we simply lose the
703 * ability to call them --fl
705 if(mheader
->mapi_hfn_list
)
707 mapi_hfn_list_av1
*m
;
708 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
709 remove_hook(m
->hapi_name
, m
->fn
);
712 if(mheader
->mapi_unregister
)
713 mheader
->mapi_unregister();
717 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
718 "Unknown/unsupported MAPI version %d when unloading %s!",
719 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
720 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
721 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
725 dlclose(modlist
[modindex
]->address
);
727 rb_free(modlist
[modindex
]->name
);
728 memmove(&modlist
[modindex
], &modlist
[modindex
+ 1],
729 sizeof(struct module *) * ((num_mods
- 1) - modindex
));
736 ilog(L_MAIN
, "Module %s unloaded", name
);
737 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
747 * inputs - path name of module, int to notice, int of core
748 * output - -1 if error 0 if success
749 * side effects - loads a module if successful
752 load_a_module(const char *path
, int warn
, int core
)
761 mod_basename
= rb_basename(path
);
763 #ifdef CHARYBDIS_PROFILE
764 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
| RTLD_PROFILE
);
766 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
);
771 const char *err
= dlerror();
773 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
774 "Error loading module %s: %s", mod_basename
, err
);
775 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
776 rb_free(mod_basename
);
782 * _mheader is actually a struct mapi_mheader_*, but mapi_version
783 * is always the first member of this structure, so we treate it
784 * as a single int in order to determine the API version.
787 mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "_mheader");
788 if((mapi_version
== NULL
789 && (mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "__mheader")) == NULL
)
790 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
792 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
793 "Data format error: module %s has no MAPI header.",
795 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
796 (void) dlclose(tmpptr
);
797 rb_free(mod_basename
);
801 switch (MAPI_VERSION(*mapi_version
))
805 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*) mapi_version
; /* see above */
806 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
808 ilog(L_MAIN
, "Module %s indicated failure during load.",
810 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
811 "Module %s indicated failure during load.",
814 rb_free(mod_basename
);
817 if(mheader
->mapi_command_list
)
820 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
824 if(mheader
->mapi_hook_list
)
827 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
828 *m
->hapi_id
= register_hook(m
->hapi_name
);
831 if(mheader
->mapi_hfn_list
)
833 mapi_hfn_list_av1
*m
;
834 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
835 add_hook(m
->hapi_name
, m
->fn
);
838 ver
= mheader
->mapi_module_version
;
843 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
844 mod_basename
, MAPI_VERSION(*mapi_version
));
845 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
846 "Module %s has unknown/unsupported MAPI version %d.",
847 mod_basename
, *mapi_version
);
849 rb_free(mod_basename
);
858 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
859 modlist
[num_mods
]->address
= tmpptr
;
860 modlist
[num_mods
]->version
= ver
;
861 modlist
[num_mods
]->core
= core
;
862 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
863 modlist
[num_mods
]->mapi_header
= mapi_version
;
864 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
869 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
870 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
871 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
872 (unsigned long) tmpptr
);
873 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
874 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
876 rb_free(mod_basename
);
885 * side effects - expand the size of modlist if necessary
888 increase_modlist(void)
890 struct module **new_modlist
= NULL
;
892 if((num_mods
+ 1) < max_mods
)
895 new_modlist
= (struct module **) rb_malloc(sizeof(struct module *) *
896 (max_mods
+ MODS_INCREMENT
));
897 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module *) * num_mods
);
900 modlist
= new_modlist
;
901 max_mods
+= MODS_INCREMENT
;
904 #else /* STATIC_MODULES */
906 /* load_all_modules()
910 * side effects - all the msgtabs are added for static modules
913 load_all_modules(int warn
)
915 load_static_modules();
918 #endif /* STATIC_MODULES */