2 * This is a data store backend for the Citadel server which uses Berkeley DB.
4 * Copyright (c) 1987-2015 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"
56 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
57 static DB_ENV *dbenv; /* The DB environment (global) */
60 void cdb_abort(void) {
62 "citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
73 /* Verbose logging callback */
74 void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
76 if (!IsEmptyStr(msg)) {
77 syslog(LOG_DEBUG, "DB: %s", msg);
83 /* Verbose logging callback */
84 void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
87 syslog(LOG_ALERT, "DB: %s", msg);
93 /* just a little helper function */
94 static void txabort(DB_TXN * tid)
98 ret = tid->abort(tid);
101 syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
106 /* this one is even more helpful than the last. */
107 static void txcommit(DB_TXN * tid)
111 ret = tid->commit(tid, 0);
114 syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
119 /* are you sensing a pattern yet? */
120 static void txbegin(DB_TXN ** tid)
124 ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
127 syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
132 static void dbpanic(DB_ENV * env, int errval)
134 syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval));
138 static void cclose(DBC * cursor)
142 if ((ret = cursor->c_close(cursor))) {
143 syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
148 static void bailIfCursor(DBC ** cursors, const char *msg)
152 for (i = 0; i < MAXCDB; i++)
153 if (cursors[i] != NULL) {
154 syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg);
160 void cdb_check_handles(void)
162 bailIfCursor(TSD->cursors, "in check_handles");
164 if (TSD->tid != NULL) {
165 syslog(LOG_EMERG, "bdb(): transaction still in progress!");
172 * Cull the database logs
174 void cdb_cull_logs(void)
183 /* Get the list of names. */
184 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
185 syslog(LOG_ERR, "cdb_cull_logs: %s", db_strerror(ret));
189 /* Print the list of names. */
191 for (file = list; *file != NULL; ++file) {
192 syslog(LOG_DEBUG, "Deleting log: %s", *file);
195 snprintf(errmsg, sizeof(errmsg),
196 " ** ERROR **\n \n \n "
197 "Citadel was unable to delete the "
198 "database log file '%s' because of the "
199 "following error:\n \n %s\n \n"
200 " This log file is no longer in use "
201 "and may be safely deleted.\n",
202 *file, strerror(errno));
203 CtdlAideMessage(errmsg, "Database Warning Message");
212 * Request a checkpoint of the database. Called once per minute by the thread manager.
214 void cdb_checkpoint(void)
218 syslog(LOG_DEBUG, "-- db checkpoint --");
219 ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
222 syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
226 /* After a successful checkpoint, we can cull the unused logs */
227 if (CtdlGetConfigInt("c_auto_cull")) {
235 * Open the various databases we'll be using. Any database which
236 * does not exist should be created. Note that we don't need a
237 * critical section here, because there aren't any active threads
238 * manipulating the database yet.
240 void open_databases(void)
246 int dbversion_major, dbversion_minor, dbversion_patch;
247 int current_dbversion = 0;
249 syslog(LOG_DEBUG, "bdb(): open_databases() starting");
250 syslog(LOG_DEBUG, "Compiled db: %s", DB_VERSION_STRING);
251 syslog(LOG_INFO, " Linked db: %s",
252 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
254 current_dbversion = (dbversion_major * 1000000) + (dbversion_minor * 1000) + dbversion_patch;
256 syslog(LOG_DEBUG, "Calculated dbversion: %d", current_dbversion);
257 syslog(LOG_DEBUG, " Previous dbversion: %d", CitControl.MMdbversion);
259 if ( (getenv("SUPPRESS_DBVERSION_CHECK") == NULL)
260 && (CitControl.MMdbversion > current_dbversion) ) {
261 syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version");
262 syslog(LOG_EMERG, "of Berkeley DB that is older than that which last created or");
263 syslog(LOG_EMERG, "updated the database. Because this would probably cause data");
264 syslog(LOG_EMERG, "corruption or loss, the server is aborting execution now.");
268 CitControl.MMdbversion = current_dbversion;
271 syslog(LOG_INFO, "Linked zlib: %s\n", zlibVersion());
274 * Silently try to create the database subdirectory. If it's
275 * already there, no problem.
277 if ((mkdir(ctdl_data_dir, 0700) != 0) && (errno != EEXIST)){
279 "unable to create database directory [%s]: %s",
280 ctdl_data_dir, strerror(errno));
282 if (chmod(ctdl_data_dir, 0700) != 0){
284 "unable to set database directory accessrights [%s]: %s",
285 ctdl_data_dir, strerror(errno));
287 if (chown(ctdl_data_dir, CTDLUID, (-1)) != 0){
289 "unable to set the owner for [%s]: %s",
290 ctdl_data_dir, strerror(errno));
292 syslog(LOG_DEBUG, "bdb(): Setting up DB environment\n");
293 /* db_env_set_func_yield((int (*)(u_long, u_long))sched_yield); */
294 ret = db_env_create(&dbenv, 0);
296 syslog(LOG_EMERG, "bdb(): db_env_create: %s\n", db_strerror(ret));
297 syslog(LOG_EMERG, "exit code %d\n", ret);
300 dbenv->set_errpfx(dbenv, "citserver");
301 dbenv->set_paniccall(dbenv, dbpanic);
302 dbenv->set_errcall(dbenv, cdb_verbose_err);
303 dbenv->set_errpfx(dbenv, "ctdl");
304 #if (DB_VERSION_MAJOR == 4) && (DB_VERSION_MINOR >= 3)
305 dbenv->set_msgcall(dbenv, cdb_verbose_log);
307 dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
308 dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
311 * We want to specify the shared memory buffer pool cachesize,
312 * but everything else is the default.
314 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
316 syslog(LOG_EMERG, "bdb(): set_cachesize: %s\n", db_strerror(ret));
317 dbenv->close(dbenv, 0);
318 syslog(LOG_EMERG, "exit code %d\n", ret);
322 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
323 syslog(LOG_EMERG, "bdb(): set_lk_detect: %s\n", db_strerror(ret));
324 dbenv->close(dbenv, 0);
325 syslog(LOG_EMERG, "exit code %d\n", ret);
329 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_RECOVER;
330 syslog(LOG_DEBUG, "dbenv->open(dbenv, %s, %d, 0)\n", ctdl_data_dir, flags);
331 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
332 if (ret == DB_RUNRECOVERY) {
333 syslog(LOG_ALERT, "dbenv->open: %s\n", db_strerror(ret));
334 syslog(LOG_ALERT, "Attempting recovery...\n");
336 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
338 if (ret == DB_RUNRECOVERY) {
339 syslog(LOG_ALERT, "dbenv->open: %s\n", db_strerror(ret));
340 syslog(LOG_ALERT, "Attempting catastrophic recovery...\n");
341 flags &= ~DB_RECOVER;
342 flags |= DB_RECOVER_FATAL;
343 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
346 syslog(LOG_EMERG, "dbenv->open: %s\n", db_strerror(ret));
347 dbenv->close(dbenv, 0);
348 syslog(LOG_EMERG, "exit code %d\n", ret);
352 syslog(LOG_INFO, "Starting up DB\n");
354 for (i = 0; i < MAXCDB; ++i) {
356 /* Create a database handle */
357 ret = db_create(&dbp[i], dbenv, 0);
359 syslog(LOG_EMERG, "db_create: %s\n", db_strerror(ret));
360 syslog(LOG_EMERG, "exit code %d\n", ret);
365 /* Arbitrary names for our tables -- we reference them by
366 * number, so we don't have string names for them.
368 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
370 ret = dbp[i]->open(dbp[i],
375 DB_CREATE | DB_AUTO_COMMIT | DB_THREAD,
379 syslog(LOG_EMERG, "db_open[%02x]: %s\n", i, db_strerror(ret));
381 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.");
383 syslog(LOG_EMERG, "exit code %d\n", ret);
391 /* Make sure we own all the files, because in a few milliseconds
392 * we're going to drop root privs.
394 void cdb_chmod_data(void) {
397 char filename[PATH_MAX];
399 dp = opendir(ctdl_data_dir);
401 while (d = readdir(dp), d != NULL) {
402 if (d->d_name[0] != '.') {
403 snprintf(filename, sizeof filename,
404 "%s/%s", ctdl_data_dir, d->d_name);
405 syslog(LOG_DEBUG, "chmod(%s, 0600) returned %d\n",
406 filename, chmod(filename, 0600)
408 syslog(LOG_DEBUG, "chown(%s, CTDLUID, -1) returned %d\n",
409 filename, chown(filename, CTDLUID, (-1))
416 syslog(LOG_DEBUG, "open_databases() finished\n");
421 * Close all of the db database files we've opened. This can be done
422 * in a loop, since it's just a bunch of closes.
424 void close_databases(void)
429 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
431 "txn_checkpoint: %s\n", db_strerror(ret));
434 /* print some statistics... */
436 dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
439 /* close the tables */
440 for (a = 0; a < MAXCDB; ++a) {
441 syslog(LOG_INFO, "Closing database %02x\n", a);
442 ret = dbp[a]->close(dbp[a], 0);
444 syslog(LOG_EMERG, "db_close: %s\n", db_strerror(ret));
449 /* Close the handle. */
450 ret = dbenv->close(dbenv, 0);
452 syslog(LOG_EMERG, "DBENV->close: %s\n", db_strerror(ret));
458 * Decompress a database item if it was compressed on disk
460 void cdb_decompress_if_necessary(struct cdbdata *cdb)
462 static int magic = COMPRESS_MAGIC;
465 (cdb->ptr == NULL) ||
466 (cdb->len < sizeof(magic)) ||
467 (memcmp(cdb->ptr, &magic, sizeof(magic))))
470 /* At this point we know we're looking at a compressed item. */
472 struct CtdlCompressHeader zheader;
473 char *uncompressed_data;
474 char *compressed_data;
475 uLongf destLen, sourceLen;
478 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
479 cplen = sizeof(struct CtdlCompressHeader);
480 if (sizeof(struct CtdlCompressHeader) > cdb->len)
482 memcpy(&zheader, cdb->ptr, cplen);
484 compressed_data = cdb->ptr;
485 compressed_data += sizeof(struct CtdlCompressHeader);
487 sourceLen = (uLongf) zheader.compressed_len;
488 destLen = (uLongf) zheader.uncompressed_len;
489 uncompressed_data = malloc(zheader.uncompressed_len);
491 if (uncompress((Bytef *) uncompressed_data,
492 (uLongf *) & destLen,
493 (const Bytef *) compressed_data,
494 (uLong) sourceLen) != Z_OK) {
495 syslog(LOG_EMERG, "uncompress() error\n");
500 cdb->len = (size_t) destLen;
501 cdb->ptr = uncompressed_data;
507 * Store a piece of data. Returns 0 if the operation was successful. If a
508 * key already exists it should be overwritten.
510 int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
517 struct CtdlCompressHeader zheader;
518 char *compressed_data = NULL;
520 size_t buffer_len = 0;
523 memset(&dkey, 0, sizeof(DBT));
524 memset(&ddata, 0, sizeof(DBT));
526 /* no, we don't care for this error. */
529 ddata.size = cdatalen;
532 /* Only compress Visit records. Everything else is uncompressed. */
533 if (cdb == CDB_VISIT) {
535 zheader.magic = COMPRESS_MAGIC;
536 zheader.uncompressed_len = cdatalen;
537 buffer_len = ((cdatalen * 101) / 100) + 100
538 + sizeof(struct CtdlCompressHeader);
539 destLen = (uLongf) buffer_len;
540 compressed_data = malloc(buffer_len);
541 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)),
542 &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK)
544 syslog(LOG_EMERG, "compress2() error\n");
547 zheader.compressed_len = (size_t) destLen;
548 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
549 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
550 ddata.data = compressed_data;
553 if (TSD->tid != NULL) {
554 ret = dbp[cdb]->put(dbp[cdb], /* db */
555 TSD->tid, /* transaction ID */
560 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
564 free(compressed_data);
568 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
573 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
574 tid, /* transaction ID */
578 if (ret == DB_LOCK_DEADLOCK) {
582 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
588 free(compressed_data);
598 * Delete a piece of data. Returns 0 if the operation was successful.
600 int cdb_delete(int cdb, void *key, int keylen)
607 memset(&dkey, 0, sizeof dkey);
611 if (TSD->tid != NULL) {
612 ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
614 syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
615 if (ret != DB_NOTFOUND) {
620 bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
625 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
626 && ret != DB_NOTFOUND) {
627 if (ret == DB_LOCK_DEADLOCK) {
631 syslog(LOG_EMERG, "cdb_delete(%d): %s\n",
632 cdb, db_strerror(ret));
642 static DBC *localcursor(int cdb)
647 if (TSD->cursors[cdb] == NULL)
648 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
650 ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
653 syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret));
662 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
663 * a struct cdbdata which it is the caller's responsibility to free later on
664 * using the cdb_free() routine.
666 struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
668 struct cdbdata *tempcdb;
672 memset(&dkey, 0, sizeof(DBT));
674 /* no we don't care about this error. */
677 if (TSD->tid != NULL) {
678 memset(&dret, 0, sizeof(DBT));
679 dret.flags = DB_DBT_MALLOC;
680 ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
685 memset(&dret, 0, sizeof(DBT));
686 dret.flags = DB_DBT_MALLOC;
688 curs = localcursor(cdb);
690 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
693 while (ret == DB_LOCK_DEADLOCK);
697 if ((ret != 0) && (ret != DB_NOTFOUND)) {
698 syslog(LOG_EMERG, "cdb_fetch(%d): %s\n", cdb, db_strerror(ret));
704 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
706 if (tempcdb == NULL) {
707 syslog(LOG_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb\n");
709 return NULL; /* make it easier for static analysis... */
713 tempcdb->len = dret.size;
714 tempcdb->ptr = dret.data;
715 cdb_decompress_if_necessary(tempcdb);
722 * Free a cdbdata item.
724 * Note that we only free the 'ptr' portion if it is not NULL. This allows
725 * other code to assume ownership of that memory simply by storing the
726 * pointer elsewhere and then setting 'ptr' to NULL. cdb_free() will then
729 void cdb_free(struct cdbdata *cdb)
737 void cdb_close_cursor(int cdb)
739 if (TSD->cursors[cdb] != NULL) {
740 cclose(TSD->cursors[cdb]);
743 TSD->cursors[cdb] = NULL;
747 * Prepare for a sequential search of an entire database.
748 * (There is guaranteed to be no more than one traversal in
749 * progress per thread at any given time.)
751 void cdb_rewind(int cdb)
755 if (TSD->cursors[cdb] != NULL) {
757 "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
759 /* cclose(TSD->cursors[cdb]); */
763 * Now initialize the cursor
765 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
767 syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
774 * Fetch the next item in a sequential search. Returns a pointer to a
775 * cdbdata structure, or NULL if we've hit the end.
777 struct cdbdata *cdb_next_item(int cdb)
780 struct cdbdata *cdbret;
783 /* Initialize the key/data pair so the flags aren't set. */
784 memset(&key, 0, sizeof(key));
785 memset(&data, 0, sizeof(data));
786 data.flags = DB_DBT_MALLOC;
788 ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
791 if (ret != DB_NOTFOUND) {
792 syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret));
795 cdb_close_cursor(cdb);
796 return NULL; /* presumably, end of file */
799 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
800 cdbret->len = data.size;
801 cdbret->ptr = data.data;
802 cdb_decompress_if_necessary(cdbret);
810 * Transaction-based stuff. I'm writing this as I bake cookies...
813 void cdb_begin_transaction(void)
816 bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
818 if (TSD->tid != NULL) {
819 syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
826 void cdb_end_transaction(void)
830 for (i = 0; i < MAXCDB; i++)
831 if (TSD->cursors[i] != NULL) {
833 "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n",
835 cclose(TSD->cursors[i]);
836 TSD->cursors[i] = NULL;
839 if (TSD->tid == NULL) {
841 "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
851 * Truncate (delete every record)
853 void cdb_trunc(int cdb)
859 if (TSD->tid != NULL) {
860 syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
863 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
868 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
869 NULL, /* transaction ID */
870 &count, /* #rows deleted */
872 if (ret == DB_LOCK_DEADLOCK) {
876 syslog(LOG_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret));
878 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.");
888 int SeentDebugEnabled = 0;
890 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SeentDebugEnabled != 0))
891 #define SEENM_syslog(LEVEL, FORMAT) \
892 DBGLOG(LEVEL) syslog(LEVEL, \
893 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
894 IOSTR, ioid, ccid, Facility, cType)
896 #define SEEN_syslog(LEVEL, FORMAT, ...) \
897 DBGLOG(LEVEL) syslog(LEVEL, \
898 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
899 IOSTR, ioid, ccid, Facility, cType, \
902 time_t CheckIfAlreadySeen(const char *Facility,
910 time_t InDBTimeStamp = 0;
912 struct cdbdata *cdbut;
916 SEEN_syslog(LOG_DEBUG, "Loading [%s]", ChrPtr(guid));
917 cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
918 if ((cdbut != NULL) && (cdbut->ptr != NULL)) {
919 memcpy(&ut, cdbut->ptr,
920 ((cdbut->len > sizeof(struct UseTable)) ?
921 sizeof(struct UseTable) : cdbut->len));
922 InDBTimeStamp = now - ut.ut_timestamp;
924 if (InDBTimeStamp < antiexpire)
926 SEEN_syslog(LOG_DEBUG, "Found - Not expired %ld < %ld", InDBTimeStamp, antiexpire);
928 return InDBTimeStamp;
932 SEEN_syslog(LOG_DEBUG, "Found - Expired. %ld >= %ld", InDBTimeStamp, antiexpire);
938 if (cdbut) cdb_free(cdbut);
940 SEENM_syslog(LOG_DEBUG, "not Found");
943 if (cType == eCheckExist)
944 return InDBTimeStamp;
947 memcpy(ut.ut_msgid, SKEY(guid));
948 ut.ut_timestamp = now;
950 SEENM_syslog(LOG_DEBUG, "Saving new Timestamp");
951 /* rewrite the record anyway, to update the timestamp */
952 cdb_store(CDB_USETABLE,
954 &ut, sizeof(struct UseTable) );
956 SEENM_syslog(LOG_DEBUG, "Done Saving");
957 return InDBTimeStamp;
961 void cmd_rsen(char *argbuf) {
967 struct cdbdata *cdbut;
969 if (CtdlAccessCheck(ac_aide)) return;
971 TLen = extract_token(Token, argbuf, 1, '|', sizeof Token);
972 if (strncmp(argbuf, "GET", 3) == 0) {
973 cdbut = cdb_fetch(CDB_USETABLE, Token, TLen);
975 memcpy(&ut, cdbut->ptr,
976 ((cdbut->len > sizeof(struct UseTable)) ?
977 sizeof(struct UseTable) : cdbut->len));
979 cprintf("%d %ld\n", CIT_OK, ut.ut_timestamp);
982 cprintf("%d not found\n", ERROR + NOT_HERE);
986 else if (strncmp(argbuf, "SET", 3) == 0) {
987 memcpy(ut.ut_msgid, Token, TLen);
988 extract_token(Time, argbuf, 2, '|', sizeof Time);
989 ut.ut_timestamp = atol(Time);
990 cdb_store(CDB_USETABLE,
992 &ut, sizeof(struct UseTable) );
993 cprintf("%d token updated\n", CIT_OK);
995 else if (strncmp(argbuf, "DEL", 3) == 0) {
996 if (cdb_delete(CDB_USETABLE, Token, TLen))
997 cprintf("%d not found\n", ERROR + NOT_HERE);
999 cprintf("%d deleted.\n", CIT_OK);
1003 cprintf("%d Usage: [GET|SET|DEL]|Token|timestamp\n", ERROR);
1007 void LogDebugEnableSeenEnable(const int n)
1009 SeentDebugEnabled = n;
1012 CTDL_MODULE_INIT(database)
1016 CtdlRegisterDebugFlagHook(HKEY("SeenDebug"), LogDebugEnableSeenEnable, &SeentDebugEnabled);
1017 CtdlRegisterProtoHook(cmd_rsen, "RSEN", "manipulate Aggregators seen database");
1020 /* return our module id for the log */