CURL: make log output configurable.
[citadel.git] / citadel / database.c
index 141e74bf75bb1cf3cf2311a3b751b76390f97bf3..26cce16330682e5b5c24f94ed4956bf2166916c7 100644 (file)
@@ -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 <sys/stat.h>
 #include <dirent.h>
 #include <syslog.h>
+#include <zlib.h>
 
 #ifdef HAVE_DB_H
 #include <db.h>
@@ -76,9 +71,17 @@ static DB *dbp[MAXCDB];              /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
 
-#ifdef HAVE_ZLIB
-#include <zlib.h>
-#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()
+       );
+       cit_backtrace();
+       exit(CTDLEXIT_DB);
+}
 
 
 /* Verbose logging callback */
@@ -106,7 +109,7 @@ static void txabort(DB_TXN * tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -119,7 +122,7 @@ static void txcommit(DB_TXN * tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -132,7 +135,7 @@ static void txbegin(DB_TXN ** tid)
 
        if (ret) {
                syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -147,7 +150,7 @@ static void cclose(DBC * cursor)
 
        if ((ret = cursor->c_close(cursor))) {
                syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 }
 
@@ -158,7 +161,7 @@ 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", i, msg);
-                       abort();
+                       cdb_abort();
                }
 }
 
@@ -169,7 +172,7 @@ void cdb_check_handles(void)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "bdb(): transaction still in progress!");
-               abort();
+               cdb_abort();
        }
 }
 
@@ -219,7 +222,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.", CIT_OK);
+       cprintf("%d Database log file cull completed.\n", CIT_OK);
 }
 
 
@@ -235,7 +238,7 @@ void cdb_checkpoint(void)
 
        if (ret != 0) {
                syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 
        /* After a successful checkpoint, we can cull the unused logs */
@@ -283,9 +286,7 @@ void open_databases(void)
        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
@@ -395,7 +396,7 @@ void open_databases(void)
                if (ret) {
                        syslog(LOG_EMERG, "db_open[%02x]: %s\n", i, 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?id=faq:troubleshooting:out_of_lock_entries for more information.");
                        }
                        syslog(LOG_EMERG, "exit code %d\n", ret);
                        exit(CTDLEXIT_DB);
@@ -473,7 +474,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)
 {
@@ -485,7 +486,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;
@@ -512,17 +512,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.");
-       syslog(LOG_EMERG, "Please see http://www.citadel.org/doku.php/faq:installation:zlib for more information.");
-       exit(CTDLEXIT_DB);
-#endif                         /* HAVE_ZLIB */
 }
 
 
@@ -531,20 +526,18 @@ 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, void *ckey, int ckeylen, void *cdata, int cdatalen)
 {
 
        DBT dkey, ddata;
        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));
@@ -553,7 +546,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;
@@ -567,14 +559,13 @@ 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 (TSD->tid != NULL) {
                ret = dbp[cdb]->put(dbp[cdb],   /* db */
@@ -584,12 +575,10 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                                    0); /* flags */
                if (ret) {
                        syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
-                       abort();
+                       cdb_abort();
                }
-#ifdef HAVE_ZLIB
                if (compressing)
                        free(compressed_data);
-#endif
                return ret;
 
        } else {
@@ -608,18 +597,17 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                                goto retry;
                        } else {
                                syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
-                               abort();
+                               cdb_abort();
                        }
                } else {
                        txcommit(tid);
-#ifdef HAVE_ZLIB
                        if (compressing) {
                                free(compressed_data);
                        }
-#endif
                        return ret;
                }
        }
+       return ret;
 }
 
 
@@ -642,7 +630,7 @@ int cdb_delete(int cdb, void *key, int keylen)
                if (ret) {
                        syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
                        if (ret != DB_NOTFOUND) {
-                               abort();
+                               cdb_abort();
                        }
                }
        } else {
@@ -659,7 +647,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);
@@ -680,7 +668,7 @@ static DBC *localcursor(int cdb)
 
        if (ret) {
                syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret));
-               abort();
+               cdb_abort();
        }
 
        return curs;
@@ -692,7 +680,7 @@ 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, void *key, int keylen)
 {
 
        struct cdbdata *tempcdb;
@@ -725,7 +713,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)
@@ -734,7 +722,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;
@@ -781,7 +769,7 @@ void cdb_rewind(int cdb)
        if (TSD->cursors[cdb] != NULL) {
                syslog(LOG_EMERG,
                        "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
-               abort();
+               cdb_abort();
                /* cclose(TSD->cursors[cdb]); */
        }
 
@@ -791,7 +779,7 @@ void cdb_rewind(int cdb)
        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();
        }
 }
 
@@ -816,7 +804,7 @@ struct cdbdata *cdb_next_item(int cdb)
        if (ret) {
                if (ret != DB_NOTFOUND) {
                        syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret));
-                       abort();
+                       cdb_abort();
                }
                cdb_close_cursor(cdb);
                return NULL;    /* presumably, end of file */
@@ -843,7 +831,7 @@ void cdb_begin_transaction(void)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
-               abort();
+               cdb_abort();
        }
 
        txbegin(&TSD->tid);
@@ -865,7 +853,7 @@ void cdb_end_transaction(void)
        if (TSD->tid == NULL) {
                syslog(LOG_EMERG,
                        "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
-               abort();
+               cdb_abort();
        } else {
                txcommit(TSD->tid);
        }
@@ -884,7 +872,7 @@ void cdb_trunc(int cdb)
 
        if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
-               abort();
+               cdb_abort();
        } else {
                bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
 
@@ -901,7 +889,7 @@ 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.");
+                                       syslog(LOG_EMERG, "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);
                        }