Skip to content
Advertisement

sending input from file descriptor

I have a binary that I’m debugging remotely, one way for me to send input to that file is throw

echo "input" > /proc/pid/fd/0

when I do the above the input is been received by the binary but I can’t simulate a keypress, and that causes me to switch to that binary and press enter every time.

here is an example

#include <stdio.h>

int main () {
   char str[50];

   printf("Enter a string : ");
   gets(str);

   printf("You entered: %s", str);

   return(0);
}

when you run the above program and send the input using

echo "input" > /proc/pid/fd/0

the above program will receive the input but it will not press the enter key

so is there is a way to do it using the file descriptor of the binary or any quick way?

in my case keypress is just the enter key

edit:

echo -en 'inputr' > /proc/pid/fd/0

or

echo -en 'inputx0d' > /proc/pid/fd/0

won’t work because the terminal will think it’s the slave sending that to the output it will not be sent to slave’s stdin

Advertisement

Answer

Can you have your process read from a named pipe (mkfifo)? Then keyboard input is possible with cat > mypipe, but while that cat is running, you can also echo foo > mypipe, because a pipe can have multiple writers (I think.)

Otherwise you’re going to want something like expect to connect your program to a pseudo-terminal, with another program managing the master side.


echo "input" > /proc/pid/fd/0 the above program will receive the input

No, the output will show up in the terminal exactly as if you did echo "input" > /dev/pts/12 or whatever terminal device the process has open as its stdin (usually with the same read/write file descriptor for all 3 of stdin, stdout, and stderr, which is why it works to write to it).

For the program to be able to read() your input, you’d need to write to the other side of the pseudo-terminal, which is probably owned by ssh or a terminal emulator. It might be possible to write to one of the FDs of ssh or xterm or whatever.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement