X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fdatabase.c;h=d60c7886ff105633039b50236c8c5c794096c140;hb=1ed760f6e9cd5e5c4b18ab0f18686d08e268b373;hp=d0aab07300d56305ba0c4203c74445b03c1317ad;hpb=f1ee61891901850ebbdee1e9440b363dc6df540a;p=citadel.git diff --git a/citadel/database.c b/citadel/database.c index d0aab0730..d60c7886f 100644 --- a/citadel/database.c +++ b/citadel/database.c @@ -1,22 +1,16 @@ /* * This is a data store backend for the Citadel server which uses Berkeley DB. * - * Copyright (c) 1987-2011 by the citadel.org team + * Copyright (c) 1987-2012 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 as published - * by the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. + * 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. * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * */ /***************************************************************************** @@ -43,6 +37,7 @@ #include #include #include +#include #ifdef HAVE_DB_H #include @@ -76,16 +71,23 @@ static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */ static DB_ENV *dbenv; /* The DB environment (global) */ -#ifdef HAVE_ZLIB -#include -#endif +void cdb_abort(void) { + syslog(LOG_DEBUG, + "citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d", + getuid(), + getgid(), + geteuid(), + getegid() + ); + exit(CTDLEXIT_DB); +} /* Verbose logging callback */ void cdb_verbose_log(const DB_ENV *dbenv, const char *msg) { if (!IsEmptyStr(msg)) { - syslog(LOG_DEBUG, "DB: %s\n", msg); + syslog(LOG_DEBUG, "DB: %s", msg); } } @@ -93,7 +95,7 @@ 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) { - syslog(LOG_ALERT, "DB: %s\n", msg); + syslog(LOG_ALERT, "DB: %s", msg); } @@ -105,8 +107,8 @@ static void txabort(DB_TXN * tid) ret = tid->abort(tid); if (ret) { - syslog(LOG_EMERG, "bdb(): txn_abort: %s\n", db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret)); + cdb_abort(); } } @@ -118,8 +120,8 @@ static void txcommit(DB_TXN * tid) ret = tid->commit(tid, 0); if (ret) { - syslog(LOG_EMERG, "bdb(): txn_commit: %s\n", db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret)); + cdb_abort(); } } @@ -131,14 +133,14 @@ static void txbegin(DB_TXN ** tid) ret = dbenv->txn_begin(dbenv, NULL, tid, 0); if (ret) { - syslog(LOG_EMERG, "bdb(): txn_begin: %s\n", db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret)); + cdb_abort(); } } static void dbpanic(DB_ENV * env, int errval) { - syslog(LOG_EMERG, "bdb(): PANIC: %s\n", db_strerror(errval)); + syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval)); } static void cclose(DBC * cursor) @@ -146,8 +148,8 @@ static void cclose(DBC * cursor) int ret; if ((ret = cursor->c_close(cursor))) { - syslog(LOG_EMERG, "bdb(): c_close: %s\n", db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret)); + cdb_abort(); } } @@ -157,29 +159,20 @@ static void bailIfCursor(DBC ** cursors, const char *msg) for (i = 0; i < MAXCDB; i++) if (cursors[i] != NULL) { - syslog(LOG_EMERG, - "bdb(): cursor still in progress on cdb %02x: %s\n", i, msg); - abort(); + syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg); + cdb_abort(); } } -void check_handles(void *arg) -{ - if (arg != NULL) { - ThreadTSD *tsd = (ThreadTSD *) arg; - - bailIfCursor(tsd->cursors, "in check_handles"); - - if (tsd->tid != NULL) { - syslog(LOG_EMERG, "bdb(): transaction still in progress!"); - abort(); - } - } -} void cdb_check_handles(void) { - check_handles(pthread_getspecific(ThreadKey)); + bailIfCursor(TSD->cursors, "in check_handles"); + + if (TSD->tid != NULL) { + syslog(LOG_EMERG, "bdb(): transaction still in progress!"); + cdb_abort(); + } } @@ -197,14 +190,14 @@ static void cdb_cull_logs(void) /* Get the list of names. */ if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) { - syslog(LOG_ERR, "cdb_cull_logs: %s\n", db_strerror(ret)); + syslog(LOG_ERR, "cdb_cull_logs: %s", db_strerror(ret)); return; } /* Print the list of names. */ if (list != NULL) { for (file = list; *file != NULL; ++file) { - syslog(LOG_DEBUG, "Deleting log: %s\n", *file); + syslog(LOG_DEBUG, "Deleting log: %s", *file); ret = unlink(*file); if (ret != 0) { snprintf(errmsg, sizeof(errmsg), @@ -228,7 +221,7 @@ static void cdb_cull_logs(void) void cmd_cull(char *argbuf) { if (CtdlAccessCheck(ac_internal)) return; cdb_cull_logs(); - cprintf("%d Database log file cull completed.\n", CIT_OK); + cprintf("%d Database log file cull completed.", CIT_OK); } @@ -239,12 +232,12 @@ void cdb_checkpoint(void) { int ret; - syslog(LOG_DEBUG, "-- db checkpoint --\n"); + syslog(LOG_DEBUG, "-- db checkpoint --"); ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0); if (ret != 0) { - syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret)); + cdb_abort(); } /* After a successful checkpoint, we can cull the unused logs */ @@ -270,31 +263,29 @@ void open_databases(void) int dbversion_major, dbversion_minor, dbversion_patch; int current_dbversion = 0; - syslog(LOG_DEBUG, "bdb(): open_databases() starting\n"); - syslog(LOG_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING); - syslog(LOG_INFO, " Linked db: %s\n", + syslog(LOG_DEBUG, "bdb(): open_databases() starting"); + syslog(LOG_DEBUG, "Compiled db: %s", DB_VERSION_STRING); + syslog(LOG_INFO, " Linked db: %s", db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)); current_dbversion = (dbversion_major * 1000000) + (dbversion_minor * 1000) + dbversion_patch; - syslog(LOG_DEBUG, "Calculated dbversion: %d\n", current_dbversion); - syslog(LOG_DEBUG, " Previous dbversion: %d\n", CitControl.MMdbversion); + syslog(LOG_DEBUG, "Calculated dbversion: %d", current_dbversion); + syslog(LOG_DEBUG, " Previous dbversion: %d", CitControl.MMdbversion); if ( (getenv("SUPPRESS_DBVERSION_CHECK") == NULL) && (CitControl.MMdbversion > current_dbversion) ) { - syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version\n" - "of Berkeley DB that is older than that which last created or\n" - "updated the database. Because this would probably cause data\n" - "corruption or loss, the server is aborting execution now.\n"); + syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version"); + syslog(LOG_EMERG, "of Berkeley DB that is older than that which last created or"); + syslog(LOG_EMERG, "updated the database. Because this would probably cause data"); + syslog(LOG_EMERG, "corruption or loss, the server is aborting execution now."); exit(CTDLEXIT_DB); } CitControl.MMdbversion = current_dbversion; put_control(); -#ifdef HAVE_ZLIB syslog(LOG_INFO, "Linked zlib: %s\n", zlibVersion()); -#endif /* * Silently try to create the database subdirectory. If it's @@ -316,7 +307,7 @@ void open_databases(void) ctdl_data_dir, strerror(errno)); } syslog(LOG_DEBUG, "bdb(): Setting up DB environment\n"); - db_env_set_func_yield((int (*)(u_long, u_long))sched_yield); + /* db_env_set_func_yield((int (*)(u_long, u_long))sched_yield); */ ret = db_env_create(&dbenv, 0); if (ret) { syslog(LOG_EMERG, "bdb(): db_env_create: %s\n", db_strerror(ret)); @@ -453,8 +444,6 @@ void close_databases(void) int a; int ret; - ctdl_thread_internal_free_tsd(); - if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) { syslog(LOG_EMERG, "txn_checkpoint: %s\n", db_strerror(ret)); @@ -484,7 +473,7 @@ void close_databases(void) /* - * Compression functions only used if we have zlib + * Decompress a database item if it was compressed on disk */ void cdb_decompress_if_necessary(struct cdbdata *cdb) { @@ -496,7 +485,6 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb) (memcmp(cdb->ptr, &magic, sizeof(magic)))) return; -#ifdef HAVE_ZLIB /* At this point we know we're looking at a compressed item. */ struct CtdlCompressHeader zheader; @@ -523,16 +511,12 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb) (const Bytef *) compressed_data, (uLong) sourceLen) != Z_OK) { syslog(LOG_EMERG, "uncompress() error\n"); - abort(); + cdb_abort(); } free(cdb->ptr); cdb->len = (size_t) destLen; cdb->ptr = uncompressed_data; -#else /* HAVE_ZLIB */ - syslog(LOG_EMERG, "Database contains compressed data, but this citserver was built without compression support.\n"); - abort(); -#endif /* HAVE_ZLIB */ } @@ -548,13 +532,11 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) DB_TXN *tid; int ret = 0; -#ifdef HAVE_ZLIB struct CtdlCompressHeader zheader; char *compressed_data = NULL; int compressing = 0; size_t buffer_len = 0; uLongf destLen = 0; -#endif memset(&dkey, 0, sizeof(DBT)); memset(&ddata, 0, sizeof(DBT)); @@ -563,7 +545,6 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) ddata.size = cdatalen; ddata.data = cdata; -#ifdef HAVE_ZLIB /* Only compress Visit records. Everything else is uncompressed. */ if (cdb == CDB_VISIT) { compressing = 1; @@ -577,33 +558,30 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK) { syslog(LOG_EMERG, "compress2() error\n"); - abort(); + cdb_abort(); } zheader.compressed_len = (size_t) destLen; memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader)); ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len); ddata.data = compressed_data; } -#endif - if (MYTID != NULL) { + if (TSD->tid != NULL) { ret = dbp[cdb]->put(dbp[cdb], /* db */ - MYTID, /* transaction ID */ + TSD->tid, /* transaction ID */ &dkey, /* key */ &ddata, /* data */ 0); /* flags */ if (ret) { - syslog(LOG_EMERG, "cdb_store(%d): %s\n", cdb, db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret)); + cdb_abort(); } -#ifdef HAVE_ZLIB if (compressing) free(compressed_data); -#endif return ret; } else { - bailIfCursor(MYCURSORS, "attempt to write during r/o cursor"); + bailIfCursor(TSD->cursors, "attempt to write during r/o cursor"); retry: txbegin(&tid); @@ -617,20 +595,18 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) txabort(tid); goto retry; } else { - syslog(LOG_EMERG, "cdb_store(%d): %s\n", - cdb, db_strerror(ret)); - abort(); + syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret)); + cdb_abort(); } } else { txcommit(tid); -#ifdef HAVE_ZLIB if (compressing) { free(compressed_data); } -#endif return ret; } } + return ret; } @@ -648,16 +624,16 @@ int cdb_delete(int cdb, void *key, int keylen) dkey.size = keylen; dkey.data = key; - if (MYTID != NULL) { - ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0); + if (TSD->tid != NULL) { + ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0); if (ret) { syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret)); if (ret != DB_NOTFOUND) { - abort(); + cdb_abort(); } } } else { - bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor"); + bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor"); retry: txbegin(&tid); @@ -670,7 +646,7 @@ int cdb_delete(int cdb, void *key, int keylen) } else { syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret)); - abort(); + cdb_abort(); } } else { txcommit(tid); @@ -684,16 +660,14 @@ static DBC *localcursor(int cdb) int ret; DBC *curs; - if (MYCURSORS[cdb] == NULL) - ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0); + if (TSD->cursors[cdb] == NULL) + ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0); else - ret = - MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs, - DB_POSITION); + ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION); if (ret) { syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret)); - abort(); + cdb_abort(); } return curs; @@ -716,10 +690,10 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) dkey.size = keylen; dkey.data = key; - if (MYTID != NULL) { + if (TSD->tid != NULL) { memset(&dret, 0, sizeof(DBT)); dret.flags = DB_DBT_MALLOC; - ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0); + ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0); } else { DBC *curs; @@ -738,7 +712,7 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) if ((ret != 0) && (ret != DB_NOTFOUND)) { syslog(LOG_EMERG, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret)); - abort(); + cdb_abort(); } if (ret != 0) @@ -747,7 +721,7 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) if (tempcdb == NULL) { syslog(LOG_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb\n"); - abort(); + cdb_abort(); } tempcdb->len = dret.size; @@ -775,11 +749,11 @@ void cdb_free(struct cdbdata *cdb) void cdb_close_cursor(int cdb) { - if (MYCURSORS[cdb] != NULL) { - cclose(MYCURSORS[cdb]); + if (TSD->cursors[cdb] != NULL) { + cclose(TSD->cursors[cdb]); } - MYCURSORS[cdb] = NULL; + TSD->cursors[cdb] = NULL; } /* @@ -791,20 +765,20 @@ void cdb_rewind(int cdb) { int ret = 0; - if (MYCURSORS[cdb] != NULL) { + if (TSD->cursors[cdb] != NULL) { syslog(LOG_EMERG, "cdb_rewind: must close cursor on database %d before reopening.\n", cdb); - abort(); - /* cclose(MYCURSORS[cdb]); */ + cdb_abort(); + /* cclose(TSD->cursors[cdb]); */ } /* * Now initialize the cursor */ - ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0); + ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0); if (ret) { syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret)); - abort(); + cdb_abort(); } } @@ -824,15 +798,14 @@ struct cdbdata *cdb_next_item(int cdb) memset(&data, 0, sizeof(data)); data.flags = DB_DBT_MALLOC; - ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb], &key, &data, DB_NEXT); + ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT); if (ret) { if (ret != DB_NOTFOUND) { syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret)); - abort(); + cdb_abort(); } - cclose(MYCURSORS[cdb]); - MYCURSORS[cdb] = NULL; + cdb_close_cursor(cdb); return NULL; /* presumably, end of file */ } @@ -853,14 +826,14 @@ struct cdbdata *cdb_next_item(int cdb) void cdb_begin_transaction(void) { - bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor"); + bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor"); - if (MYTID != NULL) { + if (TSD->tid != NULL) { syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n"); - abort(); + cdb_abort(); } - txbegin(&MYTID); + txbegin(&TSD->tid); } void cdb_end_transaction(void) @@ -868,23 +841,23 @@ void cdb_end_transaction(void) int i; for (i = 0; i < MAXCDB; i++) - if (MYCURSORS[i] != NULL) { + if (TSD->cursors[i] != NULL) { syslog(LOG_WARNING, "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n", i); - cclose(MYCURSORS[i]); - MYCURSORS[i] = NULL; + cclose(TSD->cursors[i]); + TSD->cursors[i] = NULL; } - if (MYTID == NULL) { + if (TSD->tid == NULL) { syslog(LOG_EMERG, "cdb_end_transaction: ERROR: txcommit(NULL) !!\n"); - abort(); + cdb_abort(); } else { - txcommit(MYTID); + txcommit(TSD->tid); } - MYTID = NULL; + TSD->tid = NULL; } /* @@ -896,12 +869,11 @@ void cdb_trunc(int cdb) int ret; u_int32_t count; - if (MYTID != NULL) { - syslog(LOG_EMERG, - "cdb_trunc must not be called in a transaction.\n"); - abort(); + if (TSD->tid != NULL) { + syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction."); + cdb_abort(); } else { - bailIfCursor(MYCURSORS, "attempt to write during r/o cursor"); + bailIfCursor(TSD->cursors, "attempt to write during r/o cursor"); retry: /* txbegin(&tid); */ @@ -916,9 +888,9 @@ void cdb_trunc(int cdb) } else { syslog(LOG_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret)); if (ret == ENOMEM) { - syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information.\n"); + syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information."); } - abort(); + exit(CTDLEXIT_DB); } } else { /* txcommit(tid); */