I need to find the device file (/dev/tty*
or /dev/pts/*
) connected to the standard input of any process on my system. I want to implement something similar to the tty(1)
program but which works for any process. How do I do this? This is on Linux.
The closest I've gotten is to parse the /proc/pid/stat
file and read out the 6th column. This gives me a number that corresponds to the st_rdev
of the tty
file I'm interested in. I then have to run stat(2)
on all the /dev/tty*
and /dev/pts/*
files to get the st_rdev
numbers and use that to map back. This is the approach used in the psutil package.
Update: I've taken a step back to reword this question into what I'm looking for rather than an implementation detail.
On Linux, you may just do ls -L /proc/pid/fd/0
to get the tty attached with the stdin of the process with process id pid
.