Fix some issues present in the upstream patch for condvar issue

Leftover "#define static" from upstream testing
Unnecessary renaming of variables/functions

Resolves: #1394862
This commit is contained in:
Petr Kubat 2017-05-18 11:33:15 +02:00
parent 559da4d941
commit 9ded8d2c32
2 changed files with 310 additions and 465 deletions

View File

@ -191,39 +191,6 @@ diff -r db78da0996b1 src/db/db_meta.c
switch (pagep->type) { switch (pagep->type) {
case P_OVERFLOW: case P_OVERFLOW:
case P_INVALID: case P_INVALID:
diff -r db78da0996b1 src/dbinc/atomic.h
--- a/src/dbinc/atomic.h Mon Sep 09 11:09:35 2013 -0400
+++ b/src/dbinc/atomic.h Sat Apr 29 04:10:18 2017 -0700
@@ -144,7 +144,7 @@
#define atomic_inc(env, p) __atomic_inc(p)
#define atomic_dec(env, p) __atomic_dec(p)
#define atomic_compare_exchange(env, p, o, n) \
- __atomic_compare_exchange((p), (o), (n))
+ __db_atomic_compare_exchange((p), (o), (n))
static inline int __atomic_inc(db_atomic_t *p)
{
int temp;
@@ -176,7 +176,7 @@
* http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
* which configure could be changed to use.
*/
-static inline int __atomic_compare_exchange(
+static inline int __db_atomic_compare_exchange(
db_atomic_t *p, atomic_value_t oldval, atomic_value_t newval)
{
atomic_value_t was;
diff -r db78da0996b1 src/dbinc/db_int.in
--- a/src/dbinc/db_int.in Mon Sep 09 11:09:35 2013 -0400
+++ b/src/dbinc/db_int.in Sat Apr 29 04:10:18 2017 -0700
@@ -792,7 +792,7 @@
pid_t pid_cache; /* Cached process ID */
- DB_FH *lockfhp; /* fcntl(2) locking file handle */
+ DB_FH *primary_fhp; /* fcntl(2) locking on __db.001 */
DB_LOCKER *env_lref; /* Locker in non-threaded handles */
diff -r db78da0996b1 src/dbinc/region.h diff -r db78da0996b1 src/dbinc/region.h
--- a/src/dbinc/region.h Mon Sep 09 11:09:35 2013 -0400 --- a/src/dbinc/region.h Mon Sep 09 11:09:35 2013 -0400
+++ b/src/dbinc/region.h Sat Apr 29 04:10:18 2017 -0700 +++ b/src/dbinc/region.h Sat Apr 29 04:10:18 2017 -0700
@ -278,15 +245,6 @@ diff -r db78da0996b1 src/dbinc_auto/os_ext.h
diff -r db78da0996b1 src/env/env_open.c diff -r db78da0996b1 src/env/env_open.c
--- a/src/env/env_open.c Mon Sep 09 11:09:35 2013 -0400 --- a/src/env/env_open.c Mon Sep 09 11:09:35 2013 -0400
+++ b/src/env/env_open.c Sat Apr 29 04:10:18 2017 -0700 +++ b/src/env/env_open.c Sat Apr 29 04:10:18 2017 -0700
@@ -937,6 +937,8 @@
{
DB_FH *fhp;
+ if (env->primary_fhp != NULL)
+ env->primary_fhp = NULL;
if (TAILQ_FIRST(&env->fdlist) == NULL)
return (0);
@@ -1031,11 +1033,11 @@ @@ -1031,11 +1033,11 @@
goto err; goto err;
@ -302,401 +260,6 @@ diff -r db78da0996b1 src/env/env_open.c
*/ */
if (FLD_ISSET(init_flags, DB_INITENV_CDB)) if (FLD_ISSET(init_flags, DB_INITENV_CDB))
LF_SET(DB_INIT_CDB); LF_SET(DB_INIT_CDB);
diff -r db78da0996b1 src/env/env_region.c
--- a/src/env/env_region.c Mon Sep 09 11:09:35 2013 -0400
+++ b/src/env/env_region.c Sat Apr 29 04:10:18 2017 -0700
@@ -14,17 +14,54 @@
#include "dbinc/log.h"
#include "dbinc/txn.h"
+#define static
static int __env_des_get __P((ENV *, REGINFO *, REGINFO *, REGION **));
static int __env_faultmem __P((ENV *, void *, size_t, int));
static int __env_sys_attach __P((ENV *, REGINFO *, REGION *));
static int __env_sys_detach __P((ENV *, REGINFO *, int));
+static int __env_check_recreate __P((ENV *, REGENV *, u_int32_t));
static void __env_des_destroy __P((ENV *, REGION *));
static void __env_remove_file __P((ENV *));
+
+/*
+ * If the system supports flock()-like file locking, then the primary region
+ * file __db.001 is exclusively locked during creation, and is read-locked while
+ * the environment is open. Most Unix-like systems have flock(), with the
+ * notable exception of Solaris.
+ * Note: fcntl cannot be used for this locking because of the unfortunate
+ * definition of its interaction with close(2). A process's fcntl locks are
+ * released whenever it closes any file descriptor for that file. So, if an
+ * environment is opened more than once, closing one of the DB_ENV handles would
+ * release the read lock that protects the other handle.
+ */
+#ifdef HAVE_FLOCK
+#define ENV_PRIMARY_LOCK(env, lockmode, async) \
+ ((env)->primary_fhp == NULL ? 0 : \
+ __os_fdlock((env), (env)->primary_fhp, -1, lockmode, async))
+#define ENV_PRIMARY_UNLOCK(env) \
+ ((env)->primary_fhp == NULL ? 0 : \
+ __os_fdlock((env), (env)->primary_fhp, -1, DB_LOCK_NG, 0))
+#else
+#define ENV_PRIMARY_LOCK(env, lockmode, async) (0)
+#define ENV_PRIMARY_UNLOCK(env) (0)
+#endif
+
/*
* __env_attach
* Join/create the environment
*
+ * Safely detecting and managing multiple processes' environment handles:
+ * BDB uses a shared or exclusive fcntl()-style lock on the first byte
+ * of the primary region file (__db.001) to detect whether other processes
+ * have the environment open, and to single-thread attempts to create the
+ * environment. If the open includes DB_CREATE, an exclusive lock is
+ * obtained during the open call. After the creation is finished, and
+ * anytime during a non-DB_CREATE env open, the process holds a shared
+ * lock.
+ * - single-thread creation of the environment
+ * - detect whether any other processes are currently attached to it.
+ *
* PUBLIC: int __env_attach __P((ENV *, u_int32_t *, int, int));
*/
int
@@ -104,7 +141,7 @@
if (create_ok) {
if ((ret = __os_open(env, infop->name, 0,
DB_OSO_CREATE | DB_OSO_EXCL | DB_OSO_REGION,
- env->db_mode, &env->lockfhp)) == 0)
+ env->db_mode, &env->primary_fhp)) == 0)
goto creation;
if (ret != EEXIST) {
__db_err(env, ret, "%s", infop->name);
@@ -120,8 +157,13 @@
* we're done.)
*/
if ((ret = __os_open(
- env, infop->name, 0, DB_OSO_REGION, 0, &env->lockfhp)) != 0)
+ env, infop->name, 0, DB_OSO_REGION, 0, &env->primary_fhp)) != 0)
goto err;
+ /* Wait to get shared access to the primary region. */
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_READ, 0)) != 0) {
+ __db_err(env, ret, "__env_attach: existing: shared lock error");
+ goto err;
+ }
/*
* !!!
@@ -153,7 +195,7 @@
* now, trying to make different versions of it work.)
*/
if ((ret = __os_ioinfo(env, infop->name,
- env->lockfhp, &mbytes, &bytes, NULL)) != 0) {
+ env->primary_fhp, &mbytes, &bytes, NULL)) != 0) {
__db_err(env, ret, "%s", infop->name);
goto err;
}
@@ -189,14 +231,14 @@
ret = EINVAL;
__db_err(env, ret, DB_STR_A("1535",
"%s: existing environment not created in system memory",
- "%s"), infop->name);
- goto err;
+ "%s"), infop->name);
+ goto err;
} else {
- if ((ret = __os_read(env, env->lockfhp, &rbuf,
+ if ((ret = __os_read(env, env->primary_fhp, &rbuf,
sizeof(rbuf), &nrw)) != 0 ||
nrw < (size_t)sizeof(rbuf) ||
(ret = __os_seek(env,
- env->lockfhp, 0, 0, rbuf.region_off)) != 0) {
+ env->primary_fhp, 0, 0, rbuf.region_off)) != 0) {
__db_err(env, ret, DB_STR_A("1536",
"%s: unable to read region info", "%s"),
infop->name);
@@ -204,7 +246,7 @@
}
}
- if ((ret = __os_read(env, env->lockfhp, &ref,
+ if ((ret = __os_read(env, env->primary_fhp, &ref,
sizeof(ref), &nrw)) != 0 || nrw < (size_t)sizeof(ref)) {
if (ret == 0)
ret = EIO;
@@ -218,14 +260,13 @@
segid = ref.segid;
}
-#ifndef HAVE_MUTEX_FCNTL
+#if !defined(HAVE_FCNTL) && !defined(HAVE_PTHREADS_TIMESTAMP)
/*
- * If we're not doing fcntl locking, we can close the file handle. We
- * no longer need it and the less contact between the buffer cache and
- * the VM, the better.
+ * Without fcntl-like support, we no longer need the file handle. Close
+ * it to limit the interaction between the buffer cache and the VM.
*/
- (void)__os_closehandle(env, env->lockfhp);
- env->lockfhp = NULL;
+ (void)__os_closehandle(env, env->primary_fhp);
+ env->primary_fhp = NULL;
#endif
/* Call the region join routine to acquire the region. */
@@ -233,6 +274,8 @@
tregion.size = (roff_t)size;
tregion.max = (roff_t)max;
tregion.segid = segid;
+ /* Attach to the existing primary region. */
+ /* The leaking db.001 gets open inside of here, in __os_attach(). */
if ((ret = __env_sys_attach(env, infop, &tregion)) != 0)
goto err;
@@ -246,20 +289,38 @@
infop->head = (u_int8_t *)infop->addr + sizeof(REGENV);
renv = infop->primary;
- /*
- * Make sure the region matches our build. Special case a region
- * that's all nul bytes, just treat it like any other corruption.
- */
+ if (create_ok &&
+ __env_check_recreate(env, renv, signature) == DB_OLD_VERSION &&
+ (ret = ENV_PRIMARY_LOCK(env, DB_LOCK_WRITE, 1)) == 0) {
+ if (FLD_ISSET(dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env, "Recreating idle environment");
+ F_SET(infop, REGION_CREATE_OK);
+
+ /*
+ * Detach from the environment region; we need to unmap it (and
+ * close any file handle) so that we don't leak memory or files.
+ */
+ DB_ASSERT(env, infop->rp == NULL);
+ infop->rp = &tregion;
+ (void)__env_sys_detach(env, infop, 0);
+ goto creation;
+ }
+
if (renv->majver != DB_VERSION_MAJOR ||
renv->minver != DB_VERSION_MINOR) {
- if (renv->majver != 0 || renv->minver != 0) {
+ /*
+ * Special case a region that's all nul bytes, just treat it
+ * like any other corruption.
+ */
+ if (renv->majver == 0 && renv->minver == 0)
+ ret = EINVAL;
+ else {
__db_errx(env, DB_STR_A("1538",
- "Program version %d.%d doesn't match environment version %d.%d",
+ "Program version %d.%d doesn't match in-use environment version %d.%d",
"%d %d %d %d"), DB_VERSION_MAJOR, DB_VERSION_MINOR,
renv->majver, renv->minver);
ret = DB_VERSION_MISMATCH;
- } else
- ret = EINVAL;
+ }
goto err;
}
if (renv->signature != signature) {
@@ -289,6 +350,18 @@
}
if (renv->magic != DB_REGION_MAGIC)
goto retry;
+ /*
+ * A bad magic number means that the env is new and not yet available:
+ * wait a while and try again. If the magic number says recovery is in
+ * process, remember the env creation time to record that recovery was
+ * the reason that the open failed.
+ */
+ if (renv->magic != DB_REGION_MAGIC) {
+ __db_msg(env, "attach sees bad region magic 0x%lx",
+ (u_long)renv->magic);
+ goto retry;
+ }
+
/*
* Get a reference to the underlying REGION information for this
@@ -296,7 +369,7 @@
*/
if ((ret = __env_des_get(env, infop, infop, &rp)) != 0 || rp == NULL)
goto find_err;
- infop->rp = rp;
+ infop->rp = rp;
/*
* There's still a possibility for inconsistent data. When we acquired
@@ -346,6 +419,12 @@
return (0);
creation:
+ /* Should this wait for the lock (passing 0 instead of 1)? */
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_WRITE, 1)) != 0) {
+ __db_err(env, ret, "__env_attach: creation could not lock %s",
+ env->primary_fhp->name);
+ goto err;
+ }
/* Create the environment region. */
F_SET(infop, REGION_CREATE);
@@ -437,7 +516,14 @@
renv->minver = (u_int32_t)minver;
renv->patchver = (u_int32_t)patchver;
renv->signature = signature;
-
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ renv->pthreads_timestamp = __os_pthreads_timestamp(env);
+ {
+ char *s = getenv("TS_ADJUST");
+ if (s != NULL)
+ renv->pthreads_timestamp -= atoi(s);
+ }
+#endif
(void)time(&renv->timestamp);
__os_unique_id(env, &renv->envid);
@@ -505,7 +591,7 @@
ref.segid = tregion.segid;
ref.max = tregion.max;
if ((ret = __os_write(
- env, env->lockfhp, &ref, sizeof(ref), &nrw)) != 0) {
+ env, env->primary_fhp, &ref, sizeof(ref), &nrw)) != 0) {
__db_err(env, ret, DB_STR_A("1545",
"%s: unable to write out public environment ID",
"%s"), infop->name);
@@ -513,16 +599,24 @@
}
}
-#ifndef HAVE_MUTEX_FCNTL
- /*
- * If we're not doing fcntl locking, we can close the file handle. We
- * no longer need it and the less contact between the buffer cache and
- * the VM, the better.
- */
- if (env->lockfhp != NULL) {
- (void)__os_closehandle(env, env->lockfhp);
- env->lockfhp = NULL;
+#ifdef HAVE_FCNTL
+ if ((ret = ENV_PRIMARY_UNLOCK(env)) != 0) {
+ __db_err(env, ret, "__env_attach: release exclusive lock");
+ goto err;
}
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_READ, 0)) != 0) {
+ __db_err(env, ret, "__env_attach: new: acquire shared lock");
+ goto err;
+ }
+#else
+ /*
+ * We no longer need the primary region file's handle and the less
+ * contact between the buffer cache and the VM, the better.
+ */
+ if (env->primary_fhp != NULL) {
+ (void)__os_closehandle(env, env->primary_fhp);
+ env->primary_fhp = NULL;
+ }
#endif
/* Everything looks good, we're done. */
@@ -531,9 +625,9 @@
err:
retry: /* Close any open file handle. */
- if (env->lockfhp != NULL) {
- (void)__os_closehandle(env, env->lockfhp);
- env->lockfhp = NULL;
+ if (env->primary_fhp != NULL) {
+ (void)__os_closehandle(env, env->primary_fhp);
+ env->primary_fhp = NULL;
}
/*
@@ -562,9 +656,9 @@
/* If we had a temporary error, wait awhile and try again. */
if (ret == 0) {
if (!retry_ok || ++retry_cnt > 3) {
+ ret = EAGAIN;
__db_errx(env, DB_STR("1546",
"unable to join the environment"));
- ret = EAGAIN;
} else {
__os_yield(env, retry_cnt * 3, 0);
goto loop;
@@ -575,6 +669,59 @@
}
/*
+ * __env_check_recreate --
+ * Determine whether an existing on-disk environment should be recreated
+ * because it is not compatible with this compiled BDB library.
+ *
+ * Returns:
+ * 0 -
+ * The env was generated by this library. No recreation needed.
+ * DB_OLD_VERSION -
+ * It was created by an earlier BDB version, or by an earlier
+ * version of libpthreads (on certain Linux systems). The caller
+ * will try to recreate it with the currently configured settings.
+ * DB_VERSION_MISMATCH -
+ * It was created by a newer version of BDB. Do not attempt to
+ * fix it, something is probably wrong with the application setup.
+ */
+static int
+__env_check_recreate(env, renv, signature)
+ ENV *env;
+ REGENV *renv;
+ u_int32_t signature;
+{
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ time_t pthreads_time;
+ char envtime[CTIME_BUFLEN], libtime[CTIME_BUFLEN];
+#endif
+
+ /* First, bail out if the env is too new for this code to handle. */
+ if (renv->majver > DB_VERSION_MAJOR ||
+ (renv->majver == DB_VERSION_MAJOR &&
+ renv->minver > DB_VERSION_MINOR))
+ return (DB_VERSION_MISMATCH);
+
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ pthreads_time = __os_pthreads_timestamp(env);
+ if (pthreads_time != renv->pthreads_timestamp) {
+ if (FLD_ISSET(env->dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env,
+ "Pthreads timestamp changed: env %.24s current %.24s",
+ __os_ctime(&renv->pthreads_timestamp, envtime),
+ __os_ctime(&pthreads_time, libtime));
+ return (DB_OLD_VERSION);
+ }
+#endif
+ if (renv->signature != signature || renv->majver != DB_VERSION_MAJOR ||
+ renv->minver != DB_VERSION_MINOR) {
+ if (FLD_ISSET(env->dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env, "Signature or version changed");
+ return (DB_OLD_VERSION);
+ }
+ return (0);
+}
+
+/*
* __env_turn_on --
* Turn on the created environment.
*
@@ -794,12 +941,11 @@
renv = infop->primary;
ret = 0;
- /* Close the locking file handle. */
- if (env->lockfhp != NULL) {
+ if (env->primary_fhp != NULL) {
if ((t_ret =
- __os_closehandle(env, env->lockfhp)) != 0 && ret == 0)
+ __os_closehandle(env, env->primary_fhp)) != 0 && ret == 0)
ret = t_ret;
- env->lockfhp = NULL;
+ env->primary_fhp = NULL;
}
/*
diff -r db78da0996b1 src/env/env_register.c diff -r db78da0996b1 src/env/env_register.c
--- a/src/env/env_register.c Mon Sep 09 11:09:35 2013 -0400 --- a/src/env/env_register.c Mon Sep 09 11:09:35 2013 -0400
+++ b/src/env/env_register.c Sat Apr 29 04:10:18 2017 -0700 +++ b/src/env/env_register.c Sat Apr 29 04:10:18 2017 -0700
@ -726,16 +289,6 @@ diff -r db78da0996b1 src/env/env_stat.c
STAT_HEX("Environment ID", renv->envid); STAT_HEX("Environment ID", renv->envid);
__mutex_print_debug_single(env, __mutex_print_debug_single(env,
"Primary region allocation and reference count mutex", "Primary region allocation and reference count mutex",
@@ -429,7 +433,8 @@
STAT_ULONG("Pid cache", env->pid_cache);
- STAT_ISSET("Lockfhp", env->lockfhp);
+ /* Change to Primary Region fhp? The name changed, but not its usage. */
+ STAT_ISSET("Lockfhp", env->primary_fhp);
STAT_ISSET("Locker", env->env_lref);
diff -r db78da0996b1 src/os/os_addrinfo.c diff -r db78da0996b1 src/os/os_addrinfo.c
--- a/src/os/os_addrinfo.c Mon Sep 09 11:09:35 2013 -0400 --- a/src/os/os_addrinfo.c Mon Sep 09 11:09:35 2013 -0400
+++ b/src/os/os_addrinfo.c Sat Apr 29 04:10:18 2017 -0700 +++ b/src/os/os_addrinfo.c Sat Apr 29 04:10:18 2017 -0700
@ -947,8 +500,8 @@ diff -r db78da0996b1 src/os/os_flock.c
+} +}
#endif #endif
-} -}
--- db-5.3.28/src/os/os_map.c.pthreads 2013-09-09 17:35:09.000000000 +0200 --- b/src/os/os_map.c.pthreads 2013-09-09 17:35:09.000000000 +0200
+++ db-5.3.28/src/os/os_map.c 2017-05-16 09:31:30.535713279 +0200 +++ b/src/os/os_map.c 2017-05-16 09:31:30.535713279 +0200
@@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
/* /*
@ -966,3 +519,291 @@ diff -r db78da0996b1 src/os/os_flock.c
dbenv = env->dbenv; dbenv = env->dbenv;
if (DB_GLOBAL(j_region_map) != NULL) { if (DB_GLOBAL(j_region_map) != NULL) {
--- b/src/env/env_region.c.pthreads 2013-09-09 17:35:08.000000000 +0200
+++ b/src/env/env_region.c 2017-05-22 13:18:21.898595006 +0200
@@ -18,13 +18,49 @@
static int __env_faultmem __P((ENV *, void *, size_t, int));
static int __env_sys_attach __P((ENV *, REGINFO *, REGION *));
static int __env_sys_detach __P((ENV *, REGINFO *, int));
+static int __env_check_recreate __P((ENV *, REGENV *, u_int32_t));
static void __env_des_destroy __P((ENV *, REGION *));
static void __env_remove_file __P((ENV *));
+
+/*
+ * If the system supports flock()-like file locking, then the primary region
+ * file __db.001 is exclusively locked during creation, and is read-locked while
+ * the environment is open. Most Unix-like systems have flock(), with the
+ * notable exception of Solaris.
+ * Note: fcntl cannot be used for this locking because of the unfortunate
+ * definition of its interaction with close(2). A process's fcntl locks are
+ * released whenever it closes any file descriptor for that file. So, if an
+ * environment is opened more than once, closing one of the DB_ENV handles would
+ * release the read lock that protects the other handle.
+ */
+#ifdef HAVE_FLOCK
+#define ENV_PRIMARY_LOCK(env, lockmode, async) \
+ ((env)->lockfhp == NULL ? 0 : \
+ __os_fdlock((env), (env)->lockfhp, -1, lockmode, async))
+#define ENV_PRIMARY_UNLOCK(env) \
+ ((env)->lockfhp == NULL ? 0 : \
+ __os_fdlock((env), (env)->lockfhp, -1, DB_LOCK_NG, 0))
+#else
+#define ENV_PRIMARY_LOCK(env, lockmode, async) (0)
+#define ENV_PRIMARY_UNLOCK(env) (0)
+#endif
+
/*
* __env_attach
* Join/create the environment
*
+ * Safely detecting and managing multiple processes' environment handles:
+ * BDB uses a shared or exclusive fcntl()-style lock on the first byte
+ * of the primary region file (__db.001) to detect whether other processes
+ * have the environment open, and to single-thread attempts to create the
+ * environment. If the open includes DB_CREATE, an exclusive lock is
+ * obtained during the open call. After the creation is finished, and
+ * anytime during a non-DB_CREATE env open, the process holds a shared
+ * lock.
+ * - single-thread creation of the environment
+ * - detect whether any other processes are currently attached to it.
+ *
* PUBLIC: int __env_attach __P((ENV *, u_int32_t *, int, int));
*/
int
@@ -122,7 +158,11 @@
if ((ret = __os_open(
env, infop->name, 0, DB_OSO_REGION, 0, &env->lockfhp)) != 0)
goto err;
-
+ /* Wait to get shared access to the primary region. */
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_READ, 0)) != 0) {
+ __db_err(env, ret, "__env_attach: existing: shared lock error");
+ goto err;
+ }
/*
* !!!
* The region may be in system memory not backed by the filesystem
@@ -218,11 +258,10 @@
segid = ref.segid;
}
-#ifndef HAVE_MUTEX_FCNTL
+#if !defined(HAVE_FCNTL) && !defined(HAVE_PTHREADS_TIMESTAMP)
/*
- * If we're not doing fcntl locking, we can close the file handle. We
- * no longer need it and the less contact between the buffer cache and
- * the VM, the better.
+ * Without fcntl-like support, we no longer need the file handle. Close
+ * it to limit the interaction between the buffer cache and the VM.
*/
(void)__os_closehandle(env, env->lockfhp);
env->lockfhp = NULL;
@@ -233,6 +272,8 @@
tregion.size = (roff_t)size;
tregion.max = (roff_t)max;
tregion.segid = segid;
+ /* Attach to the existing primary region. */
+ /* The leaking db.001 gets open inside of here, in __os_attach(). */
if ((ret = __env_sys_attach(env, infop, &tregion)) != 0)
goto err;
@@ -246,20 +287,38 @@
infop->head = (u_int8_t *)infop->addr + sizeof(REGENV);
renv = infop->primary;
- /*
- * Make sure the region matches our build. Special case a region
- * that's all nul bytes, just treat it like any other corruption.
- */
+ if (create_ok &&
+ __env_check_recreate(env, renv, signature) == DB_OLD_VERSION &&
+ (ret = ENV_PRIMARY_LOCK(env, DB_LOCK_WRITE, 1)) == 0) {
+ if (FLD_ISSET(dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env, "Recreating idle environment");
+ F_SET(infop, REGION_CREATE_OK);
+
+ /*
+ * Detach from the environment region; we need to unmap it (and
+ * close any file handle) so that we don't leak memory or files.
+ */
+ DB_ASSERT(env, infop->rp == NULL);
+ infop->rp = &tregion;
+ (void)__env_sys_detach(env, infop, 0);
+ goto creation;
+ }
+
if (renv->majver != DB_VERSION_MAJOR ||
renv->minver != DB_VERSION_MINOR) {
- if (renv->majver != 0 || renv->minver != 0) {
+ /*
+ * Special case a region that's all nul bytes, just treat it
+ * like any other corruption.
+ */
+ if (renv->majver == 0 && renv->minver == 0)
+ ret = EINVAL;
+ else {
__db_errx(env, DB_STR_A("1538",
- "Program version %d.%d doesn't match environment version %d.%d",
+ "Program version %d.%d doesn't match in-use environment version %d.%d",
"%d %d %d %d"), DB_VERSION_MAJOR, DB_VERSION_MINOR,
renv->majver, renv->minver);
ret = DB_VERSION_MISMATCH;
- } else
- ret = EINVAL;
+ }
goto err;
}
if (renv->signature != signature) {
@@ -289,6 +348,18 @@
}
if (renv->magic != DB_REGION_MAGIC)
goto retry;
+ /*
+ * A bad magic number means that the env is new and not yet available:
+ * wait a while and try again. If the magic number says recovery is in
+ * process, remember the env creation time to record that recovery was
+ * the reason that the open failed.
+ */
+ if (renv->magic != DB_REGION_MAGIC) {
+ __db_msg(env, "attach sees bad region magic 0x%lx",
+ (u_long)renv->magic);
+ goto retry;
+ }
+
/*
* Get a reference to the underlying REGION information for this
@@ -346,6 +417,12 @@
return (0);
creation:
+ /* Should this wait for the lock (passing 0 instead of 1)? */
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_WRITE, 1)) != 0) {
+ __db_err(env, ret, "__env_attach: creation could not lock %s",
+ env->lockfhp->name);
+ goto err;
+ }
/* Create the environment region. */
F_SET(infop, REGION_CREATE);
@@ -437,7 +514,14 @@
renv->minver = (u_int32_t)minver;
renv->patchver = (u_int32_t)patchver;
renv->signature = signature;
-
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ renv->pthreads_timestamp = __os_pthreads_timestamp(env);
+ {
+ char *s = getenv("TS_ADJUST");
+ if (s != NULL)
+ renv->pthreads_timestamp -= atoi(s);
+ }
+#endif
(void)time(&renv->timestamp);
__os_unique_id(env, &renv->envid);
@@ -513,16 +597,24 @@
}
}
-#ifndef HAVE_MUTEX_FCNTL
- /*
- * If we're not doing fcntl locking, we can close the file handle. We
- * no longer need it and the less contact between the buffer cache and
- * the VM, the better.
- */
+#ifdef HAVE_FCNTL
+ if ((ret = ENV_PRIMARY_UNLOCK(env)) != 0) {
+ __db_err(env, ret, "__env_attach: release exclusive lock");
+ goto err;
+ }
+ if ((ret = ENV_PRIMARY_LOCK(env, DB_LOCK_READ, 0)) != 0) {
+ __db_err(env, ret, "__env_attach: new: acquire shared lock");
+ goto err;
+ }
+#else
+ /*
+ * We no longer need the primary region file's handle and the less
+ * contact between the buffer cache and the VM, the better.
+ */
if (env->lockfhp != NULL) {
(void)__os_closehandle(env, env->lockfhp);
env->lockfhp = NULL;
- }
+ }
#endif
/* Everything looks good, we're done. */
@@ -562,9 +654,9 @@
/* If we had a temporary error, wait awhile and try again. */
if (ret == 0) {
if (!retry_ok || ++retry_cnt > 3) {
+ ret = EAGAIN;
__db_errx(env, DB_STR("1546",
"unable to join the environment"));
- ret = EAGAIN;
} else {
__os_yield(env, retry_cnt * 3, 0);
goto loop;
@@ -575,6 +667,59 @@
}
/*
+ * __env_check_recreate --
+ * Determine whether an existing on-disk environment should be recreated
+ * because it is not compatible with this compiled BDB library.
+ *
+ * Returns:
+ * 0 -
+ * The env was generated by this library. No recreation needed.
+ * DB_OLD_VERSION -
+ * It was created by an earlier BDB version, or by an earlier
+ * version of libpthreads (on certain Linux systems). The caller
+ * will try to recreate it with the currently configured settings.
+ * DB_VERSION_MISMATCH -
+ * It was created by a newer version of BDB. Do not attempt to
+ * fix it, something is probably wrong with the application setup.
+ */
+static int
+__env_check_recreate(env, renv, signature)
+ ENV *env;
+ REGENV *renv;
+ u_int32_t signature;
+{
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ time_t pthreads_time;
+ char envtime[CTIME_BUFLEN], libtime[CTIME_BUFLEN];
+#endif
+
+ /* First, bail out if the env is too new for this code to handle. */
+ if (renv->majver > DB_VERSION_MAJOR ||
+ (renv->majver == DB_VERSION_MAJOR &&
+ renv->minver > DB_VERSION_MINOR))
+ return (DB_VERSION_MISMATCH);
+
+#ifdef HAVE_PTHREADS_TIMESTAMP
+ pthreads_time = __os_pthreads_timestamp(env);
+ if (pthreads_time != renv->pthreads_timestamp) {
+ if (FLD_ISSET(env->dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env,
+ "Pthreads timestamp changed: env %.24s current %.24s",
+ __os_ctime(&renv->pthreads_timestamp, envtime),
+ __os_ctime(&pthreads_time, libtime));
+ return (DB_OLD_VERSION);
+ }
+#endif
+ if (renv->signature != signature || renv->majver != DB_VERSION_MAJOR ||
+ renv->minver != DB_VERSION_MINOR) {
+ if (FLD_ISSET(env->dbenv->verbose, DB_VERB_RECOVERY))
+ __db_msg(env, "Signature or version changed");
+ return (DB_OLD_VERSION);
+ }
+ return (0);
+}
+
+/*
* __env_turn_on --
* Turn on the created environment.
*

View File

@ -4,7 +4,7 @@
Summary: The Berkeley DB database library for C Summary: The Berkeley DB database library for C
Name: libdb Name: libdb
Version: 5.3.28 Version: 5.3.28
Release: 18%{?dist} Release: 19%{?dist}
Source0: http://download.oracle.com/berkeley-db/db-%{version}.tar.gz Source0: http://download.oracle.com/berkeley-db/db-%{version}.tar.gz
Source1: http://download.oracle.com/berkeley-db/db.1.85.tar.gz Source1: http://download.oracle.com/berkeley-db/db.1.85.tar.gz
# For mt19937db.c # For mt19937db.c
@ -32,7 +32,8 @@ Patch27: db-5.3.21-memp_stat-upstream-fix.patch
Patch28: db-5.3.21-mutex_leak.patch Patch28: db-5.3.21-mutex_leak.patch
# fix for overflowing hash variable inside bundled lemon # fix for overflowing hash variable inside bundled lemon
Patch29: db-5.3.28-lemon_hash.patch Patch29: db-5.3.28-lemon_hash.patch
# fix for erroneous assumption about condition variable layout (rhbz#1394862) # upstream patch adding the ability to recreate libdb's environment on version mismatch
# or when libpthread.so is modified (rhbz#1394862)
Patch30: db-5.3.28-condition_variable.patch Patch30: db-5.3.28-condition_variable.patch
URL: http://www.oracle.com/database/berkeley-db/ URL: http://www.oracle.com/database/berkeley-db/
@ -208,25 +209,25 @@ for building programs which use the Berkeley DB in Java.
cp %{SOURCE2} . cp %{SOURCE2} .
tar -xf %{SOURCE3} tar -xf %{SOURCE3}
%patch0 -p1 -b .multiarch %patch0 -p1
pushd db.1.85/PORT/linux pushd db.1.85/PORT/linux
%patch10 -p0 -b .1.1 %patch10 -p0
popd popd
pushd db.1.85 pushd db.1.85
%patch11 -p0 -b .1.2 %patch11 -p0
%patch12 -p0 -b .1.3 %patch12 -p0
%patch13 -p0 -b .1.4 %patch13 -p0
%patch20 -p1 -b .errno %patch20 -p1
popd popd
%patch22 -p1 -b .185compat %patch22 -p1
%patch24 -p1 -b .4.5.20.jni %patch24 -p1
%patch25 -p1 -b .licensefix %patch25 -p1
%patch26 -p1 -b .java8-fix %patch26 -p1
%patch27 -p1 -b .memp_stat-fix %patch27 -p1
%patch28 -p1 -b .mutex_leak %patch28 -p1
%patch29 -p1 -b .lemon_hash %patch29 -p1
%patch30 -p1 -b .condition_variable %patch30 -p1
cd dist cd dist
./s_config ./s_config
@ -435,6 +436,9 @@ rm -rf ${RPM_BUILD_ROOT}
%{_libdir}/libdb_java.so %{_libdir}/libdb_java.so
%changelog %changelog
* Wed May 24 2017 Petr Kubat <pkubat@redhat.com> - 5.3.28-19
- Fix some issues present in the upstream patch for rhbz#1394862
* Tue May 23 2017 Adam Williamson <awilliam@redhat.com> - 5.3.28-18 * Tue May 23 2017 Adam Williamson <awilliam@redhat.com> - 5.3.28-18
- Fix issue causing RPM to hang when glibc/libpthread change (#1394862) - Fix issue causing RPM to hang when glibc/libpthread change (#1394862)