hddfancontrol web UI documentation
Architecture
How the pieces fit together
How the web UI is put together: its processes, who may do what, where it keeps its data and in which order things start and stop. The reasons behind these choices are in decisions.md.
Processes
browser ──HTTPS/HTTP──▶ gateway (unprivileged) ──Unix socket──▶ core (root)
login, CSRF, TLS, settings, fans,
static files daemons, history
│
┌─────────────────────────────┼──────────────┐
▼ ▼ ▼
hddfancontrol hd-idle smartctl,
(measures only) (spin down) sensors-detect
- Core (
__main__.py,web.py), root. It builds everything from the environment, writes the PWM files, starts the daemons and keeps the history. Its API listens on a Unix socket only (folder mode 0710, socket 0660 for the gateway's group), never on the network.core_guardchecks the peer of each connection (SO_PEERCRED): only the gateway's user and root may talk to it. Every request that changes something is written to the audit log ([audit]). - Gateway (
gateway.py), the userhddfancontrol-webui(WEBUI_USER). It is the only process the network reaches: it serves the pages, checks the login, CSRF, theHostheader and the client network, speaks TLS, and forwards/api/…and/metricsto the core. It runs withpython -I, a filtered environment and no writable home. See security.md. - hddfancontrol measures the drive temperatures without waking sleeping drives and
writes them to a JSON lines log. It never touches a fan: it always runs with
--fan-cmd /bin/true. See fan-control.md. - hd-idle spins down the drives that have a spin down time.
- Short-lived programs:
smartctl(SMART data and self-tests),sensors-detectandmodprobe(sensor modules at start),openssl(the self-signed certificate, in the gateway).
Without a usable gateway user (as root: a missing user, root itself or root's group),
there is no gateway and no web UI, but the core still controls the fans. It never
falls back to nobody.
Every program is started with asyncio.create_subprocess_exec, never through a shell,
and every value that reaches a command line has been checked by settings.from_dict.
Inside the core
| Module | Job |
|---|---|
supervisor.py |
Runs hddfancontrol and hd-idle (Daemon: restart with a growing delay, 5 s doubling to 60 s), owns the fan controller, manual control and the PWM test behind one lock |
fancontrol.py |
Writes the PWM files: curve and auto modes, manual control, the safety rules |
templog.py |
Reads the last line of hddfancontrol's temperature log |
hardware.py |
Reads /dev/disk/by-id, /sys/class/hwmon, /sys/class/block, /proc; never sends a command to a drive (nor reads a drivetemp temperature, which does); the host report of the hardware support request |
settings.py |
The settings and their only validation |
monitor.py, history.py |
Records temperatures and fan speeds, reads SMART, raises alerts |
alerts.py, notify.py |
Alert states, notes, quiet hours, sending to ntfy, Gotify or JSON |
smart.py, drivedb.py |
SMART data and self-tests; brand and recording type of a model |
cron.py |
The cron expressions of every schedule |
labels.py, bays.py |
Names of drives and fans, the bay map |
updates.py |
Once a day, the latest release on GitHub, for the footer of the page (CHECK_UPDATES) |
privsep.py, tls.py, auth.py |
The gateway's user and folders, TLS, credentials |
Nothing blocks the event loop: file and SQLite work goes through
asyncio.to_thread, programs through asyncio.create_subprocess_exec.
Where the data lives
Everything is in /config (CONFIG_DIR), written with mode 0600 through
storage.write_atomic (temporary file, fsync, rename, fsync of the folder), so a power
cut never leaves an empty file. The core runs with umask 077.
| Path | Owner | What |
|---|---|---|
settings.json |
core | The settings; wins over the environment |
labels.json, bays.json |
core | Names of drives and fans, the bay map |
pwm-state.json |
core | The original mode of each PWM output, with the boot id |
history.db |
core | SQLite (WAL): samples, SMART summaries, alert notes, meta |
templog/temps.jsonl |
hddfancontrol | Its temperature log, rotated daily |
web/ |
gateway | auth.json (scrypt), the self-signed certificate |
web-tls/ |
core | Copies of TLS_CERT/TLS_KEY, readable by the gateway's group |
Root never writes in or follows anything inside web/: a gateway taken over could
plant links there. At start, tighten_config closes older files of /config, and a
/config the gateway could write is closed first (_close_config, the sticky bit
does not count); when it cannot be closed, no gateway runs (see
security.md).
Settings
settings.json wins over the environment variables, which keep working for a
container set up the upstream way (env_document). A broken file falls back to the
environment and shows an error. Applying settings restarts hddfancontrol and hd-idle;
names and bays have their own files and never restart anything.
The PWM outputs are kept with the /sys/devices/… path of their chip. When the hwmon
numbers change at a reboot (hwmon2 becomes hwmon3), Supervisor._placed finds the
same output again (Hardware.locate), saves the settings and moves the label. A fan
whose chip is gone is listed in missing_pwms and only that fan is not controlled: the
others are, and hddfancontrol measures the drives all the same. When no fan is left to
control, the mode is fans_missing rather than running. While the list is not
empty the supervisor looks again every PLACE_PERIOD (60 s) and after each sensor scan
of the page (recheck_fans), and restarts with the fan as soon as its chip is back. The
chip of a fan is always the host's: apply drops one sent with the settings, and
_placed finds the real one with Hardware.chip_of. Saving at start and
moving the label are extras: when /config is full or read only, the error is logged
(ERROR: …) and the fans are controlled at their new place anyway. An apply still
fails with save_failed when the settings cannot be saved (that is what was asked),
but never because of a label.
Start and stop
Start, in the background so the web UI answers at once (startup in __main__.py).
The checks of step 1 run beside steps 2 to 4, which never wait for them: they open
every drive and fan file, and an open() that hangs on a dying drive must never keep
the fans from being controlled. They run in a daemon thread of their own
(in_daemon_thread, so a hung one does not hold the exit either), as checks_task,
cancelled at the stop; checks in the status is empty until they end.
- The startup checks (
checks.py,run_checks): what the host or the container keeps the web UI from doing, each logged as a WARNING with what to do and shown as a banner (checksin the status; the page can hide one while it says the same,localStoragechecksHidden): the core not root (not_root),/confignot writable (config_not_writable, a temporary file written and removed), no drive in/dev/disk/by-id(no_drives), a drive of the settings that is here but does not open (drive_not_readable, an open withO_NONBLOCK, nothing read), a chosen PWM file not writable (pwm_not_writable), root withoutCAP_SYS_RAWIO,CAP_SYS_ADMINor, withLOAD_SENSOR_MODULES,CAP_SYS_MODULE(missing_capabilities, fromCapEffof/proc/self/status), a program not inPATH(program_missing). It changes nothing, and a check that breaks is logged, never raised./sysread only is left to its own mode and banner, a drive that is gone to thedrive_missingalert. They run once: a fix needs a restart anyway. - The sensor modules (
sensors-detect), unlessLOAD_SENSOR_MODULES=false. A failure never keeps the fans from being controlled. A scan always ends asdone, at start and when the page asks for one again (POST /api/sensors/scan): a program that hangs is killed atRUN_TIMEOUT(300 s) and reported, and an unexpected error lands indetect_error, so a later scan is never refused asbusy. The page's scan answers 202 at once and runs in the background (longer than the gateway'sCORE_TIMEOUT_S); the page followssensors.statein the status and reads the hardware again when it isdone. Supervisor.start: hddfancontrol, hd-idle, the fan controller.Monitor.start: recording, SMART, alerts.
Stop (SIGTERM or SIGINT):
Supervisor.release_fans: cancels a PWM test, ends manual control, gives every fan back (fans.stop(final=True)); from then on nothing starts and changes answerBusy.- The gateway stops, then the daemons.
Read only
When /sys is mounted read only (a container that is not privileged), nothing starts:
the mode is read_only, temperatures and SMART still show, and manual control and the
PWM test are refused.