ntp移植过程记录

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_socktype
4 Sep 03:25:02 ntpdate[27310]: can't find host 0.pool.ntp.org

解决办法在etc/service中加入

 

ntp             123/tcp
ntp             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权限






 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
海思移植ntpd的步骤大致如下: 1. 获取ntpd源代码:可以从ntp.org下载ntpd源代码,并解压缩到本地目录。 2. 配置交叉编译环境:使用海思提供的交叉编译工具链,配置交叉编译环境。 3. 修改Makefile文件:进入ntpd源代码目录,修改Makefile文件,将交叉编译工具链及其路径配置好。 4. 编译ntpd:执行make命令编译ntpd程序。 5. 将ntpd程序移植到目标设备:将编译好的ntpd程序通过网络或其他方式拷贝到目标设备中。 6. 配置ntpd服务:在目标设备上运行ntpd程序,并配置好ntpd服务,可以使用ntp.conf文件进行配置。 7. 启动ntpd服务:在目标设备上启动ntpd服务,可以使用systemctl命令或其他方式启动。 至于ntp步骤,可以参考下面的步骤: 1. 获取ntp源代码:可以从ntp.org下载ntp源代码,并解压缩到本地目录。 2. 配置交叉编译环境:使用海思提供的交叉编译工具链,配置交叉编译环境。 3. 修改Makefile文件:进入ntp源代码目录,修改Makefile文件,将交叉编译工具链及其路径配置好。 4. 编译ntp:执行make命令编译ntp程序。 5. 将ntp程序移植到目标设备:将编译好的ntp程序通过网络或其他方式拷贝到目标设备中。 6. 配置ntp服务:在目标设备上运行ntp程序,并配置好ntp服务,可以使用ntp.conf文件进行配置。 7. 启动ntp服务:在目标设备上启动ntp服务,可以使用systemctl命令或其他方式启动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值