LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2008, 04:16 PM   #1
Skurmedel
LQ Newbie
 
Registered: Aug 2008
Distribution: OpenSuSE/Debian
Posts: 5

Rep: Reputation: 0
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.
 
Old 08-14-2008, 04:48 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 08-14-2008, 05:14 PM   #3
Skurmedel
LQ Newbie
 
Registered: Aug 2008
Distribution: OpenSuSE/Debian
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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 )
 
Old 08-14-2008, 05:32 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.

Last edited by ta0kira; 08-14-2008 at 05:34 PM.
 
Old 08-15-2008, 02:43 AM   #5
Skurmedel
LQ Newbie
 
Registered: Aug 2008
Distribution: OpenSuSE/Debian
Posts: 5

Original Poster
Rep: Reputation: 0
Wink

Yes a little .

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

Anyway, thx for the reply
 
Old 08-15-2008, 10:51 AM   #6
Skurmedel
LQ Newbie
 
Registered: Aug 2008
Distribution: OpenSuSE/Debian
Posts: 5

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
proc, ps, stat, terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Killing a process through terminal. urbansaint Linux - Newbie 2 05-20-2008 10:56 PM
process in different virtual terminal krymsunmortis Linux - General 3 01-17-2008 11:10 PM
Terminal Process creation gmh04 Linux - General 0 01-25-2005 11:46 AM
Start up process in terminal vitaliiluchka Linux - General 1 10-18-2004 11:21 PM
Diassociating process from control terminal beginner16 Programming 0 01-18-2004 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration