def create_control_socket(self):
     dotxpra = DotXpra(self.socket_dir)
     name = "proxy-%s" % os.getpid()
     sockpath = dotxpra.norm_make_path(name, dotxpra.sockdir())
     state = dotxpra.get_server_state(sockpath)
     if state in (DotXpra.LIVE, DotXpra.UNKNOWN):
         log.warn("You already have a proxy server running at %s, the control socket will not be created!", sockpath)
         return False
     try:
         sock = create_unix_domain_socket(sockpath, None)
         sock.listen(5)
     except Exception, e:
         log.warn("failed to setup control socket %s: %s", sockpath, e)
         return False
 def create_control_socket(self):
     dotxpra = DotXpra(self.socket_dir)
     name = "proxy-%s" % os.getpid()
     sockpath = dotxpra.norm_make_path(name, dotxpra.sockdir())
     state = dotxpra.get_server_state(sockpath)
     if state in (DotXpra.LIVE, DotXpra.UNKNOWN):
         log.warn(
             "You already have a proxy server running at %s, the control socket will not be created!",
             sockpath)
         return False
     try:
         sock = create_unix_domain_socket(sockpath, None)
         sock.listen(5)
     except Exception, e:
         log.warn("failed to setup control socket %s: %s", sockpath, e)
         return False
 def create_control_socket(self):
     dotxpra = DotXpra(self.socket_dir)
     name = "proxy-%s" % os.getpid()
     sockpath = dotxpra.norm_make_path(name, dotxpra.sockdir())
     state = dotxpra.get_server_state(sockpath)
     if state in (DotXpra.LIVE, DotXpra.UNKNOWN):
         log.warn("You already have a proxy server running at %s, the control socket will not be created!", sockpath)
         return False
     try:
         sock = create_unix_domain_socket(sockpath, None, 0o600)
         sock.listen(5)
     except:
         log.warn("failed to setup control socket %s", sockpath, exc_info=True)
         return False
     self.control_socket = sock
     self.control_socket_path = sockpath
     log.info("proxy instance now also available using unix domain socket: %s", self.control_socket_path)
     return True
Example #4
0
 def create_control_socket(self):
     dotxpra = DotXpra(self.socket_dir)
     name = "proxy-%s" % os.getpid()
     sockpath = dotxpra.norm_make_path(name, dotxpra.sockdir())
     state = dotxpra.get_server_state(sockpath)
     if state in (DotXpra.LIVE, DotXpra.UNKNOWN):
         log.warn(
             "You already have a proxy server running at %s, the control socket will not be created!",
             sockpath)
         return False
     try:
         sock = create_unix_domain_socket(sockpath, None, 0o600)
         sock.listen(5)
     except:
         log.warn("failed to setup control socket %s",
                  sockpath,
                  exc_info=True)
         return False
     self.control_socket = sock
     self.control_socket_path = sockpath
     log.info(
         "proxy instance now also available using unix domain socket: %s",
         self.control_socket_path)
     return True
