1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

lib: Move the "expired" for gencache_parse calculation into gencache.c

Make it more robust

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>

Autobuild-User(master): Jeremy Allison <jra@samba.org>
Autobuild-Date(master): Tue Oct 16 21:20:19 CEST 2018 on sn-devel-144
This commit is contained in:
Volker Lendecke 2018-10-13 13:41:59 +02:00 committed by Jeremy Allison
parent fdb50817ca
commit 2557ae53ed
7 changed files with 60 additions and 26 deletions

View File

@ -42,6 +42,14 @@ static struct tdb_wrap *cache_notrans;
*
**/
struct gencache_timeout {
time_t timeout;
};
bool gencache_timeout_expired(const struct gencache_timeout *t)
{
return t->timeout <= time(NULL);
}
/**
* Cache initialisation function. Opens cache tdb file or creates
@ -131,7 +139,8 @@ struct gencache_have_val_state {
bool gotit;
};
static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
static void gencache_have_val_parser(const struct gencache_timeout *old_timeout,
DATA_BLOB data,
void *private_data)
{
struct gencache_have_val_state *state =
@ -146,7 +155,7 @@ static void gencache_have_val_parser(time_t old_timeout, DATA_BLOB data,
* value, just extending the remaining timeout by less than 10%.
*/
cache_time_left = old_timeout - now;
cache_time_left = old_timeout->timeout - now;
if (cache_time_left <= 0) {
/*
* timed out, write new value
@ -338,10 +347,11 @@ done:
return ret == 0;
}
static void gencache_del_parser(time_t timeout, DATA_BLOB blob,
static void gencache_del_parser(const struct gencache_timeout *t,
DATA_BLOB blob,
void *private_data)
{
if (timeout != 0) {
if (t->timeout != 0) {
bool *exists = private_data;
*exists = true;
}
@ -419,7 +429,9 @@ static bool gencache_pull_timeout(uint8_t *val, time_t *pres, char **payload)
}
struct gencache_parse_state {
void (*parser)(time_t timeout, DATA_BLOB blob, void *private_data);
void (*parser)(const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data);
void *private_data;
bool copy_to_notrans;
};
@ -428,21 +440,21 @@ static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
{
struct gencache_parse_state *state;
DATA_BLOB blob;
time_t t;
struct gencache_timeout t;
char *payload;
bool ret;
if (data.dptr == NULL) {
return -1;
}
ret = gencache_pull_timeout(data.dptr, &t, &payload);
ret = gencache_pull_timeout(data.dptr, &t.timeout, &payload);
if (!ret) {
return -1;
}
state = (struct gencache_parse_state *)private_data;
blob = data_blob_const(
payload, data.dsize - PTR_DIFF(payload, data.dptr));
state->parser(t, blob, state->private_data);
state->parser(&t, blob, state->private_data);
if (state->copy_to_notrans) {
tdb_store(cache_notrans->tdb, key, data, 0);
@ -452,7 +464,8 @@ static int gencache_parse_fn(TDB_DATA key, TDB_DATA data, void *private_data)
}
bool gencache_parse(const char *keystr,
void (*parser)(time_t timeout, DATA_BLOB blob,
void (*parser)(const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data),
void *private_data)
{
@ -511,17 +524,18 @@ struct gencache_get_data_blob_state {
bool result;
};
static void gencache_get_data_blob_parser(time_t timeout, DATA_BLOB blob,
static void gencache_get_data_blob_parser(const struct gencache_timeout *t,
DATA_BLOB blob,
void *private_data)
{
struct gencache_get_data_blob_state *state =
(struct gencache_get_data_blob_state *)private_data;
if (timeout == 0) {
if (t->timeout == 0) {
state->result = false;
return;
}
state->timeout = timeout;
state->timeout = t->timeout;
if (state->blob == NULL) {
state->result = true;

View File

@ -32,8 +32,17 @@ bool gencache_set(const char *keystr, const char *value, time_t timeout);
bool gencache_del(const char *keystr);
bool gencache_get(const char *keystr, TALLOC_CTX *mem_ctx, char **value,
time_t *ptimeout);
/*
* This might look like overkill, but namemap_cache.c shows it's
* necessary :-)
*/
struct gencache_timeout;
bool gencache_timeout_expired(const struct gencache_timeout *t);
bool gencache_parse(const char *keystr,
void (*parser)(time_t timeout, DATA_BLOB blob,
void (*parser)(const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data),
void *private_data);
bool gencache_get_data_blob(const char *keystr, TALLOC_CTX *mem_ctx,

View File

@ -194,7 +194,8 @@ struct idmap_cache_xid2sid_state {
bool ret;
};
static void idmap_cache_xid2sid_parser(time_t timeout, DATA_BLOB blob,
static void idmap_cache_xid2sid_parser(const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data)
{
struct idmap_cache_xid2sid_state *state =
@ -217,7 +218,7 @@ static void idmap_cache_xid2sid_parser(time_t timeout, DATA_BLOB blob,
state->ret = string_to_sid(state->sid, value);
}
if (state->ret) {
*state->expired = (timeout <= time(NULL));
*state->expired = gencache_timeout_expired(timeout);
}
}

View File

@ -94,8 +94,10 @@ struct namemap_cache_find_sid_state {
bool ok;
};
static void namemap_cache_find_sid_parser(time_t timeout, DATA_BLOB blob,
void *private_data)
static void namemap_cache_find_sid_parser(
const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data)
{
struct namemap_cache_find_sid_state *state = private_data;
const char *strv = (const char *)blob.data;
@ -132,7 +134,7 @@ static void namemap_cache_find_sid_parser(time_t timeout, DATA_BLOB blob,
state->fn(domain,
name,
(enum lsa_SidType)type,
timeout <= time(NULL),
gencache_timeout_expired(timeout),
state->private_data);
state->ok = true;
@ -241,8 +243,10 @@ struct namemap_cache_find_name_state {
bool ok;
};
static void namemap_cache_find_name_parser(time_t timeout, DATA_BLOB blob,
void *private_data)
static void namemap_cache_find_name_parser(
const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data)
{
struct namemap_cache_find_name_state *state = private_data;
const char *strv = (const char *)blob.data;
@ -284,7 +288,7 @@ static void namemap_cache_find_name_parser(time_t timeout, DATA_BLOB blob,
state->fn(&sid,
(enum lsa_SidType)type,
timeout <= time(NULL),
gencache_timeout_expired(timeout),
state->private_data);
state->ok = true;

View File

@ -1242,12 +1242,14 @@ struct ra_parser_state {
enum remote_arch_types ra;
};
static void ra_parser(time_t timeout, DATA_BLOB blob, void *priv_data)
static void ra_parser(const struct gencache_timeout *t,
DATA_BLOB blob,
void *priv_data)
{
struct ra_parser_state *state = (struct ra_parser_state *)priv_data;
const char *ra_str = NULL;
if (timeout <= time(NULL)) {
if (gencache_timeout_expired(t)) {
return;
}

View File

@ -10238,7 +10238,9 @@ static bool run_local_base64(int dummy)
return ret;
}
static void parse_fn(time_t timeout, DATA_BLOB blob, void *private_data)
static void parse_fn(const struct gencache_timeout *t,
DATA_BLOB blob,
void *private_data)
{
return;
}

View File

@ -171,12 +171,14 @@ struct dcinfo_parser_state {
struct netr_DsRGetDCNameInfo *dcinfo;
};
static void dcinfo_parser(time_t timeout, DATA_BLOB blob, void *private_data)
static void dcinfo_parser(const struct gencache_timeout *timeout,
DATA_BLOB blob,
void *private_data)
{
struct dcinfo_parser_state *state = private_data;
enum ndr_err_code ndr_err;
if (timeout <= time(NULL)) {
if (gencache_timeout_expired(timeout)) {
return;
}