我想在Linux上使用相对时间偏移量(或绝对时间值)调用程序或脚本。可以将其视为设置系统日期和时间,但不是全局设置系统,而是本地设置脚本/程序及其所有子进程。这有可能吗?
发布于 2010-10-15 21:45:15
您可以围绕C运行时库(glibc)调用创建一个包装器,并使用LD_PRELOAD
将其传递给您的脚本(有关更多信息,请参阅this question )。
要覆盖的函数并不多,比如glibc中的LSB specifies only 16 time-related函数。包装器的实现可以使用环境变量的值来调整偏差。然后,调用将如下所示:
LD_PRELOAD=libtime.so TIME_SHIFT=+10 my_program
其实现如下所示(伪代码):
struct tm *localtime(const time_t *timep)
{
//use dlopen() call to get the actual glibc
//use dlsym() to find the real localtime() function
//call this localtime function
//adjust the time in the struct tm* returned by TIME_SHIFT value
//return it to the calling program
}
感谢Konstantin Vlasov的提示。但当然,这个想法并不新颖。Hasturkun的This StackOverflow answer列出了几个库,它们的工作方式是这样的。
https://stackoverflow.com/questions/3942662
复制相似问题