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.)
35 #include "sysdep_decls.h"
39 * This array holds one gdbm handle for each Citadel database.
41 GDBM_FILE gdbms[MAXCDB];
44 * We also keep these around, for sequential searches... (one per
45 * session. Maybe there's a better way?)
52 * Reclaim unused space in the databases. We need to do each one of
53 * these discretely, rather than in a loop.
55 void defrag_databases(void) {
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);
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);
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);
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);
94 * Open the various gdbm databases we'll be using. Any database which
95 * does not exist should be created.
97 void open_databases(void) {
101 * Silently try to create the database subdirectory. If it's
102 * already there, no problem.
104 system("exec mkdir data 2>/dev/null");
106 /* a critical section is unnecessary, as this function is called before
107 any other threads are created. and it causes problems on BSDI.
109 begin_critical_section(S_DATABASE);
113 gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
114 GDBM_WRCREAT, 0600, NULL);
115 if (gdbms[CDB_MSGMAIN] == NULL) {
116 lprintf(2, "Cannot open msgmain: %s\n",
117 gdbm_strerror(gdbm_errno));
121 gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
122 GDBM_WRCREAT, 0600, NULL);
123 if (gdbms[CDB_USERSUPP] == NULL) {
124 lprintf(2, "Cannot open usersupp: %s\n",
125 gdbm_strerror(gdbm_errno));
129 gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
130 GDBM_WRCREAT, 0600, NULL);
131 if (gdbms[CDB_VISIT] == NULL) {
132 lprintf(2, "Cannot open visit file: %s\n",
133 gdbm_strerror(gdbm_errno));
137 gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
138 GDBM_WRCREAT, 0600, NULL);
139 if (gdbms[CDB_QUICKROOM] == NULL) {
140 lprintf(2, "Cannot open quickroom: %s\n",
141 gdbm_strerror(gdbm_errno));
145 gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
146 GDBM_WRCREAT, 0600, NULL);
147 if (gdbms[CDB_FLOORTAB] == NULL) {
148 lprintf(2, "Cannot open floortab: %s\n",
149 gdbm_strerror(gdbm_errno));
153 gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
154 GDBM_WRCREAT, 0600, NULL);
155 if (gdbms[CDB_MSGLISTS] == NULL) {
156 lprintf(2, "Cannot open msglists: %s\n",
157 gdbm_strerror(gdbm_errno));
161 for (a=0; a<MAXKEYS; ++a) {
163 dtkey[a].dptr = NULL;
167 end_critical_section(S_DATABASE);
174 * Close all of the gdbm database files we've opened. This can be done
175 * in a loop, since it's just a bunch of closes.
177 void close_databases(void) {
180 /* Hmm... we should decide when would be a good time to defrag.
181 * Server shutdowns might be an opportune time.
185 begin_critical_section(S_DATABASE);
186 for (a=0; a<MAXCDB; ++a) {
187 lprintf(7, "Closing database %d\n", a);
188 gdbm_close(gdbms[a]);
190 end_critical_section(S_DATABASE);
192 for (a=0; a<MAXKEYS; ++a) {
193 if (dtkey[a].dptr != NULL) {
194 phree(dtkey[a].dptr);
202 * Store a piece of data. Returns 0 if the operation was successful. If a
203 * datum already exists it should be overwritten.
205 int cdb_store(int cdb,
206 void *key, int keylen,
207 void *data, int datalen) {
214 ddata.dsize = datalen;
217 begin_critical_section(S_DATABASE);
218 retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
219 end_critical_section(S_DATABASE);
221 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
230 * Delete a piece of data. Returns 0 if the operation was successful.
232 int cdb_delete(int cdb, void *key, int keylen) {
240 begin_critical_section(S_DATABASE);
241 retval = gdbm_delete(gdbms[cdb], dkey);
242 end_critical_section(S_DATABASE);
251 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
252 * a struct cdbdata which it is the caller's responsibility to free later on
253 * using the cdb_free() routine.
255 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
257 struct cdbdata *tempcdb;
263 begin_critical_section(S_DATABASE);
264 dret = gdbm_fetch(gdbms[cdb], dkey);
265 end_critical_section(S_DATABASE);
266 if (dret.dptr == NULL) {
270 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
271 if (tempcdb == NULL) {
272 lprintf(2, "Cannot allocate memory!\n");
275 tempcdb->len = dret.dsize;
276 tempcdb->ptr = dret.dptr;
282 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
283 * more complex stuff with other database managers in the future).
285 void cdb_free(struct cdbdata *cdb) {
292 * Prepare for a sequential search of an entire database. (In the gdbm model,
293 * we do this by keeping an array dtkey[] of "the next" key for each session
294 * that is open. There is guaranteed to be no more than one traversal in
295 * progress per session at any given time.)
297 void cdb_rewind(int cdb) {
299 if (dtkey[CC->cs_pid].dptr != NULL) {
300 phree(dtkey[CC->cs_pid].dptr);
303 begin_critical_section(S_DATABASE);
304 dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
305 end_critical_section(S_DATABASE);
310 * Fetch the next item in a sequential search. Returns a pointer to a
311 * cdbdata structure, or NULL if we've hit the end.
313 struct cdbdata *cdb_next_item(int cdb) {
315 struct cdbdata *cdbret;
318 if (dtkey[CC->cs_pid].dptr == NULL) { /* end of file */
322 begin_critical_section(S_DATABASE);
323 dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
324 end_critical_section(S_DATABASE);
325 if (dret.dptr == NULL) { /* bad read */
326 phree(dtkey[CC->cs_pid].dptr);
330 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
331 cdbret->len = dret.dsize;
332 cdbret->ptr = dret.dptr;
334 begin_critical_section(S_DATABASE);
335 dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
336 end_critical_section(S_DATABASE);