I'm tired of the "one paragraph only" limit in comments =)
If you start a shell sh
, and get the pid $pid
you can find the file descriptors as you describe. An example:
$ ls -l /proc/29201/fd
total 0
lrwx------ 1 eroen users 64 Mar 22 15:52 0 -> /dev/pts/2
lrwx------ 1 eroen users 64 Mar 22 15:52 1 -> /dev/pts/2
lrwx------ 1 eroen users 64 Mar 22 15:52 2 -> /dev/pts/2
lrwx------ 1 eroen users 64 Mar 22 15:52 255 -> /dev/pts/2
You will notice that 1
, 2
and 3
are all symlinks to the same tty (a chardev). In other words, the input to the process is read from the same device node as the outputs are written to.
When you attempt to write (in a different process) to the same tty (as either /proc/$pid/fd/0
or /dev/pts/?
you accomplish exactly the same thing as the process itself does when it writes data to it's output; the data shows up in the terminal window.
Actually changing where fd[0-2] point after starting a process is fairly complicated, but not impossible. Reptyr is a free open source application that modifies an existing process so it's fd[0-2] point to a different tty (as well as some other stuff). This is accomplished through the ptrace framework. The post also mentions other softwares that do the same thing, and that it can be done through gdb.
Depending on what you actually wanted to accomplish, you might find Reptyr or some other software does what you need. Otherwise, you can look at/copy/modify the source code and find out how they do the trick.
Addendum:
This contains a few illustrating diagrams, in particular the third schematic from the top.
Essentially equivalent to Ask a running bash (interactive) to run a command from outside (on SO).
– G-Man Says 'Reinstate Monica' – 2014-11-10T20:43:56.9601Did you send the 'Return' key, too? As that would be needed to end a command. – boretom – 2012-03-21T09:24:54.220
@boretom Of course I did. – Determinant – 2012-03-21T09:34:26.867
Yes, I see, interesting question. The input to bash seems to be redirected to the stdout. Try running a command like
top
and you'll see that the stdin is just add somewhere to the output. My guess is that since the shell is started as an interactive shell it won't accept commands from stdin. But that's pure speculation. – boretom – 2012-03-21T09:43:45.180While I'm no guru here, I think what you do is writing to the terminal emulator, and not to the process in question. IE you write to the right pipe junction, but it winds up in the wrong of the two pipes. – Eroen – 2012-03-21T09:47:40.727
@boretom try
ls -l /proc/PID/fd/
. 0, 1, 2 are all redirected to/dev/pts/TERM_NUMER
No matter whether it accepts commands from stdin, I'm sure of that fd0 is for input and I've already redirect the output of cat to it. – Determinant – 2012-03-22T12:58:32.550@Eroen sorry, I don't quite catch your words which wrote "winds up in the wrong of the two pipes". (My English is rather poor... XD) – Determinant – 2012-03-22T13:03:24.433
More likely, I explained poorly. Have a look here.
– Eroen – 2012-03-22T13:31:49.790/proc/$pid/fd/0
gives terminal (or pipe or whatever) input when a process reads from it. In this case it is a symlink to a tty. when the process reads from the tty (via../fd/0
) it gets what was inputed in the tty. When you write to the tty, via the same file, the data is printed in the tty, not sent as input to the process.@Eroen What's difference between typing in tty and redirecting stream to its stdin(fd0) ? – Determinant – 2012-03-22T14:19:07.647
When you type into a tty, the data can be read by a process from
/dev/pts/xx
. (The data is also usually echoed to screen, as part of the tty emulation). This is the common way to have user input to a process. – Eroen – 2012-03-22T14:39:32.403If you start a shell in a terminal, it's fd0 (and 1 and 2) is a symlink to a tty (the same one). When you write to a tty, the data is shown in that tty. This is the common way to have output to a user from a process. – Eroen – 2012-03-22T14:43:39.623