Revert "change the hash function to use the much better Jenkins hash"
[metze/ctdb/wip.git] / common / ctdb_util.c
index 1b0988d3a740e0fccd38c33acbb1631d00a1d4f7..4acfa3f33e1badaa0b1108e07d6377f62436717a 100644 (file)
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.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;
@@ -73,6 +74,8 @@ int ctdb_parse_address(struct ctdb_context *ctdb,
        endservent();
        
        address->address = talloc_strdup(mem_ctx, str);
+       CTDB_NO_MEMORY(ctdb, address->address);
+
        if (se == NULL) {
                address->port = CTDB_PORT;
        } else {
@@ -120,24 +123,14 @@ static void *_idr_find_type(struct idr_context *idp, int id, const char *type, c
        return p;
 }
 
-
-/*
-  update a max latency number
- */
-void ctdb_latency(double *latency, struct timeval t)
-{
-       double l = timeval_elapsed(&t);
-       if (l > *latency) {
-               *latency = l;
-       }
-}
-
 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
 {
-       uint32_t id;
-
-       id  = ctdb->idr_cnt++ & 0xFFFF;
-       id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
+       int id = idr_get_new_above(ctdb->idr, state, ctdb->lastid+1, INT_MAX);
+       if (id < 0) {
+               DEBUG(DEBUG_NOTICE, ("Reqid wrap!\n"));
+               id = idr_get_new(ctdb->idr, state, INT_MAX);
+       }
+       ctdb->lastid = id;
        return id;
 }
 
@@ -145,9 +138,9 @@ void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *ty
 {
        void *p;
 
-       p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
+       p = _idr_find_type(ctdb->idr, reqid, type, location);
        if (p == NULL) {
-               DEBUG(DEBUG_ERR, ("Could not find idr:%u\n",reqid));
+               DEBUG(DEBUG_WARNING, ("Could not find idr:%u\n",reqid));
        }
 
        return p;
@@ -158,7 +151,7 @@ void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
 {
        int ret;
 
-       ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
+       ret = idr_remove(ctdb->idr, reqid);
        if (ret != 0) {
                DEBUG(DEBUG_ERR, ("Removing idr that does not exist\n"));
        }
@@ -200,51 +193,130 @@ struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
        return d;
 }
 
-#if HAVE_SCHED_H
-#include <sched.h>
-#endif
 
-/*
-  if possible, make this task real time
- */
-void ctdb_set_scheduler(struct ctdb_context *ctdb)
+/* helper function for marshalling multiple records */
+struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx, 
+                                              struct ctdb_marshall_buffer *m,
+                                              uint64_t db_id,
+                                              uint32_t reqid,
+                                              TDB_DATA key,
+                                              struct ctdb_ltdb_header *header,
+                                              TDB_DATA data)
 {
-#if HAVE_SCHED_SETSCHEDULER    
-       struct sched_param p;
-       if (ctdb->saved_scheduler_param == NULL) {
-               ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
+       struct ctdb_rec_data *r;
+       size_t m_size, r_size;
+       struct ctdb_marshall_buffer *m2;
+
+       r = ctdb_marshall_record(mem_ctx, reqid, key, header, data);
+       if (r == NULL) {
+               talloc_free(m);
+               return NULL;
        }
-       
-       if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
-               DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
-               return;
+
+       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;
        }
 
-       p = *(struct sched_param *)ctdb->saved_scheduler_param;
-       p.sched_priority = 1;
+       m_size = talloc_get_size(m);
+       r_size = talloc_get_size(r);
 
-       if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
-               DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
-                        strerror(errno)));
+       m2 = talloc_realloc_size(mem_ctx, m,  m_size + r_size);
+       if (m2 == NULL) {
+               talloc_free(m);
+               return NULL;
+       }
+
+       memcpy(m_size + (uint8_t *)m2, r, r_size);
+
+       talloc_free(r);
+
+       m2->count++;
+
+       return m2;
+}
+
+/* we've finished marshalling, return a data blob with the marshalled records */
+TDB_DATA ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
+{
+       TDB_DATA data;
+       data.dptr = (uint8_t *)m;
+       data.dsize = talloc_get_size(m);
+       return data;
+}
+
+/* 
+   loop over a marshalling buffer 
+   
+     - 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)
+{
+       if (r == NULL) {
+               r = (struct ctdb_rec_data *)&m->data[0];
        } else {
-               DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
+               r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
        }
-#endif
+
+       if (reqid != NULL) {
+               *reqid = r->reqid;
+       }
+       
+       if (key != NULL) {
+               key->dptr   = &r->data[0];
+               key->dsize  = r->keylen;
+       }
+       if (data != NULL) {
+               data->dptr  = &r->data[r->keylen];
+               data->dsize = r->datalen;
+               if (header != NULL) {
+                       data->dptr += sizeof(*header);
+                       data->dsize -= sizeof(*header);
+               }
+       }
+
+       if (header != NULL) {
+               if (r->datalen < sizeof(*header)) {
+                       return NULL;
+               }
+               *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
+       }
+
+       return r;
 }
 
 /*
-  restore previous scheduler parameters
+  if possible, make this task very high priority
  */
