* database.c: isplay the GDBM version string on startup
[citadel.git] / citadel / database.c
1 /*
2  * This file contains a set of abstractions that allow Citadel to plug into any
3  * record manager or database system for its data store.
4  *
5  * $Id$
6  */
7
8 /*
9  * Note that each call to a GDBM function is wrapped in an S_DATABASE critical
10  * section.  This is done because GDBM is not threadsafe.  This is the ONLY
11  * place in the entire Citadel server where any code enters two different
12  * classes of critical sections at the same time; this is why the GDBM calls
13  * are *tightly* wrapped in S_DATABASE.  Opening multiple concurrent critical
14  * sections elsewhere in the code can, and probably will, cause deadlock
15  * conditions to occur.  (Deadlock is bad.  Eliminate.)
16  */
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <errno.h>
26 #ifdef HAVE_PTHREAD_H
27 #include <pthread.h>
28 #endif
29 #ifdef HAVE_GDBM_H
30 #include <gdbm.h>
31 #endif
32 #include "citadel.h"
33 #include "server.h"
34 #include "database.h"
35 #include "sysdep_decls.h"
36
37
38 /*
39  * This array holds one gdbm handle for each Citadel database.
40  */
41 GDBM_FILE gdbms[MAXCDB];
42
43 /*
44  * We also keep these around, for sequential searches... (one per 
45  * session.  Maybe there's a better way?)
46  */
47 #define MAXKEYS 256
48 datum dtkey[MAXKEYS];
49
50
51 /*
52  * Reclaim unused space in the databases.  We need to do each one of
53  * these discretely, rather than in a loop.
54  */
55 void defrag_databases(void) {
56
57         /* defrag the message base */
58         lprintf(7, "Defragmenting message base\n");
59         begin_critical_section(S_MSGMAIN);
60         begin_critical_section(S_DATABASE);
61         gdbm_reorganize(gdbms[CDB_MSGMAIN]);
62         end_critical_section(S_DATABASE);
63         end_critical_section(S_MSGMAIN);
64
65         /* defrag the user file, mailboxes, and user/room relationships */
66         lprintf(7, "Defragmenting user file\n");
67         begin_critical_section(S_USERSUPP);
68         begin_critical_section(S_DATABASE);
69         gdbm_reorganize(gdbms[CDB_USERSUPP]);
70         gdbm_reorganize(gdbms[CDB_VISIT]);
71         end_critical_section(S_DATABASE);
72         end_critical_section(S_USERSUPP);
73
74         /* defrag the room files and message lists */
75         lprintf(7, "Defragmenting room files and message lists\n");
76         begin_critical_section(S_QUICKROOM);
77         begin_critical_section(S_DATABASE);
78         gdbm_reorganize(gdbms[CDB_QUICKROOM]);
79         gdbm_reorganize(gdbms[CDB_MSGLISTS]);
80         end_critical_section(S_DATABASE);
81         end_critical_section(S_QUICKROOM);
82
83         /* defrag the floor table */
84         lprintf(7, "Defragmenting floor table\n");
85         begin_critical_section(S_FLOORTAB);
86         begin_critical_section(S_DATABASE);
87         gdbm_reorganize(gdbms[CDB_FLOORTAB]);
88         end_critical_section(S_DATABASE);
89         end_critical_section(S_FLOORTAB);
90         }
91
92
93 /*
94  * Open the various gdbm databases we'll be using.  Any database which
95  * does not exist should be created.
96  */
97 void open_databases(void) {
98         int a;
99
100         lprintf(7, "%s\n", gdbm_version);
101
102         /*
103          * Silently try to create the database subdirectory.  If it's
104          * already there, no problem.
105          */
106         system("exec mkdir data 2>/dev/null");
107
108         /* a critical section is unnecessary, as this function is called before
109            any other threads are created. and it causes problems on BSDI.
110
111         begin_critical_section(S_DATABASE);
112
113          */
114
115         gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
116                 GDBM_WRCREAT, 0600, NULL);
117         if (gdbms[CDB_MSGMAIN] == NULL) {
118                 lprintf(2, "Cannot open msgmain: %s\n",
119                         gdbm_strerror(gdbm_errno));
120                 exit(1);
121                 }
122
123         gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
124                 GDBM_WRCREAT, 0600, NULL);
125         if (gdbms[CDB_USERSUPP] == NULL) {
126                 lprintf(2, "Cannot open usersupp: %s\n",
127                         gdbm_strerror(gdbm_errno));
128                 exit(1);
129                 }
130
131         gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
132                 GDBM_WRCREAT, 0600, NULL);
133         if (gdbms[CDB_VISIT] == NULL) {
134                 lprintf(2, "Cannot open visit file: %s\n",
135                         gdbm_strerror(gdbm_errno));
136                 exit(1);
137                 }
138
139         gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
140                 GDBM_WRCREAT, 0600, NULL);
141         if (gdbms[CDB_QUICKROOM] == NULL) {
142                 lprintf(2, "Cannot open quickroom: %s\n",
143                         gdbm_strerror(gdbm_errno));
144                 exit(1);
145                 }
146
147         gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
148                 GDBM_WRCREAT, 0600, NULL);
149         if (gdbms[CDB_FLOORTAB] == NULL) {
150                 lprintf(2, "Cannot open floortab: %s\n",
151                         gdbm_strerror(gdbm_errno));
152                 exit(1);
153                 }
154
155         gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
156                 GDBM_WRCREAT, 0600, NULL);
157         if (gdbms[CDB_MSGLISTS] == NULL) {
158                 lprintf(2, "Cannot open msglists: %s\n",
159                         gdbm_strerror(gdbm_errno));
160                 exit(1);
161                 }
162
163         for (a=0; a<MAXKEYS; ++a) {
164                 dtkey[a].dsize = 0;
165                 dtkey[a].dptr = NULL;
166                 }
167
168         /*
169         end_critical_section(S_DATABASE);
170          */
171
172         }
173
174
175 /*
176  * Close all of the gdbm database files we've opened.  This can be done
177  * in a loop, since it's just a bunch of closes.
178  */
179 void close_databases(void) {
180         int a;
181
182         begin_critical_section(S_DATABASE);
183         for (a=0; a<MAXCDB; ++a) {
184                 lprintf(7, "Closing database %d\n", a);
185                 gdbm_close(gdbms[a]);
186                 }
187         end_critical_section(S_DATABASE);
188
189         for (a=0; a<MAXKEYS; ++a) {
190                 if (dtkey[a].dptr != NULL) {
191                         phree(dtkey[a].dptr);
192                         }
193                 }
194
195         }
196
197
198 /*
199  * Store a piece of data.  Returns 0 if the operation was successful.  If a
200  * datum already exists it should be overwritten.
201  */
202 int cdb_store(int cdb,
203                 void *key, int keylen,
204                 void *data, int datalen) {
205
206         datum dkey, ddata;
207         int retval;
208
209         dkey.dsize = keylen;
210         dkey.dptr = key;
211         ddata.dsize = datalen;
212         ddata.dptr = data;
213
214         begin_critical_section(S_DATABASE);
215         retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
216         end_critical_section(S_DATABASE);
217         if ( retval < 0 ) {
218                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
219                 return(-1);
220                 }
221
222         return(0);
223         }
224
225
226 /*
227  * Delete a piece of data.  Returns 0 if the operation was successful.
228  */
229 int cdb_delete(int cdb, void *key, int keylen) {
230
231         datum dkey;
232         int retval;
233
234         dkey.dsize = keylen;
235         dkey.dptr = key;
236
237         begin_critical_section(S_DATABASE);
238         retval = gdbm_delete(gdbms[cdb], dkey);
239         end_critical_section(S_DATABASE);
240         return(retval);
241
242         }
243
244
245
246
247 /*
248  * Fetch a piece of data.  If not found, returns NULL.  Otherwise, it returns
249  * a struct cdbdata which it is the caller's responsibility to free later on
250  * using the cdb_free() routine.
251  */
252 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
253         
254         struct cdbdata *tempcdb;
255         datum dkey, dret;
256         
257         dkey.dsize = keylen;
258         dkey.dptr = key;
259
260         begin_critical_section(S_DATABASE);
261         dret = gdbm_fetch(gdbms[cdb], dkey);
262         end_critical_section(S_DATABASE);
263         if (dret.dptr == NULL) {
264                 return NULL;
265                 }
266
267         tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
268         if (tempcdb == NULL) {
269                 lprintf(2, "Cannot allocate memory!\n");
270                 }
271
272         tempcdb->len = dret.dsize;
273         tempcdb->ptr = dret.dptr;
274         return(tempcdb);
275         }
276
277
278 /*
279  * Free a cdbdata item (ok, this is really no big deal, but we might need to do
280  * more complex stuff with other database managers in the future).
281  */
282 void cdb_free(struct cdbdata *cdb) {
283         phree(cdb->ptr);
284         phree(cdb);
285         }
286
287
288 /* 
289  * Prepare for a sequential search of an entire database.  (In the gdbm model,
290  * we do this by keeping an array dtkey[] of "the next" key for each session
291  * that is open.  There is guaranteed to be no more than one traversal in
292  * progress per session at any given time.)
293  */
294 void cdb_rewind(int cdb) {
295
296         if (dtkey[CC->cs_pid].dptr != NULL) {
297                 phree(dtkey[CC->cs_pid].dptr);
298                 }
299
300         begin_critical_section(S_DATABASE);
301         dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
302         end_critical_section(S_DATABASE);
303         }
304
305
306 /*
307  * Fetch the next item in a sequential search.  Returns a pointer to a 
308  * cdbdata structure, or NULL if we've hit the end.
309  */
310 struct cdbdata *cdb_next_item(int cdb) {
311         datum dret;
312         struct cdbdata *cdbret;
313
314         
315         if (dtkey[CC->cs_pid].dptr == NULL) {   /* end of file */
316                 return NULL;
317                 }
318
319         begin_critical_section(S_DATABASE);
320         dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
321         end_critical_section(S_DATABASE);
322         if (dret.dptr == NULL) {        /* bad read */
323                 phree(dtkey[CC->cs_pid].dptr);
324                 return NULL;
325                 }
326
327         cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
328         cdbret->len = dret.dsize;
329         cdbret->ptr = dret.dptr;
330
331         begin_critical_section(S_DATABASE);
332         dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
333         end_critical_section(S_DATABASE);
334         return(cdbret);
335         }