#include #include #include #include #include #include #include #ifndef ECHO2TTY_DEBUG # define ECHO2TTY_DEBUG 0 #endif int main (int argc, char *argv[]) { int fd; if (argc != 2) { fprintf(stderr, "Usage: echo 'XYZ' | echo2tty /dev/ttyXXX\n"); exit(1); } fd = open(argv[1], O_RDWR); if (fd < 0) { perror("open DEVICE"); exit(1); } { char c; char* cp = &c; #if ECHO2TTY_DEBUG int i = 0; #endif while (read(0, cp, 1) == 1) { int r; /* NB! * The iosctl(fd, TIOCSTI, ...) works for our own terminal fd, * but FAILS for any other tty fd with "Operation not permitted" * or "Bad address", even for ttys owned by the same user * ("echo 'xxx' > '/dev/pts/2'" sends the data quite successfully * although shell on that tty ignores the received data). */ /* They say ioctl(TIOCSTI) required root privileges * (or CAP_SYS_ADMIN to be more specific, * but that's usually the same in practice) * BTW, the capabilities(7) man page does NOT mention this fact! */ r = ioctl(fd, TIOCSTI, (const char*)cp); #if ECHO2TTY_DEBUG fprintf(stderr, "ioctl(0x%02x): %i\n", c, r); i++; #endif if (r < 0) perror("ioctl"); } #if ECHO2TTY_DEBUG printf("%s: %i\n", argv[0], i); #endif } close(fd); }