getaddrinfo函数返回No such file or directory的分析

本文介绍了一种在嵌入式设备上解决getaddrinfo错误的方法,通过使用strace工具定位问题所在,并添加了缺失的/etc/hosts文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中实现NTP功能模块时,为了更好的兼容和更小的警告,将gethostbyname函数用getaddrinfo函数进行了替代,可是在开发板上运行时却遇到了这个错误:

getaddrinfo fail for host time.windows.com:System error
error:No such file or directory
然而同样的代码程序在PC上运行是正确的。可以确定是开发板上的文件系统环境缺少某个与网络相关的配置文件导致getaddrinfo系统调用执行失败,问题在于是哪一个文件,怎么来找到它?答案是使用strace工具在追踪getaddrinfo在PC上执行是的文件访问记录,从而找出缺少的配置文件。

为了减少干扰,使用一个简单的test程序来进行分析,使用如下命令得到追踪记录:

# strace -e trace=file -o 2.strace ./test_getaddrinfo
追踪记录文件信息如下(为了保持strace文件的语法高亮,这里使用图片):

滤掉程序执行涉及的动态库链接缓存之类的文件(因为程序运行起来了),就只剩下三个文件了:/etc/host.conf  /etc/hosts  /etc/resolv.conf.

/etc/resolv.conf文件是域名解析服务器列表的参数文件,实现域名解析,必须设置域名服务器的地址,开发板上已经配置了。

/etc/hosts 文件是主机地址列表参数文件,指定本机的ip地址和主机名称,是域名解析的优先参考映射,即先查看该文件,然后才查询DNS,开发板上不存在该文件。

于是添加该文件到开发板上,通过以下命令:

# echo "127.0.0.1 localhost" > /etc/hosts
然后再次运行程序,getaddrinfo解析域名成功,不再返回System Error的错误。

为了保证getaddrinfo之类接口的正确运行,可以采用两者途径来确保配置环境的OK:

1. 在制作设备的根文件系统时,保证上述配置文件的存在及正确。

2. 在应用程序中检测,如果不存在就创建配置文件。我在NTP的实现中就采用了这一个方法:

if(access("/etc/hosts",F_OK))
    {// create the file for getaddrinfo api
        system("echo \"127.0.0.1   localhost\" > /etc/hosts");
    }

转载于:https://my.oschina.net/shelllife/blog/297156

Microsoft Windows [版本 10.0.22631.5335] (c) Microsoft Corporation。保留所有权利。 C:\Users\dell>make --version GNU Make 4.1 Built for x86_64-pc-mingw32 Copyright (C) 1988-2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. C:\Users\dell>cmake --version cmake version 3.27.4 CMake suite maintained and supported by Kitware (kitware.com/cmake). C:\Users\dell>gcc --version gcc (Rev3, Built by MSYS2 project) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. C:\Users\dell>arm-linux-gnueabihf-gcc --version arm-linux-gnueabihf-gcc (Linaro GCC 4.9-2017.01) 4.9.4 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. C:\Users\dell>cd /d D:\20241207\KPC_PEPTXDLRI_Windos\build D:\20241207\KPC_PEPTXDLRI_Windos\build>cmake .. -G "MinGW Makefiles" CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required): Compatibility with CMake < 3.5 will be removed from a future version of CMake. Update the VERSION argument <min> value or use a ...<max> suffix to tell CMake that the project does not need compatibility with older versions. -- The C compiler identification is GNU 13.2.0 -- The CXX compiler identification is GNU 13.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: D:/VS CODE anzhuangweizhi/ucrt64/bin/cc.exe - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: D:/VS CODE anzhuangweizhi/ucrt64/bin/c++.exe - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Git version is main: -- Configuring done (9.3s) -- Generating done (0.1s) -- Build files have been written to: D:/20241207/KPC_PEPTXDLRI_Windos/build D:\20241207\KPC_PEPTXDLRI_Windos\build>mingw32-make [ 2%] Building CXX object app_PEPTXDLRI/CMakeFiles/PEPTXDLRI_APP.dir/PEPTXDLRI_console.cpp.obj In file included from D:\20241207\KPC_PEPTXDLRI_Windos\app_PEPTXDLRI\PEPTXDLRI_console.cpp:11: D:/20241207/KPC_PEPTXDLRI_Windos/app_PEPTXDLRI/hvac_if/logic_ptu.h:14:10: fatal error: arpa/inet.h: No such file or directory 14 | #include <arpa/inet.h> | ^~~~~~~~~~~~~ compilation terminated. mingw32-make[2]: *** [app_PEPTXDLRI\CMakeFiles\PEPTXDLRI_APP.dir\build.make:76: app_PEPTXDLRI/CMakeFiles/PEPTXDLRI_APP.dir/PEPTXDLRI_console.cpp.obj] Error 1 mingw32-make[1]: *** [CMakeFiles\Makefile2:117: app_PEPTXDLRI/CMakeFiles/PEPTXDLRI_APP.dir/all] Error 2 mingw32-make: *** [Makefile:90: all] Error 2 D:\20241207\KPC_PEPTXDLRI_Windos\build>
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值