Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/platform/displayfd.py : 11%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# This file is part of Xpra.
2# Copyright (C) 2017-2020 Antoine Martin <antoine@xpra.org>
3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
4# later version. See the file COPYING for details.
6import os
8from xpra.log import Logger
9from xpra.os_util import monotonic_time, POSIX
10from xpra.util import envint
12DISPLAY_FD_TIMEOUT = envint("XPRA_DISPLAY_FD_TIMEOUT", 20)
15def write_displayfd(w_pipe, display, timeout=10):
16 import select #@UnresolvedImport
17 import errno
18 buf = ("%s\n" % display).encode("ascii")
19 limit = monotonic_time()+timeout
20 log = Logger("util")
21 log("write_displayfd%s", (w_pipe, display, timeout))
22 while buf and monotonic_time()<limit:
23 try:
24 timeout = max(0, limit-monotonic_time())
25 if POSIX:
26 w = select.select([], [w_pipe], [], timeout)[1]
27 log("select.select(..) writeable=%s", w)
28 else:
29 w = [w_pipe]
30 if w_pipe in w:
31 count = os.write(w_pipe, buf)
32 buf = buf[count:]
33 log("wrote %i bytes, remains %s", count, buf)
34 except (select.error, OSError) as e:
35 if e.errno!=errno.EINTR:
36 raise
37 if not buf:
38 try:
39 os.fsync(w_pipe)
40 except OSError:
41 log("os.fsync(%i)", w_pipe, exc_info=True)
42 if w_pipe>2:
43 try:
44 os.close(w_pipe)
45 except OSError:
46 log("os.close(%i)", w_pipe, exc_info=True)
47 return len(buf)==0
49def read_displayfd(r_pipe, timeout=DISPLAY_FD_TIMEOUT, proc=None):
50 import select #@UnresolvedImport
51 import errno
52 # Read the display number from the pipe we gave to Xvfb
53 # waiting up to 10 seconds for it to show up
54 limit = monotonic_time()+timeout
55 buf = b""
56 log = Logger("util")
57 log("read_displayfd%s", (r_pipe, timeout, proc))
58 while monotonic_time()<limit and len(buf)<8 and (proc is None or proc.poll() is None):
59 try:
60 timeout = max(0, limit-monotonic_time())
61 if POSIX:
62 r = select.select([r_pipe], [], [], timeout)[0]
63 log("readable=%s", r)
64 else:
65 r = [r_pipe]
66 if r_pipe in r:
67 v = os.read(r_pipe, 8)
68 buf += v
69 log("read=%s", v)
70 if buf and (buf.endswith(b'\n') or len(buf)>=8):
71 break
72 except (select.error, OSError) as e:
73 if e.errno!=errno.EINTR:
74 raise
75 return buf
77def parse_displayfd(buf, err):
78 if not buf:
79 err("did not provide a display number using displayfd")
80 return None
81 if not buf.endswith(b"\n"):
82 err("output not terminated by newline: '%s'" % buf)
83 return None
84 buf = buf.rstrip(b"\n\r")
85 try:
86 n = int(buf)
87 except ValueError:
88 err("display number is not a valid number: %s" % buf)
89 if n<0 or n>=2**16:
90 err("provided an invalid display number: %s" % n)
91 return n