use /etc/services for ctdb
[metze/ctdb/wip.git] / 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 2 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 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
56 {
57         DEBUG(0,("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         struct servent *se;
69
70         setservent(0);
71         se = getservbyname("ctdb", "tcp");
72         endservent();
73         
74         address->address = talloc_strdup(mem_ctx, str);
75         if (se == NULL) {
76                 address->port = CTDB_PORT;
77         } else {
78                 address->port = ntohs(se->s_port);
79         }
80         return 0;
81 }
82
83
84 /*
85   check if two addresses are the same
86 */
87 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
88 {
89         return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
90 }
91
92
93 /*
94   hash function for mapping data to a VNN - taken from tdb
95 */
96 uint32_t ctdb_hash(const TDB_DATA *key)
97 {
98         uint32_t value; /* Used to compute the hash value.  */
99         uint32_t i;     /* Used to cycle through random values. */
100
101         /* Set the initial value from the key size. */
102         for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
103                 value = (value + (key->dptr[i] << (i*5 % 24)));
104
105         return (1103515243 * value + 12345);  
106 }
107
108 /*
109   hash function for a string
110 */
111 uint32_t ctdb_hash_string(const char *str)
112 {
113         TDB_DATA data;
114         data.dptr = (uint8_t *)discard_const(str);
115         data.dsize = strlen(str)+1;
116         return ctdb_hash(&data);
117 }
118
119 /*
120   a type checking varient of idr_find
121  */
122 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
123 {
124         void *p = idr_find(idp, id);
125         if (p && talloc_check_name(p, type) == NULL) {
126                 DEBUG(0,("%s idr_find_type expected type %s  but got %s\n",
127                          location, type, talloc_get_name(p)));
128                 return NULL;
129         }
130         return p;
131 }
132
133
134 /*
135   update a max latency number
136  */
137 void ctdb_latency(double *latency, struct timeval t)
138 {
139         double l = timeval_elapsed(&t);
140         if (l > *latency) {
141                 *latency = l;
142         }
143 }
144
145 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
146 {
147         uint32_t id;
148
149         id  = ctdb->idr_cnt++ & 0xFFFF;
150         id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
151         return id;
152 }
153
154 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
155 {
156         void *p;
157
158         p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
159         if (p == NULL) {
160                 DEBUG(0, ("Could not find idr:%u\n",reqid));
161         }
162
163         return p;
164 }
165
166
167 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
168 {
169         int ret;
170
171         ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
172         if (ret != 0) {
173                 DEBUG(0, ("Removing idr that does not exist\n"));
174         }
175 }
176
177
178 /*
179   form a ctdb_rec_data record from a key/data pair
180  */
181 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data)
182 {
183         size_t length;
184         struct ctdb_rec_data *d;
185
186         length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
187         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
188         if (d == NULL) {
189                 return NULL;
190         }
191         d->length = length;
192         d->reqid = reqid;
193         d->keylen = key.dsize;
194         d->datalen = data.dsize;
195         memcpy(&d->data[0], key.dptr, key.dsize);
196         memcpy(&d->data[key.dsize], data.dptr, data.dsize);
197         return d;
198 }
199
200 #if HAVE_SCHED_H
201 #include <sched.h>
202 #endif
203
204 /*
205   if possible, make this task real time
206  */
207 void ctdb_set_realtime(void)
208 {
209 #if HAVE_SCHED_SETSCHEDULER
210         struct sched_param p;
211         p.__sched_priority = 1;
212
213         if (sched_setscheduler(getpid(), SCHED_FIFO, &p) == -1) {
214                 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", strerror(errno)));
215         } else {
216                 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
217         }
218 #endif
219 }