citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

database.c
Go to the documentation of this file.
1// This is a data store backend for the Citadel server which uses Berkeley DB.
2//
3// Copyright (c) 1987-2022 by the citadel.org team
4//
5// This program is open source software. Use, duplication, or disclosure
6// is subject to the terms of the GNU General Public License, version 3.
7// The program is distributed without any warranty, expressed or implied.
8
9/*****************************************************************************
10 Tunable configuration parameters for the Berkeley DB back end
11 *****************************************************************************/
12
13/* Citadel will checkpoint the db at the end of every session, but only if
14 * the specified number of kilobytes has been written, or if the specified
15 * number of minutes has passed, since the last checkpoint.
16 */
17#define MAX_CHECKPOINT_KBYTES 256
18#define MAX_CHECKPOINT_MINUTES 15
19
20/*****************************************************************************/
21
22#include "sysdep.h"
23#include <stdlib.h>
24#include <unistd.h>
25#include <sys/stat.h>
26#include <stdio.h>
27#include <dirent.h>
28#include <zlib.h>
29#include <db.h>
30
31#if DB_VERSION_MAJOR < 5
32#error Citadel requires Berkeley DB v5.0 or newer. Please upgrade.
33#endif
34
35#include <libcitadel.h>
36#include "ctdl_module.h"
37#include "control.h"
38#include "citserver.h"
39#include "config.h"
40
41static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
42static DB_ENV *dbenv; /* The DB environment (global) */
43
44
45void cdb_abort(void) {
46 syslog(LOG_DEBUG, "db: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d",
47 getuid(), getgid(), geteuid(), getegid()
48 );
49 exit(CTDLEXIT_DB);
50}
51
52
53/* Verbose logging callback */
54void cdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *what_is_this) {
55 if (!IsEmptyStr(msg)) {
56 syslog(LOG_DEBUG, "db: %s", msg);
57 }
58}
59
60
61/* Verbose logging callback */
62void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg) {
63 syslog(LOG_ERR, "db: %s", msg);
64}
65
66
67/* wrapper for txn_abort() that logs/aborts on error */
68static void txabort(DB_TXN *tid) {
69 int ret;
70
71 ret = tid->abort(tid);
72
73 if (ret) {
74 syslog(LOG_ERR, "db: txn_abort: %s", db_strerror(ret));
75 cdb_abort();
76 }
77}
78
79
80/* wrapper for txn_commit() that logs/aborts on error */
81static void txcommit(DB_TXN *tid) {
82 int ret;
83
84 ret = tid->commit(tid, 0);
85
86 if (ret) {
87 syslog(LOG_ERR, "db: txn_commit: %s", db_strerror(ret));
88 cdb_abort();
89 }
90}
91
92
93/* wrapper for txn_begin() that logs/aborts on error */
94static void txbegin(DB_TXN **tid) {
95 int ret;
96
97 ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
98
99 if (ret) {
100 syslog(LOG_ERR, "db: txn_begin: %s", db_strerror(ret));
101 cdb_abort();
102 }
103}
104
105
106/* panic callback */
107static void dbpanic(DB_ENV * env, int errval) {
108 syslog(LOG_ERR, "db: PANIC: %s", db_strerror(errval));
109}
110
111
112static void cclose(DBC * cursor) {
113 int ret;
114
115 if ((ret = cursor->c_close(cursor))) {
116 syslog(LOG_ERR, "db: c_close: %s", db_strerror(ret));
117 cdb_abort();
118 }
119}
120
121
122static void bailIfCursor(DBC ** cursors, const char *msg) {
123 int i;
124
125 for (i = 0; i < MAXCDB; i++)
126 if (cursors[i] != NULL) {
127 syslog(LOG_ERR, "db: cursor still in progress on cdb %02x: %s", i, msg);
128 cdb_abort();
129 }
130}
131
132
134 bailIfCursor(TSD->cursors, "in check_handles");
135
136 if (TSD->tid != NULL) {
137 syslog(LOG_ERR, "db: transaction still in progress!");
138 cdb_abort();
139 }
140}
141
142
143/*
144 * Request a checkpoint of the database. Called once per minute by the thread manager.
145 */
146void cdb_checkpoint(void) {
147 int ret;
148
149 syslog(LOG_DEBUG, "db: -- checkpoint --");
150 ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
151
152 if (ret != 0) {
153 syslog(LOG_ERR, "db: cdb_checkpoint() txn_checkpoint: %s", db_strerror(ret));
154 cdb_abort();
155 }
156
157 /* After a successful checkpoint, we can cull the unused logs */
158 if (CtdlGetConfigInt("c_auto_cull")) {
159 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 1);
160 }
161 else {
162 ret = dbenv->log_set_config(dbenv, DB_LOG_AUTO_REMOVE, 0);
163 }
164}
165
166
167/*
168 * Open the various databases we'll be using. Any database which
169 * does not exist should be created. Note that we don't need a
170 * critical section here, because there aren't any active threads
171 * manipulating the database yet.
172 */
173void open_databases(void) {
174 int ret;
175 int i;
176 char dbfilename[32];
177 u_int32_t flags = 0;
178 int dbversion_major, dbversion_minor, dbversion_patch;
179
180 syslog(LOG_DEBUG, "db: open_databases() starting");
181 syslog(LOG_DEBUG, "db: Compiled libdb: %s", DB_VERSION_STRING);
182 syslog(LOG_DEBUG, "db: Linked libdb: %s", db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
183 syslog(LOG_DEBUG, "db: Linked zlib: %s", zlibVersion());
184
185 /*
186 * Silently try to create the database subdirectory. If it's already there, no problem.
187 */
188 if ((mkdir(ctdl_db_dir, 0700) != 0) && (errno != EEXIST)) {
189 syslog(LOG_ERR, "db: unable to create database directory [%s]: %m", ctdl_db_dir);
190 }
191 if (chmod(ctdl_db_dir, 0700) != 0) {
192 syslog(LOG_ERR, "db: unable to set database directory permissions [%s]: %m", ctdl_db_dir);
193 }
194 if (chown(ctdl_db_dir, CTDLUID, (-1)) != 0) {
195 syslog(LOG_ERR, "db: unable to set the owner for [%s]: %m", ctdl_db_dir);
196 }
197 syslog(LOG_DEBUG, "db: Setting up DB environment");
198 // db_env_set_func_yield((int (*)(u_long, u_long))sched_yield);
199 ret = db_env_create(&dbenv, 0);
200 if (ret) {
201 syslog(LOG_ERR, "db: db_env_create: %s", db_strerror(ret));
202 syslog(LOG_ERR, "db: exit code %d", ret);
203 exit(CTDLEXIT_DB);
204 }
205 dbenv->set_errpfx(dbenv, "citserver");
206 dbenv->set_paniccall(dbenv, dbpanic);
207 dbenv->set_errcall(dbenv, cdb_verbose_err);
208 dbenv->set_errpfx(dbenv, "ctdl");
209 dbenv->set_msgcall(dbenv, cdb_verbose_log);
210 dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
211 dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1);
212
213 /*
214 * We want to specify the shared memory buffer pool cachesize,
215 * but everything else is the default.
216 */
217 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
218 if (ret) {
219 syslog(LOG_ERR, "db: set_cachesize: %s", db_strerror(ret));
220 dbenv->close(dbenv, 0);
221 syslog(LOG_ERR, "db: exit code %d", ret);
222 exit(CTDLEXIT_DB);
223 }
224
225 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
226 syslog(LOG_ERR, "db: set_lk_detect: %s", db_strerror(ret));
227 dbenv->close(dbenv, 0);
228 syslog(LOG_ERR, "db: exit code %d", ret);
229 exit(CTDLEXIT_DB);
230 }
231
232 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_INIT_LOG;
233 syslog(LOG_DEBUG, "db: dbenv->open(dbenv, %s, %d, 0)", ctdl_db_dir, flags);
234 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try opening the database cleanly
235 if (ret == DB_RUNRECOVERY) {
236 syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
237 syslog(LOG_ERR, "db: attempting recovery...");
238 flags |= DB_RECOVER;
239 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try recovery
240 }
241 if (ret == DB_RUNRECOVERY) {
242 syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
243 syslog(LOG_ERR, "db: attempting catastrophic recovery...");
244 flags &= ~DB_RECOVER;
245 flags |= DB_RECOVER_FATAL;
246 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try catastrophic recovery
247 }
248 if (ret) {
249 syslog(LOG_ERR, "db: dbenv->open: %s", db_strerror(ret));
250 dbenv->close(dbenv, 0);
251 syslog(LOG_ERR, "db: exit code %d", ret);
252 exit(CTDLEXIT_DB);
253 }
254
255 syslog(LOG_INFO, "db: mounting databases");
256 for (i = 0; i < MAXCDB; ++i) {
257 ret = db_create(&dbp[i], dbenv, 0); // Create a database handle
258 if (ret) {
259 syslog(LOG_ERR, "db: db_create: %s", db_strerror(ret));
260 syslog(LOG_ERR, "db: exit code %d", ret);
261 exit(CTDLEXIT_DB);
262 }
263
264 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i); // table names by number
265 ret = dbp[i]->open(dbp[i], NULL, dbfilename, NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT | DB_THREAD, 0600);
266 if (ret) {
267 syslog(LOG_ERR, "db: db_open[%02x]: %s", i, db_strerror(ret));
268 if (ret == ENOMEM) {
269 syslog(LOG_ERR, "db: 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.");
270 }
271 syslog(LOG_ERR, "db: exit code %d", ret);
272 exit(CTDLEXIT_DB);
273 }
274 }
275}
276
277
278/*
279 * Make sure we own all the files, because in a few milliseconds we're going to drop root privs.
280 */
281void cdb_chmod_data(void) {
282 DIR *dp;
283 struct dirent *d;
284 char filename[PATH_MAX];
285
286 dp = opendir(ctdl_db_dir);
287 if (dp != NULL) {
288 while (d = readdir(dp), d != NULL) {
289 if (d->d_name[0] != '.') {
290 snprintf(filename, sizeof filename, "%s/%s", ctdl_db_dir, d->d_name);
291 syslog(LOG_DEBUG, "db: chmod(%s, 0600) returned %d", filename, chmod(filename, 0600));
292 syslog(LOG_DEBUG, "db: chown(%s, CTDLUID, -1) returned %d",
293 filename, chown(filename, CTDLUID, (-1))
294 );
295 }
296 }
297 closedir(dp);
298 }
299}
300
301
302/*
303 * Close all of the db database files we've opened. This can be done
304 * in a loop, since it's just a bunch of closes.
305 */
306void close_databases(void) {
307 int i;
308 int ret;
309
310 syslog(LOG_INFO, "db: performing final checkpoint");
311 if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
312 syslog(LOG_ERR, "db: txn_checkpoint: %s", db_strerror(ret));
313 }
314
315 syslog(LOG_INFO, "db: flushing the database logs");
316 if ((ret = dbenv->log_flush(dbenv, NULL))) {
317 syslog(LOG_ERR, "db: log_flush: %s", db_strerror(ret));
318 }
319
320 /* close the tables */
321 syslog(LOG_INFO, "db: closing databases");
322 for (i = 0; i < MAXCDB; ++i) {
323 syslog(LOG_INFO, "db: closing database %02x", i);
324 ret = dbp[i]->close(dbp[i], 0);
325 if (ret) {
326 syslog(LOG_ERR, "db: db_close: %s", db_strerror(ret));
327 }
328
329 }
330
331 // This seemed nifty at the time but did anyone really look at it?
332 // #ifdef DB_STAT_ALL
333 // /* print some statistics... */
334 // dbenv->lock_stat_print(dbenv, DB_STAT_ALL);
335 // #endif
336
337 /* Close the handle. */
338 ret = dbenv->close(dbenv, 0);
339 if (ret) {
340 syslog(LOG_ERR, "db: DBENV->close: %s", db_strerror(ret));
341 }
342}
343
344
345/*
346 * Decompress a database item if it was compressed on disk
347 */
349 static int magic = COMPRESS_MAGIC;
350
351 if ((cdb == NULL) || (cdb->ptr == NULL) || (cdb->len < sizeof(magic)) || (memcmp(cdb->ptr, &magic, sizeof(magic)))) {
352 return;
353 }
354
355 /* At this point we know we're looking at a compressed item. */
356
357 struct CtdlCompressHeader zheader;
358 char *uncompressed_data;
359 char *compressed_data;
360 uLongf destLen, sourceLen;
361 size_t cplen;
362
363 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
364 cplen = sizeof(struct CtdlCompressHeader);
365 if (sizeof(struct CtdlCompressHeader) > cdb->len) {
366 cplen = cdb->len;
367 }
368 memcpy(&zheader, cdb->ptr, cplen);
369
370 compressed_data = cdb->ptr;
371 compressed_data += sizeof(struct CtdlCompressHeader);
372
373 sourceLen = (uLongf) zheader.compressed_len;
374 destLen = (uLongf) zheader.uncompressed_len;
375 uncompressed_data = malloc(zheader.uncompressed_len);
376
377 if (uncompress((Bytef *) uncompressed_data,
378 (uLongf *) & destLen, (const Bytef *) compressed_data, (uLong) sourceLen) != Z_OK) {
379 syslog(LOG_ERR, "db: uncompress() error");
380 cdb_abort();
381 }
382
383 free(cdb->ptr);
384 cdb->len = (size_t) destLen;
385 cdb->ptr = uncompressed_data;
386}
387
388
389/*
390 * Store a piece of data. Returns 0 if the operation was successful. If a
391 * key already exists it should be overwritten.
392 */
393int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen) {
394
395 DBT dkey, ddata;
396 DB_TXN *tid = NULL;
397 int ret = 0;
398 struct CtdlCompressHeader zheader;
399 char *compressed_data = NULL;
400 int compressing = 0;
401 size_t buffer_len = 0;
402 uLongf destLen = 0;
403
404 memset(&dkey, 0, sizeof(DBT));
405 memset(&ddata, 0, sizeof(DBT));
406 dkey.size = ckeylen;
407 dkey.data = (void *) ckey;
408 ddata.size = cdatalen;
409 ddata.data = cdata;
410
411 /* Only compress Visit and UseTable records. Everything else is uncompressed. */
412 if ((cdb == CDB_VISIT) || (cdb == CDB_USETABLE)) {
413 compressing = 1;
414 zheader.magic = COMPRESS_MAGIC;
415 zheader.uncompressed_len = cdatalen;
416 buffer_len = ((cdatalen * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader);
417 destLen = (uLongf) buffer_len;
418 compressed_data = malloc(buffer_len);
419 if (compress2((Bytef *) (compressed_data + sizeof(struct CtdlCompressHeader)),
420 &destLen, (Bytef *) cdata, (uLongf) cdatalen, 1) != Z_OK) {
421 syslog(LOG_ERR, "db: compress2() error");
422 cdb_abort();
423 }
424 zheader.compressed_len = (size_t) destLen;
425 memcpy(compressed_data, &zheader, sizeof(struct CtdlCompressHeader));
426 ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
427 ddata.data = compressed_data;
428 }
429
430 if (TSD->tid != NULL) {
431 ret = dbp[cdb]->put(dbp[cdb], // db
432 TSD->tid, // transaction ID
433 &dkey, // key
434 &ddata, // data
435 0 // flags
436 );
437 if (ret) {
438 syslog(LOG_ERR, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
439 cdb_abort();
440 }
441 if (compressing) {
442 free(compressed_data);
443 }
444 return ret;
445 } else {
446 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
447
448 retry:
449 txbegin(&tid);
450
451 if ((ret = dbp[cdb]->put(dbp[cdb], // db
452 tid, // transaction ID
453 &dkey, // key
454 &ddata, // data
455 0))) { // flags
456 if (ret == DB_LOCK_DEADLOCK) {
457 txabort(tid);
458 goto retry;
459 } else {
460 syslog(LOG_ERR, "db: cdb_store(%d): %s", cdb, db_strerror(ret));
461 cdb_abort();
462 }
463 } else {
464 txcommit(tid);
465 if (compressing) {
466 free(compressed_data);
467 }
468 return ret;
469 }
470 }
471 return ret;
472}
473
474
475/*
476 * Delete a piece of data. Returns 0 if the operation was successful.
477 */
478int cdb_delete(int cdb, void *key, int keylen) {
479 DBT dkey;
480 DB_TXN *tid;
481 int ret;
482
483 memset(&dkey, 0, sizeof dkey);
484 dkey.size = keylen;
485 dkey.data = key;
486
487 if (TSD->tid != NULL) {
488 ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
489 if (ret) {
490 syslog(LOG_ERR, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
491 if (ret != DB_NOTFOUND) {
492 cdb_abort();
493 }
494 }
495 } else {
496 bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
497
498 retry:
499 txbegin(&tid);
500
501 if ((ret = dbp[cdb]->del(dbp[cdb], tid, &dkey, 0)) && ret != DB_NOTFOUND) {
502 if (ret == DB_LOCK_DEADLOCK) {
503 txabort(tid);
504 goto retry;
505 } else {
506 syslog(LOG_ERR, "db: cdb_delete(%d): %s", cdb, db_strerror(ret));
507 cdb_abort();
508 }
509 } else {
510 txcommit(tid);
511 }
512 }
513 return ret;
514}
515
516
517static DBC *localcursor(int cdb) {
518 int ret;
519 DBC *curs;
520
521 if (TSD->cursors[cdb] == NULL) {
522 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
523 }
524 else {
525 ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
526 }
527
528 if (ret) {
529 syslog(LOG_ERR, "db: localcursor: %s", db_strerror(ret));
530 cdb_abort();
531 }
532
533 return curs;
534}
535
536
537/*
538 * Fetch a piece of data. If not found, returns NULL. Otherwise, it returns
539 * a struct cdbdata which it is the caller's responsibility to free later on
540 * using the cdb_free() routine.
541 */
542struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) {
543
544 if (keylen == 0) { // key length zero is impossible
545 return(NULL);
546 }
547
548 struct cdbdata *tempcdb;
549 DBT dkey, dret;
550 int ret;
551
552 memset(&dkey, 0, sizeof(DBT));
553 dkey.size = keylen;
554 dkey.data = (void *) key;
555
556 if (TSD->tid != NULL) {
557 memset(&dret, 0, sizeof(DBT));
558 dret.flags = DB_DBT_MALLOC;
559 ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
560 }
561 else {
562 DBC *curs;
563
564 do {
565 memset(&dret, 0, sizeof(DBT));
566 dret.flags = DB_DBT_MALLOC;
567 curs = localcursor(cdb);
568 ret = curs->c_get(curs, &dkey, &dret, DB_SET);
569 cclose(curs);
570 } while (ret == DB_LOCK_DEADLOCK);
571 }
572
573 if ((ret != 0) && (ret != DB_NOTFOUND)) {
574 syslog(LOG_ERR, "db: cdb_fetch(%d): %s", cdb, db_strerror(ret));
575 cdb_abort();
576 }
577
578 if (ret != 0) {
579 return NULL;
580 }
581
582 tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
583 if (tempcdb == NULL) {
584 syslog(LOG_ERR, "db: cdb_fetch() cannot allocate memory for tempcdb: %m");
585 cdb_abort();
586 return NULL; /* make it easier for static analysis... */
587 }
588 else {
589 tempcdb->len = dret.size;
590 tempcdb->ptr = dret.data;
592 return (tempcdb);
593 }
594}
595
596
597/*
598 * Free a cdbdata item.
599 *
600 * Note that we only free the 'ptr' portion if it is not NULL. This allows
601 * other code to assume ownership of that memory simply by storing the
602 * pointer elsewhere and then setting 'ptr' to NULL. cdb_free() will then
603 * avoid freeing it.
604 */
605void cdb_free(struct cdbdata *cdb) {
606 if (cdb->ptr) {
607 free(cdb->ptr);
608 }
609 free(cdb);
610}
611
612
613void cdb_close_cursor(int cdb) {
614 if (TSD->cursors[cdb] != NULL) {
615 cclose(TSD->cursors[cdb]);
616 }
617
618 TSD->cursors[cdb] = NULL;
619}
620
621
622/*
623 * Prepare for a sequential search of an entire database.
624 * (There is guaranteed to be no more than one traversal in
625 * progress per thread at any given time.)
626 */
627void cdb_rewind(int cdb) {
628 int ret = 0;
629
630 if (TSD->cursors[cdb] != NULL) {
631 syslog(LOG_ERR, "db: cdb_rewind: must close cursor on database %d before reopening", cdb);
632 cdb_abort();
633 /* cclose(TSD->cursors[cdb]); */
634 }
635
636 /*
637 * Now initialize the cursor
638 */
639 ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
640 if (ret) {
641 syslog(LOG_ERR, "db: cdb_rewind: db_cursor: %s", db_strerror(ret));
642 cdb_abort();
643 }
644}
645
646
647/*
648 * Fetch the next item in a sequential search. Returns a pointer to a
649 * cdbdata structure, or NULL if we've hit the end.
650 */
651struct cdbdata *cdb_next_item(int cdb) {
652 DBT key, data;
653 struct cdbdata *cdbret;
654 int ret = 0;
655
656 /* Initialize the key/data pair so the flags aren't set. */
657 memset(&key, 0, sizeof(key));
658 memset(&data, 0, sizeof(data));
659 data.flags = DB_DBT_MALLOC;
660
661 ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
662
663 if (ret) {
664 if (ret != DB_NOTFOUND) {
665 syslog(LOG_ERR, "db: cdb_next_item(%d): %s", cdb, db_strerror(ret));
666 cdb_abort();
667 }
668 cdb_close_cursor(cdb);
669 return NULL; /* presumably, end of file */
670 }
671
672 cdbret = (struct cdbdata *) malloc(sizeof(struct cdbdata));
673 cdbret->len = data.size;
674 cdbret->ptr = data.data;
676
677 return (cdbret);
678}
679
680
681/*
682 * Transaction-based stuff. I'm writing this as I bake cookies...
683 */
685 bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
686
687 if (TSD->tid != NULL) {
688 syslog(LOG_ERR, "db: cdb_begin_transaction: ERROR: nested transaction");
689 cdb_abort();
690 }
691
692 txbegin(&TSD->tid);
693}
694
695
697 int i;
698
699 for (i = 0; i < MAXCDB; i++) {
700 if (TSD->cursors[i] != NULL) {
701 syslog(LOG_WARNING, "db: cdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
702 cclose(TSD->cursors[i]);
703 TSD->cursors[i] = NULL;
704 }
705 }
706
707 if (TSD->tid == NULL) {
708 syslog(LOG_ERR, "db: cdb_end_transaction: ERROR: txcommit(NULL) !!");
709 cdb_abort();
710 }
711 else {
712 txcommit(TSD->tid);
713 }
714
715 TSD->tid = NULL;
716}
717
718
719/*
720 * Truncate (delete every record)
721 */
722void cdb_trunc(int cdb) {
723 /* DB_TXN *tid; */
724 int ret;
725 u_int32_t count;
726
727 if (TSD->tid != NULL) {
728 syslog(LOG_ERR, "db: cdb_trunc must not be called in a transaction.");
729 cdb_abort();
730 }
731 else {
732 bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
733
734 retry:
735 /* txbegin(&tid); */
736
737 if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
738 NULL, /* transaction ID */
739 &count, /* #rows deleted */
740 0))) { /* flags */
741 if (ret == DB_LOCK_DEADLOCK) {
742 /* txabort(tid); */
743 goto retry;
744 } else {
745 syslog(LOG_ERR, "db: cdb_truncate(%d): %s", cdb, db_strerror(ret));
746 if (ret == ENOMEM) {
747 syslog(LOG_ERR, "db: 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.");
748 }
749 exit(CTDLEXIT_DB);
750 }
751 }
752 else {
753 /* txcommit(tid); */
754 }
755 }
756}
757
758
759// compact (defragment) the database, possibly returning space back to the underlying filesystem
760void cdb_compact(void) {
761 int ret;
762 int i;
763
764 syslog(LOG_DEBUG, "db: cdb_compact() started");
765 for (i = 0; i < MAXCDB; i++) {
766 syslog(LOG_DEBUG, "db: compacting database %d", i);
767 ret = dbp[i]->compact(dbp[i], NULL, NULL, NULL, NULL, DB_FREE_SPACE, NULL);
768 if (ret) {
769 syslog(LOG_ERR, "db: compact: %s", db_strerror(ret));
770 }
771 }
772 syslog(LOG_DEBUG, "db: cdb_compact() finished");
773}
774
775
776// Has an item already been seen (is it in the CDB_USETABLE) ?
777// Returns 0 if it hasn't, 1 if it has
778// In either case, writes the item to the database for next time.
779int CheckIfAlreadySeen(StrBuf *guid) {
780 int found = 0;
781 struct UseTable ut;
782 struct cdbdata *cdbut;
783
784 syslog(LOG_DEBUG, "db: CheckIfAlreadySeen(%s)", ChrPtr(guid));
785 cdbut = cdb_fetch(CDB_USETABLE, SKEY(guid));
786 if (cdbut != NULL) {
787 found = 1;
788 cdb_free(cdbut);
789 }
790
791 /* (Re)write the record, to update the timestamp. Zeroing it out makes it compress better. */
792 memset(&ut, 0, sizeof(struct UseTable));
793 memcpy(ut.ut_msgid, SKEY(guid));
794 ut.ut_timestamp = time(NULL);
795 cdb_store(CDB_USETABLE, SKEY(guid), &ut, sizeof(struct UseTable));
796 return (found);
797}
798
799
801 if (!threading) {
802 // nothing to do here
803 }
804
805 /* return our module id for the log */
806 return "database";
807}
#define ctdl_db_dir
Definition: citadel_dirs.h:14
int CtdlGetConfigInt(char *key)
Definition: config.c:391
int threading
Definition: modules_init.c:25
#define CTDLUID
Definition: ctdl_module.h:262
void cdb_checkpoint(void)
Definition: database.c:146
void cdb_check_handles(void)
Definition: database.c:133
static void cclose(DBC *cursor)
Definition: database.c:112
void cdb_close_cursor(int cdb)
Definition: database.c:613
static void bailIfCursor(DBC **cursors, const char *msg)
Definition: database.c:122
void cdb_free(struct cdbdata *cdb)
Definition: database.c:605
static void dbpanic(DB_ENV *env, int errval)
Definition: database.c:107
void cdb_decompress_if_necessary(struct cdbdata *cdb)
Definition: database.c:348
static DB * dbp[MAXCDB]
Definition: database.c:41
struct cdbdata * cdb_next_item(int cdb)
Definition: database.c:651
void cdb_begin_transaction(void)
Definition: database.c:684
void cdb_chmod_data(void)
Definition: database.c:281
int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
Definition: database.c:393
void open_databases(void)
Definition: database.c:173
void cdb_verbose_log(const DB_ENV *dbenv, const char *msg, const char *what_is_this)
Definition: database.c:54
static DB_ENV * dbenv
Definition: database.c:42
static void txcommit(DB_TXN *tid)
Definition: database.c:81
void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
Definition: database.c:62
struct cdbdata * cdb_fetch(int cdb, const void *key, int keylen)
Definition: database.c:542
int cdb_delete(int cdb, void *key, int keylen)
Definition: database.c:478
void close_databases(void)
Definition: database.c:306
static void txabort(DB_TXN *tid)
Definition: database.c:68
#define MAX_CHECKPOINT_MINUTES
Definition: database.c:18
#define MAX_CHECKPOINT_KBYTES
Definition: database.c:17
void cdb_abort(void)
Definition: database.c:45
static void txbegin(DB_TXN **tid)
Definition: database.c:94
char * ctdl_module_init_database()
Definition: database.c:800
void cdb_trunc(int cdb)
Definition: database.c:722
void cdb_rewind(int cdb)
Definition: database.c:627
void cdb_end_transaction(void)
Definition: database.c:696
int CheckIfAlreadySeen(StrBuf *guid)
Definition: database.c:779
void cdb_compact(void)
Definition: database.c:760
static DBC * localcursor(int cdb)
Definition: database.c:517
#define COMPRESS_MAGIC
Definition: database.h:49
void * malloc(unsigned)
void free(void *)
@ CDB_VISIT
Definition: server.h:190
@ MAXCDB
Definition: server.h:199
@ CDB_USETABLE
Definition: server.h:192
#define CTDLEXIT_DB
Definition: server.h:75
size_t compressed_len
Definition: database.h:54
size_t uncompressed_len
Definition: database.h:53
char ut_msgid[4096]
Definition: server.h:298
time_t ut_timestamp
Definition: server.h:299
size_t len
Definition: server.h:203
char * ptr
Definition: server.h:204
#define TSD
Definition: threads.h:29