arrange to hold the locks for the bare minimum of time.
API:
- dbfn_open
- dbfn_close
+ exim_lockfile_needed facilities predicate
+ dbfn_open_path full pathname; no lock taken, readonly
+ dbfn_open takes lockfile or opens transaction
+ dbfn_open_multi only if transactions supported;
+ no lock or transaction taken
+ dbfn_close release lockfile or transaction
+ dbfn_close_multi
+ dbfn_transaction_start only if transactions supported
+ dbfn_transaction_commit
+ dbfn_read_klen explicit key length; embedded NUL ok
dbfn_read_with_length
dbfn_read_enforce_length
dbfn_write
TLS session resumption
peer capability cache
callout & quota cache
+ DBM lookup type
*/
* Open and lock a database file *
*************************************************/
+/* Used by DBM lookups:
+full pathname for DB file rather than hintsdb name, readonly, no locking. */
+
+open_db *
+dbfn_open_path(const uschar * path, open_db * dbblock)
+{
+uschar * dirname = string_copy(path);
+
+dbblock->readonly = TRUE;
+dbblock->lockfd = -1;
+dbblock->dbptr = !exim_lockfile_needed()
+ ? exim_dbopen_multi(path, dirname, O_RDONLY, 0)
+ : exim_dbopen(path, dirname, O_RDONLY, 0);
+return dbblock->dbptr ? dbblock : NULL;;
+}
+
/* Ensure the directory for the DB is present */
static inline void
Arguments:
name The single-component name of one of Exim's database files.
flags Either O_RDONLY or O_RDWR, indicating the type of open required;
- O_RDWR implies "create if necessary"
+ optionally O_CREAT
dbblock Points to an open_db block to be filled in.
lof If TRUE, write to the log for actual open failures (locking failures
are always logged).
dbfn_open(const uschar * name, int flags, open_db * dbblock,
BOOL lof, BOOL panic)
{
-int rc, save_errno;
+int rc, save_errno, dlen, flen;
flock_t lock_data;
uschar dirname[PATHLEN], filename[PATHLEN];
unnecessarily, because usually the lock file will be there. If the directory
exists, there is no error. */
-snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
-snprintf(CS filename, sizeof(filename), "%s/%s.lockfile", dirname, name);
+dlen = snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
+dbblock->readonly = (flags & O_ACCMODE) == O_RDONLY;
dbblock->lockfd = -1;
if (!exim_lockfile_needed())
db_dir_make(panic);
else
{
+ flen = Ustrlen(name);
+ snprintf(CS filename, sizeof(filename), "%.*s/%.*s.lockfile",
+ (int)sizeof(filename) - dlen - flen - 11, dirname,
+ flen, name);
if (!lockfile_take(dbblock, filename, flags == O_RDONLY, panic))
{
DEBUG(D_hints_lookup) acl_level--;
it easy to pin this down, there are now debug statements on either side of the
open call. */
-flags &= O_RDONLY | O_RDWR;
-snprintf(CS filename, sizeof(filename), "%s/%s", dirname, name);
+snprintf(CS filename, sizeof(filename), "%.*s/%s", dlen, dirname, name);
priv_drop_temp(exim_uid, exim_gid);
-dbblock->dbptr = exim_dbopen(filename, dirname, flags, EXIMDB_MODE);
-if (!dbblock->dbptr && errno == ENOENT && flags == O_RDWR)
+dbblock->dbptr = dbblock->readonly && !exim_lockfile_needed()
+ ? exim_dbopen_multi(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE)
+ : exim_dbopen(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE);
+
+if (!dbblock->dbptr && errno == ENOENT && flags & O_CREAT)
{
DEBUG(D_hints_lookup)
debug_printf_indent("%s appears not to exist: trying to create\n", filename);
- dbblock->dbptr = exim_dbopen(filename, dirname, flags|O_CREAT, EXIMDB_MODE);
+ dbblock->dbptr = exim_dbopen(filename, dirname, flags, EXIMDB_MODE);
}
save_errno = errno;
priv_restore();
filename));
(void)close(dbblock->lockfd);
dbblock->lockfd = -1;
- errno = save_errno;
- DEBUG(D_hints_lookup) acl_level--;
- return NULL;
+ dbblock = NULL;
}
-DEBUG(D_hints_lookup)
- debug_printf_indent("opened hints database %s: flags=%s\n", filename,
- flags == O_RDONLY ? "O_RDONLY"
- : flags == O_RDWR ? "O_RDWR"
- : "??");
-
/* Pass back the block containing the opened database handle and the open fd
for the lock. */
+DEBUG(D_hints_lookup) acl_level--;
+return dbblock;
+}
+
+
+
+/* Only for transaction-capable DB types. Open without locking or
+starting a transaction. "lof" and "panic" always true; read/write mode.
+*/
+
+open_db *
+dbfn_open_multi(const uschar * name, int flags, open_db * dbblock)
+{
+int rc, save_errno, dlen;
+flock_t lock_data;
+uschar dirname[PATHLEN], filename[PATHLEN];
+
+DEBUG(D_hints_lookup) acl_level++;
+
+dbblock->lockfd = -1;
+dbblock->readonly = (flags & O_ACCMODE) == O_RDONLY;
+db_dir_make(TRUE);
+
+dlen = snprintf(CS dirname, sizeof(dirname), "%s/db", spool_directory);
+snprintf(CS filename, sizeof(filename), "%.*s/%s", dlen, dirname, name);
+
+priv_drop_temp(exim_uid, exim_gid);
+dbblock->dbptr = exim_dbopen_multi(filename, dirname, flags & O_ACCMODE, EXIMDB_MODE);
+if (!dbblock->dbptr && errno == ENOENT && flags & O_CREAT)
+ {
+ DEBUG(D_hints_lookup)
+ debug_printf_indent("%s appears not to exist: trying to create\n", filename);
+ dbblock->dbptr = exim_dbopen_multi(filename, dirname, flags, EXIMDB_MODE);
+ }
+save_errno = errno;
+priv_restore();
+
+/* If the open has failed, return NULL, leaving errno set. If lof is TRUE,
+log the event - also for debugging - but debug only if the file just doesn't
+exist. */
+
+if (!dbblock->dbptr)
+ {
+ errno = save_errno;
+ if (save_errno != ENOENT)
+ log_write(0, LOG_MAIN, "%s", string_open_failed("DB file %s",
+ filename));
+ else
+ DEBUG(D_hints_lookup)
+ debug_printf_indent("%s\n", CS string_open_failed("DB file %s",
+ filename));
+ dbblock = NULL;
+ }
+
+/* Pass back the block containing the opened database handle */
+DEBUG(D_hints_lookup) acl_level--;
return dbblock;
}
+/* Return: boolean success */
+BOOL
+dbfn_transaction_start(open_db * dbp)
+{
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_transaction_start\n");
+if (!dbp->readonly) return exim_dbtransaction_start(dbp->dbptr);
+return FALSE;
+}
+void
+dbfn_transaction_commit(open_db * dbp)
+{
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_transaction_commit\n");
+if (!dbp->readonly) exim_dbtransaction_commit(dbp->dbptr);
+}
+
/*************************************************
*/
void
-dbfn_close(open_db *dbblock)
+dbfn_close(open_db * dbp)
{
-int * fdp = &dbblock->lockfd;
+int * fdp = &dbp->lockfd;
+
+if (dbp->readonly && !exim_lockfile_needed())
+ exim_dbclose_multi(dbp->dbptr);
+else
+ exim_dbclose(dbp->dbptr);
-exim_dbclose(dbblock->dbptr);
if (*fdp >= 0) (void)close(*fdp);
DEBUG(D_hints_lookup)
- {
debug_printf_indent("closed hints database%s\n",
*fdp < 0 ? "" : " and lockfile");
- acl_level--;
- }
*fdp = -1;
}
+void
+dbfn_close_multi(open_db * dbp)
+{
+exim_dbclose_multi(dbp->dbptr);
+DEBUG(D_hints_lookup)
+ debug_printf_indent("closed hints database\n");
+}
+
+
/*************************************************
* Read from database file *
*************************************************/
-/* Passing back the pointer unchanged is useless, because there is
+/* Read, using a defined-length key (permitting embedded NULs).
+
+Passing back the pointer unchanged is useless, because there is
no guarantee of alignment. Since all the records used by Exim need
to be properly aligned to pick out the timestamps, etc., we might as
well do the copying centrally here.
-Most calls don't need the length, so there is a macro called dbfn_read which
-has two arguments; it calls this function adding NULL as the third.
-
Arguments:
dbblock a pointer to an open database block
key the key of the record to be read
+ klen length of key including a terminating NUL (if present)
length a pointer to an int into which to return the length, if not NULL
Returns: a pointer to the retrieved record, or
*/
void *
-dbfn_read_with_length(open_db * dbblock, const uschar * key, int * length)
+dbfn_read_klen(open_db * dbblock, const uschar * key, int klen, int * length)
{
void * yield;
EXIM_DATUM key_datum, result_datum;
-int klen = Ustrlen(key) + 1;
uschar * key_copy = store_get(klen, key);
unsigned dlen;
memcpy(key_copy, key, klen);
-DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: key=%s\n", key);
+DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: key=%.*s\n", klen, key);
exim_datum_init(&key_datum); /* Some DBM libraries require the datum */
exim_datum_init(&result_datum); /* to be cleared before use. */
store the taint status with the data. */
dlen = exim_datum_size_get(&result_datum);
-yield = store_get(dlen, GET_TAINTED);
-memcpy(yield, exim_datum_data_get(&result_datum), dlen);
DEBUG(D_hints_lookup) debug_printf_indent("dbfn_read: size %u return\n", dlen);
+
+yield = store_get(dlen+1, GET_TAINTED);
+memcpy(yield, exim_datum_data_get(&result_datum), dlen);
+((uschar *)yield)[dlen] = '\0';
if (length) *length = dlen;
exim_datum_free(&result_datum); /* Some DBM libs require freeing */
}
+/* Read, using a NUL-terminated key.
+
+Most calls don't need the length, so there is a macro called dbfn_read which
+has two arguments; it calls this function adding NULL as the third.
+
+Arguments:
+ dbblock a pointer to an open database block
+ key the key of the record to be read (NUL-terminated)
+ lenp a pointer to an int into which to return the data length,
+ if not NULL
+
+Returns: a pointer to the retrieved record, or
+ NULL if the record is not found
+*/
+
+void *
+dbfn_read_with_length(open_db * dbblock, const uschar * key, int * lenp)
+{
+return dbfn_read_klen(dbblock, key, Ustrlen(key)+1, lenp);
+}
+
+
+
/* Read a record. If the length is not as expected then delete it, write
an error log line, delete the record and return NULL.
Use this for fixed-size records (so not retry or wait records).
return NULL;
}
-
/*************************************************
* Write to database file *
*************************************************/
}
start = clock();
- odb = dbfn_open(s, O_RDWR, dbblock + i, TRUE, TRUE);
+ odb = dbfn_open(s, O_RDWR|O_CREAT, dbblock + i, TRUE, TRUE);
stop = clock();
if (odb)