ctdb-client: Rename ctdb_db_travese to ctdb_db_traverse_local
[sfrench/samba-autobuild/.git] / ctdb / tools / ctdb.c
index 491cbfca961ba024dae47412cfd9fc8db04f9e1c..a55812a077ef15c842a6b747046bd4f0b59f7d21 100644 (file)
@@ -1,62 +1,60 @@
-/* 
-   ctdb control tool
+/*
+   CTDB control tool
 
-   Copyright (C) Andrew Tridgell  2007
-   Copyright (C) Ronnie Sahlberg  2007
+   Copyright (C) Amitay Isaacs  2015
 
    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 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    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, see <http://www.gnu.org/licenses/>.
 */
 
 #include "replace.h"
-#include "system/time.h"
-#include "system/filesys.h"
 #include "system/network.h"
-#include "system/locale.h"
+#include "system/filesys.h"
+#include "system/time.h"
+#include "system/wait.h"
+#include "system/dir.h"
 
+#include <ctype.h>
 #include <popt.h>
 #include <talloc.h>
 #include <tevent.h>
 #include <tdb.h>
 
-#include "lib/tdb_wrap/tdb_wrap.h"
-#include "lib/util/dlinklist.h"
-#include "lib/util/debug.h"
-#include "lib/util/substitute.h"
-#include "lib/util/time.h"
-
 #include "ctdb_version.h"
-#include "ctdb_private.h"
-#include "ctdb_client.h"
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+#include "lib/util/sys_rw.h"
 
-#include "common/cmdline.h"
-#include "common/rb_tree.h"
-#include "common/system.h"
-#include "common/common.h"
+#include "common/db_hash.h"
 #include "common/logging.h"
+#include "protocol/protocol.h"
+#include "protocol/protocol_api.h"
+#include "common/system.h"
+#include "client/client.h"
 
-#define ERR_TIMEOUT    20      /* timed out trying to reach node */
-#define ERR_NONODE     21      /* node does not exist */
-#define ERR_DISNODE    22      /* node is disconnected */
+#define TIMEOUT()      timeval_current_ofs(options.timelimit, 0)
 
-static void usage(void);
+#define SRVID_CTDB_TOOL    (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
+#define SRVID_CTDB_PUSHDB  (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
 
 static struct {
+       const char *socket;
+       const char *debuglevelstr;
        int timelimit;
-       uint32_t pnn;
-       uint32_t *nodes;
+       int pnn;
        int machinereadable;
-       const char *machineseparator;
+       const char *sep;
+       int machineparsable;
        int verbose;
        int maxruntime;
        int printemptyrecords;
@@ -66,93 +64,44 @@ static struct {
        int printrecordflags;
 } options;
 
-#define LONGTIMEOUT options.timelimit*10
-
-#define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
-#define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
-
-static double timeval_delta(struct timeval *tv2, struct timeval *tv)
-{
-       return (tv2->tv_sec - tv->tv_sec) +
-               (tv2->tv_usec - tv->tv_usec)*1.0e-6;
-}
-
-static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       printf("CTDB version: %s\n", CTDB_VERSION_STRING);
-       return 0;
-}
+static poptContext pc;
 
-/* Like printf(3) but substitute for separator in format */
-static int printm(const char *format, ...) PRINTF_ATTRIBUTE(1,2);
-static int printm(const char *format, ...)
-{
-       va_list ap;
-       int ret;
-       size_t len = strlen(format);
-       char new_format[len+1];
-
-       strncpy(new_format, format, len+1);
-
-       if (options.machineseparator[0] != ':') {
-               all_string_sub(new_format,
-                              ":", options.machineseparator, len + 1);
-       }
-
-       va_start(ap, format);
-       ret = vprintf(new_format, ap);
-       va_end(ap);
+struct ctdb_context {
+       struct tevent_context *ev;
+       struct ctdb_client_context *client;
+       struct ctdb_node_map *nodemap;
+       uint32_t pnn, cmd_pnn;
+       uint64_t srvid;
+};
 
-       return ret;
-}
+static void usage(const char *command);
 
-#define CTDB_NOMEM_ABORT(p) do { if (!(p)) {                           \
-               DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n",            \
-                                  "Out of memory in " __location__ )); \
-               abort();                                                \
-       }} while (0)
+/*
+ * Utility Functions
+ */
 
-static uint32_t getpnn(struct ctdb_context *ctdb)
+static double timeval_delta(struct timeval *tv2, struct timeval *tv)
 {
-       if ((options.pnn == CTDB_BROADCAST_ALL) ||
-           (options.pnn == CTDB_MULTICAST)) {
-               DEBUG(DEBUG_ERR,
-                     ("Cannot get PNN for node %u\n", options.pnn));
-               exit(1);
-       }
-
-       if (options.pnn == CTDB_CURRENT_NODE) {
-               return ctdb_get_pnn(ctdb);
-       } else {
-               return options.pnn;
-       }
+       return (tv2->tv_sec - tv->tv_sec) +
+              (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
 }
 
-static void assert_single_node_only(void)
+static struct ctdb_node_and_flags *get_node_by_pnn(
+                                       struct ctdb_node_map *nodemap,
+                                       uint32_t pnn)
 {
-       if ((options.pnn == CTDB_BROADCAST_ALL) ||
-           (options.pnn == CTDB_MULTICAST)) {
-               DEBUG(DEBUG_ERR,
-                     ("This control can not be applied to multiple PNNs\n"));
-               exit(1);
-       }
-}
+       int i;
 
-static void assert_current_node_only(struct ctdb_context *ctdb)
-{
-       if (options.pnn != ctdb_get_pnn(ctdb)) {
-               DEBUG(DEBUG_ERR,
-                     ("This control can only be applied to the current node\n"));
-               exit(1);
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].pnn == pnn) {
+                       return &nodemap->node[i];
+               }
        }
+       return NULL;
 }
 
-/* Pretty print the flags to a static buffer in human-readable format.
- * This never returns NULL!
- */
-static const char *pretty_print_flags(uint32_t flags)
+static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
 {
-       int j;
        static const struct {
                uint32_t flag;
                const char *name;
@@ -165,5907 +114,6322 @@ static const char *pretty_print_flags(uint32_t flags)
                { NODE_FLAGS_STOPPED,               "STOPPED" },
                { NODE_FLAGS_INACTIVE,              "INACTIVE" },
        };
-       static char flags_str[512]; /* Big enough to contain all flag names */
+       char *flags_str = NULL;
+       int i;
 
-       flags_str[0] = '\0';
-       for (j=0;j<ARRAY_SIZE(flag_names);j++) {
-               if (flags & flag_names[j].flag) {
-                       if (flags_str[0] == '\0') {
-                               (void) strcpy(flags_str, flag_names[j].name);
+       for (i=0; i<ARRAY_SIZE(flag_names); i++) {
+               if (flags & flag_names[i].flag) {
+                       if (flags_str == NULL) {
+                               flags_str = talloc_asprintf(mem_ctx,
+                                               "%s", flag_names[i].name);
                        } else {
-                               (void) strncat(flags_str, "|", sizeof(flags_str)-1);
-                               (void) strncat(flags_str, flag_names[j].name,
-                                              sizeof(flags_str)-1);
+                               flags_str = talloc_asprintf_append(flags_str,
+                                               "|%s", flag_names[i].name);
+                       }
+                       if (flags_str == NULL) {
+                               return "OUT-OF-MEMORY";
                        }
                }
        }
-       if (flags_str[0] == '\0') {
-               (void) strcpy(flags_str, "OK");
+       if (flags_str == NULL) {
+               return "OK";
        }
 
        return flags_str;
 }
 
-static int h2i(char h)
+static uint64_t next_srvid(struct ctdb_context *ctdb)
 {
-       if (h >= 'a' && h <= 'f') return h - 'a' + 10;
-       if (h >= 'A' && h <= 'F') return h - 'f' + 10;
-       return h - '0';
+       ctdb->srvid += 1;
+       return ctdb->srvid;
 }
 
-static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
+/*
+ * Get consistent nodemap information.
+ *
+ * If nodemap is already cached, use that. If not get it.
+ * If the current node is BANNED, then get nodemap from "better" node.
+ */
+static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
 {
-       int i;
-       TDB_DATA key = {NULL, 0};
+       TALLOC_CTX *tmp_ctx;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_node_and_flags *node;
+       uint32_t current_node;
+       int ret;
 
-       if (len & 0x01) {
-               DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
-               return key;
+       if (force) {
+               TALLOC_FREE(ctdb->nodemap);
        }
 
-       key.dsize = len>>1;
-       key.dptr  = talloc_size(mem_ctx, key.dsize);
-
-       for (i=0; i < len/2; i++) {
-               key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
+       if (ctdb->nodemap != NULL) {
+               return ctdb->nodemap;
        }
-       return key;
-}
 
-static TDB_DATA strtodata(TALLOC_CTX *mem_ctx, const char *str, size_t len)
-{
-       TDB_DATA key;
-
-       if (!strncmp(str, "0x", 2)) {
-               key = hextodata(mem_ctx, str + 2, len - 2);
-       } else {
-               key.dptr  = talloc_memdup(mem_ctx, str, len);
-               key.dsize = len;
+       tmp_ctx = talloc_new(ctdb);
+       if (tmp_ctx == NULL) {
+               return false;
        }
 
-       return key;
-}
-
-/* Parse a nodestring.  Parameter dd_ok controls what happens to nodes
- * that are disconnected or deleted.  If dd_ok is true those nodes are
- * included in the output list of nodes.  If dd_ok is false, those
- * nodes are filtered from the "all" case and cause an error if
- * explicitly specified.
- */
-static bool parse_nodestring(struct ctdb_context *ctdb,
-                            TALLOC_CTX *mem_ctx,
-                            const char * nodestring,
-                            uint32_t current_pnn,
-                            bool dd_ok,
-                            uint32_t **nodes,
-                            uint32_t *pnn_mode)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
-       int n;
-       uint32_t i;
-       struct ctdb_node_map_old *nodemap;
-       int ret;
-
-       *nodes = NULL;
-
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
+       current_node = ctdb->pnn;
+again:
+       ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
+                                   current_node, TIMEOUT(), &nodemap);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
-               talloc_free(tmp_ctx);
-               exit(10);
-       }
-
-       if (nodestring != NULL) {
-               *nodes = talloc_array(mem_ctx, uint32_t, 0);
-               if (*nodes == NULL) {
-                       goto failed;
-               }
-
-               n = 0;
-
-               if (strcmp(nodestring, "all") == 0) {
-                       *pnn_mode = CTDB_BROADCAST_ALL;
-
-                       /* all */
-                       for (i = 0; i < nodemap->num; i++) {
-                               if ((nodemap->nodes[i].flags &
-                                    (NODE_FLAGS_DISCONNECTED |
-                                     NODE_FLAGS_DELETED)) && !dd_ok) {
-                                       continue;
-                               }
-                               *nodes = talloc_realloc(mem_ctx, *nodes,
-                                                       uint32_t, n+1);
-                               if (*nodes == NULL) {
-                                       goto failed;
-                               }
-                               (*nodes)[n] = i;
-                               n++;
-                       }
-               } else {
-                       /* x{,y...} */
-                       char *ns, *tok;
-
-                       ns = talloc_strdup(tmp_ctx, nodestring);
-                       tok = strtok(ns, ",");
-                       while (tok != NULL) {
-                               uint32_t pnn;
-                               char *endptr;
-                               i = (uint32_t)strtoul(tok, &endptr, 0);
-                               if (i == 0 && tok == endptr) {
-                                       DEBUG(DEBUG_ERR,
-                                             ("Invalid node %s\n", tok));
-                                       talloc_free(tmp_ctx);
-                                       exit(ERR_NONODE);
-                               }
-                               if (i >= nodemap->num) {
-                                       DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
-                                       talloc_free(tmp_ctx);
-                                       exit(ERR_NONODE);
-                               }
-                               if ((nodemap->nodes[i].flags & 
-                                    (NODE_FLAGS_DISCONNECTED |
-                                     NODE_FLAGS_DELETED)) && !dd_ok) {
-                                       DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
-                                       talloc_free(tmp_ctx);
-                                       exit(ERR_DISNODE);
-                               }
-                               if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
-                                       DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
-                                       talloc_free(tmp_ctx);
-                                       exit(10);
-                               }
-
-                               *nodes = talloc_realloc(mem_ctx, *nodes,
-                                                       uint32_t, n+1);
-                               if (*nodes == NULL) {
-                                       goto failed;
-                               }
-
-                               (*nodes)[n] = i;
-                               n++;
-
-                               tok = strtok(NULL, ",");
+               fprintf(stderr, "Failed to get nodemap from node %u\n",
+                       current_node);
+               goto failed;
+       }
+
+       node = get_node_by_pnn(nodemap, current_node);
+       if (node->flags & NODE_FLAGS_BANNED) {
+               /* Pick next node */
+               do {
+                       current_node = (current_node + 1) % nodemap->num;
+                       node = get_node_by_pnn(nodemap, current_node);
+                       if (! (node->flags &
+                             (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
+                               break;
                        }
-                       talloc_free(ns);
+               } while (current_node != ctdb->pnn);
 
-                       if (n == 1) {
-                               *pnn_mode = (*nodes)[0];
-                       } else {
-                               *pnn_mode = CTDB_MULTICAST;
-                       }
-               }
-       } else {
-               /* default - no nodes specified */
-               *nodes = talloc_array(mem_ctx, uint32_t, 1);
-               if (*nodes == NULL) {
+               if (current_node == ctdb->pnn) {
+                       /* Tried all nodes in the cluster */
+                       fprintf(stderr, "Warning: All nodes are banned.\n");
                        goto failed;
                }
-               *pnn_mode = CTDB_CURRENT_NODE;
 
-               if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
-                       goto failed;
-               }
+               goto again;
        }
 
-       talloc_free(tmp_ctx);
-       return true;
+       ctdb->nodemap = talloc_steal(ctdb, nodemap);
+       return nodemap;
 
 failed:
        talloc_free(tmp_ctx);
-       return false;
+       return NULL;
 }
 
-/*
- check if a database exists
-*/
-static bool db_exists(struct ctdb_context *ctdb, const char *dbarg,
-                     uint32_t *dbid, const char **dbname, uint8_t *flags)
-{
-       int i, ret;
-       struct ctdb_dbid_map_old *dbmap=NULL;
-       bool dbid_given = false, found = false;
-       uint32_t id;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       const char *name = NULL;
+static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
+{
+       struct ctdb_node_map *nodemap;
+       bool found;
+       int i;
 
-       ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
-               goto fail;
+       if (pnn == -1) {
+               return false;
        }
 
-       if (strncmp(dbarg, "0x", 2) == 0) {
-               id = strtoul(dbarg, NULL, 0);
-               dbid_given = true;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return false;
        }
 
-       for(i=0; i<dbmap->num; i++) {
-               if (dbid_given) {
-                       if (id == dbmap->dbs[i].db_id) {
-                               found = true;
-                               break;
-                       }
-               } else {
-                       ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
-                       if (ret != 0) {
-                               DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
-                               goto fail;
-                       }
-
-                       if (strcmp(name, dbarg) == 0) {
-                               id = dbmap->dbs[i].db_id;
-                               found = true;
-                               break;
-                       }
+       found = false;
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].pnn == pnn) {
+                       found = true;
+                       break;
                }
        }
-
-       if (found && dbid_given && dbname != NULL) {
-               ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, tmp_ctx, &name);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].db_id));
-                       found = false;
-                       goto fail;
-               }
+       if (! found) {
+               fprintf(stderr, "Node %u does not exist\n", pnn);
+               return false;
        }
 
-       if (found) {
-               if (dbid) *dbid = id;
-               if (dbname) *dbname = talloc_strdup(ctdb, name);
-               if (flags) *flags = dbmap->dbs[i].flags;
-       } else {
-               DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
+       if (nodemap->node[i].flags &
+           (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
+               fprintf(stderr, "Node %u has status %s\n", pnn,
+                       pretty_print_flags(ctdb, nodemap->node[i].flags));
+               return false;
        }
 
-fail:
-       talloc_free(tmp_ctx);
-       return found;
+       return true;
 }
 
-/*
-  see if a process exists
- */
-static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
+static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
+                                           struct ctdb_node_map *nodemap)
 {
-       uint32_t pnn, pid;
-       int ret;
-       if (argc < 1) {
-               usage();
-       }
+       struct ctdb_node_map *nodemap2;
 
-       if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
-               DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
-               return -1;
+       nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
+       if (nodemap2 == NULL) {
+               return NULL;
        }
 
-       ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
-       if (ret == 0) {
-               printf("%u:%u exists\n", pnn, pid);
-       } else {
-               printf("%u:%u does not exist\n", pnn, pid);
+       nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
+                                     nodemap->num);
+       if (nodemap2->node == NULL) {
+               talloc_free(nodemap2);
+               return NULL;
        }
-       return ret;
+
+       return nodemap2;
 }
 
 /*
-  display statistics structure
+ * Get the number and the list of matching nodes
+ *
+ *   nodestring :=  NULL | all | pnn,[pnn,...]
+ *
+ * If nodestring is NULL, use the current node.
  */
-static void show_statistics(struct ctdb_statistics *s, int show_header)
+static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            const char *nodestring,
+                            struct ctdb_node_map **out)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+       struct ctdb_node_map *nodemap, *nodemap2;
+       struct ctdb_node_and_flags *node;
        int i;
-       const char *prefix=NULL;
-       int preflen=0;
-       int tmp, days, hours, minutes, seconds;
-       const struct {
-               const char *name;
-               uint32_t offset;
-       } fields[] = {
-#define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
-               STATISTICS_FIELD(num_clients),
-               STATISTICS_FIELD(frozen),
-               STATISTICS_FIELD(recovering),
-               STATISTICS_FIELD(num_recoveries),
-               STATISTICS_FIELD(client_packets_sent),
-               STATISTICS_FIELD(client_packets_recv),
-               STATISTICS_FIELD(node_packets_sent),
-               STATISTICS_FIELD(node_packets_recv),
-               STATISTICS_FIELD(keepalive_packets_sent),
-               STATISTICS_FIELD(keepalive_packets_recv),
-               STATISTICS_FIELD(node.req_call),
-               STATISTICS_FIELD(node.reply_call),
-               STATISTICS_FIELD(node.req_dmaster),
-               STATISTICS_FIELD(node.reply_dmaster),
-               STATISTICS_FIELD(node.reply_error),
-               STATISTICS_FIELD(node.req_message),
-               STATISTICS_FIELD(node.req_control),
-               STATISTICS_FIELD(node.reply_control),
-               STATISTICS_FIELD(client.req_call),
-               STATISTICS_FIELD(client.req_message),
-               STATISTICS_FIELD(client.req_control),
-               STATISTICS_FIELD(timeouts.call),
-               STATISTICS_FIELD(timeouts.control),
-               STATISTICS_FIELD(timeouts.traverse),
-               STATISTICS_FIELD(locks.num_calls),
-               STATISTICS_FIELD(locks.num_current),
-               STATISTICS_FIELD(locks.num_pending),
-               STATISTICS_FIELD(locks.num_failed),
-               STATISTICS_FIELD(total_calls),
-               STATISTICS_FIELD(pending_calls),
-               STATISTICS_FIELD(childwrite_calls),
-               STATISTICS_FIELD(pending_childwrite_calls),
-               STATISTICS_FIELD(memory_used),
-               STATISTICS_FIELD(max_hop_count),
-               STATISTICS_FIELD(total_ro_delegations),
-               STATISTICS_FIELD(total_ro_revokes),
-       };
-       
-       tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
-       seconds = tmp%60;
-       tmp    /= 60;
-       minutes = tmp%60;
-       tmp    /= 60;
-       hours   = tmp%24;
-       tmp    /= 24;
-       days    = tmp;
 
-       if (options.machinereadable){
-               if (show_header) {
-                       printm("CTDB version:");
-                       printm("Current time of statistics:");
-                       printm("Statistics collected since:");
-                       for (i=0;i<ARRAY_SIZE(fields);i++) {
-                               printm("%s:", fields[i].name);
-                       }
-                       printm("num_reclock_ctdbd_latency:");
-                       printm("min_reclock_ctdbd_latency:");
-                       printm("avg_reclock_ctdbd_latency:");
-                       printm("max_reclock_ctdbd_latency:");
-
-                       printm("num_reclock_recd_latency:");
-                       printm("min_reclock_recd_latency:");
-                       printm("avg_reclock_recd_latency:");
-                       printm("max_reclock_recd_latency:");
-
-                       printm("num_call_latency:");
-                       printm("min_call_latency:");
-                       printm("avg_call_latency:");
-                       printm("max_call_latency:");
-
-                       printm("num_lockwait_latency:");
-                       printm("min_lockwait_latency:");
-                       printm("avg_lockwait_latency:");
-                       printm("max_lockwait_latency:");
-
-                       printm("num_childwrite_latency:");
-                       printm("min_childwrite_latency:");
-                       printm("avg_childwrite_latency:");
-                       printm("max_childwrite_latency:");
-                       printm("\n");
-               }
-               printm("%d:", CTDB_PROTOCOL);
-               printm("%d:", (int)s->statistics_current_time.tv_sec);
-               printm("%d:", (int)s->statistics_start_time.tv_sec);
-               for (i=0;i<ARRAY_SIZE(fields);i++) {
-                       printm("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
-               }
-               printm("%d:", s->reclock.ctdbd.num);
-               printm("%.6f:", s->reclock.ctdbd.min);
-               printm("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
-               printm("%.6f:", s->reclock.ctdbd.max);
-
-               printm("%d:", s->reclock.recd.num);
-               printm("%.6f:", s->reclock.recd.min);
-               printm("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
-               printm("%.6f:", s->reclock.recd.max);
-
-               printm("%d:", s->call_latency.num);
-               printm("%.6f:", s->call_latency.min);
-               printm("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
-               printm("%.6f:", s->call_latency.max);
-
-               printm("%d:", s->childwrite_latency.num);
-               printm("%.6f:", s->childwrite_latency.min);
-               printm("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
-               printm("%.6f:", s->childwrite_latency.max);
-               printm("\n");
-       } else {
-               printf("CTDB version %u\n", CTDB_PROTOCOL);
-               printf("Current time of statistics  :                %s", ctime(&s->statistics_current_time.tv_sec));
-               printf("Statistics collected since  : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
-
-               for (i=0;i<ARRAY_SIZE(fields);i++) {
-                       if (strchr(fields[i].name, '.')) {
-                               preflen = strcspn(fields[i].name, ".")+1;
-                               if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
-                                       prefix = fields[i].name;
-                                       printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
-                               }
-                       } else {
-                               preflen = 0;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return false;
+       }
+
+       nodemap2 = talloc_nodemap(mem_ctx, nodemap);
+       if (nodemap2 == NULL) {
+               return false;
+       }
+
+       if (nodestring == NULL) {
+               for (i=0; i<nodemap->num; i++) {
+                       if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
+                               nodemap2->node[0] = nodemap->node[i];
+                               break;
                        }
-                       printf(" %*s%-22s%*s%10u\n", 
-                              preflen?4:0, "",
-                              fields[i].name+preflen, 
-                              preflen?0:4, "",
-                              *(uint32_t *)(fields[i].offset+(uint8_t *)s));
-               }
-               printf(" hop_count_buckets:");
-               for (i=0;i<MAX_COUNT_BUCKETS;i++) {
-                       printf(" %d", s->hop_count_bucket[i]);
                }
-               printf("\n");
-               printf(" lock_buckets:");
-               for (i=0; i<MAX_COUNT_BUCKETS; i++) {
-                       printf(" %d", s->locks.buckets[i]);
+               nodemap2->num = 1;
+
+               goto done;
+       }
+
+       if (strcmp(nodestring, "all") == 0) {
+               for (i=0; i<nodemap->num; i++) {
+                       nodemap2->node[i] = nodemap->node[i];
                }
-               printf("\n");
-               printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "locks_latency      MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
+               nodemap2->num = nodemap->num;
 
-               printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd      MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
+               goto done;
+       } else {
+               char *ns, *tok;
 
-               printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd       MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
+               ns = talloc_strdup(mem_ctx, nodestring);
+               if (ns == NULL) {
+                       return false;
+               }
 
-               printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "call_latency       MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
-               printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
-       }
+               tok = strtok(ns, ",");
+               while (tok != NULL) {
+                       uint32_t pnn;
+                       char *endptr;
 
-       talloc_free(tmp_ctx);
-}
+                       pnn = (uint32_t)strtoul(tok, &endptr, 0);
+                       if (pnn == 0 && tok == endptr) {
+                               fprintf(stderr, "Invalid node %s\n", tok);
+                                       return false;
+                       }
 
-/*
-  display remote ctdb statistics combined from all nodes
- */
-static int control_statistics_all(struct ctdb_context *ctdb)
-{
-       int ret, i;
-       struct ctdb_statistics statistics;
-       uint32_t *nodes;
-       uint32_t num_nodes;
+                       node = get_node_by_pnn(nodemap, pnn);
+                       if (node == NULL) {
+                               fprintf(stderr, "Node %u does not exist\n",
+                                       pnn);
+                               return false;
+                       }
 
-       nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
-       CTDB_NO_MEMORY(ctdb, nodes);
-       
-       ZERO_STRUCT(statistics);
+                       nodemap2->node[nodemap2->num] = *node;
+                       nodemap2->num += 1;
 
-       for (i=0;i<num_nodes;i++) {
-               struct ctdb_statistics s1;
-               int j;
-               uint32_t *v1 = (uint32_t *)&s1;
-               uint32_t *v2 = (uint32_t *)&statistics;
-               uint32_t num_ints = 
-                       offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
-               ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
-                       return ret;
-               }
-               for (j=0;j<num_ints;j++) {
-                       v2[j] += v1[j];
+                       tok = strtok(NULL, ",");
                }
-               statistics.max_hop_count = 
-                       MAX(statistics.max_hop_count, s1.max_hop_count);
-               statistics.call_latency.max = 
-                       MAX(statistics.call_latency.max, s1.call_latency.max);
        }
-       talloc_free(nodes);
-       printf("Gathered statistics for %u nodes\n", num_nodes);
-       show_statistics(&statistics, 1);
-       return 0;
+
+done:
+       *out = nodemap2;
+       return true;
 }
 
-/*
-  display remote ctdb statistics
- */
-static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
+/* Compare IP address */
+static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
 {
-       int ret;
-       struct ctdb_statistics statistics;
+       bool ret = false;
 
-       if (options.pnn == CTDB_BROADCAST_ALL) {
-               return control_statistics_all(ctdb);
+       if (ip1->sa.sa_family != ip2->sa.sa_family) {
+               return false;
        }
 
-       ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
-               return ret;
+       switch (ip1->sa.sa_family) {
+       case AF_INET:
+               ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
+                             sizeof(struct in_addr)) == 0);
+               break;
+
+       case AF_INET6:
+               ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
+                             sizeof(struct in6_addr)) == 0);
+               break;
        }
-       show_statistics(&statistics, 1);
-       return 0;
-}
 
+       return ret;
+}
 
-/*
-  reset remote ctdb statistics
- */
-static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
+/* Append a node to a node map with given address and flags */
+static bool node_map_add(struct ctdb_node_map *nodemap,
+                        const char *nstr, uint32_t flags)
 {
-       int ret;
+       ctdb_sock_addr addr;
+       uint32_t num;
+       struct ctdb_node_and_flags *n;
 
-       ret = ctdb_statistics_reset(ctdb, options.pnn);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
-               return ret;
+       if (! parse_ip(nstr, NULL, 0, &addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", nstr);
+               return false;
        }
-       return 0;
-}
 
+       num = nodemap->num;
+       nodemap->node = talloc_realloc(nodemap, nodemap->node,
+                                      struct ctdb_node_and_flags, num+1);
+       if (nodemap->node == NULL) {
+               return false;
+       }
 
-/*
-  display remote ctdb rolling statistics
- */
-static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
+       n = &nodemap->node[num];
+       n->addr = addr;
+       n->pnn = num;
+       n->flags = flags;
+
+       nodemap->num = num+1;
+       return true;
+}
+
+/* Read a nodes file into a node map */
+static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
+                                                 const char *nlist)
 {
-       int ret;
-       struct ctdb_statistics_list_old *stats;
-       int i, num_records = -1;
+       char **lines;
+       int nlines;
+       int i;
+       struct ctdb_node_map *nodemap;
 
-       assert_single_node_only();
+       nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
+       if (nodemap == NULL) {
+               return NULL;
+       }
 
-       if (argc ==1) {
-               num_records = atoi(argv[0]) - 1;
+       lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
+       if (lines == NULL) {
+               return NULL;
        }
 
-       ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
-               return ret;
+       while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
+               nlines--;
        }
-       for (i=0;i<stats->num;i++) {
-               if (stats->stats[i].statistics_start_time.tv_sec == 0) {
+
+       for (i=0; i<nlines; i++) {
+               char *node;
+               uint32_t flags;
+               size_t len;
+
+               node = lines[i];
+               /* strip leading spaces */
+               while((*node == ' ') || (*node == '\t')) {
+                       node++;
+               }
+
+               len = strlen(node);
+
+               /* strip trailing spaces */
+               while ((len > 1) &&
+                      ((node[len-1] == ' ') || (node[len-1] == '\t')))
+               {
+                       node[len-1] = '\0';
+                       len--;
+               }
+
+               if (len == 0) {
                        continue;
                }
-               show_statistics(&stats->stats[i], i==0);
-               if (i == num_records) {
-                       break;
+               if (*node == '#') {
+                       /* A "deleted" node is a node that is
+                          commented out in the nodes file.  This is
+                          used instead of removing a line, which
+                          would cause subsequent nodes to change
+                          their PNN. */
+                       flags = NODE_FLAGS_DELETED;
+                       node = discard_const("0.0.0.0");
+               } else {
+                       flags = 0;
+               }
+               if (! node_map_add(nodemap, node, flags)) {
+                       talloc_free(lines);
+                       TALLOC_FREE(nodemap);
+                       return NULL;
                }
        }
-       return 0;
-}
 
+       talloc_free(lines);
+       return nodemap;
+}
 
-/*
-  display remote ctdb db statistics
- */
-static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
+static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_db_statistics_old *dbstat;
-       int i;
-       uint32_t db_id;
-       int num_hot_keys;
-       int ret;
+       struct ctdb_node_map *nodemap;
+       char *nodepath;
+       const char *nodes_list = NULL;
 
-       if (argc < 1) {
-               usage();
+       if (pnn != CTDB_UNKNOWN_PNN) {
+               nodepath = talloc_asprintf(mem_ctx, "CTDB_NODES_%u", pnn);
+               if (nodepath != NULL) {
+                       nodes_list = getenv(nodepath);
+               }
        }
-
-       if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
-               return -1;
+       if (nodes_list == NULL) {
+               nodes_list = getenv("CTDB_NODES");
        }
-
-       ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-
-       printf("DB Statistics: %s\n", argv[0]);
-       printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
-               dbstat->db_ro_delegations);
-       printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
-               dbstat->db_ro_delegations);
-       printf(" %s\n", "locks");
-       printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
-               dbstat->locks.num_calls);
-       printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
-               dbstat->locks.num_failed);
-       printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
-               dbstat->locks.num_current);
-       printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
-               dbstat->locks.num_pending);
-       printf(" %s", "hop_count_buckets:");
-       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
-               printf(" %d", dbstat->hop_count_bucket[i]);
+       if (nodes_list == NULL) {
+               const char *basedir = getenv("CTDB_BASE");
+               if (basedir == NULL) {
+                       basedir = CTDB_ETCDIR;
+               }
+               nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
+               if (nodes_list == NULL) {
+                       fprintf(stderr, "Memory allocation error\n");
+                       return NULL;
+               }
        }
-       printf("\n");
-       printf(" %s", "lock_buckets:");
-       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
-               printf(" %d", dbstat->locks.buckets[i]);
+
+       nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
+       if (nodemap == NULL) {
+               fprintf(stderr, "Failed to read nodes file \"%s\"\n",
+                       nodes_list);
+               return NULL;
        }
-       printf("\n");
-       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
-               "locks_latency      MIN/AVG/MAX",
-               dbstat->locks.latency.min,
-               (dbstat->locks.latency.num ?
-                dbstat->locks.latency.total /dbstat->locks.latency.num :
-                0.0),
-               dbstat->locks.latency.max,
-               dbstat->locks.latency.num);
-       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
-               "vacuum_latency     MIN/AVG/MAX",
-               dbstat->vacuum.latency.min,
-               (dbstat->vacuum.latency.num ?
-                dbstat->vacuum.latency.total /dbstat->vacuum.latency.num :
-                0.0),
-               dbstat->vacuum.latency.max,
-               dbstat->vacuum.latency.num);
-       num_hot_keys = 0;
-       for (i=0; i<dbstat->num_hot_keys; i++) {
-               if (dbstat->hot_keys[i].count > 0) {
-                       num_hot_keys++;
-               }
-       }
-       dbstat->num_hot_keys = num_hot_keys;
-
-       printf(" Num Hot Keys:     %d\n", dbstat->num_hot_keys);
-       for (i = 0; i < dbstat->num_hot_keys; i++) {
-               int j;
-               printf("     Count:%d Key:", dbstat->hot_keys[i].count);
-               for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
-                       printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
+
+       return nodemap;
+}
+
+static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
+                                struct ctdb_context *ctdb,
+                                struct ctdb_dbid_map *dbmap,
+                                const char *db_name)
+{
+       struct ctdb_dbid *db = NULL;
+       const char *name;
+       int ret, i;
+
+       for (i=0; i<dbmap->num; i++) {
+               ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
+                                          ctdb->pnn, TIMEOUT(),
+                                          dbmap->dbs[i].db_id, &name);
+               if (ret != 0) {
+                       return false;
+               }
+
+               if (strcmp(db_name, name) == 0) {
+                       talloc_free(discard_const(name));
+                       db = &dbmap->dbs[i];
+                       break;
                }
-               printf("\n");
        }
 
-       talloc_free(tmp_ctx);
-       return 0;
+       return db;
 }
 
-/*
-  display uptime of remote node
- */
-static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
+static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                     const char *db_arg, uint32_t *db_id,
+                     const char **db_name, uint8_t *db_flags)
 {
-       int ret;
-       struct ctdb_uptime *uptime = NULL;
-       int tmp, days, hours, minutes, seconds;
+       struct ctdb_dbid_map *dbmap;
+       struct ctdb_dbid *db = NULL;
+       uint32_t id = 0;
+       const char *name = NULL;
+       int ret, i;
 
-       ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
+       ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
+                                 ctdb->pnn, TIMEOUT(), &dbmap);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
-               return ret;
+               return false;
        }
 
-       if (options.machinereadable){
-               printm(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
-               printm(":%u:%u:%u:%lf\n",
-                       (unsigned int)uptime->current_time.tv_sec,
-                       (unsigned int)uptime->ctdbd_start_time.tv_sec,
-                       (unsigned int)uptime->last_recovery_finished.tv_sec,
-                       timeval_delta(&uptime->last_recovery_finished,
-                                     &uptime->last_recovery_started)
-               );
-               return 0;
+       if (strncmp(db_arg, "0x", 2) == 0) {
+               id = strtoul(db_arg, NULL, 0);
+               for (i=0; i<dbmap->num; i++) {
+                       if (id == dbmap->dbs[i].db_id) {
+                               db = &dbmap->dbs[i];
+                               break;
+                       }
+               }
+       } else {
+               name = db_arg;
+               db = db_find(mem_ctx, ctdb, dbmap, name);
        }
 
-       printf("Current time of node          :                %s", ctime(&uptime->current_time.tv_sec));
+       if (db == NULL) {
+               fprintf(stderr, "No database matching '%s' found\n", db_arg);
+               return false;
+       }
 
-       tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
-       seconds = tmp%60;
-       tmp    /= 60;
-       minutes = tmp%60;
-       tmp    /= 60;
-       hours   = tmp%24;
-       tmp    /= 24;
-       days    = tmp;
-       printf("Ctdbd start time              : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
+       if (name == NULL) {
+               ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
+                                          ctdb->pnn, TIMEOUT(), id, &name);
+               if (ret != 0) {
+                       return false;
+               }
+       }
 
-       tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
-       seconds = tmp%60;
-       tmp    /= 60;
-       minutes = tmp%60;
-       tmp    /= 60;
-       hours   = tmp%24;
-       tmp    /= 24;
-       days    = tmp;
-       printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
-       
-       printf("Duration of last recovery/failover: %lf seconds\n",
-               timeval_delta(&uptime->last_recovery_finished,
-                             &uptime->last_recovery_started));
+       if (db_id != NULL) {
+               *db_id = db->db_id;
+       }
+       if (db_name != NULL) {
+               *db_name = talloc_strdup(mem_ctx, name);
+       }
+       if (db_flags != NULL) {
+               *db_flags = db->flags;
+       }
+       return true;
+}
 
-       return 0;
+static int h2i(char h)
+{
+       if (h >= 'a' && h <= 'f') {
+               return h - 'a' + 10;
+       }
+       if (h >= 'A' && h <= 'F') {
+               return h - 'f' + 10;
+       }
+       return h - '0';
 }
 
-/*
-  show the PNN of the current node
- */
-static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
+static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
+                      TDB_DATA *out)
 {
-       uint32_t mypnn;
+       int i;
+       TDB_DATA data;
 
-       mypnn = getpnn(ctdb);
+       if (len & 0x01) {
+               fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
+                       str);
+               return EINVAL;
+       }
+
+       data.dsize = len / 2;
+       data.dptr = talloc_size(mem_ctx, data.dsize);
+       if (data.dptr == NULL) {
+               return ENOMEM;
+       }
+
+       for (i=0; i<data.dsize; i++) {
+               data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
+       }
 
-       printf("PNN:%d\n", mypnn);
+       *out = data;
        return 0;
 }
 
-
-static struct ctdb_node_map_old *read_nodes_file(TALLOC_CTX *mem_ctx)
+static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
+                      TDB_DATA *out)
 {
-       const char *nodes_list;
+       TDB_DATA data;
+       int ret = 0;
 
-       /* read the nodes file */
-       nodes_list = getenv("CTDB_NODES");
-       if (nodes_list == NULL) {
-               nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
-                                            getenv("CTDB_BASE"));
-               if (nodes_list == NULL) {
-                       DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
-                       exit(1);
+       if (strncmp(str, "0x", 2) == 0) {
+               ret = hex_to_data(str+2, len-2, mem_ctx, &data);
+       } else {
+               data.dptr = talloc_memdup(mem_ctx, str, len);
+               if (data.dptr == NULL) {
+                       return ENOMEM;
                }
+               data.dsize = len;
        }
 
-       return ctdb_read_nodes_file(mem_ctx, nodes_list);
+       *out = data;
+       return ret;
 }
 
-/*
-  show the PNN of the current node
-  discover the pnn by loading the nodes file and try to bind to all
-  addresses one at a time until the ip address is found.
- */
-static int find_node_xpnn(void)
+static int run_helper(TALLOC_CTX *mem_ctx, const char *command,
+                     const char *path, int argc, const char **argv)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
-       struct ctdb_node_map_old *node_map;
-       int i, pnn;
+       pid_t pid;
+       int save_errno, status, ret;
+       const char **new_argv;
+       int i;
 
-       node_map = read_nodes_file(mem_ctx);
-       if (node_map == NULL) {
-               talloc_free(mem_ctx);
-               return -1;
+       new_argv = talloc_array(mem_ctx, const char *, argc + 2);
+       if (new_argv == NULL) {
+               return ENOMEM;
        }
 
-       for (i = 0; i < node_map->num; i++) {
-               if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       continue;
-               }
-               if (ctdb_sys_have_ip(&node_map->nodes[i].addr)) {
-                       pnn = node_map->nodes[i].pnn;
-                       talloc_free(mem_ctx);
-                       return pnn;
-               }
+       new_argv[0] = path;
+       for (i=0; i<argc; i++) {
+               new_argv[i+1] = argv[i];
        }
+       new_argv[argc+1] = NULL;
 
-       printf("Failed to detect which PNN this node is\n");
-       talloc_free(mem_ctx);
-       return -1;
-}
+       pid = fork();
+       if (pid < 0) {
+               save_errno = errno;
+               talloc_free(new_argv);
+               fprintf(stderr, "Failed to fork %s (%s) - %s\n",
+                       command, path, strerror(save_errno));
+               return save_errno;
+       }
 
-static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       uint32_t pnn;
+       if (pid == 0) {
+               ret = execv(path, discard_const(new_argv));
+               if (ret == -1) {
+                       _exit(64+errno);
+               }
+               /* Should not happen */
+               _exit(64+ENOEXEC);
+       }
 
-       assert_single_node_only();
+       talloc_free(new_argv);
 
-       pnn = find_node_xpnn();
-       if (pnn == -1) {
-               return -1;
+       ret = waitpid(pid, &status, 0);
+       if (ret == -1) {
+               save_errno = errno;
+               fprintf(stderr, "waitpid() failed for %s - %s\n",
+                       command, strerror(save_errno));
+               return save_errno;
+       }
+
+       if (WIFEXITED(status)) {
+               int pstatus = WEXITSTATUS(status);
+               if (WIFSIGNALED(status)) {
+                       fprintf(stderr, "%s terminated with signal %d\n",
+                               command, WTERMSIG(status));
+                       ret = EINTR;
+               } else if (pstatus >= 64 && pstatus < 255) {
+                       fprintf(stderr, "%s failed with error %d\n",
+                               command, pstatus-64);
+                       ret = pstatus - 64;
+               } else {
+                       ret = pstatus;
+               }
+               return ret;
+       } else if (WIFSIGNALED(status)) {
+               fprintf(stderr, "%s terminated with signal %d\n",
+                       command, WTERMSIG(status));
+               return EINTR;
        }
 
-       printf("PNN:%d\n", pnn);
        return 0;
 }
 
-/* Helpers for ctdb status
+/*
+ * Command Functions
  */
-static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
+
+static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       int j;
-       bool ret = false;
+       printf("%s\n", CTDB_VERSION_STRING);
+       return 0;
+}
+
+static bool partially_online(TALLOC_CTX *mem_ctx,
+                            struct ctdb_context *ctdb,
+                            struct ctdb_node_and_flags *node)
+{
+       struct ctdb_iface_list *iface_list;
+       int ret, i;
+       bool status = false;
 
-       if (node->flags == 0) {
-               struct ctdb_iface_list_old *ifaces;
+       if (node->flags != 0) {
+               return false;
+       }
 
-               if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
-                                        tmp_ctx, &ifaces) == 0) {
-                       for (j=0; j < ifaces->num; j++) {
-                               if (ifaces->ifaces[j].link_state != 0) {
-                                       continue;
-                               }
-                               ret = true;
-                               break;
-                       }
+       ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
+                                  node->pnn, TIMEOUT(), &iface_list);
+       if (ret != 0) {
+               return false;
+       }
+
+       status = false;
+       for (i=0; i < iface_list->num; i++) {
+               if (iface_list->iface[i].link_state == 0) {
+                       status = true;
+                       break;
                }
        }
-       talloc_free(tmp_ctx);
 
-       return ret;
+       return status;
 }
 
-static void control_status_header_machine(void)
+static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
+                                 struct ctdb_context *ctdb,
+                                 struct ctdb_node_map *nodemap,
+                                 uint32_t mypnn)
 {
-       printm(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
-              ":Inactive:PartiallyOnline:ThisNode:\n");
-}
+       struct ctdb_node_and_flags *node;
+       int i;
 
-static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
-                                   struct ctdb_node_and_flags *node)
-{
-       printm(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
-              ctdb_addr_to_str(&node->addr),
-              !!(node->flags&NODE_FLAGS_DISCONNECTED),
-              !!(node->flags&NODE_FLAGS_BANNED),
-              !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
-              !!(node->flags&NODE_FLAGS_UNHEALTHY),
-              !!(node->flags&NODE_FLAGS_STOPPED),
-              !!(node->flags&NODE_FLAGS_INACTIVE),
-              is_partially_online(ctdb, node) ? 1 : 0,
-              (node->pnn == mypnn)?'Y':'N');
+       printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
+              options.sep,
+              "Node", options.sep,
+              "IP", options.sep,
+              "Disconnected", options.sep,
+              "Banned", options.sep,
+              "Disabled", options.sep,
+              "Unhealthy", options.sep,
+              "Stopped", options.sep,
+              "Inactive", options.sep,
+              "PartiallyOnline", options.sep,
+              "ThisNode", options.sep);
 
-       return node->flags;
-}
+       for (i=0; i<nodemap->num; i++) {
+               node = &nodemap->node[i];
+               if (node->flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
 
-static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
-                                 struct ctdb_node_and_flags *node)
-{
-       printf("pnn:%d %-16s %s%s\n", node->pnn,
-              ctdb_addr_to_str(&node->addr),
-              is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
-              node->pnn == mypnn?" (THIS NODE)":"");
+               printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
+                      options.sep,
+                      node->pnn, options.sep,
+                      ctdb_sock_addr_to_string(mem_ctx, &node->addr),
+                      options.sep,
+                      !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
+                      !! (node->flags & NODE_FLAGS_BANNED), options.sep,
+                      !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
+                      options.sep,
+                      !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
+                      !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
+                      !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
+                      partially_online(mem_ctx, ctdb, node), options.sep,
+                      (node->pnn == mypnn)?'Y':'N', options.sep);
+       }
 
-       return node->flags;
 }
 
-/*
-  display remote ctdb status
- */
-static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
+static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         struct ctdb_node_map *nodemap, uint32_t mypnn,
+                         bool print_header)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       int i;
-       struct ctdb_vnn_map *vnnmap=NULL;
-       struct ctdb_node_map_old *nodemap=NULL;
-       uint32_t recmode, recmaster, mypnn;
+       struct ctdb_node_and_flags *node;
        int num_deleted_nodes = 0;
-       int ret;
-
-       mypnn = getpnn(ctdb);
+       int i;
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
+                       num_deleted_nodes++;
+               }
        }
 
-       if (options.machinereadable) {
-               control_status_header_machine();
-               for (i=0;i<nodemap->num;i++) {
-                       if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                               continue;
-                       }
-                       (void) control_status_1_machine(ctdb, mypnn,
-                                                       &nodemap->nodes[i]);
+       if (print_header) {
+               if (num_deleted_nodes == 0) {
+                       printf("Number of nodes:%d\n", nodemap->num);
+               } else {
+                       printf("Number of nodes:%d "
+                              "(including %d deleted nodes)\n",
+                              nodemap->num, num_deleted_nodes);
                }
-               talloc_free(tmp_ctx);
-               return 0;
        }
 
        for (i=0; i<nodemap->num; i++) {
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       num_deleted_nodes++;
-               }
-       }
-       if (num_deleted_nodes == 0) {
-               printf("Number of nodes:%d\n", nodemap->num);
-       } else {
-               printf("Number of nodes:%d (including %d deleted nodes)\n",
-                      nodemap->num, num_deleted_nodes);
-       }
-       for(i=0;i<nodemap->num;i++){
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
+               node = &nodemap->node[i];
+               if (node->flags & NODE_FLAGS_DELETED) {
                        continue;
                }
-               (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
-       }
 
-       ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+               printf("pnn:%u %-16s %s%s\n",
+                      node->pnn,
+                      ctdb_sock_addr_to_string(mem_ctx, &node->addr),
+                      partially_online(mem_ctx, ctdb, node) ?
+                               "PARTIALLYONLINE" :
+                               pretty_print_flags(mem_ctx, node->flags),
+                      node->pnn == mypnn ? " (THIS NODE)" : "");
        }
+}
+
+static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        struct ctdb_node_map *nodemap, uint32_t mypnn,
+                        struct ctdb_vnn_map *vnnmap, int recmode,
+                        uint32_t recmaster)
+{
+       int i;
+
+       print_nodemap(mem_ctx, ctdb, nodemap, mypnn, true);
+
        if (vnnmap->generation == INVALID_GENERATION) {
                printf("Generation:INVALID\n");
        } else {
-               printf("Generation:%d\n",vnnmap->generation);
+               printf("Generation:%u\n", vnnmap->generation);
        }
-       printf("Size:%d\n",vnnmap->size);
-       for(i=0;i<vnnmap->size;i++){
+       printf("Size:%d\n", vnnmap->size);
+       for (i=0; i<vnnmap->size; i++) {
                printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
        }
 
-       ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-       printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
-
-       ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-       printf("Recovery master:%d\n",recmaster);
-
-       talloc_free(tmp_ctx);
-       return 0;
+       printf("Recovery mode:%s (%d)\n",
+              recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
+              recmode);
+       printf("Recovery master:%d\n", recmaster);
 }
 
-static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       int i, ret;
-       struct ctdb_node_map_old *nodemap=NULL;
-       uint32_t * nodes;
-       uint32_t pnn_mode, mypnn;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_vnn_map *vnnmap;
+       int recmode;
+       uint32_t recmaster;
+       int ret;
 
-       if (argc > 1) {
-               usage();
+       if (argc != 0) {
+               usage("status");
        }
 
-       if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
-                             options.pnn, true, &nodes, &pnn_mode)) {
-               return -1;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
        }
 
-       if (options.machinereadable) {
-               control_status_header_machine();
-       } else if (pnn_mode == CTDB_BROADCAST_ALL) {
-               printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
+       if (options.machinereadable == 1) {
+               print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
+               return 0;
        }
 
-       mypnn = getpnn(ctdb);
-
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
+       ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
+                                 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+               return ret;
        }
 
-       ret = 0;
+       ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), &recmode);
+       if (ret != 0) {
+               return ret;
+       }
 
-       for (i = 0; i < talloc_array_length(nodes); i++) {
-               if (options.machinereadable) {
-                       ret |= control_status_1_machine(ctdb, mypnn,
-                                                       &nodemap->nodes[nodes[i]]);
-               } else {
-                       ret |= control_status_1_human(ctdb, mypnn,
-                                                     &nodemap->nodes[nodes[i]]);
-               }
+       ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &recmaster);
+       if (ret != 0) {
+               return ret;
        }
 
-       talloc_free(tmp_ctx);
-       return ret;
+       print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
+                    recmode, recmaster);
+       return 0;
 }
 
-/*
-  display the list of nodes belonging to this natgw configuration
- */
-static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       static char prog[PATH_MAX+1] = "";
+       struct ctdb_uptime *uptime;
+       int ret, tmp, days, hours, minutes, seconds;
 
-       if (argc != 0) {
-               usage();
+       ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
+                              ctdb->cmd_pnn, TIMEOUT(), &uptime);
+       if (ret != 0) {
+               return ret;
        }
 
-       if (!ctdb_set_helper("NAT gateway helper", prog, sizeof(prog),
-                            "CTDB_NATGW_HELPER", CTDB_HELPER_BINDIR,
-                            "ctdb_natgw")) {
-               DEBUG(DEBUG_ERR, ("Unable to set NAT gateway helper\n"));
-               exit(1);
-       }
+       printf("Current time of node %-4u     :                %s",
+              ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
 
-       execl(prog, prog, "natgwlist", NULL);
+       tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
+       seconds = tmp % 60; tmp /= 60;
+       minutes = tmp % 60; tmp /= 60;
+       hours = tmp % 24; tmp /= 24;
+       days = tmp;
 
-       DEBUG(DEBUG_ERR,
-             ("Unable to run NAT gateway helper %s\n", strerror(errno)));
-       exit(1);
+       printf("Ctdbd start time              : (%03d %02d:%02d:%02d) %s",
+              days, hours, minutes, seconds,
+              ctime(&uptime->ctdbd_start_time.tv_sec));
+
+       tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
+       seconds = tmp % 60; tmp /= 60;
+       minutes = tmp % 60; tmp /= 60;
+       hours = tmp % 24; tmp /= 24;
+       days = tmp;
+
+       printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
+              days, hours, minutes, seconds,
+              ctime(&uptime->last_recovery_finished.tv_sec));
+
+       printf("Duration of last recovery/failover: %lf seconds\n",
+              timeval_delta(&uptime->last_recovery_finished,
+                            &uptime->last_recovery_started));
+
+       return 0;
 }
 
-/*
-  display the status of the scripts for monitoring (or other events)
- */
-static int control_one_scriptstatus(struct ctdb_context *ctdb,
-                                   enum ctdb_event type)
+static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                       int argc, const char **argv)
 {
-       struct ctdb_script_list_old *script_status;
-       int ret, i;
+       struct timeval tv;
+       int ret, num_clients;
 
-       ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
+       tv = timeval_current();
+       ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
+                            ctdb->cmd_pnn, TIMEOUT(), &num_clients);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
                return ret;
        }
 
-       if (script_status == NULL) {
-               if (!options.machinereadable) {
-                       printf("%s cycle never run\n",
-                              ctdb_eventscript_call_names[type]);
-               }
-               return 0;
-       }
-
-       if (!options.machinereadable) {
-               int num_run = 0;
-               for (i=0; i<script_status->num_scripts; i++) {
-                       if (script_status->scripts[i].status != -ENOEXEC) {
-                               num_run++;
-                       }
-               }
-               printf("%d scripts were executed last %s cycle\n",
-                      num_run,
-                      ctdb_eventscript_call_names[type]);
-       }
-       for (i=0; i<script_status->num_scripts; i++) {
-               const char *status = NULL;
-
-               /* The ETIME status is ignored for certain events.
-                * In that case the status is 0, but endtime is not set.
-                */
-               if (script_status->scripts[i].status == 0 &&
-                   timeval_is_zero(&script_status->scripts[i].finished)) {
-                       script_status->scripts[i].status = -ETIME;
-               }
-
-               switch (script_status->scripts[i].status) {
-               case -ETIME:
-                       status = "TIMEDOUT";
-                       break;
-               case -ENOEXEC:
-                       status = "DISABLED";
-                       break;
-               case 0:
-                       status = "OK";
-                       break;
-               default:
-                       if (script_status->scripts[i].status > 0)
-                               status = "ERROR";
-                       break;
-               }
-               if (options.machinereadable) {
-                       printm(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
-                              ctdb_eventscript_call_names[type],
-                              script_status->scripts[i].name,
-                              script_status->scripts[i].status,
-                              status,
-                              (long)script_status->scripts[i].start.tv_sec,
-                              (long)script_status->scripts[i].start.tv_usec,
-                              (long)script_status->scripts[i].finished.tv_sec,
-                              (long)script_status->scripts[i].finished.tv_usec,
-                              script_status->scripts[i].output);
-                       continue;
-               }
-               if (status)
-                       printf("%-20s Status:%s    ",
-                              script_status->scripts[i].name, status);
-               else
-                       /* Some other error, eg from stat. */
-                       printf("%-20s Status:CANNOT RUN (%s)",
-                              script_status->scripts[i].name,
-                              strerror(-script_status->scripts[i].status));
-
-               if (script_status->scripts[i].status >= 0) {
-                       printf("Duration:%.3lf ",
-                       timeval_delta(&script_status->scripts[i].finished,
-                             &script_status->scripts[i].start));
-               }
-               if (script_status->scripts[i].status != -ENOEXEC) {
-                       printf("%s",
-                              ctime(&script_status->scripts[i].start.tv_sec));
-                       if (script_status->scripts[i].status != 0) {
-                               printf("   OUTPUT:%s\n",
-                                      script_status->scripts[i].output);
-                       }
-               } else {
-                       printf("\n");
-               }
-       }
+       printf("response from %u time=%.6f sec  (%d clients)\n",
+              ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
        return 0;
 }
 
+const char *runstate_to_string(enum ctdb_runstate runstate);
+enum ctdb_runstate runstate_from_string(const char *runstate_str);
 
-static int control_scriptstatus(struct ctdb_context *ctdb,
-                               int argc, const char **argv)
+static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
-       int ret;
-       enum ctdb_event type, min, max;
-       const char *arg;
+       enum ctdb_runstate runstate;
+       bool found;
+       int ret, i;
 
-       if (argc > 1) {
-               DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
-               return -1;
+       ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
+                                    ctdb->cmd_pnn, TIMEOUT(), &runstate);
+       if (ret != 0) {
+               return ret;
        }
 
-       if (argc == 0)
-               arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
-       else
-               arg = argv[0];
+       found = true;
+       for (i=0; i<argc; i++) {
+               enum ctdb_runstate t;
+
+               found = false;
+               t = ctdb_runstate_from_string(argv[i]);
+               if (t == CTDB_RUNSTATE_UNKNOWN) {
+                       printf("Invalid run state (%s)\n", argv[i]);
+                       return 1;
+               }
 
-       for (type = 0; type < CTDB_EVENT_MAX; type++) {
-               if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
-                       min = type;
-                       max = type+1;
+               if (t == runstate) {
+                       found = true;
                        break;
                }
        }
-       if (type == CTDB_EVENT_MAX) {
-               if (strcmp(arg, "all") == 0) {
-                       min = 0;
-                       max = CTDB_EVENT_MAX;
-               } else {
-                       DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
-                       return -1;
-               }
-       }
-
-       if (options.machinereadable) {
-               printm(":Type:Name:Code:Status:Start:End:Error Output...:\n");
-       }
 
-       for (type = min; type < max; type++) {
-               ret = control_one_scriptstatus(ctdb, type);
-               if (ret != 0) {
-                       return ret;
-               }
+       if (! found) {
+               printf("CTDB not in required run state (got %s)\n",
+                      ctdb_runstate_to_string(runstate));
+               return 1;
        }
 
+       printf("%s\n", ctdb_runstate_to_string(runstate));
        return 0;
 }
 
-/*
-  enable an eventscript
- */
-static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       int ret;
+       struct ctdb_var_list *tun_var_list;
+       uint32_t value;
+       int ret, i;
+       bool found;
 
-       if (argc < 1) {
-               usage();
+       if (argc != 1) {
+               usage("getvar");
        }
 
-       ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
+       ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
        if (ret != 0) {
-         DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
+               fprintf(stderr,
+                       "Failed to get list of variables from node %u\n",
+                       ctdb->cmd_pnn);
                return ret;
        }
 
-       return 0;
-}
-
-/*
-  disable an eventscript
- */
-static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int ret;
+       found = false;
+       for (i=0; i<tun_var_list->count; i++) {
+               if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
+                       found = true;
+                       break;
+               }
+       }
 
-       if (argc < 1) {
-               usage();
+       if (! found) {
+               printf("No such tunable %s\n", argv[0]);
+               return 1;
        }
 
-       ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
+       ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
        if (ret != 0) {
-         DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
                return ret;
        }
 
+       printf("%-26s = %u\n", argv[0], value);
        return 0;
 }
 
-/*
-  display the pnn of the recovery master
- */
-static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       uint32_t recmaster;
-       int ret;
+       struct ctdb_var_list *tun_var_list;
+       struct ctdb_tunable tunable;
+       int ret, i;
+       bool found;
 
-       ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
-               return -1;
+       if (argc != 2) {
+               usage("setvar");
        }
-       printf("%d\n",recmaster);
-
-       return 0;
-}
-
-/*
-  add a tickle to a public address
- */
-static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       struct ctdb_connection t;
-       TDB_DATA data;
-       int ret;
-
-       assert_single_node_only();
 
-       if (argc < 2) {
-               usage();
+       ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
+       if (ret != 0) {
+               fprintf(stderr,
+                       "Failed to get list of variables from node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
        }
 
-       if (parse_ip_port(argv[0], &t.src) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
+       found = false;
+       for (i=0; i<tun_var_list->count; i++) {
+               if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
+                       found = true;
+                       break;
+               }
        }
-       if (parse_ip_port(argv[1], &t.dst) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
-               return -1;
+
+       if (! found) {
+               printf("No such tunable %s\n", argv[0]);
+               return 1;
        }
 
-       data.dptr = (uint8_t *)&t;
-       data.dsize = sizeof(t);
+       tunable.name = argv[0];
+       tunable.value = strtoul(argv[1], NULL, 0);
 
-       /* tell all nodes about this tcp connection */
-       ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
-                          0, data, ctdb, NULL, NULL, NULL, NULL);
+       ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), &tunable);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
-               return -1;
+               if (ret == 1) {
+                       fprintf(stderr,
+                               "Setting obsolete tunable variable '%s'\n",
+                              tunable.name);
+                       return 0;
+               }
        }
-       
-       return 0;
-}
 
+       return ret;
+}
 
-/*
-  delete a tickle from a node
- */
-static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
-       struct ctdb_connection t;
-       TDB_DATA data;
-       int ret;
-
-       assert_single_node_only();
+       struct ctdb_var_list *tun_var_list;
+       int ret, i;
 
-       if (argc < 2) {
-               usage();
+       if (argc != 0) {
+               usage("listvars");
        }
 
-       if (parse_ip_port(argv[0], &t.src) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
-       }
-       if (parse_ip_port(argv[1], &t.dst) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
-               return -1;
+       ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
+       if (ret != 0) {
+               return ret;
        }
 
-       data.dptr = (uint8_t *)&t;
-       data.dsize = sizeof(t);
-
-       /* tell all nodes about this tcp connection */
-       ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
-                          0, data, ctdb, NULL, NULL, NULL, NULL);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
-               return -1;
+       for (i=0; i<tun_var_list->count; i++) {
+               control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
        }
-       
+
        return 0;
 }
 
+const struct {
+       const char *name;
+       uint32_t offset;
+} stats_fields[] = {
+#define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
+       STATISTICS_FIELD(num_clients),
+       STATISTICS_FIELD(frozen),
+       STATISTICS_FIELD(recovering),
+       STATISTICS_FIELD(num_recoveries),
+       STATISTICS_FIELD(client_packets_sent),
+       STATISTICS_FIELD(client_packets_recv),
+       STATISTICS_FIELD(node_packets_sent),
+       STATISTICS_FIELD(node_packets_recv),
+       STATISTICS_FIELD(keepalive_packets_sent),
+       STATISTICS_FIELD(keepalive_packets_recv),
+       STATISTICS_FIELD(node.req_call),
+       STATISTICS_FIELD(node.reply_call),
+       STATISTICS_FIELD(node.req_dmaster),
+       STATISTICS_FIELD(node.reply_dmaster),
+       STATISTICS_FIELD(node.reply_error),
+       STATISTICS_FIELD(node.req_message),
+       STATISTICS_FIELD(node.req_control),
+       STATISTICS_FIELD(node.reply_control),
+       STATISTICS_FIELD(client.req_call),
+       STATISTICS_FIELD(client.req_message),
+       STATISTICS_FIELD(client.req_control),
+       STATISTICS_FIELD(timeouts.call),
+       STATISTICS_FIELD(timeouts.control),
+       STATISTICS_FIELD(timeouts.traverse),
+       STATISTICS_FIELD(locks.num_calls),
+       STATISTICS_FIELD(locks.num_current),
+       STATISTICS_FIELD(locks.num_pending),
+       STATISTICS_FIELD(locks.num_failed),
+       STATISTICS_FIELD(total_calls),
+       STATISTICS_FIELD(pending_calls),
+       STATISTICS_FIELD(childwrite_calls),
+       STATISTICS_FIELD(pending_childwrite_calls),
+       STATISTICS_FIELD(memory_used),
+       STATISTICS_FIELD(max_hop_count),
+       STATISTICS_FIELD(total_ro_delegations),
+       STATISTICS_FIELD(total_ro_revokes),
+};
+
+#define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
 
-/*
-  get a list of all tickles for this pnn
- */
-static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
+static void print_statistics_machine(struct ctdb_statistics *s,
+                                    bool show_header)
 {
-       struct ctdb_tickle_list_old *list;
-       ctdb_sock_addr addr;
-       int i, ret;
-       unsigned port = 0;
+       int i;
 
-       assert_single_node_only();
+       if (show_header) {
+               printf("CTDB version%s", options.sep);
+               printf("Current time of statistics%s", options.sep);
+               printf("Statistics collected since%s", options.sep);
+               for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
+                       printf("%s%s", stats_fields[i].name, options.sep);
+               }
+               printf("num_reclock_ctdbd_latency%s", options.sep);
+               printf("min_reclock_ctdbd_latency%s", options.sep);
+               printf("avg_reclock_ctdbd_latency%s", options.sep);
+               printf("max_reclock_ctdbd_latency%s", options.sep);
+
+               printf("num_reclock_recd_latency%s", options.sep);
+               printf("min_reclock_recd_latency%s", options.sep);
+               printf("avg_reclock_recd_latency%s", options.sep);
+               printf("max_reclock_recd_latency%s", options.sep);
+
+               printf("num_call_latency%s", options.sep);
+               printf("min_call_latency%s", options.sep);
+               printf("avg_call_latency%s", options.sep);
+               printf("max_call_latency%s", options.sep);
+
+               printf("num_lockwait_latency%s", options.sep);
+               printf("min_lockwait_latency%s", options.sep);
+               printf("avg_lockwait_latency%s", options.sep);
+               printf("max_lockwait_latency%s", options.sep);
+
+               printf("num_childwrite_latency%s", options.sep);
+               printf("min_childwrite_latency%s", options.sep);
+               printf("avg_childwrite_latency%s", options.sep);
+               printf("max_childwrite_latency%s", options.sep);
+               printf("\n");
+       }
 
-       if (argc < 1) {
-               usage();
+       printf("%u%s", CTDB_PROTOCOL, options.sep);
+       printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
+       printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
+       for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
+               printf("%u%s",
+                      *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
+                      options.sep);
+       }
+       printf("%u%s", s->reclock.ctdbd.num, options.sep);
+       printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
+       printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
+       printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
+
+       printf("%u%s", s->reclock.recd.num, options.sep);
+       printf("%.6f%s", s->reclock.recd.min, options.sep);
+       printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
+       printf("%.6f%s", s->reclock.recd.max, options.sep);
+
+       printf("%d%s", s->call_latency.num, options.sep);
+       printf("%.6f%s", s->call_latency.min, options.sep);
+       printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
+       printf("%.6f%s", s->call_latency.max, options.sep);
+
+       printf("%d%s", s->childwrite_latency.num, options.sep);
+       printf("%.6f%s", s->childwrite_latency.min, options.sep);
+       printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
+       printf("%.6f%s", s->childwrite_latency.max, options.sep);
+       printf("\n");
+}
+
+static void print_statistics(struct ctdb_statistics *s)
+{
+       int tmp, days, hours, minutes, seconds;
+       int i;
+       const char *prefix = NULL;
+       int preflen = 0;
+
+       tmp = s->statistics_current_time.tv_sec -
+             s->statistics_start_time.tv_sec;
+       seconds = tmp % 60; tmp /= 60;
+       minutes = tmp % 60; tmp /= 60;
+       hours   = tmp % 24; tmp /= 24;
+       days    = tmp;
+
+       printf("CTDB version %u\n", CTDB_PROTOCOL);
+       printf("Current time of statistics  :                %s",
+              ctime(&s->statistics_current_time.tv_sec));
+       printf("Statistics collected since  : (%03d %02d:%02d:%02d) %s",
+              days, hours, minutes, seconds,
+              ctime(&s->statistics_start_time.tv_sec));
+
+       for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
+               if (strchr(stats_fields[i].name, '.') != NULL) {
+                       preflen = strcspn(stats_fields[i].name, ".") + 1;
+                       if (! prefix ||
+                           strncmp(prefix, stats_fields[i].name, preflen) != 0) {
+                               prefix = stats_fields[i].name;
+                               printf(" %*.*s\n", preflen-1, preflen-1,
+                                      stats_fields[i].name);
+                       }
+               } else {
+                       preflen = 0;
+               }
+               printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
+                      stats_fields[i].name+preflen, preflen ? 0 : 4, "",
+                      *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
        }
 
-       if (argc == 2) {
-               port = atoi(argv[1]);
+       printf(" hop_count_buckets:");
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               printf(" %d", s->hop_count_bucket[i]);
+       }
+       printf("\n");
+       printf(" lock_buckets:");
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               printf(" %d", s->locks.buckets[i]);
        }
+       printf("\n");
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "locks_latency      MIN/AVG/MAX",
+              s->locks.latency.min, LATENCY_AVG(s->locks.latency),
+              s->locks.latency.max, s->locks.latency.num);
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "reclock_ctdbd      MIN/AVG/MAX",
+              s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
+              s->reclock.ctdbd.max, s->reclock.ctdbd.num);
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "reclock_recd       MIN/AVG/MAX",
+              s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
+              s->reclock.recd.max, s->reclock.recd.num);
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "call_latency       MIN/AVG/MAX",
+              s->call_latency.min, LATENCY_AVG(s->call_latency),
+              s->call_latency.max, s->call_latency.num);
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "childwrite_latency MIN/AVG/MAX",
+              s->childwrite_latency.min,
+              LATENCY_AVG(s->childwrite_latency),
+              s->childwrite_latency.max, s->childwrite_latency.num);
+}
+
+static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
+{
+       struct ctdb_statistics *stats;
+       int ret;
 
-       if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
+       if (argc != 0) {
+               usage("statistics");
        }
 
-       ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
-               return -1;
+       ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
+                                  ctdb->cmd_pnn, TIMEOUT(), &stats);
+       if (ret != 0) {
+               return ret;
        }
 
-       if (options.machinereadable){
-               printm(":source ip:port:destination ip:port:\n");
-               for (i=0;i<list->num;i++) {
-                       if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
-                               continue;
-                       }
-                       printm(":%s:%u", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
-                       printm(":%s:%u:\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
-               }
+       if (options.machinereadable) {
+               print_statistics_machine(stats, true);
        } else {
-               printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
-               printf("Num tickles:%u\n", list->num);
-               for (i=0;i<list->num;i++) {
-                       if (port && port != ntohs(list->connections[i].dst.ip.sin_port)) {
-                               continue;
-                       }
-                       printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->connections[i].src), ntohs(list->connections[i].src.ip.sin_port));
-                       printf("DST: %s:%u\n", ctdb_addr_to_str(&list->connections[i].dst), ntohs(list->connections[i].dst.ip.sin_port));
-               }
+               print_statistics(stats);
        }
 
