1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
7 ******************************************************/
13 #include <sys/types.h>
19 #include "dynloader.h"
22 #include "sysdep_decls.h"
24 struct CleanupFunctionHook *CleanupHookTable = NULL;
25 struct SessionFunctionHook *SessionHookTable = NULL;
27 struct ProtoFunctionHook
29 void (*handler)(char *cmdbuf);
32 struct ProtoFunctionHook *next;
33 } *ProtoHookList = NULL;
35 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
37 struct ProtoFunctionHook *p = malloc(sizeof *p);
41 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
48 p->next = ProtoHookList;
52 int DLoader_Exec_Cmd(char *cmdbuf)
54 struct ProtoFunctionHook *p;
56 for (p = ProtoHookList; p; p = p->next)
58 if (!strncmp(cmdbuf, p->cmd, 4))
60 p->handler(&cmdbuf[5]);
67 void DLoader_Init(char *pathname)
73 struct DLModule_Info* (*h_init_fcn)(void);
74 struct DLModule_Info *dl_info;
76 char pathbuf[PATH_MAX];
78 if ((dir = opendir(pathname))==NULL)
84 while ((dptr=readdir(dir))!= NULL)
86 if (dptr->d_name[0] == '.')
89 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
90 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
93 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
97 h_init_fcn = dlsym(fcn_handle, "Dynamic_Module_Init");
98 if ((dl_error = dlerror()) != NULL)
100 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
104 dl_info = h_init_fcn();
106 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
107 dl_info->major_version, dl_info->minor_version,
108 dl_info->module_author, dl_info->module_author_email);
114 void CtdlRegisterCleanupHook(void *fcn_ptr) {
116 struct CleanupFunctionHook *newfcn;
118 newfcn = (struct CleanupFunctionHook *)
119 malloc(sizeof(struct CleanupFunctionHook));
120 newfcn->next = CleanupHookTable;
121 newfcn->h_function_pointer = fcn_ptr;
122 CleanupHookTable = newfcn;
124 lprintf(5, "Registered a new cleanup function\n");
128 void CtdlRegisterSessionHook(void *fcn_ptr, int EventType) {
130 struct SessionFunctionHook *newfcn;
132 newfcn = (struct SessionFunctionHook *)
133 malloc(sizeof(struct SessionFunctionHook));
134 newfcn->next = SessionHookTable;
135 newfcn->h_function_pointer = fcn_ptr;
136 newfcn->eventtype = EventType;
137 SessionHookTable = newfcn;
139 lprintf(5, "Registered a new session function (type %d)\n",
144 void PerformSessionHooks(int EventType) {
145 struct SessionFunctionHook *fcn;
147 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
148 if (fcn->eventtype == EventType) {
149 (*fcn->h_function_pointer)();