Blame SOURCES/0005-tdb-Test-for-readonly-lock-upgrade-bug.patch

fd591b
From 397d1936ec34fa573c7d46cc51c971986e8f7fe8 Mon Sep 17 00:00:00 2001
fd591b
From: Volker Lendecke <vl@samba.org>
fd591b
Date: Tue, 8 Nov 2016 17:01:56 +0100
fd591b
Subject: [PATCH] tdb: Test for readonly lock upgrade bug
fd591b
fd591b
Signed-off-by: Volker Lendecke <vl@samba.org>
fd591b
Reviewed-by: Jeremy Allison <jra@samba.org>
fd591b
fd591b
Autobuild-User(master): Jeremy Allison <jra@samba.org>
fd591b
Autobuild-Date(master): Tue Apr 11 00:33:31 CEST 2017 on sn-devel-144
fd591b
---
fd591b
 lib/tdb/test/run-rdlock-upgrade.c | 166 ++++++++++++++++++++++++++++++++++++++
fd591b
 lib/tdb/wscript                   |   1 +
fd591b
 2 files changed, 167 insertions(+)
fd591b
 create mode 100644 lib/tdb/test/run-rdlock-upgrade.c
fd591b
fd591b
diff --git a/lib/tdb/test/run-rdlock-upgrade.c b/lib/tdb/test/run-rdlock-upgrade.c
fd591b
new file mode 100644
fd591b
index 0000000000000000000000000000000000000000..042001b4231530e93618f481d3fbeef4ecfd309b
fd591b
--- /dev/null
fd591b
+++ b/lib/tdb/test/run-rdlock-upgrade.c
fd591b
@@ -0,0 +1,166 @@
fd591b
+#include "../common/io.c"
fd591b
+#include "../common/tdb.c"
fd591b
+#include "../common/lock.c"
fd591b
+#include "../common/freelist.c"
fd591b
+#include "../common/traverse.c"
fd591b
+#include "../common/transaction.c"
fd591b
+#include "../common/error.c"
fd591b
+#include "../common/open.c"
fd591b
+#include "../common/check.c"
fd591b
+#include "../common/hash.c"
fd591b
+#include "../common/mutex.c"
fd591b
+#include "tap-interface.h"
fd591b
+#include <stdlib.h>
fd591b
+#include <sys/types.h>
fd591b
+#include <sys/wait.h>
fd591b
+#include <stdarg.h>
fd591b
+#include "logging.h"
fd591b
+
fd591b
+static TDB_DATA key, data;
fd591b
+
fd591b
+static void do_chainlock(const char *name, int tdb_flags, int up, int down)
fd591b
+{
fd591b
+	struct tdb_context *tdb;
fd591b
+	int ret;
fd591b
+	ssize_t nread, nwritten;
fd591b
+	char c = 0;
fd591b
+
fd591b
+	tdb = tdb_open_ex(name, 3, tdb_flags,
fd591b
+			  O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
fd591b
+	ok(tdb, "tdb_open_ex should succeed");
fd591b
+
fd591b
+	ret = tdb_chainlock_read(tdb, key);
fd591b
+	ok(ret == 0, "tdb_chainlock_read should succeed");
fd591b
+
fd591b
+	nwritten = write(up, &c, sizeof(c));
fd591b
+	ok(nwritten == sizeof(c), "write should succeed");
fd591b
+
fd591b
+	nread = read(down, &c, sizeof(c));
fd591b
+	ok(nread == 0, "read should succeed");
fd591b
+
fd591b
+	exit(0);
fd591b
+}
fd591b
+
fd591b
+static void do_trylock(const char *name, int tdb_flags, int up, int down)
fd591b
+{
fd591b
+	struct tdb_context *tdb;
fd591b
+	int ret;
fd591b
+	ssize_t nread, nwritten;
fd591b
+	char c = 0;
fd591b
+
fd591b
+	tdb = tdb_open_ex(name, 3, tdb_flags,
fd591b
+			  O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
fd591b
+	ok(tdb, "tdb_open_ex should succeed");
fd591b
+
fd591b
+	/*
fd591b
+	 * tdb used to have a bug where with fcntl locks an upgrade
fd591b
+	 * from a readlock to writelock did not check for the
fd591b
+	 * underlying fcntl lock. Mutexes don't distinguish between
fd591b
+	 * readlocks and writelocks, so that bug does not apply here.
fd591b
+	 */
fd591b
+
fd591b
+	ret = tdb_chainlock_read(tdb, key);
fd591b
+	ok(ret == 0, "tdb_chainlock_read should succeed");
fd591b
+
fd591b
+	ret = tdb_chainlock_nonblock(tdb, key);
fd591b
+	ok(ret == -1, "tdb_chainlock_nonblock should fail");
fd591b
+
fd591b
+	nwritten = write(up, &c, sizeof(c));
fd591b
+	ok(nwritten == sizeof(c), "write should succeed");
fd591b
+
fd591b
+	nread = read(down, &c, sizeof(c));
fd591b
+	ok(nread == 0, "read should succeed");
fd591b
+
fd591b
+	exit(0);
fd591b
+}
fd591b
+
fd591b
+static int do_tests(const char *name, int tdb_flags)
fd591b
+{
fd591b
+	int ret;
fd591b
+	pid_t chainlock_child, store_child;
fd591b
+	int chainlock_down[2];
fd591b
+	int chainlock_up[2];
fd591b
+	int store_down[2];
fd591b
+	int store_up[2];
fd591b
+	char c;
fd591b
+	ssize_t nread;
fd591b
+
fd591b
+	key.dsize = strlen("hi");
fd591b
+	key.dptr = discard_const_p(uint8_t, "hi");
fd591b
+	data.dsize = strlen("world");
fd591b
+	data.dptr = discard_const_p(uint8_t, "world");
fd591b
+
fd591b
+	ret = pipe(chainlock_down);
fd591b
+	ok(ret == 0, "pipe should succeed");
fd591b
+
fd591b
+	ret = pipe(chainlock_up);
fd591b
+	ok(ret == 0, "pipe should succeed");
fd591b
+
fd591b
+	ret = pipe(store_down);
fd591b
+	ok(ret == 0, "pipe should succeed");
fd591b
+
fd591b
+	ret = pipe(store_up);
fd591b
+	ok(ret == 0, "pipe should succeed");
fd591b
+
fd591b
+	chainlock_child = fork();
fd591b
+	ok(chainlock_child != -1, "fork should succeed");
fd591b
+
fd591b
+	if (chainlock_child == 0) {
fd591b
+		close(chainlock_up[0]);
fd591b
+		close(chainlock_down[1]);
fd591b
+		close(store_up[0]);
fd591b
+		close(store_up[1]);
fd591b
+		close(store_down[0]);
fd591b
+		close(store_down[1]);
fd591b
+		do_chainlock(name, tdb_flags,
fd591b
+			     chainlock_up[1], chainlock_down[0]);
fd591b
+		exit(0);
fd591b
+	}
fd591b
+	close(chainlock_up[1]);
fd591b
+	close(chainlock_down[0]);
fd591b
+
fd591b
+	nread = read(chainlock_up[0], &c, sizeof(c));
fd591b
+	ok(nread == sizeof(c), "read should succeed");
fd591b
+
fd591b
+	/*
fd591b
+	 * Now we have a process holding a chain read lock. Start
fd591b
+	 * another process trying to write lock. This should fail.
fd591b
+	 */
fd591b
+
fd591b
+	store_child = fork();
fd591b
+	ok(store_child != -1, "fork should succeed");
fd591b
+
fd591b
+	if (store_child == 0) {
fd591b
+		close(chainlock_up[0]);
fd591b
+		close(chainlock_down[1]);
fd591b
+		close(store_up[0]);
fd591b
+		close(store_down[1]);
fd591b
+		do_trylock(name, tdb_flags,
fd591b
+			   store_up[1], store_down[0]);
fd591b
+		exit(0);
fd591b
+	}
fd591b
+	close(store_up[1]);
fd591b
+	close(store_down[0]);
fd591b
+
fd591b
+	nread = read(store_up[0], &c, sizeof(c));
fd591b
+	ok(nread == sizeof(c), "read should succeed");
fd591b
+
fd591b
+	close(chainlock_up[0]);
fd591b
+	close(chainlock_down[1]);
fd591b
+	close(store_up[0]);
fd591b
+	close(store_down[1]);
fd591b
+	diag("%s tests done", name);
fd591b
+	return exit_status();
fd591b
+}
fd591b
+
fd591b
+int main(int argc, char *argv[])
fd591b
+{
fd591b
+	int ret;
fd591b
+
fd591b
+	ret = do_tests("rdlock-upgrade.tdb",
fd591b
+		       TDB_CLEAR_IF_FIRST |
fd591b
+		       TDB_INCOMPATIBLE_HASH);
fd591b
+	ok(ret == 0, "rdlock-upgrade.tdb tests should succeed");
fd591b
+
fd591b
+	return exit_status();
fd591b
+}
fd591b
diff --git a/lib/tdb/wscript b/lib/tdb/wscript
fd591b
index 0d682ebb4cdaa001904e0b40e50e1a2e46f230ee..693787cb75da6462a81dcae6fba8981b99e90e69 100644
fd591b
--- a/lib/tdb/wscript
fd591b
+++ b/lib/tdb/wscript
fd591b
@@ -34,6 +34,7 @@ tdb1_unit_tests = [
fd591b
     'run-readonly-check',
fd591b
     'run-rescue',
fd591b
     'run-rescue-find_entry',
fd591b
+    'run-rdlock-upgrade',
fd591b
     'run-rwlock-check',
fd591b
     'run-summary',
fd591b
     'run-transaction-expand',
fd591b
-- 
fd591b
2.9.3
fd591b