Why does reading from two connected pty’s cause an infinite loop

file-descriptorsterminaltty

I want to fake a gsm modem for testing a program. I want the program to send AT-codes to me and that I can answer it back, kind of a VirtualSerialPort. But for some reason the data written from the program is written back to it directly.

I would have expected that socat created two pipes; one that sends the output from the test program to whatever I've attached on the other side and another pipe that works in the other direction. But the data appears to "loop in one pipe", if you understand what I mean. Here's a picture from TTY demystified to illustrate my point:

Pty handling in kernel

A simplified reproduction script:

I create the pty's with:

(xterm 1)$ socat -d -d -d PTY,link=foo PTY,link=bar
2012/01/18 14:45:01 starting loop with FDs [3,3] and [5,5]

And I listen to both sides:

(xterm 2)$ cat <bar
(xterm 3)$ cat <foo

If I write a few characters…

(xterm 4)$ echo "..." > foo

.. socat logs a lot of traffic that goes forth and back infinitely.

(xterm 1)
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 3 to 5
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 3 to 5
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3
2012/01/18 14:25:58 socat[7667] I transferred 4095 bytes from 5 to 3

There's nothing outputted from either of the cat commands.

EDIT: @wnoise explained that socat creates two PTY masters. Here's an image to illustrate that:

Two PTY masters connected by socat

Best Answer

The syntax you've used creates two pty masters and connects them together bidirectionally. You're getting constant echos because you didn't add "echo=0" to the options. It's probably also necessary to add raw for your use case.

Related Question