-       talloc_free(list);
-       
        return 0;
 }
 
-
-static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
+static int control_statistics_reset(TALLOC_CTX *mem_ctx,
+                                   struct ctdb_context *ctdb,
+                                   int argc, const char **argv)
 {
-       struct ctdb_public_ip_list_old *ips;
-       struct ctdb_public_ip ip;
-       int i, ret;
-       uint32_t *nodes;
-       uint32_t disable_time;
-       TDB_DATA data;
-       struct ctdb_node_map_old *nodemap=NULL;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
+       int ret;
 
-       disable_time = 30;
-       data.dptr  = (uint8_t*)&disable_time;
-       data.dsize = sizeof(disable_time);
-       ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
-               return -1;
+       if (argc != 0) {
+               usage("statisticsreset");
        }
 
-
-
-       /* read the public ip list from the node */
-       ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
+       ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
+                                        ctdb->cmd_pnn, TIMEOUT());
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-
-       for (i=0;i<ips->num;i++) {
-               if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
-                       break;
-               }
-       }
-       if (i==ips->num) {
-               DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
-                       pnn, ctdb_addr_to_str(addr)));
-               talloc_free(tmp_ctx);
-               return -1;
+               return ret;
        }
 
-       ip.pnn  = pnn;
-       ip.addr = *addr;
+       return 0;
+}
 
-       data.dptr  = (uint8_t *)&ip;
-       data.dsize = sizeof(ip);
+static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
+{
+       struct ctdb_statistics_list *slist;
+       int ret, count = 0, i;
+       bool show_header = true;
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return ret;
+       if (argc > 1) {
+               usage("stats");
        }
 
-       nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
-       ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
-                                       nodes, 0,
-                                       LONGTIMELIMIT(),
-                                       false, data,
-                                       NULL, NULL,
-                                       NULL);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc == 1) {
+               count = atoi(argv[0]);
        }
 
-       ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
+       ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
+                                        ctdb->cmd_pnn, TIMEOUT(), &slist);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+               return ret;
        }
 
-       /* update the recovery daemon so it now knows to expect the new
-          node assignment for this ip.
-       */
-       ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
-               return -1;
+       for (i=0; i<slist->num; i++) {
+               if (slist->stats[i].statistics_start_time.tv_sec == 0) {
+                       continue;
+               }
+               if (options.machinereadable == 1) {
+                       print_statistics_machine(&slist->stats[i],
+                                                show_header);
+                       show_header = false;
+               } else {
+                       print_statistics(&slist->stats[i]);
+               }
+               if (count > 0 && i == count) {
+                       break;
+               }
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
+static int ctdb_public_ip_cmp(const void *a, const void *b)
+{
+       const struct ctdb_public_ip *ip_a = a;
+       const struct ctdb_public_ip *ip_b = b;
 
-/* 
- * scans all other nodes and returns a pnn for another node that can host this 
- * ip address or -1
- */
-static int
-find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
+       return ctdb_sock_addr_cmp(&ip_a->addr, &ip_b->addr);
+}
+
+static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                    struct ctdb_public_ip_list *ips,
+                    struct ctdb_public_ip_info **ipinfo,
+                    bool all_nodes)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_public_ip_list_old *ips;
-       struct ctdb_node_map_old *nodemap=NULL;
-       int i, j, ret;
-       int pnn;
+       int i, j;
+       char *conf, *avail, *active;
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return ret;
+       if (options.machinereadable == 1) {
+               printf("%s%s%s%s%s", options.sep,
+                      "Public IP", options.sep,
+                      "Node", options.sep);
+               if (options.verbose == 1) {
+                       printf("%s%s%s%s%s%s\n",
+                              "ActiveInterfaces", options.sep,
+                              "AvailableInterfaces", options.sep,
+                              "ConfiguredInterfaces", options.sep);
+               } else {
+                       printf("\n");
+               }
+       } else {
+               if (all_nodes) {
+                       printf("Public IPs on ALL nodes\n");
+               } else {
+                       printf("Public IPs on node %u\n", ctdb->cmd_pnn);
+               }
        }
 
-       for(i=0;i<nodemap->num;i++){
-               if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
-                       continue;
+       for (i = 0; i < ips->num; i++) {
+
+               if (options.machinereadable == 1) {
+                       printf("%s%s%s%d%s", options.sep,
+                              ctdb_sock_addr_to_string(
+                                       mem_ctx, &ips->ip[i].addr),
+                              options.sep,
+                              (int)ips->ip[i].pnn, options.sep);
+               } else {
+                       printf("%s", ctdb_sock_addr_to_string(
+                                               mem_ctx, &ips->ip[i].addr));
                }
-               if (nodemap->nodes[i].pnn == options.pnn) {
+
+               if (options.verbose == 0) {
+                       if (options.machinereadable == 1) {
+                               printf("\n");
+                       } else {
+                               printf(" %d\n", (int)ips->ip[i].pnn);
+                       }
                        continue;
                }
 
-               /* read the public ip list from this node */
-               ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
-                       return -1;
+               conf = NULL;
+               avail = NULL;
+               active = NULL;
+
+               if (ipinfo[i] == NULL) {
+                       goto skip_ipinfo;
                }
 
-               for (j=0;j<ips->num;j++) {
-                       if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
-                               pnn = nodemap->nodes[i].pnn;
-                               talloc_free(tmp_ctx);
-                               return pnn;
+               for (j=0; j<ipinfo[i]->ifaces->num; j++) {
+                       struct ctdb_iface *iface;
+
+                       iface = &ipinfo[i]->ifaces->iface[j];
+                       if (conf == NULL) {
+                               conf = talloc_strdup(mem_ctx, iface->name);
+                       } else {
+                               conf = talloc_asprintf_append(
+                                               conf, ",%s", iface->name);
                        }
-               }
-               talloc_free(ips);
-       }
 
-       talloc_free(tmp_ctx);
-       return -1;
-}
+                       if (ipinfo[i]->active_idx == j) {
+                               active = iface->name;
+                       }
 
-/* If pnn is -1 then try to find a node to move IP to... */
-static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
-{
-       bool pnn_specified = (pnn == -1 ? false : true);
-       int retries = 0;
+                       if (iface->link_state == 0) {
+                               continue;
+                       }
 
-       while (retries < 5) {
-               if (!pnn_specified) {
-                       pnn = find_other_host_for_public_ip(ctdb, addr);
-                       if (pnn == -1) {
-                               return false;
+                       if (avail == NULL) {
+                               avail = talloc_strdup(mem_ctx, iface->name);
+                       } else {
+                               avail = talloc_asprintf_append(
+                                               avail, ",%s", iface->name);
                        }
-                       DEBUG(DEBUG_NOTICE,
-                             ("Trying to move public IP to node %u\n", pnn));
                }
 
-               if (move_ip(ctdb, addr, pnn) == 0) {
-                       return true;
-               }
+       skip_ipinfo:
 
-               sleep(3);
-               retries++;
+               if (options.machinereadable == 1) {
+                       printf("%s%s%s%s%s%s\n",
+                              active ? active : "", options.sep,
+                              avail ? avail : "", options.sep,
+                              conf ? conf : "", options.sep);
+               } else {
+                       printf(" node[%d] active[%s] available[%s]"
+                              " configured[%s]\n",
+                              (int)ips->ip[i].pnn, active ? active : "",
+                              avail ? avail : "", conf ? conf : "");
+               }
        }
-
-       return false;
 }
 
+static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
+                      size_t datalen, void *private_data)
+{
+       struct ctdb_public_ip_list *ips = talloc_get_type_abort(
+               private_data, struct ctdb_public_ip_list);
+       struct ctdb_public_ip *ip;
 
-/*
-  move/failover an ip address to a specific node
- */
-static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
+       ip = (struct ctdb_public_ip *)databuf;
+       ips->ip[ips->num] = *ip;
+       ips->num += 1;
+
+       return 0;
+}
+
+static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
+                             struct ctdb_public_ip_list **out)
 {
-       uint32_t pnn;
-       ctdb_sock_addr addr;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_public_ip_list *ips;
+       struct db_hash_context *ipdb;
+       uint32_t *pnn_list;
+       int ret, count, i, j;
 
-       assert_single_node_only();
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
 
-       if (argc < 2) {
-               usage();
-               return -1;
+       ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
+       if (ret != 0) {
+               goto failed;
        }
 
-       if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
+       count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
+                                    &pnn_list);
+       if (count <= 0) {
+               goto failed;
        }
 
+       for (i=0; i<count; i++) {
+               ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
+                                              pnn_list[i], TIMEOUT(),
+                                              false, &ips);
+               if (ret != 0) {
+                       goto failed;
+               }
 
-       if (sscanf(argv[1], "%u", &pnn) != 1) {
-               DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
-               return -1;
-       }
+               for (j=0; j<ips->num; j++) {
+                       struct ctdb_public_ip ip;
 
-       if (!try_moveip(ctdb, &addr, pnn)) {
-               DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
-               return -1;
-       }
+                       ip.pnn = ips->ip[j].pnn;
+                       ip.addr = ips->ip[j].addr;
 
-       return 0;
-}
+                       if (pnn_list[i] == ip.pnn) {
+                               /* Node claims IP is hosted on it, so
+                                * save that information
+                                */
+                               ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
+                                                 sizeof(ip.addr),
+                                                 (uint8_t *)&ip, sizeof(ip));
+                               if (ret != 0) {
+                                       goto failed;
+                               }
+                       } else {
+                               /* Node thinks IP is hosted elsewhere,
+                                * so overwrite with CTDB_UNKNOWN_PNN
+                                * if there's no existing entry
+                                */
+                               ret = db_hash_exists(ipdb, (uint8_t *)&ip.addr,
+                                                    sizeof(ip.addr));
+                               if (ret == ENOENT) {
+                                       ip.pnn = CTDB_UNKNOWN_PNN;
+                                       ret = db_hash_add(ipdb,
+                                                         (uint8_t *)&ip.addr,
+                                                         sizeof(ip.addr),
+                                                         (uint8_t *)&ip,
+                                                         sizeof(ip));
+                                       if (ret != 0) {
+                                               goto failed;
+                                       }
+                               }
+                       }
+               }
 
-static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
-{
-       TDB_DATA data;
+               TALLOC_FREE(ips);
+       }
 
-       data.dptr  = (uint8_t *)&pnn;
-       data.dsize = sizeof(uint32_t);
-       if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
-               DEBUG(DEBUG_ERR,
-                     ("Failed to send message to force node %u to be a rebalancing target\n",
-                      pnn));
-               return -1;
+       talloc_free(pnn_list);
+
+       ret = db_hash_traverse(ipdb, NULL, NULL, &count);
+       if (ret != 0) {
+               goto failed;
        }
 
-       return 0;
-}
+       ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
+       if (ips == NULL) {
+               goto failed;
+       }
 
+       ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
+       if (ips->ip == NULL) {
+               goto failed;
+       }
 
-static int getips_store_callback(void *param, void *data)
-{
-       struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
-       struct ctdb_public_ip_list_old *ips = param;
-       int i;
+       ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
+       if (ret != 0) {
+               goto failed;
+       }
 
-       i = ips->num++;
-       ips->ips[i].pnn  = node_ip->pnn;
-       ips->ips[i].addr = node_ip->addr;
-       return 0;
-}
+       if (count != ips->num) {
+               goto failed;
+       }
 
-static int getips_count_callback(void *param, void *data)
-{
-       uint32_t *count = param;
+       talloc_free(ipdb);
 
-       (*count)++;
+       *out = ips;
        return 0;
+
+failed:
+       talloc_free(ipdb);
+       return 1;
 }
 
-#define IP_KEYLEN      4
-static uint32_t *ip_key(ctdb_sock_addr *ip)
+static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                     int argc, const char **argv)
 {
-       static uint32_t key[IP_KEYLEN];
-
-       bzero(key, sizeof(key));
+       struct ctdb_public_ip_list *ips;
+       struct ctdb_public_ip_info **ipinfo;
+       int ret, i;
+       bool do_all = false;
 
-       switch (ip->sa.sa_family) {
-       case AF_INET:
-               key[0]  = ip->ip.sin_addr.s_addr;
-               break;
-       case AF_INET6: {
-               uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
-               key[0]  = s6_a32[3];
-               key[1]  = s6_a32[2];
-               key[2]  = s6_a32[1];
-               key[3]  = s6_a32[0];
-               break;
+       if (argc > 1) {
+               usage("ip");
        }
-       default:
-               DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
-               return key;
-       }
-
-       return key;
-}
-
-static void *add_ip_callback(void *parm, void *data)
-{
-       return parm;
-}
 
-static int
-control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_public_ip_list_old **ips)
-{
-       struct ctdb_public_ip_list_old *tmp_ips;
-       struct ctdb_node_map_old *nodemap=NULL;
-       trbt_tree_t *ip_tree;
-       int i, j, len, ret;
-       uint32_t count;
+       if (argc == 1) {
+               if (strcmp(argv[0], "all") == 0) {
+                       do_all = true;
+               } else {
+                       usage("ip");
+               }
+       }
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
+       if (do_all) {
+               ret = get_all_public_ips(ctdb, mem_ctx, &ips);
+       } else {
+               ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
+                                              ctdb->cmd_pnn, TIMEOUT(),
+                                              false, &ips);
+       }
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
                return ret;
        }
 
-       ip_tree = trbt_create(tmp_ctx, 0);
+       qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
+             ctdb_public_ip_cmp);
 
-       for(i=0;i<nodemap->num;i++){
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       continue;
+       ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
+       if (ipinfo == NULL) {
+               return 1;
+       }
+
+       for (i=0; i<ips->num; i++) {
+               uint32_t pnn;
+               if (do_all) {
+                       pnn = ips->ip[i].pnn;
+               } else {
+                       pnn = ctdb->cmd_pnn;
                }
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
+               if (pnn == CTDB_UNKNOWN_PNN) {
+                       ipinfo[i] = NULL;
                        continue;
                }
-
-               /* read the public ip list from this node */
-               ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
+               ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
+                                                  ctdb->client, pnn,
+                                                  TIMEOUT(), &ips->ip[i].addr,
+                                                  &ipinfo[i]);
                if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
-                       return -1;
-               }
-       
-               for (j=0; j<tmp_ips->num;j++) {
-                       struct ctdb_public_ip *node_ip;
-
-                       node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
-                       node_ip->pnn  = tmp_ips->ips[j].pnn;
-                       node_ip->addr = tmp_ips->ips[j].addr;
-
-                       trbt_insertarray32_callback(ip_tree,
-                               IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
-                               add_ip_callback,
-                               node_ip);
+                       return ret;
                }
-               talloc_free(tmp_ips);
        }
 
-       /* traverse */
-       count = 0;
-       trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
-
-       len = offsetof(struct ctdb_public_ip_list_old, ips) +
-               count*sizeof(struct ctdb_public_ip);
-       tmp_ips = talloc_zero_size(tmp_ctx, len);
-       trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
-
-       *ips = tmp_ips;
-
+       print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
        return 0;
 }
 
-
-static void ctdb_every_second(struct tevent_context *ev,
-                             struct tevent_timer *te,
-                             struct timeval t, void *p)
-{
-       struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
-
-       tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
-                        ctdb_every_second, ctdb);
-}
-
-struct srvid_reply_handler_data {
-       bool done;
-       bool wait_for_all;
-       uint32_t *nodes;
-       const char *srvid_str;
-};
-
-static void srvid_broadcast_reply_handler(uint64_t srvid, TDB_DATA data,
-                                        void *private_data)
+static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       struct srvid_reply_handler_data *d =
-               (struct srvid_reply_handler_data *)private_data;
-       int i;
-       int32_t ret;
-
-       if (data.dsize != sizeof(ret)) {
-               DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
-               return;
-       }
+       struct ctdb_public_ip_info *ipinfo;
+       ctdb_sock_addr addr;
+       int ret, i;
 
-       /* ret will be a PNN (i.e. >=0) on success, or negative on error */
-       ret = *(int32_t *)data.dptr;
-       if (ret < 0) {
-               DEBUG(DEBUG_ERR,
-                     ("%s failed with result %d\n", d->srvid_str, ret));
-               return;
+       if (argc != 1) {
+               usage("ipinfo");
        }
 
-       if (!d->wait_for_all) {
-               d->done = true;
-               return;
+       if (! parse_ip(argv[0], NULL, 0, &addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
        }
 
-       /* Wait for all replies */
-       d->done = true;
-       for (i = 0; i < talloc_array_length(d->nodes); i++) {
-               if (d->nodes[i] == ret) {
-                       DEBUG(DEBUG_DEBUG,
-                             ("%s reply received from node %u\n",
-                              d->srvid_str, ret));
-                       d->nodes[i] = -1;
-               }
-               if (d->nodes[i] != -1) {
-                       /* Found a node that hasn't yet replied */
-                       d->done = false;
+       ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
+                                          ctdb->cmd_pnn, TIMEOUT(), &addr,
+                                          &ipinfo);
+       if (ret != 0) {
+               if (ret == -1) {
+                       printf("Node %u does not know about IP %s\n",
+                              ctdb->cmd_pnn, argv[0]);
                }
+               return ret;
        }
-}
-
-/* Broadcast the given SRVID to all connected nodes.  Wait for 1 reply
- * or replies from all connected nodes.  arg is the data argument to
- * pass in the srvid_request structure - pass 0 if this isn't needed.
- */
-static int srvid_broadcast(struct ctdb_context *ctdb,
-                          uint64_t srvid, uint32_t *arg,
-                          const char *srvid_str, bool wait_for_all)
-{
-       int ret;
-       TDB_DATA data;
-       uint32_t pnn;
-       uint64_t reply_srvid;
-       struct ctdb_srvid_message request;
-       struct ctdb_disable_message request_data;
-       struct srvid_reply_handler_data reply_data;
-       struct timeval tv;
-
-       ZERO_STRUCT(request);
-
-       /* Time ticks to enable timeouts to be processed */
-       tevent_add_timer(ctdb->ev, ctdb, timeval_current_ofs(1, 0),
-                        ctdb_every_second, ctdb);
 
-       pnn = ctdb_get_pnn(ctdb);
-       reply_srvid = getpid();
+       printf("Public IP[%s] info on node %u\n",
+              ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
+                                       ctdb->cmd_pnn);
 
-       if (arg == NULL) {
-               request.pnn = pnn;
-               request.srvid = reply_srvid;
+       printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
+              ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr),
+              ipinfo->ip.pnn, ipinfo->ifaces->num);
 
-               data.dptr = (uint8_t *)&request;
-               data.dsize = sizeof(request);
-       } else {
-               request_data.pnn = pnn;
-               request_data.srvid = reply_srvid;
-               request_data.timeout = *arg;
+       for (i=0; i<ipinfo->ifaces->num; i++) {
+               struct ctdb_iface *iface;
 
-               data.dptr = (uint8_t *)&request_data;
-               data.dsize = sizeof(request_data);
+               iface = &ipinfo->ifaces->iface[i];
+               iface->name[CTDB_IFACE_SIZE] = '\0';
+               printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
+                      i+1, iface->name,
+                      iface->link_state == 0 ? "down" : "up",
+                      iface->references,
+                      (i == ipinfo->active_idx) ? " (active)" : "");
        }
 
-       /* Register message port for reply from recovery master */
-       ctdb_client_set_message_handler(ctdb, reply_srvid,
-                                       srvid_broadcast_reply_handler,
-                                       &reply_data);
-
-       reply_data.wait_for_all = wait_for_all;
-       reply_data.nodes = NULL;
-       reply_data.srvid_str = srvid_str;
-
-again:
-       reply_data.done = false;
-
-       if (wait_for_all) {
-               struct ctdb_node_map_old *nodemap;
-
-               ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
-                                          CTDB_CURRENT_NODE, ctdb, &nodemap);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR,
-                             ("Unable to get nodemap from current node, try again\n"));
-                       sleep(1);
-                       goto again;
-               }
+       return 0;
+}
 
-               if (reply_data.nodes != NULL) {
-                       talloc_free(reply_data.nodes);
-               }
-               reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
-                                                          NULL, true);
+static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
+{
+       struct ctdb_iface_list *ifaces;
+       int ret, i;
 
-               talloc_free(nodemap);
+       if (argc != 0) {
+               usage("ifaces");
        }
 
-       /* Send to all connected nodes. Only recmaster replies */
-       ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
-                                      srvid, data);
+       ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
+                                  ctdb->cmd_pnn, TIMEOUT(), &ifaces);
        if (ret != 0) {
-               /* This can only happen if the socket is closed and
-                * there's no way to recover from that, so don't try
-                * again.
-                */
-               DEBUG(DEBUG_ERR,
-                     ("Failed to send %s request to connected nodes\n",
-                      srvid_str));
-               return -1;
+               return ret;
        }
 
-       tv = timeval_current();
-       /* This loop terminates the reply is received */
-       while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
-               tevent_loop_once(ctdb->ev);
+       if (ifaces->num == 0) {
+               printf("No interfaces configured on node %u\n",
+                      ctdb->cmd_pnn);
+               return 0;
        }
 
-       if (!reply_data.done) {
-               DEBUG(DEBUG_NOTICE,
-                     ("Still waiting for confirmation of %s\n", srvid_str));
-               sleep(1);
-               goto again;
+       if (options.machinereadable) {
+               printf("%s%s%s%s%s%s%s\n", options.sep,
+                      "Name", options.sep,
+                      "LinkStatus", options.sep,
+                      "References", options.sep);
+       } else {
+               printf("Interfaces on node %u\n", ctdb->cmd_pnn);
        }
 
-       ctdb_client_remove_message_handler(ctdb, reply_srvid, &reply_data);
-
-       talloc_free(reply_data.nodes);
+       for (i=0; i<ifaces->num; i++) {
+               if (options.machinereadable) {
+                       printf("%s%s%s%u%s%u%s\n", options.sep,
+                              ifaces->iface[i].name, options.sep,
+                              ifaces->iface[i].link_state, options.sep,
+                              ifaces->iface[i].references, options.sep);
+               } else {
+                       printf("name:%s link:%s references:%u\n",
+                              ifaces->iface[i].name,
+                              ifaces->iface[i].link_state ? "up" : "down",
+                              ifaces->iface[i].references);
+               }
+       }
 
        return 0;
 }
 
-static int ipreallocate(struct ctdb_context *ctdb)
-{
-       return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, NULL,
-                              "IP reallocation", false);
-}
-
-
-static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       return ipreallocate(ctdb);
-}
-
-/*
-  add a public ip address to a node
- */
-static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
-       int i, ret;
-       int len, retries = 0;
-       unsigned mask;
-       ctdb_sock_addr addr;
-       struct ctdb_addr_info_old *pub;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_public_ip_list_old *ips;
-
+       struct ctdb_iface_list *ifaces;
+       struct ctdb_iface *iface;
+       int ret, i;
 
        if (argc != 2) {
-               talloc_free(tmp_ctx);
-               usage();
+               usage("setifacelink");
        }
 
-       if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
-               DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
+               fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
+               return 1;
        }
 
-       /* read the public ip list from the node */
-       ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
+       ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
+                                  ctdb->cmd_pnn, TIMEOUT(), &ifaces);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr,
+                       "Failed to get interface information from node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
        }
-       for (i=0;i<ips->num;i++) {
-               if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
-                       DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
-                       return 0;
+
+       iface = NULL;
+       for (i=0; i<ifaces->num; i++) {
+               if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
+                       iface = &ifaces->iface[i];
+                       break;
                }
        }
 
+       if (iface == NULL) {
+               printf("Interface %s not configured on node %u\n",
+                      argv[0], ctdb->cmd_pnn);
+               return 1;
+       }
 
+       if (strcmp(argv[1], "up") == 0) {
+               iface->link_state = 1;
+       } else if (strcmp(argv[1], "down") == 0) {
+               iface->link_state = 0;
+       } else {
+               usage("setifacelink");
+               return 1;
+       }
 
-       /* Dont timeout. This command waits for an ip reallocation
-          which sometimes can take wuite a while if there has
-          been a recent recovery
-       */
-       alarm(0);
-
-       len = offsetof(struct ctdb_addr_info_old, iface) + strlen(argv[1]) + 1;
-       pub = talloc_size(tmp_ctx, len); 
-       CTDB_NO_MEMORY(ctdb, pub);
-
-       pub->addr  = addr;
-       pub->mask  = mask;
-       pub->len   = strlen(argv[1])+1;
-       memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
+       iface->references = 0;
 
-       do {
-               ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
-                       sleep(3);
-                       retries++;
-               }
-       } while (retries < 5 && ret != 0);
+       ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
+                                            ctdb->cmd_pnn, TIMEOUT(), iface);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
-               talloc_free(tmp_ctx);
                return ret;
        }
 
-       if (rebalance_node(ctdb, options.pnn) != 0) {
-               DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
-               return ret;
-       }
-
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  add a public ip address to a node
- */
-static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_process_exists(TALLOC_CTX *mem_ctx,
+                                 struct ctdb_context *ctdb,
+                                 int argc, const char **argv)
 {
-       ctdb_sock_addr addr;
-       char *iface = NULL;
+       pid_t pid;
+       int ret, status;
 
        if (argc != 1) {
-               usage();
+               usage("process-exists");
        }
 
-       if (!parse_ip(argv[0], NULL, 0, &addr)) {
-               printf("Badly formed ip : %s\n", argv[0]);
-               return -1;
+       pid = atoi(argv[0]);
+       ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
+                                      ctdb->cmd_pnn, TIMEOUT(), pid, &status);
+       if (ret != 0) {
+               return ret;
        }
 
-       iface = ctdb_sys_find_ifname(&addr);
-       if (iface == NULL) {
-               printf("Failed to get interface name for ip: %s", argv[0]);
-               return -1;
+       if (status == 0) {
+               printf("PID %u exists\n", pid);
+       } else {
+               printf("PID %u does not exist\n", pid);
        }
-
-       printf("IP on interface %s\n", iface);
-
-       free(iface);
-
-       return 0;
+       return status;
 }
 
-static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
-
-static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
+static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_node_map_old *nodemap=NULL;
-       struct ctdb_public_ip_list_old *ips;
-       int ret, i, j;
+       struct ctdb_dbid_map *dbmap;
+       int ret, i;
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
+       if (argc != 0) {
+               usage("getdbmap");
+       }
+
+       ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
+                                 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
                return ret;
        }
 
-       /* remove it from the nodes that are not hosting the ip currently */
-       for(i=0;i<nodemap->num;i++){
-               if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
-                       continue;
-               }
-               ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
-                       continue;
-               }
-
-               for (j=0;j<ips->num;j++) {
-                       if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
-                               break;
-                       }
-               }
-               if (j==ips->num) {
-                       continue;
-               }
-
-               if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
-                       continue;
-               }
-
-               options.pnn = nodemap->nodes[i].pnn;
-               control_delip(ctdb, argc, argv);
+       if (options.machinereadable == 1) {
+               printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
+                      options.sep,
+                      "ID", options.sep,
+                      "Name", options.sep,
+                      "Path", options.sep,
+                      "Persistent", options.sep,
+                      "Sticky", options.sep,
+                      "Unhealthy", options.sep,
+                      "Readonly", options.sep);
+       } else {
+               printf("Number of databases:%d\n", dbmap->num);
        }
 
+       for (i=0; i<dbmap->num; i++) {
+               const char *name;
+               const char *path;
+               const char *health;
+               bool persistent;
+               bool readonly;
+               bool sticky;
+               uint32_t db_id;
 
-       /* remove it from every node (also the one hosting it) */
-       for(i=0;i<nodemap->num;i++){
-               if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
-                       continue;
+               db_id = dbmap->dbs[i].db_id;
+
+               ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
+                                          ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                          &name);
+               if (ret != 0) {
+                       return ret;
                }
-               ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
+
+               ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
+                                         ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                         &path);
                if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
-                       continue;
+                       return ret;
                }
 
-               for (j=0;j<ips->num;j++) {
-                       if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
-                               break;
-                       }
+               ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
+                                             ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                             &health);
+               if (ret != 0) {
+                       return ret;
                }
-               if (j==ips->num) {
-                       continue;
+
+               persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
+               readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
+               sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
+
+               if (options.machinereadable == 1) {
+                       printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s\n",
+                              options.sep,
+                              db_id, options.sep,
+                              name, options.sep,
+                              path, options.sep,
+                              !! (persistent), options.sep,
+                              !! (sticky), options.sep,
+                              !! (health), options.sep,
+                              !! (readonly), options.sep);
+               } else {
+                       printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
+                              db_id, name, path,
+                              persistent ? " PERSISTENT" : "",
+                              sticky ? " STICKY" : "",
+                              readonly ? " READONLY" : "",
+                              health ? " UNHEALTHY" : "");
                }
 
-               options.pnn = nodemap->nodes[i].pnn;
-               control_delip(ctdb, argc, argv);
+               talloc_free(discard_const(name));
+               talloc_free(discard_const(path));
+               talloc_free(discard_const(health));
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
-       
-/*
-  delete a public ip address from a node
- */
-static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
+
+static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                              int argc, const char **argv)
 {
-       int i, ret;
-       ctdb_sock_addr addr;
-       struct ctdb_addr_info_old pub;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_public_ip_list_old *ips;
+       uint32_t db_id;
+       const char *db_name, *db_path, *db_health;
+       uint8_t db_flags;
+       int ret;
 
        if (argc != 1) {
-               talloc_free(tmp_ctx);
-               usage();
-       }
-
-       if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
+               usage("getdbstatus");
        }
 
-       if (options.pnn == CTDB_BROADCAST_ALL) {
-               return control_delip_all(ctdb, argc, argv, &addr);
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
+               return 1;
        }
 
-       pub.addr  = addr;
-       pub.mask  = 0;
-       pub.len   = 0;
-
-       ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
+       ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
+                                 ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                 &db_path);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
-               talloc_free(tmp_ctx);
                return ret;
        }
-       
-       for (i=0;i<ips->num;i++) {
-               if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
-                       break;
-               }
-       }
-
-       if (i==ips->num) {
-               DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
-                       ctdb_addr_to_str(&addr)));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-
-       /* This is an optimisation.  If this node is hosting the IP
-        * then try to move it somewhere else without invoking a full
-        * takeover run.  We don't care if this doesn't work!
-        */
-       if (ips->ips[i].pnn == options.pnn) {
-               (void) try_moveip(ctdb, &addr, -1);
-       }
 
-       ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
+       ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                     &db_health);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
                return ret;
        }
 
-       talloc_free(tmp_ctx);
+       printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
+       printf("PERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
+              (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
+              (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
+              (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
+              (db_health ? db_health : "OK"));
        return 0;
 }
 
-/*
-  send a gratious arp
- */
-static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int ret;
-       ctdb_sock_addr addr;
-
-       assert_single_node_only();
+struct dump_record_state {
+       uint32_t count;
+};
 
-       if (argc < 2) {
-               usage();
-       }
+#define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
 
-       if (!parse_ip(argv[0], NULL, 0, &addr)) {
-               DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
-               return -1;
-       }
+static void dump_tdb_data(const char *name, TDB_DATA val)
+{
+       int i;
 
-       ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
-               return ret;
+       fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
+       for (i=0; i<val.dsize; i++) {
+               if (ISASCII(val.dptr[i])) {
+                       fprintf(stdout, "%c", val.dptr[i]);
+               } else {
+                       fprintf(stdout, "\\%02X", val.dptr[i]);
+               }
        }
-
-       return 0;
+       fprintf(stdout, "\"\n");
 }
 
-/*
-  check if a server id exists
- */
-static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
+static void dump_ltdb_header(struct ctdb_ltdb_header *header)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       uint64_t *ids;
-       uint8_t *result;
-       int i;
-
-       if (argc < 1) {
-               talloc_free(tmp_ctx);
-               usage();
+       fprintf(stdout, "dmaster: %u\n", header->dmaster);
+       fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
+       fprintf(stdout, "flags: 0x%08x", header->flags);
+       if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
+               fprintf(stdout, " MIGRATED_WITH_DATA");
        }
-
-       ids    = talloc_array(tmp_ctx, uint64_t, argc);
-       result = talloc_array(tmp_ctx, uint8_t, argc);
-
-       for (i = 0; i < argc; i++) {
-               ids[i] = strtoull(argv[i], NULL, 0);
+       if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
+               fprintf(stdout, " VACUUM_MIGRATED");
        }
-
-       if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
-               DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
-                                 options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
+               fprintf(stdout, " AUTOMATIC");
        }
-
-       for (i=0; i < argc; i++) {
-               printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
-                      result[i] ? "exists" : "does not exist");
+       if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
+               fprintf(stdout, " RO_HAVE_DELEGATIONS");
        }
+       if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
+               fprintf(stdout, " RO_HAVE_READONLY");
+       }
+       if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
+               fprintf(stdout, " RO_REVOKING_READONLY");
+       }
+       if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
+               fprintf(stdout, " RO_REVOKE_COMPLETE");
+       }
+       fprintf(stdout, "\n");
 
-       talloc_free(tmp_ctx);
-       return 0;
 }
 
-/*
-  send a tcp tickle ack
- */
-static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
+static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
+                      TDB_DATA key, TDB_DATA data, void *private_data)
 {
-       int ret;
-       ctdb_sock_addr  src, dst;
+       struct dump_record_state *state =
+               (struct dump_record_state *)private_data;
 
-       if (argc < 2) {
-               usage();
-       }
-
-       if (!parse_ip_port(argv[0], &src)) {
-               DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
-               return -1;
-       }
+       state->count += 1;
 
-       if (!parse_ip_port(argv[1], &dst)) {
-               DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
-               return -1;
-       }
+       dump_tdb_data("key", key);
+       dump_ltdb_header(header);
+       dump_tdb_data("data", data);
+       fprintf(stdout, "\n");
 
-       ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
-       if (ret==0) {
-               return 0;
-       }
-       DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
-
-       return -1;
+       return 0;
 }
 
+struct traverse_state {
+       TALLOC_CTX *mem_ctx;
+       bool done;
+       ctdb_rec_parser_func_t func;
+       struct dump_record_state sub_state;
+};
 
