r23795: more v2->v3 conversion
[metze/samba/wip.git] / source4 / cluster / ctdb / common / ctdb_util.c
1 /* 
2    ctdb utility code
3
4    Copyright (C) Andrew Tridgell  2006
5
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.
10
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.
15
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
19 */
20
21 #include "includes.h"
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"
27
28 int LogLevel;
29
30 /*
31   return error string for last error
32 */
33 const char *ctdb_errstr(struct ctdb_context *ctdb)
34 {
35         return ctdb->err_msg;
36 }
37
38
39 /*
40   remember an error message
41 */
42 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
43 {
44         va_list ap;
45         talloc_free(ctdb->err_msg);
46         va_start(ap, fmt);
47         ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
48         DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
49         va_end(ap);
50 }
51
52 /*
53   a fatal internal error occurred - no hope for recovery
54 */
55 _NORETURN_ void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
56 {
57         DEBUG(0,("ctdb fatal error: %s\n", msg));
58         fprintf(stderr, "ctdb fatal error: '%s'\n", msg);
59         abort();
60 }
61
62 /*
63   parse a IP:port pair
64 */
65 int ctdb_parse_address(struct ctdb_context *ctdb,
66                        TALLOC_CTX *mem_ctx, const char *str,
67                        struct ctdb_address *address)
68 {
69         char *p;
70         p = strchr(str, ':');
71         if (p == NULL) {
72                 ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
73                 return -1;
74         }
75         
76         address->address = talloc_strndup(mem_ctx, str, p-str);
77         address->port = strtoul(p+1, NULL, 0);
78         return 0;
79 }
80
81
82 /*
83   check if two addresses are the same
84 */
85 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
86 {
87         return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
88 }
89
90
91 /*
92   hash function for mapping data to a VNN - taken from tdb
93 */
94 uint32_t ctdb_hash(const TDB_DATA *key)
95 {
96         uint32_t value; /* Used to compute the hash value.  */
97         uint32_t i;     /* Used to cycle through random values. */
98
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)));
102
103         return (1103515243 * value + 12345);  
104 }
105
106 /*
107   a type checking varient of idr_find
108  */
109 void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
110 {
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)));
115                 return NULL;
116         }
117         return p;
118 }
119
120
121 /*
122   update a max latency number
123  */
124 void ctdb_latency(double *latency, struct timeval t)
125 {
126         double l = timeval_elapsed(&t);
127         if (l > *latency) {
128                 *latency = l;
129         }
130 }