*/
#include "includes.h"
-#include "lib/tevent/tevent.h"
-#include "lib/tdb/include/tdb.h"
+#include "tdb.h"
#include "system/network.h"
#include "system/filesys.h"
#include "system/wait.h"
-#include "system/shmem.h"
#include "../include/ctdb_private.h"
-int LogLevel = DEBUG_NOTICE;
-int this_log_level = 0;
-
/*
return error string for last error
*/
abort();
}
+/*
+ like ctdb_fatal() but a core/backtrace would not be useful
+*/
+void ctdb_die(struct ctdb_context *ctdb, const char *msg)
+{
+ DEBUG(DEBUG_ALERT,("ctdb exiting with error: %s\n", msg));
+ exit(1);
+}
+
+/* Invoke an external program to do some sort of tracing on the CTDB
+ * process. This might block for a little while. The external
+ * program is specified by the environment variable
+ * CTDB_EXTERNAL_TRACE. This program should take one argument: the
+ * pid of the process to trace. Commonly, the program would be a
+ * wrapper script around gcore.
+ */
+void ctdb_external_trace(void)
+{
+ int ret;
+ const char * t = getenv("CTDB_EXTERNAL_TRACE");
+ char * cmd;
+
+ if (t == NULL) {
+ return;
+ }
+
+ cmd = talloc_asprintf(NULL, "%s %lu", t, (unsigned long) getpid());
+ DEBUG(DEBUG_WARNING,("begin external trace: %s\n", cmd));
+ ret = system(cmd);
+ if (ret == -1) {
+ DEBUG(DEBUG_ERR,
+ ("external trace command \"%s\" failed\n", cmd));
+ }
+ DEBUG(DEBUG_WARNING,("end external trace: %s\n", cmd));
+ talloc_free(cmd);
+}
+
/*
parse a IP:port pair
*/
struct ctdb_address *address)
{
struct servent *se;
+ ctdb_sock_addr addr;
setservent(0);
se = getservbyname("ctdb", "tcp");
endservent();
-
- address->address = talloc_strdup(mem_ctx, str);
+
+ /* Parse IP address and re-convert to string. This ensure correct
+ * string form for IPv6 addresses.
+ */
+ if (! parse_ip(str, NULL, 0, &addr)) {
+ return -1;
+ }
+
+ address->address = talloc_strdup(mem_ctx, ctdb_addr_to_str(&addr));
CTDB_NO_MEMORY(ctdb, address->address);
if (se == NULL) {
}
+static uint32_t ctdb_marshall_record_size(TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA data)
+{
+ return offsetof(struct ctdb_rec_data, data) + key.dsize +
+ data.dsize + (header ? sizeof(*header) : 0);
+}
+
+static void ctdb_marshall_record_copy(struct ctdb_rec_data *rec,
+ uint32_t reqid,
+ TDB_DATA key,
+ struct ctdb_ltdb_header *header,
+ TDB_DATA data,
+ uint32_t length)
+{
+ uint32_t offset;
+
+ rec->length = length;
+ rec->reqid = reqid;
+ rec->keylen = key.dsize;
+ memcpy(&rec->data[0], key.dptr, key.dsize);
+ offset = key.dsize;
+
+ if (header) {
+ rec->datalen = data.dsize + sizeof(*header);
+ memcpy(&rec->data[offset], header, sizeof(*header));
+ offset += sizeof(*header);
+ } else {
+ rec->datalen = data.dsize;
+ }
+ memcpy(&rec->data[offset], data.dptr, data.dsize);
+}
+
/*
form a ctdb_rec_data record from a key/data pair
note that header may be NULL. If not NULL then it is included in the data portion
of the record
*/
-struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
- TDB_DATA key,
+struct ctdb_rec_data *ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
+ TDB_DATA key,
struct ctdb_ltdb_header *header,
TDB_DATA data)
{
size_t length;
struct ctdb_rec_data *d;
- length = offsetof(struct ctdb_rec_data, data) + key.dsize +
- data.dsize + (header?sizeof(*header):0);
+ length = ctdb_marshall_record_size(key, header, data);
+
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;
- memcpy(&d->data[0], key.dptr, key.dsize);
- if (header) {
- d->datalen = data.dsize + sizeof(*header);
- memcpy(&d->data[key.dsize], header, sizeof(*header));
- memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
- } else {
- d->datalen = data.dsize;
- memcpy(&d->data[key.dsize], data.dptr, data.dsize);
- }
+
+ ctdb_marshall_record_copy(d, reqid, key, header, data, length);
return d;
}
/* helper function for marshalling multiple records */
-struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx,
+struct ctdb_marshall_buffer *ctdb_marshall_add(TALLOC_CTX *mem_ctx,
struct ctdb_marshall_buffer *m,
uint64_t db_id,
uint32_t reqid,
TDB_DATA data)
{
struct ctdb_rec_data *r;
- size_t m_size, r_size;
struct ctdb_marshall_buffer *m2;
+ uint32_t length, offset;
- r = ctdb_marshall_record(mem_ctx, reqid, key, header, data);
- if (r == NULL) {
- talloc_free(m);
- return NULL;
- }
+ length = ctdb_marshall_record_size(key, header, data);
if (m == NULL) {
- m = talloc_zero_size(mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
- if (m == NULL) {
- return NULL;
- }
- m->db_id = db_id;
+ offset = offsetof(struct ctdb_marshall_buffer, data);
+ m2 = talloc_zero_size(mem_ctx, offset + length);
+ } else {
+ offset = talloc_get_size(m);
+ m2 = talloc_realloc_size(mem_ctx, m, offset + length);
}
-
- m_size = talloc_get_size(m);
- r_size = talloc_get_size(r);
-
- m2 = talloc_realloc_size(mem_ctx, m, m_size + r_size);
if (m2 == NULL) {
- talloc_free(m);
+ TALLOC_FREE(m);
return NULL;
}
- memcpy(m_size + (uint8_t *)m2, r, r_size);
-
- talloc_free(r);
+ if (m == NULL) {
+ m2->db_id = db_id;
+ }
+ r = (struct ctdb_rec_data *)((uint8_t *)m2 + offset);
+ ctdb_marshall_record_copy(r, reqid, key, header, data, length);
m2->count++;
return m2;
if (r->datalen < sizeof(*header)) {
return NULL;
}
- *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
+ memcpy(header, &r->data[r->keylen], sizeof(*header));
}
return r;
}
-
-#if HAVE_SCHED_H
-#include <sched.h>
-#endif
-
-/*
- if possible, make this task real time
- */
-void ctdb_set_scheduler(struct ctdb_context *ctdb)
-{
-#if HAVE_SCHED_SETSCHEDULER
- struct sched_param p;
- if (ctdb->saved_scheduler_param == NULL) {
- ctdb->saved_scheduler_param = talloc_size(ctdb, sizeof(p));
- }
-
- if (sched_getparam(0, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
- DEBUG(DEBUG_ERR,("Unable to get old scheduler params\n"));
- return;
- }
-
- p = *(struct sched_param *)ctdb->saved_scheduler_param;
- p.sched_priority = 1;
-
- if (sched_setscheduler(0, SCHED_FIFO, &p) == -1) {
- DEBUG(DEBUG_CRIT,("Unable to set scheduler to SCHED_FIFO (%s)\n",
- strerror(errno)));
- } else {
- DEBUG(DEBUG_NOTICE,("Set scheduler to SCHED_FIFO\n"));
- }
-#endif
-}
-
-/*
- restore previous scheduler parameters
- */
-void ctdb_restore_scheduler(struct ctdb_context *ctdb)
-{
-#if HAVE_SCHED_SETSCHEDULER
- if (ctdb->saved_scheduler_param == NULL) {
- ctdb_fatal(ctdb, "No saved scheduler parameters\n");
- }
- if (sched_setscheduler(0, SCHED_OTHER, (struct sched_param *)ctdb->saved_scheduler_param) == -1) {
- ctdb_fatal(ctdb, "Unable to restore old scheduler parameters\n");
- }
-#endif
-}
-
-/*
- * This function forks a child process and drops the realtime
- * scheduler for the child process.
- */
-pid_t ctdb_fork(struct ctdb_context *ctdb)
-{
- pid_t pid;
-
- pid = fork();
- if (pid == 0) {
- if (ctdb->do_setsched) {
- ctdb_restore_scheduler(ctdb);
- }
- }
- return pid;
-}
-
-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);
-}
-
-
-bool parse_ipv4(const char *s, unsigned port, struct sockaddr_in *sin)
-{
- sin->sin_family = AF_INET;
- sin->sin_port = htons(port);
-
- if (inet_pton(AF_INET, s, &sin->sin_addr) != 1) {
- DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin_addr\n", s));
- return false;
- }
-
- return true;
-}
-
-static bool parse_ipv6(const char *s, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
-{
- saddr->ip6.sin6_family = AF_INET6;
- saddr->ip6.sin6_port = htons(port);
- saddr->ip6.sin6_flowinfo = 0;
- saddr->ip6.sin6_scope_id = 0;
-
- if (inet_pton(AF_INET6, s, &saddr->ip6.sin6_addr) != 1) {
- DEBUG(DEBUG_ERR, (__location__ " Failed to translate %s into sin6_addr\n", s));
- return false;
- }
-
- if (ifaces && IN6_IS_ADDR_LINKLOCAL(&saddr->ip6.sin6_addr)) {
- if (strchr(ifaces, ',')) {
- DEBUG(DEBUG_ERR, (__location__ " Link local address %s "
- "is specified for multiple ifaces %s\n",
- s, ifaces));
- return false;
- }
- saddr->ip6.sin6_scope_id = if_nametoindex(ifaces);
- }
-
- return true;
-}
-/*
- parse a ip:port pair
- */
-bool parse_ip_port(const char *addr, ctdb_sock_addr *saddr)
-{
- TALLOC_CTX *tmp_ctx = talloc_new(NULL);
- char *s, *p;
- unsigned port;
- char *endp = NULL;
- bool ret;
-
- s = talloc_strdup(tmp_ctx, addr);
- if (s == NULL) {
- DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
- talloc_free(tmp_ctx);
- return false;
- }
-
- p = rindex(s, ':');
- if (p == NULL) {
- DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a port number\n", s));
- talloc_free(tmp_ctx);
- return false;
- }
-
- port = strtoul(p+1, &endp, 10);
- if (endp == NULL || *endp != 0) {
- /* trailing garbage */
- DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the port in %s\n", s));
- talloc_free(tmp_ctx);
- return false;
- }
- *p = 0;
-
-
- /* now is this a ipv4 or ipv6 address ?*/
- ret = parse_ip(s, NULL, port, saddr);
-
- talloc_free(tmp_ctx);
- return ret;
-}
-
-/*
- parse an ip
- */
-bool parse_ip(const char *addr, const char *ifaces, unsigned port, ctdb_sock_addr *saddr)
-{
- char *p;
- bool ret;
-
- /* now is this a ipv4 or ipv6 address ?*/
- p = index(addr, ':');
- if (p == NULL) {
- ret = parse_ipv4(addr, port, &saddr->ip);
- } else {
- ret = parse_ipv6(addr, ifaces, port, saddr);
- }
-
- return ret;
-}
-
-/*
- parse a ip/mask pair
- */
-bool parse_ip_mask(const char *str, const char *ifaces, ctdb_sock_addr *addr, unsigned *mask)
-{
- TALLOC_CTX *tmp_ctx = talloc_new(NULL);
- char *s, *p;
- char *endp = NULL;
- bool ret;
-
- ZERO_STRUCT(*addr);
- s = talloc_strdup(tmp_ctx, str);
- if (s == NULL) {
- DEBUG(DEBUG_ERR, (__location__ " Failed strdup()\n"));
- talloc_free(tmp_ctx);
- return false;
- }
-
- p = rindex(s, '/');
- if (p == NULL) {
- DEBUG(DEBUG_ERR, (__location__ " This addr: %s does not contain a mask\n", s));
- talloc_free(tmp_ctx);
- return false;
- }
-
- *mask = strtoul(p+1, &endp, 10);
- if (endp == NULL || *endp != 0) {
- /* trailing garbage */
- DEBUG(DEBUG_ERR, (__location__ " Trailing garbage after the mask in %s\n", s));
- talloc_free(tmp_ctx);
- return false;
- }
- *p = 0;
-
-
- /* now is this a ipv4 or ipv6 address ?*/
- ret = parse_ip(s, ifaces, 0, addr);
-
- talloc_free(tmp_ctx);
- return ret;
-}
-
/*
This is used to canonicalize a ctdb_sock_addr structure.
*/
#endif
cip->ip.sin_family = AF_INET;
cip->ip.sin_port = ip->ip6.sin6_port;
- memcpy(&cip->ip.sin_addr, &ip->ip6.sin6_addr.s6_addr32[3], 4);
+ memcpy(&cip->ip.sin_addr, &ip->ip6.sin6_addr.s6_addr[12], 4);
}
}
break;
default:
DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family %u\n", addr->sa.sa_family));
+ ctdb_external_trace();
}
return cip;
return 0;
}
-void ctdb_block_signal(int signum)
-{
- sigset_t set;
- sigemptyset(&set);
- sigaddset(&set,signum);
- sigprocmask(SIG_BLOCK,&set,NULL);
-}
-void ctdb_unblock_signal(int signum)
-{
- sigset_t set;
- sigemptyset(&set);
- sigaddset(&set,signum);
- sigprocmask(SIG_UNBLOCK,&set,NULL);
-}
+const char *ctdb_eventscript_call_names[] = {
+ "init",
+ "setup",
+ "startup",
+ "startrecovery",
+ "recovered",
+ "takeip",
+ "releaseip",
+ "stopped",
+ "monitor",
+ "status",
+ "shutdown",
+ "reload",
+ "updateip",
+ "ipreallocated"
+};
-struct debug_levels debug_levels[] = {
- {DEBUG_EMERG, "EMERG"},
- {DEBUG_ALERT, "ALERT"},
- {DEBUG_CRIT, "CRIT"},
- {DEBUG_ERR, "ERR"},
- {DEBUG_WARNING, "WARNING"},
- {DEBUG_NOTICE, "NOTICE"},
- {DEBUG_INFO, "INFO"},
- {DEBUG_DEBUG, "DEBUG"},
- {0, NULL}
+/* Runstate handling */
+static struct {
+ enum ctdb_runstate runstate;
+ const char * label;
+} runstate_map[] = {
+ { CTDB_RUNSTATE_UNKNOWN, "UNKNOWN" },
+ { CTDB_RUNSTATE_INIT, "INIT" },
+ { CTDB_RUNSTATE_SETUP, "SETUP" },
+ { CTDB_RUNSTATE_FIRST_RECOVERY, "FIRST_RECOVERY" },
+ { CTDB_RUNSTATE_STARTUP, "STARTUP" },
+ { CTDB_RUNSTATE_RUNNING, "RUNNING" },
+ { CTDB_RUNSTATE_SHUTDOWN, "SHUTDOWN" },
+ { -1, NULL },
};
-const char *get_debug_by_level(int32_t level)
+const char *runstate_to_string(enum ctdb_runstate runstate)
{
int i;
-
- for (i=0; debug_levels[i].description != NULL; i++) {
- if (debug_levels[i].level == level) {
- return debug_levels[i].description;
+ for (i=0; runstate_map[i].label != NULL ; i++) {
+ if (runstate_map[i].runstate == runstate) {
+ return runstate_map[i].label;
}
}
- return "Unknown";
+
+ return runstate_map[0].label;
}
-int32_t get_debug_by_desc(const char *desc)
+enum ctdb_runstate runstate_from_string(const char *label)
{
int i;
-
- for (i=0; debug_levels[i].description != NULL; i++) {
- if (!strcmp(debug_levels[i].description, desc)) {
- return debug_levels[i].level;
+ for (i=0; runstate_map[i].label != NULL; i++) {
+ if (strcasecmp(runstate_map[i].label, label) == 0) {
+ return runstate_map[i].runstate;
}
}
- return DEBUG_ERR;
+ return CTDB_RUNSTATE_UNKNOWN;
}
-/* we don't lock future pages here; it would increase the chance that
- * we'd fail to mmap later on. */
-void ctdb_lockdown_memory(struct ctdb_context *ctdb)
+void ctdb_set_runstate(struct ctdb_context *ctdb, enum ctdb_runstate runstate)
{
-#ifdef HAVE_MLOCKALL
- /* Extra stack, please! */
- char dummy[10000];
- memset(dummy, 0, sizeof(dummy));
-
- if (ctdb->valgrinding) {
- return;
+ if (runstate <= ctdb->runstate) {
+ ctdb_fatal(ctdb, "runstate must always increase");
}
- /* Avoid compiler optimizing out dummy. */
- mlock(dummy, sizeof(dummy));
- if (mlockall(MCL_CURRENT) != 0) {
- DEBUG(DEBUG_WARNING,("Failed to lock memory: %s'\n",
- strerror(errno)));
- }
-#endif
+ DEBUG(DEBUG_NOTICE,("Set runstate to %s (%d)\n",
+ runstate_to_string(runstate), runstate));
+ ctdb->runstate = runstate;
}
-const char *ctdb_eventscript_call_names[] = {
- "init",
- "setup",
- "startup",
- "startrecovery",
- "recovered",
- "takeip",
- "releaseip",
- "stopped",
- "monitor",
- "status",
- "shutdown",
- "reload",
- "updateip",
- "ipreallocated"
-};
+/* Convert arbitrary data to 4-byte boundary padded uint32 array */
+uint32_t *ctdb_key_to_idkey(TALLOC_CTX *mem_ctx, TDB_DATA key)
+{
+ uint32_t idkey_size, *k;
+
+ idkey_size = 1 + (key.dsize + sizeof(uint32_t)-1) / sizeof(uint32_t);
+
+ k = talloc_zero_array(mem_ctx, uint32_t, idkey_size);
+ if (k == NULL) {
+ return NULL;
+ }
+
+ k[0] = idkey_size;
+ memcpy(&k[1], key.dptr, key.dsize);
+
+ return k;
+}