-/*
-  display public ip status
- */
-static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
+static void traverse_handler(uint64_t srvid, TDB_DATA data, void *private_data)
 {
-       int i, ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_public_ip_list_old *ips;
+       struct traverse_state *state = (struct traverse_state *)private_data;
+       struct ctdb_rec_data *rec;
+       struct ctdb_ltdb_header header;
+       int ret;
 
-       if (argc == 1 && strcmp(argv[0], "all") == 0) {
-               options.pnn = CTDB_BROADCAST_ALL;
+       ret = ctdb_rec_data_pull(data.dptr, data.dsize, state->mem_ctx, &rec);
+       if (ret != 0) {
+               return;
        }
 
-       if (options.pnn == CTDB_BROADCAST_ALL) {
-               /* read the list of public ips from all nodes */
-               ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
-       } else {
-               /* read the public ip list from this node */
-               ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
+       if (rec->key.dsize == 0 && rec->data.dsize == 0) {
+               talloc_free(rec);
+               /* end of traverse */
+               state->done = true;
+               return;
        }
+
+       ret = ctdb_ltdb_header_extract(&rec->data, &header);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
-               return ret;
+               talloc_free(rec);
+               return;
        }
 
-       if (options.machinereadable){
-               printm(":Public IP:Node:");
-               if (options.verbose){
-                       printm("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
-               }
-               printm("\n");
-       } else {
-               if (options.pnn == CTDB_BROADCAST_ALL) {
-                       printf("Public IPs on ALL nodes\n");
-               } else {
-                       printf("Public IPs on node %u\n", options.pnn);
-               }
+       if (rec->data.dsize == 0) {
+               talloc_free(rec);
+               return;
        }
 
-       for (i=1;i<=ips->num;i++) {
-               struct ctdb_public_ip_info_old *info = NULL;
-               int32_t pnn;
-               char *aciface = NULL;
-               char *avifaces = NULL;
-               char *cifaces = NULL;
+       ret = state->func(rec->reqid, &header, rec->key, rec->data,
+                         &state->sub_state);
+       talloc_free(rec);
+       if (ret != 0) {
+               state->done = true;
+       }
+}
 
-               if (options.pnn == CTDB_BROADCAST_ALL) {
-                       pnn = ips->ips[ips->num-i].pnn;
-               } else {
-                       pnn = options.pnn;
-               }
+static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
+{
+       struct ctdb_db_context *db;
+       const char *db_name;
+       uint32_t db_id;
+       uint8_t db_flags;
+       struct ctdb_traverse_start_ext traverse;
+       struct traverse_state state;
+       int ret;
 
-               if (pnn != -1) {
-                       ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
-                                                  &ips->ips[ips->num-i].addr, &info);
-               } else {
-                       ret = -1;
-               }
+       if (argc != 1) {
+               usage("catdb");
+       }
 
-               if (ret == 0) {
-                       int j;
-                       for (j=0; j < info->num; j++) {
-                               if (cifaces == NULL) {
-                                       cifaces = talloc_strdup(info,
-                                                               info->ifaces[j].name);
-                               } else {
-                                       cifaces = talloc_asprintf_append(cifaces,
-                                                                        ",%s",
-                                                                        info->ifaces[j].name);
-                               }
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
+               return 1;
+       }
 
-                               if (info->active_idx == j) {
-                                       aciface = info->ifaces[j].name;
-                               }
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
+       }
 
-                               if (info->ifaces[j].link_state == 0) {
-                                       continue;
-                               }
+       /* Valgrind fix */
+       ZERO_STRUCT(traverse);
 
-                               if (avifaces == NULL) {
-                                       avifaces = talloc_strdup(info, info->ifaces[j].name);
-                               } else {
-                                       avifaces = talloc_asprintf_append(avifaces,
-                                                                         ",%s",
-                                                                         info->ifaces[j].name);
-                               }
-                       }
-               }
+       traverse.db_id = db_id;
+       traverse.reqid = 0;
+       traverse.srvid = next_srvid(ctdb);
+       traverse.withemptyrecords = false;
 
-               if (options.machinereadable){
-                       printm(":%s:%d:",
-                               ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
-                               ips->ips[ips->num-i].pnn);
-                       if (options.verbose){
-                               printm("%s:%s:%s:",
-                                       aciface?aciface:"",
-                                       avifaces?avifaces:"",
-                                       cifaces?cifaces:"");
-                       }
-                       printf("\n");
-               } else {
-                       if (options.verbose) {
-                               printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
-                                       ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
-                                       ips->ips[ips->num-i].pnn,
-                                       aciface?aciface:"",
-                                       avifaces?avifaces:"",
-                                       cifaces?cifaces:"");
-                       } else {
-                               printf("%s %d\n",
-                                       ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
-                                       ips->ips[ips->num-i].pnn);
-                       }
-               }
-               talloc_free(info);
+       state.mem_ctx = mem_ctx;
+       state.done = false;
+       state.func = dump_record;
+       state.sub_state.count = 0;
+
+       ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
+                                             traverse.srvid,
+                                             traverse_handler, &state);
+       if (ret != 0) {
+               return ret;
+       }
+
+       ret = ctdb_ctrl_traverse_start_ext(mem_ctx, ctdb->ev, ctdb->client,
+                                          ctdb->cmd_pnn, TIMEOUT(),
+                                          &traverse);
+       if (ret != 0) {
+               return ret;
+       }
+
+       ctdb_client_wait(ctdb->ev, &state.done);
+
+       printf("Dumped %u records\n", state.sub_state.count);
+
+       ret = ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
+                                                traverse.srvid, &state);
+       if (ret != 0) {
+               return ret;
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  public ip info
- */
-static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       int i, ret;
-       ctdb_sock_addr addr;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_public_ip_info_old *info;
+       struct ctdb_db_context *db;
+       const char *db_name;
+       uint32_t db_id;
+       uint8_t db_flags;
+       struct dump_record_state state;
+       int ret;
 
        if (argc != 1) {
-               talloc_free(tmp_ctx);
-               usage();
+               usage("catdb");
        }
 
-       if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
-               DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
-               return -1;
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
+               return 1;
        }
 
-       /* read the public ip info from this node */
-       ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
-                                          tmp_ctx, &addr, &info);
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
-                                 argv[0], options.pnn));
-               talloc_free(tmp_ctx);
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
                return ret;
        }
 
-       printf("Public IP[%s] info on node %u\n",
-              ctdb_addr_to_str(&info->ip.addr),
-              options.pnn);
+       state.count = 0;
+       ret = ctdb_db_traverse_local(db, true, true, dump_record, &state);
 
-       printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
-              ctdb_addr_to_str(&info->ip.addr),
-              info->ip.pnn, info->num);
+       printf("Dumped %u record(s)\n", state.count);
+
+       return ret;
+}
 
-       for (i=0; i<info->num; i++) {
-               info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
+static int control_getmonmode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
+{
+       int mode, ret;
 
-               printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
-                      i+1, info->ifaces[i].name,
-                      info->ifaces[i].link_state?"up":"down",
-                      (unsigned int)info->ifaces[i].references,
-                      (i==info->active_idx)?" (active)":"");
+       if (argc != 0) {
+               usage("getmonmode");
        }
 
-       talloc_free(tmp_ctx);
+       ret = ctdb_ctrl_get_monmode(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), &mode);
+       if (ret != 0) {
+               return ret;
+       }
+
+       printf("%s\n",
+              (mode == CTDB_MONITORING_ENABLED) ? "ENABLED" : "DISABLED");
        return 0;
 }
 
-/*
-  display interfaces status
- */
-static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_getcapabilities(TALLOC_CTX *mem_ctx,
+                                  struct ctdb_context *ctdb,
+                                  int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       int i;
-       struct ctdb_iface_list_old *ifaces;
+       uint32_t caps;
        int ret;
 
-       /* read the public ip list from this node */
-       ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
-                                 options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc != 0) {
+               usage("getcapabilities");
        }
 
-       if (options.machinereadable){
-               printm(":Name:LinkStatus:References:\n");
-       } else {
-               printf("Interfaces on node %u\n", options.pnn);
+       ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
+                                        ctdb->cmd_pnn, TIMEOUT(), &caps);
+       if (ret != 0) {
+               return ret;
        }
 
-       for (i=0; i<ifaces->num; i++) {
-               if (options.machinereadable){
-                       printm(":%s:%s:%u:\n",
-                              ifaces->ifaces[i].name,
-                              ifaces->ifaces[i].link_state?"1":"0",
-                              (unsigned int)ifaces->ifaces[i].references);
-               } else {
-                       printf("name:%s link:%s references:%u\n",
-                              ifaces->ifaces[i].name,
-                              ifaces->ifaces[i].link_state?"up":"down",
-                              (unsigned int)ifaces->ifaces[i].references);
-               }
+       if (options.machinereadable == 1) {
+               printf("%s%s%s%s%s\n",
+                      options.sep,
+                      "RECMASTER", options.sep,
+                      "LMASTER", options.sep);
+               printf("%s%d%s%d%s\n", options.sep,
+                      !! (caps & CTDB_CAP_RECMASTER), options.sep,
+                      !! (caps & CTDB_CAP_LMASTER), options.sep);
+       } else {
+               printf("RECMASTER: %s\n",
+                      (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
+               printf("LMASTER: %s\n",
+                      (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
+static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                      int argc, const char **argv)
+{
+       printf("%u\n", ctdb_client_pnn(ctdb->client));
+       return 0;
+}
 
-/*
-  set link status of an interface
- */
-static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                      int argc, const char **argv)
 {
-       int ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_iface info;
+       char *t, *lvs_helper = NULL;
 
-       ZERO_STRUCT(info);
+       if (argc != 1) {
+               usage("lvs");
+       }
 
-       if (argc != 2) {
-               usage();
+       t = getenv("CTDB_LVS_HELPER");
+       if (t != NULL) {
+               lvs_helper = talloc_strdup(mem_ctx, t);
+       } else {
+               lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
+                                            CTDB_HELPER_BINDIR);
        }
 
-       if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
-               DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
-                                 argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (lvs_helper == NULL) {
+               fprintf(stderr, "Unable to set LVS helper\n");
+               return 1;
        }
-       strncpy(info.name, argv[0], sizeof(info.name)-1);
-       info.name[sizeof(info.name)-1] = '\0';
 
-       if (strcmp(argv[1], "up") == 0) {
-               info.link_state = 1;
-       } else if (strcmp(argv[1], "down") == 0) {
-               info.link_state = 0;
-       } else {
-               DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
-                                 argv[1]));
-               talloc_free(tmp_ctx);
-               return -1;
+       return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
+}
+
+static int control_disable_monitor(TALLOC_CTX *mem_ctx,
+                                  struct ctdb_context *ctdb,
+                                  int argc, const char **argv)
+{
+       int ret;
+
+       if (argc != 0) {
+               usage("disablemonitor");
        }
 
-       /* read the public ip list from this node */
-       ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
-                                  tmp_ctx, &info);
+       ret = ctdb_ctrl_disable_monitor(mem_ctx, ctdb->ev, ctdb->client,
+                                       ctdb->cmd_pnn, TIMEOUT());
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
-                                 argv[0], options.pnn));
-               talloc_free(tmp_ctx);
                return ret;
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  display pid of a ctdb daemon
- */
-static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_enable_monitor(TALLOC_CTX *mem_ctx,
+                                 struct ctdb_context *ctdb,
+                                 int argc, const char **argv)
 {
-       uint32_t pid;
        int ret;
 
-       ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
+       if (argc != 0) {
+               usage("enablemonitor");
+       }
+
+       ret = ctdb_ctrl_enable_monitor(mem_ctx, ctdb->ev, ctdb->client,
+                                      ctdb->cmd_pnn, TIMEOUT());
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
                return ret;
        }
-       printf("Pid:%d\n", pid);
 
        return 0;
 }
 
-typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
-
-static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
-                                             void *data,
-                                             update_flags_handler_t handler,
-                                             uint32_t flag,
-                                             const char *desc,
-                                             bool set_flag)
+static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
-       struct ctdb_node_map_old *nodemap = NULL;
-       bool flag_is_set;
+       int log_level;
        int ret;
+       bool found;
 
-       /* Check if the node is already in the desired state */
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
-               exit(10);
-       }
-       flag_is_set = nodemap->nodes[options.pnn].flags & flag;
-       if (set_flag == flag_is_set) {
-               DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
-                                    (set_flag ? "already" : "not"), desc));
-               return 0;
+       if (argc != 1) {
+               usage("setdebug");
        }
 
-       do {
-               if (!handler(ctdb, data)) {
-                       DEBUG(DEBUG_WARNING,
-                             ("Failed to send control to set state %s on node %u, try again\n",
-                              desc, options.pnn));
-               }
-
-               sleep(1);
+       found = debug_level_parse(argv[0], &log_level);
+       if (! found) {
+               fprintf(stderr,
+                       "Invalid debug level '%s'. Valid levels are:\n",
+                       argv[0]);
+               fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
+               return 1;
+       }
 
-               /* Read the nodemap and verify the change took effect.
-                * Even if the above control/hanlder timed out then it
-                * could still have worked!
-                */
-               ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
-                                        ctdb, &nodemap);
-               if (ret != 0) {
-                       DEBUG(DEBUG_WARNING,
-                             ("Unable to get nodemap from local node, try again\n"));
-               }
-               flag_is_set = nodemap->nodes[options.pnn].flags & flag;
-       } while (nodemap == NULL || (set_flag != flag_is_set));
+       ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
+                                ctdb->cmd_pnn, TIMEOUT(), log_level);
+       if (ret != 0) {
+               return ret;
+       }
 
-       return ipreallocate(ctdb);
+       return 0;
 }
 
-/* Administratively disable a node */
-static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
+static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
+       int loglevel;
+       const char *log_str;
        int ret;
 
-       ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
-                                NODE_FLAGS_PERMANENTLY_DISABLED, 0);
-       return ret == 0;
-}
-
-static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       return update_flags_and_ipreallocate(ctdb, NULL,
-                                                 update_flags_disabled,
-                                                 NODE_FLAGS_PERMANENTLY_DISABLED,
-                                                 "disabled",
-                                                 true /* set_flag*/);
-}
+       if (argc != 0) {
+               usage("getdebug");
+       }
 
-/* Administratively re-enable a node */
-static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
-{
-       int ret;
+       ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
+                                ctdb->cmd_pnn, TIMEOUT(), &loglevel);
+       if (ret != 0) {
+               return ret;
+       }
 
-       ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
-                                0, NODE_FLAGS_PERMANENTLY_DISABLED);
-       return ret == 0;
-}
+       log_str = debug_level_to_string(loglevel);
+       printf("%s\n", log_str);
 
-static int control_enable(struct ctdb_context *ctdb,  int argc, const char **argv)
-{
-       return update_flags_and_ipreallocate(ctdb, NULL,
-                                                 update_flags_not_disabled,
-                                                 NODE_FLAGS_PERMANENTLY_DISABLED,
-                                                 "disabled",
-                                                 false /* set_flag*/);
+       return 0;
 }
 
-/* Stop a node */
-static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
+static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
+       const char *db_name;
+       uint8_t db_flags = 0;
        int ret;
 
-       ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
+       if (argc < 1 || argc > 2) {
+               usage("attach");
+       }
 
-       return ret == 0;
-}
+       db_name = argv[0];
+       if (argc == 2) {
+               if (strcmp(argv[1], "persistent") == 0) {
+                       db_flags = CTDB_DB_FLAGS_PERSISTENT;
+               } else if (strcmp(argv[1], "readonly") == 0) {
+                       db_flags = CTDB_DB_FLAGS_READONLY;
+               } else if (strcmp(argv[1], "sticky") == 0) {
+                       db_flags = CTDB_DB_FLAGS_STICKY;
+               } else {
+                       usage("attach");
+               }
+       }
 
-static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       return update_flags_and_ipreallocate(ctdb, NULL,
-                                                 update_flags_stopped,
-                                                 NODE_FLAGS_STOPPED,
-                                                 "stopped",
-                                                 true /* set_flag*/);
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, NULL);
+       if (ret != 0) {
+               return ret;
+       }
+
+       return 0;
 }
 
-/* Continue a stopped node */
-static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
+static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       int ret;
+       const char *db_name;
+       uint32_t db_id;
+       uint8_t db_flags;
+       struct ctdb_node_map *nodemap;
+       int recmode;
+       int ret, ret2, i;
 
-       ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
+       if (argc < 1) {
+               usage("detach");
+       }
 
-       return ret == 0;
-}
+       ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), &recmode);
+       if (ret != 0) {
+               return ret;
+       }
 
-static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       return update_flags_and_ipreallocate(ctdb, NULL,
-                                                 update_flags_not_stopped,
-                                                 NODE_FLAGS_STOPPED,
-                                                 "stopped",
-                                                 false /* set_flag */);
-}
+       if (recmode == CTDB_RECOVERY_ACTIVE) {
+               fprintf(stderr, "Database cannot be detached"
+                               " when recovery is active\n");
+               return 1;
+       }
 
-static uint32_t get_generation(struct ctdb_context *ctdb)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_vnn_map *vnnmap=NULL;
-       int ret;
-       uint32_t generation;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
 
-       /* wait until the recmaster is not in recovery mode */
-       while (1) {
-               uint32_t recmode, recmaster;
-               
-               if (vnnmap != NULL) {
-                       talloc_free(vnnmap);
-                       vnnmap = NULL;
-               }
+       for (i=0; i<nodemap->num; i++) {
+               uint32_t value;
 
-               /* get the recmaster */
-               ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
-                       talloc_free(tmp_ctx);
-                       exit(10);
+               if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
+                       continue;
                }
-
-               /* get recovery mode */
-               ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
-                       talloc_free(tmp_ctx);
-                       exit(10);
+               if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
+               if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
+                       fprintf(stderr, "Database cannot be detached on"
+                               " inactive (stopped or banned) node %u\n",
+                               nodemap->node[i].pnn);
+                       return 1;
                }
 
-               /* get the current generation number */
-               ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
+               ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
+                                           nodemap->node[i].pnn, TIMEOUT(),
+                                           "AllowClientDBAttach", &value);
                if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
-                       talloc_free(tmp_ctx);
-                       exit(10);
+                       fprintf(stderr,
+                               "Unable to get tunable AllowClientDBAttach"
+                               " from node %u\n", nodemap->node[i].pnn);
+                       return ret;
                }
 
-               if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
-                       generation = vnnmap->generation;
-                       talloc_free(tmp_ctx);
-                       return generation;
+               if (value == 1) {
+                       fprintf(stderr,
+                               "Database access is still active on node %u."
+                               " Set AllowclientDBAttach=0 on all nodes.\n",
+                               nodemap->node[i].pnn);
+                       return 1;
                }
-               sleep(1);
        }
-}
 
-/* Ban a node */
-static bool update_state_banned(struct ctdb_context *ctdb, void *data)
-{
-       struct ctdb_ban_state *bantime = (struct ctdb_ban_state *)data;
-       int ret;
+       ret2 = 0;
+       for (i=0; i<argc; i++) {
+               if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
+                               &db_flags)) {
+                       continue;
+               }
 
-       ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
+               if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+                       fprintf(stderr,
+                               "Persistent database %s cannot be detached\n",
+                               argv[0]);
+                       return 1;
+               }
+
+               ret = ctdb_detach(mem_ctx, ctdb->ev, ctdb->client,
+                                 TIMEOUT(), db_id);
+               if (ret != 0) {
+                       fprintf(stderr, "Database %s detach failed\n", db_name);
+                       ret2 = ret;
+               }
+       }
 
-       return ret == 0;
+       return ret2;
 }
 
-static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
 {
-       struct ctdb_ban_state bantime;
+       const char *mem_str;
+       ssize_t n;
+       int ret;
 
-       if (argc < 1) {
-               usage();
+       ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(), &mem_str);
+       if (ret != 0) {
+               return ret;
        }
-       
-       bantime.pnn  = options.pnn;
-       bantime.time = strtoul(argv[0], NULL, 0);
 
-       if (bantime.time == 0) {
-               DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
-               return -1;
+       n = write(1, mem_str, strlen(mem_str)+1);
+       if (n < 0 || n != strlen(mem_str)+1) {
+               fprintf(stderr, "Failed to write talloc summary\n");
+               return 1;
        }
 
-       return update_flags_and_ipreallocate(ctdb, &bantime,
-                                                 update_state_banned,
-                                                 NODE_FLAGS_BANNED,
-                                                 "banned",
-                                                 true /* set_flag*/);
+       return 0;
 }
 
-
-/* Unban a node */
-static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
+static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
 {
-       struct ctdb_ban_state bantime;
+       bool *done = (bool *)private_data;
+       ssize_t n;
 
-       bantime.pnn  = options.pnn;
-       bantime.time = 0;
+       n = write(1, data.dptr, data.dsize);
+       if (n < 0 || n != data.dsize) {
+               fprintf(stderr, "Failed to write talloc summary\n");
+       }
 
-       return update_flags_and_ipreallocate(ctdb, &bantime,
-                                                 update_state_banned,
-                                                 NODE_FLAGS_BANNED,
-                                                 "banned",
-                                                 false /* set_flag*/);
+       *done = true;
 }
 
-/*
-  show ban information for a node
- */
-static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
+       struct ctdb_srvid_message msg = { 0 };
        int ret;
-       struct ctdb_node_map_old *nodemap=NULL;
-       struct ctdb_ban_state *bantime;
+       bool done = false;
 
-       /* verify the node exists */
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
+       msg.pnn = ctdb->pnn;
+       msg.srvid = next_srvid(ctdb);
+
+       ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
+                                             msg.srvid, dump_memory, &done);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
                return ret;
        }
 
-       ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
+       ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, &msg);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
-               return -1;
-       }       
-
-       if (bantime->time == 0) {
-               printf("Node %u is not banned\n", bantime->pnn);
-       } else {
-               printf("Node %u is banned, %d seconds remaining\n",
-                      bantime->pnn, bantime->time);
+               return ret;
        }
 
+       ctdb_client_wait(ctdb->ev, &done);
        return 0;
 }
 
-/*
-  shutdown a daemon
- */
-static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
+       pid_t pid;
        int ret;
 
-       ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
+       ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
+                               ctdb->cmd_pnn, TIMEOUT(), &pid);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
                return ret;
        }
 
+       printf("%u\n", pid);
        return 0;
 }
 
-/*
-  trigger a recovery
- */
-static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
+static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                      const char *desc, uint32_t flag, bool set_flag)
 {
-       int ret;
-       uint32_t generation, next_generation;
+       struct ctdb_node_map *nodemap;
+       bool flag_is_set;
 
-       /* record the current generation number */
-       generation = get_generation(ctdb);
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
 
-       ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
-               return ret;
+       flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
+       if (set_flag == flag_is_set) {
+               if (set_flag) {
+                       fprintf(stderr, "Node %u is already %s\n",
+                               ctdb->cmd_pnn, desc);
+               } else {
+                       fprintf(stderr, "Node %u is not %s\n",
+                               ctdb->cmd_pnn, desc);
+               }
+               return 0;
        }
 
-       /* wait until we are in a new generation */
+       return 1;
+}
+
+static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          uint32_t flag, bool set_flag)
+{
+       struct ctdb_node_map *nodemap;
+       bool flag_is_set;
+
        while (1) {
-               next_generation = get_generation(ctdb);
-               if (next_generation != generation) {
-                       return 0;
+               nodemap = get_nodemap(ctdb, true);
+               if (nodemap == NULL) {
+                       fprintf(stderr,
+                               "Failed to get nodemap, trying again\n");
+                       sleep(1);
+                       continue;
+               }
+
+               flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
+               if (flag_is_set == set_flag) {
+                       break;
                }
+
                sleep(1);
        }
-
-       return 0;
 }
 
+struct ipreallocate_state {
+       int status;
+       bool done;
+};
 
-/*
-  display monitoring mode of a remote node
- */
-static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
+static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
+                                void *private_data)
 {
-       uint32_t monmode;
-       int ret;
+       struct ipreallocate_state *state =
+               (struct ipreallocate_state *)private_data;
 
-       ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
-               return ret;
-       }
-       if (!options.machinereadable){
-               printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
-       } else {
-               printm(":mode:\n");
-               printm(":%d:\n",monmode);
+       if (data.dsize != sizeof(int)) {
+               /* Ignore packet */
+               return;
        }
-       return 0;
-}
 
+       state->status = *(int *)data.dptr;
+       state->done = true;
+}
 
-/*
-  display capabilities of a remote node
- */
-static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
+static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
 {
-       uint32_t capabilities;
+       struct ctdb_srvid_message msg = { 0 };
+       struct ipreallocate_state state;
        int ret;
 
-       ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
+       msg.pnn = ctdb->pnn;
+       msg.srvid = next_srvid(ctdb);
+
+       state.done = false;
+       ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
+                                             msg.srvid,
+                                             ipreallocate_handler, &state);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
-               return -1;
+               return ret;
        }
 
-       if (!options.machinereadable){
-               printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
-               printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
-       } else {
-               printm(":RECMASTER:LMASTER:LVS:\n");
-               printm(":%d:%d:\n",
-                       !!(capabilities&CTDB_CAP_RECMASTER),
-                       !!(capabilities&CTDB_CAP_LMASTER));
+       while (true) {
+               ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
+                                               ctdb->client,
+                                               CTDB_BROADCAST_CONNECTED,
+                                               &msg);
+               if (ret != 0) {
+                       goto fail;
+               }
+
+               ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
+                                              TIMEOUT());
+               if (ret != 0) {
+                       continue;
+               }
+
+               if (state.status >= 0) {
+                       ret = 0;
+               } else {
+                       ret = state.status;
+               }
+               break;
        }
-       return 0;
+
+fail:
+       ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
+                                          msg.srvid, &state);
+       return ret;
 }
 
-/* Display LVS status */
-static int control_lvs(struct ctdb_context *ctdb,
-                      int argc, const char **argv)
+static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
 {
-       static char prog[PATH_MAX+1] = "";
+       int ret;
 
-       if (argc != 1) {
-               usage();
+       if (argc != 0) {
+               usage("disable");
        }
 
-       if (!ctdb_set_helper("LVS helper", prog, sizeof(prog),
-                            "CTDB_LVS_HELPER", CTDB_HELPER_BINDIR,
-                            "ctdb_lvs")) {
-               DEBUG(DEBUG_ERR, ("Unable to set LVS helper\n"));
-               exit(1);
+       ret = check_flags(mem_ctx, ctdb, "disabled",
+                         NODE_FLAGS_PERMANENTLY_DISABLED, true);
+       if (ret == 0) {
+               return 0;
        }
 
-       execl(prog, prog, argv[0], NULL);
+       ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
+                                ctdb->cmd_pnn, TIMEOUT(),
+                                NODE_FLAGS_PERMANENTLY_DISABLED, 0);
+       if (ret != 0) {
+               fprintf(stderr,
+                       "Failed to set DISABLED flag on node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
+       }
 
-       DEBUG(DEBUG_ERR,
-             ("Unable to run LVS helper %s\n", strerror(errno)));
-       exit(1);
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
+       return ipreallocate(mem_ctx, ctdb);
 }
 
-/*
-  disable monitoring on a  node
- */
-static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       
        int ret;
 
-       ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
+       if (argc != 0) {
+               usage("enable");
+       }
+
+       ret = check_flags(mem_ctx, ctdb, "disabled",
+                         NODE_FLAGS_PERMANENTLY_DISABLED, false);
+       if (ret == 0) {
+               return 0;
+       }
+
+       ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
+                                ctdb->cmd_pnn, TIMEOUT(),
+                                0, NODE_FLAGS_PERMANENTLY_DISABLED);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
+               fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
+                       ctdb->cmd_pnn);
                return ret;
        }
-       printf("Monitoring mode:%s\n","DISABLED");
 
-       return 0;
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
+       return ipreallocate(mem_ctx, ctdb);
 }
 
-/*
-  enable monitoring on a  node
- */
-static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                       int argc, const char **argv)
 {
-       
        int ret;
 
-       ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
+       if (argc != 0) {
+               usage("stop");
+       }
+
+       ret = check_flags(mem_ctx, ctdb, "stopped",
+                         NODE_FLAGS_STOPPED, true);
+       if (ret == 0) {
+               return 0;
+       }
+
+       ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
+                                 ctdb->cmd_pnn, TIMEOUT());
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
+               fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
                return ret;
        }
-       printf("Monitoring mode:%s\n","ACTIVE");
 
-       return 0;
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
+       return ipreallocate(mem_ctx, ctdb);
 }
 
-/*
-  display remote list of keys/data for a db
- */
-static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
        int ret;
-       struct ctdb_dump_db_context c;
-       uint8_t flags;
 
-       if (argc < 1) {
-               usage();
+       if (argc != 0) {
+               usage("continue");
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               return -1;
+       ret = check_flags(mem_ctx, ctdb, "stopped",
+                         NODE_FLAGS_STOPPED, false);
+       if (ret == 0) {
+               return 0;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               return -1;
+       ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT());
+       if (ret != 0) {
+               fprintf(stderr, "Failed to continue stopped node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
        }
 
-       if (options.printlmaster) {
-               ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
-                                         ctdb, &ctdb->vnn_map);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
-                                         options.pnn));
-                       return ret;
-               }
-       }
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
+       return ipreallocate(mem_ctx, ctdb);
+}
 
-       ZERO_STRUCT(c);
-       c.ctdb = ctdb;
-       c.f = stdout;
-       c.printemptyrecords = (bool)options.printemptyrecords;
-       c.printdatasize = (bool)options.printdatasize;
-       c.printlmaster = (bool)options.printlmaster;
-       c.printhash = (bool)options.printhash;
-       c.printrecordflags = (bool)options.printrecordflags;
+static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                      int argc, const char **argv)
+{
+       struct ctdb_ban_state ban_state;
+       int ret;
 
-       /* traverse and dump the cluster tdb */
-       ret = ctdb_dump_db(ctdb_db, &c);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
-               DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
-                                 " and 'ctdb getvar AllowUnhealthyDBRead'\n",
-                                 db_name));
-               return -1;
+       if (argc != 1) {
+               usage("ban");
        }
-       talloc_free(ctdb_db);
 
-       printf("Dumped %d records\n", ret);
-       return 0;
-}
+       ret = check_flags(mem_ctx, ctdb, "banned",
+                         NODE_FLAGS_BANNED, true);
+       if (ret == 0) {
+               return 0;
+       }
 
-struct cattdb_data {
-       struct ctdb_context *ctdb;
-       uint32_t count;
-};
+       ban_state.pnn = ctdb->cmd_pnn;
+       ban_state.time = strtoul(argv[0], NULL, 0);
 
-static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
-{
-       struct cattdb_data *d = private_data;
-       struct ctdb_dump_db_context c;
+       if (ban_state.time == 0) {
+               fprintf(stderr, "Ban time cannot be zero\n");
+               return EINVAL;
+       }
 
-       d->count++;
+       ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &ban_state);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
+               return ret;
+       }
 
-       ZERO_STRUCT(c);
-       c.ctdb = d->ctdb;
-       c.f = stdout;
-       c.printemptyrecords = (bool)options.printemptyrecords;
-       c.printdatasize = (bool)options.printdatasize;
-       c.printlmaster = false;
-       c.printhash = (bool)options.printhash;
-       c.printrecordflags = true;
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
+       return ipreallocate(mem_ctx, ctdb);
 
-       return ctdb_dumpdb_record(key, data, &c);
 }
 
-/*
-  cat the local tdb database using same format as catdb
- */
-static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
 {
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       struct cattdb_data d;
-       uint8_t flags;
+       struct ctdb_ban_state ban_state;
+       int ret;
 
-       if (argc < 1) {
-               usage();
+       if (argc != 0) {
+               usage("unban");
+       }
+
+       ret = check_flags(mem_ctx, ctdb, "banned",
+                         NODE_FLAGS_BANNED, false);
+       if (ret == 0) {
+               return 0;
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               return -1;
+       ban_state.pnn = ctdb->cmd_pnn;
+       ban_state.time = 0;
+
+       ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &ban_state);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
+               return ret;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               return -1;
+       wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
+       return ipreallocate(mem_ctx, ctdb);
+
+}
+
+static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
+{
+       int ret;
+
+       if (argc != 0) {
+               usage("shutdown");
        }
 
-       /* traverse the local tdb */
-       d.count = 0;
-       d.ctdb  = ctdb;
-       if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
-               printf("Failed to cattdb data\n");
-               exit(10);
+       ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
+                                ctdb->cmd_pnn, TIMEOUT());
+       if (ret != 0) {
+               fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
+               return ret;
        }
-       talloc_free(ctdb_db);
 
-       printf("Dumped %d records\n", d.count);
        return 0;
 }
 
-/*
-  display the content of a database key
- */
-static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
+static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         uint32_t *generation)
 {
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       struct ctdb_record_handle *h;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA key, data;
-       uint8_t flags;
+       uint32_t recmaster;
+       int recmode;
+       struct ctdb_vnn_map *vnnmap;
+       int ret;
 
-       if (argc < 2) {
-               usage();
+again:
+       ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &recmaster);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to find recovery master\n");
+               return ret;
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               return -1;
+       ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                                   recmaster, TIMEOUT(), &recmode);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get recovery mode from node %u\n",
+                       recmaster);
+               return ret;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               return -1;
+       if (recmode == CTDB_RECOVERY_ACTIVE) {
+               sleep(1);
+               goto again;
        }
 
-       key.dptr  = discard_const(argv[1]);
-       key.dsize = strlen((char *)key.dptr);
-
-       h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
-       if (h == NULL) {
-               printf("Failed to fetch record '%s' on node %d\n", 
-                       (const char *)key.dptr, ctdb_get_pnn(ctdb));
-               talloc_free(tmp_ctx);
-               exit(10);
+       ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
+                                 recmaster, TIMEOUT(), &vnnmap);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get generation from node %u\n",
+                       recmaster);
+               return ret;
        }
 
-       printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
+       if (vnnmap->generation == INVALID_GENERATION) {
+               talloc_free(vnnmap);
+               sleep(1);
+               goto again;
+       }
 
-       talloc_free(tmp_ctx);
-       talloc_free(ctdb_db);
+       *generation = vnnmap->generation;
+       talloc_free(vnnmap);
        return 0;
 }
 
