Update websockifyserver.py

Systemd or upstart socket detection and activation.
Ability to activate systemd or upstart socket only on-demand.
This commit is contained in:
valia0906 2017-09-26 11:30:58 +03:00 committed by GitHub
parent 0506b3431f
commit 07e849630e
1 changed files with 20 additions and 4 deletions

View File

@ -369,6 +369,16 @@ class WebSockifyServer(object):
raise Exception("No 'ssl' module and SSL-only specified") raise Exception("No 'ssl' module and SSL-only specified")
if self.daemon and not resource: if self.daemon and not resource:
raise Exception("Module 'resource' required to daemonize") raise Exception("Module 'resource' required to daemonize")
# Systemd or upstart socket detection
if os.environ.get('LISTEN_PID', None) == str(os.getpid()):
self.listen_host = "socketfromfd"
self.listen_port = 3
self.msg("Detected systemd socket activation, listening fd %s", self.listen_port)
elif os.environ.get('UPSTART_EVENTS', None) == "socket":
self.listen_host = "socketfromfd"
self.listen_port = int(os.environ.get('UPSTART_FDS'))
self.msg("Detected upstart socket activation, listening fd %s", self.listen_port)
# Show configuration # Show configuration
self.msg("WebSocket server settings:") self.msg("WebSocket server settings:")
@ -425,10 +435,16 @@ class WebSockifyServer(object):
raise Exception("SSL only supported in connect mode (for now)") raise Exception("SSL only supported in connect mode (for now)")
if not connect: if not connect:
flags = flags | socket.AI_PASSIVE flags = flags | socket.AI_PASSIVE
if not unix_socket: # Starting systemd or upstart socket
addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, if host == "socketfromfd":
socket.IPPROTO_TCP, flags) sock = socket.fromfd(port, socket.AF_INET, socket.SOCK_STREAM)
if sys.version_info.major == 2:
sock = socket.socket(_sock=sock)
elif not unix_socket:
addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM,
socket.IPPROTO_TCP, flags)
if not addrs: if not addrs:
raise Exception("Could not resolve host '%s'" % host) raise Exception("Could not resolve host '%s'" % host)
addrs.sort(key=lambda x: x[0]) addrs.sort(key=lambda x: x[0])