]> code.citadel.org Git - citadel.git/blobdiff - citadel/database.c
* Debugged all possible ways for a session to terminate; do them cleanly.
[citadel.git] / citadel / database.c
index d81726affdfe0b9645c38c36c42151dd2927cc7d..79ae1981344ccdd24c7b36b0f556370626e718ac 100644 (file)
@@ -1,9 +1,21 @@
 /*
  * This file contains a set of abstractions that allow Citadel to plug into any
  * record manager or database system for its data store.
+ *
+ * $Id$
  */
 
+/*
+ * Note that each call to a GDBM function is wrapped in an S_DATABASE critical
+ * section.  This is done because GDBM is not threadsafe.  This is the ONLY
+ * place in the entire Citadel server where any code enters two different
+ * classes of critical sections at the same time; this is why the GDBM calls
+ * are *tightly* wrapped in S_DATABASE.  Opening multiple concurrent critical
+ * sections elsewhere in the code can, and probably will, cause deadlock
+ * conditions to occur.  (Deadlock is bad.  Eliminate.)
+ */
 
+#include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
-#include <pthread.h>
+#ifdef HAVE_GDBM_H
 #include <gdbm.h>
+#endif
 #include "citadel.h"
 #include "server.h"
-#include "proto.h"
+#include "database.h"
+#include "sysdep_decls.h"
 
 
 /*
@@ -25,7 +39,8 @@ GDBM_FILE gdbms[MAXCDB];
 
 /*
  * We also keep these around, for sequential searches... (one per 
- * session.  Maybe there's a better way?)
+ * session.  Maybe there's a better way?)    FIX ... there _is_ a better
+ * way.  We have TSD functions now; use them.
  */
 #define MAXKEYS 256
 datum dtkey[MAXKEYS];
@@ -35,27 +50,40 @@ datum dtkey[MAXKEYS];
  * Reclaim unused space in the databases.  We need to do each one of
  * these discretely, rather than in a loop.
  */