-/*
-  display the content of a database key
- */
-static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
+
+static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
 {
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       struct ctdb_record_handle *h;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA key, data;
-       uint8_t flags;
+       uint32_t generation, next_generation;
+       int ret;
 
-       if (argc < 3) {
-               usage();
+       if (argc != 0) {
+               usage("recover");
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               return -1;
+       ret = get_generation(mem_ctx, ctdb, &generation);
+       if (ret != 0) {
+               return ret;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               return -1;
+       ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                                   ctdb->cmd_pnn, TIMEOUT(),
+                                   CTDB_RECOVERY_ACTIVE);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to set recovery mode active\n");
+               return ret;
        }
 
-       key.dptr  = discard_const(argv[1]);
-       key.dsize = strlen((char *)key.dptr);
+       while (1) {
+               ret = get_generation(mem_ctx, ctdb, &next_generation);
+               if (ret != 0) {
+                       fprintf(stderr,
+                               "Failed to confirm end of recovery\n");
+                       return ret;
+               }
+
+               if (next_generation != generation) {
+                       break;
+               }
 
-       h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
-       if (h == NULL) {
-               printf("Failed to fetch record '%s' on node %d\n", 
-                       (const char *)key.dptr, ctdb_get_pnn(ctdb));
-               talloc_free(tmp_ctx);
-               exit(10);
+               sleep (1);
        }
 
-       data.dptr  = discard_const(argv[2]);
-       data.dsize = strlen((char *)data.dptr);
+       return 0;
+}
 
-       if (ctdb_record_store(h, data) != 0) {
-               printf("Failed to store record\n");
+static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
+{
+       if (argc != 0) {
+               usage("ipreallocate");
        }
 
-       talloc_free(h);
-       talloc_free(tmp_ctx);
-       talloc_free(ctdb_db);
-       return 0;
+       return ipreallocate(mem_ctx, ctdb);
 }
 
-/*
-  fetch a record from a persistent database
- */
-static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
+                                 struct ctdb_context *ctdb,
+                                 int argc, const char **argv)
 {
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_transaction_handle *h;
-       TDB_DATA key, data;
-       int fd, ret;
-       bool persistent;
-       uint8_t flags;
+       uint32_t recmaster;
+       int ret;
 
-       if (argc < 2) {
-               talloc_free(tmp_ctx);
-               usage();
+       if (argc != 0) {
+               usage("isnotrecmaster");
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->pnn, TIMEOUT(), &recmaster);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get recmaster\n");
+               return ret;
        }
 
-       persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
-       if (!persistent) {
-               DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (recmaster != ctdb->pnn) {
+               printf("this node is not the recmaster\n");
+               return 1;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       printf("this node is the recmaster\n");
+       return 0;
+}
 
-       h = ctdb_transaction_start(ctdb_db, tmp_ctx);
-       if (h == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
+{
+       struct ctdb_addr_info addr_info;
+       int ret;
 
-       key.dptr  = discard_const(argv[1]);
-       key.dsize = strlen(argv[1]);
-       ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc != 2) {
+               usage("gratarp");
        }
 
-       if (data.dsize == 0 || data.dptr == NULL) {
-               DEBUG(DEBUG_ERR,("Record is empty\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (! parse_ip(argv[0], NULL, 0, &addr_info.addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
        }
+       addr_info.iface = argv[1];
 
-       if (argc == 3) {
-         fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
-               if (fd == -1) {
-                       DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
-               sys_write(fd, data.dptr, data.dsize);
-               close(fd);
-       } else {
-               sys_write(1, data.dptr, data.dsize);
+       ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
+                                           ctdb->cmd_pnn, TIMEOUT(),
+                                           &addr_info);
+       if (ret != 0) {
+               fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
        }
 
-       /* abort the transaction */
-       talloc_free(h);
-
-
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  fetch a record from a tdb-file
- */
-static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
 {
-       const char *tdb_file;
-       TDB_CONTEXT *tdb;
-       TDB_DATA key, data;
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       int fd;
+       ctdb_sock_addr src, dst;
+       int ret;
 
-       if (argc < 2) {
-               usage();
+       if (argc != 0 && argc != 2) {
+               usage("tickle");
        }
 
-       tdb_file = argv[0];
+       if (argc == 0) {
+               struct ctdb_connection *clist;
+               int count;
+               int i, num_failed;
 
-       tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
-       if (tdb == NULL) {
-               printf("Failed to open TDB file %s\n", tdb_file);
-               return -1;
-       }
+               ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
+               if (ret != 0) {
+                       return ret;
+               }
 
-       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
-       if (key.dptr == NULL) {
-               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-               return -1;
-       }
+               num_failed = 0;
+               for (i=0; i<count; i++) {
+                       ret = ctdb_sys_send_tcp(&clist[i].src,
+                                               &clist[i].dst,
+                                               0, 0, 0);
+                       if (ret != 0) {
+                               num_failed += 1;
+                       }
+               }
 
-       data = tdb_fetch(tdb, key);
-       if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
-               printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
-               tdb_close(tdb);
-               return -1;
+               TALLOC_FREE(clist);
+
+               if (num_failed > 0) {
+                       fprintf(stderr, "Failed to send %d tickles\n",
+                               num_failed);
+                       return 1;
+               }
+
+               return 0;
+       }
+
+
+       if (! parse_ip_port(argv[0], &src)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
+       }
+
+       if (! parse_ip_port(argv[1], &dst)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[1]);
+               return 1;
+       }
+
+       ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to send tickle ack\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
+{
+       ctdb_sock_addr addr;
+       struct ctdb_tickle_list *tickles;
+       unsigned port = 0;
+       int ret, i;
+
+       if (argc < 1 || argc > 2) {
+               usage("gettickles");
+       }
+
+       if (argc == 2) {
+               port = strtoul(argv[1], NULL, 10);
+       }
+
+       if (! parse_ip(argv[0], NULL, port, &addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
+       }
+
+       ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
+                                           ctdb->cmd_pnn, TIMEOUT(), &addr,
+                                           &tickles);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get list of connections\n");
+               return ret;
+       }
+
+       if (options.machinereadable) {
+               printf("%s%s%s%s%s%s%s%s%s\n",
+                      options.sep,
+                      "Source IP", options.sep,
+                      "Port", options.sep,
+                      "Destiation IP", options.sep,
+                      "Port", options.sep);
+               for (i=0; i<tickles->num; i++) {
+                       printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
+                              ctdb_sock_addr_to_string(
+                                      mem_ctx, &tickles->conn[i].src),
+                              options.sep,
+                              ntohs(tickles->conn[i].src.ip.sin_port),
+                              options.sep,
+                              ctdb_sock_addr_to_string(
+                                      mem_ctx, &tickles->conn[i].dst),
+                              options.sep,
+                              ntohs(tickles->conn[i].dst.ip.sin_port),
+                              options.sep);
+               }
+       } else {
+               printf("Connections for IP: %s\n",
+                      ctdb_sock_addr_to_string(mem_ctx, &tickles->addr));
+               printf("Num connections: %u\n", tickles->num);
+               for (i=0; i<tickles->num; i++) {
+                       printf("SRC: %s:%u   DST: %s:%u\n",
+                              ctdb_sock_addr_to_string(
+                                      mem_ctx, &tickles->conn[i].src),
+                              ntohs(tickles->conn[i].src.ip.sin_port),
+                              ctdb_sock_addr_to_string(
+                                      mem_ctx, &tickles->conn[i].dst),
+                              ntohs(tickles->conn[i].dst.ip.sin_port));
+               }
+       }
+
+       talloc_free(tickles);
+       return 0;
+}
+
+typedef void (*clist_request_func)(struct ctdb_req_control *request,
+                                  struct ctdb_connection *conn);
+
+typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
+
+struct process_clist_state {
+       struct ctdb_connection *clist;
+       int count;
+       int num_failed, num_total;
+       clist_reply_func reply_func;
+};
+
+static void process_clist_done(struct tevent_req *subreq);
+
+static struct tevent_req *process_clist_send(
+                                       TALLOC_CTX *mem_ctx,
+                                       struct ctdb_context *ctdb,
+                                       struct ctdb_connection *clist,
+                                       int count,
+                                       clist_request_func request_func,
+                                       clist_reply_func reply_func)
+{
+       struct tevent_req *req, *subreq;
+       struct process_clist_state *state;
+       struct ctdb_req_control request;
+       int i;
+
+       req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
+       if (req == NULL) {
+               return NULL;
+       }
+
+       state->clist = clist;
+       state->count = count;
+       state->reply_func = reply_func;
+
+       for (i=0; i<count; i++) {
+               request_func(&request, &clist[i]);
+               subreq = ctdb_client_control_send(state, ctdb->ev,
+                                                 ctdb->client, ctdb->cmd_pnn,
+                                                 TIMEOUT(), &request);
+               if (tevent_req_nomem(subreq, req)) {
+                       return tevent_req_post(req, ctdb->ev);
+               }
+               tevent_req_set_callback(subreq, process_clist_done, req);
+       }
+
+       return req;
+}
+
+static void process_clist_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct process_clist_state *state = tevent_req_data(
+               req, struct process_clist_state);
+       struct ctdb_reply_control *reply;
+       int ret;
+       bool status;
+
+       status = ctdb_client_control_recv(subreq, NULL, state, &reply);
+       TALLOC_FREE(subreq);
+       if (! status) {
+               state->num_failed += 1;
+               goto done;
+       }
+
+       ret = state->reply_func(reply);
+       if (ret != 0) {
+               state->num_failed += 1;
+               goto done;
+       }
+
+done:
+       state->num_total += 1;
+       if (state->num_total == state->count) {
+               tevent_req_done(req);
+       }
+}
+
+static int process_clist_recv(struct tevent_req *req)
+{
+       struct process_clist_state *state = tevent_req_data(
+               req, struct process_clist_state);
+
+       return state->num_failed;
+}
+
+static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
+{
+       struct ctdb_connection conn;
+       int ret;
+
+       if (argc != 0 && argc != 2) {
+               usage("addtickle");
+       }
+
+       if (argc == 0) {
+               struct ctdb_connection *clist;
+               struct tevent_req *req;
+               int count;
+
+               ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (count == 0) {
+                       return 0;
+               }
+
+               req = process_clist_send(mem_ctx, ctdb, clist, count,
+                                ctdb_req_control_tcp_add_delayed_update,
+                                ctdb_reply_control_tcp_add_delayed_update);
+               if (req == NULL) {
+                       talloc_free(clist);
+                       return ENOMEM;
+               }
+
+               tevent_req_poll(req, ctdb->ev);
+               talloc_free(clist);
+
+               ret = process_clist_recv(req);
+               if (ret != 0) {
+                       fprintf(stderr, "Failed to add %d tickles\n", ret);
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       if (! parse_ip_port(argv[0], &conn.src)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
+       }
+       if (! parse_ip_port(argv[1], &conn.dst)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[1]);
+               return 1;
+       }
+
+       ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
+                                              ctdb->client, ctdb->cmd_pnn,
+                                              TIMEOUT(), &conn);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to register connection\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
+{
+       struct ctdb_connection conn;
+       int ret;
+
+       if (argc != 0 && argc != 2) {
+               usage("deltickle");
+       }
+
+       if (argc == 0) {
+               struct ctdb_connection *clist;
+               struct tevent_req *req;
+               int count;
+
+               ret = ctdb_parse_connections(stdin, mem_ctx, &count, &clist);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (count == 0) {
+                       return 0;
+               }
+
+               req = process_clist_send(mem_ctx, ctdb, clist, count,
+                                        ctdb_req_control_tcp_remove,
+                                        ctdb_reply_control_tcp_remove);
+               if (req == NULL) {
+                       talloc_free(clist);
+                       return ENOMEM;
+               }
+
+               tevent_req_poll(req, ctdb->ev);
+               talloc_free(clist);
+
+               ret = process_clist_recv(req);
+               if (ret != 0) {
+                       fprintf(stderr, "Failed to remove %d tickles\n", ret);
+                       return 1;
+               }
+
+               return 0;
+       }
+
+       if (! parse_ip_port(argv[0], &conn.src)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
+       }
+       if (! parse_ip_port(argv[1], &conn.dst)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[1]);
+               return 1;
+       }
+
+       ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
+                                  ctdb->cmd_pnn, TIMEOUT(), &conn);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to unregister connection\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int control_check_srvids(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
+{
+       uint64_t *srvid;
+       uint8_t *result;
+       int ret, i;
+
+       if (argc == 0) {
+               usage("check_srvids");
+       }
+
+       srvid = talloc_array(mem_ctx, uint64_t, argc);
+       if (srvid == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
+
+       for (i=0; i<argc; i++) {
+               srvid[i] = strtoull(argv[i], NULL, 0);
+       }
+
+       ret = ctdb_ctrl_check_srvids(mem_ctx, ctdb->ev, ctdb->client,
+                                    ctdb->cmd_pnn, TIMEOUT(), srvid, argc,
+                                    &result);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to check srvids on node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
+       }
+
+       for (i=0; i<argc; i++) {
+               printf("SRVID 0x%" PRIx64 " %s\n", srvid[i],
+                      (result[i] ? "exists" : "does not exist"));
+       }
+
+       return 0;
+}
+
+static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
+{
+       struct ctdb_node_map *nodemap;
+       int i;
+
+       if (argc != 0) {
+               usage("listnodes");
+       }
+
+       nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
+       if (nodemap == NULL) {
+               return 1;
+       }
+
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
+
+               if (options.machinereadable) {
+                       printf("%s%u%s%s%s\n", options.sep,
+                              nodemap->node[i].pnn, options.sep,
+                              ctdb_sock_addr_to_string(
+                                       mem_ctx, &nodemap->node[i].addr),
+                              options.sep);
+               } else {
+                       printf("%s\n",
+                              ctdb_sock_addr_to_string(
+                                       mem_ctx, &nodemap->node[i].addr));
+               }
+       }
+
+       return 0;
+}
+
+static bool nodemap_identical(struct ctdb_node_map *nodemap1,
+                             struct ctdb_node_map *nodemap2)
+{
+       int i;
+
+       if (nodemap1->num != nodemap2->num) {
+               return false;
+       }
+
+       for (i=0; i<nodemap1->num; i++) {
+               struct ctdb_node_and_flags *n1, *n2;
+
+               n1 = &nodemap1->node[i];
+               n2 = &nodemap2->node[i];
+
+               if ((n1->pnn != n2->pnn) ||
+                   (n1->flags != n2->flags) ||
+                   ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static int check_node_file_changes(TALLOC_CTX *mem_ctx,
+                                  struct ctdb_node_map *nm,
+                                  struct ctdb_node_map *fnm,
+                                  bool *reload)
+{
+       int i;
+       bool check_failed = false;
+
+       *reload = false;
+
+       for (i=0; i<nm->num; i++) {
+               if (i >= fnm->num) {
+                       fprintf(stderr,
+                               "Node %u (%s) missing from nodes file\n",
+                               nm->node[i].pnn,
+                               ctdb_sock_addr_to_string(
+                                       mem_ctx, &nm->node[i].addr));
+                       check_failed = true;
+                       continue;
+               }
+               if (nm->node[i].flags & NODE_FLAGS_DELETED &&
+                   fnm->node[i].flags & NODE_FLAGS_DELETED) {
+                       /* Node remains deleted */
+                       continue;
+               }
+
+               if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
+                   ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
+                       /* Node not newly nor previously deleted */
+                       if (! ctdb_same_ip(&nm->node[i].addr,
+                                          &fnm->node[i].addr)) {
+                               fprintf(stderr,
+                                       "Node %u has changed IP address"
+                                       " (was %s, now %s)\n",
+                                       nm->node[i].pnn,
+                                       ctdb_sock_addr_to_string(
+                                               mem_ctx, &nm->node[i].addr),
+                                       ctdb_sock_addr_to_string(
+                                               mem_ctx, &fnm->node[i].addr));
+                               check_failed = true;
+                       } else {
+                               if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
+                                       fprintf(stderr,
+                                               "WARNING: Node %u is disconnected."
+                                               " You MUST fix this node manually!\n",
+                                               nm->node[i].pnn);
+                               }
+                       }
+                       continue;
+               }
+
+               if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
+                       /* Node is being deleted */
+                       printf("Node %u is DELETED\n", nm->node[i].pnn);
+                       *reload = true;
+                       if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
+                               fprintf(stderr,
+                                       "ERROR: Node %u is still connected\n",
+                                       nm->node[i].pnn);
+                               check_failed = true;
+                       }
+                       continue;
+               }
+
+               if (nm->node[i].flags & NODE_FLAGS_DELETED) {
+                       /* Node was previously deleted */
+                       printf("Node %u is UNDELETED\n", nm->node[i].pnn);
+                       *reload = true;
+               }
+       }
+
+       if (check_failed) {
+               fprintf(stderr,
+                       "ERROR: Nodes will not be reloaded due to previous error\n");
+               return 1;
+       }
+
+       /* Leftover nodes in file are NEW */
+       for (; i < fnm->num; i++) {
+               printf("Node %u is NEW\n", fnm->node[i].pnn);
+               *reload = true;
+       }
+
+       return 0;
+}
+
+struct disable_recoveries_state {
+       uint32_t *pnn_list;
+       int node_count;
+       bool *reply;
+       int status;
+       bool done;
+};
+
+static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
+                                      void *private_data)
+{
+       struct disable_recoveries_state *state =
+               (struct disable_recoveries_state *)private_data;
+       int ret, i;
+
+       if (data.dsize != sizeof(int)) {
+               /* Ignore packet */
+               return;
+       }
+
+       /* ret will be a PNN (i.e. >=0) on success, or negative on error */
+       ret = *(int *)data.dptr;
+       if (ret < 0) {
+               state->status = ret;
+               state->done = true;
+               return;
+       }
+       for (i=0; i<state->node_count; i++) {
+               if (state->pnn_list[i] == ret) {
+                       state->reply[i] = true;
+                       break;
+               }
+       }
+
+       state->done = true;
+       for (i=0; i<state->node_count; i++) {
+               if (! state->reply[i]) {
+                       state->done = false;
+                       break;
+               }
+       }
+}
+
+static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             uint32_t timeout, uint32_t *pnn_list, int count)
+{
+       struct ctdb_disable_message disable = { 0 };
+       struct disable_recoveries_state state;
+       int ret, i;
+
+       disable.pnn = ctdb->pnn;
+       disable.srvid = next_srvid(ctdb);
+       disable.timeout = timeout;
+
+       state.pnn_list = pnn_list;
+       state.node_count = count;
+       state.done = false;
+       state.status = 0;
+       state.reply = talloc_zero_array(mem_ctx, bool, count);
+       if (state.reply == NULL) {
+               return ENOMEM;
+       }
+
+       ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
+                                             disable.srvid,
+                                             disable_recoveries_handler,
+                                             &state);
+       if (ret != 0) {
+               return ret;
+       }
+
+       for (i=0; i<count; i++) {
+               ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
+                                                     ctdb->client,
+                                                     pnn_list[i],
+                                                     &disable);
+               if (ret != 0) {
+                       goto fail;
+               }
+       }
+
+       ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
+       if (ret == ETIME) {
+               fprintf(stderr, "Timed out waiting to disable recoveries\n");
+       } else {
+               ret = (state.status >= 0 ? 0 : 1);
+       }
+
+fail:
+       ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
+                                          disable.srvid, &state);
+       return ret;
+}
+
+static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                              int argc, const char **argv)
+{
+       struct ctdb_node_map *nodemap = NULL;
+       struct ctdb_node_map *file_nodemap;
+       struct ctdb_node_map *remote_nodemap;
+       struct ctdb_req_control request;
+       struct ctdb_reply_control **reply;
+       bool reload;
+       int ret, i;
+       uint32_t *pnn_list;
+       int count;
+
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
+
+       file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
+       if (file_nodemap == NULL) {
+               return 1;
+       }
+
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
+                       continue;
+               }
+
+               ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
+                                              nodemap->node[i].pnn, TIMEOUT(),
+                                              &remote_nodemap);
+               if (ret != 0) {
+                       fprintf(stderr,
+                               "ERROR: Failed to get nodes file from node %u\n",
+                               nodemap->node[i].pnn);
+                       return ret;
+               }
+
+               if (! nodemap_identical(file_nodemap, remote_nodemap)) {
+                       fprintf(stderr,
+                               "ERROR: Nodes file on node %u differs"
+                                " from current node (%u)\n",
+                                nodemap->node[i].pnn, ctdb->pnn);
+                       return 1;
+               }
+       }
+
+       ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
+       if (ret != 0) {
+               return ret;
+       }
+
+       if (! reload) {
+               fprintf(stderr, "No change in nodes file,"
+                               " skipping unnecessary reload\n");
+               return 0;
+       }
+
+       count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
+                                       mem_ctx, &pnn_list);
+       if (count <= 0) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
+
+       ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
+                                pnn_list, count);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to disable recoveries\n");
+               return ret;
+       }
+
+       ctdb_req_control_reload_nodes_file(&request);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, &reply);
+       if (ret != 0) {
+               bool failed = false;
+
+               for (i=0; i<count; i++) {
+                       ret = ctdb_reply_control_reload_nodes_file(reply[i]);
+                       if (ret != 0) {
+                               fprintf(stderr,
+                                       "Node %u failed to reload nodes\n",
+                                       pnn_list[i]);
+                               failed = true;
+                       }
+               }
+               if (failed) {
+                       fprintf(stderr,
+                               "You MUST fix failed nodes manually!\n");
+               }
+       }
+
+       ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to enable recoveries\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                 ctdb_sock_addr *addr, uint32_t pnn)
+{
+       struct ctdb_public_ip_list *pubip_list;
+       struct ctdb_public_ip pubip;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_req_control request;
+       uint32_t *pnn_list;
+       int ret, i, count;
+
+       ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
+                                           CTDB_BROADCAST_CONNECTED,
+                                           2*options.timelimit);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to disable IP check\n");
+               return ret;
+       }
+
+       ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
+                                      pnn, TIMEOUT(), false, &pubip_list);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get Public IPs from node %u\n",
+                       pnn);
+               return ret;
+       }
+
+       for (i=0; i<pubip_list->num; i++) {
+               if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
+                       break;
+               }
+       }
+
+       if (i == pubip_list->num) {
+               fprintf(stderr, "Node %u CANNOT host IP address %s\n",
+                       pnn, ctdb_sock_addr_to_string(mem_ctx, addr));
+               return 1;
+       }
+
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
+
+       count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
+       if (count <= 0) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
+
+       pubip.pnn = pnn;
+       pubip.addr = *addr;
+       ctdb_req_control_release_ip(&request, &pubip);
+
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to release IP on nodes\n");
+               return ret;
+       }
+
+       ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
+                                   pnn, TIMEOUT(), &pubip);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
+               return ret;
+       }
+
+       return 0;
+}
+
+static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
+{
+       ctdb_sock_addr addr;
+       uint32_t pnn;
+       int ret, retries = 0;
+
+       if (argc != 2) {
+               usage("moveip");
+       }
+
+       if (! parse_ip(argv[0], NULL, 0, &addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
+       }
+
+       pnn = strtoul(argv[1], NULL, 10);
+       if (pnn == CTDB_UNKNOWN_PNN) {
+               fprintf(stderr, "Invalid PNN %s\n", argv[1]);
+               return 1;
+       }
+
+       while (retries < 5) {
+               ret = moveip(mem_ctx, ctdb, &addr, pnn);
+               if (ret == 0) {
+                       break;
+               }
+
+               sleep(3);
+               retries++;
+       }
+
+       if (ret != 0) {
+               fprintf(stderr, "Failed to move IP %s to node %u\n",
+                       argv[0], pnn);
+               return ret;
+       }
+
+       return 0;
+}
+
+static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        uint32_t pnn)
+{
+       int ret;
+
+       ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
+                                         CTDB_BROADCAST_CONNECTED, pnn);
+       if (ret != 0) {
+               fprintf(stderr,
+                       "Failed to ask recovery master to distribute IPs\n");
+               return ret;
+       }
+
+       return 0;
+}
+
+static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
+{
+       ctdb_sock_addr addr;
+       struct ctdb_public_ip_list *pubip_list;
+       struct ctdb_addr_info addr_info;
+       unsigned int mask;
+       int ret, i, retries = 0;
+
+       if (argc != 2) {
+               usage("addip");
+       }
+
+       if (! parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
+               fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
+               return 1;
+       }
+
+       ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
+                                      ctdb->cmd_pnn, TIMEOUT(),
+                                      false, &pubip_list);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get Public IPs from node %u\n",
+                       ctdb->cmd_pnn);
+               return 1;
+       }
+
+       for (i=0; i<pubip_list->num; i++) {
+               if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
+                       fprintf(stderr, "Node already knows about IP %s\n",
+                               ctdb_sock_addr_to_string(mem_ctx, &addr));
+                       return 0;
+               }
+       }
+
+       addr_info.addr = addr;
+       addr_info.mask = mask;
+       addr_info.iface = argv[1];
+
+       while (retries < 5) {
+               ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
+                                             ctdb->cmd_pnn, TIMEOUT(),
+                                             &addr_info);
+               if (ret == 0) {
+                       break;
+               }
+
+               sleep(3);
+               retries++;
        }
 
-       tdb_close(tdb);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to add public IP to node %u."
+                               " Giving up\n", ctdb->cmd_pnn);
+               return ret;
+       }
 
-       if (argc == 3) {
-         fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
-               if (fd == -1) {
-                       printf("Failed to open output file %s\n", argv[2]);
-                       return -1;
-               }
-               if (options.verbose){
-                       sys_write(fd, data.dptr, data.dsize);
-               } else {
-                       sys_write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
-               }
-               close(fd);
-       } else {
-               if (options.verbose){
-                       sys_write(1, data.dptr, data.dsize);
-               } else {
-                       sys_write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
-               }
+       ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
+       if (ret != 0) {
+               return ret;
        }
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  store a record and header to a tdb-file
- */
-static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
 {
-       const char *tdb_file;
-       TDB_CONTEXT *tdb;
-       TDB_DATA key, value, data;
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       struct ctdb_ltdb_header header;
+       ctdb_sock_addr addr;
+       struct ctdb_public_ip_list *pubip_list;
+       struct ctdb_addr_info addr_info;
+       int ret, i;
 
-       if (argc < 3) {
-               usage();
+       if (argc != 1) {
+               usage("delip");
        }
 
-       tdb_file = argv[0];
-
-       tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
-       if (tdb == NULL) {
-               printf("Failed to open TDB file %s\n", tdb_file);
-               return -1;
+       if (! parse_ip(argv[0], NULL, 0, &addr)) {
+               fprintf(stderr, "Invalid IP address %s\n", argv[0]);
+               return 1;
        }
 
-       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
-       if (key.dptr == NULL) {
-               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-               return -1;
+       ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
+                                      ctdb->cmd_pnn, TIMEOUT(),
+                                      false, &pubip_list);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get Public IPs from node %u\n",
+                       ctdb->cmd_pnn);
+               return 1;
        }
 
-       value = strtodata(tmp_ctx, argv[2], strlen(argv[2]));
-       if (value.dptr == NULL) {
-               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
-               return -1;
+       for (i=0; i<pubip_list->num; i++) {
+               if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
+                       break;
+               }
        }
 
-       ZERO_STRUCT(header);
-       if (argc > 3) {
-               header.rsn = atoll(argv[3]);
+       if (i == pubip_list->num) {
+               fprintf(stderr, "Node does not know about IP address %s\n",
+                       ctdb_sock_addr_to_string(mem_ctx, &addr));
+               return 0;
        }
-       if (argc > 4) {
-               header.dmaster = atoi(argv[4]);
+
+       addr_info.addr = addr;
+       addr_info.mask = 0;
+       addr_info.iface = NULL;
+
+       ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &addr_info);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to delete public IP from node %u\n",
+                       ctdb->cmd_pnn);
+               return ret;
        }
-       if (argc > 5) {
-               header.flags = atoi(argv[5]);
+
+       return 0;
+}
+
+#define DB_VERSION     3
+#define MAX_DB_NAME    64
+#define MAX_REC_BUFFER_SIZE    (100*1000)
+
+struct db_header {
+       unsigned long version;
+       time_t timestamp;
+       unsigned long flags;
+       unsigned long nbuf;
+       unsigned long nrec;
+       char name[MAX_DB_NAME];
+};
+
+struct backup_state {
+       TALLOC_CTX *mem_ctx;
+       struct ctdb_rec_buffer *recbuf;
+       uint32_t db_id;
+       int fd;
+       unsigned int nbuf, nrec;
+};
+
+static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
+                         TDB_DATA key, TDB_DATA data, void *private_data)
+{
+       struct backup_state *state = (struct backup_state *)private_data;
+       size_t len;
+       int ret;
+
+       if (state->recbuf == NULL) {
+               state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
+                                                    state->db_id);
+               if (state->recbuf == NULL) {
+                       return ENOMEM;
+               }
        }
 
-       data.dsize = sizeof(struct ctdb_ltdb_header) + value.dsize;
-       data.dptr = talloc_size(tmp_ctx, data.dsize);
-       if (data.dptr == NULL) {
-               printf("Failed to allocate header+value\n");
-               return -1;
+       ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
+                                 header, key, data);
+       if (ret != 0) {
+               return ret;
        }
 
-       *(struct ctdb_ltdb_header *)data.dptr = header;
-       memcpy(data.dptr + sizeof(struct ctdb_ltdb_header), value.dptr, value.dsize);
+       len = ctdb_rec_buffer_len(state->recbuf);
+       if (len < MAX_REC_BUFFER_SIZE) {
+               return 0;
+       }
 
-       if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
-               printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
-               tdb_close(tdb);
-               return -1;
+       ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to write records to backup file\n");
+               return ret;
        }
 
-       tdb_close(tdb);
+       state->nbuf += 1;
+       state->nrec += state->recbuf->count;
+       TALLOC_FREE(state->recbuf);
 
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
-  write a record to a persistent database
- */
-static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
        const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_transaction_handle *h;
-       struct stat st;
-       TDB_DATA key, data;
+       struct ctdb_db_context *db;
+       uint32_t db_id;
+       uint8_t db_flags;
+       struct backup_state state;
+       struct db_header db_hdr;
        int fd, ret;
 
-       if (argc < 3) {
-               talloc_free(tmp_ctx);
-               usage();
+       if (argc != 2) {
+               usage("backupdb");
+       }
+
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
+               return 1;
+       }
+
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
 
-       fd = open(argv[2], O_RDONLY);
+       fd = open(argv[1], O_RDWR|O_CREAT, 0600);
        if (fd == -1) {
-               DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s  %s\n", argv[2], strerror(errno)));
-               talloc_free(tmp_ctx);
-               return -1;
+               ret = errno;
+               fprintf(stderr, "Failed to open file %s for writing\n",
+                       argv[1]);
+               return ret;
        }
-       
-       ret = fstat(fd, &st);
+
+       /* Write empty header first */
+       ZERO_STRUCT(db_hdr);
+       ret = write(fd, &db_hdr, sizeof(struct db_header));
        if (ret == -1) {
-               DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
+               ret = errno;
                close(fd);
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
+               return ret;
        }
 
-       if (!S_ISREG(st.st_mode)) {
-               DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
+       state.mem_ctx = mem_ctx;
+       state.recbuf = NULL;
+       state.fd = fd;
+       state.nbuf = 0;
+       state.nrec = 0;
+
+       ret = ctdb_db_traverse_local(db, true, false, backup_handler, &state);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to collect records from DB %s\n",
+                       db_name);
                close(fd);
-               talloc_free(tmp_ctx);
-               return -1;
+               return ret;
        }
 
-       data.dsize = st.st_size;
-       if (data.dsize == 0) {
-               data.dptr  = NULL;
-       } else {
-               data.dptr = talloc_size(tmp_ctx, data.dsize);
-               if (data.dptr == NULL) {
-                       DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
-                       close(fd);
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
-               ret = sys_read(fd, data.dptr, data.dsize);
-               if (ret != data.dsize) {
-                       DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
+       if (state.recbuf != NULL) {
+               ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
+               if (ret != 0) {
+                       fprintf(stderr,
+                               "Failed to write records to backup file\n");
                        close(fd);
-                       talloc_free(tmp_ctx);
-                       return -1;
+                       return ret;
                }
-       }
-       close(fd);
 
+               state.nbuf += 1;
+               state.nrec += state.recbuf->count;
+               TALLOC_FREE(state.recbuf);
+       }
 
-       db_name = argv[0];
+       db_hdr.version = DB_VERSION;
+       db_hdr.timestamp = time(NULL);
+       db_hdr.flags = db_flags;
+       db_hdr.nbuf = state.nbuf;
+       db_hdr.nrec = state.nrec;
+       strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
+       lseek(fd, 0, SEEK_SET);
+       ret = write(fd, &db_hdr, sizeof(struct db_header));
+       if (ret == -1) {
+               ret = errno;
+               close(fd);
+               fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
+               return ret;
        }
 
-       h = ctdb_transaction_start(ctdb_db, tmp_ctx);
-       if (h == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
+       close(fd);
+       printf("Database backed up to %s\n", argv[1]);
+       return 0;
+}
+
+static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
+{
+       const char *db_name = NULL;
+       struct ctdb_db_context *db;
+       struct db_header db_hdr;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_req_control request;
+       struct ctdb_reply_control **reply;
+       struct ctdb_transdb wipedb;
+       struct ctdb_pulldb_ext pulldb;
+       struct ctdb_rec_buffer *recbuf;
+       uint32_t generation;
+       uint32_t *pnn_list;
+       char timebuf[128];
+       int fd, i;
+       int count, ret;
+
+       if (argc < 1 || argc > 2) {
+               usage("restoredb");
        }
 
-       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
-       if (key.dptr == NULL) {
-               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-               return -1;
+       fd = open(argv[0], O_RDONLY, 0600);
+       if (fd == -1) {
+               ret = errno;
+               fprintf(stderr, "Failed to open file %s for reading\n",
+                       argv[0]);
+               return ret;
        }
 
-       ret = ctdb_transaction_store(h, key, data);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to store record\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc == 2) {
+               db_name = argv[1];
        }
 
-       ret = ctdb_transaction_commit(h);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = read(fd, &db_hdr, sizeof(struct db_header));
+       if (ret == -1) {
+               ret = errno;
+               close(fd);
+               fprintf(stderr, "Failed to read db header from file %s\n",
+                       argv[0]);
+               return ret;
        }
+       db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
 
+       if (db_hdr.version != DB_VERSION) {
+               fprintf(stderr,
+                       "Wrong version of backup file, expected %u, got %lu\n",
+                       DB_VERSION, db_hdr.version);
+               close(fd);
+               return EINVAL;
+       }
 
-       talloc_free(tmp_ctx);
-       return 0;
-}
+       if (db_name == NULL) {
+               db_name = db_hdr.name;
+       }
 
-/*
- * delete a record from a persistent database
- */
-static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_transaction_handle *h;
-       TDB_DATA key;
-       int ret;
-       bool persistent;
-       uint8_t flags;
+       strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
+                localtime(&db_hdr.timestamp));
+       printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
 
-       if (argc < 2) {
-               talloc_free(tmp_ctx);
-               usage();
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_hdr.flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               close(fd);
+               return ret;
        }
 
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               talloc_free(tmp_ctx);
-               return -1;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               fprintf(stderr, "Failed to get nodemap\n");
+               close(fd);
+               return ENOMEM;
        }
 
-       persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
-       if (!persistent) {
-               DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = get_generation(mem_ctx, ctdb, &generation);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get current generation\n");
+               close(fd);
+               return ret;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
+       count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
+                                    &pnn_list);
+       if (count <= 0) {
+               close(fd);
+               return ENOMEM;
        }
 
