#include <db.h>
#elif defined(HAVE_DB4_DB_H)
#include <db4/db.h>
-#elif defined(HAVE_DB3_DB_H)
-#include <db3/db.h>
#else
-#error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
+#error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
#endif
+
+#if DB_VERSION_MAJOR < 4 || DB_VERSION_MINOR < 1
+#error Citadel requires Berkeley DB v4.1 or newer. Please upgrade.
+#endif
+
+
#include <pthread.h>
#include "citadel.h"
#include "server.h"
/* just a little helper function */
static void txabort(DB_TXN *tid) {
- int ret = txn_abort(tid);
+ int ret;
+
+ ret = tid->abort(tid);
if (ret) {
- lprintf(1, "cdb_*: txn_abort: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: txn_abort: %s\n", db_strerror(ret));
abort();
}
}
/* this one is even more helpful than the last. */
static void txcommit(DB_TXN *tid) {
- int ret = txn_commit(tid, 0);
+ int ret;
+
+ ret = tid->commit(tid, 0);
if (ret) {
- lprintf(1, "cdb_*: txn_commit: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: txn_commit: %s\n", db_strerror(ret));
abort();
}
}
/* are you sensing a pattern yet? */
static void txbegin(DB_TXN **tid) {
- int ret = txn_begin(dbenv, NULL, tid, 0);
+ int ret;
+
+ ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
if (ret) {
- lprintf(1, "cdb_*: txn_begin: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: txn_begin: %s\n", db_strerror(ret));
abort();
}
}
int ret;
if ((ret = cursor->c_close(cursor))) {
- lprintf(1, "cdb_*: c_close: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: c_close: %s\n", db_strerror(ret));
abort();
}
}
for (i = 0; i < MAXCDB; i++)
if (cursors[i] != NULL)
{
- lprintf(1, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
+ lprintf(CTDL_EMERG, "cdb_*: cursor still in progress on cdb %d: %s\n", i, msg);
abort();
}
}
bailIfCursor(tsd->cursors, "in check_handles");
if (tsd->tid != NULL) {
- lprintf(1, "cdb_*: transaction still in progress!");
+ lprintf(CTDL_EMERG, "cdb_*: transaction still in progress!");
abort();
}
}
char **file, **list;
char errmsg[SIZ];
- lprintf(5, "Database log file cull started.\n");
+ lprintf(CTDL_INFO, "Database log file cull started.\n");
flags = DB_ARCH_ABS;
/* Get the list of names. */
-#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
- if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
-#elif DB_VERSION_MAJOR >= 4
if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
-#else
- if ((ret = log_archive(dbenv, &list, flags)) != 0) {
-#endif
- lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
+ lprintf(CTDL_ERR, "cdb_cull_logs: %s\n", db_strerror(ret));
return;
}
/* Print the list of names. */
if (list != NULL) {
for (file = list; *file != NULL; ++file) {
- lprintf(9, "Deleting log: %s\n", *file);
+ lprintf(CTDL_DEBUG, "Deleting log: %s\n", *file);
ret = unlink(*file);
if (ret != 0) {
snprintf(errmsg, sizeof(errmsg),
free(list);
}
- lprintf(5, "Database log file cull ended.\n");
+ lprintf(CTDL_INFO, "Database log file cull ended.\n");
}
int ret;
static time_t last_cull = 0L;
-#if DB_VERSION_MAJOR >= 4
ret = dbenv->txn_checkpoint(dbenv,
-#else
- ret = txn_checkpoint(dbenv,
-#endif
MAX_CHECKPOINT_KBYTES,
MAX_CHECKPOINT_MINUTES,
0);
-/* The DB_INCOMPLETE error is no longer possible (or even defined) as of
- * Berkeley DB v4.1. When we get to the point where v4.0 and earlier are no
- * longer supported, we can remove this ifdef.
- */
-#ifdef DB_INCOMPLETE
- if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
- lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
- db_strerror(ret));
- abort();
- }
- if (ret == DB_INCOMPLETE) {
- lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
- }
-#else /* DB_INCOMPLETE */
if (ret != 0) {
- lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
+ lprintf(CTDL_EMERG, "cdb_checkpoint: txn_checkpoint: %s\n",
db_strerror(ret));
abort();
}
-#endif /* DB_INCOMPLETE */
/* Cull the logs if we haven't done so for 24 hours */
if ((time(NULL) - last_cull) > 86400L) {
int i;
char dbfilename[SIZ];
u_int32_t flags = 0;
+ char dbdirname[PATH_MAX];
+
+ getcwd(dbdirname, sizeof dbdirname);
+ strcat(dbdirname, "/data");
- lprintf(9, "cdb_*: open_databases() starting\n");
+ lprintf(CTDL_DEBUG, "cdb_*: open_databases() starting\n");
+ lprintf(CTDL_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
+ lprintf(CTDL_INFO, " Linked db: %s\n", db_version(NULL, NULL, NULL));
#ifdef HAVE_ZLIB
- lprintf(5, "zlib compression version %s\n", zlibVersion());
+ lprintf(CTDL_INFO, "Linked zlib: %s\n", zlibVersion());
#endif
/*
* Silently try to create the database subdirectory. If it's
* already there, no problem.
*/
- system("exec mkdir data 2>/dev/null");
+ mkdir(dbdirname, 0700);
- lprintf(9, "cdb_*: Setting up DB environment\n");
+ lprintf(CTDL_DEBUG, "cdb_*: Setting up DB environment\n");
db_env_set_func_yield(sched_yield);
ret = db_env_create(&dbenv, 0);
if (ret) {
- lprintf(1, "cdb_*: db_env_create: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: db_env_create: %s\n", db_strerror(ret));
exit(ret);
}
dbenv->set_errpfx(dbenv, "citserver");
*/
ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
if (ret) {
- lprintf(1, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: set_cachesize: %s\n", db_strerror(ret));
dbenv->close(dbenv, 0);
exit(ret);
}
if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
- lprintf(1, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: set_lk_detect: %s\n", db_strerror(ret));
dbenv->close(dbenv, 0);
exit(ret);
}
flags = DB_CREATE|DB_RECOVER|DB_INIT_MPOOL|DB_PRIVATE|DB_INIT_TXN|
DB_INIT_LOCK|DB_THREAD;
- ret = dbenv->open(dbenv, "./data", flags, 0);
+ lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", dbdirname, flags);
+ ret = dbenv->open(dbenv, dbdirname, flags, 0);
if (ret) {
- lprintf(1, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
+ lprintf(CTDL_DEBUG, "cdb_*: dbenv->open: %s\n", db_strerror(ret));
dbenv->close(dbenv, 0);
exit(ret);
}
- lprintf(7, "cdb_*: Starting up DB\n");
+ lprintf(CTDL_INFO, "cdb_*: Starting up DB\n");
for (i = 0; i < MAXCDB; ++i) {
/* Create a database handle */
ret = db_create(&dbp[i], dbenv, 0);
if (ret) {
- lprintf(1, "cdb_*: db_create: %s\n", db_strerror(ret));
+ lprintf(CTDL_DEBUG, "cdb_*: db_create: %s\n", db_strerror(ret));
exit(ret);
}
snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
ret = dbp[i]->open(dbp[i],
-#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
- NULL, /* new parameter */
-#endif
+ NULL,
dbfilename,
NULL,
DB_BTREE,
DB_CREATE|DB_THREAD
-#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
|DB_AUTO_COMMIT
-#endif
,
0600);
if (ret) {
- lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
exit(ret);
}
}
if ((ret = pthread_key_create(&tsdkey, dest_tsd))) {
- lprintf(1, "cdb_*: pthread_key_create: %s\n", strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: pthread_key_create: %s\n", strerror(ret));
exit(1);
}
cdb_allocate_tsd();
CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
- lprintf(9, "cdb_*: open_databases() finished\n");
+ lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
}
cdb_free_tsd();
-#if DB_VERSION_MAJOR >= 4
if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
-#else
- if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
-#endif
- lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
abort();
}
for (a = 0; a < MAXCDB; ++a) {
- lprintf(7, "cdb_*: Closing database %d\n", a);
+ lprintf(CTDL_INFO, "cdb_*: Closing database %d\n", a);
ret = dbp[a]->close(dbp[a], 0);
if (ret) {
- lprintf(1, "cdb_*: db_close: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: db_close: %s\n", db_strerror(ret));
abort();
}
/* Close the handle. */
ret = dbenv->close(dbenv, 0);
if (ret) {
- lprintf(1, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_*: DBENV->close: %s\n", db_strerror(ret));
abort();
}
}
compressed_data,
sourceLen
) != Z_OK) {
- lprintf(1, "uncompress() error\n");
+ lprintf(CTDL_EMERG, "uncompress() error\n");
abort();
}
(uLongf) cdatalen,
1
) != Z_OK) {
- lprintf(1, "compress2() error\n");
+ lprintf(CTDL_EMERG, "compress2() error\n");
abort();
}
zheader.compressed_len = (size_t) destLen;
0); /* flags */
if (ret)
{
- lprintf(1, "cdb_store(%d): %s\n", cdb,
- db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_store(%d): %s\n", cdb, db_strerror(ret));
abort();
}
#ifdef HAVE_ZLIB
}
else
{
- lprintf(1, "cdb_store(%d): %s\n", cdb,
- db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_store(%d): %s\n", cdb, db_strerror(ret));
abort();
}
}
ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
if (ret)
{
- lprintf(1, "cdb_delete(%d): %s\n", cdb,
- db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
if (ret != DB_NOTFOUND)
abort();
}
}
else
{
- lprintf(1, "cdb_delete(%d): %s\n", cdb,
- db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
abort();
}
}
if (ret)
{
- lprintf(1, "localcursor: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "localcursor: %s\n", db_strerror(ret));
abort();
}
if ((ret != 0) && (ret != DB_NOTFOUND))
{
- lprintf(1, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
abort();
}
if (tempcdb == NULL)
{
- lprintf(2, "cdb_fetch: Cannot allocate memory for tempcdb\n");
+ lprintf(CTDL_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb\n");
abort();
}
*/
ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
if (ret) {
- lprintf(1, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
abort();
}
}
if (ret) {
if (ret != DB_NOTFOUND) {
- lprintf(1, "cdb_next_item(%d): %s\n",
+ lprintf(CTDL_EMERG, "cdb_next_item(%d): %s\n",
cdb, db_strerror(ret));
abort();
}
if (MYTID != NULL)
{
- lprintf(1, "cdb_begin_transaction: ERROR: nested transaction\n");
+ lprintf(CTDL_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
abort();
}
for (i = 0; i < MAXCDB; i++)
if (MYCURSORS[i] != NULL) {
- lprintf(1, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
+ lprintf(CTDL_WARNING, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i);
cclose(MYCURSORS[i]);
MYCURSORS[i] = NULL;
}
if (MYTID == NULL)
{
- lprintf(1, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
+ lprintf(CTDL_EMERG, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
abort();
}
else
{
DB_TXN *tid;
int ret;
-#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 2
u_int32_t count;
-#endif
if (MYTID != NULL)
{
- lprintf(1, "cdb_trunc must not be called in a transaction.\n");
+ lprintf(CTDL_EMERG, "cdb_trunc must not be called in a transaction.\n");
abort();
}
else
{
bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
-#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
- for (;;)
- {
- DBT key, data;
-
- /* Initialize the key/data pair so the flags aren't set. */
- memset(&key, 0, sizeof(key));
- memset(&data, 0, sizeof(data));
-
- txbegin(&tid);
-
- ret = dbp[cdb]->cursor(dbp[cdb], tid, &MYCURSORS[cdb], 0);
- if (ret)
- {
- lprintf(1, "cdb_trunc: db_cursor: %s\n", db_strerror(ret));
- abort();
- }
-
- ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
- &key, &data, DB_NEXT);
- if (ret)
- {
- cclose(MYCURSORS[cdb]);
- txabort(tid);
- if (ret == DB_LOCK_DEADLOCK)
- continue;
-
- if (ret == DB_NOTFOUND)
- break;
-
- lprintf(1, "cdb_trunc: c_get: %s\n", db_strerror(ret));
- abort();
- }
-
- ret = MYCURSORS[cdb]->c_del(MYCURSORS[cdb], 0);
- if (ret)
- {
- cclose(MYCURSORS[cdb]);
- txabort(tid);
- if (ret == DB_LOCK_DEADLOCK)
- continue;
-
- lprintf(1, "cdb_trunc: c_del: %s\n", db_strerror(ret));
- abort();
- }
-
- cclose(MYCURSORS[cdb]);
- txcommit(tid);
- }
-#else
retry:
txbegin(&tid);
}
else
{
- lprintf(1, "cdb_truncate(%d): %s\n", cdb,
- db_strerror(ret));
+ lprintf(CTDL_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret));
abort();
}
}
{
txcommit(tid);
}
-#endif
}
}