When does a UPS/gateway race condition happen? When you are lots of kms from the...

@cynicalsecurity@bsd.network

When does a UPS/gateway race condition happen? When you are lots of kms from the office, of course…
So, I learned how to solve the race condition correctly using #nut and #OpenBSD with my #Eaton UPS (strongly recommended 'cos they don't cloudy cloud and speak USB or serial perfectly).
The problem statement is simple:

* UPS on battery
* shutdown initiated
* shutdown completes
* power returns before UPS is a 0%
* gateway machine remains happily off because there is no up/down on the power
* you are far from the office and there is nobody to press the power button on the gateway for you.

The solution I worked out with documentation and searches (thank you @kagihq ) is:

upsmon detects FSD (low battery / POWERDOWNFLAG trigger)
-> creates POWERDOWNFLAG file (e.g. /var/run/killpower)
-> executes SHUTDOWNCMD ("shutdown -p now")
-> OS shuts down processes, remounts FS read-only
-> /etc/rc.shutdown runs
-> checks: upsmon -K (exits 0 if POWERDOWNFLAG exists)
-> calls: upsdrvctl shutdown
-> starts a fresh driver instance: usbhid-ups -a eaton -k
-> driver sends "shutdown.return" to UPS
-> UPS timer starts (ups.delay.shutdown seconds)
-> OS completes power-off via ACPI
-> UPS cuts load output
-> (mains may already be back)
-> UPS restores load on mains presence
-> BIOS "boot on power restore" fires

Hope this helps someone else too :flan_hacker:​