-       h = ctdb_transaction_start(ctdb_db, tmp_ctx);
-       if (h == NULL) {
-               DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       wipedb.db_id = ctdb_db_id(db);
+       wipedb.tid = generation;
 
-       key = strtodata(tmp_ctx, argv[1], strlen(argv[1]));
-       if (key.dptr == NULL) {
-               printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
-               return -1;
+       ctdb_req_control_db_freeze(&request, wipedb.db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
+                                       ctdb->client, pnn_list, count,
+                                       TIMEOUT(), &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
        }
 
-       ret = ctdb_transaction_store(h, key, tdb_null);
+
+       ctdb_req_control_db_transaction_start(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               goto failed;
        }
 
-       ret = ctdb_transaction_commit(h);
+       ctdb_req_control_wipe_database(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               goto failed;
        }
 
-       talloc_free(tmp_ctx);
-       return 0;
-}
-
-static const char *ptrans_parse_string(TALLOC_CTX *mem_ctx, const char *s,
-                                      TDB_DATA *data)
-{
-       const char *t;
-       size_t n;
-       const char *ret; /* Next byte after successfully parsed value */
+       pulldb.db_id = ctdb_db_id(db);
+       pulldb.lmaster = 0;
+       pulldb.srvid = SRVID_CTDB_PUSHDB;
 
-       /* Error, unless someone says otherwise */
-       ret = NULL;
-       /* Indicates no value to parse */
-       *data = tdb_null;
+       ctdb_req_control_db_push_start(&request, &pulldb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
+       }
 
-       /* Skip whitespace */
-       n = strspn(s, " \t");
-       t = s + n;
+       for (i=0; i<db_hdr.nbuf; i++) {
+               struct ctdb_req_message message;
+               TDB_DATA data;
 
-       if (t[0] == '"') {
-               /* Quoted ASCII string - no wide characters! */
-               t++;
-               n = strcspn(t, "\"");
-               if (t[n] == '"') {
-                       if (n > 0) {
-                               *data = strtodata(mem_ctx, t, n);
-                               CTDB_NOMEM_ABORT(data->dptr);
-                       }
-                       ret = t + n + 1;
-               } else {
-                       DEBUG(DEBUG_WARNING,("Unmatched \" in input %s\n", s));
+               ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
+               if (ret != 0) {
+                       goto failed;
                }
-       } else {
-               DEBUG(DEBUG_WARNING,("Unsupported input format in %s\n", s));
-       }
 
-       return ret;
-}
+               data.dsize = ctdb_rec_buffer_len(recbuf);
+               data.dptr = talloc_size(mem_ctx, data.dsize);
+               if (data.dptr == NULL) {
+                       goto failed;
+               }
 
-static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
-                                TDB_DATA *key, TDB_DATA *value)
-{
-       char line [1024]; /* FIXME: make this more flexible? */
-       const char *t;
-       char *ptr;
+               ctdb_rec_buffer_push(recbuf, data.dptr);
 
-       ptr = fgets(line, sizeof(line), file);
+               message.srvid = pulldb.srvid;
+               message.data.data = data;
 
-       if (ptr == NULL) {
-               return false;
-       }
+               ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
+                                               ctdb->client,
+                                               pnn_list, count,
+                                               &message, NULL);
+               if (ret != 0) {
+                       goto failed;
+               }
 
-       /* Get key */
-       t = ptrans_parse_string(mem_ctx, line, key);
-       if (t == NULL || key->dptr == NULL) {
-               /* Line Ignored but not EOF */
-               return true;
+               talloc_free(recbuf);
+               talloc_free(data.dptr);
        }
 
-       /* Get value */
-       t = ptrans_parse_string(mem_ctx, t, value);
-       if (t == NULL) {
-               /* Line Ignored but not EOF */
-               talloc_free(key->dptr);
-               *key = tdb_null;
-               return true;
+       ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, &reply);
+       if (ret != 0) {
+               goto failed;
        }
 
-       return true;
-}
-
-/*
- * Update a persistent database as per file/stdin
- */
-static int control_ptrans(struct ctdb_context *ctdb,
-                         int argc, const char **argv)
-{
-       const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct ctdb_transaction_handle *h;
-       TDB_DATA key, value;
-       FILE *file;
-       int ret;
-
-       if (argc < 1) {
-               talloc_free(tmp_ctx);
-               usage();
-       }
+       for (i=0; i<count; i++) {
+               uint32_t num_records;
 
-       file = stdin;
-       if (argc == 2) {
-               file = fopen(argv[1], "r");
-               if (file == NULL) {
-                       DEBUG(DEBUG_ERR,("Unable to open file for reading '%s'\n", argv[1]));
-                       talloc_free(tmp_ctx);
-                       return -1;
+               ret = ctdb_reply_control_db_push_confirm(reply[i],
+                                                        &num_records);
+               if (ret != 0) {
+                       fprintf(stderr, "Invalid response from node %u\n",
+                               pnn_list[i]);
+                       goto failed;
                }
-       }
 
-       db_name = argv[0];
-
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               goto error;
+               if (num_records != db_hdr.nrec) {
+                       fprintf(stderr, "Node %u received %u of %lu records\n",
+                               pnn_list[i], num_records, db_hdr.nrec);
+                       goto failed;
+               }
        }
 
-       h = ctdb_transaction_start(ctdb_db, tmp_ctx);
-       if (h == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
-               goto error;
+       ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
        }
 
-       while (ptrans_get_key_value(tmp_ctx, file, &key, &value)) {
-               if (key.dsize != 0) {
-                       ret = ctdb_transaction_store(h, key, value);
-                       /* Minimise memory use */
-                       talloc_free(key.dptr);
-                       if (value.dptr != NULL) {
-                               talloc_free(value.dptr);
-                       }
-                       if (ret != 0) {
-                               DEBUG(DEBUG_ERR,("Failed to store record\n"));
-                               ctdb_transaction_cancel(h);
-                               goto error;
-                       }
-               }
+       ctdb_req_control_db_transaction_commit(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
        }
 
-       ret = ctdb_transaction_commit(h);
+       ctdb_req_control_db_thaw(&request, wipedb.db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
+                                       ctdb->client, pnn_list, count,
+                                       TIMEOUT(), &request, NULL, NULL);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
-               goto error;
+               goto failed;
        }
 
-       if (file != stdin) {
-               fclose(file);
-       }
-       talloc_free(tmp_ctx);
+       printf("Database %s restored\n", db_name);
+       close(fd);
        return 0;
 
-error:
-       if (file != stdin) {
-               fclose(file);
+
+failed:
+       close(fd);
+       ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                             ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
+       return ret;
+}
+
+struct dumpdbbackup_state {
+       ctdb_rec_parser_func_t parser;
+       struct dump_record_state sub_state;
+};
+
+static int dumpdbbackup_handler(uint32_t reqid,
+                               struct ctdb_ltdb_header *header,
+                               TDB_DATA key, TDB_DATA data,
+                               void *private_data)
+{
+       struct dumpdbbackup_state *state =
+               (struct dumpdbbackup_state *)private_data;
+       struct ctdb_ltdb_header hdr;
+       int ret;
+
+       ret = ctdb_ltdb_header_extract(&data, &hdr);
+       if (ret != 0) {
+               return ret;
        }
 
-       talloc_free(tmp_ctx);
-       return -1;
+       return state->parser(reqid, &hdr, key, data, &state->sub_state);
 }
 
-/*
-  check if a service is bound to a port or not
- */
-static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
-       int s, ret;
-       int v;
-       int port;
-        struct sockaddr_in sin;
+       struct db_header db_hdr;
+       char timebuf[128];
+       struct dumpdbbackup_state state;
+       int fd, ret, i;
 
        if (argc != 1) {
-               printf("Use: ctdb chktcport <port>\n");
-               return EINVAL;
+               usage("dumpbackup");
        }
 
-       port = atoi(argv[0]);
+       fd = open(argv[0], O_RDONLY, 0600);
+       if (fd == -1) {
+               ret = errno;
+               fprintf(stderr, "Failed to open file %s for reading\n",
+                       argv[0]);
+               return ret;
+       }
 
-       s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
-       if (s == -1) {
-               printf("Failed to open local socket\n");
-               return errno;
+       ret = read(fd, &db_hdr, sizeof(struct db_header));
+       if (ret == -1) {
+               ret = errno;
+               close(fd);
+               fprintf(stderr, "Failed to read db header from file %s\n",
+                       argv[0]);
+               return ret;
        }
+       db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
 
-       v = fcntl(s, F_GETFL, 0);
-       if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK) != 0) {
-               printf("Unable to set socket non-blocking: %s\n", strerror(errno));
+       if (db_hdr.version != DB_VERSION) {
+               fprintf(stderr,
+                       "Wrong version of backup file, expected %u, got %lu\n",
+                       DB_VERSION, db_hdr.version);
+               close(fd);
+               return EINVAL;
        }
 
-       bzero(&sin, sizeof(sin));
-       sin.sin_family = AF_INET;
-       sin.sin_port   = htons(port);
-       ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
-       close(s);
-       if (ret == -1) {
-               printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
-               return errno;
+       strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
+                localtime(&db_hdr.timestamp));
+       printf("Dumping database %s from backup @ %s\n",
+              db_hdr.name, timebuf);
+
+       state.parser = dump_record;
+       state.sub_state.count = 0;
+
+       for (i=0; i<db_hdr.nbuf; i++) {
+               struct ctdb_rec_buffer *recbuf;
+
+               ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
+               if (ret != 0) {
+                       fprintf(stderr, "Failed to read records\n");
+                       close(fd);
+                       return ret;
+               }
+
+               ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
+                                              &state);
+               if (ret != 0) {
+                       fprintf(stderr, "Failed to dump records\n");
+                       close(fd);
+                       return ret;
+               }
        }
 
+       close(fd);
+       printf("Dumped %u record(s)\n", state.sub_state.count);
        return 0;
 }
 
-
-/* Reload public IPs on a specified nodes */
-static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       uint32_t *nodes;
-       uint32_t pnn_mode;
-       uint32_t timeout;
-       int ret;
-
-       assert_single_node_only();
+       const char *db_name;
+       struct ctdb_db_context *db;
+       uint32_t db_id;
+       uint8_t db_flags;
+       struct ctdb_node_map *nodemap;
+       struct ctdb_req_control request;
+       struct ctdb_transdb wipedb;
+       uint32_t generation;
+       uint32_t *pnn_list;
+       int count, ret;
 
-       if (argc > 1) {
-               usage();
+       if (argc != 1) {
+               usage("wipedb");
        }
 
-       /* Determine the nodes where IPs need to be reloaded */
-       if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
-                             options.pnn, true, &nodes, &pnn_mode)) {
-               ret = -1;
-               goto done;
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
+               return 1;
        }
 
-again:
-       /* Disable takeover runs on all connected nodes.  A reply
-        * indicating success is needed from each node so all nodes
-        * will need to be active.  This will retry until maxruntime
-        * is exceeded, hence no error handling.
-        * 
-        * A check could be added to not allow reloading of IPs when
-        * there are disconnected nodes.  However, this should
-        * probably be left up to the administrator.
-        */
-       timeout = LONGTIMEOUT;
-       srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
-                       "Disable takeover runs", true);
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
+       }
 
-       /* Now tell all the desired nodes to reload their public IPs.
-        * Keep trying this until it succeeds.  This assumes all
-        * failures are transient, which might not be true...
-        */
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
-                                     nodes, 0, LONGTIMELIMIT(),
-                                     false, tdb_null,
-                                     NULL, NULL, NULL) != 0) {
-               DEBUG(DEBUG_ERR,
-                     ("Unable to reload IPs on some nodes, try again.\n"));
-               goto again;
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               fprintf(stderr, "Failed to get nodemap\n");
+               return ENOMEM;
        }
 
-       /* It isn't strictly necessary to wait until takeover runs are
-        * re-enabled but doing so can't hurt.
-        */
-       timeout = 0;
-       srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, &timeout,
-                       "Enable takeover runs", true);
+       ret = get_generation(mem_ctx, ctdb, &generation);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to get current generation\n");
+               return ret;
+       }
 
-       ipreallocate(ctdb);
+       count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
+                                    &pnn_list);
+       if (count <= 0) {
+               return ENOMEM;
+       }
 
-       ret = 0;
-done:
-       talloc_free(tmp_ctx);
-       return ret;
-}
+       ctdb_req_control_db_freeze(&request, db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
+                                       ctdb->client, pnn_list, count,
+                                       TIMEOUT(), &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
+       }
 
-/*
-  display a list of the databases on a remote ctdb
- */
-static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int i, ret;
-       struct ctdb_dbid_map_old *dbmap=NULL;
+       wipedb.db_id = db_id;
+       wipedb.tid = generation;
 
-       ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
+       ctdb_req_control_db_transaction_start(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
-               return ret;
+               goto failed;
        }
 
-       if(options.machinereadable){
-               printm(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
-               for(i=0;i<dbmap->num;i++){
-                       const char *path = NULL;
-                       const char *name = NULL;
-                       const char *health = NULL;
-                       bool persistent;
-                       bool readonly;
-                       bool sticky;
+       ctdb_req_control_wipe_database(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
+       }
 
-                       ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
-                                           dbmap->dbs[i].db_id, ctdb, &path);
-                       ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
-                                           dbmap->dbs[i].db_id, ctdb, &name);
-                       ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
-                                             dbmap->dbs[i].db_id, ctdb, &health);
-                       persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
-                       readonly   = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
-                       sticky     = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
-                       printm(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
-                              dbmap->dbs[i].db_id, name, path,
-                              !!(persistent), !!(sticky),
-                              !!(health), !!(readonly));
-               }
-               return 0;
+       ctdb_req_control_db_set_healthy(&request, db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
        }
 
-       printf("Number of databases:%d\n", dbmap->num);
-       for(i=0;i<dbmap->num;i++){
-               const char *path = NULL;
-               const char *name = NULL;
-               const char *health = NULL;
-               bool persistent;
-               bool readonly;
-               bool sticky;
+       ctdb_req_control_db_transaction_commit(&request, &wipedb);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list, count, TIMEOUT(),
+                                       &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
+       }
 
-               ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &path);
-               ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &name);
-               ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].db_id, ctdb, &health);
-               persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
-               readonly   = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
-               sticky     = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
-               printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
-                      dbmap->dbs[i].db_id, name, path,
-                      persistent?" PERSISTENT":"",
-                      sticky?" STICKY":"",
-                      readonly?" READONLY":"",
-                      health?" UNHEALTHY":"");
+       ctdb_req_control_db_thaw(&request, db_id);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
+                                       ctdb->client, pnn_list, count,
+                                       TIMEOUT(), &request, NULL, NULL);
+       if (ret != 0) {
+               goto failed;
        }
 
+       printf("Database %s wiped\n", db_name);
        return 0;
+
+
+failed:
+       ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
+                             ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
+       return ret;
 }
 
-/*
-  display the status of a database on a remote ctdb
- */
-static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
 {
-       const char *db_name;
-       uint32_t db_id;
-       uint8_t flags;
-       const char *path = NULL;
-       const char *health = NULL;
-
-       if (argc < 1) {
-               usage();
-       }
+       uint32_t recmaster;
+       int ret;
 
-       if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
-               return -1;
+       ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), &recmaster);
+       if (ret != 0) {
+               return ret;
        }
 
-       ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
-       ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
-       printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
-              db_id, db_name, path,
-              (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
-              (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
-              (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
-              (health ? health : "OK"));
-
+       printf("%u\n", recmaster);
        return 0;
 }
 
-/*
-  check if the local node is recmaster or not
-  it will return 1 if this node is the recmaster and 0 if it is not
-  or if the local ctdb daemon could not be contacted
- */
-static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
 {
-       uint32_t mypnn, recmaster;
-       int ret;
-
-       assert_single_node_only();
+       char *t, *event_helper = NULL;
+       char *eventd_socket = NULL;
+       const char **new_argv;
+       int i;
 
-       mypnn = getpnn(ctdb);
+       t = getenv("CTDB_EVENT_HELPER");
+       if (t != NULL) {
+               event_helper = talloc_strdup(mem_ctx, t);
+       } else {
+               event_helper = talloc_asprintf(mem_ctx, "%s/ctdb_event",
+                                              CTDB_HELPER_BINDIR);
+       }
 
-       ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
-       if (ret != 0) {
-               printf("Failed to get the recmaster\n");
+       if (event_helper == NULL) {
+               fprintf(stderr, "Unable to set event daemon helper\n");
                return 1;
        }
 
-       if (recmaster != mypnn) {
-               printf("this node is not the recmaster\n");
+       t = getenv("CTDB_SOCKET");
+       if (t != NULL) {
+               eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
+                                               dirname(t));
+       } else {
+               eventd_socket = talloc_asprintf(mem_ctx, "%s/eventd.sock",
+                                               CTDB_RUNDIR);
+       }
+
+       if (eventd_socket == NULL) {
+               fprintf(stderr, "Unable to set event daemon socket\n");
                return 1;
        }
 
-       printf("this node is the recmaster\n");
-       return 0;
-}
+       new_argv = talloc_array(mem_ctx, const char *, argc + 1);
+       if (new_argv == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
 
-/*
-  ping a node
- */
-static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int ret;
-       struct timeval tv = timeval_current();
-       ret = ctdb_ctrl_ping(ctdb, options.pnn);
-       if (ret == -1) {
-               printf("Unable to get ping response from node %u\n", options.pnn);
-               return -1;
-       } else {
-               printf("response from %u time=%.6f sec  (%d clients)\n", 
-                      options.pnn, timeval_elapsed(&tv), ret);
+       new_argv[0] = eventd_socket;
+       for (i=0; i<argc; i++) {
+               new_argv[i+1] = argv[i];
        }
-       return 0;
-}
 
+       return run_helper(mem_ctx, "event daemon helper", event_helper,
+                         argc+1, new_argv);
+}
 
-/*
-  get a node's runstate
- */
-static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
-       int ret;
-       enum ctdb_runstate runstate;
-
-       ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
-       if (ret == -1) {
-               printf("Unable to get runstate response from node %u\n",
-                      options.pnn);
-               return -1;
-       } else {
-               bool found = true;
-               enum ctdb_runstate t;
-               int i;
-               for (i=0; i<argc; i++) {
-                       found = false;
-                       t = runstate_from_string(argv[i]);
-                       if (t == CTDB_RUNSTATE_UNKNOWN) {
-                               printf("Invalid run state (%s)\n", argv[i]);
-                               return -1;
-                       }
-
-                       if (t == runstate) {
-                               found = true;
-                               break;
-                       }
-               }
+       const char *new_argv[3];
 
-               if (!found) {
-                       printf("CTDB not in required run state (got %s)\n", 
-                              runstate_to_string((enum ctdb_runstate)runstate));
-                       return -1;
-               }
+       if (argc > 1) {
+               usage("scriptstatus");
        }
 
-       printf("%s\n", runstate_to_string(runstate));
+       new_argv[0] = "status";
+       new_argv[1] = (argc == 0) ? "monitor" : argv[0];
+       new_argv[2] = NULL;
+
+       (void) control_event(mem_ctx, ctdb, 2, new_argv);
        return 0;
 }
 
-
-/*
-  get a tunable
- */
-static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                        int argc, const char **argv)
 {
-       const char *name;
-       uint32_t value;
-       int ret;
+       char *t, *natgw_helper = NULL;
 
-       if (argc < 1) {
-               usage();
+       if (argc != 1) {
+               usage("natgw");
        }
 
-       name = argv[0];
-       ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
-               return -1;
+       t = getenv("CTDB_NATGW_HELPER");
+       if (t != NULL) {
+               natgw_helper = talloc_strdup(mem_ctx, t);
+       } else {
+               natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
+                                              CTDB_HELPER_BINDIR);
        }
 
-       printf("%-23s = %u\n", name, value);
-       return 0;
+       if (natgw_helper == NULL) {
+               fprintf(stderr, "Unable to set NAT gateway helper\n");
+               return 1;
+       }
+
+       return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
+                         argc, argv);
 }
 
-/*
-  set a tunable
- */
-static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_natgwlist(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
 {
-       const char *name;
-       uint32_t value;
-       int ret;
+       char *t, *natgw_helper = NULL;
+       const char *cmd_argv[] = { "natgwlist", NULL };
 
-       if (argc < 2) {
-               usage();
+       if (argc != 0) {
+               usage("natgwlist");
        }
 
-       name = argv[0];
-       value = strtoul(argv[1], NULL, 0);
-
-       ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
-               return -1;
+       t = getenv("CTDB_NATGW_HELPER");
+       if (t != NULL) {
+               natgw_helper = talloc_strdup(mem_ctx, t);
+       } else {
+               natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
+                                              CTDB_HELPER_BINDIR);
        }
-       if (ret == 1) {
-               DEBUG(DEBUG_WARNING,
-                     ("Setting obsolete tunable variable '%s'\n",
-                      name));
+
+       if (natgw_helper == NULL) {
+               fprintf(stderr, "Unable to set NAT gateway helper\n");
+               return 1;
        }
-       return 0;
+
+       return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
+                         1, cmd_argv);
 }
 
 /*
-  list all tunables
+ * Find the PNN of the current node
+ * discover the pnn by loading the nodes file and try to bind
+ * to all addresses one at a time until the ip address is found.
  */
-static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
+static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
 {
-       uint32_t count;
-       const char **list;
-       int ret, i;
+       struct ctdb_node_map *nodemap;
+       int i;
 
-       ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
-               return -1;
+       nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
+       if (nodemap == NULL) {
+               return false;
        }
 
-       for (i=0;i<count;i++) {
-               control_getvar(ctdb, 1, &list[i]);
+       for (i=0; i<nodemap->num; i++) {
+               if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
+                       continue;
+               }
+               if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
+                       if (pnn != NULL) {
+                               *pnn = nodemap->node[i].pnn;
+                       }
+                       talloc_free(nodemap);
+                       return true;
+               }
        }
 
-       talloc_free(list);
-       
-       return 0;
+       fprintf(stderr, "Failed to detect PNN of the current node.\n");
+       talloc_free(nodemap);
+       return false;
 }
 
-/*
-  display debug level on a node
- */
-static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
 {
+       const char *reclock;
        int ret;
-       int32_t level;
 
-       ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
+       if (argc != 0) {
+               usage("getreclock");
+       }
+
+       ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
+                                        ctdb->cmd_pnn, TIMEOUT(), &reclock);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
                return ret;
-       } else {
-               enum debug_level log_level = debug_level_from_int(level);
-               const char *desc = debug_level_to_string(log_level);
-               if (desc == NULL) {
-                       /* This should never happen */
-                       desc = "Unknown";
-               }
-               if (options.machinereadable){
-                       printm(":Name:Level:\n");
-                       printm(":%s:%d:\n", desc, level);
-               } else {
-                       printf("Node %u is at debug level %s (%d)\n",
-                              options.pnn, desc, level);
-               }
        }
+
+       if (reclock != NULL) {
+               printf("%s\n", reclock);
+       }
+
        return 0;
 }
 
-/*
-  display reclock file of a node
- */
-static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
+                                 struct ctdb_context *ctdb,
+                                 int argc, const char **argv)
 {
+       uint32_t lmasterrole = 0;
        int ret;
-       const char *reclock;
 
-       ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
+       if (argc != 1) {
+               usage("setlmasterrole");
+       }
+
+       if (strcmp(argv[0], "on") == 0) {
+               lmasterrole = 1;
+       } else if (strcmp(argv[0], "off") == 0) {
+               lmasterrole = 0;
+       } else {
+               usage("setlmasterrole");
+       }
+
+       ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
+                                       ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
                return ret;
-       } else {
-               if (options.machinereadable){
-                       if (reclock != NULL) {
-                               printm("%s", reclock);
-                       }
-               } else {
-                       if (reclock == NULL) {
-                               printf("No reclock file used.\n");
-                       } else {
-                               printf("Reclock file:%s\n", reclock);
-                       }
-               }
        }
+
        return 0;
 }
 
-/*
-  set the reclock file of a node
- */
-static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
+                                   struct ctdb_context *ctdb,
+                                   int argc, const char **argv)
 {
+       uint32_t recmasterrole = 0;
        int ret;
-       const char *reclock = NULL;
 
-       if (argc == 0) {
-               reclock = NULL;
-       } else if (argc == 1) {
-               reclock = argv[0];
+       if (argc != 1) {
+               usage("setrecmasterrole");
+       }
+
+       if (strcmp(argv[0], "on") == 0) {
+               recmasterrole = 1;
+       } else if (strcmp(argv[0], "off") == 0) {
+               recmasterrole = 0;
        } else {
-               usage();
+               usage("setrecmasterrole");
        }
 
-       ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
+       ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
+                                         ctdb->cmd_pnn, TIMEOUT(),
+                                         recmasterrole);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
                return ret;
        }
+
        return 0;
 }
 
-/*
-  set the lmaster role on/off
- */
-static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
+                                struct ctdb_context *ctdb,
+                                int argc, const char **argv)
 {
+       uint32_t db_id;
+       uint8_t db_flags;
        int ret;
-       uint32_t lmasterrole = 0;
 
-       if (argc == 0) {
-               usage();
+       if (argc != 1) {
+               usage("setdbreadonly");
        }
 
-       if (!strcmp(argv[0], "on")) {
-               lmasterrole = 1;
-       } else if (!strcmp(argv[0], "off")) {
-               lmasterrole = 0;
-       } else {
-               usage();
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
+               return 1;
+       }
+
+       if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+               fprintf(stderr, "Cannot set READONLY on persistent DB\n");
+               return 1;
        }
 
-       ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
+       ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
+                                       ctdb->cmd_pnn, TIMEOUT(), db_id);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
                return ret;
        }
 
        return 0;
 }
 
-/*
-  set the recmaster role on/off
- */
-static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                              int argc, const char **argv)
 {
+       uint32_t db_id;
+       uint8_t db_flags;
        int ret;
-       uint32_t recmasterrole = 0;
 
-       if (argc == 0) {
-               usage();
+       if (argc != 1) {
+               usage("setdbsticky");
        }
 
-       if (!strcmp(argv[0], "on")) {
-               recmasterrole = 1;
-       } else if (!strcmp(argv[0], "off")) {
-               recmasterrole = 0;
-       } else {
-               usage();
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
+               return 1;
+       }
+
+       if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+               fprintf(stderr, "Cannot set STICKY on persistent DB\n");
+               return 1;
        }
 
-       ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
+       ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), db_id);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
                return ret;
        }
 
        return 0;
 }
 
-/*
-  set debug level on a node or all nodes
- */
-static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_transaction_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key, data;
        int ret;
-       int32_t level;
-       enum debug_level log_level;
 
-       if (argc == 0) {
-               printf("You must specify the debug level. Valid levels are:\n");
-               printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
-               return 0;
+       if (argc < 2 || argc > 3) {
+               usage("pfetch");
        }
 
-       if (!debug_level_parse(argv[0], &log_level)) {
-               printf("Invalid debug level, must be one of\n");
-               printf("\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
-               return -1;
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
 
-       level = debug_level_to_int(log_level);
+       if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
+               fprintf(stderr, "DB %s is not a persistent database\n",
+                       db_name);
+               return 1;
+       }
 
-       ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
-       return 0;
-}
 
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               return ret;
+       }
 
-/*
-  thaw a node
- */
-static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int ret;
-       uint32_t priority;
-       
-       if (argc == 1) {
-               priority = strtol(argv[0], NULL, 0);
-       } else {
-               priority = 0;
+       ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
+                                    TIMEOUT(), db, true, &h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to start transaction on db %s\n",
+                       db_name);
+               return ret;
        }
-       DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
 
-       ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
+       ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
-       }               
+               fprintf(stderr, "Failed to read record for key %s\n",
+                       argv[1]);
+               ctdb_transaction_cancel(h);
+               return ret;
+       }
+
+       printf("%.*s\n", (int)data.dsize, data.dptr);
+
+       ctdb_transaction_cancel(h);
        return 0;
 }
 
-
-/*
-  attach to a database
- */
-static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
        const char *db_name;
-       struct ctdb_db_context *ctdb_db;
-       bool persistent = false;
+       struct ctdb_db_context *db;
+       struct ctdb_transaction_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key, data;
+       int ret;
 
-       if (argc < 1) {
-               usage();
-       }
-       db_name = argv[0];
-       if (argc > 2) {
-               usage();
+       if (argc != 3) {
+               usage("pstore");
        }
-       if (argc == 2) {
-               if (strcmp(argv[1], "persistent") != 0) {
-                       usage();
-               }
-               persistent = true;
+
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
-               return -1;
+       if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
+               fprintf(stderr, "DB %s is not a persistent database\n",
+                       db_name);
+               return 1;
        }
 
-       return 0;
-}
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
+       }
 
-/*
- * detach from a database
- */
-static int control_detach(struct ctdb_context *ctdb, int argc,
-                         const char **argv)
-{
-       uint32_t db_id;
-       uint8_t flags;
-       int ret, i, status = 0;
-       struct ctdb_node_map_old *nodemap = NULL;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       uint32_t recmode;
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               return ret;
+       }
 
-       if (argc < 1) {
-               usage();
+       ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse value %s\n", argv[2]);
+               return ret;
        }
 
-       assert_single_node_only();
+       ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
+                                    TIMEOUT(), db, false, &h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to start transaction on db %s\n",
+                       db_name);
+               return ret;
+       }
 
-       ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn,
-                                   &recmode);
+       ret = ctdb_transaction_store_record(h, key, data);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Database cannot be detached "
-                                 "when recovery is active\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr, "Failed to store record for key %s\n",
+                       argv[1]);
+               ctdb_transaction_cancel(h);
+               return ret;
        }
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
-                                  &nodemap);
+       ret = ctdb_transaction_commit(h);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
-                                 options.pnn));
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr, "Failed to commit transaction on db %s\n",
+                       db_name);
+               ctdb_transaction_cancel(h);
+               return ret;
        }
 
-       for (i=0; i<nodemap->num; i++) {
-               uint32_t value;
+       return 0;
+}
 
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
-                       continue;
-               }
+static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
+{
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_transaction_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key;
+       int ret;
 
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       continue;
-               }
+       if (argc != 2) {
+               usage("pdelete");
+       }
 
-               if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
-                       DEBUG(DEBUG_ERR, ("Database cannot be detached on "
-                                         "inactive (stopped or banned) node "
-                                         "%u\n", nodemap->nodes[i].pnn));
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
+       }
 
-               ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(),
-                                           nodemap->nodes[i].pnn,
-                                           "AllowClientDBAttach",
-                                           &value);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to get tunable "
-                                         "AllowClientDBAttach from node %u\n",
-                                          nodemap->nodes[i].pnn));
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
+       if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
+               fprintf(stderr, "DB %s is not a persistent database\n",
+                       db_name);
+               return 1;
+       }
 
-               if (value == 1) {
-                       DEBUG(DEBUG_ERR, ("Database access is still active on "
-                                         "node %u. Set AllowClientDBAttach=0 "
-                                         "on all nodes.\n",
-                                         nodemap->nodes[i].pnn));
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
 
-       talloc_free(tmp_ctx);
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               return ret;
+       }
 
-       for (i=0; i<argc; i++) {
-               if (!db_exists(ctdb, argv[i], &db_id, NULL, &flags)) {
-                       continue;
-               }
+       ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
+                                    TIMEOUT(), db, false, &h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to start transaction on db %s\n",
+                       db_name);
+               return ret;
+       }
 
-               if (flags & CTDB_DB_FLAGS_PERSISTENT) {
-                       DEBUG(DEBUG_ERR, ("Persistent database '%s' "
-                                         "cannot be detached\n", argv[i]));
-                       status = -1;
-                       continue;
-               }
+       ret = ctdb_transaction_delete_record(h, key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to delete record for key %s\n",
+                       argv[1]);
+               ctdb_transaction_cancel(h);
+               return ret;
+       }
 
-               ret = ctdb_detach(ctdb, db_id);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Database '%s' detach failed\n",
-                                         argv[i]));
-                       status = ret;
-               }
+       ret = ctdb_transaction_commit(h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to commit transaction on db %s\n",
+                       db_name);
+               ctdb_transaction_cancel(h);
+               return ret;
        }
 
-       return status;
+       return 0;
 }
 
-/*
-  set db priority
- */
-static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
+static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
 {
-       struct ctdb_db_priority db_prio;
+       const char *t;
+       size_t n;
        int ret;
 
-       if (argc < 2) {
-               usage();
-       }
+       *data = tdb_null;
 
-       db_prio.db_id    = strtoul(argv[0], NULL, 0);
-       db_prio.priority = strtoul(argv[1], NULL, 0);
+       /* Skip whitespace */
+       n = strspn(*ptr, " \t");
+       t = *ptr + n;
 
-       ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
-               return -1;
+       if (t[0] == '"') {
+               /* Quoted ASCII string - no wide characters! */
+               t++;
+               n = strcspn(t, "\"");
+               if (t[n] == '"') {
+                       if (n > 0) {
+                               ret = str_to_data(t, n, mem_ctx, data);
+                               if (ret != 0) {
+                                       return ret;
+                               }
+                       }
+                       *ptr = t + n + 1;
+               } else {
+                       fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
+                       return 1;
+               }
+       } else {
+               fprintf(stderr, "Unsupported input format in %s\n", *ptr);
+               return 1;
        }
 
        return 0;
 }
 
-/*
-  get db priority
- */
-static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
+#define MAX_LINE_SIZE  1024
+
+static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
+                                TDB_DATA *key, TDB_DATA *value)
 {
-       uint32_t db_id, priority;
+       char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
+       const char *ptr;
        int ret;
 
-       if (argc < 1) {
-               usage();
+       ptr = fgets(line, MAX_LINE_SIZE, file);
+       if (ptr == NULL) {
+               return false;
        }
 
-       if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
-               return -1;
+       /* Get key */
+       ret = ptrans_parse_string(mem_ctx, &ptr, key);
+       if (ret != 0 || ptr == NULL || key->dptr == NULL) {
+               /* Line Ignored but not EOF */
+               *key = tdb_null;
+               return true;
        }
 
-       ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
+       /* Get value */
+       ret = ptrans_parse_string(mem_ctx, &ptr, value);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
-               return -1;
+               /* Line Ignored but not EOF */
+               talloc_free(key->dptr);
+               *key = tdb_null;
+               return true;
        }
 
-       DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
-
-       return 0;
+       return true;
 }
 
-/*
-  set the sticky records capability for a database
- */
-static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       uint32_t db_id;
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_transaction_handle *h;
+       uint8_t db_flags;
+       FILE *file;
+       TDB_DATA key, value;
        int ret;
 
-       if (argc < 1) {
-               usage();
+       if (argc < 1 || argc > 2) {
+               usage("ptrans");
        }
 
-       if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
-               return -1;
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
 
-       ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (! (db_flags & CTDB_DB_FLAGS_PERSISTENT)) {
+               fprintf(stderr, "DB %s is not a persistent database\n",
+                       db_name);
+               return 1;
        }
 
-       talloc_free(tmp_ctx);
-       return 0;
-}
+       if (argc == 2) {
+               file = fopen(argv[1], "r");
+               if (file == NULL) {
+                       fprintf(stderr, "Failed to open file %s\n", argv[1]);
+                       return 1;
+               }
+       } else {
+               file = stdin;
+       }
 
