libfaketime不适用于golang [英] libfaketime doesn't work with golang
问题描述
在我的管理员配置中,我想让我的程序在ubuntu服务器上运行(14.04),并用supervisor进行守护进程,我使用它作为执行命令:
faketime'上周五下午5点'/ home / user / main
程序运行,但显示当前时间。
根据这篇文章:
更改进程认为它是libfaketime的时间
libfaketime不能用于静态链接或setuid程序,因为LD_PRELOAD不适用于此类程序。
是否有我编译的go程序使用faketime?
问题在于 faketime
使用 LD_PRELOAD
环境变量来指示程序的动态加载器加载 li bfaketime
在启动时。 libfaketime
将执行所谓的interpositioning - 将常规动态库例程替换为它们自己的那些例程副本 - 以便在将来进行动态库调用时, libfaketime
可以影响发生的事情。特别是, libfaketime
会介入时间相关的调用,因此它可以将假值返回给程序。
这对大多数程序来说都是有效的,因为它们使用 libc
来产生系统调用。 libc
提供了与系统调用交互的高级函数,使系统编程变得更加容易。在大多数使用 libc
的语言中,二进制文件是动态链接的,这意味着 libc
实际上不包含在二进制文件中,而是预计在运行二进制文件时,系统上将存在已编译版本的 libc
(称为目标文件),并且可以加载动态库在那时候。这种动态加载是通过 LD_PRELOAD
指令实现 faketime
,从而改变加载器的行为。
然而,Go有两个不同之处。首先,它是静态链接的,因此没有可能关注 LD_PRELOAD
的加载器。其次,它不使用 libc
,所以即使它是动态链接的,并且 LD_PRELOAD
code> libc 永远不会被调用,所以它实际上并不会实现欺骗程序使用假时间函数的预期目标。
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?
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.
这篇关于libfaketime不适用于golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!