sdnotify.py documentation

This commit is contained in:
Scott Kuhl 2020-10-18 16:45:57 -04:00
parent c1cc3911df
commit 52558174b8

View File

@ -1,3 +1,13 @@
"""When sshuttle is run via a systemd service file, we can communicate
to systemd about the status of the sshuttle process. In particular, we
can send READY status to tell systemd that sshuttle has completed
startup and send STOPPING to indicate that sshuttle is beginning
shutdown.
For details, see:
https://www.freedesktop.org/software/systemd/man/sd_notify.html
"""
import socket import socket
import os import os
@ -5,6 +15,7 @@ from sshuttle.helpers import debug1
def _notify(message): def _notify(message):
"""Send a notification message to systemd."""
addr = os.environ.get("NOTIFY_SOCKET", None) addr = os.environ.get("NOTIFY_SOCKET", None)
if not addr or len(addr) == 1 or addr[0] not in ('/', '@'): if not addr or len(addr) == 1 or addr[0] not in ('/', '@'):
@ -31,16 +42,22 @@ def _notify(message):
def send(*messages): def send(*messages):
"""Send multiple messages to systemd."""
return _notify(b'\n'.join(messages)) return _notify(b'\n'.join(messages))
def ready(): def ready():
"""Constructs a message that is appropriate to send upon completion of
sshuttle startup."""
return b"READY=1" return b"READY=1"
def stop(): def stop():
"""Constructs a message that is appropriate to send when sshuttle is
beginning to shutdown."""
return b"STOPPING=1" return b"STOPPING=1"
def status(message): def status(message):
"""Constructs a status message to be sent to systemd."""
return b"STATUS=%s" % message.encode('utf8') return b"STATUS=%s" % message.encode('utf8')