Example #1
0
DB_ENV *ldbm_initialize_env(const char *home, int dbcachesize, int *envdirok)
{
	DB_ENV *env = NULL;    
	int     err;
	u_int32_t	envFlags;

	err = db_env_create( &env, 0 );

	if ( err ) {
#ifdef LDAP_SYSLOG
		syslog( LOG_INFO, "ldbm_initialize_env(): "
			"FATAL error in db_env_create() : %s (%d)\n",
			db_strerror( err ), err );
#endif
		return NULL;
	}

#if DB_VERSION_X >= 0x030300
	/* This interface appeared in 3.3 */
	env->set_alloc( env, ldbm_malloc, NULL, NULL );
#endif

	env->set_errcall( env, ldbm_db_errcall );
	env->set_errpfx( env, "==>" );
	if (dbcachesize) {
		env->set_cachesize( env, 0, dbcachesize, 0 );
	}

	envFlags = DB_CREATE | DB_INIT_MPOOL | DB_USE_ENVIRON;
#ifdef DB_PRIVATE
	envFlags |= DB_PRIVATE;
#endif
#ifdef DB_MPOOL_PRIVATE
	envFlags |= DB_MPOOL_PRIVATE;
#endif
#ifdef HAVE_BERKELEY_DB_THREAD
	envFlags |= DB_THREAD;
#endif

#if DB_VERSION_X >= 0x030100
	err = env->open( env, home, envFlags, 0 );
#else
	/* 3.0.x requires an extra argument */
	err = env->open( env, home, NULL, envFlags, 0 );
#endif

	if ( err != 0 ) {
#ifdef LDAP_SYSLOG
		syslog(	LOG_INFO, "ldbm_initialize_env(): "
			"FATAL error in dbEnv->open() : %s (%d)\n",
			db_strerror( err ), err );
#endif
		env->close( env, 0 );
		return NULL;
	}

	*envdirok = 1;
	return env;
}
Example #2
0
// Note: This actually behaves a bit like a static function,
// since DB_ENV.db_errcall has no information about which
// db_env triggered the call.  A user that has multiple DB_ENVs
// will simply not be able to have different streams for each one.
//
void DbEnv::set_error_stream(ostream *stream)
{
	DB_ENV *dbenv = unwrap(this);

	error_stream_ = stream;
	dbenv->set_errcall(dbenv, (stream == 0) ? 0 :
			   _stream_error_function_c);
}
Example #3
0
void DbEnv::set_errcall(void (*arg)(const DbEnv *, const char *, const char *))
{
	DB_ENV *dbenv = unwrap(this);

	error_callback_ = arg;
	error_stream_ = 0;

	dbenv->set_errcall(dbenv, (arg == 0) ? 0 :
			   _stream_error_function_c);
}
Example #4
0
//+   external create : unit -> t = "caml_dbenv_create"
value caml_dbenv_create(value unit){
  CAMLparam1(unit);
  CAMLlocal1(rval);
  int err;
  int flags = 0;
  DB_ENV *dbenv;
  
  err = db_env_create(&dbenv,flags);
  if (err != 0) { raise_db(db_strerror(err)); }

  dbenv->set_errcall(dbenv,raise_db_cb);

  rval = alloc_custom(&dbenv_custom,Camldbenv_wosize,0,1);
  UW_dbenv(rval) = dbenv;
  UW_dbenv_closed(rval) = False;
  CAMLreturn (rval);
}
Example #5
0
/*
 * Creates a database environment handle
 *
 * ARGUMENTS:
 *      path            Pathname of the database directory.  Shall not be NULL.
 *                      The client can free it upon return.
 *      dbEnv           Pointer to a pointer to the database environment.  Shall
 *                      not be NULL.  "*dbEnv" is set upon successful return.
 * RETURNS:
 *      0               Success.  "*dbEnv" is set.
 *      ENOMEM          System error.  "log_start()" called.
 *      EIO             Backend database error.  "log_start()" called.
 */
static RegStatus
createEnvHandle(
    const char* const   path,
    DB_ENV** const      dbEnv)
{
    RegStatus   status;
    DB_ENV*     env;

    assert(NULL != path);

    log_clear();

    if (status = db_env_create(&env, 0)) {
        log_serror("Couldn't create environment handle for database: %s",
                   db_strerror(status));
        status = ENOMEM;
    }
    else {
        env->set_errcall(env, logDbError);

        if (status = env->set_isalive(env, is_alive)) {
            log_add("Couldn't register \"is_alive()\" function for "
                    "database \"%s\"", path);
            status = EIO;
        }
        else {
            static const unsigned      threadCount = 256;

            if (status = env->set_thread_count(env, threadCount)) {
                log_add("Couldn't set thread count to %u for database \"%s\"",
                        threadCount, path);
                status = EIO;
            }
            else {
                *dbEnv = env;
            }
        }

        if (status)
            (void)env->close(env, 0);
    }                                   /* "env" allocated */

    return status;
}
Example #6
0
int
main(int argc, char **argv)
{
	// This program uses a named container, which will apear
	// on disk
	std::string containerName = "people.dbxml";
	std::string content = "<people><person><name>joe</name></person><person><name>mary</name></person></people>";
	std::string docName = "people";
	// Note that the query uses a variable, which must be set
	// in the query context
	std::string queryString =
		"collection('people.dbxml')/people/person[name=$name]";
	std::string environmentDir = ".";

	// Berkeley DB environment flags
	u_int32_t envFlags = DB_RECOVER|DB_CREATE|DB_INIT_MPOOL|
		DB_INIT_LOCK|DB_INIT_TXN|DB_INIT_LOG;
	// Berkeley DB cache size (25 MB).  The default is quite small
	u_int32_t envCacheSize = 25*1024*1024;

	// argument parsing should really use getopt(), but
	// avoid it for platform-independence
	if (argc == 3) {
		if (std::string(argv[1]) != std::string("-h"))
			usage(argv[0]);
		environmentDir = argv[2];
	} else if (argc != 1)
		usage(argv[0]);

	// Create and open a Berkeley DB Transactional Environment.
	int dberr;
	DB_ENV *dbEnv = 0;
	dberr = db_env_create(&dbEnv, 0);
	if (dberr == 0) {
		dbEnv->set_cachesize(dbEnv, 0, envCacheSize, 1);
		dbEnv->set_errcall(dbEnv, errcall); // set error callback
		dbEnv->set_lk_detect(dbEnv, DB_LOCK_DEFAULT); // handle deadlocks
		dberr = dbEnv->open(dbEnv, environmentDir.c_str(), envFlags, 0);
	}
	if (dberr) {
		std::cout << "Unable to create environment handle due to the following error: " <<
			db_strerror(dberr) << std::endl;
		if (dbEnv) dbEnv->close(dbEnv, 0);
		return -1;
	}

	try {
		// All BDB XML programs require an XmlManager instance.
		// Create it from the DbEnv
		XmlManager mgr(dbEnv, DBXML_ADOPT_DBENV);

		// Because the container will exist on disk, remove it
		// first if it exists
		if (mgr.existsContainer(containerName))
			mgr.removeContainer(containerName);

		/* Create a container that is transactional.  The container
		* type is NodeContainer, which is the default container type,
		* and the index is on nodes, which is the default for a
		* NodeContainer container.  XmlContainerConfig can be used
		* to set the container type and index type.
		*/
		XmlContainerConfig config;
		config.setTransactional(true);
		XmlContainer cont = mgr.createContainer(
			containerName,
			config);

		// All Container modification operations need XmlUpdateContext
		XmlUpdateContext uc = mgr.createUpdateContext();

		// The following putDocument call will auto-transact
		// and will abort/cleanup if the operation deadlocks or
		// otherwise fails
		cont.putDocument(docName, content, uc);

		// Querying requires an XmlQueryContext
		XmlQueryContext qc = mgr.createQueryContext();

		// Add a variable to the query context, used by the query
		qc.setVariableValue("name", "mary");

		// Use try/catch and while to handle deadlock/retry
		int retry = 0;
		while (retry < 5) { // hard-code 5 retries
			// Create a new transaction for the query
			XmlTransaction txn = mgr.createTransaction();

			try {
				// o Note the passing of txn to both methods
				// o Often the XmlQueryExpression object will be created and
				// saved for reuse in order to amortize the cost of parsing a query
				XmlQueryExpression expr = mgr.prepare(txn, queryString, qc);
				XmlResults res = expr.execute(txn, qc);

				// Note use of XmlQueryExpression::getQuery() and
				// XmlResults::size()
				std::cout << "The query, '" << expr.getQuery() << "' returned " <<
					(unsigned int)res.size() << " result(s)" << std::endl;

				// Process results -- just print them
				XmlValue value;
				std::cout << "Result: " << std::endl;
				while (res.next(value)) {
					std::cout << "\t" << value.asString() << std::endl;
				}

				// done with the transaction
				txn.commit();
				break;
			} catch (XmlException &x) {
				txn.abort(); // unconditional
				if ((x.getExceptionCode() == XmlException::DATABASE_ERROR) &&
					(x.getDbErrno() == DB_LOCK_DEADLOCK)) {
						++retry;
						continue; // try again
				}
				throw; // re-throw -- not deadlock
			}
		}
		// In C++, resources are released as objects go out
		// of scope.
	} catch (XmlException &xe) {
		std::cout << "XmlException: " << xe.what() << std::endl;
	}

	return 0;
}
Example #7
0
File: db3.c Project: crossbuild/rpm
static int db_init(rpmdb rdb, const char * dbhome)
{
    DB_ENV *dbenv = NULL;
    int rc, xx;
    int retry_open = 2;
    int lockfd = -1;
    struct dbConfig_s * cfg = &rdb->cfg;
    /* This is our setup, thou shall not have other setups before us */
    uint32_t eflags = (DB_CREATE|DB_INIT_MPOOL|DB_INIT_CDB);

    if (rdb->db_dbenv != NULL) {
	rdb->db_opens++;
	return 0;
    } else {
	/* On first call, set backend description to something... */
	free(rdb->db_descr);
	rasprintf(&rdb->db_descr, "db%u", DB_VERSION_MAJOR);
    }

    /*
     * Both verify and rebuild are rather special, if for different reasons:
     * On rebuild we dont want to be affected by eg paniced environment, and
     * CDB only slows things down there. Verify is a quirky beast unlike
     * anything else in BDB, and does not like shared env or CDB.
     */
    if (rdb->db_flags & (RPMDB_FLAG_VERIFYONLY|RPMDB_FLAG_REBUILD)) {
	eflags |= DB_PRIVATE;
	eflags &= ~DB_INIT_CDB;
    }

    rc = db_env_create(&dbenv, 0);
    rc = dbapi_err(rdb, "db_env_create", rc, _debug);
    if (dbenv == NULL || rc)
	goto errxit;

    dbenv->set_alloc(dbenv, rmalloc, rrealloc, NULL);
    dbenv->set_errcall(dbenv, NULL);
    dbenv->set_errpfx(dbenv, _errpfx);
    dbenv->set_msgcall(dbenv, warnlog);

    /* 
     * These enable automatic stale lock removal. 
     * thread_count 8 is some kind of "magic minimum" value...
     */
    dbenv->set_thread_count(dbenv, 8);
    dbenv->set_isalive(dbenv, isalive);

    dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
	    (cfg->db_verbose & DB_VERB_DEADLOCK));
    dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
	    (cfg->db_verbose & DB_VERB_RECOVERY));
    dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
	    (cfg->db_verbose & DB_VERB_WAITSFOR));

    if (cfg->db_mmapsize) {
	xx = dbenv->set_mp_mmapsize(dbenv, cfg->db_mmapsize);
	xx = dbapi_err(rdb, "dbenv->set_mp_mmapsize", xx, _debug);
    }

    if (cfg->db_cachesize) {
	xx = dbenv->set_cachesize(dbenv, 0, cfg->db_cachesize, 0);
	xx = dbapi_err(rdb, "dbenv->set_cachesize", xx, _debug);
    }

    /*
     * Serialize shared environment open (and clock) via fcntl() lock.
     * Otherwise we can end up calling dbenv->failchk() while another
     * process is joining the environment, leading to transient
     * DB_RUNRECOVER errors. Also prevents races wrt removing the
     * environment (eg chrooted operation). Silently fall back to
     * private environment on failure to allow non-privileged queries
     * to "work", broken as it might be.
     */
    if (!(eflags & DB_PRIVATE)) {
	lockfd = serialize_env(dbhome);
	if (lockfd < 0) {
	    eflags |= DB_PRIVATE;
	    retry_open--;
	    rpmlog(RPMLOG_DEBUG, "serialize failed, using private dbenv\n");
	}
    }

    /*
     * Actually open the environment. Fall back to private environment
     * if we dont have permission to join/create shared environment or
     * system doesn't support it..
     */
    while (retry_open) {
	char *fstr = prDbiOpenFlags(eflags, 1);
	rpmlog(RPMLOG_DEBUG, "opening  db environment %s %s\n", dbhome, fstr);
	free(fstr);

	rc = (dbenv->open)(dbenv, dbhome, eflags, rdb->db_perms);
	if ((rc == EACCES || rc == EROFS) || (rc == EINVAL && errno == rc)) {
	    eflags |= DB_PRIVATE;
	    retry_open--;
	} else {
	    retry_open = 0;
	}
    }
    rc = dbapi_err(rdb, "dbenv->open", rc, _debug);
    if (rc)
	goto errxit;

    dbenv->set_errcall(dbenv, errlog);

    /* stale lock removal */
    rc = dbenv->failchk(dbenv, 0);
    rc = dbapi_err(rdb, "dbenv->failchk", rc, _debug);
    if (rc)
	goto errxit;

    rdb->db_dbenv = dbenv;
    rdb->db_opens = 1;

    if (lockfd >= 0)
	close(lockfd);
    return 0;

