首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ntpdate如何更新时间?

ntpdate如何更新时间?
EN

Stack Overflow用户
提问于 2012-11-07 01:29:18
回答 1查看 1K关注 0票数 0

我在ntpdate命令上使用了strace,该命令调用adjtimex来调整时间。我得到了这个:

代码语言:javascript
复制
adjtimex({modes=ADJ_OFFSET|0x8000, offset=-479139, freq=0, maxerror=16000000, esterror=16000000, status=STA_UNSYNC, constant=2, precision=1, tolerance=32768000, time={1352220968, 428418}, tick=10000, ppsfreq=0, jitter=0, shift=0, stabil=0, jitcnt=0, calcnt=0, errcnt=0, stbcnt=0}) = 5 (TIME_ERROR)

然后我研究了adjtimex API在do_adjtimex主函数中的实现。但是从代码中我看不到ntpdate是如何调整时间的。

代码语言:javascript
复制
modes=ADJ_OFFSET|0x8000=ADJ_OFFSET|ADJ_ADJTIME

int do_adjtimex(struct timex *txc)
{
    struct timespec ts;
    int result;
/* Validate the data before disabling interrupts */
if (txc->modes & ADJ_ADJTIME) { **<----this is true. modes as ADJ_OFFSET|ADJ_ADJTIME**
    /* singleshot must not be used with any other mode bits */
    if (!(txc->modes & ADJ_OFFSET_SINGLESHOT))
        return -EINVAL;
    if (!(txc->modes & ADJ_OFFSET_READONLY) &&
        !capable(CAP_SYS_TIME))
        return -EPERM;
} else {
    .....
}

if (txc->modes & ADJ_SETOFFSET) { **《===this is false**
    .....
}

getnstimeofday(&ts);**《------------just get the walk time**

spin_lock_irq(&ntp_lock);

if (txc->modes & ADJ_ADJTIME) {<----this is true.
    long save_adjust = time_adjust; **<----this is global valu eoffset=-479139**

    if (!(txc->modes & ADJ_OFFSET_READONLY)) {
        /* adjtime() is independent from ntp_adjtime() */
        time_adjust = txc->offset;
        ntp_update_frequency();
    }
    txc->offset = save_adjust;
} else {

    .......
}

result = time_state;    /* mostly `TIME_OK' */ this is 
/* check for errors */
if (is_error_status(time_status)) **<----this is STA_UNSYNC**
    result = TIME_ERROR;

txc->freq      = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
                 PPM_SCALE_INV, NTP_SCALE_SHIFT);
txc->maxerror      = time_maxerror;
txc->esterror      = time_esterror;
txc->status    = time_status;
txc->constant      = time_constant;
txc->precision     = 1;
txc->tolerance     = MAXFREQ_SCALED / PPM_SCALE;
txc->tick      = tick_usec;
txc->tai       = time_tai;

/* fill PPS status fields */
pps_fill_timex(txc);

spin_unlock_irq(&ntp_lock);

txc->time.tv_sec = ts.tv_sec;
txc->time.tv_usec = ts.tv_nsec;
if (!(time_status & STA_NANO))
    txc->time.tv_usec /= NSEC_PER_USEC;

notify_cmos_timer(); <---- call sync_cmos_clock.

return result;
}

sync_cmos_clock检查ntp是否已同步。time_status是STA_UNSYNC。什么都不做就回来。

代码语言:javascript
复制
if (!ntp_synced()) { /*
                      * Not synced, exit, do not restart a timer (if one is
                      * running, let it run out). */
return; }

我是不是遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2012-11-07 18:30:19

ntp.c已从kernel/timer.ckernel/time.c迁移到

ntp.c内部,您可以找到对ntp_update_frequency()的调用。

代码语言:javascript
复制
if (!(txc->modes & ADJ_OFFSET_READONLY)) {
   /* adjtime() is independent from ntp_adjtime() */
   time_adjust = txc->offset;
   ntp_update_frequency();
}
txc->offset = save_adjust;

ntp_update_frequency()将更新u64 tick_lengthstatic tick_length_base。注意adjtimentp_adjtime的独立性。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13256232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档