-void defrag_databases() {
+void defrag_databases(void) {
 
        /* defrag the message base */
+       lprintf(7, "Defragmenting message base\n");
        begin_critical_section(S_MSGMAIN);
+       begin_critical_section(S_DATABASE);
        gdbm_reorganize(gdbms[CDB_MSGMAIN]);
+       end_critical_section(S_DATABASE);
        end_critical_section(S_MSGMAIN);
 
-       /* defrag the user file */
+       /* defrag the user file, mailboxes, and user/room relationships */
+       lprintf(7, "Defragmenting user file\n");
        begin_critical_section(S_USERSUPP);
+       begin_critical_section(S_DATABASE);
        gdbm_reorganize(gdbms[CDB_USERSUPP]);
+       gdbm_reorganize(gdbms[CDB_VISIT]);
+       end_critical_section(S_DATABASE);
        end_critical_section(S_USERSUPP);
 
-       /* defrag the room files */
+       /* defrag the room files and message lists */
+       lprintf(7, "Defragmenting room files and message lists\n");
        begin_critical_section(S_QUICKROOM);
+       begin_critical_section(S_DATABASE);
        gdbm_reorganize(gdbms[CDB_QUICKROOM]);
-       gdbm_reorganize(gdbms[CDB_FULLROOM]);
+       gdbm_reorganize(gdbms[CDB_MSGLISTS]);
+       end_critical_section(S_DATABASE);
        end_critical_section(S_QUICKROOM);
 
        /* defrag the floor table */
+       lprintf(7, "Defragmenting floor table\n");
        begin_critical_section(S_FLOORTAB);
+       begin_critical_section(S_DATABASE);
        gdbm_reorganize(gdbms[CDB_FLOORTAB]);
+       end_critical_section(S_DATABASE);
        end_critical_section(S_FLOORTAB);
        }
 
@@ -64,42 +92,70 @@ void defrag_databases() {
  * Open the various gdbm databases we'll be using.  Any database which
  * does not exist should be created.
  */
-void open_databases() {
+void open_databases(void) {
        int a;
 
-       gdbms[CDB_MSGMAIN] = gdbm_open("msgmain.gdbm", 8192,
-               GDBM_WRCREAT, 0700, NULL);
+       lprintf(7, "%s\n", gdbm_version);
+
+       /*
+        * Silently try to create the database subdirectory.  If it's
+        * already there, no problem.
+        */
+       system("exec mkdir data 2>/dev/null");
+
+       /* a critical section is unnecessary, as this function is called before
+          any other threads are created. and it causes problems on BSDI.
+
+       begin_critical_section(S_DATABASE);
+
+        */
+
+       gdbms[CDB_MSGMAIN] = gdbm_open("data/msgmain.gdbm", 8192,
+               GDBM_WRCREAT, 0600, NULL);
        if (gdbms[CDB_MSGMAIN] == NULL) {
                lprintf(2, "Cannot open msgmain: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
-       gdbms[CDB_USERSUPP] = gdbm_open("usersupp.gdbm", 0,
-               GDBM_WRCREAT, 0700, NULL);
+       gdbms[CDB_USERSUPP] = gdbm_open("data/usersupp.gdbm", 0,
+               GDBM_WRCREAT, 0600, NULL);
        if (gdbms[CDB_USERSUPP] == NULL) {
                lprintf(2, "Cannot open usersupp: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
-       gdbms[CDB_QUICKROOM] = gdbm_open("quickroom.gdbm", 0,
-               GDBM_WRCREAT, 0700, NULL);
-       if (gdbms[CDB_QUICKROOM] == NULL) {
-               lprintf(2, "Cannot open quickroom: %s\n",
+       gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
+               GDBM_WRCREAT, 0600, NULL);
+       if (gdbms[CDB_VISIT] == NULL) {
+               lprintf(2, "Cannot open visit file: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
-       gdbms[CDB_FULLROOM] = gdbm_open("fullroom.gdbm", 0,
-               GDBM_WRCREAT, 0700, NULL);
-       if (gdbms[CDB_FULLROOM] == NULL) {
-               lprintf(2, "Cannot open fullroom: %s\n",
+       gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
+               GDBM_WRCREAT, 0600, NULL);
+       if (gdbms[CDB_QUICKROOM] == NULL) {
+               lprintf(2, "Cannot open quickroom: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
-       gdbms[CDB_FLOORTAB] = gdbm_open("floortab.gdbm", 0,
-               GDBM_WRCREAT, 0700, NULL);
+       gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
+               GDBM_WRCREAT, 0600, NULL);
        if (gdbms[CDB_FLOORTAB] == NULL) {
                lprintf(2, "Cannot open floortab: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
+               }
+
+       gdbms[CDB_MSGLISTS] = gdbm_open("data/msglists.gdbm", 0,
+               GDBM_WRCREAT, 0600, NULL);
+       if (gdbms[CDB_MSGLISTS] == NULL) {
+               lprintf(2, "Cannot open msglists: %s\n",
+                       gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
        for (a=0; a<MAXKEYS; ++a) {
@@ -107,6 +163,9 @@ void open_databases() {
                dtkey[a].dptr = NULL;
                }
 
+       /*
+       end_critical_section(S_DATABASE);
+        */
 
        }
 
@@ -115,18 +174,19 @@ void open_databases() {
  * Close all of the gdbm database files we've opened.  This can be done
  * in a loop, since it's just a bunch of closes.
  */
-void close_databases() {
+void close_databases(void) {
        int a;
 
-       defrag_databases();
+       begin_critical_section(S_DATABASE);
        for (a=0; a<MAXCDB; ++a) {
                lprintf(7, "Closing database %d\n", a);
                gdbm_close(gdbms[a]);
                }
+       end_critical_section(S_DATABASE);
 
        for (a=0; a<MAXKEYS; ++a) {
                if (dtkey[a].dptr != NULL) {
-                       free(dtkey[a].dptr);
+                       phree(dtkey[a].dptr);
                        }
                }
 
@@ -138,17 +198,21 @@ void close_databases() {
  * datum already exists it should be overwritten.
  */
 int cdb_store(int cdb,
-               char *key, int keylen,
-               char *data, int datalen) {
+               void *key, int keylen,
+               void *data, int datalen) {
 
        datum dkey, ddata;
+       int retval;
 
        dkey.dsize = keylen;
        dkey.dptr = key;
        ddata.dsize = datalen;
        ddata.dptr = data;
 
-       if ( gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE) < 0 ) {
+       begin_critical_section(S_DATABASE);
+       retval = gdbm_store(gdbms[cdb], dkey, ddata, GDBM_REPLACE);
+       end_critical_section(S_DATABASE);
+       if ( retval < 0 ) {
                 lprintf(2, "gdbm error: %s\n", gdbm_strerror(gdbm_errno));
                 return(-1);
                }
@@ -160,14 +224,18 @@ int cdb_store(int cdb,
 /*
  * Delete a piece of data.  Returns 0 if the operation was successful.
  */
-int cdb_delete(int cdb, char *key, int keylen) {
+int cdb_delete(int cdb, void *key, int keylen) {
 
        datum dkey;
+       int retval;
 
        dkey.dsize = keylen;
        dkey.dptr = key;
 
-       return(gdbm_delete(gdbms[cdb], dkey));
+       begin_critical_section(S_DATABASE);
+       retval = gdbm_delete(gdbms[cdb], dkey);
+       end_critical_section(S_DATABASE);
+       return(retval);
 
        }
 
@@ -179,7 +247,7 @@ int cdb_delete(int cdb, char *key, int keylen) {
  * a struct cdbdata which it is the caller's responsibility to free later on
  * using the cdb_free() routine.
  */
-struct cdbdata *cdb_fetch(int cdb, char *key, int keylen) {
+struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
        
        struct cdbdata *tempcdb;
        datum dkey, dret;
@@ -187,12 +255,14 @@ struct cdbdata *cdb_fetch(int cdb, char *key, int keylen) {
        dkey.dsize = keylen;
        dkey.dptr = key;
 
+       begin_critical_section(S_DATABASE);
        dret = gdbm_fetch(gdbms[cdb], dkey);
+       end_critical_section(S_DATABASE);
        if (dret.dptr == NULL) {
                return NULL;
                }
 
-       tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
+       tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
        if (tempcdb == NULL) {
                lprintf(2, "Cannot allocate memory!\n");
                }
@@ -208,8 +278,8 @@ struct cdbdata *cdb_fetch(int cdb, char *key, int keylen) {
  * more complex stuff with other database managers in the future).
  */
 void cdb_free(struct cdbdata *cdb) {
-       free(cdb->ptr);
-       free(cdb);
+       phree(cdb->ptr);
+       phree(cdb);
        }
 
 
@@ -222,10 +292,12 @@ void cdb_free(struct cdbdata *cdb) {
 void cdb_rewind(int cdb) {
 
        if (dtkey[CC->cs_pid].dptr != NULL) {
-               free(dtkey[CC->cs_pid].dptr);
+               phree(dtkey[CC->cs_pid].dptr);
                }
 
+       begin_critical_section(S_DATABASE);
        dtkey[CC->cs_pid] = gdbm_firstkey(gdbms[cdb]);
+       end_critical_section(S_DATABASE);
        }
 
 
@@ -242,16 +314,20 @@ struct cdbdata *cdb_next_item(int cdb) {
                return NULL;
                }
 
+       begin_critical_section(S_DATABASE);
        dret = gdbm_fetch(gdbms[cdb], dtkey[CC->cs_pid]);
+       end_critical_section(S_DATABASE);
        if (dret.dptr == NULL) {        /* bad read */
-               free(dtkey[CC->cs_pid].dptr);
+               phree(dtkey[CC->cs_pid].dptr);
                return NULL;
                }
 
-       cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
+       cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
        cdbret->len = dret.dsize;
        cdbret->ptr = dret.dptr;
 
+       begin_critical_section(S_DATABASE);
        dtkey[CC->cs_pid] = gdbm_nextkey(gdbms[cdb], dtkey[CC->cs_pid]);
+       end_critical_section(S_DATABASE);
        return(cdbret);
        }