Skip to content

Commit

Permalink
Merge pull request #46 from NickHu/systemd-socket
Browse files Browse the repository at this point in the history
Allow for systemd socket activation
  • Loading branch information
jodal committed Nov 10, 2022
2 parents b6f2578 + c319734 commit 1f5c021
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions mopidy_mpd/network.py
Expand Up @@ -15,6 +15,15 @@
CONTROL_CHARS = dict.fromkeys(range(32))


def get_systemd_socket():
"""Attempt to get a socket from systemd."""
fdnames = os.environ.get("LISTEN_FDNAMES", "").split(":")
if "mpd" not in fdnames:
return None
fd = fdnames.index("mpd") + 3 # 3 is the first systemd file handle
return socket.socket(fileno=fd)


def get_unix_socket_path(socket_path):
match = re.search("^unix:(.*)", socket_path)
if not match:
Expand Down Expand Up @@ -122,6 +131,10 @@ def __init__(
self.watcher = self.register_server_socket(self.server_socket.fileno())

def create_server_socket(self, host, port):
sock = get_systemd_socket()
if sock is not None:
return sock

socket_path = get_unix_socket_path(host)
if socket_path is not None: # host is a path so use unix socket
sock = create_unix_socket()
Expand Down

0 comments on commit 1f5c021

Please sign in to comment.