Commit 937d0bab authored by Rusty Russell's avatar Rusty Russell

tdb2: save openhook, allow tdb_get_attribute() on it.

This makes it easy to call it again after a fork(), such as for
re-establishing the CLEAR_IF_FIRST files locks.
parent b87e1449
...@@ -270,11 +270,11 @@ enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb, ...@@ -270,11 +270,11 @@ enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
attr->seed.seed = tdb->hash_seed; attr->seed.seed = tdb->hash_seed;
break; break;
case TDB_ATTRIBUTE_OPENHOOK: case TDB_ATTRIBUTE_OPENHOOK:
return tdb->last_error if (!tdb->openhook)
= tdb_logerr(tdb, TDB_ERR_EINVAL, return tdb->last_error = TDB_ERR_NOEXIST;
TDB_LOG_USE_ERROR, attr->openhook.fn = tdb->openhook;
"tdb_get_attribute:" attr->openhook.data = tdb->openhook_data;
" cannot get TDB_ATTRIBUTE_OPENHOOK"); break;
case TDB_ATTRIBUTE_STATS: { case TDB_ATTRIBUTE_STATS: {
size_t size = attr->stats.size; size_t size = attr->stats.size;
if (size > tdb->stats.size) if (size > tdb->stats.size)
...@@ -306,16 +306,16 @@ void tdb_unset_attribute(struct tdb_context *tdb, ...@@ -306,16 +306,16 @@ void tdb_unset_attribute(struct tdb_context *tdb,
case TDB_ATTRIBUTE_LOG: case TDB_ATTRIBUTE_LOG:
tdb->log_fn = NULL; tdb->log_fn = NULL;
break; break;
case TDB_ATTRIBUTE_OPENHOOK:
tdb->openhook = NULL;
break;
case TDB_ATTRIBUTE_HASH: case TDB_ATTRIBUTE_HASH:
case TDB_ATTRIBUTE_SEED: case TDB_ATTRIBUTE_SEED:
case TDB_ATTRIBUTE_OPENHOOK:
tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR, tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
"tdb_unset_attribute: cannot unset %s after opening", "tdb_unset_attribute: cannot unset %s after opening",
type == TDB_ATTRIBUTE_HASH type == TDB_ATTRIBUTE_HASH
? "TDB_ATTRIBUTE_HASH" ? "TDB_ATTRIBUTE_HASH"
: type == TDB_ATTRIBUTE_SEED : "TDB_ATTRIBUTE_SEED");
? "TDB_ATTRIBUTE_SEED"
: "TDB_ATTRIBUTE_OPENHOOK");
break; break;
case TDB_ATTRIBUTE_STATS: case TDB_ATTRIBUTE_STATS:
tdb_logerr(tdb, TDB_ERR_EINVAL, tdb_logerr(tdb, TDB_ERR_EINVAL,
...@@ -347,7 +347,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -347,7 +347,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
ssize_t rlen; ssize_t rlen;
struct tdb_header hdr; struct tdb_header hdr;
struct tdb_attribute_seed *seed = NULL; struct tdb_attribute_seed *seed = NULL;
struct tdb_attribute_openhook *openhook = NULL;
tdb_bool_err berr; tdb_bool_err berr;
enum TDB_ERROR ecode; enum TDB_ERROR ecode;
int openlock; int openlock;
...@@ -372,6 +371,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -372,6 +371,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb->open_flags = open_flags; tdb->open_flags = open_flags;
tdb->last_error = TDB_SUCCESS; tdb->last_error = TDB_SUCCESS;
tdb->file = NULL; tdb->file = NULL;
tdb->openhook = NULL;
tdb->lock_fn = tdb_fcntl_lock; tdb->lock_fn = tdb_fcntl_lock;
tdb->unlock_fn = tdb_fcntl_unlock; tdb->unlock_fn = tdb_fcntl_unlock;
tdb->hash_fn = jenkins_hash; tdb->hash_fn = jenkins_hash;
...@@ -390,7 +390,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -390,7 +390,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
seed = &attr->seed; seed = &attr->seed;
break; break;
case TDB_ATTRIBUTE_OPENHOOK: case TDB_ATTRIBUTE_OPENHOOK:
openhook = &attr->openhook; tdb->openhook = attr->openhook.fn;
tdb->openhook_data = attr->openhook.data;
break; break;
default: default:
/* These are set as normal. */ /* These are set as normal. */
...@@ -498,8 +499,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ...@@ -498,8 +499,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
} }
/* call their open hook if they gave us one. */ /* call their open hook if they gave us one. */
if (openhook) { if (tdb->openhook) {
ecode = openhook->fn(tdb->file->fd, openhook->data); ecode = tdb->openhook(tdb->file->fd, tdb->openhook_data);
if (ecode != TDB_SUCCESS) { if (ecode != TDB_SUCCESS) {
tdb_logerr(tdb, ecode, TDB_LOG_ERROR, tdb_logerr(tdb, ecode, TDB_LOG_ERROR,
"tdb_open: open hook failed"); "tdb_open: open hook failed");
......
...@@ -362,6 +362,10 @@ struct tdb_context { ...@@ -362,6 +362,10 @@ struct tdb_context {
tdb_off_t ftable_off; tdb_off_t ftable_off;
unsigned int ftable; unsigned int ftable;
/* Our open hook, if any. */
enum TDB_ERROR (*openhook)(int fd, void *data);
void *openhook_data;
/* IO methods: changes for transactions. */ /* IO methods: changes for transactions. */
const struct tdb_methods *methods; const struct tdb_methods *methods;
......
...@@ -617,8 +617,6 @@ enum tdb_attribute_type { ...@@ -617,8 +617,6 @@ enum tdb_attribute_type {
* This gets an attribute from a TDB which has previously been set (or * This gets an attribute from a TDB which has previously been set (or
* may return the default values). Set @attr.base.attr to the * may return the default values). Set @attr.base.attr to the
* attribute type you want get. * attribute type you want get.
*
* Currently this does not work for TDB_ATTRIBUTE_OPENHOOK.
*/ */
enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb, enum TDB_ERROR tdb_get_attribute(struct tdb_context *tdb,
union tdb_attribute *attr); union tdb_attribute *attr);
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
/* FIXME: Check these! */ /* FIXME: Check these! */
#define INITIAL_TDB_MALLOC "open.c", 355, FAILTEST_MALLOC #define INITIAL_TDB_MALLOC "open.c", 354, FAILTEST_MALLOC
#define URANDOM_OPEN "open.c", 62, FAILTEST_OPEN #define URANDOM_OPEN "open.c", 62, FAILTEST_OPEN
#define URANDOM_READ "open.c", 42, FAILTEST_READ #define URANDOM_READ "open.c", 42, FAILTEST_READ
......
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