

These are not all the features that libfaketime supports. absolute date: FAKETIME=" 20:30:00" will render a fixed value, as if the time stopped at this point.'start at' date: 20:30:00", where the clock will start ticking from this date for each new process, but it's possible to configure it to keep the clock ticking instead 7.different offset mutlipliers: all the examples used "-16d", but instead of "d" it can also be "m", "h", "y" or nothing for seconds the offset can be set in the past ( -10d) or in the future ( +10d).Specifying a relative offset is not the only way to fake the time. The output should change after ten seconds.

While the program running keeps running, in another shell session we can type echo -10d > ~/.faketimerc. Running java Main in the shell should now render the expected output. I omitted FAKETIME because this one is likely to change. To shorten the commands, LD_PRELOAD and FAKETIME_DONT_FAKE_MONOTONIC values can be specified as the environment values of the Docker image. I must admit I wasted some time debugging it just because I skipped reading the readme. Indeed, without it even this tiny program hanged after printing the first date. This argument the java command usually hangs. Java-/JVM-based applications work but you need to pass in an extra argument It's working as expected, though I added another variable to the command: FAKETIME_DONT_FAKE_MONOTONIC=1. Here's the ~]$ docker run -name centos -v /home/test/:/app -d -it centos ~]$ docker exec -u 0 -it centos /]# cd app] javac app] java app] LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME="-15d" FAKETIME_DONT_FAKE_MONOTONIC=1 java Main


Given all the knowledge gathered so far, it's time to fake the date in a Java app.įirst of all - to test the library, I created a simple application that prints the current date and time each second, forever. Libfaketime replaces the symbols related to interactions with the system clock using the preloading mechanism. As a result, when the program references foo() in its source code, the implementation from library B will be invoked. It means that if a program uses a function foo() from library A and library A is linked at runtime, it is possible to provide a path to library B containing a different implementation of foo() in the LD_PRELOAD variable. functions) contained in the preloaded libraries take precedence over the symbols from libraries loaded afterwards 4 5 6. The LD_PRELOAD variable allows to specify paths to libraries which are to be loaded before any other libraries are loaded. LD_PRELOAD variable and the preloading mechanism To me, the most interesting bit is the LD_PRELOAD part. With the path to libfaketime provided in the LD_PRELOAD variable, the FAKETIME variable set to -15 days, the invocation of date rendered a date 15 days in the past. Wed Feb 19 17:16:34 UTC /]# LD_PRELOAD=/usr/local/lib/faketime/libfaketime.so.1 FAKETIME="-15d" date To automate this process, I created the following ~]$ docker build -t centos ~]$ docker run -name centos -d -it centos ~]$ docker exec -u 0 -it centos /]# date It'll result in a bunch of files getting created in /usr/local/lib/faketime.
#FAKE TIME CLOCK APP INSTALL#
The installation is straightforward - you grab the source code of the library and run make install in the root directory of the checked out sources. Since I'm not a Linux guru, using this library at first felt like it was a wrapper for my java application, though it's not how it works. What's more, you don't have to change a line of your existing code or add it to your app's dependency list - it'll be transparent. It is then able to provide a fake value for these calls. Libfaketime is a library which is able to 'override' system calls that applications use to retrieve current date or time. However, when looking for the workarounds, I stumbled upon a different way to change the time - libfaketime. As I'm just a casual user of Docker, I didn't want to dig deeper. I tried to find some workarounds, but as far as I understand, Docker reuses the clock of the host machine, so overriding the date in the container is either not doable or not easily doable 1 2 3. Hwclock: Use the -debug option to see the details of our search for an access method. Hwclock: Cannot access the Hardware Clock via any known method. Date: cannot set date: Operation not permitted
