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 struct DLModule_Info *(*h_init_fcn) (void);
84 struct DLModule_Info *dl_info;
86 char pathbuf[PATH_MAX];
88 if ((dir = opendir(pathname)) == NULL) {
92 while ((dptr = readdir(dir)) != NULL) {
93 if (dptr->d_name[0] == '.')
95 lprintf(9, "Attempting to load %s\n", dptr->d_name);
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 = (struct DLModule_Info * (*)(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 printf("Loaded module: %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
126 dl_info->major_version, dl_info->minor_version,
127 dl_info->module_author, dl_info->module_author_email);
133 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
136 struct LogFunctionHook *newfcn;
138 newfcn = (struct LogFunctionHook *)
139 mallok(sizeof(struct LogFunctionHook));
140 newfcn->next = LogHookTable;
141 newfcn->h_function_pointer = fcn_ptr;
142 newfcn->loglevel = loglevel;
143 LogHookTable = newfcn;
145 lprintf(5, "Registered a new logging function\n");
149 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
152 struct CleanupFunctionHook *newfcn;
154 newfcn = (struct CleanupFunctionHook *)
155 mallok(sizeof(struct CleanupFunctionHook));
156 newfcn->next = CleanupHookTable;
157 newfcn->h_function_pointer = fcn_ptr;
158 CleanupHookTable = newfcn;
160 lprintf(5, "Registered a new cleanup function\n");
164 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
167 struct SessionFunctionHook *newfcn;
169 newfcn = (struct SessionFunctionHook *)
170 mallok(sizeof(struct SessionFunctionHook));
171 newfcn->next = SessionHookTable;
172 newfcn->h_function_pointer = fcn_ptr;
173 newfcn->eventtype = EventType;
174 SessionHookTable = newfcn;
176 lprintf(5, "Registered a new session function (type %d)\n",
181 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
184 struct UserFunctionHook *newfcn;
186 newfcn = (struct UserFunctionHook *)
187 mallok(sizeof(struct UserFunctionHook));
188 newfcn->next = UserHookTable;
189 newfcn->h_function_pointer = fcn_ptr;
190 newfcn->eventtype = EventType;
191 UserHookTable = newfcn;
193 lprintf(5, "Registered a new user function (type %d)\n",
198 void PerformSessionHooks(int EventType)
200 struct SessionFunctionHook *fcn;
202 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
203 if (fcn->eventtype == EventType) {
204 (*fcn->h_function_pointer) ();
209 void PerformLogHooks(int loglevel, char *logmsg)
211 struct LogFunctionHook *fcn;
213 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
214 if (fcn->loglevel >= loglevel) {
215 (*fcn->h_function_pointer) (logmsg);
220 void PerformUserHooks(char *username, long usernum, int EventType)
222 struct UserFunctionHook *fcn;
224 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
225 if (fcn->eventtype == EventType) {
226 (*fcn->h_function_pointer) (username, usernum);