diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt index 29e65e7b..76368ae1 100644 --- a/src/client/CMakeLists.txt +++ b/src/client/CMakeLists.txt @@ -93,7 +93,7 @@ add_executable(test_cluster_client EXCLUDE_FROM_ALL ../test/test_cluster_client.cpp pg_states.cpp osd_ops.cpp cluster_client.cpp cluster_client_list.cpp cluster_client_wb.cpp msgr_op.cpp ../test/mock/messenger.cpp msgr_stop.cpp - etcd_state_client.cpp ../util/timerfd_manager.cpp ../util/str_util.cpp ../util/json_util.cpp ../../json11/json11.cpp + etcd_state_client.cpp ../util/timerfd_manager.cpp ../util/addr_util.cpp ../util/str_util.cpp ../util/json_util.cpp ../../json11/json11.cpp ) target_compile_definitions(test_cluster_client PUBLIC -D__MOCK__) target_include_directories(test_cluster_client BEFORE PUBLIC ${CMAKE_SOURCE_DIR}/src/test/mock) diff --git a/src/osd/osd_cluster.cpp b/src/osd/osd_cluster.cpp index f5583758..39b4f593 100644 --- a/src/osd/osd_cluster.cpp +++ b/src/osd/osd_cluster.cpp @@ -158,18 +158,13 @@ bool osd_t::check_peer_config(osd_client_t *cl, json11::Json conf) json11::Json osd_t::get_osd_state() { - std::vector hostname; - hostname.resize(1024); - while (gethostname(hostname.data(), hostname.size()) < 0 && errno == ENAMETOOLONG) - hostname.resize(hostname.size()+1024); - hostname.resize(strnlen(hostname.data(), hostname.size())); json11::Json::object st; st["state"] = "up"; if (bind_addresses.size() != 1 || bind_addresses[0] != "0.0.0.0") st["addresses"] = bind_addresses; else st["addresses"] = getifaddr_list(); - st["host"] = std::string(hostname.data(), hostname.size()); + st["host"] = gethostname_str(); st["version"] = VITASTOR_VERSION; st["port"] = listening_port; #ifdef WITH_RDMACM diff --git a/src/util/addr_util.cpp b/src/util/addr_util.cpp index 9111b752..a829a2da 100644 --- a/src/util/addr_util.cpp +++ b/src/util/addr_util.cpp @@ -245,3 +245,13 @@ int create_and_bind_socket(std::string bind_address, int bind_port, int listen_b return listen_fd; } + +std::string gethostname_str() +{ + std::string hostname; + hostname.resize(1024); + while (gethostname((char*)hostname.data(), hostname.size()) < 0 && errno == ENAMETOOLONG) + hostname.resize(hostname.size()+1024); + hostname.resize(strnlen(hostname.data(), hostname.size())); + return hostname; +} diff --git a/src/util/addr_util.h b/src/util/addr_util.h index 8d6fac4c..e8a044d3 100644 --- a/src/util/addr_util.h +++ b/src/util/addr_util.h @@ -21,3 +21,4 @@ bool cidr6_match(const in6_addr &address, const in6_addr &network, uint8_t bits) bool cidr_sockaddr_match(const sockaddr_storage &addr, const addr_mask_t &mask); std::vector getifaddr_list(const std::vector & masks = std::vector(), bool include_v6 = false); int create_and_bind_socket(std::string bind_address, int bind_port, int listen_backlog, int *listening_port); +std::string gethostname_str();