1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1996, 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)db_appinit.c 10.66 (Sleepycat) 12/7/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <ctype.h>
1N/A#include <errno.h>
1N/A#include <stdlib.h>
1N/A#include <string.h>
1N/A#include <unistd.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "shqueue.h"
1N/A#include "db_page.h"
1N/A#include "btree.h"
1N/A#include "hash.h"
1N/A#include "log.h"
1N/A#include "txn.h"
1N/A#include "clib_ext.h"
1N/A#include "common_ext.h"
1N/A
1N/Astatic int __db_home __P((DB_ENV *, const char *, u_int32_t));
1N/Astatic int __db_parse __P((DB_ENV *, char *));
1N/Astatic int __db_tmp_open __P((DB_ENV *, u_int32_t, char *, int *));
1N/A
1N/A/*
1N/A * This conflict array is used for concurrent db access (cdb). It
1N/A * uses the same locks as the db_rw_conflict array, but adds an IW
1N/A * mode to be used for write cursors.
1N/A */
1N/Astatic u_int8_t const db_cdb_conflicts[] = {
1N/A /* N R W IW */
1N/A /* N */ 0, 0, 0, 0,
1N/A /* R */ 0, 0, 1, 0,
1N/A /* W */ 0, 1, 1, 1,
1N/A /* IW */ 0, 0, 1, 1
1N/A};
1N/A
1N/A/*
1N/A * db_version --
1N/A * Return version information.
1N/A */
1N/Achar *
1N/Adb_version(majverp, minverp, patchp)
1N/A int *majverp, *minverp, *patchp;
1N/A{
1N/A if (majverp != NULL)
1N/A *majverp = DB_VERSION_MAJOR;
1N/A if (minverp != NULL)
1N/A *minverp = DB_VERSION_MINOR;
1N/A if (patchp != NULL)
1N/A *patchp = DB_VERSION_PATCH;
1N/A return ((char *)DB_VERSION_STRING);
1N/A}
1N/A
1N/A/*
1N/A * db_appinit --
1N/A * Initialize the application environment.
1N/A */
1N/Aint
1N/Adb_appinit(db_home, db_config, dbenv, flags)
1N/A const char *db_home;
1N/A char * const *db_config;
1N/A DB_ENV *dbenv;
1N/A u_int32_t flags;
1N/A{
1N/A FILE *fp;
1N/A int mode, ret;
1N/A char * const *p;
1N/A char *lp, buf[MAXPATHLEN * 2];
1N/A
1N/A fp = NULL;
1N/A
1N/A /* Validate arguments. */
1N/A if (dbenv == NULL)
1N/A return (EINVAL);
1N/A
1N/A#ifdef HAVE_SPINLOCKS
1N/A#define OKFLAGS \
1N/A (DB_CREATE | DB_INIT_CDB | DB_INIT_LOCK | DB_INIT_LOG | \
1N/A DB_INIT_MPOOL | DB_INIT_TXN | DB_MPOOL_PRIVATE | DB_NOMMAP | \
1N/A DB_RECOVER | DB_RECOVER_FATAL | DB_THREAD | DB_TXN_NOSYNC | \
1N/A DB_USE_ENVIRON | DB_USE_ENVIRON_ROOT)
1N/A#else
1N/A#define OKFLAGS \
1N/A (DB_CREATE | DB_INIT_CDB | DB_INIT_LOCK | DB_INIT_LOG | \
1N/A DB_INIT_MPOOL | DB_INIT_TXN | DB_MPOOL_PRIVATE | DB_NOMMAP | \
1N/A DB_RECOVER | DB_RECOVER_FATAL | DB_TXN_NOSYNC | \
1N/A DB_USE_ENVIRON | DB_USE_ENVIRON_ROOT)
1N/A#endif
1N/A if ((ret = __db_fchk(dbenv, "db_appinit", flags, OKFLAGS)) != 0)
1N/A return (ret);
1N/A
1N/A /* Transactions imply logging. */
1N/A if (LF_ISSET(DB_INIT_TXN))
1N/A LF_SET(DB_INIT_LOG);
1N/A
1N/A /* Convert the db_appinit(3) flags. */
1N/A if (LF_ISSET(DB_THREAD))
1N/A F_SET(dbenv, DB_ENV_THREAD);
1N/A
1N/A /* Set the database home. */
1N/A if ((ret = __db_home(dbenv, db_home, flags)) != 0)
1N/A goto err;
1N/A
1N/A /* Parse the config array. */
1N/A for (p = db_config; p != NULL && *p != NULL; ++p)
1N/A if ((ret = __db_parse(dbenv, *p)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Parse the config file.
1N/A *
1N/A * XXX
1N/A * Don't use sprintf(3)/snprintf(3) -- the former is dangerous, and
1N/A * the latter isn't standard, and we're manipulating strings handed
1N/A * us by the application.
1N/A */
1N/A if (dbenv->db_home != NULL) {
1N/A#define CONFIG_NAME "/DB_CONFIG"
1N/A if (strlen(dbenv->db_home) +
1N/A strlen(CONFIG_NAME) + 1 > sizeof(buf)) {
1N/A ret = ENAMETOOLONG;
1N/A goto err;
1N/A }
1N/A (void)strcpy(buf, dbenv->db_home);
1N/A (void)strcat(buf, CONFIG_NAME);
1N/A if ((fp = fopen(buf, "r")) != NULL) {
1N/A while (fgets(buf, sizeof(buf), fp) != NULL) {
1N/A if ((lp = strchr(buf, '\n')) == NULL) {
1N/A __db_err(dbenv,
1N/A "%s: line too long", CONFIG_NAME);
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A *lp = '\0';
1N/A if (buf[0] == '\0' ||
1N/A buf[0] == '#' || isspace(buf[0]))
1N/A continue;
1N/A
1N/A if ((ret = __db_parse(dbenv, buf)) != 0)
1N/A goto err;
1N/A }
1N/A (void)fclose(fp);
1N/A fp = NULL;
1N/A }
1N/A }
1N/A
1N/A /* Set up the tmp directory path. */
1N/A if (dbenv->db_tmp_dir == NULL && (ret = __os_tmpdir(dbenv, flags)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * Flag that the structure has been initialized by the application.
1N/A * Note, this must be set before calling into the subsystems as it
1N/A * is used when we're doing file naming.
1N/A */
1N/A F_SET(dbenv, DB_ENV_APPINIT);
1N/A
1N/A /*
1N/A * If we are doing recovery, remove all the old shared memory
1N/A * regions.
1N/A */
1N/A if (LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL)) {
1N/A if ((ret = log_unlink(NULL, 1, dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = memp_unlink(NULL, 1, dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = lock_unlink(NULL, 1, dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = txn_unlink(NULL, 1, dbenv)) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A /*
1N/A * Create the new shared regions.
1N/A *
1N/A * Default permissions are read-write for both owner and group.
1N/A */
1N/A mode = __db_omode("rwrw--");
1N/A if (LF_ISSET(DB_INIT_CDB)) {
1N/A if (LF_ISSET(DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN)) {
1N/A ret = EINVAL;
1N/A goto err;
1N/A }
1N/A F_SET(dbenv, DB_ENV_CDB);
1N/A dbenv->lk_conflicts = db_cdb_conflicts;
1N/A dbenv->lk_modes = DB_LOCK_RW_N + 1;
1N/A if ((ret = lock_open(NULL, LF_ISSET(DB_CREATE | DB_THREAD),
1N/A mode, dbenv, &dbenv->lk_info)) != 0)
1N/A goto err;
1N/A }
1N/A if (LF_ISSET(DB_INIT_LOCK) && (ret = lock_open(NULL,
1N/A LF_ISSET(DB_CREATE | DB_THREAD),
1N/A mode, dbenv, &dbenv->lk_info)) != 0)
1N/A goto err;
1N/A if (LF_ISSET(DB_INIT_LOG) && (ret = log_open(NULL,
1N/A LF_ISSET(DB_CREATE | DB_THREAD),
1N/A mode, dbenv, &dbenv->lg_info)) != 0)
1N/A goto err;
1N/A if (LF_ISSET(DB_INIT_MPOOL) && (ret = memp_open(NULL,
1N/A LF_ISSET(DB_CREATE | DB_MPOOL_PRIVATE | DB_NOMMAP | DB_THREAD),
1N/A mode, dbenv, &dbenv->mp_info)) != 0)
1N/A goto err;
1N/A if (LF_ISSET(DB_INIT_TXN) && (ret = txn_open(NULL,
1N/A LF_ISSET(DB_CREATE | DB_THREAD | DB_TXN_NOSYNC),
1N/A mode, dbenv, &dbenv->tx_info)) != 0)
1N/A goto err;
1N/A
1N/A /*
1N/A * If the application is running with transactions, initialize the
1N/A * function tables. Once that's done, do recovery for any previous
1N/A * run.
1N/A */
1N/A if (LF_ISSET(DB_INIT_TXN)) {
1N/A if ((ret = __bam_init_recover(dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = __db_init_recover(dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = __ham_init_recover(dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = __log_init_recover(dbenv)) != 0)
1N/A goto err;
1N/A if ((ret = __txn_init_recover(dbenv)) != 0)
1N/A goto err;
1N/A
1N/A if (LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL) &&
1N/A (ret = __db_apprec(dbenv,
1N/A LF_ISSET(DB_RECOVER | DB_RECOVER_FATAL))) != 0)
1N/A goto err;
1N/A }
1N/A
1N/A return (ret);
1N/A
1N/Aerr: if (fp != NULL)
1N/A (void)fclose(fp);
1N/A
1N/A (void)db_appexit(dbenv);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * db_appexit --
1N/A * Close down the default application environment.
1N/A */
1N/Aint
1N/Adb_appexit(dbenv)
1N/A DB_ENV *dbenv;
1N/A{
1N/A int ret, t_ret;
1N/A char **p;
1N/A
1N/A ret = 0;
1N/A
1N/A /* Close subsystems. */
1N/A if (dbenv->tx_info && (t_ret = txn_close(dbenv->tx_info)) != 0)
1N/A if (ret == 0)
1N/A ret = t_ret;
1N/A if (dbenv->lg_info && (t_ret = log_close(dbenv->lg_info)) != 0)
1N/A if (ret == 0)
1N/A ret = t_ret;
1N/A if (dbenv->mp_info && (t_ret = memp_close(dbenv->mp_info)) != 0)
1N/A if (ret == 0)
1N/A ret = t_ret;
1N/A if (dbenv->lk_info && (t_ret = lock_close(dbenv->lk_info)) != 0)
1N/A if (ret == 0)
1N/A ret = t_ret;
1N/A
1N/A /* Clear initialized flag (after subsystems, it affects naming). */
1N/A F_CLR(dbenv, DB_ENV_APPINIT);
1N/A
1N/A /* Free allocated memory. */
1N/A if (dbenv->db_home != NULL)
1N/A __os_freestr(dbenv->db_home);
1N/A if ((p = dbenv->db_data_dir) != NULL) {
1N/A for (; *p != NULL; ++p)
1N/A __os_freestr(*p);
1N/A __os_free(dbenv->db_data_dir,
1N/A dbenv->data_cnt * sizeof(char **));
1N/A }
1N/A if (dbenv->db_log_dir != NULL)
1N/A __os_freestr(dbenv->db_log_dir);
1N/A if (dbenv->db_tmp_dir != NULL)
1N/A __os_freestr(dbenv->db_tmp_dir);
1N/A
1N/A return (ret);
1N/A}
1N/A
1N/A#define DB_ADDSTR(str) { \
1N/A if ((str) != NULL) { \
1N/A /* If leading slash, start over. */ \
1N/A if (__os_abspath(str)) { \
1N/A p = start; \
1N/A slash = 0; \
1N/A } \
1N/A /* Append to the current string. */ \
1N/A len = strlen(str); \
1N/A if (slash) \
1N/A *p++ = PATH_SEPARATOR[0]; \
1N/A memcpy(p, str, len); \
1N/A p += len; \
1N/A slash = strchr(PATH_SEPARATOR, p[-1]) == NULL; \
1N/A } \
1N/A}
1N/A
1N/A/*
1N/A * __db_appname --
1N/A * Given an optional DB environment, directory and file name and type
1N/A * of call, build a path based on the db_appinit(3) rules, and return
1N/A * it in allocated space.
1N/A *
1N/A * PUBLIC: int __db_appname __P((DB_ENV *,
1N/A * PUBLIC: APPNAME, const char *, const char *, u_int32_t, int *, char **));
1N/A */
1N/Aint
1N/A__db_appname(dbenv, appname, dir, file, tmp_oflags, fdp, namep)
1N/A DB_ENV *dbenv;
1N/A APPNAME appname;
1N/A const char *dir, *file;
1N/A u_int32_t tmp_oflags;
1N/A int *fdp;
1N/A char **namep;
1N/A{
1N/A DB_ENV etmp;
1N/A size_t len;
1N/A int data_entry, ret, slash, tmp_create, tmp_free;
1N/A const char *a, *b, *c;
1N/A char *p, *start;
1N/A
1N/A a = b = c = NULL;
1N/A data_entry = -1;
1N/A tmp_create = tmp_free = 0;
1N/A
1N/A /*
1N/A * We don't return a name when creating temporary files, just an fd.
1N/A * Default to error now.
1N/A */
1N/A if (fdp != NULL)
1N/A *fdp = -1;
1N/A if (namep != NULL)
1N/A *namep = NULL;
1N/A
1N/A /*
1N/A * Absolute path names are never modified. If the file is an absolute
1N/A * path, we're done. If the directory is, simply append the file and
1N/A * return.
1N/A */
1N/A if (file != NULL && __os_abspath(file))
1N/A return (__os_strdup(file, namep));
1N/A if (dir != NULL && __os_abspath(dir)) {
1N/A a = dir;
1N/A goto done;
1N/A }
1N/A
1N/A /*
1N/A * DB_ENV DIR APPNAME RESULT
1N/A * -------------------------------------------
1N/A * null null none <tmp>/file
1N/A * null set none DIR/file
1N/A * set null none DB_HOME/file
1N/A * set set none DB_HOME/DIR/file
1N/A *
1N/A * DB_ENV FILE APPNAME RESULT
1N/A * -------------------------------------------
1N/A * null null DB_APP_DATA <tmp>/<create>
1N/A * null set DB_APP_DATA ./file
1N/A * set null DB_APP_DATA <tmp>/<create>
1N/A * set set DB_APP_DATA DB_HOME/DB_DATA_DIR/file
1N/A *
1N/A * DB_ENV DIR APPNAME RESULT
1N/A * -------------------------------------------
1N/A * null null DB_APP_LOG <tmp>/file
1N/A * null set DB_APP_LOG DIR/file
1N/A * set null DB_APP_LOG DB_HOME/DB_LOG_DIR/file
1N/A * set set DB_APP_LOG DB_HOME/DB_LOG_DIR/DIR/file
1N/A *
1N/A * DB_ENV APPNAME RESULT
1N/A * -------------------------------------------
1N/A * null DB_APP_TMP* <tmp>/<create>
1N/A * set DB_APP_TMP* DB_HOME/DB_TMP_DIR/<create>
1N/A */
1N/Aretry: switch (appname) {
1N/A case DB_APP_NONE:
1N/A if (dbenv == NULL || !F_ISSET(dbenv, DB_ENV_APPINIT)) {
1N/A if (dir == NULL)
1N/A goto tmp;
1N/A a = dir;
1N/A } else {
1N/A a = dbenv->db_home;
1N/A b = dir;
1N/A }
1N/A break;
1N/A case DB_APP_DATA:
1N/A if (dir != NULL) {
1N/A __db_err(dbenv,
1N/A "DB_APP_DATA: illegal directory specification");
1N/A return (EINVAL);
1N/A }
1N/A
1N/A if (file == NULL) {
1N/A tmp_create = 1;
1N/A goto tmp;
1N/A }
1N/A if (dbenv == NULL || !F_ISSET(dbenv, DB_ENV_APPINIT))
1N/A a = PATH_DOT;
1N/A else {
1N/A a = dbenv->db_home;
1N/A if (dbenv->db_data_dir != NULL &&
1N/A (b = dbenv->db_data_dir[++data_entry]) == NULL) {
1N/A data_entry = -1;
1N/A b = dbenv->db_data_dir[0];
1N/A }
1N/A }
1N/A break;
1N/A case DB_APP_LOG:
1N/A if (dbenv == NULL || !F_ISSET(dbenv, DB_ENV_APPINIT)) {
1N/A if (dir == NULL)
1N/A goto tmp;
1N/A a = dir;
1N/A } else {
1N/A a = dbenv->db_home;
1N/A b = dbenv->db_log_dir;
1N/A c = dir;
1N/A }
1N/A break;
1N/A case DB_APP_TMP:
1N/A if (dir != NULL || file != NULL) {
1N/A __db_err(dbenv,
1N/A "DB_APP_TMP: illegal directory or file specification");
1N/A return (EINVAL);
1N/A }
1N/A
1N/A tmp_create = 1;
1N/A if (dbenv == NULL || !F_ISSET(dbenv, DB_ENV_APPINIT))
1N/A goto tmp;
1N/A else {
1N/A a = dbenv->db_home;
1N/A b = dbenv->db_tmp_dir;
1N/A }
1N/A break;
1N/A }
1N/A
1N/A /* Reference a file from the appropriate temporary directory. */
1N/A if (0) {
1N/Atmp: if (dbenv == NULL || !F_ISSET(dbenv, DB_ENV_APPINIT)) {
1N/A memset(&etmp, 0, sizeof(etmp));
1N/A if ((ret = __os_tmpdir(&etmp, DB_USE_ENVIRON)) != 0)
1N/A return (ret);
1N/A tmp_free = 1;
1N/A a = etmp.db_tmp_dir;
1N/A } else
1N/A a = dbenv->db_tmp_dir;
1N/A }
1N/A
1N/Adone: len =
1N/A (a == NULL ? 0 : strlen(a) + 1) +
1N/A (b == NULL ? 0 : strlen(b) + 1) +
1N/A (c == NULL ? 0 : strlen(c) + 1) +
1N/A (file == NULL ? 0 : strlen(file) + 1);
1N/A
1N/A /*
1N/A * Allocate space to hold the current path information, as well as any
1N/A * temporary space that we're going to need to create a temporary file
1N/A * name.
1N/A */
1N/A#define DB_TRAIL "XXXXXX"
1N/A if ((ret =
1N/A __os_malloc(len + sizeof(DB_TRAIL) + 10, NULL, &start)) != 0) {
1N/A if (tmp_free)
1N/A __os_freestr(etmp.db_tmp_dir);
1N/A return (ret);
1N/A }
1N/A
1N/A slash = 0;
1N/A p = start;
1N/A DB_ADDSTR(a);
1N/A DB_ADDSTR(b);
1N/A DB_ADDSTR(file);
1N/A *p = '\0';
1N/A
1N/A /* Discard any space allocated to find the temp directory. */
1N/A if (tmp_free) {
1N/A __os_freestr(etmp.db_tmp_dir);
1N/A tmp_free = 0;
1N/A }
1N/A
1N/A /*
1N/A * If we're opening a data file, see if it exists. If it does,
1N/A * return it, otherwise, try and find another one to open.
1N/A */
1N/A if (data_entry != -1 && __os_exists(start, NULL) != 0) {
1N/A __os_freestr(start);
1N/A a = b = c = NULL;
1N/A goto retry;
1N/A }
1N/A
1N/A /* Create the file if so requested. */
1N/A if (tmp_create &&
1N/A (ret = __db_tmp_open(dbenv, tmp_oflags, start, fdp)) != 0) {
1N/A __os_freestr(start);
1N/A return (ret);
1N/A }
1N/A
1N/A if (namep == NULL)
1N/A __os_freestr(start);
1N/A else
1N/A *namep = start;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __db_home --
1N/A * Find the database home.
1N/A */
1N/Astatic int
1N/A__db_home(dbenv, db_home, flags)
1N/A DB_ENV *dbenv;
1N/A const char *db_home;
1N/A u_int32_t flags;
1N/A{
1N/A const char *p;
1N/A
1N/A p = db_home;
1N/A
1N/A /* Use the environment if it's permitted and initialized. */
1N/A#ifdef HAVE_GETUID
1N/A if (LF_ISSET(DB_USE_ENVIRON) ||
1N/A (LF_ISSET(DB_USE_ENVIRON_ROOT) && getuid() == 0)) {
1N/A#else
1N/A if (LF_ISSET(DB_USE_ENVIRON)) {
1N/A#endif
1N/A if ((p = getenv("DB_HOME")) == NULL)
1N/A p = db_home;
1N/A else if (p[0] == '\0') {
1N/A __db_err(dbenv,
1N/A "illegal DB_HOME environment variable");
1N/A return (EINVAL);
1N/A }
1N/A }
1N/A
1N/A if (p == NULL)
1N/A return (0);
1N/A
1N/A return (__os_strdup(p, &dbenv->db_home));
1N/A}
1N/A
1N/A/*
1N/A * __db_parse --
1N/A * Parse a single NAME VALUE pair.
1N/A */
1N/Astatic int
1N/A__db_parse(dbenv, s)
1N/A DB_ENV *dbenv;
1N/A char *s;
1N/A{
1N/A int ret;
1N/A char *local_s, *name, *value, **p, *tp;
1N/A
1N/A /*
1N/A * We need to strdup the argument in case the caller passed us
1N/A * static data.
1N/A */
1N/A if ((ret = __os_strdup(s, &local_s)) != 0)
1N/A return (ret);
1N/A
1N/A /*
1N/A * Name/value pairs are parsed as two white-space separated strings.
1N/A * Leading and trailing white-space is trimmed from the value, but
1N/A * it may contain embedded white-space. Note: we use the isspace(3)
1N/A * macro because it's more portable, but that means that you can use
1N/A * characters like form-feed to separate the strings.
1N/A */
1N/A name = local_s;
1N/A for (tp = name; *tp != '\0' && !isspace(*tp); ++tp)
1N/A ;
1N/A if (*tp == '\0' || tp == name)
1N/A goto illegal;
1N/A *tp = '\0';
1N/A for (++tp; isspace(*tp); ++tp)
1N/A ;
1N/A if (*tp == '\0')
1N/A goto illegal;
1N/A value = tp;
1N/A for (++tp; *tp != '\0'; ++tp)
1N/A ;
1N/A for (--tp; isspace(*tp); --tp)
1N/A ;
1N/A if (tp == value) {
1N/Aillegal: ret = EINVAL;
1N/A __db_err(dbenv, "illegal name-value pair: %s", s);
1N/A goto err;
1N/A }
1N/A *++tp = '\0';
1N/A
1N/A#define DATA_INIT_CNT 20 /* Start with 20 data slots. */
1N/A if (!strcmp(name, "DB_DATA_DIR")) {
1N/A if (dbenv->db_data_dir == NULL) {
1N/A if ((ret = __os_calloc(DATA_INIT_CNT,
1N/A sizeof(char **), &dbenv->db_data_dir)) != 0)
1N/A goto err;
1N/A dbenv->data_cnt = DATA_INIT_CNT;
1N/A } else if (dbenv->data_next == dbenv->data_cnt - 1) {
1N/A dbenv->data_cnt *= 2;
1N/A if ((ret = __os_realloc(&dbenv->db_data_dir,
1N/A dbenv->data_cnt * sizeof(char **))) != 0)
1N/A goto err;
1N/A }
1N/A p = &dbenv->db_data_dir[dbenv->data_next++];
1N/A } else if (!strcmp(name, "DB_LOG_DIR")) {
1N/A if (dbenv->db_log_dir != NULL)
1N/A __os_freestr(dbenv->db_log_dir);
1N/A p = &dbenv->db_log_dir;
1N/A } else if (!strcmp(name, "DB_TMP_DIR")) {
1N/A if (dbenv->db_tmp_dir != NULL)
1N/A __os_freestr(dbenv->db_tmp_dir);
1N/A p = &dbenv->db_tmp_dir;
1N/A } else
1N/A goto err;
1N/A
1N/A ret = __os_strdup(value, p);
1N/A
1N/Aerr: __os_freestr(local_s);
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * __db_tmp_open --
1N/A * Create a temporary file.
1N/A */
1N/Astatic int
1N/A__db_tmp_open(dbenv, flags, path, fdp)
1N/A DB_ENV *dbenv;
1N/A u_int32_t flags;
1N/A char *path;
1N/A int *fdp;
1N/A{
1N/A u_long pid;
1N/A int mode, isdir, ret;
1N/A const char *p;
1N/A char *trv;
1N/A
1N/A /*
1N/A * Check the target directory; if you have six X's and it doesn't
1N/A * exist, this runs for a *very* long time.
1N/A */
1N/A if ((ret = __os_exists(path, &isdir)) != 0) {
1N/A __db_err(dbenv, "%s: %s", path, strerror(ret));
1N/A return (ret);
1N/A }
1N/A if (!isdir) {
1N/A __db_err(dbenv, "%s: %s", path, strerror(EINVAL));
1N/A return (EINVAL);
1N/A }
1N/A
1N/A /* Build the path. */
1N/A for (trv = path; *trv != '\0'; ++trv)
1N/A ;
1N/A *trv = PATH_SEPARATOR[0];
1N/A for (p = DB_TRAIL; (*++trv = *p) != '\0'; ++p)
1N/A ;
1N/A
1N/A /*
1N/A * Replace the X's with the process ID. Pid should be a pid_t,
1N/A * but we use unsigned long for portability.
1N/A */
1N/A for (pid = getpid(); *--trv == 'X'; pid /= 10)
1N/A switch (pid % 10) {
1N/A case 0: *trv = '0'; break;
1N/A case 1: *trv = '1'; break;
1N/A case 2: *trv = '2'; break;
1N/A case 3: *trv = '3'; break;
1N/A case 4: *trv = '4'; break;
1N/A case 5: *trv = '5'; break;
1N/A case 6: *trv = '6'; break;
1N/A case 7: *trv = '7'; break;
1N/A case 8: *trv = '8'; break;
1N/A case 9: *trv = '9'; break;
1N/A }
1N/A ++trv;
1N/A
1N/A /* Set up open flags and mode. */
1N/A LF_SET(DB_CREATE | DB_EXCL);
1N/A mode = __db_omode("rw----");
1N/A
1N/A /* Loop, trying to open a file. */
1N/A for (;;) {
1N/A if ((ret = __db_open(path, flags, flags, mode, fdp)) == 0)
1N/A return (0);
1N/A
1N/A /*
1N/A * XXX:
1N/A * If we don't get an EEXIST error, then there's something
1N/A * seriously wrong. Unfortunately, if the implementation
1N/A * doesn't return EEXIST for O_CREAT and O_EXCL regardless
1N/A * of other possible errors, we've lost.
1N/A */
1N/A if (ret != EEXIST) {
1N/A __db_err(dbenv,
1N/A "tmp_open: %s: %s", path, strerror(ret));
1N/A return (ret);
1N/A }
1N/A
1N/A /*
1N/A * Tricky little algorithm for backward compatibility.
1N/A * Assumes the ASCII ordering of lower-case characters.
1N/A */
1N/A for (;;) {
1N/A if (*trv == '\0')
1N/A return (EINVAL);
1N/A if (*trv == 'z')
1N/A *trv++ = 'a';
1N/A else {
1N/A if (isdigit(*trv))
1N/A *trv = 'a';
1N/A else
1N/A ++*trv;
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A /* NOTREACHED */
1N/A}