diff --git a/src/bin/initdb/initdb.cpp b/src/bin/initdb/initdb.cpp index aa59059cd..4cf439253 100644 --- a/src/bin/initdb/initdb.cpp +++ b/src/bin/initdb/initdb.cpp @@ -57,6 +57,7 @@ #include "getopt_long.h" #include "miscadmin.h" #include "bin/elog.h" +#include "pgxc/nodemgr.h" #define PROG_NAME "gs_initdb" @@ -248,6 +249,7 @@ static void setup_perfviews(void); static void setup_update(void); #ifdef PGXC static void setup_nodeself(void); +static bool is_valid_nodename(const char* nodename); #endif static void setup_description(void); static void setup_collation(void); @@ -1815,6 +1817,40 @@ static void setup_nodeself(void) check_ok(); } + +static bool is_valid_nodename(const char* nodename) +{ + /* + * The node name must contain lowercase letters (a-z), underscores (_), + * special characters #, digits (0-9), or dollar ($). + * + * The node name must start with a lowercase letter (a-z), or an underscore (_). + * + * The max length of nodename is 64. + */ + int len = strlen(nodename); + if (len <= 0 || len > PGXC_NODENAME_LENGTH) { + return false; + } + + for (int i = 0; i < len; i++) { + char c = nodename[i]; + if (c == '_' || (c >= 'a' && c <= 'z')) { + continue; + } + + if (i == 0) { + return false; + } + + if ((c >= '0' && c <= '9') || c == '#' || c == '$') { + continue; + } + + return false; + } + return true; +} #endif /* @@ -3444,6 +3480,15 @@ int main(int argc, char* argv[]) write_stderr(_("Try \"%s --help\" for more information.\n"), progname); exit(1); } + + if (!is_valid_nodename(nodename)) { + write_stderr(_("%s: Postgres-XC node name:%s is invalid.\nThe node name must consist of lowercase letters " + "(a-z), underscores (_), special characters #, digits (0-9), or dollar ($).\n" + "The node name must start with a lowercase letter (a-z)," + " or an underscore (_).\nThe max length of nodename is %d.\n"), progname, nodename, PGXC_NODENAME_LENGTH); + exit(1); + } + #endif check_authmethod_unspecified(&authmethodlocal); diff --git a/src/test/regress/input/initdb.source b/src/test/regress/input/initdb.source new file mode 100644 index 000000000..6c36bd7ab --- /dev/null +++ b/src/test/regress/input/initdb.source @@ -0,0 +1,7 @@ +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_node$#A999'; +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_node$#!999'; +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='#ingle_node$#999'; +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='$ingle_node$#999'; +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='9ingle_node$#999'; +--nodename's lenth is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_nodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'; diff --git a/src/test/regress/output/initdb.source b/src/test/regress/output/initdb.source new file mode 100644 index 000000000..02d852db1 --- /dev/null +++ b/src/test/regress/output/initdb.source @@ -0,0 +1,31 @@ +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_node$#A999'; +gs_initdb: Postgres-XC node name:single_node$#A999 is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_node$#!999'; +gs_initdb: Postgres-XC node name:single_node$#!999 is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='#ingle_node$#999'; +gs_initdb: Postgres-XC node name:#ingle_node$#999 is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='$ingle_node$#999'; +gs_initdb: Postgres-XC node name:$ingle_node$#999 is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='9ingle_node$#999'; +gs_initdb: Postgres-XC node name:9ingle_node$#999 is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. +--nodename's lenth is 64. +\! @abs_bindir@/gs_initdb -w 'openGauss@123' -D @cndata@/single_nod --nodename='single_nodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'; +gs_initdb: Postgres-XC node name:single_nodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd is invalid. +The node name must consist of lowercase letters (a-z), underscores (_), special characters #, digits (0-9), or dollar ($). +The node name must start with a lowercase letter (a-z), or an underscore (_). +The max length of nodename is 64. diff --git a/src/test/regress/parallel_schedule10 b/src/test/regress/parallel_schedule10 index 5edc71597..786b5c816 100644 --- a/src/test/regress/parallel_schedule10 +++ b/src/test/regress/parallel_schedule10 @@ -77,3 +77,4 @@ test: with test: partition_foreign_key test: partition_trigger test: duplicate_column_index +test: initdb