Merge git://git.samba.org/tridge/ctdb
[metze/ctdb/wip.git] / common / ctdb_util.c
1 /* 
2    ctdb utility code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program 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
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; 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 "system/wait.h"
26 #include "../include/ctdb_private.h"
27
28 int LogLevel = DEBUG_NOTICE;
29 int this_log_level = 0;
30
31 /*
32   return error string for last error
33 */
34 const char *ctdb_errstr(struct ctdb_context *ctdb)
35 {
36         return ctdb->err_msg;
37 }
38
39
40 /*
41   remember an error message
42 */
43 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
44 {
45         va_list ap;
46         talloc_free(ctdb->err_msg);
47         va_start(ap, fmt);
48         ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
49         DEBUG(DEBUG_ERR,("ctdb error: %s\n", ctdb->err_msg));
50         va_end(ap);
51 }
52
53 /*
54   a fatal internal error occurred - no hope for recovery
55 */
56 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
57 {
58         DEBUG(DEBUG_ALERT,("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         struct servent *se;
70
71         setservent(0);
72         se = getservbyname("ctdb", "tcp");
73         endservent();
74         
75         address->address = talloc_strdup(mem_ctx, str);
76         if (se == NULL) {
77                 address->port = CTDB_PORT;
78         } else {
79                 address->port = ntohs(se->s_port);
80         }
81         return 0;
82 }
83
84
85 /*
86   check if two addresses are the same
87 */
88 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
89 {
90         return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
91 }
92
93
94 /*
95   hash function for mapping data to a VNN - taken from tdb
96 */
97 uint32_t ctdb_hash(const TDB_DATA *key)
98 {
99         uint32_t value; /* Used to compute the hash value.  */
100         uint32_t i;     /* Used to cycle through random values. */
101
102         /* Set the initial value from the key size. */
103         for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
104                 value = (value + (key->dptr[i] << (i*5 % 24)));
105
106         return (1103515243 * value + 12345);  
107 }
108
109 /*
110   a type checking varient of idr_find
111  */
112 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
113 {
114         void *p = idr_find(idp, id);
115         if (p && talloc_check_name(p, type) == NULL) {
116                 DEBUG(DEBUG_ERR,("%s idr_find_type expected type %s  but got %s\n",
117                          location, type, talloc_get_name(p)));
118                 return NULL;
119         }
120         return p;
121 }
122
123
124 /*
125   update a max latency number
126  */
127 void ctdb_latency(double *latency, struct timeval t)
128 {
129         double l = timeval_elapsed(&t);
130         if (l > *latency) {
131                 *latency = l;
132         }
133 }
134
135 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
136 {
137         uint32_t id;
138
139         id  = ctdb->idr_cnt++ & 0xFFFF;
140         id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
141         return id;
142 }
143
144 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
145 {
146         void *p;
147
148         p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
149         if (p == NULL) {
150                 DEBUG(DEBUG_ERR, ("Could not find idr:%u\n",reqid));
151         }
152
153         return p;
154 }
155
156
157 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
158 {
159         int ret;
160
161         ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
162         if (ret != 0) {
163                 DEBUG(DEBUG_ERR, ("Removing idr that does not exist\n"));
164         }
165 }
166
167
168 /*
169   form a ctdb_rec_data record from a key/data pair
170   
171   note that header may be NULL. If not NULL then it is included in the data portion
172   of the record
173  */
174 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
175                                            TDB_DATA key, 
176                                            struct ctdb_ltdb_header *header,
177                                            TDB_DATA data)
178 {
179         size_t length;
180         struct ctdb_rec_data *d;
181
182         length = offsetof(struct ctdb_rec_data, data) + key.dsize + 
183                 data.dsize + (header?sizeof(*header):0);
184         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
185         if (d == NULL) {
186                 return NULL;
187         }
188         d->length = length;
189         d->reqid = reqid;
190         d->keylen = key.dsize;
191         memcpy(&d->data[0], key.dptr, key.dsize);
192         if (header) {
193                 d->datalen = data.dsize + sizeof(*header);
194                 memcpy(&d->data[key.dsize], header, sizeof(*header));
195                 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
196         } else {
197                 d->datalen = data.dsize;
198                 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
199         }
200         return d;
201 }
202
203 #if HAVE_SCHED_H
204 #include <sched.h>
205 #endif
206
207 /*
208   if possible, make this task real time
209  */
210 void ctdb_set_scheduler(struct ctdb_context *ctdb)
211 {
212 #if HAVE_SCHED_SETSCHEDULER     
213         struct sched_param p;
214         if (ctdb->saved_scheduler_param == NULL) {
215                 ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
216         }
217         
218         if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
219                 DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
220                 return;
221         }
222
223         p = *(struct sched_param *)ctdb->saved_scheduler_param;
224         p.sched_priority = 1;
225
226         if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
227                 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
228                          strerror(errno)));
229         } else {
230                 DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
231         }
232 #endif
233 }
234
235 /*
236   restore previous scheduler parameters
237  */
238 void ctdb_restore_scheduler(struct ctdb_context *ctdb)
239 {
240 #if HAVE_SCHED_SETSCHEDULER     
241         if (ctdb->saved_scheduler_param == NULL) {
242                 ctdb_fatal(ctdb, "No saved scheduler parameters\n");
243         }
244         if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
245                 ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
246         }
247 #endif
248 }
249
250 void set_nonblocking(int fd)
251 {
252         unsigned v;
253         v = fcntl(fd, F_GETFL, 0);
254         fcntl(fd, F_SETFL, v | O_NONBLOCK);
255 }
256
257 void set_close_on_exec(int fd)
258 {
259         unsigned v;
260         v = fcntl(fd, F_GETFD, 0);
261         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
262 }
263
264
265 /*
266   parse a ip:num pair with the given separator
267  */
268 static bool parse_ip_num(const char *s, struct in_addr *addr, unsigned *num, const char sep)
269 {
270         const char *p;
271         char *endp = NULL;
272         char buf[16];
273
274         p = strchr(s, sep);
275         if (p == NULL) {
276                 return false;
277         }
278
279         if (p - s > 15) {
280                 return false;
281         }
282
283         *num = strtoul(p+1, &endp, 10);
284         if (endp == NULL || *endp != 0) {
285                 /* trailing garbage */
286                 return false;
287         }
288
289         strlcpy(buf, s, 1+p-s);
290
291         if (inet_aton(buf, addr) == 0) {
292                 return false;
293         }
294
295         return true;
296 }
297
298
299 static bool parse_ipv4(const char *s, unsigned port, ctdb_sock_addr *saddr)
300 {
301         saddr->ip.sin_family = AF_INET;
302         saddr->ip.sin_port   = htons(port);
303
304         if (inet_pton(AF_INET, s, &saddr->ip.sin_addr) != 1) {
305                 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
306                 return false;
307         }
308
309         return true;
310 }
311
312 static bool parse_ipv6(const char *s, unsigned port, ctdb_sock_addr *saddr)
313 {
314         saddr->ip6.sin6_family   = AF_INET6;
315         saddr->ip6.sin6_port     = htons(port);
316         saddr->ip6.sin6_flowinfo = 0;
317         saddr->ip6.sin6_scope_id = 0;
318
319         if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
320                 DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
321                 return false;
322         }
323
324         return true;
325 }
326 /*
327   parse a ip:port pair
328  */
329 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
330 {
331         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
332         char *s, *p;
333         unsigned port;
334         char *endp = NULL;
335         bool ret;
336
337         s = talloc_strdup(tmp_ctx, addr);
338         if (s == NULL) {
339                 DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
340                 talloc_free(tmp_ctx);
341                 return false;
342         }
343
344         p = rindex(s, ':');
345         if (p == NULL) {
346                 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
347                 talloc_free(tmp_ctx);
348                 return false;
349         }
350
351         port = strtoul(p+1, &endp, 10);
352         if (endp == NULL || *endp != 0) {
353                 /* trailing garbage */
354                 DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
355                 talloc_free(tmp_ctx);
356                 return false;
357         }
358         *p = 0;
359
360
361         /* now is this a ipv4 or ipv6 address ?*/
362         p = index(s, ':');
363         if (p == NULL) {
364                 ret = parse_ipv4(s, port, saddr);
365         } else {
366                 ret = parse_ipv6(s, port, saddr);
367         }
368
369         talloc_free(tmp_ctx);
370         return ret;
371 }
372
373 /*
374   parse a ip/mask pair
375  */
376 bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
377 {
378         if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
379                 return false;
380         }
381         if (*mask > 32) {
382                 return false;
383         }
384         ip->sin_family = AF_INET;
385         ip->sin_port   = 0;
386         return true;
387 }
388
389 /*
390   compare two sockaddr_in structures - matching only on IP
391  */
392 bool ctdb_same_ip(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
393 {
394         return ip1->sin_family == ip2->sin_family &&
395                 ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
396 }
397
398 /*
399   compare two sockaddr_in structures
400  */
401 bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
402 {
403         return ctdb_same_ip(ip1, ip2) && ip1->sin_port == ip2->sin_port;
404 }
405
406
407
408 void ctdb_block_signal(int signum)
409 {
410         sigset_t set;
411         sigemptyset(&set);
412         sigaddset(&set,signum);
413         sigprocmask(SIG_BLOCK,&set,NULL);
414 }
415
416 void ctdb_unblock_signal(int signum)
417 {
418         sigset_t set;
419         sigemptyset(&set);
420         sigaddset(&set,signum);
421         sigprocmask(SIG_UNBLOCK,&set,NULL);
422 }