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;
61 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
64 int DLoader_Exec_Cmd(char *cmdbuf)
66 struct ProtoFunctionHook *p;
68 for (p = ProtoHookList; p; p = p->next) {
69 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
70 p->handler(&cmdbuf[5]);
77 void DLoader_Init(char *pathname)
84 char *(*h_init_fcn) (void);
87 char pathbuf[PATH_MAX];
89 if ((dir = opendir(pathname)) == NULL) {
93 while ((dptr = readdir(dir)) != NULL) {
94 if (dptr->d_name[0] == '.')
97 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
99 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
101 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
104 safestrncpy(dl_error, dlerror(), sizeof dl_error);
105 for (i=0; i<strlen(dl_error); ++i)
106 if (!isprint(dl_error[i]))
108 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
112 h_init_fcn = (char * (*)(void))
114 dlsym(fcn_handle, "Dynamic_Module_Init");
116 dlsym(fcn_handle, "_Dynamic_Module_Init");
119 if (dlerror() != NULL) {
120 fprintf(stderr, "DLoader_Init dlsym failed\n");
123 dl_info = h_init_fcn();
125 lprintf(3, "Loaded module: %s\n", dl_info);
131 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
134 struct LogFunctionHook *newfcn;
136 newfcn = (struct LogFunctionHook *)
137 mallok(sizeof(struct LogFunctionHook));
138 newfcn->next = LogHookTable;
139 newfcn->h_function_pointer = fcn_ptr;
140 newfcn->loglevel = loglevel;
141 LogHookTable = newfcn;
143 lprintf(5, "Registered a new logging function\n");
147 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
150 struct CleanupFunctionHook *newfcn;
152 newfcn = (struct CleanupFunctionHook *)
153 mallok(sizeof(struct CleanupFunctionHook));
154 newfcn->next = CleanupHookTable;
155 newfcn->h_function_pointer = fcn_ptr;
156 CleanupHookTable = newfcn;
158 lprintf(5, "Registered a new cleanup function\n");
162 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
165 struct SessionFunctionHook *newfcn;
167 newfcn = (struct SessionFunctionHook *)
168 mallok(sizeof(struct SessionFunctionHook));
169 newfcn->next = SessionHookTable;
170 newfcn->h_function_pointer = fcn_ptr;
171 newfcn->eventtype = EventType;
172 SessionHookTable = newfcn;
174 lprintf(5, "Registered a new session function (type %d)\n",
179 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
182 struct UserFunctionHook *newfcn;
184 newfcn = (struct UserFunctionHook *)
185 mallok(sizeof(struct UserFunctionHook));
186 newfcn->next = UserHookTable;
187 newfcn->h_function_pointer = fcn_ptr;
188 newfcn->eventtype = EventType;
189 UserHookTable = newfcn;
191 lprintf(5, "Registered a new user function (type %d)\n",
196 void PerformSessionHooks(int EventType)
198 struct SessionFunctionHook *fcn;
200 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
201 if (fcn->eventtype == EventType) {
202 (*fcn->h_function_pointer) ();
207 void PerformLogHooks(int loglevel, char *logmsg)
209 struct LogFunctionHook *fcn;
211 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
212 if (fcn->loglevel >= loglevel) {
213 (*fcn->h_function_pointer) (logmsg);
218 void PerformUserHooks(char *username, long usernum, int EventType)
220 struct UserFunctionHook *fcn;
222 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
223 if (fcn->eventtype == EventType) {
224 (*fcn->h_function_pointer) (username, usernum);