博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ntp移植过程记录
阅读量:4186 次
发布时间:2019-05-26

本文共 5199 字,大约阅读时间需要 17 分钟。

1、目标 把ntp客户端移植到android机顶盒上。ntpdate ntpq可以正常使用

android 有自己的ntp实现,参考(http://blog.csdn.net/zhongshujunqia/article/details/53431212)

为什么还要移植?客户是上帝,有时候客户提需求,不需要理由,比如这个台湾的这个上帝。

2、移植过程。

搜了一通,比较靠谱的的一个博文:http://blog.csdn.net/zgrjkflmkyc/article/details/45098831

2.1下载ntp源码包:ntp-4.2.4p7.tar.gz

为什么用这个版本,因为参考的教程就是这个版本,另外一个版本没编过。从git上下载源码,切到ntp-4.2.4p7这个节点,居然和ntp-4.2.4p7.tar.gz不一样。只好又下了个压缩包版本。

2.2、在ntpd/ntp_loopfilter.c中增加下面的宏定义

#ifndef MOD_NANO

#define MOD_NANO  0x2000
#endif

2.3 把源码中的getaddrinfo(serv, "NTP", &hints, &addrResult);或者getaddrinfo(serv, service, &hints, &addrResult);//service=“ntp”的地方,把ntp替换成端口号"123"

2.4 配置

./configure --prefix=$PWD/install --exec-prefix=$PWD/install --host=arm-linux CC=arm-linux-gcc

交叉编译工具依平台确定,如果你刚好用的是amlogic 905L跑android 4.4 ,改为CC=aarch64-linux-gnu-gcc

2.5编译

make

make install

生成的文件在$PWD/install/bin下面,$PWD/install/lib是空的。

 ls 

bin    lib    man  

ls install/bin/

ntpd  ntpdate  ntpdc  ntp-keygen  ntpq  ntptime  ntptrace  ntp-wait  sntp  tickadj

2.6 把需要的ntpq 、ntpdate复制到目标平台运行。

3、测试

在平台上测试

ntpdate 182.92.12.11                              

19 May 05:17:47 ntpdate[7900]: adjust time server 182.92.12.11 offset -0.137309 sec

把时间调错一点,再运行

ntpdate 182.92.12.11                              
19 May 05:44:41 ntpdate[14251]: step time server 182.92.12.11 offset -144.159316 sec

# ntpq -p                                           

Name or service not known 

是不能正常工作还是因为没有连接服务器?

 

4、常见问题

4.1、运行不起来,缺少连接器

# ntpdate ntp1.aliyun.com                         

/system/bin/sh: ntpdate: No such file or directory

系统的动态链接器与XXX这个程序中的动态链接器的名字或路径不对,readelf -l 可执行文件名查看需要的连接器(解释器)

$ readelf -l install/bin/ntpdate  

Elf file type is EXEC (Executable file)
Entry point 0x4018c8
There are 7 program headers, starting at offset 64
Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000400040 0x0000000000400040
                 0x0000000000000188 0x0000000000000188  R E    8
  INTERP         0x00000000000001c8 0x00000000004001c8 0x00000000004001c8
                 0x000000000000001b 0x000000000000001b  R      1
      [Requesting program interpreter: /lib/ld-linux-aarch64.so.1]
  LOAD           0x0000000000000000 0x0000000000400000 0x0000000000400000
                 0x000000000000af0c 0x000000000000af0c  R E    10000
  LOAD           0x000000000000b000 0x000000000041b000 0x000000000041b000
                 0x0000000000001a38 0x0000000000007c88  RW     10000
  DYNAMIC        0x000000000000b018 0x000000000041b018 0x000000000041b018
                 0x00000000000001d0 0x00000000000001d0  RW     8

...

从交叉编译器目录中把ld-linux-aarch64.so.1考到目标平台/lib/ld-linux-aarch64.so.1,这个目录是固定的。

4.2、缺少依赖库

#ntpdate                                           

ntpdate: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

把对应的动态库从交叉编译器目录中考到平台库文件目录下面。readelf -d bin文件 可以查看依i赖的库文件有哪些,一并拷过去

能不能编译成静态文件。测试加-static仍然会动态编译,不知道是不是编译工具中缺少对应的静态库。

4.3Error : Servname not supported for ai_socktype

root@p201_iptv:/ # ntpdate 120.25.115.19                                       

Error : Servname not supported for ai_socktype
19 May 02:25:27 ntpdate[9194]: can't find host 120.25.115.19

网上搜到两种解决办法,对这里没啥帮助,也许对读者有用,列出来

(1)代码问题,应该把

 

struct addrinfo *myAddrinfo, *curMyAddrinfo, hint;memset(&hint, 0, sizeof(struct addrinfo));hint.ai_family = AF_INET;hint.ai_protocol = AI_PASSIVE;hint.ai_socktype = SOCK_STREAM;

中hint.ai_protocol = AI_PASSIVE;改为hint.ai_protocol = AI_PASSIVE。见:http://stackoverflow.com/questions/5958817/getaddrinfo-error-ai-socktype-not-supported

 

(2)需要在etc/service中加入

 

ntpdate 0.pool.ntp.org

 

and instead of the expected result, I got:

Error : Servname not supported for ai_socktype4 Sep 03:25:02 ntpdate[27310]: can't find host 0.pool.ntp.org

解决办法在etc/service中加入

 

ntp             123/tcpntp             123/udp

 

和这里问题很像,可能是我平台中没有ntp服务,加后没效果。见:http://www.ducea.com/2006/09/11/error-servname-not-supported-for-ai_socktype/

(3)证明了和交叉编译器版本有关,但是无解。

见:http://blog.csdn.net/code_style/article/details/55509358

 

错误提示大意是:ai_socktype不支持 服务名字,cjius与网络通信有关,(3)中定位到了error = getaddrinfo(serv, service, &hints, &addrResult);  这个函数出错的。该函数声明如下

#include <sys/socket.h>

#include <netdb.h>
int getaddrinfo(const char *restrict host,
                const char *restrict service,
                const struct addrinfo *restrict hint,
                struct addrinfo **restrict res);
Returns: 0 if OK, nonzero error code on error
nodename:节点名可以是主机名,也可以是数字地址。(IPV4的10进点分,或是IPV6的16进制)
servname:包含十进制数的端口号或服务名如(ftp,http)
hints:是一个空指针或指向一个addrinfo结构的指针,由调用者填写关于它所想返回的信息类型的线索。
res:存放返回addrinfo结构链表的指针

参考:http://www.cnblogs.com/chinacloud/archive/2011/08/11/2135141.html

这里的错误信息指的应该是这个交叉编译环境的libc实现中,不支持getaddrinfo()第二参数传 服务名进去。那么传端口号试试,改成ntp服务的端口号123,果然可以了。具体修改见2.3 。

4.3 端口占用

ntpdate 182.92.12.11                            

19 May 05:17:35 ntpdate[7848]: the NTP socket is in use, exiting

123端口被占用了。123是ntp专用端口,如果被占用了,很可能是本机开启了ntpd服务,把ntpd关掉,重新运行。

4.4 找不到ntp服务器
# ntpdate ntp1.aliyun.com                           
Error : No address associated with hostname 
19 May 10:42:58 ntpdate[1220]: can't find host ntp1.aliyun.comy

19 May 10:42:58 ntpdate[1220]: no servers can be used, exiting

错误原因和4.3类似,getaddrinfo第一个参数不支持传域名进去,只支持传点分十进制ip。这个参数是ntpdate后面的参数穿进去的。改成ip,运行正常,这个交叉编译工具链,编出来的只能直接用ip了。

# ntpdate ntp1.aliyun.com   182.92.12.11 

4.4配置时提示权限不足

./configure --prefix=/home/jihui.zhang/w/iptv2/external/ntp-4.2.4p7/install --exec-prefix=$PWD/install --host=arm-linux CC=aarch64-linux-gnu-gc

-bash: ././configure --prefix=/home/jihui.zhang/w/iptv2/external/ntp-4.2.4p7/install --exec-prefix=$PWD/install --host=arm-linux CC=aarch64-linux-gnu-gc
-bash: ./configure: Permission denied: Permission denied

检查configure文件有没有x权限

 

你可能感兴趣的文章