clock_settime 设置的是time的差值

#include
#include
#include
#include

int main()
{
        struct timespec tpset, tsreset;

        if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
                perror("clock_gettime() did not return success\n");
        }

        tpset.tv_sec = 0;
        tpset.tv_nsec = 0;

        clock_settime(CLOCK_REALTIME, &tpset);
        perror("clock_settime");
}


这段code 直接的结果会在调用clock_settime的时候提示非法参数,原因如下:
clock_settime->kc->clock_set(which_clock, &new_tp)->posix_clock_realtime_set->do_sys_settimeofday->do_sys_settimeofday64->do_settimeofday64
int do_settimeofday64(const struct timespec64 *ts)
{
    struct timekeeper *tk = &tk_core.timekeeper;
    struct timespec64 ts_delta, xt;
    unsigned long flags;
    int ret = 0;

    if (!timespec64_valid_strict(ts))
        return -EINVAL;

    raw_spin_lock_irqsave(&timekeeper_lock, flags);
    write_seqcount_begin(&tk_core.seq);

    timekeeping_forward_now(tk);

    xt = tk_xtime(tk);
    ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
    ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
// 这里就换返回failed,从这里看到调用clock_settime 是设置和当前时间的差值,像上面的case,如果tpset.tv_sec = 0 和 tpset.tv_nsec = 0 都是零的话,这个差值会非常大,例如:clock_gettime(CLOCK_REALTIME, {1498723813, 202715700})
//因此在调用timespec64_compare时候就提示failed
    if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
        ret = -EINVAL;
        goto out;
    }
}



你可能感兴趣的:(Linux,源码分析)