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 begin_critical_section(S_MSGMAIN);
59 begin_critical_section(S_DATABASE);
60 gdbm_reorganize(gdbms[CDB_MSGMAIN]);
61 end_critical_section(S_DATABASE);
62 end_critical_section(S_MSGMAIN);
64 /* defrag the user file, mailboxes, and user/room relationships */
65 begin_critical_section(S_USERSUPP);
66 begin_critical_section(S_DATABASE);
67 gdbm_reorganize(gdbms[CDB_USERSUPP]);
68 gdbm_reorganize(gdbms[CDB_VISIT]);
69 end_critical_section(S_DATABASE);
70 end_critical_section(S_USERSUPP);
72 /* defrag the room files and message lists */
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 begin_critical_section(S_FLOORTAB);
82 begin_critical_section(S_DATABASE);
83 gdbm_reorganize(gdbms[CDB_FLOORTAB]);
84 end_critical_section(S_DATABASE);
85 end_critical_section(S_FLOORTAB);
90 * Open the various gdbm databases we'll be using. Any database which
91 * does not exist should be created.
93 void open_databases(void) {
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 begin_critical_section(S_DATABASE);
104 gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
105 GDBM_WRCREAT, 0600, NULL);
106 if (gdbms[CDB_MSGMAIN] == NULL) {
107 lprintf(2, "Cannot open msgmain: %s\n",
108 gdbm_strerror(gdbm_errno));
111 gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
112 GDBM_WRCREAT, 0600, NULL);
113 if (gdbms[CDB_USERSUPP] == NULL) {
114 lprintf(2, "Cannot open usersupp: %s\n",
115 gdbm_strerror(gdbm_errno));
118 gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
119 GDBM_WRCREAT, 0600, NULL);
120 if (gdbms[CDB_VISIT] == NULL) {
121 lprintf(2, "Cannot open visit file: %s\n",
122 gdbm_strerror(gdbm_errno));
125 gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
126 GDBM_WRCREAT, 0600, NULL);
127 if (gdbms[CDB_QUICKROOM] == NULL) {
128 lprintf(2, "Cannot open quickroom: %s\n",
129 gdbm_strerror(gdbm_errno));
132 gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
133 GDBM_WRCREAT, 0600, NULL);
134 if (gdbms[CDB_FLOORTAB] == NULL) {
135 lprintf(2, "Cannot open floortab: %s\n",
136 gdbm_strerror(gdbm_errno));
139 gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
140 GDBM_WRCREAT, 0600, NULL);
141 if (gdbms[CDB_MSGLISTS] == NULL) {
142 lprintf(2, "Cannot open msglists: %s\n",
143 gdbm_strerror(gdbm_errno));
146 for (a=0; a<MAXKEYS; ++a) {
148 dtkey[a].dptr = NULL;
151 end_critical_section(S_DATABASE);
157 * Close all of the gdbm database files we've opened. This can be done
158 * in a loop, since it's just a bunch of closes.
160 void close_databases(void) {
163 /* Hmm... we should decide when would be a good time to defrag.
164 * Server shutdowns might be an opportune time.
168 begin_critical_section(S_DATABASE);
169 for (a=0; a<MAXCDB; ++a) {
170 lprintf(7, "Closing database %d\n", a);
171 gdbm_close(gdbms[a]);
173 end_critical_section(S_DATABASE);
175 for (a=0; a<MAXKEYS; ++a) {
176 if (dtkey[a].dptr != NULL) {
177 phree(dtkey[a].dptr);
185 * Store a piece of data. Returns 0 if the operation was successful. If a
186 * datum already exists it should be overwritten.
188 int cdb_store(int cdb,
189 void *key, int keylen,
190 void *data, int datalen) {
197 ddata.dsize = datalen;
200 begin_critical_section(S_DATABASE);
201 retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
202 end_critical_section(S_DATABASE);
204 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
213 * Delete a piece of data. Returns 0 if the operation was successful.
215 int cdb_delete(int cdb, void *key, int keylen) {
223 begin_critical_section(S_DATABASE);
224 retval = gdbm_delete(gdbms[cdb], dkey);
225 end_critical_section(S_DATABASE);
234 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
235 * a struct cdbdata which it is the caller's responsibility to free later on
236 * using the cdb_free() routine.
238 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) {
253 tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
254 if (tempcdb == NULL) {
255 lprintf(2, "Cannot allocate memory!\n");
258 tempcdb->len = dret.dsize;
259 tempcdb->ptr = dret.dptr;
265 * Free a cdbdata item (ok, this is really no big deal, but we might need to do
266 * more complex stuff with other database managers in the future).
268 void cdb_free(struct cdbdata *cdb) {
275 * Prepare for a sequential search of an entire database. (In the gdbm model,
276 * we do this by keeping an array dtkey[] of "the next" key for each session
277 * that is open. There is guaranteed to be no more than one traversal in
278 * progress per session at any given time.)
280 void cdb_rewind(int cdb) {
282 if (dtkey[CC->cs_pid].dptr != NULL) {
283 phree(dtkey[CC->cs_pid].dptr);
286 begin_critical_section(S_DATABASE);
287 dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
288 end_critical_section(S_DATABASE);
293 * Fetch the next item in a sequential search. Returns a pointer to a
294 * cdbdata structure, or NULL if we've hit the end.
296 struct cdbdata *cdb_next_item(int cdb) {
298 struct cdbdata *cdbret;
301 if (dtkey[CC->cs_pid].dptr == NULL) { /* end of file */
305 begin_critical_section(S_DATABASE);
306 dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
307 end_critical_section(S_DATABASE);
308 if (dret.dptr == NULL) { /* bad read */
309 phree(dtkey[CC->cs_pid].dptr);
313 cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
314 cdbret->len = dret.dsize;
315 cdbret->ptr = dret.dptr;
317 begin_critical_section(S_DATABASE);
318 dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
319 end_critical_section(S_DATABASE);