def mdns_publish(display_name, mode, listen_on, text_dict={}): global MDNS_WARNING if MDNS_WARNING is True: return try: from xpra.net import mdns assert mdns from xpra.net.mdns import XPRA_MDNS_TYPE, RFB_MDNS_TYPE PREFER_PYBONJOUR = envbool("XPRA_PREFER_PYBONJOUR", False) or WIN32 or OSX PREFER_ZEROCONF = envbool("XPRA_PREFER_ZEROCONF", False) if PREFER_PYBONJOUR: from xpra.net.mdns.pybonjour_publisher import BonjourPublishers as MDNSPublishers, get_interface_index elif PREFER_ZEROCONF: from xpra.net.mdns.zeroconf_publisher import ZeroconfPublishers as MDNSPublishers, get_interface_index else: from xpra.net.mdns.avahi_publisher import AvahiPublishers as MDNSPublishers, get_interface_index except ImportError as e: MDNS_WARNING = True from xpra.log import Logger log = Logger("mdns") log("mdns import failure", exc_info=True) log.warn("Warning: failed to load the mdns publisher") try: einfo = str(e) except: einfo = str(type(e)) log.warn(" %s", einfo) log.warn(" either install the 'python-avahi' module") log.warn(" or use the 'mdns=no' option") return d = text_dict.copy() d["mode"] = mode #ensure we don't have duplicate interfaces: f_listen_on = {} for host, port in listen_on: f_listen_on[(get_interface_index(host), port)] = (host, port) try: name = socket.gethostname() except: name = "Xpra" if display_name and not (OSX or WIN32): name += " %s" % display_name if mode not in ("tcp", "rfb"): name += " (%s)" % mode service_type = {"rfb": RFB_MDNS_TYPE}.get(mode, XPRA_MDNS_TYPE) ap = MDNSPublishers(f_listen_on.values(), name, service_type=service_type, text_dict=d) from xpra.scripts.server import add_when_ready, add_cleanup add_when_ready(ap.start) add_cleanup(ap.stop)
def setup_vsock_socket(cid, iport): from xpra.log import Logger log = Logger("network") try: from xpra.net.vsock import bind_vsocket #@UnresolvedImport vsock_socket = bind_vsocket(cid=cid, port=iport) except Exception as e: raise InitException("failed to setup vsock socket on %s:%s %s" % (cid, iport, e)) def cleanup_vsock_socket(): log.info("closing vsock socket %s:%s", cid, iport) try: vsock_socket.close() except: pass add_cleanup(cleanup_vsock_socket) return "vsock", vsock_socket, (cid, iport)
def setup_tcp_socket(host, iport, socktype="TCP"): from xpra.log import Logger log = Logger("network") try: tcp_socket = create_tcp_socket(host, iport) except Exception as e: log("create_tcp_socket%s", (host, iport), exc_info=True) raise InitException("failed to setup %s socket on %s:%s %s" % (socktype, host, iport, e)) def cleanup_tcp_socket(): log.info("closing %s socket %s:%s", socktype, host, iport) try: tcp_socket.close() except: pass add_cleanup(cleanup_tcp_socket) return "tcp", tcp_socket, (host, iport)
def setup_udp_socket(host, iport, socktype="udp"): log = get_network_logger() try: udp_socket = create_udp_socket(host, iport) except Exception as e: log("create_udp_socket%s", (host, iport), exc_info=True) raise InitException("failed to setup %s socket on %s:%s %s" % (socktype, host, iport, e)) def cleanup_udp_socket(): log.info("closing %s socket %s:%s", socktype, host, iport) try: udp_socket.close() except: pass add_cleanup(cleanup_udp_socket) log("%s: %s:%s : %s", socktype, host, iport, socket) return socktype, udp_socket, (host, iport)
def write_pidfile(pidfile, uid, gid): from xpra.log import Logger log = Logger("server") pidstr = str(os.getpid()) try: with open(pidfile, "w") as f: os.fchmod(f.fileno(), 0o600) f.write("%s\n" % pidstr) try: inode = os.fstat(f.fileno()).st_ino except: inode = -1 if POSIX and uid != getuid() or gid != getgid(): try: os.fchown(f.fileno(), uid, gid) except: pass log.info("wrote pid %s to '%s'", pidstr, pidfile) def cleanuppidfile(): #verify this is the right file! log("cleanuppidfile: inode=%i", inode) if inode > 0: try: i = os.stat(pidfile).st_ino log("cleanuppidfile: current inode=%i", i) if i != inode: return except: pass try: os.unlink(pidfile) except: pass add_cleanup(cleanuppidfile) except Exception as e: log.error("Error: failed to write pid %i to pidfile '%s':", os.getpid(), pidfile) log.error(" %s", e)
def mdns_publish(display_name, mode, listen_on, text_dict={}): global MDNS_WARNING if MDNS_WARNING is True: return PREFER_PYBONJOUR = envbool("XPRA_PREFER_PYBONJOUR", False) or WIN32 or OSX try: from xpra.net import mdns assert mdns if PREFER_PYBONJOUR: from xpra.net.mdns.pybonjour_publisher import BonjourPublishers as MDNSPublishers, get_interface_index else: from xpra.net.mdns.avahi_publisher import AvahiPublishers as MDNSPublishers, get_interface_index except ImportError as e: MDNS_WARNING = True log = get_network_logger() log("mdns import failure", exc_info=True) log.warn("Warning: failed to load the mdns %s publisher:", ["avahi", "pybonjour"][PREFER_PYBONJOUR]) log.warn(" %s", e) log.warn(" either fix your installation or use the 'mdns=no' option") return d = text_dict.copy() d["mode"] = mode #ensure we don't have duplicate interfaces: f_listen_on = {} for host, port in listen_on: f_listen_on[get_interface_index(host)] = (host, port) try: name = socket.gethostname() except: name = "Xpra" if display_name and not (OSX or WIN32): name += " %s" % display_name if mode != "tcp": name += " (%s)" % mode ap = MDNSPublishers(f_listen_on.values(), name, text_dict=d) from xpra.scripts.server import add_when_ready, add_cleanup add_when_ready(ap.start) add_cleanup(ap.stop)
def setup_tcp_socket(host, iport, socktype="tcp"): log = get_network_logger() try: tcp_socket = create_tcp_socket(host, iport) except Exception as e: log("create_tcp_socket%s", (host, iport), exc_info=True) raise InitException("failed to setup %s socket on %s:%s %s" % (socktype, host, iport, e)) def cleanup_tcp_socket(): log.info("closing %s socket %s:%s", socktype.lower(), host, iport) try: tcp_socket.close() except: pass add_cleanup(cleanup_tcp_socket) if iport == 0: iport = tcp_socket.getsockname()[1] log.info("allocated %s port %i on %s", socktype, iport, host) log("%s: %s:%s : %s", socktype, host, iport, socket) return socktype, tcp_socket, (host, iport)
def add_cleanup(fn): from xpra.scripts.server import add_cleanup add_cleanup(fn)
def add_cleanup(f): from xpra.scripts import server server.add_cleanup(f)