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.
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.)
32 #include "sysdep_decls.h"
36 * This array holds one gdbm handle for each Citadel database.
38 GDBM_FILE gdbms[MAXCDB];
41 * We also keep these around, for sequential searches... (one per
42 * session. Maybe there's a better way?)
49 * Reclaim unused space in the databases. We need to do each one of
50 * these discretely, rather than in a loop.
52 void defrag_databases(void) {
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);
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);
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);
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);
91 * Open the various gdbm databases we'll be using. Any database which
92 * does not exist should be created.
94 void open_databases(void) {
97 lprintf(7, "%s\n", gdbm_version);
100 * Silently try to create the database subdirectory. If it's
101 * already there, no problem.
103 system("exec mkdir data 2>/dev/null");
105 /* a critical section is unnecessary, as this function is called before
106 any other threads are created. and it causes problems on BSDI.
108 begin_critical_section(S_DATABASE);
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));
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));
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));
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));
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));
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));
160 for (a=0; a<MAXKEYS; ++a) {
162 dtkey[a].dptr = NULL;
166 end_critical_section(S_DATABASE);
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.
176 void close_databases(void) {
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]);
184 end_critical_section(S_DATABASE);
186 for (a=0; a<MAXKEYS; ++a) {
187 if (dtkey[a].dptr != NULL) {
188 phree(dtkey[a].dptr);
196 * Store a piece of data. Returns 0 if the operation was successful. If a
197 * datum already exists it should be overwritten.
199 int cdb_store(int cdb,
200 void *key, int keylen,
201 void *data, int datalen) {
208 ddata.dsize = datalen;
211 begin_critical_section(S_DATABASE);
212 retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
213 end_critical_section(S_DATABASE);
215 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
224 * Delete a piece of data. Returns 0 if the operation was successful.
226 int cdb_delete(int cdb, void *key, int keylen) {
234 begin_critical_section(S_DATABASE);
235 retval = gdbm_delete(gdbms[cdb], dkey);
236 end_critical_section(S_DATABASE);
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.
249 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
251 struct cdbdata *tempcdb;
257 begin_critical_section(S_DATABASE);
258 dret = gdbm_fetch(gdbms[cdb], dkey);
259 end_critical_section(S_DATABASE);
260 if (dret.dptr == NULL) {
264 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
265 if (tempcdb == NULL) {
266 lprintf(2, "Cannot allocate memory!\n");
269 tempcdb->len = dret.dsize;
270 tempcdb->ptr = dret.dptr;
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).
279 void cdb_free(struct cdbdata *cdb) {
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.)
291 void cdb_rewind(int cdb) {
293 if (dtkey[CC->cs_pid].dptr != NULL) {
294 phree(dtkey[CC->cs_pid].dptr);
297 begin_critical_section(S_DATABASE);
298 dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
299 end_critical_section(S_DATABASE);
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.
307 struct cdbdata *cdb_next_item(int cdb) {
309 struct cdbdata *cdbret;
312 if (dtkey[CC->cs_pid].dptr == NULL) { /* end of file */
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);
324 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
325 cdbret->len = dret.dsize;
326 cdbret->ptr = dret.dptr;
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);