ctdb-conf: Add a common node address handling module
These functions are intended to be used in ctdbd, the ctdb tool and
fake_ctdbd, replacing the different copies in each place.
ctdb_read_nodes() will replace ctdb_read_nodes_file(). The name
change is intentional - in future the location may be something other
than a simple filename.
The static copies of ctdb_read_nodes_file() and node_map_add() are
slightly sanitised versions of those in tools/ctdb.c, with a call to
ctdb_parse_node_address(). A bit more care is taken in node_map_add()
to avoid undefined behaviour if talloc_realloc() fails.
ctdb_parse_node_address() will replace ctdb_parse_address(). There is
an obvious argument change, since the ctdb context argument was
unused. It can only fail on an invalid node address, so return a
bool. This function might be changed later to allow the input address
string to include an optional port.
Where to put this module isn't entirely clear. It could go in common,
so be part of ctdb-util. However, if it later needs
ctdb-conf (e.g. to allow the node list location to be configurable)
then there would be a direct cyclic dependency. This is configuration
handling, so conf/ seems sane. However, I didn't want to put it into
the ctdb-conf target, since some code might need to parse a nodes list
but not need to parse ctdb.conf.
Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
2024-07-05 10:34:09 +03:00
/*
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"
2024-06-06 17:00:10 +03:00
# include "lib/util/util_strlist.h"
ctdb-conf: Add a common node address handling module
These functions are intended to be used in ctdbd, the ctdb tool and
fake_ctdbd, replacing the different copies in each place.
ctdb_read_nodes() will replace ctdb_read_nodes_file(). The name
change is intentional - in future the location may be something other
than a simple filename.
The static copies of ctdb_read_nodes_file() and node_map_add() are
slightly sanitised versions of those in tools/ctdb.c, with a call to
ctdb_parse_node_address(). A bit more care is taken in node_map_add()
to avoid undefined behaviour if talloc_realloc() fails.
ctdb_parse_node_address() will replace ctdb_parse_address(). There is
an obvious argument change, since the ctdb context argument was
unused. It can only fail on an invalid node address, so return a
bool. This function might be changed later to allow the input address
string to include an optional port.
Where to put this module isn't entirely clear. It could go in common,
so be part of ctdb-util. However, if it later needs
ctdb-conf (e.g. to allow the node list location to be configurable)
then there would be a direct cyclic dependency. This is configuration
handling, so conf/ seems sane. However, I didn't want to put it into
the ctdb-conf target, since some code might need to parse a nodes list
but not need to parse ctdb.conf.
Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
2024-07-05 10:34:09 +03:00
# 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 ;
}
2024-06-06 17:00:10 +03:00
static struct ctdb_node_map * ctdb_parse_nodes_lines ( TALLOC_CTX * mem_ctx ,
char * * lines ,
int nlines )
ctdb-conf: Add a common node address handling module
These functions are intended to be used in ctdbd, the ctdb tool and
fake_ctdbd, replacing the different copies in each place.
ctdb_read_nodes() will replace ctdb_read_nodes_file(). The name
change is intentional - in future the location may be something other
than a simple filename.
The static copies of ctdb_read_nodes_file() and node_map_add() are
slightly sanitised versions of those in tools/ctdb.c, with a call to
ctdb_parse_node_address(). A bit more care is taken in node_map_add()
to avoid undefined behaviour if talloc_realloc() fails.
ctdb_parse_node_address() will replace ctdb_parse_address(). There is
an obvious argument change, since the ctdb context argument was
unused. It can only fail on an invalid node address, so return a
bool. This function might be changed later to allow the input address
string to include an optional port.
Where to put this module isn't entirely clear. It could go in common,
so be part of ctdb-util. However, if it later needs
ctdb-conf (e.g. to allow the node list location to be configurable)
then there would be a direct cyclic dependency. This is configuration
handling, so conf/ seems sane. However, I didn't want to put it into
the ctdb-conf target, since some code might need to parse a nodes list
but not need to parse ctdb.conf.
Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
2024-07-05 10:34:09 +03:00
{
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 ;
}
}
2024-06-06 17:00:10 +03:00
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 ) ;
ctdb-conf: Add a common node address handling module
These functions are intended to be used in ctdbd, the ctdb tool and
fake_ctdbd, replacing the different copies in each place.
ctdb_read_nodes() will replace ctdb_read_nodes_file(). The name
change is intentional - in future the location may be something other
than a simple filename.
The static copies of ctdb_read_nodes_file() and node_map_add() are
slightly sanitised versions of those in tools/ctdb.c, with a call to
ctdb_parse_node_address(). A bit more care is taken in node_map_add()
to avoid undefined behaviour if talloc_realloc() fails.
ctdb_parse_node_address() will replace ctdb_parse_address(). There is
an obvious argument change, since the ctdb context argument was
unused. It can only fail on an invalid node address, so return a
bool. This function might be changed later to allow the input address
string to include an optional port.
Where to put this module isn't entirely clear. It could go in common,
so be part of ctdb-util. However, if it later needs
ctdb-conf (e.g. to allow the node list location to be configurable)
then there would be a direct cyclic dependency. This is configuration
handling, so conf/ seems sane. However, I didn't want to put it into
the ctdb-conf target, since some code might need to parse a nodes list
but not need to parse ctdb.conf.
Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
2024-07-05 10:34:09 +03:00
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 ;
2024-06-06 17:00:10 +03:00
if ( location ! = NULL & & location [ 0 ] = = ' ! ' ) {
nodemap = ctdb_read_nodes_cmd ( mem_ctx , & location [ 1 ] ) ;
} else {
nodemap = ctdb_read_nodes_file ( mem_ctx , location ) ;
}
ctdb-conf: Add a common node address handling module
These functions are intended to be used in ctdbd, the ctdb tool and
fake_ctdbd, replacing the different copies in each place.
ctdb_read_nodes() will replace ctdb_read_nodes_file(). The name
change is intentional - in future the location may be something other
than a simple filename.
The static copies of ctdb_read_nodes_file() and node_map_add() are
slightly sanitised versions of those in tools/ctdb.c, with a call to
ctdb_parse_node_address(). A bit more care is taken in node_map_add()
to avoid undefined behaviour if talloc_realloc() fails.
ctdb_parse_node_address() will replace ctdb_parse_address(). There is
an obvious argument change, since the ctdb context argument was
unused. It can only fail on an invalid node address, so return a
bool. This function might be changed later to allow the input address
string to include an optional port.
Where to put this module isn't entirely clear. It could go in common,
so be part of ctdb-util. However, if it later needs
ctdb-conf (e.g. to allow the node list location to be configurable)
then there would be a direct cyclic dependency. This is configuration
handling, so conf/ seems sane. However, I didn't want to put it into
the ctdb-conf target, since some code might need to parse a nodes list
but not need to parse ctdb.conf.
Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Anoop C S <anoopcs@samba.org>
2024-07-05 10:34:09 +03:00
return nodemap ;
}
# endif /* __CTDB_NODE_H__ */