+ /* Check existence of dbenv_home. Any error means trouble */
+ rc = stat( bdb->bi_dbenv_home, &stat1 );
+ if( rc !=0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: Cannot access database directory %s (%d)\n",
+ bdb->bi_dbenv_home, errno, 0 );
+ return -1;
+ }
+
+ /* Perform database use arbitration/recovery logic */
+ rc = alock_open( &bdb->bi_alock_info,
+ "slapd",
+ bdb->bi_dbenv_home,
+ slapMode & SLAP_TOOL_READONLY ?
+ ALOCK_LOCKED : ALOCK_UNIQUE );
+
+ if( rc == ALOCK_RECOVER ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: unclean shutdown detected;"
+ " attempting recovery.\n",
+ 0, 0, 0 );
+ if( bdb_db_recover( be ) != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: DB recovery failed.\n",
+ 0, 0, 0 );
+ return -1;
+ }
+ if( alock_recover (&bdb->bi_alock_info) != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: alock_recover failed\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ } else if( rc == ALOCK_BUSY ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: database already in use\n",
+ 0, 0, 0 );
+ return -1;
+ } else if( rc != ALOCK_CLEAN ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: alock package is unstable\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ /*
+ * The DB_CONFIG file may have changed. If so, recover the
+ * database so that new settings are put into effect. Also
+ * note the possible absence of DB_CONFIG in the log.
+ */
+ if( stat( bdb->bi_db_config_path, &stat1 ) == 0 ) {
+ ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
+ *ptr++ = LDAP_DIRSEP[0];
+ strcpy( ptr, bdbi_databases[0].file);
+ if( stat( path, &stat2 ) == 0 ) {
+ if( stat2.st_mtime <= stat1.st_mtime ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: DB_CONFIG for suffix %s has changed.\n"
+ "Performing database recovery to activate new settings.\n",
+ be->be_suffix[0].bv_val, 0, 0 );
+ if( bdb_do_recovery( be ) != 0) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: db recovery failed.\n",
+ 0, 0, 0 );
+ return -1;
+ }
+ }
+
+ }
+ }
+ else {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_open: Warning - No DB_CONFIG file found "
+ "in directory %s: (%d)\n"
+ "Expect poor performance for suffix %s.\n",
+ bdb->bi_dbenv_home, errno, be->be_suffix[0].bv_val );
+ }
+
+ flags = DB_INIT_MPOOL | DB_THREAD | DB_CREATE;
+ if ( !( slapMode & SLAP_TOOL_QUICK ))
+ flags |= DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN;