4 * GDBM database driver for Citadel/UX
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.)
30 #include "sysdep_decls.h"
34 * This array holds one gdbm handle for each Citadel database.
36 GDBM_FILE gdbms[MAXCDB];
39 * We also keep these around, for sequential searches (one per session slot)
46 * Reclaim unused space in the databases. We need to do each one of
47 * these discretely, rather than in a loop.
49 void defrag_databases(void)
52 /* defrag the message base */
53 lprintf(7, "Defragmenting message base\n");
54 begin_critical_section(S_MSGMAIN);
55 begin_critical_section(S_DATABASE);
56 gdbm_reorganize(gdbms[CDB_MSGMAIN]);
57 end_critical_section(S_DATABASE);
58 end_critical_section(S_MSGMAIN);
60 /* defrag the user file, mailboxes, and user/room relationships */
61 lprintf(7, "Defragmenting user file\n");
62 begin_critical_section(S_USERSUPP);
63 begin_critical_section(S_DATABASE);
64 gdbm_reorganize(gdbms[CDB_USERSUPP]);
65 gdbm_reorganize(gdbms[CDB_VISIT]);
66 end_critical_section(S_DATABASE);
67 end_critical_section(S_USERSUPP);
69 /* defrag the room files and message lists */
70 lprintf(7, "Defragmenting room files and message lists\n");
71 begin_critical_section(S_QUICKROOM);
72 begin_critical_section(S_DATABASE);
73 gdbm_reorganize(gdbms[CDB_QUICKROOM]);
74 gdbm_reorganize(gdbms[CDB_MSGLISTS]);
75 end_critical_section(S_DATABASE);
76 end_critical_section(S_QUICKROOM);
78 /* defrag the floor table */
79 lprintf(7, "Defragmenting floor table\n");
80 begin_critical_section(S_FLOORTAB);
81 begin_critical_section(S_DATABASE);
82 gdbm_reorganize(gdbms[CDB_FLOORTAB]);
83 end_critical_section(S_DATABASE);
84 end_critical_section(S_FLOORTAB);
89 * Open the various gdbm databases we'll be using. Any database which
90 * does not exist should be created.
92 void open_databases(void)
94 lprintf(7, "%s\n", gdbm_version);
97 * Silently try to create the database subdirectory. If it's
98 * already there, no problem.
100 system("exec mkdir data 2>/dev/null");
102 /* a critical section is unnecessary, as this function is called before
103 any other threads are created. and it causes problems on BSDI.
105 begin_critical_section(S_DATABASE);
109 gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
110 GDBM_WRCREAT, 0600, NULL);
111 if (gdbms[CDB_MSGMAIN] == NULL) {
112 lprintf(2, "Cannot open msgmain: %s\n",
113 gdbm_strerror(gdbm_errno));
116 gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
117 GDBM_WRCREAT, 0600, NULL);
118 if (gdbms[CDB_USERSUPP] == NULL) {
119 lprintf(2, "Cannot open usersupp: %s\n",
120 gdbm_strerror(gdbm_errno));
123 gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
124 GDBM_WRCREAT, 0600, NULL);
125 if (gdbms[CDB_VISIT] == NULL) {
126 lprintf(2, "Cannot open visit file: %s\n",
127 gdbm_strerror(gdbm_errno));
130 gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
131 GDBM_WRCREAT, 0600, NULL);
132 if (gdbms[CDB_QUICKROOM] == NULL) {
133 lprintf(2, "Cannot open quickroom: %s\n",
134 gdbm_strerror(gdbm_errno));
137 gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
138 GDBM_WRCREAT, 0600, NULL);
139 if (gdbms[CDB_FLOORTAB] == NULL) {
140 lprintf(2, "Cannot open floortab: %s\n",
141 gdbm_strerror(gdbm_errno));
144 gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
145 GDBM_WRCREAT, 0600, NULL);
146 if (gdbms[CDB_MSGLISTS] == NULL) {
147 lprintf(2, "Cannot open msglists: %s\n",
148 gdbm_strerror(gdbm_errno));
152 end_critical_section(S_DATABASE);
159 * Close all of the gdbm database files we've opened. This can be done
160 * in a loop, since it's just a bunch of closes.
162 void close_databases(void)
166 begin_critical_section(S_DATABASE);
167 for (a = 0; a < MAXCDB; ++a) {
168 lprintf(7, "Closing database %d\n", a);
169 gdbm_close(gdbms[a]);
171 end_critical_section(S_DATABASE);
173 for (a = 0; a < max_keys; ++a) {
174 if (dtkey[a].dptr != NULL) {
175 phree(dtkey[a].dptr);
183 * Store a piece of data. Returns 0 if the operation was successful. If a
184 * datum already exists it should be overwritten.
186 int cdb_store(int cdb,
187 void *key, int keylen,
188 void *data, int datalen)
196 ddata.dsize = datalen;
199 begin_critical_section(S_DATABASE);
200 retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
201 end_critical_section(S_DATABASE);
203 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
211 * Delete a piece of data. Returns 0 if the operation was successful.
213 int cdb_delete(int cdb, void *key, int keylen)
222 begin_critical_section(S_DATABASE);
223 retval = gdbm_delete(gdbms[cdb], dkey);
224 end_critical_section(S_DATABASE);
233 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
234 * a struct cdbdata which it is the caller's responsibility to free later on
235 * using the cdb_free() routine.
237 struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
240 struct cdbdata *tempcdb;
246 begin_critical_section(S_DATABASE);
247 dret = gdbm_fetch(gdbms[cdb], dkey);
248 end_critical_section(S_DATABASE);
249 if (dret.dptr == NULL) {
252 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
253 if (tempcdb == NULL) {
254 lprintf(2, "Cannot allocate memory!\n");
256 tempcdb->len = dret.dsize;
257 tempcdb->ptr = dret.dptr;
263 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
264 * more complex stuff with other database managers in the future).
266 void cdb_free(struct cdbdata *cdb)
274 * Prepare for a sequential search of an entire database. (In the gdbm model,
275 * we do this by keeping an array dtkey[] of "the next" key for each session
276 * that is open. There is guaranteed to be no more than one traversal in
277 * progress per session at any given time.)
279 void cdb_rewind(int cdb)
282 while (max_keys <= CC->cs_pid) {
286 mallok((sizeof(datum) * max_keys));
289 reallok(dtkey, (sizeof(datum) * max_keys));
291 dtkey[max_keys - 1].dsize = 0;
292 dtkey[max_keys - 1].dptr = NULL;
295 if (dtkey[CC->cs_pid].dptr != NULL) {
296 phree(dtkey[CC->cs_pid].dptr);
298 begin_critical_section(S_DATABASE);
299 dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
300 end_critical_section(S_DATABASE);
305 * Fetch the next item in a sequential search. Returns a pointer to a
306 * cdbdata structure, or NULL if we've hit the end.
308 struct cdbdata *cdb_next_item(int cdb)
311 struct cdbdata *cdbret;
315 if (dtkey[CC->cs_pid].dptr == NULL) { /* end of file */
318 begin_critical_section(S_DATABASE);
319 dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
320 end_critical_section(S_DATABASE);
321 if (dret.dptr == NULL) { /* bad read */
322 phree(dtkey[CC->cs_pid].dptr);
325 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
326 cdbret->len = dret.dsize;
327 cdbret->ptr = dret.dptr;
329 ptr = dtkey[CC->cs_pid].dptr;
330 begin_critical_section(S_DATABASE);
331 dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
332 end_critical_section(S_DATABASE);
334 if (ptr != NULL) { /* Free the previous key. */
344 * GDBM doesn't support transaction-based logging. Stub out these functions.
347 void cdb_begin_transaction(void) {
350 void cdb_end_transaction(void) {