ctdb-scripts: Update statd-callout to try several configuration files
[vlendec/samba-autobuild/.git] / ctdb / common / ctdb_util.c
index 0274a761c71e5850531d87fa1cc50e7cbd42168d..0f367c2100ed6e3f2c0988122ee141359d69dbc2 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "includes.h"
-#include "tdb.h"
+#include "replace.h"
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/wait.h"
-#include "system/shmem.h"
-#include "../include/ctdb_private.h"
 
-int LogLevel = DEBUG_NOTICE;
-int this_log_level = 0;
+#include <tdb.h>
+
+#include "lib/util/debug.h"
+#include "lib/util/samba_util.h"
+
+#include "ctdb_private.h"
+
+#include "protocol/protocol_util.h"
+
+#include "common/reqid.h"
+#include "common/system.h"
+#include "common/common.h"
+#include "common/logging.h"
 
 /*
   return error string for last error
@@ -68,51 +76,92 @@ void ctdb_die(struct ctdb_context *ctdb, const char *msg)
        exit(1);
 }
 
-/* Invoke an external program to do some sort of tracing on the CTDB
- * process.  This might block for a little while.  The external
- * program is specified by the environment variable
- * CTDB_EXTERNAL_TRACE.  This program should take one argument: the
- * pid of the process to trace.  Commonly, the program would be a
- * wrapper script around gcore.
- */
-void ctdb_external_trace(void)
+/* Set the path of a helper program from envvar, falling back to
+ * dir/file if envvar unset. type is a string to print in log
+ * messages.  helper is assumed to point to a statically allocated
+ * array of size bytes, initialised to "".  If file is NULL don't fall
+ * back if envvar is unset.  If dir is NULL and envvar is unset (but
+ * file is not NULL) then this is an error.  Returns true if helper is
+ * set, either previously or this time. */
+bool ctdb_set_helper(const char *type, char *helper, size_t size,
+                    const char *envvar,
+                    const char *dir, const char *file)
 {
+       const char *t;
+       struct stat st;
 
-       const char * t = getenv("CTDB_EXTERNAL_TRACE");
-       char * cmd;
+       if (helper[0] != '\0') {
+               /* Already set */
+               return true;
+       }
 
-       if (t == NULL) {
-               return;
+       t = getenv(envvar);
+       if (t != NULL) {
+               if (strlen(t) >= size) {
+                       DEBUG(DEBUG_ERR,
+                             ("Unable to set %s - path too long\n", type));
+                       return false;
+               }
+
+               strncpy(helper, t, size);
+       } else if (file == NULL) {
+               return false;
+       } else if (dir == NULL) {
+                       DEBUG(DEBUG_ERR,
+                             ("Unable to set %s - dir is NULL\n", type));
+               return false;
+       } else {
+               if (snprintf(helper, size, "%s/%s", dir, file) >= size) {
+                       DEBUG(DEBUG_ERR,
+                             ("Unable to set %s - path too long\n", type));
+                       return false;
+               }
        }
 
-       cmd = talloc_asprintf(NULL, "%s %lu", t, (unsigned long) getpid());
-       DEBUG(DEBUG_WARNING,("begin external trace: %s\n", cmd));
-       system(cmd);
-       DEBUG(DEBUG_WARNING,("end external trace: %s\n", cmd));
-       talloc_free(cmd);
+       if (stat(helper, &st) != 0) {
+               DEBUG(DEBUG_ERR,
+                     ("Unable to set %s \"%s\" - %s\n",
+                      type, helper, strerror(errno)));
+               return false;
+       }
+       if (!(st.st_mode & S_IXUSR)) {
+               DEBUG(DEBUG_ERR,
+                     ("Unable to set %s \"%s\" - not executable\n",
+                      type, helper));
+               return false;
+       }
+
+       DEBUG(DEBUG_NOTICE,
+             ("Set %s to \"%s\"\n", type, helper));
+       return true;
 }
 
 /*
   parse a IP:port pair
 */
-int ctdb_parse_address(struct ctdb_context *ctdb,
-                      TALLOC_CTX *mem_ctx, const char *str,
-                      struct ctdb_address *address)
+int ctdb_parse_address(TALLOC_CTX *mem_ctx, const char *str,
+                      ctdb_sock_addr *address)
 {
        struct servent *se;
+       int port;
+       int ret;
 
        setservent(0);
        se = getservbyname("ctdb", "tcp");
        endservent();
-       
-       address->address = talloc_strdup(mem_ctx, str);
-       CTDB_NO_MEMORY(ctdb, address->address);
 
        if (se == NULL) {
-               address->port = CTDB_PORT;
+               port = CTDB_PORT;
        } else {
-               address->port = ntohs(se->s_port);
+               port = ntohs(se->s_port);
        }
+
+       ret = ctdb_sock_addr_from_string(str, address, false);
+       if (ret != 0) {
+               return -1;
+       }
+       ctdb_sock_addr_set_port(address, port);
+
        return 0;
 }
 
@@ -120,9 +169,10 @@ int ctdb_parse_address(struct ctdb_context *ctdb,
 /*
   check if two addresses are the same
 */
-bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
+bool ctdb_same_address(ctdb_sock_addr *a1, ctdb_sock_addr *a2)
 {
-       return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
+       return ctdb_same_ip(a1, a2) &&
+               ctdb_addr_to_port(a1) == ctdb_addr_to_port(a2);
 }
 
 
@@ -134,131 +184,100 @@ uint32_t ctdb_hash(const TDB_DATA *key)
        return tdb_jenkins_hash(discard_const(key));
 }
 
-/*
-  a type checking varient of idr_find
- */
-static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
-{
-       void *p = idr_find(idp, id);
-       if (p && talloc_check_name(p, type) == NULL) {
-               DEBUG(DEBUG_ERR,("%s idr_find_type expected type %s  but got %s\n",
-                        location, type, talloc_get_name(p)));
-               return NULL;
-       }
-       return p;
-}
 
-uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
+static uint32_t ctdb_marshall_record_size(TDB_DATA key,
+                                         struct ctdb_ltdb_header *header,
+                                         TDB_DATA data)
 {
-       int id = idr_get_new_above(ctdb->idr, state, ctdb->lastid+1, INT_MAX);
-       if (id < 0) {
-               DEBUG(DEBUG_DEBUG, ("Reqid wrap!\n"));
-               id = idr_get_new(ctdb->idr, state, INT_MAX);
-       }
-       ctdb->lastid = id;
-       return id;
+       return offsetof(struct ctdb_rec_data_old, data) + key.dsize +
+              data.dsize + (header ? sizeof(*header) : 0);
 }
 
-void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
+static void ctdb_marshall_record_copy(struct ctdb_rec_data_old *rec,
+                                     uint32_t reqid,
+                                     TDB_DATA key,
+                                     struct ctdb_ltdb_header *header,
+                                     TDB_DATA data,
+                                     uint32_t length)
 {
-       void *p;
-
-       p = _idr_find_type(ctdb->idr, reqid, type, location);
-       if (p == NULL) {
-               DEBUG(DEBUG_WARNING, ("Could not find idr:%u\n",reqid));
-       }
-
-       return p;
-}
+       uint32_t offset;
 
+       rec->length = length;
+       rec->reqid = reqid;
+       rec->keylen = key.dsize;
+       memcpy(&rec->data[0], key.dptr, key.dsize);
+       offset = key.dsize;
 
-void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
-{
-       int ret;
-
-       ret = idr_remove(ctdb->idr, reqid);
-       if (ret != 0) {
-               DEBUG(DEBUG_ERR, ("Removing idr that does not exist\n"));
+       if (header) {
+               rec->datalen = data.dsize + sizeof(*header);
+               memcpy(&rec->data[offset], header, sizeof(*header));
+               offset += sizeof(*header);
+       } else {
+               rec->datalen = data.dsize;
        }
+       memcpy(&rec->data[offset], data.dptr, data.dsize);
 }
 
-
 /*
   form a ctdb_rec_data record from a key/data pair
   
   note that header may be NULL. If not NULL then it is included in the data portion
   of the record
  */
-struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,        
-                                          TDB_DATA key, 
-                                          struct ctdb_ltdb_header *header,
-                                          TDB_DATA data)
+struct ctdb_rec_data_old *ctdb_marshall_record(TALLOC_CTX *mem_ctx,
+                                              uint32_t reqid,
+                                              TDB_DATA key,
+                                              struct ctdb_ltdb_header *header,
+                                              TDB_DATA data)
 {
        size_t length;
-       struct ctdb_rec_data *d;
+       struct ctdb_rec_data_old *d;
 
-       length = offsetof(struct ctdb_rec_data, data) + key.dsize + 
-               data.dsize + (header?sizeof(*header):0);
-       d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
+       length = ctdb_marshall_record_size(key, header, data);
+
+       d = (struct ctdb_rec_data_old *)talloc_size(mem_ctx, length);
        if (d == NULL) {
                return NULL;
        }
-       d->length = length;
-       d->reqid = reqid;
-       d->keylen = key.dsize;
-       memcpy(&d->data[0], key.dptr, key.dsize);
-       if (header) {
-               d->datalen = data.dsize + sizeof(*header);
-               memcpy(&d->data[key.dsize], header, sizeof(*header));
-               memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
-       } else {
-               d->datalen = data.dsize;
-               memcpy(&d->data[key.dsize], data.dptr, data.dsize);
-       }
+
+       ctdb_marshall_record_copy(d, reqid, key, header, data, length);
        return d;
 }
 
 
 /* helper function for marshalling multiple records */
-struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx, 
+struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx,
                                               struct ctdb_marshall_buffer *m,
-                                              uint64_t db_id,
+                                              uint32_t db_id,
                                               uint32_t reqid,
                                               TDB_DATA key,
                                               struct ctdb_ltdb_header *header,
                                               TDB_DATA data)
 {
-       struct ctdb_rec_data *r;
-       size_t m_size, r_size;
+       struct ctdb_rec_data_old *r;
        struct ctdb_marshall_buffer *m2;
+       uint32_t length, offset;
 
-       r = ctdb_marshall_record(mem_ctx, reqid, key, header, data);
-       if (r == NULL) {
-               talloc_free(m);
-               return NULL;
-       }
+       length = ctdb_marshall_record_size(key, header, data);
 
        if (m == NULL) {
-               m = talloc_zero_size(mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
-               if (m == NULL) {
-                       return NULL;
-               }
-               m->db_id = db_id;
+               offset = offsetof(struct ctdb_marshall_buffer, data);
+               m2 = talloc_zero_size(mem_ctx, offset + length);
+       } else {
+               offset = talloc_get_size(m);
+               m2 = talloc_realloc_size(mem_ctx, m, offset + length);
        }
-
-       m_size = talloc_get_size(m);
-       r_size = talloc_get_size(r);
-
-       m2 = talloc_realloc_size(mem_ctx, m,  m_size + r_size);
        if (m2 == NULL) {
-               talloc_free(m);
+               TALLOC_FREE(m);
                return NULL;
        }
 
-       memcpy(m_size + (uint8_t *)m2, r, r_size);
-
-       talloc_free(r);
+       if (m == NULL) {
+               m2->db_id = db_id;
+       }
 
+       r = (struct ctdb_rec_data_old *)((uint8_t *)m2 + offset);
+       ctdb_marshall_record_copy(r, reqid, key, header, data, length);
        m2->count++;
 
        return m2;
@@ -279,15 +298,17 @@ TDB_DATA ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
      - pass r==NULL to start
      - loop the number of times indicated by m->count
 */
-struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
-                                             uint32_t *reqid,
-                                             struct ctdb_ltdb_header *header,
-                                             TDB_DATA *key, TDB_DATA *data)
+struct ctdb_rec_data_old *ctdb_marshall_loop_next(
+                               struct ctdb_marshall_buffer *m,
+                               struct ctdb_rec_data_old *r,
+                               uint32_t *reqid,
+                               struct ctdb_ltdb_header *header,
+                               TDB_DATA *key, TDB_DATA *data)
 {
        if (r == NULL) {
-               r = (struct ctdb_rec_data *)&m->data[0];
+               r = (struct ctdb_rec_data_old *)&m->data[0];
        } else {
-               r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
+               r = (struct ctdb_rec_data_old *)(r->length + (uint8_t *)r);
        }
 
        if (reqid != NULL) {
@@ -311,233 +332,56 @@ struct ctdb_rec_data *ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, st
                if (r->datalen < sizeof(*header)) {
                        return NULL;
                }
-               *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
+               memcpy(header, &r->data[r->keylen], sizeof(*header));
        }
 
        return r;
 }
 
-
-#if HAVE_SCHED_H
-#include <sched.h>
-#endif
-
 /*
-  if possible, make this task real time
- */
-void ctdb_set_scheduler(struct ctdb_context *ctdb)
+   This is used to canonicalize a ctdb_sock_addr structure.
+*/
+void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
 {
-#if HAVE_SCHED_SETSCHEDULER    
-       struct sched_param p;
-       if (ctdb->saved_scheduler_param == NULL) {
-               ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
-       }
-       
-       if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
-               DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
-               return;
-       }
+       ZERO_STRUCTP(cip);
 
-       p = *(struct sched_param *)ctdb->saved_scheduler_param;
-       p.sched_priority = 1;
-
-       if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
-               DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
-                        strerror(errno)));
-       } else {
-               DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
-       }
+       if (ip->sa.sa_family == AF_INET6) {
+               const char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
+               if (memcmp(&ip->ip6.sin6_addr, prefix, sizeof(prefix)) == 0) {
+                       /* Copy IPv4-mapped IPv6 addresses as IPv4 */
+                       cip->ip.sin_family = AF_INET;
+#ifdef HAVE_SOCK_SIN_LEN
+                       cip->ip.sin_len = sizeof(ctdb_sock_addr);
 #endif
-}
-
-/*
-  restore previous scheduler parameters
- */
-void ctdb_restore_scheduler(struct ctdb_context *ctdb)
-{
-#if HAVE_SCHED_SETSCHEDULER    
-       if (ctdb->saved_scheduler_param == NULL) {
-               ctdb_fatal(ctdb, "No saved scheduler parameters\n");
-       }
-       if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
-               ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
-       }
+                       cip->ip.sin_port   = ip->ip6.sin6_port;
+                       memcpy(&cip->ip.sin_addr,
+                              &ip->ip6.sin6_addr.s6_addr[12],
+                              sizeof(cip->ip.sin_addr));
+               } else {
+                       cip->ip6.sin6_family = AF_INET6;
+#ifdef HAVE_SOCK_SIN6_LEN
+                       cip->ip6.sin6_len = sizeof(ctdb_sock_addr);
 #endif
-}
-
-void set_nonblocking(int fd)
-{
-       unsigned v;
-       v = fcntl(fd, F_GETFL, 0);
-        fcntl(fd, F_SETFL, v | O_NONBLOCK);
-}
-
-void set_close_on_exec(int fd)
-{
-       unsigned v;
-       v = fcntl(fd, F_GETFD, 0);
-       fcntl(fd, F_SETFD, v | FD_CLOEXEC);
-}
-
-
-bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
-{
-       sin->sin_family = AF_INET;
-       sin->sin_port   = htons(port);
-
-       if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
-               DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
-               return false;
-       }
-
-       return true;
-}
-
-static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
-{
-       saddr->ip6.sin6_family   = AF_INET6;
-       saddr->ip6.sin6_port     = htons(port);
-       saddr->ip6.sin6_flowinfo = 0;
-       saddr->ip6.sin6_scope_id = 0;
-
-       if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
-               DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
-               return false;
-       }
-
-       if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
-               if (strchr(ifaces, ',')) {
-                       DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
-                                         "is specified for multiple ifaces %s\n",
-                                         s, ifaces));
-                       return false;
+                       cip->ip6.sin6_port   = ip->ip6.sin6_port;
+                       memcpy(&cip->ip6.sin6_addr,
+                              &ip->ip6.sin6_addr,
+                              sizeof(cip->ip6.sin6_addr));
                }
