54b1e4e7ff5f68887b7f1c169533401b253cf651
[jelmer/samba4-debian.git] / source / cluster / ctdb / 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 "../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 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
55 {
56         DEBUG(0,("ctdb fatal error: %s\n", msg));
57         abort();
58 }
59
60 /*
61   parse a IP:port pair
62 */
63 int ctdb_parse_address(struct ctdb_context *ctdb,
64                        TALLOC_CTX *mem_ctx, const char *str,
65                        struct ctdb_address *address)
66 {
67         struct servent *se;
68
69         setservent(0);
70         se = getservbyname("ctdb", "tcp");
71         endservent();
72         
73         address->address = talloc_strdup(mem_ctx, str);
74         if (se == NULL) {
75                 address->port = CTDB_PORT;
76         } else {
77                 address->port = ntohs(se->s_port);
78         }
79         return 0;
80 }
81
82
83 /*
84   check if two addresses are the same
85 */
86 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
87 {
88         return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
89 }
90
91
92 /*
93   hash function for mapping data to a VNN - taken from tdb
94 */
95 uint32_t ctdb_hash(const TDB_DATA *key)
96 {
97         uint32_t value; /* Used to compute the hash value.  */
98         uint32_t i;     /* Used to cycle through random values. */
99
100         /* Set the initial value from the key size. */
101         for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++)
102                 value = (value + (key->dptr[i] << (i*5 % 24)));
103
104         return (1103515243 * value + 12345);  
105 }
106
107 /*
108   a type checking varient of idr_find
109  */
110 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
111 {
112         void *p = idr_find(idp, id);
113         if (p && talloc_check_name(p, type) == NULL) {
114                 DEBUG(0,("%s idr_find_type expected type %s  but got %s\n",
115                          location, type, talloc_get_name(p)));
116                 return NULL;
117         }
118         return p;
119 }
120
121
122 /*
123   update a max latency number
124  */
125 void ctdb_latency(double *latency, struct timeval t)
126 {
127         double l = timeval_elapsed(&t);
128         if (l > *latency) {
129                 *latency = l;
130         }
131 }
132
133 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
134 {
135         uint32_t id;
136
137         id  = ctdb->idr_cnt++ & 0xFFFF;
138         id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
139         return id;
140 }
141
142 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
143 {
144         void *p;
145
146         p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
147         if (p == NULL) {
148                 DEBUG(0, ("Could not find idr:%u\n",reqid));
149         }
150
151         return p;
152 }
153
154
155 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
156 {
157         int ret;
158
159         ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
160         if (ret != 0) {
161                 DEBUG(0, ("Removing idr that does not exist\n"));
162         }
163 }
164
165
166 /*
167   form a ctdb_rec_data record from a key/data pair
168  */
169 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data)
170 {
171         size_t length;
172         struct ctdb_rec_data *d;
173
174         length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
175         d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
176         if (d == NULL) {
177                 return NULL;
178         }
179         d->length = length;
180         d->reqid = reqid;
181         d->keylen = key.dsize;
182         d->datalen = data.dsize;
183         memcpy(&d->data[0], key.dptr, key.dsize);
184         memcpy(&d->data[key.dsize], data.dptr, data.dsize);
185         return d;
186 }
187
188 #if HAVE_SCHED_H
189 #include <sched.h>
190 #endif
191
192 /*
193   if possible, make this task real time
194  */
195 void ctdb_set_scheduler(struct ctdb_context *ctdb)
196 {
197 #if HAVE_SCHED_SETSCHEDULER     
198         struct sched_param p;
199         if (ctdb->saved_scheduler_param == NULL) {
200                 ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
201         }
202         
203         if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
204                 DEBUG(0,("Unable to get old scheduler params\n"));
205                 return;
206         }
207
208         p = *(struct sched_param *)ctdb->saved_scheduler_param;
209         p.sched_priority = 1;
210
211         if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
212                 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", 
213                          strerror(errno)));
214         } else {
215                 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
216         }
217 #endif
218 }
219
220 /*
221   restore previous scheduler parameters
222  */
223 void ctdb_restore_scheduler(struct ctdb_context *ctdb)
224 {
225 #if HAVE_SCHED_SETSCHEDULER     
226         if (ctdb->saved_scheduler_param == NULL) {
227                 ctdb_fatal(ctdb, "No saved scheduler parameters\n");
228         }
229         if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
230                 ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
231         }
232 #endif
233 }
234
235 void set_nonblocking(int fd)
236 {
237         unsigned v;
238         v = fcntl(fd, F_GETFL, 0);
239         fcntl(fd, F_SETFL, v | O_NONBLOCK);
240 }
241
242 void set_close_on_exec(int fd)
243 {
244         unsigned v;
245         v = fcntl(fd, F_GETFD, 0);
246         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
247 }
248
249
250 /*
251   parse a ip:port pair
252  */
253 bool parse_ip_port(const char *s, struct sockaddr_in *ip)
254 {
255         const char *p;
256         char *endp = NULL;
257         unsigned port;
258         char buf[16];
259
260         ip->sin_family = AF_INET;
261
262         p = strchr(s, ':');
263         if (p == NULL) {
264                 return false;
265         }
266
267         if (p - s > 15) {
268                 return false;
269         }
270
271         port = strtoul(p+1, &endp, 10);
272         if (endp == NULL || *endp != 0) {
273                 /* trailing garbage */
274                 return false;
275         }
276         ip->sin_port = htons(port);
277
278         strlcpy(buf, s, 1+p-s);
279
280         if (inet_aton(buf, &ip->sin_addr) == 0) {
281                 return false;
282         }
283
284         return true;
285 }