aboutsummaryrefslogtreecommitdiffstats
path: root/deckit/sleep.py
diff options
context:
space:
mode:
Diffstat (limited to 'deckit/sleep.py')
-rw-r--r--deckit/sleep.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/deckit/sleep.py b/deckit/sleep.py
index 06cb481..c607cda 100644
--- a/deckit/sleep.py
+++ b/deckit/sleep.py
@@ -22,6 +22,8 @@ someone holds a delay inhibitor (at most InhibitDelayMaxSec, 5 s by
default). deck-it holds one; on the announcement the server marks every
answer for SHOW seconds, so the tablet's next poll sees it, then lets the
inhibitor go. On resume, or a shutdown called off, it takes it again.
+A shutdown also runs `deck-it stop` first, so the tunnels, wayvnc and the
+headless output go cleanly; the next boot's login brings it all back.
"""
import ctypes
@@ -29,10 +31,12 @@ import os
import signal
import subprocess
import time
+from pathlib import Path
SHOW = 2.0 # the tablet polls every second (0.5 s on the assistant tab)
WHY = {"PrepareForSleep": "asleep", "PrepareForShutdown": "shutting down"}
why = "" # the server sends it to the tablet while set: "The PC is <why>"
+STOP = [str(Path(__file__).resolve().parent.parent / "deck-it"), "stop"]
def _die_with_parent():
@@ -60,6 +64,16 @@ def _release(p):
p.wait()
+def _stop():
+ # Its own session: the stop kills this server, and must outlive it. The
+ # wait is bounded, the inhibitor only holds the shutdown for 5 s.
+ try:
+ subprocess.Popen(STOP, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
+ start_new_session=True).wait(timeout=2.5)
+ except (OSError, subprocess.TimeoutExpired):
+ pass
+
+
def watch():
"""Runs for the server's life; without elogind or dbus-monitor it does nothing."""
global why
@@ -80,6 +94,8 @@ def watch():
elif line == "boolean true" and member:
why = WHY[member]
time.sleep(SHOW)
+ if member == "PrepareForShutdown":
+ _stop()
_release(lock)
elif line == "boolean false" and member:
why = ""