apatch-linux chroot … uses the three-process chroot design below. apatch-linux uml boot launches the ARM64 UML kernel directly as the Termux user, optionally with umnet and passt. The guest kernel mounts the image as /dev/ubda and runs Ubuntu’s own init. Container engines installed there see the guest kernel’s cgroups, namespaces, and filesystems. The CLI doesn’t contact the broker or root daemon, bind Android directories, or mount the guest disk on the host. See the UML guide for paths and boot options.
The two Android walls
1. Seccomp — apps can’t do privileged syscalls
Android installs a seccomp filter into every app process (via Zygote). It blocks chroot, mount, umount2, swapon, and more. A blocked syscall becomes SIGSYS and kills the process:
libc: Fatal signal 31 (SIGSYS), code 1 (SYS_SECCOMP), syscall 159 ...
So the Termux app — even with su — often can’t chroot on its own.
2. SELinux — apps can’t reach a root socket
Even with root, SELinux confines processes by domain. The Termux CLI runs as untrusted_app, which is forbidden from connecting to a root-owned socket:
avc: denied { write } scontext=untrusted_app tcontext=... tclass=sock_file
So a naive “root daemon listens on a socket, app connects to it” fails — SELinux blocks the connection regardless of Unix permissions.
The three-process design
┌──────────────┐ app_data_file ┌──────────────┐ app_data_file ┌──────────────┐
│ Termux CLI │ ───────────────▶ │ broker │ ◀─────────────── │ core daemon │
│(untrusted_app│ (ALLOWED) │ (Termux uid) │ (root dials in, │ (root, │
│ no root) │ │ │ ALLOWED) │ no seccomp) │
└──────────────┘ └──────────────┘ └──────────────┘
reaches broker only ──────────── crosses the SELinux wall ───────── does chroot
Core daemon (daemon/daemon.py)
- Runs as root, started at boot by the APatch module.
- Outside the app seccomp filter, so
chroot/mount/openptywork. - Performs every privileged operation. Has no socket the CLI can reach (SELinux).
- Dials the broker (root →
app_data_fileis allowed) and serves requests over those connections.
Broker (daemon/broker.py)
- Runs as the Termux uid, so it owns an
app_data_filesocket the CLI can reach. - Pairs each CLI connection with a connected core relay and pipes bytes in both directions. One broker owns the socket paths; parked connections that have already closed are discarded.
Client (client/*.go)
- A compiled Go command in Termux’s
bin. No privileged syscalls. Connects to the broker, parses flags, renders help, and streams guest I/O. - The CLI itself doesn’t run under Python. The broker and core still require Termux Python, and the updater uses it for Android DNS when needed.
The direction is the key insight: because the CLI can’t dial root but root can dial the app’s socket, the core reaches out to the broker, and the broker bridges the two. (This is the pattern anland-termux uses for the same problem.)
Request lifecycle
- You run
apatch-chroot login ubuntu. - Client connects to the broker’s socket and sends a JSON request.
- Broker pairs that connection with one of the core’s ready relay connections and pipes the bytes to the core.
- Core dispatches (
handle_request) to the right command indaemon_cmds.py. - For
login, the corefork()s a child that: unshares a private mount namespace, sets up mounts,chroots, and execsbash -lon a PTY. - The core acknowledges the session before the client forwards stdin. PTY bytes stream back as length-prefixed frames, then the core sends the exit status. A dropped connection is an error, not a successful logout.
File map
| Path | Role |
|---|---|
daemon/daemon.py | Core. Root. Dials the broker, serves requests. |
daemon/daemon_cmds.py | All container commands + host probes. |
daemon/broker.py | Termux-uid relay. |
daemon/mounts.py | Private-namespace mount setup (proc/sys/dev/pts/shm, X11). |
daemon/syscalls.py | Raw mount/umount/unshare via ctypes. |
daemon/binfmt.py | Optional box64 registration. |
daemon/init.sh | Container init (mini-systemd PID-1 supervisor). |
daemon/systemctl-shim.sh | Drop-in systemctl for the mini-init. |
client/*.go | Native Termux CLI and wire protocol. |
client/pages.py | Help catalog (embedded via go generate ./client). |
module/* | APatch module: boot watchdog, service handoff, installer. |
Boot sequence
- Android boots. APatch runs the module’s
late_startservice. service.shwaits for Termux storage to unlock (FBE), then launches the watchdog detached.- The watchdog (
watchdog.sh) syncs the binary and broker into Termux at boot, and checks every 15s whether the core needs starting. - The core dials the broker and registers relay connections. Ready.
This is why after a reboot the daemon takes ~30-60s to be reachable — it waits for Android to finish booting and Termux storage to unlock.