-               saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
-       }
-
-       return true;
-}
-/*
-  parse a ip:port pair
- */
-bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       char *s, *p;
-       unsigned port;
-       char *endp = NULL;
-       bool ret;
-
-       s = talloc_strdup(tmp_ctx, addr);
-       if (s == NULL) {
-               DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
-               talloc_free(tmp_ctx);
-               return false;
-       }
-
-       p = rindex(s, ':');
-       if (p == NULL) {
-               DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
-               talloc_free(tmp_ctx);
-               return false;
-       }
-
-       port = strtoul(p+1, &endp, 10);
-       if (endp == NULL || *endp != 0) {
-               /* trailing garbage */
-               DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
-               talloc_free(tmp_ctx);
-               return false;
-       }
-       *p = 0;
-
-
-       /* now is this a ipv4 or ipv6 address ?*/
-       ret = parse_ip(s, NULL, port, saddr);
-
-       talloc_free(tmp_ctx);
-       return ret;
-}
-
-/*
-  parse an ip
- */
-bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
-{
-       char *p;
-       bool ret;
-
-       /* now is this a ipv4 or ipv6 address ?*/
-       p = index(addr, ':');
-       if (p == NULL) {
-               ret = parse_ipv4(addr, port, &saddr->ip);
-       } else {
-               ret = parse_ipv6(addr, ifaces, port, saddr);
-       }
 
-       return ret;
-}
-
-/*
-  parse a ip/mask pair
- */
-bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
-{
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       char *s, *p;
-       char *endp = NULL;
-       bool ret;
-
-       ZERO_STRUCT(*addr);
-       s = talloc_strdup(tmp_ctx, str);
-       if (s == NULL) {
-               DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
-               talloc_free(tmp_ctx);
-               return false;
-       }
-
-       p = rindex(s, '/');
-       if (p == NULL) {
-               DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
-               talloc_free(tmp_ctx);
-               return false;
-       }
-
-       *mask = strtoul(p+1, &endp, 10);
-       if (endp == NULL || *endp != 0) {
-               /* trailing garbage */
-               DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
-               talloc_free(tmp_ctx);
-               return false;
+               return;
        }
-       *p = 0;
 
-
-       /* now is this a ipv4 or ipv6 address ?*/
-       ret = parse_ip(s, ifaces, 0, addr);
-
-       talloc_free(tmp_ctx);
-       return ret;
-}
-
-/*
-   This is used to canonicalize a ctdb_sock_addr structure.
-*/
-void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
-{
-       char prefix[12] = { 0,0,0,0,0,0,0,0,0,0,0xff,0xff };
-
-       memcpy(cip, ip, sizeof (*cip));
-
-       if ( (ip->sa.sa_family == AF_INET6)
-       && !memcmp(&ip->ip6.sin6_addr, prefix, 12)) {
-               memset(cip, 0, sizeof(*cip));
+       if (ip->sa.sa_family == AF_INET) {
+               cip->ip.sin_family = AF_INET;
 #ifdef HAVE_SOCK_SIN_LEN
-               cip->ip.sin_len = sizeof(*cip);
+               cip->ip.sin_len = sizeof(ctdb_sock_addr);
 #endif
-               cip->ip.sin_family = AF_INET;
-               cip->ip.sin_port   = ip->ip6.sin6_port;
-               memcpy(&cip->ip.sin_addr, &ip->ip6.sin6_addr.s6_addr[12], 4);
+               cip->ip.sin_port = ip->ip.sin_port;
+               memcpy(&cip->ip.sin_addr,
+                      &ip->ip.sin_addr,
+                      sizeof(ip->ip.sin_addr));
+
+               return;
        }
 }
 
@@ -588,7 +432,6 @@ char *ctdb_addr_to_str(ctdb_sock_addr *addr)
                break;
        default:
                DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family));
-               ctdb_external_trace();
        }
 
        return cip;
@@ -610,89 +453,135 @@ unsigned ctdb_addr_to_port(ctdb_sock_addr *addr)
        return 0;
 }
 
-void ctdb_block_signal(int signum)
+/* Add a node to a node map with given address and flags */
+static bool node_map_add(TALLOC_CTX *mem_ctx,
+                        const char *nstr, uint32_t flags,
+                        struct ctdb_node_map_old **node_map)
 {
-       sigset_t set;
-       sigemptyset(&set);
-       sigaddset(&set,signum);
-       sigprocmask(SIG_BLOCK,&set,NULL);
-}
+       ctdb_sock_addr addr;
+       uint32_t num;
+       size_t s;
+       struct ctdb_node_and_flags *n;
 
-void ctdb_unblock_signal(int signum)
-{
-       sigset_t set;
-       sigemptyset(&set);
-       sigaddset(&set,signum);
-       sigprocmask(SIG_UNBLOCK,&set,NULL);
-}
+       /* Might as well do this before trying to allocate memory */
+       if (ctdb_parse_address(mem_ctx, nstr, &addr) == -1) {
+               return false;
+       }
 
-struct debug_levels debug_levels[] = {
-       {DEBUG_EMERG,   "EMERG"},
-       {DEBUG_ALERT,   "ALERT"},
-       {DEBUG_CRIT,    "CRIT"},
-       {DEBUG_ERR,     "ERR"},
-       {DEBUG_WARNING, "WARNING"},
-       {DEBUG_NOTICE,  "NOTICE"},
-       {DEBUG_INFO,    "INFO"},
-       {DEBUG_DEBUG,   "DEBUG"},
-       {0, NULL}
-};
+       num = (*node_map)->num + 1;
+       s = offsetof(struct ctdb_node_map_old, nodes) +
+               num * sizeof(struct ctdb_node_and_flags);
+       *node_map = talloc_realloc_size(mem_ctx, *node_map, s);
+       if (*node_map == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
+               return false;
+       }
 
-const char *get_debug_by_level(int32_t level)
-{
-       int i;
+       n = &(*node_map)->nodes[(*node_map)->num];
+       n->addr = addr;
+       n->pnn = (*node_map)->num;
+       n->flags = flags;
 
-       for (i=0; debug_levels[i].description != NULL; i++) {
-               if (debug_levels[i].level == level) {
-                       return debug_levels[i].description;
-               }
-       }
-       return "Unknown";
+       (*node_map)->num++;
+
+       return true;
 }
 
-int32_t get_debug_by_desc(const char *desc)
+/* Read a nodes file into a node map */
+struct ctdb_node_map_old *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
+                                          const char *nlist)
 {
+       char **lines;
+       int nlines;
        int i;
+       struct ctdb_node_map_old *ret;
+
+       /* Allocate node map header */
+       ret = talloc_zero_size(mem_ctx, offsetof(struct ctdb_node_map_old, nodes));
+       if (ret == NULL) {
+               DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
+               return false;
+       }
+
+       lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
+       if (lines == NULL) {
+               DEBUG(DEBUG_ERR, ("Failed to read nodes file \"%s\"\n", nlist));
+               return false;
+       }
+       while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
+               nlines--;
+       }
+
+       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++;
+               }
 
-       for (i=0; debug_levels[i].description != NULL; i++) {
-               if (!strcmp(debug_levels[i].description, desc)) {
-                       return debug_levels[i].level;
+               len = strlen(node);
+
+               while ((len > 1) &&
+                      ((node[len-1] == ' ') || (node[len-1] == '\t')))
+               {
+                       node[len-1] = '\0';
+                       len--;
+               }
+
+               if (len == 0) {
+                       continue;
+               }
+               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(mem_ctx, node, flags, &ret)) {
+                       talloc_free(lines);
+                       TALLOC_FREE(ret);
+                       return NULL;
                }
        }
 
