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

ctdb-protocol: Fix marshalling for ctdb_g_lock

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2017-07-13 15:22:08 +10:00 committed by Martin Schwenke
parent a0bce37089
commit 0d5cc74a91
6 changed files with 88 additions and 37 deletions

View File

@ -64,9 +64,10 @@ void ctdb_server_id_push(struct ctdb_server_id *in, uint8_t *buf,
int ctdb_server_id_pull(uint8_t *buf, size_t buflen,
struct ctdb_server_id *out, size_t *npull);
size_t ctdb_g_lock_len(struct ctdb_g_lock *lock);
void ctdb_g_lock_push(struct ctdb_g_lock *lock, uint8_t *buf);
int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *lock);
size_t ctdb_g_lock_len(struct ctdb_g_lock *in);
void ctdb_g_lock_push(struct ctdb_g_lock *in, uint8_t *buf, size_t *npush);
int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *out,
size_t *npull);
size_t ctdb_g_lock_list_len(struct ctdb_g_lock_list *lock_list);
void ctdb_g_lock_list_push(struct ctdb_g_lock_list *lock_list, uint8_t *buf);

View File

@ -4924,23 +4924,65 @@ int ctdb_server_id_pull(uint8_t *buf, size_t buflen,
return 0;
}
size_t ctdb_g_lock_len(struct ctdb_g_lock *lock)
size_t ctdb_g_lock_len(struct ctdb_g_lock *in)
{
return sizeof(struct ctdb_g_lock);
return ctdb_uint32_len(&in->type) +
ctdb_padding_len(4) +
ctdb_server_id_len(&in->sid);
}
void ctdb_g_lock_push(struct ctdb_g_lock *lock, uint8_t *buf)
void ctdb_g_lock_push(struct ctdb_g_lock *in, uint8_t *buf, size_t *npush)
{
memcpy(buf, lock, sizeof(struct ctdb_g_lock));
size_t offset = 0, np;
uint32_t type;
type = in->type;
ctdb_uint32_push(&type, buf+offset, &np);
offset += np;
ctdb_padding_push(4, buf+offset, &np);
offset += np;
ctdb_server_id_push(&in->sid, buf+offset, &np);
offset += np;
*npush = offset;
}
int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *lock)
int ctdb_g_lock_pull(uint8_t *buf, size_t buflen, struct ctdb_g_lock *out,
size_t *npull)
{
if (buflen < sizeof(struct ctdb_g_lock)) {
return EMSGSIZE;
size_t offset = 0, np;
int ret;
uint32_t type;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &type, &np);
if (ret != 0) {
return ret;
}
offset += np;
if (type == 0) {
out->type = CTDB_G_LOCK_READ;
} else if (type == 1) {
out->type = CTDB_G_LOCK_WRITE;
} else {
return EPROTO;
}
memcpy(lock, buf, sizeof(struct ctdb_g_lock));
ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
if (ret != 0) {
return ret;
}
offset += np;
ret = ctdb_server_id_pull(buf+offset, buflen-offset, &out->sid, &np);
if (ret != 0) {
return ret;
}
offset += np;
*npull = offset;
return 0;
}
@ -4951,12 +4993,12 @@ size_t ctdb_g_lock_list_len(struct ctdb_g_lock_list *lock_list)
void ctdb_g_lock_list_push(struct ctdb_g_lock_list *lock_list, uint8_t *buf)
{
size_t offset = 0;
size_t offset = 0, np;
int i;
for (i=0; i<lock_list->num; i++) {
ctdb_g_lock_push(&lock_list->lock[i], &buf[offset]);
offset += sizeof(struct ctdb_g_lock);
ctdb_g_lock_push(&lock_list->lock[i], &buf[offset], &np);
offset += np;
}
}
@ -4965,7 +5007,7 @@ int ctdb_g_lock_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
{
struct ctdb_g_lock_list *lock_list;
unsigned count;
size_t offset;
size_t offset, np;
int ret, i;
lock_list = talloc_zero(mem_ctx, struct ctdb_g_lock_list);
@ -4983,12 +5025,12 @@ int ctdb_g_lock_list_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
offset = 0;
for (i=0; i<count; i++) {
ret = ctdb_g_lock_pull(&buf[offset], buflen-offset,
&lock_list->lock[i]);
&lock_list->lock[i], &np);
if (ret != 0) {
talloc_free(lock_list);
return ret;
}
offset += sizeof(struct ctdb_g_lock);
offset += np;
}
lock_list->num = count;

View File

@ -1349,7 +1349,7 @@ void verify_ctdb_server_id(struct ctdb_server_id *p1,
assert(p1->unique_id == p2->unique_id);
}
void fill_ctdb_g_lock(TALLOC_CTX *mem_ctx, struct ctdb_g_lock *p)
void fill_ctdb_g_lock(struct ctdb_g_lock *p)
{
p->type = rand_int(2);
fill_ctdb_server_id(&p->sid);
@ -1369,7 +1369,7 @@ void fill_ctdb_g_lock_list(TALLOC_CTX *mem_ctx, struct ctdb_g_lock_list *p)
p->lock = talloc_array(mem_ctx, struct ctdb_g_lock, p->num);
assert(p->lock != NULL);
for (i=0; i<p->num; i++) {
fill_ctdb_g_lock(mem_ctx, &p->lock[i]);
fill_ctdb_g_lock(&p->lock[i]);
}
}

View File

@ -362,7 +362,7 @@ void fill_ctdb_server_id(struct ctdb_server_id *p);
void verify_ctdb_server_id(struct ctdb_server_id *p1,
struct ctdb_server_id *p2);
void fill_ctdb_g_lock(TALLOC_CTX *mem_ctx, struct ctdb_g_lock *p);
void fill_ctdb_g_lock(struct ctdb_g_lock *p);
void verify_ctdb_g_lock(struct ctdb_g_lock *p1, struct ctdb_g_lock *p2);
void fill_ctdb_g_lock_list(TALLOC_CTX *mem_ctx, struct ctdb_g_lock_list *p);

View File

@ -2188,6 +2188,27 @@ static int ctdb_server_id_pull_old(uint8_t *buf, size_t buflen,
return 0;
}
static size_t ctdb_g_lock_len_old(struct ctdb_g_lock *in)
{
return sizeof(struct ctdb_g_lock);
}
static void ctdb_g_lock_push_old(struct ctdb_g_lock *in, uint8_t *buf)
{
memcpy(buf, in, sizeof(struct ctdb_g_lock));
}
static int ctdb_g_lock_pull_old(uint8_t *buf, size_t buflen,
struct ctdb_g_lock *out)
{
if (buflen < sizeof(struct ctdb_g_lock)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(struct ctdb_g_lock));
return 0;
}
COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
@ -2233,6 +2254,7 @@ COMPAT_TYPE3_TEST(struct ctdb_srvid_message, ctdb_srvid_message);
COMPAT_TYPE3_TEST(struct ctdb_disable_message, ctdb_disable_message);
COMPAT_TYPE1_TEST(struct ctdb_server_id, ctdb_server_id);
COMPAT_TYPE1_TEST(struct ctdb_g_lock, ctdb_g_lock);
int main(int argc, char *argv[])
{
@ -2282,6 +2304,7 @@ int main(int argc, char *argv[])
COMPAT_TEST_FUNC(ctdb_srvid_message)();
COMPAT_TEST_FUNC(ctdb_disable_message)();
COMPAT_TEST_FUNC(ctdb_server_id)();
COMPAT_TEST_FUNC(ctdb_g_lock)();
return 0;
}

View File

@ -31,22 +31,6 @@ PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_data);
PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_datan);
PROTOCOL_TYPE1_TEST(struct ctdb_latency_counter, ctdb_latency_counter);
static void test_ctdb_g_lock(void)
{
TALLOC_CTX *mem_ctx = talloc_new(NULL);
struct ctdb_g_lock p1, p2;
size_t buflen;
int ret;
fill_ctdb_g_lock(mem_ctx, &p1);
buflen = ctdb_g_lock_len(&p1);
ctdb_g_lock_push(&p1, BUFFER);
ret = ctdb_g_lock_pull(BUFFER, buflen, &p2);
assert(ret == 0);
verify_ctdb_g_lock(&p1, &p2);
talloc_free(mem_ctx);
}
PROTOCOL_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
PROTOCOL_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
PROTOCOL_TYPE3_TEST(struct ctdb_dbid, ctdb_dbid);
@ -88,6 +72,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_election_message, ctdb_election_message);
PROTOCOL_TYPE3_TEST(struct ctdb_srvid_message, ctdb_srvid_message);
PROTOCOL_TYPE3_TEST(struct ctdb_disable_message, ctdb_disable_message);
PROTOCOL_TYPE1_TEST(struct ctdb_server_id, ctdb_server_id);
PROTOCOL_TYPE1_TEST(struct ctdb_g_lock, ctdb_g_lock);
DEFINE_TEST(struct ctdb_g_lock_list, ctdb_g_lock_list);
static void test_ctdb_rec_buffer_read_write(void)
@ -146,7 +131,6 @@ int main(int argc, char *argv[])
TEST_FUNC(ctdb_tdb_data)();
TEST_FUNC(ctdb_tdb_datan)();
TEST_FUNC(ctdb_latency_counter)();
test_ctdb_g_lock();
TEST_FUNC(ctdb_statistics)();
TEST_FUNC(ctdb_vnn_map)();
@ -189,6 +173,7 @@ int main(int argc, char *argv[])
TEST_FUNC(ctdb_srvid_message)();
TEST_FUNC(ctdb_disable_message)();
TEST_FUNC(ctdb_server_id)();
TEST_FUNC(ctdb_g_lock)();
TEST_FUNC(ctdb_g_lock_list)();
test_ctdb_rec_buffer_read_write();