1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
25 #include "dynloader.h"
28 #include "sysdep_decls.h"
36 struct LogFunctionHook *LogHookTable = NULL;
37 struct CleanupFunctionHook *CleanupHookTable = NULL;
38 struct SessionFunctionHook *SessionHookTable = NULL;
39 struct UserFunctionHook *UserHookTable = NULL;
41 struct ProtoFunctionHook {
42 void (*handler) (char *cmdbuf);
45 struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
48 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
50 struct ProtoFunctionHook *p = mallok(sizeof *p);
53 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
59 p->next = ProtoHookList;
63 int DLoader_Exec_Cmd(char *cmdbuf)
65 struct ProtoFunctionHook *p;
67 for (p = ProtoHookList; p; p = p->next) {
68 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
69 p->handler(&cmdbuf[5]);
76 void DLoader_Init(char *pathname)
83 char *(*h_init_fcn) (void);
86 char pathbuf[PATH_MAX];
88 if ((dir = opendir(pathname)) == NULL) {
92 while ((dptr = readdir(dir)) != NULL) {
93 if (dptr->d_name[0] == '.')
96 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
98 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
100 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
103 safestrncpy(dl_error, dlerror(), sizeof dl_error);
104 for (i=0; i<strlen(dl_error); ++i)
105 if (!isprint(dl_error[i]))
107 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
111 h_init_fcn = (char * (*)(void))
113 dlsym(fcn_handle, "Dynamic_Module_Init");
115 dlsym(fcn_handle, "_Dynamic_Module_Init");
118 if (dlerror() != NULL) {
119 fprintf(stderr, "DLoader_Init dlsym failed\n");
122 dl_info = h_init_fcn();
124 lprintf(3, "Loaded module: %s\n", dl_info);
130 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
133 struct LogFunctionHook *newfcn;
135 newfcn = (struct LogFunctionHook *)
136 mallok(sizeof(struct LogFunctionHook));
137 newfcn->next = LogHookTable;
138 newfcn->h_function_pointer = fcn_ptr;
139 newfcn->loglevel = loglevel;
140 LogHookTable = newfcn;
142 lprintf(5, "Registered a new logging function\n");
146 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
149 struct CleanupFunctionHook *newfcn;
151 newfcn = (struct CleanupFunctionHook *)
152 mallok(sizeof(struct CleanupFunctionHook));
153 newfcn->next = CleanupHookTable;
154 newfcn->h_function_pointer = fcn_ptr;
155 CleanupHookTable = newfcn;
157 lprintf(5, "Registered a new cleanup function\n");
161 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
164 struct SessionFunctionHook *newfcn;
166 newfcn = (struct SessionFunctionHook *)
167 mallok(sizeof(struct SessionFunctionHook));
168 newfcn->next = SessionHookTable;
169 newfcn->h_function_pointer = fcn_ptr;
170 newfcn->eventtype = EventType;
171 SessionHookTable = newfcn;
173 lprintf(5, "Registered a new session function (type %d)\n",
178 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
181 struct UserFunctionHook *newfcn;
183 newfcn = (struct UserFunctionHook *)
184 mallok(sizeof(struct UserFunctionHook));
185 newfcn->next = UserHookTable;
186 newfcn->h_function_pointer = fcn_ptr;
187 newfcn->eventtype = EventType;
188 UserHookTable = newfcn;
190 lprintf(5, "Registered a new user function (type %d)\n",
195 void PerformSessionHooks(int EventType)
197 struct SessionFunctionHook *fcn;
199 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
200 if (fcn->eventtype == EventType) {
201 (*fcn->h_function_pointer) ();
206 void PerformLogHooks(int loglevel, char *logmsg)
208 struct LogFunctionHook *fcn;
210 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
211 if (fcn->loglevel >= loglevel) {
212 (*fcn->h_function_pointer) (logmsg);
217 void PerformUserHooks(char *username, long usernum, int EventType)
219 struct UserFunctionHook *fcn;
221 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
222 if (fcn->eventtype == EventType) {
223 (*fcn->h_function_pointer) (username, usernum);