首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一个直接在Linux内核上设置时间的函数?

一个直接在Linux内核上设置时间的函数?
EN

Stack Overflow用户
提问于 2022-10-25 11:39:41
回答 1查看 51关注 0票数 1

我们正在试图找到一个函数,它可以直接通过linux内核来设置时间,而不需要使用用户空间。我们发现在文件clock_settime.c上有一个仅限于超级用户的函数:

代码语言:javascript
复制
* Copyright (C) 1991-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <time.h>
#include <hurd.h>
#include <hurd/port.h>
#include <shlib-compat.h>

/* Set the current time of day.
   This call is restricted to the super-user.  */
int
__clock_settime (clockid_t clock_id, const struct timespec *ts)
{
  error_t err;
  mach_port_t hostpriv;
  time_value_t tv;

  if (clock_id != CLOCK_REALTIME
      || ! valid_nanoseconds (ts->tv_nsec))
    return __hurd_fail (EINVAL);

  err = __get_privileged_ports (&hostpriv, NULL);
  if (err)
    return __hurd_fail (EPERM);

  TIMESPEC_TO_TIME_VALUE (&tv, ts);
  err = __host_set_time (hostpriv, tv);
  __mach_port_deallocate (__mach_task_self (), hostpriv);

  return __hurd_fail (err);
}
libc_hidden_def (__clock_settime)

versioned_symbol (libc, __clock_settime, clock_settime, GLIBC_2_17);
/* clock_settime moved to libc in version 2.17;
   old binaries may expect the symbol version it had in librt.  */
#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17)
strong_alias (__clock_settime, __clock_settime_2);
compat_symbol (libc, __clock_settime_2, clock_settime, GLIBC_2_2);
#endif

我们希望遵循函数__host_set_time声明,但是在任何地方都没有库引用,我们认为它可能是帮助我们解决问题的函数。有人知道它是从哪里来的吗?它是干什么的?提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-25 13:04:38

你看错源树了。您引用的代码是GNU C库的一部分,它是用户空间。此外,该代码仅在Hurd操作系统上使用,而不是在Linux上使用。这个C库在sysdeps/unix/sysv/linux/clock_settime.c中实现了Linux,您将看到这只是调用宏,这些宏扩展为系统调用陷阱的内联程序集。

您应该搜索Linux内核本身的源代码,而不是C库。我将"clock_settime“放入https://elixir.bootlin.com/上的搜索框中,经过几次跳转,找到了do_settimeofday64,我相信这就是您要寻找的API。您还可能希望查阅实际系统调用入口点的代码,这些代码位于kernel/time/time.c中;它们是执行安全内存读取和安全检查,然后调用do_settimeofday64的包装器。

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

https://stackoverflow.com/questions/74193489

复制
相关文章

相似问题

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