2 * This is a data store backend for the Citadel server which uses Berkeley DB.
4 * Copyright (c) 1987-2012 by the citadel.org team
6 * This program is open source software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 /*****************************************************************************
17 Tunable configuration parameters for the Berkeley DB back end
18 *****************************************************************************/
20 /* Citadel will checkpoint the db at the end of every session, but only if
21 * the specified number of kilobytes has been written, or if the specified
22 * number of minutes has passed, since the last checkpoint.
24 #define MAX_CHECKPOINT_KBYTES 256
25 #define MAX_CHECKPOINT_MINUTES 15
27 /*****************************************************************************/
36 #elif defined(HAVE_DB4_DB_H)
39 #error Neither <db.h> nor <db4/db.h> was found by configure. Install db4-devel.
43 #if DB_VERSION_MAJOR < 4 || DB_VERSION_MINOR < 1
44 #error Citadel requires Berkeley DB v4.1 or newer. Please upgrade.
48 #include <libcitadel.h>
50 #include "ctdl_module.h"
52 #include "citserver.h"
55 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
56 static DB_ENV *dbenv; /* The DB environment (global) */
59 void cdb_abort(void) {
61 "citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
72 /* Verbose logging callback */
73 void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
75 if (!IsEmptyStr(msg)) {
76 syslog(LOG_DEBUG, "DB: %s", msg);
82 /* Verbose logging callback */
83 void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
86 syslog(LOG_ALERT, "DB: %s", msg);
92 /* just a little helper function */
93 static void txabort(DB_TXN * tid)
97 ret = tid->abort(tid);
100 syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
105 /* this one is even more helpful than the last. */
106 static void txcommit(DB_TXN * tid)
110 ret = tid->commit(tid, 0);
113 syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
118 /* are you sensing a pattern yet? */
119 static void txbegin(DB_TXN ** tid)
123 ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
126 syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
131 static void dbpanic(DB_ENV * env, int errval)
133 syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval));
137 static void cclose(DBC * cursor)
141 if ((ret = cursor->c_close(cursor))) {
142 syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
147 static void bailIfCursor(DBC ** cursors, const char *msg)
151 for (i = 0; i < MAXCDB; i++)
152 if (cursors[i] != NULL) {
153 syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg);
159 void cdb_check_handles(void)
161 bailIfCursor(TSD->cursors, "in check_handles");
163 if (TSD->tid != NULL) {
164 syslog(LOG_EMERG, "bdb(): transaction still in progress!");
171 * Cull the database logs
173 void cdb_cull_logs(void)
182 /* Get the list of names. */
183 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
184 syslog(LOG_ERR, "cdb_cull_logs: %s", db_strerror(ret));
188 /* Print the list of names. */
190 for (file = list; *file != NULL; ++file) {
191 syslog(LOG_DEBUG, "Deleting log: %s", *file);
194 snprintf(errmsg, sizeof(errmsg),
195 " ** ERROR **\n \n \n "
196 "Citadel was unable to delete the "
197 "database log file '%s' because of the "
198 "following error:\n \n %s\n \n"
199 " This log file is no longer in use "
200 "and may be safely deleted.\n",
201 *file, strerror(errno));
202 CtdlAideMessage(errmsg, "Database Warning Message");
211 * Request a checkpoint of the database. Called once per minute by the thread manager.
213 void cdb_checkpoint(void)
217 syslog(LOG_DEBUG, "-- db checkpoint --");
218 ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
221 syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
225 /* After a successful checkpoint, we can cull the unused logs */
226 if (config.c_auto_cull) {
234 * Open the various databases we'll be using. Any database which
235 * does not exist should be created. Note that we don't need a
236 * critical section here, because there aren't any active threads
237 * manipulating the database yet.
239 void open_databases(void)
245 int dbversion_major, dbversion_minor, dbversion_patch;
246 int current_dbversion = 0;
248 syslog(LOG_DEBUG, "bdb(): open_databases() starting");
249 syslog(LOG_DEBUG, "Compiled db: %s", DB_VERSION_STRING);
250 syslog(LOG_INFO, " Linked db: %s",
251 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
253 current_dbversion = (dbversion_major * 1000000) + (dbversion_minor * 1000) + dbversion_patch;
255 syslog(LOG_DEBUG, "Calculated dbversion: %d", current_dbversion);
256 syslog(LOG_DEBUG, " Previous dbversion: %d", CitControl.MMdbversion);
258 if ( (getenv("SUPPRESS_DBVERSION_CHECK") == NULL)
259 && (CitControl.MMdbversion > current_dbversion) ) {
260 syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version");
261 syslog(LOG_EMERG, "of Berkeley DB that is older than that which last created or");
262 syslog(LOG_EMERG, "updated the database. Because this would probably cause data");
263 syslog(LOG_EMERG, "corruption or loss, the server is aborting execution now.");
267 CitControl.MMdbversion = current_dbversion;
270 syslog(LOG_INFO, "Linked zlib: %s\n", zlibVersion());
273 * Silently try to create the database subdirectory. If it's
274 * already there, no problem.
276 if ((mkdir(ctdl_data_dir, 0700) != 0) && (errno != EEXIST)){
278 "unable to create database directory [%s]: %s",
279 ctdl_data_dir, strerror(errno));
281 if (chmod(ctdl_data_dir, 0700) != 0){
283 "unable to set database directory accessrights [%s]: %s",
284 ctdl_data_dir, strerror(errno));
286 if (chown(ctdl_data_dir, CTDLUID, (-1)) != 0){
288 "unable to set the owner for [%s]: %s",
289 ctdl_data_dir, strerror(errno));
291 syslog(LOG_DEBUG, "bdb(): Setting up DB environment\n");
292 /* db_env_set_func_yield((int (*)(u_long, u_long))sched_yield); */
293 ret = db_env_create(&dbenv, 0);
295 syslog(LOG_EMERG, "bdb(): db_env_create: %s\n", db_strerror(ret));
296 syslog(LOG_EMERG, "exit code %d\n", ret);
299 dbenv->set_errpfx(dbenv, "citserver");
300 dbenv->set_paniccall(dbenv, dbpanic);
301 dbenv->set_errcall(dbenv, cdb_verbose_err);
302 dbenv->set_errpfx(dbenv, "ctdl");
303 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 3)
304 dbenv->set_msgcall(dbenv, cdb_verbose_log);
306 dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
307 dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
310 * We want to specify the shared memory buffer pool cachesize,
311 * but everything else is the default.
313 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
315 syslog(LOG_EMERG, "bdb(): set_cachesize: %s\n", db_strerror(ret));
316 dbenv->close(dbenv, 0);
317 syslog(LOG_EMERG, "exit code %d\n", ret);
321 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
322 syslog(LOG_EMERG, "bdb(): set_lk_detect: %s\n", db_strerror(ret));
323 dbenv->close(dbenv, 0);
324 syslog(LOG_EMERG, "exit code %d\n", ret);
328 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_RECOVER;
329 syslog(LOG_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", ctdl_data_dir, flags);
330 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
331 if (ret == DB_RUNRECOVERY) {
332 syslog(LOG_ALERT, "dbenv->open: %s\n", db_strerror(ret));
333 syslog(LOG_ALERT, "Attempting recovery...\n");
335 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
337 if (ret == DB_RUNRECOVERY) {
338 syslog(LOG_ALERT, "dbenv->open: %s\n", db_strerror(ret));
339 syslog(LOG_ALERT, "Attempting catastrophic recovery...\n");
340 flags &= ~DB_RECOVER;
341 flags |= DB_RECOVER_FATAL;
342 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
345 syslog(LOG_EMERG, "dbenv->open: %s\n", db_strerror(ret));
346 dbenv->close(dbenv, 0);
347 syslog(LOG_EMERG, "exit code %d\n", ret);
351 syslog(LOG_INFO, "Starting up DB\n");
353 for (i = 0; i < MAXCDB; ++i) {
355 /* Create a database handle */
356 ret = db_create(&dbp[i], dbenv, 0);
358 syslog(LOG_EMERG, "db_create: %s\n", db_strerror(ret));
359 syslog(LOG_EMERG, "exit code %d\n", ret);
364 /* Arbitrary names for our tables -- we reference them by
365 * number, so we don't have string names for them.
367 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
369 ret = dbp[i]->open(dbp[i],
374 DB_CREATE | DB_AUTO_COMMIT | DB_THREAD,
378 syslog(LOG_EMERG, "db_open[%02x]: %s\n", i, db_strerror(ret));
380 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.");
382 syslog(LOG_EMERG, "exit code %d\n", ret);
390 /* Make sure we own all the files, because in a few milliseconds
391 * we're going to drop root privs.
393 void cdb_chmod_data(void) {
396 char filename[PATH_MAX];
398 dp = opendir(ctdl_data_dir);
400 while (d = readdir(dp), d != NULL) {
401 if (d->d_name[0] != '.') {
402 snprintf(filename, sizeof filename,
403 "%s/%s", ctdl_data_dir, d->d_name);
404 syslog(LOG_DEBUG, "chmod(%s, 0600) returned %d\n",
405 filename, chmod(filename, 0600)
407 syslog(LOG_DEBUG, "chown(%s, CTDLUID, -1) returned %d\n",
408 filename, chown(filename, CTDLUID, (-1))
415 syslog(LOG_DEBUG, "open_databases() finished\n");
420 * Close all of the db database files we've opened. This can be done
421 * in a loop, since it's just a bunch of closes.
423 void close_databases(void)
428 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
430 "txn_checkpoint: %s\n", db_strerror(ret));
433 /* print some statistics... */
435 dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
438 /* close the tables */
439 for (a = 0; a < MAXCDB; ++a) {
440 syslog(LOG_INFO, "Closing database %02x\n", a);
441 ret = dbp[a]->close(dbp[a], 0);
443 syslog(LOG_EMERG, "db_close: %s\n", db_strerror(ret));
448 /* Close the handle. */
449 ret = dbenv->close(dbenv, 0);
451 syslog(LOG_EMERG, "DBENV->close: %s\n", db_strerror(ret));
457 * Decompress a database item if it was compressed on disk
459 void cdb_decompress_if_necessary(struct cdbdata *cdb)
461 static int magic = COMPRESS_MAGIC;
464 (cdb->ptr == NULL) ||
465 (cdb->len < sizeof(magic)) ||
466 (memcmp(cdb->ptr, &magic, sizeof(magic))))
469 /* At this point we know we're looking at a compressed item. */
471 struct CtdlCompressHeader zheader;
472 char *uncompressed_data;
473 char *compressed_data;
474 uLongf destLen, sourceLen;
477 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
478 cplen = sizeof(struct CtdlCompressHeader);
479 if (sizeof(struct CtdlCompressHeader) > cdb->len)
481 memcpy(&zheader, cdb->ptr, cplen);
483 compressed_data = cdb->ptr;
484 compressed_data += sizeof(struct CtdlCompressHeader);
486 sourceLen = (uLongf) zheader.compressed_len;
487 destLen = (uLongf) zheader.uncompressed_len;
488 uncompressed_data = malloc(zheader.uncompressed_len);
490 if (uncompress((Bytef *) uncompressed_data,
491 (uLongf *) & destLen,
492 (const Bytef *) compressed_data,
493 (uLong) sourceLen) != Z_OK) {
494 syslog(LOG_EMERG, "uncompress() error\n");
499 cdb->len = (size_t) destLen;
500 cdb->ptr = uncompressed_data;
506 * Store a piece of data. Returns 0 if the operation was successful. If a
507 * key already exists it should be overwritten.
509 int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
516 struct CtdlCompressHeader zheader;
517 char *compressed_data = NULL;
519 size_t buffer_len = 0;
522 memset(&dkey, 0, sizeof(DBT));
523 memset(&ddata, 0, sizeof(DBT));
525 /* no, we don't care for this error. */
528 ddata.size = cdatalen;
531 /* Only compress Visit records. Everything else is uncompressed. */
532 if (cdb == CDB_VISIT) {
534 zheader.magic = COMPRESS_MAGIC;
535 zheader.uncompressed_len = cdatalen;
536 buffer_len = ((cdatalen * 101) / 100) + 100
537 + sizeof(struct CtdlCompressHeader);
538 destLen = (uLongf) buffer_len;
539 compressed_data = malloc(buffer_len);
540 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)),
541 &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK)
543 syslog(LOG_EMERG, "compress2() error\n");
546 zheader.compressed_len = (size_t) destLen;
547 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
548 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
549 ddata.data = compressed_data;
552 if (TSD->tid != NULL) {
553 ret = dbp[cdb]->put(dbp[cdb], /* db */
554 TSD->tid, /* transaction ID */
559 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
563 free(compressed_data);
567 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
572 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
573 tid, /* transaction ID */
577 if (ret == DB_LOCK_DEADLOCK) {
581 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
587 free(compressed_data);
597 * Delete a piece of data. Returns 0 if the operation was successful.
599 int cdb_delete(int cdb, void *key, int keylen)
606 memset(&dkey, 0, sizeof dkey);
610 if (TSD->tid != NULL) {
611 ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
613 syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
614 if (ret != DB_NOTFOUND) {
619 bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
624 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
625 && ret != DB_NOTFOUND) {
626 if (ret == DB_LOCK_DEADLOCK) {
630 syslog(LOG_EMERG, "cdb_delete(%d): %s\n",
631 cdb, db_strerror(ret));
641 static DBC *localcursor(int cdb)
646 if (TSD->cursors[cdb] == NULL)
647 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
649 ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
652 syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret));
661 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
662 * a struct cdbdata which it is the caller's responsibility to free later on
663 * using the cdb_free() routine.
665 struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
667 struct cdbdata *tempcdb;
671 memset(&dkey, 0, sizeof(DBT));
673 /* no we don't care about this error. */
676 if (TSD->tid != NULL) {
677 memset(&dret, 0, sizeof(DBT));
678 dret.flags = DB_DBT_MALLOC;
679 ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
684 memset(&dret, 0, sizeof(DBT));
685 dret.flags = DB_DBT_MALLOC;
687 curs = localcursor(cdb);
689 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
692 while (ret == DB_LOCK_DEADLOCK);
696 if ((ret != 0) && (ret != DB_NOTFOUND)) {
697 syslog(LOG_EMERG, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
703 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
705 if (tempcdb == NULL) {
706 syslog(LOG_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb\n");
708 return NULL; /* make it easier for static analysis... */
712 tempcdb->len = dret.size;
713 tempcdb->ptr = dret.data;
714 cdb_decompress_if_necessary(tempcdb);
721 * Free a cdbdata item.
723 * Note that we only free the 'ptr' portion if it is not NULL. This allows
724 * other code to assume ownership of that memory simply by storing the
725 * pointer elsewhere and then setting 'ptr' to NULL. cdb_free() will then
728 void cdb_free(struct cdbdata *cdb)
736 void cdb_close_cursor(int cdb)
738 if (TSD->cursors[cdb] != NULL) {
739 cclose(TSD->cursors[cdb]);
742 TSD->cursors[cdb] = NULL;
746 * Prepare for a sequential search of an entire database.
747 * (There is guaranteed to be no more than one traversal in
748 * progress per thread at any given time.)
750 void cdb_rewind(int cdb)
754 if (TSD->cursors[cdb] != NULL) {
756 "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
758 /* cclose(TSD->cursors[cdb]); */
762 * Now initialize the cursor
764 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
766 syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
773 * Fetch the next item in a sequential search. Returns a pointer to a
774 * cdbdata structure, or NULL if we've hit the end.
776 struct cdbdata *cdb_next_item(int cdb)
779 struct cdbdata *cdbret;
782 /* Initialize the key/data pair so the flags aren't set. */
783 memset(&key, 0, sizeof(key));
784 memset(&data, 0, sizeof(data));
785 data.flags = DB_DBT_MALLOC;
787 ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
790 if (ret != DB_NOTFOUND) {
791 syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret));
794 cdb_close_cursor(cdb);
795 return NULL; /* presumably, end of file */
798 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
799 cdbret->len = data.size;
800 cdbret->ptr = data.data;
801 cdb_decompress_if_necessary(cdbret);
809 * Transaction-based stuff. I'm writing this as I bake cookies...
812 void cdb_begin_transaction(void)
815 bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
817 if (TSD->tid != NULL) {
818 syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
825 void cdb_end_transaction(void)
829 for (i = 0; i < MAXCDB; i++)
830 if (TSD->cursors[i] != NULL) {
832 "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n",
834 cclose(TSD->cursors[i]);
835 TSD->cursors[i] = NULL;
838 if (TSD->tid == NULL) {
840 "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
850 * Truncate (delete every record)
852 void cdb_trunc(int cdb)
858 if (TSD->tid != NULL) {
859 syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
862 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
867 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
868 NULL, /* transaction ID */
869 &count, /* #rows deleted */
871 if (ret == DB_LOCK_DEADLOCK) {
875 syslog(LOG_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret));
877 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.");
887 int SeentDebugEnabled = 0;
889 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SeentDebugEnabled != 0))
890 #define SEENM_syslog(LEVEL, FORMAT) \
891 DBGLOG(LEVEL) syslog(LEVEL, \
892 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
893 IOSTR, ioid, ccid, Facility, cType)
895 #define SEEN_syslog(LEVEL, FORMAT, ...) \
896 DBGLOG(LEVEL) syslog(LEVEL, \
897 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
898 IOSTR, ioid, ccid, Facility, cType, \
901 time_t CheckIfAlreadySeen(const char *Facility,
909 time_t InDBTimeStamp = 0;
911 struct cdbdata *cdbut;
915 SEEN_syslog(LOG_DEBUG, "Loading [%s]", ChrPtr(guid));
916 cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
917 if ((cdbut != NULL) && (cdbut->ptr != NULL)) {
918 memcpy(&ut, cdbut->ptr,
919 ((cdbut->len > sizeof(struct UseTable)) ?
920 sizeof(struct UseTable) : cdbut->len));
921 InDBTimeStamp = now - ut.ut_timestamp;
923 if (InDBTimeStamp < antiexpire)
925 SEEN_syslog(LOG_DEBUG, "Found - Not expired %ld < %ld", InDBTimeStamp, antiexpire);
927 return InDBTimeStamp;
931 SEEN_syslog(LOG_DEBUG, "Found - Expired. %ld >= %ld", InDBTimeStamp, antiexpire);
937 if (cdbut) cdb_free(cdbut);
939 SEENM_syslog(LOG_DEBUG, "not Found");
942 if (cType == eCheckExist)
943 return InDBTimeStamp;
946 memcpy(ut.ut_msgid, SKEY(guid));
947 ut.ut_timestamp = now;
949 SEENM_syslog(LOG_DEBUG, "Saving new Timestamp");
950 /* rewrite the record anyway, to update the timestamp */
951 cdb_store(CDB_USETABLE,
953 &ut, sizeof(struct UseTable) );
955 SEENM_syslog(LOG_DEBUG, "Done Saving");
956 return InDBTimeStamp;
960 void cmd_rsen(char *argbuf) {
966 struct cdbdata *cdbut;
968 if (CtdlAccessCheck(ac_aide)) return;
970 TLen = extract_token(Token, argbuf, 1, '|', sizeof Token);
971 if (strncmp(argbuf, "GET", 3) == 0) {
972 cdbut = cdb_fetch(CDB_USETABLE, Token, TLen);
974 memcpy(&ut, cdbut->ptr,
975 ((cdbut->len > sizeof(struct UseTable)) ?
976 sizeof(struct UseTable) : cdbut->len));
978 cprintf("%d %ld\n", CIT_OK, ut.ut_timestamp);
981 cprintf("%d not found\n", ERROR + NOT_HERE);
985 else if (strncmp(argbuf, "SET", 3) == 0) {
986 memcpy(ut.ut_msgid, Token, TLen);
987 extract_token(Time, argbuf, 2, '|', sizeof Time);
988 ut.ut_timestamp = atol(Time);
989 cdb_store(CDB_USETABLE,
991 &ut, sizeof(struct UseTable) );
992 cprintf("%d token updated\n", CIT_OK);
994 else if (strncmp(argbuf, "DEL", 3) == 0) {
995 if (cdb_delete(CDB_USETABLE, Token, TLen))
996 cprintf("%d not found\n", ERROR + NOT_HERE);
998 cprintf("%d deleted.\n", CIT_OK);
1002 cprintf("%d Usage: [GET|SET|DEL]|Token|timestamp\n", ERROR);
1006 void LogDebugEnableSeenEnable(const int n)
1008 SeentDebugEnabled = n;
1011 CTDL_MODULE_INIT(database)
1015 CtdlRegisterDebugFlagHook(HKEY("SeenDebug"), LogDebugEnableSeenEnable, &SeentDebugEnabled);
1016 CtdlRegisterProtoHook(cmd_rsen, "RSEN", "manipulate Aggregators seen database");
1019 /* return our module id for the log */