-/*
-  set the readonly capability for a database
- */
-static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       uint32_t db_id;
-       int ret;
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               goto done;
+       }
 
-       if (argc < 1) {
-               usage();
+       ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
+                                    TIMEOUT(), db, false, &h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to start transaction on db %s\n",
+                       db_name);
+               goto done;
        }
 
-       if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
-               return -1;
+       while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
+               if (key.dsize != 0) {
+                       ret = ctdb_transaction_store_record(h, key, value);
+                       if (ret != 0) {
+                               fprintf(stderr, "Failed to store record\n");
+                               ctdb_transaction_cancel(h);
+                               goto done;
+                       }
+                       talloc_free(key.dptr);
+                       talloc_free(value.dptr);
+               }
        }
 
-       ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
+       ret = ctdb_transaction_commit(h);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr, "Failed to commit transaction on db %s\n",
+                       db_name);
+               ctdb_transaction_cancel(h);
        }
 
-       talloc_free(tmp_ctx);
-       return 0;
+done:
+       if (file != stdin) {
+               fclose(file);
+       }
+       return ret;
 }
 
-/*
-  get db seqnum
- */
-static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       uint32_t db_id;
-       uint64_t seqnum;
+       struct tdb_context *tdb;
+       TDB_DATA key, data;
+       struct ctdb_ltdb_header header;
        int ret;
 
-       if (argc < 1) {
-               usage();
+       if (argc < 2 || argc > 3) {
+               usage("tfetch");
        }
 
-       if (!db_exists(ctdb, argv[0], &db_id, NULL, NULL)) {
-               return -1;
+       tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
+       if (tdb == NULL) {
+               fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
+               return 1;
        }
 
-       ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
-               return -1;
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               tdb_close(tdb);
+               return ret;
        }
 
-       printf("Sequence number:%lld\n", (long long)seqnum);
-
-       return 0;
-}
-
-/*
-  run an eventscript on a node
- */
-static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       TDB_DATA data;
-       int ret;
-       int32_t res;
-       char *errmsg;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-
-       if (argc != 1) {
-               DEBUG(DEBUG_ERR,("Invalid arguments\n"));
-               return -1;
+       data = tdb_fetch(tdb, key);
+       if (data.dptr == NULL) {
+               fprintf(stderr, "No record for key %s\n", argv[1]);
+               tdb_close(tdb);
+               return 1;
        }
 
-       data.dptr = (unsigned char *)discard_const(argv[0]);
-       data.dsize = strlen((char *)data.dptr) + 1;
-
-       DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
-
-       ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
-                          0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
-       if (ret != 0 || res != 0) {
-               if (errmsg != NULL) {
-                       DEBUG(DEBUG_ERR,
-                             ("Failed to run eventscripts - %s\n", errmsg));
-               } else {
-                       DEBUG(DEBUG_ERR, ("Failed to run eventscripts\n"));
-               }
-               talloc_free(tmp_ctx);
-               return -1;
+       if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
+               fprintf(stderr, "Invalid record for key %s\n", argv[1]);
+               tdb_close(tdb);
+               return 1;
        }
-       talloc_free(tmp_ctx);
-       return 0;
-}
 
-#define DB_VERSION 1
-#define MAX_DB_NAME 64
-struct db_file_header {
-       unsigned long version;
-       time_t timestamp;
-       unsigned long persistent;
-       unsigned long size;
-       const char name[MAX_DB_NAME];
-};
+       tdb_close(tdb);
+
+       if (argc == 3) {
+               int fd;
+               ssize_t nwritten;
 
-struct backup_data {
-       struct ctdb_marshall_buffer *records;
-       uint32_t len;
-       uint32_t total;
-       bool traverse_error;
-};
+               fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
+               if (fd == -1) {
+                       fprintf(stderr, "Failed to open output file %s\n",
+                               argv[2]);
+                       goto fail;
+               }
 
-static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
-{
-       struct backup_data *bd = talloc_get_type(private, struct backup_data);
-       struct ctdb_rec_data_old *rec;
+               nwritten = sys_write(fd, data.dptr, data.dsize);
+               if (nwritten != data.dsize) {
+                       fprintf(stderr, "Failed to write record to file\n");
+                       close(fd);
+                       goto fail;
+               }
 
-       /* add the record */
-       rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
-       if (rec == NULL) {
-               bd->traverse_error = true;
-               DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
-               return -1;
+               close(fd);
        }
-       bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
-       if (bd->records == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
-               bd->traverse_error = true;
-               return -1;
+
+fail:
+       ret = ctdb_ltdb_header_extract(&data, &header);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse header from data\n");
+               return 1;
        }
-       bd->records->count++;
-       memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
-       bd->len += rec->length;
-       talloc_free(rec);
 
-       bd->total++;
+       dump_ltdb_header(&header);
+       dump_tdb_data("data", data);
+
        return 0;
 }
 
-/*
- * backup a database to a file 
- */
-static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                         int argc, const char **argv)
 {
-       const char *db_name;
+       struct tdb_context *tdb;
+       TDB_DATA key, data[2], value;
+       struct ctdb_ltdb_header header;
+       uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
        int ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       struct db_file_header dbhdr;
-       struct ctdb_db_context *ctdb_db;
-       struct backup_data *bd;
-       int fh = -1;
-       int status = -1;
-       const char *reason = NULL;
-       uint32_t db_id;
-       uint8_t flags;
-
-       assert_single_node_only();
 
-       if (argc != 2) {
-               DEBUG(DEBUG_ERR,("Invalid arguments\n"));
-               return -1;
+       if (argc < 3 || argc > 5) {
+               usage("tstore");
        }
 
-       if (!db_exists(ctdb, argv[0], &db_id, &db_name, &flags)) {
-               return -1;
+       tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
+       if (tdb == NULL) {
+               fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
+               return 1;
        }
 
-       ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
-                                   db_id, tmp_ctx, &reason);
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
-                                argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               tdb_close(tdb);
+               return ret;
        }
-       if (reason) {
-               uint32_t allow_unhealthy = 0;
 
-               ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
-                                     "AllowUnhealthyDBRead",
-                                     &allow_unhealthy);
-
-               if (allow_unhealthy != 1) {
-                       DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
-                                        argv[0], reason));
+       ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse value %s\n", argv[2]);
+               tdb_close(tdb);
+               return ret;
+       }
 
-                       DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
-                                        allow_unhealthy));
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
+       ZERO_STRUCT(header);
 
-               DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
-                                    argv[0], argv[0]));
-               DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
-                                    "tunnable AllowUnhealthyDBRead = %u\n",
-                                    allow_unhealthy));
+       if (argc > 3) {
+               header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
        }
-
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc > 4) {
+               header.dmaster = (uint32_t)atol(argv[4]);
+       }
+       if (argc > 5) {
+               header.flags = (uint32_t)atol(argv[5]);
        }
 
+       ctdb_ltdb_header_push(&header, header_buf);
 
-       ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       data[0].dsize = ctdb_ltdb_header_len(&header);
+       data[0].dptr = header_buf;
 
+       data[1].dsize = value.dsize;
+       data[1].dptr = value.dptr;
 
-       bd = talloc_zero(tmp_ctx, struct backup_data);
-       if (bd == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to write record %s to file %s\n",
+                       argv[1], argv[0]);
        }
 
-       bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
-       if (bd->records == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       tdb_close(tdb);
 
-       bd->len = offsetof(struct ctdb_marshall_buffer, data);
-       bd->records->db_id = ctdb_db->db_id;
-       /* traverse the database collecting all records */
-       if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
-           bd->traverse_error) {
-               DEBUG(DEBUG_ERR,("Traverse error\n"));
-               talloc_free(tmp_ctx);
-               return -1;              
-       }
+       return ret;
+}
 
-       tdb_transaction_cancel(ctdb_db->ltdb->tdb);
+static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
+{
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_record_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key, data;
+       bool readonly = false;
+       int ret;
 
+       if (argc < 2 || argc > 3) {
+               usage("readkey");
+       }
 
-       fh = open(argv[1], O_RDWR|O_CREAT, 0600);
-       if (fh == -1) {
-               DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc == 3) {
+               if (strcmp(argv[2], "readonly") == 0) {
+                       readonly = true;
+               } else {
+                       usage("readkey");
+               }
        }
 
-       ZERO_STRUCT(dbhdr);
-       dbhdr.version = DB_VERSION;
-       dbhdr.timestamp = time(NULL);
-       dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
-       dbhdr.size = bd->len;
-       if (strlen(argv[0]) >= MAX_DB_NAME) {
-               DEBUG(DEBUG_ERR,("Too long dbname\n"));
-               goto done;
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
-       strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME-1);
-       ret = sys_write(fh, &dbhdr, sizeof(dbhdr));
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
-               goto done;
+
+       if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+               fprintf(stderr, "DB %s is not a volatile database\n",
+                       db_name);
+               return 1;
        }
-       ret = sys_write(fh, bd->records, bd->len);
-       if (ret == -1) {
-               DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
-               goto done;
+
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
 
-       status = 0;
-done:
-       if (fh != -1) {
-               ret = close(fh);
-               if (ret == -1) {
-                       DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
-               }
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               return ret;
        }
 
-       DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
+       ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
+                             db, key, readonly, &h, NULL, &data);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to read record for key %s\n",
+                       argv[1]);
+       } else {
+               printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
+                      (int)data.dsize, data.dptr);
+       }
 
-       talloc_free(tmp_ctx);
-       return status;
+       talloc_free(h);
+       return ret;
 }
 
-/*
- * restore a database from a file 
- */
-static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                           int argc, const char **argv)
 {
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_record_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key, data;
        int ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA outdata;
-       TDB_DATA data;
-       struct db_file_header dbhdr;
-       struct ctdb_db_context *ctdb_db;
-       struct ctdb_node_map_old *nodemap=NULL;
-       struct ctdb_vnn_map *vnnmap=NULL;
-       int i, fh;
-       struct ctdb_transdb w;
-       uint32_t *nodes;
-       uint32_t generation;
-       struct tm *tm;
-       char tbuf[100];
-       char *dbname;
-
-       assert_single_node_only();
-
-       if (argc < 1 || argc > 2) {
-               DEBUG(DEBUG_ERR,("Invalid arguments\n"));
-               return -1;
-       }
 
-       fh = open(argv[0], O_RDONLY);
-       if (fh == -1) {
-               DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc != 3) {
+               usage("writekey");
        }
 
-       sys_read(fh, &dbhdr, sizeof(dbhdr));
-       if (dbhdr.version != DB_VERSION) {
-               DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
-               close(fh);
-               talloc_free(tmp_ctx);
-               return -1;
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
 
-       dbname = discard_const(dbhdr.name);
-       if (argc == 2) {
-               dbname = discard_const(argv[1]);
+       if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+               fprintf(stderr, "DB %s is not a volatile database\n",
+                       db_name);
+               return 1;
        }
 
-       outdata.dsize = dbhdr.size;
-       outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
-       if (outdata.dptr == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
-               close(fh);
-               talloc_free(tmp_ctx);
-               return -1;
-       }               
-       sys_read(fh, outdata.dptr, outdata.dsize);
-       close(fh);
-
-       tm = localtime(&dbhdr.timestamp);
-       strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
-       printf("Restoring database '%s' from backup @ %s\n",
-               dbname, tbuf);
-
-
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
                return ret;
        }
 
-
-       ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
+       ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
-               talloc_free(tmp_ctx);
+               fprintf(stderr, "Failed to parse value %s\n", argv[2]);
                return ret;
        }
 
-       /* freeze all nodes */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       for (i=1; i<=NUM_DB_PRIORITIES; i++) {
-               if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
-                                       nodes, i,
-                                       TIMELIMIT(),
-                                       false, tdb_null,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
-                       ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
+       ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
+                             db, key, false, &h, NULL, NULL);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
+               return ret;
        }
 
-       generation = vnnmap->generation;
-       data.dptr = (void *)&generation;
-       data.dsize = sizeof(generation);
-
-       /* start a cluster wide transaction */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
-               return -1;
+       ret = ctdb_store_record(h, data);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to store record for key %s\n",
+                       argv[1]);
        }
 
+       talloc_free(h);
+       return ret;
+}
 
-       w.db_id = ctdb_db->db_id;
-       w.tid = generation;
-
-       data.dptr = (void *)&w;
-       data.dsize = sizeof(w);
+static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
+{
+       const char *db_name;
+       struct ctdb_db_context *db;
+       struct ctdb_record_handle *h;
+       uint8_t db_flags;
+       TDB_DATA key, data;
+       int ret;
 
-       /* wipe all the remote databases. */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-       
-       /* push the database */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, outdata,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc != 2) {
+               usage("deletekey");
        }
 
-       data.dptr = (void *)&ctdb_db->db_id;
-       data.dsize = sizeof(ctdb_db->db_id);
-
-       /* mark the database as healthy */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
+               return 1;
        }
 
-       data.dptr = (void *)&generation;
-       data.dsize = sizeof(generation);
+       if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
+               fprintf(stderr, "DB %s is not a volatile database\n",
+                       db_name);
+               return 1;
+       }
 
-       /* commit all the changes */
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
+                         db_flags, &db);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to attach to DB %s\n", db_name);
+               return ret;
        }
 
+       ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to parse key %s\n", argv[1]);
+               return ret;
+       }
 
-       /* thaw all nodes */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
-                                       nodes, 0,
-                                       TIMELIMIT(),
-                                       false, tdb_null,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
+                             db, key, false, &h, NULL, &data);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to fetch record for key %s\n",
+                       argv[1]);
+               return ret;
        }
 
+       ret = ctdb_delete_record(h);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to delete record for key %s\n",
+                       argv[1]);
+       }
 
-       talloc_free(tmp_ctx);
-       return 0;
+       talloc_free(h);
+       return ret;
 }
 
-/*
- * dump a database backup from a file
- */
-static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA outdata;
-       struct db_file_header dbhdr;
-       int i, fh;
-       struct tm *tm;
-       char tbuf[100];
-       struct ctdb_rec_data_old *rec = NULL;
-       struct ctdb_marshall_buffer *m;
-       struct ctdb_dump_db_context c;
-
-       assert_single_node_only();
+       struct sockaddr_in sin;
+       unsigned int port;
+       int s, v;
+       int ret;
 
        if (argc != 1) {
-               DEBUG(DEBUG_ERR,("Invalid arguments\n"));
-               return -1;
+               usage("chktcpport");
        }
 
-       fh = open(argv[0], O_RDONLY);
-       if (fh == -1) {
-               DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       port = atoi(argv[0]);
 
-       sys_read(fh, &dbhdr, sizeof(dbhdr));
-       if (dbhdr.version != DB_VERSION) {
-               DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
-               close(fh);
-               talloc_free(tmp_ctx);
-               return -1;
+       s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+       if (s == -1) {
+               fprintf(stderr, "Failed to open local socket\n");
+               return errno;
        }
 
-       outdata.dsize = dbhdr.size;
-       outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
-       if (outdata.dptr == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
-               close(fh);
-               talloc_free(tmp_ctx);
-               return -1;
+       v = fcntl(s, F_GETFL, 0);
+       if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
+               fprintf(stderr, "Unable to set socket non-blocking\n");
+               close(s);
+               return errno;
        }
-       sys_read(fh, outdata.dptr, outdata.dsize);
-       close(fh);
-       m = (struct ctdb_marshall_buffer *)outdata.dptr;
-
-       tm = localtime(&dbhdr.timestamp);
-       strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
-       printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
-               dbhdr.name, m->db_id, tbuf);
 
-       ZERO_STRUCT(c);
-       c.ctdb = ctdb;
-       c.f = stdout;
-       c.printemptyrecords = (bool)options.printemptyrecords;
-       c.printdatasize = (bool)options.printdatasize;
-       c.printlmaster = false;
-       c.printhash = (bool)options.printhash;
-       c.printrecordflags = (bool)options.printrecordflags;
-
-       for (i=0; i < m->count; i++) {
-               uint32_t reqid = 0;
-               TDB_DATA key, data;
-
-               /* we do not want the header splitted, so we pass NULL*/
-               rec = ctdb_marshall_loop_next(m, rec, &reqid,
-                                             NULL, &key, &data);
-
-               ctdb_dumpdb_record(key, data, &c);
+       bzero(&sin, sizeof(sin));
+       sin.sin_family = AF_INET;
+       sin.sin_port = htons(port);
+       ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
+       close(s);
+       if (ret == -1) {
+               fprintf(stderr, "Failed to bind to TCP port %u\n", port);
+               return errno;
        }
 
-       printf("Dumped %d records\n", i);
-       talloc_free(tmp_ctx);
        return 0;
 }
 
-/*
- * wipe a database from a file
- */
-static int control_wipedb(struct ctdb_context *ctdb, int argc,
-                         const char **argv)
+static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                              int argc, const char **argv)
 {
+       uint32_t db_id;
        const char *db_name;
+       uint64_t seqnum;
        int ret;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       TDB_DATA data;
-       struct ctdb_db_context *ctdb_db;
-       struct ctdb_node_map_old *nodemap = NULL;
-       struct ctdb_vnn_map *vnnmap = NULL;
-       int i;
-       struct ctdb_transdb w;
-       uint32_t *nodes;
-       uint32_t generation;
-       uint8_t flags;
-
-       assert_single_node_only();
 
        if (argc != 1) {
-               DEBUG(DEBUG_ERR,("Invalid arguments\n"));
-               return -1;
-       }
-
-       if (!db_exists(ctdb, argv[0], NULL, &db_name, &flags)) {
-               return -1;
+               usage("getdbseqnum");
        }
 
-       ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
-       if (ctdb_db == NULL) {
-               DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
-                                 argv[0]));
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
-                                  &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
-                                 options.pnn));
-               talloc_free(tmp_ctx);
-               return ret;
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
+               return 1;
        }
 
-       ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
-                                 &vnnmap);
+       ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
+                                     ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                     &seqnum);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
-                                 options.pnn));
-               talloc_free(tmp_ctx);
+               fprintf(stderr, "Failed to get sequence number for DB %s\n",
+                       db_name);
                return ret;
        }
 
-       /* freeze all nodes */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       for (i=1; i<=NUM_DB_PRIORITIES; i++) {
-               ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
-                                               nodes, i,
-                                               TIMELIMIT(),
-                                               false, tdb_null,
-                                               NULL, NULL,
-                                               NULL);
-               if (ret != 0) {
-                       DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
-                       ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
-                                            CTDB_RECOVERY_ACTIVE);
-                       talloc_free(tmp_ctx);
-                       return -1;
-               }
-       }
-
-       generation = vnnmap->generation;
-       data.dptr = (void *)&generation;
-       data.dsize = sizeof(generation);
-
-       /* start a cluster wide transaction */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL);
-       if (ret!= 0) {
-               DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
-                                 "transactions.\n"));
-               return -1;
-       }
-
-       w.db_id = ctdb_db->db_id;
-       w.tid = generation;
-
-       data.dptr = (void *)&w;
-       data.dsize = sizeof(w);
-
-       /* wipe all the remote databases. */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
-       }
+       printf("0x%"PRIx64"\n", seqnum);
+       return 0;
+}
 
-       data.dptr = (void *)&ctdb_db->db_id;
-       data.dsize = sizeof(ctdb_db->db_id);
+static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                             int argc, const char **argv)
+{
+       const char *nodestring = NULL;
+       struct ctdb_node_map *nodemap;
+       int ret, i;
+       bool print_hdr = false;
 
-       /* mark the database as healthy */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc > 1) {
+               usage("nodestatus");
        }
 
-       data.dptr = (void *)&generation;
-       data.dsize = sizeof(generation);
-
-       /* commit all the changes */
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
-                                       nodes, 0,
-                                       TIMELIMIT(), false, data,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
-       }
-
-       /* thaw all nodes */
-       nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
-       if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
-                                       nodes, 0,
-                                       TIMELIMIT(),
-                                       false, tdb_null,
-                                       NULL, NULL,
-                                       NULL) != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
-               ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
-               talloc_free(tmp_ctx);
-               return -1;
+       if (argc == 1) {
+               nodestring = argv[0];
+               if (strcmp(nodestring, "all") == 0) {
+                       print_hdr = true;
+               }
        }
 
-       DEBUG(DEBUG_ERR, ("Database wiped.\n"));
+       if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
+               return 1;
+       }
 
-       talloc_free(tmp_ctx);
-       return 0;
-}
+       if (options.machinereadable) {
+               print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
+       } else {
+               print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, print_hdr);
+       }
 
-/*
-  dump memory usage
- */
-static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       TDB_DATA data;
-       int ret;
-       int32_t res;
-       char *errmsg;
-       TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
-       ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
-                          0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
-       if (ret != 0 || res != 0) {
-               DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
-               talloc_free(tmp_ctx);
-               return -1;
+       ret = 0;
+       for (i=0; i<nodemap->num; i++) {
+               ret |= nodemap->node[i].flags;
        }
-       sys_write(1, data.dptr, data.dsize);
-       talloc_free(tmp_ctx);
-       return 0;
-}
 
-/*
-  handler for memory dumps
-*/
-static void mem_dump_handler(uint64_t srvid, TDB_DATA data, void *private_data)
-{
-       sys_write(1, data.dptr, data.dsize);
-       exit(0);
+       return ret;
 }
 
-/*
-  dump memory usage on the recovery daemon
- */
-static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
-{
-       int ret;
-       TDB_DATA data;
-       struct ctdb_srvid_message rd;
-
-       rd.pnn = ctdb_get_pnn(ctdb);
-       rd.srvid = getpid();
-
-       /* register a message port for receiveing the reply so that we
-          can receive the reply
-       */
-       ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
-
+const struct {
+       const char *name;
+       uint32_t offset;
+} db_stats_fields[] = {
+#define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
+       DBSTATISTICS_FIELD(db_ro_delegations),
+       DBSTATISTICS_FIELD(db_ro_revokes),
+       DBSTATISTICS_FIELD(locks.num_calls),
+       DBSTATISTICS_FIELD(locks.num_current),
+       DBSTATISTICS_FIELD(locks.num_pending),
+       DBSTATISTICS_FIELD(locks.num_failed),
+       DBSTATISTICS_FIELD(db_ro_delegations),
+};
 
-       data.dptr = (uint8_t *)&rd;
-       data.dsize = sizeof(rd);
+static void print_dbstatistics(const char *db_name,
+                              struct ctdb_db_statistics *s)
+{
+       int i;
+       const char *prefix = NULL;
+       int preflen = 0;
+
+       printf("DB Statistics %s\n", db_name);
+
+       for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
+               if (strchr(db_stats_fields[i].name, '.') != NULL) {
+                       preflen = strcspn(db_stats_fields[i].name, ".") + 1;
+                       if (! prefix ||
+                           strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
+                               prefix = db_stats_fields[i].name;
+                               printf(" %*.*s\n", preflen-1, preflen-1,
+                                      db_stats_fields[i].name);
+                       }
+               } else {
+                       preflen = 0;
+               }
+               printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
+                      db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
+                      *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
+       }
 
-       ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
-               return -1;
+       printf(" hop_count_buckets:");
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               printf(" %d", s->hop_count_bucket[i]);
        }
+       printf("\n");
 
-       /* this loop will terminate when we have received the reply */
-       while (1) {
-               tevent_loop_once(ctdb->ev);
+       printf(" lock_buckets:");
+       for (i=0; i<MAX_COUNT_BUCKETS; i++) {
+               printf(" %d", s->locks.buckets[i]);
        }
+       printf("\n");
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "locks_latency      MIN/AVG/MAX",
+              s->locks.latency.min, LATENCY_AVG(s->locks.latency),
+              s->locks.latency.max, s->locks.latency.num);
+
+       printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
+              "vacuum_latency     MIN/AVG/MAX",
+              s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
+              s->vacuum.latency.max, s->vacuum.latency.num);
 
-       return 0;
+       printf(" Num Hot Keys:     %d\n", s->num_hot_keys);
+       for (i=0; i<s->num_hot_keys; i++) {
+               int j;
+               printf("     Count:%d Key:", s->hot_keys[i].count);
+               for (j=0; j<s->hot_keys[i].key.dsize; j++) {
+                       printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
+               }
+               printf("\n");
+       }
 }
 
-/*
-  send a message to a srvid
- */
-static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
+static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                               int argc, const char **argv)
 {
-       unsigned long srvid;
+       uint32_t db_id;
+       const char *db_name;
+       struct ctdb_db_statistics *dbstats;
        int ret;
-       TDB_DATA data;
 
-       if (argc < 2) {
-               usage();
+       if (argc != 1) {
+               usage("dbstatistics");
        }
 
-       srvid      = strtoul(argv[0], NULL, 0);
-
-       data.dptr = (uint8_t *)discard_const(argv[1]);
-       data.dsize= strlen(argv[1]);
+       if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
+               return 1;
+       }
 
-       ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
+       ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
+                                         ctdb->cmd_pnn, TIMEOUT(), db_id,
+                                         &dbstats);
        if (ret != 0) {
-               DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
-               return -1;
+               fprintf(stderr, "Failed to get statistics for DB %s\n",
+                       db_name);
+               return ret;
        }
 
+       print_dbstatistics(db_name, dbstats);
        return 0;
 }
 
-/*
-  handler for msglisten
-*/
-static void msglisten_handler(uint64_t srvid, TDB_DATA data,
-                             void *private_data)
-{
-       int i;
-
-       printf("Message received: ");
-       for (i=0;i<data.dsize;i++) {
-               printf("%c", data.dptr[i]);
-       }
-       printf("\n");
-}
+struct disable_takeover_runs_state {
+       uint32_t *pnn_list;
+       int node_count;
+       bool *reply;
+       int status;
+       bool done;
+};
 
-/*
-  listen for messages on a messageport
- */
-static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
+static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
+                                        void *private_data)
 {
-       uint64_t srvid;
-
-       srvid = getpid();
+       struct disable_takeover_runs_state *state =
+               (struct disable_takeover_runs_state *)private_data;
+       int ret, i;
 
-       /* register a message port and listen for messages
-       */
-       ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
-       printf("Listening for messages on srvid:%d\n", (int)srvid);
+       if (data.dsize != sizeof(int)) {
+               /* Ignore packet */
+               return;
+       }
 
-       while (1) {
-               tevent_loop_once(ctdb->ev);
+       /* ret will be a PNN (i.e. >=0) on success, or negative on error */
+       ret = *(int *)data.dptr;
+       if (ret < 0) {
+               state->status = ret;
+               state->done = true;
+               return;
+       }
+       for (i=0; i<state->node_count; i++) {
+               if (state->pnn_list[i] == ret) {
+                       state->reply[i] = true;
+                       break;
+               }
        }
 
-       return 0;
+       state->done = true;
+       for (i=0; i<state->node_count; i++) {
+               if (! state->reply[i]) {
+                       state->done = false;
+                       break;
+               }
+       }
 }
 
-/*
-  list all nodes in the cluster
-  we parse the nodes file directly
- */
-static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
+static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
+                                struct ctdb_context *ctdb, uint32_t timeout,
+                                uint32_t *pnn_list, int count)
 {
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
-       struct ctdb_node_map_old *node_map;
-       int i;
+       struct ctdb_disable_message disable = { 0 };
+       struct disable_takeover_runs_state state;
+       int ret, i;
 
-       assert_single_node_only();
+       disable.pnn = ctdb->pnn;
+       disable.srvid = next_srvid(ctdb);
+       disable.timeout = timeout;
 
-       node_map = read_nodes_file(mem_ctx);
-       if (node_map == NULL) {
-               talloc_free(mem_ctx);
-               return -1;
+       state.pnn_list = pnn_list;
+       state.node_count = count;
+       state.done = false;
+       state.status = 0;
+       state.reply = talloc_zero_array(mem_ctx, bool, count);
+       if (state.reply == NULL) {
+               return ENOMEM;
        }
 
-       for (i = 0; i < node_map->num; i++) {
-               const char *addr;
+       ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
+                                             disable.srvid,
+                                             disable_takeover_run_handler,
+                                             &state);
+       if (ret != 0) {
+               return ret;
+       }
 
-               if (node_map->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       continue;
-               }
-               addr = ctdb_addr_to_str(&node_map->nodes[i].addr);
-               if (options.machinereadable){
-                       printm(":%d:%s:\n", node_map->nodes[i].pnn, addr);
-               } else {
-                       printf("%s\n", addr);
+       for (i=0; i<count; i++) {
+               ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
+                                                        ctdb->client,
+                                                        pnn_list[i],
+                                                        &disable);
+               if (ret != 0) {
+                       goto fail;
                }
        }
-       talloc_free(mem_ctx);
 
-       return 0;
-}
+       ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
+       if (ret == ETIME) {
+               fprintf(stderr, "Timed out waiting to disable takeover runs\n");
+       } else {
+               ret = (state.status >= 0 ? 0 : 1);
+       }
 
-/**********************************************************************/
-/* reload the nodes file on all nodes */
+fail:
+       ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
+                                          disable.srvid, &state);
+       return ret;
+}
 
-static void get_nodes_files_callback(struct ctdb_context *ctdb,
-                                    uint32_t node_pnn, int32_t res,
-                                    TDB_DATA outdata, void *callback_data)
+static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                            int argc, const char **argv)
 {
-       struct ctdb_node_map_old **maps =
-               talloc_get_type(callback_data, struct ctdb_node_map_old *);
+       const char *nodestring = NULL;
+       struct ctdb_node_map *nodemap, *nodemap2;
+       struct ctdb_req_control request;
+       uint32_t *pnn_list, *pnn_list2;
+       int ret, count, count2;
 
-       if (outdata.dsize < offsetof(struct ctdb_node_map_old, nodes) ||
-           outdata.dptr == NULL) {
-               DEBUG(DEBUG_ERR,
-                     (__location__ " Invalid return data: %u %p\n",
-                      (unsigned)outdata.dsize, outdata.dptr));
-               return;
+       if (argc > 1) {
+               usage("reloadips");
        }
 
-       if (node_pnn >= talloc_array_length(maps)) {
-               DEBUG(DEBUG_ERR,
-                     (__location__ " unexpected PNN %u\n", node_pnn));
-               return;
+       if (argc == 1) {
+               nodestring = argv[0];
        }
 
-       maps[node_pnn] = talloc_memdup(maps, outdata.dptr, outdata.dsize);
-}
+       nodemap = get_nodemap(ctdb, false);
+       if (nodemap == NULL) {
+               return 1;
+       }
 
-static void get_nodes_files_fail_callback(struct ctdb_context *ctdb,
-                                         uint32_t node_pnn, int32_t res,
-                                         TDB_DATA outdata, void *callback_data)
-{
-       DEBUG(DEBUG_ERR,
-             ("ERROR: Failed to get nodes file from node %u\n", node_pnn));
-}
+       if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
+               return 1;
+       }
 
-static struct ctdb_node_map_old **
-ctdb_get_nodes_files(struct ctdb_context *ctdb,
-                    TALLOC_CTX *mem_ctx,
-                    struct timeval timeout,
-                    struct ctdb_node_map_old *nodemap)
-{
-       uint32_t *nodes;
-       int ret;
-       struct ctdb_node_map_old **maps;
+       count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
+                                       mem_ctx, &pnn_list);
+       if (count <= 0) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
 
-       maps = talloc_zero_array(mem_ctx, struct ctdb_node_map_old *, nodemap->num);
-       CTDB_NO_MEMORY_NULL(ctdb, maps);
+       count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
+                                     mem_ctx, &pnn_list2);
+       if (count2 <= 0) {
+               fprintf(stderr, "Memory allocation error\n");
+               return 1;
+       }
 
-       nodes = list_of_connected_nodes(ctdb, nodemap, mem_ctx, true);
+       /* Disable takeover runs on all connected nodes.  A reply
+        * indicating success is needed from each node so all nodes
+        * will need to be active.
+        *
+        * A check could be added to not allow reloading of IPs when
+        * there are disconnected nodes.  However, this should
+        * probably be left up to the administrator.
+        */
+       ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
+                                   pnn_list, count);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to disable takeover runs\n");
+               return ret;
+       }
 
-       ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_GET_NODES_FILE,
-                                       nodes, 0, TIMELIMIT(),
-                                       true, tdb_null,
-                                       get_nodes_files_callback,
-                                       get_nodes_files_fail_callback,
-                                       maps);
+       /* Now tell all the desired nodes to reload their public IPs.
+        * Keep trying this until it succeeds.  This assumes all
+        * failures are transient, which might not be true...
+        */
+       ctdb_req_control_reload_public_ips(&request);
+       ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
+                                       pnn_list2, count2, TIMEOUT(),
+                                       &request, NULL, NULL);
        if (ret != 0) {
-               talloc_free(maps);
-               return NULL;
+               fprintf(stderr, "Failed to reload IPs on some nodes.\n");
+       }
+
+       /* It isn't strictly necessary to wait until takeover runs are
+        * re-enabled but doing so can't hurt.
+        */
+       ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to enable takeover runs\n");
+               return ret;
        }
 
-       return maps;
+       return ipreallocate(mem_ctx, ctdb);
 }
 
