+/*
+ * Database recovery logic:
+ * This function is called whenever the database appears to have been
+ * shut down uncleanly, as determined by the alock functions.
+ * Because of the -q function in slapadd, there is also the possibility
+ * that the shutdown happened when transactions weren't being used and
+ * the database is likely to be corrupt. The function checks for this
+ * condition by examining the environment to make sure it had previously
+ * been opened with transactions enabled. If this is the case, the
+ * database is recovered as usual. If transactions were not enabled,
+ * then this function will return a fail.
+ */
+static int
+bdb_db_recover( BackendDB *be )
+{
+ struct bdb_info *bdb = (struct bdb_info *) be->be_private;
+ DB_ENV *re_dbenv;
+ u_int32_t flags;
+ int rc;
+#ifdef HAVE_EBCDIC
+ char path[MAXPATHLEN];
+#endif
+
+ /* Create the recovery environment, then open it.
+ * We use the DB_JOIN in combination with a flags value of
+ * zero so we join an existing environment and can read the
+ * value of the flags that were used the last time the
+ * environment was opened. DB_CREATE is added because the
+ * open would fail if the only thing that had been done
+ * was an open with transactions and logs disabled.
+ */
+ rc = db_env_create( &re_dbenv, 0 );
+ if( rc != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_recover: db_env_create failed: %s (%d)\n",
+ db_strerror(rc), rc, 0 );
+ return rc;
+ }
+ re_dbenv->set_errpfx( re_dbenv, be->be_suffix[0].bv_val );
+ re_dbenv->set_errcall( re_dbenv, bdb_errcall );
+
+ Debug( LDAP_DEBUG_TRACE,
+ "bdb_db_recover: dbenv_open(%s)\n",
+ bdb->bi_dbenv_home, 0, 0);
+
+#ifdef HAVE_EBCDIC
+ strcpy( path, bdb->bi_dbenv_home );
+ __atoe( path );
+ rc = re_dbenv->open( re_dbenv,
+ path,
+ DB_JOINENV,
+ bdb->bi_dbenv_mode );
+#else
+ rc = re_dbenv->open( re_dbenv,
+ bdb->bi_dbenv_home,
+ DB_JOINENV,
+ bdb->bi_dbenv_mode );
+#endif
+
+ if( rc == ENOENT ) {
+ Debug( LDAP_DEBUG_TRACE,
+ "bdb_db_recover: DB environment files are missing, assuming it was "
+ "manually recovered\n", 0, 0, 0 );
+ return 0;
+ }
+ else if( rc != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_recover: dbenv_open failed: %s (%d)\n",
+ db_strerror(rc), rc, 0 );
+ return rc;
+ }
+
+ /*
+ * Check the flags that had been used in the previous open.
+ * The environment needed to have had both
+ * DB_INIT_LOG and DB_INIT_TXN set for us to be willing to
+ * recover the database. Otherwise the an app failed while running
+ * without transactions and logs enabled and the dn2id and id2entry
+ * mapping is likely to be corrupt.
+ */
+ rc = re_dbenv->get_open_flags( re_dbenv, &flags );
+ if( rc != 0 ) {
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_recover: get_open_flags failed: %s (%d)\n",
+ db_strerror(rc), rc, 0 );
+ return rc;
+ }
+
+ (void) re_dbenv->close( re_dbenv, 0 );
+
+ if( (flags & DB_INIT_LOG) && (flags & DB_INIT_TXN) ) {
+ return bdb_do_recovery( be );
+ }
+
+re_exit:
+ Debug( LDAP_DEBUG_ANY,
+ "bdb_db_recover: Database cannot be recovered. "\
+ "Restore from backup!\n", 0, 0, 0);
+ return -1;
+
+}
+
+