Example #5
0
def run_server(parser, opts, mode, xpra_file, extra_args):
    if len(extra_args) != 1:
        parser.error("need exactly 1 extra argument")
    assert mode in ("start", "upgrade", "shadow")
    upgrading = mode == "upgrade"
    shadowing = mode == "shadow"
    display_name = extra_args.pop(0)
    if display_name.startswith(":") and not shadowing:
        n = display_name[1:]
        p = n.find(".")
        if p>0:
            n = n[:p]
        try:
            dno = int(n)
            if dno>=0 and dno<10:
                sys.stderr.write("WARNING: low display number: %s\n" % dno)
                sys.stderr.write("You are attempting to run the xpra server against what seems to be a default X11 display '%s'.\n" % display_name)
                sys.stderr.write("This is generally not what you want.\n")
                sys.stderr.write("You should probably use a higher display number just to avoid any confusion (and also this warning message).\n")
        except:
            pass

    if not shadowing and opts.exit_with_children and not opts.start_child:
        sys.stderr.write("--exit-with-children specified without any children to spawn; exiting immediately")
        return  1

    atexit.register(run_cleanups)
    signal.signal(signal.SIGINT, deadly_signal)
    signal.signal(signal.SIGTERM, deadly_signal)

    dotxpra = DotXpra(opts.socket_dir)

    # This used to be given a display-specific name, but now we give it a
    # single fixed name and if multiple servers are started then the last one
    # will clobber the rest.  This isn't great, but the tradeoff is that it
    # makes it possible to use bare 'ssh:hostname' display names and
    # autodiscover the proper numeric display name when only one xpra server
    # is running on the remote host.  Might need to revisit this later if
    # people run into problems or autodiscovery turns out to be less useful
    # than expected.
    scriptpath = os.path.join(dotxpra.confdir(), "run-xpra")

    # Save the starting dir now, because we'll lose track of it when we
    # daemonize:
    starting_dir = os.getcwd()

    # Daemonize:
    if opts.daemon:
        if opts.log_file:
            if os.path.isabs(opts.log_file):
                logpath = opts.log_file
            else:
                logpath = os.path.join(dotxpra.sockdir(), opts.log_file)
            logpath = logpath.replace("$DISPLAY", display_name)
        else:
            logpath = dotxpra.log_path(display_name) + ".log"
        sys.stderr.write("Entering daemon mode; "
                         + "any further errors will be reported to:\n"
                         + ("  %s\n" % logpath))
        # Do some work up front, so any errors don't get lost.
        if os.path.exists(logpath):
            os.rename(logpath, logpath + ".old")
        logfd = os.open(logpath, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, o0666)
        assert logfd > 2
        os.chdir("/")

        if os.fork():
            os._exit(0)
        os.setsid()
        if os.fork():
            os._exit(0)
        close_all_fds(exceptions=[logfd])
        fd0 = os.open("/dev/null", os.O_RDONLY)
        if fd0 != 0:
            os.dup2(fd0, 0)
            os.close(fd0)
        os.dup2(logfd, 1)
        os.dup2(logfd, 2)
        os.close(logfd)
        # Make these line-buffered:
        sys.stdout = os.fdopen(1, "w", 1)
        sys.stderr = os.fdopen(2, "w", 1)

    # Write out a shell-script so that we can start our proxy in a clean
    # environment:
    scriptfile = open(scriptpath, "w")
    # Unix is a little silly sometimes:
    umask = os.umask(0)
    os.umask(umask)
    if hasattr(os, "fchmod"):
        os.fchmod(scriptfile.fileno(), o0700 & ~umask)
    else:
        os.chmod(scriptpath, o0700 & ~umask)
    scriptfile.write(xpra_runner_shell_script(xpra_file, starting_dir, opts.socket_dir))
    scriptfile.close()

    from wimpiggy.log import Logger
    log = Logger()

    # Initialize the sockets before the display,
    # That way, errors won't make us kill the Xvfb
    # (which may not be ours to kill at that point)
    sockets = []
    if opts.bind_tcp and len(opts.bind_tcp):
        def setup_tcp_socket(bind_to):
            try:
                tcp_socket = create_tcp_socket(parser, bind_to)
                sockets.append(tcp_socket)
                def cleanup_tcp_socket():
                    log.info("closing tcp socket %s", bind_to)
                    try:
                        tcp_socket.close()
                    except:
                        pass
                _cleanups.append(cleanup_tcp_socket)
            except Exception, e:
                log.error("cannot start - failed to create tcp socket at %s : %s" % (bind_to, e))
                return  1
        for tcp_s in set(opts.bind_tcp):
            setup_tcp_socket(tcp_s)
