[comp.unix.questions] Is there any way to find real tty under shell layers?

friedl@vsi.UUCP (Stephen J. Friedl) (03/06/88)

Hi,

     Does anybody know any way to find out the "real" terminal
name when running under shell layers in Sys V?  We use shl
here all the time, but we have software that keys on the real
port name as a session identifier and to identify things like
the type of the terminal on that physical port.  Once we create
a new layer we immediately su(1) to a new user (root or one of
our project ids) and we can't get the tty name.

     I know I could kludge something with having shl write
a little pid record to a file, then the children look for that
file and match it with their parent pid and go from there but
I would prefer a direct way if possible with ioctl(2) or something.

     Any suggestions?
-- 
Life : Stephen J. Friedl @ V-Systems, Inc./Santa Ana, CA   *Hi Mom*
CSNet: friedl%vsi.uucp@kent.edu  ARPA: friedl%vsi.uucp@uunet.uu.net
uucp : {kentvax, uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl

paul@whuts.UUCP (KOLB) (03/16/88)

In article <352@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
>      Does anybody know any way to find out the "real" terminal
> name when running under shell layers in Sys V?  

#include <sys/types.h>
#include <utmp.h>
#include <stdio.h>

main()
{
	struct utmp *utmp, *getutent();
	char *q;

	q = ttyname(0)+5;
	while ((utmp = getutent()) != NULL) {
		if ( (utmp->ut_type == USER_PROCESS) &&
			(strncmp(utmp->ut_line, q, strlen(q)) == 0) )
				printf("tty%d\n", atoi(utmp->ut_id));
	}	
}

This will work, iff your /etc/inittab is "right", 
ie. the id field corresponds to the tty name; in other words:

	220:2:respawn:/etc/getty -t 60 tty220 9600 
	^^^	corresponds to            ^^^

According to my utmp(4) man page, the ut_id is the "/etc/inittab
id (usually line #)". So I guess that is the convention. 

BTW, if you must edit your inittab ID field, do so in single user
mode only. Since init keeps its own internal table, if you modified
the inittab ID field in multi user mode, and did a "init q", 
init would have two entries for the same tty; one with the old id, 
one with the new id. Then all the programs (such as newgrp) that 
call getlogin, ttyslot .. etc wouldn't work. Has anyone seen
this, or is my init brain-damaged?

	Paul Ho

simon@its63b.ed.ac.uk (ECSC68 S Brown CS) (03/18/88)

In article <3932@whuts.UUCP> paul@whuts.UUCP (KOLB) writes:
>>In article <352@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
>>      Does anybody know any way to find out the "real" terminal
>> name when running under shell layers in Sys V?  
>
> [description of doing this using getutent() to search for a ut_line
>  that looks like ttyname(0)]
>

But shl stamps an entry for the current layer "/dev/sxtNNN" in /etc/utmp,
so this won't work. The only way I can think of is to look in /dev/kmem at
the sxt driver structures and find what real tty has your sxt (device number
got from fstat(0,...)) multiplexed onto it. It's hardly portable though (and
requires read-permission on /dev/kmem). It'd be *much* better if shl were to
store away the original tty name somewhere when it replaces it in utmp.

Of course, the real problem is that utmp is simply not good enough any more-
it was fine back in the Good Old Days, but when you have windows and ptys and
multiplexing and other good stuff like this, it loses badly.

--
--------------------------------------------------
| Simon Brown                                    |
| Laboratory for Foundations of Computer Science |
| Department of Computer Science                 |
| University of Edinburgh, Scotland, UK.         |
--------------------------------------------------
 UUCP:  ...!uunet!mcvax!ukc!lfcs!simon
 ARPA:  simon%lfcs.ed@nss.cs.ucl.ac.uk
 JANET: simon@uk.ac.ed.lfcs

-- 
--------------------------------------------------
| Simon Brown                                    |
| Laboratory for Foundations of Computer Science |
| Department of Computer Science                 |
| University of Edinburgh, Scotland, UK.         |
--------------------------------------------------
 UUCP:  ...!uunet!mcvax!ukc!lfcs!simon
 ARPA:  simon%lfcs.ed@nss.cs.ucl.ac.uk
 JANET: simon@uk.ac.ed.lfcs
-- 
--------------------------------------------------
| Simon Brown                                    |
| Laboratory for Foundations of Computer Science |
| Department of Computer Science                 |
| University of Edinburgh, Scotland, UK.         |
--------------------------------------------------
 UUCP:  uunet!mcvax!ukc!lfcs!simon
 ARPA:  simon%lfcs.ed@nss.cs.ucl.ac.uk      "Life's like that, you know"
 JANET: simon@uk.ac.ed.lfcs

friedl@vsi.UUCP (Stephen J. Friedl) (03/20/88)

In article <1098@its63b.ed.ac.uk>, simon@its63b.ed.ac.uk (ECSC68 S Brown CS) writes:
> In article <3932@whuts.UUCP> paul@whuts.UUCP (KOLB) writes:
> >>In article <352@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
> >> Does anybody know any way to find out the "real" terminal
> >> name when running under shell layers in Sys V?  
> >
> > [description of doing this using getutent() to search for a ut_line
> >  that looks like ttyname(0)]
> 
> But shl stamps an entry for the current layer "/dev/sxtNNN" in /etc/utmp,
> so this won't work. [...]

Paul's method works quite well but not in the rigorous manner you
probably assume.  Yes, you get the "/dev/sxtNNN" back from utmp
that matches the return from ttyname(), but then you look at
utmp->ut_id.  This field, which is defined in /etc/inittab, bears
a coincidental relationship to the real tty name: if the id is
"12" then we are on "tty12".  Yes, this relationship must be
maintained in /etc/inittab, and there are special cases ("co" is
console, "ct" is contty) but for practical use it works very
nicely (thanks again Paul!).
-- 
Steve Friedl  friedl@vsi.com
{uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl

paul@whuts.UUCP (KOLB) (03/20/88)

In article <1098@its63b.ed.ac.uk>, simon@its63b.ed.ac.uk (ECSC68 S Brown CS) writes:
> In article <3932@whuts.UUCP> paul@whuts.UUCP (KOLB) writes:
> >>In article <352@vsi.UUCP>, friedl@vsi.UUCP (Stephen J. Friedl) writes:
> >>      Does anybody know any way to find out the "real" terminal
> >> name when running under shell layers in Sys V?  
> >
> > [description of doing this using getutent() to search for a ut_line
> >  that looks like ttyname(0)]
> 
> But shl stamps an entry for the current layer "/dev/sxtNNN" in /etc/utmp,
> so this won't work.

Have you tried to run the program?  Here it is again:

#include <sys/types.h>
#include <utmp.h>
#include <stdio.h>

main()
{
	struct utmp *utmp, *getutent();
	char *q;

	q = ttyname(0)+5;
	while ((utmp = getutent()) != NULL) {
		if ( (utmp->ut_type == USER_PROCESS) &&
			(strncmp(utmp->ut_line, q, strlen(q)) == 0) )
				printf("tty%d\n", atoi(utmp->ut_id));
	}	
}

As I said in my earlier reply to Steve Friedl, inittab's ID field
(this ID field becomes utmp->ut_id) has to be corresponded to the 
tty number. Ie:

	123:2:respawn:/etc/getty -t 60 tty123
        ^^^     correspond to	          ^^^

> The only way I can think of is to look in /dev/kmem .... 

Another setgid to sys program!

> Of course, the real problem is that utmp is simply not good enough any more-
> it was fine back in the Good Old Days, but when you have windows and ptys and
> multiplexing and other good stuff like this, it loses badly.

I tried the program with shl and layers, and it works. The
Good Old Days are still here :)

	Paul Ho