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 * I have moved the dl* function definitions and
46 * the two functions (load_a_module / unload_a_module) to the
48 * And also made the necessary changes to those functions
49 * to comply with shl_load and friends.
50 * In this file, to keep consistency with the makefile,
51 * I added the ability to load *.sl files, too.
55 #ifndef STATIC_MODULES
57 struct module **modlist
= NULL
;
59 static const char *core_module_table
[] = {
75 #define MODS_INCREMENT 10
77 int max_mods
= MODS_INCREMENT
;
79 static rb_dlink_list mod_paths
;
81 static int mo_modload(struct Client
*, struct Client
*, int, const char **);
82 static int mo_modlist(struct Client
*, struct Client
*, int, const char **);
83 static int mo_modreload(struct Client
*, struct Client
*, int, const char **);
84 static int mo_modunload(struct Client
*, struct Client
*, int, const char **);
85 static int mo_modrestart(struct Client
*, struct Client
*, int, const char **);
87 struct Message modload_msgtab
= {
88 "MODLOAD", 0, 0, 0, MFLG_SLOW
,
89 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modload
, 2}}
92 struct Message modunload_msgtab
= {
93 "MODUNLOAD", 0, 0, 0, MFLG_SLOW
,
94 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modunload
, 2}}
97 struct Message modreload_msgtab
= {
98 "MODRELOAD", 0, 0, 0, MFLG_SLOW
,
99 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modreload
, 2}}
102 struct Message modlist_msgtab
= {
103 "MODLIST", 0, 0, 0, MFLG_SLOW
,
104 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modlist
, 0}}
107 struct Message modrestart_msgtab
= {
108 "MODRESTART", 0, 0, 0, MFLG_SLOW
,
109 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modrestart
, 0}}
115 mod_add_cmd(&modload_msgtab
);
116 mod_add_cmd(&modunload_msgtab
);
117 mod_add_cmd(&modreload_msgtab
);
118 mod_add_cmd(&modlist_msgtab
);
119 mod_add_cmd(&modrestart_msgtab
);
121 /* Add the default paths we look in to the module system --nenolod */
122 mod_add_path(MODPATH
);
123 mod_add_path(AUTOMODPATH
);
130 * side effects - returns a module path from path
132 static struct module_path
*
133 mod_find_path(const char *path
)
136 struct module_path
*mpath
;
138 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
142 if(!strcmp(path
, mpath
->path
))
153 * side effects - adds path to list
156 mod_add_path(const char *path
)
158 struct module_path
*pathst
;
160 if(mod_find_path(path
))
163 pathst
= rb_malloc(sizeof(struct module_path
));
165 strcpy(pathst
->path
, path
);
166 rb_dlinkAddAlloc(pathst
, &mod_paths
);
173 * side effects - clear the lists of paths
176 mod_clear_paths(void)
178 rb_dlink_node
*ptr
, *next_ptr
;
180 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
183 rb_free_rb_dlink_node(ptr
);
186 mod_paths
.head
= mod_paths
.tail
= NULL
;
187 mod_paths
.length
= 0;
198 findmodule_byname(const char *name
)
202 for (i
= 0; i
< num_mods
; i
++)
204 if(!irccmp(modlist
[i
]->name
, name
))
210 /* load_all_modules()
217 load_all_modules(int warn
)
219 DIR *system_module_dir
= NULL
;
220 struct dirent
*ldirent
= NULL
;
221 char module_fq_name
[PATH_MAX
+ 1];
226 modlist
= (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT
));
228 max_mods
= MODS_INCREMENT
;
230 system_module_dir
= opendir(AUTOMODPATH
);
232 if(system_module_dir
== NULL
)
234 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
238 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
240 len
= strlen(ldirent
->d_name
);
241 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, SHARED_SUFFIX
))
243 (void) rb_snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
244 (void) load_a_module(module_fq_name
, warn
, 0);
248 (void) closedir(system_module_dir
);
251 /* load_core_modules()
255 * side effects - core modules are loaded, if any fail, kill ircd
258 load_core_modules(int warn
)
260 char module_name
[MAXPATHLEN
];
264 for (i
= 0; core_module_table
[i
]; i
++)
266 rb_snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
267 core_module_table
[i
], SHARED_SUFFIX
);
269 if(load_a_module(module_name
, warn
, 1) == -1)
272 "Error loading core module %s%s: terminating ircd",
273 core_module_table
[i
], SHARED_SUFFIX
);
286 load_one_module(const char *path
, int coremodule
)
288 char modpath
[MAXPATHLEN
];
289 rb_dlink_node
*pathst
;
290 struct module_path
*mpath
;
294 if (server_state_foreground
== 1)
295 inotice("loading module %s ...", path
);
297 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
299 mpath
= pathst
->data
;
301 rb_snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
->path
, path
);
302 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
304 if(stat(modpath
, &statbuf
) == 0)
306 if(S_ISREG(statbuf
.st_mode
))
308 /* Regular files only please */
310 return load_a_module(modpath
, 1, 1);
312 return load_a_module(modpath
, 1, 0);
319 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
324 /* load a module .. */
326 mo_modload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
330 if(!IsOperAdmin(source_p
))
332 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
333 me
.name
, source_p
->name
, "admin");
337 m_bn
= rb_basename(parv
[1]);
339 if(findmodule_byname(m_bn
) != -1)
341 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
346 load_one_module(parv
[1], 0);
354 /* unload a module .. */
356 mo_modunload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
361 if(!IsOperAdmin(source_p
))
363 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
364 me
.name
, source_p
->name
, "admin");
368 m_bn
= rb_basename(parv
[1]);
370 if((modindex
= findmodule_byname(m_bn
)) == -1)
372 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
377 if(modlist
[modindex
]->core
== 1)
379 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
384 if(unload_one_module(m_bn
, 1) == -1)
386 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
393 /* unload and load in one! */
395 mo_modreload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
401 if(!IsOperAdmin(source_p
))
403 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
404 me
.name
, source_p
->name
, "admin");
408 m_bn
= rb_basename(parv
[1]);
410 if((modindex
= findmodule_byname(m_bn
)) == -1)
412 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
417 check_core
= modlist
[modindex
]->core
;
419 if(unload_one_module(m_bn
, 1) == -1)
421 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
426 if((load_one_module(parv
[1], check_core
) == -1) && check_core
)
428 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
429 "Error reloading core module: %s: terminating ircd", parv
[1]);
430 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", parv
[1]);
438 /* list modules .. */
440 mo_modlist(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
444 if(!IsOperAdmin(source_p
))
446 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
447 me
.name
, source_p
->name
, "admin");
451 for (i
= 0; i
< num_mods
; i
++)
455 if(match(parv
[1], modlist
[i
]->name
))
457 sendto_one(source_p
, form_str(RPL_MODLIST
),
458 me
.name
, source_p
->name
,
461 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
466 sendto_one(source_p
, form_str(RPL_MODLIST
),
467 me
.name
, source_p
->name
, modlist
[i
]->name
,
468 modlist
[i
]->address
, modlist
[i
]->version
,
469 modlist
[i
]->core
? "(core)" : "");
473 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
477 /* unload and reload all modules */
479 mo_modrestart(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
483 if(!IsOperAdmin(source_p
))
485 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
486 me
.name
, source_p
->name
, "admin");
490 sendto_one_notice(source_p
, ":Reloading all modules");
494 unload_one_module(modlist
[0]->name
, 0);
497 load_core_modules(0);
500 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
501 "Module Restart: %d modules unloaded, %d modules loaded",
503 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
510 #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */
517 #ifdef CHARYBDIS_PROFILE
518 # ifndef RTLD_PROFILE
519 # warning libdl may not support profiling, sucks. :(
520 # define RTLD_PROFILE 0
524 static void increase_modlist(void);
526 #define MODS_INCREMENT 10
528 static char unknown_ver
[] = "<unknown>";
530 /* This file contains the core functions to use dynamic libraries.
535 #ifdef HAVE_MACH_O_DYLD_H
537 ** jmallett's dl*(3) shims for NSModule(3) systems.
539 #include <mach-o/dyld.h>
543 #define RTLD_LAZY 2185 /* built-in dl*(3) don't care */
546 void undefinedErrorHandler(const char *);
547 NSModule
multipleErrorHandler(NSSymbol
, NSModule
, NSModule
);
548 void linkEditErrorHandler(NSLinkEditErrors
, int, const char *, const char *);
550 void *dlopen(char *, int);
552 void *dlsym(void *, char *);
554 static int firstLoad
= TRUE
;
555 static int myDlError
;
556 static char *myErrorTable
[] = { "Loading file as object failed\n",
557 "Loading file as object succeeded\n",
558 "Not a valid shared object\n",
559 "Architecture of object invalid on this architecture\n",
560 "Invalid or corrupt image\n",
561 "Could not access object\n",
562 "NSCreateObjectFileImageFromFile failed\n",
567 undefinedErrorHandler(const char *symbolName
)
569 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Undefined symbol: %s", symbolName
);
570 ilog(L_MAIN
, "Undefined symbol: %s", symbolName
);
575 multipleErrorHandler(NSSymbol s
, NSModule old
, NSModule
new)
578 ** This results in substantial leaking of memory... Should free one
581 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
582 "Symbol `%s' found in `%s' and `%s'",
583 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
584 ilog(L_MAIN
, "Symbol `%s' found in `%s' and `%s'",
585 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
586 /* We return which module should be considered valid, I believe */
591 linkEditErrorHandler(NSLinkEditErrors errorClass
, int errnum
,
592 const char *fileName
, const char *errorString
)
594 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
595 "Link editor error: %s for %s", errorString
, fileName
);
596 ilog(L_MAIN
, "Link editor error: %s for %s", errorString
, fileName
);
603 return myDlError
== NSObjectFileImageSuccess
? NULL
: myErrorTable
[myDlError
% 7];
607 dlopen(char *filename
, int unused
)
609 NSObjectFileImage myImage
;
615 ** If we are loading our first symbol (huzzah!) we should go ahead
616 ** and install link editor error handling!
618 NSLinkEditErrorHandlers linkEditorErrorHandlers
;
620 linkEditorErrorHandlers
.undefined
= undefinedErrorHandler
;
621 linkEditorErrorHandlers
.multiple
= multipleErrorHandler
;
622 linkEditorErrorHandlers
.linkEdit
= linkEditErrorHandler
;
623 NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers
);
626 myDlError
= NSCreateObjectFileImageFromFile(filename
, &myImage
);
627 if(myDlError
!= NSObjectFileImageSuccess
)
631 myModule
= NSLinkModule(myImage
, filename
, NSLINKMODULE_OPTION_PRIVATE
);
632 return (void *) myModule
;
636 dlclose(void *myModule
)
638 NSUnLinkModule(myModule
, FALSE
);
643 dlsym(void *myModule
, char *mySymbolName
)
647 mySymbol
= NSLookupSymbolInModule((NSModule
) myModule
, mySymbolName
);
648 return NSAddressOfSymbol(mySymbol
);
655 * HPUX dl compat functions
657 #if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN)
658 #define RTLD_LAZY BIND_DEFERRED
659 #define RTLD_GLOBAL DYNAMIC_PATH
660 #define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0)
661 #define dlclose(handle) shl_unload((shl_t)(handle))
662 #define dlsym(handle,name) hpux_dlsym(handle,name)
663 #define dlerror() strerror(errno)
666 hpux_dlsym(void *handle
, char *name
)
669 if(!shl_findsym((shl_t
*) & handle
, name
, TYPE_UNDEFINED
, &sym_addr
))
676 /* unload_one_module()
678 * inputs - name of module to unload
679 * - 1 to say modules unloaded, 0 to not
680 * output - 0 if successful, -1 if error
681 * side effects - module is unloaded
684 unload_one_module(const char *name
, int warn
)
688 if((modindex
= findmodule_byname(name
)) == -1)
692 ** XXX - The type system in C does not allow direct conversion between
693 ** data and function pointers, but as it happens, most C compilers will
694 ** safely do this, however it is a theoretical overlow to cast as we
695 ** must do here. I have library functions to take care of this, but
696 ** despite being more "correct" for the C language, this is more
697 ** practical. Removing the abuse of the ability to cast ANY pointer
698 ** to and from an integer value here will break some compilers.
701 /* Left the comment in but the code isn't here any more -larne */
702 switch (modlist
[modindex
]->mapi_version
)
706 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
707 if(mheader
->mapi_command_list
)
710 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
714 /* hook events are never removed, we simply lose the
715 * ability to call them --fl
717 if(mheader
->mapi_hfn_list
)
719 mapi_hfn_list_av1
*m
;
720 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
721 remove_hook(m
->hapi_name
, m
->fn
);
724 if(mheader
->mapi_unregister
)
725 mheader
->mapi_unregister();
729 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
730 "Unknown/unsupported MAPI version %d when unloading %s!",
731 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
732 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
733 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
737 dlclose(modlist
[modindex
]->address
);
739 rb_free(modlist
[modindex
]->name
);
740 memcpy(&modlist
[modindex
], &modlist
[modindex
+ 1],
741 sizeof(struct module) * ((num_mods
- 1) - modindex
));
748 ilog(L_MAIN
, "Module %s unloaded", name
);
749 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
759 * inputs - path name of module, int to notice, int of core
760 * output - -1 if error 0 if success
761 * side effects - loads a module if successful
764 load_a_module(const char *path
, int warn
, int core
)
773 mod_basename
= rb_basename(path
);
775 #ifdef CHARYBDIS_PROFILE
776 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
| RTLD_PROFILE
);
778 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_LOCAL
);
783 const char *err
= dlerror();
785 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
786 "Error loading module %s: %s", mod_basename
, err
);
787 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
788 rb_free(mod_basename
);
794 * _mheader is actually a struct mapi_mheader_*, but mapi_version
795 * is always the first member of this structure, so we treate it
796 * as a single int in order to determine the API version.
799 mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "_mheader");
800 if((mapi_version
== NULL
801 && (mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "__mheader")) == NULL
)
802 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
804 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
805 "Data format error: module %s has no MAPI header.",
807 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
808 (void) dlclose(tmpptr
);
809 rb_free(mod_basename
);
813 switch (MAPI_VERSION(*mapi_version
))
817 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*) mapi_version
; /* see above */
818 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
820 ilog(L_MAIN
, "Module %s indicated failure during load.",
822 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
823 "Module %s indicated failure during load.",
826 rb_free(mod_basename
);
829 if(mheader
->mapi_command_list
)
832 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
836 if(mheader
->mapi_hook_list
)
839 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
840 *m
->hapi_id
= register_hook(m
->hapi_name
);
843 if(mheader
->mapi_hfn_list
)
845 mapi_hfn_list_av1
*m
;
846 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
847 add_hook(m
->hapi_name
, m
->fn
);
850 ver
= mheader
->mapi_module_version
;
855 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
856 mod_basename
, MAPI_VERSION(*mapi_version
));
857 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
858 "Module %s has unknown/unsupported MAPI version %d.",
859 mod_basename
, *mapi_version
);
861 rb_free(mod_basename
);
870 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
871 modlist
[num_mods
]->address
= tmpptr
;
872 modlist
[num_mods
]->version
= ver
;
873 modlist
[num_mods
]->core
= core
;
874 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
875 modlist
[num_mods
]->mapi_header
= mapi_version
;
876 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
881 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
882 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
883 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
884 (unsigned long) tmpptr
);
885 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
886 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
888 rb_free(mod_basename
);
897 * side effects - expand the size of modlist if necessary
900 increase_modlist(void)
902 struct module **new_modlist
= NULL
;
904 if((num_mods
+ 1) < max_mods
)
907 new_modlist
= (struct module **) rb_malloc(sizeof(struct module) *
908 (max_mods
+ MODS_INCREMENT
));
909 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module) * num_mods
);
912 modlist
= new_modlist
;
913 max_mods
+= MODS_INCREMENT
;
916 #else /* STATIC_MODULES */
918 /* load_all_modules()
922 * side effects - all the msgtabs are added for static modules
925 load_all_modules(int warn
)
927 load_static_modules();
930 #endif /* STATIC_MODULES */