4 Copyright (C) Andrew Tridgell 2006
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "lib/events/events.h"
23 #include "lib/tdb/include/tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
31 return error string for last error
33 const char *ctdb_errstr(struct ctdb_context *ctdb)
40 remember an error message
42 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
45 talloc_free(ctdb->err_msg);
47 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
48 DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
53 a fatal internal error occurred - no hope for recovery
55 _NORETURN_ void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
57 DEBUG(0,("ctdb fatal error: %s\n", msg));
58 fprintf(stderr, "ctdb fatal error: '%s'\n", msg);
65 int ctdb_parse_address(struct ctdb_context *ctdb,
66 TALLOC_CTX *mem_ctx, const char *str,
67 struct ctdb_address *address)
72 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
76 address->address = talloc_strndup(mem_ctx, str, p-str);
77 address->port = strtoul(p+1, NULL, 0);
83 check if two addresses are the same
85 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
87 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
92 hash function for mapping data to a VNN - taken from tdb
94 uint32_t ctdb_hash(const TDB_DATA *key)
96 uint32_t value; /* Used to compute the hash value. */
97 uint32_t i; /* Used to cycle through random values. */
99 /* Set the initial value from the key size. */
100 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
101 value = (value + (key->dptr[i] << (i*5 % 24)));
103 return (1103515243 * value + 12345);
107 a type checking varient of idr_find
109 void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
111 void *p = idr_find(idp, id);
112 if (p && talloc_check_name(p, type) == NULL) {
113 DEBUG(0,("%s idr_find_type expected type %s but got %s\n",
114 location, type, talloc_get_name(p)));
122 update a max latency number
124 void ctdb_latency(double *latency, struct timeval t)
126 double l = timeval_elapsed(&t);