-void ctdb_restore_scheduler(struct ctdb_context *ctdb)
+void ctdb_high_priority(struct ctdb_context *ctdb)
 {
-#if HAVE_SCHED_SETSCHEDULER    
-       if (ctdb->saved_scheduler_param == NULL) {
-               ctdb_fatal(ctdb, "No saved scheduler parameters\n");
+       errno = 0;
+       if (nice(-20) == -1 && errno != 0) {
+               DEBUG(DEBUG_WARNING,("Unable to renice self: %s\n",
+                                    strerror(errno)));
+       } else {
+               DEBUG(DEBUG_NOTICE,("Scheduler says I'm nice: %i\n",
+                                   getpriority(PRIO_PROCESS, getpid())));
        }
-       if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
-               ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
+}
+
+/*
+  make ourselves slightly nicer: eg. a ctdb child.
+ */
+void ctdb_reduce_priority(struct ctdb_context *ctdb)
+{
+       errno = 0;
+       if (nice(10) == -1 && errno != 0) {
+               DEBUG(DEBUG_WARNING,("Unable to lower priority: %s\n",
+                                    strerror(errno)));
        }
-#endif
 }
 
 void set_nonblocking(int fd)
@@ -262,46 +334,12 @@ void set_close_on_exec(int fd)
 }
 
 
-/*
-  parse a ip:num pair with the given separator
- */
-static bool parse_ip_num(const char *s, struct in_addr *addr, unsigned *num, const char sep)
-{
-       const char *p;
-       char *endp = NULL;
-       char buf[16];
-
-       p = strchr(s, sep);
-       if (p == NULL) {
-               return false;
-       }
-
-       if (p - s > 15) {
-               return false;
-       }
-
-       *num = strtoul(p+1, &endp, 10);
-       if (endp == NULL || *endp != 0) {
-               /* trailing garbage */
-               return false;
-       }
-
-       strlcpy(buf, s, 1+p-s);
-
-       if (inet_aton(buf, addr) == 0) {
-               return false;
-       }
-
-       return true;
-}
-
-
-static bool parse_ipv4(const char *s, unsigned port, ctdb_sock_addr *saddr)
+bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
 {
-       saddr->ip.sin_family = AF_INET;
-       saddr->ip.sin_port   = htons(port);
+       sin->sin_family = AF_INET;
+       sin->sin_port   = htons(port);
 
-       if (inet_pton(AF_INET, s, &saddr->ip.sin_addr) != 1) {
+       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;
        }
@@ -309,7 +347,7 @@ static bool parse_ipv4(const char *s, unsigned port, ctdb_sock_addr *saddr)
        return true;
 }
 
