+ char path[MAXPATHLEN];
+ char *dbhome;
+ int do_recover = 0, do_alock_recover = 0, open_env = 1;
+ int alockt, quick = 0;
+
+ if ( be->be_suffix == NULL ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": need suffix\n",
+ 0, 0, 0 );
+ return -1;
+ }
+
+ Debug( LDAP_DEBUG_ARGS,
+ LDAP_XSTRING(bdb_db_open) ": %s\n",
+ be->be_suffix[0].bv_val, 0, 0 );
+
+#ifndef BDB_MULTIPLE_SUFFIXES
+ if ( be->be_suffix[1].bv_val ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": only one suffix allowed\n", 0, 0, 0 );
+ return -1;
+ }
+#endif
+
+ /* Check existence of dbenv_home. Any error means trouble */
+ rc = stat( bdb->bi_dbenv_home, &stat1 );
+ if( rc !=0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": Cannot access database directory %s (%d)\n",
+ bdb->bi_dbenv_home, errno, 0 );
+ return -1;
+ }
+
+ /* Perform database use arbitration/recovery logic */
+ alockt = (slapMode & SLAP_TOOL_READONLY) ? ALOCK_LOCKED : ALOCK_UNIQUE;
+ if ( slapMode & SLAP_TOOL_QUICK ) {
+ alockt |= ALOCK_NOSAVE;
+ quick = 1;
+ }
+
+ rc = alock_open( &bdb->bi_alock_info,
+ "slapd",
+ bdb->bi_dbenv_home, alockt );
+
+ /* alockt is TRUE if the existing environment was created in Quick mode */
+ alockt = (rc & ALOCK_NOSAVE) ? 1 : 0;
+ rc &= ~ALOCK_NOSAVE;
+
+ if( rc == ALOCK_RECOVER ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": unclean shutdown detected;"
+ " attempting recovery.\n",
+ 0, 0, 0 );
+ do_alock_recover = 1;
+ do_recover = DB_RECOVER;
+ } else if( rc == ALOCK_BUSY ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": database already in use\n",
+ 0, 0, 0 );
+ return -1;
+ } else if( rc != ALOCK_CLEAN ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(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 ) {
+ if ( !do_recover ) {
+ char *ptr = lutil_strcopy(path, bdb->bi_dbenv_home);
+ *ptr++ = LDAP_DIRSEP[0];
+ strcpy( ptr, "__db.001" );
+ if( stat( path, &stat2 ) == 0 ) {
+ if( stat2.st_mtime < stat1.st_mtime ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(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 );
+ do_recover = DB_RECOVER;
+ }
+ }
+ }
+ }
+ else {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(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 );
+ }
+
+ /* Always let slapcat run, regardless of environment state.
+ * This can be used to cause a cache flush after an unclean
+ * shutdown.
+ */
+ if ( do_recover && ( slapMode & SLAP_TOOL_READONLY )) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": Recovery skipped in read-only mode. "
+ "Run manual recovery if errors are encountered.\n",
+ 0, 0, 0 );
+ do_recover = 0;
+ quick = alockt;
+ }
+
+ /* An existing environment in Quick mode has nothing to recover. */
+ if ( alockt && do_recover ) {
+ Debug( LDAP_DEBUG_ANY,
+ LDAP_XSTRING(bdb_db_open) ": cannot recover, database must be reinitialized.\n",
+ 0, 0, 0 );
+ rc = -1;
+ goto fail;
+ }