Commit 2cad8789 authored by Rusty Russell's avatar Rusty Russell

Fix traverse nesting unlock bug.

parent e84f843d
#define _XOPEN_SOURCE 500
#include "tdb/tdb.h"
#include "tdb/io.c"
#include "tdb/tdb.c"
#include "tdb/lock.c"
#include "tdb/freelist.c"
#include "tdb/traverse.c"
#include "tdb/transaction.c"
#include "tdb/error.c"
#include "tdb/open.c"
#include "tap/tap.h"
#include <stdlib.h>
#include <stdbool.h>
#include <err.h>
static bool correct_key(TDB_DATA key)
{
return key.dsize == strlen("hi")
&& memcmp(key.dptr, "hi", key.dsize) == 0;
}
static bool correct_data(TDB_DATA data)
{
return data.dsize == strlen("world")
&& memcmp(data.dptr, "world", data.dsize) == 0;
}
static int traverse2(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *p)
{
ok1(correct_key(key));
ok1(correct_data(data));
return 0;
}
static int traverse1(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
void *p)
{
ok1(correct_key(key));
ok1(correct_data(data));
ok1(tdb->have_transaction_lock);
tdb_traverse(tdb, traverse2, NULL);
/* That should *not* release the transaction lock! */
ok1(tdb->have_transaction_lock);
return 0;
}
int main(int argc, char *argv[])
{
struct tdb_context *tdb;
TDB_DATA key, data;
plan_tests(14);
tdb = tdb_open("/tmp/test.tdb", 1024, TDB_CLEAR_IF_FIRST,
O_CREAT|O_TRUNC|O_RDWR, 0600);
ok1(tdb);
/* Tickle bug on appending zero length buffer to zero length buffer. */
key.dsize = strlen("hi");
key.dptr = (void *)"hi";
data.dptr = (void *)"world";
data.dsize = strlen("world");
ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
tdb_traverse(tdb, traverse1, NULL);
tdb_traverse_read(tdb, traverse1, NULL);
tdb_close(tdb);
return exit_status();
}
......@@ -182,6 +182,7 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
}
if (fn && fn(tdb, key, dbuf, private_data)) {
/* They want us to terminate traversal */
tdb_trace(tdb, "tdb_traverse_end = %i\n", count);
ret = count;
if (tdb_unlock_record(tdb, tl->off) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_traverse: unlock_record failed!\n"));;
......@@ -192,6 +193,7 @@ static int tdb_traverse_internal(struct tdb_context *tdb,
}
SAFE_FREE(key.dptr);
}
tdb_trace(tdb, "tdb_traverse_end\n");
out:
tdb->travlocks.next = tl->next;
if (ret < 0)
......@@ -212,17 +214,18 @@ int tdb_traverse_read(struct tdb_context *tdb,
/* we need to get a read lock on the transaction lock here to
cope with the lock ordering semantics of solaris10 */
if (tdb_transaction_lock(tdb, F_RDLCK)) {
if (tdb->traverse_read == 0 && tdb_transaction_lock(tdb, F_RDLCK)) {
return -1;
}
tdb->traverse_read++;
tdb_trace(tdb, "tdb_traverse_read_start\n");
ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
tdb_trace(tdb, "tdb_traverse_end = %i\n", ret);
tdb->traverse_read--;
tdb_transaction_unlock(tdb);
if (tdb->traverse_read == 0) {
tdb_transaction_unlock(tdb);
}
return ret;
}
......@@ -243,18 +246,20 @@ int tdb_traverse(struct tdb_context *tdb,
if (tdb->read_only || tdb->traverse_read) {
return tdb_traverse_read(tdb, fn, private_data);
}
if (tdb_transaction_lock(tdb, F_WRLCK)) {
/* Nested traversals: transaction lock doesn't nest. */
if (tdb->traverse_write == 0 && tdb_transaction_lock(tdb, F_WRLCK)) {
return -1;
}
tdb->traverse_write++;
tdb_trace(tdb, "tdb_traverse_start\n");
ret = tdb_traverse_internal(tdb, fn, private_data, &tl);
tdb_trace(tdb, "tdb_traverse_end = %i\n", ret);
tdb->traverse_write--;
tdb_transaction_unlock(tdb);
if (tdb->traverse_write == 0) {
tdb_transaction_unlock(tdb);
}
return ret;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment