1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-22 13:34:15 +03:00

ctdb-conf: add ctdb_read_nodes_cmd function

Add ctdb_read_nodes_cmd a function that works similarly to
ctdb_read_nodes_file but reads the nodes list from the stdout of a
subprocess instead of a file in the file system.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
John Mulligan 2024-06-06 10:00:10 -04:00 committed by Martin Schwenke
parent 46215ab1b3
commit bab5170528

View File

@ -29,6 +29,7 @@
#include <talloc.h>
#include "lib/util/util_file.h"
#include "lib/util/util_strlist.h"
#include "protocol/protocol.h"
#include "protocol/protocol_util.h"
@ -95,12 +96,10 @@ static bool node_map_add(struct ctdb_node_map *nodemap,
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)
static struct ctdb_node_map *ctdb_parse_nodes_lines(TALLOC_CTX *mem_ctx,
char **lines,
int nlines)
{
char **lines = NULL;
int nlines;
int i;
struct ctdb_node_map *nodemap = NULL;
@ -109,11 +108,6 @@ static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
return NULL;
}
lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
if (lines == NULL) {
return NULL;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
@ -158,12 +152,67 @@ static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
node = line;
}
if (!node_map_add(nodemap, node, flags)) {
talloc_free(lines);
TALLOC_FREE(nodemap);
return NULL;
}
}
return nodemap;
}
/* Convert a string containing a command line to an array of strings. Does not
* handle shell style quoting! A space will always create a new argument.
*/
static char **command_str_to_args(TALLOC_CTX *mem_ctx,
const char *argstring)
{
return str_list_make(mem_ctx, argstring, " \t");
}
/* 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 = NULL;
int nlines;
struct ctdb_node_map *nodemap = NULL;
lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
if (lines == NULL) {
return NULL;
}
nodemap = ctdb_parse_nodes_lines(mem_ctx, lines, nlines);
talloc_free(lines);
return nodemap;
}
/* Read a nodes file from an external process into a node map */
static struct ctdb_node_map *ctdb_read_nodes_cmd(TALLOC_CTX *mem_ctx,
const char *nodes_cmd)
{
char **lines = NULL;
int nlines;
char *p;
size_t size;
struct ctdb_node_map *nodemap = NULL;
char **argl = command_str_to_args(mem_ctx, nodes_cmd);
if (argl == NULL) {
return NULL;
}
p = file_ploadv(argl, &size);
if (!p) {
return NULL;
}
lines = file_lines_parse(p, size, &nlines, mem_ctx);
talloc_free(p);
if (lines == NULL) {
return NULL;
}
nodemap = ctdb_parse_nodes_lines(mem_ctx, lines, nlines);
talloc_free(lines);
return nodemap;
}
@ -186,7 +235,11 @@ struct ctdb_node_map *ctdb_read_nodes(TALLOC_CTX *mem_ctx,
{
struct ctdb_node_map* nodemap = NULL;
nodemap = ctdb_read_nodes_file(mem_ctx, location);
if (location != NULL && location[0] == '!') {
nodemap = ctdb_read_nodes_cmd(mem_ctx, &location[1]);
} else {
nodemap = ctdb_read_nodes_file(mem_ctx, location);
}
return nodemap;
}