First of all, a tty, even if it appears a single file object, is actually a pair of pipes/queues -- one for the output and one for the input, the other ends of each being connected either to the TX/RX of some hardware device, or made available to another program in the case of a pseudo-tty. When you're writing anything to the fd linked to a tty, you're writing into the output queue, even if that's the fd 0 (stdin) of your process, and will not be looped back into the input queue.
On most Unix systems (with the notable exception of OpenBSD), there's a special ioctl (TIOCSTI
) which allows you to insert a byte into the input queue of a tty, as if it were received from the other end. TIOCSTI
will only work as root when not used on the controlling tty of the process calling it.
$ cc -Wall tiocsti.c -o tiocsti
-- as root --
# printf '\04' | ./tiocsti >/proc/5460/fd/0
If your process is running in a pseudo-tty, another way to "fake" input is to connect with a debugger to the process holding the master side of the pseudo-tty, and write(2)
your stuff into it from there.
$ gdb -p pid_of_xterm
(gdb) p write(4, "\x04", 1) # 4 is the fd of the master pty
(gdb) c
Both methods are just overkill. If you want to control a running process, you better attach with a debugger to that very process instead of faking it some input; if you want to instrumentalize an interactive program, you better run it in expect
; both will work on any Unix system and don't require extra privileges for that.
Also notice that ^D
/ \004
will only work as EOF
if a) the tty is in canonical mode b) the c_cc[VEOF]
special character wasn't changed to something else c) the output queue is empty (otherwise c_cc[VEOF]
will have to be sent twice). And of course, it will be the process which is just reading from tty which will get the EOF or those bytes, not necessarily the shell.
tiocsti.c
#include <sys/ioctl.h>
#include <unistd.h>
#include <err.h>
int main(void){
char c; ssize_t r;
while((r = read(0, &c, 1)) > 0){
if(ioctl(1, TIOCSTI, &c)) err(1, "ioctl(TIOCSTI)");
}
if(r < 0) err(1, "read");
return 0;
}
stty -a
will dump the terminal settings for the current terminal. In the flags portion you will seeicanon
, the canonical flag. If this flag is preceded by an-
then it is off, no dash then its on. – 111--- Jan 10 '19 at 01:27TIOCSTI
ioctl
will nuke a terminal, not sure how that differs from what you're trying. for a shell like ZSH withignoreeof
set you may need to send a bunch of 4 with thatioctl
– thrig Jan 10 '19 at 03:08