Unix Programming - How to determine what process has a controlling terminal...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2007 > How to determine what process has a controlling terminal...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author How to determine what process has a controlling terminal...
K-mart Cashier

2007-01-18, 1:32 am

I have the following users on my who list:
kye p0 85.132.8.116 Tue06AM 13:11 -bash (bash)
casper p2 frau.triohost.co Thu08PM 43 less -R -E -X
-Q -r -d
cdalten p4 eecs.berkeley.edu 8:33PM - w
chocnut p5 pc8008.temp.co.l 7:06PM 6
/usr/local/bin/real/ly
najib p6 magnifix.com.my 1:38AM 18:06 mutt
marcotap p7 dsl-189-133-6-33 8:16PM 17 -bash (bash)


The problem is that some of them have a controlling terminal (ie their
terminal is written to utmp) and some don't (ie there is something like
/ftp/45678 instead of /dev/ttyp4 for example). What would be the
easiest way to go down the list and determine who has a controlling
terminal and who doesn't?

The only thing I can think of is looping through utmp and seeing if
that user has a terminal number written to utmp. If they do, then have
it print back they have a controlling terminal. If not, then have it
print back they don't have a controlling terminal.

Ideas?

Chad

Stephane CHAZELAS

2007-01-18, 7:28 am

2007-01-17, 17:41(-08), K-mart Cashier:
> I have the following users on my who list:
> kye p0 85.132.8.116 Tue06AM 13:11 -bash (bash)
> casper p2 frau.triohost.co Thu08PM 43 less -R -E -X
> -Q -r -d
> cdalten p4 eecs.berkeley.edu 8:33PM - w
> chocnut p5 pc8008.temp.co.l 7:06PM 6
> /usr/local/bin/real/ly
> najib p6 magnifix.com.my 1:38AM 18:06 mutt
> marcotap p7 dsl-189-133-6-33 8:16PM 17 -bash (bash)
>
>
> The problem is that some of them have a controlling terminal (ie their
> terminal is written to utmp)


Controlling terminals are not related to utmp. A controlling
terminal is the terminal that sends you a SIGINT when the user
types CTRL-C and you are in its foreground process group or what
sends you a SIGTTIN when you try to read from it and are not in
its foreground process group.

You can know it with:

tty_of() { # arg: <pid>
if tty=$(ps -p "$1" -o tty=); then
case $tty in
("" | "?") return 1;;
(*) printf '/dev/%s\n' "$tty";;
esac
return 0
fi
return 1
}


--
Stéphane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com