2 * This is a data store backend for the Citadel server which uses Berkeley DB.
4 * Copyright (c) 1987-2016 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 <db5/db.h> was found by configure. Install db5-devel.
42 #if DB_VERSION_MAJOR < 5
43 #error Citadel requires Berkeley DB v5.0 or newer. Please upgrade.
46 #include <libcitadel.h>
48 #include "ctdl_module.h"
50 #include "citserver.h"
53 static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
54 static DB_ENV *dbenv; /* The DB environment (global) */
57 void cdb_abort(void) {
59 "citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
70 /* Verbose logging callback */
71 void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
73 if (!IsEmptyStr(msg)) {
74 syslog(LOG_DEBUG, "DB: %s", msg);
80 /* Verbose logging callback */
81 void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
84 syslog(LOG_ALERT, "DB: %s", msg);
90 /* just a little helper function */
91 static void txabort(DB_TXN * tid)
95 ret = tid->abort(tid);
98 syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
103 /* this one is even more helpful than the last. */
104 static void txcommit(DB_TXN * tid)
108 ret = tid->commit(tid, 0);
111 syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
116 /* are you sensing a pattern yet? */
117 static void txbegin(DB_TXN ** tid)
121 ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
124 syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
129 static void dbpanic(DB_ENV * env, int errval)
131 syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval));
135 static void cclose(DBC * cursor)
139 if ((ret = cursor->c_close(cursor))) {
140 syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
145 static void bailIfCursor(DBC ** cursors, const char *msg)
149 for (i = 0; i < MAXCDB; i++)
150 if (cursors[i] != NULL) {
151 syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg);
157 void cdb_check_handles(void)
159 bailIfCursor(TSD->cursors, "in check_handles");
161 if (TSD->tid != NULL) {
162 syslog(LOG_EMERG, "bdb(): transaction still in progress!");
169 * Cull the database logs by removing log files that are no longer needed.
170 * Note: this code will be removed once we are confident that Berkeley DB can do this on its own.
172 void cdb_cull_logs(void)
181 /* Get the list of names. */
182 if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
183 syslog(LOG_ERR, "cdb_cull_logs: %s", db_strerror(ret));
187 /* Print the list of names. */
189 for (file = list; *file != NULL; ++file) {
190 syslog(LOG_DEBUG, "Deleting log: %s", *file);
193 snprintf(errmsg, sizeof(errmsg),
194 " ** ERROR **\n \n \n "
195 "Citadel was unable to delete the "
196 "database log file '%s' because of the "
197 "following error:\n \n %s\n \n"
198 " This log file is no longer in use "
199 "and may be safely deleted.\n",
200 *file, strerror(errno));
201 CtdlAideMessage(errmsg, "Database Warning Message");
210 * Request a checkpoint of the database. Called once per minute by the thread manager.
212 void cdb_checkpoint(void)
216 MARKM_syslog(LOG_DEBUG, "-- db checkpoint --");
217 ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
220 syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
224 /* After a successful checkpoint, we can cull the unused logs */
225 if (CtdlGetConfigInt("c_auto_cull")) {
226 #ifdef DB_LOG_AUTO_REMOVE
227 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 1); // This version of Berkeley DB can cull the logs on its own!
229 cdb_cull_logs(); // Citadel knows how to do it too.
232 #ifdef DB_LOG_AUTO_REMOVE
234 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 0);
242 * Open the various databases we'll be using. Any database which
243 * does not exist should be created. Note that we don't need a
244 * critical section here, because there aren't any active threads
245 * manipulating the database yet.
247 void open_databases(void)
253 int dbversion_major, dbversion_minor, dbversion_patch;
255 syslog(LOG_DEBUG, "bdb(): open_databases() starting");
256 syslog(LOG_DEBUG, "Compiled db: %s", DB_VERSION_STRING);
257 syslog(LOG_INFO, " Linked db: %s", db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
258 syslog(LOG_INFO, "Linked zlib: %s", zlibVersion());
261 * Silently try to create the database subdirectory. If it's
262 * already there, no problem.
264 if ((mkdir(ctdl_data_dir, 0700) != 0) && (errno != EEXIST)){
266 "unable to create database directory [%s]: %s",
267 ctdl_data_dir, strerror(errno));
269 if (chmod(ctdl_data_dir, 0700) != 0){
271 "unable to set database directory accessrights [%s]: %s",
272 ctdl_data_dir, strerror(errno));
274 if (chown(ctdl_data_dir, CTDLUID, (-1)) != 0){
276 "unable to set the owner for [%s]: %s",
277 ctdl_data_dir, strerror(errno));
279 syslog(LOG_DEBUG, "bdb(): Setting up DB environment\n");
280 /* db_env_set_func_yield((int (*)(u_long, u_long))sched_yield); */
281 ret = db_env_create(&dbenv, 0);
283 syslog(LOG_EMERG, "bdb(): db_env_create: %s", db_strerror(ret));
284 syslog(LOG_EMERG, "exit code %d", ret);
287 dbenv->set_errpfx(dbenv, "citserver");
288 dbenv->set_paniccall(dbenv, dbpanic);
289 dbenv->set_errcall(dbenv, cdb_verbose_err);
290 dbenv->set_errpfx(dbenv, "ctdl");
291 dbenv->set_msgcall(dbenv, cdb_verbose_log);
292 dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
293 dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
296 * We want to specify the shared memory buffer pool cachesize,
297 * but everything else is the default.
299 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
301 syslog(LOG_EMERG, "bdb(): set_cachesize: %s", db_strerror(ret));
302 dbenv->close(dbenv, 0);
303 syslog(LOG_EMERG, "exit code %d", ret);
307 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
308 syslog(LOG_EMERG, "bdb(): set_lk_detect: %s", db_strerror(ret));
309 dbenv->close(dbenv, 0);
310 syslog(LOG_EMERG, "exit code %d", ret);
314 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_RECOVER;
315 syslog(LOG_DEBUG, "dbenv->open(dbenv, %s, %d, 0)", ctdl_data_dir, flags);
316 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
317 if (ret == DB_RUNRECOVERY) {
318 syslog(LOG_ALERT, "dbenv->open: %s", db_strerror(ret));
319 syslog(LOG_ALERT, "Attempting recovery...\n");
321 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
323 if (ret == DB_RUNRECOVERY) {
324 syslog(LOG_ALERT, "dbenv->open: %s", db_strerror(ret));
325 syslog(LOG_ALERT, "Attempting catastrophic recovery...");
326 flags &= ~DB_RECOVER;
327 flags |= DB_RECOVER_FATAL;
328 ret = dbenv->open(dbenv, ctdl_data_dir, flags, 0);
331 syslog(LOG_EMERG, "dbenv->open: %s", db_strerror(ret));
332 dbenv->close(dbenv, 0);
333 syslog(LOG_EMERG, "exit code %d", ret);
337 syslog(LOG_INFO, "Starting up DB");
339 for (i = 0; i < MAXCDB; ++i) {
341 /* Create a database handle */
342 ret = db_create(&dbp[i], dbenv, 0);
344 syslog(LOG_EMERG, "db_create: %s", db_strerror(ret));
345 syslog(LOG_EMERG, "exit code %d", ret);
350 /* Arbitrary names for our tables -- we reference them by
351 * number, so we don't have string names for them.
353 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
355 ret = dbp[i]->open(dbp[i],
360 DB_CREATE | DB_AUTO_COMMIT | DB_THREAD,
364 syslog(LOG_EMERG, "db_open[%02x]: %s", i, db_strerror(ret));
366 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.");
368 syslog(LOG_EMERG, "exit code %d", ret);
376 /* Make sure we own all the files, because in a few milliseconds
377 * we're going to drop root privs.
379 void cdb_chmod_data(void) {
382 char filename[PATH_MAX];
384 dp = opendir(ctdl_data_dir);
386 while (d = readdir(dp), d != NULL) {
387 if (d->d_name[0] != '.') {
388 snprintf(filename, sizeof filename, "%s/%s", ctdl_data_dir, d->d_name);
389 syslog(LOG_DEBUG, "chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
390 syslog(LOG_DEBUG, "chown(%s, CTDLUID, -1) returned %d",
391 filename, chown(filename, CTDLUID, (-1))
398 syslog(LOG_DEBUG, "open_databases() finished");
403 * Close all of the db database files we've opened. This can be done
404 * in a loop, since it's just a bunch of closes.
406 void close_databases(void)
411 syslog(LOG_INFO, "Performing final checkpoint.");
412 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
413 syslog(LOG_ERR, "txn_checkpoint: %s", db_strerror(ret));
416 syslog(LOG_INFO, "Flushing the database logs.");
417 if ((ret = dbenv->log_flush(dbenv, NULL))) {
418 syslog(LOG_ERR, "log_flush: %s", db_strerror(ret));
421 /* print some statistics... */
423 dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
426 /* close the tables */
427 for (a = 0; a < MAXCDB; ++a) {
428 syslog(LOG_INFO, "Closing database %02x", a);
429 ret = dbp[a]->close(dbp[a], 0);
431 syslog(LOG_EMERG, "db_close: %s", db_strerror(ret));
436 /* Close the handle. */
437 ret = dbenv->close(dbenv, 0);
439 syslog(LOG_EMERG, "DBENV->close: %s", db_strerror(ret));
445 * Decompress a database item if it was compressed on disk
447 void cdb_decompress_if_necessary(struct cdbdata *cdb)
449 static int magic = COMPRESS_MAGIC;
452 (cdb->ptr == NULL) ||
453 (cdb->len < sizeof(magic)) ||
454 (memcmp(cdb->ptr, &magic, sizeof(magic))))
457 /* At this point we know we're looking at a compressed item. */
459 struct CtdlCompressHeader zheader;
460 char *uncompressed_data;
461 char *compressed_data;
462 uLongf destLen, sourceLen;
465 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
466 cplen = sizeof(struct CtdlCompressHeader);
467 if (sizeof(struct CtdlCompressHeader) > cdb->len)
469 memcpy(&zheader, cdb->ptr, cplen);
471 compressed_data = cdb->ptr;
472 compressed_data += sizeof(struct CtdlCompressHeader);
474 sourceLen = (uLongf) zheader.compressed_len;
475 destLen = (uLongf) zheader.uncompressed_len;
476 uncompressed_data = malloc(zheader.uncompressed_len);
478 if (uncompress((Bytef *) uncompressed_data,
479 (uLongf *) & destLen,
480 (const Bytef *) compressed_data,
481 (uLong) sourceLen) != Z_OK)
483 syslog(LOG_EMERG, "uncompress() error");
488 cdb->len = (size_t) destLen;
489 cdb->ptr = uncompressed_data;
494 * Store a piece of data. Returns 0 if the operation was successful. If a
495 * key already exists it should be overwritten.
497 int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
504 struct CtdlCompressHeader zheader;
505 char *compressed_data = NULL;
507 size_t buffer_len = 0;
510 memset(&dkey, 0, sizeof(DBT));
511 memset(&ddata, 0, sizeof(DBT));
513 /* no, we don't care for this error. */
516 ddata.size = cdatalen;
519 /* Only compress Visit records. Everything else is uncompressed. */
520 if (cdb == CDB_VISIT) {
522 zheader.magic = COMPRESS_MAGIC;
523 zheader.uncompressed_len = cdatalen;
524 buffer_len = ((cdatalen * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader);
525 destLen = (uLongf) buffer_len;
526 compressed_data = malloc(buffer_len);
527 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)),
528 &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK)
530 syslog(LOG_EMERG, "compress2() error");
533 zheader.compressed_len = (size_t) destLen;
534 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
535 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
536 ddata.data = compressed_data;
539 if (TSD->tid != NULL) {
540 ret = dbp[cdb]->put(dbp[cdb], /* db */
541 TSD->tid, /* transaction ID */
546 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
550 free(compressed_data);
554 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
559 if ((ret = dbp[cdb]->put(dbp[cdb], /* db */
560 tid, /* transaction ID */
564 if (ret == DB_LOCK_DEADLOCK) {
568 syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
574 free(compressed_data);
584 * Delete a piece of data. Returns 0 if the operation was successful.
586 int cdb_delete(int cdb, void *key, int keylen)
593 memset(&dkey, 0, sizeof dkey);
597 if (TSD->tid != NULL) {
598 ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
600 syslog(LOG_EMERG, "cdb_delete(%d): %s", cdb, db_strerror(ret));
601 if (ret != DB_NOTFOUND) {
606 bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
611 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0))
612 && ret != DB_NOTFOUND) {
613 if (ret == DB_LOCK_DEADLOCK) {
617 syslog(LOG_EMERG, "cdb_delete(%d): %s",
618 cdb, db_strerror(ret));
628 static DBC *localcursor(int cdb)
633 if (TSD->cursors[cdb] == NULL)
634 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
636 ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
639 syslog(LOG_EMERG, "localcursor: %s", db_strerror(ret));
648 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
649 * a struct cdbdata which it is the caller's responsibility to free later on
650 * using the cdb_free() routine.
652 struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
654 struct cdbdata *tempcdb;
658 memset(&dkey, 0, sizeof(DBT));
660 /* no we don't care about this error. */
663 if (TSD->tid != NULL) {
664 memset(&dret, 0, sizeof(DBT));
665 dret.flags = DB_DBT_MALLOC;
666 ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
671 memset(&dret, 0, sizeof(DBT));
672 dret.flags = DB_DBT_MALLOC;
674 curs = localcursor(cdb);
676 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
679 while (ret == DB_LOCK_DEADLOCK);
683 if ((ret != 0) && (ret != DB_NOTFOUND)) {
684 syslog(LOG_EMERG, "cdb_fetch(%d): %s", cdb, db_strerror(ret));
690 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
692 if (tempcdb == NULL) {
693 syslog(LOG_EMERG, "cdb_fetch: Cannot allocate memory for tempcdb");
695 return NULL; /* make it easier for static analysis... */
699 tempcdb->len = dret.size;
700 tempcdb->ptr = dret.data;
701 cdb_decompress_if_necessary(tempcdb);
708 * Free a cdbdata item.
710 * Note that we only free the 'ptr' portion if it is not NULL. This allows
711 * other code to assume ownership of that memory simply by storing the
712 * pointer elsewhere and then setting 'ptr' to NULL. cdb_free() will then
715 void cdb_free(struct cdbdata *cdb)
723 void cdb_close_cursor(int cdb)
725 if (TSD->cursors[cdb] != NULL) {
726 cclose(TSD->cursors[cdb]);
729 TSD->cursors[cdb] = NULL;
733 * Prepare for a sequential search of an entire database.
734 * (There is guaranteed to be no more than one traversal in
735 * progress per thread at any given time.)
737 void cdb_rewind(int cdb)
741 if (TSD->cursors[cdb] != NULL) {
742 syslog(LOG_EMERG, "cdb_rewind: must close cursor on database %d before reopening.", cdb);
744 /* cclose(TSD->cursors[cdb]); */
748 * Now initialize the cursor
750 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
752 syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s", db_strerror(ret));
759 * Fetch the next item in a sequential search. Returns a pointer to a
760 * cdbdata structure, or NULL if we've hit the end.
762 struct cdbdata *cdb_next_item(int cdb)
765 struct cdbdata *cdbret;
768 /* Initialize the key/data pair so the flags aren't set. */
769 memset(&key, 0, sizeof(key));
770 memset(&data, 0, sizeof(data));
771 data.flags = DB_DBT_MALLOC;
773 ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
776 if (ret != DB_NOTFOUND) {
777 syslog(LOG_EMERG, "cdb_next_item(%d): %s", cdb, db_strerror(ret));
780 cdb_close_cursor(cdb);
781 return NULL; /* presumably, end of file */
784 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
785 cdbret->len = data.size;
786 cdbret->ptr = data.data;
787 cdb_decompress_if_necessary(cdbret);
795 * Transaction-based stuff. I'm writing this as I bake cookies...
798 void cdb_begin_transaction(void)
801 bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
803 if (TSD->tid != NULL) {
804 syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction");
811 void cdb_end_transaction(void)
815 for (i = 0; i < MAXCDB; i++)
816 if (TSD->cursors[i] != NULL) {
818 "cdb_end_transaction: WARNING: cursor %d still open at transaction end",
820 cclose(TSD->cursors[i]);
821 TSD->cursors[i] = NULL;
824 if (TSD->tid == NULL) {
825 syslog(LOG_ERR, "cdb_end_transaction: ERROR: txcommit(NULL) !!");
836 * Truncate (delete every record)
838 void cdb_trunc(int cdb)
844 if (TSD->tid != NULL) {
845 syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
849 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
854 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
855 NULL, /* transaction ID */
856 &count, /* #rows deleted */
858 if (ret == DB_LOCK_DEADLOCK) {
862 syslog(LOG_EMERG, "cdb_truncate(%d): %s", cdb, db_strerror(ret));
864 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.");
874 int SeentDebugEnabled = 0;
876 #define DBGLOG(LEVEL) if ((LEVEL != LOG_DEBUG) || (SeentDebugEnabled != 0))
877 #define SEENM_syslog(LEVEL, FORMAT) \
878 DBGLOG(LEVEL) syslog(LEVEL, \
879 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
880 IOSTR, ioid, ccid, Facility, cType)
882 #define SEEN_syslog(LEVEL, FORMAT, ...) \
883 DBGLOG(LEVEL) syslog(LEVEL, \
884 "%s[%ld]CC[%ld] SEEN[%s][%d] " FORMAT, \
885 IOSTR, ioid, ccid, Facility, cType, \
888 time_t CheckIfAlreadySeen(const char *Facility,
896 time_t InDBTimeStamp = 0;
898 struct cdbdata *cdbut;
902 SEEN_syslog(LOG_DEBUG, "Loading [%s]", ChrPtr(guid));
903 cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
904 if ((cdbut != NULL) && (cdbut->ptr != NULL)) {
905 memcpy(&ut, cdbut->ptr,
906 ((cdbut->len > sizeof(struct UseTable)) ?
907 sizeof(struct UseTable) : cdbut->len));
908 InDBTimeStamp = now - ut.ut_timestamp;
910 if (InDBTimeStamp < antiexpire)
912 SEEN_syslog(LOG_DEBUG, "Found - Not expired %ld < %ld", InDBTimeStamp, antiexpire);
914 return InDBTimeStamp;
918 SEEN_syslog(LOG_DEBUG, "Found - Expired. %ld >= %ld", InDBTimeStamp, antiexpire);
924 if (cdbut) cdb_free(cdbut);
926 SEENM_syslog(LOG_DEBUG, "not Found");
927 if (cType == eCheckUpdate)
931 if (cType == eCheckExist)
932 return InDBTimeStamp;
935 memcpy(ut.ut_msgid, SKEY(guid));
936 ut.ut_timestamp = now;
938 SEENM_syslog(LOG_DEBUG, "Saving new Timestamp");
939 /* rewrite the record anyway, to update the timestamp */
940 cdb_store(CDB_USETABLE,
942 &ut, sizeof(struct UseTable) );
944 SEENM_syslog(LOG_DEBUG, "Done Saving");
945 return InDBTimeStamp;
949 void cmd_rsen(char *argbuf) {
955 struct cdbdata *cdbut;
957 if (CtdlAccessCheck(ac_aide)) return;
959 TLen = extract_token(Token, argbuf, 1, '|', sizeof Token);
960 if (strncmp(argbuf, "GET", 3) == 0) {
961 cdbut = cdb_fetch(CDB_USETABLE, Token, TLen);
963 memcpy(&ut, cdbut->ptr,
964 ((cdbut->len > sizeof(struct UseTable)) ?
965 sizeof(struct UseTable) : cdbut->len));
967 cprintf("%d %ld\n", CIT_OK, ut.ut_timestamp);
970 cprintf("%d not found\n", ERROR + NOT_HERE);
974 else if (strncmp(argbuf, "SET", 3) == 0) {
975 memcpy(ut.ut_msgid, Token, TLen);
976 extract_token(Time, argbuf, 2, '|', sizeof Time);
977 ut.ut_timestamp = atol(Time);
978 cdb_store(CDB_USETABLE,
980 &ut, sizeof(struct UseTable) );
981 cprintf("%d token updated\n", CIT_OK);
983 else if (strncmp(argbuf, "DEL", 3) == 0) {
984 if (cdb_delete(CDB_USETABLE, Token, TLen))
985 cprintf("%d not found\n", ERROR + NOT_HERE);
987 cprintf("%d deleted.\n", CIT_OK);
991 cprintf("%d Usage: [GET|SET|DEL]|Token|timestamp\n", ERROR);
995 void LogDebugEnableSeenEnable(const int n)
997 SeentDebugEnabled = n;
1000 CTDL_MODULE_INIT(database)
1004 CtdlRegisterDebugFlagHook(HKEY("SeenDebug"), LogDebugEnableSeenEnable, &SeentDebugEnabled);
1005 CtdlRegisterProtoHook(cmd_rsen, "RSEN", "manipulate Aggregators seen database");
1008 /* return our module id for the log */