Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send SSH window change message on SIGWINCH when pty=True #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions fabric/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import re
import six
import subprocess
import signal
import sys
import time
from glob import glob
Expand Down Expand Up @@ -764,9 +765,17 @@ def _execute(channel, command, pty=True, combine_stderr=None,
# Request pty with size params (default to 80x24, obtain real
# parameters if on POSIX platform)
if using_pty:
sigwinch_orig = None
rows, cols = _pty_size()
channel.get_pty(width=cols, height=rows)

def handle_window_change(signum, frame):
rows, cols = _pty_size()
channel.resize_pty(width=cols, height=rows)

if hasattr(signal, "SIGWINCH"):
sigwinch_orig = signal.signal(signal.SIGWINCH, handle_window_change)

# Use SSH agent forwarding from 'ssh' if enabled by user
config_agent = ssh_config().get('forwardagent', 'no').lower() == 'yes'
forward = None
Expand Down Expand Up @@ -796,21 +805,26 @@ def _execute(channel, command, pty=True, combine_stderr=None,
ThreadHandler('in', input_loop, channel, stdin, using_pty)
)

while True:
if channel.exit_status_ready():
break
else:
# Check for thread exceptions here so we can raise ASAP
# (without chance of getting blocked by, or hidden by an
# exception within, recv_exit_status())
for worker in workers:
worker.raise_if_needed()
try:
time.sleep(ssh.io_sleep)
except KeyboardInterrupt:
if not remote_interrupt:
raise
channel.send('\x03')
try:
while True:
if channel.exit_status_ready():
break
else:
# Check for thread exceptions here so we can raise ASAP
# (without chance of getting blocked by, or hidden by an
# exception within, recv_exit_status())
for worker in workers:
worker.raise_if_needed()
try:
time.sleep(ssh.io_sleep)
except KeyboardInterrupt:
if not remote_interrupt:
raise
channel.send('\x03')
finally:
if using_pty:
if sigwinch_orig is not None:
signal.signal(signal.SIGWINCH, sigwinch_orig)

# Obtain exit code of remote program now that we're done.
status = channel.recv_exit_status()
Expand Down
13 changes: 6 additions & 7 deletions fabric/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,15 @@ def _pty_size():
import fcntl
import termios
# We want two short unsigned integers (rows, cols)
fmt = 'HH'
# unsigned short (row, col, xpixel, ypixel) (pixel values unused)
fmt = 'HHHH'
# Create an empty (zeroed) buffer for ioctl to map onto. Yay for C!
buffer = struct.pack(fmt, 0, 0)
# Call TIOCGWINSZ to get window size of stdout, returns our filled
# buffer
buffer = struct.pack(fmt, 0, 0, 0, 0)
# Call TIOCGWINSZ to get window size of stdout, returns our filled buffer
try:
result = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ,
buffer)
result = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, buffer)
# Unpack buffer back into Python data types
rows, cols = struct.unpack(fmt, result)
rows, cols, _, _ = struct.unpack(fmt, result)
# Fall back to defaults if TIOCGWINSZ returns unreasonable values
if rows == 0:
rows = default_rows
Expand Down