Well, it doesn't crash anymore, but it also doesn't work at all. Is that an improvement?
[citadel.git] / citadel / database.c
index a07e48ae75cb287e617c1949edd130a25d603f97..45b5d27342f5c066083aa19876d723b9c605ed62 100644 (file)
@@ -1,16 +1,14 @@
-/*
- * This is a data store backend for the Citadel server which uses Berkeley DB.
- *
- * Copyright (c) 1987-2019 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- */
+// This is a data store backend for the Citadel server which uses Berkeley DB.
+//
+// Copyright (c) 1987-2021 by the citadel.org team
+//
+// This program is open source software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 /*****************************************************************************
        Tunable configuration parameters for the Berkeley DB back end
 #include <stdio.h>
 #include <dirent.h>
 #include <zlib.h>
-
-#ifdef HAVE_DB_H
 #include <db.h>
-#error <db.h> was not found by configure. Install the Berkeley DB development package.
-#endif
 
 #if DB_VERSION_MAJOR < 5
 #error Citadel requires Berkeley DB v5.0 or newer.  Please upgrade.
 #endif
 
 #include <libcitadel.h>
-
 #include "ctdl_module.h"
 #include "control.h"
 #include "citserver.h"
@@ -53,8 +46,7 @@ static DB *dbp[MAXCDB];               /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
 
