diff options
author | Håvard Pettersen <havardpe@oath.com> | 2018-06-01 11:55:05 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2018-06-01 11:55:05 +0000 |
commit | 49a310887ce70fd7e7521edcb648c7dac53331dc (patch) | |
tree | fe2f96278df02b551907b1b16620493f597f75e7 /vespalib | |
parent | 5c57852b26126d72b080fb6e0893dc3d633c28c1 (diff) |
fewer fallbacks and better error reporting
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp b/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp index 057d45e8ef5..5c84790a669 100644 --- a/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp +++ b/vespalib/src/apps/vespa-detect-hostname/detect_hostname.cpp @@ -4,6 +4,7 @@ #include <stdlib.h> #include <vespa/vespalib/net/socket_address.h> #include <vespa/vespalib/stllike/string.h> +#include <vespa/vespalib/util/stringfmt.h> #include <set> using vespalib::SocketAddress; @@ -22,14 +23,17 @@ vespalib::string get_hostname() { return SocketAddress::normalize(&result[0]); } -bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_set) { +bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_set, vespalib::string &error_msg) { auto addr_list = SocketAddress::resolve(80, name.c_str()); if (addr_list.empty()) { + error_msg = vespalib::make_string("hostname '%s' could not be resolved", name.c_str()); return false; } for (const SocketAddress &addr: addr_list) { vespalib::string ip_addr = addr.ip_address(); if (ip_set.count(ip_addr) == 0) { + error_msg = vespalib::make_string("hostname '%s' resolves to ip address not owned by this host (%s)", + name.c_str(), ip_addr.c_str()); return false; } } @@ -38,14 +42,21 @@ bool check(const vespalib::string &name, const std::set<vespalib::string> &ip_se int main(int, char **) { auto my_ip_set = make_ip_set(); - std::vector<vespalib::string> list({get_hostname(), "localhost", "127.0.0.1", "::1"}); - for (const vespalib::string &name: list) { - if (check(name, my_ip_set)) { - fprintf(stdout, "%s\n", name.c_str()); - return 0; - } + vespalib::string my_hostname = get_hostname(); + vespalib::string my_hostname_error; + vespalib::string localhost = "localhost"; + vespalib::string localhost_error; + if (check(my_hostname, my_ip_set, my_hostname_error)) { + fprintf(stdout, "%s\n", my_hostname.c_str()); + } else if (check(localhost, my_ip_set, localhost_error)) { + fprintf(stdout, "%s\n", localhost.c_str()); + } else { + fprintf(stderr, "FATAL: hostname detection failed\n"); + fprintf(stderr, " INFO: canonical hostname (from gethostname/getaddrinfo): %s\n", my_hostname.c_str()); + fprintf(stderr, " ERROR: %s\n", my_hostname_error.c_str()); + fprintf(stderr, " INFO: falling back to local hostname: %s\n", localhost.c_str()); + fprintf(stderr, " ERROR: %s\n", localhost_error.c_str()); + return 1; } - fprintf(stderr, "FATAL: hostname detection failed\n"); - // XXX we should explain why it failed - return 1; + return 0; } |