RIP BOOL. Convert BOOL -> bool. I found a few interesting
[samba.git] / source3 / lib / ctdbd_conn.c
index 8c1aab8f371fab07fc4b2a1ce0b6d199dfd59fdf..2892b8f6ab16985b50f7e2e65879fcff49d425ad 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
@@ -61,6 +60,20 @@ static void cluster_fatal(const char *why)
        _exit(0);
 }
 
+/*
+ *
+ */
+static void ctdb_packet_dump(struct ctdb_req_header *hdr)
+{
+       if (DEBUGLEVEL < 10) {
+               return;
+       }
+       DEBUGADD(10, ("len=%d, magic=%x, vers=%d, gen=%d, op=%d, reqid=%d\n",
+                     (int)hdr->length, (int)hdr->ctdb_magic,
+                     (int)hdr->ctdb_version, (int)hdr->generation,
+                     (int)hdr->operation, (int)hdr->reqid));
+}
+
 /*
  * Register a srvid with ctdbd
  */
@@ -82,7 +95,7 @@ static NTSTATUS get_cluster_vnn(struct ctdbd_connection *conn, uint32 *vnn)
        int32_t cstatus=-1;
        NTSTATUS status;
        status = ctdbd_control(conn,
-                              CTDB_CURRENT_NODE, CTDB_CONTROL_GET_VNN, 0,
+                              CTDB_CURRENT_NODE, CTDB_CONTROL_GET_PNN, 0,
                               tdb_null, NULL, NULL, &cstatus);
        if (!NT_STATUS_IS_OK(status)) {
                cluster_fatal("ctdbd_control failed\n");
@@ -142,7 +155,7 @@ static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx,
  * Do we have a complete ctdb packet in the queue?
  */
 
-static BOOL ctdb_req_complete(const struct data_blob *data,
+static bool ctdb_req_complete(const struct data_blob *data,
                              size_t *length,
                              void *private_data)
 {
@@ -158,8 +171,8 @@ static BOOL ctdb_req_complete(const struct data_blob *data,
 
        if (msglen < sizeof(struct ctdb_req_header)) {
                DEBUG(0, ("Got invalid msglen: %d, expected at least %d for "
-                         "the req_header\n", msglen,
-                         sizeof(struct ctdb_req_header)));
+                         "the req_header\n", (int)msglen,
+                         (int)sizeof(struct ctdb_req_header)));
                cluster_fatal("ctdbd protocol error\n");
        }
 
@@ -250,6 +263,11 @@ static struct messaging_rec *ctdb_pull_messaging_rec(TALLOC_CTX *mem_ctx,
                &blob, result, result,
                (ndr_pull_flags_fn_t)ndr_pull_messaging_rec);
 
+       if (DEBUGLEVEL >= 10) {
+               DEBUG(10, ("ctdb_pull_messaging_rec:\n"));
+               NDR_PRINT_DEBUG(messaging_rec, result);
+       }
+
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(0, ("ndr_pull_struct_blob failed: %s\n",
                          nt_errstr(status)));
@@ -279,13 +297,18 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
        if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_BUSY)) {
                /* EAGAIN */
                goto again;
+       } else if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+               /* EAGAIN */
+               goto again;
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(3, ("packet_fd_read failed: %s\n", nt_errstr(status)));
+               DEBUG(0, ("packet_fd_read failed: %s\n", nt_errstr(status)));
                cluster_fatal("ctdbd died\n");
        }
 
+ next_pkt:
+
        ZERO_STRUCT(state);
        state.mem_ctx = mem_ctx;
 
@@ -294,16 +317,20 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
                /*
                 * Not enough data
                 */
+               DEBUG(10, ("not enough data from ctdb socket, retrying\n"));
                goto again;
        }
 
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(3, ("Could not read packet: %s\n", nt_errstr(status)));
+               DEBUG(0, ("Could not read packet: %s\n", nt_errstr(status)));
                cluster_fatal("ctdbd died\n");
        }
 
        hdr = (struct ctdb_req_header *)state.req.data;
 