Example #6
0
def run_server(parser, opts, mode, xpra_file, extra_args):
    if len(extra_args) != 1:
        parser.error("need exactly 1 extra argument")
    if opts.encoding and opts.encoding=="help":
        from xpra.scripts.config import encodings_help
        from xpra.server.server_base import SERVER_ENCODINGS
        print("server supports the following encodings:\n * %s" % ("\n * ".join(encodings_help(SERVER_ENCODINGS))))
        return 0
    assert mode in ("start", "upgrade", "shadow")
    upgrading = mode == "upgrade"
    shadowing = mode == "shadow"
    display_name = extra_args.pop(0)
    if display_name.startswith(":") and not shadowing:
        n = display_name[1:]
        p = n.find(".")
        if p>0:
            n = n[:p]
        try:
            dno = int(n)
            if dno>=0 and dno<10:
                sys.stderr.write("WARNING: low display number: %s\n" % dno)
                sys.stderr.write("You are attempting to run the xpra server against what seems to be a default X11 display '%s'.\n" % display_name)
                sys.stderr.write("This is generally not what you want.\n")
                sys.stderr.write("You should probably use a higher display number just to avoid any confusion (and also this warning message).\n")
        except:
            pass

    if not shadowing and opts.exit_with_children and not opts.start_child:
        sys.stderr.write("--exit-with-children specified without any children to spawn; exiting immediately")
        return  1

    atexit.register(run_cleanups)
    signal.signal(signal.SIGINT, deadly_signal)
    signal.signal(signal.SIGTERM, deadly_signal)

    dotxpra = DotXpra(opts.socket_dir)

    # This used to be given a display-specific name, but now we give it a
    # single fixed name and if multiple servers are started then the last one
    # will clobber the rest.  This isn't great, but the tradeoff is that it
    # makes it possible to use bare 'ssh:hostname' display names and
    # autodiscover the proper numeric display name when only one xpra server
    # is running on the remote host.  Might need to revisit this later if
    # people run into problems or autodiscovery turns out to be less useful
    # than expected.
    scriptpath = os.path.join(dotxpra.confdir(), "run-xpra")

    # Save the starting dir now, because we'll lose track of it when we
    # daemonize:
    starting_dir = os.getcwd()

    # Daemonize:
    if opts.daemon:
        if opts.log_file:
            if os.path.isabs(opts.log_file):
                logpath = opts.log_file
            else:
                logpath = os.path.join(dotxpra.sockdir(), opts.log_file)
            logpath = logpath.replace("$DISPLAY", display_name)
        else:
            logpath = dotxpra.log_path(display_name) + ".log"
        sys.stderr.write("Entering daemon mode; "
                         + "any further errors will be reported to:\n"
                         + ("  %s\n" % logpath))
        # Do some work up front, so any errors don't get lost.
        if os.path.exists(logpath):
            os.rename(logpath, logpath + ".old")
        logfd = os.open(logpath, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, o0666)
        assert logfd > 2
        os.chdir("/")

        if os.fork():
            os._exit(0)
        os.setsid()
        if os.fork():
            os._exit(0)
        close_all_fds(exceptions=[logfd])
        fd0 = os.open("/dev/null", os.O_RDONLY)
        if fd0 != 0:
            os.dup2(fd0, 0)
            os.close(fd0)
        os.dup2(logfd, 1)
        os.dup2(logfd, 2)
        os.close(logfd)
        # Make these line-buffered:
        sys.stdout = os.fdopen(1, "w", 1)
        sys.stderr = os.fdopen(2, "w", 1)

    # Write out a shell-script so that we can start our proxy in a clean
    # environment:
    scriptfile = open(scriptpath, "w")
    # Unix is a little silly sometimes:
    umask = os.umask(0)
    os.umask(umask)
    if hasattr(os, "fchmod"):
        os.fchmod(scriptfile.fileno(), o0700 & ~umask)
    else:
        os.chmod(scriptpath, o0700 & ~umask)
    scriptfile.write(xpra_runner_shell_script(xpra_file, starting_dir, opts.socket_dir))
    scriptfile.close()

    from xpra.log import Logger
    log = Logger()

    # Initialize the sockets before the display,
    # That way, errors won't make us kill the Xvfb
    # (which may not be ours to kill at that point)
    sockets = []
    if opts.bind_tcp and len(opts.bind_tcp):
        def setup_tcp_socket(bind_to):
            try:
                tcp_socket = create_tcp_socket(parser, bind_to)
                sockets.append(("tcp", tcp_socket))
                def cleanup_tcp_socket():
                    log.info("closing tcp socket %s", bind_to)
                    try:
                        tcp_socket.close()
                    except:
                        pass
                _cleanups.append(cleanup_tcp_socket)
            except Exception, e:
                log.error("cannot start - failed to create tcp socket at %s : %s" % (bind_to, e))
                return  1
        for tcp_s in set(opts.bind_tcp):
            setup_tcp_socket(tcp_s)