-static bool parse_ipv6(const char *s, unsigned port, ctdb_sock_addr *saddr)
+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);
@@ -321,6 +359,16 @@ static bool parse_ipv6(const char *s, unsigned port, ctdb_sock_addr *saddr)
                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;
+               }
+               saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
+       }
+
        return true;
 }
 /*
@@ -359,12 +407,7 @@ bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
 
 
        /* now is this a ipv4 or ipv6 address ?*/
-       p = index(s, ':');
-       if (p == NULL) {
-               ret = parse_ipv4(s, port, saddr);
-       } else {
-               ret = parse_ipv6(s, port, saddr);
-       }
+       ret = parse_ip(s, NULL, port, saddr);
 
        talloc_free(tmp_ctx);
        return ret;
@@ -373,7 +416,7 @@ bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
 /*
   parse an ip
  */
-bool parse_ip(const char *addr, ctdb_sock_addr *saddr)
+bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
 {
        char *p;
        bool ret;
@@ -381,9 +424,9 @@ bool parse_ip(const char *addr, ctdb_sock_addr *saddr)
        /* now is this a ipv4 or ipv6 address ?*/
        p = index(addr, ':');
        if (p == NULL) {
-               ret = parse_ipv4(addr, 0, saddr);
+               ret = parse_ipv4(addr, port, &saddr->ip);
        } else {
-               ret = parse_ipv6(addr, 0, saddr);
+               ret = parse_ipv6(addr, ifaces, port, saddr);
        }
 
        return ret;
@@ -392,45 +435,86 @@ bool parse_ip(const char *addr, ctdb_sock_addr *saddr)
 /*
   parse a ip/mask pair
  */
-bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
+bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
 {
-       ZERO_STRUCT(*ip);
+       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+       char *s, *p;
+       char *endp = NULL;
+       bool ret;
 
-       if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
+       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;
        }
-       if (*mask > 32) {
+
+       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;
        }
-       ip->sin_family = AF_INET;
-       ip->sin_port   = 0;
-       return true;
+
+       *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;
+       }
+       *p = 0;
+
+
+       /* now is this a ipv4 or ipv6 address ?*/
+       ret = parse_ip(s, ifaces, 0, addr);
+
+       talloc_free(tmp_ctx);
+       return ret;
 }
 
 /*
-  compare two sockaddr_in structures - matching only on IP
- */
-bool ctdb_same_ipv4(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
+   This is used to canonicalize a ctdb_sock_addr structure.
+*/
+void ctdb_canonicalize_ip(const ctdb_sock_addr *ip, ctdb_sock_addr *cip)
 {
-       return ip1->sin_family == ip2->sin_family &&
-               ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
+       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));
+#ifdef HAVE_SOCK_SIN_LEN
+               cip->ip.sin_len = sizeof(*cip);
+#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_addr32[3], 4);
+       }
 }
 
-bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
+bool ctdb_same_ip(const ctdb_sock_addr *tip1, const ctdb_sock_addr *tip2)
 {
-       if (ip1->sa.sa_family != ip2->sa.sa_family) {
+       ctdb_sock_addr ip1, ip2;
+
+       ctdb_canonicalize_ip(tip1, &ip1);
+       ctdb_canonicalize_ip(tip2, &ip2);
+       
+       if (ip1.sa.sa_family != ip2.sa.sa_family) {
                return false;
        }
 
-       switch (ip1->sa.sa_family) {
+       switch (ip1.sa.sa_family) {
        case AF_INET:
-               return ip1->ip.sin_addr.s_addr == ip2->ip.sin_addr.s_addr;
+               return ip1.ip.sin_addr.s_addr == ip2.ip.sin_addr.s_addr;
        case AF_INET6:
-               return !memcmp(&ip1->ip6.sin6_addr.s6_addr[0],
-                               &ip2->ip6.sin6_addr.s6_addr[0],
+               return !memcmp(&ip1.ip6.sin6_addr.s6_addr[0],
+                               &ip2.ip6.sin6_addr.s6_addr[0],
                                16);
        default:
-               DEBUG(DEBUG_ERR, (__location__ " CRITICAL Can not compare sockaddr structures of type %u\n", ip1->sa.sa_family));
+               DEBUG(DEBUG_ERR, (__location__ " CRITICAL Can not compare sockaddr structures of type %u\n", ip1.sa.sa_family));
                return false;
        }
 
@@ -438,14 +522,46 @@ bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
 }
 
 /*
-  compare two sockaddr_in structures
+  compare two ctdb_sock_addr structures
  */
-bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
+bool ctdb_same_sockaddr(const ctdb_sock_addr *ip1, const ctdb_sock_addr *ip2)
 {
-       return ctdb_same_ipv4(ip1, ip2) && ip1->sin_port == ip2->sin_port;
+       return ctdb_same_ip(ip1, ip2) && ip1->ip.sin_port == ip2->ip.sin_port;
 }
 
+char *ctdb_addr_to_str(ctdb_sock_addr *addr)
+{
+       static char cip[128] = "";
+
+       switch (addr->sa.sa_family) {
+       case AF_INET:
+               inet_ntop(addr->ip.sin_family, &addr->ip.sin_addr, cip, sizeof(cip));
+               break;
+       case AF_INET6:
+               inet_ntop(addr->ip6.sin6_family, &addr->ip6.sin6_addr, cip, sizeof(cip));
+               break;
+       default:
+               DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family));
+       }
 
+       return cip;
+}
+
+unsigned ctdb_addr_to_port(ctdb_sock_addr *addr)
+{
+       switch (addr->sa.sa_family) {
+       case AF_INET:
+               return ntohs(addr->ip.sin_port);
+               break;
+       case AF_INET6:
+               return ntohs(addr->ip6.sin6_port);
+               break;
+       default:
+               DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family));
+       }
+
+       return 0;
+}
 
 void ctdb_block_signal(int signum)
 {
@@ -462,3 +578,79 @@ void ctdb_unblock_signal(int signum)
        sigaddset(&set,signum);
        sigprocmask(SIG_UNBLOCK,&set,NULL);
 }
+
+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}
+};
+
+const char *get_debug_by_level(int32_t level)
+{
+       int i;
+
+       for (i=0; debug_levels[i].description != NULL; i++) {
+               if (debug_levels[i].level == level) {
+                       return debug_levels[i].description;
+               }
+       }
+       return "Unknown";
+}
+
+int32_t get_debug_by_desc(const char *desc)
+{
+       int i;
+
+       for (i=0; debug_levels[i].description != NULL; i++) {
+               if (!strcmp(debug_levels[i].description, desc)) {
+                       return debug_levels[i].level;
+               }
+       }
+
+       return DEBUG_ERR;
+}
+
+/* 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)
+{
+#ifdef HAVE_MLOCKALL
+       /* Extra stack, please! */
+       char dummy[10000];
+       memset(dummy, 0, sizeof(dummy));
+
+       if (ctdb->valgrinding) {
+               return;
+       }
+
+       /* Avoid compiler optimizing out dummy. */
+       mlock(dummy, sizeof(dummy));
+       if (mlockall(MCL_CURRENT) != 0) {
+               DEBUG(DEBUG_WARNING,("Failed to lock memory: %s'\n",
+                                    strerror(errno)));
+       }
+#endif
+}
+
+const char *ctdb_eventscript_call_names[] = {
+       "init",
+       "setup",
+       "startup",
+       "startrecovery",
+       "recovered",
+       "takeip",
+       "releaseip",
+       "stopped",
+       "monitor",
+       "status",
+       "shutdown",
+       "reload",
+       "updateip",
+       "ipreallocated"
+};