Blame SOURCES/db-5.3.28-rpm-lock-check.patch

c7d609
diff -up db-5.3.28/src/dbinc_auto/int_def.in.rpmlock db-5.3.28/src/dbinc_auto/int_def.in
c7d609
--- db-5.3.28/src/dbinc_auto/int_def.in.rpmlock	2017-06-26 15:09:17.883356255 +0200
c7d609
+++ db-5.3.28/src/dbinc_auto/int_def.in	2017-06-26 15:09:27.421170401 +0200
c7d609
@@ -1573,6 +1573,7 @@
c7d609
 #define	__os_posix_err __os_posix_err@DB_VERSION_UNIQUE_NAME@
c7d609
 #define	__os_fileid __os_fileid@DB_VERSION_UNIQUE_NAME@
c7d609
 #define	__check_lock_fn __check_lock_fn@DB_VERSION_UNIQUE_NAME@
c7d609
+#define	__rpm_lock_free __rpm_lock_free@DB_VERSION_UNIQUE_NAME@
c7d609
 #define	__os_fdlock __os_fdlock@DB_VERSION_UNIQUE_NAME@
c7d609
 #define	__os_fsync __os_fsync@DB_VERSION_UNIQUE_NAME@
c7d609
 #define	__os_getenv __os_getenv@DB_VERSION_UNIQUE_NAME@
c7d609
diff -up db-5.3.28/src/dbinc_auto/os_ext.h.rpmlock db-5.3.28/src/dbinc_auto/os_ext.h
c7d609
--- db-5.3.28/src/dbinc_auto/os_ext.h.rpmlock	2017-06-26 15:09:21.940277203 +0200
c7d609
+++ db-5.3.28/src/dbinc_auto/os_ext.h	2017-06-26 15:09:27.354171707 +0200
c7d609
@@ -42,6 +42,7 @@ char *__os_strerror __P((int, char *, si
c7d609
 int __os_posix_err __P((int));
c7d609
 int __os_fileid __P((ENV *, const char *, int, u_int8_t *));
c7d609
 int __check_lock_fn __P((char *, pid_t));
c7d609
+int __rpm_lock_free __P((ENV *));
c7d609
 int __os_fdlock __P((ENV *, DB_FH *, off_t, db_lockmode_t, int));
c7d609
 int __os_fsync __P((ENV *, DB_FH *));
c7d609
 int __os_getenv __P((ENV *, const char *, char **, size_t));
c7d609
diff -up db-5.3.28/src/env/env_region.c.rpmlock db-5.3.28/src/env/env_region.c
c7d609
--- db-5.3.28/src/env/env_region.c.rpmlock	2017-06-26 15:09:12.479461558 +0200
c7d609
+++ db-5.3.28/src/env/env_region.c	2017-06-26 15:09:12.481461519 +0200
c7d609
@@ -291,18 +291,23 @@ user_map_functions:
c7d609
 	if (create_ok &&
c7d609
 	    ret == DB_OLD_VERSION &&
c7d609
 	    ENV_PRIMARY_LOCK(env, DB_LOCK_WRITE, 1) == 0) {
c7d609
-		if (FLD_ISSET(dbenv->verbose, DB_VERB_RECOVERY))
c7d609
-			__db_msg(env, "Recreating idle environment");
c7d609
-		F_SET(infop, REGION_CREATE_OK);
c7d609
+		/* If the rpm transaction lock is taken we cannot safely rebuild */
c7d609
+        if (!__rpm_lock_free(env))
c7d609
+            ENV_PRIMARY_UNLOCK(env);
c7d609
+        else {
c7d609
+            if (FLD_ISSET(dbenv->verbose, DB_VERB_RECOVERY))
c7d609
+                __db_msg(env, "Recreating idle environment");
c7d609
+            F_SET(infop, REGION_CREATE_OK);
c7d609
 
c7d609
-		/*
c7d609
-		 * Detach from the environment region; we need to unmap it (and
c7d609
-		 * close any file handle) so that we don't leak memory or files.
c7d609
-		 */
c7d609
-		DB_ASSERT(env, infop->rp == NULL);
c7d609
-		infop->rp = &tregion;
c7d609
-		(void)__env_sys_detach(env, infop, 0);
c7d609
-		goto creation;
c7d609
+            /*
c7d609
+             * Detach from the environment region; we need to unmap it (and
c7d609
+             * close any file handle) so that we don't leak memory or files.
c7d609
+             */
c7d609
+            DB_ASSERT(env, infop->rp == NULL);
c7d609
+            infop->rp = &tregion;
c7d609
+            (void)__env_sys_detach(env, infop, 0);
c7d609
+            goto creation;
c7d609
+        }
c7d609
 	}
c7d609
 
c7d609
 	if (renv->majver != DB_VERSION_MAJOR ||
c7d609
diff -up db-5.3.28/src/os/os_flock.c.rpmlock db-5.3.28/src/os/os_flock.c
c7d609
--- db-5.3.28/src/os/os_flock.c.rpmlock	2017-06-26 15:09:12.480461538 +0200
c7d609
+++ db-5.3.28/src/os/os_flock.c	2017-06-26 15:09:12.481461519 +0200
c7d609
@@ -79,6 +79,35 @@ int __check_lock_fn(fn, pid)
c7d609
 }
c7d609
 
c7d609
 /*
c7d609
+ * __rpm_lock_free --
c7d609
+ * Try to look at a lock used by rpm to see if libdb is being
c7d609
+ * updated and it is safe to access its environment files.
c7d609
+ * PUBLIC: int __rpm_lock_free __P((ENV *));
c7d609
+ */
c7d609
+
c7d609
+#define RPM_PATH SHAREDSTATEDIR "/rpm"
c7d609
+#define RPMLOCK_PATH RPM_PATH "/.rpm.lock"
c7d609
+
c7d609
+int __rpm_lock_free(env)
c7d609
+    ENV *env;
c7d609
+{
c7d609
+    int ret;
c7d609
+
c7d609
+    /* No need to check the transaction lock if not in rpm */
c7d609
+    if (strstr(env->db_home, RPM_PATH) == NULL)
c7d609
+        return 1;
c7d609
+
c7d609
+    /* Assume it is safe to rebuild if the lock file does not exist */
c7d609
+    if (access(RPMLOCK_PATH, F_OK) && errno == ENOENT)
c7d609
+        return 1;
c7d609
+
c7d609
+    ret = __check_lock_fn(RPMLOCK_PATH, 0);
c7d609
+    /* __check_lock_fn can return -1 on failure - return 0 (taken) instead */
c7d609
+    return ret == -1 ? 0: ret;
c7d609
+
c7d609
+}
c7d609
+
c7d609
+/*
c7d609
  * __os_fdlock --
c7d609
  *	Acquire/release a lock on a byte in a file.
c7d609
  *