ctdb-protocol: Fix marshalling for pid_t
authorAmitay Isaacs <amitay@gmail.com>
Thu, 29 Jun 2017 12:18:27 +0000 (22:18 +1000)
committerMartin Schwenke <martins@samba.org>
Wed, 30 Aug 2017 12:59:22 +0000 (14:59 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/protocol/protocol_basic.c
ctdb/protocol/protocol_control.c
ctdb/protocol/protocol_private.h
ctdb/tests/src/protocol_basic_test.c
ctdb/tests/src/protocol_common.c
ctdb/tests/src/protocol_common.h

index 10824cf4ed7808446ccc1b8a60b8916bfa010c5a..689d7decb6ea682404bb035a6a3ab997805d70d1 100644 (file)
@@ -323,23 +323,24 @@ int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
  * System defined data types
  */
 
-size_t ctdb_pid_len(pid_t pid)
+size_t ctdb_pid_len(pid_t *in)
 {
        return sizeof(pid_t);
 }
 
-void ctdb_pid_push(pid_t pid, uint8_t *buf)
+void ctdb_pid_push(pid_t *in, uint8_t *buf, size_t *npush)
 {
-       memcpy(buf, &pid, sizeof(pid_t));
+       memcpy(buf, in, sizeof(pid_t));
+       *npush = sizeof(pid_t);
 }
 
-int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                 pid_t *out)
+int ctdb_pid_pull(uint8_t *buf, size_t buflen, pid_t *out, size_t *npull)
 {
        if (buflen < sizeof(pid_t)) {
                return EMSGSIZE;
        }
 
-       *out = *(pid_t *)buf;
+       memcpy(out, buf, sizeof(pid_t));
+       *npull = sizeof(pid_t);
        return 0;
 }
index 848399041d30f361890141538f0396b84fa0aedf..20893690851909fdf720455cf4a4726b1989990f 100644 (file)
@@ -57,7 +57,7 @@ static size_t ctdb_req_control_data_len(struct ctdb_req_control_data *cd)
 
        switch (cd->opcode) {
        case CTDB_CONTROL_PROCESS_EXISTS:
-               len = ctdb_pid_len(cd->data.pid);
+               len = ctdb_pid_len(&cd->data.pid);
                break;
 
        case CTDB_CONTROL_STATISTICS:
@@ -448,7 +448,7 @@ static void ctdb_req_control_data_push(struct ctdb_req_control_data *cd,
 
        switch (cd->opcode) {
        case CTDB_CONTROL_PROCESS_EXISTS:
-               ctdb_pid_push(cd->data.pid, buf);
+               ctdb_pid_push(&cd->data.pid, buf, &np);
                break;
 
        case CTDB_CONTROL_GETDBPATH:
@@ -730,8 +730,7 @@ static int ctdb_req_control_data_pull(uint8_t *buf, size_t buflen,
 
        switch (opcode) {
        case CTDB_CONTROL_PROCESS_EXISTS:
-               ret = ctdb_pid_pull(buf, buflen, mem_ctx,
-                                   &cd->data.pid);
+               ret = ctdb_pid_pull(buf, buflen, &cd->data.pid, &np);
                break;
 
        case CTDB_CONTROL_GETDBPATH:
index 07a7ab5595e1821c64c3d943068380f09bbd0c56..9557ffea794602c0849b31028de3618931e80e97 100644 (file)
@@ -72,10 +72,9 @@ void ctdb_stringn_push(const char **in, uint8_t *buf, size_t *npush);
 int ctdb_stringn_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
                      const char **out, size_t *npull);
 
-size_t ctdb_pid_len(pid_t pid);
-void ctdb_pid_push(pid_t pid, uint8_t *buf);
-int ctdb_pid_pull(uint8_t *buf, size_t buflen, TALLOC_CTX *mem_ctx,
-                 pid_t *out);
+size_t ctdb_pid_len(pid_t *in);
+void ctdb_pid_push(pid_t *in, uint8_t *buf, size_t *npush);
+int ctdb_pid_pull(uint8_t *buf, size_t buflen, pid_t *out, size_t *npull);
 
 /*
  * From protocol/protocol_types.c
index ee350d62fce4ec2259bec5c3183a8fe46ce2fa6e..c809142c6651766eb06eb01f083b44c7b14877ac 100644 (file)
@@ -58,19 +58,7 @@ static void test_ctdb_chararray(void)
 PROTOCOL_TYPE2_TEST(const char *, ctdb_string);
 PROTOCOL_TYPE2_TEST(const char *, ctdb_stringn);
 
-static void test_ctdb_pid(void)
-{
-       pid_t p1, p2;
-       size_t buflen;
-       int ret;
-
-       p1 = rand32();
-       buflen = ctdb_pid_len(p1);
-       ctdb_pid_push(p1, BUFFER);
-       ret = ctdb_pid_pull(BUFFER, buflen, NULL, &p2);
-       assert(ret == 0);
-       assert(p1 == p2);
-}
+PROTOCOL_TYPE1_TEST(pid_t, ctdb_pid);
 
 int main(int argc, char *argv[])
 {
@@ -92,7 +80,7 @@ int main(int argc, char *argv[])
        TEST_FUNC(ctdb_string)();
        TEST_FUNC(ctdb_stringn)();
 
-       test_ctdb_pid();
+       TEST_FUNC(ctdb_pid)();
 
        return 0;
 }
index c6f7159f165b6d8ac30c766d4f4318f4e6d58018..9687de4adca9199aa8ea0e3040b77d4e77d3e4ed 100644 (file)
@@ -210,6 +210,16 @@ void verify_ctdb_stringn(const char **p1, const char **p2)
        verify_ctdb_string(p1, p2);
 }
 
+void fill_ctdb_pid(pid_t *p)
+{
+       *p = rand32();
+}
+
+void verify_ctdb_pid(pid_t *p1, pid_t *p2)
+{
+       assert(*p1 == *p2);
+}
+
 void fill_tdb_data_nonnull(TALLOC_CTX *mem_ctx, TDB_DATA *p)
 {
        p->dsize = rand_int(1024) + 1;
index fc2575a2139015cccaf02e95b92bbeddd0c3277a..7637b11bfa432562bfce38c879d8eb4fe96f7069 100644 (file)
@@ -179,6 +179,9 @@ void verify_ctdb_string(const char **p1, const char **p2);
 void fill_ctdb_stringn(TALLOC_CTX *mem_ctx, const char **p);
 void verify_ctdb_stringn(const char **p1, const char **p2);
 
+void fill_ctdb_pid(pid_t *p);
+void verify_ctdb_pid(pid_t *p1, pid_t *p2);
+
 void fill_tdb_data_nonnull(TALLOC_CTX *mem_ctx, TDB_DATA *p);
 void fill_tdb_data(TALLOC_CTX *mem_ctx, TDB_DATA *p);
 void verify_tdb_data(TDB_DATA *p1, TDB_DATA *p2);