]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
* Variable names, comments, documentation, etc... removed the acronym 'BBS'
[citadel.git] / citadel / database_sleepycat.c
index 3a5a04ee8822ab2ad12af4db956e800967484df8..473071c340e84ab1f3505fa3e04498978c612485 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * $Id$
  *
- * Sleepycat (Berkeley) DB driver for Citadel/UX
+ * Sleepycat (Berkeley) DB driver for Citadel
  *
  */
 
@@ -55,6 +55,7 @@
 #include "database.h"
 #include "msgbase.h"
 #include "sysdep_decls.h"
+#include "config.h"
 
 static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
@@ -109,6 +110,11 @@ static void txbegin(DB_TXN **tid) {
        }
 }
 
+static void dbpanic(DB_ENV* env, int errval)
+{
+       lprintf(CTDL_EMERG, "cdb_*: Berkeley DB panic: %d\n", errval);
+}
+
 static void cclose(DBC *cursor) {
        int ret;
 
@@ -146,7 +152,7 @@ static void check_handles(void *arg) {
 static void dest_tsd(void *arg) {
        if (arg != NULL) {
                check_handles(arg);
-               phree(arg);
+               free(arg);
        }
 }
 
@@ -164,7 +170,7 @@ void cdb_allocate_tsd(void) {
        if (pthread_getspecific(tsdkey) != NULL)
                return;
 
-       tsd = mallok(sizeof(struct cdbtsd));
+       tsd = malloc(sizeof(struct cdbtsd));
 
        tsd->tid = NULL;
 
@@ -267,9 +273,9 @@ static void cdb_checkpoint(void) {
 
 /*
  * Open the various databases we'll be using.  Any database which
- * does not exist should be created.  Note that we don't need an S_DATABASE
- * critical section here, because there aren't any active threads manipulating
- * the database yet -- and besides, it causes problems on BSDI.
+ * does not exist should be created.  Note that we don't need a
+ * critical section here, because there aren't any active threads
+ * manipulating the database yet.
  */
 void open_databases(void)
 {
@@ -278,6 +284,10 @@ void open_databases(void)
        char dbfilename[SIZ];
        u_int32_t flags = 0;
        char dbdirname[PATH_MAX];
+       DIR *dp;
+       struct dirent *d;
+       char filename[PATH_MAX];
+
 
        getcwd(dbdirname, sizeof dbdirname);
        strcat(dbdirname, "/data");
@@ -294,6 +304,8 @@ void open_databases(void)
          * already there, no problem.
          */
        mkdir(dbdirname, 0700);
+       chmod(dbdirname, 0700);
+       chown(dbdirname, CTDLUID, (-1) );
 
        lprintf(CTDL_DEBUG, "cdb_*: Setting up DB environment\n");
        db_env_set_func_yield(sched_yield);
@@ -303,6 +315,7 @@ void open_databases(void)
                exit(ret);
        }
        dbenv->set_errpfx(dbenv, "citserver");
+       dbenv->set_paniccall(dbenv, dbpanic);
 
         /*
          * We want to specify the shared memory buffer pool cachesize,
@@ -353,8 +366,7 @@ void open_databases(void)
                                dbfilename,
                                NULL,
                                DB_BTREE,
-                               DB_CREATE|DB_THREAD
-                               |DB_AUTO_COMMIT
+                               DB_CREATE|DB_AUTO_COMMIT|DB_THREAD
                                ,
                                0600);
                if (ret) {
@@ -370,6 +382,23 @@ void open_databases(void)
 
        cdb_allocate_tsd();
        CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
+
+       /* Now make sure we own all the files, because in a few milliseconds
+        * we're going to drop root privs.
+        */
+       dp = opendir(dbdirname);
+       if (dp != NULL) {
+               while (d = readdir(dp), d != NULL) {
+                       if (d->d_name[0] != '.') {
+                               snprintf(filename, sizeof filename, "%s/%s",
+                                       dbdirname, d->d_name);
+                               chmod(filename, 0600);
+                               chown(filename, CTDLUID, (-1) );
+                       }
+               }
+               closedir(dp);
+       }
+
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
 }
 
@@ -386,16 +415,18 @@ void close_databases(void)
        cdb_free_tsd();
 
        if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
-               lprintf(CTDL_EMERG, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
-               abort();
+               lprintf(CTDL_EMERG,
+                       "cdb_*: txn_checkpoint: %s\n",
+                       db_strerror(ret));
        }
 
        for (a = 0; a < MAXCDB; ++a) {
                lprintf(CTDL_INFO, "cdb_*: Closing database %d\n", a);
                ret = dbp[a]->close(dbp[a], 0);
                if (ret) {
-                       lprintf(CTDL_EMERG, "cdb_*: db_close: %s\n", db_strerror(ret));
-                       abort();
+                       lprintf(CTDL_EMERG,
+                               "cdb_*: db_close: %s\n",
+                               db_strerror(ret));
                }
                
        }
@@ -403,8 +434,9 @@ void close_databases(void)
         /* Close the handle. */
         ret = dbenv->close(dbenv, 0);
        if (ret) {
-                lprintf(CTDL_EMERG, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
-               abort();
+                lprintf(CTDL_EMERG,
+                       "cdb_*: DBENV->close: %s\n",
+                       db_strerror(ret));
         }
 }
 
@@ -433,18 +465,18 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb) {
 
        sourceLen = (uLongf) zheader.compressed_len;
        destLen = (uLongf) zheader.uncompressed_len;
-       uncompressed_data = mallok(zheader.uncompressed_len);
+       uncompressed_data = malloc(zheader.uncompressed_len);
 
        if (uncompress( (Bytef *) uncompressed_data,
-                       &destLen,
-                       compressed_data,
-                       sourceLen
+                       (uLongf *)&destLen,
+                       (const Bytef *)compressed_data,
+                       (uLong)sourceLen
        ) != Z_OK) {
                lprintf(CTDL_EMERG, "uncompress() error\n");
                abort();
        }
 
-       phree(cdb->ptr);
+       free(cdb->ptr);
        cdb->len = (size_t) destLen;
        cdb->ptr = uncompressed_data;
 }
@@ -489,7 +521,7 @@ int cdb_store(int cdb,
                buffer_len = ( (cdatalen * 101) / 100 ) + 100
                                + sizeof(struct CtdlCompressHeader) ;
                destLen = (uLongf) buffer_len;
-               compressed_data = mallok(buffer_len);
+               compressed_data = malloc(buffer_len);
                if (compress2(
                        (Bytef *) (compressed_data +
                                        sizeof(struct CtdlCompressHeader)),
@@ -523,7 +555,7 @@ int cdb_store(int cdb,
          abort();
        }
 #ifdef HAVE_ZLIB
-      if (compressing) phree(compressed_data);
+      if (compressing) free(compressed_data);
 #endif
       return ret;
       
@@ -556,7 +588,7 @@ int cdb_store(int cdb,
        {
          txcommit(tid);
 #ifdef HAVE_ZLIB
-         if (compressing) phree(compressed_data);
+         if (compressing) free(compressed_data);
 #endif
          return ret;
        }
@@ -684,7 +716,7 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
     }
 
   if (ret != 0) return NULL;
-  tempcdb = (struct cdbdata *) mallok(sizeof(struct cdbdata));
+  tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
 
   if (tempcdb == NULL)
     {
@@ -707,8 +739,8 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
  */
 void cdb_free(struct cdbdata *cdb)
 {
-       phree(cdb->ptr);
-       phree(cdb);
+       free(cdb->ptr);
+       free(cdb);
 }
 
 void cdb_close_cursor(int cdb)
@@ -728,8 +760,11 @@ void cdb_rewind(int cdb)
 {
        int ret = 0;
 
-       if (MYCURSORS[cdb] != NULL)
-               cclose(MYCURSORS[cdb]);
+       if (MYCURSORS[cdb] != NULL) {
+               lprintf(CTDL_EMERG, "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
+               abort();
+               /* cclose(MYCURSORS[cdb]); */
+       }
 
        /*
         * Now initialize the cursor
@@ -771,7 +806,7 @@ struct cdbdata *cdb_next_item(int cdb)
                return NULL;            /* presumably, end of file */
        }
 
-       cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
+       cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
        cdbret->len = data.size;
        cdbret->ptr = data.data;
 #ifdef HAVE_ZLIB