From bab51705280a4b8ce0869d455d5345ed36981e11 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Thu, 6 Jun 2024 10:00:10 -0400 Subject: [PATCH] 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 Reviewed-by: Martin Schwenke --- ctdb/conf/node.c | 77 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/ctdb/conf/node.c b/ctdb/conf/node.c index 082b68b0d71..a242c52dfd6 100644 --- a/ctdb/conf/node.c +++ b/ctdb/conf/node.c @@ -29,6 +29,7 @@ #include #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; }