How do I check whether my system's clock is synchronized to a NTP server?

青春壹個敷衍的年華 提交于 2021-01-05 11:23:53

问题


I have an application on a Linux system (Ubuntu Server) that needs to know whether the current system clock has been synchronized to a NTP server. While I could check timedatectl's output for System clock synchronized: yes, this seems very brittle, especially since timedatectl's human readable output might change in the future.

However, systemd seems to be full of DBus interfaces, so I suspect that there might be a way to check there. Either way, I'm looking for a bool is_ntp_synchronized().

Is there any way to simply check whether the system clock is synchronized without starting another process?


回答1:


Linux provides adjtimex, which also gets used by systemd. You can check various fields to determine if you're still synchronized. A non-negative return value unequal to TIME_ERROR might be your forte, although you might use the maxerror or other fields to check the clock's quality instead.

#include <stdio.h>
#include <sys/timex.h>

int main()
{
    struct timex timex_info = {};
    timex_info.modes = 0;         /* explicitly don't adjust any time parameters */

    int ntp_result = ntp_adjtime(&timex_info);

    printf("Max       error: %9ld (us)\n", timex_info.maxerror);
    printf("Estimated error: %9ld (us)\n", timex_info.esterror);
    printf("Clock precision: %9ld (us)\n", timex_info.precision);
    printf("Jitter:          %9ld (%s)\n", timex_info.jitter,
           (timex_info.status & STA_NANO) ? "ns" : "us");
    printf("Synchronized:    %9s\n", 
           (ntp_result >= 0 && ntp_result != TIME_ERROR) ? "yes" : "no");
    return 0;
}

Note that systemd explicitly ignores the reported result (except for errors) and instead checks that the timex_info.maxerror value hasn't become more than 16 seconds.

This interface has also been provided since the pre-git times. As such, it's guaranteed to be stable, as it might otherwise break Linux's don't-break-userspace-policy.



来源:https://stackoverflow.com/questions/65342065/how-do-i-check-whether-my-systems-clock-is-synchronized-to-a-ntp-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!