errxit:
    if (dbenv) {
	int xx;
	xx = dbenv->close(dbenv, 0);
	xx = dbapi_err(rdb, "dbenv->close", xx, _debug);
    }
    if (lockfd >= 0)
	close(lockfd);
    return rc;
}
Example #8
0
File: db3.c Project: xrg/RPM
static int db3close(dbiIndex dbi, unsigned int flags)
{
    rpmdb rpmdb = dbi->dbi_rpmdb;
    const char * root;
    const char * home;
    char * dbhome;
    const char * dbfile;
    const char * dbsubfile;
    DB * db = dbi->dbi_db;
    int _printit;
    int rc = 0, xx;

    flags = 0;	/* XXX unused */

    /*
     * Get the prefix/root component and directory path.
     */
    root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
    if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
	root = NULL;
    home = (dbi->dbi_home ? dbi->dbi_home : rpmdb->db_home);

    dbhome = rpmGenPath(root, home, NULL);
    if (dbi->dbi_temporary) {
	dbfile = NULL;
	dbsubfile = NULL;
    } else {
#ifdef	HACK	/* XXX necessary to support dbsubfile */
	dbfile = (dbi->dbi_file ? dbi->dbi_file : db3basename);
	dbsubfile = (dbi->dbi_subfile ? dbi->dbi_subfile : rpmTagGetName(dbi->dbi_rpmtag));
#else
	dbfile = (dbi->dbi_file ? dbi->dbi_file : rpmTagGetName(dbi->dbi_rpmtag));
	dbsubfile = NULL;
#endif
    }

    if (db) {
	rc = db->close(db, 0);
	/* XXX ignore not found error messages. */
	_printit = (rc == ENOENT ? 0 : _debug);
	rc = cvtdberr(dbi, "db->close", rc, _printit);
	db = dbi->dbi_db = NULL;

	rpmlog(RPMLOG_DEBUG, "closed   db index       %s/%s\n",
		dbhome, (dbfile ? dbfile : rpmTagGetName(dbi->dbi_rpmtag)));

    }

    if (rpmdb->db_dbenv != NULL && dbi->dbi_use_dbenv) {
	if (rpmdb->db_opens == 1) {
	    xx = db_fini(dbi, (dbhome ? dbhome : ""), dbfile, dbsubfile);
	    rpmdb->db_dbenv = NULL;
	}
	rpmdb->db_opens--;
    }

    if (dbi->dbi_verify_on_close && !dbi->dbi_temporary) {
	DB_ENV * dbenv = NULL;

	rc = db_env_create(&dbenv, 0);
	rc = cvtdberr(dbi, "db_env_create", rc, _debug);
	if (rc || dbenv == NULL) goto exit;

	dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall);
	dbenv->set_errfile(dbenv, rpmdb->db_errfile);
	dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);
 /*	dbenv->set_paniccall(???) */
