tty/pts 相关指令

http://unix.stackexchange.com/questions/136662/how-can-we-know-whos-at-the-other-end-of-a-pseudo-terminal-device

stty -F /dev/pts/2

ls /proc/29864/fd/* -l
strace -p 29864 |less
ls /proc/locks
ls /proc/locks -l
ls /proc/tty/drivers
cat /proc/tty/drivers
sudo fuser -v /dev/ptmx
do chkio /proc/29864/io /dev/pts/39
chkio /proc/29864/io /dev/pts/39
who
ps -t pts/39 --forest
man stty
stty -F /dev/pts/39
sudo fuser -v /dev/ptmx
ls /sys/class/tty/ptmx/subsystem/
ls /sys/class/tty/ptmx/subsystem/ -l
ls /sys/class/tty/ptmx/subsystem/ -l |grep pts
ls /sys/class/tty/ptmx/subsystem/
ls /sys/class/tty/ptmx/subsystem/
ls /sys/class/tty/ptmx/
ls /sys/class/tty/ptmx/dev
cat /sys/class/tty/ptmx/dev
sudo fuser -v /dev/ptmx
find / -name "pts/39"

 

How can we know who's at the other end of a pseudo-terminal device?

If I do a:

echo foo > /dev/pts/12

Some process will read that foo\n from its file descriptor to the master side.

Is there a way to find out what that(those) process(es) is(are)?

Or in other words, how could I find out which xterm/sshd/script/screen/tmux/expect/socat... is at the other end of /dev/pts/12?

lsof /dev/ptmx will tell me the processes that have file descriptors on the master side of any pty. A process itself can use ptsname() (TIOCGPTN ioctl) to find out the slave device based on its own fd to the master side, so I could use:

gdb --batch --pid "$the_pid" -ex "print ptsname($the_fd)"

for each of the pid/fd returned by lsof to build up that mapping, but is there a more direct, reliable and less intrusive way to get that information?

How can I figure out which pty's are from which qemu?

$ for i in `virsh list | awk '{print $2}' | egrep -v "^$|Name"`; do
     printf "%-14s:%s\n" $i $(virsh ttyconsole $i | grep -v "^$");
  done

cobbler       :/dev/pts/1
xwiki         :/dev/pts/3
fan           :/dev/pts/4
mercury       :/dev/pts/5
mungr         :/dev/pts/0
win2008R2-01  :/dev/pts/7

Incidentally those same VMs through an lsof command:
$ lsof|grep qemu|grep ptmx
qemu-kvm   3796      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   3895      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   3972      root   14u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm   4294      root   15u      CHR                5,2         0t0        993 /dev/ptmx
qemu-kvm  11897      root   14u      CHR                5,2         0t0        993 /dev/ptmx
posted @ 2016-04-23 22:44  lvmxh  阅读(419)  评论(0编辑  收藏  举报