-       return DEBUG_ERR;
+       talloc_free(lines);
+       return ret;
 }
 
-/* we don't lock future pages here; it would increase the chance that
- * we'd fail to mmap later on. */
-void ctdb_lockdown_memory(struct ctdb_context *ctdb)
+struct ctdb_node_map_old *
+ctdb_node_list_to_map(struct ctdb_node **nodes, uint32_t num_nodes,
+                     TALLOC_CTX *mem_ctx)
 {
-#ifdef HAVE_MLOCKALL
-       /* Extra stack, please! */
-       char dummy[10000];
-       memset(dummy, 0, sizeof(dummy));
+       uint32_t i;
+       size_t size;
+       struct ctdb_node_map_old *node_map;
 
-       if (ctdb->valgrinding) {
-               return;
+       size = offsetof(struct ctdb_node_map_old, nodes) +
+               num_nodes * sizeof(struct ctdb_node_and_flags);
+       node_map  = (struct ctdb_node_map_old *)talloc_zero_size(mem_ctx, size);
+       if (node_map == NULL) {
+               DEBUG(DEBUG_ERR,
+                     (__location__ " Failed to allocate nodemap array\n"));
+               return NULL;
        }
 
-       /* TODO: Add a command line option to disable memory lockdown.
-        *       This can be a performance issue on AIX since fork() copies
-        *       all locked memory pages. 
-        */
-
-       /* Ignore when running in local daemons mode */
-       if (getuid() != 0) {
-               return;
+       node_map->num = num_nodes;
+       for (i=0; i<num_nodes; i++) {
+               node_map->nodes[i].addr  = nodes[i]->address;
+               node_map->nodes[i].pnn   = nodes[i]->pnn;
+               node_map->nodes[i].flags = nodes[i]->flags;
        }
 
-       /* Avoid compiler optimizing out dummy. */
-       mlock(dummy, sizeof(dummy));
-       if (mlockall(MCL_CURRENT) != 0) {
-               DEBUG(DEBUG_WARNING,("Failed to lockdown memory: %s'\n",
-                                    strerror(errno)));
-       }
-#endif
+       return node_map;
 }
 
 const char *ctdb_eventscript_call_names[] = {
@@ -753,11 +642,30 @@ enum ctdb_runstate runstate_from_string(const char *label)
 
 void ctdb_set_runstate(struct ctdb_context *ctdb, enum ctdb_runstate runstate)
 {
+       DEBUG(DEBUG_NOTICE,("Set runstate to %s (%d)\n",
+                           runstate_to_string(runstate), runstate));
+
        if (runstate <= ctdb->runstate) {
                ctdb_fatal(ctdb, "runstate must always increase");
        }
 
-       DEBUG(DEBUG_NOTICE,("Set runstate to %s (%d)\n",
-                           runstate_to_string(runstate), runstate));
        ctdb->runstate = runstate;
 }
+
+/* Convert arbitrary data to 4-byte boundary padded uint32 array */
+uint32_t *ctdb_key_to_idkey(TALLOC_CTX *mem_ctx, TDB_DATA key)
+{
+       uint32_t idkey_size, *k;
+
+       idkey_size = 1 + (key.dsize + sizeof(uint32_t)-1) / sizeof(uint32_t);
+
+       k = talloc_zero_array(mem_ctx, uint32_t, idkey_size);
+       if (k == NULL) {
+               return NULL;
+       }
+
+       k[0] = idkey_size;
+       memcpy(&k[1], key.dptr, key.dsize);
+
+       return k;
+}