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}}
112 extern struct Message error_msgtab
;
117 mod_add_cmd(&modload_msgtab
);
118 mod_add_cmd(&modunload_msgtab
);
119 mod_add_cmd(&modreload_msgtab
);
120 mod_add_cmd(&modlist_msgtab
);
121 mod_add_cmd(&modrestart_msgtab
);
123 /* Add the default paths we look in to the module system --nenolod */
124 mod_add_path(MODPATH
);
125 mod_add_path(AUTOMODPATH
);
132 * side effects - returns a module path from path
134 static struct module_path
*
135 mod_find_path(const char *path
)
138 struct module_path
*mpath
;
140 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
144 if(!strcmp(path
, mpath
->path
))
155 * side effects - adds path to list
158 mod_add_path(const char *path
)
160 struct module_path
*pathst
;
162 if(mod_find_path(path
))
165 pathst
= rb_malloc(sizeof(struct module_path
));
167 strcpy(pathst
->path
, path
);
168 rb_dlinkAddAlloc(pathst
, &mod_paths
);
175 * side effects - clear the lists of paths
178 mod_clear_paths(void)
180 rb_dlink_node
*ptr
, *next_ptr
;
182 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
185 rb_free_rb_dlink_node(ptr
);
188 mod_paths
.head
= mod_paths
.tail
= NULL
;
189 mod_paths
.length
= 0;
199 irc_basename(const char *path
)
201 char *mod_basename
= rb_malloc(strlen(path
) + 1);
204 if(!(s
= strrchr(path
, '/')))
209 (void) strcpy(mod_basename
, s
);
221 findmodule_byname(const char *name
)
225 for (i
= 0; i
< num_mods
; i
++)
227 if(!irccmp(modlist
[i
]->name
, name
))
233 /* load_all_modules()
240 load_all_modules(int warn
)
242 DIR *system_module_dir
= NULL
;
243 struct dirent
*ldirent
= NULL
;
244 char module_fq_name
[PATH_MAX
+ 1];
249 modlist
= (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT
));
251 max_mods
= MODS_INCREMENT
;
253 system_module_dir
= opendir(AUTOMODPATH
);
255 if(system_module_dir
== NULL
)
257 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
261 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
263 len
= strlen(ldirent
->d_name
);
264 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, SHARED_SUFFIX
))
266 (void) rb_snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
267 (void) load_a_module(module_fq_name
, warn
, 0);
271 (void) closedir(system_module_dir
);
274 /* load_core_modules()
278 * side effects - core modules are loaded, if any fail, kill ircd
281 load_core_modules(int warn
)
283 char module_name
[MAXPATHLEN
];
287 for (i
= 0; core_module_table
[i
]; i
++)
289 rb_snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
290 core_module_table
[i
], SHARED_SUFFIX
);
292 if(load_a_module(module_name
, warn
, 1) == -1)
295 "Error loading core module %s%s: terminating ircd",
296 core_module_table
[i
], SHARED_SUFFIX
);
309 load_one_module(const char *path
, int coremodule
)
311 char modpath
[MAXPATHLEN
];
312 rb_dlink_node
*pathst
;
313 struct module_path
*mpath
;
317 if (server_state_foreground
== 1)
318 inotice("loading module %s ...", path
);
320 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
322 mpath
= pathst
->data
;
324 rb_snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
->path
, path
);
325 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
327 if(stat(modpath
, &statbuf
) == 0)
329 if(S_ISREG(statbuf
.st_mode
))
331 /* Regular files only please */
333 return load_a_module(modpath
, 1, 1);
335 return load_a_module(modpath
, 1, 0);
342 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
347 /* load a module .. */
349 mo_modload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
353 if(!IsOperAdmin(source_p
))
355 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
356 me
.name
, source_p
->name
, "admin");
360 m_bn
= irc_basename(parv
[1]);
362 if(findmodule_byname(m_bn
) != -1)
364 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
369 load_one_module(parv
[1], 0);
377 /* unload a module .. */
379 mo_modunload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
384 if(!IsOperAdmin(source_p
))
386 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
387 me
.name
, source_p
->name
, "admin");
391 m_bn
= irc_basename(parv
[1]);
393 if((modindex
= findmodule_byname(m_bn
)) == -1)
395 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
400 if(modlist
[modindex
]->core
== 1)
402 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
407 if(unload_one_module(m_bn
, 1) == -1)
409 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
416 /* unload and load in one! */
418 mo_modreload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
424 if(!IsOperAdmin(source_p
))
426 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
427 me
.name
, source_p
->name
, "admin");
431 m_bn
= irc_basename(parv
[1]);
433 if((modindex
= findmodule_byname(m_bn
)) == -1)
435 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
440 check_core
= modlist
[modindex
]->core
;
442 if(unload_one_module(m_bn
, 1) == -1)
444 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
449 if((load_one_module(parv
[1], check_core
) == -1) && check_core
)
451 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
452 "Error reloading core module: %s: terminating ircd", parv
[1]);
453 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", parv
[1]);
461 /* list modules .. */
463 mo_modlist(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
467 if(!IsOperAdmin(source_p
))
469 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
470 me
.name
, source_p
->name
, "admin");
474 for (i
= 0; i
< num_mods
; i
++)
478 if(match(parv
[1], modlist
[i
]->name
))
480 sendto_one(source_p
, form_str(RPL_MODLIST
),
481 me
.name
, source_p
->name
,
484 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
489 sendto_one(source_p
, form_str(RPL_MODLIST
),
490 me
.name
, source_p
->name
, modlist
[i
]->name
,
491 modlist
[i
]->address
, modlist
[i
]->version
,
492 modlist
[i
]->core
? "(core)" : "");
496 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
500 /* unload and reload all modules */
502 mo_modrestart(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
506 if(!IsOperAdmin(source_p
))
508 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
509 me
.name
, source_p
->name
, "admin");
513 sendto_one_notice(source_p
, ":Reloading all modules");
517 unload_one_module(modlist
[0]->name
, 0);
520 load_core_modules(0);
523 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
524 "Module Restart: %d modules unloaded, %d modules loaded",
526 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
533 #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */
536 #ifdef CHARYBDIS_PROFILE
537 # ifndef RTLD_PROFILE
538 # warning libdl may not support profiling, sucks. :(
539 # define RTLD_PROFILE 0
543 static void increase_modlist(void);
545 #define MODS_INCREMENT 10
547 static char unknown_ver
[] = "<unknown>";
549 /* This file contains the core functions to use dynamic libraries.
554 #ifdef HAVE_MACH_O_DYLD_H
556 ** jmallett's dl*(3) shims for NSModule(3) systems.
558 #include <mach-o/dyld.h>
562 #define RTLD_LAZY 2185 /* built-in dl*(3) don't care */
565 void undefinedErrorHandler(const char *);
566 NSModule
multipleErrorHandler(NSSymbol
, NSModule
, NSModule
);
567 void linkEditErrorHandler(NSLinkEditErrors
, int, const char *, const char *);
569 void *dlopen(char *, int);
571 void *dlsym(void *, char *);
573 static int firstLoad
= TRUE
;
574 static int myDlError
;
575 static char *myErrorTable
[] = { "Loading file as object failed\n",
576 "Loading file as object succeeded\n",
577 "Not a valid shared object\n",
578 "Architecture of object invalid on this architecture\n",
579 "Invalid or corrupt image\n",
580 "Could not access object\n",
581 "NSCreateObjectFileImageFromFile failed\n",
586 undefinedErrorHandler(const char *symbolName
)
588 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Undefined symbol: %s", symbolName
);
589 ilog(L_MAIN
, "Undefined symbol: %s", symbolName
);
594 multipleErrorHandler(NSSymbol s
, NSModule old
, NSModule
new)
597 ** This results in substantial leaking of memory... Should free one
600 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
601 "Symbol `%s' found in `%s' and `%s'",
602 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
603 ilog(L_MAIN
, "Symbol `%s' found in `%s' and `%s'",
604 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
605 /* We return which module should be considered valid, I believe */
610 linkEditErrorHandler(NSLinkEditErrors errorClass
, int errnum
,
611 const char *fileName
, const char *errorString
)
613 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
614 "Link editor error: %s for %s", errorString
, fileName
);
615 ilog(L_MAIN
, "Link editor error: %s for %s", errorString
, fileName
);
622 return myDlError
== NSObjectFileImageSuccess
? NULL
: myErrorTable
[myDlError
% 7];
626 dlopen(char *filename
, int unused
)
628 NSObjectFileImage myImage
;
634 ** If we are loading our first symbol (huzzah!) we should go ahead
635 ** and install link editor error handling!
637 NSLinkEditErrorHandlers linkEditorErrorHandlers
;
639 linkEditorErrorHandlers
.undefined
= undefinedErrorHandler
;
640 linkEditorErrorHandlers
.multiple
= multipleErrorHandler
;
641 linkEditorErrorHandlers
.linkEdit
= linkEditErrorHandler
;
642 NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers
);
645 myDlError
= NSCreateObjectFileImageFromFile(filename
, &myImage
);
646 if(myDlError
!= NSObjectFileImageSuccess
)
650 myModule
= NSLinkModule(myImage
, filename
, NSLINKMODULE_OPTION_PRIVATE
);
651 return (void *) myModule
;
655 dlclose(void *myModule
)
657 NSUnLinkModule(myModule
, FALSE
);
662 dlsym(void *myModule
, char *mySymbolName
)
666 mySymbol
= NSLookupSymbolInModule((NSModule
) myModule
, mySymbolName
);
667 return NSAddressOfSymbol(mySymbol
);
674 * HPUX dl compat functions
676 #if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN)
677 #define RTLD_LAZY BIND_DEFERRED
678 #define RTLD_GLOBAL DYNAMIC_PATH
679 #define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0)
680 #define dlclose(handle) shl_unload((shl_t)(handle))
681 #define dlsym(handle,name) hpux_dlsym(handle,name)
682 #define dlerror() strerror(errno)
685 hpux_dlsym(void *handle
, char *name
)
688 if(!shl_findsym((shl_t
*) & handle
, name
, TYPE_UNDEFINED
, &sym_addr
))
695 /* unload_one_module()
697 * inputs - name of module to unload
698 * - 1 to say modules unloaded, 0 to not
699 * output - 0 if successful, -1 if error
700 * side effects - module is unloaded
703 unload_one_module(const char *name
, int warn
)
707 if((modindex
= findmodule_byname(name
)) == -1)
711 ** XXX - The type system in C does not allow direct conversion between
712 ** data and function pointers, but as it happens, most C compilers will
713 ** safely do this, however it is a theoretical overlow to cast as we
714 ** must do here. I have library functions to take care of this, but
715 ** despite being more "correct" for the C language, this is more
716 ** practical. Removing the abuse of the ability to cast ANY pointer
717 ** to and from an integer value here will break some compilers.
720 /* Left the comment in but the code isn't here any more -larne */
721 switch (modlist
[modindex
]->mapi_version
)
725 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
726 if(mheader
->mapi_command_list
)
729 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
733 /* hook events are never removed, we simply lose the
734 * ability to call them --fl
736 if(mheader
->mapi_hfn_list
)
738 mapi_hfn_list_av1
*m
;
739 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
740 remove_hook(m
->hapi_name
, m
->fn
);
743 if(mheader
->mapi_unregister
)
744 mheader
->mapi_unregister();
748 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
749 "Unknown/unsupported MAPI version %d when unloading %s!",
750 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
751 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
752 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
756 dlclose(modlist
[modindex
]->address
);
758 rb_free(modlist
[modindex
]->name
);
759 memcpy(&modlist
[modindex
], &modlist
[modindex
+ 1],
760 sizeof(struct module) * ((num_mods
- 1) - modindex
));
767 ilog(L_MAIN
, "Module %s unloaded", name
);
768 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
778 * inputs - path name of module, int to notice, int of core
779 * output - -1 if error 0 if success
780 * side effects - loads a module if successful
783 load_a_module(const char *path
, int warn
, int core
)
792 mod_basename
= irc_basename(path
);
794 #ifdef CHARYBDIS_PROFILE
795 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_PROFILE
);
797 tmpptr
= dlopen(path
, RTLD_NOW
);
802 const char *err
= dlerror();
804 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
805 "Error loading module %s: %s", mod_basename
, err
);
806 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
807 rb_free(mod_basename
);
813 * _mheader is actually a struct mapi_mheader_*, but mapi_version
814 * is always the first member of this structure, so we treate it
815 * as a single int in order to determine the API version.
818 mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "_mheader");
819 if((mapi_version
== NULL
820 && (mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "__mheader")) == NULL
)
821 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
823 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
824 "Data format error: module %s has no MAPI header.",
826 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
827 (void) dlclose(tmpptr
);
828 rb_free(mod_basename
);
832 switch (MAPI_VERSION(*mapi_version
))
836 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*) mapi_version
; /* see above */
837 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
839 ilog(L_MAIN
, "Module %s indicated failure during load.",
841 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
842 "Module %s indicated failure during load.",
845 rb_free(mod_basename
);
848 if(mheader
->mapi_command_list
)
851 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
855 if(mheader
->mapi_hook_list
)
858 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
859 *m
->hapi_id
= register_hook(m
->hapi_name
);
862 if(mheader
->mapi_hfn_list
)
864 mapi_hfn_list_av1
*m
;
865 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
866 add_hook(m
->hapi_name
, m
->fn
);
869 ver
= mheader
->mapi_module_version
;
874 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
875 mod_basename
, MAPI_VERSION(*mapi_version
));
876 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
877 "Module %s has unknown/unsupported MAPI version %d.",
878 mod_basename
, *mapi_version
);
880 rb_free(mod_basename
);
889 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
890 modlist
[num_mods
]->address
= tmpptr
;
891 modlist
[num_mods
]->version
= ver
;
892 modlist
[num_mods
]->core
= core
;
893 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
894 modlist
[num_mods
]->mapi_header
= mapi_version
;
895 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
900 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
901 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
902 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
903 (unsigned long) tmpptr
);
904 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
905 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
907 rb_free(mod_basename
);
916 * side effects - expand the size of modlist if necessary
919 increase_modlist(void)
921 struct module **new_modlist
= NULL
;
923 if((num_mods
+ 1) < max_mods
)
926 new_modlist
= (struct module **) rb_malloc(sizeof(struct module) *
927 (max_mods
+ MODS_INCREMENT
));
928 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module) * num_mods
);
931 modlist
= new_modlist
;
932 max_mods
+= MODS_INCREMENT
;
935 #else /* STATIC_MODULES */
937 /* load_all_modules()
941 * side effects - all the msgtabs are added for static modules
944 load_all_modules(int warn
)
946 load_static_modules();
949 #endif /* STATIC_MODULES */