Here it is, the new thread interface.
[citadel.git] / citadel / database_sleepycat.c
index 1ce1e1b2b5cdee8ef917b2e7a9a0577d0921ad91..ddd8edb12b407725f25c6304278c792369a84b7e 100644 (file)
 
 /*****************************************************************************/
 
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <pthread.h>
 #include "citadel.h"
 #include "server.h"
-#include "serv_extensions.h"
 #include "citserver.h"
 #include "database.h"
 #include "msgbase.h"
 #include "sysdep_decls.h"
 #include "config.h"
 
+#include "ctdl_module.h"
+
+
 static DB *dbp[MAXCDB];                /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
@@ -74,6 +72,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)
 {
@@ -248,24 +260,42 @@ 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);
        }
+}
 
-       lprintf(CTDL_INFO, "Database log file cull ended.\n");
+/*
+ * Manually initiate log file cull.
+ */
+void cmd_cull(char *argbuf) {
+       if (CtdlAccessCheck(ac_internal)) return;
+       cdb_cull_logs();
+       cprintf("%d Database log file cull completed.\n", CIT_OK);
 }
 
 
 /*
  * Request a checkpoint of the database.
  */
-static void cdb_checkpoint(void)
+void cdb_checkpoint(void)
 {
        int ret;
-       /* static time_t last_cull = 0L; */
+//     static time_t last_run = 0L;
+
+       /* Only do a checkpoint once per minute. */
+/*
+ * Don't need this any more, since the thread that calls us sleeps for 60 seconds between calls
+       if ((time(NULL) - last_run) < 60L) {
+               return;
+       }
+       last_run = time(NULL);
+*/
 
+       lprintf(CTDL_DEBUG, "-- db checkpoint --\n");
        ret = dbenv->txn_checkpoint(dbenv,
                                    MAX_CHECKPOINT_KBYTES,
                                    MAX_CHECKPOINT_MINUTES, 0);
@@ -277,15 +307,12 @@ static void cdb_checkpoint(void)
        }
 
        /* After a successful checkpoint, we can cull the unused logs */
-       cdb_cull_logs();
+       if (config.c_auto_cull) {
+               cdb_cull_logs();
+       }
+}
 
-       /* Cull the logs if we haven't done so for 24 hours
-          if ((time(NULL) - last_cull) > 86400L) {
-          last_cull = time(NULL);
-          cdb_cull_logs();
-          } */
 
