ctdb-protocol: Fix marshalling for ctdb_g_lock
authorAmitay Isaacs <amitay@gmail.com>
Thu, 13 Jul 2017 05:22:08 +0000 (15:22 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:25 +0000 (14:59 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_api.h
ctdb/protocol/protocol_types.c
ctdb/tests/src/protocol_common.c
ctdb/tests/src/protocol_common.h
ctdb/tests/src/protocol_types_compat_test.c
ctdb/tests/src/protocol_types_test.c

index 753a08ce5986bad62833eb54d0b41e8a6bc203ff..6d25a465306c4930dc61c62b4d994d4cce0d50e3 100644 (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);
index a95795b5156c7baf91a0f3f2f14c6857122abc49..0dde5a751057fd6b46f0e874f4f7e6ecdf7fc63d 100644 (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;
 
-       memcpy(lock, buf, sizeof(struct ctdb_g_lock));
+       if (type == 0) {
+               out->type = CTDB_G_LOCK_READ;
+       } else if (type == 1) {
+               out->type = CTDB_G_LOCK_WRITE;
+       } else {
+               return EPROTO;
+       }
+
+       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;
index 1a74104174eb0ff6018c9bb35fc8b273d65ba07b..138a44ee5d173795e7a13d1da0a786d4069e5ad2 100644 (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]);
        }
 }
 
index a14631c5f130564d074d1fc9312910679ae3f7a9..c8fa5746213e93d68c1a3e76fcc6fd6812689e80 100644 (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);
index 53ed0e9aab4b3e64132e5faf594fdeca8e326784..dfeb3562b0faa518d21f4cf76c565270e7213b73 100644 (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;
 }
index 08312ef87021949defa8094e30d1760dc2c0cba9..3a4e0755a500843a5bf6c9f60c1417084435c7ea 100644 (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();