4 Copyright (C) Andrew Tridgell 2006
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.
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.
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/>.
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"
28 int LogLevel = DEBUG_NOTICE;
29 int this_log_level = 0;
32 return error string for last error
34 const char *ctdb_errstr(struct ctdb_context *ctdb)
41 remember an error message
43 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
46 talloc_free(ctdb->err_msg);
48 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
49 DEBUG(DEBUG_ERR,("ctdb error: %s\n", ctdb->err_msg));
54 a fatal internal error occurred - no hope for recovery
56 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
58 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n", msg));
65 int ctdb_parse_address(struct ctdb_context *ctdb,
66 TALLOC_CTX *mem_ctx, const char *str,
67 struct ctdb_address *address)
72 se = getservbyname("ctdb", "tcp");
75 address->address = talloc_strdup(mem_ctx, str);
77 address->port = CTDB_PORT;
79 address->port = ntohs(se->s_port);
86 check if two addresses are the same
88 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
90 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
95 hash function for mapping data to a VNN - taken from tdb
97 uint32_t ctdb_hash(const TDB_DATA *key)
99 uint32_t value; /* Used to compute the hash value. */
100 uint32_t i; /* Used to cycle through random values. */
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)));
106 return (1103515243 * value + 12345);
110 a type checking varient of idr_find
112 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
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)));
125 update a max latency number
127 void ctdb_latency(double *latency, struct timeval t)
129 double l = timeval_elapsed(&t);
135 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
139 id = ctdb->idr_cnt++ & 0xFFFF;
140 id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
144 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
148 p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
150 DEBUG(DEBUG_ERR, ("Could not find idr:%u\n",reqid));
157 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
161 ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
163 DEBUG(DEBUG_ERR, ("Removing idr that does not exist\n"));
169 form a ctdb_rec_data record from a key/data pair
171 note that header may be NULL. If not NULL then it is included in the data portion
174 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
176 struct ctdb_ltdb_header *header,
180 struct ctdb_rec_data *d;
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);
190 d->keylen = key.dsize;
191 memcpy(&d->data[0], key.dptr, key.dsize);
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);
197 d->datalen = data.dsize;
198 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
208 if possible, make this task real time
210 void ctdb_set_scheduler(struct ctdb_context *ctdb)
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));
218 if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
219 DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
223 p = *(struct sched_param *)ctdb->saved_scheduler_param;
224 p.sched_priority = 1;
226 if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
227 DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
230 DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
236 restore previous scheduler parameters
238 void ctdb_restore_scheduler(struct ctdb_context *ctdb)
240 #if HAVE_SCHED_SETSCHEDULER
241 if (ctdb->saved_scheduler_param == NULL) {
242 ctdb_fatal(ctdb, "No saved scheduler parameters\n");
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");
250 void set_nonblocking(int fd)
253 v = fcntl(fd, F_GETFL, 0);
254 fcntl(fd, F_SETFL, v | O_NONBLOCK);
257 void set_close_on_exec(int fd)
260 v = fcntl(fd, F_GETFD, 0);
261 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
266 parse a ip:num pair with the given separator
268 static bool parse_ip_num(const char *s, struct in_addr *addr, unsigned *num, const char sep)
283 *num = strtoul(p+1, &endp, 10);
284 if (endp == NULL || *endp != 0) {
285 /* trailing garbage */
289 strlcpy(buf, s, 1+p-s);
291 if (inet_aton(buf, addr) == 0) {
299 static bool parse_ipv4(const char *s, unsigned port, ctdb_sock_addr *saddr)
301 saddr->ip.sin_family = AF_INET;
302 saddr->ip.sin_port = htons(port);
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));
312 static bool parse_ipv6(const char *s, unsigned port, ctdb_sock_addr *saddr)
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;
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));
329 bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
331 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
337 s = talloc_strdup(tmp_ctx, addr);
339 DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
340 talloc_free(tmp_ctx);
346 DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
347 talloc_free(tmp_ctx);
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);
361 /* now is this a ipv4 or ipv6 address ?*/
364 ret = parse_ipv4(s, port, saddr);
366 ret = parse_ipv6(s, port, saddr);
369 talloc_free(tmp_ctx);
376 bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
378 if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
384 ip->sin_family = AF_INET;
390 compare two sockaddr_in structures - matching only on IP
392 bool ctdb_same_ip(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
394 return ip1->sin_family == ip2->sin_family &&
395 ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
399 compare two sockaddr_in structures
401 bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
403 return ctdb_same_ip(ip1, ip2) && ip1->sin_port == ip2->sin_port;
408 void ctdb_block_signal(int signum)
412 sigaddset(&set,signum);
413 sigprocmask(SIG_BLOCK,&set,NULL);
416 void ctdb_unblock_signal(int signum)
420 sigaddset(&set,signum);
421 sigprocmask(SIG_UNBLOCK,&set,NULL);