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"
31 return error string for last error
33 const char *ctdb_errstr(struct ctdb_context *ctdb)
40 remember an error message
42 void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
45 talloc_free(ctdb->err_msg);
47 ctdb->err_msg = talloc_vasprintf(ctdb, fmt, ap);
48 DEBUG(0,("ctdb error: %s\n", ctdb->err_msg));
53 a fatal internal error occurred - no hope for recovery
55 void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
57 DEBUG(0,("ctdb fatal error: %s\n", msg));
64 int ctdb_parse_address(struct ctdb_context *ctdb,
65 TALLOC_CTX *mem_ctx, const char *str,
66 struct ctdb_address *address)
71 se = getservbyname("ctdb", "tcp");
74 address->address = talloc_strdup(mem_ctx, str);
76 address->port = CTDB_PORT;
78 address->port = ntohs(se->s_port);
85 check if two addresses are the same
87 bool ctdb_same_address(struct ctdb_address *a1, struct ctdb_address *a2)
89 return strcmp(a1->address, a2->address) == 0 && a1->port == a2->port;
94 hash function for mapping data to a VNN - taken from tdb
96 uint32_t ctdb_hash(const TDB_DATA *key)
98 uint32_t value; /* Used to compute the hash value. */
99 uint32_t i; /* Used to cycle through random values. */
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)));
105 return (1103515243 * value + 12345);
109 a type checking varient of idr_find
111 static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
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)));
124 update a max latency number
126 void ctdb_latency(double *latency, struct timeval t)
128 double l = timeval_elapsed(&t);
134 uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
138 id = ctdb->idr_cnt++ & 0xFFFF;
139 id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
143 void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
147 p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
149 DEBUG(0, ("Could not find idr:%u\n",reqid));
156 void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
160 ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
162 DEBUG(0, ("Removing idr that does not exist\n"));
168 form a ctdb_rec_data record from a key/data pair
170 note that header may be NULL. If not NULL then it is included in the data portion
173 struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
175 struct ctdb_ltdb_header *header,
179 struct ctdb_rec_data *d;
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);
189 d->keylen = key.dsize;
190 memcpy(&d->data[0], key.dptr, key.dsize);
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);
196 d->datalen = data.dsize;
197 memcpy(&d->data[key.dsize], data.dptr, data.dsize);
207 if possible, make this task real time
209 void ctdb_set_scheduler(struct ctdb_context *ctdb)
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));
217 if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
218 DEBUG(0,("Unable to get old scheduler params\n"));
222 p = *(struct sched_param *)ctdb->saved_scheduler_param;
223 p.sched_priority = 1;
225 if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
226 DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n",
229 DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
235 restore previous scheduler parameters
237 void ctdb_restore_scheduler(struct ctdb_context *ctdb)
239 #if HAVE_SCHED_SETSCHEDULER
240 if (ctdb->saved_scheduler_param == NULL) {
241 ctdb_fatal(ctdb, "No saved scheduler parameters\n");
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");
249 void set_nonblocking(int fd)
252 v = fcntl(fd, F_GETFL, 0);
253 fcntl(fd, F_SETFL, v | O_NONBLOCK);
256 void set_close_on_exec(int fd)
259 v = fcntl(fd, F_GETFD, 0);
260 fcntl(fd, F_SETFD, v | FD_CLOEXEC);
265 parse a ip:num pair with the given separator
267 static bool parse_ip_num(const char *s, struct in_addr *addr, unsigned *num, const char sep)
282 *num = strtoul(p+1, &endp, 10);
283 if (endp == NULL || *endp != 0) {
284 /* trailing garbage */
288 strlcpy(buf, s, 1+p-s);
290 if (inet_aton(buf, addr) == 0) {
301 bool parse_ip_port(const char *s, struct sockaddr_in *ip)
304 if (!parse_ip_num(s, &ip->sin_addr, &port, ':')) {
307 ip->sin_family = AF_INET;
308 ip->sin_port = htons(port);
315 bool parse_ip_mask(const char *s, struct sockaddr_in *ip, unsigned *mask)
317 if (!parse_ip_num(s, &ip->sin_addr, mask, '/')) {
323 ip->sin_family = AF_INET;
329 compare two sockaddr_in structures - matching only on IP
331 bool ctdb_same_ip(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
333 return ip1->sin_family == ip2->sin_family &&
334 ip1->sin_addr.s_addr == ip2->sin_addr.s_addr;
338 compare two sockaddr_in structures
340 bool ctdb_same_sockaddr(const struct sockaddr_in *ip1, const struct sockaddr_in *ip2)
342 return ctdb_same_ip(ip1, ip2) && ip1->sin_port == ip2->sin_port;
347 void ctdb_block_signal(int signum)
351 sigaddset(&set,signum);
352 sigprocmask(SIG_BLOCK,&set,NULL);
355 void ctdb_unblock_signal(int signum)
359 sigaddset(&set,signum);
360 sigprocmask(SIG_UNBLOCK,&set,NULL);