How wolfcw/libfaketime works
1. What this repo is
- libfaketime is a preloadable time-interception system, not a server or app.
- Its primary job is to sit between a target process and libc/system time APIs, then return adjusted timestamps based on user-supplied rules.
2. Core runtime path
- The main shared library is in src/libfaketime.c.
- At process startup, the library is injected via LD_PRELOAD on Linux or DYLD_INSERT_LIBRARIES on macOS.
- Once loaded, it intercepts calls such as time(), clock_gettime(), sleep/timer functions, and optionally file timestamp operations and time-setting calls.
3. Configuration inputs
- The fake-time policy comes from environment variables first, especially FAKETIME.
- It can also be read from config files such as FAKETIME_TIMESTAMP_FILE, ~/.faketimerc, or /etc/faketimerc.
- Additional runtime controls include monotonic-clock fixes, caching controls, process scoping, spawn hooks, and shared-time settings.
4. Time computation layer
- Shared parsing and time math live in src/time_ops.h and src/faketime_common.h.
- The library supports absolute timestamps, “start at” timestamps, relative offsets, speed modifiers (x), and interval-based activation.
- This layer is what turns a user rule into a per-call fake timestamp or offset.
5. System-call interception boundary
- The important architectural boundary is “real libc / kernel” versus “intercepted facade.”
- The library forwards to the actual system functions after adjusting arguments or return values, so the target program sees a consistent fake clock while the host OS remains unchanged.
6. Concurrency and cross-process coordination
- src/ft_sem.c and src/ft_sem.h implement semaphore/shared-memory coordination.
- This is used to keep parent and child processes aligned on faketime state and to manage some shared cached data.
- The design can be switched off with FAKETIME_DISABLE_SHM when shared-memory cleanup or compatibility is a problem.
7. Optional extended behaviors
- FAKE_SETTIME mode extends the interception surface to time-setting calls like clock_settime(), settimeofday(), and adjtime().
- File-follow mode lets a file timestamp drive the fake clock.
- A deterministic-random mode can also intercept getrandom() when compiled with FAKE_RANDOM.
8. Command wrapper
- src/faketime.c builds the faketime command-line wrapper.
- The wrapper is a convenience front end that sets up the right environment variables and launches the target program.
- src/timeprivacy is a related shell wrapper for giving each run a different time basis.
9. Platform-specific adaptation
- src/android_compat.h and src/sunos_endian.h show that the code has portability shims for nonstandard libc/platform behavior.
- The macOS build uses separate makefiles and dynamic-library injection semantics rather than Linux-style LD_PRELOAD.
10. Build outputs
- The repo produces at least two library variants: a normal single-threaded/shared library and libfaketimeMT.so.1 for pthread-aware use.
- The wrapper binary/script and man page are built alongside the library.
- Makefile drives the main build; src/Makefile.OSX and man/Makefile.OSX handle the macOS path.
11. Test architecture
- The test/ tree is a separate subsystem with its own build and runner.
- It includes C harnesses, shell-based functional tests, random/repeat tests, and small snippet programs that exercise specific intercepted APIs.
- This validates both API-level behavior and cross-process/shared-memory behavior.
12. Repository-level automation
- CI is lightweight but present: GitHub Actions and Travis config run test builds.
- That implies the repo’s primary integration contract is “can the library be built and can interception tests pass across supported platforms?”
13. Documentation and packaging
- README, README.OSX, README.developers, and README.packagers act as the main architecture docs and platform notes.
- man/faketime.1 documents the user-facing wrapper, reinforcing that the product boundary is a command-line tool plus preload library rather than an application daemon.
14. How to diagram the flow
- The key diagram should show: user config/environment → wrapper or preload loader → libfaketime interception layer → time computation/state/cache/shared-memory → real libc/kernel time calls.
- A side branch should show optional file-based config, optional time-setting synchronization, and optional test/CI/build tooling around the core library.
Main entry points: libfaketime.c libfaketime · test.sh test runner · Makefile build root · src/Makefile src build
Components 23
Core runtime
Preload library, wrapper entrypoints, and fake-time computation path.
- libfaketime · Main interception library loaded into target processes to override time-related libc/syscall behavior.
src/libfaketime.c - faketime wrapper · Command-line front end that prepares environment and launches a target under faketime.
src/faketime.c - timeprivacy · Shell wrapper that gives each run a different time basis.
src/timeprivacy
State & coordination
Parsing, timing rules, and shared-memory/semaphore coordination.
- common config · Shared parsing and runtime control definitions used by the library and wrapper.
src/faketime_common.h - time math · Timestamp math and rule handling for absolute, relative, speed, and interval-based faketime.
src/time_ops.h - shared state · Semaphore and shared-memory coordination used to keep processes aligned on faketime state.
src/ft_sem.c - ipc API · Declarations for semaphore and shared-memory coordination.
src/ft_sem.h
Platform support
OS-specific build paths and compatibility shims.
- android compat · Portability shim for Android-specific libc or platform behavior.
src/android_compat.h - sunos endian · Portability shim for SunOS endianness and platform differences.
src/sunos_endian.h - build root · Top-level build entrypoint for libraries, wrapper, and packaging targets.
Makefile - src build · Main source build rules for the library and wrapper artifacts.
src/Makefile - macOS build · macOS-specific build rules and dynamic-library injection path.
src/Makefile.OSX
Tests
Functional, harness, and snippet-based validation.
- test runner · Top-level test orchestration and environment setup for validation runs.
test/test.sh - functional tests · Shell-based behavior tests covering interception, follow mode, exclusion, and shared-memory behavior.
test/functests/test_true.sh - snippets · Small C programs that exercise specific intercepted APIs and syscall paths.
test/snippets/clock_gettime.c - variadic tests · Nested-process tests that validate constructor and call-chain behavior.
test/variadic/main.c - GitHub Actions · Automated build-and-test workflow for the repository.
.github/workflows/make-test.yml - Travis CI · Legacy continuous-integration configuration for build and test coverage.
.travis.yml
Docs & packaging
User-facing documentation and packaging notes.
- man page · User-facing manual for the faketime wrapper and library behavior.
man/faketime.1 - README · Primary project overview and usage notes.
README - README.OSX · macOS-specific build and runtime notes.
README.OSX - dev notes · Developer-oriented architecture and maintenance notes.
README.developers - packaging · Packaging guidance and distribution notes.
README.packagers
Connections 27
- faketime wrapper → libfaketime: launches with env · The wrapper prepares faketime environment and execs the target under the preload library.
- timeprivacy → libfaketime: wraps · Shell wrapper provides an alternate run-time basis before the library intercepts time calls.
- libfaketime → common config: reads policy · The preload library consumes shared configuration and runtime control definitions.
- libfaketime → time math: computes fake time · Interception handlers use the shared time-math layer to derive returned timestamps.
- libfaketime → shared state: syncs state · The library uses shared-memory/semaphore coordination for parent-child alignment and caching.
- shared state → ipc API: declares API · Implementation is paired with shared coordination declarations.
- libfaketime → android compat: adapts · Platform compatibility shims support nonstandard libc behavior.
- libfaketime → sunos endian: adapts · Platform compatibility shims support nonstandard libc behavior.
- libfaketime → man page: documents · The manual describes the wrapper and the interception semantics exposed by the library.
- faketime wrapper → man page: documents · The wrapper's user-facing behavior is captured in the manual page.
- build root → src build: delegates · Top-level build drives the main source rules.
- build root → macOS build: delegates · Top-level build includes the macOS-specific path.
- build root → man page: builds · Build system produces the installed documentation alongside binaries.
- src build → libfaketime: builds · Main source makefile compiles the preload library.
- src build → faketime wrapper: builds · Main source makefile builds the CLI wrapper.
- macOS build → libfaketime: builds · macOS rules produce the library with injection semantics for that platform.
- test runner → functional tests: runs · The test frame executes shell-based functional checks.
- test runner → snippets: runs · The test frame compiles and executes focused API snippets.
- test runner → variadic tests: runs · The test frame validates constructor and nested-process behavior.
- test runner → libfaketime: validates · Tests exercise the interception boundary of the preload library.
- functional tests → shared state: validates · Functional tests cover shared-memory coordination across processes.
- GitHub Actions → test runner: invokes · GitHub Actions runs the test build and validation pipeline.
- Travis CI → test runner: invokes · Travis CI runs build and test automation.
- README → libfaketime: explains · Project README describes the preload interception model.
- README.OSX → macOS build: explains · macOS notes document the platform-specific build path.
- dev notes → time math: explains · Developer notes cover shared time computation and runtime design.
- packaging → build root: explains · Packaging notes describe build and distribution expectations.