Ack.

Thanks,
Ramesh.

On 3/14/2017 7:14 PM, Anders Widell wrote:
>   src/base/conf.cc    |  66 
> +++++++++++++++++++++++++++++++++++++++++-----------
>   src/base/conf.h     |   5 ++++
>   src/nid/nodeinit.cc |   5 ++++
>   3 files changed, 62 insertions(+), 14 deletions(-)
>
>
> Read the fully qualified domain name once and store it in the local file 
> system.
> This will solve problems with slow DNS responses, since the DNS system will 
> only
> be queried once.
>
> diff --git a/src/base/conf.cc b/src/base/conf.cc
> --- a/src/base/conf.cc
> +++ b/src/base/conf.cc
> @@ -56,7 +56,14 @@ void Conf::InitFullyQualifiedDomainName(
>     Lock lock(i->mutex_);
>     if (i->fully_qualified_domain_name_initialized_ == false) {
>       i->fully_qualified_domain_name_ =
> -        GetFullyQualifiedDomainName(i->short_host_name_);
> +        ReadFile(PKGLOCALSTATEDIR "/fully_qualified_domain_name",
> +                 NI_MAXHOST - 1, "");
> +    if (i->fully_qualified_domain_name_.empty()) {
> +      i->fully_qualified_domain_name_ =
> +          GetFullyQualifiedDomainName(i->short_host_name_);
> +      WriteFileAtomically(PKGLOCALSTATEDIR "/fully_qualified_domain_name",
> +                i->fully_qualified_domain_name_);
> +    }
>       i->fully_qualified_domain_name_initialized_ = true;
>     }
>   }
> @@ -114,10 +121,12 @@ std::string Conf::GetFullyQualifiedDomai
>                                host, sizeof(host), nullptr, 0, 0);
>           if (rc == 0) {
>             TRACE("getnameinfo() successful, hostname='%s'", host);
> -          if (strncmp(short_host_name.c_str(), host, short_host_name.size()) 
> == 0 &&
> -              host[short_host_name.size()] == '.') {
> +          std::string::size_type size = short_host_name.size();
> +          if (size == 0 ||
> +              (strncmp(short_host_name.c_str(), host, size) == 0 &&
> +               host[size] == '.')) {
>               fqdn = host;
> -            break;
> +            if (strchr(host, '.') != nullptr) break;
>             }
>           } else {
>             TRACE("getnameinfo() failed with error %d", rc);
> @@ -132,22 +141,15 @@ std::string Conf::GetFullyQualifiedDomai
>   }
>   
>   std::string Conf::GetNodeName(const std::string& short_host_name) {
> -  std::string node_name;
> -  std::ifstream str;
> -  str.width(255);
> -  try {
> -    str.open(PKGSYSCONFDIR "/node_name");
> -    str >> node_name;
> -  } catch (std::ifstream::failure) {
> -    node_name.clear();
> -  }
> -  return (str.fail() || node_name.empty()) ? short_host_name : node_name;
> +  return ReadFile(PKGSYSCONFDIR "/node_name", 255, short_host_name);
>   }
>   
>   std::string Conf::GetShortHostName() {
>     char short_host_name[HOST_NAME_MAX + 1];
>     if (gethostname(short_host_name, sizeof(short_host_name)) == 0) {
>       short_host_name[sizeof(short_host_name) - 1] = '\0';
> +    char* dot = strchr(short_host_name, '.');
> +    if (dot != nullptr) *dot = '\0';
>     } else {
>       LOG_ER("gethostname() failed, errno=%d", errno);
>       short_host_name[0] = '\0';
> @@ -155,4 +157,40 @@ std::string Conf::GetShortHostName() {
>     return short_host_name;
>   }
>   
> +std::string Conf::ReadFile(const std::string& path_name,
> +                           std::string::size_type max_length,
> +                           const std::string& default_contents) {
> +  std::string contents;
> +  std::ifstream str;
> +  str.width(max_length);
> +  try {
> +    str.open(path_name);
> +    str >> contents;
> +  } catch (std::ifstream::failure) {
> +    contents.clear();
> +  }
> +  return (str.fail() || contents.empty()) ? default_contents : contents;
> +}
> +
> +void Conf::WriteFileAtomically(const std::string& path_name,
> +                               const std::string& contents) {
> +  std::string tmp_file = path_name + "." + std::to_string(getpid());
> +  std::ofstream str;
> +  bool success = true;
> +  try {
> +    str.open(tmp_file, std::ofstream::out | std::ofstream::trunc);
> +    str << contents << std::endl;
> +  } catch (std::ofstream::failure) {
> +    success = false;
> +  }
> +  str.close();
> +  if (success && !str.fail()) {
> +    if (link(tmp_file.c_str(), path_name.c_str()) != 0) {
> +      TRACE("link('%s', '%s') failed with errno %d", tmp_file.c_str(),
> +            path_name.c_str(), errno);
> +    }
> +  }
> +  unlink(tmp_file.c_str());
> +}
> +
>   }  // namespace base
> diff --git a/src/base/conf.h b/src/base/conf.h
> --- a/src/base/conf.h
> +++ b/src/base/conf.h
> @@ -88,6 +88,11 @@ class Conf {
>         const std::string& short_host_name);
>     static std::string GetNodeName(const std::string& short_host_name);
>     static std::string GetShortHostName();
> +  static std::string ReadFile(const std::string& path_name,
> +                           std::string::size_type max_length,
> +                           const std::string& default_contents);
> +  static void WriteFileAtomically(const std::string& path_name,
> +                                  const std::string& contents);
>     static pthread_once_t once_control_;
>     static Conf* instance_;
>     std::string fully_qualified_domain_name_;
> diff --git a/src/nid/nodeinit.cc b/src/nid/nodeinit.cc
> --- a/src/nid/nodeinit.cc
> +++ b/src/nid/nodeinit.cc
> @@ -70,6 +70,7 @@
>   #include <cerrno>
>   #include <cstdio>
>   
> +#include "base/conf.h"
>   #include "base/osaf_poll.h"
>   #include "base/osaf_time.h"
>   
> @@ -1646,6 +1647,10 @@ int main(int argc, char *argv[])
>               exit(EXIT_FAILURE);
>       }
>   
> +        // Make sure /var/lib/opensaf/fully_qualified_host_name is
> +        // created.
> +        base::Conf::InitFullyQualifiedDomainName();
> +
>       if (create_svc_monitor_thread() != NCSCC_RC_SUCCESS) {
>               LOG_ER("Failed to create service monitor thread, exiting");
>               exit(EXIT_FAILURE);


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to