#if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
	xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
		(dbi->dbi_verbose & DB_VERB_CHKPOINT));
#endif
	xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
		(dbi->dbi_verbose & DB_VERB_DEADLOCK));
	xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
		(dbi->dbi_verbose & DB_VERB_RECOVERY));
	xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
		(dbi->dbi_verbose & DB_VERB_WAITSFOR));

	if (dbi->dbi_tmpdir) {
	    char * tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
	    rc = dbenv->set_tmp_dir(dbenv, tmpdir);
	    rc = cvtdberr(dbi, "dbenv->set_tmp_dir", rc, _debug);
	    tmpdir = _free(tmpdir);
	    if (rc) goto exit;
	}
	    
	rc = (dbenv->open)(dbenv, dbhome,
            DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_USE_ENVIRON, 0);
	rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
	if (rc) goto exit;

	rc = db_create(&db, dbenv, 0);
	rc = cvtdberr(dbi, "db_create", rc, _debug);

	if (db != NULL) {
		char * dbf = rpmGetPath(dbhome, "/", dbfile, NULL);

		rc = db->verify(db, dbf, NULL, NULL, flags);
		rc = cvtdberr(dbi, "db->verify", rc, _debug);

		rpmlog(RPMLOG_DEBUG, "verified db index       %s/%s\n",
			(dbhome ? dbhome : ""),
			(dbfile ? dbfile : rpmTagGetName(dbi->dbi_rpmtag)));

	        /*
		 * The DB handle may not be accessed again after
		 * DB->verify is called, regardless of its return.
		 */
		db = NULL;
		dbf = _free(dbf);
	}
	xx = dbenv->close(dbenv, 0);
	xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
	if (rc == 0 && xx) rc = xx;
    }

exit:
    dbi->dbi_db = NULL;

    free(dbhome);

    dbi = db3Free(dbi);

    return rc;
}
Example #9
0
File: db3.c Project: xrg/RPM
static int db_init(dbiIndex dbi, const char * dbhome,
		const char * dbfile,
		const char * dbsubfile,
		DB_ENV ** dbenvp)
{
    rpmdb rpmdb = dbi->dbi_rpmdb;
    DB_ENV *dbenv = NULL;
    int eflags;
    int rc;

    if (dbenvp == NULL)
	return 1;

    /* XXX HACK */
    if (rpmdb->db_errfile == NULL)
	rpmdb->db_errfile = stderr;

    eflags = (dbi->dbi_oeflags | dbi->dbi_eflags);
    if (eflags & DB_JOINENV) eflags &= DB_JOINENV;

    if (dbfile) {
	char *dbiflags = prDbiOpenFlags(eflags, 1);
	rpmlog(RPMLOG_DEBUG, "opening  db environment %s/%s %s\n",
		dbhome, dbfile, dbiflags);
	free(dbiflags);
    }

    /* XXX Can't do RPC w/o host. */
    if (dbi->dbi_host == NULL)
	dbi->dbi_ecflags &= ~DB_CLIENT;

    /* XXX Set a default shm_key. */
    if ((dbi->dbi_eflags & DB_SYSTEM_MEM) && dbi->dbi_shmkey == 0) {
#if defined(HAVE_FTOK)
	dbi->dbi_shmkey = ftok(dbhome, 0);
#else
	dbi->dbi_shmkey = 0x44631380;
#endif
    }

    rc = db_env_create(&dbenv, dbi->dbi_ecflags);
    rc = cvtdberr(dbi, "db_env_create", rc, _debug);
    if (dbenv == NULL || rc)
	goto errxit;

  { int xx;

 /* 4.1: dbenv->set_app_dispatch(???) */
 /* 4.1: dbenv->set_alloc(???) */
 /* 4.1: dbenv->set_data_dir(???) */
 /* 4.1: dbenv->set_encrypt(???) */

    dbenv->set_errcall(dbenv, (void *) rpmdb->db_errcall);
    dbenv->set_errfile(dbenv, rpmdb->db_errfile);
    dbenv->set_errpfx(dbenv, rpmdb->db_errpfx);

 /* 4.1: dbenv->set_feedback(???) */
 /* 4.1: dbenv->set_flags(???) */

 /* dbenv->set_paniccall(???) */

#if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5)
    /* 
     * These enable automatic stale lock removal. 
     * thread_count 8 is some kind of "magic minimum" value...
     */
    dbenv->set_thread_count(dbenv, 8);
    dbenv->set_isalive(dbenv, db3isalive);
#endif

    if ((dbi->dbi_ecflags & DB_CLIENT) && dbi->dbi_host) {
	const char * home;
	int retry = 0;

	if ((home = strrchr(dbhome, '/')) != NULL)
	    dbhome = ++home;

	while (retry++ < 5) {
	    xx = dbenv->set_rpc_server(dbenv, NULL, dbi->dbi_host,
		dbi->dbi_cl_timeout, dbi->dbi_sv_timeout, 0);
	    xx = cvtdberr(dbi, "dbenv->set_server", xx, _debug);
	    if (!xx)
		break;
	    (void) sleep(15);
	}
    } else {
#if !(DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
	xx = dbenv->set_verbose(dbenv, DB_VERB_CHKPOINT,
		(dbi->dbi_verbose & DB_VERB_CHKPOINT));
#endif
	xx = dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK,
		(dbi->dbi_verbose & DB_VERB_DEADLOCK));
	xx = dbenv->set_verbose(dbenv, DB_VERB_RECOVERY,
		(dbi->dbi_verbose & DB_VERB_RECOVERY));
	xx = dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR,
		(dbi->dbi_verbose & DB_VERB_WAITSFOR));

	if (dbi->dbi_mmapsize) {
	    xx = dbenv->set_mp_mmapsize(dbenv, dbi->dbi_mmapsize);
	    xx = cvtdberr(dbi, "dbenv->set_mp_mmapsize", xx, _debug);
	}
	if (dbi->dbi_tmpdir) {
	    const char * root;
	    char * tmpdir;

	    root = (dbi->dbi_root ? dbi->dbi_root : rpmdb->db_root);
	    if ((root[0] == '/' && root[1] == '\0') || rpmdb->db_chrootDone)
		root = NULL;
	    tmpdir = rpmGenPath(root, dbi->dbi_tmpdir, NULL);
	    xx = dbenv->set_tmp_dir(dbenv, tmpdir);
	    xx = cvtdberr(dbi, "dbenv->set_tmp_dir", xx, _debug);
	    tmpdir = _free(tmpdir);
	}
    }

 /* dbenv->set_lk_conflicts(???) */
 /* dbenv->set_lk_detect(???) */
 /* 4.1: dbenv->set_lk_max_lockers(???) */
 /* 4.1: dbenv->set_lk_max_locks(???) */
 /* 4.1: dbenv->set_lk_max_objects(???) */

 /* 4.1: dbenv->set_lg_bsize(???) */
 /* 4.1: dbenv->set_lg_dir(???) */
 /* 4.1: dbenv->set_lg_max(???) */
 /* 4.1: dbenv->set_lg_regionmax(???) */

    if (dbi->dbi_cachesize) {
	xx = dbenv->set_cachesize(dbenv, 0, dbi->dbi_cachesize, 0);
	xx = cvtdberr(dbi, "dbenv->set_cachesize", xx, _debug);
    }

 /* 4.1 dbenv->set_timeout(???) */
 /* dbenv->set_tx_max(???) */
 /* 4.1: dbenv->set_tx_timestamp(???) */
 /* dbenv->set_tx_recover(???) */

 /* dbenv->set_rep_transport(???) */
 /* dbenv->set_rep_limit(???) */

    if (dbi->dbi_no_fsync) {
	xx = db_env_set_func_fsync(db3_fsync_disable);
	xx = cvtdberr(dbi, "db_env_set_func_fsync", xx, _debug);
    }

    if (dbi->dbi_shmkey) {
	xx = dbenv->set_shm_key(dbenv, dbi->dbi_shmkey);
	xx = cvtdberr(dbi, "dbenv->set_shm_key", xx, _debug);
    }
  }

    rc = (dbenv->open)(dbenv, dbhome, eflags, dbi->dbi_perms);
    rc = cvtdberr(dbi, "dbenv->open", rc, _debug);
    if (rc)
	goto errxit;

