LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-19-2010, 02:21 PM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Rep: Reputation: 271Reputation: 271Reputation: 271
how do i stuff a keystroke into another virtual terminal's stdin?


echo p > /dev/tty7

causes a 'p' to appear in tty7 but not the program running in 7 to react as though a 'p' had been pressed.
 
Old 10-19-2010, 02:37 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
What is the application or program that you're trying to "stuff a p" into? Can it be told to listen to a pipe for commands? If so, send the keystroke or message through the pipe into the program. Look up the `mkfifo` command for info on this.

Also look up the `write` command. There's another command too for sending messages to other tty's but I think they're intended for sending messages to humans using the terminals, not to programs running in the terminals.

Anyhow, if the pipe idea is no good or you still want more info, maybe tell us a bit more about the situation and requirements, i.e. the program(s) involved and desired functionality you're trying to accomplish, etc. More detail might give someone an idea.
 
Old 10-19-2010, 05:53 PM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Writing to console does not send keystrokes to standard input of running programs, only to the terminal. But it can be achieved by simple C program:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(void)
    {
    int hTTY = open("/dev/tty1", O_WRONLY|O_NONBLOCK);
    ioctl(hTTY, TIOCSTI, "p");

    close(hTTY);
    return 0;
    }
Name it 'tty_send.c' Compile by
Code:
gcc -o tty_send tty_send.c
And run 'tty_send'.

Last edited by eSelix; 10-19-2010 at 05:54 PM.
 
Old 10-19-2010, 07:32 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Oooo, looks cool. I just stumbled across this thread, and sounds interesting. Once I get home from work, I am going to try this out, and possibly extend the code a bit and have fun with it. If I do, I will gladly post what I have come up with =D
 
Old 10-20-2010, 01:43 PM   #5
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
eSelix's program returns an 'operation not permitted' error, even after I make /dev/tty7's permissions 777
 
Old 10-20-2010, 04:55 PM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
And other consoles works? Usually on the console tty7 is runnig xserver, this program will not work with such code. Also the program can work without connected console. For me, I must be root, to execute ioctl function, you may suid to root your program, but this is some insecure solution.

Anyway you can move this thread to Programming forum. Those people should give you better answers.
 
Old 10-20-2010, 07:14 PM   #7
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
I rewrote the program to take arguments for the target terminal and string to pass. It fails on any target terminal but the one in which it runs.
 
Old 10-20-2010, 09:26 PM   #8
alan99
Member
 
Registered: Mar 2010
Distribution: Debian
Posts: 180

Rep: Reputation: 31
A simple solution for what you what to do is a bad idea security wise, so the system probably won't allow it. You probably need to learn some form of IPC like corba or sending messages on dbus.
 
Old 10-21-2010, 01:29 PM   #9
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Making the owner root and setting the sticky bit works. It's a personal system so I don't mind. It would be nice to limit its effectiveness to processes owned by the caller.
 
Old 10-22-2010, 12:16 AM   #10
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
The code doesn't work for me either. I don't get any error message or such, but when I try to do the terminal injection, nothing happens at all.
 
Old 10-22-2010, 08:42 AM   #11
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
This is only sample. You need add to this code error checking routines, etc. You must run it as root to work, due ioctl function (maybe there are other methods).
 
Old 10-31-2010, 03:53 PM   #12
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,967

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Quote:
Originally Posted by corp769 View Post
The code doesn't work for me either. I don't get any error message or such, but when I try to do the terminal injection, nothing happens at all.
It works for me if I run it as root. I made root the owner and set the sticky bit so anyone can use it. This is a huge security hole but I'm the only user of my computer. I added reading input for a string to pass and a target terminal and report the outcome of the calls. It works as non-root to the terminal in which I run it. Tell us what your error is.
 
Old 01-19-2011, 03:48 PM   #13
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Sorry for digging this up, but I did not even realize that you replied back man. My bad... I will post what happens tonight once I get home from work. I know for sure though that it was something to do with a ioctl function or error....
 
Old 10-14-2014, 02:27 PM   #14
jeff82
LQ Newbie
 
Registered: Nov 2013
Posts: 4

Rep: Reputation: Disabled
Thumbs up TTY/PTS send

This was one of the most awesome threads. Thanks to whoever still gets this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I customize my terminal's title bar? homer_3 Linux - General 5 11-18-2009 10:58 AM
[SOLVED] New to this virtual stuff MBA Whore Linux - Software 3 05-20-2009 06:09 PM
Anybody have the Linux Terminal's coding... in any language tr_boyz Linux - Newbie 4 04-21-2008 02:40 AM
saving Linux terminal's contents?? muby Linux - Newbie 2 12-05-2007 12:09 AM
How to set the virtual terminal's refresh rate? ForumJoiner Linux - Newbie 1 12-01-2006 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration