From 6f4867664c928a36155cea788b30a0359f285f33 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Fri, 30 Jun 2017 00:24:40 +1000 Subject: [PATCH] ctdb-protocol: Fix marshalling for ctdb_traverse_all_ext Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/protocol/protocol_control.c | 6 +- ctdb/protocol/protocol_private.h | 9 +- ctdb/protocol/protocol_types.c | 106 +++++++++++++++++--- ctdb/tests/src/protocol_types_compat_test.c | 33 ++++++ ctdb/tests/src/protocol_types_test.c | 2 +- 5 files changed, 134 insertions(+), 22 deletions(-) diff --git a/ctdb/protocol/protocol_control.c b/ctdb/protocol/protocol_control.c index a8cfe769e0f..618a89ffd43 100644 --- a/ctdb/protocol/protocol_control.c +++ b/ctdb/protocol/protocol_control.c @@ -665,7 +665,8 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd, break; case CTDB_CONTROL_TRAVERSE_ALL_EXT: - ctdb_traverse_all_ext_push(cd->data.traverse_all_ext, buf); + ctdb_traverse_all_ext_push(cd->data.traverse_all_ext, buf, + &np); break; case CTDB_CONTROL_RECEIVE_RECORDS: @@ -985,7 +986,8 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen, case CTDB_CONTROL_TRAVERSE_ALL_EXT: ret = ctdb_traverse_all_ext_pull(buf, buflen, mem_ctx, - &cd->data.traverse_all_ext); + &cd->data.traverse_all_ext, + &np); break; case CTDB_CONTROL_RECEIVE_RECORDS: diff --git a/ctdb/protocol/protocol_private.h b/ctdb/protocol/protocol_private.h index 8c7a223776a..fc3d51ca1b2 100644 --- a/ctdb/protocol/protocol_private.h +++ b/ctdb/protocol/protocol_private.h @@ -164,12 +164,13 @@ int ctdb_traverse_start_ext_pull(uint8_t *buf, size_t buflen, struct ctdb_traverse_start_ext **out, size_t *npull); -size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *traverse); -void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *traverse, - uint8_t *buf); +size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *in); +void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *in, + uint8_t *buf, size_t *npush); int ctdb_traverse_all_ext_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_traverse_all_ext **out); + struct ctdb_traverse_all_ext **out, + size_t *npull); size_t ctdb_sock_addr_len(ctdb_sock_addr *addr); void ctdb_sock_addr_push(ctdb_sock_addr *addr, uint8_t *buf); diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index e73cee450ac..19017cbc19e 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -1926,35 +1926,111 @@ fail: return ret; } -size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *traverse) +size_t ctdb_traverse_all_ext_len(struct ctdb_traverse_all_ext *in) { - return sizeof(struct ctdb_traverse_all_ext); + return ctdb_uint32_len(&in->db_id) + + ctdb_uint32_len(&in->reqid) + + ctdb_uint32_len(&in->pnn) + + ctdb_uint32_len(&in->client_reqid) + + ctdb_uint64_len(&in->srvid) + + ctdb_bool_len(&in->withemptyrecords) + + ctdb_padding_len(7); } -void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *traverse, - uint8_t *buf) +void ctdb_traverse_all_ext_push(struct ctdb_traverse_all_ext *in, + uint8_t *buf, size_t *npush) { - memcpy(buf, traverse, sizeof(struct ctdb_traverse_all_ext)); + size_t offset = 0, np; + + ctdb_uint32_push(&in->db_id, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->reqid, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->pnn, buf+offset, &np); + offset += np; + + ctdb_uint32_push(&in->client_reqid, buf+offset, &np); + offset += np; + + ctdb_uint64_push(&in->srvid, buf+offset, &np); + offset += np; + + ctdb_bool_push(&in->withemptyrecords, buf+offset, &np); + offset += np; + + ctdb_padding_push(7, buf+offset, &np); + offset += np; + + *npush = offset; } int ctdb_traverse_all_ext_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_traverse_all_ext **out) + struct ctdb_traverse_all_ext **out, + size_t *npull) { - struct ctdb_traverse_all_ext *traverse; + struct ctdb_traverse_all_ext *val; + size_t offset = 0, np; + int ret; - if (buflen < sizeof(struct ctdb_traverse_all_ext)) { - return EMSGSIZE; - } - - traverse = talloc_memdup(mem_ctx, buf, - sizeof(struct ctdb_traverse_all_ext)); - if (traverse == NULL) { + val = talloc(mem_ctx, struct ctdb_traverse_all_ext); + if (val == NULL) { return ENOMEM; } - *out = traverse; + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->db_id, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->reqid, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->pnn, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint32_pull(buf+offset, buflen-offset, &val->client_reqid, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_bool_pull(buf+offset, buflen-offset, + &val->withemptyrecords, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_padding_pull(buf+offset, buflen-offset, 7, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = val; + *npull = offset; return 0; + +fail: + talloc_free(val); + return ret; } size_t ctdb_sock_addr_len(ctdb_sock_addr *addr) diff --git a/ctdb/tests/src/protocol_types_compat_test.c b/ctdb/tests/src/protocol_types_compat_test.c index b6f4ed1f87c..345be73c811 100644 --- a/ctdb/tests/src/protocol_types_compat_test.c +++ b/ctdb/tests/src/protocol_types_compat_test.c @@ -616,6 +616,37 @@ static int ctdb_traverse_start_ext_pull_old(uint8_t *buf, size_t buflen, return 0; } +static size_t ctdb_traverse_all_ext_len_old(struct ctdb_traverse_all_ext *in) +{ + return sizeof(struct ctdb_traverse_all_ext); +} + +static void ctdb_traverse_all_ext_push_old(struct ctdb_traverse_all_ext *in, + uint8_t *buf) +{ + memcpy(buf, in, sizeof(struct ctdb_traverse_all_ext)); +} + +static int ctdb_traverse_all_ext_pull_old(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_traverse_all_ext **out) +{ + struct ctdb_traverse_all_ext *val; + + if (buflen < sizeof(struct ctdb_traverse_all_ext)) { + return EMSGSIZE; + } + + val = talloc_memdup(mem_ctx, buf, + sizeof(struct ctdb_traverse_all_ext)); + if (val == NULL) { + return ENOMEM; + } + + *out = val; + return 0; +} + COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics); COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map); @@ -630,6 +661,7 @@ COMPAT_TYPE3_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer); COMPAT_TYPE3_TEST(struct ctdb_traverse_start, ctdb_traverse_start); COMPAT_TYPE3_TEST(struct ctdb_traverse_all, ctdb_traverse_all); COMPAT_TYPE3_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext); +COMPAT_TYPE3_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext); int main(int argc, char *argv[]) { @@ -649,6 +681,7 @@ int main(int argc, char *argv[]) COMPAT_TEST_FUNC(ctdb_traverse_start)(); COMPAT_TEST_FUNC(ctdb_traverse_all)(); COMPAT_TEST_FUNC(ctdb_traverse_start_ext)(); + COMPAT_TEST_FUNC(ctdb_traverse_all_ext)(); return 0; } diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index b039a7350a7..a8d055168f3 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -59,7 +59,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer); PROTOCOL_TYPE3_TEST(struct ctdb_traverse_start, ctdb_traverse_start); PROTOCOL_TYPE3_TEST(struct ctdb_traverse_all, ctdb_traverse_all); PROTOCOL_TYPE3_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext); -DEFINE_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext); +PROTOCOL_TYPE3_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext); DEFINE_TEST(ctdb_sock_addr, ctdb_sock_addr); DEFINE_TEST(struct ctdb_connection, ctdb_connection); DEFINE_TEST(struct ctdb_tunable, ctdb_tunable);