Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Attn. Kernel Hackers, help me achieve time travel.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
nosatalian
Tux's lil' helper
Tux's lil' helper


Joined: 09 Apr 2004
Posts: 98

PostPosted: Sun Aug 07, 2005 10:56 pm    Post subject: Attn. Kernel Hackers, help me achieve time travel. Reply with quote

Can anyone give me some clarification as to how a program gets the system time from the kernel?

Basically, I would like one of my applications to think that the time is different than the actual time, for example, lets say I want to run my application as if it were Jan 20, 1975 at all times.

I don't think this can be done with a chroot can it?

If not, this will require some kernel hacking on my part. Would it be possible to do something like: <psuedo code>

long getCurrentSystemTimeInMs(){
if (application making this call is X)
then return (Jan 20, 1975)
else return actualTime;
}

I guess my main question is, does there exist a mechanism with which a method can know what called it? Obviously I could pass a referrence to the "caller," but I don't want to add extra parameters to this hypothetical getCurrentSystemTime call.
Back to top
View user's profile Send private message
widan
Veteran
Veteran


Joined: 07 Jun 2005
Posts: 1512
Location: Paris, France

PostPosted: Mon Aug 08, 2005 12:40 am    Post subject: Reply with quote

You can build a dynamic library exporting the (libc) function you want to replace (in your case, most likely gettimeofday(), but you may want to check that with strace first), and then run your application with:
Code:
LD_PRELOAD=/path/to/your/lib.so ./application

No need to hack the kernel for that. If you need to access the real libc function from within your "overlay" library, dlopen libc.so.6 and use dlsym to get the real address of gettimeofday().

An example that works for the "date" command (that uses clock_gettime(), and not gettimeofday(), so check what your app uses) :
Code:
widan@melanie ~/clock $ cat clock.c
#include <time.h>

int clock_gettime(clockid_t clk_id, struct timespec *tp) {
  tp->tv_sec = 0;
  tp->tv_nsec = 0;
  return 0;
}
widan@melanie ~/clock $ gcc -shared -fPIC -DPIC -o clock.so clock.c
widan@melanie ~/clock $ date
Mon Aug  8 02:37:43 CEST 2005
widan@melanie ~/clock $ LD_PRELOAD=./clock.so date
Thu Jan  1 01:00:00 CET 1970
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum