]> code.citadel.org Git - citadel.git/blobdiff - citadel/database.c
* added server command line option "-f" to defrag databases on startup
[citadel.git] / citadel / database.c
index 98da2e38c536189c9b6a09710632e11d2f890e55..d14ab2bc036095dd3ba5889822c8a27133861a61 100644 (file)
@@ -5,7 +5,17 @@
  * $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>
+#ifdef HAVE_PTHREAD_H
 #include <pthread.h>
+#endif
+#ifdef HAVE_GDBM_H
 #include <gdbm.h>
+#endif
 #include "citadel.h"
 #include "server.h"
 #include "database.h"
@@ -41,6 +55,7 @@ datum dtkey[MAXKEYS];
 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]);
@@ -48,6 +63,7 @@ void defrag_databases(void) {
        end_critical_section(S_MSGMAIN);
 
        /* 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]);
@@ -56,6 +72,7 @@ void defrag_databases(void) {
        end_critical_section(S_USERSUPP);
 
        /* 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]);
@@ -64,6 +81,7 @@ void defrag_databases(void) {
        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]);
@@ -85,13 +103,19 @@ void open_databases(void) {
         */
        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("data/usersupp.gdbm", 0,
@@ -99,6 +123,7 @@ void open_databases(void) {
        if (gdbms[CDB_USERSUPP] == NULL) {
                lprintf(2, "Cannot open usersupp: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
        gdbms[CDB_VISIT] = gdbm_open("data/visit.gdbm", 0,
@@ -106,6 +131,7 @@ void open_databases(void) {
        if (gdbms[CDB_VISIT] == NULL) {
                lprintf(2, "Cannot open visit file: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
        gdbms[CDB_QUICKROOM] = gdbm_open("data/quickroom.gdbm", 0,
@@ -113,6 +139,7 @@ void open_databases(void) {
        if (gdbms[CDB_QUICKROOM] == NULL) {
                lprintf(2, "Cannot open quickroom: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
        gdbms[CDB_FLOORTAB] = gdbm_open("data/floortab.gdbm", 0,
@@ -120,6 +147,7 @@ void open_databases(void) {
        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,
@@ -127,6 +155,7 @@ void open_databases(void) {
        if (gdbms[CDB_MSGLISTS] == NULL) {
                lprintf(2, "Cannot open msglists: %s\n",
                        gdbm_strerror(gdbm_errno));
+               exit(1);
                }
 
        for (a=0; a<MAXKEYS; ++a) {
@@ -134,7 +163,9 @@ void open_databases(void) {
                dtkey[a].dptr = NULL;
                }
 
+       /*
        end_critical_section(S_DATABASE);
+        */
 
        }
 
@@ -146,11 +177,6 @@ void open_databases(void) {
 void close_databases(void) {
        int a;
 
-       /* Hmm... we should decide when would be a good time to defrag.
-        * Server shutdowns might be an opportune time.
-        */
-       defrag_databases();
-
        begin_critical_section(S_DATABASE);
        for (a=0; a<MAXCDB; ++a) {
                lprintf(7, "Closing database %d\n", a);
@@ -160,7 +186,7 @@ void close_databases(void) {
 
        for (a=0; a<MAXKEYS; ++a) {
                if (dtkey[a].dptr != NULL) {
-                       free(dtkey[a].dptr);
+                       phree(dtkey[a].dptr);
                        }
                }
 
@@ -236,7 +262,7 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen) {
                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");
                }
@@ -252,8 +278,8 @@ struct cdbdata *cdb_fetch(int cdb, void *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);
        }
 
 
@@ -266,7 +292,7 @@ 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);
@@ -292,11 +318,11 @@ struct cdbdata *cdb_next_item(int cdb) {
        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;