summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSteffen Dettmer <steffen.dettmer@rail.bombardier.com>2020-04-09 15:54:23 +0200
committerSteffen Dettmer <steffen.dettmer@rail.bombardier.com>2020-04-09 16:06:32 +0200
commitc1d10321a7629225b0d79f0a1f31e10e8c065c87 (patch)
treeb3ff63a6dc8bdc305cef3d099af7ba73e74c65c9 /src
parent1e25e1f042b2bd29ed28e1fd3ef92be8a09c264c (diff)
downloadlibfaketime-c1d10321a7629225b0d79f0a1f31e10e8c065c87.tar.gz
settime functions support FAKETIME_UPDATE_TIMESTAMP_FILE (for #239).
When the environment variable FAKETIME_TIMESTAMP_FILE is set, points to a writeable (creatable) custom config file and the environment variable FAKETIME_UPDATE_TIMESTAMP_FILE is "1", then the file also is updated on each call. By this, a common "virtual time" can be shared by several processes, where each can adjust the time for all.
Diffstat (limited to 'src')
-rw-r--r--src/libfaketime.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/libfaketime.c b/src/libfaketime.c
index 5abebc5..923e9f5 100644
--- a/src/libfaketime.c
+++ b/src/libfaketime.c
@@ -3408,6 +3408,42 @@ int clock_settime(clockid_t clk_id, const struct timespec *tp) {
setenv("FAKETIME", newenv_string, 1);
force_cache_expiration = 1; /* make sure it becomes effective immediately */
+ /* If FAKETIME_TIMESTAMP_FILE was given in environment,
+ * and if FAKETIME_UPDATE_TIMESTAMP_FILE=1, then update it.
+ * This allows other process instances to share the same time. */
+ if ( (getenv("FAKETIME_TIMESTAMP_FILE") != NULL)
+ && (*getenv("FAKETIME_TIMESTAMP_FILE") != '\0')
+ && (getenv("FAKETIME_UPDATE_TIMESTAMP_FILE") != NULL)
+ && (strcmp(getenv("FAKETIME_UPDATE_TIMESTAMP_FILE"), "1") == 0))
+ {
+ const char *error = NULL;
+ FILE *envfile;
+ static char custom_filename[BUFSIZ];
+ (void) snprintf(custom_filename, BUFSIZ, "%s", getenv("FAKETIME_TIMESTAMP_FILE"));
+
+ if ((envfile = fopen(custom_filename, "wt")) != NULL)
+ {
+ if (fprintf(envfile, "%+f\n", offset) < 0)
+ {
+ error = "to write to file";
+ }
+ if (fclose(envfile) != 0)
+ {
+ error = "to close file";
+ }
+ }
+ else
+ {
+ error = "to open file";
+ }
+ if (error)
+ {
+ fprintf(stderr, "libfaketime: In clock_settime(), failed to "
+ "%s while updating FAKETIME_TIMESTAMP_FILE (`%s'): %s\n",
+ error, getenv("FAKETIME_TIMESTAMP_FILE"), strerror(errno));
+ }
+ }
+
return 0;
}