merge from ronnie
[rusty/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 = 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 /*
300   parse a ip:port pair
301  */
302 bool parse_ip_port(const char *s, struct sockaddr_in *ip)
303 {
304         unsigned port;
305         if (!parse_ip_num(s, &ip->sin_addr, &port, ':')) {
306                 return false;
307         }
308         ip->sin_family = AF_INET;
309         ip->sin_port   = htons(port);
310         return true;
311 }
312
313 /*
314   parse a ip/mask pair
315  */
316 bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
317 {
318         if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
319                 return false;
320         }
321         if (*mask > 32) {
322                 return false;
323         }
324         ip->sin_family = AF_INET;
325         ip->sin_port   = 0;
326         return true;
327 }
328
329 /*
330   compare two sockaddr_in structures - matching only on IP
331  */
332 bool ctdb_same_ip(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
333 {
334         return ip1->sin_family == ip2->sin_family &&
335                 ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
336 }
337
338 /*
339   compare two sockaddr_in structures
340  */
341 bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
342 {
343         return ctdb_same_ip(ip1, ip2) && ip1->sin_port == ip2->sin_port;
344 }
345
346
347
348 void ctdb_block_signal(int signum)
349 {
350         sigset_t set;
351         sigemptyset(&set);
352         sigaddset(&set,signum);
353         sigprocmask(SIG_BLOCK,&set,NULL);
354 }
355
356 void ctdb_unblock_signal(int signum)
357 {
358         sigset_t set;
359         sigemptyset(&set);
360         sigaddset(&set,signum);
361         sigprocmask(SIG_UNBLOCK,&set,NULL);
362 }