1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
24 #include "dynloader.h"
27 #include "sysdep_decls.h"
34 struct LogFunctionHook *LogHookTable = NULL;
35 struct CleanupFunctionHook *CleanupHookTable = NULL;
36 struct SessionFunctionHook *SessionHookTable = NULL;
37 struct UserFunctionHook *UserHookTable = NULL;
39 struct ProtoFunctionHook {
40 void (*handler) (char *cmdbuf);
43 struct ProtoFunctionHook *next;
44 } *ProtoHookList = NULL;
46 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
48 struct ProtoFunctionHook *p = mallok(sizeof *p);
51 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
57 p->next = ProtoHookList;
61 int DLoader_Exec_Cmd(char *cmdbuf)
63 struct ProtoFunctionHook *p;
65 for (p = ProtoHookList; p; p = p->next) {
66 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
67 p->handler(&cmdbuf[5]);
74 void DLoader_Init(char *pathname)
80 struct DLModule_Info *(*h_init_fcn) (void);
81 struct DLModule_Info *dl_info;
83 char pathbuf[PATH_MAX];
85 if ((dir = opendir(pathname)) == NULL) {
89 while ((dptr = readdir(dir)) != NULL) {
90 if (dptr->d_name[0] == '.')
93 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
95 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
97 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
100 /* dl_error = dlerror(); */
101 fprintf(stderr, "DLoader_Init dlopen failed\n");
104 h_init_fcn = (struct DLModule_Info * (*)(void))
106 dlsym(fcn_handle, "Dynamic_Module_Init");
108 dlsym(fcn_handle, "_Dynamic_Module_Init");
111 if ((dl_error = dlerror()) != NULL) {
112 fprintf(stderr, "DLoader_Init dlsym failed (%s)\n", dl_error);
115 dl_info = h_init_fcn();
117 printf("Loaded module: %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
118 dl_info->major_version, dl_info->minor_version,
119 dl_info->module_author, dl_info->module_author_email);
125 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
128 struct LogFunctionHook *newfcn;
130 newfcn = (struct LogFunctionHook *)
131 mallok(sizeof(struct LogFunctionHook));
132 newfcn->next = LogHookTable;
133 newfcn->h_function_pointer = fcn_ptr;
134 newfcn->loglevel = loglevel;
135 LogHookTable = newfcn;
137 lprintf(5, "Registered a new logging function\n");
141 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
144 struct CleanupFunctionHook *newfcn;
146 newfcn = (struct CleanupFunctionHook *)
147 mallok(sizeof(struct CleanupFunctionHook));
148 newfcn->next = CleanupHookTable;
149 newfcn->h_function_pointer = fcn_ptr;
150 CleanupHookTable = newfcn;
152 lprintf(5, "Registered a new cleanup function\n");
156 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
159 struct SessionFunctionHook *newfcn;
161 newfcn = (struct SessionFunctionHook *)
162 mallok(sizeof(struct SessionFunctionHook));
163 newfcn->next = SessionHookTable;
164 newfcn->h_function_pointer = fcn_ptr;
165 newfcn->eventtype = EventType;
166 SessionHookTable = newfcn;
168 lprintf(5, "Registered a new session function (type %d)\n",
173 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
176 struct UserFunctionHook *newfcn;
178 newfcn = (struct UserFunctionHook *)
179 mallok(sizeof(struct UserFunctionHook));
180 newfcn->next = UserHookTable;
181 newfcn->h_function_pointer = fcn_ptr;
182 newfcn->eventtype = EventType;
183 UserHookTable = newfcn;
185 lprintf(5, "Registered a new user function (type %d)\n",
190 void PerformSessionHooks(int EventType)
192 struct SessionFunctionHook *fcn;
194 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
195 if (fcn->eventtype == EventType) {
196 (*fcn->h_function_pointer) ();
201 void PerformLogHooks(int loglevel, char *logmsg)
203 struct LogFunctionHook *fcn;
205 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
206 if (fcn->loglevel >= loglevel) {
207 (*fcn->h_function_pointer) (logmsg);
212 void PerformUserHooks(char *username, long usernum, int EventType)
214 struct UserFunctionHook *fcn;
216 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
217 if (fcn->eventtype == EventType) {
218 (*fcn->h_function_pointer) (username, usernum);