1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
Node file loading
Copyright (C) Martin Andrew Tridgell 2007
Copyright (C) Martin Ronnie Sahlberg 2008, 2009
Copyright (C) Martin Schwenke 2015
Copyright (C) Amitay Isaacs 2015
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __CTDB_NODE_H__
#define __CTDB_NODE_H__
#include "replace.h"
#include "system/network.h"
#include <talloc.h>
#include "lib/util/util_file.h"
#include "lib/util/util_strlist.h"
#include "protocol/protocol.h"
#include "protocol/protocol_util.h"
#include "conf/node.h"
/* If unset, set port in address to the CTDB port */
static void node_set_port(ctdb_sock_addr *address)
{
struct servent *se = NULL;
unsigned int port;
port = ctdb_sock_addr_port(address);
if (port != 0) {
return;
}
setservent(0);
se = getservbyname("ctdb", "tcp");
endservent();
if (se == NULL) {
port = CTDB_PORT;
} else {
port = ntohs(se->s_port);
}
ctdb_sock_addr_set_port(address, port);
}
/* Append a node to a node map with given address and flags */
static bool node_map_add(struct ctdb_node_map *nodemap,
const char *nstr,
uint32_t flags)
{
ctdb_sock_addr addr = {};
uint32_t num;
struct ctdb_node_and_flags *n = NULL;
bool ok;
ok = ctdb_parse_node_address(nstr, &addr);
if (!ok) {
fprintf(stderr, "Invalid node address %s\n", nstr);
return false;
}
num = nodemap->num;
n = talloc_realloc(nodemap,
nodemap->node,
struct ctdb_node_and_flags,
num + 1);
if (n == NULL) {
return false;
}
nodemap->node = n;
n = &nodemap->node[num];
n->addr = addr;
n->pnn = num;
n->flags = flags;
nodemap->num = num + 1;
return true;
}
static struct ctdb_node_map *ctdb_parse_nodes_lines(TALLOC_CTX *mem_ctx,
char **lines,
int nlines)
{
int i;
struct ctdb_node_map *nodemap = NULL;
nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
if (nodemap == NULL) {
return NULL;
}
while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
nlines--;
}
for (i = 0; i < nlines; i++) {
char *line;
const char *node = NULL;
uint32_t flags;
size_t len;
line = lines[i];
/* strip leading spaces */
while((*line == ' ') || (*line == '\t')) {
line++;
}
len = strlen(line);
/* strip trailing spaces */
while (len > 1 &&
(line[len - 1] == ' ' || line[len - 1] == '\t')) {
line[len - 1] = '\0';
len--;
}
if (len == 0) {
continue;
}
if (*line == '#') {
/*
* A "deleted" node is a node that is
* commented out in the nodes file. This is
* used instead of removing a line, which
* would cause subsequent nodes to change
* their PNN.
*/
flags = NODE_FLAGS_DELETED;
node = "0.0.0.0";
} else {
flags = 0;
node = line;
}
if (!node_map_add(nodemap, node, flags)) {
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;
struct ctdb_node_map *nodemap = NULL;
char **argl = command_str_to_args(mem_ctx, nodes_cmd);
if (argl == NULL) {
return NULL;
}
lines = file_lines_ploadv(mem_ctx, argl, &nlines);
if (lines == NULL) {
return NULL;
}
nodemap = ctdb_parse_nodes_lines(mem_ctx, lines, nlines);
talloc_free(lines);
return nodemap;
}
bool ctdb_parse_node_address(const char *str, ctdb_sock_addr *address)
{
int ret;
ret = ctdb_sock_addr_from_string(str, address, false);
if (ret != 0) {
return false;
}
node_set_port(address);
return true;
}
struct ctdb_node_map *ctdb_read_nodes(TALLOC_CTX *mem_ctx,
const char *location)
{
struct ctdb_node_map* nodemap = NULL;
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;
}
#endif /* __CTDB_NODE_H__ */
|