Skip to main content
Tweeted twitter.com/#!/StackUnix/status/555588270269411329
edited tags
Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k
Source Link
mikeserv
  • 59.4k
  • 10
  • 122
  • 242

How do I come by this pty and what can I do with it?

It seems that a simple shell redirect from /dev/ptmx gets me a new pseudo-terminal.

$ ls /dev/pts; ls /dev/pts </dev/ptmx
0  1  2  ptmx
0  1  2  3  ptmx

It disappears as soon as the process that owns the fd claim on /dev/ptmx quits, but it is simple enough to retain it:

$ PS1='<$(ls -1 "$@" /dev/pts/*|uniq -u)> $ ' \
  exec 3<>/dev/ptmx "$0" -si -- /dev/pts/*
</dev/pts/3> $ ls /dev/pts; exec 3>&-
0  1  2  3  ptmx
<> $ ls /dev/pts; exec 3<>/dev/ptmx
0  1  2  ptmx
</dev/pts/3> $ exit

So it looks like the simple open() on /dev/ptmx is enough to get a pseudo-terminal. I guess that would make the shell - (or the process for which it does the redirect) - the owner of the master side of the pty.

But how do I assign the slave side - or can I assign the slave side? And if I can - what can I do with it? Can I affect its read/write/flush timings w/ stty? Will a slave side process link through to my new pty when referencing /dev/tty?