From c882182ecdf9c59f955a5020b3dc4908285bc3d2 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Thu, 29 Jun 2017 23:48:01 +1000 Subject: [PATCH] ctdb-protocol: Fix marshalling for ctdb_traverse_start Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/protocol/protocol_control.c | 8 +-- ctdb/protocol/protocol_private.h | 8 +-- ctdb/protocol/protocol_types.c | 63 ++++++++++++++++----- ctdb/tests/src/protocol_types_compat_test.c | 32 +++++++++++ ctdb/tests/src/protocol_types_test.c | 2 +- 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/ctdb/protocol/protocol_control.c b/ctdb/protocol/protocol_control.c index aeb25b33e8e..23ddfa39918 100644 --- a/ctdb/protocol/protocol_control.c +++ b/ctdb/protocol/protocol_control.c @@ -483,7 +483,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd, break; case CTDB_CONTROL_TRAVERSE_START: - ctdb_traverse_start_push(cd->data.traverse_start, buf); + ctdb_traverse_start_push(cd->data.traverse_start, buf, &np); break; case CTDB_CONTROL_TRAVERSE_ALL: @@ -579,7 +579,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd, break; case CTDB_CONTROL_TRAVERSE_KILL: - ctdb_traverse_start_push(cd->data.traverse_start, buf); + ctdb_traverse_start_push(cd->data.traverse_start, buf, &np); break; case CTDB_CONTROL_RECD_RECLOCK_LATENCY: @@ -770,7 +770,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen, case CTDB_CONTROL_TRAVERSE_START: ret = ctdb_traverse_start_pull(buf, buflen, mem_ctx, - &cd->data.traverse_start); + &cd->data.traverse_start, &np); break; case CTDB_CONTROL_TRAVERSE_ALL: @@ -886,7 +886,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen, case CTDB_CONTROL_TRAVERSE_KILL: ret = ctdb_traverse_start_pull(buf, buflen, mem_ctx, - &cd->data.traverse_start); + &cd->data.traverse_start, &np); break; case CTDB_CONTROL_RECD_RECLOCK_LATENCY: diff --git a/ctdb/protocol/protocol_private.h b/ctdb/protocol/protocol_private.h index 17bdc27cbaf..e69b2778e79 100644 --- a/ctdb/protocol/protocol_private.h +++ b/ctdb/protocol/protocol_private.h @@ -144,11 +144,11 @@ void ctdb_pulldb_ext_push(struct ctdb_pulldb_ext *in, uint8_t *buf, int ctdb_pulldb_ext_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, struct ctdb_pulldb_ext **out, size_t *npull); -size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse); -void ctdb_traverse_start_push(struct ctdb_traverse_start *traverse, - uint8_t *buf); +size_t ctdb_traverse_start_len(struct ctdb_traverse_start *in); +void ctdb_traverse_start_push(struct ctdb_traverse_start *in, uint8_t *buf, + size_t *npush); int ctdb_traverse_start_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_traverse_start **out); + struct ctdb_traverse_start **out, size_t *npull); size_t ctdb_traverse_all_len(struct ctdb_traverse_all *traverse); void ctdb_traverse_all_push(struct ctdb_traverse_all *traverse, uint8_t *buf); diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index 017283299d1..a3678ad8f33 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -1693,34 +1693,67 @@ int ctdb_rec_buffer_read(int fd, TALLOC_CTX *mem_ctx, return 0; } -size_t ctdb_traverse_start_len(struct ctdb_traverse_start *traverse) +size_t ctdb_traverse_start_len(struct ctdb_traverse_start *in) { - return sizeof(struct ctdb_traverse_start); + return ctdb_uint32_len(&in->db_id) + + ctdb_uint32_len(&in->reqid) + + ctdb_uint64_len(&in->srvid); } -void ctdb_traverse_start_push(struct ctdb_traverse_start *traverse, - uint8_t *buf) +void ctdb_traverse_start_push(struct ctdb_traverse_start *in, uint8_t *buf, + size_t *npush) { - memcpy(buf, traverse, sizeof(struct ctdb_traverse_start)); + 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_uint64_push(&in->srvid, buf+offset, &np); + offset += np; + + *npush = offset; } int ctdb_traverse_start_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_traverse_start **out) + struct ctdb_traverse_start **out, size_t *npull) { - struct ctdb_traverse_start *traverse; + struct ctdb_traverse_start *val; + size_t offset = 0, np; + int ret; - if (buflen < sizeof(struct ctdb_traverse_start)) { - return EMSGSIZE; - } - - traverse = talloc_memdup(mem_ctx, buf, - sizeof(struct ctdb_traverse_start)); - if (traverse == NULL) { + val = talloc(mem_ctx, struct ctdb_traverse_start); + 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_uint64_pull(buf+offset, buflen-offset, &val->srvid, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = val; + *npull = offset; return 0; + +fail: + talloc_free(val); + return ret; } size_t ctdb_traverse_all_len(struct ctdb_traverse_all *traverse) diff --git a/ctdb/tests/src/protocol_types_compat_test.c b/ctdb/tests/src/protocol_types_compat_test.c index 03b9508c8bb..4741db55f7a 100644 --- a/ctdb/tests/src/protocol_types_compat_test.c +++ b/ctdb/tests/src/protocol_types_compat_test.c @@ -524,6 +524,36 @@ static int ctdb_rec_buffer_pull_old(uint8_t *buf, size_t buflen, return 0; } +static size_t ctdb_traverse_start_len_old(struct ctdb_traverse_start *in) +{ + return sizeof(struct ctdb_traverse_start); +} + +static void ctdb_traverse_start_push_old(struct ctdb_traverse_start *in, + uint8_t *buf) +{ + memcpy(buf, in, sizeof(struct ctdb_traverse_start)); +} + +static int ctdb_traverse_start_pull_old(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, + struct ctdb_traverse_start **out) +{ + struct ctdb_traverse_start *val; + + if (buflen < sizeof(struct ctdb_traverse_start)) { + return EMSGSIZE; + } + + val = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_traverse_start)); + 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); @@ -535,6 +565,7 @@ COMPAT_TYPE1_TEST(struct ctdb_ltdb_header, ctdb_ltdb_header); COMPAT_TYPE3_TEST(struct ctdb_rec_data, ctdb_rec_data); COMPAT_TYPE3_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer); +COMPAT_TYPE3_TEST(struct ctdb_traverse_start, ctdb_traverse_start); int main(int argc, char *argv[]) { @@ -551,6 +582,7 @@ int main(int argc, char *argv[]) COMPAT_TEST_FUNC(ctdb_ltdb_header)(); COMPAT_TEST_FUNC(ctdb_rec_data)(); COMPAT_TEST_FUNC(ctdb_rec_buffer)(); + COMPAT_TEST_FUNC(ctdb_traverse_start)(); return 0; } diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index 0309a1278d4..2e40d35683e 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -56,7 +56,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext); PROTOCOL_TYPE1_TEST(struct ctdb_ltdb_header, ctdb_ltdb_header); PROTOCOL_TYPE3_TEST(struct ctdb_rec_data, ctdb_rec_data); PROTOCOL_TYPE3_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer); -DEFINE_TEST(struct ctdb_traverse_start, ctdb_traverse_start); +PROTOCOL_TYPE3_TEST(struct ctdb_traverse_start, ctdb_traverse_start); DEFINE_TEST(struct ctdb_traverse_all, ctdb_traverse_all); DEFINE_TEST(struct ctdb_traverse_start_ext, ctdb_traverse_start_ext); DEFINE_TEST(struct ctdb_traverse_all_ext, ctdb_traverse_all_ext);