LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Getting the name of the terminal for a given process (https://www.linuxquestions.org/questions/programming-9/getting-the-name-of-the-terminal-for-a-given-process-662846/)

Skurmedel 08-14-2008 04:16 PM

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?

Thanks in advance.

matthewg42 08-14-2008 04:48 PM

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. :)

Skurmedel 08-14-2008 05:14 PM

Quote:

Originally Posted by matthewg42 (Post 3247872)
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 :)

(And yeah, parsing the output of ps is no fun :))

ta0kira 08-14-2008 05:32 PM

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:
Code:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main()
{
        fprintf(stderr, "%s\n", ttyname(STDIN_FILENO));
        return 0;
}

ta0kira

edit:
Sorry, I think I misunderstood your question.

Skurmedel 08-15-2008 02:43 AM

Yes a little ;).

I'm trying to monitor other processes, so its their terminal name I want to access.

Anyway, thx for the reply

Skurmedel 08-15-2008 10:51 AM

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.


All times are GMT -5. The time now is 01:41 AM.