]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-conf: add ctdb_read_nodes_cmd function
authorJohn Mulligan <jmulligan@redhat.com>
Thu, 6 Jun 2024 14:00:10 +0000 (10:00 -0400)
committerMartin Schwenke <martins@samba.org>
Tue, 6 Aug 2024 00:43:36 +0000 (00:43 +0000)
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>
ctdb/conf/node.c

index 082b68b0d711abd3ddbef88737fc2ed831aa8288..a242c52dfd6f16262d4efdeed83d2ee70b1fd8da 100644 (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;
 }