4b3b5d9959a19b0d2a61bc8528d0704f33a436ed
[citadel.git] / citadel / dynloader.c
1 /*******************************************************
2  *
3  * Citadel Dynamic Loading Module
4  * Written by Brian Costello
5  * btx@calyx.net
6  *
7  * $Id$
8  *
9  ******************************************************/
10
11
12 #include "sysdep.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <dlfcn.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <strings.h>
19 #include <syslog.h>
20 #ifdef HAVE_PTHREAD_H
21 #include <pthread.h>
22 #endif
23 #include <limits.h>
24 #include <ctype.h>
25 #include "dynloader.h"
26 #include "citadel.h"
27 #include "server.h"
28 #include "sysdep_decls.h"
29 #include "tools.h"
30
31 #ifndef HAVE_SNPRINTF
32 #include <stdarg.h>
33 #include "snprintf.h"
34 #endif
35
36 struct LogFunctionHook *LogHookTable = NULL;
37 struct CleanupFunctionHook *CleanupHookTable = NULL;
38 struct SessionFunctionHook *SessionHookTable = NULL;
39 struct UserFunctionHook *UserHookTable = NULL;
40
41 struct ProtoFunctionHook {
42         void (*handler) (char *cmdbuf);
43         char *cmd;
44         char *desc;
45         struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
47
48 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
49 {
50         struct ProtoFunctionHook *p = mallok(sizeof *p);
51
52         if (p == NULL) {
53                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
54                 exit(EXIT_FAILURE);
55         }
56         p->handler = handler;
57         p->cmd = cmd;
58         p->desc = desc;
59         p->next = ProtoHookList;
60         ProtoHookList = p;
61         lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
62 }
63
64 int DLoader_Exec_Cmd(char *cmdbuf)
65 {
66         struct ProtoFunctionHook *p;
67
68         for (p = ProtoHookList; p; p = p->next) {
69                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
70                         p->handler(&cmdbuf[5]);
71                         return 1;
72                 }
73         }
74         return 0;
75 }
76
77 void DLoader_Init(char *pathname)
78 {
79         void *fcn_handle;
80         char dl_error[256];
81         DIR *dir;
82         int i;
83         struct dirent *dptr;
84         char *(*h_init_fcn) (void);
85         char *dl_info;
86
87         char pathbuf[PATH_MAX];
88
89         if ((dir = opendir(pathname)) == NULL) {
90                 perror("opendir");
91                 exit(1);
92         }
93         while ((dptr = readdir(dir)) != NULL) {
94                 if (dptr->d_name[0] == '.')
95                         continue;
96
97                 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
98 #ifdef RTLD_NOW
99                 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
100 #else                           /* OpenBSD */
101                 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
102 #endif
103                 {
104                         safestrncpy(dl_error, dlerror(), sizeof dl_error);
105                         for (i=0; i<strlen(dl_error); ++i)
106                                 if (!isprint(dl_error[i]))
107                                         dl_error[i]='.';
108                         fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
109                                 dl_error);
110                         continue;
111                 }
112                 h_init_fcn = (char * (*)(void))
113 #ifndef __OpenBSD__
114                     dlsym(fcn_handle, "Dynamic_Module_Init");
115 #else
116                     dlsym(fcn_handle, "_Dynamic_Module_Init");
117 #endif
118
119                 if (dlerror() != NULL) {
120                         fprintf(stderr, "DLoader_Init dlsym failed\n");
121                         continue;
122                 }
123                 dl_info = h_init_fcn();
124
125                 lprintf(3, "Loaded module: %s\n", dl_info);
126         }       /* While */
127 }
128
129
130
131 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
132 {
133
134         struct LogFunctionHook *newfcn;
135
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;
142
143         lprintf(5, "Registered a new logging function\n");
144 }
145
146
147 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
148 {
149
150         struct CleanupFunctionHook *newfcn;
151
152         newfcn = (struct CleanupFunctionHook *)
153             mallok(sizeof(struct CleanupFunctionHook));
154         newfcn->next = CleanupHookTable;
155         newfcn->h_function_pointer = fcn_ptr;
156         CleanupHookTable = newfcn;
157
158         lprintf(5, "Registered a new cleanup function\n");
159 }
160
161
162 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
163 {
164
165         struct SessionFunctionHook *newfcn;
166
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;
173
174         lprintf(5, "Registered a new session function (type %d)\n",
175                 EventType);
176 }
177
178
179 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
180 {
181
182         struct UserFunctionHook *newfcn;
183
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;
190
191         lprintf(5, "Registered a new user function (type %d)\n",
192                 EventType);
193 }
194
195
196 void PerformSessionHooks(int EventType)
197 {
198         struct SessionFunctionHook *fcn;
199
200         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
201                 if (fcn->eventtype == EventType) {
202                         (*fcn->h_function_pointer) ();
203                 }
204         }
205 }
206
207 void PerformLogHooks(int loglevel, char *logmsg)
208 {
209         struct LogFunctionHook *fcn;
210
211         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
212                 if (fcn->loglevel >= loglevel) {
213                         (*fcn->h_function_pointer) (logmsg);
214                 }
215         }
216 }
217
218 void PerformUserHooks(char *username, long usernum, int EventType)
219 {
220         struct UserFunctionHook *fcn;
221
222         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
223                 if (fcn->eventtype == EventType) {
224                         (*fcn->h_function_pointer) (username, usernum);
225                 }
226         }
227 }