Getting the name of the terminal for a given process
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Getting the name of the terminal for a given process
I'm working on a program (in C) that reads the stat-file in /proc/[number] directories. I have made good progress and I get a great deal of information about all the processes. However I recently discovered that my way of fetching the name of the terminal a process is attached to didn't work at all.
I naively though that the GLIBC function "ttyname(int filedesc)" worked on any file descriptor, of course it didn't, it will only give the terminal name for the own process. For example, if I feed it "0" (for STDIN), it will return "/dev/pts/6", if I'm running my program on said terminal.
When I read the stat-file, I get a value called "ttyNr" by the man pages, which describes it as the "id of the terminal". My problem is that I don't know what the hell I'm supposed to do with that value. I tried to feed it to ttyname, but as I said that didn't work at all. I want to get output similar to the "ps" program we are all so familiar with.
I'm currently thinking about the possibility of opening /proc/[number]/fd/0 with readlink or something like that. Would that give me anything useful? Or is there a much simpler way of getting the name of the terminal attached to a certain process?
What I would do to find out the "proper" way to do it is to look at the source code to ps. ps will print the tty if you use the appropriate switches, e.g.
Code:
ps axo pid,tty
You could of course execute ps and parse the output, but it would be better to simply look at how ps does it and use that method. This is one of the perks of using Free Software.
What I would do to find out the "proper" way to do it is to look at the source code to ps. ps will print the tty if you use the appropriate switches, e.g.
Code:
ps axo pid,tty
You could of course execute ps and parse the output, but it would be better to simply look at how ps does it and use that method. This is one of the perks of using Free Software.
That's probably a good idea that somehow bypassed me, I have it here right on my desktop in a tar.gz
Unfortunately there's no simple (internal) way to find out the name of the controlling terminal because ctermid always returns "/dev/tty" (in my experience.) This isn't a symlink to the "real" terminal, either.
Instead, you pretty much have to settle for determining the terminal name of standard input. This isn't really of use if you setsid your program but start it from a terminal because you will show the name of a terminal that the process is mutually out of control of.
If you want the real name of the controlling terminal, you're best off calling ps using system. Otherwise:
Well I actually seem to have answered my question in my question so to speak. If anyone is interested a possible way of doing it is presented below.
If one opens /proc/[number]/fd/0 with readlink(), one can get the name of the terminal it's STDIN is attached to. One needs to check the symlink with (l)stat however and make sure it isn't some weird pipe; in that case a process' terminal is unknown.
I've double checked with the output from ps and it looks like it's doing something similar. readlink may also report "/dev/null" or nothing at all which ps shows as "?" in the TTY column.
I'm at work now, but I'll look more into it in the evening, and also look if the method above is indeed ps uses.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.