possible replacement for dev_to_name
Craig Small <[email protected]> Sun, 17 Apr 2022 11:57:37 +1000
| Newsgroups | gmane.linux.procps.devel |
|---|---|
| Message-ID | <CALy8Cw504RC_Aya5mCEffEO+ZuLbx1j8ENAe5h_QrKxy5Z77+w@mail.gmail.com> |
Hi All,
I have been looking at part of procps that converts the tty device number
into a name. This code lives in proc/devname.c and is pretty old and sad.
I've worked out the majority of cases, now I'm looking for corner case. So
if you use strange TTYs this is for you.
My current idea is to:
Take the 7th field of /proc/<PID>/stat to get the tty device
Convert into major/minor
Match it off the second field of /proc/tty/drivers
The first two things we do already, its the matching (third step) that is
changing. The device ID is strange encoded across bits so I have a script
below that does the work.
What am I looking for is people to run the small script below matching
their tty device numbers and output with what currently ps does. If you
have the standard setup, then its probably ok, I'm looking for those wild
and wonderful terminal types out there.
An example:
$ cut -f 7 -d' ' /proc/$$/stat
34816
$ ~/ttyname.sh 34816
TTY Nr: 34816 is Maj: 136 Min: 0
Possible devices:
pty_slave /dev/pts 136 0-1048575 pty:slave
$ ps -h -o tty $$
pts/0
So for my shell, 34816 is the tty device, which converted to minor/major
136:0. Grepping 136 gives me /dev/pts which would mean this change remove
the /dev/ so its pts + "/" + "minor" or pts/0 The ps command confirms the
current output.
In case you're wondering, the fourth column is the minor device range,
which is useful for multiple matches.
---- The script ---
#!/bin/bash
if [ -z $1 ] || ! [[ $1 =~ ^[0-9]+$ ]] ; then
echo $0 '<tty number>'
exit 1
fi
tty_nr=$1
major=$(( ( tty_nr >> 8 ) & 0xfff ))
minor=$(( ( tty_nr & 0xff ) | ( ( tty_nr & 0xfff00000 ) > 12 ) ))
echo "TTY Nr: $tty_nr is Maj: $major Min: $minor"
echo
echo "Possible devices:"
egrep '/dev/[^[:space:]]+[[:space:]]+'$major'[[:space:]]' /proc/tty/drivers
--- The script ---
Thanks! If I get some odd ones I'll see if we can cater for them.
- Craig
--
Craig Small https://dropbear.xyz/ csmall at : dropbear.xyz
Debian GNU/Linux https://www.debian.org/
<http://www.debian.org/> csmall at : debian.org
GPG fingerprint: 5D2F B320 B825 D939 04D2 0519 3938 F96B DF50 FEA5