gpt4 book ai didi

linux - 如何防止 gethostid 在 linux 上进行 DNS 查找

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:39:25 26 4
gpt4 key购买 nike

我们的应用程序 (RHEL 5/c++) 使用 gethostid 返回的 hostid 进行日志记录。由于某种原因,本地网络环境的主DNS服务器掉线了。这导致 gethostid 出现大量问题:函数调用挂起超过 60 秒,这导致我们的应用程序内部超时。几分钟后,在命令行上调用 hostid 也没有返回。一旦 DNS 服务器再次启动,应用程序和 hostid 命令行工具中的超时/问题都消失了。

我的问题是:如何防止 gethostid 进行 DNS 查找?答案有一些边界条件:

  • 文件 /etc/hostid 不能存在。
  • 不允许调用 sethostid
  • 无法更改 /etc/hosts

我很惊讶这种事情发生了。据我了解 gethostid 它的工作原理是这样的:

  1. 如果已手动设置,则返回最后一个 sethostid 的值。
  2. 如果文件存在,则返回 hostid 形式的/etc/hostid。
  3. 返回主机的主 IP(如果已设置)。
  4. 其他情况未通过。

我认为不需要 DNS 查询。

要验证 gethostid 实际上依赖于工作中的 DNS 服务器,试试这个:

  • 以 root 身份创建/更改您的/etc/reslov.conf,使其仅包含无效的 nameserver 条目。
  • 在命令行上调用 hostid

在我的 debian/squeeze 安装中,这导致 hostid 为 00000000 而没有任何挂起。我假设 hostid 的 RedHat 版本不同/较旧,结果挂起。

最佳答案

我认为在不破坏系统或违反其中一个边界条件的情况下,阻止 gethostid 的 DNS 查找是不可能的。在 gnu.org我在 sethostid 函数上找到了这条评论:

The proper way to establish the primary IP address of a system is to configure the IP address resolver to associate that IP address with the system's host name as returned by gethostname. For example, put a record for the system in /etc/hosts.

据此我得出结论,gethostid 像这样确定 IP:

  • gethostname 获取主机名。
  • 通过 gethostbyname(或类似方法)确定 IP。

在主机名未与 /etc/hosts/etc/nsswitch.conf 中的 IP 地址相关联的情况下,允许 DNS 查找,一个 DNS查找将由 gethostid 进行。

关于linux - 如何防止 gethostid 在 linux 上进行 DNS 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9258228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com