void BDB_RecoverEnv(const string& path, bool fatal_recover) { DB_ENV *dbenv; int ret; if ((ret = db_env_create(&dbenv, 0)) != 0) { string msg = "Cannot create environment " + string(db_strerror(ret)); BDB_THROW(eInvalidOperation, msg); } dbenv->set_errfile(dbenv, stderr); //if (verbose) // (void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1); u_int32_t flags = 0; flags |= DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_USE_ENVIRON; flags |= fatal_recover ? DB_RECOVER_FATAL : DB_RECOVER; flags |= DB_PRIVATE; if ((ret = dbenv->open(dbenv, path.c_str(), flags, 0)) != 0) { dbenv->close(dbenv, 0); string msg = "Cannot open environment " + string(db_strerror(ret)); BDB_THROW(eInvalidOperation, msg); } ret = dbenv->close(dbenv, 0); }
int nbsp_open_dbenv(void){ DB_ENV *dbenv = NULL; int status = 0; uint32_t mb; uint32_t dbenv_flags = DBENV_FLAGS; mb = (1024 * 1024) * g.dbcache_mb; status = db_env_create(&dbenv, 0); if(status == 0) status = dbenv->set_cachesize(dbenv, 0, mb, 0); if(status == 0) status = dbenv->open(dbenv, g.dbhome, dbenv_flags, g.dbfile_mode); if(status != 0){ log_errx("Cannot initialize db environment. %s", db_strerror(status)); status = -1; if(dbenv != NULL) (void)dbenv->close(dbenv, 0); }else g.dbenv = dbenv; return(status); }
static void cleanup() { int rc; DB *db; DB_ENV *dbenv; rc = get_db(&db, 0); assert(! rc); rc = get_dbenv(&dbenv, 0); assert(! rc); if (dbkey.data) free(dbkey.data); if (db) call_db(db->close(db, 0), "DB close"); if (dbenv) { rc = call_db(db_create(&db, dbenv, 0), "db_create"); if (!rc) if (! db->remove(db, "tls_stats.db", 0, 0)) syslog(LOG_NOTICE, "Unused database tls_stats.db removed"); call_db(dbenv->txn_checkpoint(dbenv, 100 * 1024, 24 * 60, 0), "txn_checkpoint"); call_db(dbenv->log_archive(dbenv, NULL, DB_ARCH_REMOVE), "log_archive"); call_db(dbenv->close(dbenv, 0), "DB_ENV close"); } policy_cleanup(); }
int dblayer_copy_file_resetlsns(char *home_dir ,char *source_file_name, char *destination_file_name, int overwrite, dblayer_private *priv) { int retval = 0; DB_ENV *env = NULL; LDAPDebug( LDAP_DEBUG_TRACE, "=> dblayer_copy_file_resetlsns\n", 0, 0, 0 ); /* Make the environment */ retval = dblayer_make_private_simple_env(home_dir,&env); if (retval || !env) { LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Call to dblayer_make_private_simple_env failed!\n" "Unable to open an environment.", 0, 0, 0); goto out; } /* Do the copy */ retval = dblayer_copy_file_keybykey(env, source_file_name, destination_file_name, overwrite, priv); if (retval) { LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns: Copy not completed successfully.", 0, 0, 0); } out: /* Close the environment */ if (env) { int retval2 = 0; retval2 = env->close(env,0); if (retval2) { if (0 == retval) { retval = retval2; LDAPDebug(LDAP_DEBUG_ANY, "dblayer_copy_file_resetlsns, error %d: %s\n", retval, db_strerror(retval), 0); } } } LDAPDebug( LDAP_DEBUG_TRACE, "<= dblayer_copy_file_resetlsns\n", 0, 0, 0 ); return retval; }
/* * Removes the backend database. This function shall be called only when * nothing holds the database open. * * ARGUMENTS: * path Pathname of the database directory. Shall not be NULL. * The client can free it upon return. * RETURNS: * 0 Success. * ENOMEM System error. "log_start()" called. * EIO Backend database error. "log_start()" called. */ RegStatus beRemove( const char* const path) { RegStatus status; DB_ENV* env; assert(NULL != path); /* * First, remove the database. */ if (0 == (status = openEnvironment(path, &env))) { if (status = env->dbremove(env, NULL, DB_FILENAME, NULL, 0)) { log_add("Couldn't remove database file \"%s\" in \"%s\"", DB_FILENAME, path); status = EIO; } (void)env->close(env, 0); } /* "env" allocated */ /* * Then, remove the database environment. */ if (0 == status) status = removeEnvironment(path); return status; }
/* * Creates a database handle. * * ARGUMENTS: * path Pathname of the database directory. Shall not be NULL. * The client can free it upon return. * envHandle Pointer to a pointer to the database environment * handle. Shall not be NULL. "*envHandle" is set upon * successful return. * dbHandle Pointer to a pointer to the database handle. * "*dbHandle" is set upon successful return. * RETURNS: * 0 Success. "*envHandle" and "*dbHandle" are set. * ENOMEM System error. "log_start()" called. * EIO Backend database error. "log_start()" called. */ static RegStatus createDbHandle( const char* const path, DB_ENV** const envHandle, DB** const dbHandle) { RegStatus status; DB_ENV* env; assert(NULL != envHandle); assert(NULL != dbHandle); if (0 == (status = openEnvironment(path, &env))) { DB* db; if (status = db_create(&db, env, 0)) { log_add("Couldn't create database handle"); status = ENOMEM; } else { db->set_errcall(db, logDbError); *envHandle = env; *dbHandle = db; } if (status) (void)env->close(env, 0); } /* "env" allocated */ return status; }
int TestSetThreadCount(CuTest *ct) { /* SKIP */ /* Run this test only when hash is supported. */ #ifdef HAVE_HASH DB_ENV *dbenv; DB *db; CuAssert(ct, "db_env_create", db_env_create(&dbenv, 0) == 0); dbenv->set_errpfx(dbenv, "TestSetThreadCount"); CuAssert(ct, "set_thread_count", dbenv->set_thread_count(dbenv, 2) == 0); CuAssert(ct, "env->open", dbenv->open(dbenv, ".", DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_PRIVATE | DB_THREAD, 0) == 0); CuAssert(ct, "db_create", db_create(&db, dbenv, 0) == 0); CuAssert(ct, "DB->open", db->open( db, NULL, NULL, "TestSetThreadCount", DB_HASH, DB_CREATE, 0) == 0); db->close(db, 0); dbenv->close(dbenv, 0); #else printf("TestSetThreadCount is not supported by the build.\n"); #endif /* HAVE_HASH */ return (0); }
/* * Opens the database environment. * * 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 openEnvironment( const char* const path, DB_ENV** const dbEnv) { RegStatus status; DB_ENV* env; assert(NULL != path); log_clear(); if (0 == (status = createEnvHandle(path, &env))) { /* * The database is configured for "concurrent data store" * access rather than for transactional access because the * former is faster and sufficient. */ status = env->open(env, path, DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL, 0); if (status) { log_add("Couldn't open environment for database \"%s\"", path); status = EIO; } else { *dbEnv = env; } if (status) (void)env->close(env, 0); } /* "env" allocated */ return status; }
static int db_fini(dbiIndex dbi, const char * dbhome, const char * dbfile, const char * dbsubfile) { rpmdb rpmdb = dbi->dbi_rpmdb; DB_ENV * dbenv = rpmdb->db_dbenv; int rc; if (dbenv == NULL) return 0; rc = dbenv->close(dbenv, 0); rc = cvtdberr(dbi, "dbenv->close", rc, _debug); if (dbfile) rpmlog(RPMLOG_DEBUG, "closed db environment %s/%s\n", dbhome, dbfile); if (rpmdb->db_remove_env) { int xx; xx = db_env_create(&dbenv, 0); xx = cvtdberr(dbi, "db_env_create", xx, _debug); xx = dbenv->remove(dbenv, dbhome, 0); xx = cvtdberr(dbi, "dbenv->remove", xx, _debug); if (dbfile) rpmlog(RPMLOG_DEBUG, "removed db environment %s/%s\n", dbhome, dbfile); } return rc; }
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; }
/* * Opens the backend database. * * ARGUMENTS: * backend Pointer to pointer to backend structure. Shall not be * NULL. Upon successful return, "*backend" will be set. * The client should call "beClose(*backend)" when the * backend is no longer needed. * dir Pathname of the parent directory of the database. * Shall not be NULL. The client can free it upon return. * forWriting Open the database for writing? 0 <=> no * RETURNS: * 0 Success. "*backend" is set. * ENOMEM System error. "log_start()" called. * EIO Backend database error. "log_start()" called. */ RegStatus beOpen( Backend** const backend, const char* const dir, int forWriting) { RegStatus status; Backend* back = (Backend*)malloc(sizeof(Backend)); assert(NULL != dir); if (NULL == back) { log_serror("Couldn't allocate %lu bytes", (long)sizeof(Backend)); status = ENOMEM; } else { DB_ENV* env; DB* db; StringBuf* path; if (0 == (status = sb_new(&path, PATH_MAX))) { if (0 == (status = sb_cat(path, dir, "/", DB_DIRNAME))) { if (0 == (status = createDbHandle(path, &env, &db))) { if (status = db->open(db, NULL, DB_FILENAME, NULL, DB_BTREE, forWriting ? DB_CREATE : DB_RDONLY, 0)) { log_add("Couldn't open database \"%s\" in \"%s\" " "for %s", DB_FILENAME, path, forWriting ? "writing" : "reading"); status = EIO; } else { back->db = db; back->cursor.dbCursor = NULL; *backend = back; /* success */ } /* "db" opened */ /* * According to the documentation on DB->open(), if that * call fails, then DB->close() must be called to discard * the DB handle, so DB->close() is the termination * counterpart of db_create() rather than of DB->open(). */ if (status) { (void)db->close(db, 0); (void)env->close(env, 0); } } /* "env" allocated */ } /* DB directory pathname created */ sb_free(path); } /* "path" allocated */ if (status) free(back); } /* "back" allocated */ return status; }
// If the DB_ENV handle is still open, we close it. This is to make stack // allocation of DbEnv objects easier so that they are cleaned up in the error // path. Note that the C layer catches cases where handles are open in the // environment at close time and reports an error. Applications should call // close explicitly in normal (non-exceptional) cases to check the return // value. // DbEnv::~DbEnv() { DB_ENV *env = unwrap(this); if (env != NULL) { (void)env->close(env, 0); cleanup(); } }
int bdblib_create_dbenv(DB_ENV **_dbenv, char* _home) { DB_ENV *env; char *progname; int rc, flags; progname = "kamailio"; /* Create an environment and initialize it for additional error * reporting. */ if ((rc = db_env_create(&env, 0)) != 0) { ERR("db_env_create failed! bdb error: %s.\n", db_strerror(rc)); return (rc); } env->set_errpfx(env, progname); /* Specify the shared memory buffer pool cachesize */ if ((rc = env->set_cachesize(env, 0, _bdb_parms->cache_size, 0)) != 0) { ERR("dbenv set_cachsize failed! bdb error: %s.\n", db_strerror(rc)); env->err(env, rc, "set_cachesize"); goto err; } /* Concurrent Data Store flags */ flags = DB_CREATE | DB_INIT_CDB | DB_INIT_MPOOL | DB_THREAD; /* Transaction Data Store flags ; not supported yet */ /* flags = DB_CREATE | DB_RECOVER | DB_INIT_LOG | DB_INIT_LOCK | DB_INIT_MPOOL | DB_THREAD | DB_INIT_TXN; */ /* Open the environment */ if ((rc = env->open(env, _home, flags, 0)) != 0) { ERR("dbenv is not initialized! bdb error: %s.\n",db_strerror(rc)); env->err(env, rc, "environment open: %s", _home); goto err; } *_dbenv = env; return (0); err: (void)env->close(env, 0); return (rc); }
// Note: if the user has not closed, we call _destroy_check // to warn against this non-safe programming practice, // and call close anyway. // DbEnv::~DbEnv() { DB_ENV *env = unwrap(this); if (env != NULL) { _destroy_check("DbEnv", 1); (void)env->close(env, 0); // extra safety cleanup(); } }
int main(int argc,char * argv[]) { int rc; DB_ENV *env; DB *dbi; DBT key, data; DB_TXN *txn; DBC *cursor; char sval[32], kval[32]; #define FLAGS (DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_INIT_MPOOL|DB_CREATE|DB_THREAD) rc = db_env_create(&env, 0); rc = env->open(env, "./testdb", FLAGS, 0664); rc = db_create(&dbi, env, 0); rc = env->txn_begin(env, NULL, &txn, 0); rc = dbi->open(dbi, txn, "test.bdb", NULL, DB_BTREE, DB_CREATE, 0664); memset(&key, 0, sizeof(DBT)); memset(&data, 0, sizeof(DBT)); key.size = sizeof(int); key.data = sval; data.size = sizeof(sval); data.data = sval; sprintf(sval, "%03x %d foo bar", 32, 3141592); rc = dbi->put(dbi, txn, &key, &data, 0); rc = txn->commit(txn, 0); if (rc) { fprintf(stderr, "txn->commit: (%d) %s\n", rc, db_strerror(rc)); goto leave; } rc = env->txn_begin(env, NULL, &txn, 0); rc = dbi->cursor(dbi, txn, &cursor, 0); key.flags = DB_DBT_USERMEM; key.data = kval; key.ulen = sizeof(kval); data.flags = DB_DBT_USERMEM; data.data = sval; data.ulen = sizeof(sval); while ((rc = cursor->c_get(cursor, &key, &data, DB_NEXT)) == 0) { printf("key: %p %.*s, data: %p %.*s\n", key.data, (int) key.size, (char *) key.data, data.data, (int) data.size, (char *) data.data); } rc = cursor->c_close(cursor); rc = txn->abort(txn); leave: rc = dbi->close(dbi, 0); rc = env->close(env, 0); return rc; }
void env_open(DB_ENV **dbenvp) { DB_ENV *dbenv; int ret; /* Create the environment handle. */ if ((ret = db_env_create(&dbenv, 0)) != 0) { fprintf(stderr, "txnapp: db_env_create: %s\n", db_strerror(ret)); exit (1); } /* Set up error handling. */ dbenv->set_errpfx(dbenv, "txnapp"); dbenv->set_errfile(dbenv, stderr); /* * Open a transactional environment: * create if it doesn't exist * free-threaded handle * run recovery * read/write owner only */ if ((ret = dbenv->open(dbenv, ENV_DIRECTORY, DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_RECOVER , S_IRUSR | S_IWUSR)) != 0) { (void)dbenv->close(dbenv, 0); fprintf(stderr, "dbenv->open: %s: %s\n", ENV_DIRECTORY, db_strerror(ret)); exit (1); } *dbenvp = dbenv; }
void bdb_close(void) { DB *db; DBC *dbc; DB_ENV *dbenv; dbc = g.dbc; db = g.bdb; dbenv = db->dbenv; assert(dbc->close(dbc) == 0); assert(db->close(db, 0) == 0); assert(dbenv->close(dbenv, 0) == 0); key_gen_teardown(&keyitem); }
int main(int argc, char *argv[]) { extern char *optarg; DB_ENV *dbenv; const char *home; char ch; int ret; dbenv = NULL; ret = 0; home = NULL; /* Create and configure the environment handle. */ if ((ret = create_env(progname, &dbenv)) != 0) goto err; /* Collect the command line options. */ while ((ch = getopt(argc, argv, "h:")) != EOF) switch (ch) { case 'h': home = optarg; break; case '?': default: usage(); } /* Error check command line. */ if (home == NULL) usage(); /* Open the environment. */ if ((ret = env_init(dbenv, home)) != 0) goto err; if ((ret = doloop(dbenv)) != 0) { dbenv->err(dbenv, ret, "Application failed"); goto err; } err: if (dbenv != NULL) (void)dbenv->close(dbenv, 0); return (ret); }
// If the DB_ENV handle is still open, we close it. This is to make stack // allocation of DbEnv objects easier so that they are cleaned up in the error // path. Note that the C layer catches cases where handles are open in the // environment at close time and reports an error. Applications should call // close explicitly in normal (non-exceptional) cases to check the return // value. // DbEnv::~DbEnv() { DB_ENV *dbenv = unwrap(this); /* * Specify DB_FORCESYNC to make sure databases are sync'ed to disk. * Users can call DbEnv::close with 0 as real parameter to close all * but the last environment object/handle. Doing so can avoid * unnecessary database syncs. The last environment object/handle * must be closed with DB_FORCESYNC parameter, or be closed via this * function. */ if (dbenv != NULL) { (void)dbenv->close(dbenv, DB_FORCESYNC); cleanup(); } }
void bdb_close(void) { DB *db; DBC *dbc; DB_ENV *dbenv; dbc = g.dbc; db = g.bdb; dbenv = db->dbenv; assert(dbc->close(dbc) == 0); assert(db->close(db, 0) == 0); assert(dbenv->close(dbenv, 0) == 0); free(keybuf); keybuf = NULL; }
/* * 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; }
static int db_fini(rpmdb rdb, const char * dbhome) { DB_ENV * dbenv = rdb->db_dbenv; int rc; int lockfd = -1; uint32_t eflags = 0; if (dbenv == NULL) return 0; if (rdb->db_opens > 1) { rdb->db_opens--; return 0; } (void) dbenv->get_open_flags(dbenv, &eflags); if (!(eflags & DB_PRIVATE)) lockfd = serialize_env(dbhome); rc = dbenv->close(dbenv, 0); rc = dbapi_err(rdb, "dbenv->close", rc, _debug); rpmlog(RPMLOG_DEBUG, "closed db environment %s\n", dbhome); if (!(eflags & DB_PRIVATE) && rdb->db_remove_env) { int xx; xx = db_env_create(&dbenv, 0); xx = dbapi_err(rdb, "db_env_create", xx, _debug); xx = dbenv->remove(dbenv, dbhome, 0); /* filter out EBUSY as it just means somebody else gets to clean it */ xx = dbapi_err(rdb, "dbenv->remove", xx, (xx == EBUSY ? 0 : _debug)); rpmlog(RPMLOG_DEBUG, "removed db environment %s\n", dbhome); } if (lockfd >= 0) close(lockfd); return rc; }
int dblayer_copy_file_resetlsns(char *home_dir, char *source_file_name, char *destination_file_name, int overwrite, dblayer_private *priv, ldbm_instance *inst) { int retval = 0; DB_ENV *env = NULL; slapi_log_err(SLAPI_LOG_TRACE, "dblayer_copy_file_resetlsns", "=>\n"); /* Make the environment */ retval = dblayer_make_private_simple_env(home_dir,&env); if (retval || !env) { slapi_log_err(SLAPI_LOG_ERR, "dblayer_copy_file_resetlsns", "Call to dblayer_make_private_simple_env failed!\n" "Unable to open an environment."); goto out; } /* Do the copy */ retval = dblayer_copy_file_keybykey(env, source_file_name, destination_file_name, overwrite, priv, inst); if (retval) { slapi_log_err(SLAPI_LOG_ERR, "dblayer_copy_file_resetlsns", "Copy did not complete successfully."); } out: /* Close the environment */ if (env) { int retval2 = 0; retval2 = env->close(env,0); if (retval2) { if (0 == retval) { retval = retval2; slapi_log_err(SLAPI_LOG_ERR, "dblayer_copy_file_resetlsns", "error %d: %s\n", retval, db_strerror(retval)); } } } slapi_log_err(SLAPI_LOG_TRACE, "dblayer_copy_file_resetlsns", "<=\n"); return retval; }
int DbEnv::close(u_int32_t flags) { int ret; DB_ENV *env = unwrap(this); ret = env->close(env, flags); // after a close (no matter if success or failure), // the underlying DB_ENV object must not be accessed. cleanup(); // It's safe to throw an error after the close, // since our error mechanism does not peer into // the DB* structures. // if (ret != 0) DB_ERROR(this, "DbEnv::close", ret, error_policy()); return (ret); }
static int kvs_terminate(WT_DATA_SOURCE *wtds, WT_SESSION *session) { DB_ENV *dbenv; DATA_SOURCE *ds; WT_EXTENSION_API *wt_api; int ret = 0; ds = (DATA_SOURCE *)wtds; wt_api = ds->wt_api; dbenv = ds->dbenv; if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) ESET(wt_api, session, WT_ERROR, "DbEnv.close: %s", db_strerror(ret)); ETRET(lock_destroy(wt_api, session, &ds->rwlock)); return (ret); }
int storage_close(struct storage* s) { int result = 0; DB* dbp = s->db; DB_ENV* dbenv = s->env; if (dbp->close(dbp, 0) != 0) { paxos_log_error("DB_ENV close failed"); result = -1; } if (dbenv->close(dbenv, 0) != 0) { paxos_log_error("DB close failed"); result = -1; } free(s); paxos_log_info("Berkeley DB storage closed successfully"); return result; }
/* * Checks the database environment. * * ARGUMENTS: * path Pathname of the database directory. Shall not be NULL. * The client can free it upon return. * RETURNS: * 0 Success. * ENOMEM System error. "log_start()" called. * EIO Backend database error. "log_start()" called. * ECANCELED The database environment must be recovered. * "log_start()" called. */ static RegStatus verifyEnvironment( const char* const path) { RegStatus status; DB_ENV* env; assert(NULL != path); if (0 == (status = openEnvironment(path, &env))) { if (0 != (status = env->failchk(env, 0))) { log_add("The environment of database \"%s\" must be recovered", path); status = ECANCELED; } (void)env->close(env, 0); } /* "env" allocated */ return status; }
int DbEnv::close(u_int32_t flags) { DB_ENV *env = unwrap(this); int err, init_err; COMPQUIET(init_err, 0); // after a close (no matter if success or failure), // the underlying DB_ENV object must not be accessed, // so we clean up in advance. // cleanup(); // It's safe to throw an error after the close, // since our error mechanism does not peer into // the DB* structures. // if ((err = env->close(env, flags)) != 0) { DB_ERROR("DbEnv::close", err, error_policy()); } return (err); }
/* * Verifies the database. * * ARGUMENTS: * path Pathname of the database directory. Shall not be NULL. * The client can free it upon return. * RETURNS: * 0 Success. * ENOMEM System error. "log_start()" called. * EIO Backend database error. "log_start()" called. */ static RegStatus verifyDatabase( const char* const path) { RegStatus status; DB_ENV* env; DB* db; assert(NULL != path); if (0 == (status = createDbHandle(path, &env, &db))) { if (status = db->verify(db, DB_FILENAME, NULL, NULL, 0)) { log_add("Couldn't verify file \"%s\" of database " "\"%s\"", DB_FILENAME, path); status = EIO; } (void)env->close(env, 0); } /* "env" allocated */ return status; }
DB_ENV *db_setup(char *home) { DB_ENV *dbenv; int ret; /* * Create an environment and initialize it for additional error * reporting. */ if ((ret = db_env_create(&dbenv, 0)) != 0) { return (NULL); } ci_debug_printf(5,"Environment created OK.\n"); // dbenv->set_data_dir(dbenv, ""); dbenv->set_data_dir(dbenv, home); ci_debug_printf(5,"Data dir set to %s.\n", home); /* dbenv->set_shm_key(dbenv, 888888L); ci_debug_printf(5,"Shared memory set.\n"); */ /* * Specify the shared memory buffer pool cachesize: 5MB. * Databases are in a subdirectory of the environment home. */ // if ((ret = dbenv->set_cachesize(dbenv, 0, 5 * 1024 * 1024, 0)) != 0) { // dbenv->err(dbenv, ret, "set_cachesize"); // goto err; // } /* Open the environment */ if ((ret = dbenv->open(dbenv, home, DB_CREATE | DB_INIT_LOCK | DB_INIT_MPOOL|DB_THREAD /*| DB_SYSTEM_MEM*/, 0)) != 0){ ci_debug_printf(1, "Environment open failed: %s\n", db_strerror(ret)); dbenv->close(dbenv, 0); return NULL; } ci_debug_printf(5,"DB setup OK.\n"); return (dbenv); }