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"
40 #include "irc_string.h"
41 #include "sprintf_irc.h"
46 * I have moved the dl* function definitions and
47 * the two functions (load_a_module / unload_a_module) to the
49 * And also made the necessary changes to those functions
50 * to comply with shl_load and friends.
51 * In this file, to keep consistency with the makefile,
52 * I added the ability to load *.sl files, too.
56 #ifndef STATIC_MODULES
58 struct module **modlist
= NULL
;
60 static const char *core_module_table
[] = {
76 #define MODS_INCREMENT 10
78 int max_mods
= MODS_INCREMENT
;
80 static rb_dlink_list mod_paths
;
82 static int mo_modload(struct Client
*, struct Client
*, int, const char **);
83 static int mo_modlist(struct Client
*, struct Client
*, int, const char **);
84 static int mo_modreload(struct Client
*, struct Client
*, int, const char **);
85 static int mo_modunload(struct Client
*, struct Client
*, int, const char **);
86 static int mo_modrestart(struct Client
*, struct Client
*, int, const char **);
88 struct Message modload_msgtab
= {
89 "MODLOAD", 0, 0, 0, MFLG_SLOW
,
90 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modload
, 2}}
93 struct Message modunload_msgtab
= {
94 "MODUNLOAD", 0, 0, 0, MFLG_SLOW
,
95 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modunload
, 2}}
98 struct Message modreload_msgtab
= {
99 "MODRELOAD", 0, 0, 0, MFLG_SLOW
,
100 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modreload
, 2}}
103 struct Message modlist_msgtab
= {
104 "MODLIST", 0, 0, 0, MFLG_SLOW
,
105 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modlist
, 0}}
108 struct Message modrestart_msgtab
= {
109 "MODRESTART", 0, 0, 0, MFLG_SLOW
,
110 {mg_unreg
, mg_not_oper
, mg_ignore
, mg_ignore
, mg_ignore
, {mo_modrestart
, 0}}
113 extern struct Message error_msgtab
;
118 mod_add_cmd(&modload_msgtab
);
119 mod_add_cmd(&modunload_msgtab
);
120 mod_add_cmd(&modreload_msgtab
);
121 mod_add_cmd(&modlist_msgtab
);
122 mod_add_cmd(&modrestart_msgtab
);
124 /* Add the default paths we look in to the module system --nenolod */
125 mod_add_path(MODPATH
);
126 mod_add_path(AUTOMODPATH
);
133 * side effects - returns a module path from path
135 static struct module_path
*
136 mod_find_path(const char *path
)
139 struct module_path
*mpath
;
141 RB_DLINK_FOREACH(ptr
, mod_paths
.head
)
145 if(!strcmp(path
, mpath
->path
))
156 * side effects - adds path to list
159 mod_add_path(const char *path
)
161 struct module_path
*pathst
;
163 if(mod_find_path(path
))
166 pathst
= rb_malloc(sizeof(struct module_path
));
168 strcpy(pathst
->path
, path
);
169 rb_dlinkAddAlloc(pathst
, &mod_paths
);
176 * side effects - clear the lists of paths
179 mod_clear_paths(void)
181 rb_dlink_node
*ptr
, *next_ptr
;
183 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, mod_paths
.head
)
186 rb_free_rb_dlink_node(ptr
);
189 mod_paths
.head
= mod_paths
.tail
= NULL
;
190 mod_paths
.length
= 0;
200 irc_basename(const char *path
)
202 char *mod_basename
= rb_malloc(strlen(path
) + 1);
205 if(!(s
= strrchr(path
, '/')))
210 (void) strcpy(mod_basename
, s
);
222 findmodule_byname(const char *name
)
226 for (i
= 0; i
< num_mods
; i
++)
228 if(!irccmp(modlist
[i
]->name
, name
))
234 /* load_all_modules()
241 load_all_modules(int warn
)
243 DIR *system_module_dir
= NULL
;
244 struct dirent
*ldirent
= NULL
;
245 char module_fq_name
[PATH_MAX
+ 1];
250 modlist
= (struct module **) rb_malloc(sizeof(struct module) * (MODS_INCREMENT
));
252 max_mods
= MODS_INCREMENT
;
254 system_module_dir
= opendir(AUTOMODPATH
);
256 if(system_module_dir
== NULL
)
258 ilog(L_MAIN
, "Could not load modules from %s: %s", AUTOMODPATH
, strerror(errno
));
262 while ((ldirent
= readdir(system_module_dir
)) != NULL
)
264 len
= strlen(ldirent
->d_name
);
265 if((len
> 3) && !strcmp(ldirent
->d_name
+len
-3, SHARED_SUFFIX
))
267 (void) rb_snprintf(module_fq_name
, sizeof(module_fq_name
), "%s/%s", AUTOMODPATH
, ldirent
->d_name
);
268 (void) load_a_module(module_fq_name
, warn
, 0);
272 (void) closedir(system_module_dir
);
275 /* load_core_modules()
279 * side effects - core modules are loaded, if any fail, kill ircd
282 load_core_modules(int warn
)
284 char module_name
[MAXPATHLEN
];
288 for (i
= 0; core_module_table
[i
]; i
++)
290 rb_snprintf(module_name
, sizeof(module_name
), "%s/%s%s", MODPATH
,
291 core_module_table
[i
], SHARED_SUFFIX
);
293 if(load_a_module(module_name
, warn
, 1) == -1)
296 "Error loading core module %s%s: terminating ircd",
297 core_module_table
[i
], SHARED_SUFFIX
);
310 load_one_module(const char *path
, int coremodule
)
312 char modpath
[MAXPATHLEN
];
313 rb_dlink_node
*pathst
;
314 struct module_path
*mpath
;
318 if (server_state_foreground
== 1)
319 inotice("loading module %s ...", path
);
321 RB_DLINK_FOREACH(pathst
, mod_paths
.head
)
323 mpath
= pathst
->data
;
325 rb_snprintf(modpath
, sizeof(modpath
), "%s/%s", mpath
->path
, path
);
326 if((strstr(modpath
, "../") == NULL
) && (strstr(modpath
, "/..") == NULL
))
328 if(stat(modpath
, &statbuf
) == 0)
330 if(S_ISREG(statbuf
.st_mode
))
332 /* Regular files only please */
334 return load_a_module(modpath
, 1, 1);
336 return load_a_module(modpath
, 1, 0);
343 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Cannot locate module %s", path
);
348 /* load a module .. */
350 mo_modload(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
= irc_basename(parv
[1]);
363 if(findmodule_byname(m_bn
) != -1)
365 sendto_one_notice(source_p
, ":Module %s is already loaded", m_bn
);
370 load_one_module(parv
[1], 0);
378 /* unload a module .. */
380 mo_modunload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
385 if(!IsOperAdmin(source_p
))
387 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
388 me
.name
, source_p
->name
, "admin");
392 m_bn
= irc_basename(parv
[1]);
394 if((modindex
= findmodule_byname(m_bn
)) == -1)
396 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
401 if(modlist
[modindex
]->core
== 1)
403 sendto_one_notice(source_p
, ":Module %s is a core module and may not be unloaded", m_bn
);
408 if(unload_one_module(m_bn
, 1) == -1)
410 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
417 /* unload and load in one! */
419 mo_modreload(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
425 if(!IsOperAdmin(source_p
))
427 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
428 me
.name
, source_p
->name
, "admin");
432 m_bn
= irc_basename(parv
[1]);
434 if((modindex
= findmodule_byname(m_bn
)) == -1)
436 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
441 check_core
= modlist
[modindex
]->core
;
443 if(unload_one_module(m_bn
, 1) == -1)
445 sendto_one_notice(source_p
, ":Module %s is not loaded", m_bn
);
450 if((load_one_module(parv
[1], check_core
) == -1) && check_core
)
452 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
453 "Error reloading core module: %s: terminating ircd", parv
[1]);
454 ilog(L_MAIN
, "Error loading core module %s: terminating ircd", parv
[1]);
462 /* list modules .. */
464 mo_modlist(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
468 if(!IsOperAdmin(source_p
))
470 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
471 me
.name
, source_p
->name
, "admin");
475 for (i
= 0; i
< num_mods
; i
++)
479 if(match(parv
[1], modlist
[i
]->name
))
481 sendto_one(source_p
, form_str(RPL_MODLIST
),
482 me
.name
, source_p
->name
,
485 modlist
[i
]->version
, modlist
[i
]->core
? "(core)" : "");
490 sendto_one(source_p
, form_str(RPL_MODLIST
),
491 me
.name
, source_p
->name
, modlist
[i
]->name
,
492 modlist
[i
]->address
, modlist
[i
]->version
,
493 modlist
[i
]->core
? "(core)" : "");
497 sendto_one(source_p
, form_str(RPL_ENDOFMODLIST
), me
.name
, source_p
->name
);
501 /* unload and reload all modules */
503 mo_modrestart(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char **parv
)
507 if(!IsOperAdmin(source_p
))
509 sendto_one(source_p
, form_str(ERR_NOPRIVS
),
510 me
.name
, source_p
->name
, "admin");
514 sendto_one_notice(source_p
, ":Reloading all modules");
518 unload_one_module(modlist
[0]->name
, 0);
521 load_core_modules(0);
524 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
525 "Module Restart: %d modules unloaded, %d modules loaded",
527 ilog(L_MAIN
, "Module Restart: %d modules unloaded, %d modules loaded", modnum
, num_mods
);
534 #define RTLD_NOW RTLD_LAZY /* openbsd deficiency */
537 #ifdef CHARYBDIS_PROFILE
538 # ifndef RTLD_PROFILE
539 # warning libdl may not support profiling, sucks. :(
540 # define RTLD_PROFILE 0
544 static void increase_modlist(void);
546 #define MODS_INCREMENT 10
548 static char unknown_ver
[] = "<unknown>";
550 /* This file contains the core functions to use dynamic libraries.
555 #ifdef HAVE_MACH_O_DYLD_H
557 ** jmallett's dl*(3) shims for NSModule(3) systems.
559 #include <mach-o/dyld.h>
563 #define RTLD_LAZY 2185 /* built-in dl*(3) don't care */
566 void undefinedErrorHandler(const char *);
567 NSModule
multipleErrorHandler(NSSymbol
, NSModule
, NSModule
);
568 void linkEditErrorHandler(NSLinkEditErrors
, int, const char *, const char *);
570 void *dlopen(char *, int);
572 void *dlsym(void *, char *);
574 static int firstLoad
= TRUE
;
575 static int myDlError
;
576 static char *myErrorTable
[] = { "Loading file as object failed\n",
577 "Loading file as object succeeded\n",
578 "Not a valid shared object\n",
579 "Architecture of object invalid on this architecture\n",
580 "Invalid or corrupt image\n",
581 "Could not access object\n",
582 "NSCreateObjectFileImageFromFile failed\n",
587 undefinedErrorHandler(const char *symbolName
)
589 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Undefined symbol: %s", symbolName
);
590 ilog(L_MAIN
, "Undefined symbol: %s", symbolName
);
595 multipleErrorHandler(NSSymbol s
, NSModule old
, NSModule
new)
598 ** This results in substantial leaking of memory... Should free one
601 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
602 "Symbol `%s' found in `%s' and `%s'",
603 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
604 ilog(L_MAIN
, "Symbol `%s' found in `%s' and `%s'",
605 NSNameOfSymbol(s
), NSNameOfModule(old
), NSNameOfModule(new));
606 /* We return which module should be considered valid, I believe */
611 linkEditErrorHandler(NSLinkEditErrors errorClass
, int errnum
,
612 const char *fileName
, const char *errorString
)
614 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
615 "Link editor error: %s for %s", errorString
, fileName
);
616 ilog(L_MAIN
, "Link editor error: %s for %s", errorString
, fileName
);
623 return myDlError
== NSObjectFileImageSuccess
? NULL
: myErrorTable
[myDlError
% 7];
627 dlopen(char *filename
, int unused
)
629 NSObjectFileImage myImage
;
635 ** If we are loading our first symbol (huzzah!) we should go ahead
636 ** and install link editor error handling!
638 NSLinkEditErrorHandlers linkEditorErrorHandlers
;
640 linkEditorErrorHandlers
.undefined
= undefinedErrorHandler
;
641 linkEditorErrorHandlers
.multiple
= multipleErrorHandler
;
642 linkEditorErrorHandlers
.linkEdit
= linkEditErrorHandler
;
643 NSInstallLinkEditErrorHandlers(&linkEditorErrorHandlers
);
646 myDlError
= NSCreateObjectFileImageFromFile(filename
, &myImage
);
647 if(myDlError
!= NSObjectFileImageSuccess
)
651 myModule
= NSLinkModule(myImage
, filename
, NSLINKMODULE_OPTION_PRIVATE
);
652 return (void *) myModule
;
656 dlclose(void *myModule
)
658 NSUnLinkModule(myModule
, FALSE
);
663 dlsym(void *myModule
, char *mySymbolName
)
667 mySymbol
= NSLookupSymbolInModule((NSModule
) myModule
, mySymbolName
);
668 return NSAddressOfSymbol(mySymbol
);
675 * HPUX dl compat functions
677 #if defined(HAVE_SHL_LOAD) && !defined(HAVE_DLOPEN)
678 #define RTLD_LAZY BIND_DEFERRED
679 #define RTLD_GLOBAL DYNAMIC_PATH
680 #define dlopen(file,mode) (void *)shl_load((file), (mode), (long) 0)
681 #define dlclose(handle) shl_unload((shl_t)(handle))
682 #define dlsym(handle,name) hpux_dlsym(handle,name)
683 #define dlerror() strerror(errno)
686 hpux_dlsym(void *handle
, char *name
)
689 if(!shl_findsym((shl_t
*) & handle
, name
, TYPE_UNDEFINED
, &sym_addr
))
696 /* unload_one_module()
698 * inputs - name of module to unload
699 * - 1 to say modules unloaded, 0 to not
700 * output - 0 if successful, -1 if error
701 * side effects - module is unloaded
704 unload_one_module(const char *name
, int warn
)
708 if((modindex
= findmodule_byname(name
)) == -1)
712 ** XXX - The type system in C does not allow direct conversion between
713 ** data and function pointers, but as it happens, most C compilers will
714 ** safely do this, however it is a theoretical overlow to cast as we
715 ** must do here. I have library functions to take care of this, but
716 ** despite being more "correct" for the C language, this is more
717 ** practical. Removing the abuse of the ability to cast ANY pointer
718 ** to and from an integer value here will break some compilers.
721 /* Left the comment in but the code isn't here any more -larne */
722 switch (modlist
[modindex
]->mapi_version
)
726 struct mapi_mheader_av1
*mheader
= modlist
[modindex
]->mapi_header
;
727 if(mheader
->mapi_command_list
)
730 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
734 /* hook events are never removed, we simply lose the
735 * ability to call them --fl
737 if(mheader
->mapi_hfn_list
)
739 mapi_hfn_list_av1
*m
;
740 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
741 remove_hook(m
->hapi_name
, m
->fn
);
744 if(mheader
->mapi_unregister
)
745 mheader
->mapi_unregister();
749 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
750 "Unknown/unsupported MAPI version %d when unloading %s!",
751 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
752 ilog(L_MAIN
, "Unknown/unsupported MAPI version %d when unloading %s!",
753 modlist
[modindex
]->mapi_version
, modlist
[modindex
]->name
);
757 dlclose(modlist
[modindex
]->address
);
759 rb_free(modlist
[modindex
]->name
);
760 memcpy(&modlist
[modindex
], &modlist
[modindex
+ 1],
761 sizeof(struct module) * ((num_mods
- 1) - modindex
));
768 ilog(L_MAIN
, "Module %s unloaded", name
);
769 sendto_realops_snomask(SNO_GENERAL
, L_ALL
, "Module %s unloaded", name
);
779 * inputs - path name of module, int to notice, int of core
780 * output - -1 if error 0 if success
781 * side effects - loads a module if successful
784 load_a_module(const char *path
, int warn
, int core
)
793 mod_basename
= irc_basename(path
);
795 #ifdef CHARYBDIS_PROFILE
796 tmpptr
= dlopen(path
, RTLD_NOW
| RTLD_PROFILE
);
798 tmpptr
= dlopen(path
, RTLD_NOW
);
803 const char *err
= dlerror();
805 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
806 "Error loading module %s: %s", mod_basename
, err
);
807 ilog(L_MAIN
, "Error loading module %s: %s", mod_basename
, err
);
808 rb_free(mod_basename
);
814 * _mheader is actually a struct mapi_mheader_*, but mapi_version
815 * is always the first member of this structure, so we treate it
816 * as a single int in order to determine the API version.
819 mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "_mheader");
820 if((mapi_version
== NULL
821 && (mapi_version
= (int *) (uintptr_t) dlsym(tmpptr
, "__mheader")) == NULL
)
822 || MAPI_MAGIC(*mapi_version
) != MAPI_MAGIC_HDR
)
824 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
825 "Data format error: module %s has no MAPI header.",
827 ilog(L_MAIN
, "Data format error: module %s has no MAPI header.", mod_basename
);
828 (void) dlclose(tmpptr
);
829 rb_free(mod_basename
);
833 switch (MAPI_VERSION(*mapi_version
))
837 struct mapi_mheader_av1
*mheader
= (struct mapi_mheader_av1
*) mapi_version
; /* see above */
838 if(mheader
->mapi_register
&& (mheader
->mapi_register() == -1))
840 ilog(L_MAIN
, "Module %s indicated failure during load.",
842 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
843 "Module %s indicated failure during load.",
846 rb_free(mod_basename
);
849 if(mheader
->mapi_command_list
)
852 for (m
= mheader
->mapi_command_list
; *m
; ++m
)
856 if(mheader
->mapi_hook_list
)
859 for (m
= mheader
->mapi_hook_list
; m
->hapi_name
; ++m
)
860 *m
->hapi_id
= register_hook(m
->hapi_name
);
863 if(mheader
->mapi_hfn_list
)
865 mapi_hfn_list_av1
*m
;
866 for (m
= mheader
->mapi_hfn_list
; m
->hapi_name
; ++m
)
867 add_hook(m
->hapi_name
, m
->fn
);
870 ver
= mheader
->mapi_module_version
;
875 ilog(L_MAIN
, "Module %s has unknown/unsupported MAPI version %d.",
876 mod_basename
, MAPI_VERSION(*mapi_version
));
877 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
878 "Module %s has unknown/unsupported MAPI version %d.",
879 mod_basename
, *mapi_version
);
881 rb_free(mod_basename
);
890 modlist
[num_mods
] = rb_malloc(sizeof(struct module));
891 modlist
[num_mods
]->address
= tmpptr
;
892 modlist
[num_mods
]->version
= ver
;
893 modlist
[num_mods
]->core
= core
;
894 modlist
[num_mods
]->name
= rb_strdup(mod_basename
);
895 modlist
[num_mods
]->mapi_header
= mapi_version
;
896 modlist
[num_mods
]->mapi_version
= MAPI_VERSION(*mapi_version
);
901 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
902 "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
903 mod_basename
, ver
, MAPI_VERSION(*mapi_version
),
904 (unsigned long) tmpptr
);
905 ilog(L_MAIN
, "Module %s [version: %s; MAPI version: %d] loaded at 0x%lx",
906 mod_basename
, ver
, MAPI_VERSION(*mapi_version
), (unsigned long) tmpptr
);
908 rb_free(mod_basename
);
917 * side effects - expand the size of modlist if necessary
920 increase_modlist(void)
922 struct module **new_modlist
= NULL
;
924 if((num_mods
+ 1) < max_mods
)
927 new_modlist
= (struct module **) rb_malloc(sizeof(struct module) *
928 (max_mods
+ MODS_INCREMENT
));
929 memcpy((void *) new_modlist
, (void *) modlist
, sizeof(struct module) * num_mods
);
932 modlist
= new_modlist
;
933 max_mods
+= MODS_INCREMENT
;
936 #else /* STATIC_MODULES */
938 /* load_all_modules()
942 * side effects - all the msgtabs are added for static modules
945 load_all_modules(int warn
)
947 load_static_modules();
950 #endif /* STATIC_MODULES */