tools/ctdb: Pass memory context for returning nodes in parse_nodestring
[ctdb.git] / libctdb / control.c
index b4c54cd660389e3458b0f315a4c21fe75b3f3348..447a1d1275c15ba5f719666178c14c776a98b17d 100644 (file)
@@ -16,6 +16,7 @@
    You should have received a copy of the GNU General Public License
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
+#include <sys/time.h>
 #include <sys/socket.h>
 #include <string.h>
 #include <ctdb.h>
@@ -33,6 +34,7 @@
 #undef ctdb_getdbseqnum_send
 #undef ctdb_getifaces_send
 #undef ctdb_getvnnmap_send
+#undef ctdb_getcapabilities_send
 
 bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
                           struct ctdb_request *req, uint32_t *recmaster)
@@ -47,6 +49,7 @@ bool ctdb_getrecmaster_recv(struct ctdb_connection *ctdb,
                DEBUG(ctdb, LOG_ERR, "ctdb_getrecmaster_recv: status -1");
                return false;
        }
+       /* Note: data is stashed in status - see ctdb_control_dispatch() */
        *recmaster = reply->status;
        return true;
 }
@@ -74,6 +77,7 @@ bool ctdb_getrecmode_recv(struct ctdb_connection *ctdb,
                DEBUG(ctdb, LOG_ERR, "ctdb_getrecmode_recv: status -1");
                return false;
        }
+       /* Note: data is stashed in status - see ctdb_control_dispatch() */
        *recmode = reply->status;
        return true;
 }
@@ -101,6 +105,7 @@ bool ctdb_getpnn_recv(struct ctdb_connection *ctdb,
                DEBUG(ctdb, LOG_ERR, "ctdb_getpnn_recv: status -1");
                return false;
        }
+       /* Note: data is stashed in status - see ctdb_control_dispatch() */
        *pnn = reply->status;
        return true;
 }
@@ -119,7 +124,9 @@ bool ctdb_getdbstat_recv(struct ctdb_connection *ctdb,
                         struct ctdb_db_statistics **stat)
 {
        struct ctdb_reply_control *reply;
-       struct ctdb_db_statistics *s;
+       struct ctdb_db_statistics *s, *wire;
+       int i;
+       char *ptr;
 
        reply = unpack_reply_control(req, CTDB_CONTROL_GET_DB_STATISTICS);
        if (!reply) {
@@ -129,16 +136,26 @@ bool ctdb_getdbstat_recv(struct ctdb_connection *ctdb,
                DEBUG(ctdb, LOG_ERR, "ctdb_getpnn_recv: status -1");
                return false;
        }
-       if (reply->datalen != sizeof(struct ctdb_db_statistics)) {
-               DEBUG(ctdb, LOG_ERR, "ctdb_getdbstat_recv: returned data is %d bytes but should be %d", reply->datalen, (int)sizeof(struct ctdb_db_statistics));
+       if (reply->datalen < offsetof(struct ctdb_db_statistics, hot_keys_wire)) {
+               DEBUG(ctdb, LOG_ERR, "ctdb_getdbstat_recv: returned data is %d bytes but should be >= %d", reply->datalen, (int)sizeof(struct ctdb_db_statistics));
                return false;
        }
 
+       wire = (struct ctdb_db_statistics *)reply->data;
+
        s = malloc(sizeof(struct ctdb_db_statistics));
        if (!s) {
                return false;
        }
-       memcpy(s, reply->data, sizeof(struct ctdb_db_statistics));
+
+       *s = *wire;
+       ptr = &wire->hot_keys_wire[0];
+       for (i = 0; i < wire->num_hot_keys; i++) {
+               s->hot_keys[i].key.dptr = malloc(s->hot_keys[i].key.dsize);
+               memcpy(s->hot_keys[i].key.dptr, ptr, s->hot_keys[i].key.dsize);
+               ptr += s->hot_keys[i].key.dsize;
+       }
+
        *stat = s;
 
        return true;
@@ -158,9 +175,18 @@ struct ctdb_request *ctdb_getdbstat_send(struct ctdb_connection *ctdb,
 
 void ctdb_free_dbstat(struct ctdb_db_statistics *stat)
 {
+       int i;
+
        if (stat == NULL) {
                return;
        }
+
+       for (i = 0; i < stat->num_hot_keys; i++) {
+               if (stat->hot_keys[i].key.dptr != NULL) {
+                       free(stat->hot_keys[i].key.dptr);
+               }
+       }
+
        free(stat);
 }
 
@@ -483,3 +509,30 @@ struct ctdb_request *ctdb_getvnnmap_send(struct ctdb_connection *ctdb,
                                        NULL, 0, callback, private_data);
 }
 
+bool ctdb_getcapabilities_recv(struct ctdb_connection *ctdb,
+                              struct ctdb_request *req, uint32_t *capabilities)
+{
+       struct ctdb_reply_control *reply;
+
+       reply = unpack_reply_control(req, CTDB_CONTROL_GET_CAPABILITIES);
+       if (!reply) {
+               return false;
+       }
+       if (reply->status == -1) {
+               DEBUG(ctdb, LOG_ERR, "ctdb_getcapabilities_recv: status -1");
+               return false;
+       }
+       *capabilities = *((uint32_t *)reply->data);
+       return true;
+}
+
+struct ctdb_request *ctdb_getcapabilities_send(struct ctdb_connection *ctdb,
+                                              uint32_t destnode,
+                                              ctdb_callback_t callback,
+                                              void *private_data)
+{
+       return new_ctdb_control_request(ctdb, CTDB_CONTROL_GET_CAPABILITIES,
+                                       destnode,
+                                       NULL, 0, callback, private_data);
+}
+