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