1

enter image description here

Can sombody explain why this happening and how to fix it so that the data sent to stdin behaves the same way as the data typed in to stdin.

For those who can't see the .gif

Basically i have 3 terminals setup. One is running a netcat server which is the following command.

nc -l 127.0.0.1 4000

Terminal 2 is just running a netcat client which is the following command.

nc 127.0.0.1 4000

When typing into the client which basically means inputting data to stdin or file descriptor 0 of that program. The data shows up on the server once the enter key has been hit. This is expected behavior.

One would expect that if you input data to stdin from another source than the keyboard it would work the same as long as you provide a newline at the end or even a newline and a carriage return.

Though this is not the case when running terminal 3 which has the following command.

echo "test\n" > /proc/$pid/fd/0

Oddly enough the data from echo even shows up in terminal 2 but it does not get treated as keyboard input and thus no message is sent from the clinet on terminal 2 to the server on terminal 1.

This i conclude is bullshit.

  • Related: Send commands to another terminal – Mark Plotnick Aug 14 '18 at 23:38
  • For anyone unable to view or interpret the animated gif, could you please use a few words to describe what you're doing? (How you set it up what you ran, what you expected, and what you got). Thank you! – Jeff Schaller Aug 15 '18 at 1:02
  • @JeffSchaller Done – fullnitrous Aug 15 '18 at 1:19
4

If you look at the /proc/$PID/fd/0 link, you'll see it points to the terminal:

# ls -l /proc/11962/fd/0
lrwx------ 1 foo users 64 Aug 15 04:30 /proc/11962/fd/0 -> /dev/pts/15

When you output to it, you're not passing input to the nc process, you're outputting to the terminal. Which duly prints what you output.

It looks like this:

   /proc/$PID/fd/$N   <-------> [            ]
                                [  terminal  ]
[  nc  ] <--------------------> [            ]

Not like this:

           /proc/$PID/fd/$N  
               |       |
[      ] <-----+       +------> [            ]
[  nc  ] <--------------------> [  terminal  ]

If file descriptor 0 of the process was opened to a file, would you still expect a write to /proc/$PID/fd/0 to show as input to the process, or to go the opened file?

lrwx------ 1 foo users 64 Aug 15 04:36 /proc/11994/fd/0 -> /tmp/testfile

What should echo foo > /proc/11994/fd/0 do in this case?

It's the same here. When the process reads from the fd, it reads data from the file. But when you reopen the same file for writing through /proc/$PID/fd/$N, you write to the file.

You'll need to use the TIOCSTI ioctl or some similar mechanism to stuff data into the input buffer of the terminal. See tty_ioctl(4)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.