-}
 
 /*
  * Open the various databases we'll be using.  Any database which
@@ -299,14 +326,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];
-
-
-       getcwd(dbdirname, sizeof dbdirname);
-       strcat(dbdirname, "/data");
 
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() starting\n");
        lprintf(CTDL_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
@@ -320,9 +339,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);
@@ -330,10 +349,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,
@@ -344,39 +370,48 @@ 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);
+       flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_RECOVER;
+       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 == DB_RUNRECOVERY) {
+               lprintf(CTDL_ALERT, "dbenv->open: %s\n", db_strerror(ret));
+               lprintf(CTDL_ALERT, "Attempting recovery...\n");
+               flags |= DB_RECOVER;
+               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       }
+       if (ret == DB_RUNRECOVERY) {
+               lprintf(CTDL_ALERT, "dbenv->open: %s\n", db_strerror(ret));
+               lprintf(CTDL_ALERT, "Attempting catastrophic recovery...\n");
+               flags &= ~DB_RECOVER;
+               flags |= DB_RECOVER_FATAL;
+               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       }
        if (ret) {
-               lprintf(CTDL_DEBUG, "cdb_*: dbenv->open: %s\n",
-                       db_strerror(ret));
+               lprintf(CTDL_DEBUG, "dbenv->open: %s\n", db_strerror(ret));
                dbenv->close(dbenv, 0);
-               exit(ret);
+               exit(CTDLEXIT_DB);
        }
 
-       lprintf(CTDL_INFO, "cdb_*: Starting up DB\n");
+       lprintf(CTDL_INFO, "Starting up DB\n");
 
        for (i = 0; i < MAXCDB; ++i) {
 
                /* Create a database handle */
                ret = db_create(&dbp[i], dbenv, 0);
                if (ret) {
-                       lprintf(CTDL_DEBUG, "cdb_*: db_create: %s\n",
+                       lprintf(CTDL_DEBUG, "db_create: %s\n",
                                db_strerror(ret));
-                       exit(ret);
+                       exit(CTDLEXIT_DB);
                }
 
 
@@ -393,38 +428,51 @@ void open_databases(void)
                                   DB_CREATE | DB_AUTO_COMMIT | DB_THREAD,
                                   0600);
                if (ret) {
-                       lprintf(CTDL_EMERG, "cdb_*: db_open[%d]: %s\n", i,
+                       lprintf(CTDL_EMERG, "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",
+               lprintf(CTDL_EMERG, "pthread_key_create: %s\n",
                        strerror(ret));
-               exit(1);
+               exit(CTDLEXIT_DB);
        }
 
        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);
+
+/* 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);
        }
 
-       lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
+       lprintf(CTDL_DEBUG, "open_databases() finished\n");
+
+       CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
 }
 
 
@@ -441,15 +489,21 @@ void close_databases(void)
 
        if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
                lprintf(CTDL_EMERG,
-                       "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
+                       "txn_checkpoint: %s\n", db_strerror(ret));
        }
 
+       /* print some statistics... */
+#ifdef DB_STAT_ALL
+       dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
+#endif
+
+       /* close the tables */
        for (a = 0; a < MAXCDB; ++a) {
-               lprintf(CTDL_INFO, "cdb_*: Closing database %d\n", a);
+               lprintf(CTDL_INFO, "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));
+                               "db_close: %s\n", db_strerror(ret));
                }
 
        }
@@ -458,7 +512,7 @@ void close_databases(void)
        ret = dbenv->close(dbenv, 0);
        if (ret) {
                lprintf(CTDL_EMERG,
-                       "cdb_*: DBENV->close: %s\n", db_strerror(ret));
+                       "DBENV->close: %s\n", db_strerror(ret));
        }
 }
 
@@ -518,14 +572,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));
@@ -740,12 +794,18 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
 
 
 /*
- * Free a cdbdata item (ok, this is really no big deal, but we might need to do
- * more complex stuff with other database managers in the future).
+ * Free a cdbdata item.
+ *
+ * Note that we only free the 'ptr' portion if it is not NULL.  This allows
+ * other code to assume ownership of that memory simply by storing the
+ * pointer elsewhere and then setting 'ptr' to NULL.  cdb_free() will then
+ * avoid freeing it.
  */
 void cdb_free(struct cdbdata *cdb)
 {
-       free(cdb->ptr);
+       if (cdb->ptr) {
+               free(cdb->ptr);
+       }
        free(cdb);
 }
 
@@ -873,7 +933,7 @@ void cdb_end_transaction(void)
  */
 void cdb_trunc(int cdb)
 {
-       //DB_TXN *tid;
+       /* DB_TXN *tid; */
        int ret;
        u_int32_t count;
 
@@ -886,14 +946,14 @@ void cdb_trunc(int cdb)
                             "attempt to write during r/o cursor");
 
              retry:
-               //txbegin(&tid);
+               /* txbegin(&tid); */
 
                if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
                                              NULL,     /* transaction ID */
                                              &count,   /* #rows deleted */
                                              0))) {    /* flags */
                        if (ret == DB_LOCK_DEADLOCK) {
-                               //txabort(tid);
+                               /* txabort(tid); */
                                goto retry;
                        } else {
                                lprintf(CTDL_EMERG,
@@ -902,7 +962,7 @@ void cdb_trunc(int cdb)
                                abort();
                        }
                } else {
-                       //txcommit(tid);
+                       /* txcommit(tid); */
                }
        }
 }