summaryrefslogtreecommitdiff
path: root/source4/cluster/ctdb/common/ctdb_util.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2007-07-10 08:06:51 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 14:59:20 -0500
commit6504900f1f52927adab3489b8d04b6644ceaee7d (patch)
tree893cadba231a529f320046429767cedf8b0eec30 /source4/cluster/ctdb/common/ctdb_util.c
parentd4e2d157cbce5a1cdc75efb5ced75d9608a0f7df (diff)
downloadsamba-6504900f1f52927adab3489b8d04b6644ceaee7d.tar.gz
samba-6504900f1f52927adab3489b8d04b6644ceaee7d.tar.bz2
samba-6504900f1f52927adab3489b8d04b6644ceaee7d.zip
r23806: update Samba4 with the latest ctdb code.
This doesn't get the ctdb code fully working in Samba4, it just gets it building and not breaking non-clustered use of Samba. It will take a bit longer to update some of the calling ctdb_cluster.c code to make it work correctly in Samba4. Note also that Samba4 now only links to the client portion of ctdb. For the moment I am leaving the ctdbd as a separate daemon, which you install separately from http://ctdb.samba.org/. (This used to be commit b196077cbb55cbecad87065133c2d67198e31066)
Diffstat (limited to 'source4/cluster/ctdb/common/ctdb_util.c')
-rw-r--r--source4/cluster/ctdb/common/ctdb_util.c178
1 files changed, 156 insertions, 22 deletions
diff --git a/source4/cluster/ctdb/common/ctdb_util.c b/source4/cluster/ctdb/common/ctdb_util.c
index 1448e3340a..f8f7cb5150 100644
--- a/source4/cluster/ctdb/common/ctdb_util.c
+++ b/source4/cluster/ctdb/common/ctdb_util.c
@@ -3,18 +3,18 @@
Copyright (C) Andrew Tridgell 2006
- This library is free software; you can redistribute it and/or
- modify it under the terms of the GNU Lesser General Public
- License as published by the Free Software Foundation; either
- version 3 of the License, or (at your option) any later version.
-
- This library is distributed in the hope that it will be useful,
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- Lesser General Public License for more details.
-
- You should have received a copy of the GNU Lesser General Public
- License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
@@ -51,10 +51,9 @@ void ctdb_set_error(struct ctdb_context *ctdb, const char *fmt, ...)
/*
a fatal internal error occurred - no hope for recovery
*/
-_NORETURN_ void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
+void ctdb_fatal(struct ctdb_context *ctdb, const char *msg)
{
DEBUG(0,("ctdb fatal error: %s\n", msg));
- fprintf(stderr, "ctdb fatal error: '%s'\n", msg);
abort();
}
@@ -65,15 +64,18 @@ int ctdb_parse_address(struct ctdb_context *ctdb,
TALLOC_CTX *mem_ctx, const char *str,
struct ctdb_address *address)
{
- char *p;
- p = strchr(str, ':');
- if (p == NULL) {
- ctdb_set_error(ctdb, "Badly formed node '%s'\n", str);
- return -1;
- }
+ struct servent *se;
+
+ setservent(0);
+ se = getservbyname("ctdb", "tcp");
+ endservent();
- address->address = talloc_strndup(mem_ctx, str, p-str);
- address->port = strtoul(p+1, NULL, 0);
+ address->address = talloc_strdup(mem_ctx, str);
+ if (se == NULL) {
+ address->port = CTDB_PORT;
+ } else {
+ address->port = ntohs(se->s_port);
+ }
return 0;
}
@@ -105,7 +107,7 @@ uint32_t ctdb_hash(const TDB_DATA *key)
/*
a type checking varient of idr_find
*/
-void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
+static void *_idr_find_type(struct idr_context *idp, int id, const char *type, const char *location)
{
void *p = idr_find(idp, id);
if (p && talloc_check_name(p, type) == NULL) {
@@ -127,3 +129,135 @@ void ctdb_latency(double *latency, struct timeval t)
*latency = l;
}
}
+
+uint32_t ctdb_reqid_new(struct ctdb_context *ctdb, void *state)
+{
+ uint32_t id;
+
+ id = ctdb->idr_cnt++ & 0xFFFF;
+ id |= (idr_get_new(ctdb->idr, state, 0xFFFF)<<16);
+ return id;
+}
+
+void *_ctdb_reqid_find(struct ctdb_context *ctdb, uint32_t reqid, const char *type, const char *location)
+{
+ void *p;
+
+ p = _idr_find_type(ctdb->idr, (reqid>>16)&0xFFFF, type, location);
+ if (p == NULL) {
+ DEBUG(0, ("Could not find idr:%u\n",reqid));
+ }
+
+ return p;
+}
+
+
+void ctdb_reqid_remove(struct ctdb_context *ctdb, uint32_t reqid)
+{
+ int ret;
+
+ ret = idr_remove(ctdb->idr, (reqid>>16)&0xFFFF);
+ if (ret != 0) {
+ DEBUG(0, ("Removing idr that does not exist\n"));
+ }
+}
+
+
+/*
+ form a ctdb_rec_data record from a key/data pair
+ */
+struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid, TDB_DATA key, TDB_DATA data)
+{
+ size_t length;
+ struct ctdb_rec_data *d;
+
+ length = offsetof(struct ctdb_rec_data, data) + key.dsize + data.dsize;
+ d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
+ if (d == NULL) {
+ return NULL;
+ }
+ d->length = length;
+ d->reqid = reqid;
+ d->keylen = key.dsize;
+ d->datalen = data.dsize;
+ memcpy(&d->data[0], key.dptr, key.dsize);
+ memcpy(&d->data[key.dsize], data.dptr, data.dsize);
+ return d;
+}
+
+#if HAVE_SCHED_H
+#include <sched.h>
+#endif
+
+/*
+ if possible, make this task real time
+ */
+void ctdb_set_realtime(bool enable)
+{
+#if HAVE_SCHED_SETSCHEDULER
+ struct sched_param p;
+ p.__sched_priority = 1;
+
+ if (enable) {
+ if (sched_setscheduler(getpid(), SCHED_FIFO, &p) == -1) {
+ DEBUG(0,("Unable to set scheduler to SCHED_FIFO (%s)\n", strerror(errno)));
+ } else {
+ DEBUG(0,("Set scheduler to SCHED_FIFO\n"));
+ }
+ } else {
+ sched_setscheduler(getpid(), SCHED_OTHER, &p);
+ }
+#endif
+}
+
+void set_nonblocking(int fd)
+{
+ unsigned v;
+ v = fcntl(fd, F_GETFL, 0);
+ fcntl(fd, F_SETFL, v | O_NONBLOCK);
+}
+
+void set_close_on_exec(int fd)
+{
+ unsigned v;
+ v = fcntl(fd, F_GETFD, 0);
+ fcntl(fd, F_SETFD, v | FD_CLOEXEC);
+}
+
+
+/*
+ parse a ip:port pair
+ */
+bool parse_ip_port(const char *s, struct sockaddr_in *ip)
+{
+ const char *p;
+ char *endp = NULL;
+ unsigned port;
+ char buf[16];
+
+ ip->sin_family = AF_INET;
+
+ p = strchr(s, ':');
+ if (p == NULL) {
+ return false;
+ }
+
+ if (p - s > 15) {
+ return false;
+ }
+
+ port = strtoul(p+1, &endp, 10);
+ if (endp == NULL || *endp != 0) {
+ /* trailing garbage */
+ return false;
+ }
+ ip->sin_port = htons(port);
+
+ strlcpy(buf, s, 1+p-s);
+
+ if (inet_aton(buf, &ip->sin_addr) == 0) {
+ return false;
+ }
+
+ return true;
+}