1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-21 18:04:06 +03:00

ctdb-daemon: Move ctdb_read_nodes_file() to utilities

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
This commit is contained in:
Martin Schwenke 2015-02-22 06:49:04 +11:00 committed by Amitay Isaacs
parent 1ada9c4ef7
commit 5148228f41
3 changed files with 94 additions and 93 deletions

View File

@ -486,6 +486,98 @@ unsigned ctdb_addr_to_port(ctdb_sock_addr *addr)
return 0;
}
/* Add a node to a node map with given address and flags */
static bool node_map_add(TALLOC_CTX *mem_ctx,
const char *nstr, uint32_t flags,
struct ctdb_node_map **node_map)
{
ctdb_sock_addr addr;
uint32_t num;
size_t s;
struct ctdb_node_and_flags *n;
/* Might as well do this before trying to allocate memory */
if (ctdb_parse_address(mem_ctx, nstr, &addr) == -1) {
return false;
}
num = (*node_map)->num + 1;
s = offsetof(struct ctdb_node_map, nodes) +
num * sizeof(struct ctdb_node_and_flags);
*node_map = talloc_realloc_size(mem_ctx, *node_map, s);
if (*node_map == NULL) {
DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
return false;
}
n = &(*node_map)->nodes[(*node_map)->num];
n->addr = addr;
n->pnn = (*node_map)->num;
n->flags = flags;
(*node_map)->num++;
return true;
}
/* Read a nodes file into a node map */
struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
const char *nlist)
{
char **lines;
int nlines;
int i;
struct ctdb_node_map *ret;
/* Allocate node map header */
ret = talloc_zero_size(mem_ctx, offsetof(struct ctdb_node_map, nodes));
if (ret == NULL) {
DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
return false;
}
lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
if (lines == NULL) {
DEBUG(DEBUG_ERR, ("Failed to read nodes file \"%s\"\n", nlist));
return false;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
for (i=0; i < nlines; i++) {
const char *node;
uint32_t flags;
node = lines[i];
/* strip leading spaces */
while((*node == ' ') || (*node == '\t')) {
node++;
}
if (strcmp(node, "") == 0) {
continue;
}
if (*node == '#') {
/* A "deleted" node is a node that is
commented out in the nodes file. This is
used instead of removing a line, which
would cause subsequent nodes to change
their PNN. */
flags = NODE_FLAGS_DELETED;
node = "0.0.0.0";
} else {
flags = 0;
}
if (!node_map_add(mem_ctx, node, flags, &ret)) {
talloc_free(lines);
TALLOC_FREE(ret);
return NULL;
}
}
talloc_free(lines);
return ret;
}
const char *ctdb_eventscript_call_names[] = {
"init",

View File

@ -1388,6 +1388,8 @@ int ctdb_client_async_control(struct ctdb_context *ctdb,
client_async_callback fail_callback,
void *callback_data);
struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
const char *nlist);
void ctdb_load_nodes_file(struct ctdb_context *ctdb);
int ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode);

View File

@ -76,99 +76,6 @@ int ctdb_set_recovery_lock_file(struct ctdb_context *ctdb, const char *file)
return 0;
}
/* Add a node to a node map with given address and flags */
static bool node_map_add(TALLOC_CTX *mem_ctx,
const char *nstr, uint32_t flags,
struct ctdb_node_map **node_map)
{
ctdb_sock_addr addr;
uint32_t num;
size_t s;
struct ctdb_node_and_flags *n;
/* Might as well do this before trying to allocate memory */
if (ctdb_parse_address(mem_ctx, nstr, &addr) == -1) {
return false;
}
num = (*node_map)->num + 1;
s = offsetof(struct ctdb_node_map, nodes) +
num * sizeof(struct ctdb_node_and_flags);
*node_map = talloc_realloc_size(mem_ctx, *node_map, s);
if (*node_map == NULL) {
DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
return false;
}
n = &(*node_map)->nodes[(*node_map)->num];
n->addr = addr;
n->pnn = (*node_map)->num;
n->flags = flags;
(*node_map)->num++;
return true;
}
/* Read a nodes file into a node map */
static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
const char *nlist)
{
char **lines;
int nlines;
int i;
struct ctdb_node_map *ret;
/* Allocate node map header */
ret = talloc_zero_size(mem_ctx, offsetof(struct ctdb_node_map, nodes));
if (ret == NULL) {
DEBUG(DEBUG_ERR, (__location__ " Out of memory\n"));
return false;
}
lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
if (lines == NULL) {
DEBUG(DEBUG_ERR, ("Failed to read nodes file \"%s\"\n", nlist));
return false;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
for (i=0; i < nlines; i++) {
const char *node;
uint32_t flags;
node = lines[i];
/* strip leading spaces */
while((*node == ' ') || (*node == '\t')) {
node++;
}
if (strcmp(node, "") == 0) {
continue;
}
if (*node == '#') {
/* A "deleted" node is a node that is
commented out in the nodes file. This is
used instead of removing a line, which
would cause subsequent nodes to change
their PNN. */
flags = NODE_FLAGS_DELETED;
node = "0.0.0.0";
} else {
flags = 0;
}
if (!node_map_add(mem_ctx, node, flags, &ret)) {
talloc_free(lines);
TALLOC_FREE(ret);
return NULL;
}
}
talloc_free(lines);
return ret;
}
/* Load a nodes list file into a nodes array */
static int convert_node_map_to_list(struct ctdb_context *ctdb,
TALLOC_CTX *mem_ctx,