dsxsou8465 2016-03-16 00:41
浏览 212
已采纳

libfaketime不适用于golang

I would like my go program, which runs on ubuntu server (14.04), daemonized with supervisor, to use a fake server time.

In my supervisor config, I use this as the executing command:

"faketime 'last Friday 5 pm' /home/user/main"

The program runs, but displays the current time.

According to this article: Changing what time a process thinks it is with libfaketime

libfaketime cannot be used with statically linked or setuid programs, because LD_PRELOAD is not available to such programs.

Is there anyway to have my compiled go program use the faketime?

  • 写回答

1条回答 默认 最新

  • douzhuo6270 2016-03-16 01:16
    关注

    The issue is that faketime uses the LD_PRELOAD environment variable to instruct the program's dynamic loader to load libfaketime at startup. libfaketime will do what's called "interpositioning" - replacing normal dynamic library routines with its own copies of those routines - so that when future dynamic library calls are made, libfaketime can influence what happens. In particular, libfaketime interposes time-related calls, and thus it is able to return fake values to the program.

    The reason that this works for most programs is that they use libc to make syscalls. libc provides high-level functions for interacting with syscalls, making it easier to do systems programming. In most languages that use libc, binaries are dynamically linked, meaning that libc isn't actually included in the binary, but rather it is expected that a compiled version of libc (called an "object file") will exist on the system when the binary is run, and the dynamic library can be loaded at that point. This dynamic loading is what makes faketime possible by way of the LD_PRELOAD directive, which changes the loader's behavior.

    Go, however, differs in two ways. First, it is statically linked, and thus there is no loader that could ever pay attention to LD_PRELOAD. Second, it doesn't use libc, so even if it were dynamically linked, and the LD_PRELOAD trick worked, libc would never be called anyway, so it still wouldn't actually accomplish the intended goal of tricking the program into using fake time functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)