Skip to content

Commit 1f5c021

Browse files
authored
Merge pull request #46 from NickHu/systemd-socket
Allow for systemd socket activation
2 parents b6f2578 + c319734 commit 1f5c021

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

mopidy_mpd/network.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
CONTROL_CHARS = dict.fromkeys(range(32))
1616

1717

18+
def get_systemd_socket():
19+
"""Attempt to get a socket from systemd."""
20+
fdnames = os.environ.get("LISTEN_FDNAMES", "").split(":")
21+
if "mpd" not in fdnames:
22+
return None
23+
fd = fdnames.index("mpd") + 3 # 3 is the first systemd file handle
24+
return socket.socket(fileno=fd)
25+
26+
1827
def get_unix_socket_path(socket_path):
1928
match = re.search("^unix:(.*)", socket_path)
2029
if not match:
@@ -122,6 +131,10 @@ def __init__(
122131
self.watcher = self.register_server_socket(self.server_socket.fileno())
123132

124133
def create_server_socket(self, host, port):
134+
sock = get_systemd_socket()
135+
if sock is not None:
136+
return sock
137+
125138
socket_path = get_unix_socket_path(host)
126139
if socket_path is not None: # host is a path so use unix socket
127140
sock = create_unix_socket()

0 commit comments

Comments
 (0)