]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
New versions of Berkeley DB (I tested with 4.5.20) seem to
[citadel.git] / citadel / database_sleepycat.c
index 30e1e0b778af94600933f9a6a9d8f61fdae06f97..918b6d415f9c6db1de8eba55351c88ceeb159e56 100644 (file)
@@ -70,6 +70,20 @@ static pthread_key_t tsdkey;
 #define MYCURSORS      (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
 #define MYTID          (((struct cdbtsd*)pthread_getspecific(tsdkey))->tid)
 
+/* Verbose logging callback */
+void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
+{
+       lprintf(CTDL_DEBUG, "BDB: %s\n", msg);
+}
+
+
+/* Verbose logging callback */
+void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
+{
+       lprintf(CTDL_ALERT, "BDB: %s\n", msg);
+}
+
+
 /* just a little helper function */
 static void txabort(DB_TXN * tid)
 {
@@ -244,7 +258,7 @@ static void cdb_cull_logs(void)
                                         " This log file is no longer in use "
                                         "and may be safely deleted.\n",
                                         *file, strerror(errno));
-                               aide_message(errmsg);
+                               aide_message(errmsg, "Database Warning Message");
                        }
                }
                free(list);
@@ -329,17 +343,6 @@ void open_databases(void)
        int i;
        char dbfilename[SIZ];
        u_int32_t flags = 0;
-       char dbdirname[PATH_MAX];
-       DIR *dp;
-       struct dirent *d;
-       char filename[PATH_MAX];
-
-#ifndef HAVE_DATA_DIR
-       getcwd(dbdirname, sizeof dbdirname);
-       strcat(dbdirname, "/data");
-#else
-       strcat(dbdirname, DATA_DIR"/data");
-#endif
 
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() starting\n");
        lprintf(CTDL_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
@@ -353,9 +356,9 @@ void open_databases(void)
         * Silently try to create the database subdirectory.  If it's
         * already there, no problem.
         */
-       mkdir(dbdirname, 0700);
-       chmod(dbdirname, 0700);
-       chown(dbdirname, CTDLUID, (-1));
+       mkdir(ctdl_data_dir, 0700);
+       chmod(ctdl_data_dir, 0700);
+       chown(ctdl_data_dir, CTDLUID, (-1));
 
        lprintf(CTDL_DEBUG, "cdb_*: Setting up DB environment\n");
        db_env_set_func_yield(sched_yield);
@@ -363,10 +366,17 @@ void open_databases(void)
        if (ret) {
                lprintf(CTDL_EMERG, "cdb_*: db_env_create: %s\n",
                        db_strerror(ret));
-               exit(ret);
+               exit(CTDLEXIT_DB);
        }
        dbenv->set_errpfx(dbenv, "citserver");
        dbenv->set_paniccall(dbenv, dbpanic);
+       dbenv->set_errcall(dbenv, cdb_verbose_err);
+       dbenv->set_errpfx(dbenv, "ctdl");
+#if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 3)
+       dbenv->set_msgcall(dbenv, cdb_verbose_log);
+#endif
+       dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
+       dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
 
        /*
         * We want to specify the shared memory buffer pool cachesize,
@@ -377,27 +387,27 @@ void open_databases(void)
                lprintf(CTDL_EMERG, "cdb_*: set_cachesize: %s\n",
                        db_strerror(ret));
                dbenv->close(dbenv, 0);
-               exit(ret);
+               exit(CTDLEXIT_DB);
        }
 
        if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
                lprintf(CTDL_EMERG, "cdb_*: set_lk_detect: %s\n",
                        db_strerror(ret));
                dbenv->close(dbenv, 0);
-               exit(ret);
+               exit(CTDLEXIT_DB);
        }
 
        flags =
-           DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_PRIVATE |
-           DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD;
-       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", dbdirname,
-               flags);
-       ret = dbenv->open(dbenv, dbdirname, flags, 0);
+           DB_CREATE | DB_RECOVER | DB_INIT_MPOOL |
+           DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD;
+       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n",
+               ctdl_data_dir, flags);
+       ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
        if (ret) {
                lprintf(CTDL_DEBUG, "cdb_*: dbenv->open: %s\n",
                        db_strerror(ret));
                dbenv->close(dbenv, 0);
-               exit(ret);
+               exit(CTDLEXIT_DB);
        }
 
        lprintf(CTDL_INFO, "cdb_*: Starting up DB\n");
@@ -409,7 +419,7 @@ void open_databases(void)
                if (ret) {
                        lprintf(CTDL_DEBUG, "cdb_*: db_create: %s\n",
                                db_strerror(ret));
-                       exit(ret);
+                       exit(CTDLEXIT_DB);
                }
 
 
@@ -428,29 +438,40 @@ void open_databases(void)
                if (ret) {
                        lprintf(CTDL_EMERG, "cdb_*: db_open[%d]: %s\n", i,
                                db_strerror(ret));
-                       exit(ret);
+                       exit(CTDLEXIT_DB);
                }
        }
 
        if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
                lprintf(CTDL_EMERG, "cdb_*: pthread_key_create: %s\n",
                        strerror(ret));
-               exit(1);
+               exit(CTDLEXIT_DB);
        }
 
        cdb_allocate_tsd();
+}
 
-       /* Now make sure we own all the files, because in a few milliseconds
-        * we're going to drop root privs.
-        */
-       dp = opendir(dbdirname);
+
+/* Make sure we own all the files, because in a few milliseconds
+ * we're going to drop root privs.
+ */
+void cdb_chmod_data(void) {
+       DIR *dp;
+       struct dirent *d;
+       char filename[PATH_MAX];
+
+       dp = opendir(ctdl_data_dir);
        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));
+                                        "%s/%s", ctdl_data_dir, d->d_name);
+                               lprintf(9, "chmod(%s, 0600) returned %d\n",
+                                       filename, chmod(filename, 0600)
+                               );
+                               lprintf(9, "chown(%s, CTDLUID, -1) returned %d\n",
+                                       filename, chown(filename, CTDLUID, (-1))
+                               );
                        }
                }
                closedir(dp);
@@ -552,14 +573,14 @@ int cdb_store(int cdb, void *ckey, int ckeylen, void *cdata, int cdatalen)
 
        DBT dkey, ddata;
        DB_TXN *tid;
-       int ret;
+       int ret = 0;
 
 #ifdef HAVE_ZLIB
        struct CtdlCompressHeader zheader;
        char *compressed_data = NULL;
        int compressing = 0;
-       size_t buffer_len;
-       uLongf destLen;
+       size_t buffer_len = 0;
+       uLongf destLen = 0;
 #endif
 
        memset(&dkey, 0, sizeof(DBT));