support systemd socket activation (fd passing) (#515)

* add support for socket passing
* slight tweaks before merge

---------

Signed-off-by: mat <matheuz1210@gmail.com>
Signed-off-by: ed <s@ocv.me>
Co-authored-by: ed <s@ocv.me>
This commit is contained in:
mati1210 2025-08-07 12:00:53 -03:00 committed by GitHub
parent a9ee4f24d5
commit 9b9d2a92ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 24 additions and 11 deletions

View file

@ -536,7 +536,7 @@ def get_sects():
dedent( dedent(
""" """
\033[33m-i\033[0m takes a comma-separated list of interfaces to listen on; \033[33m-i\033[0m takes a comma-separated list of interfaces to listen on;
IP-addresses and/or unix-sockets (Unix Domain Sockets) IP-addresses, unix-sockets and/or open file descriptors
the default (\033[32m-i ::\033[0m) means all IPv4 and IPv6 addresses the default (\033[32m-i ::\033[0m) means all IPv4 and IPv6 addresses
@ -562,7 +562,9 @@ def get_sects():
\033[32m-i unix:\033[33m/dev/shm/party.sock\033[0m keeps umask-defined permission \033[32m-i unix:\033[33m/dev/shm/party.sock\033[0m keeps umask-defined permission
(usually \033[33m0600\033[0m) and the same user/group as copyparty (usually \033[33m0600\033[0m) and the same user/group as copyparty
\033[33m-p\033[0m (tcp ports) is ignored for unix sockets \033[32m-i fd:\033[33m3\033[0m uses the socket passed to copyparty on file descriptor 3
\033[33m-p\033[0m (tcp ports) is ignored for non ip-addresses
""" """
), ),
], ],

View file

@ -607,7 +607,7 @@ class Ftpd(object):
if "::" in ips: if "::" in ips:
ips.append("0.0.0.0") ips.append("0.0.0.0")
ips = [x for x in ips if "unix:" not in x] ips = [x for x in ips if not x.startswith(("unix:", "fd:"))]
if self.args.ftp4: if self.args.ftp4:
ips = [x for x in ips if ":" not in x] ips = [x for x in ips if ":" not in x]

View file

@ -868,7 +868,7 @@ class SvcHub(object):
have_tcp = False have_tcp = False
for zs in al.i: for zs in al.i:
if not zs.startswith("unix:"): if not zs.startswith(("unix:", "fd:")):
have_tcp = True have_tcp = True
if not have_tcp: if not have_tcp:
zb = False zb = False
@ -878,7 +878,7 @@ class SvcHub(object):
setattr(al, zs, False) setattr(al, zs, False)
zb = True zb = True
if zb: if zb:
t = "only listening on unix-sockets; cannot enable zeroconf/mdns/ssdp as requested" t = "no ip addresses provided; cannot enable zeroconf/mdns/ssdp as requested"
self.log("root", t, 3) self.log("root", t, 3)
if not self.args.no_dav: if not self.args.no_dav:

View file

@ -25,8 +25,8 @@ from .util import (
termsize, termsize,
) )
if True: if True: # pylint: disable=using-constant-test
from typing import Generator, Union from typing import Generator, Optional, Union
if TYPE_CHECKING: if TYPE_CHECKING:
from .svchub import SvcHub from .svchub import SvcHub
@ -245,8 +245,10 @@ class TcpSrv(object):
def _listen(self, ip: str, port: int) -> None: def _listen(self, ip: str, port: int) -> None:
uds_perm = uds_gid = -1 uds_perm = uds_gid = -1
bound: Optional[socket.socket] = None
tcp = False
if "unix:" in ip: if "unix:" in ip:
tcp = False
ipv = socket.AF_UNIX ipv = socket.AF_UNIX
uds = ip.split(":") uds = ip.split(":")
ip = uds[-1] ip = uds[-1]
@ -259,7 +261,12 @@ class TcpSrv(object):
import grp import grp
uds_gid = grp.getgrnam(uds[2]).gr_gid uds_gid = grp.getgrnam(uds[2]).gr_gid
elif "fd:" in ip:
fd = ip[3:]
bound = socket.socket(fileno=int(fd))
tcp = bound.proto == socket.IPPROTO_TCP
ipv = bound.family
elif ":" in ip: elif ":" in ip:
tcp = True tcp = True
ipv = socket.AF_INET6 ipv = socket.AF_INET6
@ -267,7 +274,7 @@ class TcpSrv(object):
tcp = True tcp = True
ipv = socket.AF_INET ipv = socket.AF_INET
srv = socket.socket(ipv, socket.SOCK_STREAM) srv = bound or socket.socket(ipv, socket.SOCK_STREAM)
if not ANYWIN or self.args.reuseaddr: if not ANYWIN or self.args.reuseaddr:
srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) srv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@ -285,6 +292,10 @@ class TcpSrv(object):
if getattr(self.args, "freebind", False): if getattr(self.args, "freebind", False):
srv.setsockopt(socket.SOL_IP, socket.IP_FREEBIND, 1) srv.setsockopt(socket.SOL_IP, socket.IP_FREEBIND, 1)
if bound:
self.srv.append(srv)
return
try: try:
if tcp: if tcp:
srv.bind((ip, port)) srv.bind((ip, port))
@ -437,7 +448,7 @@ class TcpSrv(object):
def detect_interfaces(self, listen_ips: list[str]) -> dict[str, Netdev]: def detect_interfaces(self, listen_ips: list[str]) -> dict[str, Netdev]:
from .stolen.ifaddr import get_adapters from .stolen.ifaddr import get_adapters
listen_ips = [x for x in listen_ips if "unix:" not in x] listen_ips = [x for x in listen_ips if not x.startswith(("unix:", "fd:"))]
nics = get_adapters(True) nics = get_adapters(True)
eps: dict[str, Netdev] = {} eps: dict[str, Netdev] = {}

View file

@ -179,7 +179,7 @@ class Tftpd(object):
if "::" in ips: if "::" in ips:
ips.append("0.0.0.0") ips.append("0.0.0.0")
ips = [x for x in ips if "unix:" not in x] ips = [x for x in ips if not x.startswith(("unix:", "fd:"))]
if self.args.tftp4: if self.args.tftp4:
ips = [x for x in ips if ":" not in x] ips = [x for x in ips if ":" not in x]