]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
change ctdb_client_read() to use the ctdb_read_pdu() helper
authorRonnie sahlberg <ronniesahlberg@gmail.com>
Mon, 9 Apr 2007 22:38:29 +0000 (08:38 +1000)
committerRonnie sahlberg <ronniesahlberg@gmail.com>
Mon, 9 Apr 2007 22:38:29 +0000 (08:38 +1000)
(This used to be ctdb commit d476aa8533b394af6aced9c80fffaf0eefae1dd0)

ctdb/common/ctdb_daemon.c
ctdb/tests/test.sh

index 5b1c33dc22629f548ee8ac0b68d3f5b0944dce5d..8ef2366074c7e981a75a38d832ba772489fdcf33 100644 (file)
@@ -159,85 +159,42 @@ static void client_incoming_packet(struct ctdb_client *client, void *data, size_
 }
 
 
-static void ctdb_client_read(struct event_context *ev, struct fd_event *fde, 
-                        uint16_t flags, void *private)
+static void ctdb_client_read_cb(uint8_t *data, int cnt, void *args)
 {
-       struct ctdb_client *client = talloc_get_type(private, struct ctdb_client);
-       int num_ready = 0;
-       ssize_t nread;
-       uint8_t *data, *data_base;
-
-/*XXX replace this and all other similar code (tcp) with ctdb_io.c/ctdb_read_pdu */
-       if (ioctl(client->fd, FIONREAD, &num_ready) != 0 ||
-           num_ready == 0) {
-               /* we've lost the connection from a client client. */
-               talloc_free(client);
+       struct ctdb_client *client = talloc_get_type(args, struct ctdb_client);
+       struct ctdb_req_header *hdr;
+
+       if (cnt < sizeof(*hdr)) {
+               ctdb_set_error(client->ctdb, "Bad packet length %d\n", cnt);
                return;
        }
-
-       client->partial.data = talloc_realloc_size(client, client->partial.data, 
-                                              num_ready + client->partial.length);
-       if (client->partial.data == NULL) {
-               /* not much we can do except drop the socket */
-               talloc_free(client);
+       hdr = (struct ctdb_req_header *)data;
+       if (cnt != hdr->length) {
+               ctdb_set_error(client->ctdb, "Bad header length %d expected %d\n", 
+                              hdr->length, cnt);
                return;
        }
 
-       nread = read(client->fd, client->partial.data+client->partial.length, num_ready);
-       if (nread <= 0) {
-               /* the connection must be dead */
-               talloc_free(client);
+       if (hdr->ctdb_magic != CTDB_MAGIC) {
+               ctdb_set_error(client->ctdb, "Non CTDB packet rejected\n");
                return;
        }
 
-       data = client->partial.data;
-       nread += client->partial.length;
-
-       client->partial.data = NULL;
-       client->partial.length = 0;
-
-       if (nread >= 4 && *(uint32_t *)data == nread) {
-               /* it is the responsibility of the incoming packet function to free 'data' */
-               client_incoming_packet(client, data, nread);
+       if (hdr->ctdb_version != CTDB_VERSION) {
+               ctdb_set_error(client->ctdb, "Bad CTDB version 0x%x rejected\n", hdr->ctdb_version);
                return;
        }
 
-       data_base = data;
+       /* it is the responsibility of the incoming packet function to free 'data' */
+       client_incoming_packet(client, data, cnt);
+}
 
-       while (nread >= 4 && *(uint32_t *)data <= nread) {
-               /* we have at least one packet */
-               uint8_t *d2;
-               uint32_t len;
-               len = *(uint32_t *)data;
-               d2 = talloc_memdup(client, data, len);
-               if (d2 == NULL) {
-                       /* sigh */
-                       talloc_free(client);
-                       return;
-               }
-               client_incoming_packet(client, d2, len);
-               data += len;
-               nread -= len;           
-       }
-
-       if (nread > 0) {
-               /* we have only part of a packet */
-               if (data_base == data) {
-                       client->partial.data = data;
-                       client->partial.length = nread;
-               } else {
-                       client->partial.data = talloc_memdup(client, data, nread);
-                       if (client->partial.data == NULL) {
-                               talloc_free(client);
-                               return;
-                       }
-                       client->partial.length = nread;
-                       talloc_free(data_base);
-               }
-               return;
-       }
+static void ctdb_client_read(struct event_context *ev, struct fd_event *fde, 
+                        uint16_t flags, void *private)
+{
+       struct ctdb_client *client = talloc_get_type(private, struct ctdb_client);
 
-       talloc_free(data_base);
+       ctdb_read_pdu(client->fd, client, &client->partial, ctdb_client_read_cb, client);
 }
 
 
index a72fdf1907fc4e60dd688f381d9f0e9c58f7bef1..b1e8ae8184c38e5bd757f27957f57c0f84069ec3 100755 (executable)
@@ -16,3 +16,18 @@ bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.3:9001 &
 bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.4:9001 &
 sleep 3
 killall ctdb_test
+
+echo "Trying 2 nodes in daemon mode"
+bin/ctdb_test --nlist tests/nodes.txt --listen 127.0.0.1:9001 --daemon &
+bin/ctdb_test --nlist tests/nodes.txt --listen 127.0.0.2:9001 --daemon &
+
+sleep 30
+killall ctdb_test
+
+echo "Trying 4 nodes in daemon mode"
+bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.1:9001 --daemon &
+bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.2:9001 --daemon &
+bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.3:9001 --daemon &
+bin/ctdb_test --nlist tests/4nodes.txt --listen 127.0.0.4:9001 --daemon &
+sleep 3
+killall ctdb_test