+       DEBUG(10, ("Received ctdb packet\n"));
+       ctdb_packet_dump(hdr);
+
        if (hdr->operation == CTDB_REQ_MESSAGE) {
                struct timed_event *evt;
                struct deferred_msg_state *msg_state;
@@ -311,8 +338,9 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
 
                if (conn->msg_ctx == NULL) {
                        DEBUG(1, ("Got a message without having a msg ctx, "
-                                 "dropping msg %llu\n", msg->srvid));
-                       goto again;
+                                 "dropping msg %llu\n",
+                                 (long long unsigned)msg->srvid));
+                       goto next_pkt;
                }
                
                if ((conn->release_ip_handler != NULL)
@@ -322,13 +350,13 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
                        conn->release_ip_handler((const char *)msg->data,
                                                 conn->release_ip_priv);
                        TALLOC_FREE(hdr);
-                       goto again;
+                       goto next_pkt;
                }
 
                if (!(msg_state = TALLOC_P(NULL, struct deferred_msg_state))) {
                        DEBUG(0, ("talloc failed\n"));
                        TALLOC_FREE(hdr);
-                       goto again;
+                       goto next_pkt;
                }
 
                if (!(msg_state->rec = ctdb_pull_messaging_rec(
@@ -336,7 +364,7 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
                        DEBUG(0, ("ctdbd_pull_messaging_rec failed\n"));
                        TALLOC_FREE(msg_state);
                        TALLOC_FREE(hdr);
-                       goto again;
+                       goto next_pkt;
                }
 
                TALLOC_FREE(hdr);
@@ -357,10 +385,10 @@ static NTSTATUS ctdb_read_req(struct ctdbd_connection *conn, uint32 reqid,
                        DEBUG(0, ("event_add_timed failed\n"));
                        TALLOC_FREE(msg_state);
                        TALLOC_FREE(hdr);
-                       goto again;
+                       goto next_pkt;
                }
                
-               goto again;
+               goto next_pkt;
        }
 
        if (hdr->reqid != reqid) {
@@ -619,6 +647,9 @@ NTSTATUS ctdbd_messaging_send(struct ctdbd_connection *conn,
        r.srvid          = dst_srvid;
        r.datalen        = blob.length;
 
+       DEBUG(10, ("ctdbd_messaging_send: Sending ctdb packet\n"));
+       ctdb_packet_dump(&r.hdr);
+
        status = packet_send(
                conn->pkt, 2,
                data_blob_const(&r, offsetof(struct ctdb_req_message, data)),
@@ -679,10 +710,13 @@ static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
        req.srvid            = srvid;
        req.datalen          = data.dsize;
 
+       DEBUG(10, ("ctdbd_control: Sending ctdb packet\n"));
+       ctdb_packet_dump(&req.hdr);
+
        status = packet_send(
                conn->pkt, 2,
                data_blob_const(&req, offsetof(struct ctdb_req_control, data)),
-               data);
+               data_blob_const(data.dptr, data.dsize));
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
@@ -731,7 +765,7 @@ static NTSTATUS ctdbd_control(struct ctdbd_connection *conn,
 /*
  * see if a remote process exists
  */
-BOOL ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
+bool ctdbd_process_exists(struct ctdbd_connection *conn, uint32 vnn, pid_t pid)
 {
        NTSTATUS status;
        TDB_DATA data;
@@ -847,10 +881,13 @@ NTSTATUS ctdbd_migrate(struct ctdbd_connection *conn, uint32 db_id,
        req.db_id            = db_id;
        req.keylen           = key.dsize;
 
+       DEBUG(10, ("ctdbd_migrate: Sending ctdb packet\n"));
+       ctdb_packet_dump(&req.hdr);
+
        status = packet_send(
                conn->pkt, 2,
                data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
-               key);
+               data_blob_const(key.dptr, key.dsize));
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
@@ -909,7 +946,7 @@ NTSTATUS ctdbd_fetch(struct ctdbd_connection *conn, uint32 db_id,
        status = packet_send(
                conn->pkt, 2,
                data_blob_const(&req, offsetof(struct ctdb_req_call, data)),
-               key);
+               data_blob_const(key.dptr, key.dsize));
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("packet_send failed: %s\n", nt_errstr(status)));
@@ -1089,11 +1126,19 @@ NTSTATUS ctdbd_traverse(uint32 db_id,
 
                status = packet_fd_read_sync(conn->pkt);
 
+               if (NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+                       /*
+                        * There might be more in the queue
+                        */
+                       continue;
+               }
+
                if (NT_STATUS_EQUAL(status, NT_STATUS_END_OF_FILE)) {
                        status = NT_STATUS_OK;
                }
 
                if (!NT_STATUS_IS_OK(status)) {
+                       DEBUG(0, ("packet_fd_read_sync failed: %s\n", nt_errstr(status)));
                        cluster_fatal("ctdbd died\n");
                }
        }