2

I'm confused from the actual meaning of the echoing feature implemented in Linux TTY driver.

As far as I know, a pseudo-terminal pair (PTY pair) consists of a PTY master, a PTY slave and a line discipline all implemented in kernel code (i.e. kernel code running in kernel space).

From my understanding, echoing is the feature of sending back what is written on the master side ptm so that the process opening such a fd (by opening the PTY multiplexer /dev/ptmx device file) receives back what it has written to.

My questions:

  • is this echo feature implemented within the line discipline kernel code ?
  • does this echo feature apply only to the PTY master side, i.e. it doesn't send back what is written to the PTY slave side /dev/pts/x ?

Thanks.

1
  • This blog post will probably help illuminate a lot of aspects of the line disciplines. Commented Jun 21 at 15:56

1 Answer 1

6

From my understanding, echoing is the feature of sending back what is written on the master side ptm so that the process opening such a fd (by opening the PTY multiplexer /dev/ptmx device file) receives back what it has written to.

No, the master side in a pseudo-terminal emulates the serial line/wire between the terminal and the computer in the case of a real terminal.

Echoing is the kernel sending back what you type to the terminal for it to display it. You press the a key, the terminal sends an a character over the wire, the kernel (the tty discipline part) sends an a back to the terminal which the terminal renders as an a glyph for you to see what you have typed.

An application can then read() from the tty device file and will get an a (provided Return has been pressed later and a backspace character had not been received in the interval if the line discipline is in canonical mode), regardless of whether or not echo is enabled in the tty discipline.

For a pseudo-terminal, it's the same thing, except that instead of sending an a character over the wire, the terminal emulator write()s it to the master side and instead of receiving the echo back on the wire, it reads it back from the master side.

The echo is only between terminal and kernel, the application interacting with the tty device file (slave side in the case of a pseudo-terminal) is not involved.

If you do a strace cat (or equivalent such as truss, tusc... if not on Linux) for instance, you'll only see read() and write() system calls happening when you press Return, you won't see anything done by cat for the echo of what you type.

Now beware most interactive applications including editors or shell line editors (such as the GNU readline line editor used by bash (the GNU shell) and many other GNU (and some non-GNU) tools), do their own echo. They disable the tty line discipline echo and builtin line editor (canonical mode) so that when the terminal sends a character it's immediately available for reading by the line editor software, which sends it back for echo for the terminal if it's a graphical character (and could do many different things if it's not, like a ^A in emacs-like editors moving the cursor to the start of the editing buffer which would require sending cursor movement escape sequences to the terminal).

readline and ksh93's line editors (as opposed to those of zsh, mksh, yash, fish or tcsh for instance) however have that particularity that they disable their own echo if the tty line discipline's echo is itself disabled (was upon entry of the editor).

If you do a strace bash --norc, you'll see one (or more for some function/motion keys) read() for each key you press, and corresponding write()s for the echo of what you type (unless echo is disabled as seen above). Pressing Ctrl+a (upon which the terminal sends a ^A character) after you have entered 5 characters in editing buffer will cause a write(2, "\10\10\10\10\10"...) which sends 5 BS characters to move the cursor back by as much.

The tty line discipline's own line editor does not support ^A, but it does support a erase (to delete previous character, bound to ^H or ^?), werase (to delete the previous word, generally bound to ^W) or kill (to delete the full editing buffer), and those also generate the echo necessary to erase the corresponding character(s) on the terminal screen (like BS, SPC, BS for a erase received whilst the line editor buffer ended in a single-width character).

20
  • 2
    @CarloC, yes echo is enabled by default outside of your shell's line editor. When a command (whether stty or any other command) runs, you're no longer at the prompt of your shell, readline (in the case of bash) is no longer active editing a command line, so stty -a shows the tty settings outside of readline. To see the settings whilst readline is running, you can do (sleep 1; stty -a) & which will run stty -a 1 second after that command returns at a time when readline is back in action editing the next command line. Commented Jun 21 at 13:41
  • 2
    You'll then see the output include -icanon and -echo, but still isig for instance (which stty -cooked would disable) as tty input signal handling is still done when readline is active (Ctrl-C for instance does cause a signal to be delivered which the shell handles). Commented Jun 21 at 13:43
  • 1
    @CarloC: Yes, that's how Stephane explained it. When bash runs a command in the foreground, it puts the TTY into canonical mode ("cooked"), and back to raw mode when the command exits (or is suspended with ^Z). But running in the background, I'm not sure it would even bother to change the TTY mode at all, leaving it raw. Yeah, stty -a & prints -icanon (not canonical mode, no line editing), so you don't need the sleep or the subshell. Except to avoid racing with the parent shell printing a prompt and making the output messy. Commented Jun 22 at 9:24
  • 1
    @PeterCordes for the latter issue you pointed out, one can simply redirect > the stdout of the process running stty -a & on a file. Commented Jun 22 at 9:32
  • 1
    @PeterCordes, readline upon exit configures the tty back to the mode if was in upon entering (before it changed to the mode it needs to edit the command line), that's not necessarily a cooked mode. If you run stty raw, and then stty -a at the next prompt, you'll see it's still in raw mode. Commented Jun 22 at 10:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.