Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Well, the manual page states:

The st_dev field describes the device on which this file resides. (The major(3) and minor(3) macros may be useful to decompose the device ID in this field.)

The st_rdev field describes the device that this file (inode) represents.

So the first one (st_dev) indicates on which device the actual inode you are inspecting resides, i.e. the disk that holds the /dev directory. This is typical meta information about the inode, like its size and so on.

The other (st_rdev) is used to map a driver to a device file, for e.g. /dev/ttyUSB0 or something. That's basically the content of a device special file.

For the second question, I'm not sure off-hand. You would need to cross-index it with the list of mounted devices, and then recursively scan the proper one, I guess.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top