这个函数用于设置系统当前时间。
其源码分析如下:
SYSCALL_DEFINE1(stime, time_t __user *, tptr)
{
struct timespec64 tv;
int err;
#从user space的buffer中copy要设置的时间到tv.tv_sec中
if (get_user(tv.tv_sec, tptr))
return -EFAULT;
tv.tv_nsec = 0;
#security_settime64->cap_settime检查是否有权限设置时间
err = security_settime64(&tv, NULL);
if (err)
return err;
#开始设置时间
do_settimeofday64(&tv);
return 0;
}
int do_settimeofday64(const struct timespec64 *ts)
{
#得到timekeeper的时间
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);
#将timekeeper转成timespec64
xt = tk_xtime(tk);
#计算当前时间和要设置时间的偏差
ts_delta.tv_sec = ts->tv_sec - xt.tv_sec;
ts_delta.tv_nsec = ts->tv_nsec - xt.tv_nsec;
#检查这个偏差是合法
if (timespec64_compare(&tk->wall_to_monotonic, &ts_delta) > 0) {
ret = -EINVAL;
goto out;
}
#设置墙上时间
tk_set_wall_to_mono(tk, timespec64_sub(tk->wall_to_monotonic, ts_delta));
#设置timekeeper
tk_set_xtime(tk, ts);
out:
timekeeping_update(tk, TK_CLEAR_NTP | TK_MIRROR | TK_CLOCK_WAS_SET);
write_seqcount_end(&tk_core.seq);
raw_spin_unlock_irqrestore(&timekeeper_lock, flags);
/* signal hrtimers about time change */
clock_was_set();
return ret;
}
系统调用之sys_settimeofday
最新推荐文章于 2024-06-12 10:50:51 发布