wolfcw / libfaketime

libfaketime modifies the system time for a single application
https://github.com/wolfcw/libfaketime
GNU General Public License v2.0
2.71k stars 325 forks source link

faketime doesn't set time #354

Closed patofet closed 2 years ago

patofet commented 2 years ago

Hi, I'm trying to set the date in a docker with alpine and it doesn't work update the date in realtime, I have this example code:

export LD_PRELOAD=/libfaketime/src/libfaketime.so.1
prntdate() {
        while true
        do
                date
                sleep 10
        done
}
prntdate &
sum=1
while true
do
  echo "Setting time"
  IN=$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-)
  IFS=" " read -r -a myarray <<< $IN
  ((sum += num))
  export FAKETIME="${myarray[2]}-$(monthnumber ${myarray[1]})-$sum ${myarray[3]}"
  sleep 20
done

it only prints the actual date and it doesn't have effect the set FAKETIME

wolfcw commented 2 years ago

Not sure what you are expecting there.

Does it work without Docker?

My guess is that you spawn a subprocess (with prntdate &) but then change the FAKETIME environment variable of only the parent process. Also, the subprocess is started before FAKETIME is initially set, so libfaketime becomes inactive.

You might find it easier to sync faketime settings across process hierarchies using a file (instead of the FAKETIME environment variable), and because you change time settings quite frequently, you might also consider disabling libfaketime's caching.

patofet commented 2 years ago

I'm expecting that the date is correctly synced with the FAKETIME(in reality I have a rust program instead of prntdate) and in this case I try to canche the day in every iteration but in reality I set the correct day. I think that it'll not work because if I set the time before I call the function it'll work as expected. I'll try to use a file and disable caching and I'll see, thanks.

EDIT: I tried the file configuration with:

export LD_PRELOAD=/libfaketime/src/libfaketime.so.1
export FAKETIME_NO_CACHE=1
export FAKETIME='%'
export FAKETIME_FOLLOW_FILE=/tmp/my-demo-file.tmp
export FAKETIME_DONT_RESET=1
prntdate() {
        while true
        do
                date
                sleep 10
        done
}
prntdate

and the clock only updates when I change the file, is there any way to auto update the time and every 5 seconds set the time?

wolfcw commented 2 years ago

The file's timestamp is used as a start-at faketime specification. For changing the file's timestamp every x seconds, you'll probably need a separate process with a simple shell script.