/*****************************************************************************/
-#ifdef DLL_EXPORT
-#define IN_LIBCIT
-#endif
-
#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
}
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);
}
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);
}
/* 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);
}
/*
struct dirent *d;
char filename[PATH_MAX];
-
- getcwd(dbdirname, sizeof dbdirname);
- strcat(dbdirname, "/data");
+ strcat(dbdirname,ctdl_data_dir);
lprintf(CTDL_DEBUG, "cdb_*: open_databases() starting\n");
lprintf(CTDL_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
}
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.
}
lprintf(CTDL_DEBUG, "cdb_*: open_databases() finished\n");
+
+ CtdlRegisterProtoHook(cmd_cull, "CULL", "Cull database logs");
}
/*
- * 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);
}