#if (DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 5)
    /* stale lock removal */
    rc = dbenv->failchk(dbenv, 0);
    rc = cvtdberr(dbi, "dbenv->failchk", rc, _debug);
    if (rc)
	goto errxit;
#endif

    *dbenvp = dbenv;

    return 0;

errxit:
    if (dbenv) {
	int xx;
	xx = dbenv->close(dbenv, 0);
	xx = cvtdberr(dbi, "dbenv->close", xx, _debug);
    }
    return rc;
}
Example #10
0
/*
 *  This should be done before the program is open for business.
 *  As such, it does not need to be reentrant.
 */
int
open_db_env(char *topdir)
{
	DB_ENV		*tmpenv = NULL;
	int		st;
	struct stat64	sbuf;
	char		logdir[MAXPATHLEN+1];
	char		tmpdir[MAXPATHLEN+1];
	char		*dirarr[3];
	int		i;

	if (topdir == NULL) {
		return (-1);
	}

	snprintf(logdir, sizeof (tmpdir), "%s/.logs", topdir);
	snprintf(tmpdir, sizeof (tmpdir), "%s/.tmp", topdir);

	dirarr[0] = topdir;
	dirarr[1] = logdir;
	dirarr[2] = tmpdir;

	/* first, set up the environment */
	st = db_env_create(&tmpenv, 0);

	if (st != 0) {
		return (st);
	}

	/* make sure the directories exist */
	for (i = 0; i < 3; i++) {
		st = stat64(dirarr[i], &sbuf);
		if ((st != 0) && (errno == ENOENT)) {
			st = mkdirp(dirarr[i], 0744);
			if (st == 0) {
				st = stat64(dirarr[i], &sbuf);
			}
		}
		if ((st == 0) && (!S_ISDIR(sbuf.st_mode))) {
			st = -1;
			break;
		}
	}

	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_data_dir(tmpenv, topdir);
	if (st != 0) {
		return (st);
	}
	st = tmpenv->set_lg_dir(tmpenv, logdir);
	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_tmp_dir(tmpenv, tmpdir);
	if (st != 0) {
		return (st);
	}

	st = tmpenv->set_flags(tmpenv, env_fl, 1);
	if (st != 0) {
		return (st);
	}

	/* overall database cache size */
	st = tmpenv->set_cachesize(tmpenv, 0, (60 * MEGA), 1);

	st = tmpenv->set_shm_key(tmpenv, FSM_SHM_MASTER_KEY);
	if (st != 0) {
		return (st);
	}

	/* log buffer in memory */
	st = tmpenv->set_lg_bsize(tmpenv, (30 * MEGA));
	if (st != 0) {
		return (st);
	}

	/* set up additional error logging */
	tmpenv->set_errcall(tmpenv, fsmdb_log_err);

	/* Increase the number of locks available */
	tmpenv->set_lk_max_locks(tmpenv, 10000);
	tmpenv->set_lk_max_lockers(tmpenv, 10000);
	tmpenv->set_lk_max_objects(tmpenv, 10000);

	/* Increase the number of concurrent transactions available */
	/* Note:  Default in 4.4-20 is '20'.  In later versions it's 100 */
	tmpenv->set_tx_max(tmpenv, 100);

	st = tmpenv->open(tmpenv, topdir, env_ofl, 0644);
	if (st != 0) {
		/* check for a major failure */
		if (st == DB_RUNRECOVERY) {
			st = tmpenv->open(tmpenv, topdir, env_ffl, 0644);
		}
		/* log catastrophic failure and remove all db files. */
		if (st == DB_RUNRECOVERY) {
			fsmdb_log_err(dbEnv, NULL,
			    "Database files corrupt, cannot recover.  "
			    "Files will be removed.  Please re-index any "
			    "recovery points to regenerate the database.");
			fsmdb_remove_all(topdir);
		}
	}

	if (st != 0) {
		return (st);
	}

	/* clear out unneeded log files */
	tmpenv->log_archive(tmpenv, NULL, DB_ARCH_REMOVE);

	/* all set, ready to use */
	dbEnv = tmpenv;

	return (st);
}
Example #11
0
int main(int argc, char **argv)
{
    // Deal with command line arguments
    const char *path2DbEnv = 0;
    u_int32_t envFlags = (DB_CREATE|DB_PRIVATE|DB_INIT_MPOOL);
    u_int32_t txnEnvFlags =	(DB_INIT_TXN|DB_INIT_LOCK|DB_INIT_LOG);
    u_int32_t dbxmlFlags = DBXML_ALLOW_EXTERNAL_ACCESS;
    vector<string> scripts;
    int verbose = 0;
    bool transactionMode = false;
    bool dbPrivate = false;
    bool envCreate = false;
    const char *progName = argv[0];
    const char *password = 0;
    int cacheSize = 64;
    int ch;
    int ret = 0;

    while ((ch = getopt(argc, argv, "?h:hs:tvxVP:cz:")) != -1) {
        switch (ch) {
        case 'h': {
            path2DbEnv = optarg;
            break;
        }
        case 'z': {
            cacheSize = atoi(optarg);
            break;
        }
        case 'c': {
            envFlags &= ~DB_PRIVATE;
            envCreate = true;
            break;
        }
        case 'x': {
            dbxmlFlags &= ~DBXML_ALLOW_EXTERNAL_ACCESS;
            break;
        }
        case 't': {
            transactionMode = true;
            envFlags |= txnEnvFlags;
            break;
        }
        case 's': {
            scripts.push_back(optarg);
            break;
        }
        case 'v': {
            ++verbose;
            break;
        }
        case 'V': {
            printf("%s\n", DbXml::dbxml_version(NULL, NULL, NULL));
            printf("%s\n", db_version(NULL, NULL, NULL));
            exit(0);
        }
        case 'P': {
            password = optarg;
            break;
        }
        case '?':
        default: {
            usage(progName, 0);
            break;
        }
        }
    }

    // Turn on logging if extra verbose is specified
    if(verbose > 1) {
        setLogLevel(LEVEL_ALL, true);
        setLogCategory(CATEGORY_ALL, true);
        setLogCategory(CATEGORY_NODESTORE, verbose > 2);
        verboseErrors = true;
    }

    SigBlock sb; // block signals, resend at end of scope
    try {
        // Create a DB environment, and XmlManager
        DB_ENV *dbenv;
        int dberr = 0;
        dberr = db_env_create(&dbenv, 0);
        if (dberr) {
            cout << "Error creating environment: " << dberr << endl;
            exit(-1);
        }
        if (password)
            dbenv->set_encrypt(dbenv, password, DB_ENCRYPT_AES);
        dbenv->set_errcall(dbenv, errcall);
        dbenv->set_cachesize(dbenv, 0, cacheSize * 1024 * 1024, 1);
        dbenv->set_lk_max_lockers(dbenv, 10000);
        dbenv->set_lk_max_locks(dbenv, 10000);
        dbenv->set_lk_max_objects(dbenv, 10000);
        if (!dbPrivate) {
            dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT);
            if (verbose && !envCreate) {
                cout <<
                     "Attempting to join environment: "
                     << (path2DbEnv ? path2DbEnv : ".")
                     << endl;
            }
            dberr = dbenv->open(dbenv, path2DbEnv, DB_USE_ENVIRON, 0);
            if (dberr != 0) {
                if (dberr == DB_VERSION_MISMATCH) {
                    cerr << "Error opening environment "
                         << (path2DbEnv ? path2DbEnv : ".")
                         << ": " << "environment version mismatch" << endl;
                    exit(-1);
                }
                if (verbose) {
                    if(envCreate) {
                        cerr << "Creating environment: "
                             << (path2DbEnv ? path2DbEnv : ".")
                             << endl;
                    } else {
                        cerr << "Unable to join environment "
                             << (path2DbEnv ? path2DbEnv : ".")
                             << ", creating a DB_PRIVATE environment" << endl;
                    }
                }
                dberr = dbenv->open(dbenv, path2DbEnv,
                                    envFlags, 0);
            } else {
                cout <<	"Joined existing environment"
                     << endl;
                u_int32_t eflags = 0;
                dbenv->get_open_flags(dbenv, &eflags);
                if (eflags & DB_INIT_TXN)
                    transactionMode = true;
                else {
                    if (verbose && (transactionMode == true))
                        cout << "Joined a non-transactional environment, turning off transaction mode" << endl;
                    transactionMode = false;
                }
            }
        } else {
            dberr = dbenv->open(dbenv, path2DbEnv,
                                envFlags, 0);
        }
        if (dberr != 0) {
            cerr << "Error opening environment "
                 << (path2DbEnv ? path2DbEnv : ".")
                 << ", error is " << dberr << endl;
            exit(-1);
        }
        XmlManager db(dbenv, dbxmlFlags|DBXML_ADOPT_DBENV);

        // Create the environment
        Environment env(db, sb);
        env.transactions() = transactionMode;

        // Create the Shell object
        DefaultShell shell;

        // Run scripts, if specified
        if(!scripts.empty()) {
            env.interactive() = false;
            env.verbose() = (verbose != 0);

            for(vector<string>::iterator i = scripts.begin();
                    i != scripts.end() && !env.quit(); ++i) {
                ifstream scriptFile(i->c_str(), ios::in);
                if(!scriptFile) {
                    cerr << progName << ": cannot open script file: " << *i << endl;
                } else {
                    env.streamName() = *i;
                    env.lineNo() = 0;
                    shell.mainLoop(scriptFile, env);
                    scriptFile.close();
                }
            }
        }

        // Perform the queries
        if(!env.quit()) {
            env.interactive() = true;
            env.verbose() = true;
            env.streamName() = "stdin";
            env.lineNo() = 0;

            do {
                shell.mainLoop(cin, env);
                if(env.sigBlock().isInterrupted())
                    env.sigBlock().reset();
            } while(!env.quit() && !cin.eof());
        }
    }
    catch(exception &e) {
        cerr << progName << ": error at lowest level: " << e.what() << endl;
        ret = 1;
    }
    catch(...) {
        cerr << progName << ": error at lowest level: " << endl;
        ret = 1;
    }
    return ret;
}