diff options
author | Wolfgang Hommel <wolf@code-wizards.com> | 2012-01-08 22:41:54 +0100 |
---|---|---|
committer | Wolfgang Hommel <wolf@code-wizards.com> | 2012-01-08 22:41:54 +0100 |
commit | 837e2c7e62ec13a946946d652e63a03732e58a8b (patch) | |
tree | 3e00fe0c9cab89d7fe3aa1a1ed1df21ed1151d24 /src/faketime.c | |
parent | e72d8e0419fac867a452a23a4ad8f3ebf0ca5d7b (diff) | |
download | faketime-ng-837e2c7e62ec13a946946d652e63a03732e58a8b.tar.gz |
Added new feature to limit libfaketime activity
These changes add a new feature that can be used to limit
libfaketime's activity period, and prepares for the upcoming
0.9 release.
Four new environment variables can optionally be set:
FAKETIME_START_AFTER_SECONDS, FAKETIME_STOP_AFTER_SECONDS,
FAKETIME_START_AFTER_NUMCALLS, and FAKETIME_START_AFTER_NUMCALLS.
libfaketime will only return faked timestamps if the program's
runtime is between FAKETIME_START_AFTER_SECONDS and
FAKETIME_STOP_AFTER_SECONDS seconds. For example, a program
may be started with the real current time, and after 60
seconds, it will start to receive faked timestamps, e.g.,
one year in the future. The other pair of environment
variables limits libfaketime's activity based on the number
of time-related system calls of the started program,
instead of its run-time in seconds.
Using this new feature will break applications that cannot
handle larger sudden changes in timestamps, so use it with
care.
Diffstat (limited to 'src/faketime.c')
-rw-r--r-- | src/faketime.c | 58 |
1 files changed, 53 insertions, 5 deletions
diff --git a/src/faketime.c b/src/faketime.c index 3b6e69a..1745380 100644 --- a/src/faketime.c +++ b/src/faketime.c @@ -457,6 +457,7 @@ static time_t _ftpl_time(time_t *time_tptr) { /* initialize our result with the real current time */ result = (*real_time)(time_tptr); #endif + return result; } @@ -628,6 +629,17 @@ time_t fake_time(time_t *time_tptr) { 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 */ + +#ifdef LIMITEDFAKING + static long callcounter = 0; + static int limited_initialized = 0; + char envvarbuf[32]; + static long FAKETIME_START_AFTER_SECONDS = -1; + static long FAKETIME_STOP_AFTER_SECONDS = -1; + static long FAKETIME_START_AFTER_NUMCALLS = -1; + static long FAKETIME_STOP_AFTER_NUMCALLS = -1; +#endif + /* * This no longer appears to be necessary in Mac OS X 10.7 Lion */ @@ -644,13 +656,49 @@ static pthread_mutex_t time_mutex=PTHREAD_MUTEX_INITIALIZER; /* Sanity check by Karl Chan since v0.8 */ if (time_tptr == NULL) return -1; +#ifdef LIMITEDFAKING + /* Check whether we actually should be faking the returned timestamp. */ + + if (ftpl_starttime > 0) { + if (limited_initialized == 0) { + if (getenv("FAKETIME_START_AFTER_SECONDS") != NULL) { + (void) strncpy(envvarbuf, getenv("FAKETIME_START_AFTER_SECONDS"), 30); + FAKETIME_START_AFTER_SECONDS = atol(envvarbuf); + } + if (getenv("FAKETIME_STOP_AFTER_SECONDS") != NULL) { + (void) strncpy(envvarbuf, getenv("FAKETIME_STOP_AFTER_SECONDS"), 30); + FAKETIME_STOP_AFTER_SECONDS = atol(envvarbuf); + } + if (getenv("FAKETIME_START_AFTER_NUMCALLS") != NULL) { + (void) strncpy(envvarbuf, getenv("FAKETIME_START_AFTER_NUMCALLS"), 30); + FAKETIME_START_AFTER_NUMCALLS = atol(envvarbuf); + } + if (getenv("FAKETIME_STOP_AFTER_NUMCALLS") != NULL) { + (void) strncpy(envvarbuf, getenv("FAKETIME_STOP_AFTER_NUMCALLS"), 30); + FAKETIME_STOP_AFTER_NUMCALLS = atol(envvarbuf); + } + limited_initialized = 1; + } + if ((callcounter + 1) >= callcounter) callcounter++; + + /* For debugging, output #seconds and #calls */ + /* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu\n", (*time_tptr - ftpl_starttime), callcounter); */ + if ((FAKETIME_START_AFTER_SECONDS != -1) && ((*time_tptr - ftpl_starttime) < FAKETIME_START_AFTER_SECONDS)) return *time_tptr; + if ((FAKETIME_STOP_AFTER_SECONDS != -1) && ((*time_tptr - ftpl_starttime) >= FAKETIME_STOP_AFTER_SECONDS)) return *time_tptr; + if ((FAKETIME_START_AFTER_NUMCALLS != -1) && (callcounter < FAKETIME_START_AFTER_NUMCALLS)) return *time_tptr; + if ((FAKETIME_STOP_AFTER_NUMCALLS != -1) && (callcounter >= FAKETIME_STOP_AFTER_NUMCALLS)) return *time_tptr; + /* fprintf(stderr, "(libfaketime limits -> runtime: %lu, callcounter: %lu continues\n", (*time_tptr - ftpl_starttime), callcounter); */ + + } +#endif + if (last_data_fetch > 0) { - if ((*time_tptr - last_data_fetch) > cache_duration) { - cache_expired = 1; - } + if ((*time_tptr - last_data_fetch) > cache_duration) { + cache_expired = 1; + } else { - cache_expired = 0; - } + cache_expired = 0; + } } #ifdef NO_CACHING |