Merge pull request #59 from mac-joker/master

New features: custom timestamp file and cache managment
This commit is contained in:
Wolfgang Hommel
2014-11-21 08:54:36 +01:00
4 changed files with 33 additions and 26 deletions

21
README
View File

@@ -169,14 +169,15 @@ will be loaded, but just report the real system time. There are three
ways to specify the faked time:
a) By setting the environment variable FAKETIME.
b) By using the file .faketimerc in your home directory.
c) By using the file /etc/faketimerc for a system-wide default.
b) By using the file given in the environment variable FAKETIME_TIMESTAMP_FILE
c) By using the file .faketimerc in your home directory.
d) By using the file /etc/faketimerc for a system-wide default.
If you want to use b) or c), $HOME/.faketimerc or /etc/faketimerc consist of
If you want to use b) c) or d), $HOME/.faketimerc or /etc/faketimerc consist of
only one line of text with exactly the same content as the FAKETIME environment
variable, which is described below. Note that /etc/faketimerc will only be used
if there is no $HOME/.faketimerc, and the FAKETIME environment variable always
has priority over the files.
if there is no $HOME/.faketimerc nor FAKETIME_TIMESTAMP_FILE file exists, and
the FAKETIME environment variable always has priority over the files.
4b) Using absolute dates
@@ -314,12 +315,14 @@ Using relative offsets or 'start at' dates solves this problem.
libfaketime then will always report the faked time based on the real
current time and the offset you've specified.
Please also note that your specification of the fake time is cached for 10
seconds in order to enhance the library's performance. Thus, if you change the
Please also note that your default specification of the fake time is cached for
10 seconds in order to enhance the library's performance. Thus, if you change the
content of $HOME/.faketimerc or /etc/faketimerc while a program is running, it
may take up to 10 seconds before the new fake time is applied. If this is a
problem in your scenario, you can disable caching at compile time by adding the
command line option -DNO_CACHING to this library's Makefile.
problem in your scenario, you can change number of seconds before the file is read
again with environment variable FAKETIME_CACHE_DURATION, or disable caching at all
with FAKETIME_NO_CACHE=1. Remember that disabling the cache may negatively
influence the performance.
4f) Faking the date and time system-wide

View File

@@ -30,12 +30,6 @@
# FAKE_TIMERS
# - Also intercept timer_settime() and timer_gettime()
#
# NO_CACHING
# - Disables the caching of the fake time offset. Only disable caching
# if you change the fake time offset during program runtime very
# frequently. Disabling the cache may negatively influence the
# performance.
#
# MULTI_ARCH
# - If MULTI_ARCH is set, the faketime wrapper program will put a literal
# $LIB into the LD_PRELOAD environment variable it creates, which makes

View File

@@ -21,12 +21,6 @@
# FAKE_SLEEP
# - Also intercept sleep(), nanosleep(), usleep(), alarm(), [p]poll()
#
# NO_CACHING
# - Disables the caching of the fake time offset. Only disable caching
# if you change the fake time offset during program runtime very
# frequently. Disabling the cache may negatively influence the
# performance.
#
# * Compilation addition: second libMT target added for building the pthread-
# enabled library as a separate library
#

View File

@@ -200,6 +200,8 @@ static long ft_spawn_secs = -1;
static long ft_spawn_ncalls = -1;
static int fake_monotonic_clock = 1;
static int cache_enabled = 1;
static int cache_duration = 10; /* cache fake time input for 10 seconds */
/*
* Static timespec to store our startup time, followed by a load-time library
@@ -1460,6 +1462,17 @@ void __attribute__ ((constructor)) ftpl_init(void)
}
#endif
if ((tmp_env = getenv("FAKETIME_CACHE_DURATION")) != NULL)
{
cache_duration = atoi(tmp_env);
}
if ((tmp_env = getenv("FAKETIME_NO_CACHE")) != NULL)
{
if (0 == strcmp(tmp_env, "1"))
{
cache_enabled = 0;
}
}
if ((tmp_env = getenv("DONT_FAKE_MONOTONIC")) != NULL)
{
if (0 == strcmp(tmp_env, "1"))
@@ -1676,7 +1689,6 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
/* variables used for caching, introduced in version 0.6 */
static time_t last_data_fetch = 0; /* not fetched previously at first call */
static int cache_expired = 1; /* considered expired at first call */
static int cache_duration = 10; /* cache fake time input for 10 seconds */
if (dont_fake) return 0;
/* Per process timers are only sped up or slowed down */
@@ -1762,9 +1774,10 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
}
}
#ifdef NO_CACHING
cache_expired = 1;
#endif
if (cache_enabled == 0)
{
cache_expired = 1;
}
if (cache_expired == 1)
{
@@ -1777,6 +1790,7 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
if (parse_config_file)
{
static char user_faked_time[BUFFERLEN]; /* changed to static for caching in v0.6 */
char custom_filename[BUFSIZ];
char filename[BUFSIZ];
FILE *faketimerc;
/* initialize with default or env. variable */
@@ -1794,8 +1808,10 @@ int fake_clock_gettime(clockid_t clk_id, struct timespec *tp)
* a system-wide /etc/faketimerc present.
* The /etc/faketimerc handling has been contributed by David Burley,
* Jacob Moorman, and Wayne Davison of SourceForge, Inc. in version 0.6 */
(void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE"));
(void) snprintf(filename, BUFSIZ, "%s/.faketimerc", getenv("HOME"));
if ((faketimerc = fopen(filename, "rt")) != NULL ||
if ((faketimerc = fopen(custom_filename, "rt")) != NULL ||
(faketimerc = fopen(filename, "rt")) != NULL ||
(faketimerc = fopen("/etc/faketimerc", "rt")) != NULL)
{
char line[BUFFERLEN];