From d7c247dd7284097c3aa4a0c6f41551edb43d6e29 Mon Sep 17 00:00:00 2001 From: Amitay Isaacs Date: Thu, 6 Jul 2017 15:34:51 +1000 Subject: [PATCH] ctdb-protocol: Fix marshalling for ctdb_uptime Signed-off-by: Amitay Isaacs Reviewed-by: Martin Schwenke --- ctdb/protocol/protocol_control.c | 4 +- ctdb/protocol/protocol_private.h | 6 +- ctdb/protocol/protocol_types.c | 74 +++++++++++++++++---- ctdb/tests/src/protocol_common.c | 12 +++- ctdb/tests/src/protocol_types_compat_test.c | 30 +++++++++ ctdb/tests/src/protocol_types_test.c | 2 +- 6 files changed, 107 insertions(+), 21 deletions(-) diff --git a/ctdb/protocol/protocol_control.c b/ctdb/protocol/protocol_control.c index 71892aa1249..ddbc75fd48a 100644 --- a/ctdb/protocol/protocol_control.c +++ b/ctdb/protocol/protocol_control.c @@ -1488,7 +1488,7 @@ static void ctdb_reply_control_data_push(struct ctdb_reply_control_data *cd, break; case CTDB_CONTROL_UPTIME: - ctdb_uptime_push(cd->data.uptime, buf); + ctdb_uptime_push(cd->data.uptime, buf, &np); break; case CTDB_CONTROL_TRY_DELETE_RECORDS: @@ -1663,7 +1663,7 @@ static int ctdb_reply_control_data_pull(uint8_t *buf, size_t buflen, case CTDB_CONTROL_UPTIME: ret = ctdb_uptime_pull(buf, buflen, mem_ctx, - &cd->data.uptime); + &cd->data.uptime, &np); break; case CTDB_CONTROL_TRY_DELETE_RECORDS: diff --git a/ctdb/protocol/protocol_private.h b/ctdb/protocol/protocol_private.h index 6c253a70dd8..9496b54fbc2 100644 --- a/ctdb/protocol/protocol_private.h +++ b/ctdb/protocol/protocol_private.h @@ -226,10 +226,10 @@ void ctdb_transdb_push(struct ctdb_transdb *in, uint8_t *buf, size_t *npush); int ctdb_transdb_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, struct ctdb_transdb **out, size_t *npull); -size_t ctdb_uptime_len(struct ctdb_uptime *uptime); -void ctdb_uptime_push(struct ctdb_uptime *uptime, uint8_t *buf); +size_t ctdb_uptime_len(struct ctdb_uptime *in); +void ctdb_uptime_push(struct ctdb_uptime *in, uint8_t *buf, size_t *npush); int ctdb_uptime_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_uptime **out); + struct ctdb_uptime **out, size_t *npull); size_t ctdb_public_ip_len(struct ctdb_public_ip *public_ip); void ctdb_public_ip_push(struct ctdb_public_ip *public_ip, uint8_t *buf); diff --git a/ctdb/protocol/protocol_types.c b/ctdb/protocol/protocol_types.c index 8de43ec2714..4e8b575496e 100644 --- a/ctdb/protocol/protocol_types.c +++ b/ctdb/protocol/protocol_types.c @@ -3311,32 +3311,80 @@ fail: return ret; } -size_t ctdb_uptime_len(struct ctdb_uptime *uptime) +size_t ctdb_uptime_len(struct ctdb_uptime *in) { - return sizeof(struct ctdb_uptime); + return ctdb_timeval_len(&in->current_time) + + ctdb_timeval_len(&in->ctdbd_start_time) + + ctdb_timeval_len(&in->last_recovery_started) + + ctdb_timeval_len(&in->last_recovery_finished); } -void ctdb_uptime_push(struct ctdb_uptime *uptime, uint8_t *buf) +void ctdb_uptime_push(struct ctdb_uptime *in, uint8_t *buf, size_t *npush) { - memcpy(buf, uptime, sizeof(struct ctdb_uptime)); + size_t offset = 0, np; + + ctdb_timeval_push(&in->current_time, buf+offset, &np); + offset += np; + + ctdb_timeval_push(&in->ctdbd_start_time, buf+offset, &np); + offset += np; + + ctdb_timeval_push(&in->last_recovery_started, buf+offset, &np); + offset += np; + + ctdb_timeval_push(&in->last_recovery_finished, buf+offset, &np); + offset += np; + + *npush = offset; } int ctdb_uptime_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx, - struct ctdb_uptime **out) + struct ctdb_uptime **out, size_t *npull) { - struct ctdb_uptime *uptime; + struct ctdb_uptime *val; + size_t offset = 0, np; + int ret; - if (buflen < sizeof(struct ctdb_uptime)) { - return EMSGSIZE; - } - - uptime = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_uptime)); - if (uptime == NULL) { + val = talloc(mem_ctx, struct ctdb_uptime); + if (val == NULL) { return ENOMEM; } - *out = uptime; + ret = ctdb_timeval_pull(buf+offset, buflen-offset, &val->current_time, + &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_timeval_pull(buf+offset, buflen-offset, + &val->ctdbd_start_time, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_timeval_pull(buf+offset, buflen-offset, + &val->last_recovery_started, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + ret = ctdb_timeval_pull(buf+offset, buflen-offset, + &val->last_recovery_finished, &np); + if (ret != 0) { + goto fail; + } + offset += np; + + *out = val; + *npull = offset; return 0; + +fail: + talloc_free(val); + return ret; } size_t ctdb_public_ip_len(struct ctdb_public_ip *pubip) diff --git a/ctdb/tests/src/protocol_common.c b/ctdb/tests/src/protocol_common.c index 6aa9720027a..f459c019b83 100644 --- a/ctdb/tests/src/protocol_common.c +++ b/ctdb/tests/src/protocol_common.c @@ -951,12 +951,20 @@ void verify_ctdb_transdb(struct ctdb_transdb *p1, struct ctdb_transdb *p2) void fill_ctdb_uptime(TALLOC_CTX *mem_ctx, struct ctdb_uptime *p) { - fill_buffer(p, sizeof(struct ctdb_uptime)); + fill_ctdb_timeval(&p->current_time); + fill_ctdb_timeval(&p->ctdbd_start_time); + fill_ctdb_timeval(&p->last_recovery_started); + fill_ctdb_timeval(&p->last_recovery_finished); } void verify_ctdb_uptime(struct ctdb_uptime *p1, struct ctdb_uptime *p2) { - verify_buffer(p1, p2, sizeof(struct ctdb_uptime)); + verify_ctdb_timeval(&p1->current_time, &p2->current_time); + verify_ctdb_timeval(&p1->ctdbd_start_time, &p2->ctdbd_start_time); + verify_ctdb_timeval(&p1->last_recovery_started, + &p2->last_recovery_started); + verify_ctdb_timeval(&p1->last_recovery_finished, + &p2->last_recovery_finished); } void fill_ctdb_public_ip(TALLOC_CTX *mem_ctx, struct ctdb_public_ip *p) diff --git a/ctdb/tests/src/protocol_types_compat_test.c b/ctdb/tests/src/protocol_types_compat_test.c index 5c13ceea4f4..ad908d0b515 100644 --- a/ctdb/tests/src/protocol_types_compat_test.c +++ b/ctdb/tests/src/protocol_types_compat_test.c @@ -1138,6 +1138,34 @@ static int ctdb_transdb_pull_old(uint8_t *buf, size_t buflen, return 0; } +static size_t ctdb_uptime_len_old(struct ctdb_uptime *in) +{ + return sizeof(struct ctdb_uptime); +} + +static void ctdb_uptime_push_old(struct ctdb_uptime *in, uint8_t *buf) +{ + memcpy(buf, in, sizeof(struct ctdb_uptime)); +} + +static int ctdb_uptime_pull_old(uint8_t *buf, size_t buflen, + TALLOC_CTX *mem_ctx, struct ctdb_uptime **out) +{ + struct ctdb_uptime *val; + + if (buflen < sizeof(struct ctdb_uptime)) { + return EMSGSIZE; + } + + val = talloc_memdup(mem_ctx, buf, sizeof(struct ctdb_uptime)); + 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); @@ -1162,6 +1190,7 @@ COMPAT_TYPE3_TEST(struct ctdb_tunable_list, ctdb_tunable_list); COMPAT_TYPE3_TEST(struct ctdb_tickle_list, ctdb_tickle_list); COMPAT_TYPE3_TEST(struct ctdb_addr_info, ctdb_addr_info); COMPAT_TYPE3_TEST(struct ctdb_transdb, ctdb_transdb); +COMPAT_TYPE3_TEST(struct ctdb_uptime, ctdb_uptime); int main(int argc, char *argv[]) { @@ -1191,6 +1220,7 @@ int main(int argc, char *argv[]) COMPAT_TEST_FUNC(ctdb_tickle_list)(); COMPAT_TEST_FUNC(ctdb_addr_info)(); COMPAT_TEST_FUNC(ctdb_transdb)(); + COMPAT_TEST_FUNC(ctdb_uptime)(); return 0; } diff --git a/ctdb/tests/src/protocol_types_test.c b/ctdb/tests/src/protocol_types_test.c index bd9c6e31411..d17beb47d1e 100644 --- a/ctdb/tests/src/protocol_types_test.c +++ b/ctdb/tests/src/protocol_types_test.c @@ -69,7 +69,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_tunable_list, ctdb_tunable_list); PROTOCOL_TYPE3_TEST(struct ctdb_tickle_list, ctdb_tickle_list); PROTOCOL_TYPE3_TEST(struct ctdb_addr_info, ctdb_addr_info); PROTOCOL_TYPE3_TEST(struct ctdb_transdb, ctdb_transdb); -DEFINE_TEST(struct ctdb_uptime, ctdb_uptime); +PROTOCOL_TYPE3_TEST(struct ctdb_uptime, ctdb_uptime); DEFINE_TEST(struct ctdb_public_ip, ctdb_public_ip); DEFINE_TEST(struct ctdb_public_ip_list, ctdb_public_ip_list); DEFINE_TEST(struct ctdb_node_and_flags, ctdb_node_and_flags);