xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #963: cleanup-real-socket-path.patch

File cleanup-real-socket-path.patch, 3.0 KB (added by Antoine Martin, 6 years ago)

attempt at figuring out the real socket path from the socket file descriptor - does not work..

  • xpra/scripts/server.py

     
    3131
    3232_cleanups = []
    3333def run_cleanups():
     34    print("run_cleanups")
    3435    global _cleanups
    3536    cleanups = _cleanups
    3637    _cleanups = []
    3738    for c in cleanups:
     39        print("run_cleanups calling %s" % c)
    3840        try:
    3941            c()
    4042        except:
     
    231233        listener.bind(sockpath)
    232234    finally:
    233235        os.umask(orig_umask)
    234     def cleanup_socket():
     236    def cleanup_socket(only_if_safe=False):
    235237        from xpra.log import Logger
    236         Logger("network").info("removing socket %s", sockpath)
     238        log = Logger("network")
     239        real_path = None
     240        proc_fd_dir = "/proc/%i/fd" % os.getpid()
     241        log.info("cleanup_socket(%s) proc fd dir=%s", only_if_safe, proc_fd_dir)
     242        if os.name=="posix" and os.path.exists(proc_fd_dir) and os.path.isdir(proc_fd_dir):
     243            try:
     244                fd_link = os.path.join(proc_fd_dir, "%i" % listener.fileno())
     245                log.info("fd link=%s", fd_link)
     246                real_path = os.readlink(fd_link)
     247                log.info("real path=%s", real_path)
     248            except:
     249                log.error("Error looking up the real socket path for %s", sockpath)
     250        if only_if_safe and not real_path:
     251            log.info("not removing unsafe socket %s", sockpath)
     252            return
     253        delpath = real_path or sockpath
     254        log.info("removing socket %s", delpath)
    237255        try:
    238             os.unlink(sockpath)
    239         except:
    240             pass
     256            os.unlink(delpath)
     257        except Exception as e:
     258            log("Error removing socket '%s': %s", delpath, e)
    241259    return listener, cleanup_socket
    242260
    243261def create_tcp_socket(host, iport):
     
    10691087        if e==ServerCore.EXITING_CODE:
    10701088            log.info("exiting: not cleaning up Xvfb")
    10711089        elif local_sockets:
    1072             # don't delete the new sockets (probably not ours!)
    1073             # FIXME: keep track of which server (new or old) owns which socket,
    1074             # so we can delete the ones that have not been replaced
     1090            # don't delete the new sockets! (only_if_safe=True)
    10751091            for socket, cleanup_socket in local_sockets:
    10761092                if cleanup_socket in _cleanups:
    1077                     log("removing cleanup: %s for %s", cleanup_socket, socket)
    10781093                    _cleanups.remove(cleanup_socket)
     1094                    _cleanups.append(cleanup_socket_ifsafe(cleanup_socket))
     1095                    log.info("replaced socket with ifsafe version for %s", socket)
    10791096            log.info("upgrading: not cleaning up Xvfb or socket")
    1080         log("cleanups=%s", _cleanups)
     1097        log.info("cleanups=%s", _cleanups)
    10811098        e = 0
     1099    log.info("run_server()=%s", e)
    10821100    return e
     1101
     1102def cleanup_socket_ifsafe(cleanup_socket):
     1103    def call_safe_cleanup():
     1104        return cleanup_socket(only_if_safe=True)
     1105    return call_safe_cleanup