How do terminal size changes get sent to command line applications though ssh or telnet?

fadedbee

How do terminal size changes get sent to command line applications through ssh or telnet?

  1. A user connects to a remote machine with ssh or telnet.

  2. They start editing a document in VIM.

  3. Then they resize their terminal window.

The terminal must tell ssh/telnet that the window size had changed. How does this happen?

ssh/telnet then uses its own particular method to send that change to sshd/telnetd. What are these methods?

sshd/telnetd then tells the application that the terminal size has changed. How is this done. Is it the same method as from the terminal to ssh/telnet?

cnicutar

This is the messy world of pseudo terminals.

Locally, when you resize your terminal your foreground process group gets a SIGWINCH and you can use ioctl to fetch the new size. But what does this have to do with the remote vim process ?

The subject is quite complicated but the gist of it is that the remove server (sshd) does this:

  1. Opens a master psedoterminal device using posix_openpt (or openpty)
  2. Forks a new child (typically this is bound to become a shell)
  3. Severs its terminal connection using setsid()
  4. Opens the terminal device (created in step 1) which becomes its controlling terminal
  5. Replaces the standard descriptors (STDIN_FILENO and friends) with the fd from step 4

At this point anything the server process writes to the master side ends up as input to the slave side BUT with a terminal line discipline so the kernel does a bit of magic - like sending signals - when writing certain combinations, and you can also issue ioctl calls with useful effects.


The best way to think about this is to explore the openssh suite.

  • The client monitors for SIGWINCH - see clientloop.c and sets received_window_change_signal = 1 when it receives it

  • The function client_check_window_change checks that flag and tells the server:

    packet_start(SSH_CMSG_WINDOW_SIZE);
    packet_put_int((u_int)ws.ws_row);
    ...
    

So now the server should receive a packet which specifies a (potentially new) size.

  • The server calls pty_change_window_size with the received sizes which does the real magic:

    struct winsize w;
    w.ws_row = row;
    ...
    (void) ioctl(ptyfd, TIOCSWINSZ, &w); /* This is it! */
    

This sets the new window size of the slave. If the new size differs from the old, the kernel sends a SIGWINCH to the foreground process group associated with that pty. Thus vim also gets that signal and can update its idea of the terminal size.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get a list of running applications by using the command line?

From Dev

How do ssh remote command line arguments get parsed

From Dev

How do I get the size of a directory on the command line?

From Dev

How to get a list of applications associated with a file using command line

From Dev

How do I change the command prompt color of a MobaXTerm SSH terminal?

From Dev

Run a GET command via telnet in Terminal.app

From Dev

How do I detach from a controlling terminal from the command line?

From Dev

How do I paste to terminal executable content by command line?

From Dev

How do I install NetDrive from a Linux command line (terminal)?

From Dev

How to get the hostname using terminal command-line?

From Dev

How to get the hostname using terminal command-line?

From Dev

How are terminal length and width forwarded over SSH and telnet?

From Dev

How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

From Dev

How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

From Dev

how do I sent escape character ^] through a spawned telnet session?

From Dev

How to do file transfer inside ssh command line?

From Dev

how to set a specific position though command line?

From Dev

Prevent the terminal changing size for a program that understands terminal size changes over SSH

From Dev

How do you get nth argument of a previous command at command line?

From Dev

Command history in terminal applications

From Dev

linux - command line to get version of terminal

From Dev

Calling telnet from an ssh command

From Dev

How to get terminal size or font size in pixels?

From Dev

How to get terminal size or font size in pixels?

From Dev

How to insert a command line to GUI applications

From Dev

How are terminal information (such as window size) sent to a Linux program?

From Dev

How do I get the total size of everything in a directory in one line?

From Dev

Open a terminal with font size option through command line

From Dev

Open a terminal with font size option through command line

Related Related

  1. 1

    How do I get a list of running applications by using the command line?

  2. 2

    How do ssh remote command line arguments get parsed

  3. 3

    How do I get the size of a directory on the command line?

  4. 4

    How to get a list of applications associated with a file using command line

  5. 5

    How do I change the command prompt color of a MobaXTerm SSH terminal?

  6. 6

    Run a GET command via telnet in Terminal.app

  7. 7

    How do I detach from a controlling terminal from the command line?

  8. 8

    How do I paste to terminal executable content by command line?

  9. 9

    How do I install NetDrive from a Linux command line (terminal)?

  10. 10

    How to get the hostname using terminal command-line?

  11. 11

    How to get the hostname using terminal command-line?

  12. 12

    How are terminal length and width forwarded over SSH and telnet?

  13. 13

    How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

  14. 14

    How to use a command-line *nix command that needs an interactive terminal over JSch SSH session in Java?

  15. 15

    how do I sent escape character ^] through a spawned telnet session?

  16. 16

    How to do file transfer inside ssh command line?

  17. 17

    how to set a specific position though command line?

  18. 18

    Prevent the terminal changing size for a program that understands terminal size changes over SSH

  19. 19

    How do you get nth argument of a previous command at command line?

  20. 20

    Command history in terminal applications

  21. 21

    linux - command line to get version of terminal

  22. 22

    Calling telnet from an ssh command

  23. 23

    How to get terminal size or font size in pixels?

  24. 24

    How to get terminal size or font size in pixels?

  25. 25

    How to insert a command line to GUI applications

  26. 26

    How are terminal information (such as window size) sent to a Linux program?

  27. 27

    How do I get the total size of everything in a directory in one line?

  28. 28

    Open a terminal with font size option through command line

  29. 29

    Open a terminal with font size option through command line

HotTag

Archive