summaryrefslogtreecommitdiff
path: root/contrib/python/podman/libs/_containers_attach.py
diff options
context:
space:
mode:
authorJhon Honce <jhonce@redhat.com>2018-06-20 19:14:27 -0700
committerAtomic Bot <atomic-devel@projectatomic.io>2018-06-22 17:25:44 +0000
commit2f0f9944b610773d2d547c59cc7d936665b2bbdc (patch)
treee30e0625d099bf0297ce70c88bf48e2955a13494 /contrib/python/podman/libs/_containers_attach.py
parent3092d2084761755776994a8febe2279d07e3d03b (diff)
downloadpodman-2f0f9944b610773d2d547c59cc7d936665b2bbdc.tar.gz
podman-2f0f9944b610773d2d547c59cc7d936665b2bbdc.tar.bz2
podman-2f0f9944b610773d2d547c59cc7d936665b2bbdc.zip
Add unittests and fix bugs
* Improved error messages * Improved checking of user input Signed-off-by: Jhon Honce <jhonce@redhat.com> Closes: #978 Approved by: mheon
Diffstat (limited to 'contrib/python/podman/libs/_containers_attach.py')
-rw-r--r--contrib/python/podman/libs/_containers_attach.py46
1 files changed, 24 insertions, 22 deletions
diff --git a/contrib/python/podman/libs/_containers_attach.py b/contrib/python/podman/libs/_containers_attach.py
index 54e6a009e..bd73542b9 100644
--- a/contrib/python/podman/libs/_containers_attach.py
+++ b/contrib/python/podman/libs/_containers_attach.py
@@ -21,6 +21,9 @@ class Mixin:
stderr is ignored.
"""
+ if not self.containerrunning:
+ raise Exception('you can only attach to running containers')
+
if stdin is None:
stdin = sys.stdin.fileno()
@@ -48,7 +51,7 @@ class Mixin:
packed = fcntl.ioctl(stdout, termios.TIOCGWINSZ,
struct.pack('HHHH', 0, 0, 0, 0))
rows, cols, _, _ = struct.unpack('HHHH', packed)
-
+ # TODO: Need some kind of timeout in case pipe is blocked
with open(ctl_socket, 'w') as skt:
# send conmon window resize message
skt.write('1 {} {}\n'.format(rows, cols))
@@ -73,38 +76,37 @@ class Mixin:
# catch any resizing events and send the resize info
# to the control fifo "socket"
signal.signal(signal.SIGWINCH, resize_handler)
+
except termios.error:
original_attr = None
try:
- # Prepare socket for communicating with conmon/container
+ # TODO: socket.SOCK_SEQPACKET may not be supported in Windows
with socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET) as skt:
+ # Prepare socket for communicating with conmon/container
skt.connect(io_socket)
skt.sendall(b'\n')
sources = [skt, stdin]
while sources:
readable, _, _ = select.select(sources, [], [])
- for r in readable:
- if r is skt:
- data = r.recv(CONMON_BUFSZ)
- if not data:
- sources.remove(skt)
-
- # Remove source marker when writing
- os.write(stdout, data[1:])
- elif r is stdin:
- data = os.read(stdin, CONMON_BUFSZ)
- if not data:
- sources.remove(stdin)
-
- skt.sendall(data)
-
- if eot in data:
- sources.clear()
- break
- else:
- raise ValueError('Unknown source in select')
+ if skt in readable:
+ data = skt.recv(CONMON_BUFSZ)
+ if not data:
+ sources.remove(skt)
+
+ # Remove source marker when writing
+ os.write(stdout, data[1:])
+
+ if stdin in readable:
+ data = os.read(stdin, CONMON_BUFSZ)
+ if not data:
+ sources.remove(stdin)
+
+ skt.sendall(data)
+
+ if eot in data:
+ sources.clear()
finally:
if original_attr:
termios.tcsetattr(stdout, termios.TCSADRAIN, original_attr)