4 Copyright (C) Martin Andrew Tridgell 2007
5 Copyright (C) Martin Ronnie Sahlberg 2008, 2009
6 Copyright (C) Martin Schwenke 2015
7 Copyright (C) Amitay Isaacs 2015
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #ifndef __CTDB_NODE_H__
24 #define __CTDB_NODE_H__
27 #include "system/network.h"
31 #include "lib/util/util_file.h"
32 #include "lib/util/util_strlist.h"
34 #include "protocol/protocol.h"
35 #include "protocol/protocol_util.h"
37 #include "conf/node.h"
40 /* If unset, set port in address to the CTDB port */
41 static void node_set_port(ctdb_sock_addr
*address
)
43 struct servent
*se
= NULL
;
46 port
= ctdb_sock_addr_port(address
);
52 se
= getservbyname("ctdb", "tcp");
58 port
= ntohs(se
->s_port
);
61 ctdb_sock_addr_set_port(address
, port
);
64 /* Append a node to a node map with given address and flags */
65 static bool node_map_add(struct ctdb_node_map
*nodemap
,
69 ctdb_sock_addr addr
= {};
71 struct ctdb_node_and_flags
*n
= NULL
;
74 ok
= ctdb_parse_node_address(nstr
, &addr
);
76 fprintf(stderr
, "Invalid node address %s\n", nstr
);
81 n
= talloc_realloc(nodemap
,
83 struct ctdb_node_and_flags
,
90 n
= &nodemap
->node
[num
];
95 nodemap
->num
= num
+ 1;
99 static struct ctdb_node_map
*ctdb_parse_nodes_lines(TALLOC_CTX
*mem_ctx
,
104 struct ctdb_node_map
*nodemap
= NULL
;
106 nodemap
= talloc_zero(mem_ctx
, struct ctdb_node_map
);
107 if (nodemap
== NULL
) {
111 while (nlines
> 0 && strcmp(lines
[nlines
-1], "") == 0) {
115 for (i
= 0; i
< nlines
; i
++) {
117 const char *node
= NULL
;
122 /* strip leading spaces */
123 while((*line
== ' ') || (*line
== '\t')) {
129 /* strip trailing spaces */
131 (line
[len
- 1] == ' ' || line
[len
- 1] == '\t')) {
133 line
[len
- 1] = '\0';
142 * A "deleted" node is a node that is
143 * commented out in the nodes file. This is
144 * used instead of removing a line, which
145 * would cause subsequent nodes to change
148 flags
= NODE_FLAGS_DELETED
;
154 if (!node_map_add(nodemap
, node
, flags
)) {
155 TALLOC_FREE(nodemap
);
163 /* Convert a string containing a command line to an array of strings. Does not
164 * handle shell style quoting! A space will always create a new argument.
166 static char **command_str_to_args(TALLOC_CTX
*mem_ctx
,
167 const char *argstring
)
169 return str_list_make(mem_ctx
, argstring
, " \t");
172 /* Read a nodes file into a node map */
173 static struct ctdb_node_map
*ctdb_read_nodes_file(TALLOC_CTX
*mem_ctx
,
178 struct ctdb_node_map
*nodemap
= NULL
;
180 lines
= file_lines_load(nlist
, &nlines
, 0, mem_ctx
);
185 nodemap
= ctdb_parse_nodes_lines(mem_ctx
, lines
, nlines
);
190 /* Read a nodes file from an external process into a node map */
191 static struct ctdb_node_map
*ctdb_read_nodes_cmd(TALLOC_CTX
*mem_ctx
,
192 const char *nodes_cmd
)
198 struct ctdb_node_map
*nodemap
= NULL
;
199 char **argl
= command_str_to_args(mem_ctx
, nodes_cmd
);
204 p
= file_ploadv(argl
, &size
);
209 lines
= file_lines_parse(p
, size
, &nlines
, mem_ctx
);
215 nodemap
= ctdb_parse_nodes_lines(mem_ctx
, lines
, nlines
);
220 bool ctdb_parse_node_address(const char *str
, ctdb_sock_addr
*address
)
224 ret
= ctdb_sock_addr_from_string(str
, address
, false);
228 node_set_port(address
);
233 struct ctdb_node_map
*ctdb_read_nodes(TALLOC_CTX
*mem_ctx
,
234 const char *location
)
236 struct ctdb_node_map
* nodemap
= NULL
;
238 if (location
!= NULL
&& location
[0] == '!') {
239 nodemap
= ctdb_read_nodes_cmd(mem_ctx
, &location
[1]);
241 nodemap
= ctdb_read_nodes_file(mem_ctx
, location
);
247 #endif /* __CTDB_NODE_H__ */