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