static DB_ENV *dbenv; // The DB environment (global)
+// Called by other functions in this module to GTFO quickly if we need to. Not part of the backend API.
void bdb_abort(void) {
syslog(LOG_DEBUG, "bdb: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
getuid(), getgid(), geteuid(), getegid()
}
-// Verbose logging callback
+// Verbose logging callback for Berkeley DB. Not part of the backend API.
void bdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *foo) {
if (!IsEmptyStr(msg)) {
syslog(LOG_DEBUG, "bdb: %s %s", msg, foo);
}
-// Verbose logging callback
+// Verbose error logging callback for Berkeley DB. Not part of the backend API.
void bdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg) {
syslog(LOG_ERR, "bdb: %s", msg);
}
// wrapper for txn_abort() that logs/aborts on error
-static void txabort(DB_TXN *tid) {
+static void bdb_txabort(DB_TXN *tid) {
int ret;
ret = tid->abort(tid);
// wrapper for txn_commit() that logs/aborts on error
-static void txcommit(DB_TXN *tid) {
+static void bdb_txcommit(DB_TXN *tid) {
int ret;
ret = tid->commit(tid, 0);
// wrapper for txn_begin() that logs/aborts on error
-static void txbegin(DB_TXN **tid) {
+static void bdb_txbegin(DB_TXN **tid) {
int ret;
ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
// panic callback
-static void dbpanic(DB_ENV *env, int errval) {
+static void bdb_dbpanic(DB_ENV *env, int errval) {
syslog(LOG_ERR, "bdb: PANIC: %s", db_strerror(errval));
bdb_abort();
}
-static void cclose(DBC *cursor) {
+static void bdb_cclose(DBC *cursor) {
int ret;
if ((ret = cursor->c_close(cursor))) {
}
-static void bailIfCursor(DBC **cursors, const char *msg) {
+static void bdb_bailIfCursor(DBC **cursors, const char *msg) {
int i;
for (i = 0; i < MAXCDB; i++)
void bdb_check_handles(void) {
- bailIfCursor(TSD->cursors, "in check_handles");
+ bdb_bailIfCursor(TSD->cursors, "in check_handles");
if (TSD->tid != NULL) {
syslog(LOG_ERR, "bdb: transaction still in progress!");
exit(CTDLEXIT_DB);
}
dbenv->set_errpfx(dbenv, "citserver");
- dbenv->set_paniccall(dbenv, dbpanic);
+ dbenv->set_paniccall(dbenv, bdb_dbpanic);
dbenv->set_errcall(dbenv, bdb_verbose_err);
dbenv->set_msgcall(dbenv, bdb_verbose_log);
dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
return ret;
}
else {
- bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
+ bdb_bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
retry:
- txbegin(&tid);
+ bdb_txbegin(&tid);
if ((ret = dbp[cdb]->put(dbp[cdb], // db
tid, // transaction ID
&ddata, // data
0))) { // flags
if (ret == DB_LOCK_DEADLOCK) {
- txabort(tid);
+ bdb_txabort(tid);
goto retry;
}
else {
}
}
else {
- txcommit(tid);
+ bdb_txcommit(tid);
if (compressing) {
free(compressed_data);
}
}
}
else {
- bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
+ bdb_bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
retry:
- txbegin(&tid);
+ bdb_txbegin(&tid);
if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0)) && ret != DB_NOTFOUND) {
if (ret == DB_LOCK_DEADLOCK) {
- txabort(tid);
+ bdb_txabort(tid);
goto retry;
}
else {
}
}
else {
- txcommit(tid);
+ bdb_txcommit(tid);
}
}
return ret;
}
-static DBC *localcursor(int cdb) {
+static DBC *bdb_localcursor(int cdb) {
int ret;
DBC *curs;
}
if (ret) {
- syslog(LOG_ERR, "bdb: localcursor: %s", db_strerror(ret));
+ syslog(LOG_ERR, "bdb: bdb_localcursor: %s", db_strerror(ret));
bdb_abort();
}
do {
memset(&dret, 0, sizeof(DBT));
dret.flags = DB_DBT_MALLOC;
- curs = localcursor(cdb);
+ curs = bdb_localcursor(cdb);
ret = curs->c_get(curs, &dkey, &dret, DB_SET);
- cclose(curs);
+ bdb_cclose(curs);
} while (ret == DB_LOCK_DEADLOCK);
}
void bdb_close_cursor(int cdb) {
if (TSD->cursors[cdb] != NULL) {
- cclose(TSD->cursors[cdb]);
+ bdb_cclose(TSD->cursors[cdb]);
}
TSD->cursors[cdb] = NULL;
if (TSD->cursors[cdb] != NULL) {
syslog(LOG_ERR, "bdb: bdb_rewind: must close cursor on database %d before reopening", cdb);
bdb_abort();
- // cclose(TSD->cursors[cdb]);
+ // bdb_cclose(TSD->cursors[cdb]);
}
// Now initialize the cursor
// Transaction-based stuff. I'm writing this as I bake cookies...
void bdb_begin_transaction(void) {
- bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
+ bdb_bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
if (TSD->tid != NULL) {
syslog(LOG_ERR, "bdb: bdb_begin_transaction: ERROR: nested transaction");
bdb_abort();
}
- txbegin(&TSD->tid);
+ bdb_txbegin(&TSD->tid);
}
for (i = 0; i < MAXCDB; i++) {
if (TSD->cursors[i] != NULL) {
syslog(LOG_WARNING, "bdb: bdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
- cclose(TSD->cursors[i]);
+ bdb_cclose(TSD->cursors[i]);
TSD->cursors[i] = NULL;
}
}
if (TSD->tid == NULL) {
- syslog(LOG_ERR, "bdb: bdb_end_transaction: ERROR: txcommit(NULL) !!");
+ syslog(LOG_ERR, "bdb: bdb_end_transaction: ERROR: bdb_txcommit(NULL) !!");
bdb_abort();
}
else {
- txcommit(TSD->tid);
+ bdb_txcommit(TSD->tid);
}
TSD->tid = NULL;
bdb_abort();
}
else {
- bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
+ bdb_bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
retry: