summaryrefslogtreecommitdiff
path: root/ctdb/protocol/protocol_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'ctdb/protocol/protocol_util.c')
-rw-r--r--ctdb/protocol/protocol_util.c86
1 files changed, 49 insertions, 37 deletions
diff --git a/ctdb/protocol/protocol_util.c b/ctdb/protocol/protocol_util.c
index c75555fa734..45c639d747f 100644
--- a/ctdb/protocol/protocol_util.c
+++ b/ctdb/protocol/protocol_util.c
@@ -22,6 +22,8 @@
#include <talloc.h>
+#include "common/line.h"
+
#include "protocol.h"
#include "protocol_util.h"
@@ -603,56 +605,66 @@ const char *ctdb_connection_list_to_string(
return out;
}
-int ctdb_connection_list_read(TALLOC_CTX *mem_ctx, bool client_first,
- struct ctdb_connection_list **conn_list)
-{
+struct ctdb_connection_list_read_state {
struct ctdb_connection_list *list;
- char line[128]; /* long enough for IPv6 */
+ bool client_first;
+};
+
+static int ctdb_connection_list_read_line(char *line, void *private_data)
+{
+ struct ctdb_connection_list_read_state *state =
+ (struct ctdb_connection_list_read_state *)private_data;
+ struct ctdb_connection conn;
int ret;
- if (conn_list == NULL) {
- return EINVAL;
+ /* Skip empty lines */
+ if (line[0] == '\0') {
+ return 0;
}
- list = talloc_zero(mem_ctx, struct ctdb_connection_list);
- if (list == NULL) {
- return ENOMEM;
+ /* Comment */
+ if (line[0] == '#') {
+ return 0;
}
- while (fgets(line, sizeof(line), stdin) != NULL) {
- char *t;
- struct ctdb_connection conn;
+ ret = ctdb_connection_from_string(line, state->client_first, &conn);
+ if (ret != 0) {
+ return ret;
+ }
- /* Skip empty lines */
- if (line[0] == '\n') {
- continue;
- }
+ ret = ctdb_connection_list_add(state->list, &conn);
+ if (ret != 0) {
+ return ret;
+ }
- /* Comment */
- if (line[0] == '#') {
- continue;
- }
+ return 0;
+}
- t = strtok(line, "\n");
- if (t == NULL) {
- goto fail;
- }
+int ctdb_connection_list_read(TALLOC_CTX *mem_ctx, bool client_first,
+ struct ctdb_connection_list **conn_list)
+{
+ struct ctdb_connection_list_read_state state;
+ int ret;
- ret = ctdb_connection_from_string(t, client_first, &conn);
- if (ret != 0) {
- goto fail;
- }
+ if (conn_list == NULL) {
+ return EINVAL;
+ }
- ret = ctdb_connection_list_add(list, &conn);
- if (ret != 0) {
- goto fail;
- }
+ state.list = talloc_zero(mem_ctx, struct ctdb_connection_list);
+ if (state.list == NULL) {
+ return ENOMEM;
}
- *conn_list = list;
- return 0;
+ state.client_first = client_first;
+
+ ret = line_read(0,
+ 128,
+ mem_ctx,
+ ctdb_connection_list_read_line,
+ &state,
+ NULL);
-fail:
- talloc_free(list);
- return EINVAL;
+ *conn_list = state.list;
+
+ return ret;
}