-void cdb_abort(void)
-{
+void cdb_abort(void) {
        syslog(LOG_DEBUG, "db: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
                getuid(), getgid(), geteuid(), getegid()
        );
@@ -63,8 +55,7 @@ void cdb_abort(void)
 
 
 /* Verbose logging callback */
-void cdb_verbose_log(const DB_ENV * dbenv, const char *msg)
-{
+void cdb_verbose_log(const DB_ENV * dbenv, const char *msg) {
        if (!IsEmptyStr(msg)) {
                syslog(LOG_DEBUG, "db: %s", msg);
        }
@@ -72,15 +63,13 @@ void cdb_verbose_log(const DB_ENV * dbenv, const char *msg)
 
 
 /* Verbose logging callback */
-void cdb_verbose_err(const DB_ENV * dbenv, const char *errpfx, const char *msg)
-{
+void cdb_verbose_err(const DB_ENV * dbenv, const char *errpfx, const char *msg) {
        syslog(LOG_ERR, "db: %s", msg);
 }
 
 
-/* just a little helper function */
-static void txabort(DB_TXN * tid)
-{
+/* wrapper for txn_abort() that logs/aborts on error */
+static void txabort(DB_TXN *tid) {
        int ret;
 
        ret = tid->abort(tid);
@@ -92,9 +81,8 @@ static void txabort(DB_TXN * tid)
 }
 
 
-/* this one is even more helpful than the last. */
-static void txcommit(DB_TXN * tid)
-{
+/* wrapper for txn_commit() that logs/aborts on error */
+static void txcommit(DB_TXN *tid) {
        int ret;
 
        ret = tid->commit(tid, 0);
@@ -106,9 +94,8 @@ static void txcommit(DB_TXN * tid)
 }
 
 
-/* are you sensing a pattern yet? */
-static void txbegin(DB_TXN ** tid)
-{
+/* wrapper for txn_begin() that logs/aborts on error */
+static void txbegin(DB_TXN **tid) {
        int ret;
 
        ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
@@ -120,14 +107,13 @@ static void txbegin(DB_TXN ** tid)
 }
 
 
-static void dbpanic(DB_ENV * env, int errval)
-{
+/* panic callback */
+static void dbpanic(DB_ENV * env, int errval) {
        syslog(LOG_ERR, "db: PANIC: %s", db_strerror(errval));
 }
 
 
-static void cclose(DBC * cursor)
-{
+static void cclose(DBC * cursor) {
        int ret;
 
        if ((ret = cursor->c_close(cursor))) {
@@ -137,8 +123,7 @@ static void cclose(DBC * cursor)
 }
 
 
-static void bailIfCursor(DBC ** cursors, const char *msg)
-{
+static void bailIfCursor(DBC ** cursors, const char *msg) {
        int i;
 
        for (i = 0; i < MAXCDB; i++)
@@ -149,8 +134,7 @@ static void bailIfCursor(DBC ** cursors, const char *msg)
 }
 
 
-void cdb_check_handles(void)
-{
+void cdb_check_handles(void) {
        bailIfCursor(TSD->cursors, "in check_handles");
 
        if (TSD->tid != NULL) {
@@ -163,8 +147,7 @@ void cdb_check_handles(void)
 /*
  * Request a checkpoint of the database.  Called once per minute by the thread manager.
  */
-void cdb_checkpoint(void)
-{
+void cdb_checkpoint(void) {
        int ret;
 
        syslog(LOG_DEBUG, "db: -- checkpoint --");
@@ -191,8 +174,7 @@ void cdb_checkpoint(void)
  * critical section here, because there aren't any active threads
  * manipulating the database yet.
  */
-void open_databases(void)
-{
+void open_databases(void) {
        int ret;
        int i;
        char dbfilename[32];
@@ -205,20 +187,19 @@ void open_databases(void)
        syslog(LOG_DEBUG, "db:    Linked zlib: %s", zlibVersion());
 
        /*
-        * Silently try to create the database subdirectory.  If it's
-        * already there, no problem.
+        * Silently try to create the database subdirectory.  If it's already there, no problem.
         */
-       if ((mkdir(ctdl_data_dir, 0700) != 0) && (errno != EEXIST)) {
-               syslog(LOG_ERR, "db: unable to create database directory [%s]: %m", ctdl_data_dir);
+       if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) {
+               syslog(LOG_ERR, "db: unable to create database directory [%s]: %m", ctdl_db_dir);
        }
-       if (chmod(ctdl_data_dir, 0700) != 0) {
-               syslog(LOG_ERR, "db: unable to set database directory accessrights [%s]: %m", ctdl_data_dir);
+       if (chmod(ctdl_db_dir, 0700) != 0) {
+               syslog(LOG_ERR, "db: unable to set database directory permissions [%s]: %m", ctdl_db_dir);
        }
-       if (chown(ctdl_data_dir, CTDLUID, (-1)) != 0) {
-               syslog(LOG_ERR, "db: unable to set the owner for [%s]: %m", ctdl_data_dir);
+       if (chown(ctdl_db_dir, CTDLUID, (-1)) != 0) {
+               syslog(LOG_ERR, "db: unable to set the owner for [%s]: %m", ctdl_db_dir);
        }
-       syslog(LOG_DEBUG, "db: Setting up DB environment\n");
-       /* db_env_set_func_yield((int (*)(u_long,  u_long))sched_yield); */
+       syslog(LOG_DEBUG, "db: Setting up DB environment");
+       // db_env_set_func_yield((int (*)(u_long,  u_long))sched_yield);
        ret = db_env_create(&dbenv, 0);
        if (ret) {
                syslog(LOG_ERR, "db: db_env_create: %s", db_strerror(ret));
@@ -253,20 +234,20 @@ void open_databases(void)
        }
 
        flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_INIT_LOG;
-       syslog(LOG_DEBUG, "db: dbenv->open(dbenv, %s, %d, 0)", ctdl_data_dir, flags);
-       ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+       syslog(LOG_DEBUG, "db: dbenv->open(dbenv, %s, %d, 0)", ctdl_db_dir, flags);
+       ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0);                                // try opening the database cleanly
        if (ret == DB_RUNRECOVERY) {
                syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
                syslog(LOG_ERR, "db: attempting recovery...");
                flags |= DB_RECOVER;
-               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+               ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0);                        // try recovery
        }
        if (ret == DB_RUNRECOVERY) {
                syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
                syslog(LOG_ERR, "db: attempting catastrophic recovery...");
                flags &= ~DB_RECOVER;
                flags |= DB_RECOVER_FATAL;
-               ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
+               ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0);                        // try catastrophic recovery
        }
        if (ret) {
                syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
@@ -276,52 +257,41 @@ void open_databases(void)
        }
 
        syslog(LOG_INFO, "db: mounting databases");
-
        for (i = 0; i < MAXCDB; ++i) {
-
-               /* Create a database handle */
-               ret = db_create(&dbp[i], dbenv, 0);
+               ret = db_create(&dbp[i], dbenv, 0);                                     // Create a database handle
                if (ret) {
                        syslog(LOG_ERR, "db: db_create: %s", db_strerror(ret));
                        syslog(LOG_ERR, "db: exit code %d", ret);
                        exit(CTDLEXIT_DB);
                }
 
-
-               /* Arbitrary names for our tables -- we reference them by
-                * number, so we don't have string names for them.
-                */
-               snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
-
+               snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);                 // table names by number
                ret = dbp[i]->open(dbp[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_THREAD, 0600);
                if (ret) {
                        syslog(LOG_ERR, "db: db_open[%02x]: %s", i, db_strerror(ret));
                        if (ret == ENOMEM) {
-                               syslog(LOG_ERR,
-                                      "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
+                               syslog(LOG_ERR, "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
                        }
                        syslog(LOG_ERR, "db: exit code %d", ret);
                        exit(CTDLEXIT_DB);
                }
        }
-
 }
 
 
-/* Make sure we own all the files, because in a few milliseconds
- * we're going to drop root privs.
+/*
+ * Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
  */
-void cdb_chmod_data(void)
-{
+void cdb_chmod_data(void) {
        DIR *dp;
        struct dirent *d;
        char filename[PATH_MAX];
 
-       dp = opendir(ctdl_data_dir);
+       dp = opendir(ctdl_db_dir);
        if (dp != NULL) {
                while (d = readdir(dp), d != NULL) {
                        if (d->d_name[0] != '.') {
-                               snprintf(filename, sizeof filename, "%s/%s", ctdl_data_dir, d->d_name);
+                               snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name);
                                syslog(LOG_DEBUG, "db: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
                                syslog(LOG_DEBUG, "db: chown(%s, CTDLUID, -1) returned %d",
                                        filename, chown(filename, CTDLUID, (-1))
@@ -330,8 +300,6 @@ void cdb_chmod_data(void)
                }
                closedir(dp);
        }
-
-       syslog(LOG_DEBUG, "db: open_databases() finished");
 }
 
 
@@ -339,14 +307,13 @@ void cdb_chmod_data(void)
  * Close all of the db database files we've opened.  This can be done
  * in a loop, since it's just a bunch of closes.
  */
-void close_databases(void)
-{
-       int a;
+void close_databases(void) {
+       int i;
        int ret;
 
        syslog(LOG_INFO, "db: performing final checkpoint");
        if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
-               syslog(LOG_ERR, "txn_checkpoint: %s", db_strerror(ret));
+               syslog(LOG_ERR, "db: txn_checkpoint: %s", db_strerror(ret));
        }
 
        syslog(LOG_INFO, "db: flushing the database logs");
@@ -354,25 +321,27 @@ void close_databases(void)
                syslog(LOG_ERR, "db: log_flush: %s", 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) {
-               syslog(LOG_INFO, "db: closing database %02x", a);
-               ret = dbp[a]->close(dbp[a], 0);
+       syslog(LOG_INFO, "db: closing databases");
+       for (i = 0; i < MAXCDB; ++i) {
+               syslog(LOG_INFO, "db: closing database %02x", i);
+               ret = dbp[i]->close(dbp[i], 0);
                if (ret) {
                        syslog(LOG_ERR, "db: db_close: %s", db_strerror(ret));
                }
 
        }
 
+       // This seemed nifty at the time but did anyone really look at it?
+       // #ifdef DB_STAT_ALL
+       // /* print some statistics... */
+       // dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
+       // #endif
+
        /* Close the handle. */
        ret = dbenv->close(dbenv, 0);
        if (ret) {
-               syslog(LOG_EMERG, "db: DBENV->close: %s", db_strerror(ret));
+               syslog(LOG_ERR, "db: DBENV->close: %s", db_strerror(ret));
        }
 }
 
@@ -380,8 +349,7 @@ void close_databases(void)
 /*
  * Decompress a database item if it was compressed on disk
  */
-void cdb_decompress_if_necessary(struct cdbdata *cdb)
-{
+void cdb_decompress_if_necessary(struct cdbdata *cdb) {
        static int magic = COMPRESS_MAGIC;
 
        if ((cdb == NULL) || (cdb->ptr == NULL) || (cdb->len < sizeof(magic)) || (memcmp(cdb->ptr, &magic, sizeof(magic)))) {
@@ -398,8 +366,9 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
 
        memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
        cplen = sizeof(struct CtdlCompressHeader);
-       if (sizeof(struct CtdlCompressHeader) > cdb->len)
+       if (sizeof(struct CtdlCompressHeader) > cdb->len) {
                cplen = cdb->len;
+       }
        memcpy(&zheader, cdb->ptr, cplen);
 
        compressed_data = cdb->ptr;
@@ -411,7 +380,7 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
 
        if (uncompress((Bytef *) uncompressed_data,
                       (uLongf *) & destLen, (const Bytef *) compressed_data, (uLong) sourceLen) != Z_OK) {
-               syslog(LOG_EMERG, "db: uncompress() error");
+               syslog(LOG_ERR, "db: uncompress() error");
                cdb_abort();
        }
 
@@ -425,13 +394,11 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
  * Store a piece of data.  Returns 0 if the operation was successful.  If a
  * key already exists it should be overwritten.
  */
-int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
-{
+int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) {
 
        DBT dkey, ddata;
-       DB_TXN *tid;
+       DB_TXN *tid = NULL;
        int ret = 0;
-
        struct CtdlCompressHeader zheader;
        char *compressed_data = NULL;
        int compressing = 0;
@@ -455,7 +422,7 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                compressed_data = malloc(buffer_len);
                if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)),
                              &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK) {
-                       syslog(LOG_EMERG, "db: compress2() error");
+                       syslog(LOG_ERR, "db: compress2() error");
                        cdb_abort();
                }
                zheader.compressed_len = (size_t) destLen;
@@ -465,14 +432,14 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
        }
 
        if (TSD->tid != NULL) {
-               ret = dbp[cdb]->put(dbp[cdb],   /* db */
-                                   TSD->tid,   /* transaction ID */
-                                   &dkey,      /* key */
-                                   &ddata,     /* data */
-                                   0           /* flags */
+               ret = dbp[cdb]->put(dbp[cdb],   // db
+                                   TSD->tid,   // transaction ID
+                                   &dkey,      // key
+                                   &ddata,     // data
+                                   0           // flags
                );
                if (ret) {
-                       syslog(LOG_EMERG, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
+                       syslog(LOG_ERR, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
                        cdb_abort();
                }
                if (compressing) {
@@ -485,16 +452,16 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
              retry:
                txbegin(&tid);
 
-               if ((ret = dbp[cdb]->put(dbp[cdb],      /* db */
-                                        tid,   /* transaction ID */
-                                        &dkey, /* key */
-                                        &ddata,        /* data */
-                                        0))) { /* flags */
+               if ((ret = dbp[cdb]->put(dbp[cdb],      // db
+                                        tid,           // transaction ID
+                                        &dkey,         // key
+                                        &ddata,        // data
+                                        0))) {         // flags
                        if (ret == DB_LOCK_DEADLOCK) {
                                txabort(tid);
                                goto retry;
                        } else {
-                               syslog(LOG_EMERG, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
+                               syslog(LOG_ERR, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
                                cdb_abort();
                        }
                } else {
@@ -512,9 +479,7 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
 /*
  * Delete a piece of data.  Returns 0 if the operation was successful.
  */
-int cdb_delete(int cdb, void *key, int keylen)
-{
-
+int cdb_delete(int cdb, void *key, int keylen) {
        DBT dkey;
        DB_TXN *tid;
        int ret;
@@ -526,7 +491,7 @@ int cdb_delete(int cdb, void *key, int keylen)
        if (TSD->tid != NULL) {
                ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
                if (ret) {
-                       syslog(LOG_EMERG, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
+                       syslog(LOG_ERR, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
                        if (ret != DB_NOTFOUND) {
                                cdb_abort();
                        }
@@ -542,7 +507,7 @@ int cdb_delete(int cdb, void *key, int keylen)
                                txabort(tid);
                                goto retry;
                        } else {
-                               syslog(LOG_EMERG, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
+                               syslog(LOG_ERR, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
                                cdb_abort();
                        }
                } else {
@@ -553,8 +518,7 @@ int cdb_delete(int cdb, void *key, int keylen)
 }
 
 
-static DBC *localcursor(int cdb)
-{
+static DBC *localcursor(int cdb) {
        int ret;
        DBC *curs;
 
@@ -566,7 +530,7 @@ static DBC *localcursor(int cdb)
        }
 
        if (ret) {
-               syslog(LOG_EMERG, "db: localcursor: %s", db_strerror(ret));
+               syslog(LOG_ERR, "db: localcursor: %s", db_strerror(ret));
                cdb_abort();
        }
 
@@ -579,8 +543,12 @@ static DBC *localcursor(int cdb)
  * 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, const void *key, int keylen)
-{
+struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) {
+
+       if (keylen == 0) {              // key length zero is impossible
+               return(NULL);
+       }
+
        struct cdbdata *tempcdb;
        DBT dkey, dret;
        int ret;
@@ -593,24 +561,21 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
                memset(&dret, 0, sizeof(DBT));
                dret.flags = DB_DBT_MALLOC;
                ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
-       } else {
+       }
+       else {
                DBC *curs;
 
                do {
                        memset(&dret, 0, sizeof(DBT));
                        dret.flags = DB_DBT_MALLOC;
-
                        curs = localcursor(cdb);
-
                        ret = curs->c_get(curs, &dkey, &dret, DB_SET);
                        cclose(curs);
-               }
-               while (ret == DB_LOCK_DEADLOCK);
-
+               } while (ret == DB_LOCK_DEADLOCK);
        }
 
        if ((ret != 0) && (ret != DB_NOTFOUND)) {
-               syslog(LOG_EMERG, "db: cdb_fetch(%d): %s", cdb, db_strerror(ret));
+               syslog(LOG_ERR, "db: cdb_fetch(%d): %s", cdb, db_strerror(ret));
                cdb_abort();
        }
 
@@ -619,12 +584,12 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
        }
 
        tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
-
        if (tempcdb == NULL) {
-               syslog(LOG_EMERG, "db: cdb_fetch: Cannot allocate memory for tempcdb");
+               syslog(LOG_ERR, "db: cdb_fetch() cannot allocate memory for tempcdb: %m");
                cdb_abort();
                return NULL;    /* make it easier for static analysis... */
-       } else {
+       }
+       else {
                tempcdb->len = dret.size;
                tempcdb->ptr = dret.data;
                cdb_decompress_if_necessary(tempcdb);
@@ -641,8 +606,7 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
  * pointer elsewhere and then setting 'ptr' to NULL.  cdb_free() will then
  * avoid freeing it.
  */
-void cdb_free(struct cdbdata *cdb)
-{
+void cdb_free(struct cdbdata *cdb) {
        if (cdb->ptr) {
                free(cdb->ptr);
        }
@@ -650,8 +614,7 @@ void cdb_free(struct cdbdata *cdb)
 }
 
 
-void cdb_close_cursor(int cdb)
-{
+void cdb_close_cursor(int cdb) {
        if (TSD->cursors[cdb] != NULL) {
                cclose(TSD->cursors[cdb]);
        }
@@ -665,12 +628,11 @@ void cdb_close_cursor(int cdb)
  * (There is guaranteed to be no more than one traversal in
  * progress per thread at any given time.)
  */
-void cdb_rewind(int cdb)
-{
+void cdb_rewind(int cdb) {
        int ret = 0;
 
        if (TSD->cursors[cdb] != NULL) {
-               syslog(LOG_EMERG, "db: cdb_rewind: must close cursor on database %d before reopening", cdb);
+               syslog(LOG_ERR, "db: cdb_rewind: must close cursor on database %d before reopening", cdb);
                cdb_abort();
                /* cclose(TSD->cursors[cdb]); */
        }
@@ -680,7 +642,7 @@ void cdb_rewind(int cdb)
         */
        ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
        if (ret) {
-               syslog(LOG_EMERG, "db: cdb_rewind: db_cursor: %s", db_strerror(ret));
+               syslog(LOG_ERR, "db: cdb_rewind: db_cursor: %s", db_strerror(ret));
                cdb_abort();
        }
 }
@@ -690,8 +652,7 @@ void cdb_rewind(int cdb)
  * Fetch the next item in a sequential search.  Returns a pointer to a 
  * cdbdata structure, or NULL if we've hit the end.
  */
-struct cdbdata *cdb_next_item(int cdb)
-{
+struct cdbdata *cdb_next_item(int cdb) {
        DBT key, data;
        struct cdbdata *cdbret;
        int ret = 0;
@@ -705,7 +666,7 @@ struct cdbdata *cdb_next_item(int cdb)
 
        if (ret) {
                if (ret != DB_NOTFOUND) {
-                       syslog(LOG_EMERG, "db: cdb_next_item(%d): %s", cdb, db_strerror(ret));
+                       syslog(LOG_ERR, "db: cdb_next_item(%d): %s", cdb, db_strerror(ret));
                        cdb_abort();
                }
                cdb_close_cursor(cdb);
@@ -724,13 +685,11 @@ struct cdbdata *cdb_next_item(int cdb)
 /*
  * Transaction-based stuff.  I'm writing this as I bake cookies...
  */
-void cdb_begin_transaction(void)
-{
-
+void cdb_begin_transaction(void) {
        bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
 
        if (TSD->tid != NULL) {
-               syslog(LOG_EMERG, "db: cdb_begin_transaction: ERROR: nested transaction");
+               syslog(LOG_ERR, "db: cdb_begin_transaction: ERROR: nested transaction");
                cdb_abort();
        }
 
@@ -738,21 +697,22 @@ void cdb_begin_transaction(void)
 }
 
 
-void cdb_end_transaction(void)
-{
+void cdb_end_transaction(void) {
        int i;
 
-       for (i = 0; i < MAXCDB; i++)
+       for (i = 0; i < MAXCDB; i++) {
                if (TSD->cursors[i] != NULL) {
                        syslog(LOG_WARNING, "db: cdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
                        cclose(TSD->cursors[i]);
                        TSD->cursors[i] = NULL;
                }
+       }
 
        if (TSD->tid == NULL) {
                syslog(LOG_ERR, "db: cdb_end_transaction: ERROR: txcommit(NULL) !!");
                cdb_abort();
-       } else {
+       }
+       else {
                txcommit(TSD->tid);
        }
 
@@ -763,16 +723,16 @@ void cdb_end_transaction(void)
 /*
  * Truncate (delete every record)
  */
-void cdb_trunc(int cdb)
-{
+void cdb_trunc(int cdb) {
        /* DB_TXN *tid; */
        int ret;
        u_int32_t count;
 
        if (TSD->tid != NULL) {
-               syslog(LOG_EMERG, "db: cdb_trunc must not be called in a transaction.");
+               syslog(LOG_ERR, "db: cdb_trunc must not be called in a transaction.");
                cdb_abort();
-       } else {
+       }
+       else {
                bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
 
              retry:
@@ -786,25 +746,22 @@ void cdb_trunc(int cdb)
                                /* txabort(tid); */
                                goto retry;
                        } else {
-                               syslog(LOG_EMERG, "db: cdb_truncate(%d): %s", cdb, db_strerror(ret));
+                               syslog(LOG_ERR, "db: cdb_truncate(%d): %s", cdb, db_strerror(ret));
                                if (ret == ENOMEM) {
-                                       syslog(LOG_EMERG,
-                                              "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
+                                       syslog(LOG_ERR, "db: You may need to tune your database; please read http://www.citadel.org/doku.php?id=faq:troubleshooting:out_of_lock_entries for more information.");
                                }
                                exit(CTDLEXIT_DB);
                        }
-               } else {
+               }
+               else {
                        /* txcommit(tid); */
                }
        }
 }
 
 
-/*
- * compact (defragment) the database , possibly returning space back to the underlying filesystem
- */
-void cdb_compact(void)
-{
+// compact (defragment) the database, possibly returning space back to the underlying filesystem
+void cdb_compact(void) {
        int ret;
        int i;
 
@@ -823,8 +780,7 @@ void cdb_compact(void)
 // Has an item already been seen (is it in the CDB_USETABLE) ?
 // Returns 0 if it hasn't, 1 if it has
 // In either case, writes the item to the database for next time.
-int CheckIfAlreadySeen(StrBuf *guid)
-{
+int CheckIfAlreadySeen(StrBuf *guid) {
        int found = 0;
        struct UseTable ut;
        struct cdbdata *cdbut;
@@ -845,53 +801,10 @@ int CheckIfAlreadySeen(StrBuf *guid)
 }
 
 
-void cmd_rsen(char *argbuf)
-{
-       char Token[SIZ];
-       long TLen;
-       char Time[SIZ];
-
-       struct UseTable ut;
-       struct cdbdata *cdbut;
-
-       if (CtdlAccessCheck(ac_aide)) {
-               return;
-       }
-
-       TLen = extract_token(Token, argbuf, 1, '|', sizeof Token);
-       if (strncmp(argbuf, "GET", 3) == 0) {
-               cdbut = cdb_fetch(CDB_USETABLE, Token, TLen);
-               if (cdbut != NULL) {
-                       memcpy(&ut, cdbut->ptr, ((cdbut->len > sizeof(struct UseTable)) ? sizeof(struct UseTable) : cdbut->len));
-
-                       cprintf("%d %ld\n", CIT_OK, ut.ut_timestamp);
-               } else {
-                       cprintf("%d not found\n", ERROR + NOT_HERE);
-               }
-
-       } else if (strncmp(argbuf, "SET", 3) == 0) {
-               memcpy(ut.ut_msgid, Token, TLen);
-               extract_token(Time, argbuf, 2, '|', sizeof Time);
-               ut.ut_timestamp = atol(Time);
-               cdb_store(CDB_USETABLE, Token, TLen, &ut, sizeof(struct UseTable));
-               cprintf("%d token updated\n", CIT_OK);
-       } else if (strncmp(argbuf, "DEL", 3) == 0) {
-               if (cdb_delete(CDB_USETABLE, Token, TLen))
-                       cprintf("%d not found\n", ERROR + NOT_HERE);
-               else
-                       cprintf("%d deleted.\n", CIT_OK);
-
-       } else {
-               cprintf("%d Usage: [GET|SET|DEL]|Token|timestamp\n", ERROR);
-       }
-
-}
-
-
 CTDL_MODULE_INIT(database)
 {
        if (!threading) {
-               CtdlRegisterProtoHook(cmd_rsen, "RSEN", "manipulate Aggregators seen database");
+               // nothing to do here
        }
 
        /* return our module id for the log */