-static bool node_files_are_identical(struct ctdb_node_map_old *nm1,
-                                    struct ctdb_node_map_old *nm2)
+static int control_ipiface(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
+                          int argc, const char **argv)
 {
-       int i;
+       ctdb_sock_addr addr;
+       char *iface;
 
-       if (nm1->num != nm2->num) {
-               return false;
+       if (argc != 1) {
+               usage("ipiface");
        }
-       for (i = 0; i < nm1->num; i++) {
-               if (memcmp(&nm1->nodes[i], &nm2->nodes[i],
-                          sizeof(struct ctdb_node_and_flags)) != 0) {
-                       return false;
-               }
+
+       if (! parse_ip(argv[0], NULL, 0, &addr)) {
+               fprintf(stderr, "Failed to Parse IP %s\n", argv[0]);
+               return 1;
        }
 
-       return true;
+       iface = ctdb_sys_find_ifname(&addr);
+       if (iface == NULL) {
+               fprintf(stderr, "Failed to find interface for IP %s\n",
+                       argv[0]);
+               return 1;
+       }
+       free(iface);
+
+       return 0;
 }
 
-static bool check_all_node_files_are_identical(struct ctdb_context *ctdb,
-                                              TALLOC_CTX *mem_ctx,
-                                              struct timeval timeout,
-                                              struct ctdb_node_map_old *nodemap,
-                                              struct ctdb_node_map_old *file_nodemap)
+
+static const struct ctdb_cmd {
+       const char *name;
+       int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
+       bool without_daemon; /* can be run without daemon running ? */
+       bool remote; /* can be run on remote nodes */
+       const char *msg;
+       const char *args;
+} ctdb_commands[] = {
+       { "version", control_version, true, false,
+               "show version of ctdb", NULL },
+       { "status", control_status, false, true,
+               "show node status", NULL },
+       { "uptime", control_uptime, false, true,
+               "show node uptime", NULL },
+       { "ping", control_ping, false, true,
+               "ping all nodes", NULL },
+       { "runstate", control_runstate, false, true,
+               "get/check runstate of a node",
+               "[setup|first_recovery|startup|running]" },
+       { "getvar", control_getvar, false, true,
+               "get a tunable variable", "<name>" },
+       { "setvar", control_setvar, false, true,
+               "set a tunable variable", "<name> <value>" },
+       { "listvars", control_listvars, false, true,
+               "list tunable variables", NULL },
+       { "statistics", control_statistics, false, true,
+               "show ctdb statistics", NULL },
+       { "statisticsreset", control_statistics_reset, false, true,
+               "reset ctdb statistics", NULL },
+       { "stats", control_stats, false, true,
+               "show rolling statistics", "[count]" },
+       { "ip", control_ip, false, true,
+               "show public ips", "[all]" },
+       { "ipinfo", control_ipinfo, false, true,
+               "show public ip details", "<ip>" },
+       { "ifaces", control_ifaces, false, true,
+               "show interfaces", NULL },
+       { "setifacelink", control_setifacelink, false, true,
+               "set interface link status", "<iface> up|down" },
+       { "process-exists", control_process_exists, false, true,
+               "check if a process exists on a node",  "<pid>" },
+       { "getdbmap", control_getdbmap, false, true,
+               "show attached databases", NULL },
+       { "getdbstatus", control_getdbstatus, false, true,
+               "show database status", "<dbname|dbid>" },
+       { "catdb", control_catdb, false, false,
+               "dump cluster-wide ctdb database", "<dbname|dbid>" },
+       { "cattdb", control_cattdb, false, false,
+               "dump local ctdb database", "<dbname|dbid>" },
+       { "getmonmode", control_getmonmode, false, true,
+               "show monitoring mode", NULL },
+       { "getcapabilities", control_getcapabilities, false, true,
+               "show node capabilities", NULL },
+       { "pnn", control_pnn, false, false,
+               "show the pnn of the currnet node", NULL },
+       { "lvs", control_lvs, false, false,
+               "show lvs configuration", "master|list|status" },
+       { "disablemonitor", control_disable_monitor, false, true,
+               "disable monitoring", NULL },
+       { "enablemonitor", control_enable_monitor, false, true,
+               "enable monitoring", NULL },
+       { "setdebug", control_setdebug, false, true,
+               "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
+       { "getdebug", control_getdebug, false, true,
+               "get debug level", NULL },
+       { "attach", control_attach, false, false,
+               "attach a database", "<dbname> [persistent]" },
+       { "detach", control_detach, false, false,
+               "detach database(s)", "<dbname|dbid> ..." },
+       { "dumpmemory", control_dumpmemory, false, true,
+               "dump ctdbd memory map", NULL },
+       { "rddumpmemory", control_rddumpmemory, false, true,
+               "dump recoverd memory map", NULL },
+       { "getpid", control_getpid, false, true,
+               "get ctdbd process ID", NULL },
+       { "disable", control_disable, false, true,
+               "disable a node", NULL },
+       { "enable", control_enable, false, true,
+               "enable a node", NULL },
+       { "stop", control_stop, false, true,
+               "stop a node", NULL },
+       { "continue", control_continue, false, true,
+               "continue a stopped node", NULL },
+       { "ban", control_ban, false, true,
+               "ban a node", "<bantime>"},
+       { "unban", control_unban, false, true,
+               "unban a node", NULL },
+       { "shutdown", control_shutdown, false, true,
+               "shutdown ctdb daemon", NULL },
+       { "recover", control_recover, false, true,
+               "force recovery", NULL },
+       { "sync", control_ipreallocate, false, true,
+               "run ip reallocation (deprecated)", NULL },
+       { "ipreallocate", control_ipreallocate, false, true,
+               "run ip reallocation", NULL },
+       { "isnotrecmaster", control_isnotrecmaster, false, false,
+               "check if local node is the recmaster", NULL },
+       { "gratarp", control_gratarp, false, true,
+               "send a gratuitous arp", "<ip> <interface>" },
+       { "tickle", control_tickle, true, false,
+               "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
+       { "gettickles", control_gettickles, false, true,
+               "get the list of tickles", "<ip> [<port>]" },
+       { "addtickle", control_addtickle, false, true,
+               "add a tickle", "<ip>:<port> <ip>:<port>" },
+       { "deltickle", control_deltickle, false, true,
+               "delete a tickle", "<ip>:<port> <ip>:<port>" },
+       { "check_srvids", control_check_srvids, false, true,
+               "check if srvid is registered", "<id> [<id> ...]" },
+       { "listnodes", control_listnodes, true, true,
+               "list nodes in the cluster", NULL },
+       { "reloadnodes", control_reloadnodes, false, false,
+               "reload the nodes file all nodes", NULL },
+       { "moveip", control_moveip, false, false,
+               "move an ip address to another node", "<ip> <node>" },
+       { "addip", control_addip, false, true,
+               "add an ip address to a node", "<ip/mask> <iface>" },
+       { "delip", control_delip, false, true,
+               "delete an ip address from a node", "<ip>" },
+       { "backupdb", control_backupdb, false, false,
+               "backup a database into a file", "<dbname|dbid> <file>" },
+       { "restoredb", control_restoredb, false, false,
+               "restore a database from a file", "<file> [dbname]" },
+       { "dumpdbbackup", control_dumpdbbackup, true, false,
+               "dump database from a backup file", "<file>" },
+       { "wipedb", control_wipedb, false, false,
+               "wipe the contents of a database.", "<dbname|dbid>"},
+       { "recmaster", control_recmaster, false, true,
+               "show the pnn for the recovery master", NULL },
+       { "event", control_event, true, false,
+               "event and event script commands", NULL },
+       { "scriptstatus", control_scriptstatus, true, false,
+               "show event script status",
+               "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
+       { "natgw", control_natgw, false, false,
+               "show natgw configuration", "master|list|status" },
+       { "natgwlist", control_natgwlist, false, false,
+               "show the nodes belonging to this natgw configuration", NULL },
+       { "getreclock", control_getreclock, false, true,
+               "get recovery lock file", NULL },
+       { "setlmasterrole", control_setlmasterrole, false, true,
+               "set LMASTER role", "on|off" },
+       { "setrecmasterrole", control_setrecmasterrole, false, true,
+               "set RECMASTER role", "on|off"},
+       { "setdbreadonly", control_setdbreadonly, false, true,
+               "enable readonly records", "<dbname|dbid>" },
+       { "setdbsticky", control_setdbsticky, false, true,
+               "enable sticky records", "<dbname|dbid>"},
+       { "pfetch", control_pfetch, false, false,
+               "fetch record from persistent database", "<dbname|dbid> <key> [<file>]" },
+       { "pstore", control_pstore, false, false,
+               "write record to persistent database", "<dbname|dbid> <key> <value>" },
+       { "pdelete", control_pdelete, false, false,
+               "delete record from persistent database", "<dbname|dbid> <key>" },
+       { "ptrans", control_ptrans, false, false,
+               "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
+       { "tfetch", control_tfetch, false, true,
+               "fetch a record", "<tdb-file> <key> [<file>]" },
+       { "tstore", control_tstore, false, true,
+               "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
+       { "readkey", control_readkey, false, false,
+               "read value of a database key", "<dbname|dbid> <key> [readonly]" },
+       { "writekey", control_writekey, false, false,
+               "write value for a database key", "<dbname|dbid> <key> <value>" },
+       { "deletekey", control_deletekey, false, false,
+               "delete a database key", "<dbname|dbid> <key>" },
+       { "checktcpport", control_checktcpport, true, false,
+               "check if a service is bound to a specific tcp port or not", "<port>" },
+       { "getdbseqnum", control_getdbseqnum, false, false,
+               "get database sequence number", "<dbname|dbid>" },
+       { "nodestatus", control_nodestatus, false, true,
+               "show and return node status", "[all|<pnn-list>]" },
+       { "dbstatistics", control_dbstatistics, false, true,
+               "show database statistics", "<dbname|dbid>" },
+       { "reloadips", control_reloadips, false, false,
+               "reload the public addresses file", "[all|<pnn-list>]" },
+       { "ipiface", control_ipiface, true, false,
+               "Find the interface an ip address is hosted on", "<ip>" },
+};
+
+static const struct ctdb_cmd *match_command(const char *command)
 {
-       static struct ctdb_node_map_old **maps;
+       const struct ctdb_cmd *cmd;
        int i;
-       bool ret = true;
-
-       maps = ctdb_get_nodes_files(ctdb, mem_ctx, timeout, nodemap);
-       if (maps == NULL) {
-               return false;
-       }
 
-       for (i = 0; i < talloc_array_length(maps); i++) {
-               if (maps[i] == NULL) {
-                       continue;
-               }
-               if (!node_files_are_identical(file_nodemap, maps[i])) {
-                       DEBUG(DEBUG_ERR,
-                             ("ERROR: Node file on node %u differs from current node (%u)\n",
-                              i, ctdb_get_pnn(ctdb)));
-                       ret = false;
+       for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
+               cmd = &ctdb_commands[i];
+               if (strlen(command) == strlen(cmd->name) &&
+                   strncmp(command, cmd->name, strlen(command)) == 0) {
+                       return cmd;
                }
        }
 
-       return ret;
+       return NULL;
 }
 
-/*
-  reload the nodes file on the local node
+
+/**
+ * Show usage message
  */
-static bool sanity_check_nodes_file_changes(TALLOC_CTX *mem_ctx,
-                                           struct ctdb_node_map_old *nodemap,
-                                           struct ctdb_node_map_old *file_nodemap)
+static void usage_full(void)
 {
        int i;
-       bool should_abort = false;
-       bool have_changes = false;
 
-       for (i=0; i<nodemap->num; i++) {
-               if (i >= file_nodemap->num) {
-                       DEBUG(DEBUG_ERR,
-                             ("ERROR: Node %u (%s) missing from nodes file\n",
-                              nodemap->nodes[i].pnn,
-                              ctdb_addr_to_str(&nodemap->nodes[i].addr)));
-                       should_abort = true;
-                       continue;
-               }
-               if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED &&
-                   file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       /* Node remains deleted */
-                       DEBUG(DEBUG_INFO,
-                             ("Node %u is unchanged (DELETED)\n",
-                              nodemap->nodes[i].pnn));
-               } else if (!(nodemap->nodes[i].flags & NODE_FLAGS_DELETED) &&
-                          !(file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED)) {
-                       /* Node not newly nor previously deleted */
-                       if (!ctdb_same_ip(&nodemap->nodes[i].addr,
-                                         &file_nodemap->nodes[i].addr)) {
-                               DEBUG(DEBUG_ERR,
-                                     ("ERROR: Node %u has changed IP address (was %s, now %s)\n",
-                                      nodemap->nodes[i].pnn,
-                                      /* ctdb_addr_to_str() returns a static */
-                                      talloc_strdup(mem_ctx,
-                                                    ctdb_addr_to_str(&nodemap->nodes[i].addr)),
-                                      ctdb_addr_to_str(&file_nodemap->nodes[i].addr)));
-                               should_abort = true;
-                       } else {
-                               DEBUG(DEBUG_INFO,
-                                     ("Node %u is unchanged\n",
-                                      nodemap->nodes[i].pnn));
-                               if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
-                                       DEBUG(DEBUG_WARNING,
-                                             ("WARNING: Node %u is disconnected. You MUST fix this node manually!\n",
-                                              nodemap->nodes[i].pnn));
-                               }
-                       }
-               } else if (file_nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       /* Node is being deleted */
-                       DEBUG(DEBUG_NOTICE,
-                             ("Node %u is DELETED\n",
-                              nodemap->nodes[i].pnn));
-                       have_changes = true;
-                       if (!(nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED)) {
-                               DEBUG(DEBUG_ERR,
-                                     ("ERROR: Node %u is still connected\n",
-                                      nodemap->nodes[i].pnn));
-                               should_abort = true;
-                       }
-               } else if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
-                       /* Node was previously deleted */
-                       DEBUG(DEBUG_NOTICE,
-                             ("Node %u is UNDELETED\n", nodemap->nodes[i].pnn));
-                       have_changes = true;
-               }
+       poptPrintHelp(pc, stdout, 0);
+       printf("\nCommands:\n");
+       for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
+               printf("  %-15s %-27s  %s\n",
+                      ctdb_commands[i].name,
+                      ctdb_commands[i].args ? ctdb_commands[i].args : "",
+                      ctdb_commands[i].msg);
        }
+}
+
+static void usage(const char *command)
+{
+       const struct ctdb_cmd *cmd;
 
-       if (should_abort) {
-               DEBUG(DEBUG_ERR,
-                     ("ERROR: Nodes will not be reloaded due to previous error\n"));
-               talloc_free(mem_ctx);
+       if (command == NULL) {
+               usage_full();
                exit(1);
        }
 
-       /* Leftover nodes in file are NEW */
-       for (; i < file_nodemap->num; i++) {
-               DEBUG(DEBUG_NOTICE, ("Node %u is NEW\n",
-                                    file_nodemap->nodes[i].pnn));
-               have_changes = true;
+       cmd = match_command(command);
+       if (cmd == NULL) {
+               usage_full();
+       } else {
+               poptPrintUsage(pc, stdout, 0);
+               printf("\nCommands:\n");
+               printf("  %-15s %-27s  %s\n",
+                      cmd->name, cmd->args ? cmd->args : "", cmd->msg);
        }
 
-       return have_changes;
+       exit(1);
 }
 
-static void reload_nodes_fail_callback(struct ctdb_context *ctdb,
-                                      uint32_t node_pnn, int32_t res,
-                                      TDB_DATA outdata, void *callback_data)
-{
-       DEBUG(DEBUG_WARNING,
-             ("WARNING: Node %u failed to reload nodes. You MUST fix this node manually!\n",
-              node_pnn));
-}
+struct poptOption cmdline_options[] = {
+       POPT_AUTOHELP
+       { "socket", 's', POPT_ARG_STRING, &options.socket, 0,
+               "CTDB socket path", "filename" },
+       { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
+               "debug level"},
+       { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
+               "timelimit (in seconds)" },
+       { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
+               "node specification - integer" },
+       { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
+               "enable machine readable output", NULL },
+       { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
+               "specify separator for machine readable output", "CHAR" },
+       { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
+               "enable machine parsable output with separator |", NULL },
+       { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
+               "enable verbose output", NULL },
+       { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
+               "die if runtime exceeds this limit (in seconds)" },
+       POPT_TABLEEND
+};
 
-static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
+static int process_command(const struct ctdb_cmd *cmd, int argc,
+                          const char **argv)
 {
-       int i, ret;
-       struct ctdb_node_map_old *nodemap=NULL;
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       struct ctdb_node_map_old *file_nodemap;
-       uint32_t *conn;
-       uint32_t timeout;
+       TALLOC_CTX *tmp_ctx;
+       struct ctdb_context *ctdb;
+       int ret;
+       bool status;
+       uint64_t srvid_offset;
 
-       assert_current_node_only(ctdb);
+       tmp_ctx = talloc_new(NULL);
+       if (tmp_ctx == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               goto fail;
+       }
 
-       /* Load both the current nodemap and the contents of the local
-        * nodes file.  Compare and sanity check them before doing
-        * anything. */
+       if (cmd->without_daemon) {
+               if (options.pnn != -1) {
+                       fprintf(stderr,
+                               "Cannot specify node for command %s\n",
+                               cmd->name);
+                       goto fail;
+               }
 
-       ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
+               ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
+               talloc_free(tmp_ctx);
                return ret;
        }
 
-       file_nodemap = read_nodes_file(tmp_ctx);
-       if (file_nodemap == NULL) {
-               DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
-               talloc_free(tmp_ctx);
-               return -1;
+       ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
+       if (ctdb == NULL) {
+               fprintf(stderr, "Memory allocation error\n");
+               goto fail;
        }
 
-       if (!check_all_node_files_are_identical(ctdb, tmp_ctx, TIMELIMIT(),
-                                               nodemap, file_nodemap)) {
-               return -1;
+       ctdb->ev = tevent_context_init(ctdb);
+       if (ctdb->ev == NULL) {
+               fprintf(stderr, "Failed to initialize tevent\n");
+               goto fail;
        }
 
-       if (!sanity_check_nodes_file_changes(tmp_ctx, nodemap, file_nodemap)) {
-               DEBUG(DEBUG_NOTICE,
-                     ("No change in nodes file, skipping unnecessary reload\n"));
-               talloc_free(tmp_ctx);
-               return 0;
-       }
+       ret = ctdb_client_init(ctdb, ctdb->ev, options.socket, &ctdb->client);
+       if (ret != 0) {
+               fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
+                       options.socket);
 
-       /* Now make the changes */
-       conn = list_of_connected_nodes(ctdb, nodemap, tmp_ctx, true);
-       for (i = 0; i < talloc_array_length(conn); i++) {
-               DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n",
-                                    conn[i]));
+               if (!find_node_xpnn(ctdb, NULL)) {
+                       fprintf(stderr, "Is this node part of CTDB cluster?\n");
+               }
+               goto fail;
        }
 
-       /* Another timeout could be used, such as ReRecoveryTimeout or
-        * a new one for this purpose.  However, this is the simplest
-        * option. */
-       timeout = options.timelimit;
-       srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
-                       "Disable recoveries", true);
+       ctdb->pnn = ctdb_client_pnn(ctdb->client);
+       srvid_offset = getpid() & 0xFFFF;
+       ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
 
+       if (options.pnn != -1) {
+               status = verify_pnn(ctdb, options.pnn);
+               if (! status) {
+                       goto fail;
+               }
 
-       ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_NODES_FILE,
-                                       conn, 0, TIMELIMIT(),
-                                       true, tdb_null,
-                                       NULL, reload_nodes_fail_callback,
-                                       NULL);
+               ctdb->cmd_pnn = options.pnn;
+       } else {
+               ctdb->cmd_pnn = ctdb->pnn;
+       }
 
-       timeout = 0;
-       srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_RECOVERIES, &timeout,
-                       "Enable recoveries", true);
+       if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
+               fprintf(stderr, "Node cannot be specified for command %s\n",
+                       cmd->name);
+               goto fail;
+       }
 
+       ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
        talloc_free(tmp_ctx);
+       return ret;
 
-       return 0;
+fail:
+       talloc_free(tmp_ctx);
+       return 1;
 }
 
-
-static const struct {
-       const char *name;
-       int (*fn)(struct ctdb_context *, int, const char **);
-       bool auto_all;
-       bool without_daemon; /* can be run without daemon running ? */
-       const char *msg;
-       const char *args;
-} ctdb_commands[] = {
-       { "version",         control_version,           true,   true,   "show version of ctdb" },
-       { "status",          control_status,            true,   false,  "show node status" },
-       { "uptime",          control_uptime,            true,   false,  "show node uptime" },
-       { "ping",            control_ping,              true,   false,  "ping all nodes" },
-       { "runstate",        control_runstate,          true,   false,  "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
-       { "getvar",          control_getvar,            true,   false,  "get a tunable variable",               "<name>"},
-       { "setvar",          control_setvar,            true,   false,  "set a tunable variable",               "<name> <value>"},
-       { "listvars",        control_listvars,          true,   false,  "list tunable variables"},
-       { "statistics",      control_statistics,        false,  false, "show statistics" },
-       { "statisticsreset", control_statistics_reset,  true,   false,  "reset statistics"},
-       { "stats",           control_stats,             false,  false,  "show rolling statistics", "[number of history records]" },
-       { "ip",              control_ip,                false,  false,  "show which public ip's that ctdb manages" },
-       { "ipinfo",          control_ipinfo,            true,   false,  "show details about a public ip that ctdb manages", "<ip>" },
-       { "ifaces",          control_ifaces,            true,   false,  "show which interfaces that ctdb manages" },
-       { "setifacelink",    control_setifacelink,      true,   false,  "set interface link status", "<iface> <status>" },
-       { "process-exists",  control_process_exists,    true,   false,  "check if a process exists on a node",  "<pid>"},
-       { "getdbmap",        control_getdbmap,          true,   false,  "show the database map" },
-       { "getdbstatus",     control_getdbstatus,       true,   false,  "show the status of a database", "<dbname|dbid>" },
-       { "catdb",           control_catdb,             true,   false,  "dump a ctdb database" ,                     "<dbname|dbid>"},
-       { "cattdb",          control_cattdb,            true,   false,  "dump a local tdb database" ,                     "<dbname|dbid>"},
-       { "getmonmode",      control_getmonmode,        true,   false,  "show monitoring mode" },
-       { "getcapabilities", control_getcapabilities,   true,   false,  "show node capabilities" },
-       { "pnn",             control_pnn,               true,   false,  "show the pnn of the currnet node" },
-       { "lvs",             control_lvs,               false,  true,   "show lvs configuration", "[master|list|status]" },
-       { "disablemonitor",      control_disable_monmode,true,  false,  "set monitoring mode to DISABLE" },
-       { "enablemonitor",      control_enable_monmode, true,   false,  "set monitoring mode to ACTIVE" },
-       { "setdebug",        control_setdebug,          true,   false,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
-       { "getdebug",        control_getdebug,          true,   false,  "get debug level" },
-       { "attach",          control_attach,            true,   false,  "attach to a database",                 "<dbname> [persistent]" },
-       { "detach",          control_detach,            false,  false,  "detach from a database",                 "<dbname|dbid> [<dbname|dbid> ...]" },
-       { "dumpmemory",      control_dumpmemory,        true,   false,  "dump memory map to stdout" },
-       { "rddumpmemory",    control_rddumpmemory,      true,   false,  "dump memory map from the recovery daemon to stdout" },
-       { "getpid",          control_getpid,            true,   false,  "get ctdbd process ID" },
-       { "disable",         control_disable,           true,   false,  "disable a nodes public IP" },
-       { "enable",          control_enable,            true,   false,  "enable a nodes public IP" },
-       { "stop",            control_stop,              true,   false,  "stop a node" },
-       { "continue",        control_continue,          true,   false,  "re-start a stopped node" },
-       { "ban",             control_ban,               true,   false,  "ban a node from the cluster",          "<bantime>"},
-       { "unban",           control_unban,             true,   false,  "unban a node" },
-       { "showban",         control_showban,           true,   false,  "show ban information"},
-       { "shutdown",        control_shutdown,          true,   false,  "shutdown ctdbd" },
-       { "recover",         control_recover,           true,   false,  "force recovery" },
-       { "sync",            control_ipreallocate,      false,  false,  "wait until ctdbd has synced all state changes" },
-       { "ipreallocate",    control_ipreallocate,      false,  false,  "force the recovery daemon to perform a ip reallocation procedure" },
-       { "thaw",            control_thaw,              true,   false,  "thaw databases", "[priority:1-3]" },
-       { "isnotrecmaster",  control_isnotrecmaster,    false,  false,  "check if the local node is recmaster or not" },
-       { "gratiousarp",     control_gratious_arp,      false,  false, "send a gratious arp", "<ip> <interface>" },
-       { "tickle",          tickle_tcp,                false,  false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
-       { "gettickles",      control_get_tickles,       false,  false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
-       { "addtickle",       control_add_tickle,        false,  false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
-
-       { "deltickle",       control_del_tickle,        false,  false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
-
-       { "check_srvids",    check_srvids,              false,  false, "check if a srvid exists", "<id>+" },
-       { "listnodes",       control_listnodes,         false,  true, "list all nodes in the cluster"},
-       { "reloadnodes",     control_reload_nodes_file, false,  false, "reload the nodes file and restart the transport on all nodes"},
-       { "moveip",          control_moveip,            false,  false, "move/failover an ip address to another node", "<ip> <node>"},
-       { "addip",           control_addip,             true,   false, "add a ip address to a node", "<ip/mask> <iface>"},
-       { "delip",           control_delip,             false,  false, "delete an ip address from a node", "<ip>"},
-       { "eventscript",     control_eventscript,       true,   false, "run the eventscript with the given parameters on a node", "<arguments>"},
-       { "backupdb",        control_backupdb,          false,  false, "backup the database into a file.", "<dbname|dbid> <file>"},
-       { "restoredb",        control_restoredb,        false,  false, "restore the database from a file.", "<file> [dbname]"},
-       { "dumpdbbackup",    control_dumpdbbackup,      false,  true,  "dump database backup from a file.", "<file>"},
-       { "wipedb",           control_wipedb,        false,     false, "wipe the contents of a database.", "<dbname|dbid>"},
-       { "recmaster",        control_recmaster,        true,   false, "show the pnn for the recovery master."},
-       { "scriptstatus",     control_scriptstatus,     true,   false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
-       { "enablescript",     control_enablescript,  true,      false, "enable an eventscript", "<script>"},
-       { "disablescript",    control_disablescript,  true,     false, "disable an eventscript", "<script>"},
-       { "natgwlist",        control_natgwlist,        false,  true, "show the nodes belonging to this natgw configuration"},
-       { "xpnn",             control_xpnn,             false,  true,  "find the pnn of the local node without talking to the daemon (unreliable)" },
-       { "getreclock",       control_getreclock,       true,   false, "Show the reclock file of a node"},
-       { "setreclock",       control_setreclock,       true,   false, "Set/clear the reclock file of a node", "[filename]"},
-       { "setlmasterrole",   control_setlmasterrole,   false,  false, "Set LMASTER role to on/off", "{on|off}"},
-       { "setrecmasterrole", control_setrecmasterrole, false,  false, "Set RECMASTER role to on/off", "{on|off}"},
-       { "setdbprio",        control_setdbprio,        false,  false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
-       { "getdbprio",        control_getdbprio,        false,  false, "Get DB priority", "<dbname|dbid>"},
-       { "setdbreadonly",    control_setdbreadonly,    false,  false, "Set DB readonly capable", "<dbname|dbid>"},
-       { "setdbsticky",      control_setdbsticky,      false,  false, "Set DB sticky-records capable", "<dbname|dbid>"},
-       { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
-       { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
-       { "pfetch",          control_pfetch,            false,  false,  "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
-       { "pstore",          control_pstore,            false,  false,  "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
-       { "pdelete",         control_pdelete,           false,  false,  "delete a record from a persistent database", "<dbname|dbid> <key>" },
-       { "ptrans",          control_ptrans,            false,  false,  "update a persistent database (from stdin)", "<dbname|dbid>" },
-       { "tfetch",          control_tfetch,            false,  true,  "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
-       { "tstore",          control_tstore,            false,  true,  "store a record (including ltdb header)", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
-       { "readkey",         control_readkey,           true,   false,  "read the content off a database key", "<dbname|dbid> <key>" },
-       { "writekey",        control_writekey,          true,   false,  "write to a database key", "<dbname|dbid> <key> <value>" },
-       { "checktcpport",    control_chktcpport,        false,  true,  "check if a service is bound to a specific tcp port or not", "<port>" },
-       { "getdbseqnum",     control_getdbseqnum,       false,  false, "get the sequence number off a database", "<dbname|dbid>" },
-       { "nodestatus",      control_nodestatus,        true,   false,  "show and return node status", "[<pnn-list>]" },
-       { "dbstatistics",    control_dbstatistics,      false,  false, "show db statistics", "<dbname|dbid>" },
-       { "reloadips",       control_reloadips,         false,  false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
-       { "ipiface",         control_ipiface,           false,  true,  "Find which interface an ip address is hosted on", "<ip>" },
-};
-
-/*
-  show usage message
- */
-static void usage(void)
+static void signal_handler(int sig)
 {
-       int i;
-       printf(
-"Usage: ctdb [options] <control>\n" \
-"Options:\n" \
-"   -n <node>          choose node number, or 'all' (defaults to local node)\n"
-"   -Y                 generate machine readable output\n"
-"   -x <char>          specify delimiter for machine readable output\n"
-"   -v                 generate verbose output\n"
-"   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
-       printf("Controls:\n");
-       for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
-               printf("  %-15s %-27s  %s\n", 
-                      ctdb_commands[i].name, 
-                      ctdb_commands[i].args?ctdb_commands[i].args:"",
-                      ctdb_commands[i].msg);
-       }
-       exit(1);
+       fprintf(stderr, "Maximum runtime exceeded - exiting\n");
 }
 
-
-static void ctdb_alarm(int sig)
+static void alarm_handler(int sig)
 {
-       printf("Maximum runtime exceeded - exiting\n");
-       _exit(ERR_TIMEOUT);
+       /* Kill any child processes */
+       signal(SIGTERM, signal_handler);
+       kill(0, SIGTERM);
+
+       _exit(1);
 }
 
-/*
-  main program
-*/
 int main(int argc, const char *argv[])
 {
-       struct ctdb_context *ctdb;
-       char *nodestring = NULL;
-       int machineparsable = 0;
-       struct poptOption popt_options[] = {
-               POPT_AUTOHELP
-               POPT_CTDB_CMDLINE
-               { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
-               { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
-               { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machine readable output", NULL },
-               { NULL, 'x', POPT_ARG_STRING, &options.machineseparator, 0, "specify separator for machine readable output", "char" },
-               { NULL, 'X', POPT_ARG_NONE, &machineparsable, 0, "enable machine parsable output with separator |", NULL },
-               { "verbose",    'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
-               { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
-               { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
-               { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
-               { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
-               { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
-               { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
-               POPT_TABLEEND
-       };
        int opt;
        const char **extra_argv;
-       int extra_argc = 0;
-       int ret=-1, i;
-       poptContext pc;
-       struct tevent_context *ev;
-       const char *control;
+       int extra_argc;
+       const struct ctdb_cmd *cmd;
+       const char *ctdb_socket;
+       int loglevel;
+       int ret;
 
        setlinebuf(stdout);
-       
-       /* set some defaults */
-       options.maxruntime = 0;
-       options.timelimit = 10;
-       options.pnn = CTDB_CURRENT_NODE;
 
-       pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
-
-       while ((opt = poptGetNextOpt(pc)) != -1) {
-               switch (opt) {
-               default:
-                       DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
-                               poptBadOption(pc, 0), poptStrerror(opt)));
-                       exit(1);
-               }
-       }
+       /* Set default options */
+       options.socket = CTDB_SOCKET;
+       options.debuglevelstr = NULL;
+       options.timelimit = 10;
+       options.sep = "|";
+       options.maxruntime = 0;
+       options.pnn = -1;
 
-       /* setup the remaining options for the main program to use */
-       extra_argv = poptGetArgs(pc);
-       if (extra_argv) {
-               extra_argv++;
-               while (extra_argv[extra_argc]) extra_argc++;
+       ctdb_socket = getenv("CTDB_SOCKET");
+       if (ctdb_socket != NULL) {
+               options.socket = ctdb_socket;
        }
 
-       if (extra_argc < 1) {
-               usage();
+       pc = poptGetContext(argv[0], argc, argv, cmdline_options,
+                           POPT_CONTEXT_KEEP_FIRST);
+       while ((opt = poptGetNextOpt(pc)) != -1) {
+               fprintf(stderr, "Invalid option %s: %s\n",
+                       poptBadOption(pc, 0), poptStrerror(opt));
+               exit(1);
        }
 
        if (options.maxruntime == 0) {
                const char *ctdb_timeout;
+
                ctdb_timeout = getenv("CTDB_TIMEOUT");
                if (ctdb_timeout != NULL) {
                        options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
                } else {
-                       /* default timeout is 120 seconds */
                        options.maxruntime = 120;
                }
        }
-
-       if (machineparsable) {
-               options.machineseparator = "|";
-       }
-       if (options.machineseparator != NULL) {
-               if (strlen(options.machineseparator) != 1) {
-                       printf("Invalid separator \"%s\" - "
-                              "must be single character\n",
-                              options.machineseparator);
-                       exit(1);
-               }
-
-               /* -x implies -Y */
-               options.machinereadable = true;
-       } else if (options.machinereadable) {
-               options.machineseparator = ":";
-       }
-
-       signal(SIGALRM, ctdb_alarm);
-       alarm(options.maxruntime);
-
-       control = extra_argv[0];
-
-       /* Default value for CTDB_BASE - don't override */
-       setenv("CTDB_BASE", CTDB_ETCDIR, 0);
-
-       ev = tevent_context_init(NULL);
-       if (!ev) {
-               DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
-               exit(1);
+       if (options.maxruntime <= 120) {
+               /* default timeout is 120 seconds */
+               options.maxruntime = 120;
        }
 
-       for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
-               if (strcmp(control, ctdb_commands[i].name) == 0) {
-                       break;
-               }
+       if (options.machineparsable) {
+               options.machinereadable = 1;
        }
 
-       if (i == ARRAY_SIZE(ctdb_commands)) {
-               DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
-               exit(1);
+       /* setup the remaining options for the commands */
+       extra_argc = 0;
+       extra_argv = poptGetArgs(pc);
+       if (extra_argv) {
+               extra_argv++;
+               while (extra_argv[extra_argc]) extra_argc++;
        }
 
-       if (ctdb_commands[i].without_daemon == true) {
-               if (nodestring != NULL) {
-                       DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
-                       exit(1);
-               }
-               return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
+       if (extra_argc < 1) {
+               usage(NULL);
        }
 
-       /* initialise ctdb */
-       ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
-
-       if (ctdb == NULL) {
-               uint32_t pnn;
-               DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
-
-               pnn = find_node_xpnn();
-               if (pnn == -1) {
-                       DEBUG(DEBUG_ERR,
-                             ("Is this node part of a CTDB cluster?\n"));
-               }
+       cmd = match_command(extra_argv[0]);
+       if (cmd == NULL) {
+               fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
                exit(1);
        }
 
-       /* setup the node number(s) to contact */
-       if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
-                             &options.nodes, &options.pnn)) {
-               usage();
-       }
-
-       if (options.pnn == CTDB_CURRENT_NODE) {
-               options.pnn = options.nodes[0];
+       /* Enable logging */
+       setup_logging("ctdb", DEBUG_STDERR);
+       if (debug_level_parse(options.debuglevelstr, &loglevel)) {
+               DEBUGLEVEL = loglevel;
+       } else {
+               DEBUGLEVEL = DEBUG_ERR;
        }
 
-       if (ctdb_commands[i].auto_all && 
-           ((options.pnn == CTDB_BROADCAST_ALL) ||
-            (options.pnn == CTDB_MULTICAST))) {
-               int j;
+       signal(SIGALRM, alarm_handler);
+       alarm(options.maxruntime);
 
-               ret = 0;
-               for (j = 0; j < talloc_array_length(options.nodes); j++) {
-                       options.pnn = options.nodes[j];
-                       ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
-               }
-       } else {
-               ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
+       ret = process_command(cmd, extra_argc, extra_argv);
+       if (ret == -1) {
+               ret = 1;
        }
 
-       talloc_free(ctdb);
-       talloc_free(ev);
        (void)poptFreeContext(pc);
 
        return ret;
-
 }