开发者

Call program with time offset on Linux

开发者 https://www.devze.com 2023-01-20 06:57 出处:网络
I would like to call a program or a script with a relati开发者_运维知识库ve time offset (or, alternatively, with an absolute time value) on Linux. Think of it as setting the system date and time, exce

I would like to call a program or a script with a relati开发者_运维知识库ve time offset (or, alternatively, with an absolute time value) on Linux. Think of it as setting the system date and time, except not globally for the system but locally for the script/program and all its child processes. Is that possible?


You can create a wrapper around C runtime library (glibc) calls and pass it to your script with LD_PRELOAD (see this question for more info).

There are not so many functions you are to override, LSB specifies only 16 time-related functions in glibc. The implementation of your wrappers could use a value of an environment variable to adjust the skew. The call would then look like this:

LD_PRELOAD=libtime.so TIME_SHIFT=+10 my_program

And the implementation would look like this (pseudocode):

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
}

Thanks to Konstantin Vlasov for the hint. But of course the idea is not novel. This StackOverflow answer by Hasturkun lists several libraries, which work this way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号