merge from tridge
[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 "../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_realtime(bool enable)
196 {
197 #if HAVE_SCHED_SETSCHEDULER
198         struct sched_param p;
199         p.__sched_priority = 1;
200
201         if (enable) {
202                 if (sched_setscheduler(getpid(), SCHED_FIFO, &p) == -1) {
203                         DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", strerror(errno)));
204                 } else {
205                         DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
206                 }
207         } else {
208                 sched_setscheduler(getpid(), SCHED_OTHER, &p);
209         }
210 #endif
211 }
212
213 void set_nonblocking(int fd)
214 {
215         unsigned v;
216         v = fcntl(fd, F_GETFL, 0);
217         fcntl(fd, F_SETFL, v | O_NONBLOCK);
218 }
219
220 void set_close_on_exec(int fd)
221 {
222         unsigned v;
223         v = fcntl(fd, F_GETFD, 0);
224         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
225 }
226
227
228 /*
229   parse a ip:port pair
230  */
231 bool parse_ip_port(const char *s, struct sockaddr_in *ip)
232 {
233         const char *p;
234         char *endp = NULL;
235         unsigned port;
236         char buf[16];
237
238         ip->sin_family = AF_INET;
239
240         p = strchr(s, ':');
241         if (p == NULL) {
242                 return false;
243         }
244
245         if (p - s > 15) {
246                 return false;
247         }
248
249         port = strtoul(p+1, &endp, 10);
250         if (endp == NULL || *endp != 0) {
251                 /* trailing garbage */
252                 return false;
253         }
254         ip->sin_port = htons(port);
255
256         strlcpy(buf, s, 1+p-s);
257
258         if (inet_aton(buf, &ip->sin_addr) == 0) {
259                 return false;
260         }
261
262         return true;
263 }