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, see <http://www.gnu.org/licenses/>.
21 #include "lib/events/events.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
30 return error string for last error
32 const char *ctdb_errstr(struct ctdb_context *ctdb)
39 remember an error message
41 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
44 talloc_free(ctdb->err_msg);
46 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
47 DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
52 a fatal internal error occurred - no hope for recovery
54 _NORETURN_ void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
56 DEBUG(0,("ctdb fatal error: %s\n", msg));
57 fprintf(stderr, "ctdb fatal error: '%s'\n", msg);
64 int ctdb_parse_address(struct ctdb_context *ctdb,
65 TALLOC_CTX *mem_ctx, const char *str,
66 struct ctdb_address *address)
71 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
75 address->address = talloc_strndup(mem_ctx, str, p-str);
76 address->port = strtoul(p+1, NULL, 0);
82 check if two addresses are the same
84 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
86 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
91 hash function for mapping data to a VNN - taken from tdb
93 uint32_t ctdb_hash(const TDB_DATA *key)
95 uint32_t value; /* Used to compute the hash value. */
96 uint32_t i; /* Used to cycle through random values. */
98 /* Set the initial value from the key size. */
99 for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
100 value = (value + (key->dptr[i] << (i*5 % 24)));
102 return (1103515243 * value + 12345);
106 a type checking varient of idr_find
108 void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
110 void *p = idr_find(idp, id);
111 if (p && talloc_check_name(p, type) == NULL) {
112 DEBUG(0,("%s idr_find_type expected type %s but got %s\n",
113 location, type, talloc_get_name(p)));
121 update a max latency number
123 void ctdb_latency(double *latency, struct timeval t)
125 double l = timeval_elapsed(&t);