Architecture¶
How the 7 tools fit into the Android stack, from the Termux shell down to the kernel subsystems. Useful for understanding where each command acts and why it needs root for certain things.
Overview¶
flowchart TB
subgraph Termux[Termux — UID u0_a87]
Shell[Bash shell]
Python[Python 3 + rich]
Cron[cron / tmux]
end
subgraph Tools[Headless Lab tools]
appctl
createswap
battery
touchctl[touch-ctl]
screenstate[screen-state<br/>screen-toggle]
userlist[user-list]
end
subgraph Elevacao[Root elevation]
su[su -c — Magisk]
sudo
end
subgraph Android[Android subsystems]
PM[PackageManager<br/>pm disable / enable]
PowerManager[PowerManager<br/>dumpsys power<br/>input keyevent]
BatteryManager[BatteryManager<br/>dumpsys battery]
Input[InputReader<br/>/dev/input/eventN]
Sysfs[sysfs<br/>/sys/class/input<br/>/sys/block/zram0]
Swap[Kernel swap<br/>swapon / swapoff<br/>/proc/swaps]
MagiskHooks[Magisk hooks<br/>post-fs-data.d<br/>service.d]
end
Shell --> Tools
Python --> Tools
Cron --> Tools
Tools --> Elevacao
Elevacao --> Android
appctl --> PM
appctl --> PowerManager
createswap --> Swap
createswap --> Sysfs
createswap --> MagiskHooks
battery --> BatteryManager
touchctl --> Input
touchctl --> Sysfs
screenstate --> PowerManager
userlist --> PM
userlist --> PowerManager
Layer by layer¶
1. Termux (shell + Python)¶
Scripts live in ~/headless-lab/bin/ (cloned from git). install.sh creates
symlinks in $PREFIX/bin/ (usually
/data/data/com.termux/files/usr/bin/), which is in Termux's PATH. Termux
runs as UID u0_aNN (an Android app sandbox), without privileges.
Python 3 + rich handles the TUIs
(appctl, createswap, battery, user-list). Pure Bash scripts
(touch-ctl, screen-state, screen-toggle) avoid dependencies to reduce
footprint on ARM 32 bits.
2. Root elevation¶
Each tool tries, in this order:
id -u == 0→ already root, run directly;su -c CMD→ Magisk (the common case);sudo CMD→ fallback (rare on Android).
Magisk intercepts su -c and grants or denies based on the list of authorized
apps in Magisk → Superuser. Each tool appears there as "Termux" (not as
appctl, battery, etc.) because Termux is what requests root.
Elevation is transparent to the user — you never type sudo. If it fails, the
message tells you exactly why:
3. Android subsystems¶
Each tool maps to specific subsystems:
| Subsystem | Tools that use it | Access path |
|---|---|---|
| PackageManager | appctl, user-list |
pm list packages, pm disable-user, pm enable |
| PowerManager | screen-state, screen-toggle, battery, appctl (repair) |
dumpsys power, input keyevent, am force-stop |
| BatteryManager | battery |
dumpsys battery, /sys/class/power_supply/battery/ |
| InputReader | touch-ctl |
/dev/input/eventN, /sys/class/input/event*/device/name |
| Memory management | createswap |
/proc/swaps, /proc/meminfo, /sys/block/zram0/, swapon, swapoff, mkswap |
| Magisk hooks | createswap (--install-boot) |
/data/adb/post-fs-data.d/, /data/adb/service.d/ |
4. Magisk hooks (only createswap --install-boot)¶
--install-boot creates /data/adb/post-fs-data.d/createswap.sh. Magisk runs
everything in post-fs-data.d/ right after mounting /data at boot. The hook:
- waits for
sys.boot_completed=1(while ! getprop ...); - reads saved config from
/data/local/tmp/createswap.cfg; - reconfigures ZRAM (
echo lz4 > /sys/block/zram0/comp_algorithm,echo 1G > disksize, etc.); mkswap+swaponthe swapfile at/data/swapfile;- logs errors to
/data/local/tmp/createswap-boot.log; exit 0always — doesn't break the boot.
The /data/local/tmp/createswap.disable flag turns the hook into a no-op
(useful for diagnostics without uninstalling).
Flow of an operation: createswap --setup-zram 1G¶
sequenceDiagram
participant U as User
participant C as createswap
participant S as su -c (Magisk)
participant K as Kernel/Android
U->>C: createswap --setup-zram 1G
C->>C: read /proc/swaps, /sys/block/zram0
C->>C: plan: comp_algorithm=lz4, disksize=1G, prio=100
C->>U: show plan, ask confirmation
U->>C: yes
C->>S: echo lz4 > /sys/block/zram0/comp_algorithm
S->>K: write to sysfs
C->>S: echo 1G > /sys/block/zram0/disksize
S->>K: configure size
C->>S: mkswap /dev/block/zram0
C->>S: swapon -p 100 /dev/block/zram0
S->>K: activate swap with priority 100
C->>U: "after" panel
Note over C: if any step fails,<br/>undoes previous ones (rollback)
Flow: touch-ctl off¶
sequenceDiagram
participant U as User
participant T as touch-ctl
participant S as su -c (Magisk)
participant K as Kernel/Android
U->>T: touch-ctl off
T->>T: elevate() — become root via su -c
T->>S: ls /sys/class/input/event*/device/name
S->>K: read sysfs
K-->>T: find event3 (touchpanel)
T->>S: cat /sys/class/input/event3/dev
K-->>T: DEVNUM=maj:min
T->>S: mknod test on temp file
K-->>T: OK (pre-flight passed)
T->>S: stat /dev/input/event3 (save perms)
T->>T: write /data/local/tmp/touch-ctl.state
T->>S: rm /dev/input/event3
K-->>K: Android InputReader detects node removal via inotify
K-->>K: stops reading touch events
T->>U: "Touch disabled"
Note over T: trap EXIT: if rm failed after removal,<br/>recreates the node automatically
Flow: appctl --profile servidor-seguro¶
sequenceDiagram
participant U as User
participant A as appctl
participant S as su -c
participant PM as PackageManager
U->>A: --profile servidor-seguro
A->>A: load profile (packages to disable)
A->>A: check blocked packages (settings, magisk, termux, keyboard)
A->>A: check plan doesn't leave device without keyboard
A->>U: show table, ask confirmation
U->>A: yes
A->>A: save JSON snapshot to ~/.appctl/snapshots
loop for each package in plan
A->>S: pm disable-user --user 0 <package>
S->>PM: disable
PM-->>A: ok
end
A->>U: summary
Design decisions¶
Why Python + Bash, not just Python?¶
- Bash for scripts that only call Android commands (
pm,dumpsys,input,mknod) — they're short, and Python dependencies on ARM 32 bits are slow to start; - Python + rich for complex TUIs (colored tables, keyboard navigation) —
would be impossible in pure Bash without
dialog/whiptcl(not included in Termux by default).
Why pm disable-user instead of pm uninstall?¶
disable-user hides the app from user 0 (the main one) but keeps the APK
installed. Advantages:
- reversible without reinstalling (
pm enable); - doesn't break OTA updates (which expect certain packages present);
- doesn't delete app data (useful if you want to re-enable later with everything intact).
Why KEYCODE_WAKEUP/SLEEP instead of power?¶
KEYCODE_POWER (26) only toggles — you can't guarantee "on" or "off"
without a race condition. KEYCODE_WAKEUP (224) and KEYCODE_SLEEP (223) are
idempotent — designed exactly for automation.
Why /dev/input instead of disabling the driver?¶
Disabling the touch driver would require rmmod or kernel recompilation —
heavy, irreversible, and driver-dependent. Removing the /dev/input/eventN
node makes Android stop reading events from that device (InputReader polls by
node, not by driver), with the same practical effect and total reversibility.
Why does the boot hook always exit 0?¶
If a post-fs-data.d hook exits with an error, Magisk may abort the boot in
some configurations. exit 0 ensures the boot continues even if swap failed —
you fix it later via SSH.