merge from tridge
[sahlberg/ctdb.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;
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   a type checking varient of idr_find
110  */
111 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
112 {
113         void *p = idr_find(idp, id);
114         if (p && talloc_check_name(p, type) == NULL) {
115                 DEBUG(0,("%s idr_find_type expected type %s  but got %s\n",
116                          location, type, talloc_get_name(p)));
117                 return NULL;
118         }
119         return p;
120 }
121
122
123 /*
124   update a max latency number
125  */
126 void ctdb_latency(double *latency, struct timeval t)
127 {
128         double l = timeval_elapsed(&t);
129         if (l > *latency) {
130                 *latency = l;
131         }
132 }
133
134 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
135 {
136         uint32_t id;
137
138         id  = ctdb->idr_cnt++ & 0xFFFF;
139         id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
140         return id;
141 }
142
143 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
144 {
145         void *p;
146
147         p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
148         if (p == NULL) {
149                 DEBUG(0, ("Could not find idr:%u\n",reqid));
150         }
151
152         return p;
153 }
154
155
156 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
157 {
158         int ret;
159
160         ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
161         if (ret != 0) {
162                 DEBUG(0, ("Removing idr that does not exist\n"));
163         }
164 }
165
166
167 /*
168   form a ctdb_rec_data record from a key/data pair
169   
170   note that header may be NULL. If not NULL then it is included in the data portion
171   of the record
172  */
173 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, 
174                                            TDB_DATA key, 
175                                            struct ctdb_ltdb_header *header,
176                                            TDB_DATA data)
177 {
178         size_t length;
179         struct ctdb_rec_data *d;
180
181         length = offsetof(struct ctdb_rec_data, data) + key.dsize + 
182                 data.dsize + (header?sizeof(*header):0);
183         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
184         if (d == NULL) {
185                 return NULL;
186         }
187         d->length = length;
188         d->reqid = reqid;
189         d->keylen = key.dsize;
190         memcpy(&d->data[0], key.dptr, key.dsize);
191         if (header) {
192                 d->datalen = data.dsize + sizeof(*header);
193                 memcpy(&d->data[key.dsize], header, sizeof(*header));
194                 memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
195         } else {
196                 d->datalen = data.dsize;
197                 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
198         }
199         return d;
200 }
201
202 #if HAVE_SCHED_H
203 #include <sched.h>
204 #endif
205
206 /*
207   if possible, make this task real time
208  */
209 void ctdb_set_scheduler(struct ctdb_context *ctdb)
210 {
211 #if HAVE_SCHED_SETSCHEDULER     
212         struct sched_param p;
213         if (ctdb->saved_scheduler_param == NULL) {
214                 ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
215         }
216         
217         if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
218                 DEBUG(0,("Unable to get old scheduler params\n"));
219                 return;
220         }
221
222         p = *(struct sched_param *)ctdb->saved_scheduler_param;
223         p.sched_priority = 1;
224
225         if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
226                 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
227                          strerror(errno)));
228         } else {
229                 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
230         }
231 #endif
232 }
233
234 /*
235   restore previous scheduler parameters
236  */
237 void ctdb_restore_scheduler(struct ctdb_context *ctdb)
238 {
239 #if HAVE_SCHED_SETSCHEDULER     
240         if (ctdb->saved_scheduler_param == NULL) {
241                 ctdb_fatal(ctdb, "No saved scheduler parameters\n");
242         }
243         if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
244                 ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
245         }
246 #endif
247 }
248
249 void set_nonblocking(int fd)
250 {
251         unsigned v;
252         v = fcntl(fd, F_GETFL, 0);
253         fcntl(fd, F_SETFL, v | O_NONBLOCK);
254 }
255
256 void set_close_on_exec(int fd)
257 {
258         unsigned v;
259         v = fcntl(fd, F_GETFD, 0);
260         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
261 }
262
263
264 /*
265   parse a ip:num pair with the given separator
266  */
267 static bool parse_ip_num(const char *s, struct in_addr *addr, unsigned *num, const char sep)
268 {
269         const char *p;
270         char *endp = NULL;
271         char buf[16];
272
273         p = strchr(s, sep);
274         if (p == NULL) {
275                 return false;
276         }
277
278         if (p - s > 15) {
279                 return false;
280         }
281
282         *num = strtoul(p+1, &endp, 10);
283         if (endp == NULL || *endp != 0) {
284                 /* trailing garbage */
285                 return false;
286         }
287
288         strlcpy(buf, s, 1+p-s);
289
290         if (inet_aton(buf, addr) == 0) {
291                 return false;
292         }
293
294         return true;
295 }
296
297
298 /*
299   parse a ip:port pair
300  */
301 bool parse_ip_port(const char *s, struct sockaddr_in *ip)
302 {
303         unsigned port;
304         if (!parse_ip_num(s, &ip->sin_addr, &port, ':')) {
305                 return false;
306         }
307         ip->sin_family = AF_INET;
308         ip->sin_port   = htons(port);
309         return true;
310 }
311
312 /*
313   parse a ip/mask pair
314  */
315 bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
316 {
317         if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
318                 return false;
319         }
320         if (*mask > 32) {
321                 return false;
322         }
323         ip->sin_family = AF_INET;
324         ip->sin_port   = 0;
325         return true;
326 }
327
328 /*
329   compare two sockaddr_in structures - matching only on IP
330  */
331 bool ctdb_same_ip(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
332 {
333         return ip1->sin_family == ip2->sin_family &&
334                 ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
335 }
336
337 /*
338   compare two sockaddr_in structures
339  */
340 bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
341 {
342         return ctdb_same_ip(ip1, ip2) && ip1->sin_port == ip2->sin_port;
343 }
344
345
346
347 void ctdb_block_signal(int signum)
348 {
349         sigset_t set;
350         sigemptyset(&set);
351         sigaddset(&set,signum);
352         sigprocmask(SIG_BLOCK,&set,NULL);
353 }
354
355 void ctdb_unblock_signal(int signum)
356 {
357         sigset_t set;
358         sigemptyset(&set);
359         sigaddset(&set,signum);
360         sigprocmask(SIG_UNBLOCK,&set,NULL);
361 }