# wolfcw/libfaketime architecture

> Architecture diagram of the GitHub repository wolfcw/libfaketime, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wolfcw/libfaketime
- Repository: https://github.com/wolfcw/libfaketime

## Overview

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: [`src/libfaketime.c`](https://github.com/wolfcw/libfaketime/blob/master/src/libfaketime.c) (libfaketime), [`test/test.sh`](https://github.com/wolfcw/libfaketime/blob/master/test/test.sh) (test runner), [`Makefile`](https://github.com/wolfcw/libfaketime/tree/master/Makefile) (build root), [`src/Makefile`](https://github.com/wolfcw/libfaketime/tree/master/src/Makefile) (src build)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core runtime"]
  node_src_libfaketime_c["libfaketime<br/>preload library<br/>[libfaketime.c]"]
  node_src_faketime_c["faketime wrapper<br/>cli launcher<br/>[faketime.c]"]
  node_src_timeprivacy["timeprivacy<br/>shell wrapper"]
end

subgraph group_state["State & coordination"]
  node_src_faketime_common_h["common config<br/>policy parser<br/>[faketime_common.h]"]
  node_src_time_ops_h["time math<br/>time engine<br/>[time_ops.h]"]
  node_src_ft_sem_c["shared state<br/>ipc coordination<br/>[ft_sem.c]"]
  node_src_ft_sem_h["ipc API<br/>ipc header<br/>[ft_sem.h]"]
end

subgraph group_platform["Platform support"]
  node_src_android_compat_h["android compat<br/>compat shim<br/>[android_compat.h]"]
  node_src_sunos_endian_h["sunos endian<br/>compat shim<br/>[sunos_endian.h]"]
  node_makefile_root["build root<br/>build system"]
  node_src_makefile["src build<br/>build system"]
  node_src_makefile_osx["macOS build<br/>platform build<br/>[Makefile.OSX]"]
end

subgraph group_tests["Tests"]
  node_test_runner["test runner<br/>test harness<br/>[test.sh]"]
  node_test_functests["functional tests<br/>test suite<br/>[test_true.sh]"]
  node_test_snippets["snippets<br/>api samples<br/>[clock_gettime.c]"]
  node_test_variadic["variadic tests<br/>test suite<br/>[main.c]"]
  node_ci_workflow["GitHub Actions<br/>ci config<br/>[make-test.yml]"]
  node_travis["Travis CI<br/>ci config<br/>[.travis.yml]"]
end

subgraph group_docs["Docs & packaging"]
  node_man_faketime_1["man page<br/>documentation<br/>[faketime.1]"]
  node_readme["README<br/>documentation"]
  node_readme_osx["README.OSX<br/>platform docs<br/>[README.OSX]"]
  node_readme_dev["dev notes<br/>documentation<br/>[README.developers]"]
  node_readme_pack["packaging<br/>documentation<br/>[README.packagers]"]
end

node_src_faketime_c -->|"launches with env"| node_src_libfaketime_c
node_src_timeprivacy -->|"wraps"| node_src_libfaketime_c
node_src_libfaketime_c -->|"reads policy"| node_src_faketime_common_h
node_src_libfaketime_c -->|"computes fake time"| node_src_time_ops_h
node_src_libfaketime_c -->|"syncs state"| node_src_ft_sem_c
node_src_ft_sem_c -->|"declares API"| node_src_ft_sem_h
node_src_libfaketime_c -.->|"adapts"| node_src_android_compat_h
node_src_libfaketime_c -.->|"adapts"| node_src_sunos_endian_h
node_src_libfaketime_c -.->|"documents"| node_man_faketime_1
node_src_faketime_c -.->|"documents"| node_man_faketime_1
node_makefile_root -->|"delegates"| node_src_makefile
node_makefile_root -->|"delegates"| node_src_makefile_osx
node_makefile_root -->|"builds"| node_man_faketime_1
node_src_makefile -->|"builds"| node_src_libfaketime_c
node_src_makefile -->|"builds"| node_src_faketime_c
node_src_makefile_osx -->|"builds"| node_src_libfaketime_c
node_test_runner -->|"runs"| node_test_functests
node_test_runner -->|"runs"| node_test_snippets
node_test_runner -->|"runs"| node_test_variadic
node_test_runner -->|"validates"| node_src_libfaketime_c
node_test_functests -->|"validates"| node_src_ft_sem_c
node_ci_workflow -->|"invokes"| node_test_runner
node_travis -->|"invokes"| node_test_runner
node_readme -.->|"explains"| node_src_libfaketime_c
node_readme_osx -.->|"explains"| node_src_makefile_osx
node_readme_dev -.->|"explains"| node_src_time_ops_h
node_readme_pack -.->|"explains"| node_makefile_root

click node_src_libfaketime_c "https://github.com/wolfcw/libfaketime/blob/master/src/libfaketime.c"
click node_src_faketime_c "https://github.com/wolfcw/libfaketime/blob/master/src/faketime.c"
click node_src_timeprivacy "https://github.com/wolfcw/libfaketime/tree/master/src/timeprivacy"
click node_src_faketime_common_h "https://github.com/wolfcw/libfaketime/blob/master/src/faketime_common.h"
click node_src_time_ops_h "https://github.com/wolfcw/libfaketime/blob/master/src/time_ops.h"
click node_src_ft_sem_c "https://github.com/wolfcw/libfaketime/blob/master/src/ft_sem.c"
click node_src_ft_sem_h "https://github.com/wolfcw/libfaketime/blob/master/src/ft_sem.h"
click node_src_android_compat_h "https://github.com/wolfcw/libfaketime/blob/master/src/android_compat.h"
click node_src_sunos_endian_h "https://github.com/wolfcw/libfaketime/blob/master/src/sunos_endian.h"
click node_makefile_root "https://github.com/wolfcw/libfaketime/tree/master/Makefile"
click node_src_makefile "https://github.com/wolfcw/libfaketime/tree/master/src/Makefile"
click node_src_makefile_osx "https://github.com/wolfcw/libfaketime/blob/master/src/Makefile.OSX"
click node_man_faketime_1 "https://github.com/wolfcw/libfaketime/blob/master/man/faketime.1"
click node_readme "https://github.com/wolfcw/libfaketime/tree/master/README"
click node_readme_osx "https://github.com/wolfcw/libfaketime/blob/master/README.OSX"
click node_readme_dev "https://github.com/wolfcw/libfaketime/blob/master/README.developers"
click node_readme_pack "https://github.com/wolfcw/libfaketime/blob/master/README.packagers"
click node_test_runner "https://github.com/wolfcw/libfaketime/blob/master/test/test.sh"
click node_test_functests "https://github.com/wolfcw/libfaketime/blob/master/test/functests/test_true.sh"
click node_test_snippets "https://github.com/wolfcw/libfaketime/blob/master/test/snippets/clock_gettime.c"
click node_test_variadic "https://github.com/wolfcw/libfaketime/blob/master/test/variadic/main.c"
click node_ci_workflow "https://github.com/wolfcw/libfaketime/blob/master/.github/workflows/make-test.yml"
click node_travis "https://github.com/wolfcw/libfaketime/blob/master/.travis.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_libfaketime_c,node_src_faketime_c,node_src_timeprivacy toneBlue
class node_src_faketime_common_h,node_src_time_ops_h,node_src_ft_sem_c,node_src_ft_sem_h toneAmber
class node_src_android_compat_h,node_src_sunos_endian_h,node_makefile_root,node_src_makefile,node_src_makefile_osx toneMint
class node_test_runner,node_test_functests,node_test_snippets,node_test_variadic,node_ci_workflow,node_travis toneRose
class node_man_faketime_1,node_readme,node_readme_osx,node_readme_dev,node_readme_pack toneIndigo
```

## Components

### Core runtime

Preload library, wrapper entrypoints, and fake-time computation path.

- [libfaketime](https://github.com/wolfcw/libfaketime/blob/master/src/libfaketime.c): preload library · Main interception library loaded into target processes to override time-related libc/syscall behavior. · `src/libfaketime.c`
- [faketime wrapper](https://github.com/wolfcw/libfaketime/blob/master/src/faketime.c): cli launcher · Command-line front end that prepares environment and launches a target under faketime. · `src/faketime.c`
- [timeprivacy](https://github.com/wolfcw/libfaketime/tree/master/src/timeprivacy): shell wrapper · Shell wrapper that gives each run a different time basis. · `src/timeprivacy`

### State & coordination

Parsing, timing rules, and shared-memory/semaphore coordination.

- [common config](https://github.com/wolfcw/libfaketime/blob/master/src/faketime_common.h): policy parser · Shared parsing and runtime control definitions used by the library and wrapper. · `src/faketime_common.h`
- [time math](https://github.com/wolfcw/libfaketime/blob/master/src/time_ops.h): time engine · Timestamp math and rule handling for absolute, relative, speed, and interval-based faketime. · `src/time_ops.h`
- [shared state](https://github.com/wolfcw/libfaketime/blob/master/src/ft_sem.c): ipc coordination · Semaphore and shared-memory coordination used to keep processes aligned on faketime state. · `src/ft_sem.c`
- [ipc API](https://github.com/wolfcw/libfaketime/blob/master/src/ft_sem.h): ipc header · Declarations for semaphore and shared-memory coordination. · `src/ft_sem.h`

### Platform support

OS-specific build paths and compatibility shims.

- [android compat](https://github.com/wolfcw/libfaketime/blob/master/src/android_compat.h): compat shim · Portability shim for Android-specific libc or platform behavior. · `src/android_compat.h`
- [sunos endian](https://github.com/wolfcw/libfaketime/blob/master/src/sunos_endian.h): compat shim · Portability shim for SunOS endianness and platform differences. · `src/sunos_endian.h`
- [build root](https://github.com/wolfcw/libfaketime/tree/master/Makefile): build system · Top-level build entrypoint for libraries, wrapper, and packaging targets. · `Makefile`
- [src build](https://github.com/wolfcw/libfaketime/tree/master/src/Makefile): build system · Main source build rules for the library and wrapper artifacts. · `src/Makefile`
- [macOS build](https://github.com/wolfcw/libfaketime/blob/master/src/Makefile.OSX): platform build · macOS-specific build rules and dynamic-library injection path. · `src/Makefile.OSX`

### Tests

Functional, harness, and snippet-based validation.

- [test runner](https://github.com/wolfcw/libfaketime/blob/master/test/test.sh): test harness · Top-level test orchestration and environment setup for validation runs. · `test/test.sh`
- [functional tests](https://github.com/wolfcw/libfaketime/blob/master/test/functests/test_true.sh): test suite · Shell-based behavior tests covering interception, follow mode, exclusion, and shared-memory behavior. · `test/functests/test_true.sh`
- [snippets](https://github.com/wolfcw/libfaketime/blob/master/test/snippets/clock_gettime.c): api samples · Small C programs that exercise specific intercepted APIs and syscall paths. · `test/snippets/clock_gettime.c`
- [variadic tests](https://github.com/wolfcw/libfaketime/blob/master/test/variadic/main.c): test suite · Nested-process tests that validate constructor and call-chain behavior. · `test/variadic/main.c`
- [GitHub Actions](https://github.com/wolfcw/libfaketime/blob/master/.github/workflows/make-test.yml): ci config · Automated build-and-test workflow for the repository. · `.github/workflows/make-test.yml`
- [Travis CI](https://github.com/wolfcw/libfaketime/blob/master/.travis.yml): ci config · Legacy continuous-integration configuration for build and test coverage. · `.travis.yml`

### Docs & packaging

User-facing documentation and packaging notes.

- [man page](https://github.com/wolfcw/libfaketime/blob/master/man/faketime.1): documentation · User-facing manual for the faketime wrapper and library behavior. · `man/faketime.1`
- [README](https://github.com/wolfcw/libfaketime/tree/master/README): documentation · Primary project overview and usage notes. · `README`
- [README.OSX](https://github.com/wolfcw/libfaketime/blob/master/README.OSX): platform docs · macOS-specific build and runtime notes. · `README.OSX`
- [dev notes](https://github.com/wolfcw/libfaketime/blob/master/README.developers): documentation · Developer-oriented architecture and maintenance notes. · `README.developers`
- [packaging](https://github.com/wolfcw/libfaketime/blob/master/README.packagers): documentation · Packaging guidance and distribution notes. · `README.packagers`

## Connections

- 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.
