]> code.citadel.org Git - citadel.git/blobdiff - citadel/database_sleepycat.c
* added message subject to all those tiny messages
[citadel.git] / citadel / database_sleepycat.c
index 1e2b4f6cebaa92f8f978952dbdbdb13117b22c3b..0573ae5de032cfce2ac565ba0e96768a3459caf5 100644 (file)
 
 /*****************************************************************************/
 
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
@@ -248,13 +244,20 @@ static void cdb_cull_logs(void)
                                         " This log file is no longer in use "
                                         "and may be safely deleted.\n",
                                         *file, strerror(errno));
-                               aide_message(errmsg);
+                               aide_message(errmsg, "Database Warning Message");
                        }
                }
                free(list);
        }
+}
 
-       lprintf(CTDL_INFO, "Database log file cull ended.\n");
+/*
+ * Manually initiate log file cull.
+ */
+void cmd_cull(char *argbuf) {
+       if (CtdlAccessCheck(ac_internal)) return;
+       cdb_cull_logs();
+       cprintf("%d Database log file cull completed.\n", CIT_OK);
 }
 
 
@@ -264,8 +267,15 @@ static void cdb_cull_logs(void)
 static void cdb_checkpoint(void)
 {
        int ret;
-       /* static time_t last_cull = 0L; */
+       static time_t last_run = 0L;
+
+       /* Only do a checkpoint once per minute. */
+       if ((time(NULL) - last_run) < 60L) {
+               return;
+       }
+       last_run = time(NULL);
 
+       lprintf(CTDL_DEBUG, "-- db checkpoint --\n");
        ret = dbenv->txn_checkpoint(dbenv,
                                    MAX_CHECKPOINT_KBYTES,
                                    MAX_CHECKPOINT_MINUTES, 0);
@@ -277,14 +287,34 @@ static void cdb_checkpoint(void)
        }
 
        /* After a successful checkpoint, we can cull the unused logs */
-       cdb_cull_logs();
+       if (config.c_auto_cull) {
+               cdb_cull_logs();
+       }
+}
 
-       /* Cull the logs if we haven't done so for 24 hours
-          if ((time(NULL) - last_cull) > 86400L) {
-          last_cull = time(NULL);
-          cdb_cull_logs();
-          } */
 
+/*
+ * Main loop for the checkpoint thread.
+ */
+void *checkpoint_thread(void *arg) {
+       struct CitContext checkpointCC;
+
+       lprintf(CTDL_DEBUG, "checkpoint_thread() initializing\n");
+
+       memset(&checkpointCC, 0, sizeof(struct CitContext));
+       checkpointCC.internal_pgm = 1;
+       checkpointCC.cs_pid = 0;
+       pthread_setspecific(MyConKey, (void *)&checkpointCC );
+
+       cdb_allocate_tsd();
+
+       while (!time_to_die) {
+               cdb_checkpoint();
+               sleep(1);
+       }
+
+       lprintf(CTDL_DEBUG, "checkpoint_thread() exiting\n");
+       pthread_exit(NULL);
 }
 
 /*
@@ -299,15 +329,10 @@ void open_databases(void)
        int i;
        char dbfilename[SIZ];
        u_int32_t flags = 0;
-       char dbdirname[PATH_MAX];
        DIR *dp;
        struct dirent *d;
        char filename[PATH_MAX];
 
-
-       getcwd(dbdirname, sizeof dbdirname);
-       strcat(dbdirname, "/data");
-
        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",
@@ -320,9 +345,9 @@ void open_databases(void)
         * Silently try to create the database subdirectory.  If it's
         * already there, no problem.
         */
-       mkdir(dbdirname, 0700);
-       chmod(dbdirname, 0700);
-       chown(dbdirname, CTDLUID, (-1));
+       mkdir(ctdl_data_dir, 0700);
+       chmod(ctdl_data_dir, 0700);
+       chown(ctdl_data_dir, CTDLUID, (-1));
 
        lprintf(CTDL_DEBUG, "cdb_*: Setting up DB environment\n");
        db_env_set_func_yield(sched_yield);
@@ -357,9 +382,9 @@ void open_databases(void)
        flags =
            DB_CREATE | DB_RECOVER | DB_INIT_MPOOL | DB_PRIVATE |
            DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD;
-       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", dbdirname,
+       lprintf(CTDL_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", ctdl_data_dir,
                flags);
-       ret = dbenv->open(dbenv, dbdirname, flags, 0);
+       ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
        if (ret) {
                lprintf(CTDL_DEBUG, "cdb_*: dbenv->open: %s\n",
                        db_strerror(ret));
@@ -406,17 +431,16 @@ void open_databases(void)
        }
 
        cdb_allocate_tsd();
-       CtdlRegisterSessionHook(cdb_checkpoint, EVT_TIMER);
 
        /* Now make sure we own all the files, because in a few milliseconds
         * we're going to drop root privs.
         */
-       dp = opendir(dbdirname);
+       dp = opendir(ctdl_data_dir);
        if (dp != NULL) {
                while (d = readdir(dp), d != NULL) {
                        if (d->d_name[0] != '.') {
                                snprintf(filename, sizeof filename,
-                                        "%s/%s", dbdirname, d->d_name);
+                                        "%s/%s", ctdl_data_dir, d->d_name);
                                chmod(filename, 0600);
                                chown(filename, CTDLUID, (-1));
                        }
@@ -425,6 +449,8 @@ void open_databases(void)
        }
 
        lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
+
+       CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
 }
 
 
@@ -740,12 +766,18 @@ struct cdbdata *cdb_fetch(int cdb, void *key, int keylen)
 
 
 /*
- * Free a cdbdata item (ok, this is really no big deal, but we might need to do
- * more complex stuff with other database managers in the future).
+ * Free a cdbdata item.
+ *
+ * Note that we only free the 'ptr' portion if it is not NULL.  This allows
+ * other code to assume ownership of that memory simply by storing the
+ * pointer elsewhere and then setting 'ptr' to NULL.  cdb_free() will then
+ * avoid freeing it.
  */
 void cdb_free(struct cdbdata *cdb)
 {
-       free(cdb->ptr);
+       if (cdb->ptr) {
+               free(cdb->ptr);
+       }
        free(cdb);
 }